%%
%% Copyright (C) 2022 by Jinwen XU
%% ------------------------------------
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%%
\NeedsTeXFormat{LaTeX2e}[2020-10-01]
\RequirePackage{l3keys2e}
\ProvidesExplPackage
  {multifootnote}
  {2022/05/31} {}
  {Multiple numbers for the same footnote}

\msg_new:nnn { multifootnote }
  { non-compatible-package }
  { "multifootnote" ~ is ~ not ~ compatible ~ with ~ the ~ package ~ "#1". }

\hook_gput_code:nnn { begindocument/before } { multifootnote }
  {
    \@ifpackageloaded { footnotebackref }
      {
        \msg_warning:nnn { multifootnote } { non-compatible-package } { footnotebackref }
      } {}
  }

\dim_new:N \l_multifootnote_space_after_comma_dim
\dim_set:Nn \l_multifootnote_space_after_comma_dim { 0.2ex }

\bool_new:N  \l__multifootnote_left_indent_bool
\bool_set_false:N  \l__multifootnote_left_indent_bool
\dim_new:N \l__multifootnote_left_indent_dim

\keys_define:nn { multifootnote }
  {
    , left-align    .bool_set:N = \l__multifootnote_left_align_bool
    , left-align    .initial:n  = { false }
    , left~align    .bool_set:N = \l__multifootnote_left_align_bool
    , left align    .bool_set:N = \l__multifootnote_left_align_bool
    , left-indent   .code:n     = {
                                    \bool_set_true:N \l__multifootnote_left_indent_bool
                                    \dim_set:Nn \l__multifootnote_left_indent_dim { #1 }
                                  }
    , left-indent   .default:n  = { 1.5em }
    , left~indent   .code:n     = {
                                    \bool_set_true:N \l__multifootnote_left_indent_bool
                                    \dim_set:Nn \l__multifootnote_left_indent_dim { #1 }
                                  }
    , left~indent   .default:n  = { 1.5em }
    , left indent   .code:n     = {
                                    \bool_set_true:N \l__multifootnote_left_indent_bool
                                    \dim_set:Nn \l__multifootnote_left_indent_dim { #1 }
                                  }
    , left indent   .default:n  = { 1.5em }
    , unknown       .code:n     = {}
  }
\ProcessKeysOptions { multifootnote }

\cs_new_protected:Nn \multifootnote_make_footnote_left_align:n
  {
    \renewcommand { \@makefntext } [1]
      {
        \skip_horizontal:n { #1 }
        \tl_if_blank:eF { \text_expand:n { \@thefnmark } }
          {
            \@makefnmark
            \nobreakspace
          }
        ##1
      }
  }

\bool_if:NT \l__multifootnote_left_align_bool
  {
    \multifootnote_make_footnote_left_align:n { 0pt }
  }
\bool_if:NT \l__multifootnote_left_indent_bool
  {
    \multifootnote_make_footnote_left_align:n { \l__multifootnote_left_indent_dim }
  }

% First approach
\NewDocumentCommand \multifootnotemark { O{} }
  {
    \footnotemark
    \group_begin:
      \tl_if_blank:nF { #1 }
        {
          \addtocounter { footnote } { -1 }
          \refstepcounter { footnote }
          \@ifpackageloaded { hyperref }
            {
              \cs_gset_eq:NN \@currentHref \Hy@footnote@currentHref
            } {}
          \label { #1 }
        }
    \group_end:
  }
\NewCommandCopy \footnotenumber \multifootnotemark

\NewDocumentCommand \multifootnotetext { O{} m }
  {
    \group_begin:
      \clist_clear:N \l_tmpa_clist
      \clist_clear:N \l_tmpb_clist
      \tl_if_blank:nF { #1 }
        {
          \clist_map_inline:nn { #1 }
            {
              \@ifpackageloaded { hyperref }
                {
                  \clist_put_right:Nn \l_tmpa_clist
                    { \ref*{ ##1 } }
                  \clist_put_right:Nn \l_tmpb_clist
                    { \Hy@raisedlink { \hypertarget { \getrefbykeydefault{##1}{anchor}{Doc-Start} } {} } }
                }
                {
                  \clist_put_right:Nn \l_tmpa_clist
                    { \ref { ##1 } }
                }
            }
          \def \thefootnote { \clist_use:Nn \l_tmpa_clist { , \skip_horizontal:n { \l_multifootnote_space_after_comma_dim } } }
        }
      \@ifpackageloaded { hyperref }
        {
          \xdef \Hy@footnote@currentHref { x\Hy@footnote@currentHref }
        } {}
      \footnotetext { \clist_use:Nn \l_tmpb_clist {} \ignorespaces #2 }
    \group_end:
  }

% Second approach
\NewDocumentCommand \multifootnotetag { m }
  {
    \footnotemark
    \group_begin:
      \def \thefootnote {}
      \footnotetext { \vspace { -\baselineskip } }
    \group_end:
    \clist_map_inline:nn { #1 }
      {
        \cs_if_exist:cTF { multifootnote-tag- ##1 }
          {
            \tl_gput_right:cx { multifootnote-tag- ##1 } { , \skip_horizontal:n { \l_multifootnote_space_after_comma_dim } \thefootnote }
          }
          {
            \cs_gset:cpx { multifootnote-tag- ##1 } { \thefootnote }
          }
      }
  }
\NewCommandCopy \footnotetag \multifootnotetag

\NewDocumentCommand \multifootnotetagtext { O{} m }
  {
    \group_begin:
      \tl_if_blank:nTF { #1 }
        {
          \def \thefootnote { }
        }
        {
          \def \thefootnote { \use:c { multifootnote-tag- #1 } }
        }
      \footnotetext { #2 }
    \group_end:
  }

% The combined interface for producing footnote text
\NewDocumentCommand \multifootnote { O{} m }
  {
    \str_if_in:nnTF { #1 } { , }
      {
        \multifootnotetext [ #1 ] { #2 }
      }
      {
        \multifootnotetagtext [ #1 ] { #2 }
      }
  }

\endinput
%%
%% End of file `multifootnote.sty'.
