%D \module
%D   [       file=strc-num,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Numbering,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Structure Macros / Numbering}

\unprotect

%  Commando's ten behoeve van nummeren:
%
%    \definenumber[name]
%    \setupnumber[name][wijze=,blok=,tekst=,plaats=,conversie=,start=]
%    \setnumber[name]{value}
%    \resetnumber[name]
%    \incrementnumber[name]
%    \decrementnumber[name]
%    \convertednumber[name] % getnumber
%    \savenumber[name]
%    \restorenumber[name]
%    \convertednumber[name]
%    \rawnumber[name]

% private (defined in core-des.tex)
%
%    \nextnumber[name][tag][reference]
%    \currentnumber[name]

\def\@@thenumber#1{\s!number\csname\s!number#1\c!number\endcsname}

% this will be the new (public) one: \let\numberparameterprefix\@@thenumber

\def\numberparameter#1#2{\csname\@@thenumber{#1}#2\endcsname} % sort of public

\def\dosetupnumber[#1][#2]%
  {\@EA\let\@EA\savedstartnumber\csname\@@thenumber{#1}\c!start\endcsname
   \getparameters[\@@thenumber{#1}][\c!start=,#2]%
   \doifelsenothing{\numberparameter{#1}\c!start}
     {\letvalue{\@@thenumber{#1}\c!start}\savedstartnumber}
     {\setcounter{\@@thenumber{#1}}{\numberparameter{#1}\c!start}}}

\def\setupnumber
  {\dodoubleargument\dosetupnumber}

\def\definenumber
  {\dodoubleempty\dodefinenumber}

\def\dodefinenumber[#1][#2]% ook overal class als localframed
  {\doifassignmentelse{#2}
     {\dododefinenumber[#1][#2]}
     {\doifelsenothing{#2} % can break on not yet defined macros in #2
        {\dododefinenumber[#1][#2]}
        {\setvalue{\s!number#1\c!number}{#2}}}}

\def\dododefinenumber[#1][#2]%
  {\getparameters
     [\s!number#1]
     [\c!number=#1,
      \s!check=,
      \c!way=\@@nrway,
      \c!way\c!local=\numberparameter{#1}\c!way,
      \c!sectionnumber=\v!yes,
      \c!text=,     % no longer used here, will go away
      \c!location=, % no longer used here, will go away (was ooit \c!zetwijze)
      \c!conversion=\v!numbers,
      \c!start=0,
      \c!state=\v!start,
      #2]%
    \makecounter{\@@thenumber{#1}}%
    \setxvalue{\@@thenumber{#1}\c!n}{\countervalue{\@@thenumber{#1}}}%
    \setcounter{\@@thenumber{#1}}{\numberparameter{#1}\c!start}}

\def\setnumber[#1]#2%
  {\setcounter{\@@thenumber{#1}}{#2}}

\def\resetnumber[#1]%
  {\setcounter{\@@thenumber{#1}}{0\numberparameter{#1}\c!start}}

\def\savenumber[#1]%
  {\savecounter{\@@thenumber{#1}}}

\def\restorenumber[#1]%
  {\restorecounter{\@@thenumber{#1}}}

%D Bonus macro (we need to keep this one for downward
%D compatibility reasons).

\def\doreset[#1]%
  {\processcommalist[#1]\dodoreset}

\def\dodoreset#1%
  {\getvalue{\s!reset#1}}%

\def\reset
  {\dosingleargument\doreset}

\def\incrementnumber[#1]% bypage tricky: needs a
  {\doifelse{\numberparameter{#1}\c!way}{\v!by\v!page}
     {\checkpagechange{#1}%
      \ifpagechanged\resetcounter{\@@thenumber{#1}}\fi}
     {\checknumber[#1]}%
   \doifelse\@@nrstate\v!start % only here
     {\doif{\numberparameter{#1}\c!state}\v!start{\pluscounter{\@@thenumber{#1}}}}
     {\setcounter{\@@thenumber{#1}}{0\numberparameter{#1}\c!start}}}

% \defineenumeration [test] [way=bypage,text=\lastchangedpage]
%
% \starttext \dorecurse{10}{\test \input tufte \par} \stoptext

\def\decrementnumber[#1]%
  {\minuscounter{\@@thenumber{#1}}}

\def\convertednumber[#1]%
  {\convertnumber
     {\numberparameter{#1}\c!conversion}
     {\countervalue{\@@thenumber{#1}}}}

\def\rawnumber[#1]%
  {\countervalue{\@@thenumber{#1}}}

\def\accumulatednumber[#1]%
  {\getvalue{\@@thenumber{#1}\c!n}}

\let\getnumber\convertednumber

\def\doifdefinednumber    #1{\doifdefined    {\csname\s!number#1\c!number\endcsname}}
\def\doifundefinednumber  #1{\doifundefined  {\csname\s!number#1\c!number\endcsname}}
\def\doifdefinednumberelse#1{\doifdefinedelse{\csname\s!number#1\c!number\endcsname}}

\ifx\checknumber\undefined \def\checknumber[#1]{} \fi

% ook de pag nummers hierheen halen ivm \@@nrwijze

\def\setupnumbering
  {\dodoubleempty\getparameters[\??nr]}

\setupnumbering
  [\c!way=\v!by\v!chapter,
   \c!blockway=,
   \c!sectionnumber=\v!yes,
   \c!state=\v!start]

\protect \endinput
