From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
Rudolf Bahr <quasi@quasi.de>
Subject: Re: \ctxlua and \startluacode ... \stopluacode
Date: Mon, 14 Oct 2019 19:19:53 +0200 [thread overview]
Message-ID: <866b77f5-c355-b158-4cd9-9c7d193abc00@xs4all.nl> (raw)
In-Reply-To: <20191014165044.GA3101@nan>
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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________
next prev parent reply other threads:[~2019-10-14 17:19 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-14 16:50 Rudolf Bahr
2019-10-14 16:55 ` RE : " Joseph
2019-10-14 17:08 ` Taco Hoekwater
2019-10-14 17:19 ` Hans Hagen [this message]
2019-10-14 19:07 ` Rudolf Bahr
2019-10-14 19:26 ` Taco Hoekwater
2019-10-14 22:50 ` Rudolf Bahr
2019-10-15 8:04 ` Taco Hoekwater
2019-10-15 8:15 ` Hans Hagen
2019-10-16 7:03 ` Rudolf Bahr
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=866b77f5-c355-b158-4cd9-9c7d193abc00@xs4all.nl \
--to=j.hagen@xs4all.nl \
--cc=ntg-context@ntg.nl \
--cc=quasi@quasi.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).