ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Get list of all buffer names from lua
@ 2019-01-08  0:37 Stanislav Sokolenko
  2019-01-08  1:06 ` Henri Menke
  2019-01-08  9:31 ` Hans Hagen
  0 siblings, 2 replies; 6+ messages in thread
From: Stanislav Sokolenko @ 2019-01-08  0:37 UTC (permalink / raw)
  To: mailing list for ConTeXt users, stanislav

Dear list,

As the subject line states, I am looking for a means of retrieving a 
table of all saved buffer names from lua. A MNWE would looks like:

\starttext
\startbuffer[ex1]
Buffer 1
\stopbuffer
\startbuffer[ex2]
Buffer 2
\stopbuffer
% Should return {ex1, ex2} or similar
\ctxlua{context(...)}
\stoptext

The underlying idea is to store a large number of example problems in 
individual buffers that could be retrieved either by specific name or as 
a complete list. I'm having a problem with the latter as it seems like 
all the lua buffer commands I've encountered assume that buffer names 
are known in advance:

buffers.getcontent(b)
buffers.raw(b)
buffers.getlines(b)
buffers.erase(b)
buffers.assign(b, text, catcodes)
buffers.append(b, text)
buffers.exists(b)
buffers.collectcontent(names, seperator)

Thanks,

Stan



___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Get list of all buffer names from lua
  2019-01-08  0:37 Get list of all buffer names from lua Stanislav Sokolenko
@ 2019-01-08  1:06 ` Henri Menke
  2019-01-08  1:29   ` Stanislav Sokolenko
  2019-01-08  9:31 ` Hans Hagen
  1 sibling, 1 reply; 6+ messages in thread
From: Henri Menke @ 2019-01-08  1:06 UTC (permalink / raw)
  To: ntg-context

On 8/01/19 1:37 PM, Stanislav Sokolenko wrote:
> Dear list,
> 
> As the subject line states, I am looking for a means of retrieving a
> table of all saved buffer names from lua. A MNWE would looks like:

It's not so easy because ConTeXt stores the buffers in a local variable
`cache` which is an upvalue for all the other functions.  However, you
can use the Lua `debug` library to access upvalues.  I access the first
upvalue of `buffers.erase`, because it only has a single upvalue which
is `cache`.  Then I can easily extract all the names from that.  You
can't get them in the order of declaration because `cache` 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

    context --debug test.tex

MWE:

\starttext
\startbuffer[ex1]
Buffer 1
\stopbuffer
\startbuffer[ex2]
Buffer 2
\stopbuffer
% Should return {ex1, ex2} or similar
\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
\stoptext

> 
> \starttext
> \startbuffer[ex1]
> Buffer 1
> \stopbuffer
> \startbuffer[ex2]
> Buffer 2
> \stopbuffer
> % Should return {ex1, ex2} or similar
> \ctxlua{context(...)}
> \stoptext
> 
> The underlying idea is to store a large number of example problems in
> individual buffers that could be retrieved either by specific name or as
> a complete list. I'm having a problem with the latter as it seems like
> all the lua buffer commands I've encountered assume that buffer names
> are known in advance:
> 
> buffers.getcontent(b)
> buffers.raw(b)
> buffers.getlines(b)
> buffers.erase(b)
> buffers.assign(b, text, catcodes)
> buffers.append(b, text)
> buffers.exists(b)
> buffers.collectcontent(names, seperator)
> 
> Thanks,
> 
> Stan
> 
> 
> 
> ___________________________________________________________________________________
> 
> If your question is of interest to others as well, please add an entry
> to the Wiki!
> 
> maillist : ntg-context@ntg.nl /
> http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Get list of all buffer names from lua
  2019-01-08  1:06 ` Henri Menke
@ 2019-01-08  1:29   ` Stanislav Sokolenko
  2019-01-08  1:39     ` Henri Menke
  0 siblings, 1 reply; 6+ messages in thread
From: Stanislav Sokolenko @ 2019-01-08  1:29 UTC (permalink / raw)
  To: ntg-context

On 2019-01-07 9:06 p.m., Henri Menke wrote:

> On 8/01/19 1:37 PM, Stanislav Sokolenko wrote:
>> Dear list,
>>
>> As the subject line states, I am looking for a means of retrieving a
>> table of all saved buffer names from lua. A MNWE would looks like:
> It's not so easy because ConTeXt stores the buffers in a local variable
> `cache` which is an upvalue for all the other functions.  However, you
> can use the Lua `debug` library to access upvalues.  I access the first
> upvalue of `buffers.erase`, because it only has a single upvalue which
> is `cache`.  Then I can easily extract all the names from that.  You
> can't get them in the order of declaration because `cache` 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
>
>      context --debug test.tex
>
> MWE:
>
> \starttext
> \startbuffer[ex1]
> Buffer 1
> \stopbuffer
> \startbuffer[ex2]
> Buffer 2
> \stopbuffer
> % Should return {ex1, ex2} or similar
> \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
> \stoptext

That's perfect, thank you! I did see the cache variable in buff-ini.lua 
but didn't realize it was being used as an upvalue in a closure. Is 
there a specific reason for using the --debug flag rather than just 
loading the debug module directly in the code? The following seems to 
work without the --debug flag but I want to make sure I'm not causing 
some sort of side effect.

