[Dev-luatex] tex.currentgrouplevel

David Kastrup dak at gnu.org
Mon Sep 15 20:07:24 CEST 2008


"Jonathan Sauer" <Jonathan.Sauer at silverstroke.com> writes:

> Hello,
>
>> > -- Assume there are TeX two count registers allocated, `stackIndex'
>> > -- and `groupIndex'

groupLevel.  The code is a bit harder to follow for a human since the
names change throughout, and quite a bit harder to follow for Lua...

>> > tex.count.stackIndex = 0
>> > tex.count.groupLevel = -1
>> >
>> > -- The `grouped variable' stack
>> > local stack = { }
>> >
>> >
>> > -- Sets a `grouped variable'
>> > function set(name, value)
>> > 	if tex.currentgrouplevel > tex.count.grouplevel then
>> > 		-- New group opened since the last time. Create a new stack
>> > 		-- element
>> > 		tex.count.stackIndex = tex.count.stackIndex + 1
>> > 		stack[tex.count.stackIndex] = { }
>> > 		tex.count.groupLevel = tex.currentgrouplevel
>> > 		
>> > 		-- TODO: Optional: Clear the stack above the new element to
>> > 		-- release old values
>> > 	end
>> > 	
>> > 	-- Add the value
>> > 	stack[tex.count.stackIndex][name] = value end
>> >
>> >
>> > -- Gets a `grouped variable'
>> > function get(name)
>> > 	-- Look through the stack, top to bottom
>> > 	for i = tex.count.stackIndex, 1, -1 do
>> > 		if stack[i][name] then
>> > 			return stack[i][name]
>> > 		end
>> > 	end
>> > 	
>> > 	-- Value not found
>> > 	return nil
>> > end
>> 
>> Would not work anyway.  Local values would survive across
>> \endgroup\begingroup
>
> Why? At \endgroup, tex.count.stackIndex would be reset to the value it
> had at the beginning of the group (since it is a tex \count register,
> not a Lua variable). Then, after \begingroup, setting a variable would
> notice that tex.count.groupLevel (which would also have been reset to
> its previous value at \endgroup) is lower than tex.currentgrouplevel,
> and a new stack element would be created, replacing the previous
> element for this grouping level.

Ok, the group housekeeping of the TeX counters should do the trick here.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


More information about the dev-luatex mailing list