%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
% todo achtergronden in kolommen

%D This file is loaded at runtime, thereby providing an
%D excellent place for hacks and new features.

\unprotect

\writestatus{\m!systems}{beware: some patches loaded from cont-new.tex!}

\def\toplinebox
  {\dowithnextbox
     {\ifdim\dp\nextbox>\strutdepth
        \scratchdimen\dp\nextbox
        \advance\scratchdimen-\strutdepth 
        \getnoflines\scratchdimen
        \struttedbox{\box\nextbox}%
        \dorecurse\noflines{\vbox{\hsize\zeropoint\strut}}%
      \else
        \box\nextbox
      \fi}%
   \tbox}

\def\expandifnonempty#1% 
  {\@EA\ifx\csname#1\endcsname\empty
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi
   {\csname#1\endcsname}}

\def\@@sectiekoppeling#1%
  {\expandifnonempty{\??ko#1\c!koppeling}{#1}}

\def\@@sectiesectie#1%
  {\expandifnonempty{\??ko#1\c!sectie}{\@@sectiekoppeling{#1}}}

\def\sectioncountervalue#1%
  {\@@sectionvalue{\@@sectiesectie{#1}}}

\def\NormalizeFontSize#1#2#3#4#5% the normal struggle with accuracy 
  {\bgroup
   \dimen0=#4% #4 can be \ht0 or so
   \setbox0\hbox{\definedfont[#5 at 5pt]#3}% 10pt 
   \ifdim\wd0>\zeropoint
     \dimen2=#10 % #1 is \wd or \ht
     \dimen4=\maxdimen % 10000pt
     \divide\dimen4 \dimen2
     \divide\dimen0 1638 % 1000
     \dimen0=\number\dimen4\dimen0
     \divide \dimen0 \plustwo % ... 
     \xdef\TheNormalizedFontSize{\the\dimen0}%
   \else
     \dimen0\bodyfontsize
   \fi
   \definedfont[#5 at \the\dimen0]%
   \expandafter
   \egroup
   \expandafter\font\expandafter#2\fontname\font\relax}

% todo namespace \@@meta:#1:... ! ! ! ! ! !

\def\presetMPvariable  
  {\dodoubleargument\dopresetMPvariable}

\def\dopresetMPvariable[#1][#2=#3]% 
  {\doifundefined{#1:#2}{\setvalue{#1:#2}{#3}}}

% experiment, not yet to be used 

\def\displaybreak
  {\ifhmode
     \removeunwantedspaces
     \ifcase\raggedstatus\hfill\fi
     \strut\penalty-9999 % \break fails on case (3) 
   \fi}

\def\startdisplay{\displaybreak\ignorespaces\startopelkaar}
\def\stopdisplay {\stopopelkaar\displaybreak\ignorespaces}

\def\tightvbox
  {\dowithnextbox{\dp\nextbox\zeropoint\box\nextbox}\vbox}

\def\tightvtop
  {\dowithnextbox{\ht\nextbox\zeropoint\box\nextbox}\vtop}

\def\startpagefigure
  {\dodoubleempty\dostartpagefigure}

\def\dostartpagefigure[#1][#2]%
  {\bgroup
   \getparameters[\??ex][\c!offset=\!!zeropoint,#2]%
   \startTEXpage[\c!offset=\@@exoffset]%
     \externalfigure[#1][#2]\ignorespaces}

\def\stoppagefigure
  {\stopTEXpage
   \egroup}

\def\pagefigure
  {\dodoubleempty\dopagefigure}

\def\dopagefigure[#1][#2]%
  {\dostartpagefigure[#1][#2]\stoppagefigure}

% pretty important (esp since we now ignore shipouts) 
%
% actually we should nil all writes, marks, specials 

\appendtoks \globallet\popcolor\relax \to \everylastshipout

\def\incrementvalue#1{\expandafter\increment\csname#1\endcsname}
\def\decrementvalue#1{\expandafter\decrement\csname#1\endcsname}

% \translateMPinput{il2-pl}
%
% \startMPenvironment[global] 
%   \setupbodyfont[plr] 
% \stopMPenvironment 
%
% \TeX: ± ¶ 
%
% \startMPcode
% draw btex MetaPost: ± ¶ etex scaled 5 ;  
% \stopMPcode

\def\begintbl
  {\doglobal\newcounter\colTBL
   \doglobal\newcounter\rowTBL
   \doglobal\decrement\rowTBL
   \tabskip\zeropoint
   \halign\bgroup
\registerparoptions
   \ignorespaces##\unskip&&\ignorespaces##\unskip\cr}

% \startcolumnset[two] \input tufte
% \startcolumnsetspan[two] \input tufte \stopcolumnsetspan
% \input tufte \stopcolumnset

% now in cont-loc.tex, for the sake of testing.
%
% %D When \type {\somecolor} is issued, we can savely assume
% %D grouping. Using \type {\groupedcommand} here (i.e.\ the
% %D definition of \type {\color}) is unsafe because in
% %D interferes with for instance switching attributes.
%
% \def\switchtocolor[#1]%
%   {\bgroup\startcolor[#1]
%    \aftergroup\stopcolor
%    \aftergroup\egroup}

% this supports:
%
% \starttypen
% \placelist[section][criterium=chapter,number=1] \blank
% \placelist[section][criterium=chapter,number=2] \blank
% \placelist[section][criterium=chapter,number=3] \blank
%
% \chapter{first}  \section{AA} \section{BB}
% \chapter{second} \section{CC} \section{DD}
% \chapter{third}  \section{EE} \section{FF}
% \stoptypen

\def\dosettoclevel#1#2%
  {\ifundefined{#1#2\c!nummer}%
     \dosetfilterlevel{\getvalue{#1#2\c!criterium}}\empty
   \else
     \doifelsevaluenothing{#1#2\c!nummer}%
       {\dosetfilterlevel{\getvalue{#1#2\c!criterium}}\empty}
       {\setsectieenkoppeling{\getvalue{#1#2\c!criterium}}%
        \dosetfilterlevel
          {\previoussection\@@sectie}%
          {\getvalue{#1#2\c!nummer}}}%
   \fi}

\def\GetPar
  {\expanded
     {\dowithpar
        {\the\BeforePar
         \BeforePar\emptytoks}
        {\the\AfterPar
         \BeforePar\emptytoks
         \AfterPar\emptytoks}}}

\def\GotoPar
  {\expanded
     {\dogotopar
        {\the\BeforePar
         \BeforePar\emptytoks}}}

\def\@@somedefinitie#1[#2]#3%
  {\dowithpar
     {\bgroup\executedoordefinitie{#1}[#2]{#3}}%
     {\@@stopdefinitie{#1}}}

% test this for a long time, esp since from now on, by default
% \commands are not expanded

\setupreferencing
  [\c!expansie=\v!nee]

\def\dotextreference[#1]#2%
  {\bgroup
   \def\asciia{#1}%
   \convertexpanded\??rf{#2}\asciib
   \@EA\rawtextreference\@EA\s!txt\@EA\asciia\@EA{\asciib}%
   \egroup}

\def\dopagereference[#1]%
  {\rawpagereference\s!pag{#1}}

\def\doreference[#1]#2%
  {\bgroup
   \def\asciia{#1}%
   \convertexpanded\??rf{#2}\asciib
   \@EA\rawreference\@EA\s!ref\@EA\asciia\@EA{\asciib}%
   \egroup}

% what is this stupid macro meant for:

\def\hyphenationpoint
  {\hskip\zeropoint}

\def\hyphenated#1%
  {\bgroup
   \!!counta\zerocount
   \def\hyphenated##1{\advance\!!counta\plusone}%
   \handletokens#1\with\hyphenated
   \!!countb\plusone
   \def\hyphenated##1%
     {##1%
      \advance\!!countb\plusone\relax
      \ifnum\!!countb>2 \ifnum\!!countb<\!!counta
        \hyphenationpoint
      \fi\fi}%
   \handletokens#1\with\hyphenated
   \egroup}

\def\obeysupersubletters
  {\let\super\normalsuper
   \let\suber\normalsuber
   \let\normalsuper\letterhat
   \let\normalsuber\letterunderscore
   \enablesupersub}

\def\obeysupersubmath
  {\let\normalsuper\letterhat
   \let\normalsuber\letterunderscore
   \enablesupersub}

%\let\normaltype\type
%
%\def\type#1%
%  {\expanded{\normaltype{\detokenize{#1}}}}

% {x123 \os x123} {\tfa x123 \os x123} {x123 \tx x123 \os x123}
% \definefontsynonym[OldStyle][Serif]
% {x123 \os x123} {\tfa x123 \os x123} {x123 \tx x123 \os x123}

% testen :
%
% \appendtoks
%   \let\registerparoptions\relax
% \to \everyforgetall

\def\startgridcorrection
  {\dosingleempty\dostartgridcorrection}

\def\dostartgridcorrection[#1]%
  {\ifgridsnapping
     \iffirstargument\doifsomething{#1}{\verplaatsopgrid[#1]}\fi
     \snaptogrid\vbox\bgroup
   \else
     \startbaselinecorrection
   \fi}

\def\stopgridcorrection
  {\ifgridsnapping
     \egroup
   \else
     \stopbaselinecorrection
   \fi}

\def\checkgridsnapping
  {\lineskip\ifgridsnapping\zeropoint\else\normallineskip\fi}

\def\startplaatsen
  {\dosingleempty\dostartplaatsen}

\def\dostartplaatsen[#1]% tzt n*links etc
  {\endgraf
   \noindent\bgroup
   \setlocalhsize
   \hbox to \localhsize\bgroup
     \doifnot{#1}\v!links\hss
     \def\stopplaatsen
       {\unskip\unskip\unskip
        \doifnot{#1}\v!rechts\hss
        \egroup
        \egroup
        \endgraf}%
     \gobblespacetokens}

% \startplaatsen[links] bla \stopplaatsen

% we don't register the paragraph characteristics, only the
% width

\appendtoks
  \setinnerparpositions % see "techniek" for application
\to \everytabulate

\appendtoks \checkcurrentlayout \to \everystarttext

\def\flushfootnotes  {\flushnotes}
\def\doflushfootnotes{\doflushnotes}

%D This alternative is slower, since it works on top of the
%D color (stack) mechanism, but it does provide nesting.

\def\dosetrastercolor#1%
  {\edef\@@cl@@s{#1}%
   \ifx\@@cl@@s\empty
     \let\@@cl@@s\@@rsraster
   \fi
   \setevalue{\??cr\??rs}{\colorSpattern}}

% beware, don't add extra grouping, else color in tables
% fails

\def\localstartraster[#1]%
  {\ifincolor\dosetrastercolor{#1}\localstartcolor[\??rs]\fi}

\def\startraster[#1]%
  {\ifincolor\dosetrastercolor{#1}\startcolor[\??rs]\fi}

\def\localstopraster{\ifincolor\localstopcolor\fi}
\def\stopraster     {\ifincolor\stopcolor\fi}

\def\fontclassname#1#2%
  {\ifcsname\??ff#1#2\endcsname
     \fontclassname{#1}{\csname\??ff#1#2\endcsname}%
   \else\ifcsname\??ff#2\endcsname
     \fontclassname{#1}{\csname\??ff#2\endcsname}%
   \else
     #2%
   \fi\fi}

\def\defineclassfontsynonym
  {\dotripleargument\dodefineclassfontsynonym}

\def\dodefineclassfontsynonym[#1][#2][#3]%
  {\definefontsynonym[#1][\fontclassname{#2}{#3}]}

%\definefontsynonym [KopFont] [\fontclassname{officina}{SerifBold}]
%
%\defineclassfontsynonym [KopFont] [officina] [SerifBold]

\def\startkolomopmaak % don't change
  {\bgroup
   \getrawnoflines\teksthoogte % teksthoogte kan topskip hebben, dus raw
   \scratchdimen\noflines\lineheight
   \advance\scratchdimen-\lineheight
   \advance\scratchdimen\topskip
   \setbox\scratchbox
   \ifcase\showgridstate\vbox\else\ruledvbox\fi to \scratchdimen\bgroup}

\def\stopkolomopmaak
  {\egroup
   \dp\scratchbox\zeropoint
   \wd\scratchbox\tekstbreedte
   \box\scratchbox
   \egroup}

% todo : hoe komt box er uit

\long\def\startexternalfigure
  {\dotripleempty\dostartexternalfigure}

\long\def\dostartexternalfigure[#1][#2][#3]#4\stopexternalfigure
  {\gdef\figuredescription{#4}%
   \externalfigure[#1][#2][#3]%
   \globallet\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}

\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 % for international purposes
%  {\bgroup
%   \doifnextcharelse[\startsetupsA\startsetupsB}
%
%\def\startsetupsA[#1]%
%  {\catcode`\^^M=\@@ignore
%   \dostartsetups{#1}}
%
%\def\startsetupsB#1 % space delimited
%  {\catcode`\^^M=\@@ignore
%   \dostartsetups{#1}}
%
%\long\def\dostartsetups#1#2\stopsetups
%  {\egroup
%   \long\setvalue{\??su#1}{#2}}

\def\startsetups % for international purposes
  {\bgroup\doifnextcharelse[{\startsetupsA\stopsetups}%
                            {\startsetupsB\stopsetups}}
 
\def\startlocalsetups % for nested purposes
  {\bgroup\doifnextcharelse[{\startsetupsA\stoplocalsetups}%
                            {\startsetupsB\stoplocalsetups}}

\def\startsetupsA#1[#2]%
  {\catcode`\^^M=\@@ignore
   \dostartsetups#1{#2}}

\def\startsetupsB#1#2 % space delimited
  {\startsetupsA#1[#2]}%

\long\def\dostartsetups#1#2% watch out: not \grabuntil 
  {\dograbuntil#1{\egroup\long\setvalue{\??su#2}}}

\newtoks\everyfirstparagraphintro
\newtoks\everynextparagraphintro

\chardef\everyparagraphintro=0

\def\setupparagraphintro
  {\dodoubleempty\dosetupparagraphintro}

\def\dosetupparagraphintro[#1][#2]%
  {\processallactionsinset
     [#1]
     [   \v!reset=>\global\chardef\everyparagraphintro0
                   \global\everyfirstparagraphintro\emptytoks
                   \global\everynextparagraphintro \emptytoks,
        \v!eerste=>\global\chardef\everyparagraphintro1
                   \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\everyparagraphintro2
     \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

% 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
     \letgvalue{\s!paragraph:p:#1}\twopassdata
     \globallet\lastchangedpage\twopassdata
   \else
     \globallet\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   {\flushnextbox},
           \v!laag=>\tbox   {\flushnextbox},
         \v!midden=>\vcenter{\flushnextbox},
           \v!laho=>\vcenter{\flushnextbox},
        \s!unknown=>\tbox   {\flushnextbox},
        \s!default=>\tbox   {\flushnextbox}]%
   \else
     \startbaselinecorrection
     \noindent\flushnextbox
     \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=\zerocount
     \TABLEnoalign
       {\globalletempty\checkTABLEautorow
        \globalletempty\chuckTABLEautorow}%
   \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\obeyfollowingtoken{{}}  % end \cs scanning

\def\gobbleparameters{\doquadrupleempty\dogobbleparameters}
\def\dogobbleparameters[#1][#2][#3][#4]{}

% \setvariables[xx][titel=]
% \setvariables[xx][titel=test test]
% \setvariables[xx][titel=test $x=1$ test]   % fatal error reported
% \setvariables[xx][titel=test {$x=1$} test]
% \setvariables[xx][titel]                   % fatal error reported
% \setvariables[xx][titel=e]

\def\??vars{@@vars}

\def\setvariables
  {\dotripleargument\dosetvariables[\getrawparameters]}

\def\globalsetvariables
  {\dotripleargument\dosetvariables[\globalgetrawparameters]}

\def\dosetvariables[#1][#2][#3]%
  {\errorisfataltrue
   \def\currentvariableclass{#2}%
   #1[\??vars:#2:][#3]%
   \errorisfatalfalse}

\beginTEX

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

\endTEX

\beginETEX \ifcsname

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

\endETEX

\def\showvariable#1#2%
  {\showvalue{\ifundefined{\??vars:#1:#2}\s!empty\else\??vars:#1:#2\fi}}

\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 \intercolumnwidth
     \hsize\@@ksn\hsize
     \advance\hsize -\intercolumnwidth
   \fi
   \dowithnextbox
     {\setbox\columnspanbox\flushnextbox
      \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 -\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}

\def\backgroundline[#1]%
 %{\doifsomething{#1}{\dobackgroundline{#1}}\hbox}
  {\doifcolorelse{#1}{\dobackgroundline{#1}\hbox}\hbox}

\def\dobackgroundline#1%
  {\dowithnextbox
     {\hbox
        {\localcolortrue
         \startcolor[#1]%
         \vrule
           \!!width \wd\nextbox
           \!!height\ht\nextbox
           \!!depth \dp\nextbox
       \stopcolor
       \hskip-\wd\nextbox
       \box\nextbox}}}

%D For Ton. To be documented.

\def\plaatsexterndocument[#1]%
  {\def\doexternaldocument[##1][##2][##3]%
     {\readlocfile{##2}\donothing\donothing}%
   \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
