%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 mainmarking

% ook nog reset koppelen aan sectie

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

\beginTEX

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

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

\endTEX

\beginETEX \ifcsname

\def\mainmarking#1%
  {\ifcsname\??mk#1\c!coupling\endcsname
     \csname\??mk#1\c!coupling\endcsname
   \fi}

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

\endETEX

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

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

\def\resetmarking
  {\dosingleargument\doresetmarking}

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

\def\setupmarking
  {\dodoubleargument\dosetupmarking}

% betere protectie

\letvalue{\??mk\??mk\v!previous}\gettopmark
\letvalue{\??mk\??mk\v!first   }\getfirstmark
\letvalue{\??mk\??mk\v!last    }\getbotmark
\letvalue{\??mk\??mk\v!current }\getcurrentmark

% todo: make it work in balancing
%
% \definemarking[vers][]
% \setupheadertexts
%   [\doiftext{\getmarking[vers][first]}
%      {\doiftextelse{\getmarking[vers][column:last]}
%         {\getmarking[vers][first] -- \getmarking[vers][column:last]}
%         {\getmarking[vers][first]}}]
% \starttext
%   \startcolumns[n=2,balance=no]
%     \dorecurse{10}{\expanded{\marking[vers]{\recurselevel}} \recurselevel:\dorecurse{4}{\input ward } \endgraf}
%   \stopcolumns
% \stoptext

\letvalue{\??mk\??mk\v!column:\v!first}\getsplitfirstmark
\letvalue{\??mk\??mk\v!column:\v!last }\getsplitbottommark

\ifx\decouplemarking\undefined \def\decouplemarking[#1]{} \fi

\def\dododefinemarking[#1][#2]%
  {\getparameters[\??mk#1]
     [\c!expansion=\v!no,  % saves a macro
      \c!separator={ --- }, % watch the spaces
      \c!limittext=\@@kolimittext,
          \c!state=\v!start]%
   \decouplemarking[#1]%  % no coupling with sections
   \setevalue{\??mk#1\c!coupling}{#2}%
   \expandafter\newmark\csname\??mk#2\endcsname
   \showmessage\m!systems{13}{#1,[#2]}}

\def\dodefinemarking[#1][#2]%
  {\doifelsenothing{#2}
     {\dododefinemarking[#1][#1]}
     {\dododefinemarking[#1][#2]}}

\def\definemarking
  {\dodoubleempty\dodefinemarking}

\def\definerawmarking[#1]% global ! ! ! !
  {\getgparameters[\??mk#1]
     [\c!expansion=\v!no,  % saves a macro
      \c!separator={ --- }, % watch the spaces
      \c!limittext=,
        \c!state=\v!start]%
   \setxvalue{\??mk#1\c!coupling}{#1}%
   \expandafter\newmark\csname\??mk#1\endcsname
   \showmessage\m!systems{13}{#1}}

\let\nomarking\empty

\def\doifmarkingelse#1%
  {\doifdefinedelse{\??mk#1}}

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

\beginTEX

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

\endTEX

\beginETEX \ifcsname

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

\endETEX

\def\fetchtwomarks[#1]%
  {\doifsomething{\fetchmark[#1][\v!first]}
     {\fetchmark[#1][\v!first]%
      \doifsomething{\fetchmark[#1][\v!last]}
        {\doifnot{\fetchmark[#1][\v!first]}{\fetchmark[#1][\v!last]}
            {\getvalue{\??mk#1\c!separator}\fetchmark[#1][\v!last]}}}}

\def\fetchallmarks[#1]%
  {\doifsomething{\fetchmark[#1][\v!first]}
     {\doifsomething{\fetchmark[#1][\v!previous]}
        {\doifnot{\fetchmark[#1][\v!previous]}{\fetchmark[#1][\v!first]}
           {\fetchmark[#1][\v!previous]\getvalue{\??mk#1\c!separator}}}}%
      \fetchtwomarks[#1]}

\def\dogetmarking[#1][#2]%
  {\doifvalue{\??mk#1\c!state}\v!start
     {\bgroup
     %\def\nomarking##1{\unknown\ }%
      \def\nomarking{\splitsequence{\getvalue{\??mk#1\c!limittext}}}%
      \setfullsectionnumber{\??mk#1}%
      \processaction % slow
        [#2]
        [   \v!both=>{\fetchtwomarks[#1]},
             \v!all=>{\fetchallmarks[#1]},
         \s!default=>{\fetchmark    [#1][\v!first]},
         \s!unknown=>{\fetchmark    [#1][#2]}]%
      \egroup}}

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

\unexpanded\def\getmarking
  {\dodoubleargument\dogetmarking}

\let\setsomemark\setmark

\beginTEX

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

\endTEX

\beginETEX \ifcsname

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

\endETEX

\def\marking
  {\dosingleargument\domarking}

%D Used in placing text lines.

\def\doifelsemarking#1%
  {\ifundefined{\??mk#1\c!coupling}%
     \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
