%D \module
%D   [       file=core-reg,
%D        version=1999.12.27,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Register Management,
%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 / Register Management}

\newif \ifautoregisterhack % for the moment a private hack

% for the moment \e!register ipv #1 (evt nog \v!register)

\unprotect 

%D Isolated but still indocumented. 

%  Formaat tex-utility-input-file <jobname.tui>:
%
%    i e {tag} {loc} {pure} {entry+..} {p:c:p:sp:ssp=>page} {realpage}
%    i s {tag} {loc} {pure} {entry+..} {other entry}
%
%  In plaats van + kan een & worden gebruikt. Ook kan als
%  eerste karakter worden opgegeven wat de scheider is.
%
%  \index                {entry}
%  \index[key]           {entry}
%  \index[pageclass::]   {entry}
%  \index[pageclass::key]{entry}
%  \index                {textclass::entry}
%  \index[key]           {textclass::entry}
%  \index[pageclass::]   {textclass::entry}
%  \index[pageclass::key]{textclass::entry}
%
%  Deze file wordt met het Perl script TeXUtil omgezet in
%  een in te lezen file met de commando's:
%
%    \registerentrya {tag} {ingang}
%    \registerentryb {tag} {subingang}
%    \registerentryc {tag} {subsubingang}
%
%    \registerpage   {tag} {pag,txt} {volgnummer} {paginanummer} {volgnummer}
%
%    \registersee    {tag} {pag,txt} {andere ingang}
%
%    \registerentry  {tag} {letter}

