%D \module
%D   [       file=cont-new,
%D        version=1995.10.10,
%D          title=\CONTEXT\ Miscellaneous Macros,
%D       subtitle=New Macros,
%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.

% manual : offsetbox alignbox

%D This file is loaded at runtime, thereby providing an
%D excellent place for hacks and new features.

\unprotect


\def\woordrechts % zie naw 
  {\groupedcommand
     {\hfill\allowbreak\strut\hfill\hbox}
     {\parfillskip\zeropoint\par}}

\def\startkolomopmaak
  {\bgroup
   \getnoflines\vsize
   \scratchdimen\noflines\lineheight
   \vbox to \scratchdimen\bgroup}

\def\stopkolomopmaak
  {\vss % niet meer aanpassen 
   \egroup
   \egroup}

% transparency ! 

\def\dodoglobalstopcolor
  {\ifcase\colorlevel \else
     \donoglobalstopcolor
     \global\@EA\let\@EA\previouscolor\csname\@@currentcolorname\endcsname
     \ifcase\colorlevel\relax
       \docolormark\empty
       \dostoptransparency
       \dostopcolormode
     \else % let's do a bit redundant testing here
       \docolormark\previouscolor
       \ifx\previouscolor\empty
         \dostoptransparency
         \dostopcolormode
       \else
         \doifcolorelse\previouscolor
           {\ifx\currentcolor\previouscolor\else
% alternatively we could let \startcolormode handle this 
\dostoptransparency % really needed 
%\dostopcolormode % less safe but more efficient 
              \startcolormode\previouscolor
            \fi}
           {\dostoptransparency
            \dostopcolormode}%
       \fi
     \fi
   \fi}

\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\getmarking[\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}}


\writestatus{\m!systems}{beware: some patches loaded from cont-new.tex!}

% todo : hoe komt box er uit 

% \def\@@itemcounter {\s!itemcount} -> non nested continue 
%
% todo : option to merge itemgroup [ig-one a b c] [ig-two d e f]

\def\@@itemcounter {\s!itemcount\currentitemgroup}

\long\def\startexternalfigure
  {\dotripleempty\dostartexternalfigure}

