1if not modules then modules = { } end modules ['core-env'] = {
2 version = 1.001,
3 comment = "companion to core-env.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
9
10
11
12
13
14local rawset = rawset
15
16local P, C, S, lpegmatch, patterns = lpeg.P, lpeg.C, lpeg.S, lpeg.match, lpeg.patterns
17local gmatch = string.gmatch
18
19local context = context
20local ctxcore = context.core
21
22local texgetintegervalue = token.getinteger
23
24local allocate = utilities.storage.allocate
25local setmetatableindex = table.setmetatableindex
26local setmetatablenewindex = table.setmetatablenewindex
27local setmetatablecall = table.setmetatablecall
28
29local createtoken = token.create
30local expandmacro = token.expandmacro
31
32local isdefined = tokens.isdefined
33
34texmodes = allocate { } tex.modes = texmodes
35texsystemmodes = allocate { } tex.systemmodes = texsystemmodes
36texconstants = allocate { } tex.constants = texconstants
37texconditionals = allocate { } tex.conditionals = texconditionals
38texifs = allocate { } tex.ifs = texifs
39texisdefined = allocate { } tex.isdefined = texisdefined
40
41local implement = interfaces.implement
42
43
44
45
46local cache = tokens.cache
47
48
49
50local commandcodes = tokens.commands
51
52local iftrue <const> = cache["iftrue"].index
53
54local conditioncode <const> = commandcodes.if_test
55local integercode <const> = commandcodes.integer
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
94do
95
96 local modes = { }
97 local systemmodes = { }
98
99 local texiscount = tex.iscount
100 local texgetcount = tex.getcount
101
102 local boolean_value <const> = tokens.values.boolean
103
104 local function nomode()
105 return false
106 end
107
108 setmetatableindex(texmodes, function(t,k)
109 local m = modes[k]
110 if not m then
111 local n = "mode>" .. k
112 local i = texiscount(n)
113 if i then
114 m = i and function() return texgetcount(i) == 1 end or nomode
115 rawset(modes,k,m)
116 else
117 return false
118 end
119 end
120 return m()
121 end)
122
123 setmetatableindex(texsystemmodes, function(t,k)
124 local m = systemmodes[k]
125 if not m then
126 local n = "mode>*" .. k
127 local i = texiscount(n)
128 if i then
129 m = i and function() return texgetcount(i) == 1 end or nomode
130 rawset(systemmodes,k,m)
131 else
132 return false
133 end
134 end
135 return m()
136 end)
137
138 local c_trialtypesetting <const> = tex.iscount("mode>*trialtypesetting")
139
140 context.settrialtypesettingmethod(function()
141
142
143 return texgetcount(c_trialtypesetting) == 1
144 end)
145
146 implement {
147 name = "ifmode",
148 public = true,
149 usage = "condition",
150 arguments = "argument",
151 actions = function(m)
152 return boolean_value, texmodes[m]
153 end
154 }
155
156 implement {
157 name = "ifsystemmode",
158 public = true,
159 usage = "condition",
160 arguments = "argument",
161 actions = function(m)
162 return boolean_value, texsystemmodes[m]
163 end
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 local cache = utilities.parsers.hashes.settings_to_list
178
179 implement {
180 name = "ifmodes",
181 public = true,
182 usage = "condition",
183 arguments = "argument",
184 actions = function(m)
185 local c = cache[m]
186 local n = #c
187 if n > 1 then
188 for i=1,n do
189 if texmodes[c[i]] then
190 return boolean_value, true
191 end
192 end
193 return boolean_value, false
194 else
195 return boolean_value, texmodes[m]
196 end
197 end
198 }
199
200 implement {
201 name = "ifallmodes",
202 public = true,
203 usage = "condition",
204 arguments = "argument",
205 actions = function(m)
206 local c = cache[m]
207 if c then
208 for i=1,#c do
209 if not texmodes[c[i]] then
210 return boolean_value, false
211 end
212 end
213 return boolean_value, true
214 else
215 return boolean_value, texmodes[m]
216 end
217 end
218 }
219
220end
221
222do
223
224 local namespace = interfaces.getnamespace("setup")
225
226 implement {
227 name = "ifsetups",
228 public = true,
229 usage = "condition",
230 arguments = "argument",
231 actions = function(m)
232 return boolean_value, isdefined(namespace .. m)
233 end
234 }
235
236end
237
238
239
240local report = logs.reporter("system")
241
242setmetatablenewindex(texmodes, function(t,k) report("you cannot set the %s named %a this way","mode", k) end)
243setmetatablenewindex(texsystemmodes, function(t,k) report("you cannot set the %s named %a this way","systemmode", k) end)
244setmetatablenewindex(texconstants, function(t,k) report("you cannot set the %s named %a this way","constant", k) end)
245setmetatablenewindex(texconditionals, function(t,k) report("you cannot set the %s named %a this way","conditional",k) end)
246setmetatablenewindex(texifs, function(t,k) end)
247
248
249
250
251setmetatableindex(texconstants, function(t,k)
252 return cache[k].command == integercode and texgetintegervalue(k) or 0
253end)
254
255do
256
257 setmetatableindex(texconditionals, function(t,k)
258 return cache[k].command == integercode and texgetintegervalue(k) == 0
259 end)
260
261
262
263
264 local hash = table.setmetatableindex(function(t,k)
265 local v = createtoken(k)
266 t[k] = v
267 return v
268 end)
269
270
271
272
273
274
275
276
277 local conditionaltrue = createtoken("conditionaltrue")
278 local conditionalfalse = createtoken("conditionalfalse")
279
280 function tex.setconditional(name,v)
281 expandmacro(hash[name],v and conditionaltrue or conditionalfalse)
282 end
283
284end
285
286setmetatableindex(texifs, function(t,k)
287 local c = cache[k]
288 return c.command == conditioncode and c.index == iftrue
289end)
290
291tex.isdefined = isdefined
292
293
294
295
296
297function context.setconditional(name,value)
298 if value then
299 ctxcore.settruevalue(name)
300 else
301 ctxcore.setfalsevalue(name)
302 end
303end
304
305function context.setmode(name,value)
306 if value then
307 ctxcore.setmode(name)
308 else
309 ctxcore.resetmode(name)
310 end
311end
312
313function context.setsystemmode(name,value)
314 if value then
315 ctxcore.setsystemmode(name)
316 else
317 ctxcore.resetsystemmode(name)
318 end
319end
320
321context.modes = texmodes
322context.systemmodes = texsystemmodes
323context.conditionals = texconditionals
324
325
326
327do
328
329 local sep = S("), ")
330 local str = C((1-sep)^1)
331 local tag = P("(") * C((1-S(")" ))^1) * P(")")
332 local arg = P("(") * C((1-S("){"))^1) * P("{") * C((1-P("}"))^0) * P("}") * P(")")
333
334 local pattern = (
335 P("lua") * tag / ctxcore.luasetup
336 + P("xml") * arg / ctxcore.setupwithargument
337 + (P("tex") * tag + str) / ctxcore.texsetup
338 + sep^1
339 )^1
340
341 implement {
342 name = "autosetups",
343 actions = function(str) lpegmatch(pattern,str) end,
344 arguments = "string"
345 }
346
347end
348
349do
350
351
352
353
354
355
356
357
358
359
360
361
362 local getmacro = tokens.getters.macro
363 local getdimensionvalue = tex.getdimensionvalue
364
365
366
367
368
369 local measures = setmetatableindex(function(t,k)
370 local v = getmacro("??measure") .. k
371 t[k] = v
372 return v
373 end)
374
375
376
377
378
379
380
381
382
383
384
385
386
387 function tex.getmeasure(name,asdimen)
388
389 local value = getdimensionvalue(measures[name])
390 if asdimen then
391 return value .. "sp"
392 else
393 return value
394 end
395 end
396
397end
398 |