%D \module
%D   [       file=core-num,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Numbering,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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 Core 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]
%    \convertednumner[name]
%    \rawnumber[name]

% private (defined in core-sec.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,
      #2]%
    \makecounter{\@@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\dodoreset#1%
  {\getvalue{\s!reset#1}}%

\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\reset
  {\dosingleargument\doreset}

% nieuw, maar kan dit (i.v.m. (sub)page?)

% \def\incrementnumber[#1]%
%   {\checknummer{#1}%
%    \doifelse\@@nrstatus\v!start
%      {\pluscounter{\@@thenumber{#1}}}
%      {\setcounter{\@@thenumber{#1}}{0\csname\@@thenumber{#1}\c!start\endcsname}}}

\def\incrementnumber[#1]% bypage tricky: needs a
  {\doifelse{\numberparameter{#1}\c!way}{\v!by\v!page}
     {\checkpagechange{#1}%
      \ifpagechanged\resetcounter{\@@thenumber{#1}}\fi}
     {\checknummer{#1}}%
   \doifelse\@@nrstate\v!start % only here
     {\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}}}

\let\getnumber\convertednumber

\ifx\checknummer\undefined \let\checknummer\gobbleoneargument \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
