mult-aux.lua /size: 5446 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mult-aux'] = {
2    version   = 1.001,
3    comment   = "companion to mult-aux.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 find = string.find
10local next = next
11
12interfaces.namespaces = interfaces.namespaces or { }
13local namespaces      = interfaces.namespaces
14local variables       = interfaces.variables
15
16local context         = context
17
18local trace_namespaces = false  trackers.register("interfaces.namespaces", function(v) trace_namespaces = v end)
19
20local report_namespaces = logs.reporter("interface","namespaces")
21
22local v_yes, v_list = variables.yes, variables.list
23
24local prefix  = "????"
25local meaning = "@@@@"
26
27local data = { }
28
29function namespaces.define(namespace,settings)
30    if trace_namespaces then
31        report_namespaces("installing namespace %a with settings %a",namespace,settings)
32    end
33    if data[namespace] then
34        report_namespaces("namespace %a is already taken",namespace)
35    end
36    if #namespace < 2 then
37        report_namespaces("namespace %a should have more than 1 character",namespace)
38    end
39    local ns = { }
40    data[namespace] = ns
41    utilities.parsers.settings_to_hash(settings,ns)
42    local name = ns.name
43    if not name or name == "" then
44        report_namespaces("provide a (command) name in namespace %a",namespace)
45    end
46    local self = "\\" .. prefix .. namespace
47    context.unprotect()
48 -- context.installnamespace(namespace)
49    context("\\def\\%s%s{%s%s}",prefix,namespace,meaning,namespace) -- or context.setvalue
50    if trace_namespaces then
51        report_namespaces("using namespace %a for %a",namespace,name)
52    end
53    local parent = ns.parent or ""
54    if parent ~= "" then
55        if trace_namespaces then
56            report_namespaces("namespace %a for %a uses parent %a",namespace,name,parent)
57        end
58        if not find(parent,"\\",1,true) then
59            parent = "\\" .. prefix .. parent
60            -- todo: check if defined
61        end
62    end
63    context.installparameterhandler(self,name)
64    if trace_namespaces then
65        report_namespaces("installing parameter handler for %a",name)
66    end
67    context.installparameterhashhandler(self,name)
68    if trace_namespaces then
69        report_namespaces("installing parameterhash handler for %a",name)
70    end
71    local style = ns.style
72    if style == v_yes then
73        context.installstyleandcolorhandler(self,name)
74        if trace_namespaces then
75            report_namespaces("installing attribute handler for %a",name)
76        end
77    end
78    local command = ns.command
79    if command == v_yes then
80        context.installdefinehandler(self,name,parent)
81        if trace_namespaces then
82            report_namespaces("installing definition  command for %a (single)",name)
83        end
84    elseif command == v_list then
85        context.installdefinehandler(self,name,parent)
86        if trace_namespaces then
87            report_namespaces("installing definition command for %a (multiple)",name)
88        end
89    end
90    local setup = ns.setup
91    if setup == v_yes then
92        context.installsetuphandler(self,name)
93        if trace_namespaces then
94            report_namespaces("installing setup command for %a (%s)",name,"single")
95        end
96    elseif setup == v_list then
97        context.installsetuphandler(self,name)
98        if trace_namespaces then
99            report_namespaces("installing setup command for %a (%s)",name,"multiple")
100        end
101    end
102    local set = ns.set
103    if set == v_yes then
104        context.installparametersethandler(self,name)
105        if trace_namespaces then
106            report_namespaces("installing set/let/reset command for %a (%s)",name,"single")
107        end
108    elseif set == v_list then
109        context.installparametersethandler(self,name)
110        if trace_namespaces then
111            report_namespaces("installing set/let/reset command for %a (%s)",name,"multiple")
112        end
113    end
114    local frame = ns.frame
115    if frame == v_yes then
116        context.installinheritedframed(name)
117        if trace_namespaces then
118            report_namespaces("installing framed command for %a",name)
119        end
120    end
121    context.protect()
122end
123
124function utilities.formatters.list(data,key,keys)
125    if not keys then
126        keys = { }
127        for _, v in next, data do
128            for k, _ in next, v do
129                keys[k] = true
130            end
131        end
132        keys = table.sortedkeys(keys)
133    end
134    context.starttabulate { "|"..string.rep("l|",#keys+1) }
135    context.NC()
136    context(key)
137    for i=1,#keys do
138        context.NC()
139        context(keys[i])
140    end context.NR()
141    context.HL()
142    for k, v in table.sortedhash(data) do
143        context.NC()
144        context(k)
145        for i=1,#keys do
146            context.NC()
147            context(v[keys[i]])
148        end context.NR()
149    end
150    context.stoptabulate()
151end
152
153function namespaces.list()
154 -- utilities.formatters.list(data,"namespace")
155    local keys = { "type", "name", "comment", "version", "parent", "definition", "setup", "style" }
156    utilities.formatters.list(data,"namespace",keys)
157end
158
159
160interfaces.implement {
161    name      = "definenamespace",
162    arguments = "2 strings",
163    actions   = namespaces.define
164}
165
166interfaces.implement {
167    name      = "listnamespaces",
168    actions   = namespaces.list
169}
170