summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/buff-ini.mkxl
blob: dd2698501b1ed6cabb4c5a2c81256adc3c709076 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
%D \module
%D   [       file=buff-ini,
%D        version=2011.11.22, % previous big effort 2000.01.05,
%D          title=\CONTEXT\ Buffer Macros,
%D       subtitle=Buffers,
%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 Buffer Macros / Buffers}

\registerctxluafile{buff-ini}{autosuffix}

\unprotect

%D There have been several iterations in \MKIV\ but here we only show the currently
%D used one. One can always look back (and maybe learn some). It will never look
%D pretty and never be efficient but it has served us for ages (conceptually it's
%D as in \MKII).

\mutable\lettonothing\currentbuffer

% \doifelsebuffer      {#1} % expandable
% \doifelsebufferempty {#1} % non expandable

\aliased\let\doifbufferelse\doifelsebuffer

\permanent\tolerant\protected\def\resetbuffer[#1]{\clf_erasebuffer{#1}} % todo: use public implementor

\permanent\protected\defcsname\e!start\v!buffer\endcsname
  {\begingroup % (3)
   \obeylines
   \buff_start}

\tolerant\def\buff_start[#1]#*[#2]%
  {\buff_start_indeed{}{#1}{\e!start\v!buffer}{\e!stop\v!buffer}}

\def\buff_start_indeed#1#2#3#4%
  {\edef\p_strip{\namedbufferparameter{#1}\c!strip}% for aditya
   \normalexpanded{\buff_pickup
     {#2}%
     {#3}%
     {#4}%
     {}%
     {\buff_stop{#4}}%
     \ifx\p_strip\v!no\zerocount\else\plusone\fi}}

\permanent\protected\def\grabbufferdatadirect % name start stop
  {\begingroup % (6)
   \buff_start_indeed\empty}

\permanent\protected\def\grabbufferdata % was: \dostartbuffer
  {\begingroup % (4)
   \obeylines
   \buff_grab_direct}

\tolerant\def\buff_grab_direct[#1]#*[#2]#*[#3]#*[#4]% [category] [name] [start] [stop]
  {\ifnum\lastarguments=\plusfour
     \expandafter\buff_start_indeed
   \else
     \expandafter\buff_start_indeed_default
   \fi{#1}{#2}{#3}{#4}}

\def\buff_start_indeed_default#1#2#3#4{\buff_start_indeed\empty{#1}{#2}{#3}}

\let\buff_finish\relax
%let\buff_gobble\relax

\protected\def\buff_pickup#1#2#3#4#5#6% name, startsequence, stopsequence, before, after, undent
  {\begingroup % (1)
   #4%
   \begingroup % (2)
   \scratchcounterone\catcodetable
   \scratchcountertwo#6\relax
   \clf_erasebuffer{#1}%
   \setcatcodetable\vrbcatcodes
   \protected\def\buff_finish
     {\endgroup % (1)
      \endgroup % (2)
      #5}%
   % todo: we need to skip the first lineending which is an active character
   % but sometimes we have something different ... this is a side effect of
   % checking for optional arguments i.e. the next token is already tokenized
   % and for that reason we had the \relax as well as the \string
   \clf_pickupbuffer
     {#1}%
     {#2}%
     {#3}%
   % {\string\dofinishpickupbuffer}%
     \buff_finish
   % \ifnum#6=\plusone\s!true\else\s!false\fi
   % \expandafter\relax\string} % dirty trick
     \scratchcounterone
     % better than \string but still a dirty trick to avoid \par mess in blocks
     \expandafter\scratchcountertwo\detokenized} % the bare token (so not \detokened !)

\protected\def\buff_stop#1%
  {\endgroup % (3 & 4 & 5 & 6)
   \begincsname#1\endcsname}

\permanent\protected\lettonothing\endbuffer

\permanent\tolerant\protected\def\setbuffer[#1]#:#2\endbuffer % seldom used so we just pass #2
  {\clf_assignbuffer{#1}{\detokenize{#2}}\catcodetable\relax}

% Beware, never adapt the global buffer settings, actually we might introduce
% a broken parent chain for this purpose but on the other hand it's not that
% different from framed cum suis.

\installcorenamespace{buffer}

\installcommandhandler \??buffer {buffer} \??buffer

\setupbuffer
  [\c!before=,
   \c!after=,
   \c!define=\v!yes]

\newinteger\c_buff_n_of_defined

\mutable\let\currentdefinedbuffer\s!dummy

\appendtoks
    \global\advanceby\c_buff_n_of_defined\plusone
    \setexpandedbufferparameter\c!number{\number\c_buff_n_of_defined}%
    \cdef\currentdefinedbuffer{def-\number\c_buff_n_of_defined}%
    \ifcstok{\bufferparameter\c!define}\v!yes
      \frozen\instance\protected\edefcsname\e!start\currentbuffer\endcsname
        {\buff_start_defined{\currentbuffer}{\currentdefinedbuffer}{\e!start\currentbuffer}{\e!stop\currentbuffer}}%
      \frozen\instance\protected\edefcsname\e!get  \currentbuffer\endcsname
        {\buff_get_stored   {\currentbuffer}{\currentdefinedbuffer}}%
    \fi
\to \everydefinebuffer

\protected\def\buff_start_defined
  {\begingroup % (5)
   \buff_start_indeed}

\permanent\def\thebuffernumber #1{\namedbufferparameter{#1}\c!number}
\permanent\def\thedefinedbuffer#1{def-\namedbufferparameter{#1}\c!number}

\permanent\def\getbufferdata[#1]% expandable
  {\clf_getbuffer{#1}}

\permanent\tolerant\protected\def\getbuffer[#1]% [namelist]
  {\namedbufferparameter\empty\c!before\relax
   \clf_getbuffer{#1}%
   \namedbufferparameter\empty\c!after\relax}

\protected\def\buff_get_stored#1#2%
  {\namedbufferparameter{#1}\c!before\relax
   \clf_getbuffer{#2}%
   \namedbufferparameter{#1}\c!after\relax}

\aliased\let\rawbuffer\clf_getbuffer % expandable

\permanent\protected\def\getdefinedbuffer[#1]%
  {\buff_get_stored{#1}{\thedefinedbuffer{#1}}}%

% We had this:
%
% \permanent\tolerant\protected\def\inlinebuffer[#1]% [name]
%   {\doifelsenothing{#1}
%      {\buff_get_stored_inline_indeed\empty}
%      {\processcommalist[#1]\buff_get_stored_inline_indeed}}
%
% \protected\def\buff_get_stored_inline_indeed#1%
%   {\ignorespaces\clf_getbuffer{#1}\removeunwantedspaces}
%
% but it makes no sense to ignore spaces in between and we now do the
% list at the \LUA\ end anyway:

\permanent\tolerant\protected\def\inlinebuffer[#1]% [name]
  {\ignorespaces\clf_getbuffer{#1}\removeunwantedspaces}

\definebuffer
  [\v!hiding]

\setupbuffer
  [\v!hiding]
  [\c!before=,
   \c!after=]

%D \starttyping
%D \startbuffer[x]
%D x y z
%D \stopbuffer
%D
%D \savebuffer[x]      [temp]     % gets name: jobname-temp.tmp
%D \savebufferinfile[x][temp.log] % gets name: temp.log
%D \stoptyping

\installcorenamespace{savebuffer}
\installcorenamespace{savebuffercounter}

\installcommandhandler \??savebuffer {savebuffer} \??savebuffer

\setupsavebuffer
  [\c!list=,
   \c!file=,
   \c!directory=,
   \c!prefix=\v!yes]

\permanent\tolerant\protected\def\savebuffer[#1]#*[#2]%
  {\begingroup
   \ifhastok={#1}%
     \setupcurrentsavebuffer[#1]%
   \else
     \setupcurrentsavebuffer[\c!list={#1},\c!file=#2]%
   \fi
   \clf_savebuffer % will become key/value
     {\savebufferparameter\c!list}%
     {\savebufferparameter\c!file}%
     {\savebufferparameter\c!prefix}%
     {\savebufferparameter\c!option}%
     {\savebufferparameter\c!directory}%
   \endgroup}

%D \starttyping
%D \definesavebuffer[slide]
%D
%D \starttext
%D     \startslide
%D         \starttext
%D     \stopslide
%D     \startslide
%D         slide 1
%D     \stopslide
%D     text 1 \par
%D     \startslide
%D         slide 2
%D     \stopslide
%D     text 2 \par
%D     \startslide
%D         \stoptext
%D     \stopslide
%D \stoptext
%D \stoptyping

% We can keep the counter at the lua end and explicitly reset it when we
% save.

\appendtoks
    \ifcsname\e!stop\currentsavebuffer\endcsname\else
      \definebuffer[\currentsavebuffer]%
      \expandafter\newinteger\csname\??savebuffercounter\currentsavebuffer\endcsname
      \protected\edefcsname\e!stop\currentsavebuffer\endcsname{\buff_stop_save_buffer{\currentsavebuffer}}%
      \setsavebufferparameter\c!file{\currentsavebuffer.tex}%
    \fi
\to \everydefinesavebuffer

\protected\def\buff_stop_save_buffer#1%
  {\cdef\currentsavebuffer{#1}%
   \global\advanceby\csname\??savebuffercounter\currentsavebuffer\endcsname\plusone
   \clf_savebuffer % will become key/value
     {\thedefinedbuffer{\currentsavebuffer}}%
     {\savebufferparameter\c!file}%
     {\savebufferparameter\c!prefix}%
     {\ifnum\csname\??savebuffercounter\currentsavebuffer\endcsname>\plusone\v!append\fi}%
     {\savebufferparameter\c!directory}}

%D Experimental: no expansion of commands in buffer!

% \startbuffer[what]
%     context("WHAT")
% \stopbuffer
% \startbuffer
%     context("JOBNAME")
% \stopbuffer
%
% \ctxluabuffer[what] \ctxluabuffer

\permanent\tolerant\protected\def\processTEXbuffer[#1]#;#=% keep case, maybe also lower
  {\pushcatcodetable
   \catcodetable\ctxcatcodes % \setcatcodetable
   \clf_getbuffer{#1#2}%
   \popcatcodetable}

\permanent\tolerant\protected\def\ctxluabuffer[#1]#;#={\clf_getbufferctxlua{#1#2}} % todo: use public implementor
\permanent\tolerant\protected\def\mkvibuffer  [#1]#;#={\clf_getbuffermkvi  {#1#2}} % todo: use public implementor
\permanent\tolerant\protected\def\texbuffer   [#1]#;#={\clf_getbuffertex   {#1#2}} % todo: use public implementor

% maybe still used elsewhere

% \aliased\doprocesstexbuffer\mkvibuffer

\aliased\let\dostartbuffer\grabbufferdata % for old times sake, this will go away

% low level helper (for math manual):

% \showboxinbuffer{temp}<boxnumber><detail> % defined in lua, detail cf \shownodedetails 0|1|2

%D \starttyping
%D \setbox\scratchbox\hbox{$fff$}
%D \showboxinbuffer{temp}\scratchbox\plusone
%D \typebuffer[temp][option=TEX]
%D \stoptyping
%D
%D More extensive multistep cases can do this:
%D
%D \starttyping
%D \pushlogfile{oeps-1.txt}
%D \setbox0\hbox{A}\showbox0
%D \pushlogfile{oeps-2.txt}
%D \setbox0\hbox{B}\showbox0
%D \poplogfile
%D \setbox0\hbox{C}\showbox0
%D \poplogfile
%D
%D % \resetlogfile{oeps-1.txt}
%D \pushlogfile{oeps-1.txt}
%D \setbox0\hbox{A}\showbox0
%D \pushlogfile{oeps-2.txt}
%D \setbox0\hbox{B}\showbox0
%D \poplogfile
%D \setbox0\hbox{C}\showbox0
%D \poplogfile
%D \stoptyping
%D
%D But in the end that was overkill and we don't really need a stepwise verbatim
%D because we need to add comments in between anyway.

\protect \endinput