summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-doc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/strc-doc.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/strc-doc.lua185
1 files changed, 99 insertions, 86 deletions
diff --git a/Master/texmf-dist/tex/context/base/strc-doc.lua b/Master/texmf-dist/tex/context/base/strc-doc.lua
index f10b175a463..a25d1e1b0da 100644
--- a/Master/texmf-dist/tex/context/base/strc-doc.lua
+++ b/Master/texmf-dist/tex/context/base/strc-doc.lua
@@ -15,14 +15,15 @@ if not modules then modules = { } end modules ['strc-doc'] = {
-- in section titles by default a zero aborts, so there we need: sectionset=bagger with \definestructureprefixset [bagger] [section-2,section-4] []
-- in lists however zero's are ignored, so there numbersegments=2:4 gives result
-local next, type = next, type
+local next, type, tonumber, select = next, type, tonumber, select
local format, gsub, find, gmatch, match = string.format, string.gsub, string.find, string.gmatch, string.match
-local concat = table.concat
+local concat, fastcopy = table.concat, table.fastcopy
local max, min = math.max, math.min
local allocate, mark, accesstable = utilities.storage.allocate, utilities.storage.mark, utilities.tables.accesstable
+local setmetatableindex = table.setmetatableindex
local catcodenumbers = catcodes.numbers
-local ctxcatcodes = tex.ctxcatcodes
+local ctxcatcodes = catcodenumbers.ctxcatcodes
local variables = interfaces.variables
local v_last = variables.last
@@ -61,10 +62,10 @@ local a_internal = attributes.private('internal')
-- -- -- document -- -- --
-local data
+local data -- the current state
function documents.initialize()
- data = {
+ data = allocate { -- whole data is marked
numbers = { },
forced = { },
ownnumbers = { },
@@ -92,11 +93,12 @@ documents.initialize()
function documents.preset(numbers)
local nofnumbers = #numbers
- data.numbers = numbers
- data.depth = nofnumbers
- data.ownnumbers = { }
+ local ownnumbers = { }
+ data.numbers = numbers
+ data.ownnumbers = ownnumbers
+ data.depth = nofnumbers
for i=1,nofnumbers do
- data.ownnumbers[i] = ""
+ ownnumbers[i] = ""
end
sections.setnumber(nofnumbers,"-1")
end
@@ -109,12 +111,12 @@ local tobesaved = allocate()
sections.collected = collected
sections.tobesaved = tobesaved
---~ local function initializer()
---~ collected = sections.collected
---~ tobesaved = sections.tobesaved
---~ end
-
---~ job.register('structures.sections.collected', tobesaved, initializer)
+-- local function initializer()
+-- collected = sections.collected
+-- tobesaved = sections.tobesaved
+-- end
+--
+-- job.register('structures.sections.collected', tobesaved, initializer)
sections.registered = sections.registered or allocate()
local registered = sections.registered
@@ -132,40 +134,39 @@ end
function sections.save(sectiondata)
-- local sectionnumber = helpers.simplify(section.sectiondata) -- maybe done earlier
local numberdata = sectiondata.numberdata
+ local ntobesaved = #tobesaved
if not numberdata or sectiondata.metadata.nolist then
- return #tobesaved
+ return ntobesaved
else
- local n = #tobesaved + 1
- tobesaved[n] = numberdata
- if not collected[n] then
- collected[n] = numberdata
+ ntobesaved = ntobesaved + 1
+ tobesaved[ntobesaved] = numberdata
+ if not collected[ntobesaved] then
+ collected[ntobesaved] = numberdata
end
- return n
+ return ntobesaved
end
end
function sections.load()
- setmetatable(collected,nil)
- local l = lists.collected
- for i=1,#l do
- local li = l[i]
- local lm = li.metadata
- if lm and lm.kind == "section" and not lm.nolist then
- local ln = li.numberdata
- if ln then
- collected[#collected+1] = ln
+ setmetatableindex(collected,nil)
+ local lists = lists.collected
+ for i=1,#lists do
+ local list = lists[i]
+ local metadata = list.metadata
+ if metadata and metadata.kind == "section" and not metadata.nolist then
+ local numberdata = list.numberdata
+ if numberdata then
+ collected[#collected+1] = numberdata
end
end
end
- sections.load = nil
+ sections.load = functions.dummy
end
-setmetatable(collected, {
- __index = function(t,i)
- sections.load()
- return t[i] or { }
- end
-})
+table.setmetatableindex(collected, function(t,i)
+ sections.load()
+ return collected[i] or { }
+end)
--
@@ -195,16 +196,10 @@ function sections.getlevel(name)
return levelmap[name] or 0
end
-local byway = "^" .. v_by
-
-function sections.way(way)
- context((gsub(way,byway,"")))
-end
-
function sections.setblock(name)
local block = name or data.block or "unknown" -- can be used to set the default
data.block = block
- context(block)
+ return block
end
function sections.pushblock(name)
@@ -213,7 +208,7 @@ function sections.pushblock(name)
data.blocks[#data.blocks+1] = block
data.block = block
documents.reset()
- context(block)
+ return block
end
function sections.popblock()
@@ -221,7 +216,7 @@ function sections.popblock()
local block = data.blocks[#data.blocks] or data.block
data.block = block
documents.reset()
- context(block)
+ return block
end
function sections.currentblock()
@@ -236,9 +231,12 @@ function sections.getcurrentlevel()
context(data.depth)
end
+local saveset = { } -- experiment, see sections/tricky-001.tex
+
function sections.somelevel(given)
-- old number
local numbers = data.numbers
+
local ownnumbers = data.ownnumbers
local forced = data.forced
local status = data.status
@@ -247,13 +245,14 @@ function sections.somelevel(given)
local mappedlevel = levelmap[givenname]
local newdepth = tonumber(mappedlevel or (olddepth > 0 and olddepth) or 1) -- hm, levelmap only works for section-*
local directives = given.directives
- local resetset = (directives and directives.resetset) or ""
+ local resetset = directives and directives.resetset or ""
-- local resetter = sets.getall("structure:resets",data.block,resetset)
- -- a trick to permits userdata to overload title, ownnumber and reference
+ -- a trick to permit userdata to overload title, ownnumber and reference
-- normally these are passed as argument but nowadays we provide several
-- interfaces (we need this because we want to be compatible)
if trace_detail then
- report_structure("name '%s', mapped level '%s', old depth '%s', new depth '%s', reset set '%s'",givenname,mappedlevel,olddepth,newdepth,resetset)
+ report_structure("name %a, mapped level %a, old depth %a, new depth %a, reset set %a",
+ givenname,mappedlevel,olddepth,newdepth,resetset)
end
local u = given.userdata
if u then
@@ -265,11 +264,14 @@ function sections.somelevel(given)
if u.label and u.label ~= "" then given.titledata.label = u.label ; u.label = nil end
end
-- so far for the trick
+ if saveset then
+ saveset[newdepth] = (resetset ~= "" and resetset) or saveset[newdepth] or ""
+ end
if newdepth > olddepth then
for i=olddepth+1,newdepth do
- local s = tonumber(sets.get("structure:resets",data.block,resetset,i))
+ local s = tonumber(sets.get("structure:resets",data.block,saveset and saveset[i] or resetset,i))
if trace_detail then
- report_structure("new>old (%s>%s), reset set '%s', reset value '%s', current '%s'",olddepth,newdepth,resetset,s or "?",numbers[i] or "?")
+ report_structure("new depth %s, old depth %s, reset set %a, reset value %a, current %a",olddepth,newdepth,resetset,s,numbers[i])
end
if not s or s == 0 then
numbers[i] = numbers[i] or 0
@@ -282,9 +284,9 @@ function sections.somelevel(given)
end
elseif newdepth < olddepth then
for i=olddepth,newdepth+1,-1 do
- local s = tonumber(sets.get("structure:resets",data.block,resetset,i))
+ local s = tonumber(sets.get("structure:resets",data.block,saveset and saveset[i] or resetset,i))
if trace_detail then
- report_structure("new<old (%s<%s), reset set '%s', reset value '%s', current '%s'",olddepth,newdepth,resetset,s or "?",numbers[i] or "?")
+ report_structure("new depth %s, old depth %s, reset set %a, reset value %a, current %a",olddepth,newdepth,resetset,s,numbers[i])
end
if not s or s == 0 then
numbers[i] = numbers[i] or 0
@@ -316,24 +318,12 @@ function sections.somelevel(given)
end
forced[newdepth] = nil
if trace_detail then
- report_structure("old depth '%s', new depth '%s, old n '%s', new n '%s', forced '%s'",olddepth,newdepth,oldn,newn,concat(fd,""))
- end
- elseif newn then
- newn = oldn + 1
- if trace_detail then
- report_structure("old depth '%s', new depth '%s, old n '%s', new n '%s', increment",olddepth,newdepth,oldn,newn)
+ report_structure("old depth %a, new depth %a, old n %a, new n %a, forced %t",olddepth,newdepth,oldn,newn,fd)
end
else
- local s = tonumber(sets.get("structure:resets",data.block,resetset,newdepth))
- if not s then
- newn = oldn or 0
- elseif s == 0 then
- newn = oldn or 0
- else
- newn = s - 1
- end
+ newn = oldn + 1
if trace_detail then
- report_structure("old depth '%s', new depth '%s, old n '%s', new n '%s', reset",olddepth,newdepth,oldn,newn)
+ report_structure("old depth %a, new depth %a, old n %a, new n %a, increment",olddepth,newdepth,oldn,newn)
end
end
numbers[newdepth] = newn
@@ -350,22 +340,28 @@ function sections.somelevel(given)
numberdata = { }
given.numberdata = numberdata
end
+
local n = { }
for i=1,newdepth do
n[i] = numbers[i]
end
numberdata.numbers = n
+-- numberdata.numbers = fastcopy(numbers)
+
if #ownnumbers > 0 then
- numberdata.ownnumbers = table.fastcopy(ownnumbers)
+ numberdata.ownnumbers = fastcopy(ownnumbers)
end
if trace_detail then
- report_structure("name '%s', numbers '%s', own numbers '%s'",givenname,concat(numberdata.numbers, " "),concat(numberdata.ownnumbers, " "))
+ report_structure("name %a, numbers % a, own numbers % a",givenname,numberdata.numbers,numberdata.ownnumbers)
end
local metadata = given.metadata
local references = given.references
- references.tag = references.tag or tags.getid(metadata.kind,metadata.name)
+ local tag = references.tag or tags.getid(metadata.kind,metadata.name)
+ if tag and tag ~= "" and tag ~= "?" then
+ references.tag = tag
+ end
local setcomponent = structures.references.setcomponent
if setcomponent then
@@ -603,9 +599,8 @@ function sections.typesetnumber(entry,kind,...) -- kind='section','number','pref
local set = ""
local segments = ""
local criterium = ""
- local dataset = { ... }
- for d=1,#dataset do
- local data = dataset[d] -- can be multiple parametersets
+ for d=1,select("#",...) do
+ local data = select(d,...) -- can be multiple parametersets
if data then
if separatorset == "" then separatorset = data.separatorset or "" end
if conversionset == "" then conversionset = data.conversionset or "" end
@@ -767,27 +762,31 @@ function sections.findnumber(depth,what) -- needs checking (looks wrong and slow
if what == v_first or what == v_previous then
for i=index,1,-1 do
local s = collected[i]
- local n = s.numbers
- if #n == depth and n[depth] and n[depth] ~= 0 then
- sectiondata = s
- if quit then
+ if s then
+ local n = s.numbers
+ if #n == depth and n[depth] and n[depth] ~= 0 then
+ sectiondata = s
+ if quit then
+ break
+ end
+ elseif #n < depth then
break
end
- elseif #n < depth then
- break
end
end
elseif what == v_last or what == v_next then
for i=index,#collected do
local s = collected[i]
- local n = s.numbers
- if #n == depth and n[depth] and n[depth] ~= 0 then
- sectiondata = s
- if quit then
+ if s then
+ local n = s.numbers
+ if #n == depth and n[depth] and n[depth] ~= 0 then
+ sectiondata = s
+ if quit then
+ break
+ end
+ elseif #n < depth then
break
end
- elseif #n < depth then
- break
end
end
end
@@ -926,3 +925,17 @@ commands.structureautocatcodedget = function(name,catcode) sections.structured
commands.namedstructurevariable = function(depth,name) sections.structuredata(depth,name) end
commands.namedstructureuservariable = function(depth,name) sections.userdata (depth,name) end
+
+--
+
+function commands.setsectionblock (name) context(sections.setblock(name)) end
+function commands.pushsectionblock(name) context(sections.pushblock(name)) end
+function commands.popsectionblock () context(sections.popblock()) end
+
+--
+
+local byway = "^" .. v_by -- ugly but downward compatible
+
+function commands.way(way)
+ context((gsub(way,byway,"")))
+end