trac-tex.lmt /size: 3601 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['trac-tex'] = {
2    version   = 1.001,
3    comment   = "companion to trac-deb.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
9local texhashtokens = tex.hashtokens
10
11local trackers  = trackers
12local token     = token
13local saved     = { }
14local create    = token.create
15local undefined = create("undefined").command
16
17function trackers.savehash()
18    saved = texhashtokens()
19    return saved
20end
21
22function trackers.dumphashtofile(filename,delta)
23    local list   = { }
24    local hash   = texhashtokens()
25    local create = token.create
26    for i=1,#hash do
27        local name = hash[i]
28        if not delta or not saved[name] then
29            local token = create(name)
30            if token.command ~= undefined then
31                local category = token.cmdname
32                local dk = list[category]
33                if not dk then
34                    dk = {
35                        names = { },
36                        found = 0,
37                     -- code  = token[1],
38                    }
39                    list[category] = dk
40                end
41                if token.protected then
42                    if token.expandable then
43                        dk.names[name] = "ep"
44                    else
45                        dk.names[name] = "-p"
46                    end
47                else
48                    if token.expandable then
49                        dk.names[name] = "ep"
50                    else
51                        dk.names[name] = "--"
52                    end
53                end
54                dk.found = dk.found + 1
55            end
56        end
57    end
58    table.save(filename or tex.jobname .. "-hash.log",list)
59end
60
61local delta = nil
62
63local function dump_hash(wanteddelta)
64    if delta == nil then
65        saved = saved or trackers.savehash()
66        luatex.registerstopactions(1,function() dump_hash(nil,wanteddelta) end) -- at front
67    end
68    delta = wanteddelta
69end
70
71directives.register("system.dumphash",  function() dump_hash(false) end)
72directives.register("system.dumpdelta", function() dump_hash(true ) end)
73
74local function saveusedfilesintrees(format)
75    local data = {
76        jobname = environment.jobname or "?",
77        version = environment.version or "?",
78        kind    = environment.kind    or "?",
79        files   = resolvers.foundintrees()
80    }
81    local filename = file.replacesuffix(environment.jobname or "context-job",'jlg')
82    if format == "lua" then
83        io.savedata(filename,table.serialize(data,true))
84    elseif format == "json" then
85        io.savedata(filename,utilities.json.tojson(data))
86    else
87        io.savedata(filename,table.toxml(data,"job"))
88    end
89end
90
91directives.register("system.dumpfiles", function(v)
92    luatex.registerstopactions(function() saveusedfilesintrees(v) end)
93end)
94
95local profiled = table.setmetatableindex("number")
96
97interfaces.implement {
98    name      = "profilemacro",
99    arguments = "csname",
100    actions   = function(cs)
101        profiled[cs] = profiled[cs] + 1
102    end,
103}
104
105interfaces.implement {
106    name      = "showprofiledmacros",
107    public    = true,
108    protected = true,
109    actions   = function(cs)
110        local n = 0
111        local m = 0
112        for k, v in table.sortedhash(profiled) do
113            logs.report("profiled", "%s : %i",k,v)
114            n = n + v
115            m = m + 1
116        end
117        if m > 0 and n > 0 then
118            logs.report("profiled", "%i profiled : %i",m,n)
119        end
120    end,
121}
122
123