summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/luat-run.lua
blob: 98f98177705ad721d53e5fa5f98e35910fcc7294 (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
if not modules then modules = { } end modules ['luat-run'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local next = next
local format, find = string.format, string.find
local insert, remove = table.insert, table.remove

-- trace_job_status is also controlled by statistics.enable that is set via the directive system.nostatistics

local trace_lua_dump   = false  trackers.register("system.dump",      function(v) trace_lua_dump   = v end)
local trace_temp_files = false  trackers.register("system.tempfiles", function(v) trace_temp_files = v end)
local trace_job_status = true   trackers.register("system.jobstatus", function(v) trace_job_status = v end)
local trace_tex_status = false  trackers.register("system.texstatus", function(v) trace_tex_status = v end)

local report_lua       = logs.reporter("system","lua")
local report_tex       = logs.reporter("system","status")
local report_tempfiles = logs.reporter("resolvers","tempfiles")

luatex        = luatex or { }
local luatex  = luatex
local synctex = luatex.synctex

if not synctex then
    synctex        = table.setmetatableindex(function() return function() end end)
    luatex.synctex = synctex
end

local startactions = { }
local stopactions  = { }
local dumpactions  = { }
local pageactions  = { }

function luatex.registerstartactions(...) insert(startactions, ...) end
function luatex.registerstopactions (...) insert(stopactions,  ...) end
function luatex.registerdumpactions (...) insert(dumpactions,  ...) end
function luatex.registerpageactions (...) insert(pageactions,  ...) end

local function start_run()
    if logs.start_run then
        logs.start_run()
    end
    for i=1,#startactions do
        startactions[i]()
    end
end

local function stop_run()
    for i=1,#stopactions do
        stopactions[i]()
    end
    local quit = logs.finalactions()
    if trace_job_status then
        statistics.show()
    end
    if trace_tex_status then
        logs.newline()
        for k, v in table.sortedhash(status.list()) do
            report_tex("%S=%S",k,v)
        end
    end
    if quit then
        if status.setexitcode then
            status.setexitcode(1)
            if type(quit) == "table" then
                logs.newline()
                report_tex("quitting due to: %, t",quit)
                logs.newline()
            end
        end
    end
    if logs.stop_run then
        logs.stop_run()
    end
end

local function start_shipout_page()
    synctex.start()
    logs.start_page_number()
end

local function stop_shipout_page()
    logs.stop_page_number()
    for i=1,#pageactions do
        pageactions[i]()
    end
    synctex.stop()
end

local function report_output_pages()
end

local function report_output_log()
end

-- local function show_open()
-- end

-- local function show_close()
-- end

local function pre_dump_actions()
    for i=1,#dumpactions do
        dumpactions[i]()
    end
    lua.finalize(trace_lua_dump and report_lua or nil)
 -- statistics.savefmtstatus("\jobname","\contextversion","context.tex")
end

local function wrapup_synctex()
    synctex.wrapup()
end

-- For Taco ...

local sequencers    = utilities.sequencers
local appendgroup   = sequencers.appendgroup
local appendaction  = sequencers.appendaction
local wrapupactions = sequencers.new { }

appendgroup(wrapupactions,"system")
appendgroup(wrapupactions,"user")

local function wrapup_run()
    local runner = wrapupactions.runner
    if runner then
        runner()
    end
end

function luatex.wrapup(action)
    appendaction(wrapupactions,"user",action)
end

appendaction(wrapupactions,"system",synctex.wrapup)

-- this can be done later

callbacks.register('start_run',               start_run,           "actions performed at the beginning of a run")
callbacks.register('stop_run',                stop_run,            "actions performed at the end of a run")

---------.register('show_open',               show_open,           "actions performed when opening a file")
---------.register('show_close',              show_close,          "actions performed when closing a file")

callbacks.register('report_output_pages',     report_output_pages, "actions performed when reporting pages")
callbacks.register('report_output_log',       report_output_log,   "actions performed when reporting log file")

---------.register('start_page_number',       start_shipout_page,  "actions performed at the beginning of a shipout")
---------.register('stop_page_number',        stop_shipout_page,   "actions performed at the end of a shipout")

callbacks.register('start_page_number',       function() end,      "actions performed at the beginning of a shipout")
callbacks.register('stop_page_number',        function() end,      "actions performed at the end of a shipout")

callbacks.register('process_input_buffer',    false,               "actions performed when reading data")
callbacks.register('process_output_buffer',   false,               "actions performed when writing data")

callbacks.register("pre_dump",                pre_dump_actions,    "lua related finalizers called before we dump the format") -- comes after \everydump

-- finish_synctex might go away (move to wrapup_run)

callbacks.register("finish_synctex",          wrapup_synctex,      "rename temporary synctex file")
callbacks.register('wrapup_run',              wrapup_run,          "actions performed after closing files")

-- temp hack for testing:

callbacks.functions.start_page_number = start_shipout_page
callbacks.functions.stop_page_number  = stop_shipout_page

-- an example:

local tempfiles = { }

function luatex.registertempfile(name,extrasuffix,keep) -- namespace might change
    if extrasuffix then
        name = name .. ".mkiv-tmp" -- maybe just .tmp
    end
    if trace_temp_files and not tempfiles[name] then
        if keep then
            report_tempfiles("%s temporary file %a","registering",name)
        else
            report_tempfiles("%s temporary file %a","unregistering",name)
        end
    end
    tempfiles[name] = keep or false
    return name
end

function luatex.cleanuptempfiles()
    for name, keep in next, tempfiles do
        if not keep then
            if trace_temp_files then
                report_tempfiles("%s temporary file %a","removing",name)
            end
            os.remove(name)
        end
    end
    tempfiles = { }
end

luatex.registerstopactions(luatex.cleanuptempfiles)

-- filenames

local report_open  = logs.reporter("open source")
local report_close = logs.reporter("close source")
local report_load  = logs.reporter("load resource")

local register     = callbacks.register

local level = 0
local total = 0
local stack = { }
local all   = false

function luatex.currentfile()
    return stack[#stack] or tex.jobname
end

local function report_start(name)
    if find(name,"virtual://",1,true) then
        insert(stack,false)
    else
        insert(stack,name)
        total = total + 1
        level = level + 1
     -- report_open("%i > %i > %s",level,total,name or "?")
        report_open("level %i, order %i, name %a",level,total,name or "?")
        synctex.setfilename(name)
    end
end

local function report_stop()
    local name = remove(stack)
    if name then
     -- report_close("%i > %i > %s",level,total,name or "?")
        report_close("level %i, order %i, name %a",level,total,name or "?")
        level = level - 1
        synctex.setfilename(stack[#stack] or tex.jobname)
    end
end

if CONTEXTLMTXMODE < 2 then

    local types = {
        "data",
        "font map",
        "image",
        "font subset",
        "full font",
    }

    local do_report_start = report_start
    local do_report_stop  = report_stop

    report_start = function(left,name)
        if not left then
            -- skip
        elseif left ~= 1 then
            if all then
                report_load("type %a, name %a",types[left],name or "?")
            end
        else
            do_report_start(name)
        end
    end

    report_stop = function(right)
        if level == 1 or not right or right == 1 then
            do_report_stop()
        end
    end

end

local function report_none()
end

register("start_file",report_start)
register("stop_file", report_stop)

directives.register("system.reportfiles", function(v)
    if v == "noresources" then
        all = false
        register("start_file",report_start)
        register("stop_file", report_stop)
    elseif toboolean(v) or v == "all" then
        all = true
        register("start_file",report_start)
        register("stop_file", report_stop)
    elseif v == "traditional" then
        register("start_file",nil)
        register("stop_file", nil)
    else
        register("start_file",report_none)
        register("stop_file", report_none)
    end
end)

-- start_run doesn't work

-- luatex.registerstartactions(function()
--     if environment.arguments.sandbox then
--         sandbox.enable()
--     end
-- end)