ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* expansion and \processaction
@ 2012-06-07 12:05 Philipp Gesang
  2012-06-07 12:32 ` Wolfgang Schuster
  0 siblings, 1 reply; 6+ messages in thread
From: Philipp Gesang @ 2012-06-07 12:05 UTC (permalink / raw)
  To: ConTeXt ML


[-- Attachment #1.1: Type: text/plain, Size: 2652 bytes --]

Hi all,

my goal is a macro \definestates[foo][...,...] which takes a comma
list and creates a monadic macro \foo[n]. \foo[n] should return
either the nth item or, if (n > list length), a default. I
thought the right tools were \processcommalist to generate the
mapping n-><list item>, as well as \processaction to retrieve the
items. But naively I didn’t consider expansion ...

My code so far:

··8<····································································
\unprotect

\def\definestates{\dodoubleempty\do_define_states}

\def\do_define_states[#1][#2]{%
  \expandafter\edef\csname#1_states\endcsname{%
    \nstates0
    %% this is supposed to be expanded so we employ the \raw... variety
    \rawprocesscommalist[#2]\add_one_state%
    unknown=>\dummystate,%
    default=>\dummystate,%
  }%
  \do_do_define_states{#1}%
}

\let\dummystate\empty

\newcount\nstates
\unexpanded\def\add_one_state#1{%
  \advance\nstates\plusone
  \the\nstates=>#1,%
}

\def\do_do_define_states#1{%
  \expandafter\def\csname#1\endcsname
    {\expandafter\dosingleempty\csname do_#1\endcsname}%
  \expandafter\def\csname do_#1\endcsname[##1]{%
    \iffirstargument
      % <[DBG] cmd:#1, state:##1>\par
      \edef\currentstates{\csname#1_states\endcsname}%
      %% At this point \currentstates should yield the whole mapping so
      %% we can use it with \processaction. But ...
      % \show\currentstates
      % \currentstates\par
      %% ... here it still contains
      %%    \nstates 0 \rawprocesscommalist [foo,bar,baz] ...
      %% and the next directive has an empty result:
      \rawprocessaction[##1][\currentstates]%
    \fi
  }%
}

\protect
\starttext

\definestates[mystates][foo,bar,baz]
%% At this point I’d like \mystates_states to contain (literally)
%%   “1=>foo,2=>bar,3=>baz,unknown=>,default=>,”
%% so the following command
\mystates[1]  % -> foo
%% would be equivalent to
%%    \processaction[1][1=>foo,2=>bar,3=>baz,unknown=>,default=>,]
\mystates[2]  % -> bar
\mystates[42] % -> dummy

\stoptext
··8<····································································

My question: How can I use \[raw]processcommalist in an expanded
definition? How can I get \[raw]processcommalist to operate on
the expansion of its second argument? Does this even make sense?
If not, what is the proper way?

I’d be grateful for any advice
Philipp


PS: Yeah, I could use Lua for it but that’s not the question.


[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

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

___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: expansion and \processaction
  2012-06-07 12:05 expansion and \processaction Philipp Gesang
@ 2012-06-07 12:32 ` Wolfgang Schuster
  2012-06-07 15:34   ` Philipp Gesang
  0 siblings, 1 reply; 6+ messages in thread
From: Wolfgang Schuster @ 2012-06-07 12:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 07.06.2012 um 14:05 schrieb Philipp Gesang:

> Hi all,
> 
> my goal is a macro \definestates[foo][...,...] which takes a comma
> list and creates a monadic macro \foo[n]. \foo[n] should return
> either the nth item or, if (n > list length), a default. I
> thought the right tools were \processcommalist to generate the
> mapping n-><list item>, as well as \processaction to retrieve the
> items. But naively I didn’t consider expansion ...
> 
> My code so far:
> 
> ··8<····································································
> \unprotect
> 
> \def\definestates{\dodoubleempty\do_define_states}
> 
> \def\do_define_states[#1][#2]{%
>  \expandafter\edef\csname#1_states\endcsname{%
>    \nstates0
>    %% this is supposed to be expanded so we employ the \raw... variety
>    \rawprocesscommalist[#2]\add_one_state%
>    unknown=>\dummystate,%
>    default=>\dummystate,%
>  }%
>  \do_do_define_states{#1}%
> }
> 
> \let\dummystate\empty
> 
> \newcount\nstates
> \unexpanded\def\add_one_state#1{%
>  \advance\nstates\plusone
>  \the\nstates=>#1,%
> }
> 
> \def\do_do_define_states#1{%
>  \expandafter\def\csname#1\endcsname
>    {\expandafter\dosingleempty\csname do_#1\endcsname}%
>  \expandafter\def\csname do_#1\endcsname[##1]{%
>    \iffirstargument
>      % <[DBG] cmd:#1, state:##1>\par
>      \edef\currentstates{\csname#1_states\endcsname}%
>      %% At this point \currentstates should yield the whole mapping so
>      %% we can use it with \processaction. But ...
>      % \show\currentstates
>      % \currentstates\par
>      %% ... here it still contains
>      %%    \nstates 0 \rawprocesscommalist [foo,bar,baz] ...
>      %% and the next directive has an empty result:
>      \rawprocessaction[##1][\currentstates]%
>    \fi
>  }%
> }
> 
> \protect
> \starttext
> 
> \definestates[mystates][foo,bar,baz]
> %% At this point I’d like \mystates_states to contain (literally)
> %%   “1=>foo,2=>bar,3=>baz,unknown=>,default=>,”
> %% so the following command
> \mystates[1]  % -> foo
> %% would be equivalent to
> %%    \processaction[1][1=>foo,2=>bar,3=>baz,unknown=>,default=>,]
> \mystates[2]  % -> bar
> \mystates[42] % -> dummy
> 
> \stoptext
> ··8<····································································
> 
> My question: How can I use \[raw]processcommalist in an expanded
> definition? How can I get \[raw]processcommalist to operate on
> the expansion of its second argument? Does this even make sense?
> If not, what is the proper way?

\unprotect

\unexpanded\def\definestates
  {\dodoubleargument\definestates_indeed}

\def\definestates_indeed[#1][#2]%
  {\setuevalue{#1}{\states_entry[#2]}}

\unexpanded\def\states_entry
  {\dodoubleargument\states_entry_indeed}

\def\states_entry_indeed[#1][#2]%
  {\getcommalistsize[#1]%
   \ifnum#2>\commalistsize
     {\tttf NO ENTRY!}%
   \else
     \getfromcommalist[#1][#2]\commalistelement
   \fi}

\protect

\starttext

\definestates[mystates][foo,bar,baz]

\starttabulate
\NC  2 \EQ \mystates [2] \NC\NR
\NC 42 \EQ \mystates[42] \NC\NR
\stoptabulate

\stoptext

Wolfgang
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: expansion and \processaction
  2012-06-07 12:32 ` Wolfgang Schuster
