%D \module
%D   [       file=page-txt, % copied from main-001,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Texts,
%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.

% \setuplayouttext in manual

\writestatus{loading}{Context Page Macros / Texts}

\unprotect

\let\dodummypageskip\gobbleoneargument % obsolete

%D Interfacing between this and other modules is handled by
%D the following macros. The current state of a text line
%D (header, footer, etc.) is checked by: 
%D 
%D \starttypen 
%D \resetlayoutlines
%D \stoptypen 
%D 
%D The main text box is finished by the following macro: 
%D 
%D \starttypen 
%D \getmainbox <box> <\vbox|\unvbox>
%D \stoptypen 
%D 
%D The text lines are collected with: 
%D 
%D \starttypen 
%D \gettextboxes
%D \stoptypen 
%D 
%D It is possible to extens the default content of the text 
%D areas by appending content to the following token list 
%D registers: 

\newtoks\toptextcontent     \newtoks\leftedgetextcontent
\newtoks\headertextcontent  \newtoks\leftmargintextcontent
\newtoks\footertextcontent  \newtoks\rightmargintextcontent
\newtoks\bottomtextcontent  \newtoks\rightedgetextcontent

\newtoks\texttextcontent

%D \macros
%D  {setuptop, setupheader, setuptext, 
%D   setupfooter, setupbottom}
%D 
%D The macros in this module sometimes look a bit more complicated
%D than needed, which is a direct result of the fact that their
%D ancestors are quite old and upward compatibility is a must.
%D
%D \showsetup{\y!setuptop}
%D \showsetup{\y!setupheader}
%D \showsetup{\y!setuptext}
%D \showsetup{\y!setupfooter}
%D \showsetup{\y!setupbottom}

\def\setuplayouttext%
  {\dotripleempty\dosetuplayouttext}

\def\dosetuplayouttext[#1][#2][#3]%
  {\ifthirdargument
     \getparameters[\??tk#1#2][#3]%
   \else
    %\getparameters[\??tk#1\v!tekst][#2]%
     \getparameters[\??tk#1][#2]%
   \fi
  %\checkcurrentlayout % no 
   \calculatevsizes}

\def\setuptop    {\dotripleempty\dosetuplayouttext[\v!boven]}
\def\setupheader {\dotripleempty\dosetuplayouttext[\v!hoofd]}
\def\setuptext   {\dotripleempty\dosetuplayouttext[\v!tekst]}
\def\setupfooter {\dotripleempty\dosetuplayouttext[\v!voet]}
\def\setupbottom {\dotripleempty\dosetuplayouttext[\v!onder]}

%D \macros
%D  {noheaderandfooterlines,notopandbottomlines}
%D 
%D Although not really needed, the following shortcuts
%D sometimes come in handy.
%D
%D \showsetup{\y!noheaderandfooterlines}
%D \showsetup{\y!notopandbottomlines}

\def\noheaderandfooterlines
  {\setupheader[\c!status=\v!leeg]%
   \setupfooter[\c!status=\v!leeg]}

\def\notopandbottomlines
  {\setuptop   [\c!status=\v!leeg]%
   \setupbottom[\c!status=\v!leeg]}

%D \macros
%D  {setuptoptexts, setupheadertexts, setuptexttexts, 
%D   setupfootertexts, setupbottomtexts}
%D 
%D The next macros take one or more arguments. The exact setup
%D depends on the number of arguments. Although not that
%D intuitive, the current scheme evolved out of the original.
%D When margin and edge texts as well as middle texts showed
%D up, the current odd|/|even scheme surfaced.
%D
%D \showsetup{\y!setuptoptexts}
%D \showsetup{\y!setupheadertexts}
%D \showsetup{\y!setuptexttexts}
%D \showsetup{\y!setupfootertexts}
%D \showsetup{\y!setupbottomtexts}

\def\setuptoptexts    {\dosixtupleempty\dosetuptexts[\v!boven]}
\def\setupheadertexts {\dosixtupleempty\dosetuptexts[\v!hoofd]}
\def\setuptexttexts   {\dosixtupleempty\dosetuptexts[\v!tekst]}
\def\setupfootertexts {\dosixtupleempty\dosetuptexts[\v!voet ]}
\def\setupbottomtexts {\dosixtupleempty\dosetuptexts[\v!onder]}

%D The left, right and center variables can also be set
%D directly using the previously discussed macros.

\def\dosetuptexts[#1][#2][#3][#4][#5][#6]%
  {\ifsixthargument
     \setvalue{\??tk#1#2\c!linkertekst}%
       {\dodoubletexts{\??tk#1}{#2}%
          {\c!linkerletter \c!linkerkleur \c!linkerbreedte }{#3}%
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#6}}%
     \setvalue{\??tk#1#2\c!rechtertekst}%
       {\dodoubletexts{\??tk#1}{#2}%
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#4}%
          {\c!linkerletter \c!linkerkleur \c!linkerbreedte }{#5}}%
   \else\iffifthargument
     \setvalue{\??tk#1\v!tekst\c!linkertekst}%
       {\dodoubletexts{\??tk#1}\v!tekst
          {\c!linkerletter \c!linkerkleur \c!linkerbreedte }{#2}%
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#5}}%
     \setvalue{\??tk#1\v!tekst\c!rechtertekst}%
       {\dodoubletexts{\??tk#1}\v!tekst
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#3}%
          {\c!linkerletter \c!linkerkleur \c!linkerbreedte }{#4}}%
   \else\iffourthargument
     \setvalue{\??tk#1#2\c!linkertekst}%
       {\dodoubletexts{\??tk#1}{#2}
          {\c!linkerletter\c!linkerkleur\c!linkerbreedte}{#3}%
          {\c!linkerletter\c!linkerkleur\c!linkerbreedte}{#3}}%
     \setvalue{\??tk#1#2\c!rechtertekst}%
       {\dodoubletexts{\??tk#1}{#2}
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#4}%
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#4}}%
   \else\ifthirdargument
     \setvalue{\??tk#1\v!tekst\c!linkertekst}%
       {\dodoubletexts{\??tk#1}\v!tekst
          {\c!linkerletter\c!linkerkleur\c!linkerbreedte}{#2}%
          {\c!linkerletter\c!linkerkleur\c!linkerbreedte}{#2}}%
     \setvalue{\??tk#1\v!tekst\c!rechtertekst}%
       {\dodoubletexts{\??tk#1}\v!tekst
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#3}%
          {\c!rechterletter\c!rechterkleur\c!rechterbreedte}{#3}}%
   \else\ifsecondargument % new
     \letvalue{\??tk#1\v!tekst\c!linkertekst }\empty
     \letvalue{\??tk#1\v!tekst\c!rechtertekst}\empty
     \setvalue{\??tk#1\v!tekst\c!middentekst }%
       {\dosingletexts{\??tk#1}\v!tekst\c!letter\c!kleur\c!breedte{#2}}%
   \else
     \dosixtupleempty\dosetuptexts[#1][\v!tekst][][][][]%
     \dosixtupleempty\dosetuptexts[#1][\v!marge][][][][]%
     \dosixtupleempty\dosetuptexts[#1][\v!rand ][][][][]%
   \fi\fi\fi\fi\fi}

%D Left and right texts are swapped on odd and even pages, but
%D only when double sided typesetting is enabled.

\def\dodoubletexts#1#2#3#4#5#6%
  {\doifoddpageelse
     {\dosingletexts{#1}{#2}#3{#4}}  % #3 => provides three arguments
     {\dosingletexts{#1}{#2}#5{#6}}} % #5 => provides three arguments

%D The next macro will be cleaned up amd made less messy and
%D dependent.

\def\placetextlinestrut#1%
  {\doifvalue{#1\c!strut}\v!ja{\setstrut\strut}}

\def\dosingletexts#1#2#3#4#5#6%
  {\bgroup
   \convertargument#6\to\ascii
   \doifsomething\ascii
     {\doattributes{#1#2}#3#4%
        {\placetextlinestrut{#1}% here !
        %\doifdefinedelse{\??mk\ascii\c!koppeling} % brrr
         \doifelsemarking\ascii
           {\dolimitatetexts{#1#2#5}{\haalmarkering[\ascii][\v!eerste]}}
           {\ConvertConstantAfter\doifelse{\v!paginanummer}{#6}
              {\@@plaatspaginanummer}
              {\ConvertConstantAfter\doifelse{\v!datum}{#6}
                 {\currentdate} 
                 {% #6{}{}{} -> {} needed for macros that look 
                  % ahead, like \uniqueMPgraphic
                  \opeenregel\dolimitatetexts{#1#2#5}{#6{}{}{}}}}}}}%
  \egroup}

%D When specified, the texts are automatically limited in
%D length.

\def\dolimitatetexts#1#2%
  {\doifelsevaluenothing{#1}{#2}{\limitatetext{#2}{\getvalue{#1}}{\unknown}}}

%D The placement of text is hooked into the token lists 
%D associated to the area at hand. 

\appendtoks \placelayouttextline\v!boven\bovenhoogte \to    \toptextcontent
\appendtoks \placelayouttextline\v!hoofd\hoofdhoogte \to \headertextcontent
\appendtoks \placelayouttextline\v!tekst\teksthoogte \to   \texttextcontent
\appendtoks \placelayouttextline\v!voet  \voethoogte \to \footertextcontent
\appendtoks \placelayouttextline\v!onder\onderhoogte \to \bottomtextcontent

%D Texts can be disables, moved up and ignored, depending in
%D the \type {status} variable. This is handled by the next
%D couple of macros. They look less readable then the original
%D implementation, but because they are expanded quite often,
%D we will not use:
%D
%D \starttypen
%D \def\plaatslayoutregel#1#2%  % handelt o.b.v. tekst
%D   {\ExpandFirstAfter\processaction
%D      [\getvalue{\??tk#1\v!tekst\c!status}]
%D      [        \v!geen=>,
%D                   ......
%D              \v!start=>...]}
%D \stoptypen
%D
%D Instead we will map the values of status onto macro
%D expansions.

%\def\settextlinestatus#1%
%  {\edef\textlinestatus{\csname\??tk#1\v!tekst\c!status\endcsname}}

\def\settextlinestatus#1%
  {\edef\textlinestatus{\csname\??tk#1\c!status\endcsname}}

%\def\resettextlinestatus#1%
%  {\letgvalue{\??tk#1\v!tekst\c!status}\v!normaal}

\def\resettextlinestatus#1%
  {\letgvalue{\??tk#1\c!status}\v!normaal}

%\def\placelayouttextline#1#2%  % handelt o.b.v. tekst
%  {\settextlinestatus{#1}%
%   \doifdefinedelse{\string\placelayouttextline\textlinestatus}
%     {\getvalue{\string\placelayouttextline\textlinestatus}{#1}{#2}}
%     {\getvalue{\string\placelayouttextline\s!unknown}{#1}{#2}}}
%
%\def\placelayouttextline#1% #2 
%  {\settextlinestatus{#1}%
%   \doifundefined{\string\placelayouttextline\textlinestatus}
%     {\let\textlinestatus\s!unknown}%
%   \getvalue{\string\placelayouttextline\textlinestatus}{#1}} % {#2}

% recently bugged 
%
% \def\placelayouttextline#1% #2 
%   {\settextlinestatus{#1}%
%    \ifundefined{\string\placelayouttextline\textlinestatus}%
%      \let\textlinestatus\s!unknown
%    \fi
%    \csname\string\placelayouttextline\textlinestatus\endcsname{#1}} % {#2}

\def\placelayouttextline#1% #2 
  {\settextlinestatus{#1}%
   \csname\string\placelayouttextline
     \ifundefined{\string\placelayouttextline\textlinestatus}%
       \s!unknown
     \else
       \textlinestatus 
     \fi
   \endcsname{#1}} % {#2}

\setvalue{\string\placelayouttextline\v!normaal}{\doplacelayouttextline}
\setvalue{\string\placelayouttextline          }{\doplacelayouttextline}
\letvalue{\string\placelayouttextline\v!geen   }\gobbletwoarguments
\letvalue{\string\placelayouttextline\v!hoog   }\gobbletwoarguments

\setvalue{\string\placelayouttextline\v!leeg}#1#2%
  {\resettextlinestatus{#1}}

\setvalue{\string\placelayouttextline\v!start}#1#2%
  {\resettextlinestatus{#1}%
   \doplacelayouttextline{#1}{#2}}

\setvalue{\string\placelayouttextline\v!stop}#1#2%
  {}

\setvalue{\string\placelayouttextline\v!geenmarkering}#1#2%
  {\bgroup
   \resettextlinestatus{#1}%
   \let\dohaalmarkering\nohaalmarkering
   \doplacelayouttextline{#1}{#2}%
   \egroup}

\setvalue{\string\placelayouttextline\s!unknown}#1#2%
  {\bgroup % new
   \resettextlinestatus{#1}%
  %\setlocallayoutline{#1\textlinestatus}%
  %\setlocallayoutline{#1\v!tekst\textlinestatus}%
  %\setlocallayoutline{#1\v!marge\textlinestatus}%
  %\setlocallayoutline{#1\v!rand\textlinestatus}%
   \getvalue{\??tk#1\textlinestatus}%
   \getvalue{\??tk#1\v!tekst\textlinestatus}%
   \getvalue{\??tk#1\v!marge\textlinestatus}%
   \getvalue{\??tk#1\v!rand\textlinestatus}%
   \doplacelayouttextline{#1}{#2}%
   \egroup}

% \def\setlocallayoutline#1%
%   {\ifundefined{\??tk#1}\else\getvalue{\??tk#1}\fi}

%D The following macro has to be called after a page
%D is flushed.

%\def\resetlayoutline#1% beware: global assignment
%  {\doifvalue{\??tk#1\v!tekst\c!status}\v!hoog
%     {\resettextlinestatus{#1}%
%      \donetrue}}

\def\resetlayoutline#1% beware: global assignment
  {\doifvalue{\??tk#1\c!status}\v!hoog
     {\resettextlinestatus{#1}%
      \donetrue}}

\def\resetlayoutlines
  {\donefalse
   \resetlayoutline\v!boven
   \resetlayoutline\v!hoofd
   \resetlayoutline\v!tekst
   \resetlayoutline\v!voet
   \resetlayoutline\v!onder
   \ifdone
     \doglobal\calculatevsizes
     \recalculatebackgrounds
     \recalculatelogos
   \fi}

%D The next series of macros is not that easy to read,
%D because they hook into the main page building macros. By
%D using token list registers for the text content, we can
%D easily hook in other code, like menu generators.

\newbox\scratchpagebox

\def\gettextboxes
  {\setbox\scratchpagebox\vbox
     {\mindermeldingen
      \calculatereducedvsizes
      \swapmargins
      \offinterlineskip
      \vskip-\bovenhoogte
      \vskip-\bovenafstand
      \ifdim\bovenhoogte>\zeropoint
        \the\toptextcontent
        \vskip\bovenhoogte
      \fi
      \vskip\bovenafstand
      \ifdim\hoofdhoogte>\zeropoint
        \the\headertextcontent
        \vskip\hoofdhoogte
      \fi
      \vskip\hoofdafstand
      \placepositionanchors
      \vskip-\teksthoogte
      \the\texttextcontent
      \vskip\teksthoogte
      \vskip\voetafstand
      \ifdim\voethoogte>\zeropoint
        \the\footertextcontent
        \vskip\voethoogte
      \fi
      \vskip\onderafstand
      \ifdim\onderhoogte>\zeropoint
        \the\bottomtextcontent
        \vskip\onderhoogte
      \fi
      \vfilll}%
  \smashbox\scratchpagebox
  \box\scratchpagebox}

\def\getmainbox#1#2%
  {\setbox\scratchpagebox\vbox
     {\offinterlineskip  % na \paginaletter !
      \calculatereducedvsizes
      \calculatehsizes
      \swapmargins
      \vskip\hoofdhoogte
      \vskip\hoofdafstand
      \vskip\layoutparameter\c!tekstafstand
      \hbox to \zetbreedte
        {\bgroup
           \swapmargins
           \goleftonpage
           \ifdim\linkerrandbreedte>\zeropoint
             \the\leftedgetextcontent
             \hskip\linkerrandbreedte
           \fi
           \hskip\linkerrandafstand
           \ifdim\linkermargebreedte>\zeropoint
             \the\leftmargintextcontent
             \hskip\linkermargebreedte
           \fi
           \hskip\linkermargeafstand
         \egroup
         \settextpagecontent\scratchpagebox{#1}{#2}%
         \addtextbackground\scratchpagebox
         \addtextgridlayer\scratchpagebox
         \localstarttextcolor
           \box\scratchpagebox
         \localstoptextcolor
         \bgroup
           \hskip\rechtermargeafstand
           \ifdim\rechtermargebreedte>\zeropoint
             \the\rightmargintextcontent
             \hskip\rechtermargebreedte
           \fi
           \hskip\rechterrandafstand
           \ifdim\rechterrandbreedte>\zeropoint
             \the\rightedgetextcontent
             \hskip\rechterrandbreedte
           \fi
         \egroup
         \hss}}%
   \smashbox\scratchpagebox
   \box\scratchpagebox}

%D The main text area has to be combined with some additional
%D (tracing) information. 

\def\settextpagecontent#1#2#3% #2 and #3 will disappear
  {\setbox#1\hbox to \zetbreedte 
     {\hss                        % so don't change this
      \vbox to \teksthoogte
        {\offinterlineskip
         \freezetextwidth
         \hsize\tekstbreedte      % local variant of \sethsize
         \boxmaxdepth\maxdepth
         \noindent                % content can be < \hsize
         \dopagecontents#2#3}%
      \hss}%
   \dp#1\zeropoint}

\def\addtextgridlayer#1% tzt run time 
  {\ifcase\showgridstate\else % 1=bottom 2=top
     \setgridbox\scratchbox\zetbreedte\teksthoogte
     \setbox#1\hbox
       {\ifcase\showgridstate\or\box#1\hskip-\zetbreedte\fi
        \bgroup % color 
        \incolortrue
        \ifcase\layoutcolumns\else
          \gray
          \hbox to \zetbreedte
            {\dorecurse\layoutcolumns
               {\hskip\layoutcolumnwidth
                \ifnum\recurselevel<\layoutcolumns
                \vrule
                  \!!height\ht\scratchbox
                  \!!depth\dp\scratchbox
                  \!!width\layoutcolumndistance
              \fi}}%
          \hskip-\zetbreedte
        \fi
        \red
        \box\scratchbox
        \egroup
        \ifcase\showgridstate\or\or\hskip-\zetbreedte\box#1\fi}%
   \fi}

%D The placement of a whole line is handled by the next two 
%D macros. These are hooked into the general purpose token
%D list registers mentioned before. 

\def\ignoredlinebreak{\unskip\space\ignorespaces}

\def\doplacelayouttextline#1#2%
  {\ifdim#2>\zeropoint\relax  % prevents pagenumbers when zero height
     \goleftonpage
     \hbox
       {\setbox\scratchpagebox\vbox to #2
          {%\forgetall
           \vsize#2\relax
           \normalbaselines
           \let\\\ignoredlinebreak
           \let\crlf\ignoredlinebreak
          %\getvalue{\??tk#1\v!tekst\c!voor}%
           \getvalue{\??tk#1\c!voor}%
           \doifbothsidesoverruled
             \dodoplacelayouttextline#1\c!linkertekst\c!middentekst\c!rechtertekst
               \gobbleoneargument\getvalue
           \orsideone
             \dodoplacelayouttextline#1\c!linkertekst\c!middentekst\c!rechtertekst
               \gobbleoneargument\getvalue
           \orsidetwo
             \dodoplacelayouttextline#1\c!rechtertekst\c!middentekst\c!linkertekst
               \getvalue\gobbleoneargument
           \od
          %\getvalue{\??tk#1\v!tekst\c!na}%
           \getvalue{\??tk#1\c!na}%
           \kern\zeropoint}% keep the \dp, beware of \vtops, never change this!
        \dp\scratchpagebox\zeropoint
        \box\scratchpagebox}%
     \vskip-#2\relax
   \fi}

\def\dodoplacelayouttextline#1#2#3#4#5#6% \hsize toegevoegd, \hss's niet meer wijzigen
  {\hbox
     {\ifdim\linkerrandbreedte>\zeropoint
        \dododoplacelayouttextline\linkerrandbreedte{#1}\v!rand
          {\hss\getvalue{\??tk#1\v!rand#2}}%
        \hskip\linkerrandafstand
      \fi
      \ifdim\linkermargebreedte>\zeropoint
        \dododoplacelayouttextline\linkermargebreedte{#1}\v!marge
          {\hbox to \linkermargebreedte
             {\hss\getvalue{\??tk#1\v!marge#2}}%
           \hskip-\linkermargebreedte
           \hbox to \linkermargebreedte
             {\hss#5{\??tk#1\v!marge\c!margetekst}}}%
        \hskip\linkermargeafstand
      \fi
      \ifdim\zetbreedte>\zeropoint
        \dododoplacelayouttextline\zetbreedte{#1}\v!tekst
          {\hbox to \zetbreedte
             {\@@nmpre{#5{\??tk#1\v!tekst\c!kantlijntekst}}%
              \getvalue{\??tk#1\v!tekst#2}\hss}%
           \hskip-\zetbreedte
           \hbox to \zetbreedte
             {\hss\getvalue{\??tk#1\v!tekst#3}\hss}%
           \hskip-\zetbreedte
           \hbox to \zetbreedte
             {\hss\getvalue{\??tk#1\v!tekst#4}%
              \@@nmpos{#6{\??tk#1\v!tekst\c!kantlijntekst}}}}%
      \fi
      \ifdim\rechtermargebreedte>\zeropoint
        \hskip\rechtermargeafstand
        \dododoplacelayouttextline\rechtermargebreedte{#1}\v!marge
          {\hbox to \rechtermargebreedte
             {\getvalue{\??tk#1\v!marge#4}\hss}%
           \hskip-\rechtermargebreedte
           \hbox to \rechtermargebreedte
             {#6{\??tk#1\v!marge\c!margetekst}\hss}}%
      \fi
      \ifdim\rechterrandbreedte>\zeropoint
        \hskip\rechterrandafstand
        \dododoplacelayouttextline\rechterrandbreedte{#1}\v!rand
          {\getvalue{\??tk#1\v!rand#4}\hss}%
      \fi}}

\def\dododoplacelayouttextline#1#2#3#4%
  {\vbox % to \vsize
     {\hsize#1\relax
      \getvalue{\??tk#2#3\c!voor}
      \hbox to #1{#4}%
      \getvalue{\??tk#2#3\c!na}}}

%D Although it is far better to use backgrounds for this 
%D purpose, one can add a rule in the following way. This 
%D method makes the rules disappear in case of an empty text 
%D line. Consider this a feature. 
%D
%D \starttypen 
%D \setupheadertexts[left][right]
%D 
%D \setupheader[text][after=\hrule,style=bold]
%D 
%D \starttext
%D   \input tufte \page
%D   \setupheader[state=empty]
%D   \input tufte \page
%D \stoptext
%D \stoptypen 

%D The next twosome will be done differently (using an 
%D existing auxiliary macro).

% \def\@@nmpre#1{\setbox0\hbox{#1}\ifdim\wd0=\zeropoint\else\unhbox0\tfskip\fi}
% \def\@@nmpos#1{\setbox0\hbox{#1}\ifdim\wd0=\zeropoint\else\tfskip\unhbox0\fi}

% cleaner 

\def\@@nmpre#1{\doiftext{#1}{{#1}\tfskip}}
\def\@@nmpos#1{\doiftext{#1}{\tfskip{#1}}}

% newer 

\def\@@nmprepos#1#2#3#4#5%
  {\doifelsenothing\@@nmbreedte
     {\doiftext{#5}{#1{#5}#2}}
     {\doiftext{#5}{\hbox to \@@nmbreedte{#3{#5}#4}}}}

\def\@@nmpre{\@@nmprepos\empty\tfskip\relax\hss}
\def\@@nmpos{\@@nmprepos\tfskip\empty\hss\relax}

%D This code will move to \type {page-flt.tex}. 

\appendtoks
  \plaatsrechtermargeblok \hskip-\rechtermargebreedte
\to \rightmargintextcontent

\appendtoks
  \plaatslinkermargeblok \hskip-\linkermargebreedte
\to \leftmargintextcontent

%D The next hook will later be used for keeping track of 
%D positions, i.e.\ it will provide a proper (page 
%D dependent) reference point. 

\ifx\undefined\placepositionanchors
  \def\placepositionanchors{\vskip\teksthoogte}
\fi

%D \macros
%D   {definetext}
%D 
%D Some macros ago, we implemented the \type {status} option
%D \type {unknown}. This one is used to take care of 
%D symbolic texts handlers. 
%D
%D \showsetup{\y!definetext}
%D 
%D The next example demonstrates how we can use this 
%D mechanism to provide page (event) dependent text lines. 
%D 
%D \starttypen 
%D \definetext[hoofdstuk][voet][paginanummer]
%D \stelkopin[hoofdstuk][hoofd=hoog,voet=hoofdstuk]
%D \setupheadertexts[paginanummer]
%D \setupfootertexts[links][rechts]
%D \hoofdstuk{eerste} \dorecurse{20}{\input tufte \relax}
%D \hoofdstuk{tweede} \dorecurse{20}{\input tufte \relax}
%D \stoptypen 

\def\definetext
  {\doseventupleempty\dodefinetext}

\def\dodefinetext[#1][#2][#3][#4][#5][#6][#7]%
  {\ifseventhargument
     \setvalue{\??tk#2#3#1}{\dosixtupleempty\dosetuptexts[#2][#3][#4][#5][#6][#7]}%
   \else\ifsixthargument
     \setvalue{\??tk  #2#1}{\dosixtupleempty\dosetuptexts[#2][#3][#4][#5][#6]}%
   \else\iffifthargument
     \setvalue{\??tk#2#3#1}{\dosixtupleempty\dosetuptexts[#2][#3][#4][#5]}%
   \else\iffourthargument
     \setvalue{\??tk  #2#1}{\dosixtupleempty\dosetuptexts[#2][#3][#4]}%
   \else
     \setvalue{\??tk  #2#1}{\dosixtupleempty\dosetuptexts[#2][#3]}%
   \fi\fi\fi\fi}

%D The rest of this file is dedicated to setting up the 
%D texts. This code is not that impressive. 

\setupheadertexts [\v!tekst] [] []
\setupheadertexts [\v!marge] [] []
\setupheadertexts [\v!rand]  [] []

\setupfootertexts [\v!tekst] [] []
\setupfootertexts [\v!marge] [] []
\setupfootertexts [\v!rand]  [] []

\setuptexttexts   [\v!tekst] [] []
\setuptexttexts   [\v!marge] [] []
\setuptexttexts   [\v!rand]  [] []

\setupbottomtexts [\v!tekst] [] []
\setupbottomtexts [\v!marge] [] []
\setupbottomtexts [\v!rand]  [] []

\setuptoptexts    [\v!tekst] [] []
\setuptoptexts    [\v!marge] [] []
\setuptoptexts    [\v!rand]  [] []

% alternative
%
% \def\resetlayouttekst%
%   {\dodoubleempty\doresetlayouttekst}
%
% \def\doresetlayouttekst[#1][#2]%
%   {\ifsecondargument
%      \dodoresetlayouttekst[#1][#2]%
%    \else
%      \dodoresetlayouttekst[#1][\v!tekst]%
%    \fi}
%
% \def\dodoresetlayouttekst[#1][#2]%
%   {...}
%
% \def\docommando#1%
%   {\resetlayouttekst[#1][\v!tekst]%
%    \resetlayouttekst[#1][\v!marge]%
%    \resetlayouttekst[#1][\v!rand]}

%D We combine a lot of similar settings in a macro that
%D we will later dispose. 

\def\dodocommando[#1][#2]%
  {\getparameters
     [\??tk#1#2]
     [%\c!status=\v!normaal, % moved 
      \c!voor=,  % both global and local are used  
      \c!na=,    % both global and local are used  
      \c!strut=, % the local one, not (yet) used 
      \c!letter=\getvalue{\??tk#1\c!letter},% hm, got lost
      \c!kleur=\getvalue{\??tk#1\c!kleur},  % hm, got lost
      \c!linkertekst=,
      \c!middentekst=,
      \c!rechtertekst=,
      \c!kantlijntekst=,
      \c!margetekst=,
      \c!breedte=]% 
   \inheritparameter[\??tk#1#2][\c!linkerletter  ][\c!letter ]% 
   \inheritparameter[\??tk#1#2][\c!rechterletter ][\c!letter ]%
   \inheritparameter[\??tk#1#2][\c!linkerkleur   ][\c!kleur  ]%
   \inheritparameter[\??tk#1#2][\c!rechterkleur  ][\c!kleur  ]%
   \inheritparameter[\??tk#1#2][\c!linkerbreedte ][\c!breedte]%
   \inheritparameter[\??tk#1#2][\c!rechterbreedte][\c!breedte]}

\def\docommando#1%
  {\dodocommando[#1][\v!tekst]%
   \dodocommando[#1][\v!marge]%
   \dodocommando[#1][\v!rand]}

\docommando\v!boven
\docommando\v!hoofd
\docommando\v!voet
\docommando\v!tekst
\docommando\v!onder

\let\docommando  \relax
\let\dodocommando\relax

%D While the header and footer lines are moved away from the
%D main text, the top and bottom lines are centered. 

\setuptop   [\c!status=\v!normaal,\c!voor=\vss,\c!na=\vss,\c!strut=]
\setupheader[\c!status=\v!normaal,\c!voor=,    \c!na=\vss,\c!strut=\v!ja]
\setuptext  [\c!status=\v!normaal,\c!voor=\vss,\c!na=\vss,\c!strut=]
\setupfooter[\c!status=\v!normaal,\c!voor=\vss,\c!na=,    \c!strut=\v!ja]
\setupbottom[\c!status=\v!normaal,\c!voor=\vss,\c!na=\vss,\c!strut=]

\setuptop   [\c!letter=,\c!kleur=]
\setupheader[\c!letter=,\c!kleur=]
\setuptext  [\c!letter=,\c!kleur=]
\setupfooter[\c!letter=,\c!kleur=]
\setupbottom[\c!letter=,\c!kleur=]

\protect \endinput
