luat-ini.mkiv /size: 14 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=luat-ini,
3%D        version=2005.08.11,
4%D          title=\CONTEXT\ Lua Macros,
5%D       subtitle=Initialization,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Lua Macros / Initialization}
15
16\unprotect
17
18%D Loading lua code can be done using \type {startup.lua}. The following
19%D method uses the \TEX\ input file locator of kpse. At least we need to
20%D use that way of loading when we haven't yet define our own code, which
21%D we keep outside the format. We will keep code outside \TEX\ files as
22%D much as possible.
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%D A few more goodies:
29
30\let\stoplua    \relax % tex catcodes
31\let\stopluacode\relax % lua catcodes
32
33% It might makes sense to have a \type {\directelua} so that we can avoid
34% the \type {\normalexpanded} around \type {\directlua}. Something to discuss
35% in the team.
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}}} % \zerocount is default
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}}} % \zerocount is default
53
54%D Some delayed definitions:
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 {% \appendtoks
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} % \to \everyluacode
100
101\protected\def\obeyluatokens
102  {\setcatcodetable\luacatcodes
103   \the\everyluacode}
104
105\edef\luamajorversion{\ctxwrite{LUAMINORVERSION}}
106\edef\luaminorversion{\ctxwrite{LUAMAJORVERSION}}
107
108%D We need a way to pass strings safely to \LUA\ without the
109%D need for tricky escaping. Compare:
110%D
111%D \starttyping
112%D \ctxlua {something("anything tricky can go here")}
113%D \ctxlua {something([\luastringsep[anything tricky can go here]\luastringsep])}
114%D \stoptyping
115
116\def\luastringsep{===} % this permits \typefile{self} otherwise nested b/e sep problems
117
118\edef\!!bs{[\luastringsep[}
119\edef\!!es{]\luastringsep]}
120
121%D We have a the following available as primitive so there is no need
122%D for it:
123%D
124%D \starttyping
125%D \edef\luaescapestring#1{\!!bs#1\!!es}
126%D \stoptyping
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% seldom used so no need for speedy variants:
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%D A handy helper:
148
149\def\luaexpanded#1{\luaescapestring\expandafter{\normalexpanded{#1}}}
150
151%D Experimental:
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% todo: \mergeparameterset
168
169% usage:
170%
171% \startluaparameterset [u3d:myset:display:1]
172%     toolbar=false,
173%     tree=true
174% \stopluaparameterset
175%
176% option=u3d:myset:display:1
177%
178% or:
179%
180% option=\luaparameterset{u3d:myset:display:1}{toolbar=false,tree=true}
181
182%D A Handy helper:
183
184\def\luaconditional#1{\ifcase#1tru\else fals\fi e}
185
186%D Goodie:
187%D
188%D \starttyping
189%D \ctxluacode{context("%0.5f",1/3)}
190%D \stoptyping
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% Hm, are we sure that the \* commands work out okay here? We could probably
202% use \setcatcodetable\luacatcodes instead of \obeyluatokens now.
203
204\def\luat_lua_code
205  {\normalexpanded{\endgroup\noexpand\directlua\expandafter{\the\scratchtoks}}} % \zerocount is default
206
207% \startctxfunction MyFunctionA
208%     context(" A1 ")
209% \stopctxfunction
210%
211% \startctxfunctiondefinition MyFunctionB
212%     context(" B2 ")
213% \stopctxfunctiondefinition
214%
215% \starttext
216%     \dorecurse{10000}{\ctxfunction{MyFunctionA}} \page
217%     \dorecurse{10000}{\MyFunctionB} \page
218%     \dorecurse{10000}{\ctxlua{context(" C3 ")}} \page
219% \stoptext
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% experiment
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% In theory this is faster due to the call not being wrapped in a function but in
251% practice the speedup can't be noticed. The actions called for often have lots of
252% lookups so an extra one doesn't matter much. The kind of calls differs a lot per
253% document and often there are other ways to optimize a style. For instance we can
254% gain a lot when defining a font, but when a frozen definition is used that gain
255% gets completely lost. For some calls (take list writers) it can get worse if only
256% because readability gets worse and passing is already efficient due to selective
257% flushing, while with the token scanners one has to scan all of them.
258
259% \startctxfunctiondefinition foo commands.foo() \stopctxfunctiondefinition
260%
261% \installctxfunction\foo{commands.foo}
262
263% This is a forward definition:
264
265% \def\checkedstrippedcsname#1% this permits \strippedcsname{\xxx} and \strippedcsname{xxx}
266%   {\expandafter\syst_helpers_checked_stripped_csname\string#1}
267%
268% \def\syst_helpers_checked_stripped_csname#1%
269%   {\if\noexpand#1\letterbackslash\else#1\fi}
270
271% \ifdefined\normalluadef
272
273    \protected\def\installctxfunction#1#2% expandable
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% expandable
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% protected
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% protected
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  % \let\installctxfunctioncall         \installctxfunction
294  % \let\installctxscannercall          \installctxscanner
295  % \let\installprotectedctxfunctioncall\installprotectedctxfunction
296  % \let\installprotectedctxscannercall \installprotectedctxscanner
297
298% \else
299%
300%     \ifdefined\luafunctioncall \else
301%         \protected\def\luafunctioncall{\luafunction}
302%     \fi
303%
304%     \protected\def\installctxfunction#1#2% expandable
305%       {\edef\m_syst_name{\csstring#1}%
306%        \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxfunction("#2",true)}\relax
307%        \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunction\csname\??luafunction\m_syst_name\endcsname}}
308%
309%     \protected\def\installctxscanner#1#2% expandable
310%       {\edef\m_syst_name{\csstring#1}%
311%        \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name","#2",true)}\relax
312%        \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunction\csname\??luafunction\m_syst_name\endcsname}}
313%
314%     \protected\def\installprotectedctxfunction#1#2% protected
315%       {\edef\m_syst_name{\csstring#1}%
316%        \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxfunction("#2",true)}\relax
317%        \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
318%
319%     \protected\def\installprotectedctxscanner#1#2% protected
320%       {\edef\m_syst_name{\csstring#1}%
321%        \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name","#2",true)}\relax
322%        \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
323%
324%     \protected\def\resetctxscanner#1%
325%       {\edef\m_syst_name{\csstring#1}%
326%        \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\zerocount
327%        \expandafter\glet\csname\m_syst_name\endcsname\relax}
328%
329%   % \protected\def\installctxfunctioncall#1#2%
330%   %   {\edef\m_syst_name{\csstring#1}%
331%   %    \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxfunction("#2",true)}\relax
332%   %    \expandafter\xdef\csname\m_syst_name\endcsname{\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
333%   %
334%   % \protected\def\installctxscannercall#1#2%
335%   %   {\edef\m_syst_name{\csstring#1}%
336%   %    \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name","#2",true)}\relax
337%   %    \expandafter\xdef\csname\m_syst_name\endcsname{\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
338%   %
339%   % \protected\def\installprotectedctxfunctioncall#1#2%
340%   %   {\edef\m_syst_name{\csstring#1}%
341%   %    \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxfunction("#2",true)}\relax
342%   %    \protected\expandafter\xdef\csname\m_syst_name\endcsname{\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
343%   %
344%   % \protected\def\installprotectedctxscannercall#1#2%
345%   %   {\edef\m_syst_name{\csstring#1}%
346%   %    \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name","#2",true)}\relax
347%   %    \protected\expandafter\xdef\csname\m_syst_name\endcsname{\luafunctioncall\csname\??luafunction\m_syst_name\endcsname}}
348%
349% \fi
350
351\protect \endinput
352