% \iffalse meta-comment
%
% Copyright (C) 2005-2014 by Ulrich M. Schwarz
% Copyright (C) 2019      by Frank Mittelbach
% Copyright (C) 2020-     by Yukai Chou
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c.
% The license can be obtained from
% http://www.latex-project.org/lppl/lppl-1-3c.txt
%
%<*code>
% \fi
%    |\@counteralias{#1}{#2}| makes |#1| a counter that uses |#2|'s count register.
%    This is useful for things like \pkg{hyperref}'s |\autoref|, which otherwise
%    can't distinguish theorems and definitions if they share a counter.
%
%    For detailed information, see Die TeXnische Kom\"odie 3/2006.
%    \StopEventually{}
%    \begin{macro}{\@addtoreset}
%    add |\@elt{#1}| to |\cl@#2|.
%    This differs from the kernel implementation insofar as we trail the
%    cl lists until we find one that is empty or starts with |\@elt|.
%    \begin{macrocode}
\def\aliasctr@f@llow#1#2\@nil#3{%
  \ifx#1\@elt
  \noexpand #3%
  \else
  \expandafter\aliasctr@f@llow#1\@elt\@nil{#1}%
  \fi
}
%    \end{macrocode}
%    
%    \begin{macrocode}
\newcommand\aliasctr@follow[1]{%
  \expandafter\aliasctr@f@llow
%    \end{macrocode}
%    Don't be confused: the third parameter is ignored here, we always
%    have recursion here since the \emph{token} |\cl@#1| is (hopefully) not |\@elt|.
%    \begin{macrocode}
  \csname cl@#1\endcsname\@elt\@nil{\csname cl@#1\endcsname}%
}
%    \end{macrocode}
%    
%    \begin{macrocode}
\renewcommand*\@addtoreset[2]{\bgroup
   \edef\aliasctr@@truelist{\aliasctr@follow{#2}}%
  \let\@elt\relax
  \expandafter\@cons\aliasctr@@truelist{{#1}}%
\egroup}
%    \end{macrocode}
%    
%    This code has been adapted from David Carlisle's \pkg{remreset}. We
%    load that here only to prevent it from being loaded again.
%    \begin{macrocode}
% FMi 2019-07-31 \@removereset is in the kernel these days
\@ifundefined{@removefromreset}{\RequirePackage{remreset}}{}
\renewcommand*\@removefromreset[2]{\bgroup
  \edef\aliasctr@@truelist{\aliasctr@follow{#2}}%
  \expandafter\let\csname c@#1\endcsname\@removefromreset
  \def\@elt##1{%
    \expandafter\ifx\csname c@##1\endcsname\@removefromreset
    \else
      \noexpand\@elt{##1}%
    \fi}%
  \expandafter\xdef\aliasctr@@truelist{%
    \aliasctr@@truelist}
\egroup}
%    \end{macrocode}
%    \end{macro}
%    
%    \begin{macro}{\@counteralias}
%    make |#1| a counter that uses counter |#2|'s count register.
%    \begin{macrocode}
\newcommand\@counteralias[2]{{%
    \def\@@gletover##1##2{%
      \expandafter\global
      \expandafter\let\csname ##1\expandafter\endcsname
      \csname ##2\endcsname
    }%
    \@ifundefined{c@#2}{\@nocounterr{#2}}{%
      \expandafter\@ifdefinable\csname c@#1\endcsname{%
%    \end{macrocode}
%    Four values make a counter foo:
%    \begin{itemize}
%    \item the count register accessed through |\c@foo|,
%    \item the output macro |\thefoo|,
%    \item the prefix macro |\p@foo|,
%    \item the reset list |\cl@foo|.
%    \end{itemize}
%    \pkg{hyperref} adds |\theHfoo| in particular.
%    \begin{macrocode}
        \@@gletover{c@#1}{c@#2}%
        \@@gletover{the#1}{the#2}%
%    \end{macrocode}
%    I don't see |\@counteralias| being called hundreds of times,
%    let's just unconditionally create  |\theHctr|-macros for \pkg{hyperref}.
%    \begin{macrocode}
        \@@gletover{theH#1}{theH#2}%
%    \end{macrocode}
%    YkC: Compatibility with \pkg{cleveref}, copied from \pkg{cleveref}'s 
%    support for \pkg{aliascnt}. 
%    Here |\cref@resetby| requires its first argument to be the actual counter
%    name, not a macro storing the name. Thanks to Willie Wong.
%    \begin{macrocode}
        \@ifpackageloaded{cleveref}{%
          \edef\aliasctr@temp{%
            \noexpand\cref@resetby{#2}{\noexpand\cref@result}}%
          \aliasctr@temp
          \ifx\cref@result\relax\else%
            \cref@addtoreset{#1}{\cref@result}%
          \fi
        }{}%
        \@@gletover{p@#1}{p@#2}%
        \expandafter\global
        \expandafter\def\csname cl@#1\expandafter\endcsname
        \expandafter{\csname cl@#2\endcsname}%
%    \end{macrocode}
%    It is not necessary to save the value again: since we share a
%    count register, we will pick up the restored value of the
%    original counter.
%    \begin{macrocode}
        %\@addtoreset{#1}{@ckpt}%
      }%
    }%
}}
%    \end{macrocode}
%    \end{macro}
%\iffalse
%</code>
%\fi