summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mult-cld.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mult-cld.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/mult-cld.lua170
1 files changed, 104 insertions, 66 deletions
diff --git a/Master/texmf-dist/tex/context/base/mult-cld.lua b/Master/texmf-dist/tex/context/base/mult-cld.lua
index c9e922afec0..81038b68b51 100644
--- a/Master/texmf-dist/tex/context/base/mult-cld.lua
+++ b/Master/texmf-dist/tex/context/base/mult-cld.lua
@@ -1,6 +1,6 @@
if not modules then modules = { } end modules ['mult-cld'] = {
version = 1.001,
- comment = "companion to mult-cld.tex",
+ comment = "companion to mult-cld.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
@@ -13,91 +13,127 @@ if not modules then modules = { } end modules ['mult-cld'] = {
-- code. It can also be handy when generating documents from databases or
-- when constructing large tables or so.
--
--- Todo: optional checking against interface!
+-- Todo: optional checking against interface
+-- Todo: coroutine trickery
context = context or { }
local format, concat = string.format, table.concat
local next, type = next, type
-local texsprint, texiowrite = tex.sprint, texio.write
+local texsprint, texiowrite, ctxcatcodes = tex.sprint, texio.write, tex.ctxcatcodes
-local flush = texsprint
-local cache
+local flush = texsprint or function(cct,...) print(table.concat{...}) end
-local function cached_flush(c,...)
- local tt = { ... }
- for i=1,#tt do
- cache[#cache+1] = tt[i]
+local _stack_, _n_ = { }, 0
+
+local function _store_(ti)
+ _n_ = _n_ + 1
+ _stack_[_n_] = ti
+ return _n_
+end
+
+local function _flush_(n)
+ if not _stack_[n]() then
+ _stack_[n] = nil
+ else
+ -- keep, beware, that way the stack can grow
end
end
+context._stack_ = _stack_
+context._store_ = _store_
+context._flush_ = _flush_
+
+function tex.fprint(...) -- goodie
+ texsprint(ctxcatcodes,format(...))
+end
+
+function context.trace(intercept)
+ local normalflush = flush
+ flush = function(c,...)
+ logs.report("context",concat({...}))
+ if not intercept then
+ normalflush(c,...)
+ end
+ end
+ context.trace = function() end
+end
+
+trackers.register("context.flush", function(v) if v then context.trace() end end)
+trackers.register("context.intercept", function(v) if v then context.trace(true) end end)
+
local function writer(k,...)
- flush(ctxcatcodes,k)
- local t = { ... }
- if next(t) then
- for i=1,#t do
- local ti = t[i]
- local typ, force = type(ti), nil
- while typ == "function" do
- local saved_flush = flush
- cache = { }
- flush = cached_flush
- ti, force = ti()
- if force then
- typ = false -- force special cases
- elseif typ == "nil" then
- typ = "string"
- ti = concat(cache)
- elseif typ == "string" then
- ti = concat(cache)
- end
- flush = saved_flush
- end
- if ti == nil then
- -- next
- elseif typ == "string" or typ == "number" then
- flush(ctxcatcodes,"{",ti,"}")
- elseif typ == "table" then
- flush(ctxcatcodes,"[")
- local c = concat(ti,",")
- if c ~= "" then
- flush(ctxcatcodes,c)
- else
- local done = false
- for k, v in next, ti do
- if done then
- flush(ctxcatcodes,",",k,'=',v)
- else
- flush(ctxcatcodes,k,'=',v)
- done = true
+ if k then
+ flush(ctxcatcodes,k)
+ local t = { ... }
+ local nt = #t
+ if nt > 0 then
+ for i=1,nt do
+ local ti = t[i]
+ local typ = type(ti)
+ if ti == nil then
+ -- next
+ elseif typ == "function" then
+ flush(ctxcatcodes,"{\\mkivflush{" .. _store_(ti) .. "}}")
+ elseif typ == "string" or typ == "number" then
+ flush(ctxcatcodes,"{",ti,"}")
+ elseif typ == "table" then
+ local tn = #ti
+ if tn > 0 then
+ for j=1,tn do
+ local tj = ti[j]
+ if type(tj) == "function" then
+ ti[j] = "\\mkivflush{" .. _store_(tj) .. "}"
+ end
end
+ flush(ctxcatcodes,"[",concat(ti,","),"]")
+ else
+ flush(ctxcatcodes,"[")
+ local done = false
+ for k, v in next, ti do
+ if done then
+ flush(ctxcatcodes,",",k,'=',v)
+ else
+ flush(ctxcatcodes,k,'=',v)
+ done = true
+ end
+ end
+ flush(ctxcatcodes,"]")
end
- end
- flush(ctxcatcodes,"]")
- -- elseif typ == "boolean" then
- -- flush(ctxcatcodes,"\n")
- elseif ti == true then
- flush(ctxcatcodes,"\n")
- elseif typ == false then
- if force == "direct" then
+ -- elseif typ == "boolean" then
+ -- flush(ctxcatcodes,"\n")
+ elseif ti == true then
+ flush(ctxcatcodes,"\n")
+ elseif typ == false then
+ -- if force == "direct" then
flush(ctxcatcodes,tostring(ti))
+ -- end
+ elseif typ == "thread" then
+ logs.report("interfaces","coroutines not supported as we cannot yeild across boundaries")
+ else
+ logs.report("interfaces","error: %s gets a weird argument %s",k,tostring(ti))
end
- else
- logs.report("interfaces","error: %s gets a weird argument %s",k,tostring(ti))
end
end
end
end
+-- -- --
+
local function indexer(t,k)
- local f = function(...) return writer("\\"..k.." ",...) end -- building the cs here saves time
+ local c = "\\" .. k .. " "
+ local f = function(...) return writer(c,...) end
t[k] = f
return f
end
-local function caller(t,f,...)
- if f then
- flush(ctxcatcodes,format(f,...))
+local function caller(t,f,a,...)
+ if a then
+ flush(ctxcatcodes,format(f,a,...))
+ elseif type(f) == "function" then
+ flush(ctxcatcodes,"{\\mkivflush{" .. _store_(f) .. "}}")
+ elseif f then
+ flush(ctxcatcodes,f)
else
flush(ctxcatcodes,"\n")
end
@@ -113,16 +149,18 @@ function context.runfile(filename)
filename = resolvers.findtexfile(filename) or ""
if filename ~= "" then
local ok = dofile(filename)
- if ok then
+ if type(ok) == "function" then
if trace_cld then
- commands.writestatus("cld","begin of file '%s'",filename)
+ commands.writestatus("cld","begin of file '%s' (function call)",filename)
end
ok()
if trace_cld then
- commands.writestatus("cld","end of file '%s'",filename)
+ commands.writestatus("cld","end of file '%s' (function call)",filename)
end
+ elseif ok then
+ commands.writestatus("cld","file '%s' is processed and returns true",filename)
else
- commands.writestatus("cld","invalid file '%s'",filename)
+ commands.writestatus("cld","file '%s' is processed and returns nothing",filename)
end
else
commands.writestatus("cld","unknown file '%s'",filename)
@@ -149,7 +187,7 @@ end)
function context.enabletrackers (str) trackers.enable (str) end
function context.disabletrackers(str) trackers.disable(str) end
--- see demo-lud.lud for an example
+-- see demo-cld.cld for an example
-- context.starttext(true)
-- context.chapter({ "label" }, "title", true)