%% l3ctr2e LaTeX2e counters in LaTeX3 ways
%%
%% Copyright (C) 2012-2013 by Simon Sigurdhsson <sigurdhsson@gmail.com>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% 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
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Simon Sigurdhsson.
%% 
%% This work consists of the file l3ctr2e.tex and the  derived file
%% l3ctr2e.sty.
\documentclass{skdoc}
\usepackage{hologo,booktabs,varioref,cleveref,hyperref}

\ExplSyntaxOn
\cs_set_protected_nopar:Npn\ExplHack{
    \char_set_catcode_letter:n{ 58 }
    \char_set_catcode_letter:n{ 95 }
}
\ExplSyntaxOff
\ExplHack

% Declare the target files
\SelfPreambleTo{\mypreamble}
\DeclareFile[key=package,preamble=\mypreamble]{l3ctr2e.sty}

% This is where the documentation begins
\begin{document}
    % Change & version info
    \version{0.1d}
    \changes{0.1}{Initial version}
    \changes{0.1c}{Moved attachment of global parent from \cs{__ctr_define:n} to \cs{ctr_new:n}}

    % Metadata
    \package[ctan=l3ctr2e,vcs=https://github.com/urdh/l3ctr2e]{l3ctr2e}
    \title{The \textbf{\thepackage} package}
    \author{Simon Sigurdhsson}
    \email{sigurdhsson@gmail.com}

    % First page
    \maketitle
    \begin{abstract}
        A reimplementation of some of the \LaTeXe\ counter functionality
        in \LaTeX3 code. It should be backwards-compatible with \LaTeXe\ 
        counters.
    \end{abstract}
    \tableofcontents

    \section{Documentation}
    This package is intended to complement the \LaTeXe\ counter macros
    with \LaTeX3 counterparts. The new macros use the same internal
    counters and macros as the \LaTeXe\ ones, and as such they should be
    fully backwards-compatible. The package is not terribly complicated
    but could be useful in adapting (or rewriting) \LaTeXe\ packages or
    document styles to \LaTeX3 standards.

    \Warning{The package is not backwards-compatible as it is now: the
    resetting of child counters is not implemented in a backwards-%
    compatible way.}
    
    \subsection{Options}
    \Option{replace}\WithValues{true,false}\AndDefault{false}
    There is only one option, \opt{replace}, which will make the package
    replace the original \LaTeXe\ public API macros with new, \LaTeX3-%
    based ones (with the same API). Its default value is \texttt{false}.

    \subsection{Public API}
    The public \LaTeX3 API has macros corresponding to the macros of the
    \LaTeXe\ API according to \vref{tab:macros}. In addition to the
    direct replacements there are also a number of new macros which are
    missing in the original \LaTeXe\ API.

    \begin{table}[tbp]
        \centering
        \caption{\LaTeX3 counterparts of the \LaTeXe\ counter macros}
        \label{tab:macros}
        \begin{tabular}{ll}
            \toprule
            \textbf{\LaTeXe} & \textbf{\LaTeX3} \\
            \midrule
            \cs*{newcounter} & \cs*{ctr_new:n} \\
            \cs*{stepcounter} & \cs*{ctr_gincr:n} \\
            \cs*{refstepcounter} & \cs*{ctr_ref_gincr:n} \\
            \cs*{addtocounter} & \cs*{ctr_gadd:nn} \\
            \cs*{setcounter} & \cs*{ctr_gset:nn} \\
            \cs*{value} & \cs*{ctr_use:n} \\
            \cs*{arabic} & \cs*{ctr_use_arabic:n} \\
            \cs*{alph} & \cs*{ctr_use_alph:n} \\
            \cs*{Alph} & \cs*{ctr_use_Alph:n} \\
            \cs*{roman} & \cs*{ctr_use_roman:n} \\
            \cs*{Roman} & \cs*{ctr_use_Roman:n} \\
            \cs*{fnsymbol} & \cs*{ctr_use_fn:n} \\
            \bottomrule
        \end{tabular}
    \end{table}

    \subsubsection{Creating counters}
    \DescribeMacro\ctr_new:n{<counter name>}
    \DescribeMacro\ctr_new:nn{<counter name>}{<parent counter>}
    This macro creates a new counter, and is the direct counterpart of the
    \cs{newcounter} \LaTeXe\ macro. It has two variants, one of which also
    sets a parent counter (\emph{i.e.} a counter which will reset the new
    counter when incremented), corresponding to using \cs{newcounter} with
    an optional argument.

    \subsubsection{Setting counters}
    \DescribeMacro\ctr_gzero:n{<counter name>}
    This macro has no \LaTeXe\ counterpart, but could be useful (and is
    used internally by other \thepkg\ macros). It sets the counter value
    to zero.

    \DescribeMacro\ctr_gincr:n{<counter name>}
    This macro increments the counter by one, and is the \LaTeX3 
    equivalent of \cs{stepcounter}.

    \DescribeMacro\ctr_ref_gincr:n{<counter name>}
    In addition to incementing the counter by one, this macro sets the
    internal \LaTeXe\ macro \cs{@currentlabel}, so that references and
    labels work properly. It is the \LaTeX3 variant of
    \cs{refstepcounter}.

    \DescribeMacro\ctr_gadd:nn{<counter name>}{<value>}
    The \LaTeX3 counterpart of \cs{addtocounter}. Simply adds \meta{value}
    to the counter.

    \DescribeMacro\ctr_gset:nn{<counter name>}{<new value>}
    Corresponds to \cs{setcounter}. Sets the counter to \meta{new value}.

    \subsubsection{Displaying counters}
    \LaTeXe\ provides a couple of different ways to express counters in
    typeset text. These macros correspond to (and extend) the repertoire
    of counter value macros provided by \LaTeXe\ using functionality from
    the \pkg{l3int} package.

    \DescribeMacro\ctr_use:n{<counter name>}
    This is simply \cs{value}. It will simply use the value of the counter
    (using \cs{int_use:c}) and as such could be used in a \pkg{l3int}
    context whenever an integer value of the counter is needed.

    \DescribeMacro\ctr_use_arabic:n{<counter name>}
    This macro typesets the counter value as arabic numbers, and
    corresponds to the \cs{arabic} macro.
    \DescribeMacro\ctr_use_alph:n{<counter name>}
    \DescribeMacro\ctr_use_Alph:n{<counter name>}
    Typesets the counter value as lower-case or upper-case alphabetic 
    characters --- the corresponding \LaTeXe\ macros are \cs{alph} and
    \cs{Alph}.
    \DescribeMacro\ctr_use_roman:n{<counter name>}
    \DescribeMacro\ctr_use_Roman:n{<counter name>}
    Typesets the counter value as lower-case or upper-case roman numerals.
    Corresponds to \cs{roman} and \cs{Roman}.
    \DescribeMacro\ctr_use_fn:n{<counter name>}
    Typesets the counter value as footnote symbols, and is intended for
    use in footnote marks only. Corresponds to \cs{fnsymbol}.
    \DescribeMacro\ctr_use_binary:n{<counter name>}
    \DescribeMacro\ctr_use_octal:n{<counter name>}
    \DescribeMacro\ctr_use_hex:n{<counter name>}
    Typesets the counter value as binary, octal or hexadecimal numbers.
    These macros have no \LaTeXe\ counterparts.
    \DescribeMacro\ctr_use_custom:nn{<counter name>}{<item>,\meta{item},...}
    Typesets the counter value according to the comma-separated list
    provided (containing a list of items). Values larger than the number
    of items in the list will cause an error.

    \section{Known issues}
    A list of current issues is available in the Github repository of this
    package\footnote{\url{https://github.com/urdh/l3ctr2e/issues}}, but as
    of the release of \theversion, there are no known issues.
    %\begin{description}
    %    \item[\#6]  ???
    %\end{description}
    
    If you discover any bugs in this package, please report them to the issue
    tracker in the \thepackage\ Github repository.

    \Implementation
    \section{Implementation}
    Start by including \pkg{expl3} and well as declaring the class.
\begin{MacroCode}{package}
\RequirePackage{expl3,l3keys2e,xparse}
\ProvidesExplClass{l3ctr2e}%
    {2013/05/25}{0.1d}{LaTeX2e counters in LaTeX3 ways}
\end{MacroCode}

    \subsection{Options}
\begin{MacroCode}{package}
\keys_define:nn{l3ctr2e}{
\end{MacroCode}
    \begin{option}{replace}
\begin{MacroCode}{package}
    replace .bool_set:N = \g__ctr_replace_latexe_bool,
    replace .default:n = true,
    replace .initial:n = false
\end{MacroCode}
    \end{option}
\begin{MacroCode}{package}
}
\ProcessKeysPackageOptions{l3ctr2e}
\end{MacroCode}
    
    \subsection{Messages}
    One message is declared for future use.
\begin{MacroCode}{package}
\msg_new:nnn{l3ctr2e}{no-counter}{No~counter~`#1'~defined!}
\end{MacroCode}

    \subsection{Private macros}
    \begin{macro}{\__ctr_exists:nT}[1]
        {Counter name}
\begin{MacroCode}{package}
\prg_new_conditional:Npnn\__ctr_exists:n#1{T}{
    \int_if_exist:cTF{c@#1}{
        \prg_return_true:
    }{
        \msg_error:nnn{l3ctr2e}{no-counter}{#1}
        \prg_return_false:
    }
}
\end{MacroCode}
    \end{macro}
    \begin{macro*}{\g__ctr_children__ctr_global_tl}
\begin{MacroCode}{package}
\tl_new:N\g__ctr_children__ctr_global_tl
\end{MacroCode}
    \end{macro*}
    \begin{macro}{\__ctr_define:n}[1]
        {Counter name}
    This does not yet fully correspond to \cs{@definecounter}.
\begin{MacroCode}{package}
\cs_new:Npn\__ctr_define:n#1{
    \int_new:c{c@#1} % actually \newcount, incorrect! FIX!
    \ctr_gzero:n{#1}
    \tl_new:c{g__ctr_children_#1_tl}
    \cs_gset:cpn{cl@#1}{}
    \cs_gset:cpn{p@#1}{}
    \cs_gset:cpn{the#1}{\ctr_use_arabic:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\__ctr_add_child:nn}[2]
        {Parent counter name}
        {Counter name}
    This probably fully corresponds to \cs{@addtoreset}.
\begin{MacroCode}{package}
\cs_new:Npn\__ctr_add_child:nn#1#2{
    \tl_gput_right:cn{g__ctr_children_#1_tl}{{#2}}
}
\end{MacroCode}
    \end{macro}

    \subsection{Public API}
    \subsubsection{Creating counters}
    \begin{macro}{\ctr_new:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_new:n#1{
    \__ctr_define:n{#1}
    \__ctr_add_child:nn{_ctr_global}{#1}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_new:nn}[2]
        {Counter name}
        {Parent counter}
    \changes{0.1c}{Changed \cs{ctr_new:n} into \cs{__ctr_define:n}}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_new:nn#1#2{
    \__ctr_define:n{#1}
    \__ctr_add_child:nn{#2}{#1}
}
\end{MacroCode}
    \end{macro}

    \subsubsection{Setting counters}
    \begin{macro}{\ctr_gzero:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gzero:n#1{
    \__ctr_exists:nT{#1}{\int_gzero:c{c@#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_gset:nn}[2]
        {Counter name}
        {New value}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gset:nn#1#2{
    \__ctr_exists:nT{#1}{\int_gset:cn{c@#1}{#2}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_gadd:nn}[2]
        {Counter name}
        {Value to add}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gadd:nn#1#2{
    \__ctr_exists:nT{#1}{\int_gadd:cn{c@#1}{#2}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_gincr:n}[1]
        {Counter name}
    \changes{0.1c}{Now also resets grandchildren and so on}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gincr:n#1{
    \ctr_gadd:nn{#1}{\c_one}
    \tl_if_exist:cT{g__ctr_children_#1_tl}{
        \tl_map_inline:cn{g__ctr_children_#1_tl}{
            \ctr_gincr:n{##1} % To reset children of the child
            \ctr_gzero:n{##1}
        }
    }
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_ref_gincr:n}[1]
        {Counter name}
    \changes{0.1a}{Fixed erroneous use of non-existent \cs{ctr_gincr:c}}
    \changes{0.1c}{Redeclaration of \cs{@currentlabel} is now local and unprotected}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_ref_gincr:n#1{
    \ctr_gincr:n{#1}
    \cs_set:Npx\@currentlabel{
        \use:c{p@#1}
        \use:c{the#1}
    }
}
\end{MacroCode}
    \end{macro}

    \subsubsection{Displaying counters}
    \begin{macro}{\ctr_value:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_value:n#1{
    \use:c{c@#1}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use:n#1{
    \int_use:c{c@#1}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_arabic:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_arabic:n#1{
    \int_to_arabic:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_alph:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_alph:n#1{
    \int_to_alph:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_Alph:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_Alph:n#1{
    \int_to_Alph:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_binary:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_binary:n#1{
    \int_to_binary:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_hex:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_hex:n#1{
    \int_to_hexadecimal:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_octal:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_octal:n#1{
    \int_to_octal:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_roman:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_roman:n#1{
    \int_to_roman:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_Roman:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_Roman:n#1{
    \int_to_Roman:n{\ctr_use:n{#1}}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_fn:n}[1]
        {Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_fn:n#1{
    \ensuremath{\ctr_use_custom:nn{#1}{
        *,           \dagger,       \ddagger,
        \mathsection,\mathparagraph,\|,
        **,          \dagger\dagger,\ddagger\ddagger
    }}
}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\ctr_use_custom:nn}[2]
        {Counter name}
        {Comma-separated list of items}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_custom:nn#1#2{
    \clist_set:Nn\l_tmpa_clist{#2}
    \tl_clear:N\l_tmpa_tl
    \int_zero:N\l_tmpa_int
    \clist_map_inline:Nn\l_tmpa_clist{
            \int_incr:N\l_tmpa_int
            \tl_put_right:Nx\l_tmpa_tl{{\int_use:N\l_tmpa_int}{##1}}
    }
    \exp_args:Nnnx\int_to_symbols:nnn{\ctr_use:n{#1}}
        {\clist_count:N\l_tmpa_clist}
        {\tl_use:N\l_tmpa_tl}
}
\end{MacroCode}
    \end{macro}

    \subsection{Replacing the \LaTeXe\ API}
\begin{MacroCode}{package}
\bool_if:NT\g__ctr_replace_latexe_bool{
\end{MacroCode}
    \begin{macro}{\newcounter}[2]
        {Counter name}
        {Parent counter}
\begin{MacroCode}{package}
    \RenewDocumentCommand\newcounter{mo}{
        \IfNoValueTF{#2}{\ctr_new:n{#1}}
                        {\ctr_new:nn{#1}{#2}}
    }
\end{MacroCode}
    \end{macro}
    \begin{macro}{\stepcounter}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\stepcounter{m}{\ctr_gincr:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\refstepcounter}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\refstepcounter{m}{\ctr_ref_gincr:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\addtocounter}[2]
        {Counter name}
        {Value to add}
\begin{MacroCode}{package}
    \RenewDocumentCommand\addtocounter{mm}{\ctr_gadd:nn{#1}{#2}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\setcounter}[2]
        {Counter name}
        {New value}
\begin{MacroCode}{package}
    \RenewDocumentCommand\setcounter{mm}{\ctr_gset:nn{#1}{#2}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\value}[1]
        {Counter name}
    \changes{0.1d}{Return a command sequence, not a number}
\begin{MacroCode}{package}
    \RenewDocumentCommand\value{m}{\ctr_value:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\arabic}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\arabic{m}{\ctr_use_arabic:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\alph}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\alph{m}{\ctr_use_alph:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\Alph}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\Alph{m}{\ctr_use_Alph:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\roman}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\roman{m}{\ctr_use_roman:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\Roman}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\Roman{m}{\ctr_use_Roman:n{#1}}
\end{MacroCode}
    \end{macro}
    \begin{macro}{\fnsymbol}[1]
        {Counter name}
\begin{MacroCode}{package}
    \RenewDocumentCommand\fnsymbol{m}{\ctr_use_fn:n{#1}}
\end{MacroCode}
    \end{macro}
\begin{MacroCode}{package}
}
\end{MacroCode}

\begin{MacroCode}{package}
\endinput
\end{MacroCode}

    \Finale
    \section{Installation}
    The easiest way to install this package is using the package
    manager provided by your \LaTeX\ installation if such a program
    is available. Failing that, provided you have obtained the package
    source (\file{l3ctr2e.tex} and \file{Makefile}) from either CTAN
    or Github, running \texttt{make install} inside the source directory
    works well. This will extract the documentation and code from
    \file{l3ctr2e.tex}, install all files into the TDS tree at
    \texttt{TEXMFHOME} and run \texttt{mktexlsr}.

    If you want to extract code and documentation without installing
    the package, run \texttt{make all} instead. If you insist on not
    using \texttt{make}, remember that packages distributed using
    \pkg{skdoc} must be extracted using \texttt{pdflatex}, \emph{not}
    \texttt{tex} or \texttt{latex}.

    \PrintChanges
    \PrintIndex
\end{document}
