%D \module
%D   [       file=core-mar,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Markings,
%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.

\writestatus{loading}{Context Core Macros / Markings}

\unprotect

\prependtoks \getallmarks \to \everybeforepagebody
\prependtoks \setallmarks \to \everyafterpagebody  % currently \relax

% voor 'interne' doeleinden zijn beschikbaar:
%
%   \fetchmark[naam][plaats]

% nog expansie in hoofdmarkering

% ook nog reset koppelen aan sectie

%\def\hoofdmarkering#1%
%  {\csname\??mk#1\c!koppeling\endcsname}
%
%\def\fastresetmarker#1%
%  {\doifdefined{\??mk\hoofdmarkering{#1}}
%     {\expandafter\resetmark\csname\??mk\hoofdmarkering{#1}\endcsname}}

\beginTEX

\def\hoofdmarkering#1%
  {\@EA\ifx\csname\??mk#1\c!koppeling\endcsname\else
     \csname\??mk#1\c!koppeling\endcsname
   \fi}

\def\fastresetmarker#1%
  {\@EA\ifx\csname\??mk#1\c!koppeling\endcsname\else
     \@EA\resetmark\csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname
   \fi}

\endTEX

\beginETEX \ifcsname

\def\hoofdmarkering#1%
  {\ifcsname\??mk#1\c!koppeling\endcsname
     \csname\??mk#1\c!koppeling\endcsname
   \fi}

\def\fastresetmarker#1%
  {\ifcsname\??mk#1\c!koppeling\endcsname
     \@EA\resetmark\csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname
   \fi}

\endETEX

\def\fastresetmarkerlist[#1]%
  {\expanded{\rawprocesscommalist[#1]}\fastresetmarker}

\def\doresetmarkering[#1]%
  {\processcommalist[#1]\fastresetmarker}

\def\resetmarkering
  {\dosingleargument\doresetmarkering}

\def\dostelmarkeringin[#1][#2]%
  {\def\docommando##1{\getparameters[\??mk##1][#2]}%
   \processcommalist[#1]\docommando}

\def\stelmarkeringin
  {\dodoubleargument\dostelmarkeringin}

% betere protectie

\letvalue{\??mk\??mk \v!vorige}\gettopmark
\letvalue{\??mk\??mk \v!eerste}\getfirstmark
\letvalue{\??mk\??mk\v!laatste}\getbotmark
\letvalue{\??mk\??mk\v!huidige}\getcurrentmark

\def\dododefinieermarkering[#1][#2]%
  {\stelmarkeringin[#1]
     [\c!expansie=\v!nee,  % saves a macro
      \c!scheider={ --- }, % watch the spaces
      \c!limittext=\@@kolimittext,
        \c!status=\v!start]%
   \ontkoppelmarkering[#1]%  % no coupling with sections
   \setevalue{\??mk#1\c!koppeling}{#2}%
   \expandafter\newmark\csname\??mk#2\endcsname
   \showmessage\m!systems{13}{#1,[#2]}}

\def\dodefinieermarkering[#1][#2]%
  {\doifelsenothing{#2}
     {\dododefinieermarkering[#1][#1]}
     {\dododefinieermarkering[#1][#2]}}

\def\definieermarkering
  {\dodoubleempty\dodefinieermarkering}

\let\geenmarkering\empty

\def\fetchmark[#1][#2]% never \unexpanded
  {\@EA\@EA\csname\??mk\??mk#2\endcsname
     \csname\??mk\hoofdmarkering{#1}\endcsname}

\beginTEX

\def\fetchmark[#1][#2]% never \unexpanded
  {\@EA\ifx\csname\??mk#1\c!koppeling\endcsname\else
     \@EA\@EA\csname\??mk\??mk#2\endcsname
     \csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname
   \fi}

\endTEX

\beginETEX \ifcsname

\def\fetchmark[#1][#2]% never \unexpanded
  {\ifcsname\??mk#1\c!koppeling\endcsname
     \@EA\@EA\csname\??mk\??mk#2\endcsname
     \csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname
   \fi}

\endETEX

\def\fetchtwomarks[#1]%
  {\doifsomething{\fetchmark[#1][\v!eerste]}
     {\fetchmark[#1][\v!eerste]%
      \doifsomething{\fetchmark[#1][\v!laatste]}
        {\doifnot{\fetchmark[#1][\v!eerste]}{\fetchmark[#1][\v!laatste]}
            {\getvalue{\??mk#1\c!scheider}\fetchmark[#1][\v!laatste]}}}}

\def\fetchallmarks[#1]%
  {\doifsomething{\fetchmark[#1][\v!eerste]}
     {\doifsomething{\fetchmark[#1][\v!vorige]}
        {\doifnot{\fetchmark[#1][\v!vorige]}{\fetchmark[#1][\v!eerste]}
           {\fetchmark[#1][\v!vorige]\getvalue{\??mk#1\c!scheider}}}}%
      \fetchtwomarks[#1]}

\def\dohaalmarkering[#1][#2]%
  {\doifvalue{\??mk#1\c!status}\v!start
     {\bgroup
     %\def\geenmarkering##1{\unknown\ }%
      \def\geenmarkering{\splitsequence{\getvalue{\??mk#1\c!limittext}}}%
      \setfullsectionnumber{\??mk#1}%
      \processaction % slow
        [#2]
        [  \v!beide=>{\fetchtwomarks[#1]},
           \v!alles=>{\fetchallmarks[#1]},
         \s!default=>{\fetchmark    [#1][\v!eerste]},
         \s!unknown=>{\fetchmark    [#1][#2]}]%
      \egroup}}

\def\nohaalmarkering[#1][#2]%
  {}

\unexpanded\def\haalmarkering%
  {\dodoubleargument\dohaalmarkering}

\let\setsomemark\setmark

\beginTEX

\def\domarking[#1]#2%
  {\@EA\ifx\csname\??mk#1\c!koppeling\endcsname\else
     \bgroup
     \doifelsevalue{\??mk#1\c!expansie}\v!ja
       \expandmarkstrue\expandmarksfalse
     \@EA\setsomemark\csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname{#2}%
     \egroup
   \fi}

\endTEX

\beginETEX \ifcsname

\def\domarking[#1]#2%
  {\ifcsname\??mk#1\c!koppeling\endcsname
     \bgroup
     \doifelsevalue{\??mk#1\c!expansie}\v!ja
       \expandmarkstrue\expandmarksfalse
     \@EA\setsomemark\csname\??mk\csname\??mk#1\c!koppeling\endcsname\endcsname{#2}%
     \egroup
   \fi}

\endETEX

\def\marking
  {\dosingleargument\domarking}

%D Used in placing text lines.

\def\doifelsemarking#1%
  {\ifundefined{\??mk#1\c!koppeling}%
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

%D And then \unknown\ we had a chaptertitle packaged in a
%D makeup environment. And we don't want to loose marks there!

\newbox\collectedmarks

\def\flushmarks % use with care to avoid empty pages
  {\ifvoid\collectedmarks\else\unhbox\collectedmarks\fi}

\def\postponemarks
  {\let\setsomemark\postponemark}

\def\postponemark#1#2%
  {%\writestatus{marks}{postponing \string#1 => #2}%
   \global\setbox\collectedmarks\hbox
     {\unhbox\collectedmarks\setmark{#1}{#2}}}

\protect \endinput

% Pseudo marks: (for Hraban)
%
% \def\RegisterPageMark#1#2%
%   {\iftrialtypesetting \else
%      \doglobal\increment\NameCounter
%      \textreference[#1:t:\NameCounter]{#2}%
%      \doifreferencefoundelse{#1:t:\NameCounter}
%        {\doifundefined{#1:f:\currentrealreference}%
%           {\setxvalue{#1:f:\currentrealreference}%
%              {\noexpand\in[#1:t:\NameCounter]}}%
%         \setxvalue{#1:l:\currentrealreference}%
%           {\noexpand\in[#1:t:\NameCounter]}}%
%        {}%
%    \fi}
%
% \def\GetFirstOnPage#1{\getvalue{#1:f:\realfolio}}
% \def\GetLastOnPage #1{\getvalue{#1:l:\realfolio}}
%
% \setupheadertexts[\GetFirstOnPage{Name}][\GetLastOnPage{Name}]
%
% \starttext
%
% \def\Name#1{\RegisterPageMark{Name}{#1}#1}
% \def\TestLine#1{\NC test \NC \Name {test: #1} \NC \NR}
%
% \starttabulate
% \dorecurse{100}{\expanded{\TestLine{\recurselevel}}}
% \stoptabulate
%
% \stoptext
