1
2
3
4
5
6
7
8
9
10
11
12
13
14\writestatus{loading}{ConTeXt Lua Macros Initialization}
15
16\unprotect
17
18
19
20
21
22
23
24\ifdefined\setnaturalcatcodes \else \let\setnaturalcatcodes\relax \fi
25\ifdefined\obeylualines \else \let\obeylualines \relax \fi
26\ifdefined\obeyluatokens \else \let\obeyluatokens \relax \fi
27
28
29
30\let\stoplua \relax
31\let\stopluacode\relax
32
33
34
35
36
37\protected\def\startlua % \stoplua
38 {\begingroup
39 \obeylualines
40 \luat_start_lua_indeed}
41
42\def\luat_start_lua_indeed#1\stoplua
43 {\normalexpanded{\endgroup\noexpand\directlua{#1}}}
44
45\protected\def\startluacode % \stopluacode
46 {\begingroup
47 \obeylualines
48 \obeyluatokens
49 \luat_start_lua_code_indeed}
50
51\def\luat_start_lua_code_indeed#1\stopluacode
52 {\normalexpanded{\endgroup\noexpand\directlua{#1}}}
53
54
55
56\ifdefined\obeylines \else \let\obeylines \relax \fi
57\ifdefined\obeyedline \else \let\obeyedline \relax \fi
58\ifdefined\obeyspaces \else \let\obeyspaces \relax \fi
59\ifdefined\obeyedspace \else \let\obeyedspace \relax \fi
60
61\let\obeylualines\relax
62
63\newtoks\everyluacode
64
65\edef\lua_letter_backslash{\string\\}
66\edef\lua_letter_bar {\string\|} \edef\lua_letter_dash {\string\-}
67\edef\lua_letter_lparent {\string\(} \edef\lua_letter_rparent {\string\)}
68\edef\lua_letter_lbrace {\string\{} \edef\lua_letter_rbrace {\string\}}
69\edef\lua_letter_squote {\string\'} \edef\lua_letter_dquote {\string\"}
70\edef\lua_letter_n {\string\n} \edef\lua_letter_r {\string\r}
71\edef\lua_letter_f {\string\f} \edef\lua_letter_t {\string\t}
72\edef\lua_letter_a {\string\a} \edef\lua_letter_b {\string\b}
73\edef\lua_letter_v {\string\v} \edef\lua_letter_s {\string\s}
74\edef\lua_letter_one {\string\1} \edef\lua_letter_two {\string\2}
75\edef\lua_letter_three {\string\3} \edef\lua_letter_four {\string\4}
76\edef\lua_letter_five {\string\5} \edef\lua_letter_six {\string\6}
77\edef\lua_letter_seven {\string\7} \edef\lua_letter_eight {\string\8}
78\edef\lua_letter_nine {\string\9} \edef\lua_letter_zero {\string\0}
79
80\pushoverloadmode
81 \let\-\explicitdiscretionary
82\popoverloadmode
83
84\everyluacode {
85 \enforced\let\\\lua_letter_backslash
86 \enforced\let\|\lua_letter_bar \enforced\let\-\lua_letter_dash
87 \enforced\let\(\lua_letter_lparent \enforced\let\)\lua_letter_rparent
88 \enforced\let\{\lua_letter_lbrace \enforced\let\}\lua_letter_rbrace
89 \enforced\let\'\lua_letter_squote \enforced\let\"\lua_letter_dquote
90 \enforced\let\n\lua_letter_n \enforced\let\r\lua_letter_r
91 \enforced\let\f\lua_letter_f \enforced\let\t\lua_letter_t
92 \enforced\let\a\lua_letter_a \enforced\let\b\lua_letter_b
93 \enforced\let\v\lua_letter_v \enforced\let\s\lua_letter_s
94 \enforced\let\1\lua_letter_one \enforced\let\2\lua_letter_two
95 \enforced\let\3\lua_letter_three \enforced\let\4\lua_letter_four
96 \enforced\let\5\lua_letter_five \enforced\let\6\lua_letter_six
97 \enforced\let\7\lua_letter_seven \enforced\let\8\lua_letter_eight
98 \enforced\let\9\lua_letter_nine \enforced\let\0\lua_letter_zero
99}
100
101\protected\def\obeyluatokens
102 {\setcatcodetable\luacatcodes
103 \the\everyluacode}
104
105\edef\luamajorversion{\ctxwrite{LUAMINORVERSION}}
106\edef\luaminorversion{\ctxwrite{LUAMAJORVERSION}}
107
108
109
110
111
112
113
114
115
116\def\luastringsep{===}
117
118\edef\!!bs{[\luastringsep[}
119\edef\!!es{]\luastringsep]}
120
121
122
123
124
125
126
127
128\protected\def\setdocumentfilename #1#2{\clf_setdocumentfilename\numexpr#1\relax{#2}}
129\protected\def\setdocumentargument #1#2{\clf_setdocumentargument{#1}{#2}}
130\protected\def\setdocumentargumentdefault#1#2{\clf_setdocumentdefaultargument{#1}{#2}}
131 \def\getdocumentfilename #1{\clf_getdocumentfilename\numexpr#1\relax}
132 \def\getdocumentargument #1{\clf_getdocumentargument{#1}{}}
133 \def\getdocumentargumentdefault#1#2{\clf_getdocumentargument{#1}{#2}}
134
135
136
137\def\doifelsedocumentargument #1{\doifelsesomething{\clf_getdocumentargument{#1}}}
138\def\doifdocumentargument #1{\doifsomething {\clf_getdocumentargument{#1}}}
139\def\doifnotdocumentargument #1{\doifnothing {\clf_getdocumentargument{#1}}}
140\def\doifelsedocumentfilename #1{\doifelsesomething{\clf_getdocumentfilename\numexpr#1\relax}}
141\def\doifdocumentfilename #1{\doifsomething {\clf_getdocumentfilename\numexpr#1\relax}}
142\def\doifnotdocumentfilename #1{\doifnothing {\clf_getdocumentfilename\numexpr#1\relax}}
143
144\let\doifdocumentargumentelse\doifelsedocumentargument
145\let\doifdocumentfilenameelse\doifelsedocumentfilename
146
147
148
149\def\luaexpanded#1{\luaescapestring\expandafter{\normalexpanded{#1}}}
150
151
152
153\protected\def\startluaparameterset[#1]%
154 {\begingroup
155 \obeylualines
156 \obeyluatokens
157 \luat_start_lua_parameter_set{#1}}
158
159\def\luat_start_lua_parameter_set#1#2\stopluaparameterset
160 {\ctxlua{parametersets["#1"]={#2}}
161 \endgroup}
162
163\let\stopluaparameterset\relax
164
165\def\luaparameterset#1#2{\ctxlua{parametersets["#1"]={#2} context("#1")}}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184\def\luaconditional#1{\ifcase#1tru\else fals\fi e}
185
186
187
188
189
190
191
192\protected\def\ctxluacode
193 {\begingroup
194 \obeylualines
195 \obeyluatokens
196 \catcode\leftbraceasciicode \plusone
197 \catcode\rightbraceasciicode\plustwo
198 \afterassignment\luat_lua_code
199 \scratchtoks=}
200
201
202
203
204\def\luat_lua_code
205 {\normalexpanded{\endgroup\noexpand\directlua\expandafter{\the\scratchtoks}}}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221\installsystemnamespace{ctxfunction}
222
223\protected\def\startctxfunctiondefinition #1 %
224 {\begingroup \obeylualines \obeyluatokens \luat_start_lua_function_definition_indeed{#1}}
225
226\installsystemnamespace{luafunction}
227
228\def\luat_start_lua_function_definition_indeed#1#2\stopctxfunctiondefinition
229 {\endgroup
230 \expandafter\chardef\csname\??luafunction#1\endcsname\ctxcommand{ctxfunction(\!!bs#2\!!es)}\relax
231 \expandafter\edef\csname#1\endcsname{\noexpand\luafunction\csname\??luafunction#1\endcsname}}
232
233\protected\def\setctxluafunction#1#2
234 {\expandafter\chardef\csname\??luafunction#1\endcsname#2\relax
235 \expandafter\edef\csname#1\endcsname{\noexpand\luafunction\csname\??luafunction#1\endcsname}}
236
237\let\stopctxfunctiondefinition\relax
238
239\protected\def\startctxfunction #1 %
240 {\begingroup \obeylualines \obeyluatokens \luat_start_lua_function_indeed{#1}}
241
242\def\luat_start_lua_function_indeed#1#2\stopctxfunction
243 {\endgroup\expandafter\edef\csname\??ctxfunction#1\endcsname{\noexpand\luafunction\ctxcommand{ctxfunction(\!!bs#2\!!es)}\relax}}
244
245\let\stopctxfunction\relax
246
247\def\ctxfunction#1
248 {\csname\??ctxfunction#1\endcsname}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 \protected\def\installctxfunction#1#2
274 {\edef\m_syst_name{\csstring#1}
275 \global\expandafter\normalluadef\csname\m_syst_name\endcsname\ctxcommand{ctxfunction(\!!bs#2\!!es,true)}\relax}
276
277 \protected\def\installctxscanner#1#2
278 {\edef\m_syst_name{\csstring#1}
279 \global\expandafter\normalluadef\csname\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name",\!!bs#2\!!es,true)}\relax}
280
281 \protected\def\installprotectedctxfunction#1#2
282 {\edef\m_syst_name{\csstring#1}
283 \global\protected\expandafter\normalluadef\csname\m_syst_name\endcsname\ctxcommand{ctxfunction(\!!bs#2\!!es,true)}\relax}
284
285 \protected\def\installprotectedctxscanner#1#2
286 {\edef\m_syst_name{\csstring#1}
287 \global\protected\expandafter\normalluadef\csname\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name",\!!bs#2\!!es,true)}\relax}
288
289 \protected\def\resetctxscanner#1
290 {\edef\m_syst_name{\csstring#1}
291 \expandafter\glet\csname\m_syst_name\endcsname\relax}
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351\protect \endinput
352 |