%D \module
%D   [      file=t-ltcnts,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility counters,
%D         author=Brooks Moses,
%D           date=\currentdate,
%D      copyright={Brooks Moses; The LaTeX3 Project}]

%D This \ConTeXt\ module is part of the \LaTeX-compatibility
%D module set.  It provides counters and associated macros.

\usemodule[ltbase]
\usemodule[ltcmds]
\unprotect

%D \section{\LaTeX\ Counters.}
%D
%D From \type{ltcounts.dtx}.  \ConTeXt\ already has counters, which are not
%D quite compatible with \LaTeX\ counters, so this gets tricky.  The primary
%D difficulty is that \type{\newcounter{count}} stores the counter in
%D \type{\count} in \ConTeXt\, but in \type{\c@count} in \LaTeX.  The result
%D is that \LaTeX\ tends to presume that name collisions are not a problem,
%D and thus names counters things like ``footnote'' and ``section''.
%D
%D There is not a clear complete solution to this problem; \ConTeXt\ 
%D undoubtably defines a number of macros that use its version in things
%D such as section headers, and so we must preserve its meaning in the body
%D of the document.  However, the body of the document may contain \LaTeX\
%D commands which similarly play with counters.
%D
%D At present, we use the solution of using \type{\newlatexcounter} and
%D \type{\setlatexcounter} as replacements for \type{\newcounter} and
%D \type{\setcounter} within the \LaTeX\ compatibility kernel, and provide
%D commands to switch the original name between the \LaTeX\ and \ConTeXt\
%D versions.
%D

%D We first store the existing \ConTeXt\ versions of the colliding macros.

\let\contextnewcounter\newcounter
\let\contextsetcounter\setcounter

%D The counter prefix is variable, in case we have a better idea later.

\def\v!counter{c@}

%D Much documentation of how counters work with environments has not been
%D copied over; see \type{ltcounts.dtx} for complete information.

%D For consistency, this implementation uses \ConTeXt\ counters internally.

%D Globally sets \type{\foocounter} equal to a value.

%D \type{\setcounter} has a \type{\global} in front of the counter setting.
%D We presume that \ConTeXt\ takes care of that.

\def\latexsetcounter#1#2{%
  \@ifundefined{\v!counter #1}%
    {\@nocounterr{#1}}%
    {\contextsetcounter{\v!counter #1}#2\relax}}

%D Globally increments \type{\foocounter} by value.

%D Note: \ConTeXt, for some reason, delineates the \type{\increment}
%D argument with {\em parentheses}!

\def\addtocounter#1#2{%
  \@ifundefined{\v!counter #1}%
    {\@nocounterr{#1}}%
    {\@EA\increment\@EA(\csname \v!counter #1\endcsname, #2)}}

%D Defines \type{#1} to be a counter, which is reset when counter
%D \type{[#2]} is stepped.  If \type{#2} already defined, produces
%D `\type{c@newctr already defined}' error.

\def\latexnewcounter#1{%
  \expandafter\@ifdefinable \csname \v!counter #1\endcsname
    {\@definecounter{#1}}%
  \@ifnextchar[{\@newctr{#1}}{}}

%D Produces the value of counter, for use with
%D a \type{\setcounter} or \type{\addtocounter} command.
%D
%D [Remarkably, this isn't a namespace collision!]

\def\value#1{\csname \v!counter #1\endcsname}

\def\@newctr#1[#2]{%
  \@ifundefined{\v!counter #2}{\@nocounterr{#2}}{\@addtoreset{#1}{#2}}}

%D Globally increments counter \type{\c@FOO}
%D and resets all subsidiary counters.

\def\stepcounter#1{%
  \addtocounter{#1}\@ne
  \begingroup
    \let\@elt\@stpelt
    \csname cl@#1\endcsname
  \endgroup}

\def\@stpelt#1{\global\csname \v!counter #1\endcsname \z@}

%D ### This looks somewhat iffy, as we certainly haven't implemented the
%D page counter properly.

\def\cl@@ckpt{\@elt{page}}

%D \type{\@definecounter} is a little different, due to being based on the
%D \ConTeXt\ counters.

\def\@definecounter#1{%
  \@EA\contextnewcounter\csname \v!counter #1\endcsname%
  \global\expandafter\let\csname cl@#1\endcsname\@empty
  \@addtoreset{#1}{@ckpt}%
  \global\expandafter\let\csname p@#1\endcsname\@empty
  \@EA\gdef\csname the#1\@EA\endcsname\@EA
         {\@EA\@arabic\csname \v!counter #1\endcsname}%
}

%D \type{\@cons}, from \type{ltdefns.dtx}, has been moved here.

% \@cons is also used in ltfloat, and ltoutput.

\def\@cons#1#2{\begingroup\let\@elt\relax\xdef#1{#1\@elt #2}\endgroup}

\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}}

%D In all of the representation of counters as numbers, we have preserved
%D the \LaTeX\ bi-level function structure, though it would probably be
%D simpler to do all of this in one command in most cases.
%D
%D Representation of counter as arabic numerals.

%D ### To do: Adjust the \LaTeX\ macros so they optionally fail over to
%D accessing \ConTeXt counters.

\def\arabic#1{\expandafter\@arabic\csname \v!counter #1\endcsname}
\def\@arabic#1{#1}

%D Representation of counter as lower-case Roman numerals.

\def\roman#1{\expandafter\@roman\csname \v!counter #1\endcsname}
\def\@roman#1{\romannumerals #1}

%D Representation of counter as upper-case Roman numerals.

\def\Roman#1{\expandafter\@Roman\csname \v!counter #1\endcsname}
\def\@Roman#1{\Romannumerals #1}

%D Representation of counter as a lower-case letter: 1 = a, 2 = b, etc.

\def\alph#1{\expandafter\@alph\csname \v!counter #1\endcsname}
\def\@alph#1{\character #1}

%D Representation of counter as an upper-case letter: 1 = A, 2 = B, etc.

\def\Alph#1{\expandafter\@Alph\csname \v!counter #1\endcsname}
\def\@Alph#1{\Character #1}

%D Representation of counter as a footnote symbol: 1 = $*$, 2 = $\dagger$, etc.      
%D
%D [While \ConTeXt\ offers a number of footnote-symbol lists, none of them
%D reproduce the \LaTeX\ ordering.]
%D
%D The original \LaTeX\ version of this relies on \type{\ensuremath}, which
%D won't be defined unless we load the math module.  Thus, we use the
%D \ConTeXt version, \type{\mathematics}.

% Replaced \mathsection with \sectionmark
% Replaced \mathparagraph with \paragraphmark
% Replaced \| with \Vert

\def\fnsymbol#1{\expandafter\@fnsymbol\csname \v!counter #1\endcsname}

\def\@fnsymbol#1{\mathematics{\ifcase#1\or *\or \dagger\or \ddagger\or
     \sectionmark\or \paragraphmark\or \Vert\or **\or \dagger\dagger
     \or \ddagger\ddagger \else\@ctrerr\fi}}

%D We also define a \ConTeXt conversion corresponding to the \LaTeX\ symbol
%D list, in case that turns out to be useful.  Note that we use the inner
%D function \type{\@fnsymbol}, which takes a number rather than a counter
%D name!

\defineconversion [latexfnsymbol]  [\@fnsymbol]



%D Commands to switch between \LaTeX\ and \ConTeXt\ counter definitions.

\def\usecontextcounters{%
  \let\newcounter\contextnewcounter
  \let\setcounter\contextsetcounter}

\def\uselatexcounters{%
  \let\newcounter\latexnewcounter
  \let\setcounter\latexsetcounter}


\protect