ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Henri Menke via ntg-context <ntg-context@ntg.nl>
To: ntg-context@ntg.nl
Cc: Henri Menke <henri@henrimenke.de>
Subject: Re: protected macro vs protected luacall
Date: Fri, 27 Jan 2023 10:35:44 +0100	[thread overview]
Message-ID: <2151cdb6c742a9f5db7e540bb65095729bde86bc.camel@henrimenke.de> (raw)
In-Reply-To: <2c0063d3-c4ce-d2fd-425f-2752900bda07@freedom.nl>

On Fri, 2023-01-27 at 10:30 +0100, Hans Hagen via ntg-context wrote:
> On 1/27/2023 10:13 AM, Henri Menke via ntg-context wrote:
> > On Thu, 2023-01-26 at 23:07 +0100, Hans Hagen via ntg-context
> > wrote:
> > > On 1/26/2023 9:33 PM, Henri Menke via ntg-context wrote:
> > > > \directlua{
> > > >     userdata = userdata or {}
> > > >     function userdata.test()
> > > >       tex.print("\string\\numexpr 17\string\\relax")
> > > >     end
> > > > }
> > > > 
> > > > \tt
> > > > \protected\def\test{\directlua{userdata.test()}}
> > > > \meaning\test\par
> > > > \edef\x{\test}\meaning\x\par
> > > > \the\test
> > > > 
> > > > \directlua{
> > > >     local t = lua.get_functions_table()
> > > >     t[\string#t + 1] = userdata.test
> > > >     token.set_lua("test", userdata.test, \string#t,
> > > > "protected")
> > > > }
> > > > \meaning\test\par
> > > > \edef\x{\test}\meaning\x\par
> > > > \the\test
> > > from the code it looks like we're talking plain luatex ...
> > > 
> > > \the is very selective and doesn't work for all primitives or
> > > macros
> > > 
> > > compare it to \number which accepts any number representation
> > > following
> > > it while \the doesn't work ok 123
> > > 
> > > that said, your set_lua is wrong and passes a function as well as
> > > an
> > > index
> > > 
> > > \directlua{
> > >      lua.get_functions_table()[999] = function()
> > >        tex.print("\string\\numexpr 17\string\\relax")
> > >      end
> > >      token.set_lua("test", 999)
> > > }
> > > 
> > > \the    \test % needs the \numexpr
> > > 
> > > \number \test % doesn't
> > > 
> > > \count0 \test % needs the \numexpr
> > > 
> > > \count0=\test % doesn't
> > > 
> > > etc
> > 
> > Thanks for pointing out my mistake. Your example works fine but now
> > the
> > luacall is no longer protected and will therefore fully expand in
> > \edef. So do I understand this correctly that there is no way to
> > make
> > the second \the\test work?
> > 
> > \directlua{
> >      lua.get_functions_table()[999] = function()
> >        tex.pri
> > nt("\string\\numexpr 17\string\\relax")
> >      end
> > }
> > 
> > \tt
> > \directlua{token.set_lua("test", 999)}
> > \meaning\test\par % expandable luacall 999
> > \edef\x{\test}\meaning\x\par % macro:->\numexpr 17\relax
> > \the\test % 17
> > 
> > \directlua{token.set_lua("test", 999, "protected")}
> > \meaning\test\par % luacall 999
> > \edef\x{\test}\meaning\x\par % macro:->\test
> > \the\test % ! You can't use `luacall 999' after \the.
> > 
> > \bye
> you can do
> 
> \directlua{
>     function TestCmd()
>       tex.print("\string\\numexpr 17\string\\relax")
>     end
> }
> 
> \protected\def\test{\directlua{TestCmd()}}
> 
> or, more efficient:
> 
> \directlua{
>     function TestCmd()
>       tex.print("17")
>     end
> }
> 
> \protected\def\test{\numexpr\directlua{TestCmd()}\relax}

Thanks, this is indeed the workaround that I currently use. However,
the downside is that this has to tokenize the contents of \directlua
every time (and is therefore susceptible to surrounding \catcode
shenanigans) and it expands in at minimum two steps, whereas a luacall
always expands in a single step. But I guess that's what I will have to
live with.

Cheers, Henri

> 
> 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 / 
> https://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : https://contextgarden.net
> _____________________________________________________________________
> ______________

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

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

  reply	other threads:[~2023-01-27  9:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-26 20:33 Henri Menke via ntg-context
2023-01-26 22:07 ` Hans Hagen via ntg-context
2023-01-27  9:13   ` Henri Menke via ntg-context
2023-01-27  9:30     ` Hans Hagen via ntg-context
2023-01-27  9:35       ` Henri Menke via ntg-context [this message]
2023-01-27 10:01         ` Hans Hagen via ntg-context
2023-01-27 11:03           ` Henri Menke via ntg-context
2023-01-27 11:52             ` Hans Hagen via ntg-context

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=2151cdb6c742a9f5db7e540bb65095729bde86bc.camel@henrimenke.de \
    --to=ntg-context@ntg.nl \
    --cc=henri@henrimenke.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).