summaryrefslogtreecommitdiff
path: root/tex/context/base/syst-lua.lua
blob: 9466ed94b1c51462fba0142e41ddc6d8d4244adc (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
if not modules then modules = { } end modules ['syst-lua'] = {
    version   = 1.001,
    comment   = "companion to syst-lua.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local find, match = string.find, string.match
local tonumber = tonumber
local S, lpegmatch, lpegtsplitat = lpeg.S, lpeg.match, lpeg.tsplitat

commands          = commands or { }
local commands    = commands

local implement   = interfaces.implement

local context     = context
local csprint     = context.sprint

local prtcatcodes = tex.prtcatcodes

function commands.writestatus(...) logs.status(...) end -- overloaded later

local ctx_firstoftwoarguments  = context.firstoftwoarguments  -- context.constructcsonly("firstoftwoarguments" )
local ctx_secondoftwoarguments = context.secondoftwoarguments -- context.constructcsonly("secondoftwoarguments")
local ctx_firstofoneargument   = context.firstofoneargument   -- context.constructcsonly("firstofoneargument"  )
local ctx_gobbleoneargument    = context.gobbleoneargument    -- context.constructcsonly("gobbleoneargument"   )

-- contextsprint(prtcatcodes,[[\ui_fo]]) -- ctx_firstofonearguments
-- contextsprint(prtcatcodes,[[\ui_go]]) -- ctx_gobbleonearguments
-- contextsprint(prtcatcodes,[[\ui_ft]]) -- ctx_firstoftwoarguments
-- contextsprint(prtcatcodes,[[\ui_st]]) -- ctx_secondoftwoarguments

function commands.doifelse(b)
    if b then
        ctx_firstoftwoarguments()
-- csprint(prtcatcodes,[[\ui_ft]]) -- ctx_firstoftwoarguments
    else
        ctx_secondoftwoarguments()
-- csprint(prtcatcodes,[[\ui_st]]) -- ctx_secondoftwoarguments
    end
end

function commands.doifelsesomething(b)
    if b and b ~= "" then
        ctx_firstoftwoarguments()
-- csprint(prtcatcodes,[[\ui_ft]]) -- ctx_firstoftwoarguments
    else
        ctx_secondoftwoarguments()
-- csprint(prtcatcodes,[[\ui_st]]) -- ctx_secondoftwoarguments
    end
end

function commands.doif(b)
    if b then
        ctx_firstofoneargument()
-- context.__flushdirect(prtcatcodes,[[\ui_fo]]) -- ctx_firstofonearguments
    else
        ctx_gobbleoneargument()
-- context.__flushdirect(prtcatcodes,[[\ui_go]]) -- ctx_gobbleonearguments
    end
end

function commands.doifsomething(b)
    if b and b ~= "" then
        ctx_firstofoneargument()
-- context.__flushdirect(prtcatcodes,[[\ui_fo]]) -- ctx_firstofonearguments
    else
        ctx_gobbleoneargument()
-- context.__flushdirect(prtcatcodes,[[\ui_go]]) -- ctx_gobbleonearguments
    end
end

function commands.doifnot(b)
    if b then
        ctx_gobbleoneargument()
-- csprint(prtcatcodes,[[\ui_go]]) -- ctx_gobbleonearguments
    else
        ctx_firstofoneargument()
-- csprint(prtcatcodes,[[\ui_fo]]) -- ctx_firstofonearguments
    end
end

function commands.doifnotthing(b)
    if b and b ~= "" then
        ctx_gobbleoneargument()
-- csprint(prtcatcodes,[[\ui_go]]) -- ctx_gobbleonearguments
    else
        ctx_firstofoneargument()
-- csprint(prtcatcodes,[[\ui_fo]]) -- ctx_firstofonearguments
    end
end

commands.testcase = commands.doifelse -- obsolete

function commands.boolcase(b)
    context(b and 1 or 0)
end

function commands.doifelsespaces(str)
    if find(str,"^ +$") then
        ctx_firstoftwoarguments()
    else
        ctx_secondoftwoarguments()
    end
end

local s = lpegtsplitat(",")
local h = { }

function commands.doifcommonelse(a,b) -- often the same test
    local ha = h[a]
    local hb = h[b]
    if not ha then
        ha = lpegmatch(s,a)
        h[a] = ha
    end
    if not hb then
        hb = lpegmatch(s,b)
        h[b] = hb
    end
    local na = #ha
    local nb = #hb
    for i=1,na do
        for j=1,nb do
            if ha[i] == hb[j] then
                ctx_firstoftwoarguments()
                return
            end
        end
    end
    ctx_secondoftwoarguments()
end

function commands.doifinsetelse(a,b)
    local hb = h[b]
    if not hb then hb = lpegmatch(s,b) h[b] = hb end
    for i=1,#hb do
        if a == hb[i] then
            ctx_firstoftwoarguments()
            return
        end
    end
    ctx_secondoftwoarguments()
end

local pattern = lpeg.patterns.validdimen

function commands.doifdimenstringelse(str)
    if lpegmatch(pattern,str) then
        ctx_firstoftwoarguments()
    else
        ctx_secondoftwoarguments()
    end
end

function commands.firstinset(str)
    local first = match(str,"^([^,]+),")
    context(first or str)
end

implement {
    name      = "ntimes",
    actions   = { string.rep, context },
    arguments = { "string", "integer" }
}

function commands.execute(str)
    os.execute(str) -- wrapped in sandbox
end

-- function commands.write(n,str)
--     if n == 18 then
--         os.execute(str)
--     elseif n == 16 then
--         -- immediate
--         logs.report(str)
--     else
--         -- at the tex end we can still drop the write / also delayed vs immediate
--         context.writeviatex(n,str)
--     end
-- end