cldf-bas.lmt /size: 7236 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['cldf-bas'] = {
2    version   = 1.001,
3    comment   = "companion to cldf-ini.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-- -- speedtest needed:
10--
11-- local flush, writer = context.getlogger()
12--
13-- trackers.register("context.trace",function(v)
14--     flush, writer = context.getlogger()
15-- end)
16--
17-- function context.bgroup()
18--     flush(ctxcatcodes,"{")
19-- end
20--
21-- function context.egroup()
22--     flush(ctxcatcodes,"}")
23-- end
24
25local tonumber      = tonumber
26local type          = type
27local format        = string.format
28local utfchar       = utf.char
29local concat        = table.concat
30
31local context       = context
32local ctxcore       = context.core
33
34local variables     = interfaces.variables
35
36local ctx_flushnode = context.nuts.flush
37local ctx_sprint    = context.sprint
38local txtcatcodes   = tex.txtcatcodes
39
40local nuts          = nodes.nuts
41local tonode        = nuts.tonode
42local nodepool      = nuts.pool
43local new_rule      = nodepool.rule
44local new_glyph     = nodepool.glyph
45local new_latelua   = nodepool.latelua
46
47local setattrlist   = nuts.setattrlist
48
49local texgetcount   = tex.getcount
50local texsetcount   = tex.setcount
51local texchardef    = tex.chardef
52local expandmacro   = token.expandmacro
53
54local is_letter     = characters.is_letter
55
56-- a set of basic fast ones
57
58function context.setfontid(n)
59    -- isn't there a setter?
60    context("\\setfontid%i\\relax",n)
61end
62
63function context.char(k) -- used as escape too, so don't change to utf
64    if type(k) == "table" then
65        local n = #k
66        if n == 1 then
67            context([[\char%s\relax]],k[1])
68        elseif n > 0 then
69            context([[\char%s\relax]],concat(k,[[\relax\char]]))
70        end
71    else
72        if type(k) == "string" then
73            k = tonumber(k)
74        end
75        if type(k) == "number" then
76            context([[\char%s\relax]],k)
77        end
78    end
79end
80
81function context.safechar(c)
82    if characters.is_letter[c] then -- not yet loaded
83        ctx_sprint(c)
84    else
85        ctx_sprint(txtcatcodes,c)
86    end
87end
88
89function context.utfchar(k)
90    if type(k) == "string" then
91        k = tonumber(k)
92    end
93    if type(k) == "number" then
94        context(utfchar(k))
95    end
96end
97
98function context.rule(w,h,d,direction)
99    local rule
100    if type(w) == "table" then
101        rule = new_rule(w.width,w.height,w.depth,w.direction)
102    else
103        rule = new_rule(w,h,d,direction)
104    end
105    setattrlist(rule,true)
106    context(tonode(rule))
107 -- ctx_flushnode(tonode(rule))
108end
109
110function context.glyph(id,k)
111    if id then
112        if not k then
113            id, k = true, id
114        end
115        local glyph = new_glyph(id,k)
116        setattrlist(glyph,true)
117        context(tonode(glyph))
118     -- ctx_flushnode(tonode(glyph))
119    end
120end
121
122-- local function ctx_par  () context("\\par")   end
123-- local function ctx_space() context("\\space") end
124
125local ctx_par   = context.cs.par
126local ctx_space = context.cs.space
127
128context.par   = ctx_par
129context.space = ctx_space
130
131ctxcore.par   = ctx_par
132ctxcore.space = ctx_space
133
134-- local function ctx_bgroup() context("{") end
135-- local function ctx_egroup() context("}") end
136
137local ctx_bgroup = context.cs.bgroup
138local ctx_egroup = context.cs.egroup
139
140context.bgroup = ctx_bgroup
141context.egroup = ctx_egroup
142
143ctxcore.bgroup = ctx_bgroup
144ctxcore.egroup = ctx_egroup
145
146-- not yet used ... but will get variant at the tex end as well
147
148local function setboxregister(kind,n)
149    context(type(n) == "number" and [[\setbox%s\%s]] or [[\setbox\%s\%s]],n,kind)
150end
151
152function ctxcore.sethboxregister(n) setboxregister("hbox",n) end
153function ctxcore.setvboxregister(n) setboxregister("vbox",n) end
154function ctxcore.setvtopregister(n) setboxregister("vtop",n) end
155
156local function startboxregister(kind,n)
157    context(type(n) == "number" and [[\setbox%s\%s{]] or [[\setbox\%s\%s{]],n,kind)
158end
159
160function ctxcore.starthboxregister(n) startboxregister("hbox",n) end
161function ctxcore.startvboxregister(n) startboxregister("vbox",n) end
162function ctxcore.startvtopregister(n) startboxregister("vtop",n) end
163
164ctxcore.stophboxregister = ctx_egroup
165ctxcore.stopvboxregister = ctx_egroup
166ctxcore.stopvtopregister = ctx_egroup
167
168function ctxcore.flushboxregister(n)
169    context(type(n) == "number" and [[\box%s ]] or [[\box\%s]],n)
170end
171
172-- function ctxcore.beginhbox() context([[\hbox\bgroup]]) end
173-- function ctxcore.beginvbox() context([[\vbox\bgroup]]) end
174-- function ctxcore.beginvtop() context([[\vtop\bgroup]]) end
175
176local ctx_hbox = context.cs.hbox
177local ctx_vbox = context.cs.vbox
178local ctx_vtop = context.cs.vtop
179
180function ctxcore.beginhbox() ctx_hbox() ctx_bgroup() end
181function ctxcore.beginvbox() ctx_vbox() ctx_bgroup() end
182function ctxcore.beginvtop() ctx_vtop() ctx_bgroup() end
183
184ctxcore.endhbox = ctx_egroup -- \egroup
185ctxcore.endvbox = ctx_egroup -- \egroup
186ctxcore.endvtop = ctx_egroup -- \egroup
187
188local function allocate(name,what,cmd)
189    local a = format("c_syst_last_allocated_%s",what)
190    local n = texgetcount(a) + 1
191    if n <= texgetcount("c_syst_max_allocated_register") then
192        texsetcount(a,n)
193    end
194    context("\\global\\expandafter\\%sdef\\csname %s\\endcsname %s\\relax",cmd or what,name,n)
195    return n
196end
197
198context.registers = {
199    newdimen  = function(name) expandmacro("syst_new_dimen", true,name) return texgetcount("c_syst_last_allocated_dimen")  end,
200    newskip   = function(name) expandmacro("syst_new_skip",  true,name) return texgetcount("c_syst_last_allocated_skip" )  end,
201    newcount  = function(name) expandmacro("syst_new_count", true,name) return texgetcount("c_syst_last_allocated_count")  end,
202    newmuskip = function(name) expandmacro("syst_new_muskip",true,name) return texgetcount("c_syst_last_allocated_muskip") end,
203    newtoks   = function(name) expandmacro("syst_new_toks",  true,name) return texgetcount("c_syst_last_allocated_toks")   end,
204    newbox    = function(name) expandmacro("syst_new_box",   true,name) return texgetcount("c_syst_last_allocated_box")    end,
205    -- not really a register but kind of belongs here
206    newchar   = texchardef,
207}
208
209function context.latelua(f)
210    -- table check moved elsewhere
211    local latelua = new_latelua(f)
212    setattrlist(latelua,true) -- will become an option
213    ctx_flushnode(latelua,true)
214end
215
216do
217
218    local NC = ctxcore.NC
219    local BC = ctxcore.BC
220    local NR = ctxcore.NR
221
222    context.nc = setmetatable({ }, {
223        __call =
224            function(t,...)
225                NC()
226                return context(...)
227            end,
228        __index =
229            function(t,k)
230                NC()
231                return context[k]
232            end,
233        }
234    )
235
236    function context.bc(...)
237        BC()
238        return context(...)
239    end
240
241    function context.nr(...)
242        NC()
243        NR()
244    end
245
246end
247
248-- for the moment here:
249
250do
251
252    local texset   = tex.set
253    local maxdimen = tex.magicconstants.maxdimen
254
255    function tex.dontcomplain()
256        texset("hbadness",maxdimen)
257        texset("vbadness",maxdimen)
258        texset("hfuzz",   maxdimen)
259        texset("vfuzz",   maxdimen)
260    end
261
262end
263