Debian Codegraph @

texmf-dist/source/latex/etl/etl.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: etl.dtx Copyright (C) 2020-2021 Jonathan P. Spratte
4 %
5 % This work may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this license or
7 % (at your option) any later version. The latest version of this license is in
8 % the file:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % ------------------------------------------------------------------------------
13 %
14 %<*driver>^^A>>=
15 \def\nameofplainTeX{plain}
16 \ifx\fmtname\nameofplainTeX\else
17 \expandafter\begingroup
18 \fi
19 \input l3docstrip.tex
20 \askforoverwritefalse
21 \preamble
22
23 --------------------------------------------------------------
24 etl -- expandable token list operations
25 E-mail: [email protected]
26 Released under the LaTeX Project Public License v1.3c or later
27 See http://www.latex-project.org/lppl.txt
28 --------------------------------------------------------------
29
30 Copyright (C) 2020-2021 Jonathan P. Spratte
31
32 This work may be distributed and/or modified under the conditions of the
33 LaTeX Project Public License (LPPL), either version 1.3c of this license or
34 (at your option) any later version. The latest version of this license is in
35 the file:
36
37 http://www.latex-project.org/lppl.txt
38
39 This work is "maintained" (as per LPPL maintenance status) by
40 Jonathan P. Spratte.
41
42 This work consists of the file etl.dtx
43 and the derived files etl.pdf
44 etl.sty
45
46 \endpreamble
47 % stop docstrip adding \endinput
48 \postamble
49 \endpostamble
50 \generate{\file{etl.sty}{\from{etl.dtx}{pkg}}}
51 \ifx\fmtname\nameofplainTeX
52 \expandafter\endbatchfile
53 \else
54 \expandafter\endgroup
55 \fi
56 %
57 \IfFileExists{etl.sty}{\RequirePackage{etl}}{}
58 \PassOptionsToPackage{full}{textcomp}
59 \documentclass{l3doc}
60 \RequirePackage[oldstylenums,nott]{kpfonts}
61 \RequirePackage{randtext,xcolor}
62 \input{glyphtounicode}
63 \usepackage{xistercian}
64 \pdfgentounicode=1
65 \let\metaORIG\meta
66 \protected\def\meta #1{\texttt{\metaORIG{#1}}}
67 \renewcommand*\thefootnote{\cistercian{footnote}}
68 \hypersetup{linkcolor=red!80!black,urlcolor=purple!80!black}
69 \makeatletter
70 \@ifdefinable\gobbledocstriptag{\def\gobbledocstriptag#1>{}}
71 \protected\def\savefootnotestuff
72 {%
73 \let\mysavethefootnote\thefootnote
74 \let\mysave@makefnmark\@makefnmark
75 \let\mysave@makefntext\@makefntext
76 }
77 \def\restorefootnotestuff{\texorpdfstring\restorefootnotestuff@{}}
78 \def\restorefootnotestuff@
79 {%
80 \global\let\thefootnote\mysavethefootnote
81 \global\let\@makefnmark\mysave@makefnmark
82 \global\let\@makefntext\mysave@makefntext
83 }
84 \makeatother
85 \begin{document}
86 \savefootnotestuff
87 \title
88 {^^A
89 \restorefootnotestuff
90 The \pkg{etl} package\\ expandable token list operations^^A
91 }
92 \GetFileInfo{etl.sty}
93 \date{\filedate\space\fileversion}
94 \author{Jonathan P. Spratte\thanks{\protect\randomize{[email protected]}}}
95 \DocInput{etl.dtx}
96 \end{document}
97 %</driver>^^A=<<
98 % \fi
99 %
100 % \maketitle
101 % \setcounter{footnote}{1}
102 %
103 % \tableofcontents
104 %
105 % \begin{documentation}^^A>>=
106 %
107 % \section{Documentation}
108 %
109 % The \pkg{etl} package provides a few \emph{slow but expandable} alternatives
110 % to unexpandable functions found inside the \pkg{l3tl} module of \pkg{expl3}.
111 % All user functions must not contain the tokens \cs{s__etl_stop}\footnote{At
112 % any nesting level of groups} or \cs{__etl_act_result:n}\footnote{On the top
113 % level (so nested usages in groups are fine)} in any argument unless specified
114 % otherwise (there might be other forbidden tokens, all of which are internals
115 % to this package, and usually shouldn't somehow end up inside the input stream
116 % by accident).
117 %
118 % There is another limitation of this package: There are tokens which cannot
119 % expandably be differentiated from each other, those are active characters let
120 % to the same character with a different category code, something like the
121 % following:
122 %
123 % \begin{verbatim}
124 % \char_set_catcode_letter:N a
125 % \char_set_active_eq:NN a a
126 % \char_set_catcode_active:N a
127 % \end{verbatim}
128 %
129 % After this the active `|a|'s couldn't be told apart from non-active `|a|'s of
130 % category letter by the parsers in this package.\footnote{Thanks to Bruno Le
131 % Floch for pointing that out.} In general two tokens are considered equal if
132 % \cs{etl_token_if_eq:NNTF} yields true (see there). Another limitation is that
133 % the parser doesn't consider the character code of tokens with category 1 or 2
134 % (group begin and group end, typically |{}|), instead all tokens found with
135 % these two category codes are normalised to \texttt{\{\textsubscript{1}} and
136 % \texttt{\}\textsubscript{2}} (an exception to this rule are the functions
137 % \cs{etl_token_replace_once:nNn} and \cs{etl_replace_once:nnn} in which this
138 % normalisation is only done up to the replacement, and the rest of the input is
139 % forwarded unchanged).
140 %
141 % The core macro \cs{etl_act:nnnnnnn} is modelled after an internal of
142 % \pkg{l3tl} called \cs{__tl_act:NNNn} but with some more possibilities added.
143 %
144 %
145 % \subsection{A general loop}
146 %
147 % \begin{function}[EXP]{\etl_act:nnnnnnn, \etl_act:nnnnnn, \etl_act:nnnnn}
148 % \begin{syntax}
149 % \cs{etl_act:nnnnnnn} \Arg{normal} \Arg{space} \Arg{group} \Arg{final} \Arg{status} \Arg{output} \Arg{token list}
150 % \cs{etl_act:nnnnnn} \Arg{normal} \Arg{space} \Arg{group} \Arg{final} \Arg{status} \Arg{token list}
151 % \cs{etl_act:nnnnn} \Arg{normal} \Arg{space} \Arg{group} \Arg{status} \Arg{token list}
152 % \end{syntax}
153 % This function will act on the \meta{token list} (somewhat a
154 % |map_tokens|-function). Both \meta{normal} and \meta{group} should be code
155 % that expects two following arguments (the first being the \meta{status}, the
156 % second the next |N|-type token or the contents of the next group in the
157 % \meta{token list}), and \meta{space} should expect only the \meta{status} as
158 % a following argument.
159 %
160 % You can also specify \meta{final} code which will receive the \meta{status}
161 % followed by the output (which you can assign with \cs{etl_act_output:n} and
162 % \cs{etl_act_output_pre:n}), and will be used inside an |e|-expansion context
163 % (so you'll want to protect anything you want to output from further
164 % expansion using \cs{exp_not:n}). Also you can specify some \meta{output}
165 % which should be there from the beginning.
166 %
167 % Variants without the argument \meta{output} will start with an empty output,
168 % and those without \meta{final} will just output the results at the end.
169 %
170 % \begin{texnote}
171 % The result is returned within \cs{exp_not:n}, which means that the token
172 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
173 % argument expansion. The result will be returned after exactly two steps of
174 % expansion. If you don't need the \meta{final} argument processor and don't
175 % have to reorder some of the output you can also use \cs{exp_not:n} to
176 % directly output tokens where you currently are, this might be faster.
177 % \end{texnote}
178 % \end{function}
179 %
180 % All other functions which start in |\etl_act_| should only be used inside the
181 % \meta{normal}, \meta{space}, or \meta{group} code of \cs{etl_act:nnnnnnn}.
182 %
183 % \begin{function}[EXP]{\etl_act_output:n, \etl_act_output_pre:n}
184 % \begin{syntax}
185 % \cs{etl_act_output:n} \Arg{token list}
186 % \end{syntax}
187 % This will add \meta{token list} to the output of \cs{etl_act:nnnnnnn}. The
188 % normal version will add \meta{token list} after the current output, the
189 % |pre| variant will put it before the current output.
190 % \end{function}
191 %
192 % \begin{function}[EXP]{\etl_act_output_rest:, \etl_act_output_rest_pre:}
193 % \begin{syntax}
194 % \cs{etl_act_output_rest:}
195 % \end{syntax}
196 % After this macro was used all remaining things inside the \meta{token list}
197 % argument of \cs{etl_act:nnnnnnn} will be added to the output. The normal
198 % version will add it to the end of the output, the |pre| variant will put the
199 % remainder before the current output in reversed order. Any begin group and
200 % end group tokens inside \meta{token list} will be normalised by this. A
201 % faster alternative (that doesn't normalise) would be
202 % \begin{verbatim}
203 % \etl_act_apply_to_rest:n { \etl_act_break_post:n }
204 % \end{verbatim}
205 % \end{function}
206 %
207 % \begin{function}[EXP]{\etl_act_status:n}
208 % \begin{syntax}
209 % \cs{etl_act_status:n} \Arg{status}
210 % \end{syntax}
211 % This will change the current status of \cs{etl_act:nnnnnnn} to
212 % \meta{status}.
213 % \end{function}
214 %
215 % \begin{function}[EXP]{\etl_act_put_back:n}
216 % \begin{syntax}
217 % \cs{etl_act_put_back:n} \Arg{token list}
218 % \end{syntax}
219 % You can put \meta{token list} back into the input stream to be reconsidered
220 % by the current \cs{etl_act:nnnnnnn} loop (of course this doesn't have to be
221 % literally put back, you might add completely new contents with this).
222 % \end{function}
223 %
224 % \begin{function}[EXP]
225 % {
226 % \etl_act_switch:nnn,
227 % \etl_act_switch_normal:n, \etl_act_switch_space:n, \etl_act_switch_group:n
228 % }
229 % \begin{syntax}
230 % \cs{etl_act_switch:nnn} \Arg{normal} \Arg{space} \Arg{group}
231 % \cs{etl_act_switch_normal:n} \Arg{normal}
232 % \cs{etl_act_switch_space:n} \Arg{space}
233 % \cs{etl_act_switch_group:n} \Arg{group}
234 % \end{syntax}
235 % With these functions you can change the provided code to act on
236 % \meta{normal} (so |N|-type) tokens, \meta{space}s, and \meta{group}s.
237 % \end{function}
238 %
239 % \begin{function}[EXP]{\etl_act_apply_to_rest:n}
240 % \begin{syntax}
241 % \cs{etl_act_apply_to_rest:n} \Arg{code}
242 % \end{syntax}
243 % This will fetch the rest of the input \meta{token list} of the current
244 % \cs{etl_act:nnnnnnn} call and leave \meta{code}\Arg{rest} in the input
245 % stream (without brace normalisation). It will not end the current
246 % \cs{etl_act:nnnnnnn} loop (but since the \meta{token list} remainder is now
247 % empty it'll end after your \meta{code} is done, except if you put new
248 % contents in the list using \cs{etl_act_put_back:n}) nor affect it in any
249 % other way.
250 % \end{function}
251 %
252 % \begin{function}[EXP]{\etl_act_do_final:}
253 % \begin{syntax}
254 % \cs{etl_act_do_final:}
255 % \end{syntax}
256 % This will immediately stop the current \cs{etl_act:nnnnnnn} invocation and
257 % execute the provided \meta{final} code (or if a variant without the
258 % \meta{final} code was used, the output). The \meta{final} code will receive
259 % the current status followed by the current output as two arguments (just
260 % like it would when the end of the \meta{token list} was reached).
261 % \end{function}
262 %
263 % \begin{function}[EXP]{\etl_act_break:, \etl_act_break_discard:}
264 % \begin{syntax}
265 % \cs{etl_act_break:}
266 % \end{syntax}
267 % This will immediately stop the current \cs{etl_act:nnnnnnn} invocation and
268 % leave the current output in the input stream. The |discard| variant will
269 % gobble the current output and leave nothing in the input stream.
270 % \end{function}
271 %
272 % \begin{function}[EXP]{\etl_act_break:n}
273 % \begin{syntax}
274 % \cs{etl_act_break:n} \Arg{token list}
275 % \end{syntax}
276 % This will immediately stop the current \cs{etl_act:nnnnnnn} invocation,
277 % gobble the current output and leave \meta{token list} in the input stream.
278 % \end{function}
279 %
280 % \begin{function}[EXP]{\etl_act_break_pre:n, \etl_act_break_post:n}
281 % \begin{syntax}
282 % \cs{etl_act_break_pre:n} \Arg{token list}
283 % \end{syntax}
284 % This will immediately stop the current \cs{etl_act:nnnnnnn} invocation and
285 % leave the current output in the input stream, and in the case of the normal
286 % variant followed by \meta{token list}, in the |pre|-case preceded by
287 % \meta{token list}.
288 % \end{function}
289 %
290 %
291 % \subsubsection{Examples}
292 %
293 % To give examples how you could use \cs{etl_act:nnnnnnn} the following could
294 % be used to reimplement \cs{tl_reverse:n}. We work with a bit of currying here
295 % (this gives a small speed gain in general, though for code clarity you might
296 % decide to not curry each and every argument). Since a reversing function
297 % doesn't need to postprocess its output we can use the shorter
298 % \cs{etl_act:nnnnn}. The three internal functions all need to gobble the
299 % status.
300 %
301 % \begin{verbatim}
302 % % argument #2 is curried
303 % \cs_new:Npn \__my_reverse_normal:nN #1 { \etl_act_output_pre:n }
304 % \cs_new:Npn \__my_reverse_space:n #1 { \etl_act_output_pre:n { ~ } }
305 % \cs_new:Npn \__my_reverse_group:nn #1#2 { \etl_act_output_pre:n { {#2} } }
306 % % argument #1 is curried
307 % \cs_new:Npn \my_reverse:n
308 % {
309 % \etl_act:nnnnn
310 % \__my_reverse_normal:nN
311 % \__my_reverse_space:n
312 % \__my_reverse_group:nn
313 % {} % empty status
314 % }
315 % \end{verbatim}
316 %
317 % We could also create a similar function that'll reverse the input up to the
318 % first space and discard the remainder (no idea why somebody would want to do
319 % that).
320 %
321 % \begin{verbatim}
322 % % argument #1 is curried
323 % \cs_new:Npn \my_reverse_discard_after_space:n
324 % {
325 % \etl_act:nnnnn
326 % \__my_reverse_normal:nN
327 % \etl_act_break:
328 % \__my_reverse_group:nn
329 % {} % empty status
330 % }
331 % \end{verbatim}
332 %
333 % Or a function that reverses the contents of groups as well.
334 %
335 % \begin{verbatim}
336 % \cs_generate_variant:Nn \etl_act_output_pre:n { e }
337 % \cs_new:Npn \__my_reverse_group_reversed:nn #1#2
338 % { \etl_act_output_pre:e { { \my_reverse_deep:n {#2} } } }
339 % % argument #1 is curried
340 % \cs_new:Npn \my_reverse_deep:n
341 % {
342 % \etl_act:nnnnn
343 % \__my_reverse_normal:nN
344 % \__my_reverse_space:n
345 % \__my_reverse_group_reversed:nn
346 % {} % empty status
347 % }
348 % \end{verbatim}
349 %
350 % Another function could count a specific token inside a token list. Since for
351 % this neither output-reordering nor post processing is needed we shortcut by
352 % not using \cs{etl_output:n} but instead directly leaving |+ \c_one_int| (which
353 % also doesn't need to be protected against further expansion) in the input.
354 %
355 % \begin{verbatim}
356 % \cs_new:Npn \__my_count_token:NN #1#2
357 % { \etl_token_if_eq:NNT #1#2 { + \c_one_int } }
358 % \cs_new:Npn \my_count_token:Nn #1#2
359 % {
360 % \int_eval:n
361 % {
362 % \c_zero_int
363 % \etl_act:nnnnn
364 % \__my_count_token:NN
365 % \use_none:n
366 % \use_none:nn
367 % {#1}
368 % {#2}
369 % }
370 % }
371 % \end{verbatim}
372 %
373 % As a last example we reimplement the \cs{etl_token_replace_once:nNn} function.
374 % The function doesn't need to reorder any tokens, so we shortcut with
375 % \cs{exp_not:n} to output things in place. We put the token we want to replace
376 % in the code for |N|-type processing and the replacement in the status. When we
377 % found the token we want to replace we put our replacement there and output the
378 % rest unaltered.
379 %
380 % \begin{verbatim}
381 % \cs_new:Npn \my_replace_token:nNn #1#2#3
382 % {
383 % \etl_act:nnnnn
384 % { \__my_replace_token:NnN #2 }
385 % { ~ \use_none:n }
386 % { \__my_replace_token:nn }
387 % {#3}
388 % {#1}
389 % }
390 % \cs_new:Npn \__my_replace_token:nn #1#2 { { \exp_not:n {#2} } }
391 % \cs_new:Npn \__my_replace_token:NnN #1#2#3
392 % {
393 % \etl_token_if_eq:NNTF #1#3
394 % { \exp_not:n {#2} \etl_act_apply_to_rest:n { \etl_act_break:n } }
395 % { \exp_not:N #3 }
396 % }
397 % \end{verbatim}
398 %
399 % I hope this gave you at least an idea on how to use the loop and that the
400 % explanations on the functions not used in these examples suffice to also give
401 % you an idea on how to use them.
402 %
403 %
404 % \subsection{Conditionals}\label{sec:conditionals}
405 %
406 % \begin{function}[pTF]{\etl_token_if_eq:NN}
407 % \begin{syntax}
408 % \cs{etl_token_if_eq_p:NN} \meta{token_1} \meta{token_2}
409 % \cs{etl_token_if_eq:NNTF} \meta{token_1} \meta{token_2} \Arg{true code} \Arg{false code}
410 % \end{syntax}
411 % Compares \meta{token_1} and \meta{token_2} and yields |true| if the two are
412 % equal. Two tokens are considered equal if they have the same meaning (so if
413 % \cs{if_meaning:w} is true) and the same string representation (so if
414 % \cs{str_if_eq:nnTF} is true).
415 % \end{function}
416 %
417 % \begin{function}[pTF]{\etl_token_if_in:nN}
418 % \begin{syntax}
419 % \cs{etl_token_if_in_p:nN} \Arg{token list} \meta{token}
420 % \cs{etl_token_if_in:nNTF} \Arg{token list} \meta{token} \Arg{true code} \Arg{false code}
421 % \end{syntax}
422 % Searches for \meta{token} inside the \meta{token list}. If it is found
423 % returns true. Brace groups inside the \meta{token list} are ignored.
424 % \end{function}
425 %
426 % \begin{function}[pTF]{\etl_token_if_in_deep:nN}
427 % \begin{syntax}
428 % \cs{etl_token_if_in_deep_p:nN} \Arg{token list} \meta{token}
429 % \cs{etl_token_if_in_deep:nNTF} \Arg{token list} \meta{token} \Arg{true code} \Arg{false code}
430 % \end{syntax}
431 % Searches for \meta{token} inside the \meta{token list}. If it is found
432 % returns true. Brace groups inside the \meta{token list} are recursively
433 % searched as well.
434 % \end{function}
435 %
436 % \begin{function}[pTF]{\etl_if_eq:nn}
437 % \begin{syntax}
438 % \cs{etl_if_eq_p:nn} \Arg{token list_1} \Arg{token list_2}
439 % \cs{etl_if_eq:nnTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
440 % \end{syntax}
441 % Compares \meta{token list_1} and \meta{token list_2} with each other on a
442 % token-by-token basis. Keep in mind that there are tokens which can't be told
443 % apart from each other, and that groups are normalised. If both token lists
444 % match (modulo the mentioned limitations) the \meta{true code} is left in the
445 % input stream, else the \meta{false code}.
446 % \end{function}
447 %
448 % \begin{function}[pTF]{\etl_if_in:nn}
449 % \begin{syntax}
450 % \cs{etl_if_in_p:nn} \Arg{token list} \Arg{search text}
451 % \cs{etl_if_in:nnTF} \Arg{token list} \Arg{search text} \Arg{true code} \Arg{false code}
452 % \end{syntax}
453 % Searches for \meta{search text} inside of \meta{token list}. If it is
454 % found the \meta{true code} is left in the input stream, else the \meta{false
455 % code}. Both macro parameter tokens as well as tokens with category code~1
456 % and 2 (normally |{}|) can be part of \meta{search text} (unlike for the
457 % similar function \cs{tl_if_in:nnTF}). Material inside of groups in
458 % \meta{token list} is ignored (except for groups contained in \meta{search
459 % text}). So the following would first yield |true| and then |false|:
460 % \end{function}
461 % \begin{verbatim}
462 % \etl_if_in:nnTF { a{b{c}} } { {b{c}} } { true } { false }
463 % \etl_if_in:nnTF { a{b{c}} } { b{c} } { true } { false }
464 % \end{verbatim}
465 %
466 % \begin{function}[pTF]{\etl_if_in_deep:nn}
467 % \begin{syntax}
468 % \cs{etl_if_in_deep_p:nn} \Arg{token list} \Arg{search text}
469 % \cs{etl_if_in_deep:nnTF} \Arg{token list} \Arg{search text} \Arg{true code} \Arg{false code}
470 % \end{syntax}
471 % Does the same as \cs{etl_if_in:nnTF} but also recursively searches inside of
472 % groups in \meta{token list}. So this would yield |true| in both of the
473 % cases in above example.
474 % \end{function}
475 %
476 %
477 % \subsection{Modifying token lists}
478 %
479 % \begin{function}[EXP]{\etl_token_replace_once:nNn}
480 % \begin{syntax}
481 % \cs{etl_token_replace_once:nNn} \Arg{token list} \meta{token} \Arg{replacement}
482 % \end{syntax}
483 % This function will replace the first occurrence of \meta{token} inside of
484 % \meta{token list} that is not hidden inside a group with \meta{replacement}.
485 % The \meta{token} has to be a valid |N|-type argument.
486 %
487 % \begin{texnote}
488 % The result is returned within \cs{exp_not:n}, which means that the token
489 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
490 % argument expansion. The result will be returned after exactly two steps of
491 % expansion.
492 % \end{texnote}
493 % \end{function}
494 %
495 % \begin{function}[EXP]{\etl_token_replace_all:nNn}
496 % \begin{syntax}
497 % \cs{etl_token_replace_all:nNn} \Arg{token list} \meta{token} \Arg{replacement}
498 % \end{syntax}
499 % This function will replace each occurrence of \meta{token} inside of
500 % \meta{token list} that is not hidden inside a group with \meta{replacement}.
501 % The \meta{token} has to be a valid |N|-type argument.
502 %
503 % \begin{texnote}
504 % The result is returned within \cs{exp_not:n}, which means that the token
505 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
506 % argument expansion. The result will be returned after exactly two steps of
507 % expansion.
508 % \end{texnote}
509 % \end{function}
510 %
511 % \begin{function}[EXP]{\etl_token_replace_all_deep:nNn}
512 % \begin{syntax}
513 % \cs{etl_token_replace_all_deep:nNn} \Arg{token list} \meta{token} \Arg{replacement}
514 % \end{syntax}
515 % This function will replace each occurrence of \meta{token} inside of
516 % \meta{token list} with \meta{replacement}.
517 % The \meta{token} has to be a valid |N|-type argument.
518 %
519 % \begin{texnote}
520 % The result is returned within \cs{exp_not:n}, which means that the token
521 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
522 % argument expansion. The result will be returned after exactly two steps of
523 % expansion.
524 % \end{texnote}
525 % \end{function}
526 %
527 % \begin{function}[EXP]{\etl_replace_once:nnn}
528 % \begin{syntax}
529 % \cs{etl_replace_once:nnn} \Arg{token list} \Arg{search text} \Arg{replacement}
530 % \end{syntax}
531 % This function will replace the first occurrence of \meta{search text}
532 % inside of \meta{token list} that is not hidden inside a group with
533 % \meta{replacement}.
534 %
535 % \begin{texnote}
536 % The result is returned within \cs{exp_not:n}, which means that the token
537 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
538 % argument expansion. The result will be returned after exactly two steps of
539 % expansion.
540 % \end{texnote}
541 % \end{function}
542 %
543 % \begin{function}[EXP]{\etl_replace_all:nnn}
544 % \begin{syntax}
545 % \cs{etl_replace_all:nnn} \Arg{token list} \Arg{search text} \Arg{replacement}
546 % \end{syntax}
547 % This function will replace all occurrences of \meta{search text}
548 % inside of \meta{token list} that are not hidden inside a group with
549 % \meta{replacement}.
550 %
551 % \begin{texnote}
552 % The result is returned within \cs{exp_not:n}, which means that the token
553 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
554 % argument expansion. The result will be returned after exactly two steps of
555 % expansion.
556 % \end{texnote}
557 % \end{function}
558 %
559 % \begin{function}[EXP]{\etl_replace_all_deep:nnn}
560 % \begin{syntax}
561 % \cs{etl_replace_all_deep:nnn} \Arg{token list} \Arg{search text} \Arg{replacement}
562 % \end{syntax}
563 % This function will replace all occurrences of \meta{search text}
564 % inside of \meta{token list} with \meta{replacement}.
565 %
566 % \begin{texnote}
567 % The result is returned within \cs{exp_not:n}, which means that the token
568 % list does not expand further when appearing in an \hbox{|x|- or} |e|-type
569 % argument expansion. The result will be returned after exactly two steps of
570 % expansion.
571 % \end{texnote}
572 % \end{function}
573 %
574 %
575 % \subsection{New expandable functions}
576 %
577 % Functions generated with the means in this section are roughly as fast as the
578 % \pkg{l3tl} variants of them (there might be performance differences; in any
579 % case they are faster than the generic functions above), but have at least one
580 % fixed argument. They don't have the drawback of not being able to tell apart
581 % an active character from a token with the same character code and different
582 % category code if the active character was let to it and they don't normalise
583 % braces to \texttt{\{\textsubscript{1}} and \texttt{\}\textsubscript{2}}.
584 %
585 % \subsubsection{Conditionals}
586 %
587 % \begin{function}{\etl_new_if_in:Nnn}
588 % \begin{syntax}
589 % \cs{etl_new_if_in:Nnn} \meta{function} \Arg{search text} \Arg{conditions}
590 % \end{syntax}
591 % This will define a new \meta{function} which will act as a conditional and
592 % search for \meta{search text} inside of an |n|-type argument completely
593 % expandable. The \meta{conditions} should be a comma-separated list
594 % containing one or more of |p|, |T|, |F| and |TF| (just like for
595 % \cs{prg_new_conditional:Npnn}). The \meta{search text} must not contain
596 % tokens with category code~1 or 2 (normally |{}|) and can't contain macro
597 % parameter tokens (normally |#|).
598 % Unlike for the conditionals in \autoref{sec:conditionals}, the \meta{search
599 % text} of functions created with \cs{etl_new_if_in:Nnn} might contain
600 % \cs{s__etl_stop} tokens.
601 %
602 % So the following would yield |true| followed by |false|:
603 % \end{function}
604 % \begin{verbatim}
605 % \etl_new_if_in:Nnn \my_if_a_in:n { a } { TF }
606 % \my_if_a_in:nTF { a text } { true } { false }
607 % \my_if_a_in:nTF { text } { true } { false }
608 % \end{verbatim}
609 %
610 %
611 % \subsubsection{Modifiers}
612 %
613 % \begin{function}{\etl_new_replace_once:Nn}
614 % \begin{syntax}
615 % \cs{etl_new_replace_once:Nn} \meta{function} \Arg{search text}
616 % \end{syntax}
617 % This defines a new \meta{function} that'll accept two arguments (the first
618 % being a token list, the second a replacement). The generated \meta{function}
619 % will replace the first occurrence of \meta{search text} inside the token
620 % list with replacement. It'll ignore things hidden inside a group in the
621 % token list. Neither the \meta{search text} nor the token list given to the
622 % generated \meta{function} can contain \cs{s__etl_stop} (this would result in
623 % undefined behaviour), the given replacement on the other hand might contain
624 % that token. Additionally \meta{search text} can't contain tokens of category
625 % group begin or group end (usually |{| and |}|) or macro parameters (usually
626 % |#|).
627 %
628 % \begin{texnote}
629 % The result of \meta{function} is returned within \cs{exp_not:n}, which
630 % means that the token list does not expand further when appearing in an
631 % \hbox{|x|- or} |e|-type argument expansion. The result will be returned
632 % after exactly two steps of expansion.
633 % \end{texnote}
634 % \end{function}
635 % So the following would yield |AcDC|:
636 % \begin{verbatim}
637 % \etl_new_replace_once:Nn \my_replace_C_once:nn { C }
638 % \my_replace_C_once:nn { ACDC } { c }
639 % \end{verbatim}
640 %
641 % \begin{function}{\etl_new_replace_all:Nn}
642 % \begin{syntax}
643 % \cs{etl_new_replace_all:Nn} \meta{function} \Arg{search text}
644 % \end{syntax}
645 % This behaves like \cs{etl_new_replace_once:Nn}, but the \meta{function} will
646 % replace all occurrences of \meta{search text} instead of just the first.
647 %
648 % \begin{texnote}
649 % The result of \meta{function} is returned within \cs{exp_not:n}, which
650 % means that the token list does not expand further when appearing in an
651 % \hbox{|x|- or} |e|-type argument expansion. The result will be returned
652 % after exactly two steps of expansion.
653 % \end{texnote}
654 % \end{function}
655 % So the following would yield |AcDc|:
656 % \begin{verbatim}
657 % \etl_new_replace_all:Nn \my_replace_C_all:nn { C }
658 % \my_replace_C_all:nn { ACDC } { c }
659 % \end{verbatim}
660 %
661 %
662 % \subsection{Bugs and Feature Requests}
663 %
664 % If you find bugs or want to request features you can do so either via email
665 % (see the first page) or via Github at
666 % \url{https://github.com/Skillmon/ltx_etl/issues}.
667 %
668 % \end{documentation}^^A=<<
669 %
670 % \clearpage
671 %
672 % \begin{implementation}^^A>>=
673 %
674 % \section{Implementation}
675 %
676 % \begin{macrocode}
677 %<*pkg>
678 % \end{macrocode}
679 %
680 % \begin{macrocode}
681 %<@@=etl>
682 % \end{macrocode}
683 %
684 % Tell who we are:
685 % \begin{macrocode}
686 \ProvidesExplPackage{etl}
687 {2021-11-07} {0.3}
688 {expandable token list manipulation}
689 % \end{macrocode}
690 %
691 % Ensure dependencies are met:
692 % \begin{macrocode}
693 \cs_if_exist:NF \tex_expanded:D
694 {
695 \msg_new:nnn { etl } { expanded-missing }
696 { The~ expanded~ primitive~ is~ required. }
697 \msg_fatal:nn { etl } { expanded-missing }
698 }
699 % \end{macrocode}
700 %
701 %
702 % \subsection{Primitives}
703 %
704 % \begin{macro}[EXP]{\@@_expanded:w,\@@_unexpanded:w,\@@_detokenize:w}
705 % Private copies of a few primitives (evil code for \pkg{expl3}).
706 % \begin{macrocode}
707 \cs_new_eq:NN \@@_expanded:w \tex_expanded:D
708 \cs_new_eq:NN \@@_unexpanded:w \tex_unexpanded:D
709 \cs_new_eq:NN \@@_detokenize:w \tex_detokenize:D
710 % \end{macrocode}
711 % \end{macro}
712 %
713 %
714 % \subsection{Variables}
715 %
716 % \begin{variable}{\s_@@_stop,\s_@@_mark}
717 % Scan marks.
718 % \begin{macrocode}
719 \scan_new:N \s_@@_stop
720 \scan_new:N \s_@@_mark
721 % \end{macrocode}
722 % \end{variable}
723 %
724 % \subsection{Small auxiliaries}
725 %
726 % \begin{macro}[EXP]{\@@_split_first:w}
727 % Can be used to extract the first element from a token list, it should always
728 % be used like this:
729 % |\exp_after:wN |\meta{function}^^A
730 % | \@@_expanded:w { \@@_split_first:w |\meta{arg}| }|.
731 % \begin{macrocode}
732 \cs_new:Npn \@@_split_first:w #1
733 {
734 { \@@_unexpanded:w {#1} }
735 \if_false: { \fi: \exp_after:wN } \exp_after:wN { \if_false: } \fi:
736 }
737 % \end{macrocode}
738 % \end{macro}
739 %
740 % \begin{macro}[EXP]{\@@_turn_true:w,\@@_fi_turn_false:w}
741 % Fast ways to change the outcome of a test.
742 % \begin{macrocode}
743 \cs_new:Npn \@@_turn_true:w \if_false: { \if_true: }
744 \cs_new:Npn \@@_fi_turn_false:w \fi: \if_true: { \fi: \if_false: }
745 % \end{macrocode}
746 % \end{macro}
747 %
748 % \begin{macro}[EXP]{\@@_rm_space:w}
749 % Fast macro to gobble an immediately following single space.
750 % \begin{macrocode}
751 \use:n { \cs_new:Npn \@@_rm_space:w } ~ {}
752 % \end{macrocode}
753 % \end{macro}
754 %
755 % \begin{macro}[EXP]
756 % {
757 % \@@_if_empty:nTF, \@@_if_empty:nT, \@@_if_empty:w,
758 % \@@_if_empty_true:w, \@@_if_empty_true_TF:w
759 % }
760 % This is a fast test whether something is empty or not. The argument must not
761 % contain \cs{s_@@_stop} for this to work (but since that limitation is true
762 % for most if not all user facing functions of this module, this is fine to
763 % gain a bit of speed).
764 % \begin{macrocode}
765 \cs_new:Npn \@@_if_empty:nT #1
766 {
767 \@@_if_empty:w
768 \s_@@_stop #1 \s_@@_stop \@@_if_empty_true:w
769 \s_@@_stop \s_@@_stop \use_none:n
770 }
771 \cs_new:Npn \@@_if_empty:nTF #1
772 {
773 \@@_if_empty:w
774 \s_@@_stop #1 \s_@@_stop \@@_if_empty_true_TF:w
775 \s_@@_stop \s_@@_stop \use_ii:nn
776 }
777 \cs_new:Npn \@@_if_empty:w #1 \s_@@_stop \s_@@_stop {}
778 \cs_new:Npn \@@_if_empty_true:w \s_@@_stop \s_@@_stop \use_none:n #1
779 {#1}
780 \cs_new:Npn \@@_if_empty_true_TF:w \s_@@_stop \s_@@_stop \use_ii:nn #1#2
781 {#1}
782 % \end{macrocode}
783 % \end{macro}
784 %
785 % \begin{macro}[EXP]{\@@_if_head_is_group:nTF,\@@_if_head_is_group:nT}
786 % This test works pretty much the same way \cs{tl_if_head_is_group:nTF} works,
787 % but it is faster because it gets rid of the unnecessary \cs{if:w} and
788 % instead only works by argument gobbling. Drawback is that if you only expand
789 % the macro twice you could end up with unbalanced braces.
790 % \begin{macrocode}
791 \cs_new:Npn \@@_if_head_is_group:nTF #1
792 {
793 \exp_after:wN \use_none:n \exp_after:wN
794 {
795 \exp_after:wN { \token_to_str:N #1 ? }
796 \exp_after:wN \use_iii:nnnn \token_to_str:N
797 }
798 \use_ii:nn
799 }
800 \cs_new:Npn \@@_if_head_is_group:nT #1
801 {
802 \exp_after:wN \use_none:n \exp_after:wN
803 {
804 \exp_after:wN { \token_to_str:N #1 ? }
805 \exp_after:wN \use_iii:nnn \token_to_str:N
806 }
807 \use_none:n
808 }
809 % \end{macrocode}
810 % \end{macro}
811 %
812 %
813 % \subsection{The \texttt{act} loop}
814 %
815 % \begin{macro}[EXP]{\etl_act:nnnnnnn,\etl_act:nnnnnn,\etl_act:nnnnn}
816 % \begin{macro}[EXP]{\@@_act:nnnnnnn}
817 % \begin{macro}[EXP]{\@@_act_just_result:nn}
818 % The act loop is modelled after the \pkg{expl3} internal \cs{__tl_act:NNNn}
819 % but with a few more features making it more general. Those are (argument
820 % to \cs{etl_act:nnnnnnn} in parentheses): a status (|#5|),
821 % |n|-type mapping instead of just |N|-type functions, a final result
822 % processing (|#4|), and the possibility to preset some output (|#6|). The
823 % other arguments are: |#7| the token list on which we should act, |#1|
824 % function to use for |N|-type elements in that list, |#2| function to use for
825 % spaces in that list, and |#3| function to use on groups in that list.
826 %
827 % Just like the \cs{__tl_act:NNNn} function, this has a token which must not
828 % occur in the arguments, in this case that token is \cs{s_@@_stop}. The
829 % result is stored as an argument to the (undefined) function
830 % \cs{@@_act_result:n}.
831 % \begin{macrocode}
832 \cs_new:Npn \etl_act:nnnnnnn #1#2#3#4#5#6#7
833 {
834 \@@_unexpanded:w \@@_expanded:w
835 {{
836 \@@_act:w #7 {\s_@@_stop} . \s_@@_stop {#5} {#1} {#2} {#3}
837 \@@_act_result:n {#6} {#4}
838 }}
839 }
840 % \end{macrocode}
841 % We also provide a version without the \cs{@@_unexpanded:w} around it for
842 % internal purposes, in which we'd otherwise have to remove it for correct
843 % behaviour. For that we use \cs{use_ii_iii:nnn} which will remove one set
844 % of braces and the \cs{@@_unexpanded:w}.
845 % \begin{macrocode}
846 \exp_args:NNno \exp_args:Nno \use:n
847 { \cs_new:Npn \@@_act:nnnnnnn #1#2#3#4#5#6#7 }
848 {
849 \exp_after:wN \use_ii_iii:nnn
850 \etl_act:nnnnnnn {#1} {#2} {#3} {#4} {#5} {#6} {#7}
851 }
852 % \end{macrocode}
853 % We also provide two reduced function variants, the first without presetting
854 % some output, the second also without the final processor.
855 % \begin{macrocode}
856 \exp_args:Nno \use:n { \cs_new:Npn \etl_act:nnnnnn #1#2#3#4#5#6 }
857 { \etl_act:nnnnnnn {#1} {#2} {#3} {#4} {#5} {} {#6} }
858 \exp_args:Nno \use:n { \cs_new:Npn \etl_act:nnnnn #1#2#3#4#5 }
859 { \etl_act:nnnnnnn {#1} {#2} {#3} \@@_act_just_result:nn {#4} {} {#5} }
860 % \end{macrocode}
861 % The final processor is provided with two |n|-type arguments (both in braces)
862 % the first being the status, the second the output. To just get the output we
863 % gobble the status and put \cs{@@_unexpanded:w} there to protect the final
864 % output from further expanding.
865 % \begin{macrocode}
866 \cs_new:Npn \@@_act_just_result:nn #1 { \@@_unexpanded:w }
867 % \end{macrocode}
868 % \end{macro}
869 % \end{macro}
870 % \end{macro}
871 %
872 % \begin{macro}[EXP]{\@@_if_head_is_space:nTF,\@@_if_head_is_space_true:w}
873 % \begin{macro}[EXP]{\@@_if_head_is_N_type:nTF,\@@_if_head_is_N_type_false:w}
874 % \begin{macro}[EXP]{\@@_act:w,\@@_act_if_space:w,\@@_act_space:w}
875 % We need a few macros with spaces at weird places so define them here. Since
876 % we got the limitation of not allowing \cs{s_@@_stop} we can use that token
877 % to get some fast tests. The first tests for a space at the front, and since
878 % that one is pretty fast we can use it to build a faster alternative to check
879 % for a starting |N|-type as well (with the drawback that this would yield
880 % |true| for an empty argument, something we have to keep in mind).
881 % \begin{macrocode}
882 \group_begin:
883 \cs_set:Npn \@@_tmp:n #1
884 {
885 \cs_new:Npn \@@_if_head_is_space:nTF ##1
886 {
887 \@@_act_if_space:w
888 \s_@@_stop ##1 \s_@@_stop \@@_if_head_is_space_true:w
889 \s_@@_stop #1 \s_@@_stop \use_ii:nn
890 }
891 \cs_new:Npn \@@_if_head_is_space_true:w
892 \s_@@_stop #1 \s_@@_stop \use_ii:nn ##1##2
893 {##1}
894 \cs_new:Npn \@@_if_head_is_N_type:nTF ##1
895 {
896 \@@_act_if_space:w
897 \s_@@_stop ##1 \s_@@_stop \@@_if_head_is_N_type_false:w
898 \s_@@_stop #1 \s_@@_stop
899 \@@_if_head_is_group:nT {##1} \use_iii:nnn
900 \use_i:nn
901 }
902 \cs_new:Npn \@@_if_head_is_N_type_false:w
903 \s_@@_stop #1 \s_@@_stop
904 \@@_if_head_is_group:nT ##1 \use_iii:nnn
905 \use_i:nn
906 ##2##3
907 {##3}
908 % \end{macrocode}
909 % The act loop \cs{@@_act:w} grabs the remainder of the list, delimited by
910 % \cs{s_@@_stop}, picks up the status (|##2|), and the user provided functions
911 % for |N|-types (|##3|), spaces (|##4|), and groups (|##5|). We need to check
912 % which type is at the head of the token list (the space test is a bit
913 % stripped down, and very fast this way).
914 % \begin{macrocode}
915 \cs_new:Npn \@@_act:w ##1 \s_@@_stop ##2##3##4##5
916 {
917 \@@_act_if_space:w
918 \s_@@_stop ##1 \s_@@_stop \@@_act_space:w {##4}
919 \s_@@_stop #1 \s_@@_stop
920 \@@_if_head_is_group:nT {##1} \@@_act_group:w
921 \@@_act_normal:w {##3} {##5}
922 {##2} ##1 \s_@@_stop {##2} {##3} {##4} {##5}
923 }
924 % \end{macrocode}
925 % The check for spaces just gobbles everything up to the first
926 % \cs{s_@@_stop}\verb*| |. If we found a space at the head we remove that
927 % space and leave in the input the space function, the status, and
928 % \cs{@@_act:w} for the next iteration.
929 % \begin{macrocode}
930 \cs_new:Npn \@@_act_if_space:w ##1 \s_@@_stop #1 ##2 \s_@@_stop {}
931 \cs_new:Npn \@@_act_space:w
932 ##1 \s_@@_stop #1 \s_@@_stop
933 \@@_if_head_is_group:nT ##2 \@@_act_group:w \@@_act_normal:w ##3 ##4
934 ##5 #1
935 { ##1 {##5} \@@_act:w }
936 }
937 \@@_tmp:n { ~ }
938 \group_end:
939 % \end{macrocode}
940 % \end{macro}
941 % \end{macro}
942 % \end{macro}
943 % \begin{macro}[EXP]{\@@_act_normal:w}
944 % For a normal token we can act quite easy, just pick up that token and leave
945 % the next iteration in the input stream (|#2| is the group code, which is
946 % gobbled).
947 % \begin{macrocode}
948 \cs_new:Npn \@@_act_normal:w #1#2#3#4 { #1 {#3} #4 \@@_act:w }
949 % \end{macrocode}
950 % \end{macro}
951 % \begin{macro}[EXP]{\@@_act_group:w,\@@_act_if_end:w}
952 % Since the end marker is a single \cs{s_@@_stop} in a group, we have to test
953 % whether that end marker is found. The test here leads to undefined behaviour
954 % if the user supplied token list contains such a marker at an any point. If
955 % the end marker is found we call the final handler (for which we have to
956 % remove the \cs{s_@@_stop} to correctly grab its arguments), else we provide
957 % the user supplied function the next group and input the next iteration. |#1|
958 % is the normal function, which is gobbled.
959 % \begin{macrocode}
960 \cs_new:Npn \@@_act_group:w \@@_act_normal:w #1#2#3#4
961 {
962 \@@_act_if_end:w #4 \use_i:nn \etl_act_do_final: \s_@@_stop
963 #2 {#3} {#4} \@@_act:w
964 }
965 \cs_new:Npn \@@_act_if_end:w #1 \s_@@_stop {}
966 % \end{macrocode}
967 % \end{macro}
968 % \begin{macro}[EXP]
969 % {
970 % \etl_act_output:n, \etl_act_output_pre:n,
971 % \etl_act_output_rest:, \etl_act_output_rest_pre:
972 % }
973 % \begin{macro}[EXP]
974 % {
975 % \@@_act_output_normal:nN, \@@_act_output_space:n, \@@_act_output_group:nn,
976 % \@@_act_output_normal_pre:nN, \@@_act_output_space_pre:n,
977 % \@@_act_output_group_pre:nn
978 % }
979 % \begin{macro}[EXP]
980 % {
981 % \@@_act_unexpanded_normal:nN, \@@_act_unexpanded_space:n,
982 % \@@_act_unexpanded_group:nn
983 % }
984 % To allow reordering the output we unfortunately can't just use
985 % \cs{@@_unexpanded:w} and be done, so we have to shift a few tokens around
986 % instead. All the output macros work by the same idea, except for
987 % \cs{etl_act_output_rest:} and \cs{etl_act_output_rest_pre:}, since it's a
988 % non-trivial task to get the remainder of the argument. Instead these two
989 % swap out the user provided functions for some that only pass through the
990 % input as output, for which we need six internal output macros.
991 %
992 % In those cases in which we don't need reordering, we can internally shortcut
993 % using \cs{@@_unexpanded:w}.
994 % \begin{macrocode}
995 \cs_new:Npn \etl_act_output:n #1 #2 \@@_act_result:n #3
996 { #2 \@@_act_result:n { #3 #1 } }
997 \cs_new:Npn \etl_act_output_pre:n #1 #2 \@@_act_result:n #3
998 { #2 \@@_act_result:n { #1 #3 } }
999 \cs_new:Npn \etl_act_output_rest: #1 \s_@@_stop #2#3#4#5
1000 {
1001 #1 \s_@@_stop {#2}
1002 \@@_act_output_normal:nN \@@_act_output_space:n \@@_act_output_group:nn
1003 }
1004 \cs_new:Npn \etl_act_output_rest_pre: #1 \s_@@_stop #2#3#4#5
1005 {
1006 #1 \s_@@_stop {#2}
1007 \@@_act_output_normal_pre:nN
1008 \@@_act_output_space_pre:n
1009 \@@_act_output_group_pre:nn
1010 }
1011 \cs_new:Npn \@@_act_output_normal:nN #1#2 #3 \@@_act_result:n #4
1012 { #3 \@@_act_result:n { #4 #2 } }
1013 \cs_new:Npn \@@_act_output_space:n #1 #2 \@@_act_result:n #3
1014 { #2 \@@_act_result:n { #3 ~ } }
1015 \cs_new:Npn \@@_act_output_group:nn #1#2 #3 \@@_act_result:n #4
1016 { #3 \@@_act_result:n { #4 {#2} } }
1017 \cs_new:Npn \@@_act_output_normal_pre:nN #1#2 #3 \@@_act_result:n #4
1018 { #3 \@@_act_result:n { #2 #4 } }
1019 \cs_new:Npn \@@_act_output_space_pre:n #1 #2 \@@_act_result:n #3
1020 { #2 \@@_act_result:n { ~ #3 } }
1021 \cs_new:Npn \@@_act_output_group_pre:nn #1#2 #3 \@@_act_result:n #4
1022 { #3 \@@_act_result:n { {#2} #4 } }
1023 \cs_new:Npn \@@_act_unexpanded_normal:nN #1 { \exp_not:N }
1024 \cs_new:Npn \@@_act_unexpanded_space:n #1 { ~ }
1025 \cs_new:Npn \@@_act_unexpanded_group:nn #1#2 { { \@@_unexpanded:w {#2} } }
1026 % \end{macrocode}
1027 % \end{macro}
1028 % \end{macro}
1029 % \end{macro}
1030 %
1031 % \begin{macro}[EXP]
1032 % {
1033 % \@@_act_unexpanded_rest:w,
1034 % \@@_act_unexpanded_rest_aux:w, \@@_act_unexpanded_rest_aux:n,
1035 % \@@_act_unexpanded_rest_done:w
1036 % }
1037 % This function tries to shortcut as much as possible to output the
1038 % remainder of the token list in an unchanged way. This can be done by
1039 % grabbing everything up to the next opening brace (since the stop marker is
1040 % contained in a group), outputting that, and checking whether we're done. If
1041 % not output the next group and loop. The first step removes the act code.
1042 %
1043 % Drawback of this is that spaces and braces are not normalised for the
1044 % remainder of that call (which is what we had documented).
1045 % \begin{macrocode}
1046 \cs_new:Npn \@@_act_unexpanded_rest:w #1 \@@_act:w #2#
1047 {
1048 \@@_unexpanded:w {#2}
1049 \@@_act_unexpanded_rest_aux:n
1050 }
1051 \cs_new:Npn \@@_act_unexpanded_rest_aux:w #1#
1052 {
1053 \@@_unexpanded:w {#1}
1054 \@@_act_unexpanded_rest_aux:n
1055 }
1056 \cs_new:Npn \@@_act_unexpanded_rest_aux:n #1
1057 {
1058 \@@_act_if_end:w #1 \@@_act_unexpanded_rest_done:w \s_@@_stop
1059 { \@@_unexpanded:w {#1} }
1060 \@@_act_unexpanded_rest_aux:w
1061 }
1062 \cs_new:Npn \@@_act_unexpanded_rest_done:w
1063 \s_@@_stop #1 \@@_act_unexpanded_rest_aux:w
1064 . \s_@@_stop #2#3#4#5
1065 \@@_act_result:n #6#7
1066 {}
1067 % \end{macrocode}
1068 % \end{macro}
1069 %
1070 % \begin{macro}[EXP]{\etl_act_status:n}
1071 % Just switch out the status which is stored immediately after \cs{s_@@_stop}.
1072 % \begin{macrocode}
1073 \cs_new:Npn \etl_act_status:n #1 #2 \s_@@_stop #3
1074 { #2 \s_@@_stop {#1} }
1075 % \end{macrocode}
1076 % \end{macro}
1077 %
1078 % \begin{macro}[EXP]{\etl_act_put_back:n}
1079 % Place the first argument after the next iteration of the loop. This macro
1080 % might strip a set of braces around |#2|, because it could happen that the
1081 % user provided code only leaves one group between this functions argument
1082 % and \cs{@@_act:w}, but that would arguably be wrong input anyway, an easy
1083 % fix would be to use
1084 % \begin{verbatim}
1085 % \cs_new:Npn \etl_act_put_back:n #1
1086 % { \@@_act_put_back:nw {#1} \prg_do_nothing: }
1087 % \cs_new:Npn \@@_act_put_back:nw #1 #2 \@@_act:w { #2 \@@_act:w #1 }
1088 % \end{verbatim}
1089 % instead of:
1090 % \begin{macrocode}
1091 \cs_new:Npn \etl_act_put_back:n #1 #2 \@@_act:w { #2 \@@_act:w #1 }
1092 % \end{macrocode}
1093 % \end{macro}
1094 %
1095 % \begin{macro}[EXP]
1096 % {
1097 % \etl_act_switch:nnn, \etl_act_switch_normal:n,
1098 % \etl_act_switch_space:n, \etl_act_switch_group:n
1099 % }
1100 % Pretty straight forward, just switch out the user provided functions for the
1101 % new argument.
1102 % \begin{macrocode}
1103 \cs_new:Npn \etl_act_switch:nnn #1#2#3 #4 \s_@@_stop #5#6#7#8
1104 { #4 \s_@@_stop {#5} {#1} {#2} {#3} }
1105 \cs_new:Npn \etl_act_switch_normal:n #1 #2 \s_@@_stop #3#4
1106 { #2 \s_@@_stop {#3} {#1} }
1107 \cs_new:Npn \etl_act_switch_space:n #1 #2 \s_@@_stop #3#4#5
1108 { #2 \s_@@_stop {#3} {#4} {#1} }
1109 \cs_new:Npn \etl_act_switch_group:n #1 #2 \s_@@_stop #3#4#5#6
1110 { #2 \s_@@_stop {#3} {#4} {#5} {#1} }
1111 % \end{macrocode}
1112 % \end{macro}
1113 %
1114 % \begin{macro}[EXP]
1115 % {
1116 % \etl_act_do_final:,
1117 % \etl_act_break:,\etl_act_break_discard:,
1118 % \etl_act_break:n, \etl_act_break_pre:n, \etl_act_break_post:n
1119 % }
1120 % These are different forms to end the loop. The first will gobble the
1121 % remainder and apply the final action on the token list currently stored for
1122 % output.
1123 %
1124 % The |break| variants will gobble the final action and output what's
1125 % currently there (except for the |discard| variant).
1126 % \begin{macrocode}
1127 \cs_new:Npn \etl_act_do_final: #1 \s_@@_stop #2#3 \@@_act_result:n #4#5
1128 { #5 {#2} {#4} }
1129 \cs_new:Npn \etl_act_break: #1 \@@_act_result:n #2#3 { \@@_unexpanded:w {#2} }
1130 \cs_new:Npn \etl_act_break_discard: #1 \@@_act_result:n #2#3 {}
1131 \cs_new:Npn \etl_act_break:n #1 #2 \@@_act_result:n #3#4
1132 { \@@_unexpanded:w {#1} }
1133 \cs_new:Npn \etl_act_break_pre:n #1 #2 \@@_act_result:n #3#4
1134 { \@@_unexpanded:w { #1 #3 } }
1135 \cs_new:Npn \etl_act_break_post:n #1 #2 \@@_act_result:n #3#4
1136 { \@@_unexpanded:w { #3 #1 } }
1137 % \end{macrocode}
1138 % \end{macro}
1139 %
1140 % \begin{macro}[EXP]{\etl_act_apply_to_rest:n}
1141 % This function can be used to get the remainder of the input \meta{token
1142 % list} and apply some user code on it. To get past the user code we use
1143 % \cs{@@_expanded:w} with a brace trick. The heavy lifting is done by
1144 % \cs{@@_act_get_rest:w}. The \cs{prg_do_nothing:} prevents accidental brace
1145 % stripping and will be removed by \cs{@@_act_get_rest:w}.
1146 % \begin{macrocode}
1147 \cs_new:Npn \etl_act_apply_to_rest:n #1
1148 {
1149 \@@_expanded:w { \@@_unexpanded:w {#1} { \if_false: }} \fi:
1150 \@@_act_get_rest:w \prg_do_nothing:
1151 }
1152 % \end{macrocode}
1153 % \end{macro}
1154 %
1155 % \begin{macro}[EXP]{\@@_act_get_rest:w}
1156 % This macro should be used with two unbalanced opening braces before it and
1157 % inside an \cs{@@_expanded:w} context with a \cs{prg_do_nothing:} following
1158 % it to prevent accidental brace stripping. It'll expand to the remainder of
1159 % the input \meta{token list} followed by a closing brace and leaves
1160 % everything else untouched.
1161 % \begin{macro}[EXP]
1162 % {\@@_act_get_rest_aux:nw, \@@_act_get_rest_aux:nn, \@@_act_get_rest_done:w}
1163 % \begin{macrocode}
1164 \cs_new:Npn \@@_act_get_rest:w #1 \@@_act:w #2#
1165 {
1166 \@@_unexpanded:w {#2}
1167 \@@_act_get_rest_aux:nn {#1}
1168 }
1169 \cs_new:Npn \@@_act_get_rest_aux:nw #1 #2#
1170 {
1171 \@@_unexpanded:w {#2}
1172 \@@_act_get_rest_aux:nn {#1}
1173 }
1174 % \end{macrocode}
1175 % Each group might be the end marker, so we check that, else leave the group
1176 % there and grab until the next group starts.
1177 % \begin{macrocode}
1178 \cs_new:Npn \@@_act_get_rest_aux:nn #1#2
1179 {
1180 \@@_act_if_end:w #2 \@@_act_get_rest_done:w \s_@@_stop
1181 { \@@_unexpanded:w {#2} }
1182 \@@_act_get_rest_aux:nw {#1}
1183 }
1184 % \end{macrocode}
1185 % This closes the two unbalanced opening braces (ending the \cs{@@_expanded:w}
1186 % context) and puts the remaining code back in the input stream (as well as
1187 % the now empty next \cs{@@_act:w} step). The \cs{exp_after:wN} removes the
1188 % leading \cs{prg_do_nothing:}.
1189 % \begin{macrocode}
1190 \cs_new:Npn \@@_act_get_rest_done:w \s_@@_stop #1 \@@_act_get_rest_aux:nw #2
1191 {
1192 \if_false: {{ \fi: } \exp_after:wN }
1193 #2
1194 \@@_act:w { \s_@@_stop }
1195 }
1196 % \end{macrocode}
1197 % \end{macro}
1198 % \end{macro}
1199 %
1200 %
1201 % \subsection{Expandable tests}
1202 %
1203 % \begin{macro}[pTF]{\etl_token_if_eq:NN}
1204 % We consider two tokens equal when they have the same meaning and the same
1205 % string representation. This isn't always correct. If an active character is
1206 % let to the same character with a different category code those two tokens
1207 % aren't distinguishable by expansion, \emph{afaik}. To get the optimisation
1208 % of \cs{prg_new_conditional:Npnn} we use \cs{if_false:} and turn it true if
1209 % both tests are true (this is easier than coding all four variants by hand,
1210 % even though that could give slightly better performance). The exception
1211 % being the |TF| variant, since that is used in the inner loop of many
1212 % functions. The braces around the arguments of \cs{token_if_eq_meaning:NNT}
1213 % are necessary because of the first step of expansion applied to that
1214 % function.
1215 % \begin{macrocode}
1216 \exp_args:Nno \use:n { \cs_new:Npn \etl_token_if_eq:NNTF #1#2 }
1217 {
1218 \token_if_eq_meaning:NNT {#1} {#2} { \str_if_eq:nnT #1#2 \use_ii:nnn }
1219 \use_ii:nn
1220 }
1221 \exp_args:Nno
1222 \use:n { \prg_new_conditional:Npnn \etl_token_if_eq:NN #1#2 { T , F , p } }
1223 {
1224 \token_if_eq_meaning:NNT {#1} {#2} { \str_if_eq:nnT #1#2 \@@_turn_true:w }
1225 \if_false:
1226 \prg_return_true:
1227 \else:
1228 \prg_return_false:
1229 \fi:
1230 }
1231 % \end{macrocode}
1232 % \end{macro}
1233 %
1234 % \begin{macro}[pTF]{\etl_token_if_in:nN}
1235 % \begin{macro}[EXP]{\@@_token_if_in:NnN}
1236 % Searching for just a single token is rather easy, we just loop over the list
1237 % and compare the |N|-type tokens to the one token provided. If we find a
1238 % match we break and return |true|, else we'll return |false| eventually.
1239 % \begin{macrocode}
1240 \exp_args:Nno
1241 \use:n { \prg_new_conditional:Npnn \etl_token_if_in:nN #1#2 { TF , T , F , p } }
1242 {
1243 \@@_act:nnnnnnn
1244 \@@_token_if_in:NN \use_none:n \use_none:nn
1245 \@@_act_just_result:nn
1246 {#2}
1247 \if_false:
1248 {#1}
1249 \prg_return_true:
1250 \else:
1251 \prg_return_false:
1252 \fi:
1253 }
1254 \exp_args:Nno \use:n { \cs_new:Npn \@@_token_if_in:NN #1#2 }
1255 {
1256 \etl_token_if_eq:NNTF {#1} {#2} { \etl_act_break:n \if_true: } {}
1257 }
1258 % \end{macrocode}
1259 % \end{macro}
1260 % \end{macro}
1261 %
1262 % \begin{macro}[pTF]{\etl_token_if_in_deep:nN}
1263 % \begin{macro}[EXP]{\@@_token_if_in_deep:Nn}
1264 % The deep variant just has to recursively call itself on groups to also
1265 % search those.
1266 % \begin{macrocode}
1267 \exp_args:Nno \use:n
1268 { \prg_new_conditional:Npnn \etl_token_if_in_deep:nN #1#2 { TF , T , F , p } }
1269 {
1270 \@@_act:nnnnnnn
1271 \@@_token_if_in:NN \use_none:n \@@_token_if_in_deep:Nn
1272 \@@_act_just_result:nn
1273 {#2}
1274 \if_false:
1275 {#1}
1276 \prg_return_true:
1277 \else:
1278 \prg_return_false:
1279 \fi:
1280 }
1281 \exp_args:Nno \use:n { \cs_new:Npn \@@_token_if_in_deep:Nn #1#2 }
1282 { \etl_token_if_in_deep:nNT {#2} {#1} { \etl_act_break:n \if_true: } }
1283 % \end{macrocode}
1284 % \end{macro}
1285 % \end{macro}
1286 %
1287 % \begin{macro}[pTF]{\etl_if_eq:nn}
1288 % \begin{macro}[EXP]{\@@_if_eq_normal:nN,\@@_if_eq_normal:NnN}
1289 % \begin{macro}[EXP]{\@@_if_eq_space:n}
1290 % \begin{macro}[EXP]{\@@_if_eq_group:nn,\@@_if_eq_group:nnn}
1291 % \begin{macro}[EXP]{\@@_if_eq_final:nn}
1292 % The test needs to compare the full lists on a token-by-token basis. One of
1293 % the two lists is stored inside the status the other is processed. The |act|
1294 % code will then leave either \cs{if_false:} or \cs{if_true:} in the input
1295 % stream.
1296 % \begin{macrocode}
1297 \exp_args:Nno
1298 \use:n { \prg_new_conditional:Npnn \etl_if_eq:nn #1#2 { TF , T , F , p } }
1299 {
1300 \@@_act:nnnnnnn
1301 \@@_if_eq_normal:nN
1302 \@@_if_eq_space:n
1303 \@@_if_eq_group:nn
1304 \@@_if_eq_final:nn
1305 {#2}
1306 {}
1307 {#1}
1308 \prg_return_true:
1309 \else:
1310 \prg_return_false:
1311 \fi:
1312 }
1313 % \end{macrocode}
1314 % To compare the next token we need to check whether the status is already
1315 % empty (which would mean that token list is longer, hence not equal), if it's
1316 % not empty and the head is |N|-type we compare these two (the test here for
1317 % |N|-type fails for empty arguments, hence we have to test this separately).
1318 % If they are equal we store the rest of the second token list in the status
1319 % and go on with the loop, else we break out and return false.
1320 % \begin{macrocode}
1321 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_normal:nN #1#2 }
1322 {
1323 \@@_if_empty:nT {#1} { \etl_act_break:n \if_false: }
1324 \@@_if_head_is_N_type:nTF {#1}
1325 {
1326 \exp_after:wN \@@_if_eq_normal:NnN
1327 \@@_expanded:w { \@@_split_first:w #1 }
1328 #2
1329 }
1330 { \etl_act_break:n \if_false: }
1331 }
1332 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_normal:NnN #1#2#3 }
1333 {
1334 \etl_token_if_eq:NNTF {#1} {#3}
1335 { \etl_act_status:n {#2} }
1336 { \etl_act_break:n \if_false: }
1337 }
1338 % \end{macrocode}
1339 % Spaces are pretty similar, but easier, we don't need to split of the first
1340 % token in a complicated manner, instead we just gobble a leading space.
1341 % \begin{macrocode}
1342 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_space:n #1 }
1343 {
1344 \@@_if_head_is_space:nTF {#1}
1345 { \exp_after:wN \etl_act_status:n \exp_after:wN { \@@_rm_space:w #1 } }
1346 { \etl_act_break:n \if_false: }
1347 }
1348 % \end{macrocode}
1349 % Groups are similarly handled to normal arguments, but instead of comparing
1350 % only two tokens we have to compare by recursion.
1351 % \begin{macrocode}
1352 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_group:nn #1 }
1353 {
1354 \@@_if_head_is_group:nTF {#1}
1355 {
1356 \exp_after:wN \@@_if_eq_group:nnn
1357 \@@_expanded:w { \@@_split_first:w #1 }
1358 }
1359 { \etl_act_break:n \if_false: }
1360 }
1361 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_group:nnn #1#2#3 }
1362 {
1363 \etl_if_eq:nnTF {#1} {#3}
1364 { \etl_act_status:n {#2} }
1365 { \etl_act_break:n \if_false: }
1366 }
1367 % \end{macrocode}
1368 % Finally, if the loop didn't break until the first token list is empty we
1369 % just have to make sure that the second list is also empty by now. If that's
1370 % the case the two are equal, else not. We need to leave either true or false
1371 % (protected against the \cs{@@_expanded:w} expansion) in the input.
1372 % \begin{macrocode}
1373 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_eq_final:nn #1#2 }
1374 {
1375 \exp_after:wN
1376 \@@_unexpanded:w
1377 \@@_if_empty:nT {#1} { { \if_true: } \use_none:n } { \if_false: }
1378 }
1379 % \end{macrocode}
1380 % \end{macro}
1381 % \end{macro}
1382 % \end{macro}
1383 % \end{macro}
1384 % \end{macro}
1385 %
1386 % \begin{macro}[pTF]{\etl_if_in:nn}
1387 % \begin{macro}[EXP]
1388 % {\@@_if_in_normal:nN,\@@_if_in_normal:nnnN,\@@_if_in_normal:NnnnN}
1389 % \begin{macro}[EXP]{\@@_if_in_space:n,\@@_if_in_space:nnn}
1390 % \begin{macro}[EXP]
1391 % {\@@_if_in_group:nn,\@@_if_in_group:nnnn,\@@_if_in_group:nnnnn}
1392 % \begin{macro}[EXP]{\@@_if_in_put_back:n}
1393 % \cs{etl_if_in:nn} has to reevaluate every token but the very first in order
1394 % to compare them, else something like |aab| wouldn't contain |ab| according
1395 % to the test, because the second |a| would've been gobbled. For this we need
1396 % \cs{@@_if_in_put_back:n} which will remove the first token (we need to
1397 % watch out for spaces) and puts the rest back using \cs{etl_act_put_back:n}.
1398 % \begin{macrocode}
1399 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_put_back:n #1 }
1400 {
1401 \@@_if_head_is_space:nTF {#1}
1402 { \exp_after:wN \etl_act_put_back:n \exp_after:wN { \@@_rm_space:w #1 } }
1403 { \exp_after:wN \etl_act_put_back:n \exp_after:wN { \use_none:n #1 } }
1404 }
1405 % \end{macrocode}
1406 % As already said, we'll need to reinsert some tokens, and we'll might have to
1407 % revert what was already matched, so inside of the status we store the
1408 % remainder of the pattern which needs to be matched, followed by the entire
1409 % pattern, followed by the tokens which were already matched (and might need
1410 % to be put back). As soon as the pattern is matched the remainder will be
1411 % empty and we'll leave \cs{if_true:} in the input, at the end of the entire
1412 % list we'll leave \cs{if_false:}, which we store in the prefilled output. The
1413 % emptiness of the pattern will be checked before the next token is evaluated,
1414 % so the trailing space after |#1| does no harm but allows the token list to
1415 % end in the pattern.
1416 %
1417 % All of the macros used as arguments to \cs{@@_act:nnnnnnn} will need to
1418 % unbrace the status which will then lead to three arguments. Else this is
1419 % pretty much the same idea as \cs{etl_if_eq:nnTF}.
1420 % \begin{macrocode}
1421 \exp_args:Nno
1422 \use:n { \prg_new_conditional:Npnn \etl_if_in:nn #1#2 { TF , T , F , p } }
1423 {
1424 \@@_act:nnnnnnn
1425 \@@_if_in_normal:nN \@@_if_in_space:n \@@_if_in_group:nn
1426 \@@_act_just_result:nn
1427 { { #2 } { #2 } {} }
1428 \if_false:
1429 { #1 ~ }
1430 \prg_return_true:
1431 \else:
1432 \prg_return_false:
1433 \fi:
1434 }
1435 % \end{macrocode}
1436 % Just like \cs{@@_if_in_group:nn}, \cs{@@_if_in_normal:nN} needs to split off
1437 % the first token of the pattern, for which \cs{@@_split_first:w} is used,
1438 % and \cs{@@_if_in_space:n} needs to trim off a leading space.
1439 % \begin{macrocode}
1440 \cs_new:Npn \@@_if_in_normal:nN #1 { \@@_if_in_normal:nnnN #1 }
1441 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_normal:nnnN #1#2#3#4 }
1442 {
1443 \@@_if_empty:nT {#1} { \etl_act_break:n \if_true: }
1444 \@@_if_head_is_N_type:nTF {#1}
1445 {
1446 \exp_after:wN \@@_if_in_normal:NnnnN
1447 \@@_expanded:w { \@@_split_first:w #1 } {#2} {#3} #4
1448 }
1449 {
1450 \etl_act_status:n { {#2} {#2} {} }
1451 \@@_if_in_put_back:n { #3 #4 }
1452 }
1453 }
1454 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_normal:NnnnN #1#2#3#4#5 }
1455 {
1456 \etl_token_if_eq:NNTF {#1} {#5}
1457 { \etl_act_status:n { {#2} {#3} {#4#5} } }
1458 {
1459 \@@_if_in_put_back:n { #4 #5 }
1460 \etl_act_status:n { {#3} {#3} {} }
1461 }
1462 }
1463 \cs_new:Npn \@@_if_in_space:n #1 { \@@_if_in_space:nnn #1 }
1464 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_space:nnn #1#2#3 }
1465 {
1466 \@@_if_empty:nT {#1} { \etl_act_break:n \if_true: }
1467 \@@_if_head_is_space:nTF {#1}
1468 {
1469 \exp_after:wN \etl_act_status:n \exp_after:wN
1470 { \exp_after:wN { \@@_rm_space:w #1 } {#2} { #3 ~ } }
1471 }
1472 {
1473 \@@_if_in_put_back:n { #3 ~ }
1474 \etl_act_status:n { {#2} {#2} {} }
1475 }
1476 }
1477 \cs_new:Npn \@@_if_in_group:nn #1 { \@@_if_in_group:nnnn #1 }
1478 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group:nnnn #1 }
1479 {
1480 \@@_if_empty:nT {#1} { \etl_act_break:n \if_true: }
1481 \@@_if_head_is_group:nTF {#1}
1482 {
1483 \exp_after:wN \@@_if_in_group:nnnnn
1484 \@@_expanded:w { \@@_split_first:w #1 }
1485 }
1486 { \@@_if_in_group_false:nnn }
1487 }
1488 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group:nnnnn #1#2#3#4#5 }
1489 {
1490 \etl_if_eq:nnTF {#1} {#5}
1491 { \etl_act_status:n { {#2} {#3} { #4 {#5} } } }
1492 {
1493 \@@_if_in_put_back:n { #4 {#5} }
1494 \etl_act_status:n { {#3} {#3} {} }
1495 }
1496 }
1497 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group_false:nnn #1#2#3 }
1498 {
1499 \@@_if_in_put_back:n { #2 {#3} }
1500 \etl_act_status:n { {#1} {#1} {} }
1501 }
1502 % \end{macrocode}
1503 % \end{macro}
1504 % \end{macro}
1505 % \end{macro}
1506 % \end{macro}
1507 % \end{macro}
1508 %
1509 % \begin{macro}[pTF]{\etl_if_in_deep:nn}
1510 % \begin{macro}[EXP]
1511 % {
1512 % \@@_if_in_group_deep:nn, \@@_if_in_group_deep:nnnn,
1513 % \@@_if_in_group_deep:nnnnn, \@@_if_in_group_deep_false:nnn
1514 % }
1515 % In essence this is the same as \cs{etl_if_in:nnTF}, but additionally
1516 % every time a group is encountered we need to search that group by recursion
1517 % as well after directly comparing it to the pattern.
1518 % \begin{macrocode}
1519 \exp_args:Nno
1520 \use:n { \prg_new_conditional:Npnn \etl_if_in_deep:nn #1#2 { TF , T , F , p } }
1521 {
1522 \@@_act:nnnnnnn
1523 \@@_if_in_normal:nN \@@_if_in_space:n \@@_if_in_group_deep:nn
1524 \@@_act_just_result:nn
1525 { { #2 } { #2 } {} }
1526 \if_false:
1527 { #1 ~ }
1528 \prg_return_true:
1529 \else:
1530 \prg_return_false:
1531 \fi:
1532 }
1533 \cs_new:Npn \@@_if_in_group_deep:nn #1 { \@@_if_in_group_deep:nnnn #1 }
1534 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group_deep:nnnn #1 }
1535 {
1536 \@@_if_empty:nT {#1} { \etl_act_break:n \if_true: }
1537 \@@_if_head_is_group:nTF {#1}
1538 {
1539 \exp_after:wN \@@_if_in_group_deep:nnnnn
1540 \@@_expanded:w { \@@_split_first:w #1 }
1541 }
1542 { \@@_if_in_group_deep_false:nnn }
1543 }
1544 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group_deep:nnnnn #1#2#3#4#5 }
1545 {
1546 \etl_if_eq:nnTF {#1} {#5}
1547 { \etl_act_status:n { {#2} {#3} { #4 {#5} } } }
1548 {
1549 \etl_if_in_deep:nnT {#5} {#3} { \etl_act_break:n \if_true: }
1550 \@@_if_in_put_back:n { #4 {#5} }
1551 \etl_act_status:n { {#3} {#3} {} }
1552 }
1553 }
1554 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_in_group_deep_false:nnn #1#2#3 }
1555 {
1556 \etl_if_in_deep:nnT {#3} {#1} { \etl_act_break:n \if_true: }
1557 \@@_if_in_put_back:n { #2 {#3} }
1558 \etl_act_status:n { {#1} {#1} {} }
1559 }
1560 % \end{macrocode}
1561 % \end{macro}
1562 % \end{macro}
1563 %
1564 %
1565 % \subsection{Expandably modify token lists}
1566 %
1567 % \begin{macro}[EXP]{\etl_token_replace_all:nNn}
1568 % \begin{macro}[EXP]{\@@_token_replace:NnnN}
1569 % Replacing a single token (and in fact the same is true for all the
1570 % replacement actions in this package) doesn't need reordering and no post
1571 % processing, so we can use in place output using \cs{@@_unexpanded:w}. We
1572 % store the token we want to replace inside the act function, as well as an
1573 % additional argument which will be executed once a replacement was done
1574 % (this is used for the \cs{etl_token_replace_once:nNn} function).
1575 % \begin{macrocode}
1576 \exp_args:Nno \use:n { \cs_new:Npn \etl_token_replace_all:nNn #1#2#3 }
1577 {
1578 \etl_act:nnnnnn
1579 { \@@_token_replace:NnnN #2 {} }
1580 \@@_act_unexpanded_space:n
1581 \@@_act_unexpanded_group:nn
1582 \use_none:nn
1583 {#3}
1584 {#1}
1585 }
1586 \exp_args:Nno \use:n { \cs_new:Npn \@@_token_replace:NnnN #1#2#3#4 }
1587 {
1588 \etl_token_if_eq:NNTF {#1} {#4}
1589 { \@@_unexpanded:w {#3} #2 }
1590 { \@@_unexpanded:w {#4} }
1591 }
1592 % \end{macrocode}
1593 % \end{macro}
1594 % \end{macro}
1595 %
1596 % \begin{macro}[EXP]{\etl_token_replace_all_deep:nNn}
1597 % \begin{macro}[EXP]{\@@_token_replace_deep:Nnn}
1598 % Deep replacement is done by recursion. Since the deep variant will not
1599 % execute any additional code we omit such an additional argument for it.
1600 % \begin{macrocode}
1601 \exp_args:Nno \use:n { \cs_new:Npn \etl_token_replace_all_deep:nNn #1#2#3 }
1602 {
1603 \etl_act:nnnnnn
1604 { \@@_token_replace:NnnN #2 {} }
1605 \@@_act_unexpanded_space:n
1606 { \@@_token_replace_deep:Nnn #2 }
1607 \use_none:nn
1608 {#3}
1609 {#1}
1610 }
1611 % \end{macrocode}
1612 % Here |{#1}| is used to get correct results from the first step of expansion
1613 % done directly.
1614 % \begin{macrocode}
1615 \exp_args:Nno \use:n { \cs_new:Npn \@@_token_replace_deep:Nnn #1#2#3 }
1616 { \exp_after:wN { \etl_token_replace_all_deep:nNn {#3} {#1} {#2} } }
1617 % \end{macrocode}
1618 % \end{macro}
1619 % \end{macro}
1620 %
1621 % \begin{macro}[EXP]{\etl_token_replace_once:nNn}
1622 % To only handle the first matching token we just let the replacement internal
1623 % exchange the function to directly output any token.
1624 % \begin{macrocode}
1625 \exp_args:Nno \use:n { \cs_new:Npn \etl_token_replace_once:nNn #1#2#3 }
1626 {
1627 \etl_act:nnnnnn
1628 { \@@_token_replace:NnnN #2 \@@_act_unexpanded_rest:w }
1629 \@@_act_unexpanded_space:n
1630 \@@_act_unexpanded_group:nn
1631 \use_none:nn
1632 {#3}
1633 {#1}
1634 }
1635 % \end{macrocode}
1636 % \end{macro}
1637 %
1638 % \begin{macro}[EXP]{\etl_replace_all:nnn}
1639 % \begin{macro}[EXP]
1640 % {
1641 % \@@_replace_put_back:nnnN,
1642 % \@@_replace_put_back_normal:Nn, \@@_replace_put_back_group:nn
1643 % }
1644 % \begin{macro}[EXP]
1645 % {
1646 % \@@_replace_normal:nN, \@@_replace_normal:nnnnNN,
1647 % \@@_replace_normal:NnnnnNN, \@@_replace_normal_false:nnnNN
1648 % }
1649 % \begin{macro}[EXP]
1650 % {
1651 % \@@_replace_space:n, \@@_replace_space:nnnnN, \@@_replace_space_aux:nnnnN,
1652 % \@@_replace_space_false:nnnN
1653 % }
1654 % \begin{macro}[EXP]
1655 % {
1656 % \@@_replace_group:nn, \@@_replace_group:nnnnNn, \@@_replace_group:nnnnnNn,
1657 % \@@_replace_group_false:nnnNn
1658 % }
1659 % \begin{macro}[EXP]{\@@_replace_final:nn, \@@_replace_final:nnnnNn}
1660 % Replacing an arbitrary number of tokens (which might include braces and
1661 % spaces) is quite a bit harder than a single |N|-type. We place in the status
1662 % the remainder of the pattern, the full pattern, delayed tokens (those
1663 % which matched the pattern), the replacement, and a marker which should tell
1664 % us whether we want to only replace the first match (if so use
1665 % \cs{@@_act_unexpanded_rest:w}, else \cs{prg_do_nothing:}).
1666 % \begin{macrocode}
1667 \exp_args:Nno \use:n { \cs_new:Npn \etl_replace_all:nnn #1#2#3 }
1668 {
1669 \etl_act:nnnnnn
1670 \@@_replace_normal:nN
1671 \@@_replace_space:n
1672 \@@_replace_group:nn
1673 \@@_replace_final:nn
1674 { {#2} {#2} {} {#3} \prg_do_nothing: }
1675 {#1}
1676 }
1677 % \end{macrocode}
1678 % We again need to be able to put back a few tokens, but this time we also
1679 % need to know whether the first token is an |N|-type or group, because we
1680 % can't just gobble the first element but need to output it unchanged.
1681 % \begin{macrocode}
1682 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_put_back:nnnN #1#2#3#4 }
1683 {
1684 \@@_if_head_is_space:nTF {#1}
1685 {
1686 \exp_after:wN \etl_act_put_back:n \exp_after:wN { \@@_rm_space:w #1 } ~
1687 }
1688 {
1689 \@@_if_head_is_group:nTF {#1}
1690 { \exp_after:wN \@@_replace_put_back_group:nn }
1691 { \exp_after:wN \@@_replace_put_back_normal:Nn }
1692 \@@_expanded:w { \@@_split_first:w #1 }
1693 }
1694 \etl_act_status:n { {#2} {#2} {} {#3} #4 }
1695 }
1696 \cs_new:Npn \@@_replace_put_back_group:nn #1
1697 {
1698 \@@_unexpanded:w { {#1} }
1699 \etl_act_put_back:n
1700 }
1701 \cs_new:Npn \@@_replace_put_back_normal:Nn #1
1702 {
1703 \@@_unexpanded:w {#1}
1704 \etl_act_put_back:n
1705 }
1706 \cs_new:Npn \@@_replace_normal:nN #1 { \@@_replace_normal:nnnnNN #1 }
1707 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_normal:nnnnNN #1 }
1708 {
1709 \@@_if_head_is_N_type:nTF {#1}
1710 {
1711 \exp_after:wN \@@_replace_normal:NnnnnNN
1712 \@@_expanded:w { \@@_split_first:w #1 }
1713 }
1714 { \@@_replace_normal_false:nnnNN }
1715 }
1716 % \end{macrocode}
1717 % Just to keep track of the different arguments here: |#1| is the next token
1718 % in the pattern, |#2| is the remainder of the pattern, |#3| is the full
1719 % pattern stored for reuse, |#4| are the delayed tokens, which might need to
1720 % be put back, |#5| is the replacement text, |#6| is the marker which might
1721 % indicate the |once| function, and |#7| is the next token of the input.
1722 % \begin{macrocode}
1723 \exp_args:Nno
1724 \use:n { \cs_new:Npn \@@_replace_normal:NnnnnNN #1#2#3#4#5#6#7 }
1725 {
1726 \etl_token_if_eq:NNTF {#1} {#7}
1727 {
1728 \@@_if_empty:nTF {#2}
1729 {
1730 \@@_unexpanded:w {#5}
1731 #6
1732 \etl_act_status:n { {#3} {#3} {} {#5} #6 }
1733 }
1734 { \etl_act_status:n { {#2} {#3} { #4 #7 } {#5} #6 } }
1735 }
1736 { \@@_replace_put_back:nnnN { #4 #7 } {#3} {#5} #6 }
1737 }
1738 \exp_args:Nno
1739 \use:n { \cs_new:Npn \@@_replace_normal_false:nnnNN #1#2#3#4#5 }
1740 { \@@_replace_put_back:nnnN { #2 #5 } {#1} {#3} {#4} }
1741 \cs_new:Npn \@@_replace_space:n #1 { \@@_replace_space:nnnnN #1 }
1742 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_space:nnnnN #1 }
1743 {
1744 \@@_if_head_is_space:nTF {#1}
1745 {
1746 \exp_after:wN \@@_replace_space_aux:nnnnN \exp_after:wN
1747 { \@@_rm_space:w #1 }
1748 }
1749 { \@@_replace_space_false:nnnN }
1750 }
1751 % \end{macrocode}
1752 % Again, to keep track, |#1| is the remainder of the pattern, |#2| is the full
1753 % pattern, |#3| the delayed tokens, |#4| the replacement text, |#5| the marker
1754 % for the |once| function.
1755 % \begin{macrocode}
1756 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_space_aux:nnnnN #1#2#3#4#5 }
1757 {
1758 \@@_if_empty:nTF {#1}
1759 {
1760 \@@_unexpanded:w {#4}
1761 #5
1762 \etl_act_status:n { {#2} {#2} {} {#4} #5 }
1763 }
1764 { \etl_act_status:n { {#1} {#2} { #3 ~ } {#4} #5 } }
1765 }
1766 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_space_false:nnnN #1#2#3#4 }
1767 { \@@_replace_put_back:nnnN { #2 ~ } {#1} {#3} {#4} }
1768 \cs_new:Npn \@@_replace_group:nn #1 { \@@_replace_group:nnnnNn #1 }
1769 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_group:nnnnNn #1 }
1770 {
1771 \@@_if_head_is_group:nTF {#1}
1772 {
1773 \exp_after:wN \@@_replace_group:nnnnnNn
1774 \@@_expanded:w { \@@_split_first:w #1 }
1775 }
1776 { \@@_replace_group_false:nnnNn }
1777 }
1778 % \end{macrocode}
1779 % And again, |#1| the next group of the pattern, |#2| the remainder of the
1780 % pattern, |#3| the full pattern, |#4| the delayed stuff, |#5| the replacement
1781 % text, |#6| the marker for the |once| function, |#7| the next group in the
1782 % input.
1783 % \begin{macrocode}
1784 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_group:nnnnnNn #1#2#3#4#5#6#7 }
1785 {
1786 \etl_if_eq:nnTF {#1} {#7}
1787 {
1788 \@@_if_empty:nTF {#2}
1789 {
1790 \@@_unexpanded:w {#5}
1791 #6
1792 \etl_act_status:n { {#3} {#3} {} {#5} #6 }
1793 }
1794 { \etl_act_status:n { {#2} {#3} { #4 {#7} } {#5} #6 } }
1795 }
1796 { \@@_replace_put_back:nnnN { #4 {#7} } {#3} {#5} #6 }
1797 }
1798 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_group_false:nnnNn #1#2#3#4#5 }
1799 { \@@_replace_put_back:nnnN { #2 {#5} } {#1} {#3} {#4} }
1800 \cs_new:Npn \@@_replace_final:nn #1 { \@@_replace_final:nnnnNn #1 }
1801 \cs_new:Npn \@@_replace_final:nnnnNn #1#2#3#4#5#6 { \@@_unexpanded:w { #6#3 } }
1802 % \end{macrocode}
1803 % \end{macro}
1804 % \end{macro}
1805 % \end{macro}
1806 % \end{macro}
1807 % \end{macro}
1808 % \end{macro}
1809 %
1810 % \begin{macro}[EXP]{\etl_replace_all_deep:nnn}
1811 % \begin{macro}[EXP]
1812 % {
1813 % \@@_replace_group_deep:nn, \@@_replace_group_deep:nnnnNn,
1814 % \@@_replace_group_deep:nnnnnNn, \@@_replace_group_deep_false:nnnNn
1815 % }
1816 % The |deep| variant works again pretty much the same as the |all| variant,
1817 % except that it searches groups recursively.
1818 % \begin{macrocode}
1819 \exp_args:Nno \use:n { \cs_new:Npn \etl_replace_all_deep:nnn #1#2#3 }
1820 {
1821 \etl_act:nnnnnn
1822 \@@_replace_normal:nN
1823 \@@_replace_space:n
1824 \@@_replace_group_deep:nn
1825 \@@_replace_final:nn
1826 { {#2} {#2} {} {#3} \prg_do_nothing: }
1827 {#1}
1828 }
1829 \cs_new:Npn \@@_replace_group_deep:nn #1
1830 { \@@_replace_group_deep:nnnnNn #1 }
1831 \exp_args:Nno \use:n { \cs_new:Npn \@@_replace_group_deep:nnnnNn #1 }
1832 {
1833 \@@_if_head_is_group:nTF {#1}
1834 {
1835 \exp_after:wN \@@_replace_group_deep:nnnnnNn
1836 \@@_expanded:w { \@@_split_first:w #1 }
1837 }
1838 { \@@_replace_group_deep_false:nnnNn }
1839 }
1840 \exp_args:Nno
1841 \use:n { \cs_new:Npn \@@_replace_group_deep:nnnnnNn #1#2#3#4#5#6#7 }
1842 {
1843 \etl_if_eq:nnTF {#1} {#7}
1844 {
1845 \@@_if_empty:nTF {#2}
1846 {
1847 \@@_unexpanded:w {#5}
1848 \etl_act_status:n { {#3} {#3} {} {#5} #6 }
1849 }
1850 { \etl_act_status:n { {#2} {#3} { #4 {#7} } {#5} #6 } }
1851 }
1852 {
1853 \@@_if_empty:nTF {#4}
1854 {
1855 { \etl_replace_all_deep:nnn {#7} {#3} {#5} }
1856 \etl_act_status:n { {#3} {#3} {} {#5} #6 }
1857 }
1858 { \@@_replace_put_back:nnnN { #4 {#7} } {#3} {#5} #6 }
1859 }
1860 }
1861 \exp_args:Nno
1862 \use:n { \cs_new:Npn \@@_replace_group_deep_false:nnnNn #1#2#3#4#5 }
1863 {
1864 \@@_if_empty:nTF {#2}
1865 {
1866 { \etl_replace_all_deep:nnn {#5} {#1} {#3} }
1867 \etl_act_status:n { {#1} {#1} {} {#3} #4 }
1868 }
1869 { \@@_replace_put_back:nnnN { #2 {#5} } {#1} {#3} #4 }
1870 }
1871 % \end{macrocode}
1872 % \end{macro}
1873 % \end{macro}
1874 %
1875 % \begin{macro}[EXP]{\etl_replace_once:nnn}
1876 % And this is the same as the |all| variant except that we now use the
1877 % \cs{@@_act_unexpanded_rest:w} marker inside the status.
1878 % \begin{macrocode}
1879 \exp_args:Nno \use:n { \cs_new:Npn \etl_replace_once:nnn #1#2#3 }
1880 {
1881 \etl_act:nnnnnn
1882 \@@_replace_normal:nN
1883 \@@_replace_space:n
1884 \@@_replace_group:nn
1885 \@@_replace_final:nn
1886 { {#2} {#2} {} {#3} \@@_act_unexpanded_rest:w }
1887 {#1}
1888 }
1889 % \end{macrocode}
1890 % \end{macro}
1891 %
1892 %
1893 % \subsection{Defining new tests}
1894 %
1895 % \begin{macro}{\etl_new_if_in:Nnn}
1896 % \begin{macro}{\@@_new_if_in:NnNnn, \@@_new_if_in:NNnn}
1897 % These tests work essentially in the same way as \cs{tl_if_in:nnTF}, but
1898 % instead they use a predefined internal macro so that no definition at use
1899 % time is necessary. We use a small loop to get a unique auxiliary macro name
1900 % for the search text.
1901 % \begin{macrocode}
1902 \exp_args:Nno \use:n { \cs_new_protected:Npn \etl_new_if_in:Nnn #1#2#3 }
1903 {
1904 \scan_stop:
1905 \if_false: { \fi:
1906 \exp_args:Nc \@@_new_if_in:NnNnn
1907 { @@_user_function ~ if_in ~ \tl_to_str:n {#2} :w }
1908 ?
1909 #1 {#2}
1910 {#3}
1911 \if_false: } \fi:
1912 }
1913 \cs_new_protected:Npn \@@_new_if_in:NnNnn #1#2#3#4
1914 {
1915 \cs_if_exist:NTF #1
1916 {
1917 \cs_set:Npn \@@_tmp:w ##1 #4 {}
1918 \cs_if_eq:NNTF #1 \@@_tmp:w
1919 { \@@_new_if_in:NNnn #1 #3 {#4} }
1920 {
1921 \exp_args:Nc \@@_new_if_in:NnNnn
1922 { @@_user_function ~ if_in #2 ~ \tl_to_str:n {#4} :w }
1923 { #2? }
1924 #3 {#4}
1925 }
1926 }
1927 { \@@_new_if_in:NNnn #1 #3 {#4} }
1928 }
1929 \cs_new_protected:Npn \@@_new_if_in:NNnn #1#2#3#4
1930 {
1931 \cs_gset:Npn #1 ##1 #3 {}
1932 \prg_new_conditional:Npnn #2 ##1 {#4}
1933 {
1934 \if:w
1935 \scan_stop:
1936 \@@_detokenize:w \exp_after:wN { #1 ##1 {}{} #3 }
1937 \scan_stop:
1938 \@@_fi_turn_false:w
1939 \fi:
1940 \if_true:
1941 \prg_return_true:
1942 \else:
1943 \prg_return_false:
1944 \fi:
1945 }
1946 }
1947 % \end{macrocode}
1948 % \end{macro}
1949 % \end{macro}
1950 %
1951 %
1952 % \subsection{Defining new modifiers}
1953 %
1954 % The implementation of |replace_once| and |replace_all| is modelled closely on
1955 % the implementation used in \pkg{l3tl}. The difference is that we use a hard
1956 % coded delimiter (\cs{s_@@_stop}) instead of searching for one that is always
1957 % legal (we can't do redefinitions, so can't change the delimiter later based on
1958 % the token list input).
1959 %
1960 %
1961 % \begin{macro}
1962 % {
1963 % \@@_new_replace_def:NNn,
1964 % \@@_new_replace_def_aux:NnNnN, \@@_new_replace_def_aux:Nn
1965 % }
1966 % We need another loop to guarantee unique names, if everything's alright we
1967 % go on and define the user function using |#1| of
1968 % \cs{@@_new_replace_def:NNn}. An empty search pattern is forbidden and should
1969 % throw an error.
1970 % \begin{macrocode}
1971 \msg_new:nnn { etl } { empty-search-text }
1972 { The~ search~ text~ of~ #1 must~ not~ be~ empty. }
1973 \cs_new_protected:Npn \@@_new_replace_def:NNn #1#2#3
1974 {
1975 \tl_if_empty:nTF {#3}
1976 { \msg_error:nnn { etl } { empty-search-text } { #2 } }
1977 {
1978 \scan_stop:
1979 \if_false: { \fi:
1980 \exp_args:Nc \@@_new_replace_def_aux:NnNnN
1981 { @@_user_function ~ replace ~ \tl_to_str:n {#3} ~ :Nnw }
1982 ?
1983 #2 {#3}
1984 #1
1985 \if_false: } \fi:
1986 }
1987 }
1988 \cs_new_protected:Npn \@@_new_replace_def_aux:NnNnN #1#2#3#4#5
1989 {
1990 \cs_if_exist:NTF #1
1991 {
1992 \@@_new_replace_def_aux:Nn \@@_tmp:w {#4}
1993 \cs_if_eq:NNTF #1 \@@_tmp:w
1994 { #5 #1#3 {#4} }
1995 {
1996 \exp_args:Nc \@@_new_replace_def_aux:NnNnN
1997 { @@_user_function ~ replace #2 ~ \tl_to_str:n {#4} ~ :Nnw }
1998 { #2? } #3 {#4} #5
1999 }
2000 }
2001 {
2002 \@@_new_replace_def_aux:Nn #1 {#4}
2003 #5 #1#3 {#4}
2004 }
2005 }
2006 % \end{macrocode}
2007 % The auxiliary macro uses a loop for the replacement. This is also used for
2008 % the |once| variant. This saves internal functions if both an |all| and a
2009 % |once| function are generated for the same search text (though the |once|
2010 % variant could be coded easier and faster otherwise, but the performance hit
2011 % should be small).
2012 % \begin{macrocode}
2013 \cs_new_protected:Npn \@@_new_replace_def_aux:Nn #1#2
2014 {
2015 \cs_gset:Npn #1 ##1##2 ##3#2
2016 {
2017 \@@_new_replace_wrap:w ##3 \s_@@_stop \@@_unexpanded:w {##2}
2018 ##1 #1 {##2} {}{}
2019 }
2020 }
2021 % \end{macrocode}
2022 % \end{macro}
2023 %
2024 % \begin{macro}{\@@_new_replace_wrap:w}
2025 % \begin{macro}{\@@_new_replace_once:w}
2026 % \begin{macro}{\@@_new_replace_done:w}
2027 % We need a few auxiliaries for the two replacement variants here. The first
2028 % just grabs the already processed part of the token list and protects it from
2029 % further expanding. The second breaks the loop for the |once| variant by
2030 % protecting the remainder of the token list from further expanding. The last
2031 % just gobbles the remainder of the loop by using an unbalanced brace trick.
2032 % \begin{macrocode}
2033 \cs_new:Npn \@@_new_replace_wrap:w #1\s_@@_stop
2034 { \@@_unexpanded:w \exp_after:wN { \use_none:nn #1 } }
2035 \cs_new:Npn \@@_new_replace_once:w #1#2 #3\s_@@_stop
2036 { \@@_unexpanded:w \exp_after:wN { \use_none:nn #3 } }
2037 \cs_new:Npn \@@_new_replace_done:w
2038 { \exp_after:wN \use_none:n \exp_after:wN { \if_false: } \fi: }
2039 % \end{macrocode}
2040 % \end{macro}
2041 % \end{macro}
2042 % \end{macro}
2043 %
2044 % \begin{macro}{\etl_new_replace_once:Nn}
2045 % \begin{macro}{\@@_new_replace_once:NNn}
2046 % The |once| variant will use \cs{@@_new_replace_done_once:w} if the
2047 % replacement is successful (that will remove the remainder of the loop, and
2048 % protect both the replacement and the rest of the token list on which we work
2049 % from further expanding).
2050 % \begin{macrocode}
2051 \cs_new_protected:Npn \etl_new_replace_once:Nn
2052 { \@@_new_replace_def:NNn \@@_new_replace_once:NNn }
2053 \cs_new_protected:Npn \@@_new_replace_once:NNn #1#2#3
2054 {
2055 \cs_new:Npn #2 ##1##2
2056 {
2057 \@@_unexpanded:w \@@_expanded:w
2058 {{
2059 \if_false: { \fi:
2060 #1 \@@_new_replace_once:w {##2} {}{} ##1 \s_@@_stop
2061 \@@_new_replace_done:w #3
2062 }
2063 }}
2064 }
2065 }
2066 % \end{macrocode}
2067 % \end{macro}
2068 % \end{macro}
2069 %
2070 % \begin{macro}{\etl_new_replace_all:Nn}
2071 % \begin{macro}{\@@_new_replace_all:NNn}
2072 % The |all| variant will directly protect the replacement from further
2073 % expanding and reiterate (due to the way the auxiliary is defined) until the
2074 % replacement isn't found anymore.
2075 % \begin{macrocode}
2076 \cs_new_protected:Npn \etl_new_replace_all:Nn
2077 { \@@_new_replace_def:NNn \@@_new_replace_all:NNn }
2078 \cs_new_protected:Npn \@@_new_replace_all:NNn #1#2#3
2079 {
2080 \cs_new:Npn #2 ##1##2
2081 {
2082 \@@_unexpanded:w \@@_expanded:w
2083 {{
2084 \if_false: { \fi:
2085 #1 #1 {##2} {}{} ##1 \s_@@_stop
2086 \@@_new_replace_done:w #3
2087 }
2088 }}
2089 }
2090 }
2091 % \end{macrocode}
2092 % \end{macro}
2093 % \end{macro}
2094 %
2095 % \subsection{Undefine now unnecessary functions}
2096 %
2097 % \begin{macrocode}
2098 \cs_undefine:N \@@_act:nnnnnnn
2099 % \end{macrocode}
2100 %
2101 % \begin{macrocode}
2102 %</pkg>
2103 % \end{macrocode}
2104 %
2105 % \end{implementation}^^A=<<
2106 %
2107 % \clearpage
2108 % \PrintIndex
2109 %
2110 % \Finale
2111 \endinput
2112 %
2113 ^^A vim: ft=tex fdm=marker fmr=>>=,=<<