summaryrefslogtreecommitdiff
path: root/tex/context/base/syst-lua.lua
blob: 7c619b6d311e1a68a763902fd9d338dde405fc39 (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
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 texsprint, texprint, texwrite, texiowrite_nl = tex.sprint, tex.print, tex.write, texio.write_nl
local format, find = string.format, string.find
local tonumber = tonumber
local S, Ct, lpegmatch, lpegsplitat = lpeg.S, lpeg.Ct, lpeg.match, lpeg.splitat

local ctxcatcodes = tex.ctxcatcodes

commands = commands or { } -- cs = commands -- shorter, maybe some day, not used now

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

-- todo: use shorter names i.e. less tokenization

local function testcase(b)
    if b then -- looks faster with if than with expression
        texsprint(ctxcatcodes,"\\firstoftwoarguments")
    else
        texsprint(ctxcatcodes,"\\secondoftwoarguments")
    end
end

commands.testcase = testcase
commands.doifelse = testcase

function commands.doif(b)
    if b then
        texsprint(ctxcatcodes,"\\firstofoneargument")
    else
        texsprint(ctxcatcodes,"\\gobbleoneargument")
    end
end

function commands.doifnot(b)
    if b then
        texsprint(ctxcatcodes,"\\gobbleoneargument")
    else
        texsprint(ctxcatcodes,"\\firstofoneargument")
    end
end

function commands.boolcase(b)
    if b then texwrite(1) else texwrite(0) end
end

function commands.doifelsespaces(str)
    return commands.doifelse(find(str,"^ +$"))
end

local s = Ct(lpegsplitat(","))
local h = { }

function commands.doifcommonelse(a,b)
    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
    for i=1,#ha do
        for j=1,#hb do
            if ha[i] == hb[j] then
                return testcase(true)
            end
        end
    end
    return testcase(false)
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
            return testcase(true)
        end
    end
    return testcase(false)
end

local pattern = lpeg.patterns.validdimen

function commands.doifdimenstringelse(str)
    testcase(lpegmatch(pattern,str))
end

local splitter = lpegsplitat(S(". "))

function commands.doifolderversionelse(one,two) -- no checking done
    if not two then
        one, two = environment.version, one
    elseif one == "" then
        one = environment.version
    end
    local y_1, m_1, d_1 = lpeg.match(splitter,one)
    local y_2, m_2, d_2 = lpeg.match(splitter,two)
    commands.testcase ( not (
        (tonumber(y_2) or  0) >= (tonumber(y_1) or 0) and
        (tonumber(m_2) or 99) >= (tonumber(m_1) or 0) and
        (tonumber(d_2) or 99) >= (tonumber(d_1) or 0)
    ) )
end