| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3expan.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 1990-2026 The LaTeX Project |
| 6 |
% |
| 7 |
% It may be distributed and/or modified under the conditions of the |
| 8 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 9 |
% license or (at your option) any later version. The latest version |
| 10 |
% of this license is in the file |
| 11 |
% |
| 12 |
% https://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
% This file is part of the "l3kernel bundle" (The Work in LPPL) |
| 15 |
% and all files in that bundle must be distributed together. |
| 16 |
% |
| 17 |
% ----------------------------------------------------------------------- |
| 18 |
% |
| 19 |
% The development version of the bundle can be found at |
| 20 |
% |
| 21 |
% https://github.com/latex3/latex3 |
| 22 |
% |
| 23 |
% for those people who are interested. |
| 24 |
% |
| 25 |
%<*driver> |
| 26 |
\documentclass[full,kernel]{l3doc} |
| 27 |
\begin{document} |
| 28 |
\DocInput{\jobname.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% \fi |
| 32 |
% |
| 33 |
% \title{^^A |
| 34 |
% The \pkg{l3expan} module\\ Argument expansion^^A |
| 35 |
% } |
| 36 |
% |
| 37 |
% \author{^^A |
| 38 |
% The \LaTeX{} Project\thanks |
| 39 |
% {^^A |
| 40 |
% E-mail: |
| 41 |
% \href{mailto:[email protected]} |
| 42 |
% {[email protected]}^^A |
| 43 |
% }^^A |
| 44 |
% } |
| 45 |
% |
| 46 |
% \date{Released 2026-05-15} |
| 47 |
% |
| 48 |
% \maketitle |
| 49 |
% |
| 50 |
% \begin{documentation} |
| 51 |
% |
| 52 |
% This module provides generic methods for expanding \TeX{} arguments in a |
| 53 |
% systematic manner. The functions in this module all have prefix |exp|. |
| 54 |
% |
| 55 |
% Not all possible variations are implemented for every base |
| 56 |
% function. Instead only those that are used within the \LaTeX3 kernel |
| 57 |
% or otherwise seem to be of general interest are implemented. |
| 58 |
% Consult the module description to find out which functions are |
| 59 |
% actually defined. The next section explains how to define missing |
| 60 |
% variants. |
| 61 |
% |
| 62 |
% \section{Defining new variants} |
| 63 |
% \label{sec:l3expan:defining-variants} |
| 64 |
% |
| 65 |
% The definition of variant forms for base functions may be necessary |
| 66 |
% when writing new functions or when applying a kernel function in a |
| 67 |
% situation that we haven't thought of before. |
| 68 |
% |
| 69 |
% Internally preprocessing of arguments is done with functions of the form |
| 70 |
% \cs[no-index]{exp_\ldots{}}. They all look alike, an example would be |
| 71 |
% \cs{exp_args:NNo}. This function has three arguments, the first and the |
| 72 |
% second are a single tokens, while the third argument should be given |
| 73 |
% in braces. Applying \cs{exp_args:NNo} expands the content of third |
| 74 |
% argument once before any expansion of the first and second arguments. |
| 75 |
% If \cs{seq_gpush:No} was not defined it could be coded in the following way: |
| 76 |
% \begin{verbatim} |
| 77 |
% \exp_args:NNo \seq_gpush:Nn |
| 78 |
% \g_file_name_stack |
| 79 |
% { \l_tmpa_tl } |
| 80 |
% \end{verbatim} |
| 81 |
% In other words, the first argument to \cs{exp_args:NNo} is the base |
| 82 |
% function and the other arguments are preprocessed and then passed to |
| 83 |
% this base function. In the example the first argument to the base |
| 84 |
% function should be a single token which is left unchanged while the |
| 85 |
% second argument is expanded once. From this example we can also see |
| 86 |
% how the variants are defined. They just expand into the appropriate |
| 87 |
% |\exp_| function followed by the desired base function, \emph{e.g.} |
| 88 |
% \begin{quote} |
| 89 |
% |\cs_generate_variant:Nn \seq_gpush:Nn { No } | |
| 90 |
% \end{quote} |
| 91 |
% results in the definition of |\seq_gpush:No| |
| 92 |
% \begin{quote} |
| 93 |
% |\cs_new:Npn \seq_gpush:No { \exp_args:NNo \seq_gpush:Nn }| |
| 94 |
% \end{quote} |
| 95 |
% Providing variants in this way in style files is safe as the |
| 96 |
% \cs{cs_generate_variant:Nn} function will only create new definitions if |
| 97 |
% there is not already one available. Therefore adding |
| 98 |
% such definition to later releases of the kernel will not make such |
| 99 |
% style files obsolete. |
| 100 |
% |
| 101 |
% The steps above may be automated by using the function |
| 102 |
% \cs{cs_generate_variant:Nn}, described next. |
| 103 |
% |
| 104 |
% \section{Methods for defining variants} |
| 105 |
% \label{sec:l3expan:variants-method} |
| 106 |
% |
| 107 |
% We recall the set of available argument specifiers. |
| 108 |
% \begin{itemize} |
| 109 |
% \item |N|~is used for single-token arguments while |c|~constructs a |
| 110 |
% control sequence from its name and passes it to a parent function as |
| 111 |
% an |N|-type argument. |
| 112 |
% \item Many argument types extract or expand some tokens and provide it |
| 113 |
% as an |n|-type argument, namely a braced multiple-token argument: |
| 114 |
% |V|~extracts the value of a variable, |v|~extracts the value from |
| 115 |
% the name of a variable, |n|~uses the argument as it is, |o|~expands |
| 116 |
% once, |f|~expands fully the front of the token list, |e| and |
| 117 |
% |x|~expand fully all tokens (differences are explained later). |
| 118 |
% \item A few odd argument types remain: |T|~and |F|~for conditional |
| 119 |
% processing, otherwise identical to |n|-type arguments, |
| 120 |
% |p|~for the parameter text |
| 121 |
% in definitions, |w|~for arguments with a specific syntax, and |D|~to |
| 122 |
% denote primitives that should not be used directly. |
| 123 |
% \end{itemize} |
| 124 |
% |
| 125 |
% \begin{function}[updated = 2017-11-28] |
| 126 |
% {\cs_generate_variant:Nn, \cs_generate_variant:cn} |
| 127 |
% \begin{syntax} |
| 128 |
% \cs{cs_generate_variant:Nn} \meta{parent control sequence} \Arg{variant argument specifiers} |
| 129 |
% \end{syntax} |
| 130 |
% This function is used to define argument-specifier variants of the |
| 131 |
% \meta{parent control sequence} for \LaTeX3 code-level macros. The |
| 132 |
% \meta{parent control sequence} is first separated into the |
| 133 |
% \meta{base name} and \meta{original argument specifier}. The |
| 134 |
% comma-separated list of \meta{variant argument specifiers} is |
| 135 |
% then used to define variants of the |
| 136 |
% \meta{original argument specifier} if these are not already |
| 137 |
% defined; entries which correspond to existing functions are silently |
| 138 |
% ignored. For each \meta{variant} given, a function is created |
| 139 |
% that expands its arguments as detailed and passes them |
| 140 |
% to the \meta{parent control sequence}. So for example |
| 141 |
% \begin{verbatim} |
| 142 |
% \cs_set:Npn \foo:Nn #1#2 { code here } |
| 143 |
% \cs_generate_variant:Nn \foo:Nn { c } |
| 144 |
% \end{verbatim} |
| 145 |
% creates a new function |\foo:cn| which expands its first |
| 146 |
% argument into a control sequence name and passes the result to |
| 147 |
% |\foo:Nn|. Similarly |
| 148 |
% \begin{verbatim} |
| 149 |
% \cs_generate_variant:Nn \foo:Nn { NV , cV } |
| 150 |
% \end{verbatim} |
| 151 |
% generates the functions |\foo:NV| and |\foo:cV| in the same |
| 152 |
% way. The \cs{cs_generate_variant:Nn} function should only be applied if |
| 153 |
% the \meta{parent control sequence} is already defined. (This is only |
| 154 |
% enforced if debugging support \texttt{check-declarations} is enabled.) |
| 155 |
% If the \meta{parent |
| 156 |
% control sequence} is protected or if the \meta{variant} involves any |
| 157 |
% |x|~argument, then the \meta{variant control sequence} is also |
| 158 |
% protected. The \meta{variant} is created globally, as is any |
| 159 |
% \cs[no-index]{exp_args:N\meta{variant}} function needed to carry out the |
| 160 |
% expansion. There is no need to re-apply \cs{cs_generate_variant:Nn} after |
| 161 |
% changing the definition of the parent function: the variant will always |
| 162 |
% use the current definition of the parent. Providing variants repeatedly is |
| 163 |
% safe as \cs{cs_generate_variant:Nn} will only create new definitions if |
| 164 |
% there is not already one available. |
| 165 |
% |
| 166 |
% Only |n|~and |N| arguments can be changed to other types. The only |
| 167 |
% allowed changes are |
| 168 |
% \begin{itemize} |
| 169 |
% \item |c|~variant of an |N|~parent; |
| 170 |
% \item |o|, |V|, |v|, |f|, |e|, or~|x| variant of an |n|~parent; |
| 171 |
% \item |N|, |n|, |T|, |F|, or |p| argument unchanged. |
| 172 |
% \end{itemize} |
| 173 |
% This means the \meta{parent} of a \meta{variant} form is always |
| 174 |
% unambiguous, even in cases where both an |n|-type parent and an |
| 175 |
% |N|-type parent exist, such as for \cs{tl_count:n} and |
| 176 |
% \cs{tl_count:N}. |
| 177 |
% |
| 178 |
% When creating variants for conditional functions, |
| 179 |
% \cs{prg_generate_conditional_variant:Nnn} provides a convenient way |
| 180 |
% of handling the related function set. |
| 181 |
% |
| 182 |
% For backward compatibility it is currently possible to make |n|, |
| 183 |
% |o|, |V|, |v|, |f|, |e|, or |x|-type variants of an |N|-type argument or |
| 184 |
% |N| or |c|-type variants of an |n|-type argument. Both are |
| 185 |
% deprecated. The first because passing more than one token to an |
| 186 |
% |N|-type argument will typically break the parent function's code. |
| 187 |
% The second because programmers who use that most often want to |
| 188 |
% access the value of a variable given its name, hence should use a |
| 189 |
% |V|-type or |v|-type variant instead of |c|-type. In those cases, |
| 190 |
% using the lower-level \cs{exp_args:No} or \cs{exp_args:Nc} |
| 191 |
% functions explicitly is preferred to defining confusing variants. |
| 192 |
% \end{function} |
| 193 |
% |
| 194 |
% \begin{function}[added = 2018-04-04, updated = 2019-02-08] |
| 195 |
% {\exp_args_generate:n} |
| 196 |
% \begin{syntax} |
| 197 |
% \cs{exp_args_generate:n} \Arg{variant argument specifiers} |
| 198 |
% \end{syntax} |
| 199 |
% Defines \cs[no-index]{exp_args:N\meta{variant}} functions for each |
| 200 |
% \meta{variant} given in the comma list \Arg{variant argument |
| 201 |
% specifiers}. Each \meta{variant} should consist of the letters |N|, |
| 202 |
% |c|, |n|, |V|, |v|, |o|, |f|, |e|, |x|, |p| and the resulting function is |
| 203 |
% protected if the letter |x| appears in the \meta{variant}. This is |
| 204 |
% only useful for cases where \cs{cs_generate_variant:Nn} is not |
| 205 |
% applicable. |
| 206 |
% \end{function} |
| 207 |
% |
| 208 |
% \section{Introducing the variants} |
| 209 |
% |
| 210 |
% The |V| type returns the value of a register, which can be one of |
| 211 |
% |tl|, |clist|, |int|, |skip|, |dim|, |muskip|, or built-in \TeX{} |
| 212 |
% registers. The |v| type is the same except it first creates a |
| 213 |
% control sequence out of its argument before returning the |
| 214 |
% value. |
| 215 |
% |
| 216 |
% In general, the programmer should not need to be concerned with |
| 217 |
% expansion control. When simply using the content of a variable, |
| 218 |
% functions with a |V| specifier should be used. For those referred to by |
| 219 |
% (cs)name, the |v| specifier is available for the same purpose. Only when |
| 220 |
% specific expansion steps are needed, such as when using delimited |
| 221 |
% arguments, should the lower-level functions with |o| specifiers be employed. |
| 222 |
% |
| 223 |
% The |e| type expands all tokens fully, starting from the first. More |
| 224 |
% precisely the expansion is identical to that of \TeX{}'s \tn{message} |
| 225 |
% (in particular |#| needs not be doubled). It relies on the |
| 226 |
% primitive \tn{expanded} hence is fast. |
| 227 |
% |
| 228 |
% The |x| type expands all tokens fully, starting from the first. In |
| 229 |
% contrast to |e|, all macro parameter characters |#| must be doubled, |
| 230 |
% and omitting this leads to low-level errors. In addition this type of |
| 231 |
% expansion is not expandable, namely functions that have |x| in their |
| 232 |
% signature do not themselves expand when appearing inside |e| or |x| |
| 233 |
% expansion. |
| 234 |
% |
| 235 |
% The |f| type is so special that it deserves an example. It is |
| 236 |
% typically used in contexts where only expandable commands are allowed. |
| 237 |
% Then |x|-expansion cannot be used, and |f|-expansion provides an |
| 238 |
% alternative that expands the front of the token list |
| 239 |
% as much as can be done in such contexts. For |
| 240 |
% instance, say that we want to evaluate the integer expression $3 + 4$ |
| 241 |
% and pass the result $7$ as an argument to an expandable function |
| 242 |
% |\example:n|. For this, one should define a variant using |
| 243 |
% \cs{cs_generate_variant:Nn} |\example:n| |{| |f| |}|, then do |
| 244 |
% \begin{quote} |
| 245 |
% |\example:f { \int_eval:n { 3 + 4 } }| |
| 246 |
% \end{quote} |
| 247 |
% Note that |x|-expansion would also expand \cs{int_eval:n} fully to its |
| 248 |
% result~$7$, but the variant |\example:x| cannot be expandable. Note |
| 249 |
% also that |o|-expansion would not expand \cs{int_eval:n} fully to its |
| 250 |
% result since that function requires several expansions. Besides the |
| 251 |
% fact that |x|-expansion is protected rather than expandable, another |
| 252 |
% difference between |f|-expansion and |x|-expansion is that |
| 253 |
% |f|-expansion expands tokens from the beginning and stops as soon as a |
| 254 |
% non-expandable token is encountered, while |x|-expansion continues |
| 255 |
% expanding further tokens. Thus, for instance |
| 256 |
% \begin{quote} |
| 257 |
% |\example:f { \int_eval:n { 1 + 2 } , \int_eval:n { 3 + 4 } }| |
| 258 |
% \end{quote} |
| 259 |
% results in the call |
| 260 |
% \begin{quote} |
| 261 |
% |\example:n { 3 , \int_eval:n { 3 + 4 } }| |
| 262 |
% \end{quote} |
| 263 |
% while using |\example:x| or |\example:e| instead results in |
| 264 |
% \begin{quote} |
| 265 |
% |\example:n { 3 , 7 }| |
| 266 |
% \end{quote} |
| 267 |
% at the cost of being protected for |x|-type. |
| 268 |
% If you use |f| type expansion in conditional processing then |
| 269 |
% you should stick to using |TF| type functions only as the expansion |
| 270 |
% does not finish any |\if... \fi:| itself! |
| 271 |
% |
| 272 |
% It is important to note that both \texttt{f}- and \texttt{o}-type |
| 273 |
% expansion are concerned with the expansion of tokens from left to |
| 274 |
% right in their arguments. In particular, \texttt{o}-type expansion |
| 275 |
% applies to the first \emph{token} in the argument it receives: it |
| 276 |
% is conceptually similar to |
| 277 |
% \begin{verbatim} |
| 278 |
% \exp_after:wN <base function> \exp_after:wN { <argument> } |
| 279 |
% \end{verbatim} |
| 280 |
% At the same time, \texttt{f}-type expansion stops at the \emph{first} |
| 281 |
% non-expandable token. This means for example that both |
| 282 |
% \begin{verbatim} |
| 283 |
% \tl_set:No \l_tmpa_tl { { \g_tmpb_tl } } |
| 284 |
% \end{verbatim} |
| 285 |
% and |
| 286 |
% \begin{verbatim} |
| 287 |
% \tl_set:Nf \l_tmpa_tl { { \g_tmpb_tl } } |
| 288 |
% \end{verbatim} |
| 289 |
% leave |\g_tmpb_tl| unchanged: |{| is the first token in the |
| 290 |
% argument and is non-expandable. |
| 291 |
% |
| 292 |
% It is usually best to keep the following in mind when using variant |
| 293 |
% forms. |
| 294 |
% \begin{itemize} |
| 295 |
% \item |
| 296 |
% Variants with |x|-type arguments (that are fully expanded before |
| 297 |
% being passed to the |n|-type base function) are never expandable |
| 298 |
% even when the base function is. Such variants cannot work |
| 299 |
% correctly in arguments that are themselves subject to expansion. |
| 300 |
% Consider using |f| or |e| expansion. |
| 301 |
% \item |
| 302 |
% In contrast, |e|~expansion (full expansion, almost like~|x| except |
| 303 |
% for the treatment of~|#|) does not prevent variants from being |
| 304 |
% expandable (if the base function is). |
| 305 |
% \item |
| 306 |
% Finally |f|~expansion only expands the front of the token list, |
| 307 |
% stopping at the first non-expandable token. This may fail to |
| 308 |
% fully expand the argument. |
| 309 |
% \end{itemize} |
| 310 |
% |
| 311 |
% When speed is essential (for functions that do very little work and |
| 312 |
% whose variants are used numerous times in a document) the following |
| 313 |
% considerations apply because the speed of internal functions that |
| 314 |
% expand the arguments of a base function depend on what needs doing |
| 315 |
% with each argument and where this happens in the list of arguments: |
| 316 |
% \begin{itemize} |
| 317 |
% \item for fastest processing any |c|-type arguments should come first |
| 318 |
% followed by all other modified arguments; |
| 319 |
% \item unchanged |N|-type args that appear before modified ones have |
| 320 |
% a small performance hit; |
| 321 |
% \item unchanged |n|-type args that appear before modified ones have |
| 322 |
% a relative larger performance hit. |
| 323 |
% \end{itemize} |
| 324 |
% |
| 325 |
% \section{Manipulating the first argument} |
| 326 |
% |
| 327 |
% These functions are described in detail: expansion of multiple tokens follows |
| 328 |
% the same rules but is described in a shorter fashion. |
| 329 |
% |
| 330 |
% \begin{function}[EXP]{\exp_args:Nc, \exp_args:cc} |
| 331 |
% \begin{syntax} |
| 332 |
% \cs{exp_args:Nc} \meta{function} \Arg{tokens} |
| 333 |
% \end{syntax} |
| 334 |
% This function absorbs two arguments (the \meta{function} name and |
| 335 |
% the \meta{tokens}). The \meta{tokens} are expanded until only characters |
| 336 |
% remain, and are then turned into a control sequence. |
| 337 |
% The result is inserted into the input stream \emph{after} reinsertion |
| 338 |
% of the \meta{function}. Thus the \meta{function} may take more than |
| 339 |
% one argument: all others are left unchanged. |
| 340 |
% |
| 341 |
% The |:cc| variant constructs the \meta{function} name in the same |
| 342 |
% manner as described for the \meta{tokens}. |
| 343 |
% \end{function} |
| 344 |
% |
| 345 |
% \begin{function}[EXP]{\exp_args:No} |
| 346 |
% \begin{syntax} |
| 347 |
% \cs{exp_args:No} \meta{function} \Arg{tokens} ... |
| 348 |
% \end{syntax} |
| 349 |
% This function absorbs two arguments (the \meta{function} name and |
| 350 |
% the \meta{tokens}). The \meta{tokens} are expanded once, and the result |
| 351 |
% is inserted in braces into the input stream \emph{after} reinsertion |
| 352 |
% of the \meta{function}. Thus the \meta{function} may take more than |
| 353 |
% one argument: all others are left unchanged. |
| 354 |
% \end{function} |
| 355 |
% |
| 356 |
% \begin{function}[EXP]{\exp_args:NV} |
| 357 |
% \begin{syntax} |
| 358 |
% \cs{exp_args:NV} \meta{function} \meta{variable} |
| 359 |
% \end{syntax} |
| 360 |
% This function absorbs two arguments (the names of the \meta{function} and |
| 361 |
% the \meta{variable}). The content of the \meta{variable} are recovered |
| 362 |
% and placed inside braces into the input stream \emph{after} reinsertion |
| 363 |
% of the \meta{function}. Thus the \meta{function} may take more than |
| 364 |
% one argument: all others are left unchanged. |
| 365 |
% \end{function} |
| 366 |
% |
| 367 |
% \begin{function}[EXP]{\exp_args:Nv} |
| 368 |
% \begin{syntax} |
| 369 |
% \cs{exp_args:Nv} \meta{function} \Arg{tokens} |
| 370 |
% \end{syntax} |
| 371 |
% This function absorbs two arguments (the \meta{function} name and |
| 372 |
% the \meta{tokens}). The \meta{tokens} are expanded until only characters |
| 373 |
% remain, and are then turned into a control sequence. |
| 374 |
% This control sequence should |
| 375 |
% be the name of a \meta{variable}. The content of the \meta{variable} are |
| 376 |
% recovered and placed inside braces into the input stream \emph{after} |
| 377 |
% reinsertion of the \meta{function}. Thus the \meta{function} may take more |
| 378 |
% than one argument: all others are left unchanged. |
| 379 |
% \end{function} |
| 380 |
% |
| 381 |
% \begin{function}[EXP, added = 2018-05-15]{\exp_args:Ne} |
| 382 |
% \begin{syntax} |
| 383 |
% \cs{exp_args:Ne} \meta{function} \Arg{tokens} |
| 384 |
% \end{syntax} |
| 385 |
% This function absorbs two arguments (the \meta{function} name and |
| 386 |
% the \meta{tokens}) and exhaustively expands the \meta{tokens}. |
| 387 |
% The result is inserted in braces into the input stream |
| 388 |
% \emph{after} reinsertion of the \meta{function}. |
| 389 |
% Thus the \meta{function} may take more |
| 390 |
% than one argument: all others are left unchanged. |
| 391 |
% \end{function} |
| 392 |
% |
| 393 |
% \begin{function}[EXP]{\exp_args:Nf} |
| 394 |
% \begin{syntax} |
| 395 |
% \cs{exp_args:Nf} \meta{function} \Arg{tokens} |
| 396 |
% \end{syntax} |
| 397 |
% This function absorbs two arguments (the \meta{function} name and |
| 398 |
% the \meta{tokens}). The \meta{tokens} are fully expanded until the |
| 399 |
% first non-expandable token is found (if that is a space it is |
| 400 |
% removed), and the result |
| 401 |
% is inserted in braces into the input stream \emph{after} reinsertion |
| 402 |
% of the \meta{function}. Thus the \meta{function} may take more than |
| 403 |
% one argument: all others are left unchanged. |
| 404 |
% \end{function} |
| 405 |
% |
| 406 |
% \begin{function}{\exp_args:Nx} |
| 407 |
% \begin{syntax} |
| 408 |
% \cs{exp_args:Nx} \meta{function} \Arg{tokens} |
| 409 |
% \end{syntax} |
| 410 |
% This function absorbs two arguments (the \meta{function} name and |
| 411 |
% the \meta{tokens}) and exhaustively expands the \meta{tokens}. |
| 412 |
% The result is inserted in braces into the input stream |
| 413 |
% \emph{after} reinsertion of the \meta{function}. |
| 414 |
% Thus the \meta{function} may take more |
| 415 |
% than one argument: all others are left unchanged. |
| 416 |
% \end{function} |
| 417 |
% |
| 418 |
% \section{Manipulating two arguments} |
| 419 |
% |
| 420 |
% \begin{function}[EXP, updated = 2018-05-15] |
| 421 |
% { |
| 422 |
% \exp_args:NNc, |
| 423 |
% \exp_args:NNo, |
| 424 |
% \exp_args:NNV, |
| 425 |
% \exp_args:NNv, |
| 426 |
% \exp_args:NNe, |
| 427 |
% \exp_args:NNf, |
| 428 |
% \exp_args:Ncc, |
| 429 |
% \exp_args:Nco, |
| 430 |
% \exp_args:NcV, |
| 431 |
% \exp_args:Ncv, |
| 432 |
% \exp_args:Ncf, |
| 433 |
% \exp_args:NVV |
| 434 |
% } |
| 435 |
% \begin{syntax} |
| 436 |
% \cs{exp_args:NNc} \meta{token_1} \meta{token_2} \Arg{tokens} |
| 437 |
% \end{syntax} |
| 438 |
% These optimized functions absorb three arguments and expand the second and |
| 439 |
% third as detailed by their argument specifier. The first argument |
| 440 |
% of the function is then the next item on the input stream, followed |
| 441 |
% by the expansion of the second and third arguments. |
| 442 |
% \end{function} |
| 443 |
% |
| 444 |
% \begin{function}[EXP, updated = 2018-05-15] |
| 445 |
% { |
| 446 |
% \exp_args:Nnc, |
| 447 |
% \exp_args:Nno, |
| 448 |
% \exp_args:Nnf, |
| 449 |
% \exp_args:NnV, |
| 450 |
% \exp_args:Nnv, |
| 451 |
% \exp_args:Nne, |
| 452 |
% \exp_args:Nce, |
| 453 |
% \exp_args:Noc, |
| 454 |
% \exp_args:Noo, |
| 455 |
% \exp_args:Nof, |
| 456 |
% \exp_args:Nfo, |
| 457 |
% \exp_args:Nff, |
| 458 |
% \exp_args:NVo, |
| 459 |
% \exp_args:Nee |
| 460 |
% } |
| 461 |
% \begin{syntax} |
| 462 |
% \cs{exp_args:Nnc} \meta{token} \Arg{tokens_1} \Arg{tokens_2} |
| 463 |
% \end{syntax} |
| 464 |
% These functions absorb three arguments and expand the second and |
| 465 |
% third as detailed by their argument specifier. The first argument |
| 466 |
% of the function is then the next item on the input stream, followed |
| 467 |
% by the expansion of the second and third arguments. |
| 468 |
% \end{function} |
| 469 |
% |
| 470 |
% \begin{function} |
| 471 |
% { |
| 472 |
% \exp_args:NNx, |
| 473 |
% \exp_args:Ncx, |
| 474 |
% \exp_args:Nnx, |
| 475 |
% \exp_args:Nox, |
| 476 |
% \exp_args:Nxo, |
| 477 |
% \exp_args:Nxx |
| 478 |
% } |
| 479 |
% \begin{syntax} |
| 480 |
% \cs{exp_args:NNx} \meta{token_1} \meta{token_2} \Arg{tokens} |
| 481 |
% \end{syntax} |
| 482 |
% These functions absorb three arguments and expand the second and |
| 483 |
% third as detailed by their argument specifier. The first argument |
| 484 |
% of the function is then the next item on the input stream, followed |
| 485 |
% by the expansion of the second and third arguments. These functions |
| 486 |
% are not expandable due to their |x|-type argument. |
| 487 |
% \end{function} |
| 488 |
% |
| 489 |
% \section{Manipulating three arguments} |
| 490 |
% |
| 491 |
% \begin{function}[EXP] |
| 492 |
% { |
| 493 |
% \exp_args:NNNo, |
| 494 |
% \exp_args:NNNV, |
| 495 |
% \exp_args:NNNv, |
| 496 |
% \exp_args:NNNe, |
| 497 |
% \exp_args:Nccc, |
| 498 |
% \exp_args:NcNc, |
| 499 |
% \exp_args:NcNo, |
| 500 |
% \exp_args:Ncco |
| 501 |
% } |
| 502 |
% \begin{syntax} |
| 503 |
% \cs{exp_args:NNNo} \meta{token_1} \meta{token_2} \meta{token_3} \Arg{tokens} |
| 504 |
% \end{syntax} |
| 505 |
% These optimized functions absorb four arguments and expand the second, third |
| 506 |
% and fourth as detailed by their argument specifier. The first |
| 507 |
% argument of the function is then the next item on the input stream, |
| 508 |
% followed by the expansion of the second argument, \emph{etc}. |
| 509 |
% \end{function} |
| 510 |
% |
| 511 |
% \begin{function}[EXP] |
| 512 |
% { |
| 513 |
% \exp_args:NNno, |
| 514 |
% \exp_args:NNnV, |
| 515 |
% \exp_args:NNnv, |
| 516 |
% \exp_args:NNne, |
| 517 |
% \exp_args:NNcc, |
| 518 |
% \exp_args:NNcf, |
| 519 |
% \exp_args:NNoo, |
| 520 |
% \exp_args:NNVV, |
| 521 |
% \exp_args:NNVv, |
| 522 |
% \exp_args:NNVe, |
| 523 |
% \exp_args:NNvV, |
| 524 |
% \exp_args:NNvv, |
| 525 |
% \exp_args:NNve, |
| 526 |
% \exp_args:NNeV, |
| 527 |
% \exp_args:NNev, |
| 528 |
% \exp_args:NNee, |
| 529 |
% \exp_args:NnNV, |
| 530 |
% \exp_args:Nnnc, |
| 531 |
% \exp_args:Nnno, |
| 532 |
% \exp_args:Nnnf, |
| 533 |
% \exp_args:NnnV, |
| 534 |
% \exp_args:Nnnv, |
| 535 |
% \exp_args:Nnne, |
| 536 |
% \exp_args:Nnff, |
| 537 |
% \exp_args:Nnee, |
| 538 |
% \exp_args:Ncnc, |
| 539 |
% \exp_args:Ncno, |
| 540 |
% \exp_args:NcnV, |
| 541 |
% \exp_args:Ncnv, |
| 542 |
% \exp_args:Ncne, |
| 543 |
% \exp_args:Ncoo, |
| 544 |
% \exp_args:NcVV, |
| 545 |
% \exp_args:NcVv, |
| 546 |
% \exp_args:NcVe, |
| 547 |
% \exp_args:NcvV, |
| 548 |
% \exp_args:Ncvv, |
| 549 |
% \exp_args:Ncve, |
| 550 |
% \exp_args:NceV, |
| 551 |
% \exp_args:Ncev, |
| 552 |
% \exp_args:Ncee, |
| 553 |
% \exp_args:Nooo, |
| 554 |
% \exp_args:Noof, |
| 555 |
% \exp_args:Nffo, |
| 556 |
% \exp_args:NVNV, |
| 557 |
% \exp_args:Neee |
| 558 |
% } |
| 559 |
% \begin{syntax} |
| 560 |
% \cs{exp_args:NNno} \meta{token_1} \meta{token_2} \Arg{token_3} \Arg{tokens} |
| 561 |
% \end{syntax} |
| 562 |
% These functions absorb four arguments and expand the second, third |
| 563 |
% and fourth as detailed by their argument specifier. The first |
| 564 |
% argument of the function is then the next item on the input stream, |
| 565 |
% followed by the expansion of the second argument, \emph{etc}. |
| 566 |
% \end{function} |
| 567 |
% |
| 568 |
% \begin{function}[added = 2015-08-12] |
| 569 |
% { |
| 570 |
% \exp_args:NNNx, |
| 571 |
% \exp_args:NNnx, |
| 572 |
% \exp_args:NNox, |
| 573 |
% \exp_args:Nccx, |
| 574 |
% \exp_args:Ncnx, |
| 575 |
% \exp_args:Nnnx, |
| 576 |
% \exp_args:Nnox, |
| 577 |
% \exp_args:Noox, |
| 578 |
% } |
| 579 |
% \begin{syntax} |
| 580 |
% \cs{exp_args:NNNx} \meta{token_1} \meta{token_2} \meta{tokens_1} \Arg{tokens_2} |
| 581 |
% \end{syntax} |
| 582 |
% These functions absorb four arguments and expand the second, third |
| 583 |
% and fourth as detailed by their argument specifier. The first |
| 584 |
% argument of the function is then the next item on the input stream, |
| 585 |
% followed by the expansion of the second argument, etc. |
| 586 |
% \end{function} |
| 587 |
% |
| 588 |
% \section{Unbraced expansion} |
| 589 |
% |
| 590 |
% \begin{function}[EXP, updated = 2018-05-15] |
| 591 |
% { |
| 592 |
% \exp_last_unbraced:No, |
| 593 |
% \exp_last_unbraced:NV, |
| 594 |
% \exp_last_unbraced:Nv, |
| 595 |
% \exp_last_unbraced:Ne, |
| 596 |
% \exp_last_unbraced:Nf, |
| 597 |
% \exp_last_unbraced:NNo, |
| 598 |
% \exp_last_unbraced:NNV, |
| 599 |
% \exp_last_unbraced:NNf, |
| 600 |
% \exp_last_unbraced:Nco, |
| 601 |
% \exp_last_unbraced:NcV, |
| 602 |
% \exp_last_unbraced:Nno, |
| 603 |
% \exp_last_unbraced:Nnf, |
| 604 |
% \exp_last_unbraced:Noo, |
| 605 |
% \exp_last_unbraced:Nfo, |
| 606 |
% \exp_last_unbraced:NNNo, |
| 607 |
% \exp_last_unbraced:NNNV, |
| 608 |
% \exp_last_unbraced:NNNf, |
| 609 |
% \exp_last_unbraced:NnNo, |
| 610 |
% \exp_last_unbraced:NNNNo, |
| 611 |
% \exp_last_unbraced:NNNNf, |
| 612 |
% } |
| 613 |
% \begin{syntax} |
| 614 |
% \cs{exp_last_unbraced:No} \meta{token} \Arg{tokens_1} |
| 615 |
% \end{syntax} |
| 616 |
% These functions absorb the number of arguments given by their |
| 617 |
% specification, carry out the expansion |
| 618 |
% indicated and leave the results in the input stream, with the |
| 619 |
% last argument not surrounded by the usual braces. |
| 620 |
% Of these, the |:Nno|, |:Noo|, |:Nfo| and |:NnNo| |
| 621 |
% variants need slower processing. |
| 622 |
% \begin{texnote} |
| 623 |
% As an optimization, the last argument is unbraced by some |
| 624 |
% of those functions before expansion. This can cause problems |
| 625 |
% if the argument is empty: for instance, |
| 626 |
% \cs{exp_last_unbraced:Nf} |\foo_bar:w| |{ }| \cs{q_stop} |
| 627 |
% leads to an infinite loop, as the quark is \texttt{f}-expanded. |
| 628 |
% \end{texnote} |
| 629 |
% \end{function} |
| 630 |
% |
| 631 |
% \begin{function}{\exp_last_unbraced:Nx} |
| 632 |
% \begin{syntax} |
| 633 |
% \cs{exp_last_unbraced:Nx} \meta{function} \Arg{tokens} |
| 634 |
% \end{syntax} |
| 635 |
% This function fully expands the \meta{tokens} and leaves the result |
| 636 |
% in the input stream after reinsertion of the \meta{function}. |
| 637 |
% This function is not expandable. |
| 638 |
% \end{function} |
| 639 |
% |
| 640 |
% \begin{function}[EXP]{\exp_last_two_unbraced:Noo} |
| 641 |
% \begin{syntax} |
| 642 |
% \cs{exp_last_two_unbraced:Noo} \meta{token} \Arg{tokens_1} \Arg{tokens_2} |
| 643 |
% \end{syntax} |
| 644 |
% This function absorbs three arguments and expands the second and third |
| 645 |
% once. The first argument of the function is then the next item on the |
| 646 |
% input stream, followed by the expansion of the second and third arguments, |
| 647 |
% which are not wrapped in braces. |
| 648 |
% This function needs special (slower) processing. |
| 649 |
% \end{function} |
| 650 |
% |
| 651 |
% \begin{function}[EXP]{\exp_after:wN} |
| 652 |
% \begin{syntax} |
| 653 |
% \cs{exp_after:wN} \meta{token_1} \meta{token_2} |
| 654 |
% \end{syntax} |
| 655 |
% Carries out a single expansion of \meta{token_2} (which may consume |
| 656 |
% arguments) prior to the expansion of \meta{token_1}. If \meta{token_2} has |
| 657 |
% no expansion (for example, if it is a character) then it is left |
| 658 |
% unchanged. It is important to notice that \meta{token_1} may be |
| 659 |
% \emph{any} single token, including group-opening and -closing |
| 660 |
% tokens (|{| or |}| assuming normal \TeX{} category codes). Unless |
| 661 |
% specifically required this should be avoided: expansion should be carried out using an |
| 662 |
% appropriate argument specifier variant or the appropriate |
| 663 |
% \cs[no-index]{exp_args:N\meta{variant}} function. |
| 664 |
% \begin{texnote} |
| 665 |
% This is the \TeX{} primitive \tn{expandafter}. |
| 666 |
% \end{texnote} |
| 667 |
% \end{function} |
| 668 |
% |
| 669 |
% |
| 670 |
% \section{Preventing expansion} |
| 671 |
% |
| 672 |
% Despite the fact that the following functions are all about preventing |
| 673 |
% expansion, they're designed to be used in an expandable context and hence |
| 674 |
% are all marked as being `expandable' since they themselves disappear |
| 675 |
% after the expansion has completed. |
| 676 |
% |
| 677 |
% \begin{function}[EXP]{\exp_not:N} |
| 678 |
% \begin{syntax} |
| 679 |
% \cs{exp_not:N} \meta{token} |
| 680 |
% \end{syntax} |
| 681 |
% Prevents expansion of the \meta{token} in a context where it would |
| 682 |
% otherwise be expanded, for example an |e|-type or |x|-type argument or |
| 683 |
% the first token in an |o|-type or |f|-type argument. |
| 684 |
% \begin{texnote} |
| 685 |
% This is the \TeX{} primitive \tn{noexpand}. It only prevents |
| 686 |
% expansion. At the beginning of an |f|-type argument, a space |
| 687 |
% \meta{token} is removed even if it appears as \cs{exp_not:N} |
| 688 |
% \cs{c_space_token}. In an |e|-expanding definition |
| 689 |
% (\cs{cs_new:Npe}), a macro parameter introduces an argument even |
| 690 |
% if it appears as \cs{exp_not:N} |#| |1|. This differs from |
| 691 |
% \cs{exp_not:n}. |
| 692 |
% \end{texnote} |
| 693 |
% \end{function} |
| 694 |
% |
| 695 |
% \begin{function}[EXP]{\exp_not:c} |
| 696 |
% \begin{syntax} |
| 697 |
% \cs{exp_not:c} \Arg{tokens} |
| 698 |
% \end{syntax} |
| 699 |
% Expands the \meta{tokens} until only characters remain, and then |
| 700 |
% converts this into a control sequence. |
| 701 |
% Further expansion of this control sequence is then inhibited using |
| 702 |
% \cs{exp_not:N}. |
| 703 |
% \end{function} |
| 704 |
% |
| 705 |
% \begin{function}[EXP]{\exp_not:n} |
| 706 |
% \begin{syntax} |
| 707 |
% \cs{exp_not:n} \Arg{tokens} |
| 708 |
% \end{syntax} |
| 709 |
% Prevents expansion of the \meta{tokens} in an |e|-type or |x|-type argument. |
| 710 |
% In all other cases the \meta{tokens} continue to be expanded, for |
| 711 |
% example in the input stream or in other types of arguments such as |
| 712 |
% \texttt{c}, \texttt{f}, \texttt{v}. The argument of \cs{exp_not:n} |
| 713 |
% \emph{must} be surrounded by braces. |
| 714 |
% \begin{texnote} |
| 715 |
% This is the \eTeX{} primitive \tn{unexpanded}. In an |
| 716 |
% |e|-expanding definition (\cs{cs_new:Npe}), \cs{exp_not:n}~|{#1}| |
| 717 |
% is equivalent to |##1| rather than to~|#1|, namely it inserts the |
| 718 |
% two characters |#| and~|1|, and |
| 719 |
% \cs{exp_not:n}~|{#}| is equivalent to |#|, namely it inserts the |
| 720 |
% character~|#|. |
| 721 |
% \end{texnote} |
| 722 |
% \end{function} |
| 723 |
% |
| 724 |
% \begin{function}[EXP]{\exp_not:o} |
| 725 |
% \begin{syntax} |
| 726 |
% \cs{exp_not:o} \Arg{tokens} |
| 727 |
% \end{syntax} |
| 728 |
% Expands the \meta{tokens} once, then prevents any further expansion |
| 729 |
% in |e|-type or |x|-type arguments using \cs{exp_not:n}. |
| 730 |
% \end{function} |
| 731 |
% |
| 732 |
% \begin{function}[EXP]{\exp_not:V} |
| 733 |
% \begin{syntax} |
| 734 |
% \cs{exp_not:V} \meta{variable} |
| 735 |
% \end{syntax} |
| 736 |
% Recovers the content of the \meta{variable}, then prevents expansion |
| 737 |
% of this material in |e|-type or |x|-type arguments using \cs{exp_not:n}. |
| 738 |
% \end{function} |
| 739 |
% |
| 740 |
% \begin{function}[EXP]{\exp_not:v} |
| 741 |
% \begin{syntax} |
| 742 |
% \cs{exp_not:v} \Arg{tokens} |
| 743 |
% \end{syntax} |
| 744 |
% Expands the \meta{tokens} until only characters remains, and then |
| 745 |
% converts this into a control sequence which should be a \meta{variable} |
| 746 |
% name. |
| 747 |
% The content of the \meta{variable} is recovered, and further |
| 748 |
% expansion in |e|-type or |x|-type arguments is prevented using \cs{exp_not:n}. |
| 749 |
% \end{function} |
| 750 |
% |
| 751 |
% \begin{function}[EXP]{\exp_not:e} |
| 752 |
% \begin{syntax} |
| 753 |
% \cs{exp_not:e} \Arg{tokens} |
| 754 |
% \end{syntax} |
| 755 |
% Expands \meta{tokens} exhaustively, then protects the result of the |
| 756 |
% expansion (including any tokens which were not expanded) from |
| 757 |
% further expansion in |e|-type or |x|-type arguments using \cs{exp_not:n}. |
| 758 |
% This is very rarely useful but is provided for consistency. |
| 759 |
% \end{function} |
| 760 |
% |
| 761 |
% \begin{function}[EXP]{\exp_not:f} |
| 762 |
% \begin{syntax} |
| 763 |
% \cs{exp_not:f} \Arg{tokens} |
| 764 |
% \end{syntax} |
| 765 |
% Expands \meta{tokens} fully until the first unexpandable token is |
| 766 |
% found (if it is a space it is removed). Expansion then stops, and |
| 767 |
% the result of the expansion (including any tokens which were not |
| 768 |
% expanded) is protected from further expansion in |e|-type or |x|-type arguments |
| 769 |
% using \cs{exp_not:n}. |
| 770 |
% \end{function} |
| 771 |
% |
| 772 |
% \begin{function}[updated = 2011-06-03, EXP]{\exp_stop_f:} |
| 773 |
% \begin{syntax} |
| 774 |
% |\foo_bar:f| \{ \meta{tokens} \cs{exp_stop_f:} \meta{more tokens} \} |
| 775 |
% \end{syntax} |
| 776 |
% This function terminates an \texttt{f}-type expansion. Thus if |
| 777 |
% a function |\foo_bar:f| starts an \texttt{f}-type expansion |
| 778 |
% and all of \meta{tokens} are expandable \cs{exp_stop_f:} |
| 779 |
% terminates the expansion of tokens even if \meta{more tokens} |
| 780 |
% are also expandable. The function itself is an implicit space |
| 781 |
% token. Inside an \texttt{e}-type or \texttt{x}-type expansion, it retains its |
| 782 |
% form, but when typeset it produces the underlying space (\verb*| |). |
| 783 |
% \end{function} |
| 784 |
% |
| 785 |
% |
| 786 |
% \section{Controlled expansion} |
| 787 |
% |
| 788 |
% The \pkg{expl3} language makes all efforts to hide the complexity of |
| 789 |
% \TeX{} expansion from the programmer by providing concepts that |
| 790 |
% evaluate/expand arguments of functions prior to calling the \enquote{base} |
| 791 |
% functions. Thus, instead of using many \tn{expandafter} calls and |
| 792 |
% other trickery it is usually a matter of choosing the right variant |
| 793 |
% of a function to achieve a desired result. |
| 794 |
% |
| 795 |
% Of course, deep down \TeX{} is using expansion as always and there |
| 796 |
% are cases where a programmer needs to control that expansion |
| 797 |
% directly; typical situations are basic data manipulation tools. This |
| 798 |
% section documents the functions for that level. These |
| 799 |
% commands are used throughout the kernel code, but we hope that outside |
| 800 |
% the kernel there will be little need to resort to them. Instead the |
| 801 |
% argument manipulation methods document above should usually be sufficient. |
| 802 |
% |
| 803 |
% While \cs{exp_after:wN} expands one token (out of order) it is |
| 804 |
% sometimes necessary to expand several tokens in one go. The next set |
| 805 |
% of commands provide this functionality. Be aware that it is |
| 806 |
% absolutely required that the programmer has full control over the |
| 807 |
% tokens to be expanded, i.e., it is not possible to use these |
| 808 |
% functions to expand unknown input as part of |
| 809 |
% \meta{expandable-tokens} as that will break badly if unexpandable |
| 810 |
% tokens are encountered in that place! |
| 811 |
% |
| 812 |
% \begin{function}[added=2015-08-23,EXP] |
| 813 |
% { |
| 814 |
% \exp:w , |
| 815 |
% \exp_end: |
| 816 |
% } |
| 817 |
% \begin{syntax} |
| 818 |
% \cs{exp:w} \meta{expandable tokens} \cs{exp_end:} \\ |
| 819 |
% \end{syntax} |
| 820 |
% Expands \meta{expandable-tokens} until reaching \cs{exp_end:} at |
| 821 |
% which point expansion stops. |
| 822 |
% The full expansion of \meta{expandable tokens} has to be empty. |
| 823 |
% If any token in \meta{expandable tokens} or any token generated by |
| 824 |
% expanding the tokens therein is not expandable the expansion will end |
| 825 |
% prematurely and as a result \cs{exp_end:} will be misinterpreted |
| 826 |
% later on.\footnotemark |
| 827 |
% |
| 828 |
% In typical use cases the \cs{exp_end:} is hidden somewhere |
| 829 |
% in the replacement text of \meta{expandable-tokens} rather than |
| 830 |
% being on the same expansion level than \cs{exp:w}, e.g., you may |
| 831 |
% see code such as |
| 832 |
%\begin{verbatim} |
| 833 |
% \exp:w \@@_case:NnTF #1 {#2} { } { } |
| 834 |
%\end{verbatim} |
| 835 |
% where somewhere during the expansion of |\@@_case:NnTF| the |
| 836 |
% \cs{exp_end:} gets generated. |
| 837 |
% \begin{texnote} |
| 838 |
% The current implementation uses \tn{romannumeral} hence ignores |
| 839 |
% space tokens and explicit signs |+| and |-| in the expansion of the |
| 840 |
% \meta{expandable tokens}, but this should not be relied upon. |
| 841 |
% \end{texnote} |
| 842 |
% \end{function} |
| 843 |
% \footnotetext{Due to the implementation you might get the character |
| 844 |
% in position 0 in the current font (typically \enquote{\texttt{`}}) |
| 845 |
% in the output without any error message!} |
| 846 |
% |
| 847 |
% \begin{function}[added=2015-08-23, EXP, label = \exp_end_continue_f:w] |
| 848 |
% { |
| 849 |
% \exp:w , |
| 850 |
% \exp_end_continue_f:w |
| 851 |
% } |
| 852 |
% \begin{syntax} |
| 853 |
% \cs{exp:w} \meta{expandable-tokens} \cs{exp_end_continue_f:w} \meta{further-tokens} |
| 854 |
% \end{syntax} |
| 855 |
% Expands \meta{expandable-tokens} until reaching \cs{exp_end_continue_f:w} at |
| 856 |
% which point expansion continues as an \texttt{f}-type expansion expanding |
| 857 |
% \meta{further-tokens} until an unexpandable token is encountered (or |
| 858 |
% the \texttt{f}-type expansion is explicitly terminated by |
| 859 |
% \cs{exp_stop_f:}). As with all \texttt{f}-type expansions a space ending |
| 860 |
% the expansion gets removed. |
| 861 |
% |
| 862 |
% The full expansion of \meta{expandable-tokens} has to be empty. |
| 863 |
% If any token in \meta{expandable-tokens} or any token generated by |
| 864 |
% expanding the tokens therein is not expandable the expansion will end |
| 865 |
% prematurely and as a result \cs{exp_end_continue_f:w} will be misinterpreted |
| 866 |
% later on.\footnotemark |
| 867 |
% |
| 868 |
% |
| 869 |
% In typical use cases \meta{expandable-tokens} contains no tokens at all, |
| 870 |
% e.g., you will see code such as |
| 871 |
%\begin{verbatim} |
| 872 |
% \exp_after:wN { \exp:w \exp_end_continue_f:w #2 } |
| 873 |
%\end{verbatim} |
| 874 |
% where the \cs{exp_after:wN} triggers an \texttt{f}-expansion of the tokens |
| 875 |
% in |#2|. For technical reasons this has to happen using two tokens |
| 876 |
% (if they would be hidden inside another command \cs{exp_after:wN} |
| 877 |
% would only expand the command but not trigger any additional |
| 878 |
% |f|-expansion). |
| 879 |
% |
| 880 |
% You might wonder why there are two different approaches available, |
| 881 |
% after all the effect of |
| 882 |
% \begin{quote} |
| 883 |
% \cs{exp:w} \meta{expandable-tokens} \cs{exp_end:} |
| 884 |
% \end{quote} |
| 885 |
% can be alternatively achieved through an \texttt{f}-type expansion by using |
| 886 |
% \cs{exp_stop_f:}, i.e. |
| 887 |
% \begin{quote} |
| 888 |
% \cs{exp:w} \cs{exp_end_continue_f:w} \meta{expandable-tokens} \cs{exp_stop_f:} |
| 889 |
% \end{quote} |
| 890 |
% The reason is simply that the first approach is slightly faster |
| 891 |
% (one less token to parse and less expansion internally) |
| 892 |
% so in places where such performance really matters and where we |
| 893 |
% want to explicitly stop the expansion at a defined point the first |
| 894 |
% form is preferable. |
| 895 |
% \end{function} |
| 896 |
% \footnotetext{In this particular case you may get a character into |
| 897 |
% the output as well as an error message.} |
| 898 |
% |
| 899 |
% \begin{function}[added=2015-08-23, EXP, label = \exp_end_continue_f:nw] |
| 900 |
% { |
| 901 |
% \exp:w , |
| 902 |
% \exp_end_continue_f:nw |
| 903 |
% } |
| 904 |
% \begin{syntax} |
| 905 |
% \cs{exp:w} \meta{expandable-tokens} \cs{exp_end_continue_f:nw} \meta{further-tokens} |
| 906 |
% \end{syntax} |
| 907 |
% The difference to \cs{exp_end_continue_f:w} is that we first we pick |
| 908 |
% up an argument which is then returned to the input stream. If |
| 909 |
% \meta{further-tokens} starts with space tokens then these space |
| 910 |
% tokens are removed while searching for the argument. If it starts |
| 911 |
% with a brace group then the braces are removed. Thus such spaces or |
| 912 |
% braces will not terminate the \texttt{f}-type expansion. |
| 913 |
% \end{function} |
| 914 |
% |
| 915 |
% \section{Internal functions} |
| 916 |
% |
| 917 |
% \begin{function}{\::n, \::N, \::p, \::c, \::o, \::e, \::f, \::x, \::v, \::V, \:::} |
| 918 |
% \begin{syntax} |
| 919 |
% |\cs_new:Npn \exp_args:Ncof { \::c \::o \::f \::: }| |
| 920 |
% \end{syntax} |
| 921 |
% Internal forms for the base expansion types. These names do \emph{not} |
| 922 |
% conform to the general \LaTeX3 approach as this makes them more readily |
| 923 |
% visible in the log and so forth. They should not be used outside this module. |
| 924 |
% \end{function} |
| 925 |
% |
| 926 |
% \begin{function} |
| 927 |
% {\::o_unbraced, \::e_unbraced, \::f_unbraced, \::x_unbraced, \::v_unbraced, \::V_unbraced} |
| 928 |
% \begin{syntax} |
| 929 |
% |\cs_new:Npn \exp_last_unbraced:Nno { \::n \::o_unbraced \::: }| |
| 930 |
% \end{syntax} |
| 931 |
% Internal forms for the expansion types which leave the terminal argument |
| 932 |
% unbraced. These names do \emph{not} |
| 933 |
% conform to the general \LaTeX3 approach as this makes them more readily |
| 934 |
% visible in the log and so forth. They should not be used outside this module. |
| 935 |
% \end{function} |
| 936 |
% |
| 937 |
% \end{documentation} |
| 938 |
% |
| 939 |
% \begin{implementation} |
| 940 |
% |
| 941 |
% \section{\pkg{l3expan} implementation} |
| 942 |
% |
| 943 |
% \begin{macrocode} |
| 944 |
%<*code> |
| 945 |
% \end{macrocode} |
| 946 |
% |
| 947 |
% \begin{macrocode} |
| 948 |
%<@@=exp> |
| 949 |
% \end{macrocode} |
| 950 |
% |
| 951 |
% \begin{variable}{\l_@@_tmp_tl} |
| 952 |
% The |\exp_| module has its private variable to temporarily store the |
| 953 |
% result of |x|-type argument expansion. This is done to avoid interference |
| 954 |
% with other functions using temporary variables. |
| 955 |
% \end{variable} |
| 956 |
% |
| 957 |
% \begin{macro}{\exp_after:wN} |
| 958 |
% \begin{macro}{\exp_not:N} |
| 959 |
% \begin{macro}{\exp_not:n} |
| 960 |
% These are defined in \pkg{l3basics}, as they are needed |
| 961 |
% \enquote{early}. This is just a reminder of that fact! |
| 962 |
% \end{macro} |
| 963 |
% \end{macro} |
| 964 |
% \end{macro} |
| 965 |
% |
| 966 |
% \subsection{General expansion} |
| 967 |
% |
| 968 |
% In this section a general mechanism for defining functions that handle |
| 969 |
% arguments is defined. These general expansion functions are |
| 970 |
% expandable unless |x| is used. (Any version of |x| is going to have |
| 971 |
% to use one of the \LaTeX3 names for \cs{cs_set:Npx} at some |
| 972 |
% point, and so is never going to be expandable.) |
| 973 |
% |
| 974 |
% The definition of expansion functions with this technique happens |
| 975 |
% in section~\ref{sec:l3expan:gendef}. |
| 976 |
% In section~\ref{sec:l3expan:handtune} some common cases are coded by a more direct |
| 977 |
% method for efficiency, typically using calls to \cs{exp_after:wN}. |
| 978 |
% |
| 979 |
% \begin{variable}{\l_@@_tmp_tl} |
| 980 |
% This scratch token list variable is defined in \pkg{l3basics}. |
| 981 |
% \end{variable} |
| 982 |
% |
| 983 |
% This code uses internal functions with names that start with |\::| to |
| 984 |
% perform the expansions. All macros are |long| since the tokens |
| 985 |
% undergoing expansion may be arbitrary user input. |
| 986 |
% |
| 987 |
% An argument manipulator |\::|\meta{Z} always has signature |#1\:::#2#3| |
| 988 |
% where |#1| holds the remaining argument manipulations to be performed, |
| 989 |
% \cs{:::} serves as an end marker for the list of manipulations, |#2| |
| 990 |
% is the carried over result of the previous expansion steps and |#3| is |
| 991 |
% the argument about to be processed. |
| 992 |
% One exception to this rule is \cs{::p}, which has to grab an argument |
| 993 |
% delimited by a left brace. |
| 994 |
% |
| 995 |
% \begin{macro}[EXP]{\@@_arg_next:nnn} |
| 996 |
% \begin{macro}[EXP]{\@@_arg_next:Nnn} |
| 997 |
% |#1| is the result of an expansion step, |#2| is the remaining |
| 998 |
% argument manipulations and |#3| is the current result of the |
| 999 |
% expansion chain. This auxiliary function moves |#1| back after |
| 1000 |
% |#3| in the input stream and checks if any expansion is left to |
| 1001 |
% be done by calling |#2|. In by far the most cases we need |
| 1002 |
% to add a set of braces to the result of an argument manipulation |
| 1003 |
% so it is more effective to do it directly here. Actually, so far |
| 1004 |
% only the |c| of the final argument manipulation variants does not |
| 1005 |
% require a set of braces. |
| 1006 |
% \begin{macrocode} |
| 1007 |
\cs_new:Npn \@@_arg_next:nnn #1#2#3 { #2 \::: { #3 {#1} } } |
| 1008 |
\cs_new:Npn \@@_arg_next:Nnn #1#2#3 { #2 \::: { #3 #1 } } |
| 1009 |
% \end{macrocode} |
| 1010 |
% \end{macro} |
| 1011 |
% \end{macro} |
| 1012 |
% |
| 1013 |
% \begin{macro}{\:::} |
| 1014 |
% The end marker is just another name for the identity function. |
| 1015 |
% \begin{macrocode} |
| 1016 |
\cs_new:Npn \::: #1 {#1} |
| 1017 |
% \end{macrocode} |
| 1018 |
% \end{macro} |
| 1019 |
% |
| 1020 |
% \begin{macro}[EXP]{\::n} |
| 1021 |
% This function is used to skip an argument that doesn't need to |
| 1022 |
% be expanded. |
| 1023 |
% \begin{macrocode} |
| 1024 |
\cs_new:Npn \::n #1 \::: #2#3 { #1 \::: { #2 {#3} } } |
| 1025 |
% \end{macrocode} |
| 1026 |
% \end{macro} |
| 1027 |
% |
| 1028 |
% \begin{macro}[EXP]{\::N} |
| 1029 |
% This function is used to skip an argument that consists of a |
| 1030 |
% single token and doesn't need to be expanded. |
| 1031 |
% \begin{macrocode} |
| 1032 |
\cs_new:Npn \::N #1 \::: #2#3 { #1 \::: {#2#3} } |
| 1033 |
% \end{macrocode} |
| 1034 |
% \end{macro} |
| 1035 |
% |
| 1036 |
% \begin{macro}[EXP]{\::p} |
| 1037 |
% This function is used to skip an argument that is delimited by a |
| 1038 |
% left brace and doesn't need to be expanded. It is not |
| 1039 |
% wrapped in braces in the result. |
| 1040 |
% \begin{macrocode} |
| 1041 |
\cs_new:Npn \::p #1 \::: #2#3# { #1 \::: {#2#3} } |
| 1042 |
% \end{macrocode} |
| 1043 |
% \end{macro} |
| 1044 |
% |
| 1045 |
% \begin{macro}[EXP]{\::c} |
| 1046 |
% This function is used to skip an argument that is turned into |
| 1047 |
% a control sequence without expansion. |
| 1048 |
% \begin{macrocode} |
| 1049 |
\cs_new:Npn \::c #1 \::: #2#3 |
| 1050 |
{ \exp_after:wN \@@_arg_next:Nnn \cs:w #3 \cs_end: {#1} {#2} } |
| 1051 |
% \end{macrocode} |
| 1052 |
% \end{macro} |
| 1053 |
% |
| 1054 |
% \begin{macro}[EXP]{\::o} |
| 1055 |
% This function is used to expand an argument once. |
| 1056 |
% \begin{macrocode} |
| 1057 |
\cs_new:Npn \::o #1 \::: #2#3 |
| 1058 |
{ \exp_after:wN \@@_arg_next:nnn \exp_after:wN {#3} {#1} {#2} } |
| 1059 |
% \end{macrocode} |
| 1060 |
% \end{macro} |
| 1061 |
% |
| 1062 |
% \begin{macro}[EXP]{\::e} |
| 1063 |
% With the \tn{expanded} primitive available, just expand. |
| 1064 |
% \begin{macrocode} |
| 1065 |
\cs_new:Npn \::e #1 \::: #2#3 |
| 1066 |
{ \tex_expanded:D { \exp_not:n { #1 \::: } { \exp_not:n {#2} {#3} } } } |
| 1067 |
% \end{macrocode} |
| 1068 |
% \end{macro} |
| 1069 |
% |
| 1070 |
% \begin{macro}[EXP]{\::f} |
| 1071 |
% \begin{macro}{\exp_stop_f:} |
| 1072 |
% This function is used to expand a token list until the first |
| 1073 |
% unexpandable token is found. This is achieved through \cs{exp:w} |
| 1074 |
% \cs{exp_end_continue_f:w} that expands everything in its way |
| 1075 |
% following it. This scanning procedure is terminated once the |
| 1076 |
% expansion hits something non-expandable (if that is a space it is |
| 1077 |
% removed). We introduce \cs{exp_stop_f:} to mark such an |
| 1078 |
% end-of-expansion marker. For example, |f|-expanding |
| 1079 |
% |\cs_set_eq:Nc \aaa { b \l_tmpa_tl b }| where |\l_tmpa_tl| contains |
| 1080 |
% the characters |lur| gives |\tex_let:D \aaa = \blurb| which then |
| 1081 |
% turns out to start with the non-expandable token |\tex_let:D|. |
| 1082 |
% Since the expansion of \cs{exp:w} \cs{exp_end_continue_f:w} is |
| 1083 |
% empty, we wind up with a fully expanded list, only \TeX{} has not |
| 1084 |
% tried to execute any of the non-expandable tokens. This is what |
| 1085 |
% differentiates this function from the |e| and |x| argument type. |
| 1086 |
% \begin{macrocode} |
| 1087 |
\cs_new:Npn \::f #1 \::: #2#3 |
| 1088 |
{ |
| 1089 |
\exp_after:wN \@@_arg_next:nnn |
| 1090 |
\exp_after:wN { \exp:w \exp_end_continue_f:w #3 } |
| 1091 |
{#1} {#2} |
| 1092 |
} |
| 1093 |
\use:nn { \cs_new_eq:NN \exp_stop_f: } { ~ } |
| 1094 |
% \end{macrocode} |
| 1095 |
% \end{macro} |
| 1096 |
% \end{macro} |
| 1097 |
% |
| 1098 |
% \begin{macro}{\::x} |
| 1099 |
% This function is used to expand an argument fully. |
| 1100 |
% We build in the expansion of \cs{@@_arg_next:nnn}. |
| 1101 |
% \begin{macrocode} |
| 1102 |
\cs_new_protected:Npn \::x #1 \::: #2#3 |
| 1103 |
{ |
| 1104 |
\cs_set_nopar:Npe \l_@@_tmp_tl |
| 1105 |
{ \exp_not:n { #1 \::: } { \exp_not:n {#2} {#3} } } |
| 1106 |
\l_@@_tmp_tl |
| 1107 |
} |
| 1108 |
% \end{macrocode} |
| 1109 |
% \end{macro} |
| 1110 |
% |
| 1111 |
% \begin{macro}[EXP]{\::v} |
| 1112 |
% \begin{macro}[EXP]{\::V} |
| 1113 |
% These functions return the value of a register, i.e., one of |
| 1114 |
% |tl|, |clist|, |int|, |skip|, |dim|, |muskip|, or built-in |
| 1115 |
% \TeX{} register. The |V| version |
| 1116 |
% expects a single token whereas |v| like |c| creates a csname from |
| 1117 |
% its argument given in braces and then evaluates it as if it was a |
| 1118 |
% |V|. The \cs{exp:w} sets off an expansion |
| 1119 |
% similar to an |f|-type expansion, which we terminate using |
| 1120 |
% \cs{exp_end:}. The argument is returned in braces. |
| 1121 |
% \begin{macrocode} |
| 1122 |
\cs_new:Npn \::V #1 \::: #2#3 |
| 1123 |
{ |
| 1124 |
\exp_after:wN \@@_arg_next:nnn |
| 1125 |
\exp_after:wN { \exp:w \@@_eval_register:N #3 } |
| 1126 |
{#1} {#2} |
| 1127 |
} |
| 1128 |
\cs_new:Npn \::v #1 \::: #2#3 |
| 1129 |
{ |
| 1130 |
\exp_after:wN \@@_arg_next:nnn |
| 1131 |
\exp_after:wN { \exp:w \@@_eval_register:c {#3} } |
| 1132 |
{#1} {#2} |
| 1133 |
} |
| 1134 |
% \end{macrocode} |
| 1135 |
% \end{macro} |
| 1136 |
% \end{macro} |
| 1137 |
% |
| 1138 |
% \begin{macro}[EXP]{\@@_eval_register:N, \@@_eval_register:c} |
| 1139 |
% \begin{macro}[EXP]{\@@_eval_error_msg:w} |
| 1140 |
% This function evaluates a register. Now a register might exist as |
| 1141 |
% one of two things: A parameter-less macro or a built-in \TeX{} |
| 1142 |
% register such as |\count|. For the \TeX{} registers we have to |
| 1143 |
% utilize a \tn{the} whereas for the macros we merely have to |
| 1144 |
% expand them once. The trick is to find out when to use |
| 1145 |
% \tn{the} and when not to. What we want here is to find out |
| 1146 |
% whether the token expands to something else when hit with |
| 1147 |
% \cs{exp_after:wN}. The technique is to compare the meaning of the |
| 1148 |
% token in question when it has been prefixed with \cs{exp_not:N} |
| 1149 |
% and the token itself. If it is a macro, the prefixed |
| 1150 |
% \cs{exp_not:N} temporarily turns it into the primitive |
| 1151 |
% \cs{scan_stop:}. |
| 1152 |
% \begin{macrocode} |
| 1153 |
\cs_new:Npn \@@_eval_register:N #1 |
| 1154 |
{ |
| 1155 |
\exp_after:wN \if_meaning:w \exp_not:N #1 #1 |
| 1156 |
% \end{macrocode} |
| 1157 |
% If the token was not a macro it may be a malformed variable from a |
| 1158 |
% |c| expansion in which case it is equal to the primitive |
| 1159 |
% \cs{scan_stop:}. In that case we throw an error. We could let \TeX{} |
| 1160 |
% do it for us but that would result in the rather obscure |
| 1161 |
% \begin{quote} |
| 1162 |
% |! You can't use `\relax' after \the.| |
| 1163 |
% \end{quote} |
| 1164 |
% which while quite true doesn't give many hints as to what actually |
| 1165 |
% went wrong. We provide something more sensible. |
| 1166 |
% \begin{macrocode} |
| 1167 |
\if_meaning:w \scan_stop: #1 |
| 1168 |
\@@_eval_error_msg:w |
| 1169 |
\fi: |
| 1170 |
% \end{macrocode} |
| 1171 |
% The next bit requires some explanation. The function must be |
| 1172 |
% initiated by \cs{exp:w} and we want to |
| 1173 |
% terminate this expansion chain by inserting the \cs{exp_end:} |
| 1174 |
% token. |
| 1175 |
% However, we have to expand the register |#1| before we do |
| 1176 |
% that. If it is a \TeX{} register, we need to execute the sequence |
| 1177 |
% |\exp_after:wN \exp_end: \tex_the:D #1| and if it is a macro we |
| 1178 |
% need to execute |\exp_after:wN \exp_end: #1|. We therefore issue |
| 1179 |
% the longer of the two sequences and if the register is a macro, we |
| 1180 |
% remove the \cs{tex_the:D}. |
| 1181 |
% \begin{macrocode} |
| 1182 |
\else: |
| 1183 |
\exp_after:wN \use_i_ii:nnn |
| 1184 |
\fi: |
| 1185 |
\exp_after:wN \exp_end: \tex_the:D #1 |
| 1186 |
} |
| 1187 |
\cs_new:Npn \@@_eval_register:c #1 |
| 1188 |
{ \exp_after:wN \@@_eval_register:N \cs:w #1 \cs_end: } |
| 1189 |
% \end{macrocode} |
| 1190 |
% Clean up nicely, then call the undefined control sequence. The |
| 1191 |
% result is an error message looking like this: |
| 1192 |
% \begin{verbatim} |
| 1193 |
% ! Undefined control sequence. |
| 1194 |
% <argument> \LaTeX3 error: |
| 1195 |
% Erroneous variable used! |
| 1196 |
% l.55 \tl_set:Nv \l_tmpa_tl {undefined_tl} |
| 1197 |
% \end{verbatim} |
| 1198 |
% \begin{macrocode} |
| 1199 |
\cs_new:Npn \@@_eval_error_msg:w #1 \tex_the:D #2 |
| 1200 |
{ |
| 1201 |
\fi: |
| 1202 |
\fi: |
| 1203 |
\msg_expandable_error:nnn { kernel } { bad-variable } {#2} |
| 1204 |
\exp_end: |
| 1205 |
} |
| 1206 |
% \end{macrocode} |
| 1207 |
% \end{macro} |
| 1208 |
% \end{macro} |
| 1209 |
% |
| 1210 |
% \subsection{Hand-tuned definitions} |
| 1211 |
% \label{sec:l3expan:handtune} |
| 1212 |
% |
| 1213 |
% One of the most important features of these functions is that they |
| 1214 |
% are fully expandable. |
| 1215 |
% |
| 1216 |
% \begin{macro}[EXP]{\exp_args:Nc, \exp_args:cc} |
| 1217 |
% In \pkg{l3basics}. |
| 1218 |
% \end{macro} |
| 1219 |
% |
| 1220 |
% \begin{macro}[EXP]{\exp_args:NNc, \exp_args:Ncc, \exp_args:Nccc} |
| 1221 |
% Here are the functions that turn their argument into csnames but are |
| 1222 |
% expandable. |
| 1223 |
% \begin{macrocode} |
| 1224 |
\cs_new:Npn \exp_args:NNc #1#2#3 |
| 1225 |
{ \exp_after:wN #1 \exp_after:wN #2 \cs:w # 3\cs_end: } |
| 1226 |
\cs_new:Npn \exp_args:Ncc #1#2#3 |
| 1227 |
{ \exp_after:wN #1 \cs:w #2 \exp_after:wN \cs_end: \cs:w #3 \cs_end: } |
| 1228 |
\cs_new:Npn \exp_args:Nccc #1#2#3#4 |
| 1229 |
{ |
| 1230 |
\exp_after:wN #1 |
| 1231 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1232 |
\cs:w #3 \exp_after:wN \cs_end: |
| 1233 |
\cs:w #4 \cs_end: |
| 1234 |
} |
| 1235 |
% \end{macrocode} |
| 1236 |
% \end{macro} |
| 1237 |
% |
| 1238 |
% \begin{macro}[EXP]{\exp_args:No} |
| 1239 |
% \begin{macro}[EXP]{\exp_args:NNo} |
| 1240 |
% \begin{macro}[EXP]{\exp_args:NNNo} |
| 1241 |
% Those lovely runs of expansion! |
| 1242 |
% \begin{macrocode} |
| 1243 |
\cs_new:Npn \exp_args:No #1#2 { \exp_after:wN #1 \exp_after:wN {#2} } |
| 1244 |
\cs_new:Npn \exp_args:NNo #1#2#3 |
| 1245 |
{ \exp_after:wN #1 \exp_after:wN #2 \exp_after:wN {#3} } |
| 1246 |
\cs_new:Npn \exp_args:NNNo #1#2#3#4 |
| 1247 |
{ \exp_after:wN #1 \exp_after:wN#2 \exp_after:wN #3 \exp_after:wN {#4} } |
| 1248 |
% \end{macrocode} |
| 1249 |
% \end{macro} |
| 1250 |
% \end{macro} |
| 1251 |
% \end{macro} |
| 1252 |
% |
| 1253 |
% \begin{macro}[EXP]{\exp_args:Ne} |
| 1254 |
% When the \tn{expanded} primitive is available, use it. |
| 1255 |
% \begin{macrocode} |
| 1256 |
\cs_new:Npn \exp_args:Ne #1#2 |
| 1257 |
{ \exp_after:wN #1 \tex_expanded:D { {#2} } } |
| 1258 |
% \end{macrocode} |
| 1259 |
% \end{macro} |
| 1260 |
% |
| 1261 |
% \begin{macro}[EXP]{\exp_args:Nf, \exp_args:NV, \exp_args:Nv} |
| 1262 |
% \begin{macrocode} |
| 1263 |
\cs_new:Npn \exp_args:Nf #1#2 |
| 1264 |
{ \exp_after:wN #1 \exp_after:wN { \exp:w \exp_end_continue_f:w #2 } } |
| 1265 |
\cs_new:Npn \exp_args:Nv #1#2 |
| 1266 |
{ |
| 1267 |
\exp_after:wN #1 \exp_after:wN |
| 1268 |
{ \exp:w \@@_eval_register:c {#2} } |
| 1269 |
} |
| 1270 |
\cs_new:Npn \exp_args:NV #1#2 |
| 1271 |
{ |
| 1272 |
\exp_after:wN #1 \exp_after:wN |
| 1273 |
{ \exp:w \@@_eval_register:N #2 } |
| 1274 |
} |
| 1275 |
% \end{macrocode} |
| 1276 |
% \end{macro} |
| 1277 |
% |
| 1278 |
% \begin{macro}[EXP] |
| 1279 |
% { |
| 1280 |
% \exp_args:NNV, \exp_args:NNv, \exp_args:NNe, \exp_args:NNf, |
| 1281 |
% \exp_args:Nco, \exp_args:NcV, \exp_args:Ncv, \exp_args:Ncf, |
| 1282 |
% \exp_args:NVV, |
| 1283 |
% } |
| 1284 |
% Some more hand-tuned function with three arguments. |
| 1285 |
% If we forced that an |o| argument always has braces, |
| 1286 |
% we could implement \cs{exp_args:Nco} with less tokens |
| 1287 |
% and only two arguments. |
| 1288 |
% \begin{macrocode} |
| 1289 |
\cs_new:Npn \exp_args:NNV #1#2#3 |
| 1290 |
{ |
| 1291 |
\exp_after:wN #1 |
| 1292 |
\exp_after:wN #2 |
| 1293 |
\exp_after:wN { \exp:w \@@_eval_register:N #3 } |
| 1294 |
} |
| 1295 |
\cs_new:Npn \exp_args:NNv #1#2#3 |
| 1296 |
{ |
| 1297 |
\exp_after:wN #1 |
| 1298 |
\exp_after:wN #2 |
| 1299 |
\exp_after:wN { \exp:w \@@_eval_register:c {#3} } |
| 1300 |
} |
| 1301 |
\cs_new:Npn \exp_args:NNe #1#2#3 |
| 1302 |
{ |
| 1303 |
\exp_after:wN #1 |
| 1304 |
\exp_after:wN #2 |
| 1305 |
\tex_expanded:D { {#3} } |
| 1306 |
} |
| 1307 |
\cs_new:Npn \exp_args:NNf #1#2#3 |
| 1308 |
{ |
| 1309 |
\exp_after:wN #1 |
| 1310 |
\exp_after:wN #2 |
| 1311 |
\exp_after:wN { \exp:w \exp_end_continue_f:w #3 } |
| 1312 |
} |
| 1313 |
\cs_new:Npn \exp_args:Nco #1#2#3 |
| 1314 |
{ |
| 1315 |
\exp_after:wN #1 |
| 1316 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1317 |
\exp_after:wN {#3} |
| 1318 |
} |
| 1319 |
\cs_new:Npn \exp_args:NcV #1#2#3 |
| 1320 |
{ |
| 1321 |
\exp_after:wN #1 |
| 1322 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1323 |
\exp_after:wN { \exp:w \@@_eval_register:N #3 } |
| 1324 |
} |
| 1325 |
\cs_new:Npn \exp_args:Ncv #1#2#3 |
| 1326 |
{ |
| 1327 |
\exp_after:wN #1 |
| 1328 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1329 |
\exp_after:wN { \exp:w \@@_eval_register:c {#3} } |
| 1330 |
} |
| 1331 |
\cs_new:Npn \exp_args:Ncf #1#2#3 |
| 1332 |
{ |
| 1333 |
\exp_after:wN #1 |
| 1334 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1335 |
\exp_after:wN { \exp:w \exp_end_continue_f:w #3 } |
| 1336 |
} |
| 1337 |
\cs_new:Npn \exp_args:NVV #1#2#3 |
| 1338 |
{ |
| 1339 |
\exp_after:wN #1 |
| 1340 |
\exp_after:wN { \exp:w \exp_after:wN |
| 1341 |
\@@_eval_register:N \exp_after:wN #2 \exp_after:wN } |
| 1342 |
\exp_after:wN { \exp:w \@@_eval_register:N #3 } |
| 1343 |
} |
| 1344 |
% \end{macrocode} |
| 1345 |
% \end{macro} |
| 1346 |
% |
| 1347 |
% \begin{macro}[EXP] |
| 1348 |
% { |
| 1349 |
% \exp_args:NNNV, \exp_args:NNNv, \exp_args:NNNe, |
| 1350 |
% \exp_args:NcNc, \exp_args:NcNo, |
| 1351 |
% \exp_args:Ncco, |
| 1352 |
% } |
| 1353 |
% A few more that we can hand-tune. |
| 1354 |
% \begin{macrocode} |
| 1355 |
\cs_new:Npn \exp_args:NNNV #1#2#3#4 |
| 1356 |
{ |
| 1357 |
\exp_after:wN #1 |
| 1358 |
\exp_after:wN #2 |
| 1359 |
\exp_after:wN #3 |
| 1360 |
\exp_after:wN { \exp:w \@@_eval_register:N #4 } |
| 1361 |
} |
| 1362 |
\cs_new:Npn \exp_args:NNNv #1#2#3#4 |
| 1363 |
{ |
| 1364 |
\exp_after:wN #1 |
| 1365 |
\exp_after:wN #2 |
| 1366 |
\exp_after:wN #3 |
| 1367 |
\exp_after:wN { \exp:w \@@_eval_register:c {#4} } |
| 1368 |
} |
| 1369 |
\cs_new:Npn \exp_args:NNNe #1#2#3#4 |
| 1370 |
{ |
| 1371 |
\exp_after:wN #1 |
| 1372 |
\exp_after:wN #2 |
| 1373 |
\exp_after:wN #3 |
| 1374 |
\tex_expanded:D { {#4} } |
| 1375 |
} |
| 1376 |
\cs_new:Npn \exp_args:NcNc #1#2#3#4 |
| 1377 |
{ |
| 1378 |
\exp_after:wN #1 |
| 1379 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1380 |
\exp_after:wN #3 |
| 1381 |
\cs:w #4 \cs_end: |
| 1382 |
} |
| 1383 |
\cs_new:Npn \exp_args:NcNo #1#2#3#4 |
| 1384 |
{ |
| 1385 |
\exp_after:wN #1 |
| 1386 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1387 |
\exp_after:wN #3 |
| 1388 |
\exp_after:wN {#4} |
| 1389 |
} |
| 1390 |
\cs_new:Npn \exp_args:Ncco #1#2#3#4 |
| 1391 |
{ |
| 1392 |
\exp_after:wN #1 |
| 1393 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1394 |
\cs:w #3 \exp_after:wN \cs_end: |
| 1395 |
\exp_after:wN {#4} |
| 1396 |
} |
| 1397 |
% \end{macrocode} |
| 1398 |
% \end{macro} |
| 1399 |
% |
| 1400 |
% \begin{macro}{\exp_args:Nx} |
| 1401 |
% \begin{macrocode} |
| 1402 |
\cs_new_protected:Npn \exp_args:Nx #1#2 |
| 1403 |
{ \use:x { \exp_not:N #1 {#2} } } |
| 1404 |
% \end{macrocode} |
| 1405 |
% \end{macro} |
| 1406 |
% |
| 1407 |
% \subsection{Last-unbraced versions} |
| 1408 |
% |
| 1409 |
% \begin{macro}[EXP]{\@@_arg_last_unbraced:nn} |
| 1410 |
% \begin{macro}[EXP]{\::o_unbraced} |
| 1411 |
% \begin{macro}[EXP]{\::V_unbraced} |
| 1412 |
% \begin{macro}[EXP]{\::v_unbraced} |
| 1413 |
% \begin{macro}[EXP]{\::e_unbraced} |
| 1414 |
% \begin{macro}[EXP]{\::f_unbraced} |
| 1415 |
% \begin{macro}[EXP]{\::x_unbraced} |
| 1416 |
% There are a few places where the last argument needs to be available |
| 1417 |
% unbraced. First some helper macros. |
| 1418 |
% \begin{macrocode} |
| 1419 |
\cs_new:Npn \@@_arg_last_unbraced:nn #1#2 { #2#1 } |
| 1420 |
\cs_new:Npn \::o_unbraced \::: #1#2 |
| 1421 |
{ \exp_after:wN \@@_arg_last_unbraced:nn \exp_after:wN {#2} {#1} } |
| 1422 |
\cs_new:Npn \::V_unbraced \::: #1#2 |
| 1423 |
{ |
| 1424 |
\exp_after:wN \@@_arg_last_unbraced:nn |
| 1425 |
\exp_after:wN { \exp:w \@@_eval_register:N #2 } {#1} |
| 1426 |
} |
| 1427 |
\cs_new:Npn \::v_unbraced \::: #1#2 |
| 1428 |
{ |
| 1429 |
\exp_after:wN \@@_arg_last_unbraced:nn |
| 1430 |
\exp_after:wN { \exp:w \@@_eval_register:c {#2} } {#1} |
| 1431 |
} |
| 1432 |
\cs_new:Npn \::e_unbraced \::: #1#2 |
| 1433 |
{ \tex_expanded:D { \exp_not:n {#1} #2 } } |
| 1434 |
\cs_new:Npn \::f_unbraced \::: #1#2 |
| 1435 |
{ |
| 1436 |
\exp_after:wN \@@_arg_last_unbraced:nn |
| 1437 |
\exp_after:wN { \exp:w \exp_end_continue_f:w #2 } {#1} |
| 1438 |
} |
| 1439 |
\cs_new_protected:Npn \::x_unbraced \::: #1#2 |
| 1440 |
{ |
| 1441 |
\cs_set_nopar:Npe \l_@@_tmp_tl { \exp_not:n {#1} #2 } |
| 1442 |
\l_@@_tmp_tl |
| 1443 |
} |
| 1444 |
% \end{macrocode} |
| 1445 |
% \end{macro} |
| 1446 |
% \end{macro} |
| 1447 |
% \end{macro} |
| 1448 |
% \end{macro} |
| 1449 |
% \end{macro} |
| 1450 |
% \end{macro} |
| 1451 |
% \end{macro} |
| 1452 |
% |
| 1453 |
% \begin{macro}[EXP] |
| 1454 |
% { |
| 1455 |
% \exp_last_unbraced:No, |
| 1456 |
% \exp_last_unbraced:NV, |
| 1457 |
% \exp_last_unbraced:Nv, |
| 1458 |
% \exp_last_unbraced:Ne, |
| 1459 |
% \exp_last_unbraced:Nf, |
| 1460 |
% \exp_last_unbraced:NNo, |
| 1461 |
% \exp_last_unbraced:NNV, |
| 1462 |
% \exp_last_unbraced:NNf, |
| 1463 |
% \exp_last_unbraced:Nco, |
| 1464 |
% \exp_last_unbraced:NcV, |
| 1465 |
% \exp_last_unbraced:NNNo, |
| 1466 |
% \exp_last_unbraced:NNNV, |
| 1467 |
% \exp_last_unbraced:NNNf, |
| 1468 |
% \exp_last_unbraced:Nno, |
| 1469 |
% \exp_last_unbraced:Nnf, |
| 1470 |
% \exp_last_unbraced:Noo, |
| 1471 |
% \exp_last_unbraced:Nfo, |
| 1472 |
% \exp_last_unbraced:NnNo, |
| 1473 |
% \exp_last_unbraced:NNNNo, |
| 1474 |
% \exp_last_unbraced:NNNNf, |
| 1475 |
% } |
| 1476 |
% \begin{macro}{\exp_last_unbraced:Nx} |
| 1477 |
% Now the business end: most of these are hand-tuned for speed, but the |
| 1478 |
% general system is in place. |
| 1479 |
% \begin{macrocode} |
| 1480 |
\cs_new:Npn \exp_last_unbraced:No #1#2 { \exp_after:wN #1 #2 } |
| 1481 |
\cs_new:Npn \exp_last_unbraced:NV #1#2 |
| 1482 |
{ \exp_after:wN #1 \exp:w \@@_eval_register:N #2 } |
| 1483 |
\cs_new:Npn \exp_last_unbraced:Nv #1#2 |
| 1484 |
{ \exp_after:wN #1 \exp:w \@@_eval_register:c {#2} } |
| 1485 |
\cs_new:Npn \exp_last_unbraced:Ne #1#2 |
| 1486 |
{ \exp_after:wN #1 \tex_expanded:D {#2} } |
| 1487 |
\cs_new:Npn \exp_last_unbraced:Nf #1#2 |
| 1488 |
{ \exp_after:wN #1 \exp:w \exp_end_continue_f:w #2 } |
| 1489 |
\cs_new:Npn \exp_last_unbraced:NNo #1#2#3 |
| 1490 |
{ \exp_after:wN #1 \exp_after:wN #2 #3 } |
| 1491 |
\cs_new:Npn \exp_last_unbraced:NNV #1#2#3 |
| 1492 |
{ |
| 1493 |
\exp_after:wN #1 |
| 1494 |
\exp_after:wN #2 |
| 1495 |
\exp:w \@@_eval_register:N #3 |
| 1496 |
} |
| 1497 |
\cs_new:Npn \exp_last_unbraced:NNf #1#2#3 |
| 1498 |
{ |
| 1499 |
\exp_after:wN #1 |
| 1500 |
\exp_after:wN #2 |
| 1501 |
\exp:w \exp_end_continue_f:w #3 |
| 1502 |
} |
| 1503 |
\cs_new:Npn \exp_last_unbraced:Nco #1#2#3 |
| 1504 |
{ \exp_after:wN #1 \cs:w #2 \exp_after:wN \cs_end: #3 } |
| 1505 |
\cs_new:Npn \exp_last_unbraced:NcV #1#2#3 |
| 1506 |
{ |
| 1507 |
\exp_after:wN #1 |
| 1508 |
\cs:w #2 \exp_after:wN \cs_end: |
| 1509 |
\exp:w \@@_eval_register:N #3 |
| 1510 |
} |
| 1511 |
\cs_new:Npn \exp_last_unbraced:NNNo #1#2#3#4 |
| 1512 |
{ \exp_after:wN #1 \exp_after:wN #2 \exp_after:wN #3 #4 } |
| 1513 |
\cs_new:Npn \exp_last_unbraced:NNNV #1#2#3#4 |
| 1514 |
{ |
| 1515 |
\exp_after:wN #1 |
| 1516 |
\exp_after:wN #2 |
| 1517 |
\exp_after:wN #3 |
| 1518 |
\exp:w \@@_eval_register:N #4 |
| 1519 |
} |
| 1520 |
\cs_new:Npn \exp_last_unbraced:NNNf #1#2#3#4 |
| 1521 |
{ |
| 1522 |
\exp_after:wN #1 |
| 1523 |
\exp_after:wN #2 |
| 1524 |
\exp_after:wN #3 |
| 1525 |
\exp:w \exp_end_continue_f:w #4 |
| 1526 |
} |
| 1527 |
\cs_new:Npn \exp_last_unbraced:Nno { \::n \::o_unbraced \::: } |
| 1528 |
\cs_new:Npn \exp_last_unbraced:Nnf { \::n \::f_unbraced \::: } |
| 1529 |
\cs_new:Npn \exp_last_unbraced:Noo { \::o \::o_unbraced \::: } |
| 1530 |
\cs_new:Npn \exp_last_unbraced:Nfo { \::f \::o_unbraced \::: } |
| 1531 |
\cs_new:Npn \exp_last_unbraced:NnNo { \::n \::N \::o_unbraced \::: } |
| 1532 |
\cs_new:Npn \exp_last_unbraced:NNNNo #1#2#3#4#5 |
| 1533 |
{ \exp_after:wN #1 \exp_after:wN #2 \exp_after:wN #3 \exp_after:wN #4 #5 } |
| 1534 |
\cs_new:Npn \exp_last_unbraced:NNNNf #1#2#3#4#5 |
| 1535 |
{ |
| 1536 |
\exp_after:wN #1 |
| 1537 |
\exp_after:wN #2 |
| 1538 |
\exp_after:wN #3 |
| 1539 |
\exp_after:wN #4 |
| 1540 |
\exp:w \exp_end_continue_f:w #5 |
| 1541 |
} |
| 1542 |
\cs_new_protected:Npn \exp_last_unbraced:Nx { \::x_unbraced \::: } |
| 1543 |
% \end{macrocode} |
| 1544 |
% \end{macro} |
| 1545 |
% \end{macro} |
| 1546 |
% |
| 1547 |
% \begin{macro}[EXP]{\exp_last_two_unbraced:Noo} |
| 1548 |
% \begin{macro}[EXP]{\@@_last_two_unbraced:noN} |
| 1549 |
% If |#2| is a single token then this can be implemented as |
| 1550 |
% \begin{verbatim} |
| 1551 |
% \cs_new:Npn \exp_last_two_unbraced:Noo #1 #2 #3 |
| 1552 |
% { \exp_after:wN \exp_after:wN \exp_after:wN #1 \exp_after:wN #2 #3 } |
| 1553 |
% \end{verbatim} |
| 1554 |
% However, for robustness this is not suitable. Instead, a bit of a |
| 1555 |
% shuffle is used to ensure that |#2| can be multiple tokens. |
| 1556 |
% \begin{macrocode} |
| 1557 |
\cs_new:Npn \exp_last_two_unbraced:Noo #1#2#3 |
| 1558 |
{ \exp_after:wN \@@_last_two_unbraced:noN \exp_after:wN {#3} {#2} #1 } |
| 1559 |
\cs_new:Npn \@@_last_two_unbraced:noN #1#2#3 |
| 1560 |
{ \exp_after:wN #3 #2 #1 } |
| 1561 |
% \end{macrocode} |
| 1562 |
% \end{macro} |
| 1563 |
% \end{macro} |
| 1564 |
% |
| 1565 |
% \subsection{Preventing expansion} |
| 1566 |
% |
| 1567 |
% \begin{macro}{\__kernel_exp_not:w} |
| 1568 |
% At the kernel level, we need the primitive behavior to allow expansion |
| 1569 |
% \emph{before} the brace group. |
| 1570 |
% \begin{macrocode} |
| 1571 |
\cs_new_eq:NN \__kernel_exp_not:w \tex_unexpanded:D |
| 1572 |
% \end{macrocode} |
| 1573 |
% \end{macro} |
| 1574 |
% |
| 1575 |
% \begin{macro}[EXP]{\exp_not:c} |
| 1576 |
% \begin{macro}[EXP]{\exp_not:o} |
| 1577 |
% \begin{macro}[EXP]{\exp_not:e} |
| 1578 |
% \begin{macro}[EXP]{\exp_not:f} |
| 1579 |
% \begin{macro}[EXP]{\exp_not:V} |
| 1580 |
% \begin{macro}[EXP]{\exp_not:v} |
| 1581 |
% All these except \cs{exp_not:c} call the kernel-internal |
| 1582 |
% \cs{__kernel_exp_not:w} namely \cs{tex_unexpanded:D}. |
| 1583 |
% \begin{macrocode} |
| 1584 |
\cs_new:Npn \exp_not:c #1 { \exp_after:wN \exp_not:N \cs:w #1 \cs_end: } |
| 1585 |
\cs_new:Npn \exp_not:o #1 { \__kernel_exp_not:w \exp_after:wN {#1} } |
| 1586 |
\cs_new:Npn \exp_not:e #1 |
| 1587 |
{ \__kernel_exp_not:w \tex_expanded:D { {#1} } } |
| 1588 |
\cs_new:Npn \exp_not:f #1 |
| 1589 |
{ \__kernel_exp_not:w \exp_after:wN { \exp:w \exp_end_continue_f:w #1 } } |
| 1590 |
\cs_new:Npn \exp_not:V #1 |
| 1591 |
{ |
| 1592 |
\__kernel_exp_not:w \exp_after:wN |
| 1593 |
{ \exp:w \@@_eval_register:N #1 } |
| 1594 |
} |
| 1595 |
\cs_new:Npn \exp_not:v #1 |
| 1596 |
{ |
| 1597 |
\__kernel_exp_not:w \exp_after:wN |
| 1598 |
{ \exp:w \@@_eval_register:c {#1} } |
| 1599 |
} |
| 1600 |
% \end{macrocode} |
| 1601 |
% \end{macro} |
| 1602 |
% \end{macro} |
| 1603 |
% \end{macro} |
| 1604 |
% \end{macro} |
| 1605 |
% \end{macro} |
| 1606 |
% \end{macro} |
| 1607 |
% |
| 1608 |
% \subsection{Controlled expansion} |
| 1609 |
% |
| 1610 |
% \begin{macro}{\exp:w} |
| 1611 |
% \begin{macro}{\exp_end:} |
| 1612 |
% \begin{macro}{\exp_end_continue_f:w} |
| 1613 |
% \begin{macro}{\exp_end_continue_f:nw} |
| 1614 |
% To trigger a sequence of \enquote{arbitrarily} many expansions we |
| 1615 |
% need a method to invoke \TeX's expansion mechanism in such a way |
| 1616 |
% that (a) we are able to stop it in a controlled manner and (b) the |
| 1617 |
% result of what triggered the expansion in the first place is null, |
| 1618 |
% i.e., that we do not get any unwanted side effects. There aren't |
| 1619 |
% that many possibilities in \TeX{}; in fact the one explained below |
| 1620 |
% might well be the only one (as normally the result of expansion is |
| 1621 |
% not null). |
| 1622 |
% |
| 1623 |
% The trick here is to make use of the fact that |
| 1624 |
% \cs{tex_romannumeral:D} expands the tokens following it when looking |
| 1625 |
% for a number and that its expansion is null if that number turns out |
| 1626 |
% to be zero or negative. So we use that to start the expansion |
| 1627 |
% sequence: \cs{exp:w} is set equal to \cs{tex_romannumeral:D} in |
| 1628 |
% \pkg{l3basics}. To stop the expansion sequence in a controlled way |
| 1629 |
% all we need to provide is a constant integer zero as part of |
| 1630 |
% expanded tokens. As this is an integer constant it immediately stops |
| 1631 |
% \cs{tex_romannumeral:D}'s search for a number. Again, the |
| 1632 |
% definition of \cs{exp_end:} as the integer constant zero is in |
| 1633 |
% \pkg{l3basics}. (Note that according to our specification all |
| 1634 |
% tokens we expand initiated by \cs{exp:w} are supposed to be |
| 1635 |
% expandable (as well as their replacement text in the expansion) so |
| 1636 |
% we will not encounter a \enquote{number} that actually result in a |
| 1637 |
% roman numeral being generated. Or if we do then the programmer made |
| 1638 |
% a mistake.) |
| 1639 |
% |
| 1640 |
% If on the other hand we want to stop the initial expansion sequence |
| 1641 |
% but continue with an \texttt{f}-type expansion we provide the |
| 1642 |
% alphabetic constant |`^^@| that also represents |0| but this time |
| 1643 |
% \TeX's syntax for a \meta{number} continues searching for an |
| 1644 |
% optional space (and it continues expansion doing that) --- see |
| 1645 |
% \TeX{}book page~269 for details. |
| 1646 |
% \begin{macrocode} |
| 1647 |
\group_begin: |
| 1648 |
\tex_catcode:D `\^^@ = 13 |
| 1649 |
\cs_new_protected:Npn \exp_end_continue_f:w { `^^@ } |
| 1650 |
% \end{macrocode} |
| 1651 |
% If the above definition ever appears outside its proper context |
| 1652 |
% the active character |^^@| will be executed so we turn this into an |
| 1653 |
% error. The test for existence covers the (unlikely) case that some |
| 1654 |
% other code has already defined |^^@|: this is true for example for |
| 1655 |
% \texttt{xmltex.tex}. |
| 1656 |
% \begin{macrocode} |
| 1657 |
\if_cs_exist:N ^^@ |
| 1658 |
\else: |
| 1659 |
\cs_new:Npn ^^@ |
| 1660 |
{ \msg_expandable_error:nn { kernel } { bad-exp-end-f } } |
| 1661 |
\fi: |
| 1662 |
% \end{macrocode} |
| 1663 |
% The same but grabbing an argument to remove spaces and braces. |
| 1664 |
% \begin{macrocode} |
| 1665 |
\cs_new:Npn \exp_end_continue_f:nw #1 { `^^@ #1 } |
| 1666 |
\group_end: |
| 1667 |
% \end{macrocode} |
| 1668 |
% \end{macro} |
| 1669 |
% \end{macro} |
| 1670 |
% \end{macro} |
| 1671 |
% \end{macro} |
| 1672 |
% |
| 1673 |
% \subsection{Defining function variants} |
| 1674 |
% |
| 1675 |
% \begin{macrocode} |
| 1676 |
%<@@=cs> |
| 1677 |
% \end{macrocode} |
| 1678 |
% |
| 1679 |
% \begin{variable}{\s_@@_mark,\s_@@_stop} |
| 1680 |
% Internal scan marks. No \pkg{l3quark} yet, so do things by hand. |
| 1681 |
% \begin{macrocode} |
| 1682 |
\cs_new_eq:NN \s_@@_mark \scan_stop: |
| 1683 |
\cs_new_eq:NN \s_@@_stop \scan_stop: |
| 1684 |
% \end{macrocode} |
| 1685 |
% \end{variable} |
| 1686 |
% |
| 1687 |
% \begin{variable}{\q_@@_recursion_stop} |
| 1688 |
% Internal recursion quarks. No \pkg{l3quark} yet, so do things by hand. |
| 1689 |
% \begin{macrocode} |
| 1690 |
\cs_new:Npn \q_@@_recursion_stop { \q_@@_recursion_stop } |
| 1691 |
% \end{macrocode} |
| 1692 |
% \end{variable} |
| 1693 |
% |
| 1694 |
% \begin{macro}[EXP]{ |
| 1695 |
% \@@_use_none_delimit_by_s_stop:w, |
| 1696 |
% \@@_use_i_delimit_by_s_stop:nw, |
| 1697 |
% \@@_use_none_delimit_by_q_recursion_stop:w |
| 1698 |
% } |
| 1699 |
% Internal scan marks. |
| 1700 |
% \begin{macrocode} |
| 1701 |
\cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { } |
| 1702 |
\cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1} |
| 1703 |
\cs_new:Npn \@@_use_none_delimit_by_q_recursion_stop:w |
| 1704 |
#1 \q_@@_recursion_stop { } |
| 1705 |
% \end{macrocode} |
| 1706 |
% \end{macro} |
| 1707 |
% |
| 1708 |
% \begin{macro}{\cs_generate_variant:Nn, \cs_generate_variant:cn} |
| 1709 |
% \begin{arguments} |
| 1710 |
% \item Base form of a function; \emph{e.g.},~\cs{tl_set:Nn} |
| 1711 |
% \item One or more variant argument specifiers; e.g., |{Nx,c,cx}| |
| 1712 |
% \end{arguments} |
| 1713 |
% After making sure that the base form exists, test whether it is |
| 1714 |
% protected or not and define \cs{@@_tmp:w} as either |
| 1715 |
% \cs{cs_new:Npe} or \cs{cs_new_protected:Npe}, which is |
| 1716 |
% then used to define all the variants (except those involving |
| 1717 |
% \texttt{x}-expansion, always protected). Split up the original base |
| 1718 |
% function only once, to grab its name and signature. Then we wish to |
| 1719 |
% iterate through the comma list of variant argument specifiers, which |
| 1720 |
% we first convert to a string: the reason is explained later. |
| 1721 |
% \begin{macrocode} |
| 1722 |
\cs_new_protected:Npn \cs_generate_variant:Nn #1#2 |
| 1723 |
{ |
| 1724 |
\@@_generate_variant:N #1 |
| 1725 |
\use:e |
| 1726 |
{ |
| 1727 |
\@@_generate_variant:nnNN |
| 1728 |
\cs_split_function:N #1 |
| 1729 |
\exp_not:N #1 |
| 1730 |
\tl_to_str:n {#2} , |
| 1731 |
\exp_not:N \scan_stop: , |
| 1732 |
\exp_not:N \q_@@_recursion_stop |
| 1733 |
} |
| 1734 |
} |
| 1735 |
\cs_new_protected:Npn \cs_generate_variant:cn |
| 1736 |
{ \exp_args:Nc \cs_generate_variant:Nn } |
| 1737 |
% \end{macrocode} |
| 1738 |
% \end{macro} |
| 1739 |
% |
| 1740 |
% \begin{macro}{\@@_generate_variant:N} |
| 1741 |
% \begin{macro}{\@@_generate_variant:ww, \@@_generate_variant:wwNw} |
| 1742 |
% The goal here is to pick up protected parent functions. There are |
| 1743 |
% four cases: the parent function can be a primitive or a macro, and |
| 1744 |
% can be expandable or not. For non-expandable primitives, all |
| 1745 |
% variants should be protected; skipping the \cs{else:} branch is safe |
| 1746 |
% because non-expandable primitives cannot be \TeX{} conditionals. |
| 1747 |
% |
| 1748 |
% The other case where variants should be protected is when the parent |
| 1749 |
% function is a protected macro: then |protected| appears in the |
| 1750 |
% meaning before the first occurrence of |macro|. The |ww| auxiliary |
| 1751 |
% removes everything in the meaning string after the first |ma|. We |
| 1752 |
% use |ma| rather than the full |macro| because the meaning of the |
| 1753 |
% \tn{firstmark} primitive (and four others) can contain an arbitrary |
| 1754 |
% string after a leading |firstmark:|. Then, look for |pr| in the |
| 1755 |
% part we extracted: no need to look for anything longer: the only |
| 1756 |
% strings we can have are an empty string, \verb*|\long |, |
| 1757 |
% \verb*|\protected |, \verb*|\protected\long |, |\first|, |\top|, |
| 1758 |
% |\bot|, |\splittop|, or |\splitbot|, with |\| replaced by the |
| 1759 |
% appropriate escape character. If |pr| appears in the part before |
| 1760 |
% |ma|, the first \cs{s_@@_mark} is taken as an argument of the |wwNw| |
| 1761 |
% auxiliary, and |#3| is \cs{cs_new_protected:Npe}, otherwise it |
| 1762 |
% is \cs{cs_new:Npe}. |
| 1763 |
% \begin{macrocode} |
| 1764 |
\cs_new_protected:Npe \@@_generate_variant:N #1 |
| 1765 |
{ |
| 1766 |
\exp_not:N \exp_after:wN \exp_not:N \if_meaning:w |
| 1767 |
\exp_not:N \exp_not:N #1 #1 |
| 1768 |
\cs_set_eq:NN \exp_not:N \@@_tmp:w \cs_new_protected:Npe |
| 1769 |
\exp_not:N \else: |
| 1770 |
\exp_not:N \exp_after:wN \exp_not:N \@@_generate_variant:ww |
| 1771 |
\exp_not:N \token_to_meaning:N #1 \tl_to_str:n { ma } |
| 1772 |
\s_@@_mark |
| 1773 |
\s_@@_mark \cs_new_protected:Npe |
| 1774 |
\tl_to_str:n { pr } |
| 1775 |
\s_@@_mark \cs_new:Npe |
| 1776 |
\s_@@_stop |
| 1777 |
\exp_not:N \fi: |
| 1778 |
} |
| 1779 |
\exp_last_unbraced:NNNNo |
| 1780 |
\cs_new_protected:Npn \@@_generate_variant:ww |
| 1781 |
#1 { \tl_to_str:n { ma } } #2 \s_@@_mark |
| 1782 |
{ \@@_generate_variant:wwNw #1 } |
| 1783 |
\exp_last_unbraced:NNNNo |
| 1784 |
\cs_new_protected:Npn \@@_generate_variant:wwNw |
| 1785 |
#1 { \tl_to_str:n { pr } } #2 \s_@@_mark #3 #4 \s_@@_stop |
| 1786 |
{ \cs_set_eq:NN \@@_tmp:w #3 } |
| 1787 |
% \end{macrocode} |
| 1788 |
% \end{macro} |
| 1789 |
% \end{macro} |
| 1790 |
% |
| 1791 |
% \begin{macro}{\@@_generate_variant:nnNN} |
| 1792 |
% \begin{arguments} |
| 1793 |
% \item Base name. |
| 1794 |
% \item Base signature. |
| 1795 |
% \item Boolean. |
| 1796 |
% \item Base function. |
| 1797 |
% \end{arguments} |
| 1798 |
% If the boolean is \cs{c_false_bool}, the base function has no colon |
| 1799 |
% and we abort with an error; otherwise, set off a loop through the |
| 1800 |
% desired variant forms. The original function is retained as |#4| for |
| 1801 |
% efficiency. |
| 1802 |
% \begin{macrocode} |
| 1803 |
\cs_new_protected:Npn \@@_generate_variant:nnNN #1#2#3#4 |
| 1804 |
{ |
| 1805 |
\if_meaning:w \c_false_bool #3 |
| 1806 |
\msg_error:nne { kernel } { missing-colon } |
| 1807 |
{ \token_to_str:c {#1} } |
| 1808 |
\exp_after:wN \@@_use_none_delimit_by_q_recursion_stop:w |
| 1809 |
\fi: |
| 1810 |
\@@_generate_variant:Nnnw #4 {#1}{#2} |
| 1811 |
} |
| 1812 |
% \end{macrocode} |
| 1813 |
% \end{macro} |
| 1814 |
% |
| 1815 |
% \begin{macro}{\@@_generate_variant:Nnnw} |
| 1816 |
% \begin{arguments} |
| 1817 |
% \item Base function. |
| 1818 |
% \item Base name. |
| 1819 |
% \item Base signature. |
| 1820 |
% \item Beginning of variant signature. |
| 1821 |
% \end{arguments} |
| 1822 |
% First check whether to terminate the loop over variant forms. Then, |
| 1823 |
% for each variant form, construct a new function name using the |
| 1824 |
% original base name, the variant signature consisting of $l$ letters |
| 1825 |
% and the last $k-l$ letters of the base signature (of length $k$). |
| 1826 |
% For example, for a base function \cs{prop_put:Nnn} which needs a |
| 1827 |
% |cV| variant form, we want the new signature to be |cVn|. |
| 1828 |
% |
| 1829 |
% There are further subtleties: |
| 1830 |
% \begin{itemize} |
| 1831 |
% \item In \cs{cs_generate_variant:Nn} |\foo:nnTF| |{xxTF}|, we must define |
| 1832 |
% |\foo:xxTF| using |\exp_args:Nxx|, |
| 1833 |
% rather than a hypothetical |\exp_args:NxxTF|. Thus, we wish to |
| 1834 |
% trim a common trailing part from the base signature and the |
| 1835 |
% variant signature. |
| 1836 |
% \item In \cs{cs_generate_variant:Nn} |\foo:on| |{ox}|, the |
| 1837 |
% function |\foo:ox| must be defined using |\exp_args:Nnx|, not |
| 1838 |
% |\exp_args:Nox|, to avoid double |o| expansion. |
| 1839 |
% \item Lastly, \cs{cs_generate_variant:Nn} |\foo:on| |{xn}| must |
| 1840 |
% trigger an error, because we do not have a means to replace |
| 1841 |
% |o|-expansion by |x|-expansion. |
| 1842 |
% More generally, we can only convert |N| to |c|, or convert |n| |
| 1843 |
% to |V|, |v|, |o|, |e|, |f|, or |x|. |
| 1844 |
% \end{itemize} |
| 1845 |
% All this boils down to a few rules. Only |n| and |N|-type |
| 1846 |
% arguments can be replaced by \cs{cs_generate_variant:Nn}. Other |
| 1847 |
% argument types are allowed to be passed unchanged from the base |
| 1848 |
% form to the variant: in the process they are changed to |n| |
| 1849 |
% except for |N| and |p|-type arguments. A common trailing |
| 1850 |
% part is ignored. |
| 1851 |
% |
| 1852 |
% We compare the base and variant signatures one character at a time |
| 1853 |
% within |e|-expansion. The result is given to |
| 1854 |
% \cs{@@_generate_variant:wwNN} (defined later) in the form |
| 1855 |
% \meta{processed variant signature} \cs{s_@@_mark} \meta{errors} |
| 1856 |
% \cs{s_@@_stop} \meta{base function} \meta{new function}. If all went |
| 1857 |
% well, \meta{errors} is empty; otherwise, it is a kernel error |
| 1858 |
% message and some clean-up code. |
| 1859 |
% |
| 1860 |
% Note the space after |#3| and after the following brace group. |
| 1861 |
% Those are ignored by \TeX{} when fetching the last argument for |
| 1862 |
% \cs{@@_generate_variant_loop:nNwN}, but can be used as a delimiter |
| 1863 |
% for \cs{@@_generate_variant_loop_end:nwwwNNnn}. |
| 1864 |
% \begin{macrocode} |
| 1865 |
\cs_new_protected:Npn \@@_generate_variant:Nnnw #1#2#3#4 , |
| 1866 |
{ |
| 1867 |
\if_meaning:w \scan_stop: #4 |
| 1868 |
\exp_after:wN \@@_use_none_delimit_by_q_recursion_stop:w |
| 1869 |
\fi: |
| 1870 |
\@@_generate_variant_chk:nnTF {#3} {#4} |
| 1871 |
{ |
| 1872 |
\msg_error:nne { kernel } { variant-same-as-base } |
| 1873 |
{ \token_to_str:N #1 } |
| 1874 |
\str_map_inline:nn {#4} |
| 1875 |
{ |
| 1876 |
\str_if_in:nnF { NnpcofeVvx } {##1} |
| 1877 |
{ |
| 1878 |
\msg_error:nneeee { kernel } { invalid-variant } |
| 1879 |
{#4} { \token_to_str:N #1 } {##1} {##1} |
| 1880 |
\str_map_break: |
| 1881 |
} |
| 1882 |
} |
| 1883 |
} |
| 1884 |
{ |
| 1885 |
\use:e |
| 1886 |
{ |
| 1887 |
\exp_not:N \@@_generate_variant:wwNN |
| 1888 |
\@@_generate_variant_loop:nNwN { } |
| 1889 |
#4 |
| 1890 |
\@@_generate_variant_loop_end:nwwwNNnn |
| 1891 |
\s_@@_mark |
| 1892 |
#3 ~ |
| 1893 |
{ ~ { } \fi: \@@_generate_variant_loop_long:wNNnn } ~ |
| 1894 |
{ } |
| 1895 |
\s_@@_stop |
| 1896 |
\exp_not:N #1 {#2} {#4} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
\@@_generate_variant:Nnnw #1 {#2} {#3} |
| 1900 |
} |
| 1901 |
% \end{macrocode} |
| 1902 |
% \end{macro} |
| 1903 |
% \begin{macro}{\@@_generate_variant_chk:nnTF} |
| 1904 |
% Simple auxiliary function that always returns false, for now at least. |
| 1905 |
% \begin{macrocode} |
| 1906 |
\cs_new:Npn \@@_generate_variant_chk:nnTF #1#2 { \use_ii:nn } |
| 1907 |
% \end{macrocode} |
| 1908 |
% \end{macro} |
| 1909 |
% |
| 1910 |
% \begin{macro}[rEXP] |
| 1911 |
% { |
| 1912 |
% \@@_generate_variant_loop:nNwN, |
| 1913 |
% \@@_generate_variant_loop_base:N, |
| 1914 |
% \@@_generate_variant_loop_same:w, |
| 1915 |
% \@@_generate_variant_loop_end:nwwwNNnn, |
| 1916 |
% \@@_generate_variant_loop_long:wNNnn, |
| 1917 |
% \@@_generate_variant_loop_invalid:NNwNNnn, |
| 1918 |
% \@@_generate_variant_loop_special:NNwNNnn |
| 1919 |
% } |
| 1920 |
% \begin{arguments} |
| 1921 |
% \item Last few consecutive letters common between the base and |
| 1922 |
% variant (more precisely, \cs{@@_generate_variant_same:N} |
| 1923 |
% \meta{letter} for each letter). |
| 1924 |
% \item Next variant letter. |
| 1925 |
% \item Remainder of variant form. |
| 1926 |
% \item Next base letter. |
| 1927 |
% \end{arguments} |
| 1928 |
% The first argument is populated by |
| 1929 |
% \cs{@@_generate_variant_loop_same:w} when a variant letter and a |
| 1930 |
% base letter match. It is flushed into the input stream whenever the |
| 1931 |
% two letters are different: if the loop ends before, the argument is |
| 1932 |
% dropped, which means that trailing common letters are ignored. |
| 1933 |
% |
| 1934 |
% The case where the two letters are different is only allowed if the |
| 1935 |
% base is |N| and the variant is |c|, or when the base is |n| and the |
| 1936 |
% variant is |V|, |v|, |o|, |e|, |f|, or |x|. Otherwise, call |
| 1937 |
% \cs{@@_generate_variant_loop_invalid:NNwNNnn} to remove the end of |
| 1938 |
% the loop, get arguments at the end of the loop, and place an |
| 1939 |
% appropriate error message as a second argument of |
| 1940 |
% \cs{@@_generate_variant:wwNN}. If the letters are distinct and the |
| 1941 |
% base letter is indeed |n| or |N|, leave in the input stream whatever |
| 1942 |
% argument |#1| was collected, and the next variant letter |#2|, then |
| 1943 |
% loop by calling \cs{@@_generate_variant_loop:nNwN}. |
| 1944 |
% |
| 1945 |
% The loop can stop in three ways. |
| 1946 |
% \begin{itemize} |
| 1947 |
% \item If the end of the variant form is encountered first, |#2| is |
| 1948 |
% \cs{@@_generate_variant_loop_end:nwwwNNnn} (expanded by the |
| 1949 |
% conditional \cs{if:w}), which inserts some tokens to end the |
| 1950 |
% conditional; grabs the \meta{base name} as |#7|, the |
| 1951 |
% \meta{variant signature} |#8|, the \meta{next base letter} |#1| |
| 1952 |
% and the part |#3| of the base signature that wasn't read yet; |
| 1953 |
% and combines those into the \meta{new function} to be defined. |
| 1954 |
% \item If the end of the base form is encountered first, |#4| is |
| 1955 |
% |~{}\fi:| which ends the conditional (with an empty expansion), |
| 1956 |
% followed by \cs{@@_generate_variant_loop_long:wNNnn}, which |
| 1957 |
% places an error as the second argument of |
| 1958 |
% \cs{@@_generate_variant:wwNN}. |
| 1959 |
% \item The loop can be interrupted early if the requested expansion |
| 1960 |
% is unavailable, namely when the variant and base letters differ |
| 1961 |
% and the base is not the right one (|n| or |N| to support the |
| 1962 |
% variant). In that case too an error is placed as the second |
| 1963 |
% argument of \cs{@@_generate_variant:wwNN}. |
| 1964 |
% \end{itemize} |
| 1965 |
% Note that if the variant form has the same length as the base form, |
| 1966 |
% |#2| is as described in the first point, and |#4| as described in |
| 1967 |
% the second point above. The \cs{@@_generate_variant_loop_end:nwwwNNnn} |
| 1968 |
% breaking function takes the empty brace group in |#4| as its first |
| 1969 |
% argument: this empty brace group produces the correct signature for |
| 1970 |
% the full variant. |
| 1971 |
% \begin{macrocode} |
| 1972 |
\cs_new:Npn \@@_generate_variant_loop:nNwN #1#2#3 \s_@@_mark #4 |
| 1973 |
{ |
| 1974 |
\if:w #2 #4 |
| 1975 |
\exp_after:wN \@@_generate_variant_loop_same:w |
| 1976 |
\else: |
| 1977 |
\if:w #4 \@@_generate_variant_loop_base:N #2 \else: |
| 1978 |
\if:w 0 |
| 1979 |
\if:w N #4 \else: \if:w n #4 \else: 1 \fi: \fi: |
| 1980 |
\if:w \scan_stop: \@@_generate_variant_loop_base:N #2 1 \fi: |
| 1981 |
0 |
| 1982 |
\@@_generate_variant_loop_special:NNwNNnn #4#2 |
| 1983 |
\else: |
| 1984 |
\@@_generate_variant_loop_invalid:NNwNNnn #4#2 |
| 1985 |
\fi: |
| 1986 |
\fi: |
| 1987 |
\fi: |
| 1988 |
#1 |
| 1989 |
\prg_do_nothing: |
| 1990 |
#2 |
| 1991 |
\@@_generate_variant_loop:nNwN { } #3 \s_@@_mark |
| 1992 |
} |
| 1993 |
\cs_new:Npn \@@_generate_variant_loop_base:N #1 |
| 1994 |
{ |
| 1995 |
\if:w c #1 N \else: |
| 1996 |
\if:w o #1 n \else: |
| 1997 |
\if:w V #1 n \else: |
| 1998 |
\if:w v #1 n \else: |
| 1999 |
\if:w f #1 n \else: |
| 2000 |
\if:w e #1 n \else: |
| 2001 |
\if:w x #1 n \else: |
| 2002 |
\if:w n #1 n \else: |
| 2003 |
\if:w N #1 N \else: |
| 2004 |
\scan_stop: |
| 2005 |
\fi: |
| 2006 |
\fi: |
| 2007 |
\fi: |
| 2008 |
\fi: |
| 2009 |
\fi: |
| 2010 |
\fi: |
| 2011 |
\fi: |
| 2012 |
\fi: |
| 2013 |
\fi: |
| 2014 |
} |
| 2015 |
\cs_new:Npn \@@_generate_variant_loop_same:w |
| 2016 |
#1 \prg_do_nothing: #2#3#4 |
| 2017 |
{ #3 { #1 \@@_generate_variant_same:N #2 } } |
| 2018 |
\cs_new:Npn \@@_generate_variant_loop_end:nwwwNNnn |
| 2019 |
#1#2 \s_@@_mark #3 ~ #4 \s_@@_stop #5#6#7#8 |
| 2020 |
{ |
| 2021 |
\scan_stop: \scan_stop: \fi: |
| 2022 |
\s_@@_mark \s_@@_stop |
| 2023 |
\exp_not:N #6 |
| 2024 |
\exp_not:c { #7 : #8 #1 #3 } |
| 2025 |
} |
| 2026 |
\cs_new:Npn \@@_generate_variant_loop_long:wNNnn #1 \s_@@_stop #2#3#4#5 |
| 2027 |
{ |
| 2028 |
\exp_not:n |
| 2029 |
{ |
| 2030 |
\s_@@_mark |
| 2031 |
\msg_error:nnee { kernel } { variant-too-long } |
| 2032 |
{#5} { \token_to_str:N #3 } |
| 2033 |
\use_none:nnn |
| 2034 |
\s_@@_stop |
| 2035 |
#3 |
| 2036 |
#3 |
| 2037 |
} |
| 2038 |
} |
| 2039 |
\cs_new:Npn \@@_generate_variant_loop_invalid:NNwNNnn |
| 2040 |
#1#2 \fi: \fi: \fi: #3 \s_@@_stop #4#5#6#7 |
| 2041 |
{ |
| 2042 |
\fi: \fi: \fi: |
| 2043 |
\exp_not:n |
| 2044 |
{ |
| 2045 |
\s_@@_mark |
| 2046 |
\msg_error:nneeee { kernel } { invalid-variant } |
| 2047 |
{#7} { \token_to_str:N #5 } {#1} {#2} |
| 2048 |
\use_none:nnn |
| 2049 |
\s_@@_stop |
| 2050 |
#5 |
| 2051 |
#5 |
| 2052 |
} |
| 2053 |
} |
| 2054 |
\cs_new:Npn \@@_generate_variant_loop_special:NNwNNnn |
| 2055 |
#1#2#3 \s_@@_stop #4#5#6#7 |
| 2056 |
{ |
| 2057 |
#3 \s_@@_stop #4 #5 {#6} {#7} |
| 2058 |
\exp_not:n |
| 2059 |
{ |
| 2060 |
\msg_error:nneeee |
| 2061 |
{ kernel } { deprecated-variant } |
| 2062 |
{#7} { \token_to_str:N #5 } {#1} {#2} |
| 2063 |
} |
| 2064 |
} |
| 2065 |
% \end{macrocode} |
| 2066 |
% \end{macro} |
| 2067 |
% |
| 2068 |
% \begin{macro}[rEXP]{\@@_generate_variant_same:N} |
| 2069 |
% When the base and variant letters are identical, don't do any |
| 2070 |
% expansion. For most argument types, we can use the |n|-type |
| 2071 |
% no-expansion, but the |N| and |p| types require a slightly different |
| 2072 |
% behavior with respect to braces. For |V|-type this function could |
| 2073 |
% output |N| to avoid adding useless braces but that is not a problem. |
| 2074 |
% \begin{macrocode} |
| 2075 |
\cs_new:Npn \@@_generate_variant_same:N #1 |
| 2076 |
{ |
| 2077 |
\if:w N #1 #1 \else: |
| 2078 |
\if:w p #1 #1 \else: |
| 2079 |
\token_to_str:N n |
| 2080 |
\if:w n #1 \else: |
| 2081 |
\@@_generate_variant_loop_special:NNwNNnn #1#1 |
| 2082 |
\fi: |
| 2083 |
\fi: |
| 2084 |
\fi: |
| 2085 |
} |
| 2086 |
% \end{macrocode} |
| 2087 |
% \end{macro} |
| 2088 |
% |
| 2089 |
% \begin{macro}{\@@_generate_variant:wwNN} |
| 2090 |
% If the variant form has already been defined, log its existence |
| 2091 |
% (provided \texttt{log-functions} is active). |
| 2092 |
% Otherwise, make sure that the |\exp_args:N #3| form is defined, and |
| 2093 |
% if it contains |x|, change \cs{@@_tmp:w} locally to |
| 2094 |
% \cs{cs_new_protected:Npe}. Then define the variant by |
| 2095 |
% combining the |\exp_args:N #3| variant and the base function. |
| 2096 |
% \begin{macrocode} |
| 2097 |
\cs_new_protected:Npn \@@_generate_variant:wwNN |
| 2098 |
#1 \s_@@_mark #2 \s_@@_stop #3#4 |
| 2099 |
{ |
| 2100 |
#2 |
| 2101 |
\use_ii:nn #4 |
| 2102 |
{ |
| 2103 |
\if_meaning:w #3 #4 |
| 2104 |
\msg_error:nne { kernel } { variant-same-as-base } |
| 2105 |
{ \token_to_str:N #3 } |
| 2106 |
\else: |
| 2107 |
\cs_if_free:NT #4 |
| 2108 |
{ |
| 2109 |
\group_begin: |
| 2110 |
\@@_generate_internal_variant:n {#1} |
| 2111 |
\@@_tmp:w #4 { \exp_not:c { exp_args:N #1 } \exp_not:N #3 } |
| 2112 |
\group_end: |
| 2113 |
} |
| 2114 |
\fi: |
| 2115 |
} |
| 2116 |
} |
| 2117 |
% \end{macrocode} |
| 2118 |
% \end{macro} |
| 2119 |
% |
| 2120 |
% \begin{macro}{\@@_generate_internal_variant:n} |
| 2121 |
% \begin{macro}[rEXP]{\@@_generate_internal_variant_loop:n} |
| 2122 |
% First test for the presence of |x| (this is where working with |
| 2123 |
% strings makes our lives easier), as the result should be protected, |
| 2124 |
% and the next variant to be defined using that internal variant |
| 2125 |
% should be protected (done by setting \cs{@@_tmp:w}). Then call |
| 2126 |
% \cs{@@_generate_internal_variant:NNn} with arguments |
| 2127 |
% \cs{cs_new_protected:cpn} \cs{use:x} (for protected) or |
| 2128 |
% \cs{cs_new:cpn} \cs{tex_expanded:D} (expandable) and the signature. If |p| |
| 2129 |
% appears in the signature, or if the function to be defined is |
| 2130 |
% expandable and the primitive \tn{expanded} is not available, or if there |
| 2131 |
% are more than $8$ arguments, call |
| 2132 |
% some fall-back code that just puts the appropriate |\::| commands. |
| 2133 |
% Otherwise, call \cs{@@_generate_internal_one_go:NNn} to construct |
| 2134 |
% the \cs[no-index]{exp_args:N\dots} function as a macro taking up to |
| 2135 |
% $9$~arguments and expanding them using \cs{use:x} or \cs{tex_expanded:D}. |
| 2136 |
% \begin{macrocode} |
| 2137 |
\cs_new_protected:Npe \@@_generate_internal_variant:n #1 |
| 2138 |
{ |
| 2139 |
\exp_not:N \@@_generate_internal_variant:wwnNwn |
| 2140 |
#1 \s_@@_mark |
| 2141 |
{ \cs_set_eq:NN \exp_not:N \@@_tmp:w \cs_new_protected:Npe } |
| 2142 |
\cs_new_protected:cpn |
| 2143 |
\use:x |
| 2144 |
\token_to_str:N x \s_@@_mark |
| 2145 |
{ } |
| 2146 |
\cs_new:cpn |
| 2147 |
\exp_not:N \tex_expanded:D |
| 2148 |
\s_@@_stop |
| 2149 |
{#1} |
| 2150 |
} |
| 2151 |
\exp_last_unbraced:NNNNo |
| 2152 |
\cs_new_protected:Npn \@@_generate_internal_variant:wwnNwn #1 |
| 2153 |
{ \token_to_str:N x } #2 \s_@@_mark #3#4#5#6 \s_@@_stop #7 |
| 2154 |
{ |
| 2155 |
#3 |
| 2156 |
\cs_if_free:cT { exp_args:N #7 } |
| 2157 |
{ \@@_generate_internal_variant:NNn #4 #5 {#7} } |
| 2158 |
} |
| 2159 |
\cs_set_protected:Npn \@@_tmp:w #1 |
| 2160 |
{ |
| 2161 |
\cs_new_protected:Npn \@@_generate_internal_variant:NNn ##1##2##3 |
| 2162 |
{ |
| 2163 |
\if_catcode:w X \use_none:nnnnnnnn ##3 |
| 2164 |
\prg_do_nothing: \prg_do_nothing: \prg_do_nothing: |
| 2165 |
\prg_do_nothing: \prg_do_nothing: \prg_do_nothing: |
| 2166 |
\prg_do_nothing: \prg_do_nothing: X |
| 2167 |
\exp_after:wN \@@_generate_internal_test:Nw \exp_after:wN ##2 |
| 2168 |
\else: |
| 2169 |
\exp_after:wN \@@_generate_internal_test_aux:w \exp_after:wN #1 |
| 2170 |
\fi: |
| 2171 |
##3 |
| 2172 |
\s_@@_mark |
| 2173 |
{ |
| 2174 |
\use:e |
| 2175 |
{ |
| 2176 |
##1 { exp_args:N ##3 } |
| 2177 |
{ \@@_generate_internal_variant_loop:n ##3 { : \use_i:nn } } |
| 2178 |
} |
| 2179 |
} |
| 2180 |
#1 |
| 2181 |
\s_@@_mark |
| 2182 |
{ \exp_not:n { \@@_generate_internal_one_go:NNn ##1 ##2 {##3} } } |
| 2183 |
\s_@@_stop |
| 2184 |
} |
| 2185 |
\cs_new_protected:Npn \@@_generate_internal_test_aux:w |
| 2186 |
##1 #1 ##2 \s_@@_mark ##3 ##4 \s_@@_stop {##3} |
| 2187 |
\cs_new_eq:NN \@@_generate_internal_test:Nw |
| 2188 |
\@@_generate_internal_test_aux:w |
| 2189 |
} |
| 2190 |
\exp_args:No \@@_tmp:w { \token_to_str:N p } |
| 2191 |
\cs_new_protected:Npn \@@_generate_internal_one_go:NNn #1#2#3 |
| 2192 |
{ |
| 2193 |
\@@_generate_internal_loop:nwnnw |
| 2194 |
{ \exp_not:N ##1 } 1 . { } { } |
| 2195 |
#3 { ? \@@_generate_internal_end:w } X ; |
| 2196 |
23456789 { ? \@@_generate_internal_long:w } ; |
| 2197 |
#1 #2 {#3} |
| 2198 |
} |
| 2199 |
\cs_new_protected:Npn \@@_generate_internal_loop:nwnnw #1#2 . #3#4#5#6 ; #7 |
| 2200 |
{ |
| 2201 |
\use_none:n #5 |
| 2202 |
\use_none:n #7 |
| 2203 |
\cs_if_exist_use:cF { @@_generate_internal_#5:NN } |
| 2204 |
{ \@@_generate_internal_other:NN } |
| 2205 |
#5 #7 |
| 2206 |
#7 . |
| 2207 |
{ #3 #1 } { #4 ## #2 } |
| 2208 |
#6 ; |
| 2209 |
} |
| 2210 |
\cs_new_protected:Npn \@@_generate_internal_N:NN #1#2 |
| 2211 |
{ \@@_generate_internal_loop:nwnnw { \exp_not:N ###2 } } |
| 2212 |
\cs_new_protected:Npn \@@_generate_internal_c:NN #1#2 |
| 2213 |
{ \exp_args:No \@@_generate_internal_loop:nwnnw { \exp_not:c {###2} } } |
| 2214 |
\cs_new_protected:Npn \@@_generate_internal_n:NN #1#2 |
| 2215 |
{ \@@_generate_internal_loop:nwnnw { { \exp_not:n {###2} } } } |
| 2216 |
\cs_new_protected:Npn \@@_generate_internal_x:NN #1#2 |
| 2217 |
{ \@@_generate_internal_loop:nwnnw { {###2} } } |
| 2218 |
\cs_new_protected:Npn \@@_generate_internal_other:NN #1#2 |
| 2219 |
{ |
| 2220 |
\exp_args:No \@@_generate_internal_loop:nwnnw |
| 2221 |
{ |
| 2222 |
\exp_after:wN |
| 2223 |
{ |
| 2224 |
\exp:w \exp_args:NNc \exp_after:wN \exp_end: |
| 2225 |
{ exp_not:#1 } {###2} |
| 2226 |
} |
| 2227 |
} |
| 2228 |
} |
| 2229 |
\cs_new_protected:Npn \@@_generate_internal_end:w #1 . #2#3#4 ; #5 ; #6#7#8 |
| 2230 |
{ #6 { exp_args:N #8 } #3 { #7 {#2} } } |
| 2231 |
\cs_new_protected:Npn \@@_generate_internal_long:w #1 N #2#3 . #4#5#6# |
| 2232 |
{ |
| 2233 |
\exp_args:Nx \@@_generate_internal_long:nnnNNn |
| 2234 |
{ \@@_generate_internal_variant_loop:n #2 #6 { : \use_i:nn } } |
| 2235 |
{#4} {#5} |
| 2236 |
} |
| 2237 |
\cs_new:Npn \@@_generate_internal_long:nnnNNn #1#2#3#4 ; ; #5#6#7 |
| 2238 |
{ #5 { exp_args:N #7 } #3 { #6 { \exp_not:n {#1} {#2} } } } |
| 2239 |
% \end{macrocode} |
| 2240 |
% This command grabs char by char outputting |\::#1| (not expanded |
| 2241 |
% further). We avoid tests by putting a trailing |: \use_i:nn|, which |
| 2242 |
% leaves \cs{cs_end:} and removes the looping macro. The colon is in |
| 2243 |
% fact also turned into \cs{:::} so that the required structure for |
| 2244 |
% |\exp_args:N...| commands is correctly terminated. |
| 2245 |
% \begin{macrocode} |
| 2246 |
\cs_new:Npn \@@_generate_internal_variant_loop:n #1 |
| 2247 |
{ |
| 2248 |
\exp_after:wN \exp_not:N \cs:w :: #1 \cs_end: |
| 2249 |
\@@_generate_internal_variant_loop:n |
| 2250 |
} |
| 2251 |
% \end{macrocode} |
| 2252 |
% \end{macro} |
| 2253 |
% \end{macro} |
| 2254 |
% |
| 2255 |
% \begin{macro} |
| 2256 |
% { |
| 2257 |
% \prg_generate_conditional_variant:Nnn, |
| 2258 |
% \@@_generate_variant:nnNnn, |
| 2259 |
% \@@_generate_variant:w, |
| 2260 |
% \@@_generate_variant:n, |
| 2261 |
% \@@_generate_variant_p_form:nnn, |
| 2262 |
% \@@_generate_variant_T_form:nnn, |
| 2263 |
% \@@_generate_variant_F_form:nnn, |
| 2264 |
% \@@_generate_variant_TF_form:nnn, |
| 2265 |
% \@@_generate_variant_check:nn |
| 2266 |
% } |
| 2267 |
% \begin{macrocode} |
| 2268 |
\cs_new_protected:Npn \prg_generate_conditional_variant:Nnn #1 |
| 2269 |
{ |
| 2270 |
\use:e |
| 2271 |
{ |
| 2272 |
\@@_generate_variant:nnNnn |
| 2273 |
\cs_split_function:N #1 |
| 2274 |
} |
| 2275 |
} |
| 2276 |
\cs_new_protected:Npn \@@_generate_variant:nnNnn #1#2#3#4#5 |
| 2277 |
{ |
| 2278 |
\if_meaning:w \c_false_bool #3 |
| 2279 |
\msg_error:nne { kernel } { missing-colon } |
| 2280 |
{ \token_to_str:c {#1} } |
| 2281 |
\@@_use_i_delimit_by_s_stop:nw |
| 2282 |
\fi: |
| 2283 |
\exp_after:wN \@@_generate_variant:w |
| 2284 |
\tl_to_str:n {#5} , \scan_stop: , \q_@@_recursion_stop |
| 2285 |
\@@_use_none_delimit_by_s_stop:w \s_@@_mark {#1} {#2} {#4} \s_@@_stop |
| 2286 |
} |
| 2287 |
\cs_new_protected:Npn \@@_generate_variant:w |
| 2288 |
#1 , #2 \s_@@_mark #3#4#5 |
| 2289 |
{ |
| 2290 |
\if_meaning:w \scan_stop: #1 \scan_stop: |
| 2291 |
\if_meaning:w \q_@@_nil #1 \q_@@_nil |
| 2292 |
\use_i:nnn |
| 2293 |
\fi: |
| 2294 |
\exp_after:wN \@@_use_none_delimit_by_q_recursion_stop:w |
| 2295 |
\else: |
| 2296 |
\cs_if_exist_use:cTF { @@_generate_variant_#1_form:nnn } |
| 2297 |
{ {#3} {#4} {#5} } |
| 2298 |
{ |
| 2299 |
\msg_error:nnee |
| 2300 |
{ kernel } { conditional-form-unknown } |
| 2301 |
{#1} { \token_to_str:c { #3 : #4 } } |
| 2302 |
} |
| 2303 |
\fi: |
| 2304 |
\@@_generate_variant:w #2 \s_@@_mark {#3} {#4} {#5} |
| 2305 |
} |
| 2306 |
\cs_new_protected:Npn \@@_generate_variant_p_form:nnn #1#2 |
| 2307 |
{ \@@_generate_variant_check:nn { #1 _p : #2 } } |
| 2308 |
\cs_new_protected:Npn \@@_generate_variant_T_form:nnn #1#2 |
| 2309 |
{ \@@_generate_variant_check:nn { #1 : #2 T } } |
| 2310 |
\cs_new_protected:Npn \@@_generate_variant_F_form:nnn #1#2 |
| 2311 |
{ \@@_generate_variant_check:nn { #1 : #2 F } } |
| 2312 |
\cs_new_protected:Npn \@@_generate_variant_TF_form:nnn #1#2 |
| 2313 |
{ \@@_generate_variant_check:nn { #1 : #2 TF } } |
| 2314 |
\cs_new_protected:Npn \@@_generate_variant_check:nn #1#2 |
| 2315 |
{ |
| 2316 |
\cs_if_exist:cTF {#1} |
| 2317 |
{ \cs_generate_variant:cn {#1} {#2} } |
| 2318 |
{ |
| 2319 |
\msg_error:nne |
| 2320 |
{ kernel } { conditional-base-undefined } |
| 2321 |
{ \token_to_str:c {#1} } |
| 2322 |
} |
| 2323 |
} |
| 2324 |
% \end{macrocode} |
| 2325 |
% \end{macro} |
| 2326 |
% |
| 2327 |
% \begin{macro}{\exp_args_generate:n} |
| 2328 |
% This function is not used in the kernel hence we can use functions |
| 2329 |
% that are defined in later modules. It also does not need to be fast |
| 2330 |
% so use inline mappings. For each requested variant we check that |
| 2331 |
% there are no characters besides |NnpcofVvx|, in particular that |
| 2332 |
% there are no spaces. Then we just call the internal function. |
| 2333 |
% \begin{macrocode} |
| 2334 |
\cs_new_protected:Npn \exp_args_generate:n #1 |
| 2335 |
{ |
| 2336 |
\exp_args:No \clist_map_inline:nn { \tl_to_str:n {#1} } |
| 2337 |
{ |
| 2338 |
\str_map_inline:nn {##1} |
| 2339 |
{ |
| 2340 |
\str_if_in:nnF { NnpcofeVvx } {####1} |
| 2341 |
{ |
| 2342 |
\msg_error:nnnn { kernel } { invalid-exp-args } |
| 2343 |
{####1} {##1} |
| 2344 |
\str_map_break:n { \use_none:nn } |
| 2345 |
} |
| 2346 |
} |
| 2347 |
\@@_generate_internal_variant:n {##1} |
| 2348 |
} |
| 2349 |
} |
| 2350 |
% \end{macrocode} |
| 2351 |
% \end{macro} |
| 2352 |
% |
| 2353 |
% \subsection{Definitions with the automated technique} |
| 2354 |
% \label{sec:l3expan:gendef} |
| 2355 |
% |
| 2356 |
% Some of these could be done more efficiently, but the complexity of |
| 2357 |
% coding then becomes an issue. Notice that the auto-generated functions |
| 2358 |
% actually take no arguments themselves. |
| 2359 |
% |
| 2360 |
% \begin{macro}[EXP] |
| 2361 |
% { |
| 2362 |
% \exp_args:Nnc, \exp_args:Nno, \exp_args:NnV, \exp_args:Nnv, \exp_args:Nne, \exp_args:Nnf, |
| 2363 |
% \exp_args:Noc, \exp_args:Noo, \exp_args:Nof, |
| 2364 |
% \exp_args:NVo, \exp_args:Nfo, \exp_args:Nff, \exp_args:Nee |
| 2365 |
% } |
| 2366 |
% \begin{macro} |
| 2367 |
% { |
| 2368 |
% \exp_args:NNx, \exp_args:Ncx, \exp_args:Nnx, |
| 2369 |
% \exp_args:Nox, \exp_args:Nxo, \exp_args:Nxx, |
| 2370 |
% } |
| 2371 |
% Here are the actual function definitions, using the helper |
| 2372 |
% functions above. The group is used because |
| 2373 |
% \cs{@@_generate_internal_variant:n} redefines \cs{@@_tmp:w} locally. |
| 2374 |
% \begin{macrocode} |
| 2375 |
\cs_set_protected:Npn \@@_tmp:w #1 |
| 2376 |
{ |
| 2377 |
\group_begin: |
| 2378 |
\exp_args:No \@@_generate_internal_variant:n |
| 2379 |
{ \tl_to_str:n {#1} } |
| 2380 |
\group_end: |
| 2381 |
} |
| 2382 |
\@@_tmp:w { nc } |
| 2383 |
\@@_tmp:w { no } |
| 2384 |
\@@_tmp:w { nV } |
| 2385 |
\@@_tmp:w { nv } |
| 2386 |
\@@_tmp:w { ne } |
| 2387 |
\@@_tmp:w { nf } |
| 2388 |
\@@_tmp:w { oc } |
| 2389 |
\@@_tmp:w { oo } |
| 2390 |
\@@_tmp:w { of } |
| 2391 |
\@@_tmp:w { Vo } |
| 2392 |
\@@_tmp:w { fo } |
| 2393 |
\@@_tmp:w { ff } |
| 2394 |
\@@_tmp:w { ee } |
| 2395 |
\@@_tmp:w { Nx } |
| 2396 |
\@@_tmp:w { cx } |
| 2397 |
\@@_tmp:w { nx } |
| 2398 |
\@@_tmp:w { ox } |
| 2399 |
\@@_tmp:w { xo } |
| 2400 |
\@@_tmp:w { xx } |
| 2401 |
% \end{macrocode} |
| 2402 |
% \end{macro} |
| 2403 |
% \end{macro} |
| 2404 |
% |
| 2405 |
% \begin{macro}[EXP] |
| 2406 |
% { |
| 2407 |
% \exp_args:NNcf, |
| 2408 |
% \exp_args:NNno, \exp_args:NNnV, \exp_args:NNoo, \exp_args:NNVV, |
| 2409 |
% \exp_args:Ncno, \exp_args:NcnV, \exp_args:Ncoo, \exp_args:NcVV, |
| 2410 |
% \exp_args:Nnnc, \exp_args:Nnno, \exp_args:Nnnf, \exp_args:Nnff, |
| 2411 |
% \exp_args:Nooo, \exp_args:Noof, \exp_args:Nffo, \exp_args:Neee |
| 2412 |
% } |
| 2413 |
% \begin{macro} |
| 2414 |
% { |
| 2415 |
% \exp_args:NNNx, \exp_args:NNnx, \exp_args:NNox, |
| 2416 |
% \exp_args:Nccx, \exp_args:Ncnx, |
| 2417 |
% \exp_args:Nnnx, \exp_args:Nnox, \exp_args:Noox, |
| 2418 |
% } |
| 2419 |
% \begin{macrocode} |
| 2420 |
\@@_tmp:w { Ncf } |
| 2421 |
\@@_tmp:w { Nno } |
| 2422 |
\@@_tmp:w { NnV } |
| 2423 |
\@@_tmp:w { Noo } |
| 2424 |
\@@_tmp:w { NVV } |
| 2425 |
\@@_tmp:w { cno } |
| 2426 |
\@@_tmp:w { cnV } |
| 2427 |
\@@_tmp:w { coo } |
| 2428 |
\@@_tmp:w { cVV } |
| 2429 |
\@@_tmp:w { nnc } |
| 2430 |
\@@_tmp:w { nno } |
| 2431 |
\@@_tmp:w { nnf } |
| 2432 |
\@@_tmp:w { nff } |
| 2433 |
\@@_tmp:w { ooo } |
| 2434 |
\@@_tmp:w { oof } |
| 2435 |
\@@_tmp:w { ffo } |
| 2436 |
\@@_tmp:w { eee } |
| 2437 |
\@@_tmp:w { NNx } |
| 2438 |
\@@_tmp:w { Nnx } |
| 2439 |
\@@_tmp:w { Nox } |
| 2440 |
\@@_tmp:w { nnx } |
| 2441 |
\@@_tmp:w { nox } |
| 2442 |
\@@_tmp:w { ccx } |
| 2443 |
\@@_tmp:w { cnx } |
| 2444 |
\@@_tmp:w { oox } |
| 2445 |
% \end{macrocode} |
| 2446 |
% \end{macro} |
| 2447 |
% \end{macro} |
| 2448 |
% |
| 2449 |
% \subsection{Held-over variant generation} |
| 2450 |
% |
| 2451 |
% \begin{macro}[documented-as = \cs_generate_from_arg_count:NNnn] |
| 2452 |
% {\cs_generate_from_arg_count:NNno} |
| 2453 |
% \begin{macro}[documented-as = \cs_replacement_spec:N]{\cs_replacement_spec:c} |
| 2454 |
% \begin{macro}[EXP, documented-as = \debug_assert:nNn]{\debug_assert:nNe} |
| 2455 |
% \begin{macro}[EXP, documented-as = \debug_assert:nnn]{\debug_assert:nne} |
| 2456 |
% A couple of variants that are from early functions. |
| 2457 |
% \begin{macrocode} |
| 2458 |
\cs_generate_variant:Nn \cs_generate_from_arg_count:NNnn { NNno } |
| 2459 |
\cs_generate_variant:Nn \cs_replacement_spec:N { c } |
| 2460 |
\cs_generate_variant:Nn \debug_assert:nNn { nNe } |
| 2461 |
\cs_generate_variant:Nn \debug_assert:nnn { nne } |
| 2462 |
% \end{macrocode} |
| 2463 |
% \end{macro} |
| 2464 |
% \end{macro} |
| 2465 |
% \end{macro} |
| 2466 |
% \end{macro} |
| 2467 |
% |
| 2468 |
% \begin{macrocode} |
| 2469 |
%</code> |
| 2470 |
% \end{macrocode} |
| 2471 |
% |
| 2472 |
% \end{implementation} |
| 2473 |
% |
| 2474 |
% \PrintIndex |