%D \module
%D   [       file=strc-mar,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Markings,
%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 Structure Macros / Markings}

\registerctxluafile{strc-mar-old}{autosuffix}

\unprotect

%D Synchronizing marks is a rather tricky and messy business. When setting a mark, a
%D node is added to the list in order for to \TEX\ be able to figure out the 3
%D current marks when a page is made (last mark on previous page, first on current
%D page, last on current page; in \LUATEX\ we might at one point have the first on
%D the next page as well).
%D
%D Resetting a mark is not easy. An empty one will not erase the last one on the
%D previous page for instance. In \LUATEX\ we can clear a marks state register with
%D \type {\clearmarks} but since this is an immediate operation it might have
%D unwanted side effects when \TEX\ has collected several pages of text and
%D finishing off these pages uses marks.
%D
%D In \MKIV\ we provide an alternative model that permits some more control over the
%D way marks are used. It is not entirely compatible with \MKII\ or previous \MKIV\
%D implementations but in practice this is not a real problem. It's also easier now
%D to extend this mechanism.

% first last previous next top bottom [:nocheck]
% current
% default page all keep

\definesystemattribute [marks] [global]

\installcorenamespace{marking}

\installcommandhandler \??marking {marking} \??marking

\newconditional\inhibitgetmarking % will become private
\newconditional\inhibitsetmarking % will become private

\newtoks       \everymarking

\appendtoks
    \clf_definemarking{\currentmarking}{\currentmarkingparent}%
\to \everydefinemarking

\permanent\protected\tolerant\def\relatemarking           [#1]#*[#2]{\clf_relatemarking{#1}{#2}}
\permanent\protected\tolerant\def\resetmarking                  [#1]{\clf_resetmarking{#1}}
\permanent\protected\tolerant\def\synchronizemarking[#1]#*[#2]#*[#3]{\ifvoid#2\else\clf_synchronizemarking{#1}#2{#3}\fi}
\permanent                   \def\doifelsemarking                 #1{\clf_doifelsemarking{#1}}  % no \noexpanded

\aliased\let\clearmarking\resetmarking % different in the new situation

\def\strc_markings_synchronize#1#2#3{\ifvoid#2\else\clf_synchronizemarking{#1}#2{#3}\fi} % called in page-ini

\permanent\protected\tolerant\def\setmarking[#1]#:#2%
  {\ifconditional\inhibitsetmarking\else
     \doifelse{\namedmarkingparameter{#1}\c!expansion}\v!yes
       {\clf_setmarking{#1}{#2}}%
       {\clf_setmarking{#1}{\detokenize{#2}}}%
   \fi}

\aliased\let\marking        \setmarking
\aliased\let\doifmarkingelse\doifelsemarking

% defaults

\setupmarking
  [\c!expansion=\v!no,
   \c!separator=\space\emdash\space,
   \c!filtercommand=\firstofoneargument,
   \c!state=\v!start]

% fetching, regular interface

\permanent\protected\def\getmarking
  {\ifconditional\inhibitgetmarking
     \expandafter\strc_markings_get_nop
   \else
     \expandafter\strc_markings_get_yes
   \fi}

\tolerant\def\strc_markings_get_nop[#-]#*[#-]#*[#-]%
  {}

\tolerant\def\strc_markings_get_yes[#1]#*[#2]#*[#3]%
  {\ifcstok{\namedmarkingparameter{#1}\c!state}\v!start
     \begingroup
     \setsystemmode\v!marking
     \the\everymarking
     \ifparameter#3\or
       \clf_getmarking{#1}{#2}{#3}%
     \else
       \clf_getmarking{#1}{\v!page}{#2}%
     \fi
     \endgroup
  \fi}

% the fetchers are fully expandable: [name][method]

\permanent\tolerant\def\fetchonemark[#1]#*[#2]{\ifconditional\inhibitgetmarking\else\clf_fetchonemark {#1}{\v!page}{#2}\fi}
\permanent\tolerant\def\fetchtwomarks     [#1]{\ifconditional\inhibitgetmarking\else\clf_fetchtwomarks{#1}{\v!page}\fi}
\permanent\tolerant\def\fetchallmarks     [#1]{\ifconditional\inhibitgetmarking\else\clf_fetchallmarks{#1}{\v!page}\fi}

\aliased\let\fetchmark\fetchonemark

% also fully expandable but here we have: [name][range][method]

\permanent\tolerant\def\fetchonemarking[#1]#*[#2]#*[#3]{\ifconditional\inhibitgetmarking\else\clf_fetchonemark {#1}{#2}{#3}\fi}
\permanent\tolerant\def\fetchtwomarkings     [#1]#*[#2]{\ifconditional\inhibitgetmarking\else\clf_fetchtwomarks{#1}{#2}\fi}
\permanent\tolerant\def\fetchallmarkings     [#1]#*[#2]{\ifconditional\inhibitgetmarking\else\clf_fetchallmarks{#1}{#2}\fi}

\aliased\let\fetchmarking\fetchonemarking

\permanent\def\markingseparator#1{\namedmarkingparameter{#1}\c!separator}
\permanent\def\markingcommand  #1{\namedmarkingparameter{#1}\c!filtercommand}

%D Experimental:
%D
%D \starttyping
%D \definemarking[boxmark]
%D
%D \setbox0\ruledvbox{
%D     \marking[boxmark]{tufte} \input tufte \par
%D     \marking[boxmark]{ward}  \input ward  \par
%D }
%D
%D \synchronizemarking[zerobox][0] \box0
%D
%D marks: (\getmarking[boxmark][zerobox][first],\getmarking[boxmark][zerobox][last])
%D \stoptyping

\protect \endinput

