%D \module
%D   [       file=supp-box,
%D        version=1995.10.10,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Boxes,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Support Macros / Boxes}

\unprotect

\registerctxluafile{supp-box}{1.001}

% This file is partially cleaned up.

% handy to have
%
% \hbox to \hsize
%   {\en
%    \switchnaarkorps[5pt]%
%    \emergencystretch2em
%    \dimen0=\baselineskip
%    \baselineskip=\dimen0 plus 1pt
%    \hsize=.2\hsize
%    \vsize=2\hsize
%    \ruledvbox to \vsize{\input tufte \par}\hss
%    \ruledvbox to \vsize{\input tufte \par\kern-\prevdepth}\hss
%    \ruledvbox to \vsize{\input tufte \par\kern0pt}\hss
%    \ruledvbox to \vsize{\input tufte \par\vfill}\hss
%    \ruledvbox to \vsize{\input tufte \par\kern-\prevdepth\vfill}}
%
% \hbox to \hsize
%   {\en
%    \switchnaarkorps[5pt]%
%    \emergencystretch2em
%    \dimen0=\baselineskip
%    \baselineskip=\dimen0 plus 1pt
%    \hsize=.18\hsize
%    \vsize=2.5\hsize
%    \setbox0=\vbox{\input tufte\relax}%
%    \ruledvbox to \vsize{\unvcopy0}\hss
%    \ruledvbox to \vsize{\unvcopy0\kern-\dp0}\hss
%    \ruledvbox to \vsize{\unvcopy0\kern0pt}\hss
%    \ruledvbox to \vsize{\unvcopy0\vfill}\hss
%    \ruledvbox to \vsize{\unvcopy0\kern-\dp0\vfill}}

%D \macros
%D   {dontcomplain}
%D
%D The next macro suppresses over- and underfull messages which
%D often makes sense when we deal with boxes.

\unexpanded\def\dontcomplain
  {\hbadness\plustenthousand
   \vbadness\plustenthousand
   \hfuzz   \maxdimen
   \vfuzz   \maxdimen}

