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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
if not modules then modules = { } end modules ['mult-ini'] = {
version = 1.001,
comment = "companion to mult-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local format, gmatch, gsub = string.format, string.gmatch, string.gsub
local lpegmatch = lpeg.match
local serialize = table.serialize
local texsprint = tex.sprint
local report_interface = logs.reporter("interface","initialization")
interfaces = interfaces or { }
interfaces.messages = interfaces.messages or { }
interfaces.constants = interfaces.constants or { }
interfaces.variables = interfaces.variables or { }
interfaces.elements = interfaces.elements or { }
interfaces.formats = interfaces.formats or { }
interfaces.translations = interfaces.translations or { }
storage.register("interfaces/messages", interfaces.messages, "interfaces.messages")
storage.register("interfaces/constants", interfaces.constants, "interfaces.constants")
storage.register("interfaces/variables", interfaces.variables, "interfaces.variables")
storage.register("interfaces/elements", interfaces.elements, "interfaces.elements")
storage.register("interfaces/formats", interfaces.formats, "interfaces.formats")
storage.register("interfaces/translations", interfaces.translations, "interfaces.translations")
interfaces.interfaces = {
"cs", "de", "en", "fr", "it", "nl", "ro", "pe",
}
storage.shared.currentinterface = storage.shared.currentinterface or "en"
storage.shared.currentresponse = storage.shared.currentresponse or "en"
local currentinterface = storage.shared.currentinterface
local currentresponse = storage.shared.currentresponse
local complete = { } interfaces.complete = complete
setmetatable(complete, { __index = function(t,k) -- one access needed to get loaded
report_interface("loading interface definitions from 'mult-def.lua'")
complete = dofile(resolvers.findfile("mult-def.lua"))
report_interface("loading interface messages from 'mult-mes.lua'")
complete.messages = dofile(resolvers.findfile("mult-mes.lua"))
interfaces.complete = complete
return rawget(complete,k)
end } )
local messages = interfaces.messages
local constants = interfaces.constants
local variables = interfaces.variables
local elements = interfaces.elements
local formats = interfaces.formats
local translations = interfaces.translations
local reporters = { } -- just an optimization
local valueiskey = { __index = function(t,k) t[k] = k return k end } -- will be helper
setmetatable(variables, valueiskey)
setmetatable(constants, valueiskey)
setmetatable(elements, valueiskey)
setmetatable(formats, valueiskey)
setmetatable(translations, valueiskey)
setmetatable(messages, { __index = function(t,k) local v = { } ; t[k] = v ; return v end })
setmetatable(reporters, { __index = function(t,k) local v = logs.reporter(k) ; t[k] = v ; return v end })
for category, m in next, messages do
-- We pre-create reporters for already defined messages
-- because otherwise listing is incomplete and we want
-- to use that for checking so delaying makes not much
-- sense there.
local r = reporters[m.title or category]
end
function interfaces.setmessages(category,str)
local m = messages[category]
for k, v in gmatch(str,"(%S+) *: *(.-) *[\n\r]") do
m[k] = gsub(v,"%-%-","%%s")
end
messages[category] = m
end
function interfaces.setmessage(category,tag,message)
local m = messages[category]
m[tag] = gsub(message,"%-%-","%%s")
end
function interfaces.getmessage(category,tag,default)
local m = messages[category]
return (m and m[tag]) or default or "unknown message"
end
function interfaces.doifelsemessage(category,tag)
local m = messages[category]
return commands.testcase(m and m[tag])
end
local messagesplitter = lpeg.splitat(",")
local function makemessage(category,tag,arguments)
local m = messages[category]
m = (m and (m[tag] or m[tostring(tag)])) or format("unknown message, category '%s', tag '%s'",category,tag)
if not m then
return m .. " " .. tag
elseif not arguments then
return m
else
return format(m,lpegmatch(messagesplitter,arguments))
end
end
interfaces.makemessage = makemessage
function interfaces.showmessage(category,tag,arguments)
local m = messages[category]
local t = m and m.title or category
local r = reporters[t]
r(makemessage(category,tag,arguments))
end
function interfaces.setvariable(variable,given)
variables[given] = variable
end
function interfaces.setconstant(constant,given)
constants[given] = constant
end
function interfaces.setelement(element,given)
elements[given] = element
end
-- the real thing:
logs.setmessenger(context.verbatim.ctxreport)
-- status
function commands.writestatus(category,message)
local r = reporters[category]
r(message)
end
-- initialization
function interfaces.setuserinterface(interface,response)
-- texsprint(format("\\input{mult-%s}", interface))
-- texsprint(format("\\input{mult-m%s}", response))
storage.shared.currentinterface, currentinterface = interface, interface
storage.shared.currentresponse, currentresponse = response, response
if environment.initex then
local nofconstants = 0
for given, constant in next, complete.constants do
constant = constant[interface] or constant.en or given
constants[constant] = given -- breedte -> width
texsprint("\\do@sicon{",given,"}{",constant,"}")
nofconstants = nofconstants + 1
end
local nofvariables = 0
for given, variable in next, complete.variables do
variable = variable[interface] or variable.en or given
variables[given] = variable -- ja -> yes
texsprint("\\do@sivar{",given,"}{",variable,"}")
nofvariables = nofvariables + 1
end
local nofelements = 0
for given, element in next, complete.elements do
element = element[interface] or element.en or given
elements[element] = given
texsprint("\\do@siele{",given,"}{",element,"}")
nofelements = nofelements + 1
end
local nofcommands = 0
for given, command in next, complete.commands do
command = command[interface] or command.en or given
if command ~= given then
texsprint("\\do@sicom{",given,"}{",command,"}")
end
nofcommands = nofcommands + 1
end
local nofmessages = 0
for category, message in next, complete.messages.originals do
local m = messages[category]
for tag, set in next, message do
if tag ~= "files" then
m[tag] = set[interface] or set.en -- there are no --'s any longer in the lua file
end
end
nofmessages = nofmessages + 1
end
-- experimental code:
local nofformats = 0
for given, format in next, complete.messages.formats do
formats[given] = format[interface] or format.en or given
nofformats = nofformats + 1
end
local noftranslations = 0
for given, translation in next, complete.messages.translations do
translations[given] = translation[interface] or translation.en or given
noftranslations = noftranslations + 1
end
--
report_interface("definitions: %s constants, %s variables, %s elements, %s commands, %s message groups, % formats, %s translations",
nofconstants,nofvariables,nofelements,nofcommands,nofmessages,nofformats,noftranslations)
end
end
-- it's nicer to have numbers as reference than a hash
interfaces.cachedsetups = interfaces.cachedsetups or { }
interfaces.hashedsetups = interfaces.hashedsetups or { }
storage.register("interfaces/cachedsetups", interfaces.cachedsetups, "interfaces.cachedsetups")
storage.register("interfaces/hashedsetups", interfaces.hashedsetups, "interfaces.hashedsetups")
local cachedsetups = interfaces.cachedsetups
local hashedsetups = interfaces.hashedsetups
function interfaces.cachesetup(t)
local hash = serialize(t)
local done = hashedsetups[hash]
if done then
return cachedsetups[done]
else
done = #cachedsetups + 1
cachedsetups[done] = t
hashedsetups[hash] = done
return t
end
end
function interfaces.is_command(str)
return (str and str ~= "" and token.csname_name(token.create(str)) ~= "") or false -- there will be a proper function for this
end
function interfaces.interfacedcommand(name)
local command = complete.commands[name]
return command and command[currentinterface] or name
end
|