Difference between revisions of "System Macros/Action Processing"

From Wiki
Jump to navigation Jump to search
m (Basic navigation)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
< '''Prev:''' [[System Macros/Branches and Decisions|Branches & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] >
+
< '''Prev:''' [[System Macros/Loops_and_Recursion|Loops & Recursion]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] >
 
 
[[Category:System Macros]]
 
[[Category:ConTeXt programming]]
 
  
 
ConTeXt makes extensive use of a sort of case or switch command. Depending of the presence of one or more provided items, some actions is taken. These macros can be nested without problems.
 
ConTeXt makes extensive use of a sort of case or switch command. Depending of the presence of one or more provided items, some actions is taken. These macros can be nested without problems.
Line 41: Line 38:
 
</texcode>
 
</texcode>
  
< '''Prev:''' [[System Macros/Branches and Decisions|Branches & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] >
+
== Problems with expansion ==
 +
Be aware that <code>\processaction</code> is not fully expandable. This means that the following won't work.
 +
 
 +
<texcode>
 +
\def\MyMacro#1{\processaction[#1][a=>\a,b=>\b]}
 +
\edef\x{\MyMacro{a}}
 +
</texcode>
 +
 
 +
A solution in MkIV proposed by Taco on [[http://archive.contextgarden.net/message/20100513.073030.d6d1fa3a.en.html 2010-05-13]] is to define <code>\MyMacro</code> as follows
 +
 
 +
<texcode>
 +
\def\MyMacro#1{\directlua{
 +
    local known = {a = '\a', b = '\b'}
 +
    tex.sprint(known['#1'] or '\a')}}
 +
</texcode>
 +
 
 +
Another way that should work in MkII too:
 +
 
 +
<texcode>
 +
\getparameters[MyMacro:][a=\a,b=\b]
 +
\def\MyMacro#1{\doifdefinedelse{MyMacro:#1}{\getvalue{MyMacro:#1}}{\a}}
 +
</texcode>
 +
 
 +
 
 +
< '''Prev:''' [[System Macros/Loops and Recursion|Loops & Recusion]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] >
  
[[Category:System Macros]]
+
[[Category:Programming and Databases]]
[[Category:ConTeXt programming]]
+
[[Category:Tools]]

Latest revision as of 16:51, 8 June 2020

< Prev: Loops & Recursion | Top: System Macros | Next: Comma Separated Lists >

ConTeXt makes extensive use of a sort of case or switch command. Depending of the presence of one or more provided items, some actions is taken. These macros can be nested without problems.

\processaction           [x]     [a=>\a,b=>\b,c=>\c]
\processfirstactioninset [x,y,z] [a=>\a,b=>\b,c=>\c]
\processallactionsinset  [x,y,z] [a=>\a,b=>\b,c=>\c]

This macro is most often used in the key-value parser, like in this (simplified) example, where the user has said width=small or something similar:

\processaction
    [\TESTwidth] % should expand to 'small'
    [small=>\message{small was chosen},
     medium=>\message{medium was chosen}]

You can supply both a default action and an action to be undertaken when an unknown value is met:

\processallactionsinset
    [x,y,z]
    [a=>\a,
     b=>\b,
     c=>\c,
     default=>\default,
     unknown=>\unknown{... \commalistelement ...}]

If the first argument is empty, this macro scans the list for the keyword default and executes the related action, if present. If the first argument is not empty but also not in the list, the action related to unknown is executed. Both keywords must be at the end of list #2. Afterwards, the actually found keyword is available in \commalistelement.

Sometimes an action needs to be undertaken that depends only on the first character of something (for instance, checking if some string represents a number or not). This macro get this character and puts it in \firstcharacter.

\getfirstcharacter {string}

Problems with expansion

Be aware that \processaction is not fully expandable. This means that the following won't work.

\def\MyMacro#1{\processaction[#1][a=>\a,b=>\b]}
\edef\x{\MyMacro{a}}

A solution in MkIV proposed by Taco on [2010-05-13] is to define \MyMacro as follows

\def\MyMacro#1{\directlua{
    local known = {a = '\a', b = '\b'}
    tex.sprint(known['#1'] or '\a')}}

Another way that should work in MkII too:

\getparameters[MyMacro:][a=\a,b=\b]
\def\MyMacro#1{\doifdefinedelse{MyMacro:#1}{\getvalue{MyMacro:#1}}{\a}}


< Prev: Loops & Recusion | Top: System Macros | Next: Comma Separated Lists >