summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/core-env.lua
blob: 583a6ed1e5ecc332ec5d2a28e27e5ea8ec32058e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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 rawset = rawset

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

local context              = context
local ctxcore              = context.core

local texgetcount          = tex.getcount

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

local createtoken          = token.create

texmodes                   = allocate { }  tex.modes        = texmodes
texsystemmodes             = allocate { }  tex.systemmodes  = texsystemmodes
texconstants               = allocate { }  tex.constants    = texconstants
texconditionals            = allocate { }  tex.conditionals = texconditionals
texifs                     = allocate { }  tex.ifs          = texifs
texisdefined               = allocate { }  tex.isdefined    = texisdefined

local modes                = { }
local systemmodes          = { }

-- we could use the built-in tex.is[count|dimen|skip|toks] here but caching
-- at the lua end is not that bad (and we need more anyway)

-- undefined: mode == 0 or cmdname = "undefined_cs"

local cache = setmetatableindex(function(t,k)
    local v = createtoken(k)
    t[k] = v
    return v
end)

-- we can have a modes cache too

local iftrue    = cache["iftrue"].mode
local undefined = cache["*undefined*crap*"].mode -- is this ok?

setmetatableindex(texmodes, function(t,k)
    local m = modes[k]
    if m then
        return m()
    else
        local n = "mode>" .. k
        if cache[n].mode == 0 then
            return false
        else
            rawset(modes,k, function() return texgetcount(n) == 1 end)
            return texgetcount(n) == 1 -- 2 is prevented
        end
    end
end)
setmetatablenewindex(texmodes, function(t,k)
    report_mode("you cannot set the %s named %a this way","mode",k)
end)

setmetatableindex(texsystemmodes, function(t,k)
    local m = systemmodes[k]
    if m then
        return m()
    else
        local n = "mode>*" .. k
        if cache[n].mode == 0 then
            return false
        else
            rawset(systemmodes,k,function() return texgetcount(n) == 1 end)
            return texgetcount(n) == 1 -- 2 is prevented
        end
    end
end)
setmetatablenewindex(texsystemmodes, function(t,k)
    report_mode("you cannot set the %s named %a this way","systemmode",k)
end)

setmetatableindex(texconstants, function(t,k)
    return cache[k].mode ~= 0 and texgetcount(k) or 0
end)
setmetatablenewindex(texconstants, function(t,k)
    report_mode("you cannot set the %s named %a this way","constant",k)
end)

setmetatableindex(texconditionals, function(t,k) -- 0 == true
    return cache[k].mode ~= 0 and texgetcount(k) == 0
end)
setmetatablenewindex(texconditionals, function(t,k)
    report_mode("you cannot set the %s named %a this way","conditional",k)
end)

table.setmetatableindex(texifs, function(t,k)
    return cache[k].mode == iftrue
end)
setmetatablenewindex(texifs, function(t,k)
    -- just ignore
end)

setmetatableindex(texisdefined, function(t,k)
    return k and cache[k].mode ~= 0
end)
setmetatablecall(texisdefined, function(t,k)
    return k and cache[k].mode ~= 0
end)
setmetatablenewindex(texisdefined, function(t,k)
    -- just ignore
end)

local dimencode     = cache["scratchdimen"]    .command
local countcode     = cache["scratchcounter"]  .command
local tokencode     = cache["scratchtoken"]    .command
local skipcode      = cache["scratchskip"]     .command
local muskipcode    = cache["scratchmuskip"]   .command
---- attributecode = cache["scratchattribute"].command

local types = {
    [dimencode]     = "dimen",
    [countcode]     = "count",
    [tokencode]     = "token",
    [skipcode]      = "skip",
    [muskipcode]    = "muskip",
 -- [attributecode] = "attribute",
}

function tex.isdimen(name)
    local hit = cache[name]
    return hit.command == dimencode and hit.index or true
end

function tex.iscount(name)
    local hit = cache[name]
    return hit.command == countcode and hit.index or true
end

function tex.istoken(name)
    local hit = cache[name]
    return hit.command == tokencode and hit.index or true
end

function tex.isskip(name)
    local hit = cache[name]
    return hit.command == skipcode and hit.index or true
end

function tex.ismuskip(name)
    local hit = cache[name]
    return hit.command == muskipcode and hit.index or true
end

function tex.type(name)
    return types[cache[name].command] or "macro"
end

function context.setconditional(name,value)
    if value then
        ctxcore.settruevalue(name)
    else
        ctxcore.setfalsevalue(name)
    end
end

function context.setmode(name,value)
    if value then
        ctxcore.setmode(name)
    else
        ctxcore.resetmode(name)
    end
end

function context.setsystemmode(name,value)
    if value then
        ctxcore.setsystemmode(name)
    else
        ctxcore.resetsystemmode(name)
    end
end

context.modes        = texmodes
context.systemmodes  = texsystemmodes
context.conditionals = texconditionals
-------.constants    = texconstants
-------.ifs          = texifs

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        / ctxcore.luasetup
  +  P("xml") * arg        / ctxcore.setupwithargument -- or xmlw as xmlsetup has swapped arguments
  + (P("tex") * tag + str) / ctxcore.texsetup
  +             sep^1
)^1

interfaces.implement {
    name      = "autosetups",
    actions   = function(str) lpegmatch(pattern,str) end,
    arguments = "string"
}