ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Checking existence of a macro (control sequence) by Lua
Date: Fri, 29 May 2020 09:36:25 +0200	[thread overview]
Message-ID: <6eb726fb-aae8-7750-0d29-860a8d48059a@xs4all.nl> (raw)
In-Reply-To: <263ba20f41f00d3da9433eaf9a66e5b7@vivaldi.net>

On 5/28/2020 11:24 PM, context@vivaldi.net wrote:
> Hello,
> 
> one more question - why macros \Undefined and \DoNothing show "defined" 
> although I (tried to) undefine them?
> 
> ----
> \starttext
>    \def\MyMacro{Ahoj}
>    \def\MyMac#1#2{Something}
> 
>    \def\Undefined{}
>    \let\Undefined\undefined
> 
>    \def\DoNothing{}
>    \let\DoNothing\donothing
> 
>    \startluacode
>      local str = { [true] = "defined", [false] = "undefined" }
> 
>      local function whatever(s)
>        context.type("\\" .. s)
>        context(" is " .. (tokens.defined(s) and "defined" or 
> "undefined")) --% Hans' way
>        context(" and is " .. str[token.is_defined(s)] .. ".") --% 
> Henri's way
>        context.par()
>      end
> 
>      whatever("MyMacro")
>      whatever("MyMacroD")
>      whatever(" ")
>      whatever("-")
>      whatever("Undefined")
>      whatever("DoNothing")
>    \stopluacode
> 
>    %\Undefined % Causes "! Undefined control sequence"
> \stoptext
> ----
> 
> Gives:
> 
> "
> \MyMacro is defined and is defined.
> \MyMacroD is undefined and is undefined.
> \ is defined and is defined.
> \- is defined and is defined.
> \Undefined is defined and is defined.
> \DoNothing is defined and is defined.
> "
Because the macro actually *is* defined: as soon as tex sees

\foo

it reserves the name and gives it the meaning undefined, so even 
\undefined is defined.

Anyway, that is why we have \ifdefined that does a different kind of 
checking. In retrospect, that is a better one, so I'll adapt that in lmtx.

\starttext
   \let\MyMacroA\undefined

   \startluacode
     local function whatever(s)
         context.type("\\" .. s)
         context(" is " .. (tokens.defined(s,true) and "defined" or 
"undefined")) --% Hans' way
         context(" and has meaning " .. (tokens.defined(s) and "defined" 
or "undefined")) --% Hans' way
         context.par()
     end

     whatever("MyMacroA")
     whatever("MyMacroB")
   \stopluacode

\stoptext

\MyMacroA is defined and has meaning undefined
\MyMacroB is undefined and has meaning undefined


You can do this in current luatex/mkiv:

     if CONTEXTLMTXMODE == 0 then

         local d = tokens.defined
         local c = tokens.create

         function tokens.defined(s,b)
             if b then
                 return d(s)
             else
                 return c(s).cmd_name == "undefined_cmd"
             end
         end

     end

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
___________________________________________________________________________________

  reply	other threads:[~2020-05-29  7:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-27 22:42 context
2020-05-27 22:49 ` luigi scarso
2020-05-28  7:56   ` Hans Hagen
2020-05-28  6:50 ` Henri Menke
2020-05-28  8:34 ` Hans Hagen
2020-05-28 11:24   ` context
2020-05-28 21:24   ` context
2020-05-29  7:36     ` Hans Hagen [this message]
2020-05-29 21:21       ` 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=6eb726fb-aae8-7750-0d29-860a8d48059a@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=ntg-context@ntg.nl \
    /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).