ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: context@vivaldi.net
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Checking existence of a macro (control sequence) by Lua
Date: Thu, 28 May 2020 13:24:45 +0200	[thread overview]
Message-ID: <5b0b27e8c2d61f74858bf2a0e6c47d74@vivaldi.net> (raw)
In-Reply-To: <e3765ad9-34e4-8e24-e0bb-324078a9267b@xs4all.nl>

Hello,

thanks Henri and Hans for all solutions!

On 2020-05-28 10:34, Hans Hagen wrote:
> On 5/28/2020 12:42 AM, context@vivaldi.net wrote:
>> Hello,
>> 
>> is it possible to check whether a macro exists (or - is it a 
>> non-\undefined control sequence) with Lua? Suppose:
>> 
>> ----
>> \starttext
>>    \def\MyMacro{Ahoj}
>>    \def\MyMac#1#2{Something}
>> 
>>    \startluacode
>>      IsDefined = function(ctl_seq)
>>        print("CS " .. ctl_seq ..
>>                                  (tex.IsCS(ctl_seq) -- Or what to come 
>> here?
>>                                                     "is defined" or 
>> "is unknown") .. ".")
>>      end
>> 
>>      -- So the function should print to the console:
>> 
>>      IsDefined("MyMacro") --> "CS MyMacro is defined."
>>      IsDefined("MyMac") --> "CS MyMac is defined."
>>      IsDefined("bf") --> "CS bf is defined."
>>      IsDefined("dummy") --> "CS dummy is unknown."
>> 
>>    \stopluacode
>> \stoptext
>> ----
>> 
>> Is it possible?
> A lot is possible. Take this:
> 
>            \def\MyMacroA{Ahoj A}
> \unexpanded\def\MyMacroB{Ahoj B}
> \frozen    \def\MyMacroC{Ahoj C} % in lmtx
>          % \def\MyMacroC{Ahoj C} % barks
> 
> At the tex end you can check for several properties:
> 
> \ifusercmd \hbox     Y\else N\fi % in lmtx
> \iffrozen  \hbox     Y\else N\fi % in lmtx
> \ifusercmd \MyMacroA Y\else N\fi % in lmtx
> \iffrozen  \MyMacroA Y\else N\fi % in lmtx
> \iffrozen  \MyMacroC Y\else N\fi % in lmtx
> 
> At the Lua end you can check for being defined:
> 
> \startluacode
>     local function whatever(s)
>         context.type("\\" .. s)
>         context(" is %s", tokens.defined(s) and "defined" or 
> "undefined")
>         context.par()
>     end
>     whatever("MyMacroA")
>     whatever("MyMacroD")
>     whatever(" ")
>     whatever("-")
> \stopluacode
> 
> alternatively you can say:
> 
> \startluacode
>    local t = token.create("MyMacro")
>    print(t.cmdname == "undefined_cs")
> \stopluacode
> 
> which is less efficient unless you want to access more properties.
> 
> Hans

(@Hans: I'm using vivaldi's mail account in "good will" - I didn't 
suppose it would cause any unwanted actions, like resending mails or 
whatever.)

Bets regards,

Lukas
___________________________________________________________________________________
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-28 11:24 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 [this message]
2020-05-28 21:24   ` context
2020-05-29  7:36     ` Hans Hagen
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=5b0b27e8c2d61f74858bf2a0e6c47d74@vivaldi.net \
    --to=context@vivaldi.net \
    --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).