summaryrefslogtreecommitdiff
path: root/tex/context/base/core-env.lua
blob: 8b9ae172b45dd45fd993759558a998165fb3e03a (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
if not modules then modules = { } end modules ['core-env'] = {
    version   = 1.001,
    comment   = "companion to core-env.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- maybe this will move to the context name space although the
-- plurals are unlikely to clash with future tex primitives
--
-- if tex.modes['xxxx'] then .... else .... end

local P, C, S, Cc, lpegmatch, patterns = lpeg.P, lpeg.C, lpeg.S, lpeg.Cc, lpeg.match, lpeg.patterns

local csname_id         = token.csname_id
local create            = token.create
local texcount          = tex.count

local allocate          = utilities.storage.allocate
local setmetatableindex = table.setmetatableindex

local undefined         = csname_id("*undefined*crap*")
local iftrue            = create("iftrue")[2] -- inefficient hack

tex.modes               = allocate { }
tex.systemmodes         = allocate { }
tex.constants           = allocate { }
tex.conditionals        = allocate { }
tex.ifs                 = allocate { }

local modes             = { }
local systemmodes       = { }

setmetatableindex(tex.modes, function(t,k)
    local m = modes[k]
    if m then
        return m()
    else
        local n = "mode>" .. k
        if csname_id(n) == undefined then
            return false
        else
            modes[k] = function() return texcount[n] >= 1 end
            return texcount[n] >= 1
        end
    end
end)

setmetatableindex(tex.systemmodes, function(t,k)
    local m = systemmodes[k]
    if m then
        return m()
    else
        local n = "mode>*" .. k
        if csname_id(n) == undefined then
            return false
        else
            systemmodes[k] = function() return texcount[n] >= 1 end
            return texcount[n] >= 1
        end
    end
end)

setmetatableindex(tex.constants, function(t,k)
    return csname_id(k) ~= undefined and texcount[k] or 0
end)

setmetatableindex(tex.conditionals, function(t,k) -- 0 == true
    return csname_id(k) ~= undefined and texcount[k] == 0
end)

setmetatableindex(tex.ifs, function(t,k)
 -- k = "if" .. k -- better not
    return csname_id(k) ~= undefined and create(k)[2] == iftrue -- inefficient, this create, we need a helper
end)

-- todo : global

function tex.settrue(name)
    texcount[name] = 0
end

function tex.setfalse(name)
    texcount[name] = 1
end

----  arg = P("{") * C(patterns.nested) * P("}") + Cc("")

local sep = S("), ")
local str = C((1-sep)^1)
local tag = P("(") * C((1-S(")" ))^1) * P(")")
local arg = P("(") * C((1-S("){"))^1) * P("{") * C((1-P("}"))^0) * P("}") * P(")")

local pattern = (
     P("lua") * tag        / context.luasetup
  +  P("xml") * arg        / context.setupwithargument -- or xmlw as xmlsetup has swapped arguments
  + (P("tex") * tag + str) / context.texsetup
  +             sep^1
)^1

function commands.autosetups(str)
    lpegmatch(pattern,str)
end

-- new (inefficient)

local lookuptoken = token.lookup

local dimencode   = lookuptoken("scratchdimen"  )[1]
local countcode   = lookuptoken("scratchcounter")[1]
local tokencode   = lookuptoken("scratchtoken"  )[1]
local skipcode    = lookuptoken("scratchskip"   )[1]

local types = {
    [dimencode] = "dimen",
    [countcode] = "count",
    [tokencode] = "token",
    [skipcode ] = "skip",
}

function tex.isdimen(name)
    return lookuptoken(name)[1] == dimencode
end

function tex.iscount(name)
    return lookuptoken(name)[1] == countcode
end

function tex.istoken(name)
    return lookuptoken(name)[1] == tokencode
end

function tex.isskip(name)
    return lookuptoken(name)[1] == skipcode
end

function tex.type(name)
    return types[lookuptoken(name)[1]] or "macro"
end

--  inspect(tex.isdimen("xxxxxxxxxxxxxxx"))
--  inspect(tex.isdimen("textwidth"))