supp-mrk.mkii / last modification: 2020-01-30 14:15
%D \module
%D   [       file=supp-mrk,
%D        version=1995.10.10,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Marks,
%D         author=Jim Fox / Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Remark: due to the lack of \type {\clearmark}, the \ETEX\
%D dedicated mechanism is not yet operational.

%D This module has deverted so much from the original that I
%D can probably rewrite it to a more efficient one now and
%D I will do so.

%D \BOXES, 16~in- and output buffers, but there is only one
%D \MARK. In TugBoat~8 (1987, no~1) Jim Fox presents a set of
%D macros that can be used to mimmick multiple marks. We
%D gladly adopt them here. I may rewrite this module from
%D scratch some day, since some low level \CONTEXT\ commands
%D can be used.
%D This module was changed on behalf of \ETEX. The main
%D extension is that \type{\get..} and alike is used instead of
%D direct calls. The \TEX\ based multiple marks needs to store
%D the mark data but \ETEX\ uses a different approach.

\writestatus{loading}{ConTeXt Support Macros / Marks}


%D We start with the presetting the interface macros.
%D \starttabulate[|||]
%D \NC \type{\getmarks}          \NC sets the marks to their values \NC \NR
%D \NC \type{\getallmarks}       \NC sets all marks to their values \NC \NR
%D \NC \type{\getsplitmarks}     \NC sets the splitmarks to their values\NC \NR
%D \NC \type{\getallsplitmarks}  \NC sets all splitmarks to their values\NC \NR
%D \NC \type{\setmarks}          \NC synchronizes topmarks (\ETEX) \NC \NR
%D \stoptabulate
%D Later we will overload these, dependent of the brand of
%D \TEX\ that we use.

\let \getmarks           \gobbleoneargument
\let \getallmarks        \relax
\let \getsplitmarks      \gobbleoneargument
\let \getallsplitmarks   \relax
\let \setallmarks        \relax

\let \newmark            \gobbleoneargument
\let \newpersistentmark  \gobbleoneargument
\let \resetmark          \gobbleoneargument
\let \setmark            \gobbletwoargument

%D \macros
%D   {expandmarks}
%D We can force expansion of marks with the following switch.

\newif\ifexpandmarks  \expandmarkstrue % hm, true indeed ?


%D This implementation is more or less compatible with the
%D other \type {\new} macros in \PLAIN\ \TEX. A mark is
%D defined by:
%D \starttyping
%D \newmark\name
%D \stoptyping
%D and can be called upon with:
%D \starttyping
%D \gettopmark  \name  % or \topname
%D \getbotmark  \name  % or \botname
%D \getfirstmark\name  % or \firstname
%D \stoptyping
%D The only drawback of his approach is that the marks must be
%D preloaded in the output routine. This is accomplished by
%D means of:
%D \starttyping
%D \getmarks\name
%D \stoptyping
%D The macros presented here are in most aspects copies of
%D those presented by Jim Fox. We've taken the freedom to
%D change a few things for more or less obvious reasons:
%D \startitemize
%D \item  Because the original macros look quite complicated,
%D       which is mainly due to extensive use of
%D       \type{\expandafter}'s and \type{\csname}'s, we changed
%D       those in favor of \type{\getvalue}.
%D \item  To be more in line with the rest of \CONTEXT, we've
%D       changed some of the names of macros.
%D \item  Because we are already short on \COUNTERS\ we use
%D       macros when possible.
%D \item  We maintain a list of defined marks and use one
%D       call for getting them all at once.
%D \item  We have extended the mechanism to splitmarks (not
%D       perfected yet).
%D \item  We've introduced optional expansion of the contents
%D       of marks.
%D \stopitemize
%D Whatever changes we've made, the credits still go to Jim,
%D whatever goes wrong is due to me. The method is described
%D in the TugBoat mentioned before, so we won't go into
%D details. All marks belonging to a group are packed in a
%D list. In this list they are preceded by a macro that can
%D be defined at will and a number concerning the position at
%D which it was defined.
%D \starttyping
%D \def\somelist{... \domark5{this} ... \domark31{that} ...}
%D \stoptyping
%D The original \type{\mark} keeps track of the number and
%D \type{\topmark} and \type{\botmark} are used to extract the
%D actual marks from the list. The counting is done by
%D \starttyping
%D \currentmarker
%D \stoptyping
%D In \CONTEXT\ we use the mark mechanism to keep track of
%D colors. In a complicated documents with many colors per
%D page, \type{\currentmarker} can therefore get pretty high.
%D (Well, this is not completely true, because we don't
%D always have to use marks.)


%D The original implementation used a few more \COUNTERS. Two
%D have been substituted by macros, one has been replaced by
%D our scratch counter.
%D \starttyping
%D \newcount\topmarker
%D \newcount\botmarker
%D \newcount\foundmarker
%D \stoptyping
%D We've also introduced some constants, one for the lists and
%D three for composing the mark commands.

\def\@@marklist@@    {*m*} % {marklist}
\def\@@marktop@@     {*t*} % {top}
\def\@@markbot@@     {*b*} % {bot}
\def\@@markfirst@@   {*f*} % {first}
\def\@@markcurrent@@ {*c*} % {current}

%D The next one is new too. All defined marks are packed in a
%D comma seperated list. This could of course have been a token
%D list but \CONTEXT\ has some preference for comma lists.


%D The next macro replaces the multiple step expansion and
%D command name constructors of Jim. This alternative leads to
%D a more readable source (we hope).

\def\makemarknames#1% kan genest werken

%D \macros
%D   {newmark,resetmark}
%D A mark is defined by \type{\newmark}. At the same time,
%D the name of the mark is added to a commalist. The
%D three initializations were not in the original design, but
%D make calls from outside the output routine a bit more
%D robust.

\let\domark\relax % saves a restore on the stack

   \letgvalueempty{\@@marktop@@    \markname}%
   \letgvalueempty{\@@markfirst@@  \markname}%
   \letgvalueempty{\@@markbot@@    \markname}%
   \setgvalue{\marklist}{\domark0{}}% beware of halfway definitions


%D Don't ask me, but sometimes we need more control over
%D updating the marks, thereby we have:

\def\newpersistentmark#1% for an example see core-grd.tex

\let\setmark  \empty

%D Some more natural interfacing macros:

\def\getcurrentmark    #1{\getvalue{\@@markcurrent@@\strippedcsname#1}}
\def\gettopmark        #1{\getvalue{\@@marktop@@    \strippedcsname#1}}
\def\getbottommark     #1{\getvalue{\@@markbot@@    \strippedcsname#1}}
\def\getfirstmark      #1{\getvalue{\@@markfirst@@  \strippedcsname#1}}
\def\getsplitbottommark#1{\getvalue{\@@markbot@@    \strippedcsname#1}}
\def\getsplitfirstmark #1{\getvalue{\@@markfirst@@  \strippedcsname#1}}

%D \macros
%D   {setmark}
%D Setting a new mark and adding a mark to the designated
%D list is done by \type{\addmarker}. This is an internal
%D command, the user set a marks bij calling it's name:
%D \starttyping
%D \setmark\mymark{some text} % or \mymark{some text}
%D \stoptyping
%D Where \type{\mymark} is previously defined by
%D \type{\newmark}.

   \global\advance\currentmarker \plusone
     \!!toksb\@EA{#2}% one level, why ? handy for cs

%D \macros
%D   {getmarks,getallmarks,
%D    getsplitmarks,getallsplitmarks}
%D In fact, marks make only sense in the output routine. Marks
%D are derived from their list by means of \type{\getmarks}.
%D Only one call per mark is permitted in the output routine.
%D Therefore, it's far more easy to get them all at once, by
%D means of \type{\getallmarks}, which is not part of the
%D original design.
%D This grabbing is done by processing the list using the
%D embedded \type{\domark} macros. When a relevant mark is
%D found, this macro is reassigned and from then on serves
%D in building the new list.

% Hm, resetting \!!toksa got lost and took me a half a day to
% trace down ([] showed up in the pagebody); I really have
% to clean up this messy module (write it from scratch).

\newif\ifnofirstmarker % an auxiliary switch

   % does more worse than good
   %\message{markstatus : [\the\!!toksa\the\!!toksb\the\!!toksc]}%

   % \@EA\let\@EA\savedmarklist\@EA\csname\marklist\endcsname
   % does more worse than good
   % \global\@EA\let\csname\marklist\endcsname\savedmarklist

\def\getallmarks     {\processcommacommand[\allmarks]\getmarks}

\def\getallmarks     {\@EA\processcommalist\@EA[\allmarks]\getmarks}



\long\def\dorecovermarks#1\lastmark% nice trick

\def\lastmark% rest of trick

%D The \ETEX\ way of doing things \unknown

\beginETEX \marks cum suis

\newtoks \listofmarks


%D We will use two state variables per mark, one to signal
%D that a new mark value is set, and one to trigger (on the
%D next page) the setting of the top mark.

\def\checkedtopmarks  #1{\csname\@@trk\string#1\endcsname}
\def\checkedbotmarks  #1{\csname\@@brk\string#1\endcsname}
\def\thecurrentmarks  #1{\csname\@@crk\string#1\endcsname}

  {%\writestatus{marks}{setting \string#1}\wait

\def\setexpandedmark#1#2% % marks expand anyway

\def\setnormalmark#1#2% using a tok prevents unwanted expansion in mark
  {\begingroup % don't ask why, but grouping is needed
   \scratchtoks{#2}% in order to to mess up scratchtoks
   \normalmarks#1{\the\scratchtoks}% one level expansion

  {%\writestatus{marks}{checking \string#1}\wait

%  {\ifcase0\@EA\normalfirstmarks\csname\@@prk\string#1\endcsname\else
%     \@EA\ifx\csname\@@frk\string#1\endcsname\empty
%       \@EA\gdef\csname\@@frk\string#1\endcsname{\normalfirstmarks#1}%
%       \@EA\gdef\csname\@@brk\string#1\endcsname{\normalbotmarks  #1}%
%     \else
%       \@EA\gdef\csname\@@trk\string#1\endcsname{\normaltopmarks  #1}%
%       \global\@EA\chardef\csname\@@mrk\string#1\endcsname\zerocount
%     \fi
%   \fi}

       \@EA\gdef\csname\@@brk\string#1\endcsname{\normalbotmarks  #1}%
       \@EA\gdef\csname\@@trk\string#1\endcsname{\normaltopmarks  #1}%

%\def\resetmark#1% we cannot use \normalmarks#1{}
%  {\global\@EA\chardef\csname\@@mrk\string#1\endcsname\zerocount
%   \@EA\normalmarks\csname\@@prk\string#1\endcsname{0}%
%   \global\@EA\let\csname\@@trk\string#1\endcsname\empty
%   \global\@EA\let\csname\@@frk\string#1\endcsname\empty
%   \global\@EA\let\csname\@@brk\string#1\endcsname\empty
%   \global\@EA\let\csname\@@crk\string#1\endcsname\empty}

\def\resetmark#1% we cannot use \normalmarks#1{}
   \@EA\normalmarks\csname\@@prk\string#1\endcsname{}% {0}%

  {\ifcsname\@@prk\string#1\endcsname\else % this is etex -)
     \@EA\newmarks\csname\@@prk\string#1\endcsname % status mark
   \@EA\gdef\csname\@@trk\string#1\endcsname{\normaltopmarks  #1}%
   \@EA\gdef\csname\@@brk\string#1\endcsname{\normalbotmarks  #1}}

\let \newmark           \definenewmark
\let \newpersistentmark \newmarks % \definenewmark
\let \normalsetmark     \setmark

\def\getallmarks{\the\listofmarks} % \def\setallmarks{\the\listofmarks}

%D In \type {page-ini} or \type {core-mar} we should say:
%D \starttyping
%D \appendtoks \getallmarks \to \everybeforepagebody
%D \appendtoks \setallmarks \to \everyafterpagebody
%D \stoptyping

\let\getcurrentmark     \thecurrentmarks
\let\gettopmark         \checkedtopmarks
\let\getbottommark      \checkedbotmarks     % \normalbotmarks
\let\getfirstmark       \checkedfirstmarks   % \normalfirstmarks
\let\getsplitbottommark \normalsplitbotmarks
\let\getsplitfirstmark  \normalsplitfirstmarks

\let\getbotmark         \getbottommark
\let\getsplitbotmark    \normalsplitbotmarks
\let\getsplittopmark    \normalsplitfirstmarks


%D A couple of goodies:

\let\getbotmark     \getbottommark

%D \macros
%D   {noninterferingmarks}
%D Marks can interfere badly with for instance postprocessing
%D paragraphs, for instance when we want to grab the last box
%D using \type {\lastbox}, when at the same time using colors.




%D This macro is for instance used in the inline headings
%D postprocessing, as needed when we want to make those
%D clickable.

%D Right from the beginning, \CONTEXT\ supported more than one
%D mark, using an extended version of Jim Fox multiple mark
%D mechanism. In \ETEX\ we can however directly access more
%D marks than we will ever need.

%D Resetting marks in not compatible with the old method.
%D Here a node is inserted, which can interfere badly. In
%D fact, a real \type {\clearmarks\name} is needed.
%D \starttyping
%D \def\resetmark#1{\marks#1{}}
%D \stoptyping
%D A possible macro solution is presented here. When discussing
%D \type {\clearmarks} on the \ETEX\ discussion list, Chris
%D Rowley suggested to use a reset flag. Unfortunately this
%D is not enough since we need to keep track of both set and
%D reset state then. This means that we must postpone resetting
%D to the page following the set, and as a result we need
%D another auxiliary macro. The current solution is the best
%D I could come up with so far, especially given that we
%D need a fast mechanism.

%D For those who want to know the story behind resetting
%D marks, here are some examples of interference
%D \startbuffer
%D \setbox0=\vbox{test}
%D \unvbox0\setbox0=\lastbox
%D \ruledhbox{\unhbox0}
%D \stopbuffer
%D \typebuffer\blank\getbuffer\blank
%D \startbuffer
%D \setbox0=\vbox{test\normalmark{}}
%D \unvbox0\setbox0=\lastbox
%D \ruledhbox{\unhbox0}
%D \stopbuffer
%D \typebuffer\blank\getbuffer\blank
%D \startbuffer
%D \setbox0=\vbox{test\hbox{\normalmark{}}}
%D \unvbox0\setbox0=\lastbox
%D \ruledhbox{\unhbox0}
%D \stopbuffer
%D \typebuffer\blank\getbuffer\blank
%D \startbuffer
%D \setbox0=\vbox{test\vbox{\normalmark{}}}
%D \unvbox0\setbox0=\lastbox
%D \ruledhbox{\unhbox0}
%D \stopbuffer
%D \typebuffer\blank\getbuffer\blank

%D One final advice. Use marks with care. When used in globally
%D assigned boxes, the list can grow quite big, and processing
%D can slow down considerably. This drawback is removed in
%D \ETEX\ mode.


\let\rawnewmark          \newmark
\let\rawdefinemark       \newmark
\let\rawsetmark          \setmark
\let\rawgettopmark       \gettopmark
\let\rawgetfirstmark     \getfirstmark
\let\rawgetbotmark       \getbotmark
\let\rawgetsplitbotmark  \normalsplitbotmark
\let\rawgetsplittopmark  \normalsplitfirstmark



\let\rawnewmark          \newmarks
\let\rawdefinemark       \newmarks
\let\rawsetmark          \normalmarks
\let\rawgettopmark       \normaltopmarks
\let\rawgetfirstmark     \normalfirstmarks
\let\rawgetbotmark       \normalbotmarks
\let\rawgetsplitbotmark  \normalsplitbotmarks
\let\rawgetsplittopmark  \normalsplitfirstmarks


\protect \endinput