summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/page-ins.lua
blob: 0323952d00f94a36ab87d12c6685c739e760e339 (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
if not modules then modules = { } end modules ['page-ins'] = {
    version   = 1.001,
    comment   = "companion to page-mix.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

local next = next

structures           = structures or { }
structures.inserts   = structures.inserts or { }
local inserts        = structures.inserts

local allocate       = utilities.storage.allocate

inserts.stored       = inserts.stored or allocate { } -- combining them in one is inefficient in the
inserts.data         = inserts.data   or allocate { } -- bytecode storage pool

local variables      = interfaces.variables
local v_page         = variables.page

local context        = context
local implement      = interfaces.implement

storage.register("structures/inserts/stored", inserts.stored, "structures.inserts.stored")

local data           = inserts.data
local stored         = inserts.stored

for name, specification in next, stored do
    data[specification.number] = specification
    data[name]                 = specification
end

function inserts.define(name,specification)
    specification.name= name
    local number = specification.number or 0
    data[name]   = specification
    data[number] = specification
    -- only needed at runtime as this get stored in a bytecode register
    stored[name] = specification
    if not specification.location then
        specification.location = v_page
    end
    return specification
end

function inserts.setup(name,settings)
    local specification = data[name]
    for k, v in next, settings do
        -- maybe trace change
        specification[k] = v
    end
    return specification
end

function inserts.setlocation(name,location) -- a practical fast one
    data[name].location = location
end

function inserts.getlocation(name,location)
    return data[name].location or v_page
end

function inserts.getdata(name) -- or number
    return data[name]
end

function inserts.getname(number)
    return data[name].name
end

function inserts.getnumber(name)
    return data[name].number
end

-- interface

implement {
    name      = "defineinsertion",
    actions   = inserts.define,
    arguments = {
        "string",
        {
            { "number", "integer" }
        }
    }
}

implement {
    name      = "setupinsertion",
    actions   = inserts.setup,
    arguments = {
        "string",
        {
            { "location" }
        }
    }
}

implement {
    name      = "setinsertionlocation",
    actions   = inserts.setlocation,
    arguments = "2 strings",
}

implement {
    name      = "insertionnumber",
    actions   = function(name) context(data[name].number or 0) end,
    arguments = "string"
}