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

-- slower than lpeg:
--
-- utfmatch(str,"(.?)(.*)$")
-- utf.sub(str,1,1)

local tonumber = tonumber
local format = string.format
local utfsub = utf.sub
local P, S, R, C, Cc, Cs, Carg, lpegmatch = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.Carg, lpeg.match
local todimen = number.todimen

local commands          = commands
local context           = context

local setcatcode        = tex.setcatcode

local utf8character     = lpeg.patterns.utf8character
local settings_to_array = utilities.parsers.settings_to_array

local setvalue          = context.setvalue

local pattern           = C(utf8character^-1) * C(P(1)^0)

function commands.getfirstcharacter(str)
    local first, rest = lpegmatch(pattern,str)
    setvalue("firstcharacter",first)
    setvalue("remainingcharacters",rest)
end

function commands.thefirstcharacter(str)
    local first, rest = lpegmatch(pattern,str)
    context(first)
end
function commands.theremainingcharacters(str)
    local first, rest = lpegmatch(pattern,str)
    context(rest)
end

local pattern = C(utf8character^-1)

function commands.doiffirstcharelse(chr,str)
    commands.doifelse(lpegmatch(pattern,str) == chr)
end

function commands.getsubstring(str,first,last)
    context(utfsub(str,tonumber(first),tonumber(last)))
end

-- function commands.addtocommalist(list,item)
--     if list == "" then
--         context(item)
--     else
--         context("%s,%s",list,item) -- using tex.print is some 10% faster
--     end
-- end
--
-- function commands.removefromcommalist(list,item)
--     if list == "" then
--         context(item)
--     else
--         -- okay, using a proper lpeg is probably faster
--         -- we could also check for #l = 1
--         local l = settings_to_array(list)
--         local t, n = { }
--         for i=1,#l do
--             if l[i] ~= item then
--                 n = n + 1
--                 t[n] = item
--             end
--         end
--         if n == 0 then
--             context(item)
--         else
--             context(concat(list,","))
--         end
--     end
-- end

local pattern = (C((1-P("%"))^1) * Carg(1)) /function(n,d) return format("%.0fsp",d * tonumber(n)/100) end * P("%") * P(-1) -- .0 ?

-- commands.percentageof("10%",65536*10)

function commands.percentageof(str,dim)
    context(lpegmatch(pattern,str,1,dim) or str)
end

-- \gdef\setpercentdimen#1#2%
--   {#1=\ctxcommand{percentageof("#2",\number#1)}\relax}

local spaces    = P(" ")^0 / ""
local nohash    = 1 - P("#")
local digit     = R("09")
local double    = P("##") / "#"
local single    = P("#")
local sentinel  = spaces * (nohash^1 / "\\%0")
local sargument = (single * digit)^1
local dargument = (double * digit)^1

local usespaces   = nil
local texpreamble = nil

local pattern = Cs( -- ^-1
    ( P("spaces") / function() usespaces = true return "" end )^0
  * spaces
  * ( P("nospaces") / function() usespaces = false return "" end )^0
  * spaces
  * ( P("global") / "\\global" )^0
  * spaces
  * ( P("unexpanded") / "\\unexpanded" )^0
  * spaces
  * Cc("\\expandafter\\")
  * spaces
  * ( P("expanded") / "e" )^0
  * spaces
  * ( P((1-S(" #"))^1) / "def\\csname %0\\endcsname" )
  * spaces
  * (
   --   (double * digit)^1 * sentinel^-1 * double^-1
   -- + (single * digit)^1 * sentinel^-1 * single^-1
        ( P("[") * dargument * P("]") + dargument)^1 * sentinel^-1 * double^-1
      + ( P("[") * sargument * P("]") + sargument)^1 * sentinel^-1 * single^-1
      + sentinel^-1 * (double+single)^-1
    )
)

local ctx_dostarttexdefinition = context.dostarttexdefinition

function commands.texdefinition_1(str)
    usespaces   = nil
    texpreamble = lpegmatch(pattern,str)
    if usespaces == true then
        setcatcode(32,10) -- space
        setcatcode(13, 5) -- endofline
    elseif usespaces == false then
        setcatcode(32, 9) -- ignore
        setcatcode(13, 9) -- ignore
    else
        -- this is default
     -- setcatcode(32,10) -- space
     -- setcatcode(13, 9) -- ignore
    end
    ctx_dostarttexdefinition()
end

function commands.texdefinition_2()
    context(texpreamble)
end

local upper, lower, strip = utf.upper, utf.lower, string.strip

function commands.upper(s) context(upper(s)) end
function commands.lower(s) context(lower(s)) end
function commands.strip(s) context(strip(s)) end

function commands.converteddimen(dimen,unit) context(todimen(dimen,unit or "pt","%0.5f")) end -- no unit appended (%F)