summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/cldf-ini.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/cldf-ini.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/cldf-ini.lua240
1 files changed, 167 insertions, 73 deletions
diff --git a/Master/texmf-dist/tex/context/base/cldf-ini.lua b/Master/texmf-dist/tex/context/base/cldf-ini.lua
index ed86c29234f..4a7d9f025e4 100644
--- a/Master/texmf-dist/tex/context/base/cldf-ini.lua
+++ b/Master/texmf-dist/tex/context/base/cldf-ini.lua
@@ -20,15 +20,19 @@ if not modules then modules = { } end modules ['cldf-ini'] = {
--
-- tex.print == line with endlinechar appended
+-- todo: context("%bold{total: }%s",total)
+-- todo: context.documentvariable("title")
+
local tex = tex
context = context or { }
local context = context
-local format, find, gmatch, gsub = string.format, string.find, string.gmatch, string.gsub
-local next, type, tostring, tonumber, setmetatable = next, type, tostring, tonumber, setmetatable
+local format, gsub, validstring = string.format, string.gsub, string.valid
+local next, type, tostring, tonumber, setmetatable, unpack, select = next, type, tostring, tonumber, setmetatable, unpack, select
local insert, remove, concat = table.insert, table.remove, table.concat
-local lpegmatch, lpegC, lpegS, lpegP, lpegCc = lpeg.match, lpeg.C, lpeg.S, lpeg.P, lpeg.Cc
+local lpegmatch, lpegC, lpegS, lpegP, lpegCc, patterns = lpeg.match, lpeg.C, lpeg.S, lpeg.P, lpeg.Cc, lpeg.patterns
+local formatters = string.formatters -- using formatteds is slower in this case
local texsprint = tex.sprint
local textprint = tex.tprint
@@ -40,12 +44,14 @@ local isnode = node.is_node -- after 0.65 just node.type
local writenode = node.write
local copynodelist = node.copy_list
-local ctxcatcodes = tex.ctxcatcodes
-local prtcatcodes = tex.prtcatcodes
-local texcatcodes = tex.texcatcodes
-local txtcatcodes = tex.txtcatcodes
-local vrbcatcodes = tex.vrbcatcodes
-local xmlcatcodes = tex.xmlcatcodes
+local catcodenumbers = catcodes.numbers
+
+local ctxcatcodes = catcodenumbers.ctxcatcodes
+local prtcatcodes = catcodenumbers.prtcatcodes
+local texcatcodes = catcodenumbers.texcatcodes
+local txtcatcodes = catcodenumbers.txtcatcodes
+local vrbcatcodes = catcodenumbers.vrbcatcodes
+local xmlcatcodes = catcodenumbers.xmlcatcodes
local flush = texsprint
local flushdirect = texprint
@@ -76,7 +82,7 @@ end
local function _flush_f_(n)
local sn = _stack_f_[n]
if not sn then
- report_cld("data with id %s cannot be found on stack",n)
+ report_cld("data with id %a cannot be found on stack",n)
else
local tn = type(sn)
if tn == "function" then
@@ -100,7 +106,7 @@ end
local function _flush_n_(n)
local sn = _stack_n_[n]
if not sn then
- report_cld("data with id %s cannot be found on stack",n)
+ report_cld("data with id %a cannot be found on stack",n)
elseif texcount["@@trialtypesetting"] == 0 then -- @@trialtypesetting is private!
writenode(sn)
_stack_n_[n] = nil
@@ -154,14 +160,8 @@ context.popcatcodes = popcatcodes
-- -- --
---~ local capture = (
---~ space^0 * newline^2 * lpeg.Cc("") / texprint +
---~ space^0 * newline * space^0 * lpeg.Cc(" ") / texsprint +
---~ content / texsprint
---~ )^0
-
-local newline = lpeg.patterns.newline
-local space = lpeg.patterns.spacer
+local newline = patterns.newline
+local space = patterns.spacer
local spacing = newline * space^0
local content = lpegC((1-spacing)^1) -- texsprint
local emptyline = space^0 * newline^2 -- texprint("")
@@ -344,16 +344,18 @@ end
local methodhandler = resolvers.methodhandler
-function context.viafile(data)
+function context.viafile(data,tag)
if data and data ~= "" then
- local filename = resolvers.savers.byscheme("virtual","viafile",data)
+ local filename = resolvers.savers.byscheme("virtual",validstring(tag,"viafile"),data)
-- context.startregime { "utf" }
context.input(filename)
-- context.stopregime()
end
end
--- -- --
+-- -- -- "{" .. ti .. "}" is somewhat slower in a cld-mkiv run than "{",ti,"}"
+
+local containseol = patterns.containseol
local function writer(parent,command,first,...) -- already optimized before call
local t = { first, ... }
@@ -366,7 +368,7 @@ local function writer(parent,command,first,...) -- already optimized before call
if typ == "string" or typ == "number" then
flush(currentcatcodes,ti)
else -- node.write
- report_context("error: invalid use of direct in '%s', only strings and numbers can be flushed directly, not '%s'",command,typ)
+ report_context("error: invalid use of direct in %a, only strings and numbers can be flushed directly, not %a",command,typ)
end
direct = false
elseif ti == nil then
@@ -375,7 +377,7 @@ local function writer(parent,command,first,...) -- already optimized before call
flush(currentcatcodes,"{}")
elseif typ == "string" then
-- is processelines seen ?
- if processlines and find(ti,"[\n\r]") then -- we can check for ti == "\n"
+ if processlines and lpegmatch(containseol,ti) then
flush(currentcatcodes,"{")
local flushlines = parent.__flushlines or flushlines
flushlines(ti)
@@ -410,7 +412,11 @@ local function writer(parent,command,first,...) -- already optimized before call
done = true
end
end
- flush(currentcatcodes,"]")
+ if done then
+ flush(currentcatcodes,"]")
+ else
+ flush(currentcatcodes,"[]")
+ end
elseif tn == 1 then -- some 20% faster than the next loop
local tj = ti[1]
if type(tj) == "function" then
@@ -440,7 +446,7 @@ local function writer(parent,command,first,...) -- already optimized before call
elseif isnode(ti) then -- slow
flush(currentcatcodes,"{\\cldn{",_store_n_(ti),"}}")
else
- report_context("error: '%s' gets a weird argument '%s'",command,tostring(ti))
+ report_context("error: %a gets a weird argument %a",command,ti)
end
end
end
@@ -448,16 +454,20 @@ end
local generics = { } context.generics = generics
local function indexer(parent,k)
- local c = "\\" .. tostring(generics[k] or k)
- local f = function(first,...)
- if first == nil then
- flush(currentcatcodes,c)
- else
- return writer(parent,c,first,...)
+ if type(k) == "string" then
+ local c = "\\" .. tostring(generics[k] or k)
+ local f = function(first,...)
+ if first == nil then
+ flush(currentcatcodes,c)
+ else
+ return writer(parent,c,first,...)
+ end
end
+ parent[k] = f
+ return f
+ else
+ return context -- catch
end
- parent[k] = f
- return f
end
-- Potential optimization: after the first call we know if there will be an
@@ -518,8 +528,8 @@ local function caller(parent,f,a,...)
local typ = type(f)
if typ == "string" then
if a then
- flush(contentcatcodes,format(f,a,...)) -- was currentcatcodes
- elseif processlines and find(f,"[\n\r]") then
+ flush(contentcatcodes,formatters[f](a,...)) -- was currentcatcodes
+ elseif processlines and lpegmatch(containseol,f) then
local flushlines = parent.__flushlines or flushlines
flushlines(f)
else
@@ -538,10 +548,9 @@ local function caller(parent,f,a,...)
if f then
if a ~= nil then
local flushlines = parent.__flushlines or flushlines
- flushlines(f)
- -- ignore ... maybe some day
+ flushlines(a)
else
- flushdirect(currentcatcodes,"\r")
+ flushdirect(currentcatcodes,"\n") -- no \r, else issues with \startlines ... use context.par() otherwise
end
else
if a ~= nil then
@@ -557,7 +566,7 @@ local function caller(parent,f,a,...)
-- writenode(f)
flush(currentcatcodes,"\\cldn{",_store_n_(f),"}")
else
- report_context("error: 'context' gets a weird argument '%s'",tostring(f))
+ report_context("error: %a gets a weird argument %a","context",f)
end
end
end
@@ -592,13 +601,13 @@ end
function context.fprint(catcodes,fmt,first,...)
if type(catcodes) == "number" then
if first then
- flush(catcodes,format(fmt,first,...))
+ flush(catcodes,formatters[fmt](first,...))
else
flush(catcodes,fmt)
end
else
if fmt then
- flush(format(catodes,fmt,first,...))
+ flush(formatters[catcodes](fmt,first,...))
else
flush(catcodes)
end
@@ -607,7 +616,7 @@ end
function tex.fprint(fmt,first,...) -- goodie
if first then
- flush(currentcatcodes,format(fmt,first,...))
+ flush(currentcatcodes,formatters[fmt](first,...))
else
flush(currentcatcodes,fmt)
end
@@ -625,6 +634,11 @@ local currenttrace = nil
local nofwriters = 0
local nofflushes = 0
+local visualizer = lpeg.replacer {
+ { "\n","<<newline>>" },
+ { "\r","<<par>>" },
+}
+
statistics.register("traced context", function()
if nofwriters > 0 or nofflushes > 0 then
return format("writers: %s, flushes: %s, maxstack: %s",nofwriters,nofflushes,_n_f_)
@@ -638,7 +652,7 @@ local tracedwriter = function(parent,...) -- also catcodes ?
local t, n = { "w : - : " }, 1
local traced = function(normal,catcodes,...) -- todo: check for catcodes
local s = concat({...})
- s = gsub(s,"\r","<<newline>>") -- unlikely
+ s = lpegmatch(visualizer,s)
n = n + 1
t[n] = s
normal(catcodes,...)
@@ -660,17 +674,17 @@ local traced = function(normal,one,two,...)
normal(one,two,...)
local catcodes = type(one) == "number" and one
local arguments = catcodes and { two, ... } or { one, two, ... }
- local collapsed, c = { format("f : %s : ", catcodes or '-') }, 1
+ local collapsed, c = { formatters["f : %s : "](catcodes or '-') }, 1
for i=1,#arguments do
local argument = arguments[i]
local argtype = type(argument)
c = c + 1
if argtype == "string" then
- collapsed[c] = gsub(argument,"\r","<<newline>>")
+ collapsed[c] = lpegmatch(visualizer,argument)
elseif argtype == "number" then
collapsed[c] = argument
else
- collapsed[c] = format("<<%s>>",tostring(argument))
+ collapsed[c] = formatters["<<%S>>"](argument)
end
end
currenttrace(concat(collapsed))
@@ -679,11 +693,11 @@ local traced = function(normal,one,two,...)
normal(one)
local argtype = type(one)
if argtype == "string" then
- currenttrace(format("f : - : %s",gsub(one,"\r","<<newline>>")))
+ currenttrace(formatters["f : - : %s"](lpegmatch(visualizer,one)))
elseif argtype == "number" then
- currenttrace(format("f : - : %s",one))
+ currenttrace(formatters["f : - : %s"](one))
else
- currenttrace(format("f : - : <<%s>>",tostring(one)))
+ currenttrace(formatters["f : - : <<%S>>"](one))
end
end
end
@@ -769,19 +783,19 @@ function context.runfile(filename)
local ok = dofile(foundname)
if type(ok) == "function" then
if trace_cld then
- report_context("begin of file '%s' (function call)",foundname)
+ report_context("begin of file %a (function call)",foundname)
end
ok()
if trace_cld then
- report_context("end of file '%s' (function call)",foundname)
+ report_context("end of file %a (function call)",foundname)
end
elseif ok then
- report_context("file '%s' is processed and returns true",foundname)
+ report_context("file %a is processed and returns true",foundname)
else
- report_context("file '%s' is processed and returns nothing",foundname)
+ report_context("file %a is processed and returns nothing",foundname)
end
else
- report_context("unknown file '%s'",filename)
+ report_context("unknown file %a",filename)
end
end
@@ -815,6 +829,44 @@ local function caller(parent,...) -- todo: nodes
end
end
+-- local function indexer(parent,k)
+-- local f = function(a,...)
+-- if not a then
+-- return function()
+-- return context[k]()
+-- end
+-- elseif select("#",...) == 0 then
+-- return function()
+-- return context[k](a)
+-- end
+-- elseif a then
+-- local t = { ... }
+-- return function()
+-- return context[k](a,unpack(t))
+-- end
+-- end
+-- end
+-- parent[k] = f
+-- return f
+-- end
+--
+-- local function caller(parent,a,...) -- todo: nodes
+-- if not a then
+-- return function()
+-- return context()
+-- end
+-- elseif select("#",...) == 0 then
+-- return function()
+-- return context(a)
+-- end
+-- elseif a then
+-- local t = { ... }
+-- return function()
+-- return context(a,unpack(t))
+-- end
+-- end
+-- end
+
setmetatable(delayed, { __index = indexer, __call = caller } )
-- context.nested (todo: lines)
@@ -874,6 +926,60 @@ end
setmetatable(verbatim, { __index = indexer, __call = caller } )
+-- formatted
+
+local formatted = { } context.formatted = formatted
+
+-- local function indexer(parent,k)
+-- local command = context[k]
+-- local f = function(fmt,...)
+-- command(formatters[fmt](...))
+-- end
+-- parent[k] = f
+-- return f
+-- end
+
+local function indexer(parent,k)
+ if type(k) == "string" then
+ local c = "\\" .. tostring(generics[k] or k)
+ local f = function(first,second,...)
+ if first == nil then
+ flush(currentcatcodes,c)
+ elseif second then
+ return writer(parent,c,formatters[first](second,...))
+ else
+ return writer(parent,c,first)
+ end
+ end
+ parent[k] = f
+ return f
+ else
+ return context -- catch
+ end
+end
+
+-- local function caller(parent,...)
+-- context.fprint(...)
+-- end
+
+local function caller(parent,catcodes,fmt,first,...)
+ if type(catcodes) == "number" then
+ if first then
+ flush(catcodes,formatters[fmt](first,...))
+ else
+ flush(catcodes,fmt)
+ end
+ else
+ if fmt then
+ flush(formatters[catcodes](fmt,first,...))
+ else
+ flush(catcodes)
+ end
+ end
+end
+
+setmetatable(formatted, { __index = indexer, __call = caller } )
+
-- metafun (this will move to another file)
local metafun = { } context.metafun = metafun
@@ -887,7 +993,7 @@ local function caller(parent,f,a,...)
local typ = type(f)
if typ == "string" then
if a then
- flush(currentcatcodes,mpdrawing,"{",format(f,a,...),"}")
+ flush(currentcatcodes,mpdrawing,"{",formatters[f](a,...),"}")
else
flush(currentcatcodes,mpdrawing,"{",f,"}")
end
@@ -905,10 +1011,10 @@ local function caller(parent,f,a,...)
if f then
flush(currentcatcodes,mpdrawing,"{^^M}")
else
- report_context("warning: 'metafun' gets argument 'false' which is currently unsupported")
+ report_context("warning: %a gets argument 'false' which is currently unsupported","metafun")
end
else
- report_context("error: 'metafun' gets a weird argument '%s'",tostring(f))
+ report_context("error: %a gets a weird argument %a","metafun",tostring(f))
end
end
end
@@ -925,7 +1031,7 @@ function metafun.stop()
end
function metafun.color(name)
- return format([[\MPcolor{%s}]],name)
+ return formatters[ [[\MPcolor{%s}]] ](name)
end
-- metafun.delayed
@@ -955,18 +1061,6 @@ setmetatable(delayed, { __index = indexer, __call = caller } )
-- helpers:
--- we could have faster calls here
-
-function context.concat(t,separator)
- local done = false
- for i=1,#t do
- local ti = t[i]
- if ti ~= "" then
- if done then
- context(separator)
- end
- context(ti)
- done = true
- end
- end
+function context.concat(...)
+ context(concat(...))
end