%D This module implements some box manipulation macros. Some
%D are quite simple, some are more advanced and when understood
%D well, all can be of use.
%D
%D \macros
%D   {strutdp,strutht,strutwd}
%D
%D The next shortcuts save memory and keying. The width is
%D normally zero points (if not, you're in trouble). These
%D shortcuts can be used like a dimension, opposite to the
%D core macros \type {\strutdepth} and alike, which are
%D values.

\def\strutdp  {\dp\strutbox}
\def\strutht  {\ht\strutbox}
\def\strutwd  {\wd\strutbox}
\def\struthtdp{\dimexpr\ht\strutbox+\dp\strutbox\relax}

%D \macros
%D   {voidbox,nextbox}
%D
%D Let's start with an easy one. The next macro hides the
%D ugly \type {@} in \type {\voidb@x}.

\ifdefined\voidbox \else \newbox\voidbox \fi
\ifdefined\nextbox \else \newbox\nextbox \fi

%D \macros
%D   {nextdepth}
%D
%D Let's start with a rather simple declaration. Sometimes we
%D need to save the \TEX\ \DIMENSION\ \type{\prevdepth} and
%D append it later on. The name \type{\nextdepth} suits
%D this purpose well.

\newdimen\nextdepth

%D \macros
%D   {smashbox, smashedbox}
%D
%D Smashing is introduced in \PLAIN\ \TEX, and stands for
%D reducing the dimensions of a box to zero. The most resolute
%D one is presented first.

\unexpanded\def\smashbox#1%
  {\wd#1\zeropoint
   \ht#1\zeropoint
   \dp#1\zeropoint}

\unexpanded\def\smashboxed#1%
  {\wd#1\zeropoint
   \ht#1\zeropoint
   \dp#1\zeropoint
   \box#1\relax}

%D \macros
%D   {hsmashbox,vsmashbox}
%D
%D Smashing can be used for overlaying boxes. Depending on
%D the mode, horizontal or vertical, one can use:

\unexpanded\def\hsmashbox#1%
  {\wd#1\zeropoint}

\unexpanded\def\vsmashbox#1%
  {\ht#1\zeropoint
   \dp#1\zeropoint}

%D The next implementation is less sensitive for spurious
%D spaces.

\newcount\c_boxes_register

\unexpanded\def\smashbox
  {\afterassignment\syst_boxes_smash_boxes_register\c_boxes_register}

\def\syst_boxes_smash_boxes_register
  {\wd\c_boxes_register\zeropoint
   \ht\c_boxes_register\zeropoint
   \dp\c_boxes_register\zeropoint}

\unexpanded\def\hsmashbox
  {\afterassignment\syst_boxes_hsmashed_boxes_register\c_boxes_register}

\def\syst_boxes_hsmashed_boxes_register
  {\wd\c_boxes_register\zeropoint}

\unexpanded\def\vsmashbox
  {\afterassignment\syst_boxes_vsmashed_boxes_register\c_boxes_register}

\def\syst_boxes_vsmashed_boxes_register
  {\ht\c_boxes_register\zeropoint
   \dp\c_boxes_register\zeropoint}

\unexpanded\def\smashedbox
  {\afterassignment\syst_boxes_smashed_boxes_register\c_boxes_register}

\unexpanded\def\syst_boxes_smashed_boxes_register
  {\wd\c_boxes_register\zeropoint
   \ht\c_boxes_register\zeropoint
   \dp\c_boxes_register\zeropoint
   \box\c_boxes_register}

%D \macros
%D   {hsmash,vsmash,
%D    hsmashed,vsmashed}
%D
%D While the previous macros expected a \BOX, the next act on a
%D content. They are some subtle differences betreen the smash
%D and smashed alternatives. The later ones reduce all
%D dimensions to zero.

\unexpanded\def\hsmash  {\bgroup\dowithnextboxcs\syst_boxes_hsmashed_nextbox\hbox}
\unexpanded\def\vsmash  {\bgroup\dowithnextboxcs\syst_boxes_vsmashed_nextbox\vbox}
\unexpanded\def\hsmashed{\bgroup\dowithnextboxcs\syst_boxes_smashed_nextbox \hbox}
\unexpanded\def\vsmashed{\bgroup\dowithnextboxcs\syst_boxes_smashed_nextbox \vbox}

\unexpanded\def\syst_boxes_hsmashed_nextbox
  {\wd\nextbox\zeropoint
   \box\nextbox
   \egroup}

\unexpanded\def\syst_boxes_vsmashed_nextbox
  {\ht\nextbox\zeropoint
   \dp\nextbox\zeropoint
   \box\nextbox
   \egroup}

\unexpanded\def\syst_boxes_smashed_nextbox
  {\ht\nextbox\zeropoint
   \dp\nextbox\zeropoint
   \wd\nextbox\zeropoint
   \box\nextbox
   \egroup}

%D \macros
%D   {smashedhbox,smashedvbox}
%D
%D Also handy (all dimensions zeroed):
%D
%D \starttyping
%D \smashedhbox to ... {...}
%D \smashedvbox to ... {...}
%D \stoptyping

\unexpanded\def\smashedhbox{\hbox\bgroup\dowithnextboxcs\syst_boxes_smashed_nextbox\hbox}
\unexpanded\def\smashedvbox{\vbox\bgroup\dowithnextboxcs\syst_boxes_smashed_nextbox\vbox}

%D First we define a helper. We use a \LUATEX\ feature in order to avoid
%D mathpalettes.

\newcount\c_boxes_math_style

\unexpanded\def\syst_boxes_math_set_nextbox#1%
  {\c_boxes_math_style\mathstyle
   \setbox\nextbox\hbox{$\mathsurround\zeropoint\triggermathstyle\c_boxes_math_style{#1}$}}

%D \macros
%D   {smash}
%D
%D This smash alternative takes an optional arg [whdtb] as
%D well as is potentially catcode safer. It is needed by the
%D math module (although the \type {\leavevmode} is not added
%D here).

\unexpanded\def\smash
  {\begingroup
   \futurelet\nexttoken\syst_boxes_smash}

\def\syst_boxes_smash
  {\ifx\nexttoken[%
     \expandafter\syst_boxes_smash_yes
   \else
     \expandafter\syst_boxes_smash_nop
   \fi}

\def\syst_boxes_smash_nop
  {\edef\m_boxes_smash_options{hd}%
   \futurelet\nexttoken\syst_boxes_smash_indeed}

\def\syst_boxes_smash_yes[#1]%
  {\edef\m_boxes_smash_options{#1}%
   \futurelet\nexttoken\syst_boxes_smash_indeed}

\def\syst_boxes_smash_indeed
  {\ifmmode
     \expandafter\syst_boxes_smash_math
   \else\ifx\nexttoken\bgroup
     \doubleexpandafter\syst_boxes_smash_hbox
   \else
     \doubleexpandafter\syst_boxes_smash_text
   \fi\fi}

\def\syst_boxes_smash_math#1%
  {\syst_boxes_math_set_nextbox{#1}%
   \syst_boxes_smash_process}

\def\syst_boxes_smash_hbox
  {\dowithnextboxcs\syst_boxes_smash_process\hbox}

\def\syst_boxes_smash_text#1%
  {\setbox\nextbox\hbox{#1}%
   \syst_boxes_smash_process}

\def\syst_boxes_smash_process
  {\expandafter\syst_boxes_smash_process_option\m_boxes_smash_options\relax
   \box\nextbox
   \endgroup}

\def\syst_boxes_s_w{\wd\nextbox\zeropoint}
\def\syst_boxes_s_h{\ht\nextbox\zeropoint}
\def\syst_boxes_s_d{\dp\nextbox\zeropoint}
\def\syst_boxes_s_t{\ht\nextbox\zeropoint}
\def\syst_boxes_s_b{\dp\nextbox\zeropoint}

\def\syst_boxes_smash_process_option#1%
  {\ifx#1\relax\else
     \csname boxes_s_#1\endcsname
     \expandafter\syst_boxes_smash_process_option
   \fi}

%D \starttabulate[|l|l|]
%D \NC w   \NC \ruledhbox{\smash  [w]{This is some great smashing, isn't it?}} \NC \NR
%D \NC h   \NC \ruledhbox{\smash  [h]{This is some great smashing, isn't it?}} \NC \NR
%D \NC d   \NC \ruledhbox{\smash  [d]{This is some great smashing, isn't it?}} \NC \NR
%D \NC tb  \NC \ruledhbox{\smash [tb]{This is some great smashing, isn't it?}} \NC \NR
%D \NC whd \NC \ruledhbox{\smash[whd]{This is some great smashing, isn't it?}} \NC \NR
%D \stoptabulate

%D \macros
%D   {phantom, hphantom, vphantom, mathstrut}
%D
%D The next implementation of \type {\phantom} cum suis does
%D not grab an argument in the non||math case, which is better.
%D
%D Due to a complicated call to \type {\mathpallete} and
%D thereby \type {\mathchoice}, the next macro looks ugly.
%D We also take care of non||braced arguments.

\unexpanded\def\phantom {\begingroup\futurelet\nexttoken\syst_boxes_phantom_indeed  }
\unexpanded\def\vphantom{\begingroup\futurelet\nexttoken\syst_boxes_phantom_indeed_v}
\unexpanded\def\hphantom{\begingroup\futurelet\nexttoken\syst_boxes_phantom_indeed_h}

\def\syst_boxes_phantom_math  #1{\syst_boxes_math_set_nextbox{#1}\syst_boxes_phantom_make  }
\def\syst_boxes_phantom_math_v#1{\syst_boxes_math_set_nextbox{#1}\syst_boxes_phantom_make_v}
\def\syst_boxes_phantom_math_h#1{\syst_boxes_math_set_nextbox{#1}\syst_boxes_phantom_make_h}

\def\syst_boxes_phantom_hbox  {\dowithnextboxcs\syst_boxes_phantom_make  \hbox} % always hbox
\def\syst_boxes_phantom_hbox_v{\dowithnextboxcs\syst_boxes_phantom_make_v\hbox} % always hbox
\def\syst_boxes_phantom_hbox_h{\dowithnextboxcs\syst_boxes_phantom_make_h\hbox} % always hbox

\def\syst_boxes_phantom_text  #1{\setbox\nextbox\hbox{#1}\syst_boxes_phantom_make  } % always hbox
\def\syst_boxes_phantom_text_v#1{\setbox\nextbox\hbox{#1}\syst_boxes_phantom_make_v} % always hbox
\def\syst_boxes_phantom_text_h#1{\setbox\nextbox\hbox{#1}\syst_boxes_phantom_make_h} % always hbox

\def\syst_boxes_phantom_indeed
  {\ifmmode
     \expandafter\syst_boxes_phantom_math
   \else\ifx\nexttoken\bgroup
     \doubleexpandafter\syst_boxes_phantom_hbox
   \else
     \doubleexpandafter\syst_boxes_phantom_text
   \fi\fi}

\def\syst_boxes_phantom_indeed_v
  {\ifmmode
     \expandafter\syst_boxes_phantom_math_v
   \else\ifx\nexttoken\bgroup
     \doubleexpandafter\syst_boxes_phantom_hbox_v
   \else
     \doubleexpandafter\syst_boxes_phantom_text_v
   \fi\fi}

\def\syst_boxes_phantom_indeed_h
  {\ifmmode
     \expandafter\syst_boxes_phantom_math_h
   \else\ifx\nexttoken\bgroup
     \doubleexpandafter\syst_boxes_phantom_hbox_h
   \else
     \doubleexpandafter\syst_boxes_phantom_text_h
   \fi\fi}

\def\syst_boxes_phantom_make
  {\setbox\scratchbox\emptyhbox
   \ht\scratchbox\ht\nextbox
   \dp\scratchbox\dp\nextbox
   \wd\scratchbox\wd\nextbox
   \box\scratchbox
   \endgroup}

\def\syst_boxes_phantom_make_v
  {\setbox\scratchbox\emptyhbox
   \ht\scratchbox\ht\nextbox
   \dp\scratchbox\dp\nextbox
   \box\scratchbox
   \endgroup}

\def\syst_boxes_phantom_make_h
  {\setbox\scratchbox\emptyhbox
   \wd\scratchbox\wd\nextbox
   \box\scratchbox
   \endgroup}

%D We also define plain's \type {\mathstrut}.

\unexpanded\def\mathstrut{\vphantom(} % can be made faster by inlining

%D \macros
%D   {getboxheight}
%D
%D Although often needed, \TEX\ does not support arithmics
%D like:
%D
%D \starttyping
%D \dimen0 = \ht0 + \dp0
%D \stoptyping
%D
%D so we implemented:
%D
%D \starttyping
%D \getboxheight ... \of \box...
%D \stoptyping
%D
%D For instance,
%D
%D \starttyping
%D \getboxheight \dimen0 \of \box0
%D \getboxheight \someheight \of \box \tempbox
%D \stoptyping
%D
%D The implementation is rather stupid:
%D
%D \starttyping
%D \def\getboxheight#1\of#2\box#3%
%D   {#1\ht#3\advance#1\dp#3\relax}
%D \stoptyping
%D
%D The next alternative is slightly more clever, since
%D it accepts \type {{12}} as well as \type {12} as box
%D number.

\unexpanded\def\getboxheight#1\of#2\box#3%
  {\def\next{#1\dimexpr\ht\c_boxes_register+\dp\c_boxes_register\relax}%
   \afterassignment\next\c_boxes_register=#3}

%D For a long time the following three macros were part of
%D the grid snapping core module, but it makes more sense to
%D have them here so that users can see them.
%D
%D \macros
%D   {getnoflines, getroundednoflines, getrawnoflines}
%D
%D Het commando \type{\getnoflines} converteert een hoogte
%D (dimensie) in een aantal regels en kent dit toe aan
%D \type{\noflines}.
%D
%D \starttyping
%D \getnoflines{dimensie}
%D \stoptyping
%D
%D Er wordt gedeeld door \type{\openlineheight} en een hoogte
%D van~0pt komt overeen met 0~regels. The raw alternative
%D does not round.

%D For a long time we had:
%D
%D \starttyping
%D \newcount\noflines
%D \newdimen\noflinesheight
%D
%D \def\dogetnoflines#1#2%
%D   {\noflinesheight#2\relax
%D    \ifzeropt\noflinesheight % \ifdim\noflinesheight=\zeropoint
%D      \noflines\zerocount
%D    \else
%D      \divide\noflinesheight \openlineheight
%D      \noflines\noflinesheight
%D      #1\ifdim\noflines\openlineheight=#2\relax \else
%D        \advance\noflines\ifdim#2>\zeropoint\plusone\else\minusone\fi
%D      \fi\fi
%D    \fi}
%D
%D \def\getnoflines   {\dogetnoflines\iftrue } % compensated
%D \def\getrawnoflines{\dogetnoflines\iffalse} % no compensation
%D \stoptyping
%D
%D A more recent variant is:

\ifx\roundingeps\undefined \newdimen\roundingeps \roundingeps=10sp \fi

\newcount\noflines
\newdimen\noflinesheight

\unexpanded\def\getnoflines#1%
  {\noflinesheight#1\relax
   \ifzeropt\noflinesheight
     \noflines\zerocount
   \else\ifdim\noflinesheight>\zeropoint
     \advance\noflinesheight-\roundingeps
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
     \advance\noflines\plusone
   \else
     \advance\noflinesheight\roundingeps
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
     \advance\noflines\minusone
   \fi\fi}

\unexpanded\def\getroundednoflines#1%
  {\noflinesheight#1\relax
   \ifzeropt\noflinesheight
     \noflines\zerocount
   \else\ifdim\noflinesheight>\zeropoint
     \advance\noflinesheight\roundingeps
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
   \else
     \advance\noflinesheight-\roundingeps
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
   \fi\fi}

\unexpanded\def\getrawnoflines#1%
  {\noflinesheight#1\relax
   \ifzeropt\noflinesheight
     \noflines\zerocount
   \else\ifdim\noflinesheight>\zeropoint
     \advance\noflinesheight\roundingeps
     \advance\noflinesheight.5\openlineheight
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
   \else
     \advance\noflinesheight-\roundingeps
     \advance\noflinesheight-.5\openlineheight
     \divide\noflinesheight\openlineheight
     \noflines\noflinesheight
   \fi\fi}

%D Let's proof that it works:
%D
%D \startbuffer
%D \scratchdimen\dimexpr(3pt)               \getnoflines\scratchdimen  1=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight)     \getnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.1\lineheight)   \getnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.5\lineheight)   \getnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.9\lineheight)   \getnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight+3pt) \getnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight+3sp) \getnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight-3sp) \getnoflines\scratchdimen 10=\the\noflines \endgraf
%D
%D \scratchdimen\dimexpr(3pt)               \getrawnoflines\scratchdimen  0=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight)     \getrawnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.1\lineheight)   \getrawnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.5\lineheight)   \getrawnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10.9\lineheight)   \getrawnoflines\scratchdimen 11=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight+3pt) \getrawnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight+3sp) \getrawnoflines\scratchdimen 10=\the\noflines \endgraf
%D \scratchdimen\dimexpr(10\lineheight-3sp) \getrawnoflines\scratchdimen 10=\the\noflines \endgraf
%D \stopbuffer
%D
%D \typebuffer \getbuffer

%D \macros
%D   {determinenoflines}
%D
%D The next macro determines the number of lines and
%D returns it it \type {\noflines}. The macro works
%D reasonable well as long as the content can be unboxed.
%D
%D \starttyping
%D \determinenoflines{test\\test}
%D \determinenoflines{\bfd test\\test}
%D \determinenoflines{\definedfont[Sans at 40pt]test\\test}
%D \stoptyping

\def\dodeterminenoflines % can be mkiv'd
  {\beginofshapebox
     \unvbox\nextbox
   \endofshapebox
   % \global\count1\zerocount
   % \reshapebox{\global\advance\count1\plusone}%
   % \egroup\noflines\count1 }%
   \scratchcounter\zerocount
   \reshapebox{\global\advance\scratchcounter\plusone}%
   \expandafter\egroup\expandafter\noflines\the\scratchcounter\relax}

\unexpanded\def\determinenoflines
  {\bgroup
   \forgetall
   \let\crlf\endgraf
   \let\\\endgraf
   \dowithnextboxcs\dodeterminenoflines\vbox}

%D \macros
%D   {doiftextelse, doiftext}
%D
%D When \type {\doifelse} cum suis hopelessly fail, for
%D instance because we pass data, we can fall back on the next
%D macro:
%D
%D \starttyping
%D \doiftextelse {data} {then branch} {else branch}
%D \doiftext     {data} {then branch}
%D \stoptyping

\unexpanded\def\doiftextelse#1%
  {\begingroup
   \setbox\scratchbox\hbox
     {\settrialtypesetting
      \ignorespaces#1\removeunwantedspaces}%
   \ifzeropt\wd\scratchbox
     \endgroup\expandafter\secondoftwoarguments
   \else
     \endgroup\expandafter\firstoftwoarguments
   \fi}

\unexpanded\def\doiftext#1%
  {\begingroup
   \setbox\scratchbox\hbox
     {\settrialtypesetting
      \ignorespaces#1\removeunwantedspaces}%
   \ifzeropt\wd\scratchbox
     \endgroup\expandafter\gobbleoneargument
   \else
     \endgroup\expandafter\firstofoneargument
   \fi}

%D \macros
%D   {dowithnextbox,nextbox}
%D
%D Sometimes we want a macro to grab a box and do something
%D on the content. One could pass an argument to a box, but
%D this can violate the specific \CATCODES\ of its content and
%D leads to unexpected results. The next macro treats the
%D following braced text as the content of a box and
%D manipulates it afterwards in a predefined way.
%D
%D The first argument specifies what to do with the content.
%D This content is available in \type{\nextbox}. The second
%D argument is one of \type{\hbox}, \type{\vbox} or
%D \type{\vtop}. The third argument must be grouped with
%D \type{\bgroup} and \type{\egroup}, \type{{...}} or can be
%D a \type{\box} specification.
%D
%D In \CONTEXT\ this macro is used for picking up a box and
%D treating it according to earlier specifications. We use for
%D instance something like:
%D
%D \starttyping
%D \def\getfloat%
%D   {\def\handlefloat{...\box\nextbox...}
%D    \dowithnextboxcs\handlefloat\vbox}
%D \stoptyping
%D
%D instead of:
%D
%D \starttyping
%D \def\getfloat#1%
%D   {...#1...}
%D \stoptyping
%D
%D In this implementation the \type{\aftergroup} construction
%D is needed because \type{\afterassignment} is executed inside
%D the box.

\unexpanded\def\dowithnextbox#1%
  {\def\syst_boxes_with_next_box{#1}%
   \afterassignment\syst_boxes_with_next_box_indeed
   \setbox\nextbox}

\def\syst_boxes_with_next_box_indeed
  {\aftergroup\syst_boxes_with_next_box}

\unexpanded\def\dowithnextboxcs#1%
  {\let\syst_boxes_with_next_box#1%
   \afterassignment\syst_boxes_with_next_box_indeed
   \setbox\nextbox}

%D So in fact we get:
%D
%D \starttyping
%D \setbox\nextbox { \aftergroup\syst_boxes_with_next_box ... }
%D \stoptyping
%D
%D or
%D
%D \starttyping
%D \setbox\nextbox { ... } \syst_boxes_with_next_box
%D \stoptyping
%D
%D A slower but more versatile implementation is:
%D
%D \starttyping
%D \long\def\dowithnextbox#1#2%
%D   {\long\def\syst_boxes_with_next_box{#1}%
%D    \ifx#2\hbox
%D      \afterassignment\syst_boxes_with_next_box_indeed
%D    \else\ifx#2\vbox
%D      \afterassignment\syst_boxes_with_next_box_indeed
%D    \else\ifx#2\normalvtop
%D      \afterassignment\syst_boxes_with_next_box_indeed
%D    \else\ifx#2\normalvcenter
%D      \afterassignment\syst_boxes_with_next_box_indeed
%D    \else
%D      \afterassignment\syst_boxes_with_next_box
%D    \fi\fi\fi\fi
%D    \setbox\nextbox#2}
%D \stoptyping
%D
%D This alternative also accepts \type{\box0} and alike, but
%D we don't really need this functionality now.

%D \macros
%D   {nextboxht,nextboxwd,nextboxdp,flushnextbox}
%D
%D The next couple of shortcuts saves us memory as well as
%D \type {{}}'s in passing parameters.

\def\nextboxht   {\ht\nextbox}
\def\nextboxwd   {\wd\nextbox}
\def\nextboxdp   {\dp\nextbox}
\def\nextboxhtdp {\dimexpr\ht\nextbox+\dp\nextbox\relax}

\unexpanded\def\flushnextbox{\box\nextbox}

%D \macros
%D   {dowithnextboxcontent}
%D
%D But, occasionally we do need to pass some local settings
%D without wanting to use additional grouping. Therefore we
%D provide:
%D
%D \starttyping
%D \dowithnextboxcontent{inside}{after}{box content}
%D \stoptyping
%D
%D {\em todo: Search source for potential usage!}

\unexpanded\def\dowithnextboxcontent#1#2% inside, after
  {\def\syst_boxes_with_next_box_one{#2}%
   \def\syst_boxes_with_next_box_two{#1}%
   \afterassignment\syst_boxes_with_next_box_content_indeed
   \setbox\nextbox}

\unexpanded\def\dowithnextboxcontentcs#1#2% inside, after
  {\let\syst_boxes_with_next_box_one#2%
   \let\syst_boxes_with_next_box_two#1%
   \afterassignment\syst_boxes_with_next_box_content_indeed
   \setbox\nextbox}

\def\syst_boxes_with_next_box_content_indeed
  {\syst_boxes_with_next_box_two\aftergroup\syst_boxes_with_next_box_one}

%D \macros
%D   {llap, rlap, tlap, blap, clap}
%D
%D Some well known friends, but we implement them our own
%D way. We want the macros to work in both math and text mode.

\def\dodorlap{\hbox to \zeropoint{\box\nextbox\normalhss}\endgroup}
\def\dodollap{\hbox to \zeropoint{\normalhss\box\nextbox}\endgroup}
\def\dodoclap{\hbox to \zeropoint{\normalhss\box\nextbox\normalhss}\endgroup}

\def\dorlap{\begingroup\dowithnextboxcs\dodorlap\hbox}
\def\dollap{\begingroup\dowithnextboxcs\dodollap\hbox}
\def\doclap{\begingroup\dowithnextboxcs\dodoclap\hbox}

\def\domathclap{\mathpalette\dodomathclap} \def\dodomathclap#1#2{\doclap{$\mathsurround\zeropoint#1#2$}}
\def\domathllap{\mathpalette\dodomathllap} \def\dodomathllap#1#2{\dollap{$\mathsurround\zeropoint#1#2$}}
\def\domathrlap{\mathpalette\dodomathrlap} \def\dodomathrlap#1#2{\dorlap{$\mathsurround\zeropoint#1#2$}}

\unexpanded\def\rlap{\mathortext\domathrlap\dorlap}
\unexpanded\def\llap{\mathortext\domathllap\dollap}
\unexpanded\def\clap{\mathortext\domathclap\doclap}

\def\dodotlap{\vbox to \zeropoint{\normalvss\box\nextbox}\endgroup}
\def\dodoblap{\vbox to \zeropoint{\box\nextbox\normalvss}\endgroup}

\unexpanded\def\tlap{\begingroup\dowithnextboxcs\dodotlap\vbox}
\unexpanded\def\blap{\begingroup\dowithnextboxcs\dodoblap\vbox}

%D \macros
%D   {beginofshapebox,
%D    reshapebox, doreshapebox,
%D    flushshapebox,
%D    innerflushshapebox,
%D    shapebox,
%D    ifreshapingbox}
%D
%D The next utility macro originates from some linenumbering
%D mechanism. Due to \TEX's advanced way of typesetting
%D paragraphs, it's not easy to do things on a line||by||line
%D basis. This macro is able to reprocess a given box and can
%D act upon its vertical boxed components, such as lines. The
%D unwinding sequence in this macro is inspired by a \NTG\
%D workshop of David Salomon in June 1992.
%D
%D First we have to grab the piece of text we want to act
%D upon. This is done by means of the duo macros:
%D
%D \starttyping
%D \beginofshapebox
%D a piece of text
%D \endofshapebox
%D \stoptyping
%D
%D When all texts is collected, we can call \type{\reshapebox}
%D and do something with it's vertical components. We can make
%D as much passes as needed. When we're done, the box can be
%D unloaded with \type{\flushshapebox}. The only condition in
%D this scheme is that \type{\reshapebox} must somehow unload
%D the \BOX\ \type{\shapebox}.
%D
%D An important aspect is that the content is unrolled
%D bottom||up. The next example illustrates this maybe
%D unexpected characteristic.
%D
%D \startbuffer
%D \beginofshapebox
%D \em \input tufte
%D \endofshapebox
%D
%D \newcounter\LineNumber
%D
%D \reshapebox
%D   {\doglobal\increment\LineNumber
%D    \hbox{\llap{\LineNumber\hskip2em}\box\shapebox}}
%D
%D \flushshapebox
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D As we can see, when some kind of numbering is done, we have
%D to add a second pass.
%D
%D \startbuffer
%D \newcounter\LineNumber
%D \newcounter\NumberOfLines
%D
%D \reshapebox
%D   {\doglobal\increment\NumberOfLines
%D    \box\shapebox}
%D
%D \reshapebox
%D   {\doglobal\increment\LineNumber
%D    \hbox
%D      {\llap{\LineNumber\ (\NumberOfLines)\hskip2em}%
%D       \box\shapebox}%
%D    \doglobal\decrement\NumberOfLines}
%D
%D \flushshapebox
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D This example shows that the content of the box is still
%D available after flushing. Another feature is that only the
%D last reshaping counts. Multiple reshaping can be done by:
%D
%D \startbuffer
%D \beginofshapebox
%D \flushshapebox
%D \endofshapebox
%D
%D \reshapebox
%D   {\doglobal\increment\LineNumber
%D    \hbox{\llap{$\star$\hskip1em}\box\shapebox}%
%D    \doglobal\decrement\NumberOfLines}
%D
%D \flushshapebox
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D The macros are surprisingly easy to follow and in fact
%D introduce no new concepts. Nearly all books on \TEX\ show
%D similar solutions for unwinding \BOXES.
%D
%D Some macros, like footnote ones, can be sensitive for
%D reshaping, which can result in an endless loop. We
%D therefore offer:
%D
%D \starttyping
%D \ifreshapingbox
%D \stoptyping
%D
%D Some \CONTEXT\ commands are protected this way. Anyhow,
%D reshaping is aborted after 100 dead cycles.
%D
%D By the way, changing the height and depth of \BOX\
%D \type{\shapebox} results in bad spacing. This means that
%D for instance linenumbers etc. should be given zero height
%D and depth before being lapped into the margin. The
%D previous examples ignore this side effect, but beware!

\newif    \ifsomeshapeleft
\newif    \ifreshapingbox

\newbox   \shapebox
\newcount \shapepenalty
\newdimen \shapekern
\newskip  \shapeskip

\newbox   \newshapebox
\newbox   \oldshapebox

\newcount \shapecounter

\newevery \everyshapebox \relax

\def\shapesignal{.12345678pt} % or 12345sp

\unexpanded\def\reshapebox#1%
  {\doreshapebox
     {#1}%
     {\penalty\shapepenalty}%
     {\kern   \shapekern   }%
     {\vskip  \shapeskip   }}

\newbox\tmpshapebox

\newif\ifreshapingfailed % may save redundant runs

\def\doreshapebox#1#2#3#4% \shapebox, \shapepenalty, \shapekern, \shapeskip
  {\global\reshapingfailedfalse
   \ifzeropt\ht\oldshapebox % \ifdim\ht\oldshapebox=\zeropoint
     \setbox\newshapebox\emptyvbox
   \else
     \setbox\newshapebox\vbox
       {\unvcopy\oldshapebox
        \setbox\newshapebox\emptybox
        \shapecounter\zerocount
        \doloop{\dodoreshapebox{#1}{#2}{#3}{#4}}}%
     \setbox\newshapebox\box\tmpshapebox
   \fi}

\ifx\originalshapebox\undefined \let\originalshapebox\oldshapebox \fi

% We will turn this into a \MKIV\ variant.

\unexpanded\def\insertshapesignal
  {\hbox to \shapesignal{\strut\hss}% plus \strut
   \prevdepth\strutdp} % never \nointerlineskip

\unexpanded\def\restoreshapebox % compensates for the signal
  {\global\setbox\tmpshapebox\vbox{\vskip-\lineheight\unvcopy\oldshapebox}}

\def\dodoreshapebox#1#2#3#4% \shapebox, \shapepenalty, \shapekern, \shapeskip
  {\ifnum\lastnodetype=\gluenodecode
     \shapeskip\lastskip
     \global\setbox\tmpshapebox\vbox{#4\unvbox\tmpshapebox}%
     \unskip
   \else\ifnum\lastnodetype=\kernnodecode
     \shapekern\lastkern
     \global\setbox\tmpshapebox\vbox{#3\unvbox\tmpshapebox}%
     \unkern
   \else\ifnum\lastnodetype=\penaltynodecode
     \shapepenalty\lastpenalty
     \global\setbox\tmpshapebox\vbox{#2\unvbox\tmpshapebox}%
     \unpenalty
   \else\ifnum\lastnodetype<\zeropoint
     \exitloop
   \else
     \setbox\shapebox\lastbox
     \ifvoid\shapebox
     \else\ifdim\wd\shapebox=\shapesignal\relax
       \exitloop
     \else
       \shapecounter\zerocount
       \global\setbox\tmpshapebox\vbox{#1\unvbox\tmpshapebox}%
     \fi\fi
   \fi\fi\fi\fi
   \ifnum\shapecounter>100 % can be less
     \global\reshapingfailedtrue
     \message{!!forced exit from shapebox \the\lastnodetype !!}%
     \restoreshapebox
     \exitloop
   \else
     \advance\shapecounter \plusone
   \fi}

\unexpanded\def\beginofshapebox
  {\setbox\oldshapebox\vbox
     \bgroup
     \reshapingboxtrue
     \the\everyshapebox
     \insertshapesignal}

\unexpanded\def\endofshapebox
  {\endgraf
   \egroup}

\let\beginshapebox\beginofshapebox
\let\endshapebox  \endofshapebox

\unexpanded\def\flushshapebox
  {\bgroup
   \ifzeropt\ht\newshapebox % \ifdim\ht\newshapebox=\zeropoint
   \else
     % make \prevdepth legal
     % \par before the next \vskip gives far worse results
     \ifdim\parskip>\zeropoint\vskip\parskip\else\par\fi
     % and take a look
     \ifdim\prevdepth=-\thousandpoint
       \prevdepth\zeropoint
     \fi
     \ifdim\prevdepth<\zeropoint\relax
       % something like a line or a signal or ...
       \donetrue
     \else\ifinner
       % not watertight and not ok
       \donefalse
     \else\ifdim\pagegoal=\maxdimen
       \donetrue
     \else
       % give the previous line a normal depth
       \donetrue
       {\forgeteverypar\verticalstrut}\nobreak
       \kern-\struttotal % geen \vskip
       \kern-\parskip
     % \vskip-\strutdp
     \fi\fi\fi
     \scratchdimen\dp\newshapebox
     \unvbox\newshapebox
     % \prevdepth=0pt and \dp\newshapebox depend on last line
     \kern-\scratchdimen % ??
     % now \prevdepth=0pt
     \ifdone
       \kern\strutdp
       \prevdepth\strutdp
     \fi
   \fi
   \egroup}

%D In real inner situations we can use:
%D
%D \starttyping
%D \flushinnershapebox
%D \stoptyping
%D
%D This one is used in \type{\framed}.

% The kern fails on for instance:
%
% \omlijnd[offset=0pt,hoogte=8mm,uitlijnen={rechts,laho}]{\bfa test}

\unexpanded\def\innerflushshapebox
  {\ifzeropt\ht\newshapebox \else
     \unvcopy\newshapebox\relax % unvcopy ! else spacing problem
   % \kern-\dp\newshapebox\relax
   \fi}

%D For absolute control, one can use \type{\doreshapebox}
%D directly. This macro takes four arguments, that take care
%D of:
%D
%D \startitemize[n,packed]
%D \item \type{\shapebox}
%D \item \type{\shapepenalty}
%D \item \type{\shapekern}
%D \item \type{\shapeskip}
%D \stopitemize

%D \macros
%D   {shapedhbox}
%D
%D When constructing a new box, using the content of \type
%D {\shapebox}, one can best use \type {\shapedhbox} instead
%D of \type {\hbox}, since it manages the height and depth of
%D the line.

\unexpanded\def\shapedhbox        % lines with non strutted dimensions have
  {\expanded{\dowithnextbox       % interlineskip so if we want the original
     {\dp\nextbox\the\ht\shapebox % spacing, we need to preserve the original
      \dp\nextbox\the\dp\shapebox % height and depth which is definitely
      \box\nextbox}}              % needed if we apply struts to the 'new'
   \hbox}                   % box or do something that changed ist size

%D \macros
%D   {hyphenatedword,
%D    hyphenatedpar,
%D    hyphenatedfile,
%D    dohyphenateword}
%D
%D We no longer use the pure \TEX\ variant. In due time we will
%D report some more advanced statistics.
%D
%D \starttyping
%D \showhyphens{dohyphenatedword}
%D \stoptyping

\def\doshowhyphenatednextbox
  {\ctxcommand{showhyphenatedinlist(tex.box[\number\nextbox].list)}}

\unexpanded\def\showhyphens{\dowithnextboxcs\doshowhyphenatednextbox\hbox}

%D The following macros are seldom used but handy for tracing.
%D
%D \starttyping
%D \hyphenatedword{dohyphenatedword}
%D \hyphenatedpar {\dorecurse{10}{dohyphenatedword }}
%D \hyphenatedfile{tufte}
%D \stoptyping

\def\dohyphenatednextbox
  {\ctxcommand{hyphenatedlist(tex.box[\number\nextbox])}%
   \unhbox\nextbox}

\unexpanded\def\hyphenatedword  {\dowithnextboxcs\dohyphenatednextbox    \hbox}
\unexpanded\def\hyphenatedpar   {\dowithnextboxcs\dohyphenatednextbox    \hbox}
\unexpanded\def\hyphenatedfile#1{\dowithnextboxcs\dohyphenatednextbox    \hbox{\readfile{#1}\donothing\donothing}}

%D \macros
%D   {processtokens}
%D
%D We fully agree with (most) typographers that inter||letter
%D spacing is only permitted in fancy titles, we provide a
%D macro that can be used to do so. Because this is
%D (definitely and fortunately) no feature of \TEX, we have to
%D step through the token list ourselves.
%D
%D \starttyping
%D \processtokens {before} {between} {after} {space} {tokens}
%D \stoptyping
%D
%D An example of a call is:
%D
%D \startbuffer
%D \processtokens {[} {+} {]} {\space} {hello world}
%D \stopbuffer
%D
%D \typebuffer
%D
%D This results in:
%D
%D \getbuffer
%D
%D The list of tokens may contain spaces, while \type{\\},
%D \type{{}} and \type{\ } are handled as space too.

\unexpanded\def\processtokens#1#2#3#4#5%
  {\begingroup
   \def\lastcharacter{\lastcharacter}%
   \def\space{ }%
   \let\\=\space
   \def\before {#1}%
   \def\between{#2}%
   \def\after  {#3}%
   \def\white  {#4}%
   \let\savedbefore\before
   \doprocesstokens#5\lastcharacter
   \endgroup}

\def\doprocesstokens% the space after = is essential
  {\afterassignment\dodoprocesstokens\let\nextprocessedtoken= }

\def\dodoprocesstokens
  {\ifx\nextprocessedtoken\lastcharacter
     \after
     \let\nextprocessedtoken\relax
   \else\ifx\nextprocessedtoken\bgroup
     \def\nextprocessedtoken
       {\dowithnextbox
          {\before{\copy\nextbox}% \before can use nextbox several times
           \let\before\between
           \doprocesstokens}
           \hbox\bgroup}%
   \else
     \expandafter\if\space\nextprocessedtoken
       \after\white
       \let\before\savedbefore
     \else
       \before\nextprocessedtoken
       \let\before\between
     \fi
     \let\nextprocessedtoken\doprocesstokens
   \fi\fi
   \nextprocessedtoken}

%D \macros
%D   {doboundtext}
%D
%D Sometimes there is not enough room to show the complete
%D (line of) text. In such a situation we can strip of some
%D characters by using \type{\doboundtext}. When the text is
%D wider than the given width, it's split and the third
%D argument is appended. When the text to be checked is packed
%D in a command, we'll have to use \type{\expandafter}.
%D
%D \starttyping
%D \doboundtext{a very, probably to long, text}{3cm}{...}
%D \stoptyping
%D
%D When calculating the room needed, we take the width of the
%D third argument into account, which leads to a bit more
%D complex macro than needed at first sight.

\def\dodoboundtext#1%
  {\setbox0\hbox{#1}%
   \advance\scratchdimen -\wd0
   \ifdim\scratchdimen>\zeropoint\relax#1\fi}%

\def\doboundtext#1#2#3% still used?
  {\hbox
     {\setbox\scratchbox\hbox{#1}%
      \scratchdimen#2\relax
      \ifdim\wd\scratchbox>\scratchdimen
        \setbox\scratchbox\hbox{#3}%
        \advance\scratchdimen -\wd\scratchbox
        \handletokens#1\with\dodoboundtext
      \fi
      \box\scratchbox}}

%D \macros
%D   {limitatetext}
%D
%D A bit more beautiful alternative for the previous command is
%D the next one. This command is more robust because we let
%D \TEX\ do most of the job. The previous command works better
%D on text that cannot be hyphenated.
%D
%D \starttyping
%D \limitatetext {text}  {width} {sentinel}
%D \limitatetext {text} {-width} {prelude}
%D \stoptyping
%D
%D When no width is given, the whole text comes available. The
%D sentinel is optional. This is about the third version.

\ifdefined\fakecompoundhyphen\else \let\fakecompoundhyphen\relax      \fi
\ifdefined\veryraggedright   \else \def\veryraggedright{\raggedright} \fi

\unexpanded\def\limitatetext
  {\bgroup % evt \setstrut
   \forgetall % otherwise indentation and so
   \let\limitatetext\firstofthreearguments
   \fakecompoundhyphen % dangerous ! ! ! ! ! ! ! ! !
   \dowithnextboxcs\syst_boxes_limitate_text\hbox}

\def\syst_boxes_limitate_text#1% #2
  {\doifelsenothing{#1}\syst_boxes_limitate_text_nop\syst_boxes_limitate_text_yes{#1}} % {#2}

\def\syst_boxes_limitate_text_nop#1#2%
  {\unhbox\nextbox
   \egroup}

\def\syst_boxes_limitate_text_yes#1#2%
  {\nopenalties
   \scratchdimen#1\relax
   \ifdim\scratchdimen<\zeropoint\relax % we'll take the last line
     \donefalse
     \scratchdimen-\scratchdimen
   \else
     \donetrue
   \fi
   \ifdim\wd\nextbox>\scratchdimen
     \setbox\scratchbox\hbox{\ifdone\space#2\else#2\space\fi}%
     \advance\scratchdimen -\wd\scratchbox
     \setbox0\box\nextbox
     \setbox\nextbox\vbox
       {\hsize\scratchdimen
        \hfuzz\maxdimen
        \veryraggedright
        \strut
        \ifdone \else
          \parfillskip\zeropoint
          \rightskip\zeropoint
          \hskip\zeropoint \s!plus 1\s!fill % \hsize
        \fi
        \unhcopy0}%
     \ifdim\ht\nextbox>\strutht
       \setbox\nextbox\vbox % if omitted: missing brace reported
         {\splittopskip\openstrutheight
          \ifdone
            \setbox\nextbox\vsplit\nextbox to \strutht
          \else
            \doloop
              {\setbox0\vsplit\nextbox to \strutht
               \ifdim\ht\nextbox>\strutht \else \exitloop \fi}%
          \fi
          \unvbox\nextbox
          \setbox\nextbox\lastbox
          \global\setbox1\hbox
            {\ifdone
               \unhbox\nextbox\unskip\kern\zeropoint\box\scratchbox
             \else
               \box\scratchbox\unhbox\nextbox
             \fi
             \unskip}}%
       \unhbox1
     \else
       \unhbox0
     \fi
   \else
     \unhbox\nextbox
   \fi
   \egroup}

%D We can also limit a text with more control:
%D
%D \startbuffer
%D \limitatetext {\input tufte } {2cm,5mm} {\unknown}
%D \limitatetext {ton en hans} {2cm,5mm} {\unknown}
%D \limitatetext {ton en hans zijn eikels} {2cm,5mm} {\unknown}
%D \limitatetext {ton} {2cm,5mm} {\unknown}
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D We build this feature on top of the previous macro.

% we could move the text argument to the end

\let\normallimitatetext\limitatetext

\def\speciallimitatetext#1#2#3#4% text left right placeholder
  {%\dontleavehmode
   \bgroup
   \let\speciallimitatetext\firstoffourarguments
   \setbox0\hbox
     {\nohyphens
      \normallimitatetext{#1}{+#2}{}#4%
      \normallimitatetext{#1}{-#3}{}}%
   \setbox2\hbox
     {#1}%
   \ifdim\wd2<\wd0 #1\else\unhbox0\fi
   \egroup}

\unexpanded\def\limitatetext#1#2#3% \expanded added 2003/01/16
  {\expanded{\beforesplitstring#2}\at,\to\leftlimit
   \expanded{\aftersplitstring #2}\at,\to\rightlimit
   \ifx\rightlimit\empty
     \normallimitatetext {#1}\leftlimit           {#3}%
   \else
     \speciallimitatetext{#1}\leftlimit\rightlimit{#3}%
   \fi}

%D Undocumented bonus (see wiki):
%D
%D \starttyping
%D \limitatefirstline{\input tufte\relax}{10cm}{\unknown}
%D \stoptyping

\unexpanded\def\limitatefirstline#1#2#3%
  {\hbox\bgroup\strut
   \setbox\scratchbox\hbox{\begstrut#1\endstrut}%
   \ifdim\wd\scratchbox>#2\relax
     \setbox\scratchbox\hbox{#3}%
     \hsize#2\relax
     \advance\hsize-\wd\scratchbox
     \setbox\scratchbox\vbox{\forgetall\veryraggedright#1}%
     \setbox\scratchbox\vsplit\scratchbox to \lineheight
     \vbox
       {\unvbox\scratchbox
        \global\setbox\plusone\lastbox
        \global\setbox\plusone\hbox{\strut\unhbox\plusone}%
        \hbox % to #2
          {\ifx\clip\undefined
             \box\plusone
           \else\ifdim\wd\plusone>\hsize
             \lower\strutdepth\hbox{\clip[\c!width=\hsize,\c!height=\lineheight]{\hbox{\raise\strutdepth\box\plusone}}}%
           \else
             \box\plusone
           \fi\fi
           \removeunwantedspaces#3}}% \removeunwantedspaces\hss#3}}%
   \else
     #1%
   \fi
   \egroup}

%D \macros
%D   {processisolatedwords,processisolatedchars}
%D
%D \startbuffer
%D \processisolatedchars{some more words}                 \ruledhbox \par
%D \processisolatedchars{and some $x + y = z$ math}       \ruledhbox \par
%D \processisolatedchars{and a \hbox{$x + y = z$}}  \ruledhbox \par
%D \processisolatedwords{some more words}                 \ruledhbox \par
%D \processisolatedwords{and some $x + y = z$ math}       \ruledhbox \par
%D \processisolatedwords{and a \hbox{$x + y = z$}}  \ruledhbox \par
%D \stopbuffer
%D
%D \typebuffer \blank \getbuffer \blank

% todo: provide variant with #1 picked up as box

\unexpanded\def\processisolatedchars#1#2%
  {\dontleavehmode
   \begingroup
   \setbox\scratchbox\hbox{\settrialtypesetting#2{\savecurrentattributes{pic}}}%
   \setbox\scratchbox\hbox{\restorecurrentattributes{pic}#1}%
   \ctxcommand{applytochars(\number\scratchbox,"\strippedcsname#2",true)}%
   \endgroup}

\unexpanded\def\processisolatedwords#1#2%
  {\dontleavehmode
   \begingroup
   \setbox\scratchbox\hbox{\settrialtypesetting#2{\savecurrentattributes{pic}}}%
   \setbox\scratchbox\hbox{\restorecurrentattributes{pic}#1}%
   \ctxcommand{applytowords(\number\scratchbox,"\strippedcsname#2",true)}%
   \endgroup}

\unexpanded\def\processwords#1%
  {\processisolatedwords{#1}\processword}

\let\processword\relax

%D The better variant:

\unexpanded\def\applytocharacters#1%
  {\dontleavehmode
   \dowithnextbox{\ctxcommand{applytochars(\number\nextbox,"\strippedcsname#1",true)}}%
   \hbox}

\unexpanded\def\applytowords#1%
  {\dontleavehmode
   \dowithnextbox{\ctxcommand{applytowords(\number\nextbox,"\strippedcsname#1",true)}}%
   \hbox}

%D \macros
%D   {sbox}
%D
%D This is a rather strange command. It grabs some box content
%D and and limits the size to the height and depth of a
%D \type{\strut}. The resulting bottom||alligned box can be used
%D aside other ones, without disturbing the normal baseline
%D distance.
%D
%D \startbuffer
%D \ruledhbox to .5\hsize{\sbox{eerste\par tweede \par derde}}
%D \stopbuffer
%D
%D \typebuffer
%D
%D Shows up as:
%D
%D \startexample
%D \vskip3\baselineskip
%D \getbuffer
%D \stopexample
%D
%D Before displaying the result we added some skip, otherwise
%D the first two lines would have ended up in the text. This
%D macro can be useful when building complicated menus, headers
%D and footers and|/|or margin material.

\unexpanded\def\sbox
  {\vbox\bgroup
   \dowithnextboxcs\syst_boxes_sbox_finish\vbox}

\unexpanded\def\syst_boxes_sbox_finish
  {\setbox\nextbox\hbox
     {\strut
      \dp\nextbox\zeropoint
      \lower\strutdp\box\nextbox}%
    \dp\nextbox\strutdp
    \ht\nextbox\strutht
    \box\nextbox
    \egroup}

%D A variant on this:

\unexpanded\def\inlinedbox
  {\bgroup
   \dowithnextbox
     {\setbox\nextbox\hbox
        {\lower
           \dimexpr(\htdp\nextbox-\lineheight)/\plustwo+\strutdp\relax
           \box\nextbox}%
      \ht\nextbox\strutht
      \dp\nextbox\strutdp
      \box\nextbox
      \egroup}%
     \hbox}

%D \macros
%D   {struttedbox}
%D
%D This boxing macro limits the height and depth to those of
%D a strut.

\unexpanded\def\struttedbox
  {\hbox\bgroup
   \dowithnextboxcs\syst_boxes_struttedbox_finish\hbox}

\def\syst_boxes_struttedbox_finish
  {\dp\nextbox\strutdepth
   \ht\nextbox\strutheight
   \box\nextbox
   \egroup}

%D \macros
%D   {topskippedbox}
%D
%D This macro compensates the difference between the topskip
%D and strutheight. Watch how we preserve the depth when it
%D equals strutdepth.

\unexpanded\def\topskippedbox
  {\hbox\bgroup\dowithnextboxcs\syst_boxes_topskippedbox_finish\hbox}

\def\syst_boxes_topskippedbox_finish
  {\edef\m_boxes_tmp{\ifdim\strutdepth=\dp\nextbox\dp\nextbox\the\dp\nextbox\fi}%
   \lower\topskip\hbox{\raise\strutheight\box\nextbox}%
   \m_boxes_tmp
   \egroup}

%D \macros
%D   {centeredbox, centerednextbox}
%D
%D Here is another strange one. This one offers a sort of overlay
%D with positive or negative offsets. This command can be used
%D in well defined areas where no offset options are available.
%D We first used it when building a button inside the margin
%D footer, where the button should have a horizontal offset and
%D should be centered with respect to the surrounding box. The
%D last of the three examples we show below says:
%D
%D \starttyping
%D \vsize=3cm
%D \hsize=3cm
%D \ruledvbox to \vsize
%D   {\centeredbox height .5cm width -1cm
%D      {\vrule width \hsize height \vsize}}}
%D \stoptyping
%D
%D Here the \type{\ruledvbox} just shows the surrounding box
%D and \type{\vrule} is used to show the centered box.
%D
%D \def\AnExample#1#2%
%D   {\vsize=3cm
%D    \hsize=3cm
%D    \ruledvbox to \vsize
%D      {\centeredbox height #1 width #2
%D         {\color[green]{\vrule width \hsize height \vsize}}}}
%D
%D \startlinecorrection
%D \startcombination[3*1]
%D   {\AnExample {-1cm}  {.5cm}} {}
%D   {\AnExample {.5cm}  {-1cm}} {}
%D   {\AnExample {-1cm} {-.5cm}} {}
%D \stopcombination
%D \stoplinecorrection
%D
%D This command takes two optional arguments: \type{width} and
%D \type{height}. Observing readers can see that we use \TEX's
%D own scanner for grabbing these arguments: \type{#1#} reads
%D everyting till the next brace and passes it to both rules.
%D The setting of the box dimensions at the end is needed for
%D special cases. The dimensions of the surrounding box are kept
%D intact. This commands handles positive and negative
%D dimensions (which is why we need two boxes with rules).

\unexpanded\def\centeredbox#1#%   height +/-dimen width +/-dimen
  {\bgroup
   \setbox0\vbox to \vsize
     \bgroup
       \dontcomplain
       \forgetall
       \setbox0\hbox{\vrule\s!width \zeropoint#1}%
       \setbox2\vbox{\hrule\s!height\zeropoint#1}%
       \advance\vsize \ht2
       \advance\hsize \wd0
       \vbox to \vsize
         \bgroup
           \vskip-\ht2
           \vss
           \hbox to \hsize
             \bgroup
               \dowithnextbox
                 {\hskip-\wd0
                  \hss
                  \box\nextbox
                  \hss
             \egroup
           \vss
         \egroup
     \egroup
     \wd0\hsize
     \ht0\vsize
     \box0
     \egroup}
   \hbox}

%D For those who don't want to deal with \type {\hsize}
%D and \type {\vsize}, we have:
%D
%D \starttyping
%D \centerednextbox width 2bp height 2bp
%D   {\framed[width=100bp,height=100bp]{}}
%D \stoptyping
%D
%D Do you see what we call this one \type {next}?

\unexpanded\def\centerednextbox#1#%
  {\bgroup
   \dowithnextbox
     {\hsize\wd\nextbox
      \vsize\ht\nextbox
      \centeredbox#1{\box\nextbox}%
      \egroup}
   \hbox}

%D \macros
%D   {centerbox}
%D
%D Centering on the available space is done by:
%D
%D \starttyping
%D \centerbox <optional specs> {content}
%D \stoptyping
%D
%D When omitted, the current \type {\hsize} and \type
%D {\vsize} are used. Local dimensions are supported.

\unexpanded\def\centerbox#1#%   optional height +/-dimen width +/-dimen
  {\bgroup
   \dowithnextbox
     {\setlocalhsize
      \setbox\scratchbox\hbox{\vrule\s!width \zeropoint#1}%
      \ifzeropt\wd\scratchbox\else\hsize\wd\scratchbox\fi
      \setbox\scratchbox\vbox{\hrule\s!height\zeropoint#1}%
      \ifzeropt\ht\scratchbox\else\vsize\ht\scratchbox\fi
      \vbox to \vsize{\vss\hbox to \hsize{\hss\box\nextbox\hss}\vss}%
      \egroup}%
     \hbox}

%D \macros
%D   {setrigidcolumnhsize,rigidcolumnbalance,rigidcolumnlines}
%D
%D These macros are copied from the \TEX book, page~397, and
%D extended by a macro that sets the \type{\hsize}.
%D
%D \starttyping
%D \setrigidcolumnhsize {total width} {distance} {n}
%D \rigidcolumnbalance  {box}
%D \stoptyping
%D
%D Both these macros are for instance used in typesetting
%D footnotes.
%D
%D Men kan het proces van breken enigzins beinvloeden met de
%D volgende twee switches:

\newif\ifalignrigidcolumns
\newif\ifstretchrigidcolumns
\newif\iftightrigidcolumns    % if true: just a vbox, no depth/noflines/gridsnap corrrections

%D De eerste switch bepaald het uitlijnen, de tweede rekt de
%D individuele kolommen op naar \type{\vsize}.

\unexpanded\def\setrigidcolumnhsize#1#2#3% todo: \dimexpr
  {\xdef\savedrigidhsize{\the\hsize}%
   \hsize#1\relax
   \global\chardef\rigidcolumns#3\relax
   \scratchdimen -#2\relax
   \multiply\scratchdimen #3\relax
   \advance\scratchdimen  #2\relax
   \advance\hsize \scratchdimen
   \divide\hsize #3\relax}

% ==
%
% \def\setrigidcolumnhsize#1#2#3%
%   {\xdef\savedrigidhsize{\the\hsize}%
%    \global\chardef\rigidcolumns#3\relax
%    \hsize=\dimexpr(#1-\numexpr#3-1\relax\dimexpr#2\relax)/#3\relax}

\newbox\rigidcolumnbox

\let\rigidcolumnlines\!!zerocount

\unexpanded\def\rigidcolumnbalance#1%
  {\ifnum\rigidcolumns=1 % tzt ook h/d correctie
     \ifinner\ifhmode\box\else\unvbox\fi\else\unvbox\fi#1\relax
   \else
     \vbox
       {\forgetall
        \nopenalties
        \dontcomplain
        \setbox\rigidcolumnbox\vbox
          {\line{}\goodbreak\unvbox#1\removebottomthings}%
        \splittopskip\openstrutheight
        \setbox\scratchbox\vsplit\rigidcolumnbox to \zeropoint
        \ifcase\rigidcolumnlines\relax
          % \iffalse
          %  % maybe some day an option
          %  \scratchskip\ht\rigidcolumnbox
          %  \advance\scratchskip\dp\rigidcolumnbox
          %  \getnoflines\scratchskip
          %  \ifodd\noflines
          %    \advance\noflines\plusone
          %  \fi
          %  \divide\noflines\rigidcolumns
          %\else
            \scratchdimen\ht\rigidcolumnbox
            \divide\scratchdimen \rigidcolumns
            \getnoflines\scratchdimen
          %\fi
        \else
          \noflines\rigidcolumnlines % to be sure
        \fi
        \scratchdimen\noflines\lineheight
        % new: we now loop so that we don't loose content
        % since in practice we also use this macro for
        % funny lineheights and border cases
        \setbox0=\box\rigidcolumnbox
        \doloop
          {\setbox\rigidcolumnbox=\copy0
           \setbox\scratchbox\hbox to \savedrigidhsize
             {\dorecurse\rigidcolumns
                {\setbox\scratchbox\vsplit\rigidcolumnbox to \scratchdimen
                   \dp\scratchbox\openstrutdepth
                   \setbox\scratchbox\normalvtop
                     \ifalignrigidcolumns to
                       \ifstretchrigidcolumns\vsize\else\scratchdimen\fi
                     \fi
                     {\unvbox\scratchbox}%
                 \wd\scratchbox\hsize
                 \box\scratchbox
                 \hfill}%
              \hfillneg}%
          \ifvoid\rigidcolumnbox\exitloop\else\advance\scratchdimen\lineheight\fi}%
       \iftightrigidcolumns
         \setbox\scratchbox\hbox{\raise\dp\scratchbox\box\scratchbox}%
       \else
         \advance\scratchdimen -\openstrutdepth
         \setbox\scratchbox\hbox{\raise\scratchdimen\box\scratchbox}%
         \dp\scratchbox\openstrutdepth
         \ht\scratchbox\scratchdimen
       \fi
       \box\scratchbox}%
   \fi}

%D \macros
%D   {startvboxtohbox,stopvboxtohbox,convertvboxtohbox}
%D
%D Here is another of Knuth's dirty tricks, as presented on
%D pages 398 and 399 of the \TEX book. These macros can be used
%D like:
%D
%D \starttyping
%D \vbox
%D   \bgroup
%D     \startvboxtohbox ... \stopvboxtohbox
%D     \startvboxtohbox ... \stopvboxtohbox
%D     \startvboxtohbox ... \stopvboxtohbox
%D   \egroup
%D
%D \vbox
%D   \bgroup
%D     \convertvboxtohbox
%D   \egroup
%D \stoptyping
%D
%D These macros are used in reformatting footnotes, so they do
%D what they're meant for.

\unexpanded\def\setvboxtohbox
  {\bgroup
   \ifdim\baselineskip<16pt \relax
     \scratchdimen\baselineskip
     \multiply\scratchdimen 1024
   \else
     \message{cropping \baselineskip to 16pt}%
     \scratchdimen\maxdimen
   \fi
   \divide\scratchdimen \hsize
   \multiply\scratchdimen 64
   \xdef\vboxtohboxfactor{\withoutpt\the\scratchdimen}%
   \egroup}

\unexpanded\def\startvboxtohbox
 {\bgroup
  \setvboxtohbox
  \setbox\scratchbox\hbox\bgroup}

\unexpanded\def\stopvboxtohbox
  {\egroup
   \dp\scratchbox\zeropoint
   \ht\scratchbox\vboxtohboxfactor\wd\scratchbox
   \box\scratchbox
   \egroup}

\unexpanded\def\convertvboxtohbox
  {\setvboxtohbox
   \makehboxofhboxes
   \setbox0\hbox{\unhbox0 \removehboxes}%
   \noindent\unhbox0\par}

\unexpanded\def\makehboxofhboxes
  {\setbox0\emptyhbox
   \loop                  % \doloop { .. \exitloop .. }
     \setbox2\lastbox
     \ifhbox2
       \setbox0\hbox{\box2\unhbox0}%
   \repeat}

\unexpanded\def\removehboxes
  {\setbox0\lastbox
   \ifhbox0
     {\removehboxes}\unhbox0
   \fi}

%D \macros
%D   {unhhbox}
%D
%D The next macro is used in typesetting inline headings.
%D Let's first look at the macro and then show an example.

\newbox   \unhhedbox
\newbox   \hhbox
\newdimen \lasthhboxwidth
\newskip  \hhboxindent

\unexpanded\def\unhhbox#1\with#2%
  {\bgroup
   \nopenalties
   \dontcomplain
   \forgetall
   \setbox\unhhedbox\vbox{\hskip\hhboxindent\strut\unhbox#1}% => \hsize
   \doloop
     {\setbox\hhbox\vsplit\unhhedbox to \lineheight
      \ifvoid\unhhedbox
        \setbox\hhbox\hbox{\strut\hboxofvbox\hhbox}%
      \fi
      \ht\hhbox\strutht
      \dp\hhbox\strutdp
      \ifzeropt\hhboxindent\else % \ifdim\hhboxindent=\zeropoint\else
        \setbox\hhbox\hbox{\kern-\hhboxindent\box\hhbox}%
        \hhboxindent\zeropoint
      \fi
      \global\lasthhboxwidth\wd\hhbox
      #2\relax
      \ifvoid\unhhedbox
        \exitloop
      \else
        \hskip\zeropoint \s!plus \zeropoint
      \fi}%
   \egroup}

\def\dohboxofvbox
  {\setbox0\vbox{\unvbox\scratchcounter\global\setbox1\lastbox}%
   \unhbox1
   \egroup}

\unexpanded\def\hboxofvbox
  {\bgroup
   \afterassignment\dohboxofvbox
   \scratchcounter=}

%D This macro can be used to break a paragraph apart and treat
%D each line seperately, for instance, making it clickable. The
%D main complication is that we want to be able to continue the
%D paragraph, something that's needed in the in line section
%D headers.
%D
%D \startbuffer
%D \setbox0=\hbox{\input tufte \relax}
%D \setbox2=\hbox{\input knuth \relax}
%D \unhhbox0\with{\ruledhbox{\box\hhbox}}
%D \hskip1em plus 1em minus 1em
%D \hhboxindent=\lasthhboxwidth
%D \advance\hhboxindent by \lastskip
%D \unhhbox2\with{\ruledhbox{\box\hhbox}}
%D \stopbuffer
%D
%D \getbuffer
%D
%D This piece of text was typeset by saying:
%D
%D \typebuffer
%D
%D Not that nice a definition, but effective. Note the stretch
%D we've build in the line that connects the two paragraphs.

%D \macros
%D   {doifcontent}
%D
%D When processing depends on the availability of content, one
%D can give the next macro a try.
%D
%D \starttyping
%D \doifcontent{pre content}{post content}{no content}\somebox
%D \stoptyping
%D
%D Where \type{\somebox} is either a \type{\hbox} or
%D \type{\vbox}. If the dimension of this box suggest some
%D content, the resulting box is unboxed and surrounded by the
%D first two arguments, else the third arguments is executed.

\unexpanded\def\doifcontent#1#2#3%
  {\dowithnextbox
     {\ifhbox\nextbox
        \ifdim\wd\nextbox>\zeropoint
          #1\unhbox\nextbox#2\relax
        \else
          #3\relax
        \fi
      \else
        \ifdim\ht\nextbox>\zeropoint
          #1\unvbox\nextbox#2\relax
        \else
          #3\relax
        \fi
      \fi}}

%D So when we say:
%D
%D \startbuffer
%D \doifcontent{[}{]}{}\hbox{content sensitive typesetting}
%D
%D \doifcontent{}{\page}{}\vbox{content sensitive typesetting}
%D
%D \doifcontent{}{}{\message{Didn't you forget something?}}\hbox{}
%D \stopbuffer
%D
%D \typebuffer
%D
%D We get:
%D
%D \getbuffer
%D
%D Where the last call of course does not show up in this
%D document, but definitely generates a confusing message.

%D \macros
%D   {processboxes}
%D
%D The next macro gobble boxes and is for instance used for
%D overlays. First we show the general handler.

% we cannot use \futurelet here as we want to skip spaces between
% boxes (see startoverlay for an example usage)

% \newbox\processbox % public : this is the one where \nextbox's end up in
%
% \unexpanded\def\processboxes#1%
%   {\bgroup
%    \def\syst_boxes_process_indeed{#1}% #1 can be redefined halfway
%    \setbox\processbox\emptybox
%    \afterassignment\syst_boxes_process
%    \let\nexttoken}
%
% \def\syst_boxes_process
%   {\ifx\nexttoken\bgroup
%      \expandafter\syst_boxes_process_yes
%    \else
%      \expandafter\syst_boxes_process_nop
%    \fi}
%
% \def\syst_boxes_process_yes
%   {\dowithnextboxcs\syst_boxes_process_content\hbox\bgroup}
%
% \def\syst_boxes_process_content
%   {\removeunwantedspaces
%    \syst_boxes_process_indeed % takes \nextbox makes \processbox
%    \afterassignment\syst_boxes_process
%    \let\nexttoken}
%
% \unexpanded\def\syst_boxes_process_nop
%   {\removeunwantedspaces
%    \box\processbox
%    \nexttoken % messy as we are still in the group
%    \egroup}

\newbox\processbox % public : this is the one where \nextbox's end up in

\unexpanded\def\processboxes#1%
  {\bgroup
   \def\syst_boxes_process_indeed{#1}% #1 can be redefined halfway
   \setbox\processbox\emptybox
   \doifnextbgroupelse\syst_boxes_process_yes\syst_boxes_process_nop}

\def\syst_boxes_process_yes
  {\dowithnextboxcs\syst_boxes_process_content\hbox}

\def\syst_boxes_process_content
  {\removeunwantedspaces
   \syst_boxes_process_indeed % takes \nextbox makes \processbox
   \doifnextbgroupelse\syst_boxes_process_yes\syst_boxes_process_nop}

\unexpanded\def\syst_boxes_process_nop
  {\removeunwantedspaces
   \box\processbox
   \egroup}

%D \macros
%D   {startoverlay}
%D
%D We can overlay boxes by saying:
%D
%D \startbuffer
%D \startoverlay
%D   {\framed{hans}}
%D   {\framed[width=3cm]{ton}}
%D   {\framed[height=2cm]{oeps}}
%D \stopoverlay
%D \stopbuffer
%D
%D \typebuffer
%D
%D shows up as:
%D
%D \leavevmode\getbuffer

\def\syst_boxes_overlay_process
  {%\removeunwantedspaces % already done
   \scratchdepth\dp\ifdim\dp\nextbox>\dp\processbox\nextbox\else\processbox\fi
   \ifdim\ht\nextbox>\ht\processbox
     \setbox\processbox\vbox to \ht\nextbox   {\dp\processbox\zeropoint\vss\box\processbox\vss}%
   \else
     \setbox\nextbox   \vbox to \ht\processbox{\dp\nextbox   \zeropoint\vss\box\nextbox   \vss}%
   \fi
   \dp\nextbox   \scratchdepth
   \dp\processbox\scratchdepth
   \scratchwidth\wd\ifdim\wd\nextbox>\wd\processbox\nextbox\else\processbox\fi
   \setbox\processbox\hbox to \scratchwidth
     {\hbox to \scratchwidth{\hss\box\processbox\hss}%
      \kern-\scratchwidth
      \hbox to \scratchwidth{\hss\box\nextbox   \hss}}}

\unexpanded\def\startoverlay
  {\bgroup
   \let\stopoverlay\egroup
   \processboxes\syst_boxes_overlay_process}

\let\stopoverlay\relax

%D \macros
%D   {fakebox}
%D
%D The next macro is a rather silly one, but saves space.
%D
%D \starttyping
%D \hbox{\fakebox0}
%D \stoptyping
%D
%D returns an empty box with the dimensions of the box
%D specified, here being zero.

\unexpanded\def\fakebox
  {\bgroup
   \afterassignment\syst_boxes_fakebox_finish\scratchcounter}

\def\syst_boxes_fakebox_finish
  {\setbox\scratchbox\ifhbox\scratchcounter\emptyhbox\else\emptyvbox\fi
   \wd\scratchbox\wd\scratchcounter
   \ht\scratchbox\ht\scratchcounter
   \dp\scratchbox\dp\scratchcounter
   \box\scratchbox
   \egroup}

%D \macros
%D   {lbox,rbox,cbox,tbox,bbox}
%D
%D Here are some convenient alternative box types:
%D
%D \starttyping
%D \lbox{text ...}
%D \cbox{text ...}
%D \rbox{text ...}
%D \stoptyping
%D
%D Are similar to \type {\vbox}, which means that they also
%D accept something like \type{to 3cm}, but align to the left,
%D middle and right. These box types can be used to typeset
%D paragraphs.

\def\syst_boxes_lrc_process#1{\bgroup\forgetall\let\\\endgraf#1\let\next}

\unexpanded\def\lbox#1#{\vbox#1\syst_boxes_lrc_process\raggedleft  }
\unexpanded\def\cbox#1#{\vbox#1\syst_boxes_lrc_process\raggedcenter}
\unexpanded\def\rbox#1#{\vbox#1\syst_boxes_lrc_process\raggedright }

\unexpanded\def\ltop#1#{\normalvtop#1\syst_boxes_lrc_process\raggedleft  }
\unexpanded\def\ctop#1#{\normalvtop#1\syst_boxes_lrc_process\raggedcenter}
\unexpanded\def\rtop#1#{\normalvtop#1\syst_boxes_lrc_process\raggedright }

%D The alternatives \type {\tbox} and \type {\bbox} can be used
%D to properly align boxes, like in:
%D
%D \setupexternalfigures[directory={../sample}]
%D \startbuffer
%D \starttable[|||]
%D \HL
%D \VL \tbox{\externalfigure[cow][height=3cm,frame=on]} \VL top aligned    \VL\SR
%D \HL
%D \VL \bbox{\externalfigure[cow][height=3cm,frame=on]} \VL bottom aligned \VL\SR
%D \HL
%D \stoptable
%D \stopbuffer
%D
%D \typebuffer
%D
%D The positioning depends on the strut settings:
%D
%D \getbuffer

\unexpanded\def\tbox{\hbox\bgroup\dowithnextboxcs\syst_boxes_tbox_finish\hbox}
\unexpanded\def\bbox{\hbox\bgroup\dowithnextboxcs\syst_boxes_bbox_finish\hbox}

\def\syst_boxes_tbox_finish
  {\scratchdepth\dimexpr\ht\nextbox+\dp\nextbox-\ht\strutbox\relax
   \ht\nextbox\ht\strutbox
   \dp\nextbox\scratchdepth
   \setbox\nextbox\hbox{\lower\dp\nextbox\box\nextbox}%
   \ht\nextbox\ht\strutbox
   \dp\nextbox\scratchdepth
   \box\nextbox
   \egroup}

\def\syst_boxes_bbox_finish
  {\scratchheight\dimexpr\ht\nextbox+\dp\nextbox-\dp\strutbox\relax
   \dp\nextbox\dp\strutbox
   \ht\nextbox\scratchheight
   \setbox\nextbox\hbox{\lower\dp\nextbox\box\nextbox}%
   \dp\nextbox\dp\strutbox
   \ht\nextbox\scratchheight
   \box\nextbox
   \egroup}

%D \macros
%D   {lhbox,mhbox,rhbox}
%D
%D A few more boxes.

\def\dodolhbox{\hbox to \hsize{\box\nextbox\hss    }}
\def\dodomhbox{\hbox to \hsize{\hss\box\nextbox\hss}}
\def\dodorhbox{\hbox to \hsize{\hss\box\nextbox    }}

\unexpanded\def\lhbox{\dowithnextboxcs\dodolhbox\hbox}
\unexpanded\def\mhbox{\dowithnextboxcs\dodomhbox\hbox}
\unexpanded\def\rhbox{\dowithnextboxcs\dodorhbox\hbox}

\let\lefthbox \lhbox
\let\midhbox  \mhbox
\let\righthbox\rhbox

%D \macros
%D   {boxofsize}
%D
%D Sometimes we need to construct a box with a height or
%D width made up of several dimensions. Instead of cumbersome
%D additions, we can use:
%D
%D \starttyping
%D \boxofsize \vbox 10cm 3cm -5cm {the text to be typeset}
%D \stoptyping
%D
%D This example demonstrates that one can use positive and
%D negative values. Dimension registers are also accepted.

\newdimen\sizeofbox

\unexpanded\def\boxofsize#1%
  {\bgroup
   \sizeofbox\zeropoint
   \scratchdimen\zeropoint
   \def\docommand
     {\advance\sizeofbox\scratchdimen
      \futurelet\next\dodocommand}%
   \def\dodocommand
     {\ifx\next\bgroup
        \expanded{\egroup#1 to \the\sizeofbox}%
      \else
        \@EA\afterassignment\@EA\docommand\@EA\scratchdimen
      \fi}%
   \docommand}

%D Some new, still undocumented features:

% limitatetext -> beter {text} als laatste !!
%
% \limitvbox
% \limithbox

\unexpanded\def\limitatelines#1#2% size sentinel
  {\dowithnextbox
     {\dimen0=#1\hsize
      \ifdim\wd\nextbox>\dimen0
        \setbox\nextbox\hbox
          {\advance\dimen0 -.1\hsize
           \limitatetext{\unhbox\nextbox}{\dimen0}{\nobreak#2}}%
      \fi
      \unhbox\nextbox}
     \hbox}

\unexpanded\def\fittoptobaselinegrid % weg hier
  {\dowithnextbox
     {\bgroup
      \par
      \dimen0\ht\nextbox
      \ht\nextbox\strutht
      \dp\nextbox\strutdp
      \hbox{\box\nextbox}
      \prevdepth\strutdp
      \doloop
        {\advance\dimen0 -\lineheight
         \ifdim\dimen0<\zeropoint
           \exitloop
         \else
           \nobreak
           \hbox{\strut}
         \fi}
      \egroup}
     \vbox}

%D Some more undocumented macros (used in m-chart).

\newif\iftraceboxplacement % \traceboxplacementtrue

\newbox\fakedboxcursor

\setbox\fakedboxcursor\hbox
  {\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\zeropoint}

\unexpanded\def\boxcursor % overloaded in core-vis
  {\iftraceboxplacement
     \bgroup
     \scratchdimen2\onepoint
     \setbox\scratchbox\hbox to \zeropoint
       {\hss
        \vrule
          \s!width \scratchdimen
          \s!height\scratchdimen
          \s!depth \scratchdimen
        \hss}%
     \smashedbox\scratchbox
     \egroup
   \else
     \copy\fakedboxcursor
   \fi}

\unexpanded\def\placedbox
  {\iftraceboxplacement\ruledhbox\else\hbox\fi}

\newdimen\boxoffset
\newdimen\boxhdisplacement
\newdimen\boxvdisplacement

\unexpanded\def\rightbox      {\hbox\bgroup\dowithnextboxcs\syst_boxes_rightbox_finish      \placedbox}
\unexpanded\def\leftbox       {\hbox\bgroup\dowithnextboxcs\syst_boxes_leftbox_finish       \placedbox}
\unexpanded\def\topbox        {\hbox\bgroup\dowithnextboxcs\syst_boxes_topbox_finish        \placedbox}
\unexpanded\def\bottombox     {\hbox\bgroup\dowithnextboxcs\syst_boxes_bottombox_finish     \placedbox}
\unexpanded\def\lefttopbox    {\hbox\bgroup\dowithnextboxcs\syst_boxes_lefttopbox_finish    \placedbox}
\unexpanded\def\righttopbox   {\hbox\bgroup\dowithnextboxcs\syst_boxes_righttopbox_finish   \placedbox}
\unexpanded\def\leftbottombox {\hbox\bgroup\dowithnextboxcs\syst_boxes_leftbottombox_finish \placedbox}
\unexpanded\def\rightbottombox{\hbox\bgroup\dowithnextboxcs\syst_boxes_rightbottombox_finish\placedbox}

\let\topleftbox    \lefttopbox
\let\toprightbox   \righttopbox
\let\bottomleftbox \leftbottombox
\let\bottomrightbox\rightbottombox

\def\syst_boxes_rightbox_finish
  {\global\boxhdisplacement\boxoffset
   \global\boxvdisplacement.5\ht\nextbox
   \global\advance\boxvdisplacement-.5\dp\nextbox
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_leftbox_finish
  {\global\boxhdisplacement-\wd\nextbox
   \global\advance\boxhdisplacement-\boxoffset
   \global\boxvdisplacement.5\ht\nextbox
   \global\advance\boxvdisplacement-.5\dp\nextbox
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_topbox_finish
  {\global\boxhdisplacement-.5\wd\nextbox
   \global\boxvdisplacement-\dp\nextbox
   \global\advance\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_bottombox_finish
  {\global\boxhdisplacement-.5\wd\nextbox
   \global\boxvdisplacement\ht\nextbox
   \global\advance\boxvdisplacement\boxoffset
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_lefttopbox_finish
  {\global\boxhdisplacement-\wd\nextbox
   \global\advance\boxhdisplacement-\boxoffset
   \global\boxvdisplacement-\dp\nextbox
   \global\advance\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_righttopbox_finish
  {\global\boxhdisplacement\boxoffset
   \global\boxvdisplacement-\dp\nextbox
   \global\advance\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_leftbottombox_finish
  {\global\boxhdisplacement-\wd\nextbox
   \global\advance\boxhdisplacement-\boxoffset
   \global\boxvdisplacement\ht\nextbox
   \global\advance\boxvdisplacement\boxoffset
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_rightbottombox_finish
  {\global\boxhdisplacement\boxoffset
   \global\boxvdisplacement\ht\nextbox
   \global\advance\boxvdisplacement\boxoffset
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\unexpanded\def\middlebox        {\hbox\bgroup\dowithnextboxcs\syst_boxes_middlebox_finish        \placedbox}
\unexpanded\def\baselinemiddlebox{\hbox\bgroup\dowithnextboxcs\syst_boxes_baselinemiddlebox_finish\placedbox}
\unexpanded\def\baselineleftbox  {\hbox\bgroup\dowithnextboxcs\syst_boxes_baselineleftbox_finish  \placedbox}
\unexpanded\def\baselinerightbox {\hbox\bgroup\dowithnextboxcs\syst_boxes_baselinerightbox_finish \placedbox}

\def\syst_boxes_middlebox_finish
  {\global\boxhdisplacement-.5\wd\nextbox
   \global\boxvdisplacement.5\ht\nextbox
   \global\advance\boxvdisplacement-.5\dp\nextbox
   \boxcursor\kern\boxhdisplacement\lower\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_baselinemiddlebox_finish
  {\global\boxhdisplacement-.5\wd\nextbox
   \global\advance\boxhdisplacement-\boxoffset
   \global\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_baselineleftbox_finish
  {\global\boxhdisplacement-\wd\nextbox
   \global\advance\boxhdisplacement-\boxoffset
   \global\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

\def\syst_boxes_baselinerightbox_finish
  {\global\boxhdisplacement\boxoffset
   \global\boxvdisplacement-\boxoffset
   \boxcursor\kern\boxhdisplacement\raise-\boxvdisplacement\box\nextbox
   \egroup}

%D \macros
%D   {obox}
%D
%D Experimental, not yet frozen:

\unexpanded\def\lrtbbox#1#2#3#4% l r t b
  {\bgroup
   \dowithnextboxcontent
     {\advance\hsize-#1\advance\hsize-#2\relax
      \advance\vsize-#3\advance\vsize-#4\relax}
     {\forgetall\vbox to \vsize{\vskip#3\hbox to \hsize{\hskip#1\box\nextbox\hss}\vss}\egroup}
     \vbox}

%D \macros
%D   {toplinebox}
%D
%D See core-tbl.tex for an example of its usage:

\unexpanded\def\toplinebox
  {\dowithnextboxcs\syst_boxes_toplinebox_finish\tbox}

\def\syst_boxes_toplinebox_finish
  {\ifdim\dp\nextbox>\strutdepth
     \scratchdimen\dp\nextbox
     \advance\scratchdimen-\strutdepth
     \getnoflines\scratchdimen
     \struttedbox{\box\nextbox}%
     \dorecurse\noflines\verticalstrut
   \else
     \box\nextbox
   \fi}

%D \macros
%D   {initializeboxstack,savebox,foundbox}
%D
%D At the cost of some memory, but saving box registers, we
%D have implemented a box repository.
%D
%D \starttyping
%D \initializeboxstack{one}
%D
%D \savebox{one}{a}{test a}
%D \savebox{one}{p}{test p}
%D \savebox{one}{q}{test q}
%D
%D \hbox{a:\foundbox{one}{a}} \par
%D \hbox{q:\foundbox{one}{q}} \par
%D \hbox{p:\foundbox{one}{p}} \par
%D \hbox{x:\foundbox{one}{x}} \par
%D \hbox{y:\foundbox{two}{a}} \par
%D \stoptyping

\def\@@stackbox{@box@}
\def\@@stacklst{@xob@}

\unexpanded\def\setstackbox#1#2%
  {\ifcsname\@@stackbox:#1:#2\endcsname\else
     \expandafter\newbox\csname\@@stackbox:#1:#2\endcsname
   \fi
   \global\setbox\csname\@@stackbox:#1:#2\endcsname\vbox}

\unexpanded\def\initializeboxstack#1%
  {\def\docommand##1{\setstackbox{#1}{##1}{}}%
   \ifcsname\@@stacklst#1\endcsname
     \processcommacommand[\getvalue{\@@stacklst#1}]\docommand
   \fi
   \letgvalueempty{\@@stacklst#1}}

\unexpanded\def\savebox#1#2% stack name
  {% beware, \setxvalue defines the cs beforehand so we cannot use the
   % test inside the { }
   \ifcsname\@@stacklst#1\endcsname
     \setxvalue{\@@stacklst#1}{\csname\@@stacklst#1\endcsname,#2}%
   \else
     \setxvalue{\@@stacklst#1}{#2}%
   \fi
   \setstackbox{#1}{#2}}

\unexpanded\def\foundbox#1#2%
  {\vbox
     {\ifcsname\@@stackbox:#1:#2\endcsname
        \copy\csname\@@stackbox:#1:#2\endcsname
      \fi}}

\unexpanded\def\doifboxelse#1#2#3#4%
  {\ifcsname\@@stackbox:#1:#2\endcsname
     \ifvoid\csname\@@stackbox:#1:#2\endcsname#4\else#3\fi
   \else
     #4%
   \fi}

%D \macros
%D   {removedepth, obeydepth}
%D
%D While \type {\removedepth} removes the preceding depth,
%D \type {\obeydepth} makes sure we have depth. Both macros
%D leave the \type {\prevdepth} untouched.

\unexpanded\def\removedepth
  {\ifvmode
     \ifdim\prevdepth>\zeropoint
       \kern-\prevdepth
     \fi
   \fi}

\unexpanded\def\obeydepth
  {\par
  \ifvmode
     \ifdim\prevdepth>\zeropoint
       \kern-\prevdepth
     \fi
     \kern\strutdp
   \fi}

\unexpanded\def\undepthed
  {\dowithnextbox{\dp\nextbox\zeropoint\box\nextbox}\hbox}

%D \macros
%D   {removebottomthings, removelastskip}
%D
%D A funny (but rather stupid) one, plus a redefinition.

\unexpanded\def\removebottomthings
  {\dorecurse5{\unskip\unkern\unpenalty}}

\unexpanded\def\removelastskip % \ifvmode the plain tex one \fi
  {\ifvmode\ifzeropt\lastskip\else\vskip-\lastskip\fi\fi}

%D \macros
%D   {makestrutofbox}
%D
%D This macro sets the dimensions of a box to those of a strut.

\def\domakestrutofbox
  {\ht\c_boxes_register\strutht
   \dp\c_boxes_register\strutdp
   \wd\c_boxes_register\zeropoint}

\unexpanded\def\makestrutofbox % not used
  {\afterassignment\domakestrutofbox\c_boxes_register}

%D \macros
%D   {raisebox,lowerbox}
%D
%D Some more box stuff, related to positioning (under
%D construction). Nice stuff for a tips and tricks maps
%D article.
%D
%D \starttyping
%D \raisebox{100pt}\hbox{test}
%D \hsmash{\raisebox{100pt}\hbox{test}}
%D \stoptyping

\unexpanded\def\raisebox#1{\bgroup\afterassignment\syst_boxes_raise_indeed\scratchdimen#1} % so both 10pt and {10pt} is accepted
\unexpanded\def\lowerbox#1{\bgroup\afterassignment\syst_boxes_lower_indeed\scratchdimen#1} % so both 10pt and {10pt} is accepted

\def\syst_boxes_raise_indeed{\dowithnextboxcs\syst_boxes_raise_finish}
\def\syst_boxes_lower_indeed{\dowithnextboxcs\syst_boxes_lower_finish}

\def\syst_boxes_raise_finish
  {\setbox\nextbox\hbox{\raise\scratchdimen\box\nextbox}%
   \ht\nextbox\strutht
   \dp\nextbox\strutdp
   \box\nextbox
   \egroup}

\def\syst_boxes_lower_finish
  {\setbox\nextbox\hbox{\lower\scratchdimen\box\nextbox}%
   \ht\nextbox\strutht
   \dp\nextbox\strutdp
   \box\nextbox
   \egroup}

% vcenter in text, we kunnen vcenter overloaden

\unexpanded\def\halfwaybox
  {\hbox\bgroup
   \dowithnextboxcs\syst_boxes_halfwaybox_finish\hbox}

\def\syst_boxes_halfwaybox_finish
  {\dp\nextbox\zeropoint
   \lower.5\ht\nextbox\box\nextbox
   \egroup}

\unexpanded\def\depthonlybox
  {\vtop\bgroup
   \dowithnextboxcs\syst_boxes_depthonlybox_finish\vbox}

\def\syst_boxes_depthonlybox_finish
  {\hsize\wd\nextbox
   \kern\zeropoint\box\nextbox
   \egroup}

%D New:

\def\setdimentoatleast#1#2{\ifdim#1>\zeropoint\else#1=#2\fi}
\def\setdimentoatmost #1#2{\ifdim#1>#2\relax  \else#1=#2\fi}

%D And even rawer:

\let\naturalhbox   \hbox
\let\naturalvbox   \vbox
\let\naturalvtop   \normalvtop
\let\naturalvcenter\normalvtop

\ifdefined\textdir

    \unexpanded\def\naturalhbox{\hbox dir TLT}
    \unexpanded\def\naturalvbox{\vbox dir TLT}
   %\unexpanded\def\naturalvtop{\normalvtop dir TLT}

\fi

%D \macros
%D   {vcenter}
%D
%D Also new: tex mode \type {\vcenter}.

\unexpanded\def\vcenter
  {\vbox\bgroup
   \dowithnextboxcs\syst_boxes_vcenter_finish\vbox}

\def\syst_boxes_vcenter_finish
  {\hbox{$\normalvcenter{\box\nextbox}$}%
   \egroup}

% could be \everymathematics

\prependtoks \let\vcenter\normalvcenter \to \everymath
\prependtoks \let\vcenter\normalvcenter \to \everydisplay

% \appendtoks \let\vcenter\normalvcenter \to \everymathematics

%D \macros
%D   {frozenhbox}
%D
%D A not so well unhboxable box can be made with:

\unexpanded\def\frozenhbox
  {\hbox\bgroup
   \dowithnextboxcs\syst_boxes_frozenhbox_finish\hbox}

\def\syst_boxes_frozenhbox_finish
  {\hbox{\hbox{\box\nextbox}}%
   \egroup}

%D \macros
%D   {setboxllx,setboxlly,gsetboxllx,gsetboxlly,getboxllx,getboxlly}
%D
%D A prelude to an extended \TEX:

\unexpanded\def\setboxllx #1#2{\expandafter\edef\csname boxes_x_\number#1\endcsname{\the\dimexpr#2\relax}}
\unexpanded\def\setboxlly #1#2{\expandafter\edef\csname boxes_y_\number#1\endcsname{\the\dimexpr#2\relax}}

\unexpanded\def\gsetboxllx#1#2{\expandafter\xdef\csname boxes_x_\number#1\endcsname{\the\dimexpr#2\relax}}
\unexpanded\def\gsetboxlly#1#2{\expandafter\xdef\csname boxes_y_\number#1\endcsname{\the\dimexpr#2\relax}}

\def\getboxllx#1{\ifcsname boxes_x_\number#1\endcsname\csname boxes_x_\number#1\endcsname\else\zeropoint\fi}
\def\getboxlly#1{\ifcsname boxes_y_\number#1\endcsname\csname boxes_y_\number#1\endcsname\else\zeropoint\fi}

\def\directgetboxllx#1{\csname boxes_x_\number#1\endcsname} % use when sure existence
\def\directgetboxlly#1{\csname boxes_y_\number#1\endcsname} % use when sure existence

%D \macros
%D   {shownextbox}
%D
%D Handy for tracing
%D
%D \starttyping
%D \shownextbox\vbox{test}
%D \shownextbox\vbox{test\endgraf}
%D \shownextbox\vbox{test\endgraf\strut\endgraf}
%D \shownextbox\vbox{test\endgraf\thinrule}
%D \shownextbox\vbox{\setupwhitespace[big]test\endgraf\thinrule}
%D \stoptyping

\unexpanded\def\shownextbox % seldom used
  {\dowithnextbox
     {\bgroup
      \showboxbreadth\maxdimen
      \showboxdepth  \maxdimen
      \scratchcounter\interactionmode
      \batchmode
      \showbox\nextbox
      \box\nextbox
      \interactionmode\scratchcounter
      \egroup}}

\unexpanded\def\spreadhbox#1% rebuilds \hbox{<box><hss><box><hss><box>}
  {\bgroup
   \ifhbox#1\relax
     \setbox2\emptybox
     \unhbox#1%
     \doloop
       {\unpenalty\unskip\unpenalty\unskip\unpenalty\unskip
        \setbox0\lastbox
        \ifvoid0
          \exitloop
        \else
          \setbox2\hbox
            {\ifhbox0 \spreadhbox0\else\box0\fi
             \ifvoid2 \else\hss\unhbox2\fi}%
        \fi}%
     \ifvoid2\else\unhbox2\fi
   \else
     \box#1%
   \fi
   \egroup}

% makes sense but too much log for overfull boxes:
%
% \showboxbreadth\maxdimen
% \showboxdepth  \maxdimen

%D Moved from cont-new:
%D
%D \starttyping
%D \minimalhbox 100pt {test}
%D \stoptyping

\unexpanded\def\minimalhbox#1#%
  {\dowithnextbox
     {\bgroup
      \setbox\scratchbox\hbox#1{\hss}%
      \ifdim\wd\nextbox<\wd\scratchbox\wd\nextbox\wd\scratchbox\fi
      \box\nextbox
      \egroup}
     \hbox}

\protect \endinput

% a bit of test code:

\hbox \bgroup
    \ruledvbox                {\hbox{\strut gans}}
    \ruledvbox to \lineheight {\hbox{\strut gans}}
    \ruledvbox to \lineheight {\hbox       {gans}}
    \ruledvbox to \strutheight{\hbox       {gans}}
    \ruledvbox to \strutheight{\hbox{\strut gans}}
    \ruledvbox to \strutheight{\vss\hbox{gans}}
\egroup

% to be considered

% \startluacode
%
%     local spacer = lpeg.patterns.spacer
%
%     function commands.withwords(command,str)
%         if str then
%             command = command or "ruledhbox"
%             local done = false
%             local function apply(s)
%                 if done then
%                     context.space()
%                     done = true
%                 else
%                     context.dontleavehmode()
%                 end
%                 context[command](s)
%             end
%             lpeg.match(lpeg.splitter(spacer,apply),str)
%         end
%
%     end
%
% \stopluacode
%
% \unprotect
%
% \unexpanded\def\withwordsinstring#1#2% command str
%   {\ctxcommand{withwords(\!!bs#1\!!es,\!!bs#2\!!es)}}
%
% \unexpanded\def\withwordsinfile#1#2% command name
%   {\ctxcommand{withwords(\!!bs#1\!!es,io.loaddata(resolvers.findfile("#2")))}}
%
% \protect
%
% \starttext
%
%     \defineframed[colored][foregroundcolor=red,foregroundstyle=\bfc\underbar,location=low]
%
%     \withwordsinstring{colored}{bla bla}
%     \withwordsinfile{colored}{ward.tex}
%
% \stoptext
