1
2
3
4
5
6
7
8
9
10
11
12
13
14\writestatus{loading}{ConTeXt Typesetting Macros Local Boxes}
15
16
17
18
19
20
21
22\registerctxluafile{typolbx}{autosuffix}
23
24
25
26
27
28
29
30
31\unprotect
32
33
34
35
36
37
38
39
40
41\aliased\let\typo_localboxes_reset\resetlocalboxes
42
43\appendtoks
44 \typo_localboxes_reset
45\to \everyforgetall
46
47\definesystemattribute[localboxesmark][public]
48
49
50
51\newbox\localboxcontentbox
52
53\installcorenamespace{localboxes}
54\installcorenamespace{localboxesattribute}
55\installcorenamespace{localboxesnamespace}
56\installcorenamespace{localboxeslocations}
57\installcorenamespace{localboxesresetters}
58
59\installcommandhandler \??localboxes {localboxes} \??localboxes
60
61\setuplocalboxes
62 [\c!command=\localboxcontent,
63 \c!width=\zeropoint,
64 \c!location=\v!left,
65 \c!distance=\zeropoint]
66
67\newinteger\c_typo_localboxes
68\newinteger\c_typo_localboxes_index
69
70\appendtoks
71 \global\advanceby\c_typo_localboxes\plusone
72 \global\expandafter\integerdef\csname\??localboxesattribute\currentlocalboxes\endcsname\c_typo_localboxes
73 \gletcsname\??localboxesnamespace\the\c_typo_localboxes\endcsname\currentlocalboxes
74\to \everydefinelocalboxes
75
76
77
78
79
80
81
82\def\typo_paragraphs_l#1{\localleftbox \ifcstok{#1}\v!global par\fi}
83\def\typo_paragraphs_r#1{\localrightbox \ifcstok{#1}\v!global par\fi}
84\def\typo_paragraphs_m#1{\localmiddlebox\ifcstok{#1}\v!global par\fi}
85
86\permanent\tolerant\protected\def\leftparbox [#1]{\typo_paragraphs_l{#1}\bgroup\enforced\let\leftparbox \relax\let\next}
87\permanent\tolerant\protected\def\rightparbox [#1]{\typo_paragraphs_r{#1}\bgroup\enforced\let\rightparbox\relax\let\next}
88\permanent\tolerant\protected\def\middleparbox[#1]{\typo_paragraphs_m{#1}\bgroup\enforced\let\rightparbox\relax\let\next}
89
90
91
92\permanent\protected\def\localboxcontent
93 {\box\localboxcontentbox}
94
95\permanent\protected\def\localboxcommand
96 {\ifcsname\??localboxesnamespace\number\localboxindex\endcsname
97
98 \expandafter\let\expandafter\currentlocalboxes\lastnamedcs
99 \setbox\localboxcontentbox\hbox
100 {\uselocalboxesstyleandcolor\c!style\c!color
101 \localboxesparameter\c!command}
102 \fi}
103
104
105
106
107
108\mutable\lettonothing\currentlocalboxeslocation
109
110\def\typo_localboxes_localbox
111 {\ifx\currentlocalboxeslocation\v!right
112 \localrightbox
113 \orelse\ifx\currentlocalboxeslocation\v!left
114 \localleftbox
115 \else
116 \localmiddlebox
117 \fi}
118
119\def\typo_localboxes_zero#1#2#3
120 {\ifcsname\??localboxesattribute#2\endcsname
121 \c_typo_localboxes_index\lastnamedcs
122 \cdef\currentlocalboxes{#2}
123 \edef\currentlocalboxeslocation{#1}
124 \typo_localboxes_localbox
125 \s!index \c_typo_localboxes_index
126 \bgroup
127 \hpack
128 \ifcstok{\localboxesparameter\c!repeat}\v!no
129 \s!attr \localboxesmarkattribute \localboxmarkonce\c_typo_localboxes_index\relax
130 \fi
131 \s!to \zeropoint
132 \bgroup
133 \uselocalboxesstyleandcolor\c!style\c!color
134 \hbox{#3}
135 \hss
136 \egroup
137 \egroup
138 \fi}
139
140\def\typo_localboxes_asis#1#2#3
141 {\ifcsname\??localboxesattribute#2\endcsname
142 \c_typo_localboxes_index\lastnamedcs
143 \cdef\currentlocalboxes{#2}
144 \edef\currentlocalboxeslocation{#1}
145 \typo_localboxes_localbox
146 \s!index \c_typo_localboxes_index
147 \bgroup
148 \hpack
149 \ifcstok{\localboxesparameter\c!repeat}\v!no
150 \s!attr \localboxesmarkattribute \localboxmarkonce\c_typo_localboxes_index\relax
151 \fi
152
153 \bgroup
154 \uselocalboxesstyleandcolor\c!style\c!color
155 \hbox{#3}
156 \egroup
157 \egroup
158 \fi}
159
160\defcsname\??localboxeslocations\v!left \endcsname{\typo_localboxes_zero\v!left }
161\defcsname\??localboxeslocations\v!right \endcsname{\typo_localboxes_zero\v!right }
162\defcsname\??localboxeslocations\v!lefttext \endcsname{\typo_localboxes_asis\v!left }
163\defcsname\??localboxeslocations\v!righttext\endcsname{\typo_localboxes_asis\v!right }
164\defcsname\??localboxeslocations\v!middle \endcsname{\typo_localboxes_asis\v!middle}
165
166\letcsname\??localboxesresetters\v!left \endcsname\v!left
167\letcsname\??localboxesresetters\v!right \endcsname\v!right
168\letcsname\??localboxesresetters\v!lefttext \endcsname\v!left
169\letcsname\??localboxesresetters\v!righttext\endcsname\v!right
170\letcsname\??localboxesresetters\v!middle \endcsname\v!middle
171
172\permanent\tolerant\protected\def\resetlocalbox[#1]
173 {\ifcsname\??localboxesattribute#1\endcsname
174 \c_typo_localboxes_index\lastnamedcs
175 \cdef\currentlocalboxes{#1}
176 \ifcsname\??localboxesresetters\localboxesparameter\c!location\endcsname
177 \edef\currentlocalboxeslocation{\lastnamedcs}
178 \typo_localboxes_localbox \s!index \c_typo_localboxes_index {}
179 \fi
180 \fi}
181
182\def\typo_localboxes_box#1
183 {\dowithnextboxcontent
184 {\cdef\currentlocalboxes{#1}
185 \uselocalboxesstyleandcolor\c!style\c!color}
186 {\ifcsname\??localboxeslocations\namedlocalboxesparameter{#1}\c!location\endcsname
187 \expandafter\lastnamedcs
188 \else
189 \csname\??localboxeslocations\v!left\expandafter\endcsname
190 \fi{#1}{\unhbox\nextbox}}}
191
192
193\permanent\tolerant\protected\def\localbox[#1]
194 {\typo_localboxes_box{#1}\hbox}
195
196\permanent\tolerant\protected\def\startlocalbox[#1]
197 {\dowithnextbox
198 {\ifcsname\??localboxeslocations\namedlocalboxesparameter{#1}\c!location\endcsname
199 \expandafter\lastnamedcs
200 \else
201 \csname\??localboxeslocations\v!left\expandafter\endcsname
202 \fi{#1}{\unhbox\nextbox}}
203 \hbox\bgroup
204 \cdef\currentlocalboxes{#1}
205 \uselocalboxesstyleandcolor\c!style\c!color
206 \enforced\def\stoplocalbox{\removeunwantedspaces\egroup}
207 \ignorespaces}
208
209\aliased\let\stoplocalbox\donothing
210
211\permanent\tolerant\protected\def\startlocalboxrange[#1]
212 {\globalpushmacro\stoplocalboxrange
213 \ifcsname\??localboxeslocations\namedlocalboxesparameter{#1}\c!location\endcsname
214 \lastnamedcs{#1}{}
215 \fi}
216
217\permanent\protected\def\stoplocalboxrange
218 {\globalpopmacro\stoplocalboxrange}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246\permanent\protected\def\localmarginlefttext#1
247 {\ifzeropt\localboxesparameter\c!width\relax
248
249 \setbox\localboxcontentbox\hpack
250 {\unhbox\localboxcontentbox
251 \setbox\localboxcontentbox\lastbox
252 \unhbox\localboxcontentbox}
253 \fi
254 \hpack \s!xoffset {
255 #1
256 \localboxprogress
257 \wd\localboxcontentbox
258 (\localboxesparameter\c!distance)
259 }{\box\localboxcontentbox}}
260
261\permanent\protected\def\localmarginrighttext#1
262 {\hpack \s!xoffset {
263 #1
264 \localboxlinewidth
265 \localboxprogress
266 {\localboxesparameter\c!distance}
267 }{\box\localboxcontentbox}}
268
269
270
271\permanent\protected\def\localmargintext[#1]#2
272 {\dontleavehmode
273 \ifcsname\??localboxesattribute#1\endcsname
274 \c_typo_localboxes_index\lastnamedcs
275 \cdef\currentlocalboxes{#1}
276 \edef\currentlocalboxeslocation{\localboxesparameter\c!location}
277 \ifx\currentlocalboxeslocation\v!right\localrightbox\orelse\ifx\currentlocalboxeslocation\v!left\localleftbox\else\localmiddlebox\fi
278 \s!index \c_typo_localboxes_index
279 \bgroup
280 \hpack
281 \ifcstok{\localboxesparameter\c!repeat}\v!no
282 \s!attr \localboxesmarkattribute \localboxmarkonce\c_typo_localboxes_index\relax
283 \fi
284 to \zeropoint
285 \bgroup
286 \uselocalboxesstyleandcolor\c!style\c!color
287 \hbox{#2}
288 \hss
289 \egroup
290 \egroup
291 \fi}
292
293\definelocalboxes
294 [\v!leftmargin]
295 [\c!command=\localmarginlefttext\zeropoint,
296 \c!repeat=\v!no,
297 \c!distance=\leftmargindistance,
298
299 \c!location=\v!middle]
300
301\definelocalboxes
302 [\v!rightmargin]
303 [\c!command=\localmarginrighttext\zeropoint,
304 \c!repeat=\v!no,
305 \c!distance=\rightmargindistance,
306
307 \c!location=\v!middle]
308
309\definelocalboxes
310 [\v!leftedge]
311 [\c!command=\localmarginlefttext\leftmargintotal,
312 \c!repeat=\v!no,
313 \c!distance=\leftedgedistance,
314
315 \c!location=\v!middle]
316
317\definelocalboxes
318 [\v!rightedge]
319 [\c!command=\localmarginrighttext\rightmargintotal,
320 \c!repeat=\v!no,
321 \c!distance=\rightedgedistance,
322
323 \c!location=\v!middle]
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384\protect \endinput
385
386 |