Debian Codegraph @

texmf-dist/doc/generic/olsak-misc/eparam.tex

raw
1 % eparam.tex
2 %%%%%%%%%%%%%%%%%%%%
3 % Petr Olsak 2014
4
5 % This macro enables full expansion during parameter scanning.
6 % The usage:
7 % \def\macro#1{macro with #1}
8 % \eparam\macro prameter-text
9 %
10 % The delimiters for the parameter are {...} or \start...\stop or
11 % combination {...\stop, \start...}, where \start is declared by \eparamopen
12 % and \stop is declared by \eparamclose. See more information at the end of
13 % this file or at
14 % http://tex.stackexchange.com/questions/196776/arguments-possibly-delimited-by-bgroup-and-egroup
15
16
17 \def\tmp{% all expandable primitives (only from classical TeX, you can add others):
18 \botmark \csname \else \endcsname \endinput \expandafter \fi \firstmark \fontname
19 \if \ifcase \ifcat \ifdim \ifeof \iffalse \ifhbox \ifhmode \ifinner
20 \ifmmode \ifnum \ifodd \iftrue \ifvbox \ifvmode \ifvoid \ifx
21 \input \jobname \meaning \noexpand \number \or \romannumeral
22 \splitbotmark \splitfirstmark \string \the \topmark
23 }
24 \def\skipmm#1->{} \def\showmm#1->{#1}
25 \edef\textmm{\expandafter\showmm\meaning\empty}
26 \edef\expandprimitives{\expandafter\skipmm\meaning\tmp}
27
28 \def\isinlist#1#2#3{% from opmac.tex
29 \def\tmp##1#2##2\end{\def\tmp{##2}%
30 \ifx\tmp\empty \csname iffalse\expandafter\endcsname \else
31 \csname iftrue\expandafter\endcsname \fi}% end of \def\tmp
32 \expandafter\tmp#1\endlistsep#2\end
33 }
34 \def\isexpanded#1#2{% \isexpanded X\iftrue the X is expandable primitive or macro\fi
35 \edef\tmpb{\meaning#1\space}%
36 \expandafter\isinlist\expandafter\tmpb\expandafter{\textmm}%
37 \iftrue \csname iftrue\expandafter\endcsname\else
38 \def\nexxt{\expandafter\isinlist\expandafter\expandprimitives\expandafter{\tmpb}.}%
39 \expandafter\nexxt\fi
40 }
41 \def\eparamopen#1{\def\eparamopenA{\let#1=\eparamopenA}}
42 \def\eparamclose#1{\def\eparamcloseA{\let#1=\eparamcloseA}}
43
44 \newtoks\eparamT
45 \def\eparam#1{\begingroup
46 \toks0={#1}\let\bgroup=\relax \let\egroup=\relax
47 \let\ifIamInGroup=\iffalse
48 \ifx\eparamopenA\undefined \def\eparamopenA{^\eparam^}\else \eparamopenA\fi
49 \ifx\eparamcloseA\undefined \def\eparamcloseA{^\eparam^}\else \eparamcloseA\fi
50 \eparamT={}\eparamA
51 }
52 \def\eparamA{\futurelet\tmpc\eparamB}
53 \def\eparamB{\let\next=\eparamD
54 \isexpanded\tmpc\iftrue \def\next{\expandafter\eparamA}\fi
55 \ifx\tmpc\bgroupOri \let\next=\eparamC \let\nexxt=\eparamD \fi
56 \ifx\tmpc\eparamopenA \let\next=\eparamC \let\nexxt=\eparamD \fi
57 \next
58 }
59 \def\eparamC{\afterassignment\nexxt \let\next= }
60 \def\eparamD{\futurelet\tmpc\eparamE}
61 \def\eparamE{\let\next=\eparamN
62 \isexpanded\tmpc\iftrue \def\next{\expandafter\eparamD}\fi
63 \ifx\tmpc\spacetoken \let\next=\eparamC \let\nexxt=\eparamD \eparamX{ }\fi
64 \ifx\tmpc\eparamcloseA \ifIamInGroup \let\next=\eparamN
65 \else \let\next=\eparamC \let\nexxt=\eparamF \fi\fi
66 \ifx\tmpc\egroupOri \let\next=\eparamC \let\nexxt=\eparamF \fi
67 \ifx\tmpc\bgroupOri \let\next=\eparamC \let\nexxt=\eparamG \fi
68 \next
69 }
70 \def\eparamN#1{\eparamX#1\eparamD}
71 \def\eparamG{\begingroup \let\ifIamInGroup=\iftrue \eparamT={}\eparamD}
72 \def\eparamF{\ifIamInGroup \let\next=\eparamY \else \let\next=\eparamZ \fi \next}
73 \long\def\eparamX#1{\eparamT\expandafter{\the\eparamT#1}}
74 \def\eparamY{\expandafter\endgroup
75 \expandafter\eparamT\expandafter\expandafter\expandafter
76 {\expandafter\the\expandafter\eparamT\expandafter{\the\eparamT}}%
77 \eparamD
78 }
79 \def\eparamZ{\expandafter\endgroup\the\toks0\expandafter{\the\eparamT}}
80
81 \let\bgroupOri=\bgroup
82 \let\egroupOri=\egroup
83 \def\tmp/{\let\spacetoken= }\tmp/ %
84
85 \endinput
86
87 tests:
88
89 \def\macro#1{\toks0={#1}\message{the parameter is "\the\toks0"}}
90
91 \eparam\macro {abc} % the parameter is "abc"
92
93 \def\x{ww}
94 \eparam\macro {ab\x c} % the parameter is "abwwc"
95 \eparam\macro {a b\the\pageno c} % the parameter is "ab1c"
96 \eparam\macro {ab\ifx\x\x true\else false\fi c} % the parameter is "abtruec"
97 \eparam\macro {ab\ifnum\folio=1 true\else false\fi c} % the parameter is "abtruec"
98 \eparam\macro {ab\ifcase\pageno oo\or one\or two\fi c} % the parameter is "abonec"
99
100 \eparamopen\start \eparamclose\stop
101
102 \eparam\macro {abc\stop % the parameter is "abc"
103 \eparam\macro \start abc\stop % the parameter is "abc"
104 \eparam\macro \start abc} % the parameter is "abc"
105
106 \eparam\macro abc} % the parameter is "abc"
107 \eparam\macro abc\stop % the parameter is "abc"
108
109 \eparam\macro \start abc{uf\stop fu}ee\stop % the parameter is "abc{uf\stop fu}ee"
110
111 \def\y{end\stop}
112
113 \eparam\macro {a\x\y % the parameter is "awwend"
114
115 \eparam {\def\aha#1#2}\start params: #1 and #2\stop
116
117 \eparamopen\bgroup \eparamclose\egroup
118 \eparam {\def\foo#1#2}\bgroup Something with #1 and #2\egroup
119
120 \message{\meaning\foo}
121
122 \end
123
124 The question gives me a sense if it is read from its end: *give
125 the possibility of creating a macro which expands its parameter during
126 parameter scanning*. Then the variants `}` or `\egroup` as a delimiter of the
127 parameter is serviceable.
128
129 I've created the `\eparam` macro with this syntax:
130
131 \def\mymacro #1{the #1 parameter is declared as undelimited}
132 ...
133 \eparam\mymacro parameter-text
134
135 The `parameter-text` is equal to `real-parameter-text` enclosed by braces or
136 by another control sequences declared by `\eparamopen` and \eparamclose`.
137 Example:
138
139 \eparamopen\start \eparamclose\stop
140 \eparam\mymacro {real-parameter-text}
141 \eparam\mymacro \start real-parameter-text\stop
142 \eparam\mymacro \start real-parameter-text}
143 \eparam\mymacro {real-parameter-text\stop
144
145 The main point of the `\eparam` is that this is **Expanded Parameter**.
146 The `real-parameter-text` is expanded during parameter scanning like
147 by `\edef`. This means that all expandable primitives and macros are
148 expanded during the parameter is read. Unexpandable primitives does nothing
149 in this time (like `\edef`) so you can do reassigmnent of registers/macros
150 inside this parameter but without any effect for parameter scaninng.
151 This is main difference between this case and the `\hbox {...}` primitive
152 syntax.
153
154 There is one difference between `\edef` and parameter scanning:
155 undefined control sequences do nothing (like unexpandable primitives)
156 during parameter scanning. The error can be occur only when the parameter is
157 used (no during parameter scanning).
158
159 The separator declared by `\eparamclose` can be hidden in a macro. Example:
160
161 \def\x{-text\stop}
162 \eparam\mymacro {real-parameter\x
163
164 The first open brace or delimiter given by `\eparamopen` is optional. I.e.
165 you can omit it:
166
167 \eparam\mymacro real-parameter-text\stop
168
169 The parameter is always balanced. This means that the delimiter declared by
170 `\eparamclose` does no effect inside inner braces pair (like normal
171 parameter scanning):
172
173 \eparam \start text{inside \stop braces}text\stop
174 % the parameter is: "text{inside \stop braces}text"
175
176 The implementation (or wipet's sorcery :) and little tests follow.
177