[dev-context] Reset counter with each heading
Wolfgang Schuster
wolfgang.schuster at gmail.com
Mon Oct 1 19:49:49 CEST 2012
Am 01.10.2012 um 19:47 schrieb Wolfgang Schuster <wolfgang.schuster at gmail.com>:
> Hi Hans,
>
> can you add a option for the “way” key for \setupcounter which resets a counter for every section independent of the level which was requested by Peter Schorsch on the mailing list. I looked into the source file (strc-num.lua and strc-sec.mkiv) and only four lines have to be changed to add this feature (I used “head” for the name because there was a predefined variable).
>
>
> strc-sec.mkiv:
>
> \setvalue{\??headlevel\v!block}{0}
> \setvalue{\??headlevel\v!none }{-1}
> \setvalue{\??headlevel\v!text }{-2}
> +\setvalue{\??headlevel\v!head }{-3}
>
>
> strc-num.lua:
>
> function counters.setown(name,n,value)
> local cd = counterdata[name]
> if cd then
> local d = allocate(name,n)
> d.own = value
> d.number = (d.number or d.start or 0) + (d.step or 0)
> local level = cd.level
> if not level or level == -1 then
> -- -1 is signal that we reset manually
> - elseif level > 0 then
> + elseif level > 0 or level == -3 then
> check(name,d,n+1)
> elseif level == 0 then
> -- happens elsewhere, check this for block
> end
> synchronize(name,d)
> end
> end
>
> function counters.add(name,n,delta)
> local cd = counterdata[name]
> if cd and (cd.state == v_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
> if trace_counters then
> report_counters("adding, name: %s, level: manually, action: no checking",name)
> end
> elseif level == -2 then
> -- -2 is signal that we work per text
> if trace_counters then
> report_counters("adding, name: %s, level: text, action: checking",name)
> end
> check(name,data,n+1)
> - elseif level > 0 then
> + elseif level > 0 or level == -3 then
> -- within countergroup
> if trace_counters then
> report_counters("adding, name: %s, level: %s, action: checking within group",name,level)
> end
> check(name,data,n+1)
> elseif level == 0 then
> -- happens elsewhere
> if trace_counters then
> report_counters("adding, name: %s, level: %s, action: no checking",name,level)
> end
> else
> if trace_counters then
> report_counters("adding, name: %s, level: unknown, action: no checking",name)
> end
> end
> synchronize(name,d)
> return d.number -- not needed
> end
> return 0
> end
>
> function counters.check(level)
> for name, cd in next, counterdata do
> - if cd.level == level then
> + if cd.level == level or level > 0 and cd.level == -3 then
> if trace_counters then
> report_counters("resetting, name: %s, level: %s",name,level)
> end
> reset(name)
> end
> end
> end
>
>
> The question for the counter.check function is if it should also reset the counter for each new sectionblock (replace my change above with “if cd.level == level then”) or only at the start of a new section (that’s how my change behaves now).
Here is a example to test the option:
\definecounter[test]
%\setupcounter[test][way=bytext]
\setupcounter[test][way=byhead]
\define\test
{\incrementcounter[test]%
\convertedcounter[test]}
\starttext
\startbodymatter
\chapter{Chapter}\test
\section{Section}\test
\subsection{Subsection}\test
\subsubsection{Subsubsection}\test
\stopbodymatter
\startbackmatter
%\chapter{Another chapter}
\test
\stopbackmatter
\stoptext
Wolfgang
More information about the dev-context
mailing list