%D \module
%D   [      file=t-ltitem,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility list environments,
%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 the enumerate and itemize environments,
%D and related symbols.

\usemodule[ltcnts]

\unprotect

%D \section{\LaTeX\ List Environments.}
%D
%D From \type{ltlists.dtx}.  \ConTeXt\ already has itemization 
%D environments, so this section is built on those, rather than 
%D defining the environments from scratch.
%D
%D Since \ConTeXt already has item counters, we define the \LaTeX\
%D counter names as ``dummy'' counters, and set them to the
%D \ConTeXt value immediately prior to calling the relevant 
%D \type{\theenum...} command to typeset them.  This provides 
%D compatibility both with \ConTeXt's item counter tracking and
%D with the \LaTeX\ methods for defining the typesetting.
%D
%D Note that the internal \LaTeX\ list definitions, while also 
%D in \type{ltlists.dtx}, are not included in this file, but are
%D instead in \type{t-ltlist.tex}.  This produces a cleaner
%D dependency tree.
%D

%D Set up the spacing lengths.  \type{\leftmargin*} becomes the
%D width value of the corresponding item level.  \LaTeX\ document
%D classes pass this in by setting the \type{\leftmargin} length
%D to the relevant one of these each time an itemize is started,
%D but that is difficult to do with \ConTeXt, and using these
%D directly is quite simple.

\newdimen\leftmargini
\newdimen\leftmarginii
\newdimen\leftmarginiii
\newdimen\leftmarginiv
\newdimen\labelsep
\newskip\itemsep



%D Set up ``dummy'' counters for \LaTeX\ enumerations.

\latexnewcounter{enumi}
\latexnewcounter{enumii}
\latexnewcounter{enumiii}
\latexnewcounter{enumiv}

%D Define conversions.  The ``number'' argument is assigned to
%D the appropriate counter, and then the relevant \type{\the...}
%D command is called to typeset the counter.
%D
%D Note that the original \LaTeX\ versions use \type{\llap}, but
%D \ConTeXt has redefined this to involve boxes in a way that fails
%D badly here, so we use the plain \TeX\ definition of it instead.

\defineconversion[latexenumi][\dolatexenumi]
\def\dolatexenumi#1{%
  \contextsetcounter{\v!counter enumi}{#1}%
  \hbox to 0pt{\hss\labelenumi}}

\defineconversion[latexenumii][\dolatexenumii]
\def\dolatexenumii#1{%
  \contextsetcounter{\v!counter enumii}{#1}%
  \hbox to 0pt{\hss\labelenumii}}

\defineconversion[latexenumiii][\dolatexenumiii]
\def\dolatexenumiii#1{%
  \contextsetcounter{\v!counter enumiii}{#1}%
  \hbox to 0pt{\hss\labelenumiii}}

\defineconversion[latexenumiv][\dolatexenumiv]
\def\dolatexenumiv#1{%
  \contextsetcounter{\v!counter enumiv}{#1}%
  \hbox to 0pt{\hss\labelenumiv}}

%D Define \type{latexenumerate} as an item group.

\defineitemgroup[latexenumerate][levels=4]
\setupitemgroup[latexenumerate][1][latexenumi]
\setupitemgroup[latexenumerate][2][latexenumii]
\setupitemgroup[latexenumerate][3][latexenumiii]
\setupitemgroup[latexenumerate][4][latexenumiv]
\setuplatexenumerate[stopper=]

%D Set up default values of the \type{\the...} commands.
%D
%D (From \type{classes.dtx}, modified to use the top-level conversion
%D commands, in case \type{\v!counter} is changed.)

\renewcommand\theenumi{\arabic{enumi}}
\renewcommand\theenumii{\alph{enumii}}
\renewcommand\theenumiii{\roman{enumiii}}
\renewcommand\theenumiv{\Alph{enumiv}}

\newcommand\labelenumi{\theenumi.}
\newcommand\labelenumii{(\theenumii)}
\newcommand\labelenumiii{\theenumiii.}
\newcommand\labelenumiv{\theenumiv.}

\setuplatexenumerate[stopper=]


%D Define \type{latexitemize} as an item group.

\defineitemgroup[latexitemize][levels=4]
\setupitemgroup[latexitemize][1][labelitemi]
\setupitemgroup[latexitemize][2][labelitemii]
\setupitemgroup[latexitemize][3][labelitemiii]
\setupitemgroup[latexitemize][4][labelitemiv]

%D Set up the item labels for \LaTeX\ \type{itemize} environments,
%D and define these as \ConTeXt symbols.

% \textendash =-> --
% \textasteriskcentered -> \mathematics{*}
% \textperiodcentered -> \mathematics{\cdot}

\newcommand\labelitemi{\textbullet}
\newcommand\labelitemii{{\bf --}}
\newcommand\labelitemiii{\mathematics{*}}
\newcommand\labelitemiv{\mathematics{\cdot}}

\definesymbol[labelitemi][\labelitemi]
\definesymbol[labelitemii][\labelitemii]
\definesymbol[labelitemiii][\labelitemiii]
\definesymbol[labelitemiv][\labelitemiv]

%D Associate label spacings with the correct dimensions.

\setuplatexenumerate[1][
    inbetween={\vskip \itemsep},
    width=\leftmargini,
    left={\hskip\leftmargini\hskip-\labelsep}]

\setuplatexenumerate[2][
    inbetween={\vskip \itemsep},
    width=\leftmarginii,
    left={\hskip\leftmarginii\hskip-\labelsep}]

\setuplatexenumerate[3][
    inbetween={\vskip \itemsep},
    width=\leftmarginiii,
    left={\hskip\leftmarginiii\hskip-\labelsep}]

\setuplatexenumerate[4][
    inbetween={\vskip \itemsep},
    width=\leftmarginiv,
    left={\hskip\leftmarginiv\hskip-\labelsep}]

%D ### To Do: fix this so the bullets are flush-right in their
%D boxes.  (The \type{left} doesn't seem to affect them.)

\setuplatexitemize[1][
    inbetween={\vskip \itemsep},
    width=\leftmargini,
    left={\hskip\leftmargini\hskip-\labelsep}]

\setuplatexitemize[2][
    inbetween={\vskip \itemsep},
    width=\leftmarginii,
    left={\hskip\leftmarginii\hskip-\labelsep}]

\setuplatexitemize[3][
    inbetween={\vskip \itemsep},
    width=\leftmarginiii,
    left={\hskip\leftmarginiii\hskip-\labelsep}]

\setuplatexitemize[4][
    inbetween={\vskip \itemsep},
    width=\leftmarginiv,
    left={\hskip\leftmarginiv\hskip-\labelsep}]

%D Set spacing values, based on the \LaTeX\ standard class values.
%D (We use the single-column versions.)

\leftmargini 2.5em
\leftmarginii 2.2em
\leftmarginiii 1.87em
\leftmarginiv 1.75em
\labelsep .5em

%D This is the value from the 11pt version; it's a good compromise.
\itemsep 4.5pt plus 2.5pt minus 1pt

%D Finally, switch commands to use either the \LaTeX\ or \ConTeXt
%D version of \type{itemize}.  Also, since \type{enumerate} isn't
%D a conflict, we define synonyms for that.

\let\contextstartitemize=\startitemize
\let\contextstopitemize=\stopitemize

\let\startenumerate=\startlatexenumerate
\let\stopenumerate=\stoplatexenumerate

\def\uselatexitemize{%
  \let\startitemize=\startlatexitemize
  \let\stopitemize=\stoplatexitemize}

\def\usecontextitemize{%
  \let\startitemize=\startcontextitemize
  \let\stopitemize=\stopcontextitemize}

\protect
