ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context]  Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition
@ 2024-01-25  8:18 Gerion Entrup
  2024-01-29  8:30 ` [NTG-context] " Taco Hoekwater
  2024-01-29 15:31 ` Wolfgang Schuster
  0 siblings, 2 replies; 3+ messages in thread
From: Gerion Entrup @ 2024-01-25  8:18 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1.1: Type: text/plain, Size: 1484 bytes --]

Hi,

I want to make a proper definition of a new command to be able to
process it in Lua. I found something on StackOverflow [1] but that
breaks in enumeration _and_ when interaction is enabled and I do not
understand why? Can you give me some help here?

I want to make commands like \foo{bla} and \foo[b]{bla} possible at the
same time. Here is a minimal example (also attached):

```
\startluacode
function userdata.my_command(keywords, text)
	commands.writestatus("test", string.format("text: %s, config: %s", text, keywords))
    local cfg = utilities.parsers.settings_to_hash(keywords)

	if cfg['emph'] ~= nil then
		context("\\emph{" .. text .. "}")
	elseif cfg['bold'] ~= nil then
		context("\\bold{" .. text .. "}")
	else
		context(text)
	end
end
\stopluacode

% without that line there is no problem
\setupinteraction[state=start]

\defineenumeration[enu][text=Enu]

\unprotect
\def\mc{\dosingleempty\do_my_command}
\def\do_my_command[#1]#2{%
 	\iffirstargument{%
		\ctxlua{userdata.my_command('#1', [==[#2]==])}%
	}\else {%
		\ctxlua{userdata.my_command('', [==[#2]==])}%
	}\fi
}
\protect

\starttext

% all the following works
\mc{foo} \mc[bold]{foo} \mc[emph]{foo}

\startenu[title=Some]
\stopenu

% this breaks
\startenu[title=Some \mc{foo}]
\stopenu

\stoptext
```

The compilation fails with:
```
tex error       > tex error on line 41 in file ./test.tex: Use of \do_my_command doesn't match its definition
```

Best,
Gerion

[1] https://tex.stackexchange.com/a/55778

