buff-ini.mkiv /size: 8419 b    last modification: 2020-07-01 14:35
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
%
26
{
\clf_doifelsebuffer
{
#
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 250
\installsetuponlycommandhandler
\??savebuffer
{
savebuffer
}
251 252
\setupsavebuffer
253
[
\c!list
=
,
254
\c!file
=
,
255
\c!prefix
=
\v!yes
]
256 257
\unexpanded
\def
\savebuffer
258
{
\dodoubleempty
\buff_save
}
259 260
\def
\buff_save
[#
1
][#
2
]
%
261
{
\begingroup
262
\doifelseassignment
{
#
1
}
263
{
\setupcurrentsavebuffer
[#
1
]
}
%
264
{
\setupcurrentsavebuffer
[
\c!list
=
{
#
1
}
,
\c!file
=#
2
]
}
%
265
\clf_savebuffer
{
\directsavebufferparameter
\c!list
}{
\directsavebufferparameter
\c!file
}{
\directsavebufferparameter
\c!prefix
}
%
266
\endgroup
}
267 268
%D Experimental: no expansion of commands in buffer!
269 270
% \startbuffer[what]
271
% context("WHAT")
272
% \stopbuffer
273
% \startbuffer
274
% context("JOBNAME")
275
% \stopbuffer
276
%
277
% \ctxluabuffer[what] \ctxluabuffer
278 279
\unexpanded
\def
\ctxluabuffer
{
\dosingleempty
\buff_ctxlua
}
280
\unexpanded
\def
\mkvibuffer
{
\dosingleempty
\buff_mkvi
}
281
% what was: \mkvibufferraw
282 283
\def
\buff_ctxlua
[#
1
]
{
\clf_getbufferctxlua
{
#
1
}}
284
\def
\buff_mkvi
[#
1
]
{
\clf_getbuffermkvi
{
#
1
}}
285 286
% used elsewhere
287 288
\unexpanded
\def
\doprocesstexbuffer
#
1
{
\buff_process_tex
[#
1
]
}
% still used?
289 290
\let
\dostartbuffer
\grabbufferdata
% for old times sake
291 292
% new (expandable):
293 294
\def
\getbufferdata
[#
1
]
{
\buff_get_stored_indeed
{
#
1
}}
295 296
\protect
\endinput
297