summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-num.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/strc-num.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/strc-num.lua227
1 files changed, 120 insertions, 107 deletions
diff --git a/Master/texmf-dist/tex/context/base/strc-num.lua b/Master/texmf-dist/tex/context/base/strc-num.lua
index 8165d078617..95cf6d94127 100644
--- a/Master/texmf-dist/tex/context/base/strc-num.lua
+++ b/Master/texmf-dist/tex/context/base/strc-num.lua
@@ -6,46 +6,52 @@ if not modules then modules = { } end modules ['strc-num'] = {
license = "see context related readme files"
}
+-- this will be reimplemented
+
local format = string.format
local next, type = next, type
local min, max = math.min, math.max
local texsprint, texcount = tex.sprint, tex.count
-local trace_counters = false trackers.register("structure.counters", function(v) trace_counters = v end)
-
-structure = structure or { }
-structure.helpers = structure.helpers or { }
-structure.sections = structure.sections or { }
-structure.counters = structure.counters or { }
-structure.documents = structure.documents or { }
+local allocate = utilities.storage.allocate
+local setmetatableindex = table.setmetatableindex
-structure.counters = structure.counters or { }
-structure.counters.data = structure.counters.data or { }
-structure.counters.specials = structure.counters.specials or { }
+local trace_counters = false trackers.register("structures.counters", function(v) trace_counters = v end)
+local report_counters = logs.reporter("structure","counters")
-local helpers = structure.helpers
-local sections = structure.sections
-local counters = structure.counters
-local documents = structure.documents
+local structures = structures
+local helpers = structures.helpers
+local sections = structures.sections
+local counters = structures.counters
+local documents = structures.documents
-local variables = interfaces.variables
+local variables = interfaces.variables
-- state: start stop none reset
-local counterdata = counters.data
+counters.specials = counters.specials or { }
+local counterspecials = counters.specials
+
local counterranges, tbs = { }, 0
-local counterspecials = counters.specials
-counters.collected = counters.collected or { }
+counters.collected = allocate()
counters.tobesaved = counters.tobesaved or { }
+counters.data = counters.data or { }
-storage.register("structure/counters/data", structure.counters.data, "structure.counters.data")
-storage.register("structure/counters/tobesaved", structure.counters.tobesaved, "structure.counters.tobesaved")
+storage.register("structures/counters/data", counters.data, "structures.counters.data")
+storage.register("structures/counters/tobesaved", counters.tobesaved, "structures.counters.tobesaved")
-local collected, tobesaved = counters.collected, counters.tobesaved
+local collected = counters.collected
+local tobesaved = counters.tobesaved
+local counterdata = counters.data
+
+local function initializer() -- not really needed
+ collected = counters.collected
+ tobesaved = counters.tobesaved
+ counterdata = counters.data
+end
local function finalizer()
- local ct = counters.tobesaved
for name, cd in next, counterdata do
local cs = tobesaved[name]
local data = cd.data
@@ -58,13 +64,7 @@ local function finalizer()
end
end
-local function initializer()
- collected, tobesaved = counters.collected, counters.tobesaved
-end
-
-if job then
- job.register('structure.counters.collected', structure.counters.tobesaved, initializer, finalizer)
-end
+job.register('structures.counters.collected', tobesaved, initializer, finalizer)
local function constructor(t,s,name,i)
if s == "last" then
@@ -108,28 +108,29 @@ local function constructor(t,s,name,i)
end
end
-local enhance = function()
+local function enhance()
for name, cd in next, counterdata do
local data = cd.data
for i=1,#data do
local ci = data[i]
- setmetatable(ci, { __index = function(t,s) return constructor(t,s,name,i) end })
+ setmetatableindex(ci, function(t,s) return constructor(t,s,name,i) end)
end
end
enhance = nil
end
-local function allocate(name,i)
+local function allocate(name,i) -- can be metatable
local cd = counterdata[name]
if not cd then
cd = {
- level = 1,
---~ block = "", -- todo
+ level = 1,
+ -- block = "", -- todo
numbers = nil,
- state = variables.start, -- true
- data = { }
+ state = variables.start, -- true
+ data = { },
+ saved = { },
}
- tobesaved[name] = { }
+ tobesaved[name] = { }
counterdata[name] = cd
end
cd = cd.data
@@ -137,15 +138,14 @@ local function allocate(name,i)
if not ci then
ci = {
number = 0,
- start = 0,
- saved = 0,
- step = 1,
- range = 1,
+ start = 0,
+ saved = 0,
+ step = 1,
+ range = 1,
offset = false,
- -- via metatable: last, first, and for tracing:
- stop = 0,
+ stop = 0, -- via metatable: last, first, stop only for tracing
}
- setmetatable(ci, { __index = function(t,s) return constructor(t,s,name,i) end })
+ setmetatableindex(ci, function(t,s) return constructor(t,s,name,i) end)
cd[i] = ci
tobesaved[name][i] = { }
else
@@ -163,9 +163,17 @@ local function savevalue(name,i)
local cd = counterdata[name].data[i]
local cs = tobesaved[name][i]
local cc = collected[name]
+ if trace_counters then
+ report_counters("saving value %s of counter named %s",cd.number,name)
+ end
local cr = cd.range
local old = (cc and cc[i] and cc[i][cr]) or 0
- cs[cr] = cd.number
+ local number = cd.number
+ if cd.method == variables.page then
+ -- we can be one page ahead
+ number = number - 1
+ end
+ cs[cr] = (number >= 0) and number or 0
cd.range = cr + 1
return old
else
@@ -173,11 +181,13 @@ local function savevalue(name,i)
end
end
-function counters.define(name, start, counter) -- todo: step
+function counters.define(name, start, counter, method) -- todo: step
local d = allocate(name,1)
d.start = start
+ d.state = variables.start or ""
if counter ~= "" then
d.counter = counter -- only for special purposes, cannot be false
+ d.method = method -- frozen at define time
end
end
@@ -270,19 +280,18 @@ function counters.setoffset(name,value)
counters.setvalue(name,"offset",value)
end
-
local function synchronize(name,d)
local dc = d.counter
if dc then
if trace_counters then
- logs.report("counters","setting counter %s with name %s to %s",dc,name,d.number)
+ report_counters("setting counter %s with name %s to %s",dc,name,d.number)
end
tex.setcount("global",dc,d.number)
end
local cs = counterspecials[name]
if cs then
if trace_counters then
- logs.report("counters","invoking special for name %s",name)
+ report_counters("invoking special for name %s",name)
end
cs()
end
@@ -334,7 +343,7 @@ function counters.setown(name,n,value)
elseif level > 0 then
check(name,d,n+1)
elseif level == 0 then
- -- happens elsewhere
+ -- happens elsewhere, check this for block
end
synchronize(name,d)
end
@@ -355,27 +364,31 @@ end
function counters.save(name) -- or just number
local cd = counterdata[name]
if cd then
- cd.saved = table.copy(cd.data)
+ table.insert(cd.saved,table.copy(cd.data))
end
end
function counters.restore(name)
local cd = counterdata[name]
if cd and cd.saved then
- cd.data = cd.saved
- cd.saved = nil
+ cd.data = table.remove(cd.saved)
end
end
function counters.add(name,n,delta)
local cd = counterdata[name]
- if cd and cd.state == variables.start then
+--~ table.print(cd,name)
+ if cd and (cd.state == variables.start or cd.state == "") then
local data = cd.data
local d = allocate(name,n)
d.number = (d.number or d.start or 0) + delta*(d.step or 0)
+ -- d.own = nil
local level = cd.level
if not level or level == -1 then
-- -1 is signal that we reset manually
+ elseif level == -2 then
+ -- -2 is signal that we work per text
+ check(name,data,n+1)
elseif level > 0 then
-- within countergroup
check(name,data,n+1)
@@ -390,10 +403,10 @@ end
function counters.check(level) -- not used (yet)
for name, cd in next, counterdata do
- -- logs.report("counters","%s %s %s",name,cd.level,level)
+ -- report_counters("%s %s %s",name,cd.level,level)
if cd.level == level then
if trace_counters then
- logs.report("counters","resetting %s at level %s",name,level)
+ report_counters("resetting %s at level %s",name,level)
end
counters.reset(name)
end
@@ -468,54 +481,54 @@ function counters.converted(name,spec) -- name can be number and reference to st
end
end
--- move to strc-pag.lua
-
-function counters.analyse(name,counterspecification)
- local cd = counterdata[name]
- -- safeguard
- if not cd then
- return false, false, "no counter data"
- end
- -- section data
- local sectiondata = sections.current()
- if not sectiondata then
- return cd, false, "not in section"
- end
- local references = sectiondata.references
- if not references then
- return cd, false, "no references"
- end
- local section = references.section
- if not section then
- return cd, false, "no section"
- end
- sectiondata = jobsections.collected[references.section]
- if not sectiondata then
- return cd, false, "no section data"
- end
- -- local preferences
- local no = variables.no
- if counterspecification and counterspecification.prefix == no then
- return cd, false, "current spec blocks prefix"
- end
- -- stored preferences (not used)
- if cd.prefix == no then
- return cd, false, "entry blocks prefix"
- end
- -- sectioning
- -- if sectiondata.prefix == no then
- -- return false, false, "sectiondata blocks prefix"
- -- end
- -- final verdict
- return cd, sectiondata, "okay"
-end
-
-function counters.prefixedconverted(name,prefixspec,numberspec)
- local cd, prefixdata, result = counters.analyse(name,prefixspec)
- if cd then
- if prefixdata then
- sections.typesetnumber(prefixdata,"prefix",prefixspec or false,cd or false)
- end
- counters.converted(name,numberspec)
- end
-end
+--~ -- move to strc-pag.lua
+
+--~ function counters.analyze(name,counterspecification)
+--~ local cd = counterdata[name]
+--~ -- safeguard
+--~ if not cd then
+--~ return false, false, "no counter data"
+--~ end
+--~ -- section data
+--~ local sectiondata = sections.current()
+--~ if not sectiondata then
+--~ return cd, false, "not in section"
+--~ end
+--~ local references = sectiondata.references
+--~ if not references then
+--~ return cd, false, "no references"
+--~ end
+--~ local section = references.section
+--~ if not section then
+--~ return cd, false, "no section"
+--~ end
+--~ sectiondata = sections.collected[references.section]
+--~ if not sectiondata then
+--~ return cd, false, "no section data"
+--~ end
+--~ -- local preferences
+--~ local no = variables.no
+--~ if counterspecification and counterspecification.prefix == no then
+--~ return cd, false, "current spec blocks prefix"
+--~ end
+--~ -- stored preferences (not used)
+--~ if cd.prefix == no then
+--~ return cd, false, "entry blocks prefix"
+--~ end
+--~ -- sectioning
+--~ -- if sectiondata.prefix == no then
+--~ -- return false, false, "sectiondata blocks prefix"
+--~ -- end
+--~ -- final verdict
+--~ return cd, sectiondata, "okay"
+--~ end
+
+--~ function counters.prefixedconverted(name,prefixspec,numberspec)
+--~ local cd, prefixdata, result = counters.analyze(name,prefixspec)
+--~ if cd then
+--~ if prefixdata then
+--~ sections.typesetnumber(prefixdata,"prefix",prefixspec or false,cd or false)
+--~ end
+--~ counters.converted(name,numberspec)
+--~ end
+--~ end