core-env.lua /size: 6791 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
core-env
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to core-env.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
-- maybe this will move to the context name space although the
10
-- plurals are unlikely to clash with future tex primitives
11
--
12
-- if tex.modes['xxxx'] then .... else .... end
13 14
local
rawset
=
rawset
15 16
local
P
,
C
,
S
,
lpegmatch
,
patterns
=
lpeg
.
P
,
lpeg
.
C
,
lpeg
.
S
,
lpeg
.
match
,
lpeg
.
patterns
17 18
local
context
=
context
19
local
ctxcore
=
context
.
core
20 21
local
texgetcount
=
tex
.
getcount
22 23
local
allocate
=
utilities
.
storage
.
allocate
24
local
setmetatableindex
=
table
.
setmetatableindex
25
local
setmetatablenewindex
=
table
.
setmetatablenewindex
26
local
setmetatablecall
=
table
.
setmetatablecall
27 28
local
createtoken
=
token
.
create
29
local
isdefined
=
tokens
.
isdefined
30 31
texmodes
=
allocate
{
}
tex
.
modes
=
texmodes
32
texsystemmodes
=
allocate
{
}
tex
.
systemmodes
=
texsystemmodes
33
texconstants
=
allocate
{
}
tex
.
constants
=
texconstants
34
texconditionals
=
allocate
{
}
tex
.
conditionals
=
texconditionals
35
texifs
=
allocate
{
}
tex
.
ifs
=
texifs
36
texisdefined
=
allocate
{
}
tex
.
isdefined
=
texisdefined
37 38
local
modes
=
{
}
39
local
systemmodes
=
{
}
40 41
-- we could use the built-in tex.is[count|dimen|skip|toks] here but caching
42
-- at the lua end is not that bad (and we need more anyway)
43 44
local
cache
=
tokens
.
cache
45 46
-- we can have a modes cache too
47 48
local
iftrue
=
cache
[
"
iftrue
"
]
.
mode
49 50
local
dimencode
=
cache
[
"
scratchdimen
"
]
.
command
-- tokens.commands.assign_dimen
51
local
countcode
=
cache
[
"
scratchcounter
"
]
.
command
-- tokens.commands.assign_int
52
local
tokencode
=
cache
[
"
scratchtoken
"
]
.
command
-- tokens.commands.assign_toks
53
local
skipcode
=
cache
[
"
scratchskip
"
]
.
command
-- tokens.commands.assign_glue
54
local
muskipcode
=
cache
[
"
scratchmuskip
"
]
.
command
-- tokens.commands.assign_mu_glue
55
----- attributecode = cache["scratchattribute"].command -- tokens.commands.assign_attr
56
local
conditioncode
=
cache
[
"
iftrue
"
]
.
command
-- tokens.commands.if_test
57 58
local
types
=
{
59
[
dimencode
]
=
"
dimen
"
,
60
[
countcode
]
=
"
count
"
,
61
[
tokencode
]
=
"
token
"
,
62
[
skipcode
]
=
"
skip
"
,
63
[
muskipcode
]
=
"
muskip
"
,
64
-- [attributecode] = "attribute",
65
[
conditioncode
]
=
"
condition
"
66
}
67 68
setmetatableindex
(
texmodes
,
function
(
t
,
k
)
69
local
m
=
modes
[
k
]
70
if
m
then
71
return
m
(
)
72
elseif
k
then
73
local
n
=
"
mode>
"
.
.
k
74
if
isdefined
(
n
)
then
75
rawset
(
modes
,
k
,
function
(
)
return
texgetcount
(
n
)
=
=
1
end
)
76
return
texgetcount
(
n
)
=
=
1
-- 2 is prevented
77
else
78
return
false
79
end
80
else
81
return
false
82
end
83
end
)
84 85
setmetatablenewindex
(
texmodes
,
function
(
t
,
k
)
86
report_mode
(
"
you cannot set the %s named %a this way
"
,
"
mode
"
,
k
)
87
end
)
88 89
setmetatableindex
(
texsystemmodes
,
function
(
t
,
k
)
90
local
m
=
systemmodes
[
k
]
91
if
m
then
92
return
m
(
)
93
else
94
local
n
=
"
mode>*
"
.
.
k
95
if
isdefined
(
n
)
then
96
rawset
(
systemmodes
,
k
,
function
(
)
return
texgetcount
(
n
)
=
=
1
end
)
97
return
texgetcount
(
n
)
=
=
1
-- 2 is prevented
98
else
99
return
false
100
end
101
end
102
end
)
103
setmetatablenewindex
(
texsystemmodes
,
function
(
t
,
k
)
104
report_mode
(
"
you cannot set the %s named %a this way
"
,
"
systemmode
"
,
k
)
105
end
)
106 107
setmetatableindex
(
texconstants
,
function
(
t
,
k
)
108
return
cache
[
k
]
.
mode
~
=
0
and
texgetcount
(
k
)
or
0
109
end
)
110
setmetatablenewindex
(
texconstants
,
function
(
t
,
k
)
111
report_mode
(
"
you cannot set the %s named %a this way
"
,
"
constant
"
,
k
)
112
end
)
113 114
setmetatableindex
(
texconditionals
,
function
(
t
,
k
)
-- 0 == true
115
return
cache
[
k
]
.
mode
~
=
0
and
texgetcount
(
k
)
=
=
0
116
end
)
117
setmetatablenewindex
(
texconditionals
,
function
(
t
,
k
)
118
report_mode
(
"
you cannot set the %s named %a this way
"
,
"
conditional
"
,
k
)
119
end
)
120 121
table
.
setmetatableindex
(
texifs
,
function
(
t
,
k
)
122
return
cache
[
k
]
.
mode
=
=
iftrue
123
end
)
124
setmetatablenewindex
(
texifs
,
function
(
t
,
k
)
125
-- just ignore
126
end
)
127 128
if
CONTEXTLMTXMODE
>
0
then
129 130
iftrue
=
cache
[
"
iftrue
"
]
.
index
131 132
-- if we really need performance we can have a dedicated cache for each
133
-- kind of variable
134 135
setmetatableindex
(
texconstants
,
function
(
t
,
k
)
136
return
cache
[
k
]
.
command
=
=
countcode
and
texgetcount
(
k
)
or
0
137
end
)
138 139
setmetatableindex
(
texconditionals
,
function
(
t
,
k
)
-- 0 == true
140
return
cache
[
k
]
.
command
=
=
countcode
and
texgetcount
(
k
)
=
=
0
141
end
)
142 143
table
.
setmetatableindex
(
texifs
,
function
(
t
,
k
)
144
local
c
=
cache
[
k
]
145
print
(
k
)
146
inspect
(
c
)
147
return
c
.
command
=
=
conditioncode
and
c
.
index
=
=
iftrue
148
end
)
149 150
end
151 152
tex
.
isdefined
=
isdefined
153 154
function
tex
.
isdimen
(
name
)
155
local
hit
=
cache
[
name
]
156
return
hit
.
command
=
=
dimencode
and
hit
.
index
or
true
157
end
158 159
function
tex
.
iscount
(
name
)
160
local
hit
=
cache
[
name
]
161
return
hit
.
command
=
=
countcode
and
hit
.
index
or
true
162
end
163 164
function
tex
.
istoken
(
name
)
165
local
hit
=
cache
[
name
]
166
return
hit
.
command
=
=
tokencode
and
hit
.
index
or
true
167
end
168 169
function
tex
.
isskip
(
name
)
170
local
hit
=
cache
[
name
]
171
return
hit
.
command
=
=
skipcode
and
hit
.
index
or
true
172
end
173 174
function
tex
.
ismuskip
(
name
)
175
local
hit
=
cache
[
name
]
176
return
hit
.
command
=
=
muskipcode
and
hit
.
index
or
true
177
end
178 179
function
tex
.
type
(
name
)
180
return
types
[
cache
[
name
]
.
command
]
or
"
macro
"
181
end
182 183
function
context
.
setconditional
(
name
,
value
)
184
if
value
then
185
ctxcore
.
settruevalue
(
name
)
186
else
187
ctxcore
.
setfalsevalue
(
name
)
188
end
189
end
190 191
function
context
.
setmode
(
name
,
value
)
192
if
value
then
193
ctxcore
.
setmode
(
name
)
194
else
195
ctxcore
.
resetmode
(
name
)
196
end
197
end
198 199
function
context
.
setsystemmode
(
name
,
value
)
200
if
value
then
201
ctxcore
.
setsystemmode
(
name
)
202
else
203
ctxcore
.
resetsystemmode
(
name
)
204
end
205
end
206 207
context
.
modes
=
texmodes
208
context
.
systemmodes
=
texsystemmodes
209
context
.
conditionals
=
texconditionals
210
-------.constants = texconstants
211
-------.ifs = texifs
212 213
local
sep
=
S
(
"
),
"
)
214
local
str
=
C
(
(
1
-
sep
)
^
1
)
215
local
tag
=
P
(
"
(
"
)
*
C
(
(
1
-
S
(
"
)
"
)
)
^
1
)
*
P
(
"
)
"
)
216
local
arg
=
P
(
"
(
"
)
*
C
(
(
1
-
S
(
"
){
"
)
)
^
1
)
*
P
(
"
{
"
)
*
C
(
(
1
-
P
(
"
}
"
)
)
^
0
)
*
P
(
"
}
"
)
*
P
(
"
)
"
)
217 218
local
pattern
=
(
219
P
(
"
lua
"
)
*
tag
/
ctxcore
.
luasetup
220
+
P
(
"
xml
"
)
*
arg
/
ctxcore
.
setupwithargument
-- or xmlw as xmlsetup has swapped arguments
221
+
(
P
(
"
tex
"
)
*
tag
+
str
)
/
ctxcore
.
texsetup
222
+
sep
^
1
223
)
^
1
224 225
interfaces
.
implement
{
226
name
=
"
autosetups
"
,
227
actions
=
function
(
str
)
lpegmatch
(
pattern
,
str
)
end
,
228
arguments
=
"
string
"
229
}
230