@ 2012-06-07 15:34   ` Philipp Gesang
  2012-06-07 16:02     ` Wolfgang Schuster
  0 siblings, 1 reply; 6+ messages in thread
From: Philipp Gesang @ 2012-06-07 15:34 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 4384 bytes --]

Hi Wolfgang!

···<date: 2012-06-07, Thursday>···<from: Wolfgang Schuster>···

> 
> Am 07.06.2012 um 14:05 schrieb Philipp Gesang:
> 
> > Hi all,
> > 
> > my goal is a macro \definestates[foo][...,...] which takes a comma
> > list and creates a monadic macro \foo[n]. \foo[n] should return
> > either the nth item or, if (n > list length), a default. I
> > thought the right tools were \processcommalist to generate the
> > mapping n-><list item>, as well as \processaction to retrieve the
> > items. But naively I didn’t consider expansion ...
> > 
> > My code so far:
> > 
> > ··8<····································································
> > \unprotect
> > 
> > \def\definestates{\dodoubleempty\do_define_states}
> > 
> > \def\do_define_states[#1][#2]{%
> >  \expandafter\edef\csname#1_states\endcsname{%
> >    \nstates0
> >    %% this is supposed to be expanded so we employ the \raw... variety
> >    \rawprocesscommalist[#2]\add_one_state%
> >    unknown=>\dummystate,%
> >    default=>\dummystate,%
> >  }%
> >  \do_do_define_states{#1}%
> > }
> > 
> > \let\dummystate\empty
> > 
> > \newcount\nstates
> > \unexpanded\def\add_one_state#1{%
> >  \advance\nstates\plusone
> >  \the\nstates=>#1,%
> > }
> > 
> > \def\do_do_define_states#1{%
> >  \expandafter\def\csname#1\endcsname
> >    {\expandafter\dosingleempty\csname do_#1\endcsname}%
> >  \expandafter\def\csname do_#1\endcsname[##1]{%
> >    \iffirstargument
> >      % <[DBG] cmd:#1, state:##1>\par
> >      \edef\currentstates{\csname#1_states\endcsname}%
> >      %% At this point \currentstates should yield the whole mapping so
> >      %% we can use it with \processaction. But ...
> >      % \show\currentstates
> >      % \currentstates\par
> >      %% ... here it still contains
> >      %%    \nstates 0 \rawprocesscommalist [foo,bar,baz] ...
> >      %% and the next directive has an empty result:
> >      \rawprocessaction[##1][\currentstates]%
> >    \fi
> >  }%
> > }
> > 
> > \protect
> > \starttext
> > 
> > \definestates[mystates][foo,bar,baz]
> > %% At this point I’d like \mystates_states to contain (literally)
> > %%   “1=>foo,2=>bar,3=>baz,unknown=>,default=>,”
> > %% so the following command
> > \mystates[1]  % -> foo
> > %% would be equivalent to
> > %%    \processaction[1][1=>foo,2=>bar,3=>baz,unknown=>,default=>,]
> > \mystates[2]  % -> bar
> > \mystates[42] % -> dummy
> > 
> > \stoptext
> > ··8<····································································
> > 
> > My question: How can I use \[raw]processcommalist in an expanded
> > definition? How can I get \[raw]processcommalist to operate on
> > the expansion of its second argument? Does this even make sense?
> > If not, what is the proper way?
> 
> \unprotect
> 
> \unexpanded\def\definestates
>   {\dodoubleargument\definestates_indeed}
> 
> \def\definestates_indeed[#1][#2]%
>   {\setuevalue{#1}{\states_entry[#2]}}
> 
> \unexpanded\def\states_entry
>   {\dodoubleargument\states_entry_indeed}
> 
> \def\states_entry_indeed[#1][#2]%
>   {\getcommalistsize[#1]%
>    \ifnum#2>\commalistsize
>      {\tttf NO ENTRY!}%
>    \else
>      \getfromcommalist[#1][#2]\commalistelement
>    \fi}

Thanks very much. This solves my problem. Very convenient that I
can rewrite it to use commacommand.

Philipp

> 
> \protect
> 
> \starttext
> 
> \definestates[mystates][foo,bar,baz]
> 
> \starttabulate
> \NC  2 \EQ \mystates [2] \NC\NR
> \NC 42 \EQ \mystates[42] \NC\NR
> \stoptabulate
> 
> \stoptext
> 
> Wolfgang
> ___________________________________________________________________________________
> 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://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

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

___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: expansion and \processaction
  2012-06-07 15:34   ` Philipp Gesang
@ 2012-06-07 16:02     ` Wolfgang Schuster
  2012-06-07 16:16       ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Wolfgang Schuster @ 2012-06-07 16:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 07.06.2012 um 17:34 schrieb Philipp Gesang:

> Hi Wolfgang!
> 
> ···<date: 2012-06-07, Thursday>···<from: Wolfgang Schuster>···
> 
>> 
>> Am 07.06.2012 um 14:05 schrieb Philipp Gesang:
>> 
>>> Hi all,
>>> 
>>> my goal is a macro \definestates[foo][...,...] which takes a comma
>>> list and creates a monadic macro \foo[n]. \foo[n] should return
>>> either the nth item or, if (n > list length), a default. I
>>> thought the right tools were \processcommalist to generate the
>>> mapping n-><list item>, as well as \processaction to retrieve the
>>> items. But naively I didn’t consider expansion ...
>>> 
>>> My code so far:
>>> 
>>> ··8<····································································
>>> \unprotect
>>> 
>>> \def\definestates{\dodoubleempty\do_define_states}
>>> 
>>> \def\do_define_states[#1][#2]{%
>>> \expandafter\edef\csname#1_states\endcsname{%
>>>   \nstates0
>>>   %% this is supposed to be expanded so we employ the \raw... variety
>>>   \rawprocesscommalist[#2]\add_one_state%
>>>   unknown=>\dummystate,%
>>>   default=>\dummystate,%
>>> }%
>>> \do_do_define_states{#1}%
>>> }
>>> 
>>> \let\dummystate\empty
>>> 
>>> \newcount\nstates
>>> \unexpanded\def\add_one_state#1{%
>>> \advance\nstates\plusone
>>> \the\nstates=>#1,%
>>> }
>>> 
>>> \def\do_do_define_states#1{%
>>> \expandafter\def\csname#1\endcsname
>>>   {\expandafter\dosingleempty\csname do_#1\endcsname}%
>>> \expandafter\def\csname do_#1\endcsname[##1]{%
>>>   \iffirstargument
>>>     % <[DBG] cmd:#1, state:##1>\par
>>>     \edef\currentstates{\csname#1_states\endcsname}%
>>>     %% At this point \currentstates should yield the whole mapping so
>>>     %% we can use it with \processaction. But ...
>>>     % \show\currentstates
>>>     % \currentstates\par
>>>     %% ... here it still contains
>>>     %%    \nstates 0 \rawprocesscommalist [foo,bar,baz] ...
>>>     %% and the next directive has an empty result:
>>>     \rawprocessaction[##1][\currentstates]%
>>>   \fi
>>> }%
>>> }
>>> 
>>> \protect
>>> \starttext
>>> 
>>> \definestates[mystates][foo,bar,baz]
>>> %% At this point I’d like \mystates_states to contain (literally)
>>> %%   “1=>foo,2=>bar,3=>baz,unknown=>,default=>,”
>>> %% so the following command
>>> \mystates[1]  % -> foo
>>> %% would be equivalent to
>>> %%    \processaction[1][1=>foo,2=>bar,3=>baz,unknown=>,default=>,]
>>> \mystates[2]  % -> bar
>>> \mystates[42] % -> dummy
>>> 
>>> \stoptext
>>> ··8<····································································
>>> 
>>> My question: How can I use \[raw]processcommalist in an expanded
>>> definition? How can I get \[raw]processcommalist to operate on
>>> the expansion of its second argument? Does this even make sense?
>>> If not, what is the proper way?
>> 
>> \unprotect
>> 
>> \unexpanded\def\definestates
>>  {\dodoubleargument\definestates_indeed}
>> 
>> \def\definestates_indeed[#1][#2]%
>>  {\setuevalue{#1}{\states_entry[#2]}}
>> 
>> \unexpanded\def\states_entry
>>  {\dodoubleargument\states_entry_indeed}
>> 
>> \def\states_entry_indeed[#1][#2]%
>>  {\getcommalistsize[#1]%
>>   \ifnum#2>\commalistsize
>>     {\tttf NO ENTRY!}%
>>   \else
>>     \getfromcommalist[#1][#2]\commalistelement
>>   \fi}
> 
> Thanks very much. This solves my problem. Very convenient that I
> can rewrite it to use comma command.

You can also try to use a Lua variant.

\def\states_entry_indeed[#1][#2]%
 {\startlua
  local entries = utilities.parsers.settings_to_array("#1")
  local entry   = entries[#2]
  if entry then
    context(entry)
  else
    context("\\tttf NO ENTRY!")
  end
  \stoplua}

Wolfgang
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: expansion and \processaction
  2012-06-07 16:02     ` Wolfgang Schuster
@ 2012-06-07 16:16       ` Hans Hagen
  2012-06-07 16:55         ` Wolfgang Schuster
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2012-06-07 16:16 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 7-6-2012 18:02, Wolfgang Schuster wrote:

> You can also try to use a Lua variant.
>
> \def\states_entry_indeed[#1][#2]%
>   {\startlua
>    local entries = utilities.parsers.settings_to_array("#1")
>    local entry   = entries[#2]
>    if entry then
>      context(entry)
>    else
>      context("\\tttf NO ENTRY!")

shouldn't that be grouped?

>    end
>    \stoplua}

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | 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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: expansion and \processaction
  2012-06-07 16:16       ` Hans Hagen
@ 2012-06-07 16:55         ` Wolfgang Schuster
  0 siblings, 0 replies; 6+ messages in thread
From: Wolfgang Schuster @ 2012-06-07 16:55 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users


Am 07.06.2012 um 18:16 schrieb Hans Hagen:

> On 7-6-2012 18:02, Wolfgang Schuster wrote:
> 
>> You can also try to use a Lua variant.
>> 
>> \def\states_entry_indeed[#1][#2]%
>>  {\startlua
>>   local entries = utilities.parsers.settings_to_array("#1")
>>   local entry   = entries[#2]
>>   if entry then
>>     context(entry)
>>   else
>>     context("\\tttf NO ENTRY!")
> 
> shouldn't that be grouped?

Yes, of course.

Wolfgang
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

end of thread, other threads:[~2012-06-07 16:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-07 12:05 expansion and \processaction Philipp Gesang
2012-06-07 12:32 ` Wolfgang Schuster
2012-06-07 15:34   ` Philipp Gesang
2012-06-07 16:02     ` Wolfgang Schuster
2012-06-07 16:16       ` Hans Hagen
2012-06-07 16:55         ` 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).