summaryrefslogtreecommitdiff
path: root/tex/context/base/strc-mar.mkiv
blob: 2fe8631b27b52c3ccc36f9af4f1986d1813e66bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
%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 / Hans Hagen]
%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}{1.001}

\unprotect

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

% if global has side effects we will need to nil it selectively
% and optionally enable it in for instance postponed content

\definesystemattribute [marks] [global]

\let\currentmarking\empty

\def\markingparameter          #1{\csname\domarkingparameter{\??mk\currentmarking}#1\endcsname}
\def\namedmarkingparameter   #1#2{\csname\domarkingparameter{\??mk#1}#2\endcsname}
\def\domarkingparameter      #1#2{\ifcsname#1#2\endcsname#1#2\else\expandafter\domarkingparentparameter\csname#1\s!parent\endcsname#2\fi}
\def\domarkingparentparameter#1#2{\ifx#1\relax\s!empty\else\domarkingparameter#1#2\fi}

% \def\markingparameterhash          #1{\domarkingparameterhash{\??mk\currentmarking}#1}
% \def\domarkingparameterhash      #1#2{\ifcsname#1#2\endcsname#1\else\expandafter\domarkingparentparameterhash\csname#1\s!parent\endcsname#2\fi}
% \def\domarkingparentparameterhash#1#2{\ifx#1\relax\else\domarkingparameterhash#1#2\fi}
%
% \def\detokenizedmarkingparameter#1{\detokenize\expandafter\expandafter\expandafter{\csname\??mk\currentmarking#1\endcsname}}
%
% \def\dosetmarkingattributes#1#2% style color
%   {\edef\fontattributehash {\markingparameterhash#1}%
%    \edef\colorattributehash{\markingparameterhash#2}%
%    \ifx\fontattributehash \empty\else\dosetfontattribute \fontattributehash #1\fi
%    \ifx\colorattributehash\empty\else\dosetcolorattribute\colorattributehash#2\fi}

\newconditional\inhibitgetmarking
\newconditional\inhibitsetmarking
\newtoks       \everymarking

\unexpanded\def\setupmarking
  {\dodoubleargument\dosetupmarking}

\def\dosetupmarking[#1][#2]%
  {\ifsecondargument
     \def\docommand##1{\getparameters[\??mk##1][#2]}%
     \processcommalist[#1]\docommand
   \else
     \getparameters[\??mk][#1]%
   \fi}

% management

\unexpanded\def\definemarking     {\dodoubleempty   \dodefinemarking}
\unexpanded\def\relatemarking     {\dodoubleempty   \dorelatemarking}
\unexpanded\def\setmarking        {\dosingleargument\dosetmarking   } \let\marking\setmarking
\unexpanded\def\resetmarking      {\dosingleargument\doresetmarking }
\unexpanded\def\synchronizemarking{\dodoubleargument\dosynchronizemarking }

\def\dodefinemarking[#1][#2]% marking parent
  {\doifelsenothing{#2}
     {\ctxcommand{definemarking("#1")}%
      \getparameters[\??mk#1][\s!parent=\??mk]}
     {\ctxcommand{definemarking("#1",{ parent = "#2" })}%
      \getparameters[\??mk#1][\s!parent=\??mk#2]}}

\def\dorelatemarking[#1][#2]%
  {\ctxcommand{relatemarking("#1","#2")}}

\def\dosetmarking[#1]#2%
  {\ifconditional\inhibitsetmarking
     % nothing
   \else
     \doifelse{\namedmarkingparameter{#1}\c!expansion}\v!yes
       {\ctxcommand{setmarking("#1",\!!bs#2\!!es)}}
       {\ctxcommand{setmarking("#1",\!!bs\detokenize{#2}\!!es)}}%
   \fi}

\def\doresetmarking[#1]%
  {\ctxcommand{resetmarking("#1")}}

\def\doifelsemarking#1%
  {\ctxcommand{doifelsemarking("#1")}}

\def\dosynchronizemarking[#1][#2]% class boxnumber (some day also name), maybe second argument table
  {\ifvoid#2\else\ctxcommand{synchronizemarking("#1",\number#2)}\fi}

% \appendtoks
%     \dosynchronizemarking[\v!page][\normalpagebox]%
% \to \everybeforepagebody

% defaults

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

% fetching, regular interface

\unexpanded\def\getmarking
  {\ifconditional\inhibitgetmarking
     \expandafter\dotripleargument\expandafter\nogetmarking
   \else
     \expandafter\dotripleargument\expandafter\dogetmarking
   \fi}

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

\def\dogetmarking[#1][#2][#3]%
  {\doif{\namedmarkingparameter{#1}\c!state}\v!start
     {\begingroup
      \setsystemmode\v!marking
      \the\everymarking
      \ifthirdargument
        \ctxcommand{getmarking("#1","#2","#3")}%
      \else
        \ctxcommand{getmarking("#1","\v!page","#2")}%
      \fi
      \endgroup}}

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

\def\fetchonemark[#1]#2[#3]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchonemark ("#1","\v!page","#2")}\fi}
\def\fetchtwomarks     [#1]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchtwomarks("#1","\v!page")}\fi}
\def\fetchallmarks     [#1]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchallmarks("#1","\v!page")}\fi}

\let\fetchmark\fetchonemark

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

\def\fetchonemarking[#1]#2[#3]#4[#5]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchonemark ("#1","#3","#5")}\fi}
\def\fetchtwomarkings     [#1]#2[#3]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchtwomarks("#1","#3")}\fi}
\def\fetchallmarkings     [#1]#2[#3]{\ifconditional\inhibitgetmarking\else\ctxcommand{fetchallmarks("#1","#3")}\fi}

\let\fetchmarking\fetchonemarking

\def\markingseparator#1{\namedmarkingparameter{#1}\c!separator}
\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