summaryrefslogtreecommitdiff
path: root/tex/context/base/syst-lua.mkiv
blob: 2a6bcdf6db8545270770f3c65adddf779b23458b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
94
95
96
97
98
99
%D \module
%D   [       file=syst-lua,
%D        version=2008.01.25,
%D          title=\CONTEXT\ System Macros,
%D       subtitle=Helper macros based on \LUA,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\registerctxluafile{syst-lua}{1.001}

\unprotect

\def\expdoifelse#1#2{\ctxcommand{doifelse(\!!bs#1\!!es==\!!bs#2\!!es)}}
\def\expdoif    #1#2{\ctxcommand{doif    (\!!bs#1\!!es==\!!bs#2\!!es)}}
\def\expdoifnot #1#2{\ctxcommand{doifnot (\!!bs#1\!!es==\!!bs#2\!!es)}}

% \testfeatureonce{100000}{\doifelse{hello world}{here i am}{}}           % 0.3
% \testfeatureonce{100000}{\expandabledoifelse{hello world}{here i am}{}} % 1.5

\def\expdoifcommonelse#1#2{\ctxcommand{doifcommonelse("#1","#2")}}
\def\expdoifinsetelse #1#2{\ctxcommand{doifinsetelse("#1","#2")}}

% we define these here, just in case ...

\def\luastringsep{===} % this permits \typefile{self} otherwise nested b/e sep problems

\edef\!!bs{[\luastringsep[}
\edef\!!es{]\luastringsep]}

\unexpanded\def\writestatus#1#2{\ctxcommand{writestatus(\!!bs#1\!!es,\!!bs#2\!!es)}}

% A handy helper:

% \def\luaexpr#1{\ctxlua{context(tostring(#1))}} % more efficient:

% We can omit the tex.ctxcatcodes here as nowadays we seldom
% change the regime at the \TEX\ end:

\def\luaexpr#1{\directlua{tex.print(tostring(#1))}}

% helpers:

\def\ui_fo  #1{#1}
\def\ui_go  #1{}
\def\ui_ft#1#2{#1}
\def\ui_st#1#2{#2}

%D Let's bring this under \LUA\ (and therefore \MKIV\ sandbox) control:

% \setnewconstant\c_syst_write 18
%
% \unexpanded\def\write#1#% so we can handle \immediate
%   {\ifnum#1=\c_syst_write
%      \expandafter\syst_execute
%    \else
%      \normalwrite#1%
%    \fi}
%
% \unexpanded\def\syst_execute#1%
%   {\ctxlua{os.execute(\!!bs#1\!!es)}}

%D But as we only use write 16 we could as well do all in \LUA\
%D and ignore the rest. Okay, we still can do writes here but only
%D when not blocked.

% Nicer would be if we could just disable write 18 and keep os.execute
% which in fact we can do by defining write18 as macro instead of
% primitive ... todo.

% \unexpanded\def\write#1#%
%   {\syst_write{#1}}
%
% \def\syst_write#1#2%
%   {\ctxcommand{write(\number#1,\!!bs\normalunexpanded{#2}\!!es)}}
%
% \unexpanded\def\writeviatex#1#2%
%   {\ifx\normalwrite\relax\else
%      \normalwrite#1{#2}%
%    \fi}

% we have no way yet to pickup \immediate unless we redefine it
% we assume immediate execution

\def\syst_write_execute#1%
  {\ctxcommand{execute(\!!bs#1\!!es)}}

\unexpanded\def\write#1#%
  {\ifnum#1=18
     \expandafter\syst_write_execute
   \else
     \normalwrite#1%
   \fi}

\protect \endinput