page-ins.lua /size: 2999 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['page-ins'] = {
2    version   = 1.001,
3    comment   = "companion to page-mix.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 next = next
10
11structures           = structures or { }
12structures.inserts   = structures.inserts or { }
13local inserts        = structures.inserts
14
15local allocate       = utilities.storage.allocate
16
17inserts.stored       = inserts.stored or allocate { } -- combining them in one is inefficient in the
18inserts.data         = inserts.data   or allocate { } -- bytecode storage pool
19
20local variables      = interfaces.variables
21local v_page         = variables.page
22local v_auto         = variables.auto
23
24local context        = context
25local implement      = interfaces.implement
26
27storage.register("structures/inserts/stored", inserts.stored, "structures.inserts.stored")
28
29local data           = inserts.data
30local stored         = inserts.stored
31
32for name, specification in next, stored do
33    data[specification.number] = specification
34    data[name]                 = specification
35end
36
37function inserts.define(name,specification)
38    specification.name= name
39    local number = specification.number or 0
40    data[name]   = specification
41    data[number] = specification
42    -- only needed at runtime as this get stored in a bytecode register
43    stored[name] = specification
44    if not specification.location then
45        specification.location = v_page
46    end
47    return specification
48end
49
50function inserts.setup(name,settings)
51    local specification = data[name]
52    for k, v in next, settings do
53        -- maybe trace change
54        specification[k] = v
55    end
56    return specification
57end
58
59function inserts.setlocation(name,location) -- a practical fast one
60    data[name].location = location
61end
62
63function inserts.getlocation(name,location)
64    return data[name].location or v_page
65end
66
67function inserts.getdata(name) -- or number
68    return data[name]
69end
70
71function inserts.getname(number)
72    return data[name].name
73end
74
75function inserts.getnumber(name)
76    return data[name].number
77end
78
79-- interface
80
81implement {
82    name      = "defineinsertion",
83    actions   = inserts.define,
84    arguments = {
85        "string",
86        {
87            { "number", "integer" }
88        }
89    }
90}
91
92implement {
93    name      = "setupinsertion",
94    actions   = inserts.setup,
95    arguments = {
96        "string",
97        {
98            { "location" }
99        }
100    }
101}
102
103implement {
104    name      = "setinsertionlocation",
105    actions   = inserts.setlocation,
106    arguments = "2 strings",
107}
108
109implement {
110    name      = "insertionnumber",
111    actions   = function(name) context(data[name].number or 0) end,
112    arguments = "string"
113}
114
115implement {
116    name      = "setinsertmigration",
117    arguments = "string",
118    actions   = function(state)
119        nodes.migrations.setinserts(state == v_auto)
120    end
121}
122