typo-inj.lmt /size: 3620 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['typo-inj'] = { -- was node-par
2    version   = 1.001,
3    comment   = "companion to typo-inj.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 tonumber = tonumber
10
11local context           = context
12local implement         = interfaces.implement
13
14local injectors         = { }
15typesetters.injectors   = injectors
16local list              = { }
17injectors.list          = list
18local showall           = false
19
20local settings_to_array = utilities.parsers.settings_to_array
21
22local v_next            <const> = interfaces.variables.next
23local v_previous        <const> = interfaces.variables.previous
24
25local ctx_domarkinjector     = context.domarkinjector
26local ctx_doactivateinjector = context.doactivateinjector
27
28table.setmetatableindex(list,function(t,k)
29    local v = {
30        counter = 0,
31        actions = { },
32        show    = false,
33        active  = false,
34    }
35    t[k] = v
36    return v
37end)
38
39function injectors.reset(name)
40    list[name] = nil
41end
42
43local function activate(injector,name)
44    if not injector.active then
45        ctx_doactivateinjector(name)
46        injector.active = true
47        if showall then
48            -- in case we already enabled tracing
49            injector.show = true
50        end
51    end
52end
53
54function injectors.set(name,numbers,command)
55    local injector = list[name]
56    local actions  = injector.actions
57    local places   = settings_to_array(numbers)
58    for i=1,#places do
59        actions[tonumber(places[i])] = command
60    end
61    -- not: injector.show = true
62    activate(injector,name)
63end
64
65function injectors.show(name)
66    if not name or name == "" then
67        showall = true
68        local names = settings_to_array(name)
69        for name, injector in next, list do
70            injector.show = true
71            activate(injector,name)
72        end
73    else
74        local names = settings_to_array(name)
75        for i=1,#names do
76            local name     = names[i]
77            local injector = list[name]
78            if injector then
79                injector.show = true
80                activate(injector,name)
81            end
82        end
83    end
84end
85
86function injectors.mark(name,show)
87    local injector = list[name]
88    local n = injector.counter + 1
89    injector.counter = n
90    if showall or injector.show then
91        ctx_domarkinjector(injector.actions[n] and 1 or 0,n)
92    end
93end
94
95function injectors.check(name,n) -- we could also accent n = number : +/- 2
96    local injector = list[name]
97    if not n or n == "" or n == v_next then
98        n = injector.counter + 1
99    elseif n == v_previous then
100        n = injector.counter
101    else
102        n = tonumber(n) or 0
103    end
104    local action = injector.actions[n]
105    if action then
106        context(action)
107    end
108end
109
110-- maybe string -> argument
111
112implement { name = "resetinjector",         actions = injectors.reset, arguments = "string" }
113implement { name = "showinjector",          actions = injectors.show,  arguments = "string" }
114implement { name = "setinjector",           actions = injectors.set,   arguments = "3 strings" }
115implement { name = "markinjector",          actions = injectors.mark,  arguments = "string" }
116implement { name = "checkinjector",         actions = injectors.check, arguments = "2 strings" }
117--------- { name = "checkpreviousinjector", actions = injectors.check, arguments = { "string", v_previous } }
118--------- { name = "checknextinjector",     actions = injectors.check, arguments = { "string", v_next } }
119