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
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
|
-- filename : core-buf.lua
-- comment : companion to core-buf.tex
-- author : Hans Hagen, PRAGMA-ADE, Hasselt NL
-- copyright: PRAGMA ADE / ConTeXt Development Team
-- license : see context related readme files
-- ctx lua reference model / hooks and such
-- to be optimized
if not versions then versions = { } end versions['core-buf'] = 1.001
if unicode and not utf then utf = unicode.utf8 end
buffers = { }
buffers.data = { }
buffers.hooks = { }
buffers.flags = { }
buffers.commands = { }
function buffers.erase(name)
buffers.data[name] = nil
end
function buffers.set(name, str)
buffers.data[name] = { str } -- CHECK THIS
end
function buffers.append(name, str)
buffers.data[name] = (buffers.data[name] or "") .. str
end
buffers.flags.store_as_table = true
-- to be sorted out: crlf + \ ; slow now
function buffers.grab(name,begintag,endtag,data)
if not buffers.data[name] or buffers.data[name] == "" then
buffers.data[name] = ""
buffers.level = 0
end
buffers.level = buffers.level + data:count("\\"..begintag) - data:count("\\"..endtag)
local more = buffers.level>0
if more then
buffers.data[name] = buffers.data[name] .. data .. endtag
buffers.level = buffers.level - 1
else
if buffers.data[name] == "" then
buffers.data[name] = data:sub(1,#data-1)
else
buffers.data[name] = buffers.data[name] .. "\n" .. data:sub(1,#data-1)
end
buffers.data[name] = buffers.data[name]:gsub("[\010\013]$","")
if buffers.flags.store_as_table then
-- todo: specific splitter, do we really want to erase the spaces?
buffers.data[name] = string.split(buffers.data[name]," *[\010\013]")
end
end
cs.testcase(more)
end
function buffers.doifelsebuffer(name)
cs.testcase(buffers.data[name] ~= nil)
end
buffers.flags.optimize_verbatim = true
buffers.flags.count_empty_lines = false
buffers.commands.no_break = "\\doverbatimnobreak"
buffers.commands.do_break = "\\doverbatimgoodbreak"
buffers.commands.begin_of_line_command = "\\doverbatimbeginofline"
buffers.commands.end_of_line_command = "\\doverbatimendofline"
buffers.commands.empty_line_command = "\\doverbatimemptyline"
function buffers.verbatimbreak(n,m)
if buffers.flags.optimize_verbatim then
if (n==2) or (n==m) then
tex.sprint(buffers.commands.no_break)
else
tex.sprint(buffers.commands.do_break)
end
end
end
function buffers.type(name)
if buffers.data[name] then
if type(buffers.data[name]) == "string" then
-- todo: use linestepper (no need for a table)
local lines = string.split(buffers.data[name]," *[\010\013]")
local line, n, m = 0, 0, #lines
for _,str in ipairs(lines) do
n, line = buffers.typeline(str, n, m, line)
end
else
local line, n, m = 0, 0, #buffers.data[name]
for _,str in ipairs(buffers.data[name]) do
n, line = buffers.typeline(str, n, m, line)
end
end
end
end
function buffers.typefile(name)
local t = input.openfile(name)
if t then
local line, n, m = 0, 0, t.noflines
while true do
str = t.reader(t)
if str then
n, line = buffers.typeline(str, n, m, line)
else
break
end
end
t.close()
end
end
function buffers.typeline(str,n,m,line)
n = n + 1
buffers.verbatimbreak(n,m)
if str:find("%S") then
line = line + 1
buffers.hooks.begin_of_line(line)
buffers.hooks.flush_line(buffers.hooks.line(str))
buffers.hooks.end_of_line()
else
if buffers.flags.count_empty_lines then
line = line + 1
end
buffers.hooks.empty_line(line)
end
return n, line
end
function buffers.get(name)
if buffers.data[name] then
if type(buffers.data[name]) == "table" then
for _,v in ipairs(buffers.data[name]) do
tex.print(v)
end
else
string.piecewise(buffers.data[name], " *[\010\013]", tex.print)
end
end
end
function buffers.inspect(name)
if buffers.data[name] then
if type(buffers.data[name]) == "table" then
for _,v in ipairs(buffers.data[name]) do
if v == "" then
tex.sprint(tex.ctxcatcodes,"[crlf]\\par ")
else
tex.sprint(tex.ctxcatcodes,(string.gsub("(.)",function(c)
return " [" .. string.byte(c) .. "] "
end)) .. "\\par")
end
end
else
tex.sprint(tex.ctxcatcodes,(string.gsub(buffers.data[name],"(.)",function(c)
return " [" .. string.byte(c) .. "] "
end)))
end
end
end
-- maybe just line(n,str) empty(n,str)
buffers.visualizers = { }
buffers.visualizers.default = { }
buffers.visualizers.tex = { }
buffers.visualizers.mp = { }
buffers.visualizers.escapetoken = nil
buffers.visualizers.tablength = 7
buffers.visualizers.enabletab = false
buffers.visualizers.enableescape = false
function buffers.visualizers.reset()
buffers.visualizers.enabletab = false
buffers.visualizers.enableescape = false
buffers.currentvisualizer = 'default'
end
buffers.currentvisualizer = 'default'
function buffers.setvisualizer(str)
buffers.currentvisualizer = string.lower(str)
if not buffers.visualizers[buffers.currentvisualizer] then
buffers.currentvisualizer = 'default'
end
end
function buffers.doifelsevisualizer(str)
cs.testcase((str ~= "") and (buffers.visualizers[string.lower(str)] ~= nil))
end
-- calling routines, don't change
function buffers.hooks.flush_line(str,nesting)
if buffers.visualizers[buffers.currentvisualizer].flush_line then
buffers.visualizers[buffers.currentvisualizer].flush_line(str,nesting)
--~ elseif nesting then
--~ buffers.visualizers.flush_nested(str,false) -- no real nesting
else
buffers.visualizers.default.flush_line(str,nesting)
end
end
function buffers.hooks.begin_of_line(n)
if buffers.visualizers[buffers.currentvisualizer].begin_of_line then
buffers.visualizers[buffers.currentvisualizer].begin_of_line(n)
else
buffers.visualizers.default.begin_of_line(n)
end
end
function buffers.hooks.end_of_line()
if buffers.visualizers[buffers.currentvisualizer].end_of_line then
buffers.visualizers[buffers.currentvisualizer].end_of_line()
else
buffers.visualizers.default.end_of_line(str)
end
end
function buffers.hooks.empty_line()
if buffers.visualizers[buffers.currentvisualizer].empty_line then
buffers.visualizers[buffers.currentvisualizer].empty_line()
else
buffers.visualizers.default.empty_line()
end
end
function buffers.hooks.line(str)
if buffers.visualizers[buffers.currentvisualizer].line then
return buffers.visualizers[buffers.currentvisualizer].line(str)
else
return buffers.visualizers.default.line(str)
end
end
-- defaults
function buffers.visualizers.default.flush_line(str)
tex.sprint(tex.ctxcatcodes,buffers.escaped(str))
end
function buffers.visualizers.default.begin_of_line(n)
tex.sprint(tex.ctxcatcodes, buffers.commands.begin_of_line_command .. "{" .. n .. "}")
end
function buffers.visualizers.default.end_of_line()
tex.sprint(tex.ctxcatcodes,buffers.commands.end_of_line_command)
end
function buffers.visualizers.default.empty_line()
tex.sprint(tex.ctxcatcodes,buffers.commands.empty_line_command)
end
function buffers.visualizers.default.line(str)
return str
end
-- special one
buffers.commands.nested = "\\switchslantedtype "
-- todo : utf + faster
function buffers.visualizers.flush_nested(str, enable) -- no utf, kind of obsolete mess
local result, c, nested, i = "", "", 0, 1
local sb, ss, sf = string.byte, string.sub, string.find
while i < #str do -- slow
c = ss(str,i,i+1)
if c == "<<" then
nested = nested + 1
if enable then
result = result .. "{" .. buffers.commands.nested
else
result = result .. "{"
end
i = i + 2
elseif c == ">>" then
if nested > 0 then
nested = nested - 1
result = result .. "}"
end
i = i + 2
else
c = ss(str,i,i)
if c == " " then
result = result .. "\\obs "
elseif sf(c,"%a") then
result = result .. c
else
result = result .. "\\char" .. sb(c) .. " "
end
i = i + 1
end
end
result = result .. "\\char" .. sb(ss(str,i,i)) .. " " .. string.rep("}",nested)
tex.sprint(tex.ctxcatcodes,result)
end
-- handy helpers
--
-- \sop[color] switch_of_pretty
-- \bop[color] begin_of_pretty
-- \eop end_of_pretty
-- \obs obeyedspace
-- \char <n> special characters
buffers.currentcolors = { }
function buffers.change_state(n, state, result)
if n then
if state ~= n then
if state > 0 then
result[#result+1] = "\\sop[" .. buffers.currentcolors[n] .. "]"
else
result[#result+1] = "\\bop[" .. buffers.currentcolors[n] .. "]"
end
return n
end
elseif state > 0 then
result[#result+1] = "\\eop "
return 0
end
return state
end
function buffers.finish_state(state, result)
if state > 0 then
result[#result+1] = "\\eop "
return 0
else
return state
end
end
buffers.open_nested = string.rep("\\char"..string.byte('<').." ",2)
buffers.close_nested = string.rep("\\char"..string.byte('>').." ",2)
function buffers.replace_nested(result)
return (string.gsub(string.gsub(result,buffers.open_nested,"{"),buffers.close_nested,"}"))
end
function buffers.flush_result(result,nested)
if nested then
tex.sprint(tex.ctxcatcodes,buffers.replace_nested(table.concat(result,"")))
else
tex.sprint(tex.ctxcatcodes,table.concat(result,""))
end
end
function buffers.escaped(str)
local sb, sf = utf.byte, utf.find
return (utf.gsub(str,"(.)", function(c)
if sf(c,"^(%a%d)$") then
return c
elseif c == " " then
return "\\obs "
else
return "\\char" .. sb(c) .. " "
end
end))
end
function buffers.escaped_chr(ch)
local b = utf.byte(ch)
if b == 32 then
return "\\obs "
else
return "\\char" .. b .. " "
end
end
|