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

-- This is an old mechanism, a result of some experiments in the early days of
-- luatex and mkiv, but still nice anyway. I will clean it up in lmtx.

local status = status

local tonumber, tostring, type = tonumber, tostring, type
local format, concat, match, find, gsub = string.format, table.concat, string.match, string.find, string.gsub

local report_nl    = logs.newline
local report_str   = logs.writer

tracers            = tracers or { }
local tracers      = tracers

local implement    = interfaces.implement

local ioflush      = io.flush
local ioread       = io.read
local ossleep      = os.sleep
local osexit       = os.exit
local writenl      = texio.writenl
local write        = texio.write

local runlocal     = tex.runlocal
local terminaldata = false
local context      = context

implement {
    name    = "fetchterminaldata",
    actions = function()
        context(terminaldata)
    end,
}

function texio.terminal()
    writenl("\n" .. "entering interactive mode, use \\quit to abort reading" .."\n\n")
    while true do
        write(">")
        ioflush()
        terminaldata = ioread()
        if terminaldata == "\\quit" then
            terminaldata = false
            break
        else
            runlocal("t_syst_terminal_data",nil,nil,true) -- obeymode
        end
    end
end

implement {
    name      = "readfromterminal",
    public    = true,
    protected = true,
    actions   = texio.terminal,
}

local savedluaerror = nil
local usescitelexer = nil
local quitonerror   = true

local function errorreporter(luaerror)
    local category = luaerror and "lua error" or "tex error"
    local report = logs.reporter(category)
    logs.enable(category)
    return report
end