\long\def\dostartexternalfigure[#1][#2][#3]#4\stopexternalfigure
  {\gdef\figuredescription{#4}%
   \externalfigure[#1][#2][#3]%
   \global\let\figuredescription\empty}

\let\figuredescription\empty

% very experimental 

\def\redoanalyzefigurefiles#1%
  {\ifcase\figurestatus
     \def\@@efcurrenttype{#1}%
     \dododoanalyzefigurefiles\empty
   \fi}

\def\analyzefigurefiles
  {\let\externalfigurelog\empty
   \let\@@efcurrenttype\empty
   \let\@@efcurrentpath\empty
   \let\@@efcurrentfile\empty
   \doanalyzefigurefiles\doanalyzefigurefilesA
   \doanalyzefigurefiles\doanalyzefigurefilesB
   \doanalyzefigurefiles\doanalyzefigurefilesC
   % new, permits rather raw names like e:/....
   \let\dodoanalyzefigurefiles\redoanalyzefigurefiles
   \doanalyzefigurefiles\doanalyzefigurefilesA
   \doanalyzefigurefiles\doanalyzefigurefilesB
   \doanalyzefigurefiles\doanalyzefigurefilesC}

\definesystemvariable {bl} % bleeding

\newcounter\nofbleeds % per pag 

\def\setupbleeding
  {\dodoubleempty\getparameters[\??bl]}

\setupbleeding
  [\c!plaats=l,
   \c!rek=\v!ja,
   \c!breedte=3cm,
   \c!hoogte=3cm,
   \c!offset=2mm,
   \c!voffset=\@@bloffset,
   \c!hoffset=\@@bloffset]

\def\bleed
  {\dosingleempty\dobleed}

\def\dobleed[#1]#2%
  {\hbox\bgroup  
   \def\bleedwidth {\hsize}%
   \def\bleedheight{\vsize}%
   \doglobal\increment\nofbleeds
   \getparameters[\??bl][#1]%
   \!!doneafalse % left
   \!!donebfalse % right 
   \!!donecfalse % top
   \!!donedfalse % bottom  
   \processaction
     [\@@blplaats]
     [ t=>\!!donectrue\let\@@blhoffset\!!zeropoint, 
       b=>\!!donedtrue\let\@@blhoffset\!!zeropoint,
       l=>\!!doneatrue\let\@@blvoffset\!!zeropoint, 
       r=>\!!donebtrue\let\@@blvoffset\!!zeropoint,
      bl=>\!!doneatrue\!!donedtrue,   
      lb=>\!!doneatrue\!!donedtrue,   
      br=>\!!donebtrue\!!donedtrue,   
      rb=>\!!donebtrue\!!donedtrue,  
      tl=>\!!doneatrue\!!donectrue,  
      lt=>\!!doneatrue\!!donectrue,  
      tr=>\!!donebtrue\!!donectrue,  
      rt=>\!!donebtrue\!!donectrue]%  
   \doifelse\@@blrek\v!ja\donetrue\donefalse
   \scratchdimen\@@blbreedte
   \ifdone
     \if!!donea
       \advance\scratchdimen\MPx{\??bl:\nofbleeds}%
     \else\if!!doneb
       \scratchdimen\papierbreedte
       \advance\scratchdimen-\MPx{\??bl:\nofbleeds}%
     \fi\fi
   \fi
   \advance\scratchdimen\@@blhoffset
   \edef\bleedwidth{\the\scratchdimen}%
   \scratchdimen\@@blhoogte
   \ifdone
     \if!!donec
       \scratchdimen\papierhoogte
       \advance\scratchdimen-\MPy{\??bl:\nofbleeds}%
     \else\if!!doned
       \advance\scratchdimen\MPy{\??bl:\nofbleeds}%
     \fi\fi
   \fi
   \advance\scratchdimen\@@blvoffset
   \edef\bleedheight{\the\scratchdimen}%
   \hsize\bleedwidth
   \vsize\bleedheight
   \setbox\scratchbox\hbox{#2}%
   \setbox\scratchbox\hbox to \@@blbreedte
     {\if!!donea\hss\fi\box\scratchbox\if!!doneb\hss\fi}%
   \if!!doned 
     \setbox\scratchbox\hbox
       {\lower\bleedheight\hbox{\raise\@@blhoogte\box\scratchbox}}%
   \fi
   \wd\scratchbox\@@blbreedte
   \ht\scratchbox\@@blhoogte
   \dp\scratchbox\zeropoint
   \ifdone
     \hpos{\??bl:\nofbleeds}{\box\scratchbox}%
   \else
     \box\scratchbox
   \fi
   \egroup}

\setupbleeding[\c!rek=\v!ja]

\defineexternalfigure[bleed][\c!breedte=\bleedwidth,\c!hoogte=\bleedheight]

% \placefloat[left]{none}
%   {\bleed[width=5cm,height=3cm,location=lt]{\externalfigure[koe][bleed]}}
% 
% \input tufte 
% 
% \placefloat[left]{none}
%   {\bleed[width=5cm,height=3cm,location=l]{\externalfigure[koe][bleed]}}
% 
% \input tufte 
% 
% \placesomefloat[right]{none}
%   {\bleed[width=5cm,height=3cm,location=r]{\externalfigure[koe][bleed]}}
% 
% \input tufte 
% 
% \placesomefloat[right]{none}
%   {\bleed[width=5cm,height=3cm,location=rb]{\externalfigure[koe][bleed]}}
% 
% \input tufte 

\def\OTRSETgotocell#1#2% not that robust 
  {\endgraf
   \gdef\gotocellcounter{0}%
   \doloop
     {\ifnum\mofcolumns<#1\relax 
        \doglobal\increment\gotocellcounter\relax
        \ifnum\gotocellcounter>#1\relax
          \line{\strut}\crlf 
          \line{\strut}\crlf 
          \column 
          \writestatus{columnset}{quitting goto cell}%
          \exitloop
        \else
          \column 
        \fi 
      \else 
        \exitloop 
      \fi}% 
   \ifnum\mofcolumns=#1\relax
     \ifnum#2>1
       \scratchcounter=0
       \currenthcell\mofcolumns
       \currentvcell#2\advance\currentvcell -1 
       \dorecurse\currentvcell
         {\OTRSETdoifcellelse\mofcolumns\recurselevel\donothing
            {\advance\scratchcounter1\relax}}
       \getnoflines\pagetotal
       \advance\scratchcounter-\noflines
       \ifnum\scratchcounter>0 
         \dorecurse\scratchcounter{\line{\strut}}%
       \fi
     \fi
   \fi
   \OTRSETsetvsize}

\def\setlocalfloatdimensions#1#2% experimental ! ! ! !
  {\doifvaluesomething{\??fl#1\c!maxbreedte}
     {\scratchdimen=\getvalue{\??fl#1\c!maxbreedte}\relax
      \ifdim\wd\nextbox>\scratchdimen
        \setbox\nextbox=\hbox to \scratchdimen
          {\doifcommonelse{\v!rechts,\v!inrechter,\v!links,\v!inlinker}{#2}
             {\doifnotcommon{\v!rechts,\v!inrechter}{#2}\hss
              \box\nextbox
              \doifnotcommon{\v!links ,\v!inlinker }{#2}\hss}%
             {\doifvalue{\??fl#1\c!plaats}\v!rechts\hss
              \box\nextbox
              \doifvalue{\??fl#1\c!plaats}\v!links \hss}}%
      \fi}}

\def\phantombox[#1]%
  {\hbox\bgroup
   \getparameters
     [\??ol]
     [\c!breedte=\zeropoint,\c!hoogte=\zeropoint,\c!diepte=\zeropoint,#1]%
   \setbox\scratchbox\null
   \wd\scratchbox\@@olbreedte
   \ht\scratchbox\@@olhoogte
   \dp\scratchbox\@@oldiepte
   \box\scratchbox
   \egroup}

\long\@EA\def\csname\e!start\e!instellingen\endcsname#1 %
  {\bgroup
   \catcode`\^^M=\@@ignore
   \xdostartsetups{#1}}

\expanded
  {\long\noexpand\def\noexpand\xdostartsetups##1##2\csname\e!stop\e!instellingen\endcsname%
     {\egroup
      \long\noexpand\setvalue{\??su##1}{##2}}}

\def\startsetups#1 % for international purposes
  {\bgroup
   \catcode`\^^M=\@@ignore
   \dostartsetups{#1}}

\long\def\dostartsetups#1#2\stopsetups
  {\egroup
   \long\setvalue{\??su#1}{#2}}

\def\definefont
  {\dotripleempty\dodefinefont}

\def\dodefinefont[#1][#2][#3]% [name][spec][1.6 | line=10pt | setup_id]
  {\doifinstringelse{ }{#2}
     {\ifthirdargument
        \unexpanded\setvalue{#1}{\redodefinefont{#1}{#2}{#3}}%
      \else
        \unexpanded\setvalue{#1}{\dododefinefont{#1}{#2}}%
      \fi}
     {\definefont[#1][#2 sa 1][#3]}}

\def\redodefinefont#1#2#3%
  {\dododefinefont{#1}{#2}%
   \doifnumberelse{#3}
     {\stelinterliniein[#3]\stelspatieringin}
     {\doifassignmentelse{#3}
        {\stelinterliniein[#3]\stelspatieringin}
        {\setups[#3]}}}

% todo, but now more easy to do:\definepage

\def\docomplexpagina[#1]%
  {\flushfootnotes
   \bgroup
   \def\dodocomplexpagina##1%
     {\csname
        @@\v!pagina\ifundefined{@@\v!pagina##1}\s!unknown\else##1\fi
      \endcsname}%
   \processcommalist[#1]\dodocomplexpagina
   \egroup}

\def\installpagehandler#1#2%
  {\setvalue{@@\v!pagina#1}{#2}}

\installpagehandler \s!unknown
  {\let\@@pagespecification\commalistelement
   \doifinstringelse{+}\@@pagespecification
     {\ejectinsert\ejectpage
      \dorecurse\@@pagespecification\ejectdummypage}
     {\doifnumberelse\@@pagespecification
        {\ejectinsert\ejectpage
         \doloop
           {\ifnum\userpageno<\@@pagespecification\relax
              \ejectdummypage
            \else
              \exitloop
            \fi}}
        {}}}

\installpagehandler \s!default
  {} % do nothing if empty

\installpagehandler  \v!reset
  {\global\paginageblokkeerdfalse}

\installpagehandler \v!blokkeer
  {\global\paginageblokkeerdtrue}

\installpagehandler \v!ja
  {\ifpaginageblokkeerd\else
     \ejectinsert
     \ejectpage
     \ifbinnenkolommen
       \ejectpage  % anders soms geen overgang
     \fi
   \fi}

\installpagehandler \v!opmaak
  {\ifpaginageblokkeerd\else
     \eject
   \fi}

\installpagehandler \v!blanko
  {\pagebodyornamentsfalse}

\installpagehandler \v!nee
  {\ifpaginageblokkeerd\else
     \dosomebreak\nobreak
   \fi}

\installpagehandler \v!voorkeur
  {\ifpaginageblokkeerd\else
     \ifbinnenkolommen
       \dosomebreak\goodbreak
     \else
       \testpagina[3][\!!zeropoint]%
     \fi
    \fi}

\installpagehandler \v!grotevoorkeur
  {\ifpaginageblokkeerd\else
     \ifbinnenkolommen
       \dosomebreak\goodbreak
     \else
       \testpagina[5][\!!zeropoint]%
     \fi
   \fi}

\installpagehandler \v!leeg
  {\ejectinsert
   \ejectpage
   \doifnotvalue{\??tk\v!hoofd\c!status}{\v!stop}
     {\setupheader[\c!status=\v!leeg]}%
   \doifnotvalue{\??tk\v!voet\c!status}{\v!stop}
     {\setupfooter[\c!status=\v!leeg]}%
   \ejectdummypage}

\installpagehandler \v!links
  {\ejectinsert
   \superejectpage
   \doifbothsidesoverruled
   \orsideone
     \resetcurrentsectionmarks
     \ejectdummypage
   \orsidetwo
   \od}

\installpagehandler \v!rechts
  {\ejectinsert
   \superejectpage
   \doifbothsidesoverruled
   \orsideone
   \orsidetwo
     \resetcurrentsectionmarks
     \ejectdummypage
   \od}

\installpagehandler \v!even
  {\pagina
   \doifonevenpaginaelse
     {\resetcurrentsectionmarks\ejectdummypage}{}}

\installpagehandler \v!oneven
  {\pagina
   \doifonevenpaginaelse
     {}{\resetcurrentsectionmarks\ejectdummypage}}

\installpagehandler \v!viertal
  {\ifdubbelzijdig
     \!!counta=\realpageno
     \!!countb=\realpageno
     \divide\!!counta 4
     \divide\!!countb 2
     \ifnum\!!counta=\!!countb
     \else
       \pagina
       \pagina[\v!leeg]%
       \pagina[\v!leeg]%
     \fi
   \fi}

\installpagehandler \v!laatste
  {\ejectinsert
   \superejectpage\relax
   \doifbothsidesoverruled
     \naastpagina
   \orsideone
   \orsidetwo
     %\ifodd\realpageno \else % kan weer weg
       \noheaderandfooterlines
       \ejectdummypage
     %\fi
   \od
   \filluparrangedpages}

% \setlayerframed[layer id][layer settings][framed setting]{data}
% \setlayerframed[layer id][combined settings]{data}

\def\setlayerframed
  {\dotripleempty\dosetlayerframed}

\def\dosetlayerframed
  {\ifthirdargument
     \expandafter\dosetlayerframedT
   \else
     \expandafter\dosetlayerframedS
   \fi}

\def\dosetlayerframedT[#1][#2][#3]%
  {\dowithnextbox{\setlayer[#1][#2]{\box\nextbox}}%
   \hbox\framed[#3]}

\def\dosetlayerframedS[#1][#2][#3]%
  {\dowithnextbox
     {\setlayer
        [#1]
        [\c!breedte=\wd\nextbox,\c!hoogte=\ht\nextbox,
         \c!offset=\!!zeropoint,#2]
        {\box\nextbox}}%
   \hbox\framed[\c!plaats=\v!normaal,#2]}

\beginETEX \dimexpr

\definepapersize
  [oversized]
  [\c!breedte=\dimexpr(\papierbreedte+1.5cm),
   \c!hoogte=\dimexpr(\papierhoogte+1.5cm)]

\definepapersize
  [doublesized]
  [\c!breedte=\dimexpr(\papierbreedte),
   \c!hoogte=\dimexpr(2\papierhoogte)]

\definepapersize
  [doubleoversized]
  [\c!breedte=\dimexpr(\papierhoogte+1.5cm),
   \c!hoogte=\dimexpr(2\papierbreedte+1.5cm)]

\endETEX

\newtoks\everyfirstparagraphintro
\newtoks\everynextparagraphintro

\chardef\everyparagraphintro=0

\def\setupparagraphintro
  {\dodoubleempty\dosetupparagraphintro}

\def\dosetupparagraphintro[#1][#2]%
  {\processallactionsinset
     [#1]
     [   \v!reset=>\global\chardef\everyparagraphintro=0
                   \global\everyfirstparagraphintro\emptytoks
                   \global\everynextparagraphintro \emptytoks,
        \v!eerste=>\global\chardef\everyparagraphintro=1
                   \doglobal\appendtoks#2\to\everyfirstparagraphintro,
      \v!volgende=>\ifcase\everyparagraphintro\global\chardef\everyparagraphintro=2\fi
                   \doglobal\appendtoks#2\to\everynextparagraphintro,
           \v!elk=>\ifcase\everyparagraphintro\global\chardef\everyparagraphintro=2\fi
                   \doglobal\appendtoks#2\to\everyfirstparagraphintro
                   \doglobal\appendtoks#2\to\everynextparagraphintro]}

\def\doinsertparagraphintro
  {\ifcase\everyparagraphintro\relax
     % no data
   \or
     % first data
     \global\chardef\everyparagraphintro=2
     \scratchtoks\everyfirstparagraphintro
     \global\everyfirstparagraphintro\emptytoks
   \or
     % next data
     \scratchtoks\everynextparagraphintro
   \fi
   \the\scratchtoks}

\def\insertparagraphintro
  {\ifcase\everyparagraphintro\else\@EA\doinsertparagraphintro\fi}

\appendtoks\insertparagraphintro\to\everypar

%D \starttypen
%D \setupparagraphintro[first][\hbox to 3.5em{\tt FIRST \hss}]
%D \setupparagraphintro[first][\hbox to 3.5em{\tt TSRIF \hss}]
%D \setupparagraphintro[next] [\hbox to 3.5em{\tt NEXT  \hss}]
%D \setupparagraphintro[next] [\hbox to 3.5em{\tt TXEN  \hss}]
%D \setupparagraphintro[each] [\hbox to 3.0em{\tt EACH  \hss}]
%D \setupparagraphintro[each] [\hbox to 3.0em{\tt HCEA  \hss}]
%D
%D some paragraph \par
%D some paragraph \par
%D some paragraph \par
%D
%D \definelabel[parnumber]
%D
%D \setupparagraphintro[reset,each][\inleft{\slxx\parnumber}]
%D
%D some paragraph \par
%D some paragraph \par
%D some paragraph \par
%D \stoptypen

\def\@@themaintextcolor{themaintextcolor}

\def\starttextcolor[#1]%
  {\doifsomething{#1}
     {\bgroup
      \def\stoptextcolor          % also goes ok with \page after
        {\let\maintextcolor\empty % this one because the top of
         \stopregistercolor       % page sets the color right (side
         \egroup}%                % effect)
      \def\starttextcolor[##1]%
        {\bgroup
         \definecolor[\@@themaintextcolor][##1]%
         \let\stoptextcolor\egroup}%
      \startregistercolor[\@@themaintextcolor]%
      \definecolor[\@@themaintextcolor][#1]%
      \let\maintextcolor\@@themaintextcolor}}

\def\initializemaintextcolor
  {\doifelsenothing\@@cltekstkleur
     {\let\maintextcolor\empty}
     {\let\maintextcolor\@@themaintextcolor
      \definecolor[\@@themaintextcolor][\@@cltekstkleur]%
      \doinitializemaintextcolor}}

\def\doinitializemaintextcolor
  {\appendtoks\starttextcolor[\@@themaintextcolor]\to\everystarttext
   \appendtoks\stoptextcolor                      \to\everystoptext
   \let\doinitializemaintextcolor\relax}

% wrong names

\newif\ifpagechanged \let\lastchangedpage\empty

\def\checkpagechange#1%
  {\gettwopassdata\s!paragraph
   \pagechangedfalse
   \iftwopassdatafound
     \ifnum\twopassdata>0\getvalue{\s!paragraph:p:#1}\relax
       \pagechangedtrue
     \fi
   \fi
   \ifpagechanged
     \global\letvalue{\s!paragraph:p:#1}\twopassdata
     \global\let\lastchangedpage\twopassdata
   \else
     \global\let\lastchangedpage\realfolio
   \fi
   \doparagraphreference}

\def\changedpage#1%
  {\getvalue{\s!paragraph:p:#1}}

\def\startfixed{\dosingleempty\dostartfixed}

\long\def\dostartfixed[#1]%
  {\expanded{\dowithnextbox{\noexpand\dodofixed{\ifhmode0\else1\fi}{#1}}}%
   \vbox\bgroup
   \setlocalhsize}

\def\stopfixed%
  {\egroup}

\def\dodofixed#1#2%
  {\ifcase#1\relax
     \processaction
       [#2]
       [   \v!hoog=>\bbox{\box\nextbox},
           \v!laag=>\tbox{\box\nextbox},
         \v!midden=>\vcenter{\box\nextbox},
           \v!laho=>\vcenter{\box\nextbox},
        \s!unknown=>\tbox{\box\nextbox},
        \s!default=>\tbox{\box\nextbox}]%
   \else
     \startbaselinecorrection
     \noindent\box\nextbox
     \stopbaselinecorrection
   \fi}

% \startitemize
%
% \item \externalfigure[koe][height=2cm]
% \item \externalfigure[koe][height=2cm]
% \item \externalfigure[koe][height=2cm]
% \item \externalfigure[koe][height=2cm]
%
% \page
%
% \item \startfixed      \externalfigure[koe][height=2cm]\stopfixed
% \item \startfixed[high]\externalfigure[koe][height=2cm]\stopfixed
% \item \startfixed[low] \externalfigure[koe][height=2cm]\stopfixed
% \item \startfixed[lohi]\externalfigure[koe][height=2cm]\stopfixed
%
% \page
%
% \item test \startfixed      \externalfigure[koe][height=2cm]\stopfixed
% \item test \startfixed[high]\externalfigure[koe][height=2cm]\stopfixed
% \item test \startfixed[low] \externalfigure[koe][height=2cm]\stopfixed
% \item test \startfixed[lohi]\externalfigure[koe][height=2cm]\stopfixed
%
% \page
%
% \item test \par \startfixed      \externalfigure[koe][height=2cm]\stopfixed
% \item test \par \startfixed[high]\externalfigure[koe][height=2cm]\stopfixed
% \item test \par \startfixed[low] \externalfigure[koe][height=2cm]\stopfixed
% \item test \par \startfixed[lohi]\externalfigure[koe][height=2cm]\stopfixed
%
% \stopitemize

% \def\docalculatefigurenorm#1#2%
%   {\dodocalculatefigurenorm{#1}[#2\empty\empty]}
%
% \def\dodocalculatefigurenorm#1[#2#3#4]#5#6#7%
%   {\ExpandFirstAfter\processaction
%       [#2#3#4]
%       [     \v!max=>\global#1=#6\relax,
%           \v!kolom=>\global#1=#6\relax,
%           \v!tekst=>\global#1=#6\relax,
%         \v!passend=>\global#1=#7\relax,
%            \v!ruim=>\global#1=#7\relax
%                     \global\advance #1 -4\@@exkorps\relax,
%        #2*\v!kolom=>\global#1=#6\relax
%                     \ifbinnenkolommen
%                       \global\advance#1 \intercolumnwidth
%                       \global\multiply#1 #2\relax
%                       \global\advance#1 -\intercolumnwidth
%                     \fi,
%        #2*\v!tekst=>\global#1=\zetbreedte
%                     \global\advance#1 \papierbreedte,
%         \s!default=>\doifsomething{#5}{\global#1=#5\relax},
%         \s!unknown=>\global#1=\@@exkorps\relax
%                     \global\divide#1 \!!ten\relax
%                     \global\multiply#1 #2#3#4\relax]}

\def\complexTableTB[#1]{\TABLEnoalign{\blanko[#1]}}
\def\simpleTableTB     {\TABLEnoalign{\blanko}}

\def\TabulateTB
  {\complexorsimpleTable{TB}}

\def\doTableinterline% #1
  {\ifnum\currentTABLEcolumn>\maxTABLEcolumn
     \chuckTABLEautorow
   \else\ifnum\currentTABLEcolumn=0
     \TABLEnoalign
       {\global\let\checkTABLEautorow=\empty
        \global\let\chuckTABLEautorow=\empty}%
   \else
     \setTABLEerror\TABLEmissingcolumn
     \handleTABLEerror
   \fi\fi
   \complexorsimpleTable} % {#1}

\def\TableHL{\doTableinterline{HL}}
\def\TableTB{\doTableinterline{TB}}

\appendtoks\let\TB\TableTB   \to\everytable
\appendtoks\let\TB\TabulateTB\to\everytabulate

% \starttabulate
% \NC text \NC text \NC \NR
% \TB[small]
% \NC text \NC text \NC \NR
% \TB[4*big]
% \NC text \NC text \NC \NR
% \stoptabulate
%
% \starttable[|||]
% \VL text \VL text \VL \AR
% \TB[small]
% \VL text \VL text \VL \AR
% \TB[4*big]
% \VL text \VL text \VL \AR
% \stoptable

% still needed for uguide

\let\placefloatlabel          \placefloatcaption
\let\placefloatlabeltext      \placefloatcaptiontext
\let\placefloatlabelreference \placefloatcaptionreference

\def\checkframedtext%
  {\ifinsidefloat
     \localhsize\hsize
   \else\ifdim\sidefloatvsize>\zeropoint % will be proper handle
   %  \strut            % rather clean way to invoke the sidefloat OTR
   %  \setbox0=\lastbox % and get the widths set, so from now on we
   %  \setlocalhsize    % can have framed texts alongside sidefloats
   \checksidefloat
   \setlocalhsize
     \advance\localhsize-\hangindent
   \else
     \localhsize\hsize
   \fi \fi}

\def\obeyfollowingtoken{{}}  % end \cs scanning

\def\gobbleparameters{\doquadrupleempty\dogobbleparameters}
\def\dogobbleparameters[#1][#2][#3][#4]{}

\def\setvariables%
  {\dodoubleargument\dosetvariables}

\def\dosetvariables[#1][#2]%
  {\def\currentvariableclass{#1}%
   \getparameters[vars:#1:][#2]}

% \def\getvariable#1#2%
%   {\ifundefined{vars:#1:#2}\else\getvalue{vars:#1:#2}\fi}

\def\getvariable#1#2% to be sped up
  {\csname\ifundefined{vars:#1:#2}empty\else vars:#1:#2\fi\endcsname}

\let\currentvariableclass\empty

% Let's see how fast Mr Bigfoot aka GB tracks down this new
% feature -)

\def\defineTABLEdivisions
  {\global\TABLEdivisionfalse % in start
   \let\DL\TableDL
   \let\DC\TableDC
   \let\DV\TableDV
   \let\DR\TableDR}

\def\defineTABLErules
  {\let\VL\TableVL
   \let\VC\TableVC
   \let\HL\TableHL
   \let\HC\TableHC
   \let\VS\TableVS
   \let\VD\TableVD
   \let\VT\TableVT}

\def\TableVS{\gdef\@VLn{1}\VL}
\def\TableVD{\gdef\@VLn{2}\VL}
\def\TableVT{\gdef\@VLn{3}\VL}

\def\@VLn{1}
\def\@VLd{.125em}

\def\do!ttInsertVrule % will be merged in 2005
  {\vrule \!thWidth
   \ifnum\!tgCode=1
     \ifx\!tgValue\empty
       \LineThicknessFactor
     \else
       \!tgValue
     \fi
     \LineThicknessUnit
   \else
     \!tgValue
   \fi
   \hskip\@VLd}

\def\!ttInsertVrule%
  {\hfil
   \TABLEbeforebar % added
   \startglobalTABLEcolor % added
   % we could do without this speedup, some day merge 'm
   \ifcase\@VLn\or
     \do!ttInsertVrule
     \unskip
   \else
     \dorecurse\@VLn\do!ttInsertVrule
     \gdef\@VLn{1}%
     \unskip
   \fi
   \stopglobalTABLEcolor % added
   \TABLEafterbar % added
   \hfil
   &}

% \starttable[|||]
% \HL
% \VL test \VS test \VL \FR
% \VL test \VD test \VL \MR
% \VL test \VT test \VL \LR
% \HL
% \stoptable

%D To be documented, \type {\includemenu[menu]}.
%D To be documented, \type {\emphbf} cum suis.

%D A prelude to strategies. Note for myself: overloads
%D previous stuff from local pragma test files.

\def\s!strategy{strategy}

\def\currentstrategypass    {1}
\def\currentstrategyvariable{0}
\def\maximumstrategypass    {8}

\newconditional\strategypassneeded
\newconditional\strategypassforced

\definetwopasslist{\s!strategy}

\def\registerstrategypass%
  {\ifnum\currentstrategypass>\maximumstrategypass \else
     \ifconditional\strategypassforced
       \doglobal\increment\currentstrategypass
     \else%\ifconditional\strategypassneeded
       %\doglobal\increment\currentstrategypass
     \fi%\fi
   \fi
   \savecurrentvalue\currentstrategypass{\currentstrategypass}}

\appendtoks \registerstrategypass \to \everybye % \everylastshipout

\def\setstrategyvariable#1#2% key value
  {%\doifnotstrategyvariable{#1}{\global\settrue\strategypassneeded}%
   \doglobal\increment\currentstrategyvariable
   \expanded{\immediatewriteutilitycommand{\noexpand
     \twopassentry{\s!strategy}{\currentstrategyvariable}{#1::#2}}}}

\def\doifstrategyvariableelse#1#2#3%
  {\getstrategyvariable{#1}\iftwopassdatafound#2\else#3\fi}

\def\getstrategyvariable#1% key
  {\findtwopassdata{\s!strategy}{#1::}%
   \setxvalue{\s!strategy:#1}{\twopassdata}}

\def\retainstrategyvariable#1% key
  {\expanded{\setstrategyvariable{#1}{\strategyvariable{#1}}}}

\def\strategyvariable#1% key
  {\csname\s!strategy:#1\endcsname}

\let\stratvar\strategyvariable

\def\forcestrategy{\global\settrue \strategypassforced}
\def\abortstrategy{\global\setfalse\strategypassforced}

\def\doifstrategyvariableelse#1#2#3%
  {\getstrategyvariable{#1}\iftwopassdatafound#2\else#3\fi}

\def\doifstrategyvariable   #1#2{\doifstrategyvariableelse{#1}{#2}{}}
\def\doifnotstrategyvariable#1#2{\doifstrategyvariableelse{#1}{}{#2}}

%D New: only at start of columns; may change ! Rather
%D interwoven and therefore to be integrated when the multi
%D column modules are merged.

%  already taken care of: \definesystemvariable{ks}

% is buggy now and does not work any longer

\def\setupcolumnspan[#1]%
  {\getparameters[\??ks][#1]}

\presetlocalframed
  [\??ks]

\setupcolumnspan
  [\c!n=2,
   \c!offset=\v!overlay,
   \c!kader=\v!uit]

\newbox\columnspanbox \let\postprocesscolumnspanbox\gobbleoneargument

\def\dostartcolumnspan[#1]%
  {\bgroup
   \setupcolumnspan[#1]%
   \forgetall
   \ifbinnenkolommen
     \advance\hsize by \intercolumnwidth
     \hsize=\@@ksn\hsize
     \advance\hsize by -\intercolumnwidth
   \fi
   \dowithnextbox
     {\setbox\columnspanbox=\box\nextbox
      \ifbinnenkolommen\wd\columnspanbox=\hsize\fi
      \postprocesscolumnspanbox\columnspanbox
      \scratchdimen=\ht\columnspanbox
      \setbox\columnspanbox=\hbox % depth to be checked, probably option!
        {\localframed[\??ks][\c!offset=\v!overlay]{\box\columnspanbox}}%
      \ht\columnspanbox=\scratchdimen
      \dp\columnspanbox=\dp\strutbox
      \wd\columnspanbox=\hsize
      \ifbinnenkolommen
        \ifnum\@@ksn>1
          \setvsize
          \dohandleallcolumns
            {\ifnum\currentcolumn>\@@ksn\else
               \global\setbox\currenttopcolumnbox=\vbox
                 {\ifnum\currentcolumn=1
                    \snaptogrid\vbox{\copy\columnspanbox}
                  \else
                    \snaptogrid\vbox{\vphantom{\copy\columnspanbox}}
                  \fi}%
               \wd\currenttopcolumnbox=\hsize
               \global\advance\vsize by -\ht\currenttopcolumnbox
             \fi}
          \global\pagegoal=\vsize
        \else
          \snaptogrid\vbox{\box\columnspanbox}
        \fi
      \else
        \snaptogrid\vbox{\box\columnspanbox}
      \fi
      \endgraf
      \prevdepth\dp\strutbox
      \egroup}
     \vbox\bgroup
      %\topskipcorrection % becomes an option !
       \EveryPar{\begstrut\EveryPar{}}} % also !

\def\startcolumnspan%
  {\dosingleempty\dostartcolumnspan}

\def\stopcolumnspan%
  {\egroup}

%D For Ton. To be documented.

\def\plaatsexterndocument[#1]%
  {\def\doexternaldocument[##1][##2][##3]%
     {\readlocfile{##2}{}{}}%
   \getvalue{\v!file:::#1}}

%D Far from complete.

\def\startgeheel%
  {\startregelcorrectie
   \insidefloattrue}

\def\stopgeheel
  {\stopregelcorrectie}

%D No more news.

\protect

%D A few local optimizations and new features, if defined:

\readfile {cont-loc} {} {}

\endinput

