[NTG-context] Evaluating a Lua expression at the end (repeat)

Henri Menke henrimenke at gmail.com
Thu Aug 25 14:19:22 CEST 2016


Hi Kumar,

the function job.variables.save seems to save the key in job.variables.collected.macros instead of job.variables.collected.  The problem is that macros is not created unconditionally, which is why you might run into a »attempt to index a nil value« error without some manual error checking.  Therefore I recommend assigning the temp value to job.variables.tobesaved["document:temp:"..name].  Perhaps Hans joins this discussion and schools me about why this could be a bad idea, but so far it works.

Also, I'd recommend making all your user level macros protected and check for the optional argument in \startwhatever (otherwise you'll get errors like »Macro doesn't match its definition«).  Because I'm really cautious I also use \luaescapestring{#1} (Try for example »foo"bar« as a value without).

\starttext

\startluacode
local name = nil
local temp = 0

function document.startwhatever(s)
   name = s
   temp = 0
end

function document.addwhatever(n)
   temp = temp + n
   context(n)
end

function document.stopwhatever()
   job.variables.tobesaved["document:temp:"..name] = temp
end

function document.getwhatever(s)
   context(job.variables.collected["document:temp:"..s])
end
\stopluacode

\define\startwhatever{\dosingleempty\dostartwhatever}
\def\dostartwhatever[#1]{\ctxlua{document.startwhatever("\luaescapestring{#1}")}}
\define   \stopwhatever {\ctxlua{document.stopwhatever()}}
\define[1]\addwhatever  {\ctxlua{document.addwhatever(\luaescapestring{#1})}}
\define[1]\getwhatever  {\ctxlua{document.getwhatever("\luaescapestring{#1}")}}

total: \getwhatever{foo}

\startwhatever[foo]

test 1: \addwhatever{10}\par
test 2: \addwhatever{20}\par
test 3: \addwhatever{30}\par

\stopwhatever

\stoptext

On 08/25/2016 01:28 PM, Kumar Appaiah wrote:
> Dear List,
> 
> Last year, Hans helped me with this example:
> 
> 
> \starttext
> 
> \startluacode
>      local name = nil
>           local temp = 0
> 
>      function document.startwhatever(s)
>            name = s
>            temp = 0
>             end
>          function document.addwhatever(n)
>           temp = temp + n
>            context(n)
>         end
>      function
>               document.stopwhatever()
>                job.variables.save("document:temp:"..name,temp)
>             end
>          function document.getwhatever(s)
>           context(job.variables.collected["document:temp:"..s])
>        end
>        \stopluacode
> 
> \def\startwhatever[#1]{\ctxlua{document.startwhatever("#1")}}
> \def\stopwhatever     {\ctxlua{document.stopwhatever()}}
> \def\addwhatever    #1{\ctxlua{document.addwhatever(#1)}}
> \def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}
> 
> total: \getwhatever{foo}
> 
> \startwhatever[foo]
> 
> test 1: \addwhatever{10}\par
> test 2: \addwhatever{20}\par
> test 3: \addwhatever{30}\par
> 
> \stopwhatever
> 
> \stoptext
> 
> However, on my current ConTeXt on Debian (2016.05.17.20160523-1), I am
> unable to get it working. It says:
> 
> lua error       > lua error on line 37 in file /tmp/test.tex:
> 
> /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: attempt to
> index upvalue 'tobesavedmacros' (a nil value)
> stack traceback:
>         /usr/share/texmf/tex/context/base/mkiv/core-uti.lua:165: in
> 	function 'save'
> 	        [ctxlua]:14: in function 'stopwhatever'
> 		        [ctxlua]:1: in main chunk
> 
> 27     \def\getwhatever    #1{\ctxlua{document.getwhatever("#1")}}
> 28
> 29     total: \getwhatever{foo}
> 30
> 31     \startwhatever[foo]
> 32
> 33     test 1: \addwhatever{10}\par
> 34     test 2: \addwhatever{20}\par
> 35     test 3: \addwhatever{30}\par
> 36
> 37 >>  \stopwhatever
> 38
> 39     \stoptext
> 40
> 41
> 
> Could you please suggest a workaround?
> 
> Thanks.
> 
> Kumar
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context at ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________
> 



More information about the ntg-context mailing list