summaryrefslogtreecommitdiff
path: root/tex/context/base/typo-inj.lua
blob: b5d9e1c513a16496be9e7174ba385944f813208e (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
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 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

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
    if not injector.active then
        ctx_doactivateinjector(name)
        injector.active = true
    end
end

function injectors.show(name)
    if not name or name == "" then
        showall = true
    else
        local list = settings_to_array(name)
        for i=1,#list do
            list[list[i]].show = true
        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 n == false then
        n = injector.counter
    elseif n == nil then
        n = injector.counter + 1 -- next (upcoming)
    else
        n = tonumber(n) or 0
    end
    local action = injector.actions[n]
    if action then
        context(action)
    end
end

implement { name = "resetinjector",         actions = injectors.reset, arguments = "string" }
implement { name = "showinjector",          actions = injectors.show,  arguments = "string" }
implement { name = "setinjector",           actions = injectors.set,   arguments = { "string", "string", "string" } }
implement { name = "markinjector",          actions = injectors.mark,  arguments = "string" }
implement { name = "checkinjector",         actions = injectors.check, arguments = "string" }
implement { name = "checkpreviousinjector", actions = injectors.check, arguments = { "string", true } }
implement { name = "checknextinjector",     actions = injectors.check }