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}{autosuffix} 15 16\unprotect 17 18\permanent\def\expdoifelse#1#2{{#1}{#2}} 19\permanent\def\expdoif #1#2{ {#1}{#2}} 20\permanent\def\expdoifnot #1#2{ {#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\permanent\def\expandeddoifelse#1#2{\iftok{#1}{#2}\expandafter\firstoftwoarguments\else\expandafter\secondoftwoarguments\fi} 28\permanent\def\expandeddoif #1#2{\iftok{#1}{#2}\expandafter\firstofoneargument \else\expandafter\gobbleoneargument \fi} 29\permanent\def\expandeddoifnot #1#2{\iftok{#1}{#2}\expandafter\gobbleoneargument \else\expandafter\firstofoneargument \fi} 30 31% \testfeatureonce{100000}{\doifelse{hello world}{here i am}{}} % 0.3 32% \testfeatureonce{100000}{\expandabledoifelse{hello world}{here i am}{}} % 1.5 33 34\permanent\def\expdoifelsecommon#1#2{{#1}{#2}} 35\permanent\def\expdoifelseinset #1#2{ {#1}{#2}} 36 37\aliased\let\expdoifcommonelse\expdoifelsecommon 38\aliased\let\expdoifinsetelse \expdoifelseinset 39 40%D We define these here, just in case. This permits \typefile {self} otherwise 41%D nested b|/|e sep problems. 42 43\immutable\def\luastringsep{===} 44 45\immutable\edef\!!bs{[\luastringsep[} 46\immutable\edef\!!es{]\luastringsep]} 47 48\pushoverloadmode 49 50\permanent\protected\def\writestatus#1#2{{#1}{#2}} 51 52\popoverloadmode 53 54%D We can omit the tex.ctxcatcodes here as nowadays we seldom change the regime at 55%D the \TEX\ end. Beware: the expression is compiled each time so it is not that 56%D efficient. 57 58% \def\luaexpr #1{\ctxlua {context (tostring(#1))}} % more efficient: 59% \permanent\def\luaexpr #1{\ctxlua {tex.print(tostring(#1))}} % no use is shortcutting has to be compiled 60\tolerant\permanent\edef\luaexpr[#1]#:#2{\noexpand\noexpand\ifempty{#1}\noexpand\else"\letterpercent #1",\noexpand\fi#2} 61 62%D But as we only use write 16 we could as well do all in \LUA\ and ignore the rest. 63%D Okay, we still can do writes here but only when not blocked. 64 65\aliased\let\syst_write_execute % always {...} 66 67%D There is a probably rather useless \type {\expression} command that can mix \LUA\ 68%D numeric expressions with \TEX\ commands (for as far that makes sense) that works 69%D like this: 70%D 71%D \starttyping 72%D \mathchardef\foooo 1234 73%D \def \mymacro {123.45} 74%D \scratchdimen 123pt 75%D \scratchskip 123pt 76%D \scratchtoks {34} 77%D 78%D \testfeatureonce{10000}{\edef\foo{\expression 79%D round( 80%D 12.3 * \scratchdimen 81%D - (12.3 * \scratchskip) / 2 82%D + 2 ^ 3 83%D * ( 84%D - sqrt(12) 85%D - 10pt 86%D - \foooo 87%D + \luatexversion 88%D - \mymacro 89%D + \scratchtoks 90%D + \the\scratchcounter 91%D + \number\scratchcounter 92%D ) 93%D ) 94%D \relax}} 95%D 96%D \expression string.format("\letterpercent f",\elapsedtime/50000) \relax 97%D \stoptyping 98%D 99%D The final \type {\relax} is mandate because we also accept spaces. Of course we 100%D could have avoided it by using outer parenthesis but this is cleaner anyway. 101%D 102%D The \LMTX\ version supports te \type {xmath} and \type {xcomplex} functions and 103%D therefore one can have expressions that don't work in \MKIV. 104 105% maybe: \let\texexpr\expression 106 107\protect \endinput 108 |