1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16\writestatus{loading}{ConTeXt Structure Macros Line Notes}
17
18
19
20\unprotect
21
22\installcorenamespace{linenote}
23
24\aliased\let\setuplinenote\setupnote
25
26\newinteger\c_strc_linenotes
27\newtoks \everydefinelinenote
28
29\permanent\tolerant\protected\def\definelinenote[#1]#*[#S#2]#*[#S#3]
30 {\ifarguments
31
32 \orelse\ifcsname\??linenote#1\endcsname
33
34 \ifparameters\or\or
35 \setupnote[#1][#2]
36 \or
37 \setupnote[#1][#3]
38 \fi
39 \else
40 \ifparameters\or
41 \definenote[#1]
42 \or
43 \definenote[#1][#2]
44 \or
45 \definenote[#1][#2][#3]
46 \fi
47 \pushmacro\currentnote
48 \cdef\currentnote{#1}
49 \letcsname\??linenote\currentnote\expandafter\endcsname\csname\currentnote\endcsname
50 \frozen\instance\protected\edefcsname \currentnote\endcsname{\strc_linenotes_direct{\currentnote}}
51 \frozen\instance\protected\edefcsname\e!start\currentnote\endcsname{\strc_linenotes_start {\currentnote}}
52 \frozen\instance\protected\edefcsname\e!stop \currentnote\endcsname{\strc_linenotes_stop }
53 \expand\everydefinelinenote
54 \popmacro\currentnote
55 \fi}
56
57\protected\def\strc_linenotes_direct#1#2
58 {\global\advanceby\c_strc_linenotes\plusone
59 \strc_linenotes_indeed{#1}{\the\c_strc_linenotes}{#2}
60 \strc_linenotes_traced\empty
61 \normalexpanded{\someline[\the\c_strc_linenotes]}}
62
63\protected\def\strc_linenotes_start#1[#2]#3
64 {\global\advanceby\c_strc_linenotes\plusone
65 \keepunwantedspaces
66 \strc_linenotes_indeed{#1}{#2}{#3}
67 \strc_linenotes_traced{#2}
68 \startline[#2]}
69
70\protected\def\strc_linenotes_stop[#1]
71 {\stopline[#1]}
72
73\let\m_page_lines_previous_to \relax
74\let\m_page_lines_previous_from\relax
75
76\let\m_page_lines_current_to \relax
77\let\m_page_lines_current_from \relax
78
79\newconditional\c_page_lines_current_to
80\newconditional\c_page_lines_current_from
81
82\installcorenamespace{linenotespreviousfrom}
83\installcorenamespace{linenotespreviousto}
84
85\letvalue\??linenotespreviousfrom\empty
86\letvalue\??linenotespreviousto \empty
87
88
89
90\mutable\lettonothing\currentlinenotereference
91
92\def\page_lines_in_from{\in[lr:b:\currentlinenotereference]}
93\def\page_lines_in_to {\in[lr:e:\currentlinenotereference]}
94
95\protected\def\strc_linenotes_range_normal#1
96 {\doifelsereferencefound{lr:b:\currentlinenotereference}
97 {\c_page_lines_current_from\conditionaltrue}
98 {\c_page_lines_current_from\conditionalfalse}
99 \ifconditional\c_page_lines_current_from
100 \xdef\m_page_lines_current_from{\currentreferencelinenumber}
101 \doifelsereferencefound{lr:e:\currentlinenotereference}
102 {\c_page_lines_current_to\conditionaltrue}
103 {\c_page_lines_current_to\conditionalfalse}
104 \ifconditional\c_page_lines_current_to
105 \xdef\m_page_lines_current_to{\currentreferencelinenumber}
106 \page_lines_in_from
107 \ifx\m_page_lines_current_from\m_page_lines_current_to \else
108 \endash
109 \page_lines_in_to
110 \fi
111 \else
112 \page_lines_in_from
113 \fi
114 \else
115 \page_lines_in_from
116 \fi}
117
118\protected\def\strc_linenotes_range_sparse#1
119 {\doifelsereferencefound{lr:b:\currentlinenotereference}
120 {\c_page_lines_current_from\conditionaltrue}
121 {\c_page_lines_current_from\conditionalfalse}
122 \ifconditional\c_page_lines_current_from
123 \xdef\m_page_lines_current_from{\currentreferencelinenumber}
124 \doifelsereferencefound{lr:e:\currentlinenotereference}
125 {\c_page_lines_current_to\conditionaltrue}
126 {\c_page_lines_current_to\conditionalfalse}
127 \ifconditional\c_page_lines_current_to
128 \xdef\m_page_lines_current_to{\currentreferencelinenumber}
129 \ifx\m_page_lines_previous_from\m_page_lines_current_from
130 \ifx\m_page_lines_previous_to\m_page_lines_current_to \else
131 \page_lines_in_from
132 \ifx\m_page_lines_current_from\m_page_lines_current_to\else\endash\page_lines_in_to\fi
133 \fi
134 \else
135 \page_lines_in_from
136 \ifx\m_page_lines_current_from\m_page_lines_current_to\else\endash\page_lines_in_to\fi
137 \fi
138 \else
139 \page_lines_in_from
140 \fi
141 \else
142 \ifx\m_page_lines_previous_from\m_page_lines_current_from \else
143 \page_lines_in_from
144 \fi
145 \fi}
146
147\lettonothing\currentlinenotereference
148
149\mutable\let\linenotelinenumber\relax
150
151\protected\def\strc_linenotes_indeed#1#2#3
152 {\begingroup
153
154 \cdef\currentnotation{#1}
155 \edef\currentlinenotereference{#2}
156 \xdef\m_page_lines_previous_from{\begincsname\??linenotespreviousfrom\currentnotation\endcsname}
157 \xdef\m_page_lines_previous_to {\begincsname\??linenotespreviousto \currentnotation\endcsname}
158 \strc_linenotes_check_compression
159 \let\currentnote\currentnotation
160 \letnotationparameter\c!numbercommand\linenotelinenumber
161 \letnoteparameter \c!textcommand \gobbleoneargument
162 \csname\??linenote\currentnotation\endcsname{#3}
163 \gletcsname\??linenotespreviousfrom\currentnotation\endcsname\m_page_lines_current_from
164 \gletcsname\??linenotespreviousto \currentnotation\endcsname\m_page_lines_current_to
165 \endgroup}
166
167
168
169\installcorenamespace{linenotescompressmethod}
170
171
172
173
174\defcsname\??linenotescompressmethod\v!separator\endcsname
175 {\edef\p_compressseparator{\noteparameter\c!compressseparator}
176 \scratchskip\noteparameter\c!compressdistance\relax
177 \ifempty\p_compressseparator
178 \hskip\scratchskip
179 \else
180 \hskip.5\scratchskip
181 \begingroup\p_compressseparator\endgroup
182 \hskip.5\scratchskip
183 \fi}
184
185\defcsname\??linenotescompressmethod\v!stopper\endcsname
186 {\edef\p_compressstopper{\noteparameter\c!compressstopper}
187 \scratchskip\noteparameter\c!compressdistance\relax
188 \ifempty\p_compressstopper
189 \hskip\scratchskip
190 \else
191 \begingroup\p_compressstopper\endgroup
192 \hskip.5\scratchskip
193 \fi}
194
195\defcsname\??linenotescompressmethod\v!space\endcsname
196 {\hskip\noteparameter\c!compressdistance\relax}
197
198\def\strc_linenotes_check_compression
199 {\edef\p_linenotes_compressmethod{\noteparameter\c!compressmethod}
200 \ifcstok{\noteparameter\c!compress}\v!yes
201 \let\linenotelinenumber\strc_linenotes_range_sparse
202 \else
203 \let\linenotelinenumber\strc_linenotes_range_normal
204 \fi
205 \ifcsname\??linenotescompressmethod\p_linenotes_compressmethod\endcsname \else
206 \let\p_linenotes_compressmethod\v!space
207 \fi}
208
209\def\strc_linenotes_inbetween
210 {\begingroup
211 \strc_linenotes_check_compression
212 \begincsname\??linenotescompressmethod\p_linenotes_compressmethod\endcsname
213 \endgroup}
214
215\def\strc_notes_compress_distance{\emwidth \s!plus .5\emwidth \s!minus .25\emwidth}
216
217\setupnotes
218 [
219 \c!compressdistance=\strc_notes_compress_distance,
220 \c!compressseparator=\symbol{\v!compressseparator},
221 \c!compressstopper=\symbol{\v!compressstopper}]
222
223\appendtoks
224 \letnoteparameter\c!inbetween\strc_linenotes_inbetween
225\to \everydefinelinenote
226
227
228
229\permanent\protected\def\doresetlinenotecompression#1
230 {\gletcsname\??linenotespreviousfrom#1\endcsname\empty
231 \gletcsname\??linenotespreviousto #1\endcsname\empty}
232
233\definesymbol
234 [\v!compressseparator]
235 [\hbox{\vl\thinspace\vl}]
236
237\definesymbol
238 [\v!compressstopper]
239 [,]
240
241
242
243
244
245\let\strc_linenotes_traced\gobbleoneargument
246
247\def\strc_linenotes_traced_indeed#1
248 {\hpack to \zeropoint
249 {\forgetall
250 \hsize\zeropoint
251 \hss
252 \vpack to \strutheight{\llap{\red\infofont\setstrut\the\c_strc_linenotes}\vss}
253 {\color[blue]{\vl}}
254 \vpack to \strutheight{\rlap{\red\infofont\setstrut#1}\vss}
255 \hss}
256 \prewordbreak}
257
258\permanent\protected\def\tracelinenotes
259 {\let\strc_linenotes_traced\strc_linenotes_traced_indeed}
260
261
262
263\definelinenote[\v!linenote]
264
265
266
267\aliased\let\fromlinenote\startlinenote
268\aliased\let\tolinenote \stoplinenote
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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\protect \endinput
372 |