syst-lua.mkiv /size: 3947 b    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=syst-lua,
3%D        version=2008.01.25,
4%D          title=\CONTEXT\ System Macros,
5%D       subtitle=Helper macros based on \LUA,
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\registerctxluafile{syst-lua}{}
15
16\unprotect
17
18\def\expdoifelse#1#2{\clf_doifelsesame{#1}{#2}}
19\def\expdoif    #1#2{\clf_doifsame    {#1}{#2}}
20\def\expdoifnot #1#2{\clf_doifnotsame {#1}{#2}}
21
22%D Here is variant using a brainwave of the 12\high{+} hour \quotation {Long Road
23%D Out of Eden}\footnote {Eden being Backo\TeX\ 2018, where the virtues of the \type
24%D {\expanded} primitive were mentioned in talks.} trip. For now I don't really see
25%D other useful applications.
26
27\def\expandeddoifelse#1#2%
28  {\immediateassignment\edef\m_syst_string_one{#1}%
29   \immediateassignment\edef\m_syst_string_two{#2}%
30   \ifx\m_syst_string_one\m_syst_string_two
31     \expandafter\firstoftwoarguments
32   \else
33     \expandafter\secondoftwoarguments
34   \fi}
35
36\def\expandeddoif#1#2%
37  {\immediateassignment\edef\m_syst_string_one{#1}%
38   \immediateassignment\edef\m_syst_string_two{#2}%
39   \ifx\m_syst_string_one\m_syst_string_two
40     \expandafter\firstofoneargument
41   \else
42     \expandafter\gobbleoneargument
43   \fi}
44
45\def\expandeddoifnot#1#2%
46  {\immediateassignment\edef\m_syst_string_one{#1}%
47   \immediateassignment\edef\m_syst_string_two{#2}%
48   \ifx\m_syst_string_one\m_syst_string_two
49     \expandafter\gobbleoneargument
50   \else
51     \expandafter\firstofoneargument
52   \fi}
53
54% \testfeatureonce{100000}{\doifelse{hello world}{here i am}{}}           % 0.3
55% \testfeatureonce{100000}{\expandabledoifelse{hello world}{here i am}{}} % 1.5
56
57\def\expdoifelsecommon#1#2{\clf_doifelsecommon{#1}{#2}}
58\def\expdoifelseinset #1#2{\clf_doifelseinset {#1}{#2}}
59
60\let\expdoifcommonelse\expdoifelsecommon
61\let\expdoifinsetelse \expdoifelseinset
62
63%D We define these here, just in case. This permits \typefile {self} otherwise
64%D nested b|/|e sep problems.
65
66\def\luastringsep{===}
67
68\edef\!!bs{[\luastringsep[}
69\edef\!!es{]\luastringsep]}
70
71\protected\def\writestatus#1#2{\clf_writestatus{#1}{#2}}
72
73%D We can omit the tex.ctxcatcodes here as nowadays we seldom change the regime at
74%D the \TEX\ end:
75
76%def\luaexpr#1{\ctxlua{context  (tostring(#1))}} % more efficient:
77\def\luaexpr#1{\ctxlua{tex.print(tostring(#1))}} % no use in shortcutting has to be compiled
78
79%D But as we only use write 16 we could as well do all in \LUA\ and ignore the rest.
80%D Okay, we still can do writes here but only when not blocked.
81
82\let\syst_write_execute\clf_execute % always {...}
83
84%D There is a probably rather useless \type {\expression} command that can mix \LUA\ numeric
85%D expressions with \TEX\ commands (for as far that makes sense) that works like this:
86%D
87%D \starttyping
88%D \mathchardef\foooo   1234
89%D \def        \mymacro {123.45}
90%D \scratchdimen 123pt
91%D \scratchskip  123pt
92%D \scratchtoks  {34}
93%D
94%D \testfeatureonce{10000}{\edef\foo{\expression
95%D     round(
96%D         12.3 * \scratchdimen
97%D       - (12.3 * \scratchskip) / 2
98%D       + 2 ^ 3
99%D       * (
100%D           - sqrt(12)
101%D           - 10pt
102%D           - \foooo
103%D           + \luatexversion
104%D           - \mymacro
105%D           + \scratchtoks
106%D           + \the\scratchcounter
107%D           + \number\scratchcounter
108%D         )
109%D     )
110%D \relax}}
111%D
112%D \expression string.format("\letterpercent f",\elapsedtime/50000) \relax
113%D \stoptyping
114%D
115%D The final \type {\relax} is mandate because we also accept spaces. Of course we
116%D could have avoided it by using outer parenthesis but this is cleaner anyway.
117%D
118%D The \MKIV\ version is a bit more limited than the \LMTX\ version because it only
119%D supports the standard math functions.
120
121% maybe: \let\texexpr\expression
122
123\protect \endinput
124