% \iffalse meta-comment
%%% File: algo-lists.sty
%% Copyright (C) 2017 Michael N\"usken.
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3a
% 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.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% -----------------------------------------------------------
% 
%<*chartab>
%\fi
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
% \iffalse meta-comment
%</chartab>
% \fi
%
% \CheckSum{0}
%
%
%  \GetFileInfo{algo-lists.sty}
%  \CodelinePrefix{A}
%
% \section{Source of \filename (\fileprefix)}
%
%  This file (\file{\filename}) has version number \fileversion, last
%  revised \filedate.
%    \begin{macrocode}
%<*algolists>
%    \end{macrocode}
%
%  Announce the package and its version:
%    \begin{macrocode}
% \iffalse
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algo-lists}[2017/04/21 v0.94 %
Lists for algorithms. MNuesken]
% \fi
%    \end{macrocode}
%  
%    \begin{macrocode}
\DeclareOption{override-cc}{%
  %%% \let\c@ccalgonumerate\c@algonumerate
  %%% \let\labelccalgonumerate\labelalgonumerate
  %%% \let\ccalgonumerate\algonumerate
  %%% \let\endccalgonumerate\endalgonumerate
  \let\c@algonumerate\@undefined
  \let\labelalgonumerate\@undefined
  \let\algonumerate\@undefined
  \let\endalgonumerate\@undefined
  \let\algonumerateinit\@undefined
}
\ProcessOptions
%    \end{macrocode}
%
% This provides \@ifempty and does not hurt.
%    \begin{macrocode}
\RequirePackage{gaux}
%    \end{macrocode}
%
%
%  For a dirty trick below we rely on the specific implementation of
%  |\@item|.  Thus we verify it!
%    \begin{macrocode}
{\def\algolist@numerate@item[#1]{%
  \if@noparitem
    \@donoparitem
  \else
    \if@inlabel
      \indent \par
    \fi
    \ifhmode
      \unskip\unskip \par
    \fi
    \if@newlist
      \if@nobreak
        \@nbitem
      \else
        \addpenalty\@beginparpenalty
        \addvspace\@topsep
        \addvspace{-\parskip}%
      \fi
    \else
      \addpenalty\@itempenalty
      \addvspace\itemsep
    \fi
    \global\@inlabeltrue
  \fi
  \everypar{%
    \@minipagefalse
    \global\@newlistfalse
    \if@inlabel
      \global\@inlabelfalse
      {\setbox\z@\lastbox
       \ifvoid\z@
         \kern-\itemindent
       \fi}%
      \box\@labels
      \penalty\z@
    \fi
    \if@nobreak
      \@nobreakfalse
      \clubpenalty \@M
    \else
      \clubpenalty \@clubpenalty
      \everypar{}%
    \fi}%
  \if@noitemarg
    \@noitemargfalse
    \if@nmbrlist
      \refstepcounter\@listctr
    \fi
  \fi
  \sbox\@tempboxa{\makelabel{#1}}%
%    \end{macrocode}
%  We require that |\global| follows this |\makelabel| to insert a
%  hook.  Moreover we use that the produced label is put in
%  \cs{@tempboxa} to reinitialize the paragraph indention in
%  \cs{reinit@list@label}.
%    \begin{macrocode}
  \global\setbox\@labels\hbox{%
    \unhbox\@labels
    \hskip \itemindent
    \hskip -\labelwidth
    \hskip -\labelsep
    \ifdim \wd\@tempboxa >\labelwidth
      \box\@tempboxa
    \else
      \hbox to\labelwidth {\unhbox\@tempboxa}%
    \fi
    \hskip \labelsep}%
  \ignorespaces}
\ifx\algolist@numerate@item\@item\else
\PackageWarningNoLine{algo-lists}{%
  Command \protect\@item\space  has changed.\MessageBreak
  Inform package maintainer.}
\fi}
%    \end{macrocode}
%
%  This macro restores the parshape of the current list.
%    \begin{macrocode}
\def\algolist@list@parshape{%
%  \typeout{parshape 1 \the\@totalleftmargin+\the\linewidth}% DEBUG
  \parshape \@ne \@totalleftmargin \linewidth}
%    \end{macrocode}
%  The dimensions \cs{algolist@numerate@minindent} and
%  \cs{algolist@numerate@maxindent} control the hanging indention of list
%  entries.  By default the length of the label determines the hanging
%  indention, but that is restricted by these two values.
%    \begin{macrocode}
\newdimen\algolist@numerate@minindent \algolist@numerate@minindent\z@
\newdimen\algolist@numerate@maxindent \algolist@numerate@maxindent16383.1pt
\AtBeginDocument{\ifdim\algolist@numerate@maxindent>16383pt
  \algolist@numerate@maxindent.2\textwidth\fi}
%    \end{macrocode}
%  Reinitializing the list means to readjust the left margin while
%  leaving the position of the list's labels fixed.
%    \begin{macrocode}
\newdimen\algolist@reinit@list@last
\def\algolist@reinit@list@maxonly{%
  \@tempswafalse\algolist@reinit@list@swa}
\def\algolist@reinit@list@both{%
  \@tempswatrue\algolist@reinit@list@swa}
\def\algolist@reinit@list{%
  \@tempswafalse
  \ifcsname @listctr\endcsname%
    \expandafter\ifnum\value{\@listctr}<\@ne% only for algonumerate
      \@tempswatrue
    \fi
  \fi
  \algolist@reinit@list@swa}
\def\algolist@reinit@list@swa#1{%
  %%% \typeout{Reinit \the#1: old \the\algolist@reinit@list@last, \the\itemindent, \the\labelwidth, \the\labelsep}%%% DEBUG
  %%% \typeout{\the\@totalleftmargin + \the\linewidth}% DEBUG
  \advance\itemindent\algolist@reinit@list@last
  \advance\@totalleftmargin-\algolist@reinit@list@last
  \advance\linewidth\algolist@reinit@list@last
  %%% \global
  \algolist@reinit@list@last#1\relax
  \advance\labelsep-\itemindent
  \itemindent\z@% assuming \itemindent=0pt in algonumerate
  \if@tempswa
    \ifdim\algolist@reinit@list@last<\algolist@numerate@minindent
    \advance\itemindent\algolist@numerate@minindent
    \advance\itemindent-\algolist@reinit@list@last
    \advance\labelsep\algolist@numerate@minindent
    \advance\labelsep-\algolist@reinit@list@last
    %%% \global
    \algolist@reinit@list@last\algolist@numerate@minindent
    \fi
  \fi%
  \ifdim\algolist@reinit@list@last>\algolist@numerate@maxindent
    %%% \global
    \algolist@reinit@list@last\algolist@numerate@maxindent
  \fi
  \advance\itemindent-\algolist@reinit@list@last
  \advance\@totalleftmargin\algolist@reinit@list@last
  \advance\linewidth-\algolist@reinit@list@last
  %%% \typeout{Reinit \the#1: new \the\algolist@reinit@list@last, \the\itemindent, \the\labelwidth, \the\labelsep}%%% DEBUG
  %%% \typeout{\the\@totalleftmargin + \the\linewidth}% DEBUG
  \algolist@list@parshape}
%    \end{macrocode}
%  Often reinitialization is needed with respect to the label's
%  width.
%    \begin{macrocode}
\def\algolist@reinit@list@label{%
  \algolist@reinit@list{\wd\@tempboxa}%
  }
%    \end{macrocode}
%  \begin{macro}{\makealgolistlabel}
%    The standard format of the labels is the number followed by a dot.
%    \begin{macrocode}
\newcommand{\makealgolistlabel}[1]{#1.}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\makeoptalgolistlabel}
%    A special label is surrounded by parentheses.
%    \begin{macrocode}
\newcommand{\makeoptalgolistlabel}[1]{(#1)}
%    \end{macrocode}
%  \end{macro}
%  
%  \begin{macro}{\makeoptalgolistcurrentlabel}
%    This macro is used to produce the |\bare| reference form for a
%    special label.
%    \begin{macrocode}
\let\makeoptalgolistcurrentlabel\makeoptalgolistlabel
%    \end{macrocode}
%  \end{macro}
%
%  \subsection{The \Lenv{algonumerate} environment}
%
%  This environment is the basis of the algorithm environment.   It
%  allows some items with names (as |Input|, |Output|) which have a
%  special indention.  These named and the following numbered items
%  are separated by a |\medskip|.
%
%  We need a counter.  By default we do not use a prefix.
%    \begin{macrocode}
\newcounter{algonumerate}
\@namedef{p@algonumerate}{}
%    \end{macrocode}
%  This count holds the number of named items.
%    \begin{macrocode}
\newcount\algolist@numerate@items
%    \end{macrocode}
%  Formatting the counter.
%    \begin{macrocode}
\renewcommand{\thealgonumerate}{%
  \expandafter\ifnum\value{algonumerate}<\@ne
  \algonumeratefield%
  \else
  \arabic{algonumerate}\fi}%
%    \end{macrocode}
%  This macro returns the name corresponding to the counter
%  |algonumerate|.   The macro |\@algonumeratefield| should be
%  defined using |\ifcase-#1| \dots{} |\fi|  and return the
%  corresponding name.  This is done be |\algonumerateinit|, see
%  below.
%    \begin{macrocode}
\def\algonumeratefield{\ignorespaces
  \ifx\@algonumeratefield\relax\else
  \expandafter\@algonumeratefield\csname c@algonumerate\endcsname\fi
  \unskip}
%    \end{macrocode}
%  A toggle to distinguish special labels and numbered labels.
%    \begin{macrocode}
\newif\if@speciallabel
%    \end{macrocode}
%  This macro produces the labels in all cases.
%    \begin{macrocode}
\newcommand{\labelalgonumerate}{%
  \count@\value{algonumerate}%
  \ifnum\count@<\z@
  \algonumeratefield%
  \else
  \makealgolistlabel{\thealgonumerate}%
  \fi
  }%
%    \end{macrocode}
%  These two dimensions define the width of a label and its distance
%  to the text.
%    \begin{macrocode}
\newdimen\algolist@labelsep
\newdimen\steplabelwidth
%    \end{macrocode}
%  This token list is executed at the next |\item|.
%    \begin{macrocode}
\newtoks\AtNextItem
\newtoks\AtNextNumberedItem
%    \end{macrocode}
%  A toggle noting whether the named items are complete or not.
%    \begin{macrocode}
\newif\if@noitemargcopy
\let\algolist@orig@item\item
\def\item{%
  \@ifnextchar[%
  {\@noitemargcopyfalse\algolist@orig@item}%
  {\@noitemargcopytrue\algolist@orig@item}%
}
\newcount\last@algolistlabeltype
\newcount\this@algolistlabeltype
%    \end{macrocode}
%  This is |\makelabel| for our list.  It produces the label,
%  inserts the hook and makes all necessary modifications.
%    \begin{macrocode}
\newcommand{\algolist@numerate@label}[1]{%
% \typeout{algolist@numerate@label #1}% DEBUG
% \typeout{...counter=\the\count@}% DEBUG
% \if@noitemargcopy\typeout{...noitemarg}\else\typeout{...explicititemarg}\fi% DEBUG
%    \end{macrocode}
%  Now, fork with respect to the item type.  There are four variants:
%  a numbered item with negative counter procudes an item with hanging
%  indention and a text label from the list supplied at the list
%  initialisation.  A numbered item with positive counter produces a
%  numbered item.  The counter is stepped in these two cases and zero
%  is skipped.  An item with empty optional argument produces an
%  item like a numbered one just without a number.  An item with a
%  non-empty optional argument produces an item with hanging indention
%  and the optional argument as label.
%    \begin{macrocode}
  \if@noitemargcopy
    \expandafter\count@\value{\@listctr}%
    \ifnum\count@<\z@
      \let\next\algolist@numerate@labelautomatic
      \global\last@algolistlabeltype\this@algolistlabeltype
      \global\this@algolistlabeltype\m@ne
    \else
      \let\next\algolist@numerate@labelnumber
      \global\last@algolistlabeltype\this@algolistlabeltype
      \global\this@algolistlabeltype\@ne
    \fi
  \else
    \def\@tempa{#1}%
    \ifx\@tempa\@empty
      \let\next\algolist@numerate@labelempty
      \global\last@algolistlabeltype\this@algolistlabeltype
      \global\this@algolistlabeltype\tw@
    \else
      \let\next\algolist@numerate@labelexplicit
      \global\last@algolistlabeltype\this@algolistlabeltype
      \global\this@algolistlabeltype\z@
    \fi
  \fi\next{#1}}
%    \end{macrocode}
%  For each of the four variants we now specify how to typeset them.
%    \begin{macrocode}
\newcommand{\algolist@numerate@makelabelexplicit}[1]{%
  #1:\hskip\algolist@labelsep%
}
\newcommand{\algolist@numerate@makelabelempty}[1]{%
  \hskip\steplabelwidth\hskip\algolist@labelsep%
}
\newcommand{\algolist@numerate@makelabelautomatic}[1]{%
  #1:\hskip\algolist@labelsep%
}
\newcommand{\algolist@numerate@makelabelnumber}[1]{%
  \hb@xt@\steplabelwidth{\hss
      #1%
  }\hskip\algolist@labelsep%
}
%    \end{macrocode}
%  
%  The next four macros are similar to each other.  For each item type
%  they produces the label and use the dirty trick to get counting and
%  paragraph formatting as desired.
%    \begin{macrocode}
\newcommand{\algolist@numerate@labelexplicit}[1]{% For algorithm i/o
  %%% \typeout{item explicit [#1]}% DEBUG
  \algolist@numerate@makelabelexplicit{#1}%
  \algolist@defnext@hookintoitem{%
    \algolist@numerate@postprocess{%
      \protected@edef\@currentlabel{\csname p@\@listctr\endcsname
        {\makeoptalgolistcurrentlabel{#1}}}%
      \protected@edef\@bare@currentlabel{#1}%
    }%
  }\next%
}
\newcommand{\algolist@numerate@labelempty}[1]{% For algorithm i/o
  %%% \typeout{item empty [#1]}% DEBUG
  \algolist@numerate@makelabelempty{}%
  \algolist@defnext@hookintoitem{%
    \algolist@numerate@postprocess{}%
  }\next
}
\newcommand{\algolist@numerate@labelautomatic}[1]{% For algorithm steps
  %%% \typeout{item automatic [#1]}% DEBUG
  \algolist@numerate@makelabelautomatic{#1}%
  \algolist@defnext@hookintoitem{%
    \algolist@numerate@postprocess{%
      \protected@edef\@currentlabel{\csname p@\@listctr\endcsname
        {\makeoptalgolistcurrentlabel{#1}}}%
      \protected@edef\@bare@currentlabel{#1}%
    }%
  }\next
}
\newcommand{\algolist@numerate@labelnumber}[1]{% For algorithm steps
  %%% \typeout{item numbered [#1]}% DEBUG
  \algolist@numerate@makelabelnumber{#1}%
  \algolist@defnext@hookintoitem{%
    \algolist@numerate@postprocess{}%
  }\next
}
%    \end{macrocode}
%
%  The macro \cs{algolist@numerate@postprocess} contains all the stuff to
%  be done after producing the label and putting it into
%  \cs{@tempboxa}.
%    \begin{macrocode}
\newcommand{\algolist@numerate@postprocess}[1]{%
%%% \tracingall% DEBUG
%%% \typeout{Calling algolist@numerate@postprocess with arg1=#1}% DEBUG
%%% \tracingnone% DEBUG
%    \end{macrocode}
%  We insert a \cs{medskip} if a (positively) numbered item follows an
%  automatic or explicit text item.
%    \begin{macrocode}
    \@expandonearg\@ifequal{\@listctr}{algonumerate}{%
      \expandafter\ifnum\value{algonumerate}=\m@ne
      \stepcounter{algonumerate}%
      \fi
    }{}%
    \ifnum\last@algolistlabeltype<\@ne
    \ifnum\this@algolistlabeltype=\@ne
    \medskip
    \fi
    \fi
%    \end{macrocode}
%  Next, we execute case specific commands. (For text items we need to
%  set \cs{@currentlabel} here.)
%    \begin{macrocode}
    #1%
    \global\let\latest@item@currentlabel\@currentlabel
    \global\let\latest@item@bare@currentlabel\@bare@currentlabel
    \global\@speciallabeltrue
%    \end{macrocode}
%  Execute |\AtNextItem| once and clear it. If in a numbered item
%  execute |\AtNextNumberedItem| once and clear it.
%    \begin{macrocode}
    \the\AtNextItem \global\AtNextItem{}%
    \ifnum\this@algolistlabeltype=\@ne
    \the\AtNextNumberedItem \AtNextNumberedItem{}%
    \fi
%    \end{macrocode}
%  Reinitialize the list indentions.
%    \begin{macrocode}
    \algolist@reinit@list@label
}
%    \end{macrocode}
%
% The next macro establishes the hooking of its argument into
% \cs{@item} right after having defined \cs{@tempboxa} with the
% present label.  Be careful in using it: If you try to install a
% hook before the previous one is removed it will crash.
%    \begin{macrocode}
\newcommand{\algolist@defnext@hookintoitem}[1]{%
%    \end{macrocode}
%  To be able to defer this we define next to be this hooking stuff.
%  Use \cs{next} at the appropriate place, usually right after, to
%  execute it.
%    \begin{macrocode}
  \def\next{%
%    \end{macrocode}
%  The following lines are the dirty trick!!!  They establishes the hook.
%    \begin{macrocode}
    \global\let\algolist@numerate@global\global
    \gdef\global{%
      \algolist@numerate@global\let\global\algolist@numerate@global
%    \end{macrocode}
%  Now insert the hooked code.
%    \begin{macrocode}
      #1%
%    \end{macrocode}
%  Execute the original |\global| which was used to implement this hook.
%    \begin{macrocode}
      \global
    }%
  }%
}
%    \end{macrocode}
%
%  \begin{macro}{\algonumerateinit}
%    This macro prepares all things for the named items.  Its argument
%    is just the comma separated list of the names.
%    \begin{macrocode}
\newcommand{\algonumerateinit}[1]{%
  \setcounter{algonumerate}{-1}%
  \this@algolistlabeltype=\@M
  \def\@algonumeratefield{}%
  \let\algolist@org@or\or\let\or\relax
  \algolist@numerate@items\z@
  \@for\@tempa:=#1\do{%
    \advance\algolist@numerate@items\@ne
    \@temptokena\expandafter{\@tempa}%
    \toks@\expandafter{\@algonumeratefield}%
    \edef\@algonumeratefield{\the\@temptokena\or\the\toks@}%
  }%
  \let\or\algolist@org@or
  \toks@\expandafter{\@algonumeratefield}%
  \edef\@algonumeratefield##1{\noexpand\ifcase-##1\noexpand\or
    \the\toks@\noexpand\fi}%
  %%% \typeout{Starting ...}% DEBUG
}
%    \end{macrocode}
%  \end{macro}
%
%  We add a mechanism to find and store the length of the longest
%  macro.
% \begin{macro}{\algonumerateminadjust}
%   This macro is mainly for internal use.  It uses list settings and
%   the result of \cs{algonumerateinit} to determine the width of the
%   longest label and sets the minimal indention for the initial items
%   of an algonumerate list.  As a result these initial items
%   typically have all that same indention.
%   \begin{macrocode}
\newdimen\algolist@longestlabel
\def\algonumerateminadjust{%
  \algolist@longestlabel\z@
  \@tempcnta-\algolist@numerate@items
  \@whilenum\@tempcnta<\z@\do{%
    \settowidth\dimen@{\algolist@numerate@makelabelautomatic{\@algonumeratefield{\@tempcnta}}}%
    %%% \typeout{Adjust: label \the\@tempcnta = \the\dimen@ (\@algonumeratefield{\@tempcnta})}% DEBUG
    \ifdim\dimen@>\algolist@longestlabel\relax
    \algolist@longestlabel\dimen@\fi    
    \advance\@tempcnta\@ne
  }%
  %%% \typeout{Adjust: longest label = \the\algolist@longestlabel}% DEBUG
  \ifdim\algolist@numerate@minindent<\algolist@longestlabel
  \algolist@numerate@minindent\algolist@longestlabel
  \fi
  \ifdim\algolist@numerate@minindent>\algolist@numerate@maxindent
  \algolist@numerate@minindent\algolist@numerate@maxindent
  \fi
}
%   \end{macrocode}
% \end{macro}
%
%  \begin{environment}{algonumerate}
%    Finally, we define the environment \Lenv{algonumerate}.  Apart
%    from initiation the list, it also activates the environments
%    \Lenv{range}, \Lenv{indention} and \Lenv{meta}.
%    The initial items are typeset with hanging indentation taylored
%    to the widest label, but restricted to \algolist@numerate@maxindent.
%    \begin{macrocode}
\newtoks\everyalgonumerate \everyalgonumerate{\algonumerateminadjust}
\newtoks\AtNextAlgonumerate \AtNextAlgonumerate{}
\newenvironment{algonumerate}{%
  \if@inlabel~\fi\par
  \list{\labelalgonumerate}{%
    \usecounter{algonumerate}%
    \let\makelabel\algolist@numerate@label
    \parsep\z@%
    \topsep\z@%
    \itemsep\z@%
    \parskip\z@%
    \leftmargin\labelsep
    \algolist@labelsep\z@
    \settowidth\steplabelwidth{\makealgolistlabel{99}}%
    \labelwidth\z@
    \global\algolist@reinit@list@last\z@
    \the\everyalgonumerate
    \the\AtNextAlgonumerate \global\AtNextAlgonumerate{}
  }%
  \setcounter{algonumerate}{-\algolist@numerate@items}%
  \ifnum\algolist@numerate@items>\z@
  \addtocounter{algonumerate}\m@ne
  \fi
  \stepcounter{algolist@range@cnt}% another numerate, reset subcounter
  \let\range\algolist@range
  \let\endrange\endalgolist@range
  \let\indention\algolist@indention
  \let\endindention\endalgolist@indention
  \let\meta\algolist@meta
  \let\endmeta\endalgolist@meta
  \def\skipremainingspecialitems{\setcounter{algonumerate}{0}}%
  }{%
  \endlist\vskip\topskip}
%    \end{macrocode}
%  \end{environment}
%
%  This is |\makelabel| for our list.  It produces the label,
%  inserts the hook and makes all necessary modifications.
%    \begin{macrocode}
\def\@testnumberdot#1.#2\@@@{%
  %%% \typeout{Testnumberdot {#1}{#2}}% DEBUG
  \@ifempty{#2}{\@tempswafalse}{\@tempswatrue}%
  \if@tempswa
    \if!\ifnum9<1#1!\else_\fi%
      \@tempswatrue
    \else
      \@tempswafalse
    \fi
  \fi
  %%% \if@tempswa\typeout{=T}\else\typeout{=F}\fi% DEBUG
}
\newcommand{\algolist@temize@label}[1]{%
  %%% \typeout{algolist@temize@label{#1}}% DEBUG
%    \end{macrocode}
%  Now, fork with respect to the item type.  There are four variants:
%  a non-numbered item procudes an item with hanging
%  indention.  A numbered item with positive counter produces a
%  numbered item.  The counter is stepped in these two cases and zero
%  is skipped.  An item with empty optional argument produces an
%  item like a numbered one just without a number.  An item without an
%  optional argument ???
%    \begin{macrocode}
  \let\next\relax
  \if@noitemargcopy
  %%% \typeout{...no item arg}% DEBUG
  \global\last@algolistlabeltype\this@algolistlabeltype
  \global\this@algolistlabeltype\z@
  \algolist@numerate@makelabelnumber{ }% <- Do nothing to produce the label, it's empty.
  \algolist@defnext@hookintoitem{%
    \algolist@reinit@list@maxonly{\wd\@tempboxa}%
  }%
  \else
    \def\@tempa{#1}%
    \ifx\@tempa\@empty
    %%% \typeout{...empty item arg}% DEBUG
    \global\last@algolistlabeltype\this@algolistlabeltype
    \global\this@algolistlabeltype\tw@
    % <- Do nothing to produce the label, it's empty.
    \algolist@defnext@hookintoitem{%
      \algolist@reinit@list@maxonly{\wd\@tempboxa}%
    }%
    \else
      \@testnumberdot#1.\@@@
      \if@tempswa
        %%% \typeout{...number}% DEBUG
        \global\last@algolistlabeltype\this@algolistlabeltype
        \global\this@algolistlabeltype\@ne
        \algolist@numerate@makelabelnumber{#1}%
        \def\@currentlabel{#1}%
        \def\@bare@currentlabel{#1}%
        \algolist@defnext@hookintoitem{%
          \algolist@reinit@list@maxonly{\wd\@tempboxa}%
        }%
      \else
        %%% \typeout{...no number}% DEBUG
        \global\last@algolistlabeltype\this@algolistlabeltype
        \global\this@algolistlabeltype\m@ne
        #1\hskip\algolist@labelsep%
        \def\@currentlabel{#1}%
        \def\@bare@currentlabel{#1}%
        \algolist@defnext@hookintoitem{%
          \algolist@reinit@list@both{\wd\@tempboxa}%
        }%
      \fi
    \fi
  \fi\next}
%    \end{macrocode}
%
%  \begin{environment}{algoitemize}
%    The alternate unnumbered environment is \Lenv{algoitemize}.  Apart
%    from initiation the list, it also activates the environments
%    \Lenv{range}, \Lenv{indention} and \Lenv{meta}.
%    There are no initial items.
%    In its typical use, all items use the optional argument.
%    \begin{macrocode}
\newtoks\everyalgoitemize \everyalgoitemize{}
\newtoks\AtNextAlgoitemize \AtNextAlgoitemize{}
\newcounter{nocounter}
\setcounter{nocounter}{99}
\newcommand{\algoitemizeadjust}{%
  %%% \advance\@totalleftmargin-\labelwidth\advance\linewidth\labelwidth\labelwidth\z@
  %%% \def\@listctr{nocounter}\def\p@nocounter{}%
  %%% \global\algolist@reinit@list@last\z@
  %%% \algolist@numerate@minindent=10em
  %%% \algolist@numerate@maxindent=5em
  %%% \settowidth\steplabelwidth{\makealgolistlabel{99}}%
  %%% \let\makelabel\algolist@temize@label
}
\newenvironment{algoitemize}{%
  \if@inlabel~\fi\par
  %%% \itemize\relax
  %%% \parsep\z@%
  %%% \topsep\z@%
  %%% \itemsep\z@%
  %%% \parskip\z@%
  \list{}{%
    \let\makelabel\algolist@temize@label
    \parsep\z@%
    \topsep\z@%
    \itemsep\z@%
    \parskip\z@%
    \leftmargin\labelsep
    \algolist@labelsep\z@
    \settowidth\steplabelwidth{\makealgolistlabel{99}}%
    \labelwidth\z@
    \global\algolist@reinit@list@last\z@
    \the\everyalgoitemize
    \the\AtNextAlgoitemize \global\AtNextAlgoitemize{}
  }
  \let\algolist@range\relax% We may use \algo@range, but need to make sure
                  % that \item[arg] issues \def\@currentlabel{arg}.
  \let\endalgolist@range\relax
  \let\indention\algolist@indention
  \let\endindention\endalgolist@indention
  \let\meta\algolist@meta
  \let\endmeta\endalgolist@meta
}{%
  \endlist%\vskip\topskip
}%
%    \end{macrocode}
%  \end{environment}
%
%
%
%
%  Note: the following environments are defined with a prefix |algolist@|
%  in their names since they should not be available outside the
%  \Lenv{algonumerate} environment.
%
%  The \Lenv{indention} environment indents the steps inside it by an
%  amount of |\algolistindent|.  Its default value is |1em|.
%    \begin{macrocode}
\newdimen\algolistindent \algolistindent1em
%    \end{macrocode}
%  \begin{environment}{indention}
%    The items inside this environment are indented by an amount of
%    |\algolistindent|.
%    \begin{macrocode}
\newdimen\indention@reinit@list@last
\newenvironment{algolist@indention}{%
  \if@inlabel\leavevmode\fi% Make sure that a possible item is shipped.
  \par
  \indention@reinit@list@last\algolist@reinit@list@last
  \advance\leftmargin\algolistindent
  \advance\labelsep\algolistindent
  \advance\@totalleftmargin\algolistindent
  \advance\linewidth-\algolistindent
  \def\algolist@indention@undo{%
    \algolist@reinit@list{\indention@reinit@list@last}%
    \advance\leftmargin-\algolistindent
    \advance\labelsep-\algolistindent
    \advance\@totalleftmargin-\algolistindent
    \advance\linewidth\algolistindent
    }%
  \algolist@list@parshape
  \algolist@range
  }{%
  \par
  \endalgolist@range
  \algolist@indention@undo
  \algolist@list@parshape
  \aftergroup\algolist@list@parshape
  \global\let\algolist@block@currentlabel\@currentlabel
  \global\let\algolist@block@bare@currentlabel\@bare@currentlabel
  }
%    \end{macrocode}
%  \end{environment}
%
%  \begin{macro}{\algolistnextblock}
%    This is just a counter for internal use.  |\algolistnextblock| stores
%    locally a new value in |\currentblock|.  See the sophisticated
%    things below on how to use this.
%    \begin{macrocode}
\newcounter{algolist@range@cnt}
\newcounter{algolist@range@subcnt}[algolist@range@cnt]
\newcommand{\algolist@nextblock}{% package internal
  \stepcounter{algolist@range@subcnt}%
  \edef\block@{\the\c@algolist@range@cnt.\the\c@algolist@range@subcnt}%
  }
\newcommand{\algolistnextblock}{% for others
  \stepcounter{algolist@range@subcnt}%
  \edef\currentblock{\the\c@algolist@range@cnt.\the\c@algolist@range@subcnt}%
  }
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\algolistsetcurrentlabeltorange}
%    This sets the current reference to the range of argument 1 to
%    argument 2, each argument is interpreted as a \meta{subkey}.  The
%    current reference will be empty if the range is known to be
%    trivial.
%    \begin{macrocode}
\newif\if@trivialrange
\newcommand{\algolistsetcurrentlabeltorange}[2]{%
  {\@ifundefined{shortlabeltrue}{}{\shortlabeltrue}%
    \let\index\relax
    \let\@tempa\relax
    \protected@edef\@tempa{%
      \csname r@@@@-#1\endcsname}%
    \let\@tempc\relax
    \protected@edef\@tempc{%
      \csname r@@@@-#2\endcsname}%
    \ifx\@tempa\@tempc\global\@trivialrangetrue
    \else\global\@trivialrangefalse\fi
  }%
  \if@trivialrange
  \protected@edef\@currentlabel{}%
  \else
  \let\next\relax%
  \@ifundefined{short}{%
    \protected@edef\@currentlabel{%
      \csname p@\@listctr\endcsname{%
        \protect\ref{#1}%
        --%
        \protect\ref{#2}%
      }}%
  }{%
    \protected@edef\@currentlabel{%
      \csname p@\@listctr\endcsname{%
        \protect\short\protect\ref{#1}%
        --%
        \protect\short\protect\ref{#2}%
      }}%
  }%
  \fi
  \@ifundefined{bare}{}{%
    \protected@edef\@bare@currentlabel{%
      \protect\bare\protect\ref{#1}%
      --%
      \protect\bare\protect\ref{#2}%
    }%
  }%
}
%    \end{macrocode}
%  \end{macro}
%  \begin{environment}{range}
%    A |\label| (or an |\algolistlabel|) after the |\begin{range}| will
%    refer to the \emph{range} of items inside the environment.
%    \begin{macrocode}
\newenvironment{algolist@range}{%
  %%% \par
  \algolist@nextblock
  %%% \typeout{algoliste@range: block@=\block@ (should never repeat)}% DEBUG
  \AtNextNumberedItem{%
    %%% MN2017/04/03, TODO: Make sure that the label is not recursive.
    \ifx\@currentlabel\range@label\Label{@@@\block@-bob}{}%
    \else\label{@@@\block@-bob}%
    \fi}%
  \algolistsetcurrentlabeltorange{@@@\block@-bob}{@@@\block@-eob}%
  \let\range@label\@currentlabel
  }{%
    \the\AtNextNumberedItem\AtNextNumberedItem{}%
    \ifx\@currentlabel\range@label\Label{@@@\block@-eob}{}\else\label{@@@\block@-eob}\fi%
  %%% \par
  \global\let\algolist@block@currentlabel\@currentlabel
  \global\let\algolist@block@bare@currentlabel\@bare@currentlabel
  \aftergroup\algolist@endrange@aftergroup
  }
%    \end{macrocode}
%  \end{environment}
%  This command restores the latest reference to the latest
%  encountered item.
%    \begin{macrocode}
\def\algolist@endrange@aftergroup{%
%   \let\@currentlabel\algolist@block@currentlabel
%   \let\@bare@currentlabel\algolist@block@bare@currentlabel
  \let\@currentlabel\latest@item@currentlabel
  \let\@bare@currentlabel\latest@item@bare@currentlabel
%   [Restored \@bare@currentlabel]
  }
%    \end{macrocode}
%
%  \begin{environment}{meta}
%    This environment allows to insert comment-like things in
%    algorithms.  It is indented by an amount of |meta@indent| which
%    defaults to |0pt|.
%    \begin{macrocode}
\newdimen\meta@indent
\newenvironment{algolist@meta}{%
  \@newlistfalse
  \vskip\topskip
  \par
  \meta@indent\@totalleftmargin
  \advance\labelsep-\meta@indent
  \advance\@totalleftmargin-\meta@indent
  \advance\linewidth\meta@indent
  \advance\leftmargin-\meta@indent
  \algolist@list@parshape
  }{%
  \vskip\topskip
  \par
  \advance\labelsep\meta@indent
  \advance\@totalleftmargin\meta@indent
  \advance\linewidth-\meta@indent
  \advance\leftmargin\meta@indent
  \aftergroup\algolist@list@parshape
  }
%    \end{macrocode}
%  \end{environment}
%
%%% % Well, let's make the environments ubiquitous even though they only
%%% % work properly inside algonumerate and algoitemize.
%%% %    \begin{macrocode}
%%% \let\meta\algolist@meta
%%% \let\endmeta\endalgolist@meta
%%% \let\indention\algolist@indention
%%% \let\endindention\endalgolist@indention
%%% \let\range\algolist@range
%%% \let\endrange\endalgolist@range
%%% %    \end{macrocode}
%  
%    \begin{macrocode}
%</algolists>
%    \end{macrocode}
