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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
if not modules then modules = { } end modules ['buff-ini'] = {
version = 1.001,
comment = "companion to buff-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local concat = table.concat
local type, next, load = type, next, load
local sub, format = string.sub, string.format
local splitlines, validstring, replacenewlines = string.splitlines, string.valid, string.replacenewlines
local P, Cs, patterns, lpegmatch = lpeg.P, lpeg.Cs, lpeg.patterns, lpeg.match
local utfchar = utf.char
local nameonly = file.nameonly
local totable = string.totable
local md5hex = md5.hex
local isfile = lfs.isfile
local savedata = io.savedata
local trace_run = false trackers.register("buffers.run", function(v) trace_run = v end)
local trace_grab = false trackers.register("buffers.grab", function(v) trace_grab = v end)
local trace_visualize = false trackers.register("buffers.visualize", function(v) trace_visualize = v end)
local report_buffers = logs.reporter("buffers","usage")
local report_typeset = logs.reporter("buffers","typeset")
----- report_grabbing = logs.reporter("buffers","grabbing")
local context = context
local commands = commands
local implement = interfaces.implement
local scanners = tokens.scanners
local scanstring = scanners.string
local scaninteger = scanners.integer
local scanboolean = scanners.boolean
local scancode = scanners.code
----- scantoken = scanners.token
local getters = tokens.getters
local gettoken = getters.token
local scanners = interfaces.scanners
local variables = interfaces.variables
local settings_to_array = utilities.parsers.settings_to_array
local formatters = string.formatters
local addsuffix = file.addsuffix
local replacesuffix = file.replacesuffix
local registertempfile = luatex.registertempfile
local v_yes = variables.yes
local eol = patterns.eol
local space = patterns.space
local whitespace = patterns.whitespace
local blackspace = whitespace - eol
local whatever = (1-eol)^1 * eol^0
local emptyline = space^0 * eol
local catcodenumbers = catcodes.numbers
local ctxcatcodes = catcodenumbers.ctxcatcodes
local txtcatcodes = catcodenumbers.txtcatcodes
local setdata = job.datasets.setdata
local getdata = job.datasets.getdata
buffers = buffers or { }
local buffers = buffers
local cache = { }
local function erase(name)
cache[name] = nil
end
local function assign(name,str,catcodes)
cache[name] = {
data = str,
catcodes = catcodes,
typeset = false,
}
end
local function combine(name,str,prepend)
local buffer = cache[name]
if buffer then
buffer.data = prepend and (str .. buffer.data) or (buffer.data .. str)
buffer.typeset = false
else
cache[name] = {
data = str,
typeset = false,
}
end
end
local function prepend(name,str)
combine(name,str,true)
end
local function append(name,str)
combine(name,str)
end
local function exists(name)
return cache[name]
end
local function getcontent(name)
local buffer = name and cache[name]
return buffer and buffer.data or ""
end
local function getlines(name)
local buffer = name and cache[name]
return buffer and splitlines(buffer.data)
end
local function getnames(name)
if type(name) == "string" then
return settings_to_array(name)
else
return name
end
end
local function istypeset(name)
local names = getnames(name)
if #names == 0 then
return false
end
for i=1,#names do
local c = cache[names[i]]
if c and not c.typeset then
return false
end
end
return true
end
local function markastypeset(name)
local names = getnames(name)
for i=1,#names do
local c = cache[names[i]]
if c then
c.typeset = true
end
end
end
local function collectcontent(name,separator) -- no print
local names = getnames(name)
local nnames = #names
if nnames == 0 then
return getcontent("") -- default buffer
elseif nnames == 1 then
return getcontent(names[1])
else
local t, n = { }, 0
for i=1,nnames do
local c = getcontent(names[i])
if c ~= "" then
n = n + 1
t[n] = c
end
end
-- the default separator was \r, then \n and is now os.newline because buffers
-- can be loaded in other applications
return concat(t,separator or os.newline)
end
end
local function loadcontent(name) -- no print
local content = collectcontent(name,"\n") -- tex likes \n
local ok, err = load(content)
if ok then
return ok()
else
report_buffers("invalid lua code in buffer %a: %s",name,err or "unknown error")
end
end
buffers.raw = getcontent
buffers.erase = erase
buffers.assign = assign
buffers.prepend = prepend
buffers.append = append
buffers.exists = exists
buffers.getcontent = getcontent
buffers.getlines = getlines
buffers.collectcontent = collectcontent
buffers.loadcontent = loadcontent
-- the context interface
implement {
name = "assignbuffer",
actions = assign,
arguments = { "string", "string", "integer" }
}
implement {
name = "erasebuffer",
actions = erase,
arguments = "string"
}
local anything = patterns.anything
local alwaysmatched = patterns.alwaysmatched
local utf8character = patterns.utf8character
local function countnesting(b,e)
local n
local g = P(b) / function() n = n + 1 end
+ P(e) / function() n = n - 1 end
-- + anything
+ utf8character
local p = alwaysmatched / function() n = 0 end
* g^0
* alwaysmatched / function() return n end
return p
end
local counters = { }
local nesting = 0
local autoundent = true
local continue = false
-- Beware: the first character of bufferdata has to be discarded as it's there to
-- prevent gobbling of newlines in the case of nested buffers. The last one is
-- a newlinechar and is removed too.
--
-- An \n is unlikely to show up as \r is the endlinechar but \n is more generic
-- for us.
-- This fits the way we fetch verbatim: the indentation before the sentinel
-- determines the stripping.
-- str = [[
-- test test test test test test test
-- test test test test test test test
-- test test test test test test test
--
-- test test test test test test test
-- test test test test test test test
-- test test test test test test test
-- ]]
-- local function undent(str)
-- local margin = match(str,"[\n\r]( +)[\n\r]*$") or ""
-- local indent = #margin
-- if indent > 0 then
-- local lines = splitlines(str)
-- local ok = true
-- local pattern = "^" .. margin
-- for i=1,#lines do
-- local l = lines[i]
-- if find(l,pattern) then
-- lines[i] = sub(l,indent+1)
-- else
-- ok = false
-- break
-- end
-- end
-- if ok then
-- return concat(lines,"\n")
-- end
-- end
-- return str
-- end
-- how about tabs
local strippers = { }
local nofspaces = 0
local normalline = space^0 / function(s) local n = #s if n < nofspaces then nofspaces = n end end
* whatever
local getmargin = (emptyline + normalline)^1
local function undent(str) -- new version, needs testing: todo: not always needed, like in xtables
nofspaces = #str
local margin = lpegmatch(getmargin,str)
if nofspaces == #str or nofspaces ==0 then
return str
end
local stripper = strippers[nofspaces]
if not stripper then
stripper = Cs(((space^-nofspaces)/"" * whatever + emptyline)^1)
strippers[nofspaces] = stripper
end
return lpegmatch(stripper,str) or str
end
buffers.undent = undent
-- function commands.grabbuffer(name,begintag,endtag,bufferdata,catcodes,doundent) -- maybe move \\ to call
-- local dn = getcontent(name)
-- if dn == "" then
-- nesting = 0
-- continue = false
-- end
-- if trace_grab then
-- if #bufferdata > 30 then
-- report_grabbing("%s => |%s..%s|",name,sub(bufferdata,1,10),sub(bufferdata,-10,#bufferdata))
-- else
-- report_grabbing("%s => |%s|",name,bufferdata)
-- end
-- end
-- local counter = counters[begintag]
-- if not counter then
-- counter = countnesting(begintag,endtag)
-- counters[begintag] = counter
-- end
-- nesting = nesting + lpegmatch(counter,bufferdata)
-- local more = nesting > 0
-- if more then
-- dn = dn .. sub(bufferdata,2,-1) .. endtag
-- nesting = nesting - 1
-- continue = true
-- else
-- if continue then
-- dn = dn .. sub(bufferdata,2,-2) -- no \r, \n is more generic
-- elseif dn == "" then
-- dn = sub(bufferdata,2,-2)
-- else
-- dn = dn .. "\n" .. sub(bufferdata,2,-2) -- no \r, \n is more generic
-- end
-- local last = sub(dn,-1)
-- if last == "\n" or last == "\r" then -- \n is unlikely as \r is the endlinechar
-- dn = sub(dn,1,-2)
-- end
-- if doundent or (autoundent and doundent == nil) then
-- dn = undent(dn)
-- end
-- end
-- assign(name,dn,catcodes)
-- commands.doifelse(more)
-- end
function tokens.pickup(start,stop)
local stoplist = totable(stop)
local stoplength = #stoplist
local stoplast = stoplist[stoplength]
local startlist = totable(start)
local startlength = #startlist
local startlast = startlist[startlength]
local list = { }
local size = 0
local depth = 0
-- local done = 32
while true do -- or use depth
local char = scancode()
if char then
-- if char < done then
-- -- we skip leading control characters so that we can use them to
-- -- obey spaces (a dirty trick)
-- else
-- done = 0
char = utfchar(char)
size = size + 1
list[size] = char
if char == stoplast and size >= stoplength then
local done = true
local last = size
for i=stoplength,1,-1 do
if stoplist[i] ~= list[last] then
done = false
break
end
last = last - 1
end
if done then
if depth > 0 then
depth = depth - 1
else
break
end
char = false -- trick: let's skip the next (start) test
end
end
if char == startlast and size >= startlength then
local done = true
local last = size
for i=startlength,1,-1 do
if startlist[i] ~= list[last] then
done = false
break
end
last = last - 1
end
if done then
depth = depth + 1
end
end
-- end
else
-- local t = scantoken()
local t = gettoken()
if t then
-- we're skipping leading stuff, like obeyedlines and relaxes
else
break
end
end
end
local start = 1
local stop = size-stoplength-1
-- not good enough: only empty lines, but even then we miss the leading
-- for verbatim
for i=start,stop do
local li = list[i]
if lpegmatch(blackspace,li) then
-- keep going
elseif lpegmatch(eol,li) then
-- okay
start = i + 1
else
break
end
end
for i=stop,start,-1 do
if lpegmatch(whitespace,list[i]) then
stop = i - 1
else
break
end
end
if start <= stop then
return concat(list,"",start,stop)
else
return ""
end
end
-- function buffers.pickup(name,start,stop,finish,catcodes,doundent)
-- local data = tokens.pickup(start,stop)
-- if doundent or (autoundent and doundent == nil) then
-- data = buffers.undent(data)
-- end
-- buffers.assign(name,data,catcodes)
-- context(finish)
-- end
-- commands.pickupbuffer = buffers.pickup
scanners.pickupbuffer = function()
local name = scanstring()
local start = scanstring()
local stop = scanstring()
local finish = scanstring()
local catcodes = scaninteger()
local doundent = scanboolean()
local data = tokens.pickup(start,stop)
if doundent or (autoundent and doundent == nil) then
data = undent(data)
end
buffers.assign(name,data,catcodes)
-- context[finish]()
context(finish)
end
local function savebuffer(list,name,prefix) -- name is optional
if not list or list == "" then
list = name
end
if not name or name == "" then
name = list
end
local content = collectcontent(list,nil) or ""
if content == "" then
content = "empty buffer"
end
if prefix == v_yes then
name = addsuffix(tex.jobname .. "-" .. name,"tmp")
end
io.savedata(name,replacenewlines(content))
end
implement {
name = "savebuffer",
actions = savebuffer,
arguments = { "string", "string", "string" }
}
-- we can consider adding a size to avoid unlikely clashes
local olddata = nil
local newdata = nil
local getrunner = sandbox.getrunner
local runner = sandbox.registerrunner {
name = "run buffer",
program = "context",
method = "execute",
template = "--purgeall " .. (jit and "--jit" or "") .. " %filename%",
reporter = report_typeset,
checkers = {
filename = "readable",
}
}
local function runbuffer(name,encapsulate,runnername,suffixes)
if not runnername or runnername == "" then
runnername = "run buffer"
end
local suffix = "pdf"
if type(suffixes) == "table" then
suffix = suffixes[1]
elseif type(suffixes) == "string" and suffixes ~= "" then
suffix = suffixes
suffixes = { suffix }
else
suffixes = { suffix }
end
local runner = getrunner(runnername)
if not runner then
report_typeset("unknown runner %a",runnername)
return
end
if not olddata then
olddata = getdata("buffers","runners") or { }
local suffixes = olddata.suffixes
local hashes = olddata.hashes
if hashes and suffixes then
for k, hash in next, hashes do
for h, v in next, hash do
for s, v in next, suffixes do
local tmp = addsuffix(h,s)
-- report_typeset("mark for deletion: %s",tmp)
registertempfile(tmp)
end
end
end
end
end
if not newdata then
newdata = {
version = environment.version,
suffixes = { },
hashes = { },
}
setdata {
name = "buffers",
tag = "runners",
data = newdata,
}
end
local oldhashes = olddata.hashes or { }
local newhashes = newdata.hashes or { }
local old = oldhashes[suffix]
local new = newhashes[suffix]
if not old then
old = { }
oldhashes[suffix] = old
for hash, n in next, old do
local tag = formatters["%s-t-b-%s"](tex.jobname,hash)
local tmp = addsuffix(tag,"tmp")
-- report_typeset("mark for deletion: %s",tmp)
registertempfile(tmp) -- to be sure
end
end
if not new then
new = { }
newhashes[suffix] = new
end
local names = getnames(name)
local content = collectcontent(names,nil) or ""
if content == "" then
content = "empty buffer"
end
if encapsulate then
content = formatters["\\starttext\n%s\n\\stoptext\n"](content)
end
--
local hash = md5hex(content)
local tag = formatters["%s-t-b-%s"](nameonly(tex.jobname),hash) -- make sure we run on the local path
--
local filename = addsuffix(tag,"tmp")
local resultname = addsuffix(tag,suffix)
--
if new[tag] then
-- done
elseif not old[tag] or olddata.version ~= newdata.version or not isfile(resultname) then
if trace_run then
report_typeset("changes in %a, processing forced",name)
end
savedata(filename,content)
report_typeset("processing saved buffer %a\n",filename)
runner { filename = filename }
end
new[tag] = (new[tag] or 0) + 1
report_typeset("no changes in %a, processing skipped",name)
registertempfile(filename)
-- report_typeset("mark for persistence: %s",filename)
for i=1,#suffixes do
local suffix = suffixes[i]
newdata.suffixes[suffix] = true
local tmp = addsuffix(tag,suffix)
-- report_typeset("mark for persistance: %s",tmp)
registertempfile(tmp,nil,true)
end
--
return resultname -- first result
end
local function getbuffer(name)
local str = getcontent(name)
if str ~= "" then
-- characters.showstring(str)
context.viafile(str,formatters["buffer.%s"](validstring(name,"noname")))
end
end
local function getbuffermkvi(name) -- rather direct !
context.viafile(resolvers.macros.preprocessed(getcontent(name)),formatters["buffer.%s.mkiv"](validstring(name,"noname")))
end
local function gettexbuffer(name)
local buffer = name and cache[name]
if buffer and buffer.data ~= "" then
context.pushcatcodetable()
if buffer.catcodes == txtcatcodes then
context.setcatcodetable(txtcatcodes)
else
context.setcatcodetable(ctxcatcodes)
end
-- context(function() context.viafile(buffer.data) end)
context.getbuffer { name } -- viafile flushes too soon
context.popcatcodetable()
end
end
buffers.run = runbuffer
implement { name = "getbufferctxlua", actions = loadcontent, arguments = "string" }
implement { name = "getbuffer", actions = getbuffer, arguments = "string" }
implement { name = "getbuffermkvi", actions = getbuffermkvi, arguments = "string" }
implement { name = "gettexbuffer", actions = gettexbuffer, arguments = "string" }
implement {
name = "typesetbuffer",
actions = { runbuffer, context },
arguments = { "string", true }
}
implement {
name = "runbuffer",
actions = { runbuffer, context },
arguments = { "string", false, "string" }
}
implement {
name = "doifelsebuffer",
actions = { exists, commands.doifelse },
arguments = "string"
}
-- This only used for mp buffers and is a kludge. Don't change the
-- texprint into texsprint as it fails because "p<nl>enddef" becomes
-- "penddef" then.
implement {
name = "feedback", -- bad name, maybe rename to injectbuffercontent
actions = { collectcontent, context.printlines },
arguments = "string"
}
do
local context = context
local ctxcore = context.core
local startbuffer = ctxcore.startbuffer
local stopbuffer = ctxcore.stopbuffer
local startcollecting = context.startcollecting
local stopcollecting = context.stopcollecting
function ctxcore.startbuffer(...)
startcollecting()
startbuffer(...)
end
function ctxcore.stopbuffer()
stopbuffer()
stopcollecting()
end
end
|