\starttext
\startbuffer[ex1]
Buffer 1
\stopbuffer
\startbuffer[ex2]
Buffer 2
\stopbuffer
% Should return {ex1, ex2} or similar
\startluacode
local debug = require('debug')
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
\stoptext

>
>> \starttext
>> \startbuffer[ex1]
>> Buffer 1
>> \stopbuffer
>> \startbuffer[ex2]
>> Buffer 2
>> \stopbuffer
>> % Should return {ex1, ex2} or similar
>> \ctxlua{context(...)}
>> \stoptext
>>
>> The underlying idea is to store a large number of example problems in
>> individual buffers that could be retrieved either by specific name or as
>> a complete list. I'm having a problem with the latter as it seems like
>> all the lua buffer commands I've encountered assume that buffer names
>> are known in advance:
>>
>> buffers.getcontent(b)
>> buffers.raw(b)
>> buffers.getlines(b)
>> buffers.erase(b)
>> buffers.assign(b, text, catcodes)
>> buffers.append(b, text)
>> buffers.exists(b)
>> buffers.collectcontent(names, seperator)
>>
>> Thanks,
>>
>> Stan
>>
>>
>>
>> ___________________________________________________________________________________
>>
>> If your question is of interest to others as well, please add an entry
>> to the Wiki!
>>
>> maillist : ntg-context@ntg.nl /
>> http://www.ntg.nl/mailman/listinfo/ntg-context
>> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
>> archive  : https://bitbucket.org/phg/context-mirror/commits/
>> wiki     : http://contextgarden.net
>> ___________________________________________________________________________________
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Get list of all buffer names from lua
  2019-01-08  1:29   ` Stanislav Sokolenko
@ 2019-01-08  1:39     ` Henri Menke
  2019-01-08  9:29       ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Henri Menke @ 2019-01-08  1:39 UTC (permalink / raw)
  To: ntg-context

On 8/01/19 2:29 PM, Stanislav Sokolenko wrote:
> That's perfect, thank you! I did see the cache variable in buff-ini.lua
> but didn't realize it was being used as an upvalue in a closure. Is
> there a specific reason for using the --debug flag rather than just
> loading the debug module directly in the code? The following seems to
> work without the --debug flag but I want to make sure I'm not causing
> some sort of side effect.

Interesting... Actually it should not work, because this way you can
escape the sandboxing.  I guess Hans will fix that in the future, so you
shouldn't rely on that.

@Hans: To disable debug completely you should add
`package.loaded["debug"] = nil` somewhere.

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Get list of all buffer names from lua
  2019-01-08  1:39     ` Henri Menke
@ 2019-01-08  9:29       ` Hans Hagen
  0 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2019-01-08  9:29 UTC (permalink / raw)
  To: Henri Menke, ntg-context

On 1/8/2019 2:39 AM, Henri Menke wrote:
> On 8/01/19 2:29 PM, Stanislav Sokolenko wrote:
>> That's perfect, thank you! I did see the cache variable in buff-ini.lua
>> but didn't realize it was being used as an upvalue in a closure. Is
>> there a specific reason for using the --debug flag rather than just
>> loading the debug module directly in the code? The following seems to
>> work without the --debug flag but I want to make sure I'm not causing
>> some sort of side effect.
> 
> Interesting... Actually it should not work, because this way you can
> escape the sandboxing.  I guess Hans will fix that in the future, so you
> shouldn't rely on that.
> 
> @Hans: To disable debug completely you should add
> `package.loaded["debug"] = nil` somewhere.
we never disable it completely (we keep the traceback for instance) but 
indeed we also need to adapt loaded

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Get list of all buffer names from lua
  2019-01-08  0:37 Get list of all buffer names from lua Stanislav Sokolenko
  2019-01-08  1:06 ` Henri Menke
@ 2019-01-08  9:31 ` Hans Hagen
  1 sibling, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2019-01-08  9:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Stanislav Sokolenko

On 1/8/2019 1:37 AM, Stanislav Sokolenko wrote:
> Dear list,
> 
> As the subject line states, I am looking for a means of retrieving a 
> table of all saved buffer names from lua. A MNWE would looks like:
> 
> \starttext
> \startbuffer[ex1]
> Buffer 1
> \stopbuffer
> \startbuffer[ex2]
> Buffer 2
> \stopbuffer
> % Should return {ex1, ex2} or similar
> \ctxlua{context(...)}
> \stoptext
> 
> The underlying idea is to store a large number of example problems in 
> individual buffers that could be retrieved either by specific name or as 
> a complete list. I'm having a problem with the latter as it seems like 
> all the lua buffer commands I've encountered assume that buffer names 
> are known in advance:
> 
> buffers.getcontent(b)
> buffers.raw(b)
> buffers.getlines(b)
> buffers.erase(b)
> buffers.assign(b, text, catcodes)
> buffers.append(b, text)
> buffers.exists(b)
> buffers.collectcontent(names, seperator)
be aware of the fact that such a list would also include buffers not 
created by you (ones that the system uses)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-01-08  9:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-08  0:37 Get list of all buffer names from lua Stanislav Sokolenko
2019-01-08  1:06 ` Henri Menke
2019-01-08  1:29   ` Stanislav Sokolenko
2019-01-08  1:39     ` Henri Menke
2019-01-08  9:29       ` Hans Hagen
2019-01-08  9:31 ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).