[NTG-context] \ctxlua and \startluacode ... \stopluacode

Hans Hagen j.hagen at xs4all.nl
Mon Oct 14 19:19:53 CEST 2019


On 10/14/2019 6:50 PM, Rudolf Bahr wrote:
> 
> Dear List!
> 
> THis MWE must be faulty, but where is the error?
> 
> ====================================================
> 
> \startluacode
> 
> userdata = userdata or {}
> 
> function userdata.file_exists (name)
> 
>     local name = name
>     local f    = assert ( io.open ( name, "r" ))
> 
>     if f then
>        f:close ()
>        context ( name )
>        context ( "  exists!" )
>     else
>        context ( name )
>        context ( "  doesn't exist!" )
>     end
>     
> end
> 
> \stopluacode
> 
> 
> \starttext
> 
> \def\lookupfile#1%
> 	{\ctxlua{userdata.file_exists([==[#1]==])}}
> 
> \lookupfile{"$HOME/context-tests/AAA.lua"}	% "AAA.lua" intentionally does    exist
> \lookupfile{"$HOME/context-tests/BBB.lua"}	% "BBB.lua" intentionally doesn't exist
> 
> \stoptext
> 
> ====================================================
> 
> It makes no difference in commenting out the first or the second "\lookupfile" command.
> The error message eventually is always nearly the same:
> 
> token call, execute: [ctxlua]:8: "$HOME/context-tests/AAA.lua": No such file or directory
> token call, execute: [ctxlua]:8: "$HOME/context-tests/BBB.lua": No such file or directory
You're in a tex environment, so no $HOME expansion. You can use the 
prefix resolvers:

\startluacode
     function userdata.file_exists(name)
         local name = name and resolvers.resolve(name)
         if name and lfs.isfile(name) then
             context("file %a exists",name)
         else
             context("file %a doesn't exist",name)
         end
     end
\stopluacode

\starttext

   % \def\lookupfile#1{\ctxlua{userdata.file_exists([==[#1]==])}}
 
\def\lookupfile{\ctxlua{userdata.file_exists(tokens.scanners.string())}}

     \lookupfile{home:context-tests/AAA.lua}{xx}
     \lookupfile{home:context-tests/BBB.lua}{xx}

\stoptext



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list