Changes

Jump to navigation Jump to search
1,169 bytes added ,  02:02, 8 January 2019
Extended section to discuss how to access buffer names from lua based on mailing list thread.
* <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>
1

edit

Navigation menu