[-- Attachment #1.1.2: test.tex --]
[-- Type: text/x-tex, Size: 889 bytes --]

\startluacode
function userdata.my_command(keywords, text)
	commands.writestatus("test", string.format("text: %s, config: %s", text, keywords))
    local cfg = utilities.parsers.settings_to_hash(keywords)

	if cfg['emph'] ~= nil then
		context("\\emph{" .. text .. "}")
	elseif cfg['bold'] ~= nil then
		context("\\bold{" .. text .. "}")
	else
		context(text)
	end
end
\stopluacode

% without that line there is no problem
\setupinteraction[state=start]

\defineenumeration[enu][text=Enu]

\unprotect
\def\mc{\dosingleempty\do_my_command}
\def\do_my_command[#1]#2{%
 	\iffirstargument{%
		\ctxlua{userdata.my_command('#1', [==[#2]==])}%
	}\else {%
		\ctxlua{userdata.my_command('', [==[#2]==])}%
	}\fi
}
\protect

\starttext

% all the following works
\mc{foo} \mc[bold]{foo} \mc[emph]{foo}

\startenu[title=Some]
\stopenu

% this breaks
\startenu[title=Some \mc{foo}]
\stopenu

\stoptext

[-- Attachment #1.2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition
  2024-01-25  8:18 [NTG-context] Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition Gerion Entrup
@ 2024-01-29  8:30 ` Taco Hoekwater
  2024-01-29 15:31 ` Wolfgang Schuster
  1 sibling, 0 replies; 3+ messages in thread
From: Taco Hoekwater @ 2024-01-29  8:30 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

You can try: 

> \unexpanded\def\do_my_command[#1]#2{%

But there is probably a cleaner way to implement optional arguments with lua?

Best wishes,
Taco

> On 25 Jan 2024, at 09:18, Gerion Entrup <gerion.entrup@flump.de> wrote:
> 
> Hi,
> 
> I want to make a proper definition of a new command to be able to
> process it in Lua. I found something on StackOverflow [1] but that
> breaks in enumeration _and_ when interaction is enabled and I do not
> understand why? Can you give me some help here?
> 
> I want to make commands like \foo{bla} and \foo[b]{bla} possible at the
> same time. Here is a minimal example (also attached):
> 
> ```
> \startluacode
> function userdata.my_command(keywords, text)
> commands.writestatus("test", string.format("text: %s, config: %s", text, keywords))
>    local cfg = utilities.parsers.settings_to_hash(keywords)
> 
> if cfg['emph'] ~= nil then
> context("\\emph{" .. text .. "}")
> elseif cfg['bold'] ~= nil then
> context("\\bold{" .. text .. "}")
> else
> context(text)
> end
> end
> \stopluacode
> 
> % without that line there is no problem
> \setupinteraction[state=start]
> 
> \defineenumeration[enu][text=Enu]
> 
> \unprotect
> \def\mc{\dosingleempty\do_my_command}
> \def\do_my_command[#1]#2{%
> \iffirstargument{%
> \ctxlua{userdata.my_command('#1', [==[#2]==])}%
> }\else {%
> \ctxlua{userdata.my_command('', [==[#2]==])}%
> }\fi
> }
> \protect
> 
> \starttext
> 
> % all the following works
> \mc{foo} \mc[bold]{foo} \mc[emph]{foo}
> 
> \startenu[title=Some]
> \stopenu
> 
> % this breaks
> \startenu[title=Some \mc{foo}]
> \stopenu
> 
> \stoptext
> ```
> 
> The compilation fails with:
> ```
> tex error       > tex error on line 41 in file ./test.tex: Use of \do_my_command doesn't match its definition
> ```
> 
> Best,
> Gerion
> 
> [1] https://tex.stackexchange.com/a/55778
> <test.tex>___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
> webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
> archive  : https://github.com/contextgarden/context
> wiki     : https://wiki.contextgarden.net
> ___________________________________________________________________________________

— 
Taco Hoekwater              E: taco@bittext.nl
genderfluid (all pronouns)


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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition
  2024-01-25  8:18 [NTG-context] Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition Gerion Entrup
  2024-01-29  8:30 ` [NTG-context] " Taco Hoekwater
@ 2024-01-29 15:31 ` Wolfgang Schuster
  1 sibling, 0 replies; 3+ messages in thread
From: Wolfgang Schuster @ 2024-01-29 15:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Gerion Entrup

Gerion Entrup schrieb am 25.01.2024 um 09:18:
> Hi,
> 
> I want to make a proper definition of a new command to be able to
> process it in Lua. I found something on StackOverflow [1] but that
> breaks in enumeration _and_ when interaction is enabled and I do not
> understand why? Can you give me some help here?
> 
> I want to make commands like \foo{bla} and \foo[b]{bla} possible at the
> same time. Here is a minimal example (also attached):
> 
> ```
> \startluacode
> function userdata.my_command(keywords, text)
> 	commands.writestatus("test", string.format("text: %s, config: %s", text, keywords))
>      local cfg = utilities.parsers.settings_to_hash(keywords)
> 
> 	if cfg['emph'] ~= nil then
> 		context("\\emph{" .. text .. "}")
> 	elseif cfg['bold'] ~= nil then
> 		context("\\bold{" .. text .. "}")
> 	else
> 		context(text)
> 	end
> end
> \stopluacode

\startluacode
function userdata.my_command(keywords, text)
   local keywords = utilities.parsers.settings_to_hash(keywords)
   if keywords['emph'] then
     context.emph(text)
   elseif keywords['bold'] then
     context.bold(text)
   else
     context(text)
   end
end
\stopluacode

> % without that line there is no problem
> \setupinteraction[state=start]
> 
> \defineenumeration[enu][text=Enu]

\defineenumeration[enu][text=Enu,title=yes]

> \unprotect
> \def\mc{\dosingleempty\do_my_command}
> \def\do_my_command[#1]#2{%
>   	\iffirstargument{%
> 		\ctxlua{userdata.my_command('#1', [==[#2]==])}%
> 	}\else {%
> 		\ctxlua{userdata.my_command('', [==[#2]==])}%
> 	}\fi
> }
> \protect

\tolerant\protected\def\mc[#1]#2%
   {\ctxlua{userdata.my_command("#1","#2")}}

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-29 15:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25  8:18 [NTG-context] Own command definition breaks with interaction: Use of \do_my_command doesn't match its definition Gerion Entrup
2024-01-29  8:30 ` [NTG-context] " Taco Hoekwater
2024-01-29 15:31 ` Wolfgang Schuster

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).