summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-mar.mkiv
blob: 3685b66a7024bce4843999442e308155d1d608b0 (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
%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}{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]

\installcorenamespace{marking}

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

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

\newtoks       \everymarking

\unexpanded\def\relatemarking     {\dodoubleempty   \strc_markings_relate     }
\unexpanded\def\setmarking        {\dosingleargument\strc_markings_set        } \let\marking\setmarking
\unexpanded\def\resetmarking      {\dosingleargument\strc_markings_reset      }
\unexpanded\def\synchronizemarking{\dotripleargument\strc_markings_synchronize}

\appendtoks
    \ctxcommand{definemarking("\currentmarking",{ parent = "\currentmarkingparent" })}%
\to \everydefinemarking

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

\def\strc_markings_set[#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\strc_markings_reset[#1]%
  {\ctxcommand{resetmarking("#1")}}

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

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

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

\def\doifelsemarking#1%
  {\ctxcommand{doifelsemarking(\!!bs#1\!!es)}}

% \appendtoks
%     \strc_markings_synchronize[\v!page][\normalpagebox][\v!keep]% keep if no marks
% \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\strc_markings_get_nop
   \else
     \expandafter\dotripleargument\expandafter\strc_markings_get_yes
   \fi}

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

\def\strc_markings_get_yes[#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