back-ini.lmt /size: 4273 b    last modification: 2024-01-16 09:02
1if not modules then modules = { } end modules ['back-ini'] = {
2    version   = 1.001,
3    comment   = "companion to back-ini.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, type = next, type
10local format = string.format
11
12backends                = backends or { }
13local backends          = backends
14
15local context           = context
16
17local trace             = false  trackers.register("backend", function(v) trace = v end)
18local report            = logs.reporter("backend")
19
20local allocate          = utilities.storage.allocate
21local setmetatableindex = table.setmetatableindex
22----- setaction         = nodes.tasks.setaction
23
24local implement         = interfaces.implement
25local variables         = interfaces.variables
26
27local texset            = tex.set
28
29local nodeinjections    = { }
30local codeinjections    = { }
31local registrations     = { }
32local tables            = allocate()
33
34local function nothing()
35    return nil
36end
37
38backends.nothing = nothing
39
40local function donothing(t,k)
41    t[k] = nothing
42    return nothing
43end
44
45setmetatableindex(nodeinjections, donothing)
46setmetatableindex(codeinjections, donothing)
47setmetatableindex(registrations,  donothing)
48
49local defaults = {
50    nodeinjections = nodeinjections,
51    codeinjections = codeinjections,
52    registrations  = registrations,
53    tables         = tables,
54}
55
56backends.defaults = defaults -- so, when not in the specific namespace we need to register here (to be checked)
57
58backends.nodeinjections = { }  setmetatableindex(backends.nodeinjections, nodeinjections)
59backends.codeinjections = { }  setmetatableindex(backends.codeinjections, codeinjections)
60backends.registrations  = { }  setmetatableindex(backends.registrations,  registrations)
61backends.tables         = { }  setmetatableindex(backends.tables,         tables)
62
63backends.current = "unknown"
64
65local registered = table.setmetatableindex(function(t,k)
66    local v = {
67        name           = k,
68        nodeinjections = { },
69        codeinjections = { },
70        registrations  = { },
71        tables         = { },
72    }
73    t[k] = v
74    return v
75end)
76
77backends.registered = registered
78
79-- The binding feature is mostly there to prevent side effects of overloading
80-- not so much for performance because there are not that many calls.
81
82function backends.initialize(what)
83    if type(what) == "string" then
84        local backend = registered[what]
85        if backend then
86            if trace then
87                report("initializing backend %a",what)
88            end
89            for category, default in next, defaults do
90                local target = backends[category]
91                local plugin = backend [category]
92                setmetatableindex(plugin, default)
93                setmetatableindex(target, plugin)
94            end
95            --
96            backends.current = what
97            -- delayed / out-of-order locals: like lpdf.* (a few forward references)
98            updaters.apply("backends." .. what .. ".latebindings")
99            -- delayed / out-of-order locals: backends.[c|n]odeinjections.* (not all, only critical ones)
100            updaters.apply("backends.injections.latebindings")
101        elseif trace then
102            report("no backend named %a",what)
103        end
104    end
105end
106
107statistics.register("used backend", function()
108    local bc = backends.current
109    return bc ~= "unknown" and bc or nil
110end)
111
112-- can best be here
113
114interfaces.implement {
115    name      = "setrealspaces",
116    arguments = "string",
117    actions   = function(v)
118        directives.enable("backends.spaces", v == variables.yes)
119     -- setaction("shipouts","nodes.handlers.accessibility",v == variables.yes)
120    end
121}
122
123-- moved to here
124
125local included = table.setmetatableindex( {
126    context  = true,
127    id       = true,
128    metadata = true,
129    date     = true,
130    id       = true,
131    pdf      = true,
132}, function(t,k)
133    return true
134end)
135
136backends.included = included
137
138-- could also be codeinjections
139
140function backends.getcallbackstate()
141    return { count = status.late_callbacks or 0 }
142end
143
144--
145
146function backends.setencryption(specification)
147    codeinjections.setencryption(specification)
148end
149