summaryrefslogtreecommitdiff
path: root/tex/context/base/syst-lua.lua
blob: ef524c339fa9b64f80c51d1033454d8dabe44c59 (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
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 format, find, match, rep = string.format, string.find, string.match, string.rep
local tonumber = tonumber
local S, lpegmatch, lpegtsplitat = lpeg.S, lpeg.match, lpeg.tsplitat

local context = context

commands = commands or { }

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

local firstoftwoarguments  = context.firstoftwoarguments  -- context.constructcsonly("firstoftwoarguments" )
local secondoftwoarguments = context.secondoftwoarguments -- context.constructcsonly("secondoftwoarguments")
local firstofoneargument   = context.firstofoneargument   -- context.constructcsonly("firstofoneargument"  )
local gobbleoneargument    = context.gobbleoneargument    -- context.constructcsonly("gobbleoneargument"   )

-- contextsprint(prtcatcodes,[[\ui_fo]]) -- firstofonearguments
-- contextsprint(prtcatcodes,[[\ui_go]]) -- gobbleonearguments
-- contextsprint(prtcatcodes,[[\ui_ft]]) -- firstoftwoarguments
-- contextsprint(prtcatcodes,[[\ui_st]]) -- secondoftwoarguments

function commands.doifelse(b)
    if b then
        firstoftwoarguments()
    else
        secondoftwoarguments()
    end
end

function commands.doif(b)
    if b then
        firstofoneargument()
    else
        gobbleoneargument()
    end
end

function commands.doifnot(b)
    if b then
        gobbleoneargument()
    else
        firstofoneargument()
    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
        firstoftwoarguments()
    else
        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
                firstoftwoarguments()
                return
            end
        end
    end
    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
            firstoftwoarguments()
            return
        end
    end
    secondoftwoarguments()
end

local pattern = lpeg.patterns.validdimen

function commands.doifdimenstringelse(str)
    if lpegmatch(pattern,str) then
        firstoftwoarguments()
    else
        secondoftwoarguments()
    end
end

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

function commands.ntimes(str,n)
    context(rep(str,n or 1))
end