On 11/27/2021 6:13 PM, Joey McCollum wrote:
All right, I think I've solved the expansion problem I described before: to ensure that the first input is expanded when it is passed to Lua, I have to pass it as [==[#1]==], not "#1". But the updated MWE below still does not seem to work, as the \DoIfPrefixElse macro is printing "NOP" instead of "YES":

```

\starttexdefinition loc [#1]

\doifassignmentelse{#1} {

% if an assignment, then parse and format accordingly

\getparameters[loc][#1]

% Was a section number specified?

\doifdefined{locsec} {

§ \locsec

}

} {

% otherwise, just print the input as-is

#1

}

\stoptexdefinition


\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}


\def\currentbtxloctext{\loc[sec=31]}


\starttext

\currentbtxloctext\blank

\DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}

\stoptext

```

Indeed, if I add a simple \doifelse equality check, it looks like the value I expect is not the same as what the macro produces, even though they look identical:

```

\starttexdefinition loc [#1]
     \doifassignmentelse{#1} {
         % if an assignment, then parse and format accordingly
         \getparameters[loc][#1]
         % Was a section number specified?
         \doifdefined{locsec} {
             § \locsec
         }
     } {
         % otherwise, just print the input as-is
         #1
     }
\stoptexdefinition

\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([==[#2]==],"^"..[==[#1]==]))}}

\def\currentbtxloctext{\loc[sec=31]}

\starttext
     § 31\blank%the raw text we expect
     \currentbtxloctext\blank%the text as produced by the macro
    \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank% should output YES, but doesn't     \DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}\blank% should output YES, but doesn't
\stoptext

```

What am I missing here?
Expansion hell ... and i fear that you draw yourself into more and more trouble with this approach (which is why you don't find that kind of hackery in the core unless we're real desperate) so maybe try to explain what the real problem is that needs to be solved. Parsing tex is seldom a solution (at least not in context).

You can add:

    \edef\Whatever{\currentbtxloctext}\meaning\Whatever
    \doifelse{\currentbtxloctext}{§ 31}{YES}{NOP}\blank

and see what comes back. Now, as always in tex, there's of course a solution because after all it's a programming language too (and at some point these solutions start looking so complex that one enters guru state)

\starttexdefinition loc [#1]
    \beginlocalcontrol
    \doifassignmentelse{#1} {
        \getparameters[loc][#1]
        \doifdefinedelse{locsec} {
        \endlocalcontrol
            § \locsec
        } {
            \endlocalcontrol
        }
    } {
        \endlocalcontrol
        #1
    }
\stoptexdefinition

a curious mix between a fully expanded result, using protected macros and hiding what tex does but hard to explain

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 via email to