%D \module
%D   [       file=core-sec,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Sectioning,
%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.

% this module needs a clean up, currently some manipulations
% take place multiple times

% in manual (zie prikkels) : tussen=\blanko is enige hook om
% met kop-in-hoofd een spatiering af te dwingen 

\writestatus{loading}{Context Core Macros / Sectioning}

\startmessages  dutch  library: structures
  title: structuur
      1: begin van sectieblok --
      2: eind van sectieblok --
\stopmessages

\startmessages  english  library: structures
  title: structure
      1: begin of sectionblock --
      2: end of sectionblock --
\stopmessages

\startmessages  german  library: structures
  title: struktur
      1: Begin des Abschnittsblock --
      2: Ende des Abschnittsblock --
\stopmessages

\startmessages  czech  library: structures
  title: struktury
      1: zacatek oddilu (sekce) --
      2: konec oddilu (sekce) --
\stopmessages

\startmessages  italian  library: structures
  title: struttura
      1: inizio del blocco (sezione) --
      2: fine del blocco (sezione) --
\stopmessages

\startmessages  norwegian  library: structures
  title: struktur
      1: starten av blokk -- (seksjon)
      2: slutten av blokk -- (seksjon)
\stopmessages

\startmessages  romanian  library: structures
  title: structuri
      1: inceput de bloc sectiune --
      2: sfarsit de bloc sectiune --
\stopmessages

\unprotect

% from now on, internaly numbers are separated by a period 
% and postprocessed on demand

\def\numberseparator {.} % reasonable default 
\def\sectionseparator{:} % was : but is now - 

\def\@@filterfirstpart      [#1::#2]{#1}
\def\@@filtersecondpart     [#1::#2]{#2}

\def\@@filterblockpart      [#1::#2::#3]{#1}
\def\@@filternumberpart     [#1::#2::#3]{#2}
\def\@@filterpagepart       [#1::#2::#3]{#3}
\def\@@filterblocknumberpart[#1::#2::#3]{#1::#2}

\def\@@filterheadpart[#1]{\@EA\@@dofilterheadpart\@EA[#1:0]}
\def\@@filtertailpart[#1]{\@EA\@@dofiltertailpart\@EA[#1:0]}

\def\@@dofilterheadpart[#1:#2]{#1}
\def\@@dofiltertailpart[#1:#2]{#2}

\def\@@filterlevelpart[#1::#2::#3]{\@@dofilterlevelpart[#2:0:0:0:0]}

\def\@@dofilterlevelpart[#1:0:0:0:#2]{#1}

\def\gobbleuntilrelax#1\relax{}

\def\separatednumber  #1{\doseparatednumber  #1.\empty\relax}
\def\removefirstprefix#1{\doremovefirstprefix#1.\empty\relax}
\def\removeallprefixes#1{\doremoveallprefixes#1.\empty\relax}

\def\doseparatednumber#1.#2%
  {#1%
   \ifx#2\empty
     \@EA\gobbleuntilrelax
   \else \numberseparator
     \@EA\doseparatednumber
   \fi#2}

\def\doremoveallprefixes#1.#2%
  {\ifx#2\empty
     #1\@EA\gobbleuntilrelax
   \else 
     \@EA\doremoveallprefixes
   \fi#2}

\def\doremovefirstprefix#1.#2%
  {\ifx#2\empty
     #1\@EA\gobbleuntilrelax
   \else 
     \@EA\noremovefirstprefix
   \fi#2}

\def\noremovefirstprefix#1.\empty\relax
  {#1}

% we need to expand in order to get something separatable

\def\dohandleheadnumber#1% 
  {\expanded{\separatednumber{#1}}}

\def\dodochecknummer#1#2#3% will become ugly after speed up 
  {\bgroup
   \doifinstringelse{.0}{.#2}  
     {\doifnot{#3}\v!per
        {%\debuggerinfo{\m!systems}{number #1 #3 becomes \getnumbervariable{#1\c!wijze}}%
         \setevalue{\@@thenumber{#1}\c!wijze}{#3}% geen \xdef, gaat mis met \subpage
         \dochecknummer{#1}}} % tricky and ugly
     {\doifnotvalue{\@@thenumber{#1}\s!check}{#2}
        {\setcounter{\@@thenumber{#1}}{0\getvalue{\@@thenumber{#1}\c!start}}%
         \setxvalue{\@@thenumber{#1}\c!wijze\c!lokaal}%
           {\getvalue{\@@thenumber{#1}\c!wijze}}%
         \setxvalue{\@@thenumber{#1}\s!check}%
           {#2}}}%
   \egroup}

\def\dochecknummer#1%
  {\edef\currentsection{\csname\??by\csname\@@thenumber{#1}\c!wijze\endcsname\endcsname}%
   \ifx\currentsection\empty\else
     \dodochecknummer
       {#1}%
       {\csname\currentsection\c!nummer\endcsname}%
       {\v!per\previoussection\currentsection}%
   \fi}

\def\checknummer#1%
  {\bgroup
  %\ifcase\blocklevel\else
   \ifdoingblocks
     \doifnotvalue{\@@thenumber{#1}\c!blokwijze}\v!nee\setblockcounters
   \fi
   \dochecknummer{#1}%
   \egroup}

\def\domaakvoorafgaandenummer[#1]% will become ugly after speed up 
  {\bgroup % added
   \globallet\voorafgaandenummer\empty
   \ifsectienummer
     \doifvalue{\??sb\@@sectieblok\c!nummer}\v!ja % added
       {\doifelsevalue{\@@thenumber{#1}\c!sectienummer}\v!ja
          \donetrue\donefalse
        \doifvalue{\@@thenumber{#1}\c!sectienummer}\v!nummer
          {\donetrue\let\@@sectionconversion\gobbleoneargument}%
        \ifdone
          \edef\currentsection
            {\getvalue{\??by\getvalue{\@@thenumber{#1}\c!wijze\c!lokaal}}}%
          \doifnot{\currentsection}\zerosection
            {\doifnot{\@@sectionvalue{\currentsection}}{0}
               {\xdef\voorafgaandenummer%
                  {\getvalue{\currentsection\c!nummer}.}}}%
        \fi}%
   \fi
   \egroup}

\def\maakvoorafgaandenummer[#1]%
  {\bgroup
  %\ifnum\blocklevel>0 
  %\ifcase\blocklevel\else
   \ifdoingblocks
     \doifnotvalue{\@@thenumber{#1}\c!blokwijze}\v!nee\setblockcounters
   \fi
   \domaakvoorafgaandenummer[#1]%
   \egroup}

% \def\maakhetnummer[#1]%
%   {\maakvoorafgaandenummer[#1]%
%    \xdef\hetnummer%
%      {\voorafgaandenummer\nummer[#1]}}%
%
% hack needed for chinese and oldstyle in normal tex, will change

\def\maakhetnummer[#1]%
  {\bgroup
   \forceunexpanded % i don't like this hack
   \maakvoorafgaandenummer[#1]%
   \xdef\hetnummer% was \xdef maar dat gaat fout met font switches
     {\voorafgaandenummer\nummer[#1]}%
   \egroup}

\def\preparethenumber#1#2#3% {\??id#1} \number \result 
  {\doifelsevaluenothing{#1\c!scheider}
     {\let\numberseparator\empty
      \let#3#2}
     {\unexpanded\def\numberseparator{\getvalue{#1\c!scheider}}%
      \edef#3{\@EA\separatednumber\@EA{#2}}}}

\def\lossenummer[#1]%
  {\maakhetnummer[#1]%
   \hetnummer}

%\def\huidigenummer[#1]%
%  {%\getvalue{\getvalue{\@@thenumber{#1}\c!zetwijze}}%
%   \getvalue{\getvalue{\@@thenumber{#1}\c!plaats}}%
%     {\dotextprefix{\getvalue{\@@thenumber{#1}\c!tekst}}\lossenummer[#1]}}

\def\huidigenummer[#1]% kan tekst hier weg ? 
  {\dotextprefix{\getvalue{\@@thenumber{#1}\c!tekst}}\lossenummer[#1]}

\def\volgendenummer[#1][#2][#3]%
  {\verhoognummer[#1]%
   \huidigenummer[#1]%
   \rawreference{#2}{#3}{\hetnummer}}

% sectioning

\newcount\nofsections

\let\zerosection \v!tekst
\let\firstsection\empty
\let\lastsection \empty
\let\@@sectie    \empty
\let\@@koppeling \empty

\makecounter{\??se\v!tekst}

\letvalueempty{\??se\v!tekst\c!voor}
\letvalueempty{\??se\v!tekst\c!na  }

\setvalue     {\v!tekst\c!nummer}{0}
\letvalueempty{\v!tekst\s!format}

\letvalueempty{\??sk\v!tekst}
\letvalueempty{\??sk        }

\letvalue{\??by               }\v!tekst
\letvalue{\??by\v!tekst       }\v!tekst
\letvalue{\??by\v!alles       }\v!tekst
\letvalue{\??by\v!per         }\v!tekst
\letvalue{\??by\v!per\v!tekst }\v!tekst
\letvalue{\??by\v!per\v!alles }\v!tekst
\letvalue{\??by\v!per\v!pagina}\v!tekst % see footnotes

%%%%%%%%% old

\def\dostelsectiein[#1][#2]%
  {\getparameters[\??se#1][#2]%
   \doifelsevalue{\??se#1\c!vorigenummer}\v!ja
     {\setvalue{#1\c!nummer}{\@@longsectionnumber{#1}}}
     {\setvalue{#1\c!nummer}{\@@shortsectionnumber{#1}}}}

\def\stelsectiein%
  {\dodoubleargument\dostelsectiein}

%%%%%%%%% new, multilingual

\def\dostelsectiein[#1][#2][#3]%
  {\ifthirdargument
     \getparameters[\??se#1#2][#3]%
   \else
     \getparameters[\??se#1][#2]%
   \fi
   \doifelsevalue{\??se#1\c!vorigenummer}\v!ja
     {\setvalue{#1\c!nummer}{\@@longsectionnumber {#1}}}
     {\setvalue{#1\c!nummer}{\@@shortsectionnumber{#1}}}}

\def\stelsectiein%
  {\dotripleempty\dostelsectiein}

%%%%%%%%%

\def\dokoppelmarkering[#1][#2]%
  {\doifdefinedelse{\??ko#2\c!sectie}
     {\dokoppelmarkering[#1][\getvalue{\??ko#2\c!sectie}]}
     {\def\donexttrackcommando##1%
        {\edef\gekoppeldemarkeringen{\getvalue{\??se##1\c!markering}}%
         \doifelse{##1}{#2}
           {\addtocommalist{#1}\gekoppeldemarkeringen}
           {\removefromcommalist{#1}\gekoppeldemarkeringen}%
         \setevalue{\??se##1\c!markering}{\gekoppeldemarkeringen}%
         \donexttracklevel{##1}}%
      \donexttracklevel{\zerosection}}} % \firstsection

\def\koppelmarkering
  {\dodoubleargument\dokoppelmarkering}

\def\ontkoppelmarkering[#1]%
  {\koppelmarkering[#1][]}

\def\definieersectie[#1]%
  {\doifundefined{\??se#1}
     {\doifelsenothing\firstsection
        {\def\firstsection{#1}%
         \setevalue{\??se#1\c!voor}{\v!tekst}%
         \setevalue{\??se\v!tekst\c!na}{#1}}
        {\setevalue{\??se\commalistelement\c!na}{#1}%
         \setevalue{\??se#1\c!voor}{\lastsection}%
         \setevalue{\??se\lastsection\c!na}{#1}}%
      \advance\nofsections \plusone
      \setevalue{\??se#1\c!niveau}{\the\nofsections}%
      \letvalue{\??se#1\c!na}\empty
      \setvalue{\e!volgende#1}{\@@nextsectionnumber{#1}}%
      \setvalue{#1\c!nummer}{\@@longsectionnumber{#1}}%
      \setvalue{#1\s!format}{\@@longformatnumber{#1}}%
      \setevalue{\??by#1}{#1}%
      \setevalue{\??by\v!per#1}{#1}%
      \makecounter{\??se#1}%
      \edef\lastsection{#1}%
      \setvalue{\??sk#1}{#1}%
      \letvalue{\??se#1\c!markering}\empty
      \stelsectiein[#1][\c!vorigenummer=\v!ja]}}%

\def\previoussection#1{\csname\??se#1\c!voor\endcsname}
\def\nextsection    #1{\csname\??se#1\c!na  \endcsname}

\def\@@setsectionnumber#1#2%
  {\letgvalueempty{\??se#1\s!start}% signal i.p.v. boolean
   \setcounter{\??se#1}{#2}%
   \resetsectioncounters{#1}%
   \checkpagecounter}

\def\@@nextsectionnumber#1%
  {\letgvalueempty{\??se#1\s!start}% signal i.p.v. boolean
   \pluscounter{\??se#1}%
   \resetsectioncounters{#1}%
   \checkpagecounter}

\def\@@sectionvalue#1%       % nog niet overal doorgevoerd
  {\countervalue{\??se#1}}   % zoeken op \??se

% \def\@@sectionconversion#1%
%   {\getvalue{\??cv\getvalue{\??se#1\@@sectieblok\c!conversie}}}

% suited for chinese too:

% \def\@@sectionconversion#1#2% a doublure with \@@shortsectionnumber
%   {\ifnum#2=0 0\else % else troubles with \uchar
%      \@EA\ifx\csname\??se#1\@@sectieblok\c!conversie\endcsname\relax
%        \@EA\ifx\csname\??se#1\c!conversie\endcsname\relax
%          #2%
%        \else
%          \getvalue{\??cv\getvalue{\??se#1\c!conversie}}{#2}%
%        \fi
%      \else
%        \getvalue{\??cv\getvalue{\??se#1\@@sectieblok\c!conversie}}{#2}%
%      \fi
%    \fi}

\def\@@sectionconversion#1#2% a doublure with \@@shortsectionnumber
  {\ifnum#2=0 0\else % else troubles with \uchar
     \@EA\ifx\csname\??se#1\@@sectieblok\c!conversie\endcsname\relax
       \@EA\ifx\csname\??se#1\c!conversie\endcsname\relax
         #2%
       \else
         \convertnumber{\getvalue{\??se#1\c!conversie}}{#2}%
       \fi
     \else
       \convertnumber{\getvalue{\??se#1\@@sectieblok\c!conversie}}{#2}%
     \fi
   \fi}

\def\@@sectionlevel#1%
  {\ifundefined{\??se#1\c!niveau}0\else\getvalue{\??se#1\c!niveau}\fi}

% Omdat een markering kan worden herdefinieerd moeten we
% eerst testen of er wel een keten||afhankelijkheid is.

% \def\doresetsectionmarks#1%
%   {\doifdefined{\??se#1\c!markering} % skip zero level
%      {\fastresetmarkerlist[\getvalue{\??se#1\c!markering}]}%
%    \donexttracklevel{#1}}
% 
% \def\resetsectionmarks#1%
%   {\doifdefinedelse{\??se#1}
%      {\let\donexttrackcommando\doresetsectionmarks
%       \donexttracklevel{#1}}%
%      {\fastresetmarker[\hoofdmarkering{#1}]}} % redundant \hoofdmarkering

% \def\doresetsectionmarks#1%
%   {\ifundefined{\??se#1\c!markering}\else % skip zero level
%      \fastresetmarkerlist[\csname\??se#1\c!markering\endcsname]%
%    \fi
%    \donexttracklevel{#1}}
%
% not sure if the next one is better: 

\def\doresetsectionmarks#1%
  {\ifundefined{\??se#1\c!markering}% skip zero level
     \donexttracklevel{#1}%
   \else
     \fastresetmarkerlist[\csname\??se#1\c!markering\endcsname]%
   \fi}

\def\resetsectionmarks#1%
  {\ifundefined{\??se#1}%
     \fastresetmarker[\hoofdmarkering{#1}]% % redundant \hoofdmarkering 
   \else
     \let\donexttrackcommando\doresetsectionmarks
     \donexttracklevel{#1}%
   \fi}

% packaged:
%
% \def\resetsectioncounters#1%
%   {\def\donexttrackcommando##1%
%      {\resetcounter{\??se##1}%
%       \donexttracklevel{##1}}%
%    \donexttracklevel{#1}}
%
% nicer
% 
% \def\doresetsectioncounters#1%
%   {\resetcounter{\??se#1}%
%    \donexttracklevel{#1}}
%
% obey eigennummer

\def\doresetsectioncounters#1%
  {\resetcounter{\??se#1}%
   \letgvalue{\??se#1\c!eigennummer}\relax
   \donexttracklevel{#1}}

\def\resetsectioncounters % #1
  {\let\donexttrackcommando\doresetsectioncounters
   \donexttracklevel} % #1

% bij checken kan geen prefix worden bekeken, anders vallen
% er titels buiten de inhoudsopgave

% evt ook level gaan opslaan tbv snelle selectie

\def\makesectionformat
  {\edef\sectionformat
     {\@@sectiontype\sectionseparator
      \csname\lastsection\s!format\endcsname}}

\def\dobacktracklevel#1%
  {\doifnot{\previoussection{#1}}\zerosection
     {\dobacktrackcommando{\previoussection{#1}}}}

\def\donexttracklevel#1%
  {\doifnot{#1}\lastsection
     {\donexttrackcommando{\nextsection{#1}}}}

\newif\ifalllevels

\let\currentlevel\empty

\def\dosetcurrentlevel#1%
  {\global\alllevelsfalse
   \xdef\currentlevel{\getvalue{\lastsection\s!format}}}

\def\dosetpreviouslevel#1%
  {\global\alllevelstrue
   \globallet\currentlevel\empty
   \def\dobacktrackcommando##1%
     {\ifnum\countervalue{\??se##1}>\zerocount
        \global\alllevelsfalse
        \xdef\currentlevel{\getvalue{\previoussection{##1}\s!format}}%
      \else
        \dobacktracklevel{##1}%
      \fi}%
   \dobacktrackcommando\lastsection}

\def\dosettextlevel#1%
  {\global\alllevelstrue
   \globallet\currentlevel\empty}

\def\dosetotherlevel#1%
  {\doifdefinedelse{\??ko#1\c!sectie}              % beter alteratief: ook
     {\edef\@@sectie{\getvalue{\??ko#1\c!sectie}}} % hoofdstuk\c!format
     {\edef\@@sectie{#1}}%
   \doifdefinedelse{\??se\@@sectie}
     {\global\alllevelsfalse
      \xdef\currentlevel{\getvalue{\@@sectie\s!format}}}
     {\global\alllevelstrue
      \globallet\currentlevel\empty
      \def\dobacktrackcommando##1%
        {\@EA\ifx\csname\??se##1\c!start\endcsname\relax
           \dobacktracklevel{##1}%
         \else
           \ifnum\countervalue{\??se##1}>\zerocount
             \global\alllevelsfalse
             \xdef\currentlevel{\getvalue{##1\s!format}}%
           \else
             \dobacktracklevel{##1}%
           \fi
         \fi}%
      \dobacktrackcommando\lastsection}}

\def\dosetfilterlevel#1#2% beware: this one is \let
  {\bgroup
   \edef\askedlevel{#1}%
   \edef\askedfilter{#2}%
   \ifx\askedlevel\v!huidige
     \dosetcurrentlevel\askedlevel
   \else\ifx\askedlevel\v!vorige
     \dosetpreviouslevel\askedlevel
   \else\ifx\askedlevel\v!alles
     \global\alllevelstrue  
   \else\ifx\askedlevel\v!tekst
     \global\alllevelstrue  
   \else
     \edef\byaskedlevel{\csname\??by\askedlevel\endcsname}%
     \ifx\byaskedlevel\v!tekst
       \dosettextlevel\askedlevel
     \else
       \dosetotherlevel\askedlevel
     \fi
   \fi\fi\fi\fi
   % experiment 
   \ifx\askedfilter\empty \else 
     \xdef\currentlevel{\currentlevel\sectionseparator\askedfilter}%
   \fi
   \egroup}

\def\dontsetfilterlevel#1%
  {\let\currentlevel\somesavedlevel
   \alllevelsfalse}

\def\honorlocalfilterlevel  % local lists will be real local
  {\let\dosetfilterlevel\dontsetfilterlevel}

% cleaner
%
% \def\doifnextlevelelse[#1::#2]#3#4%
%   {\ifalllevels
%      #3%
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}
%           {\doifinstringelse{=\currentlevel:0}{=:#2:}{#4}{#3}}
%           {#4}}
%        {#4}%
%    \fi}
%
% \def\doifprevlevelelse[#1::#2]#3#4%
%   {\ifalllevels
%      #3%
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}{#3}{#4}}
%        {#4}%
%    \fi}
%
% faster
%
% \def\doifnextlevelelse[#1::#2]%
%   {\ifalllevels
%      \donetrue
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}
%           {\doifinstringelse{=\currentlevel:0}{=:#2:}\donefalse\donetrue}
%           \donefalse}
%        \donefalse
%    \fi
%    \ifdone
%      \expandafter\firstoftwoarguments
%    \else
%      \expandafter\secondoftwoarguments
%    \fi}
%
% \def\doifprevlevelelse[#1::#2]%
%   {\ifalllevels
%      \donetrue
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}\donetrue\donefalse}
%        \donefalse
%    \fi
%    \ifdone
%      \expandafter\firstoftwoarguments
%    \else
%      \expandafter\secondoftwoarguments
%    \fi}
%
% meaner
%
% \stellijstin
%   [hoofdstuk]
%   [na={\startkolommen\plaatslijst[paragraaf]\stopkolommen}]

\def\somesavedlevel{0}

% \def\dosavesomelevel[#1:0:0:0:#2]%
%   {\def\somesavedlevel{:#1}}

% \def\doifnextlevelelse[#1::#2]%
%   {\dosavesomelevel[#2:0:0:0:0]%
%    \ifalllevels
%      \donetrue
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}
%           {\doifinstringelse{=\currentlevel:0}{=:#2:}\donefalse\donetrue}
%           \donefalse}
%        \donefalse
%    \fi
%    \ifdone
%      \expandafter\firstoftwoarguments
%    \else
%      \expandafter\secondoftwoarguments
%    \fi}
%
% \def\doifprevlevelelse[#1::#2]%
%   {\dosavesomelevel[#2:0:0:0:0]%
%    \ifalllevels
%      \donetrue
%    \else
%      \doifelse{\@@sectiontype}{#1}
%        {\doifinstringelse{=\currentlevel:}{=:#2:}\donetrue\donefalse}
%        \donefalse
%    \fi
%    \ifdone
%      \expandafter\firstoftwoarguments
%    \else
%      \expandafter\secondoftwoarguments
%    \fi}
%
% again faster:

% \def\doifnextlevelelse[#1::#2]% beware: this one is \let
%  {\dosavesomelevel[#2:0:0:0:0]%
%   \ifalllevels
%     \donetrue
%   \else\ifnum\@@sectiontype=#1
%     \def\levelstring{=:#2:}%
%     \doifincsnameelse{=\currentlevel:}\levelstring
%       {\doifincsnameelse{=\currentlevel:0}\levelstring\donefalse\donetrue}
%       \donefalse
%   \else
%     \donefalse
%   \fi\fi
%   \ifdone
%     \expandafter\firstoftwoarguments
%   \else
%     \expandafter\secondoftwoarguments
%   \fi}
%
%\def\doifprevlevelelse[#1::#2]% beware: this one is \let
%  {\dosavesomelevel[#2:0:0:0:0]%
%   \ifalllevels
%     \donetrue
%   \else\ifnum\@@sectiontype=#1
%     \doifinstringelse{=\currentlevel:}{=:#2:}\donetrue\donefalse
%   \else
%     \donefalse
%   \fi\fi
%   \ifdone
%     \expandafter\firstoftwoarguments
%   \else
%     \expandafter\secondoftwoarguments
%   \fi}
%
% \let\doiftoclevelelse\doifnextlevelelse
% \let\doifreglevelelse\doifprevlevelelse
% \let\doifblklevelelse\doifprevlevelelse
%
% we want to be able to overload them globally 

% This will be reimplmented
%
% {nn}{xx}{yy}
%
% -> \scan{..}{..}{0} met 0 als sentinel 

\def\doifnextlevelelse[#1]% !! this one is \let / uti seperator --
  {\edef\somesavedlevel{\sectionseparator\@@filterlevelpart[#1]}%
   \ifalllevels
     \donetrue
   \else\ifnum\@@sectiontype=\@@filterblockpart[#1]\relax
     \edef\levelstring{=\sectionseparator\@@filternumberpart[#1]\sectionseparator}%
     \doifincsnameelse{=\currentlevel\sectionseparator}\levelstring
       {\doifincsnameelse{=\currentlevel\sectionseparator0}\levelstring
          \donefalse
          \donetrue}
       \donefalse
   \else
     \donefalse
   \fi\fi
   \ifdone
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\def\doifprevlevelelse[#1]% !! this one is \let / uti seperator --
  {\edef\somesavedlevel{\sectionseparator\@@filterlevelpart[#1]}%
   \ifalllevels
     \donetrue
   \else\ifnum\@@sectiontype=\@@filterblockpart[#1]\relax
     \doifinstringelse
       {=\currentlevel\sectionseparator}
       {=\sectionseparator\@@filternumberpart[#1]\sectionseparator}
       \donetrue\donefalse
   \else
     \donefalse
   \fi\fi
   \ifdone
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\def\doiftoclevelelse{\doifnextlevelelse}
\def\doifreglevelelse{\doifprevlevelelse}
\def\doifblklevelelse{\doifprevlevelelse}

\def\@@longformatnumber#1%
  {\csname\previoussection{#1}\s!format\endcsname
   \sectionseparator
   \@@shortsectionnumber{#1}}

% \def\@@longsectionnumber#1%
%   {\ifnum\countervalue{\??se\previoussection{#1}}>\zerocount
%      \csname\previoussection{#1}\c!nummer\endcsname.%
%    \fi
%    \@@shortsectionnumber{#1}}

\newif\ifreversesectionnumbers % todo: key/val

\def\@@longsectionnumber#1%
  {\ifreversesectionnumbers
     \@@shortsectionnumber{#1}%
     \ifnum\countervalue{\??se\previoussection{#1}}>\zerocount
       .\csname\previoussection{#1}\c!nummer\endcsname
     \fi
   \else
     \ifnum\countervalue{\??se\previoussection{#1}}>\zerocount
       \csname\previoussection{#1}\c!nummer\endcsname.%
     \fi
     \@@shortsectionnumber{#1}%
   \fi}


% \def\@@shortsectionnumber#1%
%   {\@EA\ifx\csname\??se#1\@@sectieblok\c!conversie\endcsname\relax
%      \@@sectionvalue{#1}%
%    \else
%      \@@sectionconversion{#1}{\@@sectionvalue{#1}}%
%    \fi}
%
% obey eigennummer 

\def\@@shortsectionnumber#1%
  {\@EA\ifx\csname\??se#1\c!eigennummer\endcsname\relax
     \@EA\ifx\csname\??se#1\@@sectieblok\c!conversie\endcsname\relax
       \@EA\ifx\csname\??se#1\c!conversie\endcsname\relax
         \@@sectionvalue{#1}%
       \else
         \@@sectionconversion{#1}{\@@sectionvalue{#1}}%
       \fi
     \else
       \@@sectionconversion{#1}{\@@sectionvalue{#1}}%
     \fi
   \else
     \csname\??se#1\c!eigennummer\endcsname
   \fi}

% suited for chinese too:

\def\@@shortsectionnumber#1%
  {\@EA\ifx\csname\??se#1\@@sectieblok\c!conversie\endcsname\relax
     \@EA\ifx\csname\??se#1\c!conversie\endcsname\relax
       \@@sectionvalue{#1}%
     \else
       \@@sectionconversion{#1}{\@@sectionvalue{#1}}%
     \fi
   \else
     \@@sectionconversion{#1}{\@@sectionvalue{#1}}%
   \fi}

\def\dosetlocalsectieblok#1#2#3% new \edef's 
  {\edef\@@sectiontype  {#1}%
   \edef\@@sectieblok   {#2}%
   \edef\@@sectieblokken{#3}}

% beware, the \resetsectionmarks generates some nodes that 
% will result in an additional last page, which needs to be 
% captured at the end 

\def\doaroundsectieblok#1%
  {\doifvaluesomething{\??sb#1\c!pagina}
     {\ExpandFirstAfter\pagina[\getvalue{\??sb#1\c!pagina}]}%
   \resetsectioncounters\zerosection % was firstsection
   \resetsectionmarks\zerosection}

\def\dostartsectieblok#1#2%
  {\begingroup
   \doaroundsectieblok{#1}%            % going to a new page or so
   \getvalue{\??sb#1}%                 % set name of section block
   \getsectieblokomgeving{#1}%         % special settings, grouped
  %\expandafter\csname#2true\endcsname % obsolete
   \setsystemmode{#1}%                 % can be used in conditionals
   \getvalue{\??sb\@@sectieblok\c!voor}% this one is not to be moved!
   \showmessage\m!structures1\@@sectieblokken}

\def\dostopsectieblok
  {\showmessage\m!structures2\@@sectieblokken
   \getvalue{\??sb\@@sectieblok\c!na}% don't move
   \doaroundsectieblok\@@sectieblok
   \endgroup}

\def\dostelsectieblokin[#1]% [#2]
  {\getparameters[\??sb#1]} 

\def\stelsectieblokin
  {\dodoubleargument\dostelsectieblokin}

\long\def\setsectieblokomgeving#1#2%
  {\long\setvalue{\??sb\s!do#1}{\do{#2}}}

\def\getsectieblokomgeving#1%
  {\let\do\firstofoneargument\getvalue{\??sb\s!do#1}}

\setvalue{\e!start\e!sectieblokomgeving}%
  {\dosingleargument\dostartsectieblokomgeving}

\def\dostartsectieblokomgeving[#1]% evt \pushendofline \popendofline
  {\long\def\do##1##2{\setsectieblokomgeving{#1}{##1##2}}%
   \grabuntil{\e!stop\e!sectieblokomgeving}{\getvalue{\??sb\s!do#1}}}

%D \starttypen
%D \startsectionblockenvironment[frontpart]
%D   \setuppagenumbering[conversion=romannumerals]
%D \stopsectionblockenvironment
%D
%D \startsectionblockenvironment[bodypart]
%D   \setuppagenumber[number=1]
%D \stopsectionblockenvironment
%D
%D \startsectionblockenvironment[frontpart]
%D   \setuppagenumbering[conversion=character]
%D \stopsectionblockenvironment
%D
%D \starttext
%D   \startfrontmatter \chapter{test} \stopfrontmatter
%D   \startbodymatter  \chapter{test} \stopbodymatter
%D   \startappendices  \chapter{test} \stopappendices
%D \stoptext
%D \stoptypen

% We used to use the first char as id, but a counter is
% better, because in english we get a name clash.

\newcounter\currentsectionblock

\def\dodefinieersectieblok[#1][#2][#3]%
  {\getparameters
     [\??sb#1]
     [\c!nummer=\v!ja,
      \c!pagina=\v!rechts, % anders worden marks te vroeg gereset !
      %\c!voor=,
      %\c!na=,
      #3]%
   \expandafter\newif\csname if#2\endcsname % better a mode 
   \doglobal\increment\currentsectionblock
   \setsectieblokomgeving{#1}{}%
   \setevalue{\??sb#1}%
     {\noexpand\dosetlocalsectieblok{\currentsectionblock}{#1}{#2}}%
   \setvalue{\e!start#2}%
     {\dostartsectieblok{#1}{#2}}%
   \setvalue{\e!stop#2}%
     {\dostopsectieblok}}

\def\definieersectieblok
  {\dotripleargument\dodefinieersectieblok}

\def\sectiebloklabel#1#2%
  {\@EA\ifx\csname\??ko#1\@@sectieblok\c!label\endcsname\relax
     \labeltexts{#1}{#2}%
   \else
     \labeltexts{\getvalue{\??ko#1\@@sectieblok\c!label}}{#2}%
   \fi}

\dosetlocalsectieblok{2}{\v!hoofdtekst}{\v!hoofdteksten} % hm, dirty

\def\setsectiontype[#1]%
  {\getvalue{\??sb#1}}

\def\writesection#1#2#3% #3 -> \asciititle
  {\bgroup
   \edef\!!stringa{#1}%
   \@EA\writestatus\@EA
     {\!!stringa}
     {\ifsectienummer#2\else(#2)\fi\normalspace\asciititle}%
   \egroup}

\def\@@koniveau{1}        \def\kopniveau{\@@koniveau}

\def\dohandelpaginaafAA#1%
  {\ifnum\lastpenalty>0
     \global\paginageblokkeerdtrue
   \fi}

\def\dohandelpaginaafAB#1%
  {\flushsidefloats
   \getvalue{\??ko#1\c!voor}%
   %\witruimte vervangen door \noindent elders
   \relax
   \ifpaginageblokkeerd
     \global\paginageblokkeerdfalse
   \else
     \!!countb\getvalue{\??se\@@sectie\c!niveau}\relax
     \ifnum\!!countb>\@@koniveau\relax
       \!!counta20000
       \multiply\!!countb 500
       \advance\!!counta \!!countb
       \dosomebreak{\penalty\!!counta}%
     \else
       \dosomebreak{\allowbreak}%
     \fi
   \fi
   \xdef\@@koniveau{\getvalue{\??se\@@sectie\c!niveau}}}

\def\dohandelpaginaafBB#1#2#3%
%  {\doifinset{\getvalue{\??tk#2\v!tekst\c!status}}{\v!normaal,\v!start}
  {\doifinset{\getvalue{\??tk#2\c!status}}{\v!normaal,\v!start}
     {\doifvaluesomething{\??ko#1#3}
        {\setuplayouttext[#2][\c!status=\getvalue{\??ko#1#3}]}}}

\def\dohandelpaginaafB#1%
  {\doifinset{\getvalue{\??ko#1\c!pagina}}{\v!ja,\v!rechts,\v!links}
     {\def\resetcurrentsectionmarks% toegevoegd, zie \pagina
        {\resetsectionmarks{\previoussection\@@sectie}}%
      \pagina[\getvalue{\??ko#1\c!pagina}]%
      \dohandelpaginaafBB{#1}\v!hoofd\c!hoofd
      \dohandelpaginaafBB{#1}\v!tekst\c!tekst
      \dohandelpaginaafBB{#1}\v!voet \c!voet}}

\def\dohandelpaginaafX#1% zie doordefinieren / boven
  {\bgroup
   \!!countb\@@koniveau
   \advance\!!countb #1
   \multiply\!!countb 500
   \!!counta20000
   \advance\!!counta \!!countb
   \dosomebreak{\penalty\!!counta}%
   \egroup}

\def\handelpaginaaf#1%
  {\dohandelpaginaafAA{#1}%
   \ifnum\countervalue{\??se\previoussection\@@sectie}>\zerocount\relax
     \ifnum\countervalue{\??se\@@sectie}>\zerocount
       \dohandelpaginaafB{#1}%
     \else
       \doifnotvalue{\??ko#1\c!doorgaan}\v!ja
         {\dohandelpaginaafB{#1}}%
     \fi
   \else
     \dohandelpaginaafB{#1}%
   \fi
   \dohandelpaginaafAB{#1}}

\def\handelpaginaafC#1%
  {\xdef\@@koniveau{\getvalue{\??se\@@sectie\c!niveau}}%
   \nobreak}

%\def\dolocalheadsetup#1%  koppeling met standaard kopcommando / engels
%  {\forgetall
%   \doifvaluesomething{\??ko#1\c!uitlijnen}
%     {\ExpandFirstAfter\steluitlijnenin[\getvalue{\??ko#1\c!uitlijnen}]}%
%   \doifvaluesomething{\??ko#1\c!tolerantie}
%     {\ExpandFirstAfter\steltolerantiein[\getvalue{\??ko#1\c!tolerantie}]}%
%   \def\\{\crlf\strut\ignorespaces}}

\def\dolocalheadsetup#1%  koppeling met standaard kopcommando / engels
  {\forgetall         %  traag dus ...
   \doifvaluesomething{\??ko#1\c!uitlijnen} % wordt al expanded in spa
     {\expanded{\steluitlijnenin[\getvalue{\??ko#1\c!uitlijnen}]}}%
   \doifvaluesomething{\??ko#1\c!tolerantie} % wordt al expanded in spa
     {\expanded{\steltolerantiein[\getvalue{\??ko#1\c!tolerantie}]}}%
   \def\\{\crlf\strut\ignorespaces}}

\def\localkopsetup{\localheadsetup} % kan tzt weg 

\newif\ifplaatskop
\newif\iflegekop
\newif\ifnaarlijst
\newif\ifverhoognummer
\newif\ifkopnummer

\def\setsectieenkoppeling#1%
  {\edef\@@koppeling{\getvalue{\??ko#1\c!koppeling}}%
   \edef\@@sectie{\getvalue{\??ko#1\c!sectie}}%
   \doifnothing\@@koppeling
     {\edef\@@koppeling{#1}}%
   \doifnothing\@@sectie
     {\edef\@@sectie{\getvalue{\??ko\@@koppeling\c!sectie}}}}

\newif\ifkopprefix

% \handelpaginaaf komt het eerst omdat eventueel
% subpaginanummers moeten worden afgehandeld. Vervolgens
% worden de nummers opgehoogd en referenties geset, dan
% volgt de kop en tot slot de worden de marks en de prefix
% geset.

% \hoofdstuk {tekst}
% \hoofdstuk tekst
% \hoofdstuk <niets>

\def\dofinalsectionnumber
  {\ifundefined{\@@sectie\c!nummer}\else
     \ifsomeheadconversion
       \@@shortsectionnumber\@@sectie
     \else
       \getvalue{\@@sectie\c!nummer}%
     \fi
   \fi}

\def\findsectionnumber#1#2#3% class file title / uti seperator --
  {\begingroup
   \setsectieenkoppeling{#1}%
   \xdef\foundsectionnumber{1}%
   \def\dolijstelement##1##2##3##4##5##6%
     {\doif{##1}{#1}
        {\ConvertConstantAfter\doif{##4}{#3}
           {\global\utilitydonetrue
            \scratchcounter=0\getvalue{\??se\@@sectie\c!niveau}%
            %
            %\advance\scratchcounter 2
            %\@EA\def\@EA\do\@EA####\@EA1\sectionseparator####2]%
            %  {\advance\scratchcounter -1
            %   \ifcase\scratchcounter
            %     \xdef\foundsectionnumber{####1}%
            %   \else
            %     \do####2]%
            %   \fi}%
            %\do##5]}}}%
            %
            \def\do####1\relax % :/- clean 
              {\advance\scratchcounter \minusone
               \ifcase\scratchcounter
                 \xdef\foundsectionnumber{\@@filterheadpart[####1]}%
               \else
                 \@EAEAEA\do\@@filtertailpart[####1]\relax
               \fi}%
            \@EA\do\@@filternumberpart[##5]\relax}}}%
   \setbox0\vbox
     {\doutilities{#1}{#2}{#1}\relax\relax}%
   \endgroup
   \doifnumberelse{\foundsectionnumber}
     {\doif{\foundsectionnumber}{0}{\xdef\foundsectionnumber{1}}}
     {\xdef\foundsectionnumber{1}}% an appendix or so
   \stelkopnummerin[#1][\foundsectionnumber]%
   \stelkopnummerin[#1][-1]}

\newif\ifsomeheadconversion

% \def\setsomeheadconversion#1#2%
%    {\someheadconversionfalse
%     \doifelsevalue{\??ko#1\c!eigennummer}\v!ja
%       {\def\someheadconversion{#2}}
%       {\bepaalkopnummer[#1]%
%        \@EA\ifx\csname\??se\@@sectie\@@sectieblok\c!kopconversie\endcsname\relax
%          \@EA\ifx\csname\??se\@@sectie\c!kopconversie\endcsname\relax
%            \def\someheadconversion{#2}%
%          \else
%            \@EA\ifx\csname\??se\@@sectie\c!kopconversie\endcsname\empty
%              \def\someheadconversion{#2}%
%            \else
%              \someheadconversiontrue
%              \def\someheadconversion%
%                {\fullsectionnumber{#1}{\getvalue{\??se\@@sectie\c!kopconversie}}{#2}}%
%            \fi
%          \fi
%        \else
%          \@EA\ifx\csname\??se\@@sectie\@@sectieblok\c!kopconversie\endcsname\empty
%            \def\someheadconversion{#2}%
%          \else
%            \someheadconversiontrue
%            \def\someheadconversion%
%              {\fullsectionnumber{#1}{\getvalue{\??se\@@sectie\@@sectieblok\c!kopconversie}}{#2}}%
%          \fi
%        \fi}}
% 
% deal with eigennummer

\def\setsomeheadconversion#1#2%
   {\someheadconversionfalse
    \doifelsevalue{\??ko#1\c!eigennummer}\v!ja
      {\setgvalue{\??se\@@sectie\c!eigennummer}{#2}%
       \def\someheadconversion{#2}}
      {\letgvalue{\??se\@@sectie\c!eigennummer}\relax
       \bepaalkopnummer[#1]%
       \@EA\ifx\csname\??se\@@sectie\@@sectieblok\c!kopconversie\endcsname\relax
         \@EA\ifx\csname\??se\@@sectie\c!kopconversie\endcsname\relax
           \def\someheadconversion{#2}%
         \else
           \@EA\ifx\csname\??se\@@sectie\c!kopconversie\endcsname\empty
             \def\someheadconversion{#2}%
           \else
             \someheadconversiontrue
             \def\someheadconversion%
               {\fullsectionnumber{#1}{\getvalue{\??se\@@sectie\c!kopconversie}}{#2}}%
           \fi
         \fi
       \else
         \@EA\ifx\csname\??se\@@sectie\@@sectieblok\c!kopconversie\endcsname\empty
           \def\someheadconversion{#2}%
         \else
           \someheadconversiontrue
           \def\someheadconversion%
             {\fullsectionnumber{#1}{\getvalue{\??se\@@sectie\@@sectieblok\c!kopconversie}}{#2}}%
         \fi
       \fi}}

            \def   \writtenfullsectionnumber      {\string\fullsectionnumber}
            \def   \ignoredfullsectionnumber#1#2#3{#3}
            \let    \storedfullsectionnumber       \relax 
%           \def\expandablefullsectionnumber#1#2#3{#3}
%\unexpanded\def   \naturalfullsectionnumber#1#2#3{\sectiebloklabel{#1}{\getvalue{\??cv#2}{#3}}}
%\unexpanded\def   \limitedfullsectionnumber#1#2#3{\getvalue{\??cv#2}{#3}}

% under test:
%
%           \def\expandablefullsectionnumber#1#2#3{\getvalue{\??cv#2}{#3}}

\def\expandablefullsectionnumber#1#2#3%
  {\convertnumber{#2}{#3}}

\unexpanded\def\naturalfullsectionnumber#1#2#3%
  {\sectiebloklabel{#1}{\convertnumber{#2}{#3}}}
 
\unexpanded\def\limitedfullsectionnumber#1#2#3%
  {\convertnumber{#2}{#3}}

\def\setfullsectionnumber#1%
  {\doifelsevalue{#1\c!kopconversie}\v!ja
     {\doifelsevalue{#1\c!koplabel}\v!ja
        {\let\fullsectionnumber\naturalfullsectionnumber}
        {\let\fullsectionnumber\limitedfullsectionnumber}}
     {\let\fullsectionnumber\ignoredfullsectionnumber}}

\let\fullsectionnumber\limitedfullsectionnumber

% \dodododosomekop IS NON GROUPED, SO WE NEED TO RESTORE !!!!
%
% dit kan dus beter \everyaroundhead zijn

\let\currentheadnumber\empty
\let\currentheadtext  \empty

\def\dodosomekop#1[#2]#3% [ref] {title}
  {\doifelsevalue{\??ko#1\c!eigennummer}\v!ja
     {\doquadruplegroupempty\dododosomekop{#1}{#2}{#3}}
     {\fourthargumentfalse  \dododosomekop{#1}{#2}{#3}{}}}

\def\dododosomekop#1#2#3#4% [ref] {own} {title}
  {\iffourthargument
     \def\next{\dodododosomekop{#1}[#2]{#3}{#4}}%
   \else
     \def\next{\dodododosomekop{#1}[#2]{\finalsectionnumber}{#3}}%
   \fi
   \next}

% pas met \ExpandFirstAfter op bij twee||taligheid

\ifx\dohandleheadnumber\undefined
  \let\dohandleheadnumber\firstofoneargument 
\fi

\unexpanded\def\\{\space}

\def\emptyheadcorrection % experimental, should work 
  {\iflegekop            % well with na=\blanko
     \vskip-\lineheight
     \dosomebreak\nobreak
     \kern\zeropoint
     \prevdepth\strutdepth
   \fi}

\let\localkopprefix\empty

\def\dodododosomekop#1[#2]#3#4% [ref] {number} {title} 
  {\def\currenthead{#1}%
\let\finalsectionnumber\dofinalsectionnumber % overloaded ungrouped -)
   \unexpanded\def\\{\space}%         
   \def\numberseparator{\getvalue{\??ko\currenthead\c!scheider}}%
   \flushingcolumnfloatsfalse % {number} can be \finalsectionnumber    
   \someheadconversionfalse
   \let\fullsectionnumber\limitedfullsectionnumber
   \setsectieenkoppeling{#1}%
   \doifelsevalue{\??ko#1\c!prefix}{}
     {\kopprefixfalse}
     {\kopprefixtrue}%
   \ifkopprefix
     \doifelsevalue{\??ko#1\c!prefix}{+}
       {\doifelsenothing{#2}
          {\def\localkopprefix{+}}
          {\def\localkopprefix{#2}}} % eigenlijk alleen eerste
       {\edef\localkoprefix{\getvalue{\??ko#1\c!prefix}}}%
   \else
     \let\localkoprefix\empty
   \fi
   \doifelsevalue{\??ko#1\c!plaatskop}\v!ja
     \plaatskoptrue\plaatskopfalse
   \processaction
     [\getvalue{\??ko#1\c!plaatskop}]
     [  \v!ja=>\plaatskoptrue \legekopfalse,
      \v!leeg=>\plaatskoptrue \legekoptrue,
       \v!nee=>\plaatskopfalse\legekoptrue]%
   \naarlijstfalse
   \processaction
     [\getvalue{\??ko#1\c!verhoognummer}]
     [     \v!ja=>\verhoognummertrue,
          \v!nee=>\verhoognummerfalse,
        \v!lijst=>\verhoognummerfalse
                  \naarlijsttrue,
      \s!unknown=>{\ifx\currentproduct\empty
                     \findsectionnumber{#1}\commalistelement{#4}%
                   \fi
                   \verhoognummertrue}]%
   \edef\numberheaddistance   {\getvalue{\??ko#1\c!afstand}}%
   \edef\numberheadalternative{\getvalue{\??ko#1\c!variant}}%
   \dostelkopvariantin[\numberheadalternative]%
   \ifsectienummer
     \doifelsevalue{\??sb\@@sectieblok\c!nummer}\v!ja
       {\doifelsevalue{\??ko#1\c!nummer}\v!ja
          \kopnummertrue 
          \kopnummerfalse}
       {\kopnummerfalse}%
   \else
     \kopnummerfalse
   \fi
   \convertexpanded{\??ko#1}{#4}\asciititle
   %
   \gdef\currentheadtext{#4}% scheelt args
   \globallet\currentheadnumber\empty
   %
   \ifverhoognummer
     \ifplaatskop
       \checknexthead\handelpaginaaf{#1}%
       \setsectieenkoppeling{#1}% can be changed when [voor=\somehead{..}...]
       \ifkopprefix
        %\setupreferencing[\c!prefix=-]%
         \setupreferenceprefix[-]%
       \fi
       \getvalue{\e!volgende\@@sectie}%
       \ifkopnummer
         \setsomeheadconversion{#1}{#3}%
         \let\fullsectionnumber\expandablefullsectionnumber
         \xdef\currentheadnumber{\someheadconversion}%
         \getvalue{\??ko#1\c!tussen}%
         \ifsomeheadconversion
           \let\fullsectionnumber\naturalfullsectionnumber
           \doplaatskopnummertekst
             {#1}
             {\setsectionlistreference{\@@sectie}{#1}%
              \soortpagina[\@@koppeling]%
              \let\fullsectionnumber\writtenfullsectionnumber
              \rawreference\s!sec{#2}{{\someheadconversion}{\asciititle}}%
              \resetsectionmarks\@@sectie
              \setlistparameter\@@koppeling\c!expansie{\getvalue{\??ko#1\c!expansie}}%
              \let\fullsectionnumber\writtenfullsectionnumber
              \doschrijfnaarlijst{\@@koppeling}{\someheadconversion}{#4}{\v!kop}}%
             {\dohandleheadnumber\someheadconversion}% handle is new 
             {#4}
             {\marking[#1]{#4}%
              \let\fullsectionnumber\storedfullsectionnumber
              \expanded{\marking[#1\v!nummer]{\someheadconversion}}}%
           \let\fullsectionnumber\ignoredfullsectionnumber
           \writesection{#1}{\someheadconversion}{#4}%
         \else
           \doplaatskopnummertekst
             {#1}
             {\setsectionlistreference{\@@sectie}{#1}%
              \soortpagina[\@@koppeling]%
              \rawreference\s!sec{#2}{{#3}{\asciititle}}%
              \resetsectionmarks\@@sectie
              \setlistparameter\@@koppeling\c!expansie{\getvalue{\??ko#1\c!expansie}}%
              \doschrijfnaarlijst{\@@koppeling}{#3}{#4}{\v!kop}}
             {\sectiebloklabel{#1}{\dohandleheadnumber{#3}}}% handle is new 
             {#4}
             {\marking[#1]{#4}%
              \doifelsevalue{\??ko#1\c!eigennummer}\v!ja % rommelig omdat
                {\edef\finalsectionnumber{#3}} % #3 al is toegekend
                {\bepaalkopnummer[#1]}% migreert naar 3e argument
              \expanded{\marking[#1\v!nummer]{\finalsectionnumber}}}%
           \writesection{#1}{#3}{#4}%
         \fi
       \else
         \getvalue{\??ko#1\c!tussen}%
         \doplaatskoptekst
           {#1}
           {\setsectionlistreference{\@@sectie}{#1}%
            \soortpagina[\@@koppeling]%
            \rawreference\s!sec{#2}{{#3}{\asciititle}}%
            \resetsectionmarks\@@sectie
            \setlistparameter\@@koppeling\c!expansie{\getvalue{\??ko#1\c!expansie}}%
            \doschrijfnaarlijst{\@@koppeling}{}{#4}{\v!kop}}
           {#4}
           {\marking[#1]{#4}%
            \doifelsevalue{\??ko#1\c!eigennummer}\v!ja % brrr 
              {\edef\finalsectionnumber{#3}}
              {\bepaalkopnummer[#1]}%
% todo : geen markering (leeg maken) 
            \expanded{\marking[#1\v!nummer]{\finalsectionnumber}}}%
         \writesection{#1}{-}{#4}%
       \fi
       \ifkopprefix
         \setupreferenceprefix[\localkopprefix]%
       \fi
       \dosomebreak\nobreak
       \ifdisplaysectionhead
         \emptyheadcorrection
         \getvalue{\??ko#1\c!na}%
       \fi
     \else
       % Whatever future tex's will do with nodes, 
       % we assume a node here, because other \c!na=\blanko
       % will fail! See 'prikkels'
       %
       % so, maybe we need an explicit \kern 
       % 
       % do nothing / should be vbox to 0pt 
       % 
       \checknexthead\dohandelpaginaafB{#1}% toegevoegd ivm subpaginanr / tug sheets
       \setsectieenkoppeling{#1}% can be changed when [voor=\somehead{..}...]
       \ifkopprefix
         \setupreferenceprefix[-]%
       \fi
       \getvalue{\e!volgende\@@sectie}%
       \ifkopnummer
         \setsomeheadconversion{#1}{#3}%
         \let\fullsectionnumber\expandablefullsectionnumber
         \xdef\currentheadnumber{\someheadconversion}%
       \fi
       \getvalue{\??ko#1\c!tussen}% documenteren, is enige hook 
       \bgroup
       \setsectionlistreference{\@@sectie}{#1}%
       \resetsectionmarks\@@sectie
       \marking[#1]{#4}%
       \doifelsevalue{\??ko#1\c!eigennummer}\v!ja
         {\edef\finalsectionnumber{#3}}
         {\bepaalkopnummer[#1]}%
       \expanded{\marking[#1\v!nummer]{\finalsectionnumber}}%
       \soortpagina[\@@koppeling]%
%       \bgroup
       \setlistparameter\@@koppeling\c!expansie{\getvalue{\??ko#1\c!expansie}}%
       \ifkopnummer
         \rawreference\s!sec{#2}{{#3}{\asciititle}}%
         \doschrijfnaarlijst{\@@koppeling}{#3}{#4}{\v!kop}%
         \writesection{#1}{#3}{#4}%
       \else
         \rawreference\s!sec{#2}{{#3}{\asciititle}}%
         \doschrijfnaarlijst{\@@koppeling}{}{#4}{\v!kop}%
         \writesection{#1}{-}{#4}%
       \fi
       \egroup
       \ifkopprefix
         \setupreferenceprefix[\localkopprefix]%
       \fi
     \fi
   \else
     % todo : ref prefix 
     \ifplaatskop
       \checknexthead\handelpaginaaf{#1}%
       \setsectieenkoppeling{#1}% can be changed when [voor=\somehead{..}...]
       \getvalue{\??ko#1\c!tussen}%
       \doplaatskoptekst
         {#1}
         {\forcesectiontolist{#1}{#4}%
          \rawreference\s!sec{#2}{{#3}{\asciititle}}} % #3 ?
         {#4}
        %{}% new:
         {\marking[#1]{#4}%
          \marking[#1\v!nummer]{}}%
       \writesection{#1}{-}{#4}%
       \dosomebreak\nobreak
       \ifdisplaysectionhead
         \emptyheadcorrection
         \getvalue{\??ko#1\c!na}%
       \fi
     \else
       % do nothing / should be vbox to 0pt 
       \checknexthead\handelpaginaaf{#1}%
       \setsectieenkoppeling{#1}% can be changed when [voor=\somehead{..}...]
       \getvalue{\??ko#1\c!tussen}%
       \forcesectiontolist{#1}{#4}%
       \rawreference\s!sec{#2}{{#3}{\asciititle}}% #3 ? 
       \marking[#1]{#4}%
       \marking[#1\v!nummer]{}%
       \writesection{#1}{-}{#4}%
     \fi
   \fi
   \flushingcolumnfloatstrue
   \someheadconversionfalse
   \let\fullsectionnumber\limitedfullsectionnumber
   \ifdisplaysectionhead\else\expandafter\GotoPar\fi}

\def\forcesectiontolist#1#2%
  {\ifnaarlijst
     % we need to make sure that there is a number set (non 
     % zero) else the list mechanism cannot determine the 
     % level 
     \bgroup
     \stelkopnummerin[#1][+1]% traag, wordt \getvalue{\c!volgende...}
     \setlistparameter\@@koppeling\c!expansie{\getvalue{\??ko#1\c!expansie}}%
     \doschrijfnaarlijst{\@@koppeling}{}{#2}{\v!kop}%
     \stelkopnummerin[#1][-1]% traag, wordt \getvalue{\c!vorige...}
     \egroup
   \fi}

\let\previoussectionformat\empty
\let\currentsectionformat \empty

\let\updatelistreferences \relax
\let\updatedlistreferences\empty

\def\setsectionlistreference#1#2%
  {\ifnum\countervalue{\??se\previoussection{#1}}>0\relax
     \xdef\previoussectionformat{\@@longformatnumber{\previoussection{#1}}}%
   \else
     \globallet\previoussectionformat\empty
   \fi
   \xdef\currentsectionformat{\@@longformatnumber{#1}}}

\def\startlistreferences#1%
  {\thisissomeinternal{\s!lst}{#1\currentsectionformat}%
   \setxvalue{\s!lst:#1}{\realfolio}% to be sure
   \setxvalue{\s!lst:#1\currentsectionformat}{\realfolio}%
   \setxvalue{\e!vorigelokale#1}{\s!lst:#1\previoussectionformat}%
   \setxvalue{\e!huidigelokale#1}{\s!lst:#1\currentsectionformat}%
   \doifelse{\currentsectionformat}{}
     {\setglobalcrossreference
        {\e!vorige#1}{}{\realfolio}{}}
%
     {\setglobalsystemreference\rt!list
        {\e!vorige#1}{\getvalue{\e!vorigelokale#1}}}%
%
%         {\definereference[\e!vorige#1][\getvalue{\e!vorigelokale#1}]%
%
   \def\stoplistreferences{\dostoplistreferences}}

\def\dostoplistreferences#1%
  {\iflijstgeplaatst
     \addtocommalist{#1}\updatedlistreferences               % nog global (\doglobal)
     \globallet\updatedlistreferences\updatedlistreferences % een noodverbandje
     \gdef\updatelistreferences%
       {\def\docommando####1%
%
          {\setglobalsystemreference\rt!list
             {\e!vorige####1}{\getvalue{\e!huidigelokale####1}}}%
%
%         {\definereference[\e!vorige####1][\getvalue{\e!huidigelokale####1}]%
%
        \processcommacommand[\updatedlistreferences]\docommando
        \globallet\updatelistreferences\relax
        \globallet\updatedlistreferences\empty}%
   \fi}

\def\stoplistreferences%
  {\gobbleoneargument}

% \prevdepth\dp\strutbox is belangrijk, vergelijk naast elkaar:
%
% \onderwerp{test} \input tufte
% \onderwerp{test} \strut \input tufte
% \onderwerp{test} \plaatslijst[...]

\newif\ifheadnumbercontent % niet meer wijzigen / wordt mode

% todo: kap

% \def\doplaatskoptekst#1#2#3#4%
%   {\beginheadplacement{#1}%
%    \iflegekop
%      \setbox0\ifvertical\vbox\else\hbox\fi to \zeropoint
%        {\headnumbercontentfalse
%         \resetsystemmode\v!sectienummer
%         #2}%
%      \makestrutofbox0
%    \else
%      \setbox0=\ifvertical\vbox\else\hbox\fi % \vhbox
%        {\headnumbercontentfalse
%         \resetsystemmode\v!sectienummer
%         \getvalue{\??ko#1\c!commando}
%           {} % no number
%           {\doattributes
%              {\??ko#1}\c!letter\c!kleur
%              {\doattributes
%                 {\??ko#1}\c!tekstletter\c!tekstkleur
%                 {\dontconvertfont
%                  \ifdisplaysectionhead
%                    \stelinterliniein
%                  \else
%                    \stelspatieringin
%                  \fi
%                  #2%
%                  \getvalue{\??ko#1\c!voorcommando}%
%                  \ifdisplaysectionhead
%                    \getvalue{\??ko#1\c!tekstcommando}%
%                      {\setstrut\begstrut#3\endstrut}%
%                    \xdef\localheaddepth{\the\dp\strutbox}%
%                  \else
%                    \getvalue{\??ko#1\c!tekstcommando}{#3}%
%                  \fi
%                  \getvalue{\??ko#1\c!nacommando}%
%                  \ifdisplaysectionhead\endgraf\fi}}}}%  
%    \fi  
%    \endheadplacement{#1}{#4}}
% 
% \def\doplaatskopnummertekst#1#2#3#4#5%
%   {\beginheadplacement{#1}%
%    \iflegekop
%      \setbox0\ifvertical\vbox\else\hbox\fi to \zeropoint
%        {\doiftextelse{#3}
%           {\setsystemmode  \v!sectienummer\headnumbercontenttrue }
%           {\resetsystemmode\v!sectienummer\headnumbercontentfalse}%
%         #2}%
%      \makestrutofbox0
%    \else
%      \setbox0=\ifvertical\vbox\else\hbox\fi % \vhbox
%        {\doiftextelse{#3}
%           {\setsystemmode  \v!sectienummer\headnumbercontenttrue }
%           {\resetsystemmode\v!sectienummer\headnumbercontentfalse}%
%         \getvalue{\??ko#1\c!commando}%
%           {\doattributes{\??ko#1}\c!letter\c!kleur
%              {\doattributes{\??ko#1}\c!nummerletter\c!nummerkleur
%                 {\getvalue{\??ko#1\c!voorcommando}%
%                  \ifdisplaysectionhead
%                    \getvalue{\??ko#1\c!nummercommando}%
%                      {\setstrut\begstrut#3\endstrut}%
%                  \else
%                    \getvalue{\??ko#1\c!nummercommando}{#3}%
%                  \fi}}}
%           {\doattributes{\??ko#1}\c!letter\c!kleur
%              {\doattributes{\??ko#1}\c!tekstletter\c!tekstkleur
%                 {\dontconvertfont
%                  \ifdisplaysectionhead
%                    \stelinterliniein
%                  \else
%                    \stelspatieringin
%                  \fi
%                  #2%
%                  \ifdisplaysectionhead
%                    \getvalue{\??ko#1\c!tekstcommando}%
%                      {\setstrut\begstrut#4\endstrut}%
%                    \xdef\localheaddepth{\the\dp\strutbox}%
%                  \else
%                    \getvalue{\??ko#1\c!tekstcommando}{#4}%
%                  \fi
%                  \getvalue{\??ko#1\c!nacommando}%
%                  \ifdisplaysectionhead\endgraf\fi}}}}%
%    \fi
%    \endheadplacement{#1}{#5}}

% to be documented: \placeheadtext \placeheadnumber

\unexpanded\def\placeheadtext
  {\doquintupleempty\doplaceheadtextornumber
     [\c!tekstletter][\c!tekstkleur][\empty]}

\unexpanded\def\placeheadnumber
  {\doquintupleempty\doplaceheadtextornumber
     [\c!nummerletter][\c!nummerkleur][\v!nummer]}

\def\doplaceheadtextornumber[#1][#2][#3][#4][#5]%
  {\bgroup
   \edef\@@sectie{\??ko\iffifthargument#5\else#4\fi}%
   \dostartattributes\@@sectie\c!letter\c!kleur\empty
     \dontconvertfont
     \dostartattributes\@@sectie{#1}{#2}\empty
       \stelinterliniein % \setupinterlinespace
       \begstrut\haalmarkering[\hoofdmarkering{#4#3}]\endstrut
       \endgraf
     \dostopattributes
   \dostopattributes
   \egroup}

\def\doplaatskoptekst#1#2#3#4%
  {\beginheadplacement{#1}%
   \iflegekop
     \setbox0\ifvertical\vbox\else\hbox\fi to \zeropoint
       {\headnumbercontentfalse
        \resetsystemmode\v!sectienummer
        #2}%
     \makestrutofbox0
   \else
     \setbox0=\ifvertical\vbox\else\hbox\fi % \vhbox
       {\headnumbercontentfalse
        \resetsystemmode\v!sectienummer
        % outerside font determines distance
        \dosetfontattribute{\??ko#1}\c!letter
        % but we don't want color to influence user commands 
        \getvalue{\??ko#1\c!commando}        
          {} % no number
          {\dostartattributes{\??ko#1}\c!letter\c!kleur\empty 
             \dostartattributes{\??ko#1}\c!tekstletter\c!tekstkleur\empty
               \dontconvertfont
               \ifdisplaysectionhead
                 \stelinterliniein
               \else
                 \stelspatieringin
               \fi
               #2%
               \getvalue{\??ko#1\c!voorcommando}%
               \ifdisplaysectionhead
                 \getvalue{\??ko#1\c!tekstcommando}%
                   {\setstrut\begstrut#3\endstrut}%
                 \xdef\localheaddepth{\the\dp\strutbox}%
               \else
                 \getvalue{\??ko#1\c!tekstcommando}{#3}%
               \fi
               \getvalue{\??ko#1\c!nacommando}%
               \ifdisplaysectionhead\endgraf\fi
             \dostopattributes
           \dostopattributes}}%  
   \fi  
   \endheadplacement{#1}{#4}}

\def\doplaatskopnummertekst#1#2#3#4#5%
  {\beginheadplacement{#1}%
   \iflegekop
     \setbox0\ifvertical\vbox\else\hbox\fi to \zeropoint
       {\doiftextelse{#3}
          {\setsystemmode  \v!sectienummer\headnumbercontenttrue }
          {\resetsystemmode\v!sectienummer\headnumbercontentfalse}%
        #2}%
     \makestrutofbox0
   \else
     \setbox0=\ifvertical\vbox\else\hbox\fi % \vhbox
       {\doiftextelse{#3}
          {\setsystemmode  \v!sectienummer\headnumbercontenttrue }
          {\resetsystemmode\v!sectienummer\headnumbercontentfalse}%
        % outerside font determines distance
        \dosetfontattribute{\??ko#1}\c!letter
        % but we don't want color to influence user commands 
        \getvalue{\??ko#1\c!commando}%
          {\dostartattributes{\??ko#1}\c!letter\c!kleur\empty
             \dostartattributes{\??ko#1}\c!nummerletter\c!nummerkleur\empty
               \getvalue{\??ko#1\c!voorcommando}%
               \ifdisplaysectionhead
                 \getvalue{\??ko#1\c!nummercommando}%
                   {\setstrut\begstrut#3\endstrut}%
               \else
                 \getvalue{\??ko#1\c!nummercommando}{#3}%
               \fi
             \dostopattributes
           \dostopattributes}
          {\dostartattributes{\??ko#1}\c!letter\c!kleur\empty
             \dostartattributes{\??ko#1}\c!tekstletter\c!tekstkleur\empty
               \dontconvertfont
               \ifdisplaysectionhead
                 \stelinterliniein
               \else
                 \stelspatieringin
               \fi
               #2%
               \ifdisplaysectionhead
                 \getvalue{\??ko#1\c!tekstcommando}%
                   {\setstrut\begstrut#4\endstrut}%
                 \xdef\localheaddepth{\the\dp\strutbox}%
               \else
                 \getvalue{\??ko#1\c!tekstcommando}{#4}%
               \fi
               \getvalue{\??ko#1\c!nacommando}%
               \ifdisplaysectionhead\endgraf\fi
            \dostopattributes
          \dostopattributes}}%
   \fi
   \endheadplacement{#1}{#5}}

\newsignal\headsignal
\let\headlastlinewidth\!!zeropoint
\newif\ifcontinuoushead

% \def\beginheadplacement#1%
%   {\bgroup
%    \gdef\localheaddepth{\dp\strutbox}%
%    \everypar{}% needed indeed
%    \noindent  % ipv \witruimte elders, na \forgetall !
%    \bgroup
%    \forgetall % now we may forget everything
%   %\showcomposition
%    \mindermeldingen
%    \postponefootnotes
%    \iflocation\ifdisplaysectionhead\else\noninterferingmarks\fi\fi
%    %\setupinteraction
%    %  [\c!letter=,
%    %   \c!kleur=,
%    %   \c!contrastkleur=]%
%    \resetinteractionparameter\c!letter
%    \resetinteractionparameter\c!kleur
%    \resetinteractionparameter\c!contrastkleur
%    \strictouterreferencestrue % tzt instelling
%    \def\localheadsetup%
%      {\dolocalheadsetup{#1}}%
%    \startsynchronisatie}

% \def\beginheadplacement#1%
%   {\bgroup
%    \setsystemmode{#1}% to be documented                 
% \ifgridsnapping\iftracegridsnapping\showstruts\fi\fi
%    \gdef\localheaddepth{\dp\strutbox}%
%    \everypar\emptytoks % needed indeed
%    \noindent           % ipv \witruimte elders, na \forgetall !
%    \bgroup
%    \doifelsevalue{\??ko#1\c!titeluitlijnen}\v!ja % new 
%      {\leftskip 1\leftskip
%       \rightskip1\rightskip
%       \edef\next{\leftskip\the\leftskip\rightskip\the\rightskip}%
%       \xdef\localheadskip{\the\leftskip}%
%       \expandafter\forgetall\next % now we may forget everything
%       \setlocalhsize\hsize\localhsize}
%      {\globallet\localheadskip\!!zeropoint
%       \forgetall}%
%    \mindermeldingen
%    \postponefootnotes
%    \iflocation\ifdisplaysectionhead\else\noninterferingmarks\fi\fi
%    \resetinteractionparameter\c!letter
%    \resetinteractionparameter\c!kleur
%    \resetinteractionparameter\c!contrastkleur
%    \strictouterreferencestrue % tzt instelling
%    \def\localheadsetup%
%      {\dolocalheadsetup{#1}}%
%    \startsynchronisatie}

\def\beginheadplacement#1%
  {\bgroup
   \setsystemmode{#1}% to be documented                 
   \ifgridsnapping\iftracegridsnapping\showstruts\fi\fi
   \gdef\localheaddepth{\dp\strutbox}%
   \everypar\emptytoks % needed indeed
   \noindent           % ipv \witruimte elders, na \forgetall !
   \bgroup
   \doifelsevalue{\??ko#1\c!titeluitlijnen}\v!ja % new 
     {\skip0 1\leftskip
      \skip2 1\rightskip
      \xdef\localheadskip{\the\skip0}%
      \forgetall
      \leftskip\skip0 
      \rightskip\skip2
      \setlocalhsize\hsize\localhsize
      \forgetbothskips}
     {\globallet\localheadskip\!!zeropoint
      \forgetall}%
   \mindermeldingen
   \postponefootnotes
   \iflocation\ifdisplaysectionhead\else\noninterferingmarks\fi\fi
   \resetinteractionparameter\c!letter
   \resetinteractionparameter\c!kleur
   \resetinteractionparameter\c!contrastkleur
   \strictouterreferencestrue % tzt instelling
   \def\localheadsetup%
     {\dolocalheadsetup{#1}}%
   \startsynchronisatie}


\def\endheadplacement#1#2%
  {\doifelsevalue{\??rf#1\c!status}\v!start
     {\doifvaluenothing{\??ko#1\c!file}{\autocrossdocumentfalse}}
     {\autocrossdocumentfalse}%
   % no message needed here, should be a proper switch
   % \let\unknownreference\gobbleoneargument
   \ifdisplaysectionhead
     \let\headlastlinewidth\!!zeropoint
     \snaptogrid[\getvalue{\??ko#1\c!grid}]\hbox
       {\hskip\localheadskip
        \iflocation
          \ifautocrossdocument
            \doifreferencefoundelse{\getvalue{\??ko#1\c!file}::#1}
              {\edef\currentinnerreference{\s!aut:\currenttextreference}% stored in
               \gotoouterlocation{}{\box0}}                             % text slot
              {\hbox{\box0}}%
          \else
            \hbox{\box0}%
          \fi
        \else
          \hbox{\box0}%
        \fi}%
     \doflushnotes % new, not really needed
     \endgraf
     \nointerlineskip
     \dosomebreak\nobreak
     #2%
   \else
     \strut
     \doflushnotes % new, here since we're in par mode
     \iflocation
       \ifautocrossdocument
         \hhboxindent=\ifcontinuoushead\headlastlinewidth\else\zeropoint\fi
         \unhhbox0\with{\naarbox{\box\hhbox}[\getvalue{\??ko#1\c!file}::#1]}%
         \advance\lasthhboxwidth by \numberheaddistance
         \xdef\headlastlinewidth{\the\lasthhboxwidth}%
       \else
         \unhbox0
         \globallet\headlastlinewidth\!!zeropoint
       \fi
     \else
       \unhbox0
       \globallet\headlastlinewidth\!!zeropoint
     \fi
     #2%
     \dimen0=\numberheaddistance
     \hskip\dimen0 \!!plus \dimen0 \!!minus .25\dimen0
     \hskip\headsignal\ignorespaces
   \fi
   \ifdisplaysectionhead
     \ifgridsnapping % important, font related depth, see comment
       \prevdepth\dp\strutbox
     \else
       \prevdepth\localheaddepth
     \fi
   \fi
   \stopsynchronisatie
   \egroup
   \egroup
   \ifdisplaysectionhead
     \doifvalue{\??ko#1\c!springvolgendein}\v!nee\noindentation
   \else
     \nonoindentation % recently added, was a bug
   \fi}

\def\checknexthead#1#2% nog optioneel
  {\ifhmode
     \scratchcounter=\lastpenalty\unpenalty % no beauty in this
     \ifdim\lastskip=\headsignal
       \handelpaginaafC{#1}%
       \global\continuousheadtrue
     \else
       \penalty\scratchcounter
       \global\continuousheadfalse
       #1{#2}%
     \fi
   \else
     \global\continuousheadfalse
     #1{#2}%
   \fi}

\def\dostelkopnummerin[#1][#2#3]%
  {\bgroup
   \setsectieenkoppeling{#1}%
   \doifinstringelse{#2}{+-}
     {\doifelsenothing{#3}
        {\@@nextsectionnumber\@@sectie}
        {\!!counta=#2#3\relax
         \advance\!!counta \@@sectionvalue\@@sectie
         \@@setsectionnumber\@@sectie\!!counta}}
     {\@@setsectionnumber\@@sectie{#2#3}}%
   \egroup}

\def\stelkopnummerin
  {\dodoubleargument\dostelkopnummerin}

\def\huidigekopnummer{0}

\def\bepaalkopnummer[#1]%
  {\bgroup
   \setsectieenkoppeling{#1}%
   \xdef\huidigekopnummer{\@@sectionvalue{\@@sectie}}%
   \egroup}

\def\complexkopnummer[#1]%
  {\bgroup
   \edef\huidigekopnummer{#1}%
   \doifinsetelse{-}{#1} % br undocumented
     {\removefromcommalist{-}\huidigekopnummer % br 
      \setsectieenkoppeling\huidigekopnummer
      \stelsectiein[\@@sectie][\c!vorigenummer=\v!nee]}%
     {\setsectieenkoppeling\huidigekopnummer}%
   \xdef\huidigekopnummer{\@@sectionvalue{\@@sectie}}%
   \doifnot{\huidigekopnummer}{0}{\finalsectionnumber}%
   \egroup}

\def\simplekopnummer
  {\huidigekopnummer}

\definecomplexorsimple\kopnummer

\def\alinea%
  {\par}

% nice testcase 
%
% \setupheads[aligntitle=yes]
% 
% \startnarrower
%   \subject{\dorecurse{100}{x }}
%   \section{\dorecurse{100}{x }}
%   \input tufte \par
%   \setupheads[alternative=inmargin]
%   \subject{\dorecurse{100}{x }}
%   \section{\dorecurse{100}{x }}
%   \input tufte \par
% \stopnarrower

\def\plaatskopalinea#1#2%
  {\vbox
     {\localheadsetup
      \begstrut\ifheadnumbercontent#1\hskip\numberheaddistance\fi#2}}

\def\plaatskopnormaal#1#2%
  {\ifheadnumbercontent
     \setbox0=\hbox{{#1}\hskip\numberheaddistance}%
     \vbox
       {\localheadsetup
        \hangindent 1\wd0
        \hangafter 1
        \noindent
        \unhbox0    %  don't use \strut's here!
        #2}%
   \else
     \vbox
       {\localheadsetup\noindent#2}%
   \fi}

% \def\plaatskopinmarge#1#2%
%   {\vbox
%      {\localheadsetup
%       \begstrut            % but use one \strut here!
%       \ifheadnumbercontent
%         \llap{\hbox to 5em{\hfill{#1}\hskip\linkermargeafstand}}%
%       \fi
%       {#2}}}

%\def\plaatskopinmarge#1#2%
%  {\vbox
%     {\scratchdimen\linkermargeafstand
%      \advance\scratchdimen\leftskip
%      \edef\plaatskopinmarge{\the\scratchdimen}% re-use saves hash entry 
%      \localheadsetup
%      \begstrut            % but use one \strut here!
%      \ifheadnumbercontent
%        \llap{\hbox to 5em{\hfill{#1}\hskip\plaatskopinmarge}}%
%      \fi
%      {#2}}}

\def\plaatskopinmarge#1#2%
  {\vbox
     {\localheadsetup
      \begstrut % use one \strut here!
      \ifheadnumbercontent
        \llap{\hbox to 5em{\hfill{#1}%
          \hskip\localheadskip\hskip\linkermargeafstand}}%
      \fi
      {#2}}}

\def\plaatskopmidden#1#2%
  {\vbox
     {\localheadsetup
      \veryraggedcenter
      \let\\\endgraf
      \let\crlf\endgraf
      \ifheadnumbercontent\strut#1\par\fi\begstrut#2}}

\def\plaatskopintekst#1#2%
  {\bgroup
   \localheadsetup % no stretch in distance
   \ifheadnumbercontent{#1}\kern\numberheaddistance\fi{\begstrut#2}%
   \egroup}

% default   == instellingen
% koppeling == koppen, breaks, marks, enz.
% sectie    == nummering

\let\@@kolijst=\empty

\def\dodefinieerkop[#1][#2]%   % don't preset prefix to much
  {\presetlabeltext[#1=]%
   \getparameters
     [\??ko#1]
     [\c!nummerletter=\getvalue{\??ko#1\c!letter},
      \c!tekstletter=\getvalue{\??ko#1\c!letter},
      \c!nummerkleur=\getvalue{\??ko#1\c!kleur},
      \c!tekstkleur=\getvalue{\??ko#1\c!kleur}]%
%   \ConvertToConstant\doifinstringelse{=}{#2}
   \doifassignmentelse{#2}
     {\getparameters
        [\??ko#1]
        [\c!sectie=\getvalue{\??ko\getvalue{\??ko#1\c!koppeling}\c!sectie},
         \c!default=,
         \c!koppeling=,
         \c!prefix=,
         \c!voor=,
         \c!na=,
         \c!afstand=\!!zeropoint,
         \c!pagina=,
         \c!hoofd=,
         \c!tekst=,
         \c!voet=,
         \c!letter=,
         \c!nummercommando=,
         \c!tekstcommando=,
         \c!eigennummer=\v!nee,
         \c!nummer=\v!ja,
         \c!kleur=,
         \c!doorgaan=\v!ja,
         \c!plaatskop=\v!ja,
         \c!verhoognummer=\v!ja,
         \c!variant=\@@kovariant,
         \c!commando=\@@plaatskop,
         \c!scheider=\@@koscheider,
         \c!uitlijnen=\@@kouitlijnen,
         \c!titeluitlijnen=\@@kotiteluitlijnen,
         \c!tolerantie=\@@kotolerantie,
         \c!springvolgendein=\@@kospringvolgendein,
         \c!file=,
         \c!expansie=,
         \c!grid=,
         #2]%
      \ConvertToConstant\doifnot{#1}{\getvalue{\??ko#1\c!default}}
        {\doifsomething{\getvalue{\??ko#1\c!default}}
           {\copyparameters
              [\??ko#1][\??ko\getvalue{\??ko#1\c!default}]
              [\c!voor,\c!na,\c!commando,\c!file,\c!pagina,\c!doorgaan,
               \c!hoofd,\c!tekst,\c!voet,\c!scheider,
               \c!nummer,\c!eigennummer,\c!plaatskop,\c!verhoognummer,
               \c!letter,\c!kleur,\c!afstand,\c!variant,\c!springvolgendein,
              %\c!nummerletter,\c!tekstletter,
              %\c!expansie, % why njet ?
               \c!uitlijnen,\c!titeluitlijnen,\c!tolerantie,\c!grid,
               \c!nummercommando,\c!tekstcommando]}}%
      \getparameters[\??ko#1][#2]%
      \doifsomething{\getvalue{\??ko#1\c!sectie}}
        {\doifelsemarking{#1}% \doifundefined{\??mk#1}
           {}% marking #1 already defined 
           {\definieermarkering[#1]%
            \koppelmarkering[#1][\getvalue{\??ko#1\c!sectie}]%
            \definieermarkering[#1\v!nummer]%
% klopt dit wel ? 
            \koppelmarkering[#1\v!nummer][\getvalue{\??ko#1\c!sectie}]}}%
%            \koppelmarkering[#1\v!nummer][\getvalue{\??ko#1\c!sectie}\v!nummer]}}%
      \doifundefined{\??li#1}{\definieerlijst[#1]}}
     {\ConvertToConstant\doifelse{#1}{#2}
        {\doifundefined{\??li#1}{\definieerlijst[#1]}}
        {\copyparameters
           [\??ko#1][\??ko#2]
           [\c!niveau,\c!sectie,\c!koppeling,\c!prefix,
            \c!voor,\c!na,\c!commando,\c!file,\c!pagina,\c!doorgaan,
            \c!scheider,
            \c!hoofd,\c!tekst,\c!voet,
            \c!nummer,\c!eigennummer,\c!plaatskop,\c!verhoognummer,
            \c!letter,\c!kleur,\c!afstand,\c!variant,\c!springvolgendein,
           %\c!nummerletter,\c!tekstletter,
           %\c!expansie, % why njet ?
            \c!uitlijnen,\c!titeluitlijnen,\c!tolerantie,\c!grid,
            \c!nummercommando,\c!tekstcommando]%
\getparameters[\??ko#1][\c!expansie=]% iig een value, rather fuzzy
         \definieermarkering[#1][#2]%
         \definieermarkering[#1\v!nummer][#2\v!nummer]%
         \doifundefined{\??li#1}{\definieerlijst[#1][#2]}}}%
   \addtocommalist{#1}\@@kolijst
   \setevalue{\??sk#1}%
     {\getvalue{\??ko#1\c!koppeling}}%
   \setevalue{\??by#1}%
     {\getvalue{\??ko#1\c!sectie}}%
   \setevalue{\??by\v!per#1}%
     {\getvalue{\??ko#1\c!sectie}}%
   \setvalue{#1}%
     {\dodoubleempty\dosomekop[#1]}}

\def\definieerkop%
  {\dodoubleemptywithset\dodefinieerkop}

\def\dosomekop[#1][#2]%
  {\dowithpargument{\dodosomekop{#1}[#2]}}

\def\dostelkopin[#1][#2]%
  {\getparameters[\??ko#1][#2]%
   % The next check prevents hard to trace problems. I once
   % set \c!commando to nothing and (quite natural) got the
   % wrong references etc. The whole bunch should be boxed!
   \expandafter\convertcommand\csname\??ko#1\c!commando\endcsname\to\ascii
   \doifnothing\ascii{\setvalue{\??ko#1\c!commando}{\@@plaatskop}}}

\def\stelkopin
  {\dodoubleargumentwithset\dostelkopin}

\newif\ifsectienummer       \sectienummertrue
\newif\ifdisplaysectionhead \displaysectionheadtrue

\def\@@plaatskop{\plaatskopnormaal}

\def\dostelkopvariantin[#1]% will be installable ! ! ! ! ! !
  {\displaysectionheadtrue
   \processaction
     [#1]
     [ \v!normaal=>\def\@@plaatskop{\plaatskopnormaal},
        \v!midden=>\def\@@plaatskop{\plaatskopmidden},
         \v!marge=>\def\@@plaatskop{\plaatskopinmarge},
       \v!inmarge=>\def\@@plaatskop{\plaatskopinmarge},
         \v!tekst=>\def\@@plaatskop{\plaatskopintekst}\displaysectionheadfalse,
        \v!alinea=>\def\@@plaatskop{\plaatskopalinea},
       \s!unknown=>\def\@@plaatskop{\plaatskopnormaal}]}

\def\dostelkoppenin[#1]%
  {\getparameters[\??ko][#1]%
   \doifelse{\@@kosectienummer}\v!ja\sectienummertrue\sectienummerfalse
   \dostelkopvariantin[\@@kovariant]} % not really needed here 

\def\stelkoppenin
  {\dosingleargument\dostelkoppenin}

\def\systemsuppliedchapter {\getvalue{\v!hoofdstuk}}
\def\systemsuppliedtitle   {\getvalue{\v!titel}}

% a left over

\def\complexbijlage[#1]#2%
  {\pagina[\v!rechts]
   \setuppagenumbering[\c!status=\v!stop]
   \systemsuppliedchapter[#1]{#2}
   \pagina[\v!rechts]
   \setuppagenumbering[\c!status=\v!start]
   \setuppagenumbering[\c!nummer=1]}

\setvalue{\v!bijlage}%
  {\complexorsimpleempty\bijlage}

\stelkoppenin
  [\c!variant=\v!normaal,
   \c!sectienummer=\v!ja,
   \c!scheider=.,
   \c!limittext=\v!ja,
   \c!uitlijnen=,
   \c!titeluitlijnen=,
   \c!tolerantie=,
   \c!springvolgendein=\v!nee,
   \c!commando=]

\definieersectieblok [\v!hoofdtekst] [\v!hoofdteksten] [\c!nummer=\v!ja]
\definieersectieblok [\v!bijlage]    [\v!bijlagen]     [\c!nummer=\v!ja]
\definieersectieblok [\v!inleiding]  [\v!inleidingen]  [\c!nummer=\v!nee]
\definieersectieblok [\v!uitleiding] [\v!uitleidingen] [\c!nummer=\v!nee]

\definieersectie[\v!sectionlevel-1]   % deel
\definieersectie[\v!sectionlevel-2]   % hoofdstuk
\definieersectie[\v!sectionlevel-3]   % paragraaf
\definieersectie[\v!sectionlevel-4]   % subparagraaf
\definieersectie[\v!sectionlevel-5]   % subsubparagraaf
\definieersectie[\v!sectionlevel-6]   % subsubsubparagraaf
\definieersectie[\v!sectionlevel-7]   % subsubsubsubparagraaf

% \c!eigennummer ook hier?

\definieerkop
  [\v!deel]
  [\c!sectie=\v!sectionlevel-1]

\definieerkop
  [\v!hoofdstuk]
  [\c!sectie=\v!sectionlevel-2]

\definieerkop
  [\v!paragraaf]
  [\c!sectie=\v!sectionlevel-3]

\definieerkop
  [\v!sub\v!paragraaf]
  [\c!sectie=\v!sectionlevel-4,
   \c!default=\v!paragraaf]

\definieerkop
  [\v!sub\v!sub\v!paragraaf]
  [\c!sectie=\v!sectionlevel-5,
  %\c!default=\v!paragraaf]
   \c!default=\v!sub\v!paragraaf]             % nieuw

\definieerkop
  [\v!sub\v!sub\v!sub\v!paragraaf]
  [\c!sectie=\v!sectionlevel-6,
  %\c!default=\v!paragraaf]
   \c!default=\v!sub\v!sub\v!paragraaf]       % nieuw

\definieerkop
  [\v!sub\v!sub\v!sub\v!sub\v!paragraaf]
  [\c!sectie=\v!sectionlevel-7,
  %\c!default=\v!paragraaf]
   \c!default=\v!sub\v!sub\v!sub\v!paragraaf] % nieuw

\definieerkop
  [\v!titel]
  [\c!koppeling=\v!hoofdstuk,
   \c!default=\v!hoofdstuk,
   \c!verhoognummer=\v!nee]

\definieerkop
  [\v!onderwerp]
  [\c!koppeling=\v!paragraaf,
   \c!default=\v!paragraaf,
   \c!verhoognummer=\v!nee]

\definieerkop
  [\v!sub\v!onderwerp]
  [\c!koppeling=\v!sub\v!paragraaf,
   \c!default=\v!sub\v!paragraaf,
   \c!verhoognummer=\v!nee]

\definieerkop
  [\v!sub\v!sub\v!onderwerp]
  [\c!koppeling=\v!sub\v!sub\v!paragraaf,
   \c!default=\v!sub\v!sub\v!paragraaf,
   \c!verhoognummer=\v!nee]

\definieerkop
  [\v!sub\v!sub\v!sub\v!onderwerp]
  [\c!koppeling=\v!sub\v!sub\v!sub\v!paragraaf,
   \c!default=\v!sub\v!sub\v!sub\v!paragraaf,
   \c!verhoognummer=\v!nee]

\definieerkop
  [\v!sub\v!sub\v!sub\v!sub\v!onderwerp]
  [\c!koppeling=\v!sub\v!sub\v!sub\v!sub\v!paragraaf,
   \c!default=\v!sub\v!sub\v!sub\v!sub\v!paragraaf,
   \c!verhoognummer=\v!nee]

\stelsectiein
  [\v!sectionlevel-2]
  [\v!bijlage\c!conversie=\v!Letter,
   \c!vorigenummer=\v!nee]

\stelkopin
  [\v!deel]
  [\c!plaatskop=\v!nee]

\stelkopin
  [\v!hoofdstuk]
  [\v!bijlage\c!label=\v!bijlage,
   \v!hoofdtekst\c!label=\v!hoofdstuk]             %   bijlageconversie=\Character

\stelkopin
  [\v!paragraaf]
  [\v!bijlage\c!label=\v!paragraaf,
   \v!hoofdtekst\c!label=\v!paragraaf]             %   bijlageconversie=\Character

\stelkopin
  [\v!sub\v!paragraaf]
  [\v!bijlage\c!label=\v!sub\v!paragraaf,
   \v!hoofdtekst\c!label=\v!sub\v!paragraaf]       %   bijlageconversie=\Character

\stelkopin
  [\v!sub\v!sub\v!paragraaf]
  [\v!bijlage\c!label=\v!sub\v!sub\v!paragraaf,
   \v!hoofdtekst\c!label=\v!sub\v!sub\v!paragraaf] %   bijlageconversie=\Character

\stelkopin
  [\v!deel,\v!hoofdstuk]
  [%\c!uitlijnen=,
   %\c!springvolgendein=\v!nee, 
   \c!doorgaan=\v!nee,
   \c!pagina=\v!rechts,
   \c!hoofd=,
   \c!letter=\tfc,
   \c!afstand=.75em,
   \c!voor={\blanko[2*\v!groot]},
   \c!na={\blanko[2*\v!groot]}]

\stelkopin
  [\v!paragraaf]
  [%\c!uitlijnen=,
   %\c!springvolgendein=\v!nee,
   \c!letter=\tfa,
   \c!afstand=.75em,
   \c!voor={\blanko[2*\v!groot]},
   \c!na=\blanko]

\stelkopin                 % nieuw
  [\v!sub\v!paragraaf]
  [\c!pagina=]

\definieersamengesteldelijst
  [\v!inhoud]
  [\v!deel,
   \v!hoofdstuk,
   \v!paragraaf,
   \v!sub\v!paragraaf,
   \v!sub\v!sub\v!paragraaf,
   \v!sub\v!sub\v!sub\v!paragraaf,
   \v!sub\v!sub\v!sub\v!sub\v!paragraaf]
  [\c!niveau=\v!sub\v!sub\v!sub\v!sub\v!paragraaf,
   \c!criterium=\v!lokaal]

\stellijstin
  [\v!deel]
  [\c!voor={\blanko\pagina[\v!voorkeur]},
   \c!na=\blanko,
   \c!label=\v!ja,
   \c!scheider=:,
   \c!afstand=1em]

\stellijstin
  [\v!hoofdstuk]
  [\c!voor={\blanko\pagina[\v!voorkeur]},
   \c!na=]

\stellijstin [\v!deel]                              [\c!breedte=0em]
\stellijstin [\v!hoofdstuk]                         [\c!breedte=2em]
\stellijstin [\v!paragraaf]                         [\c!breedte=3em]
\stellijstin [\v!sub\v!paragraaf]                   [\c!breedte=4em]
\stellijstin [\v!sub\v!sub\v!paragraaf]             [\c!breedte=5em]
\stellijstin [\v!sub\v!sub\v!sub\v!paragraaf]       [\c!breedte=6em]
\stellijstin [\v!sub\v!sub\v!sub\v!sub\v!paragraaf] [\c!breedte=7em]

% hm

\setuppagenumbering % na instellen hoofdteksten !
  [\c!variant=\v!enkelzijdig,
   \c!plaats={\v!hoofd,\v!midden},
   \c!conversie=\v!cijfers,
   \c!breedte=, % in geval van \v!kantlijn 
   \c!links=,
   \c!rechts=,
   \c!wijze=\v!per\v!deel,
   \c!tekst=,
   \v!hoofdstuk\v!nummer=\v!nee, % v
   \v!deel\v!nummer=\v!ja,       % v
   \c!nummerscheider=--,
   \c!tekstscheider=\tfskip,
   \c!status=\v!start,
   \c!commando=,
   \c!strut=\v!ja, % nieuw
   \c!letter=, % \v!normaal, % empty, otherwise conflict
   \c!kleur=]

\protect \endinput

