buff-ini.mkiv /size: 9858 b    last modification: 2021-10-28 13:50
1
%D \module
2
%D [ file=buff-ini,
3
%D version=2011.11.22, % previous big effort 2000.01.05,
4
%D title=\CONTEXT\ Buffer Macros,
5
%D subtitle=Buffers,
6
%D author=Hans Hagen,
7
%D date=\currentdate,
8
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
%C
10
%C This module is part of the \CONTEXT\ macro||package and is
11
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12
%C details.
13 14
\writestatus
{
loading
}
{
ConTeXt
Buffer
Macros
/
Buffers
}
15 16
\registerctxluafile
{
buff
-
ini
}
{
}
17 18
\unprotect
19 20
% number is messy and not needed as we store the number anyway
21
% we can get rid of \c!number
22 23
\let
\currentbuffer
\empty
24 25
\def
\doifelsebuffer
#
1
{
\clf_doifelsebuffer
{
#
1
}
}
26
\unexpanded
\def
\doifelsebufferempty
#
1
{
\clf_doifelsebufferempty
{
#
1
}
}
27 28
\let
\doifbufferelse
\doifelsebuffer
29 30
\unexpanded
\def
\resetbuffer
31
{
\dosingleempty
\buff_reset
}
32 33
\def
\buff_reset
[
#
1
]
%
34
{
\clf_erasebuffer
{
#
1
}
}
35 36
\setuvalue
{
\e!start
\v!buffer
}
%
37
{
\begingroup
% (3)
38
\obeylines
39
\dodoubleempty
\buff_start
}
40 41
\def
\buff_start
[
#
1
]
[
#
2
]
%
42
{
\buff_start_indeed
{
}
{
#
1
}
{
\e!start
\v!buffer
}
{
\e!stop
\v!buffer
}
}
43 44
% \def\buff_start_indeed#1#2#3#4#5% \donothing needed ! #5=undent)
45
% {\normalexpanded{\buff_pickup{#2}{#3}{#4}{}{\buff_stop{#4}}\plusone}}
46 47
% \def\buff_start_indeed#1#2#3#4%
48
% {\normalexpanded{\buff_pickup{#2}{#3}{#4}{}{\buff_stop{#4}}\plusone}}
49 50
\def
\buff_start_indeed
#
1
#
2
#
3
#
4
%
51
{
\edef
\p_strip
{
\namedbufferparameter
{
#
1
}
\c!strip
}
% for aditya
52
\normalexpanded
{
\buff_pickup
53
{
#
2
}
%
54
{
#
3
}
%
55
{
#
4
}
%
56
{
}
%
57
{
\buff_stop
{
#
4
}
}
%
58
\ifx
\p_strip
\v!no
\zerocount
\else
\plusone
\fi
}
}
59 60
\unexpanded
\def
\grabbufferdata
% was: \dostartbuffer
61
{
\begingroup
% (4)
62
\obeylines
63
\doquadrupleempty
\buff_grab_direct_indeed
}
64 65
\unexpanded
\def
\grabbufferdatadirect
% name start stop
66
{
\begingroup
% (6)
67
\buff_start_indeed
\empty
}
68 69
\def
\buff_grab_direct_indeed
% [category] [name] [start] [stop]
70
{
\iffourthargument
71
\expandafter
\buff_grab_direct_indeed_a
72
\else
73
\expandafter
\buff_grab_direct_indeed_b
74
\fi
}
75 76
\def
\buff_grab_direct_indeed_a
[
#
1
]
[
#
2
]
[
#
3
]
[
#
4
]
{
\buff_start_indeed
{
#
1
}
{
#
2
}
{
#
3
}
{
#
4
}
}
77
\def
\buff_grab_direct_indeed_b
[
#
1
]
[
#
2
]
[
#
3
]
[
#
4
]
{
\buff_start_indeed
\empty
{
#
1
}
{
#
2
}
{
#
3
}
}
78 79
\let
\buff_finish
\relax
80
\let
\buff_gobble
\relax
81 82
% \unexpanded\def\buff_pickup#1#2#3#4#5#6% name, startsequence, stopsequence, before, after, undent
83
% {\begingroup % (1)
84
% #4%
85
% \begingroup % (2)
86
% \edef\catcodetableofbuffer{\number\catcodetable}%
87
% \clf_erasebuffer{#1}%
88
% \setcatcodetable\vrbcatcodes
89
% \def\buff_finish
90
% {\endgroup % (1)
91
% \endgroup % (2)
92
% #5}%
93
% \def\buff_gobble##1#3% is detokenize needed? TEST
94
% %{\ctxcommand{grabbuffer("#1","#2","#3",\!!bs\detokenize{##1}\!!es)} % space ?
95
% {\ctxcommand{grabbuffer("#1","#2","#3",\!!bs>##1\!!es,\catcodetableofbuffer,\ifnum#6=\plusone true\else false\fi)}% space ?
96
% \buff_gobble
97
% \buff_finish}%
98
% \buff_gobble}
99 100
% % \def\startgrab
101
% % {\begingroup
102
% % \setcatcodetable\vrbcatcodes
103
% % \ctxlua{tokens.pickup("\\startgrab","\\stopgrab") context("\\endgroup")}}
104
% %
105
% % \def\stopgrab
106
% % {}
107 108
\unexpanded
\def
\buff_pickup
#
1
#
2
#
3
#
4
#
5
#
6
% name, startsequence, stopsequence, before, after, undent
109
{
\begingroup
% (1)
110
#
4
%
111
\begingroup
% (2)
112
\scratchcounter
\catcodetable
113
\clf_erasebuffer
{
#
1
}
%
114
\setcatcodetable
\vrbcatcodes
115
\unexpanded
\def
\dofinishpickupbuffer
116
{
\endgroup
% (1)
117
\endgroup
% (2)
118
#
5
}
%
119
% todo: we need to skip the first lineending which is an active character
120
% but sometimes we have something different ... this is a side effect of
121
% checking for optional arguments i.e. the next token is already tokenized
122
% and for that reason we have the \relax as well as the \string
123
\clf_pickupbuffer
124
{
#
1
}
%
125
{
#
2
}
%
126
{
#
3
}
%
127
{
\string
\dofinishpickupbuffer
}
%
128
\scratchcounter
129
\ifnum
#
6
=
\plusone
\s!true
\else
\s!false
\fi
130
% \relax}
131
\expandafter
\relax
\string
}
% maybe \normalexpanded{\relax\utfchar{7}}} signal
132 133
\unexpanded
\def
\buff_stop
#
1
%
134
{
\endgroup
% (3 & 4 & 5 & 6)
135
\begincsname
#
1
\endcsname
}
136 137
% \installctxfunction\dopickupbuffer{commands.dopickupbuffer}
138 139
\unexpanded
\def
\setbuffer
140
{
\dosingleempty
\buff_set
}
141 142
\let
\endbuffer
\relax
143 144
\def
\buff_set
[
#
1
]
#
2
\endbuffer
% seldom used so we just pass #2
145
{
\clf_assignbuffer
{
#
1
}
{
\detokenize
{
#
2
}
}
\catcodetable
\relax
}
146 147
% beware, never adapt the global buffer settings, actually we might introduce
148
% a broken parent chain for this purpose but on the other hand it's not that
149
% different from framed cum suis
150 151
\installcorenamespace
{
buffer
}
152 153
\installcommandhandler
\??buffer
{
buffer
}
\??buffer
154 155
\setupbuffer
156
[
\c!before
=
,
157
\c!after
=
]
158 159
\newcount
\c_buff_n_of_defined
160 161
\let
\currentdefinedbuffer
\s!dummy
162 163
\appendtoks
164
\global
\advance
\c_buff_n_of_defined
\plusone
165
\setexpandedbufferparameter
\c!number
{
\number
\c_buff_n_of_defined
}
%
166
\edef
\currentdefinedbuffer
{
def
-
\number
\c_buff_n_of_defined
}
%
167
\setuevalue
{
\e!start
\currentbuffer
}
{
\buff_start_defined
{
\currentbuffer
}
{
\currentdefinedbuffer
}
{
\e!start
\currentbuffer
}
{
\e!stop
\currentbuffer
}
}
%
168
\setuevalue
{
\e!get
\currentbuffer
}
{
\buff_get_stored
{
\currentbuffer
}
{
\currentdefinedbuffer
}
}
%
169
\to
\everydefinebuffer
170 171
\unexpanded
\def
\buff_start_defined
172
{
\begingroup
% (5)
173
\buff_start_indeed
}
174 175
\def
\thebuffernumber
#
1
{
\namedbufferparameter
{
#
1
}
\c!number
}
176
\def
\thedefinedbuffer
#
1
{
def
-
\namedbufferparameter
{
#
1
}
\c!number
}
177 178
\unexpanded
\def
\getbuffer
% no [settings yet]
179
{
\dosingleempty
\buff_get
}
180 181
\unexpanded
\def
\buff_get
[
#
1
]
% [name]
182
{
\namedbufferparameter
\empty
\c!before
\relax
183
\doifelsenothing
{
#
1
}
184
{
\buff_get_stored_indeed
\empty
}
185
{
\processcommalist
[
#
1
]
\buff_get_stored_indeed
}
%
186
\namedbufferparameter
\empty
\c!after
\relax
}
187 188
% \installcommalistprocessorcommand \buff_get_stored_list \empty \buff_get_stored_indeed
189
%
190
% \unexpanded\def\buff_get[#1]% [name]
191
% {\namedbufferparameter\empty\c!before\relax
192
% \doifelsenothing{#1}
193
% {\buff_get_stored_indeed\empty}
194
% {\buff_get_stored_list[#1]}%
195
% \namedbufferparameter\empty\c!after\relax}
196 197
\unexpanded
\def
\buff_get_stored
#
1
#
2
%
198
{
\namedbufferparameter
{
#
1
}
\c!before
\relax
199
\buff_get_stored_indeed
{
#
2
}
%
200
\namedbufferparameter
{
#
1
}
\c!after
\relax
}
201 202
\unexpanded
\def
\buff_get_stored_indeed
#
1
%
203
{
\clf_getbuffer
{
#
1
}
}
204 205
\unexpanded
\def
\getdefinedbuffer
[
#
1
]
%
206
{
\buff_get_stored
{
#
1
}
{
\thedefinedbuffer
{
#
1
}
}
}
%
207 208
\unexpanded
\def
\inlinebuffer
209
{
\dosingleempty
\buff_get_inline
}
210 211
\unexpanded
\def
\buff_get_inline
[
#
1
]
% [name]
212
{
\doifelsenothing
{
#
1
}
213
{
\buff_get_stored_inline_indeed
\empty
}
214
{
\processcommalist
[
#
1
]
\buff_get_stored_inline_indeed
}
}
215 216
\unexpanded
\def
\buff_get_stored_inline_indeed
#
1
%
217
{
\ignorespaces
\clf_getbuffer
{
#
1
}
\removeunwantedspaces
}
218 219
\def
\rawbuffer
#
1
% expandable
220
{
\clf_getbuffer
{
#
1
}
}
221 222
\definebuffer
223
[
\v!hiding
]
224 225
\setupbuffer
226
[
\v!hiding
]
227
[
\c!before
=
,
228
\c!after
=
]
229 230
\unexpanded
\def
\processTEXbuffer
% keep case, maybe also lower
231
{
\dosingleempty
\buff_process_tex
}
232 233
\def
\buff_process_tex
[
#
1
]
%
234
{
\pushcatcodetable
235
\catcodetable
\ctxcatcodes
% \setcatcodetable
236
\buff_get_stored_indeed
{
#
1
}
%
237
\popcatcodetable
}
238 239
% only mkiv:
240
%
241
% \startbuffer[x]
242
% x
243
% \stopbuffer
244
%
245
% \savebuffer[x] [temp] % gets name: jobname-temp.tmp
246
% \savebufferinfile[x][temp.log] % gets name: temp.log
247 248
\installcorenamespace
{
savebuffer
}
249
\installcorenamespace
{
savebuffercounter
}
250 251
\installcommandhandler
\??savebuffer
{
savebuffer
}
\??savebuffer
252 253
\setupsavebuffer
254
[
\c!list
=
,
255
\c!file
=
,
256
\c!directory
=
,
257
\c!prefix
=
\v!yes
]
258 259
\unexpanded
\def
\savebuffer
260
{
\dodoubleempty
\buff_save
}
261 262
\def
\buff_save
[
#
1
]
[
#
2
]
%
263
{
\begingroup
264
\doifelseassignment
{
#
1
}
265
{
\setupcurrentsavebuffer
[
#
1
]
}
%
266
{
\setupcurrentsavebuffer
[
\c!list
=
{
#
1
}
,
\c!file
=
#
2
]
}
%
267
\clf_savebuffer
% will become key/value
268
{
\savebufferparameter
\c!list
}
%
269
{
\savebufferparameter
\c!file
}
%
270
{
\savebufferparameter
\c!prefix
}
%
271
{
\savebufferparameter
\c!option
}
%
272
{
\savebufferparameter
\c!directory
}
%
273
\endgroup
}
274 275
%D \starttyping
276
%D \definesavebuffer[slide]
277
%D
278
%D \starttext
279
%D \startslide
280
%D \starttext
281
%D \stopslide
282
%D \startslide
283
%D slide 1
284
%D \stopslide
285
%D text 1 \par
286
%D \startslide
287
%D slide 2
288
%D \stopslide
289
%D text 2 \par
290
%D \startslide
291
%D \stoptext
292
%D \stopslide
293
%D \stoptext
294
%D \stoptyping
295 296
\appendtoks
297
\ifcsname
\e!stop
\currentsavebuffer
\endcsname
\else
298
\definebuffer
[
\currentsavebuffer
]
%
299
\expandafter
\newcount
\csname
\??savebuffercounter
\currentsavebuffer
\endcsname
300
\setuevalue
{
\e!stop
\currentsavebuffer
}
{
\buff_stop_save_buffer
{
\currentsavebuffer
}
}
%
301
\setsavebufferparameter
\c!file
{
\currentsavebuffer
.
tex
}
%
302
\fi
303
\to
\everydefinesavebuffer
304 305
\protected
\def
\buff_stop_save_buffer
#
1
%
306
{
\edef
\currentsavebuffer
{
#
1
}
%
307
\global
\advance
\csname
\??savebuffercounter
\currentsavebuffer
\endcsname
\plusone
308
\clf_savebuffer
% will become key/value
309
{
\thedefinedbuffer
{
\currentsavebuffer
}
}
%
310
{
\savebufferparameter
\c!file
}
%
311
{
\savebufferparameter
\c!prefix
}
%
312
{
\ifnum
\csname
\??savebuffercounter
\currentsavebuffer
\endcsname
>
\plusone
\v!append
\fi
}
%
313
{
\savebufferparameter
\c!directory
}
}
314 315
%D Experimental: no expansion of commands in buffer!
316 317
% \startbuffer[what]
318
% context("WHAT")
319
% \stopbuffer
320
% \startbuffer
321
% context("JOBNAME")
322
% \stopbuffer
323
%
324
% \ctxluabuffer[what] \ctxluabuffer
325 326
\unexpanded
\def
\ctxluabuffer
{
\dosingleempty
\buff_ctxlua
}
327
\unexpanded
\def
\mkvibuffer
{
\dosingleempty
\buff_mkvi
}
328
% what was: \mkvibufferraw
329 330
\def
\buff_ctxlua
[
#
1
]
{
\clf_getbufferctxlua
{
#
1
}
}
331
\def
\buff_mkvi
[
#
1
]
{
\clf_getbuffermkvi
{
#
1
}
}
332 333
% used elsewhere
334 335
\unexpanded
\def
\doprocesstexbuffer
#
1
{
\buff_process_tex
[
#
1
]
}
% still used?
336 337
\let
\dostartbuffer
\grabbufferdata
% for old times sake
338 339
% new (expandable):
340 341
\def
\getbufferdata
[
#
1
]
{
\buff_get_stored_indeed
{
#
1
}
}
342 343
\protect
\endinput
344