summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/trac-tex.lmt
blob: 58dfb0316adf9904c4345342340be881b129b253 (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
if not modules then modules = { } end modules ['trac-tex'] = {
    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"
}

local texhashtokens = tex.hashtokens

local trackers  = trackers
local token     = token
local saved     = { }
local create    = token.create
local undefined = create("undefined").command

function trackers.savehash()
    saved = texhashtokens()
    return saved
end

function trackers.dumphashtofile(filename,delta)
    local list   = { }
    local hash   = texhashtokens()
    local create = token.create
    for i=1,#hash do
        local name = hash[i]
        if not delta or not saved[name] then
            local token = create(name)
            if token.command ~= undefined then
                local category = token.cmdname
                local dk = list[category]
                if not dk then
                    dk = {
                        names = { },
                        found = 0,
                     -- code  = token[1],
                    }
                    list[category] = dk
                end
                if token.protected then
                    if token.expandable then
                        dk.names[name] = "ep"
                    else
                        dk.names[name] = "-p"
                    end
                else
                    if token.expandable then
                        dk.names[name] = "ep"
                    else
                        dk.names[name] = "--"
                    end
                end
                dk.found = dk.found + 1
            end
        end
    end
    table.save(filename or tex.jobname .. "-hash.log",list)
end

local delta = nil

local function dump_hash(wanteddelta)
    if delta == nil then
        saved = saved or trackers.savehash()
        luatex.registerstopactions(1,function() dump_hash(nil,wanteddelta) end) -- at front
    end
    delta = wanteddelta
end

directives.register("system.dumphash",  function() dump_hash(false) end)
directives.register("system.dumpdelta", function() dump_hash(true ) end)

local function saveusedfilesintrees(format)
    local data = {
        jobname = environment.jobname or "?",
        version = environment.version or "?",
        kind    = environment.kind    or "?",
        files   = resolvers.foundintrees()
    }
    local filename = file.replacesuffix(environment.jobname or "context-job",'jlg')
    if format == "lua" then
        io.savedata(filename,table.serialize(data,true))
    else
        io.savedata(filename,table.toxml(data,"job"))
    end
end

directives.register("system.dumpfiles", function(v)
    luatex.registerstopactions(function() saveusedfilesintrees(v) end)
end)

local profiled = table.setmetatableindex("number")

interfaces.implement {
    name      = "profilemacro",
    arguments = "csname",
    actions   = function(cs)
        profiled[cs] = profiled[cs] + 1
    end,
}

interfaces.implement {
    name      = "showprofiledmacros",
    public    = true,
    protected = true,
    actions   = function(cs)
        local n = 0
        local m = 0
        for k, v in table.sortedhash(profiled) do
            logs.report("profiled", "%s : %i",k,v)
            n = n + v
            m = m + 1
        end
        if m > 0 and n > 0 then
            logs.report("profiled", "%i profiled : %i",m,n)
        end
    end,
}