\def\dostelregisterin[#1][#2][#3]%
  {\ifthirdargument
     \def\dodostelregisterin##1%
       {\getparameters[\??id##1#2][#3]%
        \preparepaginaprefix{\??id##1}}%
   \else
     \def\dodostelregisterin##1%
       {\getparameters[\??id##1][#2]%
        \doifvalue{\??id##1\c!koppeling}{\v!ja}
          {\appendtoks\koppelregister[##1][#2]\to\everystarttext}%
        \preparepaginaprefix{\??id##1}}%
   \fi
   \processcommalist[#1]\dodostelregisterin}

\def\stelregisterin%
  {\dotripleempty\dostelregisterin}

\def\getlastregisterentry#1%
  {\def\docommando##1%
     {\def\!!stringa{##1}}%
   \processseparatedlist[#1][+]\docommando
   \!!stringa}

\def\doprocesspageregister[#1]#2%
  {\begingroup
   \thisisnextinternal{\s!ind}%
   \ifduplicate\getlastregisterentry{#2}\fi
   \convertexpanded{\??id\currentregister}{#2}\asciiregisterentry
   \makesectionformat
   \edef\schrijfwegnaarregister%
     {\writeutility%
        {r \ifcase\registerpagestatus\space\or e \or f \or t \fi
         {\currentregister} %
         {\nextinternalreference} %
         {#1} %
         {\asciiregisterentry} %
         {\sectionformat::\noexpand\pagenumber} %
         {\noexpand\realfolio}}}%
   \schrijfwegnaarregister
   \getfirstcharacter\currentregister
   \registerinfo{> \firstcharacter}{#2}%
   \endgroup}

\def\complexdoregister[#1]#2%
  {\doprocesspageregister[#1]{#2}%
   \ifvmode\nobreak\fi
   \GotoPar}

%\def\doregister#1%
%  {\def\currentregister{#1}%
%   \complexorsimpleempty\doregister}

\def\doregister#1%
  {\chardef\registerpagestatus=1
   \def\currentregister{#1}%
   \complexorsimpleempty\doregister}

\def\startregister%
  {\dodoubleargument\dostartregister}

\def\dostartregister[#1][#2]#3%
  {\chardef\registerpagestatus=2
   \def\currentregister{#1}%
   \setgvalue{\??id#1\??id#2}{\dodostopregister[#1][#2]{#3}}%
   \complexdoregister[#2]{#3}}

\def\stopregister%
  {\dodoubleargument\dostopregister}

\def\dostopregister[#1][#2]%
  {\getvalue{\??id#1\??id#2}\setgvalue{\??id#1\??id#2}{}}

\def\dodostopregister[#1][#2]%
  {\chardef\registerpagestatus=3
   \def\currentregister{#1}%
   \complexdoregister[#2]}

\def\complexdozieregister[#1]#2#3%
  {\begingroup
     \thisisnextinternal{\s!ind}%
     \ifduplicate\getlastregisterentry{#2}\fi
     \convertexpanded{\??id\currentregister}{#2}\asciiregisterentry
     \makesectionformat
     \edef\schrijfwegnaarregister%
       {\writeutility%
          {r s %
           {\currentregister} %
           {\nextinternalreference} %
           {#1} %
           {\asciiregisterentry} %
           {#3} %
           {\sectionformat}}}%
     \schrijfwegnaarregister
   \endgroup
   \registerinfo{> zie}{#2}%
   \GotoPar}

\def\dozieregister#1%
  {\def\currentregister{#1}%
   \complexorsimpleempty\dozieregister}

\def\doschrijfnaarregister[#1]%  % de twee-traps-aanroep is nodig
  {\edef\currentregister{#1}%    % om gebruik van \ExpandBothAfter
   \doprocesspageregister}           % mogelijk te maken

\def\schrijfnaarregister%
  {\dodoubleempty\doschrijfnaarregister}

\def\ifregistergeplaatst{\ifutilitydone}

\newif\iffirstregisterpage

\def\c!entrya{}
\def\c!entryb{}
\def\c!entryc{}

\def\nextregisterpage%
  {\iffirstregisterpage
     \doglobal\newcounter\registerpagenumber
   \fi
   \doglobal\increment\registerpagenumber}

\def\doregisterpagelocation#1#2%
  {\nextregisterpage
   \hbox to 1em{\hss\doregisterpagehowto{#1}{#2}\hss}}

\def\setregisterpage#1%
  {\let\registerpageseparator=\empty
   \processaction
     [\getvalue{\??id#1\c!symbool}]
     [      \c!n=>{\def\doregisterpage##1[##2]%
                     {\doregisterpagelocation{#1}{\registerpagenumber}\/}},
            \c!a=>{\def\doregisterpage##1[##2]%
                     {\doregisterpagelocation{#1}{\character{\registerpagenumber}\/}}},
               1=>{\def\doregisterpage##1[##2]%
                     {\doregisterpagelocation{#1}{$\bullet$}}},
               2=>{\def\doregisterpage##1[##2]%
                     {\doregisterpagelocation{#1}{\vrule\!!width1em\!!height1ex\!!depth\!!zeropoint}}},
         \v!geen=>{\def\doregisterpage##1[##2]{}},%
      \s!unknown=>{\def\registerpagesymbol{\getvalue{\??id#1\c!symbool}}%
                   \def\doregisterpage##1[##2]%
                     {\doregisterpagelocation{#1}{\registerpagesymbol}}},
      \s!default=>{\def\registerpageseparator%
                     {,}%
                   \def\doregisterpage##1[##2]%
                     {\doregisterpagehowto{##1}
                        {\strut
                         \paginaprefix{\??id##1}[##2]%
                         \translatednumber[##2]}}}]}

\let\registerpagehowto\empty
\let\registertexthowto\empty

\def\setregisterhowto[#1,#2]%
  {\def\registerpagehowto{#1}%
   \def\registertexthowto{#2}}%

\def\doregistertexthowto#1#2%
  {\dostartattributes{\??id#1\registertexthowto}\c!tekstletter\c!tekstkleur{}%
   \getvalue{\??id#1\c!tekstcommando}{#2}%
   \dostopattributes}

\def\doregisterpagehowto#1#2%
  {\dostartattributes{\??id#1\registerpagehowto}\c!paginaletter\c!paginakleur{}%
   \getvalue{\??id#1\c!paginacommando}{#2}%
   \dostopattributes}

\def\registerentry #1{\executeifdefined{#1\s!entry}\gobbleoneargument}
\def\registerentrya#1{\executeifdefined{#1\s!entrya}\gobbleoneargument}
\def\registerentryb#1{\executeifdefined{#1\s!entryb}\gobbleoneargument}
\def\registerentryc#1{\executeifdefined{#1\s!entryc}\gobbleoneargument}
\def\registersee   #1{\executeifdefined{#1\s!see}\gobblethrearguments}
\def\registerpage  #1{\executeifdefined{#1\s!page}\gobblefourarguments}
\def\registerfrom  #1{\executeifdefined{#1\s!from}\gobblefourarguments}
\def\registerto    #1{\executeifdefined{#1\s!to}\gobblefourarguments}

\def\doresetregister#1%
  {\letvalue{#1\s!entrya}=\gobbleoneargument
   \letvalue{#1\s!entryb}=\gobbleoneargument
   \letvalue{#1\s!entryc}=\gobbleoneargument
   \letvalue   {#1\s!see}=\gobblethreearguments
   \letvalue  {#1\s!page}=\gobblefourarguments
   \letvalue  {#1\s!from}=\gobblefourarguments
   \letvalue    {#1\s!to}=\gobblefourarguments
   \letvalue {#1\s!entry}=\gobbleoneargument}

\newif\iffirstsubentry
\newif\iffirstsubsubentry

\newcounter\currententrylevel

\let\c!entryletter   =\empty
\let\c!entryreference=\empty
\let\c!entrya        =\relax
\let\c!entryb        =\relax
\let\c!entryc        =\relax

\chardef\lastregisterpagestatus=0

\def\limitedregisterentry#1#2%
  {\getvalue{\??id#1\c!tekstcommando}%
     {\doifelsenothing{\??id#1\c!maxbreedte}
        {#2}
        {\limitatetext{#2}{\getvalue{\??id#1\c!maxbreedte}}{\unknown}}}}

\def\dosetpageregisterpage#1#2#3#4#5#6%
  {\doifreglevelelse[#5]
     {\global\utilitydonetrue
      \c!entryletter
      \setregisterhowto[#3]%
      \def\dohandleregisterentry##1%
        {\bgroup
         \if!!donea % \strut nieuw
           \setbox0=\hbox{\showlocation{\doregistertexthowto{#2}
             {\strut\limitedregisterentry{#2}{##1}}}}% 
           \gotonextinternal{\s!ind}{#4}{#6}{\box0}%
         \else
           \doregistertexthowto{#2}{##1}%
         \fi
         \egroup
         \!!doneafalse}%
      \!!doneafalse
      \doifelsevalue{\??id#2\c!interactie}{\v!tekst}
        {\ifcase\currententrylevel                 \or
           \!!doneatrue\c!entrya\c!entryb\c!entryc \or
           \c!entrya\!!doneatrue\c!entryb\c!entryc \or
           \c!entrya\c!entryb\!!doneatrue\c!entryc \fi}
        {\c!entrya\c!entryb\c!entryc}%
      \global\let\c!entrya=\relax
      \global\let\c!entryb=\relax
      \global\let\c!entryc=\relax
      \global\let\c!entryletter=\relax
      \global\let\c!entryreference=\relax
      \iffirstregisterpage
        \global\chardef\lastregisterpagestatus=0
        \expandafter\hskip\getvalue{\??id#2\c!afstand}\relax
        \donetrue
      \else\ifnum#1=3
        |--|\relax % -- !
        \donetrue
      \else\ifnum\lastregisterpagestatus=2
        \donefalse % waiting for "to" pagenumber
      \else
        \registerpageseparator
        |\spatie|\relax % \relax needed because | looks ahead
        \donetrue
      \fi\fi\fi
      \ifdone
        \doifelsevalue{\??id#2\c!interactie}{\v!paginanummer}
          {\bgroup
           \setbox0=\hbox
             {\showlocation{\doregisterpage{#2}[#5]\ifnum#1=2\/\fi}}%
           \gotonextinternal{\s!ind}{#4}{#6}{\box0}%{\copy0}%
           \egroup}
          {\hbox{\doregisterpage{#2}[#5]\ifnum#1=2\/\fi}}%
        \ignorespaces
        \global\chardef\lastregisterpagestatus=#1\relax
      \fi
      \global\firstregisterpagefalse}
     {}}

\def\dosetpageregistersee#1#2#3#4%
  {\doifreglevelelse[#4::0]
     {{\global\utilitydonetrue
       \setregisterhowto[#2]%
       \def\dohandleregisterentry##1% dubbelop | \strut nieuw
         {\doregistertexthowto{#1}{\strut\limitedregisterentry{#1}{##1}}}% 
       \getvalue
         {#1\ifcase\currententrylevel\s!entrya\or\s!entryb\else\s!entryc\fi}%
         {\doregisterpagehowto{#1}{\labeltexts{\v!zie}{#3}}}%
       \c!entryletter\c!entrya\c!entryb\c!entryc
       \global\let\c!entrya=\relax
       \global\let\c!entryb=\relax
       \global\let\c!entryc=\relax
       \global\let\c!entryletter=\relax
       \global\let\c!entryreference=\relax
       \global\chardef\lastregisterpagestatus=0
       \global\firstregisterpagefalse}}
     {}}

\def\dosetpageregisterletter#1#2%
  {\gdef\c!entryreference%
     {\global\let\c!entryreference=\relax
      \doifvalue{\??id#1\c!refereren}{\v!aan}
        {\pagereference[#1:#2]}}%
   \gdef\c!entryletter%
     {\global\let\c!entryletter=\relax
      \global\utilitydonetrue
      \getvalue{\??id#1\c!voor}%
      \vskip\lineheight\goodbreak\vskip-\lineheight
      \doifelsevalue{\??id#1\c!aanduiding}{\v!ja}
        {\ifhmode\unskip\else\noindent\fi
         \getvalue{\??id#1\c!commando}% % needed
           {\doattributes{\??id#1}\c!letter\c!kleur{\strut\ignorespaces#2}}%
         \getvalue{\??id#1\c!na}%
         \par\nobreak}       % don't use \string#2, other hack
        {\goodbreak}}}       % needed #2 can be \string...

\def\dosetpageregisterentrya#1#2%
  {\edef\currententrylevel{1}%
   \global\let\c!entryb=\relax
   \global\let\c!entryc=\relax
   \gdef\c!entrya%
     {\global\firstregisterpagetrue
      \endgraf
      \hangindent1em\noindent\c!entryreference
      \dohandleregisterentry{#2}%
      \global\firstsubentrytrue
      \global\firstsubsubentrytrue}}

\def\dosetpageregisterentryb#1#2%
  {\edef\currententrylevel{2}%
   \global\let\c!entryc=\relax
   \global\def\c!entryb%
     {\global\firstregisterpagetrue
      \global\let\c!entrya=\relax
      \endgraf
      \iffirstsubentry\nobreak\fi
      \hangindent2em\noindent\c!entryreference\hskip1em\relax
      \dohandleregisterentry{#2}%
      \global\firstsubentryfalse
      \global\firstsubsubentrytrue}}

\def\dosetpageregisterentryc#1#2%
  {\edef\currententrylevel{3}%
   \gdef\c!entryc%
     {\global\firstregisterpagetrue
      \global\let\c!entrya=\relax
      \global\let\c!entryb=\relax
      \endgraf
      \iffirstsubsubentry\nobreak\fi
      \hangindent3em\noindent\c!entryreference\hskip2em\relax
      \dohandleregisterentry{#2}%
      \global\firstsubsubentryfalse}}

\def\dosetpageregister#1%
  {\dosetreglevel{\getvalue{\??id#1\c!criterium}}%
   \global\let\currentregisterentry\empty
   \global\firstsubentrytrue
   \global\firstsubsubentrytrue
   \setregisterpage{#1}%
   \chardef\lastregisterpagestatus=0
   \setvalue{#1\s!entrya}{\dosetpageregisterentrya {#1}}%
   \setvalue{#1\s!entryb}{\dosetpageregisterentryb {#1}}%
   \setvalue{#1\s!entryc}{\dosetpageregisterentryc {#1}}%
   \setvalue  {#1\s!page}{\dosetpageregisterpage{1}{#1}}%
   \setvalue  {#1\s!from}{\dosetpageregisterpage{2}{#1}}%
   \setvalue    {#1\s!to}{\dosetpageregisterpage{3}{#1}}%
   \setvalue   {#1\s!see}{\dosetpageregistersee    {#1}}%
   \setvalue {#1\s!entry}{\dosetpageregisterletter {#1}}}

\def\getalllistreferences#1#2%
  {\doglobal\convertexpanded{\??id#1}{#2}\currentregisterentry 
   \doifdefinedelse{\??id#1\??id\currentregisterentry}
     {\edef\alllistreferences%
        {\getvalue{\??id#1\??id\currentregisterentry}}%
      \beforesplitstring\alllistreferences\at::\to\internallistreference
      \aftersplitstring\alllistreferences\at::\to\alllistreferences}
     {\let\alllistreferences=\empty
      \def\internallistreference{0}}}

\def\dosetlinkregister#1% is die page reference echt nodig?
  {\dosetreglevel{\getvalue{\??id#1\c!criterium}}%
   \setregisterpage{#1}%
   \global\let\currentregisterentry\empty
   \global\firstsubentrytrue % not needed 
   \global\firstsubsubentrytrue % not needed too
   \setvalue{#1\s!entrya}##1%
     {\dosetlinkregisterentrya{#1}{##1}}%
   \setvalue{#1\s!entry}##1% 
     {\dosetpageregisterletter{#1}{##1}}}

\def\dosetlinkregisterentrya#1#2%
   {\global\utilitydonetrue
    \c!entryletter
    \iflocation
      \getalllistreferences{#1}{#2}%
      \endgraf\hangindent1em\noindent\c!entryreference
      %
      %\thisissomeinternal{\s!lin}{\internallistreference}%
      %
      \pagereference[-:\s!lin:\internallistreference]% -: added
      %
      \getcommacommandsize[\alllistreferences]%
      \getfromcommacommand[\alllistreferences][1]%
      \ifnum\commalistsize=1
        \let\firstlistreference=\empty
        \let\midlistreference=\commalistelement
        \let\lastlistreference=\empty
      \else
        \let\firstlistreference=\commalistelement
        \getfromcommacommand[\alllistreferences][\commalistsize]%
        \let\lastlistreference=\commalistelement
        \ifnum\commalistsize=2
          \let\midlistreference=\empty
        \else
          \!!counta=\commalistsize
          \divide\!!counta by 2
          \getfromcommacommand[\alllistreferences][\!!counta]%
          \let\midlistreference=\commalistelement
        \fi
      \fi
      % aangepast 
      \def\dodocommando[####1-####2]%
        {\gotonextinternal{\s!ind}{####1}{####2}{\box0}}%
      \doifelsevalue{\??id#1\c!interactie}{\v!paginanummer}     
        {\limitedregisterentry{#1}{#2}} % paginanummer
        {{\setbox0=\hbox{\limitedregisterentry{#1}{\begstrut#2}}%  
          \ifx\firstlistreference\empty  % tekst,alles
            \ifx\midlistreference\empty
              \box0
            \else
              \expandafter\dodocommando\expandafter[\midlistreference]%
            \fi
          \else
            \expandafter\dodocommando\expandafter[\firstlistreference]%
          \fi}}%
      \doifvalue{\??id#1\c!nummer}{\v!ja}
        {\hskip\getvalue{\??id#1\c!afstand}(\commalistsize)}%
      \doifnotvalue{\??id#1\c!interactie}{\v!tekst} % paginanummer,alles
        {\def\docommando####1####2%
           {{\setbox0=\hbox{\showlocation{\hbox to 1em{\hss\symbol[####2]\hss}}}%
             \ifx####1\empty
               % \hskip\wd0 % (optioneel maken)
             \else
               \expandafter\dodocommando\expandafter[####1]%
             \fi}}%
         \hskip\getvalue{\??id#1\c!afstand}%
         \docommando\firstlistreference\v!vorige
         \docommando\midlistreference\v!ergens
         \docommando\lastlistreference\v!volgende}%
      % tot hier 
    \else
      \endgraf\noindent\c!entryreference
      \limitedregisterentry{#1}{#2}%
    \fi}

%\def\dosetregister#1%
%  {\doifelsevalue{\??id#1\c!koppeling}{\v!ja}
%     {\dosetlinkregister{#1}}
%     {\dosetpageregister{#1}}}

\def\dosetregister#1%
  {\doifelsevalue{\??id#1\c!koppeling}{\v!ja}
     {\ifautoregisterhack 
        \dosetautoregister{#1}%
      \else
        \dosetlinkregister{#1}%
      \fi}
     {\dosetpageregister{#1}}}

\newcounter\internallistreference

\def\doloadregisterlinks#1%
  {\dosetreglevel{\getvalue{\??id#1\c!criterium}}%
   \setregisterpage{#1}%
   \global\let\currentregisterentry\empty
   \global\firstregisterpagetrue
   \setvalue{#1\s!entrya}##1%
     {\global\firstregisterpagetrue
      \doglobal\convertargument##1\to\currentregisterentry % \doglobal nodig?
      \doglobal\increment\internallistreference}%
   \setvalue{#1\s!from}%
     {\getvalue{#1\s!page}}%
   \setvalue{#1\s!page}##1##2##3##4%
     {\doifreglevelelse[##3]
        {\global\utilitydonetrue
         \iffirstregisterpage
           \global\firstregisterpagefalse
\ifautoregisterhack 
  \setxvalue{\??id#1\??id\currentregisterentry}%
    {\internallistreference::##4}%
\else
           \setxvalue{\??id#1\??id\currentregisterentry}%
             {\internallistreference::##2-##4}%
\fi
         \else % catches errors in index
\ifautoregisterhack % binnen doif
  \doifdefined{\??id#1\??id\currentregisterentry}
    {\setxvalue{\??id#1\??id\currentregisterentry}%
       {\getvalue{\??id#1\??id\currentregisterentry},##4}}%
\else
           \doifdefined{\??id#1\??id\currentregisterentry}
             {\setxvalue{\??id#1\??id\currentregisterentry}%
                {\getvalue{\??id#1\??id\currentregisterentry},##2-##4}}%
\fi
         \fi}
        {}}}

%\def\dokoppelregister[#1][#2]%
%  {\iflocation
%     \begingroup
%     \let\dosetregister=\doloadregisterlinks
%     \stelregisterin[#1][#2]%
%     \doutilities{#1}{\jobname}{#1}{}{}%
%     \endgroup
%   \fi}

\def\dokoppelregister[#1][#2]%
  {\iflocation
     \ifcase0\countervalue{autolink:#1}\relax % only once 
       \begingroup
       \let\dosetregister=\doloadregisterlinks
       \stelregisterin[#1][#2]%
       \doutilities{#1}{\jobname}{#1}{}{}%
       \endgroup
       \ifautoregisterhack 
         \doinitializeautoregister{#1}%
       \else
         \doinitializelinkregister{#1}%
       \fi
     \fi
   \fi}

\def\koppelregister%
  {\dodoubleempty\dokoppelregister}

\def\doprocesslinkregister[#1][#2]#3%
  {\hbox
     {\doprocesspageregister[#2]{#3}%
      \let\firstlistreference=\empty
      \let\lastlistreference=\empty
      \let\selflistreference=\empty
      \let\prevlistreference=\empty
      \let\nextlistreference=\empty
      \getalllistreferences{#1}{#3}%
      \doifnot{\alllistreferences}{}
        {\def\dodocommando[##1-##2]%
           {\ifx\firstlistreference\empty
              \def\firstlistreference{##1-##2}%
            \fi
            \def\lastlistreference{##1-##2}%
            \ifnum##1<\nextinternalreference\relax
              \def\prevlistreference{##1-##2}%
             \else\ifnum##1>\nextinternalreference\relax
               \def\nextlistreference{##1-##2}%
               \def\dodocommando[####1-####2]%
                 {\def\lastlistreference{####1-####2}}%
             \else
               \def\selflistreference{##1-##2}%
             \fi\fi}%
         \def\docommando##1%
           {\dodocommando[##1]}%
         \processcommacommand[\alllistreferences]\docommando}%
       \ifx\prevlistreference\empty
         \let\prevlistreference=\lastlistreference
       \fi
       \ifx\nextlistreference\empty
         \let\nextlistreference=\firstlistreference
       \fi
       \ifx\prevlistreference\selflistreference
         \let\prevlistreference=\empty
         \let\nextlistreference=\empty
       \fi
       \def\dodocommando[##1-##2]%
         {\gotonextinternal{\s!ind}{##1}{##2}{\box0}}%{\copy0}}%
       \def\docommando##1##2%
         {\bgroup
          \ifx##1\empty
            \doifvalue{\??id#1\c!onbekendeverwijzing}{\v!leeg}
              {\hskip1em}%
          \else
            \setbox0=\hbox to 1em{\hss\showlocation{\symbol[##2]}\hss}%
            \expandafter\dodocommando\expandafter[##1]%
          \fi
          \egroup}%
       \processaction
         [\getvalue{\??id#1\c!plaats}]
         [\v!midden=>\docommando\prevlistreference\v!vorige,
           \v!links=>\docommando\prevlistreference\v!vorige
                     \docommando\nextlistreference\v!volgende]%
       \doifreferencefoundelse{\s!lin:\internallistreference}
         {\gotosomeinternal
            {\s!lin}{\internallistreference}{\currentrealreference}
            {\showlocation{\limitedregisterentry{#1}{#3}}}}
         {\hbox{\limitedregisterentry{#1}{#3}}}%
       \processaction
         [\getvalue{\??id#1\c!plaats}]
         [ \v!midden=>\docommando\nextlistreference\v!volgende,
           \v!rechts=>\docommando\prevlistreference\v!vorige
                      \docommando\nextlistreference\v!volgende]}}

\def\dodolinkedregister[#1][#2]#3% page auto link 
  {\bgroup
   \chardef\registerpagestatus=1
   \def\currentregister{#1}%
   \iflocation
     \ifautoregisterhack 
       \def\next{\doprocessautoregister[#1][#2]{#3}}%
     \else
       \def\next{\doprocesslinkregister[#1][#2]{#3}}%
     \fi
   \else
     \def\next{\doprocesspageregister[#2]{#3}}%
   \fi
   \next
   \egroup
   \ifvmode\nobreak\fi
   \GotoPar}

\def\dolinkedregister#1%
  {\dodoubleempty\dodolinkedregister[#1]}

\def\dosetautoregister#1% 
  {\makecounter{autolink:#1}%
   \dosetreglevel{\getvalue{\??id#1\c!criterium}}%
   \setregisterpage{#1}%
   \global\let\currentregisterentry\empty
   \global\firstsubentrytrue % not needed 
   \global\firstsubsubentrytrue % not needed too
   \setvalue{#1\s!entrya}##1%
     {\dosetautoregisterentrya{#1}{##1}}%
   \setvalue{#1\s!entry}##1% 
     {\dosetpageregisterletter{#1}{##1}}}

\def\dosetautoregisterentrya#1#2% for the moment \e!register ipv #1
  {\global\utilitydonetrue
   \c!entryletter
   \iflocation
     \getalllistreferences{#1}{#2}%
     \endgraf\hangindent1em\noindent\c!entryreference
     \pagereference[-:\s!lin:\internallistreference]% 
     \pluscounter{autolink:#1}%
     \bgroup
     \setupinteraction[\c!kleur=,\c!contrastkleur=,\c!letter=]% kan sneller
     \naarbox
       {\limitedregisterentry{#1}{\begstrut#2}}%  
       [JS(SetRegisterEntry{\e!register,\countervalue{autolink:#1},#2,{\alllistreferences}})]%
     \egroup
   \else
     \endgraf\noindent\c!entryreference
     \limitedregisterentry{#1}{#2}%
   \fi}

\def\doprocessautoregister[#1][#2]#3%
  {\hbox
     {\doprocesspageregister[#2]{#3}%
      \doifreferencefoundelse{\s!lin:\internallistreference}
        {\gotosomeinternal
           {\s!lin}{\internallistreference}{\currentrealreference}
           {\showlocation{\limitedregisterentry{#1}{#3}}}}
        {\hbox{\limitedregisterentry{#1}{#3}}}}}

% \appendmacro aan openpaginaactie (in shipout)

%D The first implementation used one main field with clones.
%D In a 2500 page document this resulted in a rather (anoying)
%D long start||up time. This \quote {every page its own field}
%D solution, combined with a \quote {page open action}, works
%D much faster, but is conceptually pretty weak. 

\def\complexregisterfield[#1]% 
  {\definefield[#1:\realfolio][line][\e!register]%
   \field[#1:\realfolio]}

\def\simpleregisterfield%
  {\complexregisterfield[\e!register]}

\definecomplexorsimple\registerfield

\setupfield
  [\e!register]
  [\c!breedte=10em,
   \c!hoogte=3ex,
   \c!uitlijnen=\v!midden,
   \c!optie=\v!alleenleesbaar,
   \c!plaats=\v!laag]

\def\doinitializeautoregister#1% for the moment \e!register ipv #1
  {\useJSscripts[reg]%
   \useJSpreamblenow{LinkedRegisters}% 
   \setupinteraction[\c!openpaginaactie=JS(UpdateRegisterField{\e!register})]% 
   \definereference[\v!reset   \e!register][JS(ResetRegisterEntry{\e!register})]%
   \definereference[\v!eerste  \e!register][JS(GotoFirstRegisterEntry{\e!register})]%
   \definereference[\v!vorige  \e!register][JS(GotoPreviousRegisterEntry{\e!register})]%
   \definereference[\v!volgende\e!register][JS(GotoNextRegisterEntry{\e!register})]%
   \definereference[\v!laatste \e!register][JS(GotoLastRegisterEntry{\e!register})]}

\def\doinitializelinkregister#1%
  {}

\def\complexdoplaatsregister[#1]%
  {\begingroup
   \stelregisterin[\currentregister][#1]%
   \raggedright
   \startkolommen
     [\c!n=\getvalue{\??id\currentregister\c!n},
      \c!balanceren=\getvalue{\??id\currentregister\c!balanceren},
      \c!uitlijnen=\getvalue{\??id\currentregister\c!uitlijnen}]%
   \mindermeldingen
   \startopelkaar[\v!blanko]%
   \doutilities{\currentregister}{\jobname}{\currentregister}{}{\par}%
   \stopopelkaar
   \stopkolommen
   \endgroup}

\def\doplaatsregister[#1]%
  {\def\currentregister{#1}%
   \complexorsimpleempty\doplaatsregister}

\def\plaatsregister%
  {\dosingleargument\doplaatsregister}

\def\complexdovolledigregister[#1]% \@EA's kunnen weg
  {\@EA\plaatsvolledig\@EA{\@EA\systemsuppliedchapter\@EA}%
     \@EA{\@EA\currentregister\@EA}%
     \@EA{\@EA\headtext\@EA{\currentregister}}%
     {\complexdoplaatsregister[#1]}}

\def\dovolledigregister[#1]%
  {\def\currentregister{#1}%
   \complexorsimpleempty\dovolledigregister}

\def\volledigregister%
  {\dosingleargument\doplaatsregister}

\def\dodefinieerregister[#1][#2]%
  {\stelregisterin[#1]%
     [\c!n=2,
      \c!balanceren=\v!ja,  % \v!nee komt niet zo vaak voor
      \c!uitlijnen=\v!nee,
      \c!voor=\blanko,      % binnen kolommen: \blanko[\v!regel]
      \c!na=,
      \c!symbool=,
      \c!interactie=\v!paginanummer,
      \c!afstand=1em,
      \c!letter=\v!vet,
      \c!paginaletter=\v!schuin,
      \c!aanduiding=\v!ja,
      \v!deel\v!nummer=\v!ja, % v
      \v!hoofdstuk\c!nummer=\v!nee,
      \c!criterium=\v!alles,
      \c!commando=,
      \c!refereren=\v!aan,
      \c!plaats=\v!midden,
      \c!maxbreedte=,
      \c!nummer=\v!nee,
      \c!onbekendeverwijzing=\v!leeg]%
   \presetheadtext[#1=\Word{#1}]%
   \setvalue{#1}%
     {\doregister{#1}}%
   \setvalue{\e!gekoppelde#1}%
     {\dolinkedregister{#1}}%
   \setvalue{\s!set#1}%
     {\dosetregister{#1}}%
   \setvalue{\s!reset#1}%
     {\doresetregister{#1}}%
   \addutilityreset{#1}%
   \setvalue{\e!zie#1}%
     {\dozieregister{#1}}%
   \setvalue{\e!plaats#1}%
     {\doplaatsregister[#1]}%
   \setvalue{\e!volledige#1}%
     {\dovolledigregister[#1]}%
   \setvalue{\e!stel#1\e!in}[##1]%
     {\getparameters[\??id#1][##1]}}

\def\definieerregister%
  {\dodoubleargument\dodefinieerregister}

\def\registerlengte  {\utilityregisterlength}

\def\utilityregisterlength {0}

\def\dobepaalregisterkenmerken[#1][#2]%
  {\begingroup
   \stelregisterin[#1][#2]%
   \dosetreglevel{\getvalue{\??id#1\c!criterium}}%
   \setvalue{#1\s!from}%
     {\getvalue{#1\s!page}}%
   \setvalue{#1\s!page}##1##2##3##4%
     {\doifreglevelelse[##3]
        {\doglobal\increment\utilitylistlength
         \global\utilitydonetrue}
        {}}%
   \doglobal\newcounter\utilityregisterlength
   \setbox0=\vbox
     {\doutilities{#1}{\jobname}{#1}{}{}}%
   \endgroup
   \ifregistergeplaatst\enablemode[\e!register]\else\disablemode[\e!register]\fi}

\def\bepaalregisterkenmerken%
  {\dodoubleempty\dobepaalregisterkenmerken}

\definieerregister
  [\v!index]
  [\v!indices]

% \stelregisterin[index][koppeling=ja]
% 
% \stelveldenin
%   [register][achtergrond=raster,kader=uit]
%
% \stelvoettekstenin
%   [{\field[index]}]
% 
% \stelhoofdtekstenin
%   [{\naar   {first}[eersteindex]\quad
%     \naar{previous}[vorigeindex]\quad
%     \naar    {next}[volgendeindex]\quad
%     \naar    {last}[laatsteindex]\quad\quad
%     \naar   {index}[index]}]
% 
% \starttekst 
% 
% oeps~~~\gekoppeldeindex{oeps} \blanko
% flop~~~\gekoppeldeindex{flop} \blanko
% test~~~\gekoppeldeindex{test} \pagina
% flop~~~\gekoppeldeindex{flop} \blanko
% test~~~\gekoppeldeindex{test} \pagina
% oeps~~~\gekoppeldeindex{oeps} \blanko
% test~~~\gekoppeldeindex{test} \pagina
% flop~~~\gekoppeldeindex{flop} \blanko
% oeps~~~\gekoppeldeindex{oeps} \pagina
% 
% \volledigeindex

\protect 

\endinput 
