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

local tonumber = tonumber

local context           = context
local implement         = interfaces.implement

local injectors         = { }
typesetters.injectors   = injectors
local list              = { }
injectors.list          = list
local showall           = false

local settings_to_array = utilities.parsers.settings_to_array

local variables         = interfaces.variables
local v_next            = variables.next
local v_previous        = variables.previous

local ctx_domarkinjector     = context.domarkinjector
local ctx_doactivateinjector = context.doactivateinjector

table.setmetatableindex(list,function(t,k)
    local v = {
        counter = 0,
        actions = { },
        show    = false,
        active  = false,
    }
    t[k] = v
    return v
end)

function injectors.reset(name)
    list[name] = nil
end

local function activate(injector,name)
    if not injector.active then
        ctx_doactivateinjector(name)
        injector.active = true
        if showall then
            -- in case we already enabled tracing
            injector.show = true
        end
    end
end

function injectors.set(name,numbers,command)
    local injector = list[name]
    local actions  = injector.actions
    local places   = settings_to_array(numbers)
    for i=1,#places do
        actions[tonumber(places[i])] = command
    end
    -- not: injector.show = true
    activate(injector,name)
end

function injectors.show(name)
    if not name or name == "" then
        showall = true
        local names = settings_to_array(name)
        for name, injector in next, list do
            injector.show = true
            activate(injector,name)
        end
    else
        local names = settings_to_array(name)
        for i=1,#names do
            local name     = names[i]
            local injector = list[name]
            if injector then
                injector.show = true
                activate(injector,name)
            end
        end
    end
end

function injectors.mark(name,show)
    local injector = list[name]
    local n = injector.counter + 1
    injector.counter = n
    if showall or injector.show then
        ctx_domarkinjector(injector.actions[n] and 1 or 0,n)
    end
end

function injectors.check(name,n) -- we could also accent n = number : +/- 2
    local injector = list[name]
    if not n or n == "" or n == v_next then
        n = injector.counter + 1
    elseif n == v_previous then
        n = injector.counter
    else
        n = tonumber(n) or 0
    end
    local action = injector.actions[n]
    if action then
        context(action)
    end
end

-- maybe string -> argument

implement { name = "resetinjector",         actions = injectors.reset, arguments = "string" }
implement { name = "showinjector",          actions = injectors.show,  arguments = "string" }
implement { name = "setinjector",           actions = injectors.set,   arguments = "3 strings" }
implement { name = "markinjector",          actions = injectors.mark,  arguments = "string" }
implement { name = "checkinjector",         actions = injectors.check, arguments = "2 strings" }
--------- { name = "checkpreviousinjector", actions = injectors.check, arguments = { "string", v_previous } }
--------- { name = "checknextinjector",     actions = injectors.check, arguments = { "string", v_next } }