function tracers.showlines(filename,linenumber,offset,luaerrorline)
    local data = io.loaddata(filename)
    if not data or data == "" then
        local hash = url.hashed(filename)
        if not hash.noscheme then
            local ok, d, n = resolvers.loaders.byscheme(hash.scheme,filename)
            if ok and n > 0 then
                data = d
            end
        end
    end
    local scite = usescitelexer and require("util-sci")
    if scite then
        return utilities.scite.tohtml(data,"tex",linenumber or true,false)
    else
        local lines = data and string.splitlines(data)
        if lines and #lines > 0 then
            if luaerrorline and luaerrorline > 0 then
                -- lua error: linenumber points to last line
                local start = "\\startluacode"
                local stop  = "\\stopluacode"
                local n = linenumber
                for i=n,1,-1 do
                    local line = lines[i]
                    if not line then
                        break
                    elseif find(line,start) then
                        n = i + luaerrorline - 1
                        if n <= linenumber then
                            linenumber = n
                        end
                        break
                    end
                end
            end
            offset = tonumber(offset) or 10
            linenumber = tonumber(linenumber) or 10
            local start = math.max(linenumber - offset,1)
            local stop = math.min(linenumber + offset,#lines)
            if stop > #lines then
                return "<linenumber past end of file>"
            else
                local result, fmt = { }, "%" .. #tostring(stop) .. "d %s  %s"
                for n=start,stop do
                    result[#result+1] = format(fmt,n,n == linenumber and ">>" or "  ",lines[n])
                end
                return concat(result,"\n")
            end
        else
            return "<empty file>"
        end
    end
end

-- todo: last tex error has ! prepended
-- todo: some nested errors have two line numbers
-- todo: collect errorcontext in string (after code cleanup)
-- todo: have a separate status.lualinenumber
-- todo: \starttext bla \blank[foo] bla \stoptext

local nop = function() end
local resetmessages = status.resetmessages or nop

local function processerror(offset,errortype)
    local readstate      = status.getreadstate()
    local filename       = readstate.filename
    local linenumber     = readstate.linenumber
    local skiplinenumber = readstate.skiplinenumber
    local errorstate     = status.errorstate
    local lastcontext    = errorstate.errorcontext
    local lasttexerror   = errorstate.error or "?"
    local lastluaerror   = errorstate.luaerror or "?" -- lasttexerror
    local luaerrorline   = match(lastluaerror,[[lua%]?:.-(%d+)]]) or (lastluaerror and find(lastluaerror,"?:0:",1,true) and 0)
    local lastmpserror   = match(lasttexerror,[[^.-mp%serror:%s*(.*)$]])
    resetmessages()
    lastluaerror = gsub(lastluaerror,"%[\\directlua%]","[ctxlua]")
    tracers.printerror {
        filename       = filename,
        linenumber     = linenumber,
        skiplinenumber = skiplinenumber,
        offset         = tonumber(offset) or 10,
        lasttexerror   = lasttexerror,
        lastmpserror   = lastmpserror,
        lastluaerror   = lastluaerror, -- can be the same as lasttexerror
        luaerrorline   = luaerrorline,
        lastcontext    = lastcontext,
        lasttexhelp    = tex.gethelptext and tex.gethelptext() or nil,
        errortype      = errortype,
    }
    if job and type(job.disablesave) == "function" then
        job.disablesave()
    end
    lua.setexitcode(1)
end

directives.register("system.quitonerror",function(v)
    quitonerror = toboolean(v)
end)

directives.register("system.usescitelexer",function(v)
    usescitelexer = toboolean(v)
end)

local busy = false

function tracers.printerror(specification)
    if not busy then
        busy = true
        local errorvalues  = table.swapped(tex.geterrorvalues())
        local filename     = specification.filename
        local linenumber   = specification.linenumber
        local lasttexerror = specification.lasttexerror
        local lastmpserror = specification.lastmpserror
        local lastluaerror = specification.lastluaerror
        local lastcontext  = specification.lastcontext
        local luaerrorline = specification.luaerrorline
        local errortype    = specification.errortype
        local offset       = specification.offset
        local endoffile    = specification.endoffile
        local report       = errorreporter(luaerrorline)
        if errortype == errorvalues.warning then
            report("warning error: %s",lasttexerror or "-")
            busy = false;
            return;
        elseif errortype == errorvalues.eof then
            report("runaway error: %s",lasttexerror or "-")
            if not quitonerror and texio.terminal then
                texio.terminal() -- not well tested
            end
        elseif errortype == errorvalues.condition then
            linenumber = specification.skiplinenumber
            if linenumber > 0 then
                report("condition error on line %s in file %s: %s",linenumber,filename,lasttexerror)
                report_nl()
                report_str(tracers.showlines(filename,linenumber,offset,tonumber(luaerrorline)))
                report_nl()
            else
                report("runaway condition error: %s",lasttexerror or "-")
            end
            quitonerror = true
        elseif not filename then
            report("fuzzy error:")
            report("  tex: %s",lasttexerror or "-")
            report("  lua: %s",lastluaerror or "-")
            report("  mps: %s",lastmpserror or "-")
        elseif type(filename) == "number" then
            report("error on line %s of filehandle %s: %s ...",linenumber,lasttexerror)
        else
            report_nl()
            if luaerrorline then
                if linenumber == 0 or not filename or filename == "" then
                    print("\nfatal lua error:\n\n",lastluaerror,"\n")
                    luatex.abort()
                    return
                else
                    report("lua error on line %s in file %s:\n\n%s",linenumber,filename,lastluaerror)
                end
            elseif lastmpserror then
                report("mp error on line %s in file %s:\n\n%s",linenumber,filename,lastmpserror)
            else
                report("tex error on line %s in file %s: %s",linenumber,filename,lasttexerror)
                if lastcontext then
                    report_nl()
                    report_str(lastcontext)
                    report_nl()
                else
                    report_nl()
                 -- tex.showcontext()
                end
                if lastluaerror and not match(lastluaerror,"^%s*[%?]*%s*$") then
                    print("\nlua error:\n\n",lastluaerror,"\n")
                    quitonerror = true
                end
            end
            report_nl()
            report_str(tracers.showlines(filename,linenumber,offset,tonumber(luaerrorline)))
            report_nl()
        end
        if quitonerror then
            local name = tex.jobname or ""
            if name ~= "" then
                table.save(name .. "-error.log",specification)
            end
            local help = specification.lasttexhelp
            if help and #help > 0 then
                report_nl()
                report_str(help)
                report_nl()
                report_nl()
            end
            luatex.abort()
        end
        busy = false
    end
end

luatex.wrapup(function() os.remove(tex.jobname .. "-error.log") end)

local function processwarning(offset)
    local warningstate = status.warningstate
    local lastwarning  = warningstate.warning or "?"
    local lastlocation = warningstate.warningtag or "?"
    resetmessages()
    tracers.printwarning {
        lastwarning  = lastwarning,
        lastlocation = lastlocation,
    }
end

function tracers.printwarning(specification)
    logs.report("luatex warning","%s: %s",specification.lastlocation,specification.lastwarning)
end

directives.register("system.errorcontext", function(v)
    local register = callback.register
    if v then
        register('show_error_message',   nop)
        register('show_warning_message', function()         processwarning(v)   end)
        register('intercept_lua_error',  function()         processerror(v)     end)
        register('intercept_tex_error',  function(mode,eof) processerror(v,eof) return mode end)
    else
        register('show_error_message',   nil)
        register('show_warning_message', nil)
        register('intercept_lua_error',  nil)
        register('intercept_tex_error',  nil)
    end
end)

-- this might move

lmx = lmx or { }

lmx.htmfile = function(name) return environment.jobname .. "-status.html" end
lmx.lmxfile = function(name) return resolvers.findfile(name,'tex') end

local function reportback(lmxname,default,variables)
    if lmxname == false then
        return variables
    else
        local name = lmx.show(type(lmxname) == "string" and lmxname or default,variables)
        if name then
            logs.report("context report","file: %s",name)
        end
    end
end

local function showerror(lmxname)
    local readstate    = status.getreadstate()
    local filename     = readstate.filename
    local linenumber   = tonumber(readstate.linenumber) or 0
    local errorcontext = ""
    if not filename then
        filename     = status.iocodes[readstate.iocode]
        errorcontext = 'error in filename'
    else
        errorcontext = tracers.showlines(filename,linenumber,offset)
    end
    local variables = {
        ['title']                = 'ConTeXt Error Information',
        ['errormessage']         = status.geterrorstate().error or "?",
        ['linenumber']           = linenumber,
        ['color-background-one'] = lmx.get('color-background-yellow'),
        ['color-background-two'] = lmx.get('color-background-purple'),
        ['filename']             = filename,
        ['errorcontext']         = errorcontext,
    }
    reportback(lmxname,"context-error.lmx",variables)
    luatex.abort()
end

lmx.showerror = showerror

function lmx.overloaderror(v)
    if v == "scite" then
        usescitelexer = true
    end
    callback.register('show_error_message',  function() showerror() end)
--     callback.register('intercept_lua_error', function() showerror() end)
--     callback.register('intercept_tex_error', function() showerror() end)
end

directives.register("system.showerror", lmx.overloaderror)

-- local debugger = utilities.debugger
--
-- local function trace_calls(n)
--     debugger.enable()
--     luatex.registerstopactions(function()
--         debugger.disable()
--         debugger.savestats(tex.jobname .. "-luacalls.log",tonumber(n))
--     end)
--     trace_calls = function() end
-- end
--
-- directives.register("system.tracecalls", function(n)
--     trace_calls(n)
-- end) -- indirect is needed for nilling

-- Obsolete ... not that usefull as normally one runs from an editor and
-- when run unattended it makes no sense either.

-- local editor = [[scite "-open:%filename%" -goto:%linenumber%]]
--
-- directives.register("system.editor",function(v)
--     editor = v
-- end)
--
-- callback.register("call_edit",function(filename,linenumber)
--     if editor then
--         editor = gsub(editor,"%%s",filename)
--         editor = gsub(editor,"%%d",linenumber)
--         editor = gsub(editor,"%%filename%%",filename)
--         editor = gsub(editor,"%%linenumber%%",linenumber)
--         logs.report("system","starting editor: %s",editor)
--         os.execute(editor)
--     end
-- end)

implement { name = "showtrackers",       public = true, protected = true, actions = trackers.show }
implement { name = "enabletrackers",     public = true, protected = true, actions = trackers.enable,     arguments = "optional" }
implement { name = "disabletrackers",    public = true, protected = true, actions = trackers.disable,    arguments = "optional" }
implement { name = "resettrackers",      public = true, protected = true, actions = trackers.reset }

implement { name = "showdirectives",     public = true, protected = true, actions = directives.show }
implement { name = "enabledirectives",   public = true, protected = true, actions = directives.enable,   arguments = "optional" }
implement { name = "disabledirectives",  public = true, protected = true, actions = directives.disable,  arguments = "optional" }

implement { name = "showexperiments",    public = true, protected = true, actions = experiments.show }
implement { name = "enableexperiments",  public = true, protected = true, actions = experiments.enable,  arguments = "optional" }
implement { name = "disableexperiments", public = true, protected = true, actions = experiments.disable, arguments = "optional" }

implement { name = "overloaderror",      public = true, protected = true, actions = lmx.overloaderror }
implement { name = "showlogcategories",  public = true, protected = true, actions = logs.show }

local debugger = utilities.debugger

directives.register("system.profile",function(n)
    luatex.registerstopactions(function()
        debugger.disable()
        debugger.savestats("luatex-profile.log",tonumber(n) or 0)
        report_nl()
        logs.report("system","profiler stopped, log saved in %a","luatex-profile.log")
        report_nl()
    end)
    logs.report("system","profiler started")
    debugger.enable()
end)

local report = logs.reporter("[[diagnostic]]")

implement {
    name      = "diagnostic",
    public    = true,
 -- protected = false, -- expandable
    arguments = { "optional", "string" },
    actions   = function(t,s)
        if t == "quit" then
            report(s)
            osexit()
        else
            t = tonumber(t)
            if t then
                report("%s (sleep: %.3N)",s,t)
                ioflush()
                ossleep(t)
            else
                report(s)
                ioflush()
            end
        end
    end
}