diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/strc-num.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/strc-num.lua | 110 |
1 files changed, 73 insertions, 37 deletions
diff --git a/Master/texmf-dist/tex/context/base/strc-num.lua b/Master/texmf-dist/tex/context/base/strc-num.lua index 95cf6d94127..0715a9e4913 100644 --- a/Master/texmf-dist/tex/context/base/strc-num.lua +++ b/Master/texmf-dist/tex/context/base/strc-num.lua @@ -6,12 +6,12 @@ if not modules then modules = { } end modules ['strc-num'] = { license = "see context related readme files" } --- this will be reimplemented +-- this will be reimplemented and some more will move to the commands namespace local format = string.format local next, type = next, type local min, max = math.min, math.max -local texsprint, texcount = tex.sprint, tex.count +local texcount = tex.count local allocate = utilities.storage.allocate local setmetatableindex = table.setmetatableindex @@ -66,7 +66,7 @@ end job.register('structures.counters.collected', tobesaved, initializer, finalizer) -local function constructor(t,s,name,i) +local function constructor(t,s,name,i) -- variables ? if s == "last" then local cc = collected[name] t.stop = (cc and cc[i] and cc[i][t.range]) or 0 -- stop is available for diagnostics purposes only @@ -181,26 +181,18 @@ local function savevalue(name,i) end end -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 - -function counters.trace(name) - local cd = counterdata[name] - if cd then - texsprint(format("[%s:",name)) - local data = cd.data - for i=1,#data do - local d = data[i] - texsprint(format(" (%s: %s,%s,%s s:%s r:%s)",i,(d.start or 0),d.number or 0,d.last,d.step or 0,d.range or 0)) +function counters.define(specification) + local name = specification.name + if name and name ~= "" then + -- todo: step + local d = allocate(name,1) + d.start = specification.start + d.state = variables.start or "" + local counter = specification.counter + if counter and counter ~= "" then + d.counter = counter -- only for special purposes, cannot be false + d.method = specification.method -- frozen at define time end - texsprint("]") end end @@ -228,34 +220,30 @@ end -- depends on when incremented, before or after (driven by d.offset) -function counters.doifelse(name) - commands.doifelse(counterdata[name]) -end - function counters.previous(name,n) - texsprint(allocate(name,n).previous) + return allocate(name,n).previous end function counters.next(name,n) - texsprint(allocate(name,n).next) + return allocate(name,n).next end counters.prev = counters.previous -function counters.current(name,n) - texsprint(allocate(name,n).number) +function counters.currentvalue(name,n) + return allocate(name,n).number end function counters.first(name,n) - texsprint(allocate(name,n).first) + return allocate(name,n).first end function counters.last(name,n) - texsprint(allocate(name,n).last) + return allocate(name,n).last end function counters.subs(name,n) - texsprint(counterdata[name].data[n].subs or 0) + return counterdata[name].data[n].subs or 0 end function counters.setvalue(name,tag,value) @@ -349,14 +337,16 @@ function counters.setown(name,n,value) end end -function counters.restart(name,n,newstart) +function counters.restart(name,n,newstart,noreset) local cd = counterdata[name] if cd then newstart = tonumber(newstart) if newstart then local d = allocate(name,n) d.start = newstart - counters.reset(name,n) + if not noreset then + counters.reset(name,n) -- hm + end end end end @@ -377,13 +367,14 @@ end function counters.add(name,n,delta) local cd = counterdata[name] ---~ table.print(cd,name) +-- inspect(cd) 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 +-- print(name,n,delta,level) if not level or level == -1 then -- -1 is signal that we reset manually elseif level == -2 then @@ -426,7 +417,7 @@ function counters.get(name,n,key) end function counters.value(name,n) -- what to do with own - tex.write(counters.get(name,n or 1,'number') or 0) + return counters.get(name,n or 1,'number') or 0 end function counters.converted(name,spec) -- name can be number and reference to storage @@ -481,6 +472,51 @@ function counters.converted(name,spec) -- name can be number and reference to st end end +-- interfacing + +commands.definecounter = counters.define +commands.setcounter = counters.set +commands.setowncounter = counters.setown +commands.resetcounter = counters.reset +commands.restartcounter = counters.restart +commands.savecounter = counters.save +commands.restorecounter = counters.restore +commands.addcounter = counters.add + +commands.rawcountervalue = function(...) context(counters.raw (...)) end +commands.countervalue = function(...) context(counters.value (...)) end +commands.lastcountervalue = function(...) context(counters.last (...)) end +commands.firstcountervalue = function(...) context(counters.first (...)) end +commands.nextcountervalue = function(...) context(counters.next (...)) end +commands.prevcountervalue = function(...) context(counters.previous(...)) end +commands.subcountervalues = function(...) context(counters.subs (...)) end + +function commands.showcounter(name) + local cd = counterdata[name] + if cd then + context("[%s:",name) + local data = cd.data + for i=1,#data do + local d = data[i] + context(" (%s: %s,%s,%s s:%s r:%s)",i,(d.start or 0),d.number or 0,d.last,d.step or 0,d.range or 0) + end + context("]") + end +end + +function commands.doifelsecounter(name) commands.doifelse(counterdata[name]) end +function commands.doifcounter (name) commands.doif (counterdata[name]) end +function commands.doifnotcounter (name) commands.doifnot (counterdata[name]) end + +function commands.incrementedcounter(...) context(counters.add(...)) end + +function commands.checkcountersetup(name,level,start,state) + counters.restart(name,1,start,true) -- no reset + counters.setstate(name,state) + counters.setlevel(name,level) + sections.setchecker(name,level,counters.reset) +end + --~ -- move to strc-pag.lua --~ function counters.analyze(name,counterspecification) |