luat-run.lua /size: 8890 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
luat-run
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to luat-lib.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
next
=
next
10
local
find
=
string
.
find
11
local
insert
,
remove
=
table
.
insert
,
table
.
remove
12
local
osexit
=
os
.
exit
13 14
-- trace_job_status is also controlled by statistics.enable that is set via the directive system.nostatistics
15 16
local
trace_lua_dump
=
false
trackers
.
register
(
"
system.dump
"
,
function
(
v
)
trace_lua_dump
=
v
end
)
17
local
trace_temp_files
=
false
trackers
.
register
(
"
system.tempfiles
"
,
function
(
v
)
trace_temp_files
=
v
end
)
18
local
trace_job_status
=
true
trackers
.
register
(
"
system.jobstatus
"
,
function
(
v
)
trace_job_status
=
v
end
)
19
local
trace_tex_status
=
false
trackers
.
register
(
"
system.texstatus
"
,
function
(
v
)
trace_tex_status
=
v
end
)
20 21
local
report_lua
=
logs
.
reporter
(
"
system
"
,
"
lua
"
)
22
local
report_tex
=
logs
.
reporter
(
"
system
"
,
"
status
"
)
23
local
report_tempfiles
=
logs
.
reporter
(
"
resolvers
"
,
"
tempfiles
"
)
24 25
luatex
=
luatex
or
{
}
26
local
luatex
=
luatex
27
local
synctex
=
luatex
.
synctex
28 29
if
not
synctex
then
30
synctex
=
table
.
setmetatableindex
(
function
(
)
return
function
(
)
end
end
)
31
luatex
.
synctex
=
synctex
32
end
33 34
local
startactions
=
{
}
35
local
stopactions
=
{
}
36
local
dumpactions
=
{
}
37
local
pageactions
=
{
}
38 39
function
luatex
.
registerstartactions
(
...
)
insert
(
startactions
,
...
)
end
40
function
luatex
.
registerstopactions
(
...
)
insert
(
stopactions
,
...
)
end
41
function
luatex
.
registerdumpactions
(
...
)
insert
(
dumpactions
,
...
)
end
42
function
luatex
.
registerpageactions
(
...
)
insert
(
pageactions
,
...
)
end
43 44
local
function
start_run
(
)
45
if
logs
.
start_run
then
46
logs
.
start_run
(
)
47
end
48
for
i
=
1
,
#
startactions
do
49
startactions
[
i
]
(
)
50
end
51
end
52 53
local
function
stop_run
(
)
54
for
i
=
1
,
#
stopactions
do
55
stopactions
[
i
]
(
)
56
end
57
local
quit
=
logs
.
finalactions
(
)
58
if
trace_job_status
then
59
statistics
.
show
(
)
60
end
61
if
trace_tex_status
then
62
logs
.
newline
(
)
63
for
k
,
v
in
table
.
sortedhash
(
status
.
list
(
)
)
do
64
report_tex
(
"
%S=%S
"
,
k
,
v
)
65
end
66
end
67
if
quit
then
68
local
setexitcode
=
lua
.
setexitcode
or
status
.
setexitcode
69
if
setexitcode
then
70
setexitcode
(
1
)
71
if
type
(
quit
)
=
=
"
table
"
then
72
logs
.
newline
(
)
73
report_tex
(
"
quitting due to: %, t
"
,
quit
)
74
logs
.
newline
(
)
75
end
76
end
77
end
78
if
logs
.
stop_run
then
79
logs
.
stop_run
(
)
80
end
81
end
82 83
local
function
start_shipout_page
(
)
84
synctex
.
start
(
)
85
logs
.
start_page_number
(
)
86
end
87 88
local
function
stop_shipout_page
(
)
89
logs
.
stop_page_number
(
)
90
for
i
=
1
,
#
pageactions
do
91
pageactions
[
i
]
(
)
92
end
93
synctex
.
stop
(
)
94
end
95 96
local
function
report_output_pages
(
)
97
end
98 99
local
function
report_output_log
(
)
100
end
101 102
-- local function show_open()
103
-- end
104 105
-- local function show_close()
106
-- end
107 108
local
function
pre_dump_actions
(
)
109
for
i
=
1
,
#
dumpactions
do
110
dumpactions
[
i
]
(
)
111
end
112
lua
.
finalize
(
trace_lua_dump
and
report_lua
or
nil
)
113
-- statistics.savefmtstatus("\jobname","\contextversion","context.tex")
114
end
115 116
local
function
wrapup_synctex
(
)
117
synctex
.
wrapup
(
)
118
end
119 120
-- For Taco ...
121 122
local
sequencers
=
utilities
.
sequencers
123
local
appendgroup
=
sequencers
.
appendgroup
124
local
appendaction
=
sequencers
.
appendaction
125
local
wrapupactions
=
sequencers
.
new
{
}
126
local
cleanupactions
=
sequencers
.
new
{
}
127 128
appendgroup
(
wrapupactions
,
"
system
"
)
129
appendgroup
(
wrapupactions
,
"
user
"
)
130 131
appendgroup
(
cleanupactions
,
"
system
"
)
132
appendgroup
(
cleanupactions
,
"
user
"
)
133 134
local
function
wrapup_run
(
)
135
local
runner
=
wrapupactions
.
runner
136
if
runner
then
137
runner
(
)
138
end
139
end
140 141
local
function
cleanup_run
(
)
142
local
runner
=
cleanupactions
.
runner
143
if
runner
then
144
runner
(
)
145
end
146
end
147 148
function
luatex
.
wrapup
(
action
)
149
appendaction
(
wrapupactions
,
"
user
"
,
action
)
150
end
151 152
function
luatex
.
cleanup
(
action
)
153
appendaction
(
cleanupactions
,
"
user
"
,
action
)
154
end
155 156
function
luatex
.
abort
(
)
157
cleanup_run
(
)
158
osexit
(
1
)
159
end
160 161
appendaction
(
wrapupactions
,
"
system
"
,
synctex
.
wrapup
)
162 163
-- this can be done later
164 165
callbacks
.
register
(
'
start_run
'
,
start_run
,
"
actions performed at the beginning of a run
"
)
166
callbacks
.
register
(
'
stop_run
'
,
stop_run
,
"
actions performed at the end of a run
"
)
167 168
---------.register('show_open', show_open, "actions performed when opening a file")
169
---------.register('show_close', show_close, "actions performed when closing a file")
170 171
callbacks
.
register
(
'
report_output_pages
'
,
report_output_pages
,
"
actions performed when reporting pages
"
)
172
callbacks
.
register
(
'
report_output_log
'
,
report_output_log
,
"
actions performed when reporting log file
"
)
173 174
---------.register('start_page_number', start_shipout_page, "actions performed at the beginning of a shipout")
175
---------.register('stop_page_number', stop_shipout_page, "actions performed at the end of a shipout")
176 177
callbacks
.
register
(
'
start_page_number
'
,
function
(
)
end
,
"
actions performed at the beginning of a shipout
"
)
178
callbacks
.
register
(
'
stop_page_number
'
,
function
(
)
end
,
"
actions performed at the end of a shipout
"
)
179 180
callbacks
.
register
(
'
process_input_buffer
'
,
false
,
"
actions performed when reading data
"
)
181
callbacks
.
register
(
'
process_output_buffer
'
,
false
,
"
actions performed when writing data
"
)
182 183
callbacks
.
register
(
"
pre_dump
"
,
pre_dump_actions
,
"
lua related finalizers called before we dump the format
"
)
-- comes after \everydump
184 185
-- finish_synctex might go away (move to wrapup_run)
186 187
callbacks
.
register
(
"
finish_synctex
"
,
wrapup_synctex
,
"
rename temporary synctex file
"
)
188
callbacks
.
register
(
'
wrapup_run
'
,
wrapup_run
,
"
actions performed after closing files
"
)
189 190
-- temp hack for testing:
191 192
callbacks
.
functions
.
start_page_number
=
start_shipout_page
193
callbacks
.
functions
.
stop_page_number
=
stop_shipout_page
194 195
-- an example:
196 197
local
tempfiles
=
{
}
198 199
function
luatex
.
registertempfile
(
name
,
extrasuffix
,
keep
)
-- namespace might change
200
if
extrasuffix
then
201
name
=
name
.
.
"
.mkiv-tmp
"
-- maybe just .tmp
202
end
203
if
trace_temp_files
and
not
tempfiles
[
name
]
then
204
if
keep
then
205
report_tempfiles
(
"
%s temporary file %a
"
,
"
registering
"
,
name
)
206
else
207
report_tempfiles
(
"
%s temporary file %a
"
,
"
unregistering
"
,
name
)
208
end
209
end
210
tempfiles
[
name
]
=
keep
or
false
211
return
name
212
end
213 214
function
luatex
.
cleanuptempfiles
(
)
215
for
name
,
keep
in
next
,
tempfiles
do
216
if
not
keep
then
217
if
trace_temp_files
then
218
report_tempfiles
(
"
%s temporary file %a
"
,
"
removing
"
,
name
)
219
end
220
os
.
remove
(
name
)
221
end
222
end
223
tempfiles
=
{
}
224
end
225 226
luatex
.
registerstopactions
(
luatex
.
cleanuptempfiles
)
227 228
-- Reporting filenames has been simplified since lmtx because we don't need the
229
-- traditional () {} <> etc methods (read: that directive option was never chosen).
230 231
local
report_open
=
logs
.
reporter
(
"
open source
"
)
232
local
report_close
=
logs
.
reporter
(
"
close source
"
)
233
local
report_load
=
logs
.
reporter
(
"
load resource
"
)
234 235
local
register
=
callbacks
.
register
236 237
local
level
=
0
238
local
total
=
0
239
local
stack
=
{
}
240 241
function
luatex
.
currentfile
(
)
242
return
stack
[
#
stack
]
or
tex
.
jobname
243
end
244 245
local
function
report_start
(
name
,
rest
)
246
if
rest
then
247
-- luatex
248
if
name
~
=
1
then
249
insert
(
stack
,
false
)
250
return
251
end
252
name
=
rest
253
end
254
if
find
(
name
,
"
virtual://
"
,
1
,
true
)
then
255
insert
(
stack
,
false
)
256
else
257
insert
(
stack
,
name
)
258
total
=
total
+
1
259
level
=
level
+
1
260
-- report_open("%i > %i > %s",level,total,name or "?")
261
report_open
(
"
level %i, order %i, name %a
"
,
level
,
total
,
name
or
"
?
"
)
262
synctex
.
setfilename
(
name
)
263
end
264
end
265 266
local
function
report_stop
(
)
267
local
name
=
remove
(
stack
)
268
if
name
then
269
-- report_close("%i > %i > %s",level,total,name or "?")
270
report_close
(
"
level %i, order %i, name %a
"
,
level
,
total
,
name
or
"
?
"
)
271
level
=
level
-
1
272
synctex
.
setfilename
(
stack
[
#
stack
]
or
tex
.
jobname
)
273
end
274
end
275 276
local
function
report_none
(
)
277
end
278 279
register
(
"
start_file
"
,
report_start
)
280
register
(
"
stop_file
"
,
report_stop
)
281 282
directives
.
register
(
"
system.reportfiles
"
,
function
(
v
)
283
if
v
then
284
register
(
"
start_file
"
,
report_start
)
285
register
(
"
stop_file
"
,
report_stop
)
286
else
287
register
(
"
start_file
"
,
report_none
)
288
register
(
"
stop_file
"
,
report_none
)
289
end
290
end
)
291 292
-- start_run doesn't work
293 294
-- luatex.registerstartactions(function()
295
-- if environment.arguments.sandbox then
296
-- sandbox.enable()
297
-- end
298
-- end)
299 300