%D \module
%D   [       file=cont-new,
%D        version=1995.10.10,
%D          title=\CONTEXT\ Miscellaneous Macros,
%D       subtitle=New Macros,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

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

\unprotect

\writestatus{\m!systems}{beware: some patches loaded from cont-new.tex!}

% Speeds up when having a field with thousands of clones, 
% which we happen to have -)

\def\getmainfieldkids#1%
  {\let\@@FieldKids\empty
   \ifdefinemainfield 
     \definerawpasslist{fld:#1}% runtime defined by system 
     \moverawpasslist{fld:#1}\@@FieldKids
   \fi}

%D Let's also support a negative offset.

\def\dooutlinebox#1\color#2\offset#3\corner#4\radius#5\depth#6\toggle#7\\%
  {\vbox % rules on top of box
     {\dimen0=#3\relax
      \dimen2=\wd#1\advance\dimen2 by 2\dimen0
      \dimen4=\ht#1\advance\dimen4 by  \dimen0
      \dimen6=\dp#1\advance\dimen6 by  \dimen0\advance\dimen6 by #6\relax
\ifdim\dimen6<\!!zeropoint
  \advance\dimen4 by \dimen6 \dimen8=-\dimen6 \dimen6=\!!zeropoint
\else
  \dimen8=\!!zeropoint
\fi
      \setbox\extraframebox=\hbox
        {\dostrokedbox\width\dimen2\height\dimen4\depth\dimen6%
           \corner#4\radius#5\toggle#7\\}%
      \setbox\extraframebox=\hbox
        {\raise\dimen8\vbox{\moveleft#3\box\extraframebox}}%
      \wd\extraframebox=\wd#1%
      \ht\extraframebox=\ht#1%
      \dp\extraframebox=\dp#1%
      \hbox
        {%\copy#1\hskip-\wd#1%
         \box#1\hskip-\wd\extraframebox % faster
         \doifelsenothing{#2} % speeds up a little
           {\box\extraframebox}
           {\color[#2]{\box\extraframebox}}}}}

%D More chinese:

\unexpanded\def\uc#1#2% used in tricky situations
  {\bgroup
   \edef\unicodeone{#1}%
   \edef\unicodetwo{#2}%
   \futurelet\nextglyph\dohandleucflowglyph}

\def\dohandleucflowglyph%
  {\unicodeposition=\unicodeone\unicodetwo\relax
   \handleunicodeglyph
   \egroup}

%D Some adaptions due to active /'s. This goes to supp-lan.tex.

%D And then came the Polish users, and who can deny them? Like
%D the German \TEX\ users demand an active \type {"}, the
%D Polish cannot do without the \type {/}. The next alternative
%D is a bit slower but far the most robust alternative. As a
%D bonus it also offers \type {|^|} and even \type {|||} which
%D both result in a breakable|^|bar.
%D
%D The trick we use here is to convert the argument to a
%D string and compare this string to string'd tokens.

\def\activedotextmodediscretionary#1%
  {\bgroup
   \let\nextnextnext\egroup
   \def\handlecompoundcharacter##1% new, needed for polish
     {\getvalue{\@nc@\string##1}}%  where / is active
   \def\next##1#1%
     {\def\next{\activedododotextmodediscretionary#1{##1}}%
      \futurelet\nextnext\next}%
   \next}

\def\activedododotextmodediscretionary#1#2%
  {\convertargument#2\to\discretionarytoken
   \def\textmodediscretionary{\getvalue{textmodediscretionary\string#1}}%
   \ifx#1\nextnext % takec care of ||| and +++ and ......
     \prewordbreak\discretionary{\hbox{$#1$}}{}{\hbox{$#1$}}%
       \allowbreak\postwordbreak
     \def\nextnextnext{\afterassignment\egroup\let\next=}%
   \else\ifx\discretionarytoken\empty
     \checkafterdiscretionary
     \bgroup
       \checkbeforediscretionary
       \prewordbreak\hbox{\textmodediscretionary\nextnext}\allowbreak\postwordbreak
     \egroup
   \else\convertargument=\to\next\ifx\next\discretionarytoken
     \prewordbreak\textmodediscretionary
   \else\convertargument~\to\next\ifx\next\discretionarytoken
     \prewordbreak\discretionary{-}{}{\thinspace}\postwordbreak
   \else\convertargument_\to\next\ifx\next\discretionarytoken
     \prewordbreak\discretionary{\textmodediscretionary}
       {\textmodediscretionary}{\textmodediscretionary}\prewordbreak
   \else\convertargument(\to\next\ifx\next\discretionarytoken
     \ifdim\lastskip>\!!zeropoint\relax
       (\prewordbreak
     \else
       \prewordbreak\discretionary{}{(-}{(}\prewordbreak
     \fi
   \else\convertargument)\to\next\ifx\next\discretionarytoken
     \ifx\nextnext\blankspace
       \prewordbreak)\relax
     \else\ifx\nextnext\space
       \prewordbreak)\relax
     \else
       \prewordbreak\discretionary{-)}{}{)}\prewordbreak
     \fi\fi
   \else\convertargument'\to\next\ifx\next\discretionarytoken
     \prewordbreak\discretionary{-}{}{'}\postwordbreak
   \else\convertargument<\to\next\ifx\next\discretionarytoken
     \beginofsubsentence\prewordbreak\beginofsubsentencespacing
   \else\convertargument>\to\next\ifx\next\discretionarytoken
     \endofsubsentencespacing\prewordbreak\endofsubsentence
   \else\convertargument^\to\next\ifx\next\discretionarytoken
     \prewordbreak\discretionary{\hbox{$|$}}{}{\hbox{$|$}}%
       \allowbreak\postwordbreak
   \else
     \checkafterdiscretionary
     \bgroup
       \checkbeforediscretionary
       \prewordbreak
       \discretionary{\hbox{#2}}{}{\hbox{#2}}%
       \allowbreak\postwordbreak
     \egroup
   \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
   \nextnextnext}

%D Since we don't have to bother about active characters any
%D longer, we end up with a pretty simple activating macro:

\def\enableactivediscretionaries%
  {\let\dotextmodediscretionary=\activedotextmodediscretionary}

\enableactivediscretionaries

\def\handlecompoundcharacter#1%
  {\def\dohandlecompoundcharacter%
     {\ifx\next\bgroup
       %\def\next{\dodohandlecompoundcharacter#1}%    % handle "{ee} -> \"ee
       %\let\next\relax                               % forget "{ee} -> ee
        \def\next{\handlecompoundcharacterone#1}%     % ignore "{ee} -> "ee
      \else\ifx\next\egroup
        \def\next{\getvalue{\@nc@\string#1}}%
      \else\ifx\next\blankspace
        \def\next{\getvalue{\@nc@\string#1}}%
      \else
        \def\next{\dodohandlecompoundcharacter#1}%
      \fi\fi\fi
      \next}%
   \futurelet\next\dohandlecompoundcharacter}

%D So far for supp-lan.tex.

%D For Berend:

\def\simpledoblanko%
  {\doifelse{\currentwitruimte}{\v!geen}
     {\blanko[\currentblanko]}
     {\blanko[\s!default]}}

\definieerblanko
  [\s!default]
  [\v!wit]

%\definieerblanko
%  [\s!default]
%  [\v!groot]

%D Yes or no:

\def\collectlocalfootnotes%
  {\def\localfootinsert##1% was \gdef, but never reset!
     {%\message{[local footnote]}%
      \global\setbox\localpostponedfootnotes=\vbox\bgroup
        \unvbox\localpostponedfootnotes
        \let\next}}

\def\doplaatskoptekst#1#2#3#4%
  {\beginheadplacement{#1}%
   \setbox0=\hbox
     {\getvalue{\??ko#1\c!commando}
        {} % no number
        {\doattributes
           {\??ko#1}\c!letter\c!kleur
           {\doattributes
              {\??ko#1}\c!tekstletter\c!tekstkleur
              {\dontconvertfont
               \ifdisplaysectionhead\stelinterliniein
\else
  \stelspatieringin
\fi
               #2%
               \getvalue{\??ko#1\c!voorcommando}%
               \getvalue{\??ko#1\c!tekstcommando}%
                 {\ifdisplaysectionhead
                    \setstrut\begstrut#3\endstrut
\xdef\localheaddepth{\the\dp\strutbox}%
                  \else
                    #3%
                  \fi}%
               \getvalue{\??ko#1\c!nacommando}%
               \ifdisplaysectionhead\endgraf\fi}}}}%
   \endheadplacement{#1}{#4}}

\def\doplaatskopnummertekst#1#2#3#4#5%
  {\beginheadplacement{#1}%
   \setbox0=\hbox
     {\getvalue{\??ko#1\c!commando}%
        {\doattributes{\??ko#1}\c!letter\c!kleur
           {\doattributes{\??ko#1}\c!nummerletter\c!nummerkleur
              {\getvalue{\??ko#1\c!voorcommando}%
               \getvalue{\??ko#1\c!nummercommando}%
                 {\ifdisplaysectionhead
                    \setstrut\begstrut#3\endstrut
                  \else
                    #3%
                  \fi}}}}
        {\doattributes{\??ko#1}\c!letter\c!kleur
           {\doattributes{\??ko#1}\c!tekstletter\c!tekstkleur
              {\dontconvertfont
               \ifdisplaysectionhead\stelinterliniein
\else
  \stelspatieringin
\fi
               #2%
               \getvalue{\??ko#1\c!tekstcommando}%
                 {\ifdisplaysectionhead
                    \setstrut\begstrut#4\endstrut
\xdef\localheaddepth{\the\dp\strutbox}%
                  \else
                    #4%
                  \fi}%
               \getvalue{\??ko#1\c!nacommando}%
               \ifdisplaysectionhead\endgraf\fi}}}}%
   \endheadplacement{#1}{#5}}

%D depth

\def\beginheadplacement#1%
  {\bgroup
   \gdef\localheaddepth{\dp\strutbox}%
   \everypar{}% needed indeed
   \noindent  % ipv \witruimte elders, na \forgetall !
   \bgroup
   \forgetall % now we may forget everything
  %\showcomposition
   \mindermeldingen
   \postponefootnotes
   \iflocation\ifdisplaysectionhead\else\noninterferingmarks\fi\fi
   \setupinteraction
     [\c!letter=,
      \c!kleur=,
      \c!contrastkleur=]%
   \strictouterreferencestrue % tzt instelling
   \def\localkopsetup%
     {\dolocalkopsetup{#1}}%
   \startsynchronisatie}

\def\endheadplacement#1#2%
  {\doifvaluenothing{\??ko#1\c!file}{\locationfalse}%
   \ifdisplaysectionhead
     \let\headlastlinewidth\!!zeropoint
     \snaptogrid\hbox
       {\iflocation
          \ifautocrossdocument
            \naarbox{\box0}[\getvalue{\??ko#1\c!file}::#1]%
          \else
            \hbox{\box0}%
          \fi
        \else
          \hbox{\box0}%
        \fi
        \doflushfootnotes}% new
     \endgraf
     \nointerlineskip
     \dosomebreak\nobreak
     #2%
   \else
     \strut
     \doflushfootnotes % new
     \iflocation
       \ifautocrossdocument
         \hhboxindent=\ifcontinuoushead\headlastlinewidth\else\!!zeropoint\fi
         \unhhbox0\with{\naarbox{\box\hhbox}[\getvalue{\??ko#1\c!file}::#1]}%
         \advance\lasthhboxwidth by \numberheaddistance
         \xdef\headlastlinewidth{\the\lasthhboxwidth}%
       \else
         \unhbox0
         \global\let\headlastlinewidth\!!zeropoint
       \fi
     \else
       \unhbox0
       \global\let\headlastlinewidth\!!zeropoint
     \fi
     #2%
     \dimen0=\numberheaddistance
     \hskip\dimen0 \!!plus \dimen0 \!!minus .25\dimen0
     \hskip\headsignal\ignorespaces
   \fi
   \ifdisplaysectionhead
     \ifgridsnapping % important, font related depth, see comment
       \prevdepth\dp\strutbox
     \else
       \prevdepth\localheaddepth
     \fi
   \fi
   \stopsynchronisatie
   \egroup
   \egroup
\doflushfootnotes % new, still needed?
   \ifdisplaysectionhead
     \doifvalue{\??ko#1\c!springvolgendein}{\v!nee}{\noindentation}%
   \else
     \nonoindentation % recently added, was a bug
   \fi}

%D Due to polish active /'s we have to deal a bit more
%D robust with filename:

%D ...

%D Some day:

% \definieermarkering[\v!margetitel]
%
% \def\complexmargewoord[#1][#2]#3%
%   {\doglobal\increment\margincontent
%    \stelinmargein[\margincontent][]% see next macro
%    \@EA\setgvalue\@EA{\@EA\??im\@EA\margincontent\@EA}\@EA
%      {\@EA\stelinmargein\@EA[\margincontent][]%  see next macro
%       \flushmargincontent[#1][#2]{#3}%
%       \markeer[\v!margetitel]{#3}}}

%D Table


%D New indeed. Undocumented and under development.

\def\startsimplecolumns%
  {\dosingleempty\dostartsimplecolumns}

\def\dostartsimplecolumns[#1]%
  {\bgroup
   \getparameters[\??kl][\c!breedte=\hsize,\c!afstand=1em,\c!n=2,#1]%
   \setrigidcolumnhsize\@@klbreedte\@@klafstand\@@kln
   \setbox\scratchbox=\vbox\bgroup
   \forgetall}

\def\stopsimplecolumns%
  {\removelastskip
   \egroup
   \rigidcolumnbalance\scratchbox
   \egroup}

%D Aha.

\def\rigidcolumnbalance#1%
  {\global\chardef\rigidcolumnbox=#1\relax
   \ifnum\rigidcolumns=1
     \ifinner\ifhmode\box\else\unvbox\fi\else\unvbox\fi\rigidcolumnbox
   \else
     \hbox to \savedrigidhsize % was \hsize
       {\vbadness=10000
        \tabskip\!!zeropoint
        \setbox\rigidcolumnbox=\vbox
          {\unvbox\rigidcolumnbox
           \unpenalty\removelastskip}% get rid of \blank's
        \splittopskip=\openstrutheight
\splittopskip=\!!zeropoint
       %\scratchdimen=\ht\rigidcolumnbox      % sensitive for overflow
       %\divide\scratchdimen by \rigidcolumns % therefore we need the hack:
        \scratchdimen=1pt
        \divide\scratchdimen by \rigidcolumns
        \expanded{\scratchdimen=\withoutpt{\the\scratchdimen}\ht\rigidcolumnbox}%
        \advance\scratchdimen by \ht\strutbox
        \valign{##\vfill\cr\dorigidcolumnsplits}}%
   \fi}

%D Is this a better default?

\def\doverbatimgoodbreak%
  {} % instead of {\ifoptimizeverbatim\penalty100\fi}

%D Hm:

\def\startgeheel%
  {\startregelcorrectie
   \insidefloattrue}

\def\stopgeheel
  {\stopregelcorrectie}

%D Can save lots of mpgraph files when using pdftex:

\newif\ifrecycleMPslots \recycleMPslotsfalse

\def\allocateMPslot#1%
  {\ifrunMPgraphics
     \ifcase\pdfoutput \recycleMPslotsfalse \fi
   \else
     \recycleMPslotsfalse
   \fi
   \ifrecycleMPslots
     \doloop
       {\doifundefined{\recurselevel MP}
          {\global\letvalue{\recurselevel MP}=\empty
           \global#1=\recurselevel\exitloop}}%
    %\message{[MP slot + \number#1]%
   \else
     \global\advance#1 by 1
   \fi}

\def\deallocateMPslot#1%
  {\ifrunMPgraphics
     \ifcase\pdfoutput \recycleMPslotsfalse \fi
   \else
     \recycleMPslotsfalse
   \fi
   \ifrecycleMPslots
     \global\letbeundefined{\number#1MP}%
    %\message{[MP slot - \number#1]}%
   \fi}

%D For Taco and David: (to be documented and added to 
%D setupb too). 

\def\definereferenceformat%
  {\dodoubleargument\dodefinereferenceformat}

\def\dodefinereferenceformat[#1][#2]%
  {\iffirstargument
     \getparameters[\??rf#1]
       [\c!links={[},
        \c!rechts={]},
        \c!tekst=,
        \c!label=,
        \c!commando=\in,
        #2]%
     \unexpanded\setvalue{#1}{\doexecutereferenceformat{#1}}%
   \fi}

\def\noexecutelabelreferenceformat#1%
  {\doifvaluesomething{\??rf#1\c!tekst}
     {\gdef\textofreference{\getvalue{\??rf#1\c!tekst}}}%
   \getvalue{\??rf#1\c!commando}}

\def\doexecutelabelreferenceformat#1%
  {\getvalue{\??rf#1\c!commando}%
     {\leftlabeltext {\getvalue{\??rf#1\c!label}}}%
     {\rightlabeltext{\getvalue{\??rf#1\c!label}}}}

\def\doexecutereferenceformat#1%
  {\gdef\leftofreference {\getvalue{\??rf#1\c!links }}%
   \gdef\rightofreference{\getvalue{\??rf#1\c!rechts}}%
   \global\let\textofreference\empty % otherwise ~ added
   \doifelsevaluenothing{\??rf#1\c!label}
     {\let\next\noexecutelabelreferenceformat}
     {\let\next\doexecutelabelreferenceformat}%
   \next{#1}}

\let\leftofreference \relax
\let\rightofreference\relax
\let\textofreference \relax

\def\dodododoinatreference#1#2#3[#4]%
  {\ifx\next\bgroup
     \dododododoinatreference
       {\leftofreference#1\ignorespaces#3\rightofreference}{#2}[#4]%
   \else
     \dododododoinatreference
       {\leftofreference#1\rightofreference}{#2#3}[#4]%
   \fi}

\def\dododododoinatreference#1#2[#3]%
  {\bgroup
   \forgetall
   \leaveoutervmode
   \doifreferencefoundelse{#3}
     {\bgroup
      \let\ignorespaces    \empty % rather dirty trick, but ok
      \let\leftofreference \empty % the same, again ok
      \let\rightofreference\empty % and once more
      \def\textofreference {#2}%  % temporary value
      \ifx\textofreference\empty  % simple expansion
        \doifelsenothing{#1}
          {\egroup\dosymbolreference{#1}{\textofreference}[#3]}
          {\egroup\dowantedreference{#1}{\textofreference}[#3]}%
      \else
        \doifelsenothing{#1}
          {\egroup\dosymbolreference{#1}{#2}[#3]}
          {\egroup\dowantedreference{#1}{#2}[#3]}%
      \fi}
     {\dounknownreference{#1}{#2}[#3]}%
   \referentieinfo{<}{#3}%
   \global\let\leftofreference \empty
   \global\let\rightofreference\empty
   \global\let\textofreference \empty
   \egroup}

%D The next hack is used for some exiting new feature test.

\def\resetpositions%
  {\let\someposition=\gobblesevenarguments}

\resetpositions

%D The next line can be uncommented when Taco's extensions to
%D \quote {\TEX\ The Program} are used.

% \readfile {syst-eet} {} {}

% I have to scan for \unhbox and use the vbox/hbox trick instead

\protect

\endinput
