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