Changes

Jump to navigation Jump to search
1,328 bytes added ,  09:00, 9 March 2021
m
category again
== A bit about buffers ==
Buffers are named chunks of text, saved by ConTeXt to be retrieved by the user later. They are meant for storing information not being typeset, yet. Typeset material is stored in [[TeX Boxes|Boxes]]. Buffers are usually defined as follows:
<texcode>
% Start a buffer with a custom name
* <code>buffers.exists(b)</code>: Returns the name of the buffer if it exists, or else <code>nil</code>.
* <code>buffers.collectcontent(names, seperator)</code>: Returns the contents of the buffers in <code>names</code>, seperated by <code>seperator</code> (<code>'\n'</code> by default). Names can be either a table, or a comma-seperated string (surrounding braces are automatically removed &mdash; see <code>parsers.settings_to_array</code> in <code>util-prs.lua</code>.
 
== Accessing buffer names ==
 
From the [https://mailman.ntg.nl/pipermail/ntg-context/2019/093692.html mailing list]:
 
It's not easy to access buffer names from ConTeXt because ConTeXt stores the buffers in a local variable <code>cache</code> which is an upvalue for all the other functions. However, you can use the Lua <code>debug</code> library to access upvalues. The <code>buffers.erase</code> function only has a single upvalue which is <code>cache</code>, making it easy to extract all the buffer names from that. However, you can't get them in the order of declaration because <code>cache</code> is a hashmap and not an array. Moreover, ConTeXt is usually in sandboxing mode. To get access to the `debug` library you have to run ConTeXt with:
 
<code>context --debug test.tex</code>
 
MWE:
 
<texcode>
\starttext
 
\startbuffer[ex1]
Buffer 1
\stopbuffer
 
\startbuffer[ex2]
Buffer 2
\stopbuffer
 
\startluacode
local _, cache = debug.getupvalue(buffers.erase,1)
local names = {}
for name,_ in pairs(cache) do
names[#names+1] = name
end
context(table.concat(names,", "))
\stopluacode
% Prints ex2, ex1 (but specific order is not guaranteed)
 
\stoptext
</texcode>
 
[[Category:ConTeXt]] [[Category:Basics]]
138

edits

Navigation menu