Debian Codegraph @

texmf-dist/source/latex-dev/base/ltcmd.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 1999 Frank Mittelbach, Chris Rowley, David Carlisle
4 % Copyright (C) 2004-2008 Frank Mittelbach, The LaTeX Project
5 % Copyright (C) 2009-2026
6 % The LaTeX Project and any individual authors listed elsewhere
7 % in this file.
8 %
9 % This file is part of the LaTeX base system.
10 % -------------------------------------------
11 %
12 % It may be distributed and/or modified under the
13 % conditions of the LaTeX Project Public License, either version 1.3c
14 % of this license or (at your option) any later version.
15 % The latest version of this license is in
16 % https://www.latex-project.org/lppl.txt
17 % and version 1.3c or later is part of all distributions of LaTeX
18 % version 2008 or later.
19 %
20 % This file has the LPPL maintenance status "maintained".
21 %
22 % The list of all files belonging to the LaTeX base distribution is
23 % given in the file `manifest.txt'. See also `legal.txt' for additional
24 % information.
25 %
26 % The list of derived (unpacked) files belonging to the distribution
27 % and covered by LPPL is defined by the unpacking scripts (with
28 % extension .ins) which are part of the distribution.
29 %
30 % \fi
31 %
32 % \iffalse
33 %
34 %%% From File: ltcmd.dtx
35 %
36 % \begin{macrocode}
37 \def\ltcmdversion{v1.3l}
38 \def\ltcmddate{2026-04-26}
39 % \end{macrocode}
40 %
41 %<*driver>
42 % \fi
43 \ProvidesFile{ltcmd.dtx}
44 [\ltcmddate\space \ltcmdversion\space
45 LaTeX Kernel (Document commands)]
46 % \iffalse
47 \documentclass{l3in2edoc}
48 \GetFileInfo{ltcmd.dtx}
49 \title{\filename}
50 \date{\filedate}
51 \author{Frank Mittelbach, Chris Rowley, David Carlisle, \LaTeX{} Project Team}
52 \begin{document}
53 \maketitle
54 \DocInput{ltcmd.dtx}
55 \end{document}
56 %</driver>
57 % \fi
58 %
59 % \section{Creating document commands}
60 %
61 % \changes{v1.0a}{2020/11/20}{Initial version derived from \texttt{xparse.dtx}}
62 % \changes{v1.2a}{2023/08/19}{Removed commands that should have remained
63 % only in \texttt{xparse.dtx}}
64 % \changes{v1.3j}{2026-02-3}{Add documentation as part of \texttt{cmdguide}
65 % work}
66 %
67 % \subsection{Overview}
68 %
69 % Creating document commands and environments using the \LaTeX3 toolset is based
70 % around the idea that a common set of descriptions can be used to cover almost
71 % all argument types used in real documents. Thus parsing is reduced to a simple
72 % description of which arguments a command takes: this description provides the
73 % \enquote{glue} between the document syntax and the implementation of the
74 % command.
75 %
76 % First, we will describe the argument types, then move on to explain how these
77 % can be used to create both document commands and environments. Various more
78 % specialized features are then described, which allow an even richer application
79 % of a simple interface set up.
80 %
81 % The details here are intended to help users create document commands in
82 % general. More technical detail, suitable for \TeX{} programmers, is included
83 % in \texttt{interface3}.
84 %
85 % \subsection{Describing argument types}
86 %
87 % In order to allow each argument to be defined independently, the parser does
88 % not simply need to know the number of arguments for a function, but also the
89 % nature of each one. This is done by constructing an \emph{argument
90 % specification}, which defines the number of arguments, the type of each
91 % argument and any additional information needed for the parser to read the user
92 % input and properly pass it through to internal functions.
93 %
94 % The basic form of the argument specifier is a list of letters, where each
95 % letter defines a type of argument. As will be described below, some of the
96 % types need additional information, such as default values. The argument types
97 % can be divided into two, those which define arguments that are mandatory
98 % (potentially raising an error if not found) and those which define optional
99 % arguments. The mandatory types
100 % \begin{itemize}
101 % \item[\texttt{m}] A standard mandatory argument, which can either be a single token
102 % alone or multiple tokens surrounded by curly braces |{}|. Regardless of the
103 % input, the argument will be passed to the internal code without the outer
104 % braces. This is the type specifier for a normal \TeX{} argument.
105 % \item[\texttt{r}] Given as \texttt{r}\meta{token1}\meta{token2}, this denotes a
106 % \enquote{required} delimited argument, where the delimiters are
107 % \meta{token1} and \meta{token2}. If the opening delimiter \meta{token1} is
108 % missing, the default marker \cs{NoValue} will be inserted after a suitable
109 % error.
110 % \item[\texttt{R}] Given as \texttt{R}\meta{token1}\meta{token2}\marg{default},
111 % this is a \enquote{required} delimited argument as for~\texttt{r},
112 % but it has a user-definable recovery \meta{default} instead of
113 % \cs{NoValue}.
114 % \item[\texttt{v}] Reads an argument \enquote{verbatim}, between the following
115 % character and its next occurrence, in a way similar to the argument
116 % of the \LaTeXe{} command \cs{verb}. Thus a \texttt{v}-type argument
117 % is read between two identical characters, which cannot be any of |%|, |\|,
118 % |#|, |{|, |}| or \verb*| |. The verbatim argument can also be enclosed
119 % between braces, |{| and |}|. A command with a verbatim argument will
120 % produce an error when it appears within an argument of another command.
121 % \item[\texttt{b}] Only suitable in the argument specification of an
122 % environment, it denotes the body of the environment, between
123 % |\begin|\marg{environment} and |\end|\marg{environment}. See
124 % Section~\ref{sec:cmd:body} for details.
125 % \item[\texttt{c}] Only suitable in the argument specification of an
126 % environment, it denotes collection of the environment verbatim, between
127 % |\begin|\marg{environment} and |\end|\marg{environment}. See
128 % Section~\ref{sec:cmd:verbenv} for details.
129 % \end{itemize}
130 % The types which define optional arguments are:
131 % \begin{itemize}
132 % \item[\texttt{o}] A standard \LaTeX{} optional argument, surrounded with square
133 % brackets, which will supply the special \cs{NoValue} marker if not given
134 % (as described later).
135 % \item[\texttt{d}] Given as \texttt{d}\meta{token1}\meta{token2}, an optional
136 % argument which is delimited by \meta{token1} and \meta{token2}. As with
137 % \texttt{o}, if no value is given the special marker \cs{NoValue} is
138 % returned.
139 % \item[\texttt{O}] Given as \texttt{O}\marg{default}, is like \texttt{o}, but returns
140 % \meta{default} if no value is given.
141 % \item[\texttt{D}] Given as \texttt{D}\meta{token1}\meta{token2}\marg{default}, it is
142 % as for \texttt{d}, but returns \meta{default} if no value is given.
143 % Internally, the \texttt{o}, \texttt{d} and \texttt{O} types are
144 % short-cuts to an appropriated-constructed \texttt{D} type argument.
145 % \item[\texttt{s}] An optional star, which will result in a value
146 % \cs{BooleanTrue} if a star is present and \cs{BooleanFalse}
147 % otherwise (as described later).
148 % \item[\texttt{t}] An optional \meta{token}, which will result in a value
149 % \cs{BooleanTrue} if \meta{token} is present and \cs{BooleanFalse}
150 % otherwise. Given as \texttt{t}\meta{token}.
151 % \item[\texttt{e}] Given as \texttt{e}\marg{tokens}, a set of optional
152 % \emph{embellishments}, each of which requires a \emph{value}.
153 % If an embellishment is not present, \cs{NoValue} is returned. Each
154 % embellishment gives one argument, ordered as for the list of
155 % \meta{tokens} in the argument specification. All \meta{tokens}
156 % must be distinct.
157 % \item[\texttt{E}] As for \texttt{e} but returns one or more \meta{defaults}
158 % if values are not given: \texttt{E}\marg{tokens}\marg{defaults}. See
159 % Section~\ref{sec:cmd:embellishment} for more details.
160 % \end{itemize}
161 %
162 % \subsection{Modifying argument descriptions}
163 %
164 % In addition to the argument \emph{types} discussed above, the argument
165 % description also gives special meaning to three other characters.
166 %
167 % First, \texttt{+} is used to make an argument long (to accept paragraph
168 % tokens). In contrast to \cs{newcommand}, this applies on an
169 % argument-by-argument basis. So modifying the example to
170 % \enquote{\texttt{s o o +m O{default}}}
171 % means that the mandatory argument is now \cs{long}, whereas the optional
172 % arguments are not.
173 %
174 % Secondly, \texttt{!} is used to control whether spaces are allowed before
175 % optional arguments. There are some subtleties to this, as \TeX{} itself
176 % has some restrictions on where spaces can be \enquote{detected}: more detail
177 % is given in Section~\ref{sec:cmd:opt-space}.
178 %
179 % Thirdly, \texttt{=} is used to declare that the following argument should
180 % be interpreted as a series of keyvals. See Section~\ref{sec:cmd:keyval}
181 % for more details.
182 %
183 % Finally, the character \texttt{>} is used to declare so-called
184 % `argument processors', which can be used to modify the contents of an
185 % argument before it is passed to the macro definition. The use of argument
186 % processors is a somewhat advanced topic, (or at least a less commonly used
187 % feature) and is covered in Section~\ref{sec:cmd:processors}.
188 %
189 % \subsection{Creating document commands and environments}
190 %
191 % \begin{function}
192 % {
193 % \NewDocumentCommand,
194 % \RenewDocumentCommand,
195 % \ProvideDocumentCommand,
196 % \DeclareDocumentCommand
197 % }
198 % \begin{syntax}
199 % \cs{NewDocumentCommand} \Arg{cmd} \Arg{arg spec} \Arg{code}
200 % \end{syntax}
201 % \end{function}
202 % This family of commands are used to create a \meta{cmd}. The argument
203 % specification for the function is given by \meta{arg spec}, and the command
204 % uses the \meta{code} with |#1|, |#2|, etc.\ replaced by the arguments found
205 % by the parser.
206 %
207 % An example:
208 % \begin{Verbatim}[gobble = 3]
209 % \NewDocumentCommand\chapter{s o m}
210 % {%
211 % \IfBooleanTF{#1}%
212 % {\typesetstarchapter{#3}}%
213 % {\typesetnormalchapter{#2}{#3}}%
214 % }
215 % \end{Verbatim}
216 % would be a way to define a \cs{chapter} command which would essentially behave
217 % like the current \LaTeXe{} command (except that it would accept an optional
218 % argument even when a \texttt{*} was parsed). The \cs{typesetnormalchapter}
219 % could test its first argument for being \cs{NoValue} to see if an optional
220 % argument was present. (See Section~\ref{sec:cmd:special} for details of
221 % \cs{IfBooleanTF} and testing for \cs{NoValue}.)
222 %
223 % The difference between the \cs{New...} \cs{Renew...}, \cs{Provide...}
224 % and \cs{Declare...} versions is the behavior if \meta{cmd} is already
225 % defined.
226 % \begin{itemize}
227 % \item \cs{NewDocumentCommand} will issue an error if \meta{cmd}
228 % has already been defined.
229 % \item \cs{RenewDocumentCommand} will issue an error if \meta{cmd}
230 % has not previously been defined.
231 % \item \cs{ProvideDocumentCommand} creates a new definition for
232 % \meta{cmd} only if one has not already been given.
233 % \item \cs{DeclareDocumentCommand} will always create the new
234 % definition, irrespective of any existing \meta{cmd} with the
235 % same name. This should be used sparingly.
236 % \end{itemize}
237 %
238 % If the \meta{cmd} can't be provided as a single token but needs
239 % \enquote{constructing}, you can use \cs{ExpandArgs} as explained in
240 % Section~\ref{sec:preconstructing-csnames} which also gives an example
241 % in which this is needed.
242 %
243 % \begin{function}
244 % {
245 % \NewDocumentEnvironment,
246 % \RenewDocumentEnvironment,
247 % \ProvideDocumentEnvironment,
248 % \DeclareDocumentEnvironment
249 % }
250 % \begin{syntax}
251 % \cs{NewDocumentEnvironment} \Arg{env} \Arg{arg spec} \Arg{beg-code} \Arg{end-code}
252 % \end{syntax}
253 % \end{function}
254 % These commands work in the same way as \cs{NewDocumentCommand}, etc.\@, but
255 % create environments (\cs{begin}\Arg{env} \ldots{}
256 % \cs{end}\Arg{env}). Both the \meta{beg-code} and \meta{end-code}
257 % may access the arguments as defined by \meta{arg spec}. The arguments will be
258 % given following \cs{begin}\Arg{env}. Any spaces at the start and end of the
259 % \Arg{env} are removed before the definition takes place, thus
260 % \begin{Verbatim}[gobble = 3]
261 % \NewDocumentEnvironment{foo}
262 % \end{Verbatim}
263 % and
264 % \begin{Verbatim}[gobble = 3]
265 % \NewDocumentEnvironment{ foo }
266 % \end{Verbatim}
267 % both create the same \enquote{\texttt{foo}} environment.
268 %
269 % \subsection{Optional arguments}
270 % \label{sec:cmd:opt}
271 %
272 % In contrast to commands created using \LaTeXe{}'s \cs{newcommand}, optional
273 % arguments created using \cs{NewDocumentCommand} may safely be nested. Thus for
274 % example, following
275 % \begin{Verbatim}[gobble = 3]
276 % \NewDocumentCommand\foo{om}{I grabbed `#1' and `#2'}
277 % \NewDocumentCommand\baz{o}{#1-#1}
278 % \end{Verbatim}
279 % using the command as
280 % \begin{Verbatim}[gobble = 3]
281 % \foo[\baz[stuff]]{more stuff}
282 % \end{Verbatim}
283 % will print
284 % \begin{quote}
285 % I grabbed `stuff-stuff' and `more stuff'
286 % \end{quote}
287 % This is particularly useful when placing a command with an optional argument
288 % \emph{inside} the optional argument of a second command.
289 %
290 % When an optional argument is followed by a mandatory argument with the same
291 % delimiter, the parser issues a warning because the optional argument could not
292 % be omitted by the user, thus becoming in effect mandatory. This can apply to
293 % \texttt{o}, \texttt{d}, \texttt{O}, \texttt{D}, \texttt{s}, \texttt{t},
294 % \texttt{e}, and \texttt{E} type arguments followed by \texttt{r} or
295 % \texttt{R}-type required arguments.
296 %
297 % The default for \texttt{O}, \texttt{D} and \texttt{E} arguments can be
298 % the result of grabbing another argument. Thus for example
299 % \begin{Verbatim}[gobble = 3]
300 % \NewDocumentCommand\foo{O{#2} m}
301 % \end{Verbatim}
302 % would use the mandatory argument as the default for the leading optional
303 % one.
304 %
305 % \subsection{Spacing and optional arguments}
306 % \label{sec:cmd:opt-space}
307 %
308 % \TeX{} will find the first argument after a function name irrespective of any
309 % intervening spaces. This is true for both mandatory and optional arguments. So
310 % |\foo[arg]| and \verb*|\foo [arg]| are equivalent. Spaces are also ignored when
311 % collecting arguments up to the last mandatory argument to be collected (as it
312 % must exist). So after
313 % \begin{Verbatim}[gobble = 3]
314 % \NewDocumentCommand\foo{m o m}{ ... }
315 % \end{Verbatim}
316 % the user input |\foo{arg1}[arg2]{arg3}| and \verb*|\foo{arg1} [arg2] {arg3}|
317 % will both be parsed in the same way.
318 %
319 % The behavior of optional arguments \emph{after} any mandatory arguments is
320 % selectable. The standard settings will allow spaces here, and thus with
321 % \begin{Verbatim}[gobble = 3]
322 % \NewDocumentCommand\foobar{m o}{ ... }
323 % \end{Verbatim}
324 % both |\foobar{arg1}[arg2]| and \verb*|\foobar{arg1} [arg2]| will find an
325 % optional argument. This can be changed by giving the modified |!| in the
326 % argument specification:
327 % \begin{Verbatim}[gobble = 3]
328 % \NewDocumentCommand\foobar{m !o}{ ... }
329 % \end{Verbatim}
330 % where \verb*|\foobar{arg1} [arg2]| will not find an optional argument.
331 %
332 % There is one subtlety here due to the difference in handling by \TeX{} of
333 % `control symbols', where the command name is made up of a single
334 % character, such as `\texttt{\textbackslash\textbackslash}'.
335 % Spaces are not ignored by \TeX{} here,
336 % and thus it is possible to require an optional argument directly follow such a
337 % command. The most common example is the use of \texttt{\textbackslash\textbackslash}
338 % in \pkg{amsmath} environments, which in the terms here would be defined as
339 % \begin{Verbatim}[gobble = 3]
340 % \NewDocumentCommand\\{!s !o}{ ... }
341 % \end{Verbatim}
342 %
343 % Also notable when using optional arguments in the last position is that \TeX{}
344 % will necessarily look ahead for the argument opening token. This means that
345 % the value of |\inputlineno| will be `out by one' if such a trailing optional
346 % argument is \emph{not} present and the command ends a line; it will be one
347 % greater than the line number containing the last mandatory argument.
348 %
349 % \subsection{`Embellishments'}
350 % \label{sec:cmd:embellishment}
351 %
352 % The \texttt{E}-type argument allows one default value per test token. This is
353 % achieved by giving a list of defaults for each entry in the list, for example:
354 % \begin{Verbatim}[gobble = 3]
355 % E{^_}{{UP}{DOWN}}
356 % \end{Verbatim}
357 % If the list of default values is \emph{shorter} than the list of test tokens,
358 % the special \cs{NoValue} marker will be returned (as for the \texttt{e}-type
359 % argument). Thus for example
360 % \begin{Verbatim}[gobble = 3]
361 % E{^_}{{UP}}
362 % \end{Verbatim}
363 % has default \texttt{UP} for the |^| test character, but will return the
364 % \cs{NoValue} marker as a default for |_|. This allows mixing of explicit
365 % defaults with testing for missing values.
366 %
367 % \subsection{Testing special values}
368 % \label{sec:cmd:special}
369 %
370 % Optional arguments make use of dedicated variables to return information about
371 % the nature of the argument received.
372 %
373 % \begin{function}{\IfNoValueTF, \IfNoValueT, \IfNoValueF}
374 % \begin{syntax}
375 % \cs{IfNoValueTF} \Arg{arg} \Arg{true code} \Arg{false code}
376 % \end{syntax}
377 % \end{function}
378 % The \cs{IfNoValue(TF)} tests are used to check if \meta{argument} (|#1|,
379 % |#2|, \emph{etc.}) is the special \cs{NoValue} marker. For example
380 % \begin{Verbatim}[gobble = 3]
381 % \NewDocumentCommand\foo{o m}
382 % {%
383 % \IfNoValueTF {#1}%
384 % {\DoSomethingJustWithMandatoryArgument{#2}}%
385 % {\DoSomethingWithBothArguments{#1}{#2}}%
386 % }
387 % \end{Verbatim}
388 % will use a different internal function if the optional argument
389 % is given than if it is not present.
390 %
391 % Note that three tests are available, depending on which outcome
392 % branches are required: \cs{IfNoValueTF}, \cs{IfNoValueT} and
393 % \cs{IfNoValueF}.
394 %
395 % As the \cs{IfNoValue(TF)} tests are expandable, it is possible to
396 % test these values later, for example at the point of typesetting or
397 % in an expansion context.
398 %
399 % When two optional arguments follow each other (a syntax we typically
400 % discourage), it can make sense to allow users of the command to
401 % specify only the second argument by providing an empty first
402 % argument.
403 % If you wish to test if an argument is blank or not, but are not
404 % concerned with distinguishing an entirely absent argument from
405 % an empty one, use the |O|~type specifier with the
406 % conditional \cs{IfBlankTF} (described below).
407 %
408 % \begin{function}{\IfValueTF, \IfValueT, \IfValueF}
409 % \begin{syntax}
410 % \cs{IfValueTF} \Arg{arg} \Arg{true code} \Arg{false code}
411 % \end{syntax}
412 % \end{function}
413 % The reverse form of the \cs{IfNoValue(TF)} tests are also available
414 % as \cs{IfValue(TF)}. The context will determine which logical
415 % form makes the most sense for a given code scenario.
416 %
417 % \begin{function}{\IfBlankTF, \IfBlankT, \IfBlankF}
418 % \begin{syntax}
419 % \cs{IfBlankTF} \Arg{arg} \Arg{true code} \Arg{false code}
420 % \end{syntax}
421 % \end{function}
422 %
423 % The \cs{IfNoValueTF} command chooses the \meta{true code} if the optional argument has not
424 % been used at all (and it returns the special \cs{NoValue}
425 % marker), but not if it has been given an empty value. In contrast
426 % \cs{IfBlankTF} returns true if its argument is either truly empty or
427 % only contains one or more normal blanks.
428 % For example
429 % \begin{Verbatim}[gobble = 3]
430 % \NewDocumentCommand\foo{m!o}{\par #1:
431 % \IfNoValueTF{#2}
432 % {No optional}%
433 % {%
434 % \IfBlankTF{#2}
435 % {Blanks in or empty}%
436 % {Real content in}%
437 % }%
438 % \space argument!}
439 % \foo{1}[bar] \foo{2}[ ] \foo{3}[] \foo{4}[\space] \foo{5} [x]
440 % \end{Verbatim}
441 % results
442 % in the following output:
443 % \begin{quote}
444 % \NewDocumentCommand\foo{m!o}{\par #1:
445 % \IfNoValueTF{#2}{No optional}%
446 % {\IfBlankTF{#2}{Blanks in or empty}%
447 % {Real content in}}%
448 % \space argument!}
449 % \foo{1}[bar] \foo{2}[ ] \foo{3}[] \foo{4}[\space] \foo{5} [x]
450 % \end{quote}
451 %
452 % Note that the \cs{space} in (4) is considered real content---because it is
453 % a command and not a \enquote{space} character---even though it results
454 % in producing a space. You can also observe in (5) the effect of the
455 % \texttt{!} specifier, preventing the last \cs{foo}
456 % from interpreting \texttt{[x]} as its optional argument.
457 %
458 % \begin{variable}{\BooleanTrue, \BooleanFalse}
459 % \end{variable}
460 % The \texttt{true} and \texttt{false} flags set when searching for
461 % an optional character (using \texttt{s} or \texttt{t\meta{char}}) have
462 % names which are accessible outside of code blocks.
463 %
464 % \begin{function}{\IfBooleanTF, \IfBooleanT, \IfBooleanF}
465 % \begin{syntax}
466 % \cs{IfBooleanTF} \Arg{arg} \Arg{true code} \Arg{false code}
467 % \end{syntax}
468 % \end{function}
469 % Used to test if \meta{argument} (|#1|, |#2|, \emph{etc.}) is
470 % \cs{BooleanTrue} or \cs{BooleanFalse}. For example
471 % \begin{Verbatim}[gobble = 3]
472 % \NewDocumentCommand\foo{sm}
473 % {%
474 % \IfBooleanTF {#1}%
475 % {\DoSomethingWithStar{#2}}%
476 % {\DoSomethingWithoutStar{#2}}%
477 % }
478 % \end{Verbatim}
479 % checks for a star as the first argument, then chooses the action to
480 % take based on this information.
481 %
482 % \subsection{Auto-converting to key--value format}
483 % \label{sec:cmd:keyval}
484 %
485 % Some document commands have a long history of accepting a `free text' optional
486 % argument, for example \cs{caption} and the sectioning commands \cs{section},
487 % etc. Introducing more sophisticated (keyval) options to these commands
488 % therefore needs a method to interpret the optional argument \emph{either} as
489 % free text \emph{or} as a series of keyvals. This needs to take place
490 % during argument grabbing as there is a need for careful treatment of
491 % braces to obtain the correct result.
492 %
493 % The \texttt{=} modifier is available to allow \pkg{ltcmd} to correctly
494 % implement this process. The modifier guarantees that the argument will be
495 % passed to further code as a series of keyvals. To do that, the \texttt{=}
496 % should be followed by an argument containing the default key name. This is used
497 % as the key in a key--value pair \emph{if} the \enquote{raw} argument does \emph{not}
498 % have the correct form to be interpreted as a set of keyvals.
499 %
500 % Taking \cs{caption} as an example, with the demonstration implementation
501 % \begin{Verbatim}[gobble = 3]
502 % \DeclareDocumentCommand\caption{s ={short-text} +O{#3} +m}
503 % {%
504 % \showtokens{Grabbed arguments:^^J(#2)^^Jand^^J(#3)}%
505 % }
506 % \end{Verbatim}
507 % the default key name is \texttt{short-text}. When the command \cs{caption} is
508 % then used, if the optional argument is free text such as
509 % \begin{Verbatim}[gobble = 3]
510 % \caption[Some short text]{A much longer and more detailed text for
511 % demonstration purposes}
512 % \end{Verbatim}
513 % then the output will be
514 % \begin{Verbatim}[gobble = 3]
515 % Grabbed arguments:
516 % (short-text={Some short text})
517 % and
518 % (A much longer and more detailed text for demonstration purposes)
519 % \end{Verbatim}
520 % On the other hand, if the caption is given with a keyval-form argument
521 % \begin{Verbatim}[gobble = 3]
522 % \caption[label = cap:demo]%
523 % {A much longer and more detailed text for demonstration purposes}
524 % \end{Verbatim}
525 % then this will be respected
526 % \begin{Verbatim}[gobble = 3]
527 % Grabbed arguments:
528 % (label = cap:demo)
529 % and
530 % (A much longer and more detailed text for demonstration purposes)
531 % \end{Verbatim}
532 %
533 % Interpretation as keyval form is determined by the presence of \texttt{=}
534 % characters within the argument. Those in inline math mode (enclosed within
535 % \verb|$...$| or \verb|\(...\)|) are ignored. An argument can be forced to be
536 % read as keyvals by including an empty entry at the start
537 % \begin{Verbatim}[gobble = 3]
538 % \caption[=,This is now a keyval]%
539 % % ...
540 % \caption[This is not $=$ keyval]%
541 % \end{Verbatim}
542 %
543 % This empty entry is \emph{not} passed to the underlying code, so will not lead
544 % to issues with keyval parsers that do not allow an empty key name. Any text-mode
545 % \texttt{=} signs will need to be braced to avoid being misinterpreted: this
546 % is likely most conveniently handled by bracing the entire argument
547 % \begin{Verbatim}[gobble = 3]
548 % \caption[{Not = to a keyval!}]%
549 % \end{Verbatim}
550 % which will be passed correctly as
551 % \begin{Verbatim}[gobble = 3]
552 % Grabbed arguments:
553 % (short-text = {Not = to a keyval!})
554 % \end{Verbatim}
555 %
556 % If the argument is completely blank, the conversion results in an empty keyval
557 % list, not \verb*|short-text = |. This reflects the fact that with a move toward
558 % keyval processing, an empty argument is best modelled as an empty keyval list.
559 % If the user does want an empty classical argument, using |[{}]| will work
560 % with both the new processor code and older formats.
561 %
562 % \subsection{Argument processors}
563 % \label{sec:cmd:processors}
564 %
565 % Argument processor are applied to an argument \emph{after} it has been grabbed
566 % by the underlying system but before it is passed to \meta{code}. An argument
567 % processor can therefore be used to regularize input at an early stage, allowing
568 % the internal functions to be completely independent of input form. Processors
569 % are applied to user input and to default values for optional arguments, but
570 % \emph{not} to the special \cs{NoValue} marker.
571 %
572 % Each argument processor is specified by the syntax \texttt{>}\marg{processor}
573 % in the argument specification. Processors are applied from right to left, so
574 % that
575 % \begin{Verbatim}[gobble = 3]
576 % >{\ProcessorB} >{\ProcessorA} m
577 % \end{Verbatim}
578 % would apply \cs{ProcessorA} followed by \cs{ProcessorB} to the tokens grabbed
579 % by the \texttt{m} argument.
580 %
581 % \begin{function}{\SplitArgument}
582 % \begin{syntax}
583 % \cs{SplitArgument} \Arg{number} \Arg{token(s)}
584 % \end{syntax}
585 % \end{function}
586 % This processor splits the argument given at each occurrence of the
587 % \meta{tokens} up to a maximum of \meta{number} tokens (thus
588 % dividing the input into $\text{\meta{number}} + 1$ parts).
589 % An error is given if too many \meta{tokens} are present in the
590 % input. The processed input is placed inside
591 % $\text{\meta{number}} + 1$ sets of braces for further use.
592 % If there are fewer than \Arg{number} of \Arg{tokens} in the argument
593 % then \cs{NoValue} markers are added at the end of the processed
594 % argument.
595 % \begin{Verbatim}[gobble = 3]
596 % \NewDocumentCommand\foo{>{\SplitArgument{2}{;}} m}
597 % {\InternalFunctionOfThreeArguments#1}
598 % \end{Verbatim}
599 % If only a single character \meta{token} is used for the split, any
600 % category code $13$ (active) character matching the \meta{token} will
601 % be replaced before the split takes place.
602 % Spaces are trimmed at each end of each item parsed.
603 %
604 % The \texttt{E} argument type is somewhat special, because with a single
605 % \texttt{E} in the command declaration you may end up with several
606 % arguments in a command (one formal argument per embellishment token).
607 % Therefore, when an argument processor is applied to an
608 % \texttt{e}/\texttt{E}-type
609 % argument, all the arguments pass through that processor before being fed
610 % to the \meta{code}. For example, this command
611 % \begin{Verbatim}[gobble = 3]
612 % \NewDocumentCommand\foo{ >{\TrimSpaces} e{_^} }
613 % { [#1](#2) }
614 % \end{Verbatim}
615 % applies \cs{TrimSpaces} to both arguments.
616 %
617 % \begin{function}{\SplitList}
618 % \begin{syntax}
619 % \cs{SplitList} \Arg{token(s)}
620 % \end{syntax}
621 % \end{function}
622 % This processor splits the argument given at each occurrence of the
623 % \meta{token(s)} where the number of items is not fixed. Each item is
624 % then wrapped in braces within |#1|. The result is that the
625 % processed argument can be further processed using a mapping function
626 % (see below).
627 % \begin{Verbatim}[gobble = 3]
628 % \NewDocumentCommand\foo{>{\SplitList{;}} m}
629 % {\MappingFunction#1}
630 % \end{Verbatim}
631 % If only a single character \meta{token} is used for the split, it will
632 % take account of the possibility that the \meta{token} has been made active
633 % (category code~$13$) and will split at such tokens.
634 % Spaces are trimmed at each end of each item parsed. Exactly one set
635 % of braces will be stripped if an entire item is surrounded by them,
636 % i.e.~the following inputs and outputs result (each separate item as
637 % a brace group).
638 % \begin{Verbatim}[gobble = 3]
639 % a ==> {a}
640 % {a} ==> {a}
641 % {a}b ==> {{a}b}
642 % a,b ==> {a}{b}
643 % {a},b ==> {a}{b}
644 % a,{b} ==> {a}{b}
645 % a,{b}c ==> {a}{{b}c}
646 % \end{Verbatim}
647 %
648 % \begin{function}{\ProcessList}
649 % \begin{syntax}
650 % \cs{ProcessList} \Arg{list} \Arg{token(s)}
651 % \end{syntax}
652 % \end{function}
653 % To support \cs{SplitList}, the function \cs{ProcessList} is available
654 % to apply \meta{tokens} to every entry in a \meta{list}. The
655 % \meta{tokens} can be arbitrary contents that should expect one argument
656 % after it: the list entry. For example
657 % \begin{Verbatim}[gobble = 3]
658 % \NewDocumentCommand\foo{>{\SplitList{;}} m}
659 % {\ProcessList{#1}{\SomeDocumentCommand}}
660 % \end{Verbatim}
661 % or
662 % \begin{Verbatim}[gobble = 3]
663 % \NewDocumentCommand\foo{>{\SplitList{;}} m}
664 % {\ProcessList{#1}{Abc \SomeDocumentCommand}}
665 % \end{Verbatim}
666 %
667 % \begin{function}{\ReverseBoolean}
668 % \begin{syntax}
669 % \cs{ReverseBoolean}
670 % \end{syntax}
671 % \end{function}
672 % This processor reverses the logic of \cs{BooleanTrue} and
673 % \cs{BooleanFalse}, so that the example from earlier would become
674 % \begin{Verbatim}[gobble = 3]
675 % \NewDocumentCommand\foo{>{\ReverseBoolean} s m}
676 % {%
677 % \IfBooleanTF#1%
678 % {\DoSomethingWithoutStar{#2}}%
679 % {\DoSomethingWithStar{#2}}%
680 % }
681 % \end{Verbatim}
682 %
683 % \begin{function}{\TrimSpaces}
684 % \begin{syntax}
685 % \cs{TrimSpaces}
686 % \end{syntax}
687 % \end{function}
688 % Removes any leading and trailing spaces (tokens with character code~$32$
689 % and category code~$10$) for the ends of the argument. Thus for example
690 % declaring a function
691 % \begin{Verbatim}[gobble = 3]
692 % \NewDocumentCommand\foo{>{\TrimSpaces} m}
693 % {\showtokens{#1}}
694 % \end{Verbatim}
695 % and using it in a document as
696 % \begin{flushleft}
697 % \verb= =\verb*=\foo{ hello world }=
698 % \end{flushleft}
699 % will show `\verb*=hello world=' at the terminal, with the space at each
700 % end removed. \cs{TrimSpaces} will remove multiple spaces from the ends of
701 % the input in cases where these have been included such that the standard
702 % \TeX{} conversion of multiple spaces to a single space does not apply.
703 %
704 % \subsection{Body of an environment}
705 % \label{sec:cmd:body}
706 %
707 % While environments |\begin|\marg{environment}\ \dots{}\,|\end|\marg{environment}
708 % are typically used in cases where the code implementing the \meta{environment}
709 % does not need to access the contents of the environment (its \enquote{body}),
710 % it is sometimes useful to have the body as a standard argument.
711 %
712 % This is achieved by ending the argument specification with~\texttt{b}, which is
713 % a dedicated argument type for this situation. For instance
714 % \begin{Verbatim}[gobble = 3]
715 % \NewDocumentEnvironment{twice}{O{\ttfamily} +b}
716 % {#2#1#2} {}
717 % \begin{twice}[\itshape]
718 % Hello world!
719 % \end{twice}
720 % \end{Verbatim}
721 % typesets `Hello world!{\itshape Hello world!}'.
722 %
723 % The prefix |+| is used to allow multiple paragraphs in the environment's body.
724 % Argument processors can also be applied to \texttt{b}~arguments. By default,
725 % spaces are trimmed at both ends of the body: in the example there would
726 % otherwise be spaces coming from the ends the lines after |[\itshape]| and
727 % |world!|. Putting the prefix |!| before \texttt{b} suppresses space-trimming.
728 %
729 % When \texttt{b} is used in the argument specification, the last
730 % argument of the environment declaration (e.g.,
731 % \cs{NewDocumentEnvironment}), which consists of an \meta{end code} to
732 % insert at |\end|\marg{environment}, is redundant since one can simply
733 % put that code at the end of the \meta{start code}. Nevertheless this
734 % (empty) \meta{end code} must be provided.
735 %
736 % Environments that use this feature can be nested.
737 %
738 % \subsection{Fully-expandable document commands\label{sec:ltcmd:expandable}}
739 %
740 % Document commands created using \cs{NewDocumentCommand}, etc.\@, are normally
741 % created so that they do not expand unexpectedly. This is done using engine
742 % features, so is more powerful than \LaTeXe{}'s \cs{protect} mechanism. There
743 % are \emph{very rare} occasion when it may be useful to create functions using a
744 % expansion-only grabber. This imposes a number of restrictions on the
745 % nature of the arguments accepted by a function, and the code it implements.
746 % This facility should only be used when \emph{necessary}.
747 %
748 % \begin{function}
749 % {
750 % \NewExpandableDocumentCommand,
751 % \RenewExpandableDocumentCommand,
752 % \ProvideExpandableDocumentCommand,
753 % \DeclareExpandableDocumentCommand
754 % }
755 % \begin{syntax}
756 % \cs{NewExpandableDocumentCommand} \Arg{cmd} \Arg{arg spec} \Arg{code}
757 % \end{syntax}
758 % \end{function}
759 % This family of commands is used to create a document-level \meta{cmd},
760 % which will grab its arguments in a fully-expandable manner. The
761 % argument specification for the function is given by \meta{arg spec},
762 % and the \meta{cmd} will execute \meta{code}. In general, \meta{code} will
763 % also be fully expandable, although it is possible that this will
764 % not be the case (for example, a function for use in a table might
765 % expand so that \cs{omit} is the first non-expandable non-space token).
766 %
767 % Parsing arguments by pure expansion imposes a number of restrictions on
768 % both the type of arguments that can be read and the error checking
769 % available:
770 % \begin{itemize}
771 % \item The last argument (if any are present) must be one of the
772 % mandatory types \texttt{m}, \texttt{r} or \texttt{R}.
773 % \item The \enquote{verbatim} argument type \texttt{v} is not available.
774 % \item Argument processors (using \texttt{>}) are not available.
775 % \item It is not possible to differentiate between, for example
776 % |\foo[| and |\foo{[}|: in both cases the \texttt{[} will be
777 % interpreted as the start of an optional argument. As a
778 % result, checking for optional arguments is less robust than
779 % in the standard version.
780 % \end{itemize}
781 %
782 % \subsection{Commands at the start of tabular cells}
783 %
784 % Creating commands that are used at the start of tabular cells imposes
785 % some restrictions on the underlying implementation. The standard \LaTeX{}
786 % tabular environments (\texttt{tabular}, etc.) use a mechanism which requires
787 % that any command wrapping \cs{multicolumn} or similar must be
788 % \enquote{expandable}. This is \emph{not} the case for commands created using
789 % \cs{NewDocumentCommand}, etc., which as detailed in
790 % Section~\ref{sec:ltcmd:expandable} use an engine feature which prevents
791 % such \enquote{expansion}. Therefore, to create such wrappers for use at the start
792 % of tabular cells, you must use \cs{NewExpandableDocumentCommand}, for example
793 % \begin{Verbatim}[gobble = 3]
794 % \NewExpandableDocumentCommand\MyMultiCol{m}{\multicolumn{3}{c}{#1}}
795 % \begin{tabular}{lcr}
796 % a & b & c \\
797 % \MyMultiCol{stuff} \\
798 % \end{tabular}
799 % \end{Verbatim}
800 %
801 % \subsection{Using the verbatim argument types}
802 %
803 % As described above, the \texttt{v}-type argument may be viewed as similar to
804 % \cs{verb}. Before looking at exactly what that means, it is important to
805 % highlight some key differences. Most notably, \emph{grabbing} a verbatim-like
806 % argument is separate from \emph{typesetting} it: the latter is covered in the
807 % next section.
808 %
809 % When grabbing a \texttt{v}-type argument, \LaTeX{} first uses the kernel
810 % command \cs{dospecials} to turn off the \enquote{special} nature of characters.
811 % It then makes both spaces and tabs \enquote{active}, so that they can be given
812 % a custom definition. Any other characters are grabbed as-is: this means that if
813 % any characters have been made \enquote{special} and are not listed in
814 % \cs{dospecials}, an error will arise (see below).
815 %
816 % The characters that are grabbed as the argument are all those between two
817 % identical: in contrast to \cs{verb}, the characters \texttt{\textbackslash},
818 % |{|, |}| and |%| \emph{cannot} be used as the delimiter character. If any of
819 % the grabbed tokens have \enquote{special} meaning, an error will be issued.
820 %
821 % For the \texttt{+v}-type argument, which allows line breaks within the
822 % argument, newline characters are converted into \cs{obeyedline} commands. The
823 % standard definition of \cs{obeyedline} is simple |\par|, thus allowing the
824 % grabbed tokens to be used directly in typesetting. A local redefinition of
825 % \cs{obeyedline} can be used to achieve other outputs. For example, to retain
826 % blank lines whilst typesetting, one could use
827 % \begin{Verbatim}[gobble = 3]
828 % \renewcommand*\obeyedline{\mbox{}\par}
829 % \end{Verbatim}
830 % More information about using these arguments in typesetting is in the following
831 % subsection.
832 %
833 % Some additional details that may be useful for those with more \TeX{}
834 % knowledge: do not worry if this does not make sense to you! Spaces and tabs are
835 % stored as active characters. In $8$-bit engines, non-ASCII characters are
836 % \enquote{active}, whilst other than the letters a--zA--Z, ASCII characters are
837 % \enquote{other}. In Unicode engines, non-ASCII codepoints will be either
838 % letters or \enquote{other}, based on the standard \LaTeX{} settings derived
839 % from Unicode data. For token-based comparisons, it is likely that the active
840 % spaces and tabs should be replaced: this can be done conveniently by expansion.
841 %
842 % \subsection{Typesetting verbatim-like material}
843 %
844 % In contrast to \cs{verb}, the \texttt{(+)v}-type argument is only about
845 % \emph{grabbing} the argument, not \emph{typesetting} it. As such, features that
846 % users often associate with \enquote{verbatim} are not automatically activated,
847 % e.g., selecting a monospaced font. Material grabbed by the \texttt{v}-type
848 % argument does not automatically suppress ligatures: with modern \TeX{} engines,
849 % this largely can be done without the token manipulation which \cs{verb} uses.
850 % (In \cs{verb}, ligatures are suppressed by making characters active and
851 % inserting a zero-width kern before the character itself.)
852 %
853 % The \cs{verb} command also selects a monospaced font: this is not intrinsic to
854 % verbatim material, so will need to be set up using for example \cs{ttfamily}.
855 % Similarly, the \texttt{verbatim} environment sets up the meaning of \cs{par}
856 % suitable for breaking lines.
857 %
858 % \subsection{Verbatim environments}
859 % \label{sec:cmd:verbenv}
860 %
861 % In some cases, when grabbing the body of an environment you will want the
862 % contents to be treated verbatim. This is available using the argument
863 % specification~\texttt{c}. Like the \texttt{b} specification, this has to be the
864 % last one. Thus for example
865 % \begin{Verbatim}[gobble = 3]
866 % \NewDocumentEnvironment{MyVerbatim}{!O{\ttfamily} c}
867 % {\begin{center} #1 #2\end{center}} {}
868 % \begin{MyVerbatim}[\ttfamily\itshape]
869 % % Some code is shown here
870 % $y = mx + c$
871 % \end{MyVerbatim}
872 % \end{Verbatim}
873 % will typeset verbatim the content, thus:
874 % \NewDocumentEnvironment{MyVerbatim}{!O{\ttfamily} c}
875 % {\begin{center} #1 #2\end{center}} {}
876 % \begin{MyVerbatim}[\ttfamily\itshape]
877 % % Some code is shown here
878 % $y = mx + c$
879 % \end{MyVerbatim}
880 %
881 % Since grabbing the entire contents verbatim will result in there being no
882 % \cs{par} tokens, newlines are always permitted: there is no need for a
883 % |+|~modifier here. As for the \texttt{v} specification, newlines are stored as
884 % \cs{obeyedline}. In a similar fashion to the \texttt{b}~specification, by
885 % default \emph{newlines} are trimmed at both ends of the body. Putting the
886 % prefix |!| before \texttt{c} suppresses this trimming.
887 %
888 % Collection of the body takes place on a line-by-line basis: content is
889 % collected up to the end-of-line in the source, then examined before storage.
890 % This means that the line ending the environment (containing in the example
891 % above |\end{MyVerbatim}|) cannot have any text \emph{after} the end of the
892 % environment. Text \emph{before} the end of environment is treated normally,
893 % but note that there is no trailing \cs{obeyedline} added if there is text here.
894 % Other than optional arguments, no text is allowed on the opening line of the
895 % environment.
896 %
897 % Special handling is applied to a \texttt{o}, \texttt{O}, \texttt{d} or
898 % \texttt{D}~specification argument immediately before an
899 % \texttt{c}~specification. This means that when the optional argument is absent,
900 % the first character of the next line will be read with the correctly applied
901 % verbatim category code. Issues may arise if \emph{multiple} optional arguments
902 % are used before a \texttt{c}~specification: this will only work reliably where
903 % the optional tokens are \enquote{other} characters.
904 %
905 % For technical reasons, we recommend that spaces are \emph{not} ignored when
906 % searching for an optional argument before an \texttt{c} specification: this can
907 % be achieved by adding the \texttt{!} modifier as shown in the example.
908 % However, this is left as a choice for the user.
909 %
910 % \subsection{Performance}
911 %
912 % For document commands where the argument specification is entirely
913 % comprised of |m| or |+m| entries (or is entirely empty), the internal structure
914 % created by \cs{NewDocumentCommand} is essentially as efficient
915 % as provided by |\newcommand(*)|. As such, document commands may replace
916 % constructs arising from \cs{newcommand}, etc., without a need to be concerned
917 % about performance. It should be noted that \cs{newcommand(*)} produces expandable
918 % results, so the direct replacement is \cs{NewExpandableDocumentCommand};
919 % in most cases, however, it is better to use \cs{NewDocumentCommand} to
920 % give more robust structures.
921 %
922 % \subsection{Details about argument delimiters}
923 %
924 % In normal (non-expandable) commands, the delimited types look for the
925 % initial delimiter by peeking ahead (using \pkg{expl3}'s |\peek_...|
926 % functions) looking for the delimiter token. The token has to have the
927 % same meaning and \enquote{shape} of the token defined as delimiter.
928 % There are three possible cases of delimiters: character tokens, control
929 % sequence tokens, and active character tokens. For all practical purposes
930 % of this description, active character tokens will behave exactly as
931 % control sequence tokens.
932 %
933 % \subsubsection{Character tokens}
934 %
935 % A character token is characterized by its character code, and its meaning
936 % is the category code~(|\catcode|). When a command is defined, the meaning
937 % of the character token is fixed into the definition of the command and
938 % cannot change. A command will correctly see an argument delimiter if
939 % the open delimiter has the same character and category codes as at the
940 % time of the definition. For example in:
941 % \begin{Verbatim}[gobble = 3]
942 % \NewDocumentCommand { \foobar } { D<>{default} } {(#1)}
943 % \foobar <hello> \par
944 % \char_set_catcode_letter:N <
945 % \foobar <hello>
946 % \end{Verbatim}
947 % the output would be:
948 % \begin{Verbatim}[gobble = 3]
949 % (hello)
950 % (default)<hello>
951 % \end{Verbatim}
952 % as the open-delimiter |<| changed in meaning between the two calls to
953 % |\foobar|, so the second one doesn't see the |<| as a valid delimiter.
954 % Commands assume that if a valid open-delimiter was found, a matching
955 % close-delimiter will also be there. If it is not (either by being
956 % omitted or by changing in meaning), a low-level \TeX{} error is raised
957 % and the command call is aborted.
958 %
959 % \subsubsection{Control sequence tokens}
960 %
961 % A control sequence (or control character) token is characterized by
962 % its name, and its meaning is its definition.
963 % A token cannot have two different meanings at the same time.
964 % When a control sequence is defined as delimiter in a command,
965 % it will be detected as delimiter whenever the control sequence name
966 % is found in the document regardless of its current definition.
967 % For example in:
968 % \begin{Verbatim}[gobble = 3]
969 % \cs_set:Npn \x { abc }
970 % \NewDocumentCommand { \foobar } { D\x\y{default} } {(#1)}
971 % \foobar \x hello\y \par
972 % \cs_set:Npn \x { def }
973 % \foobar \x hello\y
974 % \end{Verbatim}
975 % the output would be:
976 % \begin{Verbatim}[gobble = 3]
977 % (hello)
978 % (hello)
979 % \end{Verbatim}
980 % with both calls to the command seeing the delimiter |\x|.
981 %
982 % \subsection{Creating new argument processors}
983 %
984 % \begin{variable}{\ProcessedArgument}
985 % \end{variable}
986 % Argument processors allow manipulation of a grabbed argument before it is
987 % passed to the underlying code. New processor implementations may be created
988 % as functions which take one trailing argument, and which leave their result in
989 % the \cs{ProcessedArgument} variable. For example, \cs{ReverseBoolean} is
990 % defined as
991 % \begin{Verbatim}[gobble = 3]
992 % \ExplSyntaxOn
993 % \cs_new_protected:Npn \ReverseBoolean #1
994 % {
995 % \bool_if:NTF #1
996 % { \tl_set:Nn \ProcessedArgument { \c_false_bool } }
997 % { \tl_set:Nn \ProcessedArgument { \c_true_bool } }
998 % }
999 % \ExplSyntaxOff
1000 % \end{Verbatim}
1001 % [As an aside: the code is written in \pkg{expl3}, so we don't have to
1002 % worry about spaces creeping into the definition.]
1003 %
1004 % \MaybeStop{}
1005 %
1006 % \begin{macrocode}
1007 %<@@=cmd>
1008 % \end{macrocode}
1009 %
1010 % \begin{macrocode}
1011 %<*2ekernel>
1012 \message{document commands,}
1013 %</2ekernel>
1014 % \end{macrocode}
1015 %
1016 % \changes{v1.0b}{2021/03/18}{Use \cs{NewModuleRelease}.}
1017 % \changes{v1.0e}{2021/05/24}{Use \cs{msg_...} instead of \cs{__kernel_msg...}}
1018 % \changes{v1.3k}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
1019 % expansion in expl3 code}
1020 %
1021 % \pkg{ltcmd} code contains an |^^@| character, which usually has
1022 % catcode~15, so \cs{IncludeInRelease} will break when this code is
1023 % being skipped, so we'll save the catcode of |^^@| to restore later:
1024 % \begin{macrocode}
1025 %<*2ekernel|latexrelease>
1026 %<latexrelease>\edef\@latexrelease@catcode@null{\the\catcode`\^^@ }
1027 %<latexrelease>\catcode`\^^@=12
1028 \ExplSyntaxOn
1029 %<latexrelease>\NewModuleRelease{2020/10/01}{ltcmd}
1030 %<latexrelease> {Document~command~parser}%
1031 % \end{macrocode}
1032 %
1033 % \begin{macrocode}
1034 \cs_generate_variant:Nn \tl_head:n { e }
1035 % \end{macrocode}
1036 %
1037 % \subsection{Variables and constants}
1038 %
1039 % \begin{variable}{\l_@@_arg_spec_tl}
1040 % Holds the argument specification after normalization of shorthands.
1041 % \begin{macrocode}
1042 \tl_new:N \l_@@_arg_spec_tl
1043 % \end{macrocode}
1044 % \end{variable}
1045 %
1046 % \begin{variable}{\l_@@_args_tl}
1047 % Token list variable for grabbed arguments.
1048 % \begin{macrocode}
1049 \tl_new:N \l_@@_args_tl
1050 % \end{macrocode}
1051 % \end{variable}
1052 %
1053 % \begin{variable}{\l_@@_args_i_tl, \l_@@_args_ii_tl}
1054 % Hold the modified arguments when dealing with default values or
1055 % processors.
1056 % \begin{macrocode}
1057 \tl_new:N \l_@@_args_i_tl
1058 \tl_new:N \l_@@_args_ii_tl
1059 % \end{macrocode}
1060 % \end{variable}
1061 %
1062 % \begin{variable}{\l_@@_current_arg_int}
1063 % The number of the current argument being set up: this is used to
1064 % make sure there are at most 9 arguments, then for creating the
1065 % expandable auxiliary functions and knowing how many arguments the
1066 % code function should take.
1067 % \begin{macrocode}
1068 \int_new:N \l_@@_current_arg_int
1069 % \end{macrocode}
1070 % \end{variable}
1071 %
1072 % \begin{variable}{\l_@@_total_args_int}
1073 % \changes{v1.3a}{2025-01-23}{New variable}
1074 % The total number of arguments found during normalization: this is required
1075 % where special action is needed for the penultimate argument.
1076 % \begin{macrocode}
1077 \int_new:N \l_@@_total_args_int
1078 % \end{macrocode}
1079 % \end{variable}
1080 %
1081 % \begin{variable}{\l_@@_last_mandatory_arg_int}
1082 % \changes{v1.3c}{2025-04-12}{New variable}
1083 % \changes{v1.3f}{2025-09-23}{Rename variable}
1084 % Needed to allow handling of verbatim catcodes at the end of the
1085 % setup.
1086 % \begin{macrocode}
1087 \int_new:N \l_@@_last_mandatory_arg_int
1088 % \end{macrocode}
1089 % \end{variable}
1090 %
1091 % \begin{variable}{\l_@@_defaults_bool, \l_@@_defaults_tl}
1092 % The boolean indicates whether there are any argument with default
1093 % value other than \cs{NoValue}; the token list holds the code to
1094 % determine these default values in terms of other arguments.
1095 % \begin{macrocode}
1096 \bool_new:N \l_@@_defaults_bool
1097 \tl_new:N \l_@@_defaults_tl
1098 % \end{macrocode}
1099 % \end{variable}
1100 %
1101 % \begin{variable}{\l_@@_environment_bool}
1102 % Generating environments uses the same mechanism as generating functions.
1103 % However, full processing of arguments is always needed for environments,
1104 % and so the function-generating code needs to know this.
1105 % This variable is also used at run time to give correct error messages.
1106 % \begin{macrocode}
1107 \bool_new:N \l_@@_environment_bool
1108 % \end{macrocode}
1109 % \end{variable}
1110 %
1111 % \begin{variable}{\l_@@_environment_str}
1112 % Name of the environment, used at definition time and at run time.
1113 % \begin{macrocode}
1114 \str_new:N \l_@@_environment_str
1115 % \end{macrocode}
1116 % \end{variable}
1117 %
1118 % \begin{variable}{\l_@@_expandable_bool}
1119 % Used to indicate if an expandable command is begin generated, as this
1120 % affects both the acceptable argument types and how they are implemented.
1121 % \begin{macrocode}
1122 \bool_new:N \l_@@_expandable_bool
1123 % \end{macrocode}
1124 % \end{variable}
1125 %
1126 % \begin{variable}{\l_@@_expandable_aux_name_tl}
1127 % Used to create pretty-printing names for the auxiliaries: although the
1128 % immediate definition does not vary, the full expansion does and so it
1129 % does not count as a constant.
1130 % \begin{macrocode}
1131 \tl_new:N \l_@@_expandable_aux_name_tl
1132 \tl_set:Nn \l_@@_expandable_aux_name_tl
1133 {
1134 \l_@@_function_tl \c_space_tl
1135 ( arg~ \int_use:N \l_@@_current_arg_int )
1136 }
1137 % \end{macrocode}
1138 % \end{variable}
1139 %
1140 % \begin{variable}{\g_@@_grabber_int}
1141 % Used (in exceptional cases) to get unique names for grabbers used by
1142 % expandable commands.
1143 % \begin{macrocode}
1144 \int_new:N \g_@@_grabber_int
1145 % \end{macrocode}
1146 % \end{variable}
1147 %
1148 % \begin{variable}{\l_@@_fn_tl}
1149 % For passing the pre-formed name of the auxiliary to be used as the
1150 % parsing function.
1151 % \begin{macrocode}
1152 \tl_new:N \l_@@_fn_tl
1153 % \end{macrocode}
1154 % \end{variable}
1155 %
1156 % \begin{variable}{\l_@@_fn_code_tl}
1157 % For passing the pre-formed name of the auxiliary that contains the
1158 % actual code.
1159 % \begin{macrocode}
1160 \tl_new:N \l_@@_fn_code_tl
1161 % \end{macrocode}
1162 % \end{variable}
1163 %
1164 % \begin{variable}{\l_@@_function_tl}
1165 % Holds the control sequence name of the function currently being
1166 % defined: used to avoid passing this as an argument and to avoid repeated
1167 % use of \cs{cs_to_str:N}.
1168 % \begin{macrocode}
1169 \tl_new:N \l_@@_function_tl
1170 % \end{macrocode}
1171 % \end{variable}
1172 %
1173 % \begin{variable}{\l_@@_grab_expandably_bool}
1174 % When defining a non-expandable command, indicates whether the
1175 % arguments can all safely be grabbed by expandable grabbers. This is
1176 % to support abuses of \pkg{xparse} that use protected functions
1177 % inside csname constructions.
1178 % \begin{macrocode}
1179 \bool_new:N \l_@@_grab_expandably_bool
1180 % \end{macrocode}
1181 % \end{variable}
1182 %
1183 % \begin{variable}{\l_@@_obey_spaces_bool}
1184 % For trailing optionals.
1185 % \begin{macrocode}
1186 \bool_new:N \l_@@_obey_spaces_bool
1187 % \end{macrocode}
1188 % \end{variable}
1189 %
1190 % \begin{variable}{\l_@@_last_delimiters_tl}
1191 % Holds the delimiters (first tokens) of all optional arguments since
1192 % the previous mandatory argument, to warn about cases where it would
1193 % be impossible to omit optional arguments completely because the
1194 % following mandatory argument has the same delimiter as one of the
1195 % optional arguments.
1196 % \begin{macrocode}
1197 \tl_new:N \l_@@_last_delimiters_tl
1198 % \end{macrocode}
1199 % \end{variable}
1200 %
1201 % \begin{variable}{\l_@@_long_bool}
1202 % Used to indicate that an argument is long, on a per-argument basis.
1203 % \begin{macrocode}
1204 \bool_new:N \l_@@_long_bool
1205 % \end{macrocode}
1206 % \end{variable}
1207 %
1208 % \begin{variable}{\l_@@_suppress_strip_bool}
1209 % \changes{v1.1a}{2022/08/10}{New switch}
1210 % Used to indicate that an a pair of braces should not be stripped from
1211 % an optional argument.
1212 % \begin{macrocode}
1213 \bool_new:N \l_@@_suppress_strip_bool
1214 % \end{macrocode}
1215 % \end{variable}
1216 %
1217 % \begin{variable}{\l_@@_m_args_int}
1218 % The number of \texttt{m} arguments: if this is the same as the total
1219 % number of arguments, then a short-cut can be taken in the creation of
1220 % the grabber code.
1221 % \begin{macrocode}
1222 \int_new:N \l_@@_m_args_int
1223 % \end{macrocode}
1224 % \end{variable}
1225 %
1226 % \begin{variable}{\l_@@_prefixed_bool}
1227 % When preparing the signature of non-expandable commands, indicates
1228 % that the current argument is affected by a processor or by |+|
1229 % (namely is long).
1230 % \begin{macrocode}
1231 \bool_new:N \l_@@_prefixed_bool
1232 % \end{macrocode}
1233 % \end{variable}
1234 %
1235 % \begin{variable}{\l_@@_process_all_tl, \l_@@_process_one_tl, \l_@@_process_some_bool}
1236 % When preparing the signature, the processors that will be applied to
1237 % a given argument are collected in \cs{l_@@_process_one_tl}, while
1238 % \cs{l_@@_process_all_tl} contains processors for all arguments. The
1239 % boolean indicates whether there are any processors (to bypass the
1240 % whole endeavour otherwise).
1241 % \begin{macrocode}
1242 \tl_new:N \l_@@_process_all_tl
1243 \tl_new:N \l_@@_process_one_tl
1244 \bool_new:N \l_@@_process_some_bool
1245 % \end{macrocode}
1246 % \end{variable}
1247 %
1248 % \begin{variable}{\l_@@_saved_args_tl}
1249 % Stores \cs{l_@@_args_tl} to deal with space-trimming of
1250 % \texttt{b}-type arguments.
1251 % \begin{macrocode}
1252 \tl_new:N \l_@@_saved_args_tl
1253 % \end{macrocode}
1254 % \end{variable}
1255 %
1256 % \begin{variable}{\l_@@_signature_tl}
1257 % Used when constructing the signature (code for argument grabbing) to
1258 % hold what will become the implementation of the main function.
1259 % When arguments are grabbed (at point of use of the command/environment),
1260 % it also stores the code for grabbing the remaining arguments.
1261 % \begin{macrocode}
1262 \tl_new:N \l_@@_signature_tl
1263 % \end{macrocode}
1264 % \end{variable}
1265 %
1266 % \begin{variable}
1267 % {\l_@@_some_obey_spaces_bool, \l_@@_some_long_bool, \l_@@_some_short_bool}
1268 % These flags are set while normalizing the argument specification.
1269 % The \texttt{obey_spaces} one is used to detect when |!| is used on
1270 % an argument that is not a trailing optional argument.
1271 % The other two are used to check whether all short arguments appear
1272 % before long arguments: this is needed to grab arguments expandably.
1273 % As soon as the first long argument is seen (other than
1274 % \texttt{t}-type, whose long status is ignored) the
1275 % \texttt{some_long} flag is set. The \texttt{some_short} flag is
1276 % used for expandable commands, to know whether to define a short
1277 % auxiliary too.
1278 % \begin{macrocode}
1279 \bool_new:N \l_@@_some_obey_spaces_bool
1280 \bool_new:N \l_@@_some_long_bool
1281 \bool_new:N \l_@@_some_short_bool
1282 % \end{macrocode}
1283 % \end{variable}
1284 %
1285 % \begin{variable}{\l_@@_final_verb_bool}
1286 % \changes{v1.3a}{2025-01-23}{New variable}
1287 % Needed to establish whether optional arguments should be collected
1288 % \enquote{verbatim safe}.
1289 % \begin{macrocode}
1290 \bool_new:N \l_@@_final_verb_bool
1291 % \end{macrocode}
1292 % \end{variable}
1293 %
1294 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
1295 % \begin{macro}{\@@_if_recursion_tail_stop_do:Nn}
1296 % \begin{macro}{\@@_use_i_delimit_by_q_recursion_stop:nw}
1297 % Quarks and functions for internal processing.
1298 % \begin{macrocode}
1299 \quark_new:N \q_@@_recursion_tail
1300 \quark_new:N \q_@@_recursion_stop
1301 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop_do:Nn
1302 % \end{macrocode}
1303 % \end{macro}
1304 % \end{macro}
1305 % \end{variable}
1306 %
1307 % \begin{variable}{\l_@@_tmp_prop, \l_@@_tmpa_tl, \l_@@_tmpb_tl}
1308 % \begin{macro}{\@@_tmp:w}
1309 % Scratch space.
1310 % \begin{macrocode}
1311 \prop_new:N \l_@@_tmp_prop
1312 \tl_new:N \l_@@_tmpa_tl
1313 \tl_new:N \l_@@_tmpb_tl
1314 \cs_new_eq:NN \@@_tmp:w ?
1315 % \end{macrocode}
1316 % \end{macro}
1317 % \end{variable}
1318 %
1319 % With \pkg{xparse}, information about commands being (re)defined was
1320 % switched off by default, unless the |log-declarations| package
1321 % option was used, so here we'll switch that off as well.
1322 % \begin{macrocode}
1323 \msg_redirect_module:nnn { cmd } { info } { none }
1324 % \end{macrocode}
1325 %
1326 % Also add \pkg{cmd} to the \pkg{LaTeX} messages.
1327 % \begin{macrocode}
1328 \prop_gput:Nnn \g_msg_module_type_prop { cmd } { LaTeX }
1329 % \end{macrocode}
1330 %
1331 % \subsection{Declaring commands and environments}
1332 %
1333 % \begin{macro}{\@@_declare_cmd:Nnn, \@@_declare_expandable_cmd:Nnn}
1334 % \begin{macro}{\@@_declare_cmd_aux:Nnn}
1335 % \begin{macro}{\@@_declare_cmd_internal:Nnnn}
1336 % The main functions for creating commands set the appropriate flag then
1337 % use the same internal code to do the definition.
1338 % \begin{macrocode}
1339 \cs_new_protected:Npn \@@_declare_cmd:Nnn
1340 {
1341 \bool_set_false:N \l_@@_expandable_bool
1342 \@@_declare_cmd_aux:Nnn
1343 }
1344 \cs_new_protected:Npn \@@_declare_expandable_cmd:Nnn
1345 {
1346 \bool_set_true:N \l_@@_expandable_bool
1347 \@@_declare_cmd_aux:Nnn
1348 }
1349 % \end{macrocode}
1350 % The first stage is to log information, both for the user in the log and
1351 % for programmatic use in a property list of all declared commands.
1352 % \begin{macrocode}
1353 \cs_new_protected:Npn \@@_declare_cmd_aux:Nnn #1#2#3
1354 {
1355 \cs_if_exist:NTF #1
1356 {
1357 \msg_info:nnee { cmd } { redefine }
1358 { \token_to_str:N #1 } { \tl_to_str:n {#2} }
1359 }
1360 {
1361 \bool_lazy_or:nnT
1362 { \cs_if_exist_p:c { \cs_to_str:N #1 ~ code } }
1363 { \cs_if_exist_p:c { \cs_to_str:N #1 ~ defaults } }
1364 {
1365 \msg_warning:nne { cmd } { unsupported-let }
1366 { \token_to_str:N #1 }
1367 }
1368 \msg_info:nnee { cmd } { define-command }
1369 { \token_to_str:N #1 } { \tl_to_str:n {#2} }
1370 }
1371 \bool_set_false:N \l_@@_environment_bool
1372 \@@_declare_cmd_internal:Nnnn #1 {#2} {#3} { }
1373 }
1374 % \end{macrocode}
1375 % At definition time, the variable \cs{l_@@_fn_tl} is only used for error messages.
1376 % The real business of defining a document command starts with setting up
1377 % the appropriate name, then normalizing the argument specification to get rid of
1378 % shorthands.
1379 % \begin{macrocode}
1380 \cs_new_protected:Npn \@@_declare_cmd_internal:Nnnn #1#2#3#4
1381 {
1382 \tl_set:Ne \l_@@_function_tl { \cs_to_str:N #1 }
1383 \@@_normalize_arg_spec:n {#2}
1384 \exp_args:No \@@_prepare_signature:n \l_@@_arg_spec_tl
1385 \@@_declare_cmd_code:Nnn #1 {#2} {#3}
1386 #4
1387 \@@_break_point:n {#2}
1388 }
1389 % \end{macrocode}
1390 % \end{macro}
1391 % \end{macro}
1392 % \end{macro}
1393 %
1394 % \begin{macro}{\@@_break_point:n}
1395 % A marker used to escape from creating a definition if necessary.
1396 % \begin{macrocode}
1397 \cs_new_eq:NN \@@_break_point:n \use_none:n
1398 % \end{macrocode}
1399 % \end{macro}
1400 %
1401 % \begin{macro}[EXP]{\@@_all_m_check:n, \@@_all_m_check_aux:n}
1402 % A quick loop to check for all |(+)m|-type arguments.
1403 % \begin{macrocode}
1404 \cs_new:Npn \@@_all_m_check:n #1
1405 { \tl_map_function:nN {#1} \@@_all_m_check_aux:n }
1406 \cs_new:Npn \@@_all_m_check_aux:n #1
1407 {
1408 \str_if_eq:nnF {#1} { m }
1409 {
1410 \str_if_eq:nnF {#1} { + }
1411 { X }
1412 }
1413 }
1414 % \end{macrocode}
1415 % \end{macro}
1416 %
1417 % \begin{macro}{\@@_declare_cmd_code:Nnn}
1418 % \begin{macro}
1419 % {
1420 % \@@_declare_cmd_optimized:Nnn,
1421 % \@@_declare_cmd_code_aux:Nnn,
1422 % \@@_declare_cmd_code_expandable:Nnn
1423 % }
1424 % \begin{macro}{\@@_start_optimized:}
1425 % \changes{v1.2b}{2023/12/01}
1426 % {Optimize cmd creation for all-\texttt{m} arguments}
1427 % At this stage we can check for a short-cut possibility: if the argument
1428 % specification is made up of just |(+)m| tokens, and if all arguments are
1429 % either short or long, then we can produce an optimized document command.
1430 % This only applies to document commands, not creation of environments (which
1431 % are more complex).
1432 % \begin{macrocode}
1433 \cs_new_protected:Npn \@@_declare_cmd_code:Nnn #1#2
1434 {
1435 \bool_lazy_any:nTF
1436 {
1437 { \l_@@_environment_bool }
1438 {
1439 \bool_lazy_and_p:nn
1440 { \l_@@_some_short_bool }
1441 { \l_@@_some_long_bool }
1442 }
1443 { ! \tl_if_blank_p:e { \@@_all_m_check:n {#2} } }
1444 }
1445 {
1446 \tl_set:Ne \l_@@_fn_tl
1447 { \exp_not:c { \l_@@_function_tl \c_space_tl } }
1448 \bool_if:NTF \l_@@_grab_expandably_bool
1449 { \@@_declare_cmd_code_expandable:Nnn }
1450 { \@@_declare_cmd_code_aux:Nnn }
1451 }
1452 { \@@_declare_cmd_optimized:Nnn }
1453 #1 {#2}
1454 }
1455 % \end{macrocode}
1456 % The optimized version of commands just has to worry about whether to make
1457 % them protected or long. The commands start with an expandable marker so
1458 % that other parts of the kernel know these are set up by \pkg{ltcmd}.
1459 % We need the two layers of redirection so that the \texttt{code} internal
1460 % function has the same form as it would for any other document command.
1461 % Optimization means that there is no \cs{group_align_safe_begin:} before
1462 % grabbing the arguments, so anything involving |&| tokens will not work.
1463 % However, this is really only intended for making optional argument
1464 % processing safe anyway, so in practice should not be an issue.
1465 % \begin{macrocode}
1466 \cs_new_protected:Npn \@@_declare_cmd_optimized:Nnn #1#2#3
1467 {
1468 \bool_if:NTF \l_@@_expandable_bool
1469 { \cs_set_nopar:Npe }
1470 { \cs_set_protected_nopar:Npe }
1471 #1
1472 {
1473 \exp_not:N \@@_start_optimized:
1474 \exp_not:c { \l_@@_function_tl \c_space_tl code }
1475 }
1476 \exp_args:Ncc \cs_generate_from_arg_count:NNnn
1477 { \l_@@_function_tl \c_space_tl code }
1478 {
1479 cs_set
1480 \bool_if:NF \l_@@_expandable_bool { _protected }
1481 \bool_if:NF \l_@@_some_long_bool { _nopar }
1482 :Npn
1483 }
1484 \l_@@_current_arg_int
1485 {#3}
1486 }
1487 \cs_new:Npn \@@_start_optimized: { }
1488 % \end{macrocode}
1489 % Standard functions call \cs{@@_start:nNNnnn}, which receives the
1490 % argument specification, an auxiliary used for
1491 % grabbing arguments, an auxiliary containing the code, and then the
1492 % signature, default arguments, and processors.
1493 % \begin{macrocode}
1494 \cs_new_protected:Npn \@@_declare_cmd_code_aux:Nnn #1#2#3
1495 {
1496 \cs_generate_from_arg_count:cNnn
1497 { \l_@@_function_tl \c_space_tl code }
1498 \cs_set_protected:Npn \l_@@_current_arg_int {#3}
1499 \cs_set_protected_nopar:Npe #1
1500 {
1501 \bool_if:NTF \l_@@_environment_bool
1502 {
1503 \@@_start_env:nnnnn { \exp_not:n {#2} }
1504 { \l_@@_environment_str }
1505 }
1506 {
1507 \@@_start:nNNnnn { \exp_not:n {#2} }
1508 \exp_not:c { \l_@@_function_tl \c_space_tl }
1509 \exp_not:c { \l_@@_function_tl \c_space_tl code }
1510 }
1511 { \exp_not:o \l_@@_signature_tl }
1512 {
1513 \bool_if:NT \l_@@_defaults_bool
1514 { \exp_not:o \l_@@_defaults_tl }
1515 }
1516 {
1517 \bool_if:NT \l_@@_process_some_bool
1518 { \exp_not:o \l_@@_process_all_tl }
1519 }
1520 }
1521 }
1522 % \end{macrocode}
1523 % Expandable functions and functions whose arguments can be grabbed
1524 % expandably call \cs{@@_start_expandable:nNNNNn}, which receives the
1525 % argument specification, four auxiliaries (two for grabbing arguments, one for
1526 % the code, and one for default arguments), and finally the signature.
1527 % Non-expandable functions that take this branch should nevertheless
1528 % be protected, as well as their \texttt{code} function. They will
1529 % only be expanded in contexts such as constructing a csname.
1530 % The two grabbers (named after the function with one or two spaces)
1531 % are needed when there are both short and long arguments; otherwise
1532 % the same grabber is included twice in the definition. If all
1533 % arguments are long or all are short the (only) grabber is defined
1534 % correspondingly to be long/short. Otherwise two grabbers are
1535 % defined, one long, one short.
1536 % \begin{macrocode}
1537 \cs_new_protected:Npn \@@_declare_cmd_code_expandable:Nnn #1#2#3
1538 {
1539 \exp_args:Ncc \cs_generate_from_arg_count:NNnn
1540 { \l_@@_function_tl \c_space_tl code }
1541 { cs_set \bool_if:NF \l_@@_expandable_bool { _protected } :Npn }
1542 \l_@@_current_arg_int {#3}
1543 \bool_if:NT \l_@@_defaults_bool
1544 {
1545 \use:e
1546 {
1547 \cs_generate_from_arg_count:cNnn
1548 { \l_@@_function_tl \c_space_tl defaults }
1549 \cs_set:Npn \l_@@_current_arg_int
1550 { \exp_not:o \l_@@_defaults_tl }
1551 }
1552 }
1553 \bool_if:NTF \l_@@_expandable_bool
1554 { \cs_set_nopar:Npe } { \cs_set_protected_nopar:Npe } #1
1555 {
1556 \exp_not:N \@@_start_expandable:nNNNNn
1557 { \exp_not:n {#2} }
1558 \exp_not:c { \l_@@_function_tl \c_space_tl }
1559 \exp_not:c
1560 {
1561 \l_@@_function_tl \c_space_tl
1562 \bool_if:NT \l_@@_some_short_bool
1563 { \bool_if:NT \l_@@_some_long_bool { \c_space_tl } }
1564 }
1565 \exp_not:c { \l_@@_function_tl \c_space_tl code }
1566 \bool_if:NTF \l_@@_defaults_bool
1567 { \exp_not:c { \l_@@_function_tl \c_space_tl defaults } }
1568 { ? }
1569 { \exp_not:o \l_@@_signature_tl }
1570 }
1571 \bool_if:NTF \l_@@_some_long_bool
1572 {
1573 \bool_if:NT \l_@@_some_short_bool
1574 {
1575 \cs_set_nopar:cpe { \l_@@_function_tl \c_space_tl \c_space_tl }
1576 ##1##2 { ##1 {##2} }
1577 }
1578 \cs_set:cpe
1579 }
1580 { \cs_set_nopar:cpe }
1581 { \l_@@_function_tl \c_space_tl } ##1##2 { ##1 {##2} }
1582 }
1583 % \end{macrocode}
1584 % \end{macro}
1585 % \end{macro}
1586 % \end{macro}
1587 %
1588 % \begin{macro}{\@@_declare_env:nnnn, \@@_declare_env:ennn}
1589 % \changes{v1.2f}{2024/07/06}{Use space-trimmed envname directly (gh/1399)}
1590 % \begin{macro}{\@@_declare_env_internal:nnnn}
1591 % \begin{macro}{\@@_set_environment_end:n}
1592 % The lead-off to creating an environment is much the same as that for
1593 % creating a command: issue the appropriate message, store the argument
1594 % specification then hand off to an internal function.
1595 % \begin{macrocode}
1596 %<latexrelease>\IncludeInRelease{2024/11/01}{\@@_declare_env:nnnn}%
1597 %<latexrelease> {Use~space-trimmed~envname~directly}
1598 \cs_new_protected:Npn \@@_declare_env:nnnn #1#2
1599 {
1600 \str_set:Nn \l_@@_environment_str {#1}
1601 \cs_if_exist:cTF { #1 }
1602 { \msg_info:nnnn { cmd } { redefine-env } { #1 } { #2 } }
1603 { \msg_info:nnnn { cmd } { define-env } { #1 } { #2 } }
1604 \bool_set_false:N \l_@@_expandable_bool
1605 \bool_set_true:N \l_@@_environment_bool
1606 \@@_declare_env_internal:nnnn {#1} {#2}
1607 }
1608 \cs_generate_variant:Nn \@@_declare_env:nnnn { e }
1609 %<latexrelease>\EndIncludeInRelease
1610 %<latexrelease>\IncludeInRelease{2024/06/01}{\@@_declare_env:nnnn}%
1611 %<latexrelease> {Use~space-trimmed~envname~directly}
1612 %<latexrelease>\cs_new_protected:Npn \@@_declare_env:nnnn #1#2
1613 %<latexrelease> {
1614 %<latexrelease> \str_set:Nx \l_@@_environment_str {#1}
1615 %<latexrelease> \str_set:Nx \l_@@_environment_str
1616 %<latexrelease> { \tl_trim_spaces:o { \l_@@_environment_str } }
1617 %<latexrelease> \cs_if_exist:cTF { \l_@@_environment_str }
1618 %<latexrelease> {
1619 %<latexrelease> \msg_info:nnxx { cmd } { redefine-env }
1620 %<latexrelease> { \l_@@_environment_str } { \tl_to_str:n {#2} }
1621 %<latexrelease> }
1622 %<latexrelease> {
1623 %<latexrelease> \msg_info:nnxx { cmd } { define-env }
1624 %<latexrelease> { \l_@@_environment_str } { \tl_to_str:n {#2} }
1625 %<latexrelease> }
1626 %<latexrelease> \bool_set_false:N \l_@@_expandable_bool
1627 %<latexrelease> \bool_set_true:N \l_@@_environment_bool
1628 %<latexrelease> \exp_args:NV \@@_declare_env_internal:nnnn
1629 %<latexrelease> \l_@@_environment_str {#2}
1630 %<latexrelease> }
1631 %<latexrelease>
1632 %<latexrelease>\EndIncludeInRelease
1633 % \end{macrocode}
1634 % Creating a document environment requires a few more steps than creating
1635 % a single command. In order to pass the arguments of the command to the
1636 % end of the function, it is necessary to store the grabbed arguments.
1637 % To do that, the function used at the end of the environment has to be
1638 % redefined to contain the appropriate information. To minimize the amount
1639 % of expansion at point of use, the code here is expanded now as well as
1640 % when used.
1641 % The last argument of \cs{@@_declare_cmd_internal:Nnnn} is only run
1642 % if the definition succeeded. In package mode this ensures that the
1643 % original definition of the environment is not changed if the
1644 % definition fails for any reason. This also avoids an error when
1645 % defining the \verb*|end aux | function when the user asks for more
1646 % than $9$ arguments.
1647 % \begin{macrocode}
1648 \cs_new_protected:Npn \@@_declare_env_internal:nnnn #1#2#3#4
1649 {
1650 \exp_args:Nc \@@_declare_cmd_internal:Nnnn { environment~ #1 } {#2}
1651 {#3}
1652 {
1653 \cs_set_nopar:cpe { environment~ #1 ~end }
1654 { \exp_not:c { environment~ #1 ~end~aux } }
1655 \cs_generate_from_arg_count:cNnn
1656 { environment~ #1 ~end~aux~ } \cs_set:Npn
1657 \l_@@_current_arg_int {#4}
1658 \cs_set_eq:cc {#1} { environment~ #1 }
1659 \cs_set_eq:cc { end #1 } { environment~ #1 ~end }
1660 }
1661 }
1662 \cs_new_protected:Npn \@@_set_environment_end:n #1
1663 {
1664 \cs_set_nopar:cpe { environment~ #1 ~end~aux }
1665 {
1666 \exp_not:c { environment~ #1 ~end~aux~ }
1667 \exp_not:o \l_@@_args_tl
1668 }
1669 }
1670 % \end{macrocode}
1671 % \end{macro}
1672 % \end{macro}
1673 % \end{macro}
1674 %
1675 % \subsection{Structure of \pkg{xparse} commands}
1676 %
1677 % \begin{macro}{\@@_start_env:nnnnn, \@@_start:nNNnnn}
1678 % For error messages that occur during run-time when getting arguments
1679 % of environments it is necessary to keep track of the environment
1680 % name. We begin non-expandable commands with a token equal to
1681 % \cs{scan_stop:}, whose name gives a reasonable error message if the
1682 % command is used inside a csname and protects against
1683 % \texttt{f}-expansion. This is useless for environments since
1684 % \cs{begin} is already not expandable. Both the command and
1685 % environment codes start with \cs{group_align_safe_begin:}, then
1686 % \cs{@@_run_code:} (used by both) does \cs{group_align_safe_end:}, so
1687 % that delimited arguments may be grabbed in alignments if they
1688 % contain and alignment tab token (see latex3/latex3/issues/839).
1689 % \begin{macrocode}
1690 \cs_new_protected:Npn \@@_start_env:nnnnn #1#2
1691 {
1692 \conditionally@traceoff
1693 \group_align_safe_begin:
1694 \str_set:Nn \l_@@_environment_str {#2}
1695 \bool_set_true:N \l_@@_environment_bool
1696 \@@_start_aux:ccnnnn
1697 { environment~ \l_@@_environment_str \c_space_tl }
1698 { environment~ \l_@@_environment_str \c_space_tl code }
1699 {#1}
1700 }
1701 \cs_new_protected:Npe \@@_start:nNNnnn #1#2#3
1702 {
1703 \exp_not:c { xparse~function~is~not~expandable }
1704 \exp_not:N \conditionally@traceoff
1705 \exp_not:N \group_align_safe_begin:
1706 \exp_not:n { \bool_set_false:N \l_@@_environment_bool }
1707 \exp_not:N \@@_start_aux:NNnnnn
1708 #2 #3 {#1}
1709 }
1710 % \end{macrocode}
1711 % \end{macro}
1712 %
1713 % \begin{macro}{\@@_start_aux:NNnnnn, \@@_start_aux:ccnnnn}
1714 % \changes{v1.0i}{2021/12/02}
1715 % {Correct defaults for optional arguments in end-of-environment code (gh/712)}
1716 % This sets up a few variables to minimize the boilerplate code
1717 % included in all \pkg{xparse}-defined commands. It then runs the
1718 % grabbers~|#4|. Again, the argument specification |#1| is only for
1719 % diagnostics.
1720 % \begin{macrocode}
1721 \cs_new_protected:Npn \@@_start_aux:NNnnnn #1#2#3#4#5#6
1722 {
1723 \tl_clear:N \l_@@_args_tl
1724 \tl_set:Nn \l_@@_fn_tl {#1}
1725 \tl_set:Nn \l_@@_fn_code_tl {#2}
1726 \tl_set:Nn \l_@@_defaults_tl {#5}
1727 \tl_set:Nn \l_@@_process_all_tl {#6}
1728 #4
1729 \@@_run_code:
1730 }
1731 \cs_generate_variant:Nn \@@_start_aux:NNnnnn { cc }
1732 % \end{macrocode}
1733 % \end{macro}
1734 %
1735 % \begin{macro}{\@@_run_code:}
1736 % \changes{v1.0i}{2021/12/02}
1737 % {Correct defaults for optional arguments in end-of-environment code (gh/712)}
1738 % After arguments are grabbed, this function is responsible for
1739 % inserting default values, running processors, and finally doing
1740 % \cs{group_align_safe_end:} as promised, and running the code.
1741 % \begin{macrocode}
1742 \cs_new_protected:Npn \@@_run_code:
1743 {
1744 \tl_if_empty:NF \l_@@_defaults_tl { \@@_defaults: }
1745 \tl_if_empty:NF \l_@@_process_all_tl { \@@_args_process: }
1746 \bool_if:NT \l_@@_environment_bool
1747 { \exp_args:No \@@_set_environment_end:n \l_@@_environment_str }
1748 \group_align_safe_end:
1749 \conditionally@traceon
1750 \exp_after:wN \l_@@_fn_code_tl \l_@@_args_tl
1751 }
1752 % \end{macrocode}
1753 % \end{macro}
1754 %
1755 % \begin{macro}{\@@_defaults:}
1756 % \begin{macro}{\@@_defaults_def:, \@@_defaults_def:nn, \@@_defaults_def:nnn}
1757 % \begin{macro}{\@@_defaults_aux:, \@@_defaults_error:w}
1758 % First construct \cs{@@_tmp:w} (see below) that will receive
1759 % the arguments found so far and determine default values for any
1760 % missing argument. Then call it repeatedly until the set of
1761 % arguments stabilizes. Since that could lead to an infinite loop we
1762 % only call it up to nine times, the maximal number needed for
1763 % stabilization if there is a chain of arguments that depend on each
1764 % other. If that fails to stabilize raise an error.
1765 % \begin{macrocode}
1766 \cs_new_protected:Npn \@@_defaults:
1767 {
1768 \@@_defaults_def:
1769 \tl_set_eq:NN \l_@@_args_i_tl \l_@@_args_tl
1770 \@@_defaults_aux: \@@_defaults_aux: \@@_defaults_aux:
1771 \@@_defaults_aux: \@@_defaults_aux: \@@_defaults_aux:
1772 \@@_defaults_aux: \@@_defaults_aux: \@@_defaults_aux:
1773 \@@_defaults_error:w
1774 \q_recursion_stop
1775 \tl_set_eq:NN \l_@@_args_tl \l_@@_args_i_tl
1776 }
1777 \cs_new_protected:Npn \@@_defaults_aux:
1778 {
1779 \tl_set:Ne \l_@@_args_ii_tl
1780 { \exp_after:wN \@@_tmp:w \l_@@_args_i_tl }
1781 \tl_if_eq:NNT \l_@@_args_ii_tl \l_@@_args_i_tl
1782 { \use_none_delimit_by_q_recursion_stop:w }
1783 \tl_set_eq:NN \l_@@_args_i_tl \l_@@_args_ii_tl
1784 }
1785 \cs_new_protected:Npn \@@_defaults_error:w \q_recursion_stop
1786 {
1787 \msg_error:nne { cmd } { default-loop }
1788 { \@@_environment_or_command: }
1789 }
1790 % \end{macrocode}
1791 %
1792 % \changes{v1.1e}{2023/05/26}
1793 % {Use simpler variant \cs{cs_generate_from_arg_count:NNno}}
1794 % \changes{v1.3k}{2026-03-13}{Tidy up variants}
1795 % To construct \cs{@@_tmp:w}, first go through the arguments
1796 % found and the corresponding defaults, building a token list with
1797 % |{#|\meta{arg number}|}| for arguments found in the input (whose
1798 % default will not be used) and otherwise
1799 % |{|\cs{exp_not:n}\Arg{default}|}| for arguments whose default will
1800 % be used.
1801 % \begin{macrocode}
1802 \cs_new_protected:Npn \@@_defaults_def:
1803 {
1804 \tl_clear:N \l_@@_tmpa_tl
1805 \int_zero:N \l_@@_current_arg_int
1806 \@@_tl_mapthread_function:NNN \l_@@_args_tl \l_@@_defaults_tl
1807 \@@_defaults_def:nn
1808 \cs_generate_from_arg_count:NNno \@@_tmp:w \cs_set:Npn
1809 \l_@@_current_arg_int \l_@@_tmpa_tl
1810 }
1811 \cs_new_protected:Npn \@@_defaults_def:nn
1812 {
1813 \int_incr:N \l_@@_current_arg_int
1814 \exp_args:NV \@@_defaults_def:nnn \l_@@_current_arg_int
1815 }
1816 \cs_new_protected:Npn \@@_defaults_def:nnn #1#2#3
1817 {
1818 \tl_put_right:Ne \l_@@_tmpa_tl
1819 {
1820 {
1821 \exp_not:N \exp_not:n
1822 {
1823 \tl_if_novalue:nTF {#2}
1824 { \exp_not:o {#3} }
1825 { \exp_not:n { ## #1 } }
1826 }
1827 }
1828 }
1829 }
1830 % \end{macrocode}
1831 % \end{macro}
1832 % \end{macro}
1833 % \end{macro}
1834 %
1835 % \begin{macro}{\@@_args_process:}
1836 % \begin{macro}{\@@_args_process_loop:nn, \@@_args_process_aux:n}
1837 % Loop through arguments (stored in \cs{l_@@_args_tl}) and the
1838 % corresponding processors (in \cs{l_@@_process_all_tl})
1839 % simultaneously, apply all processors for each argument and store the
1840 % result back into \cs{l_@@_args_tl}. To allow processors to depend
1841 % on other arguments, for every processor define a temporary auxiliary
1842 % that receives all arguments \cs{l_@@_args_tl}.
1843 % \begin{macrocode}
1844 \cs_new_protected:Npn \@@_args_process:
1845 {
1846 \tl_clear:N \l_@@_args_ii_tl
1847 \@@_tl_mapthread_function:NNN
1848 \l_@@_args_tl
1849 \l_@@_process_all_tl
1850 \@@_args_process_loop:nn
1851 \tl_set_eq:NN \l_@@_args_tl \l_@@_args_ii_tl
1852 }
1853 \cs_new_protected:Npn \@@_args_process_loop:nn #1#2
1854 {
1855 \tl_set:Nn \ProcessedArgument {#1}
1856 \tl_if_novalue:nF {#1}
1857 { \tl_map_function:nN {#2} \@@_args_process_aux:n }
1858 \tl_put_right:No \l_@@_args_ii_tl
1859 { \exp_after:wN { \ProcessedArgument } }
1860 }
1861 \cs_new_protected:Npn \@@_args_process_aux:n #1
1862 {
1863 \cs_generate_from_arg_count:NNnn \@@_tmp:w \cs_set:Npn
1864 { \tl_count:N \l_@@_args_tl } {#1}
1865 \exp_args:NNNo \exp_after:wN \@@_tmp:w \l_@@_args_tl
1866 { \ProcessedArgument }
1867 }
1868 % \end{macrocode}
1869 % \end{macro}
1870 % \end{macro}
1871 %
1872 % \begin{macro}[EXP]{\@@_start_expandable:nNNNNn}
1873 % This is called for all expandable commands. |#6| is the signature,
1874 % responsible for grabbing arguments. |#5| is used to determine
1875 % default values (or is |?| if there are none). |#4| is the code to run.
1876 % |#2|~and~|#3| are functions (named after the command) that grab a single
1877 % argument in the input stream (|#3|~is~short). The argument specification |#1| is
1878 % only used by diagnostic functions. Same as for the non-expandable
1879 % version, this starts with \cs{group_align_safe_begin:}, which
1880 % expands to nothing, so may be safely used in an expandable context.
1881 % \begin{macrocode}
1882 \cs_new:Npn \@@_start_expandable:nNNNNn #1#2#3#4#5#6
1883 {
1884 \group_align_safe_begin:
1885 #6 \@@_end_expandable:NNw #5 #4 \q_@@ #2#3
1886 }
1887 % \end{macrocode}
1888 % \end{macro}
1889 %
1890 % \begin{macro}[EXP]{\@@_end_expandable:NNw}
1891 % \begin{macro}[EXP]{\@@_end_expandable_aux:w}
1892 % \begin{macro}[EXP]{\@@_end_expandable_aux:nNNNN}
1893 % \begin{macro}[EXP]{\@@_end_expandable_defaults:nnnNNn}
1894 % \begin{macro}[EXP]{\@@_end_expandable_defaults:nnw}
1895 % \begin{macro}[EXP]{\@@_end_expandable_defaults:nw}
1896 % Followed by a function |#1| to determine default values (or |?| if
1897 % there are no defaults), the code
1898 % |#2|, arguments that have been grabbed, then \cs{q_@@} and two generic
1899 % grabbers. The idea to find default values is similar to the
1900 % non-expandable case but we cannot define an auxiliary function, so
1901 % at every step in the loop we need to go through all arguments
1902 % searching for which ones started out as \cs{NoValue} and replacing
1903 % these by the newly computed values. In fact we need to keep track
1904 % of three versions of all arguments: the original version, the
1905 % previous version with default values, and the currently built
1906 % version (first argument of \cs{@@_end_expandable_defaults:nnnNNn}).
1907 % \begin{macrocode}
1908 \cs_new:Npn \@@_end_expandable:NNw #1#2
1909 { \@@_end_expandable_aux:w #1#2 \prg_do_nothing: }
1910 \cs_new:Npn \@@_end_expandable_aux:w #1#2#3 \q_@@
1911 { \exp_args:No \@@_end_expandable_aux:nNNNN {#3} #1 #2 }
1912 \cs_new:Npn \@@_end_expandable_aux:nNNNN #1#2#3#4#5
1913 {
1914 \token_if_eq_charcode:NNT ? #2 { \exp_after:wN \use_iv:nnnn }
1915 \@@_end_expandable_defaults:nnnNNn {#1} { } {#1} #2#3
1916 { } { } { } { } { } { } { } { } { } { }
1917 {
1918 \msg_expandable_error:nnf { cmd } { default-loop }
1919 { \exp_args:Nf \tl_trim_spaces:n { \token_to_str:N #4 } }
1920 \use_iv:nnnn
1921 }
1922 \q_stop
1923 }
1924 \cs_new:Npn \@@_end_expandable_defaults:nnnNNn #1#2#3#4#5#6
1925 {
1926 #6
1927 \str_if_eq:nnTF {#1} {#2}
1928 { \use_i_delimit_by_q_stop:nw { \group_align_safe_end: #5 #1 } }
1929 {
1930 \exp_args:No \@@_tl_mapthread_function:nnN
1931 { #4 #1 } {#3}
1932 \@@_end_expandable_defaults:nnw
1933 \@@_end_expandable_defaults:nnnNNn { } {#1} {#3} #4 #5
1934 }
1935 }
1936 \cs_new:Npn \@@_end_expandable_defaults:nnw #1#2
1937 {
1938 \tl_if_novalue:nTF {#2}
1939 { \exp_args:No \@@_end_expandable_defaults:nw {#1} }
1940 { \@@_end_expandable_defaults:nw {#2} }
1941 }
1942 \cs_new:Npn \@@_end_expandable_defaults:nw
1943 #1#2 \@@_end_expandable_defaults:nnnNNn #3
1944 { #2 \@@_end_expandable_defaults:nnnNNn { #3 {#1} } }
1945 % \end{macrocode}
1946 % \end{macro}
1947 % \end{macro}
1948 % \end{macro}
1949 % \end{macro}
1950 % \end{macro}
1951 % \end{macro}
1952 %
1953 % \subsection{Normalizing the argument specifications}
1954 %
1955 % The goal here is to expand aliases and check that the argument
1956 % specification is valid before the main parsing run. If it is not
1957 % valid the entire set up is abandoned to avoid any strange internal
1958 % errors. A function is provided for each argument type that will grab
1959 % any extra data items and call the loop function after performing the
1960 % following checks and tasks.
1961 % \begin{itemize}
1962 % \item Check that each argument has the correct number of data items
1963 % associated with it, and that where a single character is required,
1964 % one has actually been supplied.
1965 % \item Check that processors and the markers~|+|, |!| and~|=| are followed
1966 % by an argument for which they make sense, and are not redundant.
1967 % \item Check the absence of forbidden types for expandable commands,
1968 % namely \texttt{G}/\texttt{v} always, and \texttt{l}/\texttt{u}
1969 % after optional arguments (\pkg{xparse} may have inserted braces
1970 % due to a failed search for an optional argument).
1971 % \item Check that no optional argument is followed by a mandatory
1972 % argument with the same delimiter, as otherwise the optional
1973 % argument could never be omitted.
1974 % \item Keep track in \cs{l_@@_some_long_bool} and
1975 % \cs{l_@@_some_short_bool} of whether the command has some
1976 % long/short arguments.
1977 % \item Keep track in \cs{l_@@_grab_expandably_bool} of whether all
1978 % arguments are \texttt{m}/\texttt{l}/\texttt{u} type and short
1979 % arguments appear before long ones, in which case they can be
1980 % grabbed expandably just as safely as they could be grabbed
1981 % nonexpandably. Regardless of that, arguments of expandable
1982 % commands will be grabbed expandably and arguments of environments
1983 % will not (because the list of arguments built by non-expandable
1984 % grabbing is used to pass them to the end-environment code).
1985 % \end{itemize}
1986 % Further checks happen at the end of the loop:
1987 % \begin{itemize}
1988 % \item that there are at most $9$ arguments;
1989 % \item that an expandable command does not end with an optional
1990 % argument (this case is detected by using the fact that
1991 % \cs{l_@@_last_delimiters_tl} is cleared by every mandatory argument
1992 % and filled by every optional argument).
1993 % \end{itemize}
1994 %
1995 % \begin{macro}{\@@_normalize_arg_spec:n}
1996 % \changes{v1.3c}{2025-04-12}{Track total mandatory args}
1997 % \begin{macro}{\@@_normalize_arg_spec_loop:n}
1998 % Loop through the argument specification, calling an auxiliary
1999 % specific to each argument type. If any argument is unknown stop the
2000 % definition.
2001 % \begin{macrocode}
2002 \cs_new_protected:Npn \@@_normalize_arg_spec:n #1
2003 {
2004 \int_zero:N \l_@@_current_arg_int
2005 \int_zero:N \l_@@_last_mandatory_arg_int
2006 \tl_clear:N \l_@@_last_delimiters_tl
2007 \tl_clear:N \l_@@_arg_spec_tl
2008 \bool_set_true:N \l_@@_grab_expandably_bool
2009 \bool_set_false:N \l_@@_obey_spaces_bool
2010 \bool_set_false:N \l_@@_long_bool
2011 \bool_set_false:N \l_@@_suppress_strip_bool
2012 \bool_set_false:N \l_@@_some_obey_spaces_bool
2013 \bool_set_false:N \l_@@_some_long_bool
2014 \bool_set_false:N \l_@@_some_short_bool
2015 \@@_normalize_arg_spec_loop:n #1
2016 \q_recursion_tail \q_recursion_tail \q_recursion_tail \q_recursion_stop
2017 \int_compare:nNnT \l_@@_current_arg_int > 9
2018 {
2019 \msg_error:nnee { cmd } { too-many-args }
2020 { \@@_environment_or_command: } { \tl_to_str:n {#1} }
2021 \@@_bad_def:wn
2022 }
2023 \bool_if:NT \l_@@_expandable_bool
2024 {
2025 \tl_if_empty:NF \l_@@_last_delimiters_tl
2026 {
2027 \msg_error:nnee { cmd } { expandable-ending-optional }
2028 { \iow_char:N \\ \l_@@_function_tl } { \tl_to_str:n {#1} }
2029 \@@_bad_def:wn
2030 }
2031 }
2032 \bool_if:NT \l_@@_expandable_bool
2033 { \bool_set_true:N \l_@@_grab_expandably_bool }
2034 \bool_if:NT \l_@@_environment_bool
2035 { \bool_set_false:N \l_@@_grab_expandably_bool }
2036 }
2037 \cs_new_protected:Npn \@@_normalize_arg_spec_loop:n #1
2038 {
2039 \quark_if_recursion_tail_stop:n {#1}
2040 \int_incr:N \l_@@_current_arg_int
2041 \cs_if_exist_use:cF { @@_normalize_type_ \tl_to_str:n {#1} :w }
2042 {
2043 \bool_lazy_any:nTF
2044 {
2045 { \str_if_eq_p:nn {#1} { G } }
2046 { \str_if_eq_p:nn {#1} { g } }
2047 { \str_if_eq_p:nn {#1} { l } }
2048 { \str_if_eq_p:nn {#1} { u } }
2049 }
2050 {
2051 \msg_error:nnee { cmd } { xparse-arg-type }
2052 { \@@_environment_or_command: } { \tl_to_str:n {#1} }
2053 }
2054 {
2055 \msg_error:nnee { cmd } { unknown-argument-type }
2056 { \@@_environment_or_command: } { \tl_to_str:n {#1} }
2057 }
2058 \@@_bad_def:wn
2059 }
2060 }
2061 % \end{macrocode}
2062 % \end{macro}
2063 % \end{macro}
2064 %
2065 % \begin{macro}
2066 % {
2067 % \@@_normalize_type_d:w,
2068 % \@@_normalize_type_e:w,
2069 % \@@_normalize_type_o:w,
2070 % \@@_normalize_type_O:w,
2071 % \@@_normalize_type_r:w,
2072 % \@@_normalize_type_s:w,
2073 % }
2074 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
2075 % \changes{v1.3l}{2026-04-26}{Update \cs{@@_normalize_type_r:w} to use \cs{@@_normalize_type_R_aux:w} (gh/1204)}
2076 % These argument types are aliases of more general ones, for example
2077 % with the default argument \cs{NoValue}. For argument types that need
2078 % additional data, check that the data is present (not
2079 % \cs{q_recursion_tail}) before proceeding.
2080 % \begin{macrocode}
2081 \cs_new_protected:Npn \@@_normalize_type_d:w #1#2
2082 {
2083 \quark_if_recursion_tail_stop_do:nn {#2} { \@@_bad_arg_spec:wn }
2084 \@@_normalize_type_D:w {#1} {#2} { \NoValue }
2085 }
2086 \cs_new_protected:Npn \@@_normalize_type_e:w #1
2087 {
2088 \quark_if_recursion_tail_stop_do:nn {#1} { \@@_bad_arg_spec:wn }
2089 \@@_normalize_type_E:w {#1} { }
2090 }
2091 \cs_new_protected:Npn \@@_normalize_type_o:w
2092 { \@@_normalize_type_D:w [ ] { \NoValue } }
2093 \cs_new_protected:Npn \@@_normalize_type_O:w
2094 { \@@_normalize_type_D:w [ ] }
2095 \cs_new_protected:Npn \@@_normalize_type_r:w #1#2
2096 {
2097 \quark_if_recursion_tail_stop_do:nn {#2} { \@@_bad_arg_spec:wn }
2098 \@@_normalize_type_R_aux:w { r #1 #2 } {#1} {#2} { \NoValue }
2099 }
2100 \cs_new_protected:Npn \@@_normalize_type_s:w
2101 { \@@_normalize_type_t:w * }
2102 % \end{macrocode}
2103 % \end{macro}
2104 %
2105 % \begin{macro}
2106 % {
2107 % \@@_normalize_type_>:w,
2108 % \@@_normalize_type_+:w,
2109 % \@@_normalize_type_!:w,
2110 % \@@_normalize_type_=:w
2111 % }
2112 % \changes{v1.1a}{2022/08/10}{Refactor to use common auxiliary}
2113 % \changes{v1.1a}{2022/08/10}{Add support for \texttt{=} modifier}
2114 % \begin{macro}{\@@_normalize_type_aux:NnNn}
2115 % Check that these prefixes have arguments, namely that the next token
2116 % is not \cs{q_recursion_tail}, and remember to leave it after the
2117 % looping macro. Processors are forbidden in expandable commands.
2118 % If all is good, store the prefix in the cleaned up
2119 % \cs{l_@@_arg_spec_tl}, and decrement the argument number as prefixes
2120 % do not correspond to arguments.
2121 % \begin{macrocode}
2122 \cs_new_protected:cpn { @@_normalize_type_>:w } #1#2
2123 {
2124 \quark_if_recursion_tail_stop_do:nn {#2} { \@@_bad_arg_spec:wn }
2125 \bool_if:NT \l_@@_expandable_bool
2126 {
2127 \msg_error:nnee { cmd } { processor-in-expandable }
2128 { \iow_char:N \\ \l_@@_function_tl } { \tl_to_str:n {#1} }
2129 \@@_bad_def:wn
2130 }
2131 \tl_put_right:Ne \l_@@_arg_spec_tl { > { \tl_trim_spaces:n {#1} } }
2132 \int_decr:N \l_@@_current_arg_int
2133 \bool_set_false:N \l_@@_grab_expandably_bool
2134 \@@_normalize_arg_spec_loop:n {#2}
2135 }
2136 \cs_new_protected:cpn { @@_normalize_type_+:w } #1
2137 {
2138 \@@_normalize_type_aux:NnNn + {#1}
2139 \l_@@_long_bool
2140 { \bool_set_true:N \l_@@_long_bool }
2141 }
2142 \cs_new_protected:cpn { @@_normalize_type_!:w } #1
2143 {
2144 \@@_normalize_type_aux:NnNn ! {#1}
2145 \l_@@_obey_spaces_bool
2146 {
2147 \bool_set_true:N \l_@@_obey_spaces_bool
2148 \bool_set_true:N \l_@@_some_obey_spaces_bool
2149 }
2150 }
2151 \cs_new_protected:cpn { @@_normalize_type_=:w } #1#2
2152 {
2153 \@@_normalize_type_aux:NnNn = {#2}
2154 \l_@@_suppress_strip_bool
2155 {
2156 \bool_if:NT \l_@@_expandable_bool
2157 {
2158 \msg_error:nnee { cmd } { keyval-in-expandable }
2159 { \iow_char:N \\ \l_@@_function_tl } { \tl_to_str:n {#1} }
2160 \@@_bad_def:wn
2161 }
2162 \bool_set_true:N \l_@@_suppress_strip_bool
2163 \bool_set_false:N \l_@@_grab_expandably_bool
2164 \tl_put_right:Ne \l_@@_arg_spec_tl
2165 { = { \tl_trim_spaces:n {#1} } }
2166 }
2167 }
2168 \cs_new_protected:Npn \@@_normalize_type_aux:NnNn #1#2#3#4
2169 {
2170 \quark_if_recursion_tail_stop_do:nn {#2} { \@@_bad_arg_spec:wn }
2171 \bool_if:NT #3
2172 {
2173 \msg_error:nnee { cmd } { two-markers }
2174 { \@@_environment_or_command: } { #1 }
2175 \@@_bad_def:wn
2176 }
2177 #4
2178 \int_decr:N \l_@@_current_arg_int
2179 \@@_normalize_arg_spec_loop:n {#2}
2180 }
2181 % \end{macrocode}
2182 % \end{macro}
2183 % \end{macro}
2184 %
2185 % \begin{macro}
2186 % {
2187 % \@@_normalize_type_D:w,
2188 % \@@_normalize_type_E:w,
2189 % \@@_normalize_type_t:w,
2190 % }
2191 % \begin{macro}{\@@_normalize_E_unique_check:w}
2192 % Optional argument types. Check that all required data is present
2193 % (and consists of single characters if applicable) and check for
2194 % forbidden types for expandable commands. For \texttt{E}-type
2195 % require that there is at least one embellishment, that each one is a
2196 % single character, and that there aren't more optional arguments than
2197 % embellishments; also remember that each embellishment counts as one
2198 % argument for \cs{l_@@_current_arg_int}. Then in each case
2199 % store the data in \cs{l_@@_arg_spec_tl}, and
2200 % for later checks store in \cs{l_@@_last_delimiters_tl} the tokens
2201 % whose presence determines whether there is an optional argument (for
2202 % braces store |{}|, seen later as an empty delimiter).
2203 % \begin{macrocode}
2204 \cs_new_protected:Npn \@@_normalize_type_D:w #1#2#3
2205 {
2206 \quark_if_recursion_tail_stop_do:nn {#3} { \@@_bad_arg_spec:wn }
2207 \@@_single_token_check:n {#1} \@@_allowed_token_check:N #1
2208 \@@_single_token_check:n {#2}
2209 \@@_add_arg_spec:n { D #1 #2 {#3} }
2210 \tl_put_right:Nn \l_@@_last_delimiters_tl {#1}
2211 \bool_set_false:N \l_@@_grab_expandably_bool
2212 \@@_normalize_arg_spec_loop:n
2213 }
2214 \cs_new_protected:Npn \@@_normalize_type_E:w #1#2
2215 {
2216 \quark_if_recursion_tail_stop_do:nn {#2} { \@@_bad_arg_spec:wn }
2217 \tl_if_blank:nT {#1} { \@@_bad_arg_spec:wn }
2218 \tl_map_function:nN {#1} \@@_single_token_check:n
2219 \tl_map_function:nN {#1} \@@_allowed_token_check:N
2220 \@@_normalize_E_unique_check:w #1 \q_nil \q_stop
2221 \int_compare:nNnT { \tl_count:n {#2} } > { \tl_count:n {#1} }
2222 { \@@_bad_arg_spec:wn }
2223 \@@_add_arg_spec:n { E {#1} {#2} }
2224 \tl_put_right:Nn \l_@@_last_delimiters_tl {#1}
2225 \bool_set_false:N \l_@@_grab_expandably_bool
2226 \int_add:Nn \l_@@_current_arg_int { \tl_count:n {#1} - 1 }
2227 \@@_normalize_arg_spec_loop:n
2228 }
2229 \cs_new_protected:Npn \@@_normalize_E_unique_check:w #1#2 \q_stop
2230 {
2231 \quark_if_nil:NF #1
2232 {
2233 \tl_if_in:nnT {#2} {#1} { \@@_bad_arg_spec:wn }
2234 \@@_normalize_E_unique_check:w #2 \q_stop
2235 }
2236 }
2237 \cs_new_protected:Npn \@@_normalize_type_t:w #1
2238 {
2239 \quark_if_recursion_tail_stop_do:Nn #1 { \@@_bad_arg_spec:wn }
2240 \@@_single_token_check:n {#1} \@@_allowed_token_check:N #1
2241 \tl_put_right:Ne \l_@@_arg_spec_tl
2242 {
2243 \bool_if:NT \l_@@_obey_spaces_bool { ! }
2244 t \exp_not:n {#1}
2245 }
2246 \tl_put_right:Nn \l_@@_last_delimiters_tl {#1}
2247 \bool_set_false:N \l_@@_grab_expandably_bool
2248 \bool_set_false:N \l_@@_obey_spaces_bool
2249 \bool_set_false:N \l_@@_long_bool
2250 \@@_normalize_arg_spec_loop:n
2251 }
2252 % \end{macrocode}
2253 % \end{macro}
2254 % \end{macro}
2255 %
2256 % \begin{macro}
2257 % {
2258 % \@@_normalize_type_m:w,
2259 % \@@_normalize_type_R:w,
2260 % \@@_normalize_type_R_aux:w,
2261 % \@@_normalize_type_v:w
2262 % }
2263 % \changes{v1.3l}{2026-04-26}{Update \cs{@@_normalize_type_R:w} to use a
2264 % new auxillary function (gh/1204)}
2265 % Mandatory arguments. First check the required data is present,
2266 % consists of single characters where applicable, and that the argument
2267 % type is allowed for expandable commands if applicable. For the
2268 % \texttt{m} and \texttt{R} argument types check that they do not
2269 % follow some optional argument with that delimiter as otherwise the
2270 % optional argument could not be omitted. Then save data in
2271 % \cs{l_@@_arg_spec_tl}, count the mandatory argument, and empty the
2272 % list of last delimiters.
2273 % \begin{macrocode}
2274 \cs_new_protected:Npn \@@_normalize_type_m:w
2275 {
2276 \@@_delimiter_check:nnn { } { m } { \iow_char:N \{ }
2277 \@@_add_arg_spec_mandatory:n { m }
2278 \@@_normalize_arg_spec_loop:n
2279 }
2280 \cs_new_protected:Npn \@@_normalize_type_R:w #1#2#3
2281 {
2282 \quark_if_recursion_tail_stop_do:nn {#3} { \@@_bad_arg_spec:wn }
2283 \@@_normalize_type_R_aux:w { R #1 #2 {#3} } {#1} {#2} {#3}
2284 }
2285 \cs_new_protected:Npn \@@_normalize_type_R_aux:w #1#2#3#4
2286 {
2287 \@@_single_token_check:n {#2} \@@_allowed_token_check:N #2
2288 \@@_single_token_check:n {#3}
2289 \@@_delimiter_check:nnn {#2} { R/r } { \tl_to_str:n {#2} }
2290 \bool_set_false:N \l_@@_grab_expandably_bool
2291 \@@_add_arg_spec_mandatory:nn { R #2 #3 {#4} } {#1}
2292 \@@_normalize_arg_spec_loop:n
2293 }
2294 \cs_new_protected:Npn \@@_normalize_type_v:w
2295 {
2296 \@@_normalize_check_gv:N v
2297 \@@_add_arg_spec_mandatory:n { v }
2298 \@@_normalize_arg_spec_loop:n
2299 }
2300 % \end{macrocode}
2301 % \end{macro}
2302 %
2303 % \begin{macro}{\@@_normalize_type_b:w}
2304 % \changes{v1.3a}{2025-01-23}{Extend to cover \texttt{c}-type grabbing}
2305 % \begin{macro}{\@@_normalize_type_c:w}
2306 % \changes{v1.3a}{2025-01-23}{New function}
2307 % \begin{macro}{\@@_normalize_type_b_or_c:nn}
2308 % \changes{v1.3a}{2025-01-23}{New function}
2309 % These argument types are not allowed for commands. They are only
2310 % allowed at the end of the argument specification, hence we check
2311 % that |#1| is the end.
2312 % \begin{macrocode}
2313 \cs_new_protected:Npn \@@_normalize_type_b:w #1
2314 { \@@_normalize_type_b_or_c:nn {#1} { b } }
2315 \cs_new_protected:Npn \@@_normalize_type_c:w #1
2316 { \@@_normalize_type_b_or_c:nn {#1} { c } }
2317 \cs_new_protected:Npn \@@_normalize_type_b_or_c:nn #1#2
2318 {
2319 \bool_if:NF \l_@@_environment_bool
2320 {
2321 \msg_error:nnee { cmd } { invalid-command-arg }
2322 { \@@_environment_or_command: } {#2}
2323 \@@_bad_def:wn
2324 }
2325 \tl_clear:N \l_@@_last_delimiters_tl
2326 \@@_add_arg_spec:n {#2}
2327 \quark_if_recursion_tail_stop:n {#1}
2328 \msg_error:nneee { cmd } { arg-after-body }
2329 {#2}
2330 { \@@_environment_or_command: }
2331 { \tl_to_str:n {#1} }
2332 \@@_bad_def:wn
2333 }
2334 % \end{macrocode}
2335 % \end{macro}
2336 % \end{macro}
2337 % \end{macro}
2338 %
2339 % \begin{macro}{\@@_single_token_check:n}
2340 % Checks that the argument is a single (non-space) token (possibly
2341 % surrounded by spaces), and aborts the definition otherwise.
2342 % \begin{macrocode}
2343 \cs_new_protected:Npn \@@_single_token_check:n #1
2344 {
2345 \tl_trim_spaces_apply:nN {#1} \tl_if_single_token:nF
2346 {
2347 \msg_error:nnee { cmd } { not-single-token }
2348 { \@@_environment_or_command: } { \tl_to_str:n {#1} }
2349 \@@_bad_def:wn
2350 }
2351 }
2352 % \end{macrocode}
2353 % \end{macro}
2354 %
2355 % \begin{macro}{\@@_allowed_token_check:N}
2356 % Some tokens are not allowed as delimiters for some argument types,
2357 % notably implicit begin/end-group tokens (|\bgroup|/|\egroup|).
2358 % The major problem with these tokens is that for |\peek_...| functions,
2359 % a literal~|{|$_1$ is virtually indistinguishable from a |\bgroup| or
2360 % other token which was |\let| to a~|{|$_1$, and the same goes
2361 % for~|}|$_2$. All other tokens can be easily distinguished from their
2362 % implicit counterparts by grabbing them and looking at the string
2363 % length (see \cs{@@_token_if_cs:NTF}), but for begin/end group tokens
2364 % that is not possible without the risk of mistakenly grabbing the
2365 % entire brace group (potentially leading to a~\texttt{!~Runaway argument}
2366 % error) or trying to grab a |}|$_2$, leading to
2367 % \iffalse{\fi an~\verb|! Argument of \dots has an extra }| error.
2368 % \begin{macrocode}
2369 \cs_new_protected:Npn \@@_allowed_token_check:N #1
2370 {
2371 \token_if_eq_meaning:NNTF #1 \c_group_begin_token
2372 { \use:n }
2373 {
2374 \token_if_eq_meaning:NNTF #1 \c_group_end_token
2375 { \use:n }
2376 { \use_none:n }
2377 }
2378 {
2379 \msg_error:nneee { cmd } { forbidden-group-token }
2380 { \@@_environment_or_command: } { \tl_to_str:n {#1} }
2381 {
2382 \token_if_eq_meaning:NNTF #1 \c_group_begin_token
2383 { begin } { end }
2384 }
2385 \@@_bad_def:wn
2386 }
2387 }
2388 % \end{macrocode}
2389 % \end{macro}
2390 %
2391 % \begin{macro}{\@@_normalize_check_gv:N, \@@_normalize_check_lu:N}
2392 % Called for arguments that are always forbidden, or forbidden after
2393 % an optional argument, for expandable commands.
2394 % \begin{macrocode}
2395 \cs_new_protected:Npn \@@_normalize_check_gv:N #1
2396 {
2397 \bool_if:NT \l_@@_expandable_bool
2398 {
2399 \msg_error:nnee { cmd } { invalid-expandable-arg }
2400 { \iow_char:N \\ \l_@@_function_tl } { \tl_to_str:n {#1} }
2401 \@@_bad_def:wn
2402 }
2403 \bool_set_false:N \l_@@_grab_expandably_bool
2404 }
2405 \cs_new_protected:Npn \@@_normalize_check_lu:N #1
2406 {
2407 \bool_if:NT \l_@@_expandable_bool
2408 {
2409 \tl_if_empty:NF \l_@@_last_delimiters_tl
2410 {
2411 \msg_error:nnee { cmd } { invalid-after-optional-expandably }
2412 { \iow_char:N \\ \l_@@_function_tl } { \tl_to_str:n {#1} }
2413 \@@_bad_def:wn
2414 }
2415 }
2416 }
2417 % \end{macrocode}
2418 % \end{macro}
2419 %
2420 % \begin{macro}{\@@_delimiter_check:nnn}
2421 % Called for \texttt{m} and \texttt{R} arguments. Checks that the
2422 % leading token does not coincide with the token denoting the presence
2423 % of a previous optional argument. Instead of dealing with braces for
2424 % the \texttt{m}-type we use an empty delimiter to denote that case.
2425 % \begin{macrocode}
2426 \cs_new_protected:Npn \@@_delimiter_check:nnn #1#2#3
2427 {
2428 \tl_map_inline:Nn \l_@@_last_delimiters_tl
2429 {
2430 \tl_if_eq:nnT {##1} {#1}
2431 {
2432 \msg_warning:nnee { cmd } { optional-mandatory }
2433 {#2} {#3}
2434 }
2435 }
2436 }
2437 % \end{macrocode}
2438 % \end{macro}
2439 %
2440 % \begin{macro}{\@@_bad_arg_spec:wn, \@@_bad_def:wn}
2441 % If the argument specification is wrong, this provides an escape from
2442 % the entire definition process.
2443 % \begin{macrocode}
2444 \cs_new_protected:Npn \@@_bad_arg_spec:wn #1 \@@_break_point:n #2
2445 {
2446 \msg_error:nnee { cmd } { bad-arg-spec }
2447 { \@@_environment_or_command: } { \tl_to_str:n {#2} }
2448 }
2449 \cs_new_protected:Npn \@@_bad_def:wn #1 \@@_break_point:n #2 { }
2450 % \end{macrocode}
2451 % \end{macro}
2452 %
2453 % \begin{macro}{
2454 % \@@_add_arg_spec:n,
2455 % \@@_add_arg_spec_mandatory:n,
2456 % \@@_add_arg_spec_mandatory:nn
2457 % }
2458 % \changes{v1.2c}{2023/12/22}
2459 % {Clarify error message when \texttt{!} prefix is applied to
2460 % non-trailing opt-arg (gh/1198)}
2461 % \changes{v1.3c}{2025-04-12}{Track total mandatory args}
2462 % \changes{v1.3f}{2025-09-23}{Correct logic for recording position of
2463 % mandatory args}
2464 % \changes{v1.3l}{2026-04-26}{Update mandatory spec function to use
2465 % two arguments (gh/1204)}
2466 % When adding an argument to the argument specification, set the
2467 % \texttt{some_long} or \texttt{some_short} booleans as appropriate
2468 % and clear the booleans keeping track of |+|, |!| and |=| markers.
2469 % Before that, test for a short argument following some long
2470 % arguments: this is forbidden for expandable commands and prevents
2471 % grabbing arguments expandably.
2472 %
2473 % For mandatory arguments do some more work, in particular complain if
2474 % they were preceded by~|!|.
2475 % \begin{macrocode}
2476 \cs_new_protected:Npn \@@_add_arg_spec:n #1
2477 {
2478 \bool_lazy_and:nnT
2479 { ! \l_@@_long_bool }
2480 { \l_@@_some_long_bool }
2481 {
2482 \bool_if:NT \l_@@_expandable_bool
2483 {
2484 \msg_error:nne { cmd } { long-short-mix }
2485 { \iow_char:N \\ \l_@@_function_tl }
2486 \@@_bad_def:wn
2487 }
2488 \bool_set_false:N \l_@@_grab_expandably_bool
2489 }
2490 \bool_if:NTF \l_@@_long_bool
2491 { \bool_set_true:N \l_@@_some_long_bool }
2492 { \bool_set_true:N \l_@@_some_short_bool }
2493 \tl_put_right:Ne \l_@@_arg_spec_tl
2494 {
2495 \bool_lazy_and:nnT
2496 { \l_@@_long_bool }
2497 { ! \str_if_eq_p:nn {#1} { c } }
2498 { + }
2499 \bool_if:NT \l_@@_obey_spaces_bool { ! }
2500 \exp_not:n {#1}
2501 }
2502 \bool_set_false:N \l_@@_long_bool
2503 \bool_set_false:N \l_@@_obey_spaces_bool
2504 }
2505 \cs_new_protected:Npn \@@_add_arg_spec_mandatory:n #1
2506 { \@@_add_arg_spec_mandatory:nn { #1 } { #1 } }
2507 \cs_new_protected:Npn \@@_add_arg_spec_mandatory:nn #1 #2
2508 {
2509 \bool_if:NT \l_@@_some_obey_spaces_bool
2510 {
2511 \msg_error:nnee { cmd } { invalid-bang }
2512 { \@@_environment_or_command: }
2513 {
2514 \bool_if:NTF \l_@@_obey_spaces_bool
2515 { \tl_to_str:n {'#2'} }
2516 { an~optional~argument~before~mandatory~ \tl_to_str:n {'#2'} }
2517 }
2518 \@@_bad_def:wn
2519 }
2520 \tl_clear:N \l_@@_last_delimiters_tl
2521 \int_set_eq:NN \l_@@_last_mandatory_arg_int \l_@@_current_arg_int
2522 \@@_add_arg_spec:n {#1}
2523 }
2524 % \end{macrocode}
2525 % \end{macro}
2526 %
2527 % \subsection{Preparing the signature: general mechanism}
2528 %
2529 % \begin{macro}{\@@_prepare_signature:n}
2530 % \changes{v1.3a}{2025-01-23}{Extend to cover \texttt{c}-type grabbing}
2531 % \begin{macro}{\@@_prepare_signature_verb_chk:n}
2532 % \begin{macro}{\@@_prepare_signature:N}
2533 % \begin{macro}{\@@_prepare_signature_bypass:N}
2534 % Actually creating the signature uses the same loop approach as
2535 % normalizing the signature. There are first a number of variables which need
2536 % to be set to track what is going on. Many of these variables are unused
2537 % when defining expandable commands.
2538 % \begin{macrocode}
2539 \cs_new_protected:Npn \@@_prepare_signature:n #1
2540 {
2541 \int_set_eq:NN \l_@@_total_args_int \l_@@_current_arg_int
2542 \int_zero:N \l_@@_current_arg_int
2543 \bool_set_false:N \l_@@_long_bool
2544 \bool_set_false:N \l_@@_obey_spaces_bool
2545 \bool_set_false:N \l_@@_suppress_strip_bool
2546 \int_zero:N \l_@@_m_args_int
2547 \bool_set_false:N \l_@@_defaults_bool
2548 \tl_clear:N \l_@@_defaults_tl
2549 \tl_clear:N \l_@@_process_all_tl
2550 \tl_clear:N \l_@@_process_one_tl
2551 \bool_set_false:N \l_@@_process_some_bool
2552 \tl_clear:N \l_@@_signature_tl
2553 \@@_prepare_signature_verb_chk:n {#1}
2554 \@@_prepare_signature:N #1 \q_recursion_tail \q_recursion_stop
2555 \bool_if:NF \l_@@_expandable_bool { \@@_flush_m_args: }
2556 }
2557 % \end{macrocode}
2558 % A quick check on the final arg.~type.
2559 % \begin{macrocode}
2560 \cs_new_protected:Npn \@@_prepare_signature_verb_chk:n #1
2561 {
2562 \str_if_eq:eeTF { \tl_head:e { \tl_reverse:n {#1} } } { c }
2563 { \bool_set_true:N \l_@@_final_verb_bool }
2564 { \bool_set_false:N \l_@@_final_verb_bool }
2565 }
2566 % \end{macrocode}
2567 % The main looping function does not take an argument, but carries out the
2568 % reset on the processor boolean. This is split off from the rest of the
2569 % process so that when actually setting up processors the flag-reset can
2570 % be bypassed.
2571 %
2572 % For each known argument type there is an appropriate function to actually
2573 % do the addition to the signature. These are separate for expandable and
2574 % standard functions, as the approaches are different.
2575 % \begin{macrocode}
2576 \cs_new_protected:Npn \@@_prepare_signature:N
2577 {
2578 \bool_set_false:N \l_@@_prefixed_bool
2579 \@@_prepare_signature_bypass:N
2580 }
2581 \cs_new_protected:Npn \@@_prepare_signature_bypass:N #1
2582 {
2583 \quark_if_recursion_tail_stop:N #1
2584 \use:c
2585 {
2586 @@_add
2587 \bool_if:NT \l_@@_grab_expandably_bool { _expandable }
2588 _type_ \token_to_str:N #1 :w
2589 }
2590 }
2591 % \end{macrocode}
2592 % \end{macro}
2593 % \end{macro}
2594 % \end{macro}
2595 % \end{macro}
2596 %
2597 % \subsection{Setting up a standard signature}
2598 %
2599 % Each argument-adding function appends to the signature a grabber (and
2600 % for some types, the delimiters or default value), except the one for
2601 % \texttt{m} arguments. These are collected and added to the signature
2602 % all at once by \cs{@@_flush_m_args:}, called for every other argument
2603 % type. All of the functions then call the loop function
2604 % \cs{@@_prepare_signature:N}. Default values of arguments are
2605 % collected by \cs{@@_add_default:n} rather than being stored with the
2606 % argument; this function and \cs{@@_add_default:} are also responsible
2607 % for keeping track of \cs{l_@@_current_arg_int}.
2608 %
2609 % \begin{macro}{\@@_add_type_+:w}
2610 % Making the next argument long means setting the flag. The \texttt{m}
2611 % arguments are recorded here as
2612 % this has to be done for every case where there is then a long argument.
2613 % \begin{macrocode}
2614 \cs_new_protected:cpn { @@_add_type_+:w }
2615 {
2616 \@@_flush_m_args:
2617 \bool_set_true:N \l_@@_long_bool
2618 \bool_set_true:N \l_@@_prefixed_bool
2619 \@@_prepare_signature_bypass:N
2620 }
2621 % \end{macrocode}
2622 % \end{macro}
2623 %
2624 % \begin{macro}{\@@_add_type_!:w}
2625 % Much the same for controlling trailing optional arguments.
2626 % \begin{macrocode}
2627 \cs_new_protected:cpn { @@_add_type_!:w }
2628 {
2629 \@@_flush_m_args:
2630 \bool_set_true:N \l_@@_obey_spaces_bool
2631 \bool_set_true:N \l_@@_prefixed_bool
2632 \@@_prepare_signature_bypass:N
2633 }
2634 % \end{macrocode}
2635 % \end{macro}
2636 %
2637 % \begin{macro}{\@@_add_type_>:w}
2638 % When a processor is found, the processor code is stored. It will be
2639 % used by \cs{@@_args_process:} once arguments are all found. Here too
2640 % the loop calls \cs{@@_prepare_signature_bypass:N} rather than
2641 % \cs{@@_prepare_signature:N} so that the flag is not reset.
2642 % \begin{macrocode}
2643 \cs_new_protected:cpn { @@_add_type_>:w } #1
2644 {
2645 \@@_flush_m_args:
2646 \bool_set_true:N \l_@@_prefixed_bool
2647 \bool_set_true:N \l_@@_process_some_bool
2648 \tl_put_left:Nn \l_@@_process_one_tl { {#1} }
2649 \@@_prepare_signature_bypass:N
2650 }
2651 % \end{macrocode}
2652 % \end{macro}
2653 %
2654 % \begin{macro}{\@@_add_type_=:w}
2655 % \changes{v1.1a}{2022/08/10}{Add support for \texttt{=} modifier}
2656 % A mix of the ideas from above: set a flag and add a processor.
2657 % \begin{macrocode}
2658 \cs_new_protected:cpn { @@_add_type_=:w } #1
2659 {
2660 \@@_flush_m_args:
2661 \bool_set_true:N \l_@@_prefixed_bool
2662 \bool_set_true:N \l_@@_suppress_strip_bool
2663 \bool_set_true:N \l_@@_process_some_bool
2664 \tl_put_left:Nn \l_@@_process_one_tl
2665 { { \@@_arg_to_keyvalue:nn {#1} } }
2666 \@@_prepare_signature_bypass:N
2667 }
2668 % \end{macrocode}
2669 % \end{macro}
2670 %
2671 % \begin{macro}{\@@_add_type_b:w}
2672 % \changes{v1.3a}{2025-01-23}{Extend to cover \texttt{c}-type grabbing}
2673 % \begin{macro}{\@@_add_type_c:w}
2674 % \changes{v1.3a}{2025-01-23}{New function}
2675 % \begin{macro}{\@@_add_type_b_or_c:N}
2676 % \changes{v1.3a}{2025-01-23}{New function}
2677 % \begin{macrocode}
2678 \cs_new_protected:Npn \@@_add_type_b:w
2679 { \@@_add_type_b_or_c:N b }
2680 \cs_new_protected:Npn \@@_add_type_c:w
2681 { \@@_add_type_b_or_c:N c }
2682 \cs_new_protected:Npn \@@_add_type_b_or_c:N #1
2683 {
2684 \@@_flush_m_args:
2685 \@@_add_default:
2686 \@@_add_grabber:N #1
2687 \@@_prepare_signature:N
2688 }
2689 % \end{macrocode}
2690 % \end{macro}
2691 % \end{macro}
2692 % \end{macro}
2693 %
2694 % \begin{macro}{\@@_add_type_D:w}
2695 % \begin{macrocode}
2696 \cs_new_protected:Npn \@@_add_type_D:w #1#2#3
2697 {
2698 \@@_flush_m_args:
2699 \@@_add_default:n {#3}
2700 \@@_add_grabber:N D
2701 \tl_put_right:Nn \l_@@_signature_tl { #1 #2 }
2702 \@@_prepare_signature:N
2703 }
2704 % \end{macrocode}
2705 % \end{macro}
2706 %
2707 % \begin{macro}{\@@_add_type_E:w}
2708 % The \texttt{E}-type argument needs a special handling of default
2709 % values. Since each embellishment is a separate argument, it
2710 % also needs to replicate the argument processors for each embellishment
2711 % argument so that the numbers of arguments and processors remain in sync.
2712 % \changes{v1.0g}{2021/08/07}
2713 % {Replicate argument processors for all embellishments (gh/639)}
2714 % \begin{macrocode}
2715 \cs_new_protected:Npn \@@_add_type_E:w #1#2
2716 {
2717 \@@_flush_m_args:
2718 \@@_add_default_E:nn {#1} {#2}
2719 \use:e
2720 {
2721 \@@_replicate_processor:nn { \tl_count:n {#1} }
2722 { \exp_not:o \l_@@_process_one_tl }
2723 }
2724 \@@_add_grabber:N E
2725 \tl_put_right:Nn \l_@@_signature_tl { {#1} }
2726 \@@_prepare_signature:N
2727 }
2728 % \end{macrocode}
2729 % \end{macro}
2730 %
2731 % \begin{macro}{\@@_replicate_processor:nn}
2732 % In the command's argument processor signature (the final argument of
2733 % \cs{@@_start:nNNnnn}) there is one braced item for each formal
2734 % argument (up to nine), and in each of these items there is one
2735 % braced item for each processor (as many as there were processors
2736 % declared for a given argument). Something like this:
2737 % \begin{quote}
2738 % \ttfamily \obeylines
2739 % \{ \% argument processors
2740 % \ \ \{ \% argument 1
2741 % \ \ \ \ \{ processor 1 \} \{ processor 2 \} \ldots\ \{ processor n \}
2742 % \ \ \} \% end argument 1
2743 % \ \ \{ \ldots\ \} \% argument 2
2744 % \ \ \ \ \ \vdots
2745 % \ \ \{ \ldots\ \} \% argument n
2746 % \} \% end argument processors
2747 % \end{quote}
2748 %
2749 % The function \cs{@@_add_grabber:N} adds one single grabber for an
2750 % argument, and adds the braced item for that one argument. However,
2751 % in an |E|-type argument each embellishment requires its own formal
2752 % argument, so we need to break out of one layer of braces in
2753 % \cs{l_@@_process_one_tl}, add copies of the processor as necessary,
2754 % and then return the removed brace. The function below does just
2755 % that: it defines \cs{l_@@_process_one_tl} starting with a
2756 % \iffalse{\fi |}|$_2$
2757 % and ending with a |{|$_1$, \iffalse}\fi
2758 % so that it adds as many processors as
2759 % needed when |x|-expanded.
2760 % \begin{macrocode}
2761 \cs_new_protected:Npn \@@_replicate_processor:nn #1 #2
2762 {
2763 \int_compare:nNnF {#1} > { 1 } { \use_none:nnn }
2764 \tl_set:Ne \l_@@_process_one_tl
2765 {
2766 \exp_not:n { \exp_not:n {#2} \if_false: { \fi: } }
2767 \prg_replicate:nn { #1 - 2 }
2768 { \exp_not:n { \exp_not:n { {#2} } } }
2769 \exp_not:n { { \if_false: } \fi: \exp_not:n {#2} }
2770 }
2771 }
2772 % \end{macrocode}
2773 % \end{macro}
2774 %
2775 % \begin{macro}{\@@_add_type_m:w}
2776 % The \texttt{m} type is special as short arguments which are not
2777 % post-processed are simply counted at this stage. Thus there is a check
2778 % to see if either of these cases apply. If so, a one-argument grabber
2779 % is added to the signature. On the other hand, if a standard short
2780 % argument is required it is simply counted at this stage, to be
2781 % added later using \cs{@@_flush_m_args:}.
2782 % \begin{macrocode}
2783 \cs_new_protected:Npn \@@_add_type_m:w
2784 {
2785 \@@_add_default:
2786 \bool_if:NTF \l_@@_prefixed_bool
2787 { \@@_add_grabber:N m }
2788 { \int_incr:N \l_@@_m_args_int }
2789 \@@_prepare_signature:N
2790 }
2791 % \end{macrocode}
2792 % \end{macro}
2793 %
2794 % \begin{macro}{\@@_add_type_R:w}
2795 % The \texttt{R}-type argument is very similar to the \texttt{D}-type.
2796 % \begin{macrocode}
2797 \cs_new_protected:Npn \@@_add_type_R:w #1#2#3
2798 {
2799 \@@_flush_m_args:
2800 \@@_add_default:n {#3}
2801 \@@_add_grabber:N R
2802 \tl_put_right:Nn \l_@@_signature_tl { #1 #2 }
2803 \@@_prepare_signature:N
2804 }
2805 % \end{macrocode}
2806 % \end{macro}
2807 %
2808 % \begin{macro}{\@@_add_type_t:w}
2809 % Setting up a \texttt{t} argument means collecting one token for the test,
2810 % and adding it along with the grabber to the signature.
2811 % \begin{macrocode}
2812 \cs_new_protected:Npn \@@_add_type_t:w #1
2813 {
2814 \@@_flush_m_args:
2815 \@@_add_default:
2816 \@@_add_grabber:N t
2817 \tl_put_right:Nn \l_@@_signature_tl {#1}
2818 \@@_prepare_signature:N
2819 }
2820 % \end{macrocode}
2821 % \end{macro}
2822 %
2823 % \begin{macro}{\@@_add_type_v:w}
2824 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
2825 % At this stage, the \texttt{v} argument is identical to \texttt{l}
2826 % except that since the grabber may fail to read a verbatim argument
2827 % we need a default value.
2828 % \begin{macrocode}
2829 \cs_new_protected:Npn \@@_add_type_v:w
2830 {
2831 \@@_flush_m_args:
2832 \@@_add_default:n { \NoValue }
2833 \@@_add_grabber:N v
2834 \@@_prepare_signature:N
2835 }
2836 % \end{macrocode}
2837 % \end{macro}
2838 %
2839 % \begin{macro}{\@@_flush_m_args:}
2840 % As \texttt{m} arguments are simply counted, there is a need to add
2841 % them to the token register in a block. As this function can only
2842 % be called if something other than \texttt{m} turns up, the flag can
2843 % be switched here.
2844 % \begin{macrocode}
2845 \cs_new_protected:Npn \@@_flush_m_args:
2846 {
2847 \int_compare:nNnT \l_@@_m_args_int > 0
2848 {
2849 \tl_put_right:Ne \l_@@_signature_tl
2850 { \exp_not:c { @@_grab_m_ \int_use:N \l_@@_m_args_int :w } }
2851 \tl_put_right:Ne \l_@@_process_all_tl
2852 { \prg_replicate:nn { \l_@@_m_args_int } { { } } }
2853 }
2854 \int_zero:N \l_@@_m_args_int
2855 }
2856 % \end{macrocode}
2857 % \end{macro}
2858 %
2859 % \begin{macro}{\@@_add_grabber:N}
2860 % To keep the various checks needed in one place, adding the grabber
2861 % to the signature is done here. The only questions are whether the
2862 % grabber should be long or not, and whether to obey spaces. The
2863 % \cs{l_@@_obey_spaces_bool} boolean can only be \texttt{true} for
2864 % trailing optional arguments. In that case spaces will not be
2865 % ignored when looking for that optional argument.
2866 % \changes{v1.0g}{2021/08/07}
2867 % {Replicate argument processors for all embellishments (gh/639)}
2868 % \changes{v1.3a}{2025-01-23}{Extend to support \texttt{c}-type grabbing}
2869 % \changes{v1.3c}{2025-04-12}{Refine handling of optional arguments for
2870 % \texttt{c}-type grabbing}
2871 % \begin{macrocode}
2872 \cs_new_protected:Npn \@@_add_grabber:N #1
2873 {
2874 \tl_put_right:Ne \l_@@_signature_tl
2875 {
2876 \exp_not:c
2877 {
2878 @@_grab_ #1
2879 \bool_if:NT \l_@@_long_bool { _long }
2880 \bool_if:NT \l_@@_obey_spaces_bool { _obey_spaces }
2881 \bool_lazy_and:nnT
2882 { \l_@@_suppress_strip_bool }
2883 { \str_if_eq_p:nn {#1} { D } }
2884 { _no_strip }
2885 \bool_lazy_all:nT
2886 {
2887 { \l_@@_final_verb_bool }
2888 { \str_if_eq_p:nn {#1} { D } }
2889 {
2890 \int_compare_p:nNn \l_@@_current_arg_int
2891 > \l_@@_last_mandatory_arg_int
2892 }
2893 }
2894 { _verb_safe }
2895 :w
2896 }
2897 }
2898 \bool_set_false:N \l_@@_long_bool
2899 \bool_set_false:N \l_@@_obey_spaces_bool
2900 \bool_set_false:N \l_@@_suppress_strip_bool
2901 \bool_set_false:N \l_@@_verb_safe_bool
2902 \tl_put_right:Ne \l_@@_process_all_tl
2903 {
2904 {
2905 \if_charcode:w E #1 \use_i:nn \fi:
2906 \exp_not:o \l_@@_process_one_tl
2907 }
2908 }
2909 \tl_clear:N \l_@@_process_one_tl
2910 }
2911 % \end{macrocode}
2912 % \end{macro}
2913 %
2914 % \begin{macro}{\@@_add_default:n, \@@_add_default:, \@@_add_default_E:nn}
2915 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
2916 % Store the default value of an argument, or rather code that gives
2917 % that default value (it may involve other arguments). This is
2918 % (in a brace group) \cs{prg_do_nothing:}
2919 % followed by a default value or \cs{NoValue}. For \texttt{E}-type
2920 % arguments, pad the defaults |#2| with some \cs{NoValue}
2921 % until there are as many as embellishments~|#1|. These functions are
2922 % also used when defining expandable commands. The \cs{prg_do_nothing:} here
2923 % are removed in \cs{@@_defaults_def:nnn}: for a user-supplied default the
2924 % token is injected to avoid brace stripping, and for \cs{NoValue} means
2925 % we have a common code path later.
2926 % \begin{macrocode}
2927 \cs_new_protected:Npn \@@_add_default:n #1
2928 {
2929 \tl_if_novalue:nTF {#1}
2930 { \@@_add_default: }
2931 {
2932 \int_incr:N \l_@@_current_arg_int
2933 \bool_set_true:N \l_@@_defaults_bool
2934 \tl_put_right:Nn \l_@@_defaults_tl { { \prg_do_nothing: #1 } }
2935 }
2936 }
2937 \cs_new_protected:Npn \@@_add_default:
2938 {
2939 \int_incr:N \l_@@_current_arg_int
2940 \tl_put_right:Nn \l_@@_defaults_tl { { \prg_do_nothing: \NoValue } }
2941 }
2942 \cs_new_protected:Npn \@@_add_default_E:nn #1#2
2943 {
2944 \tl_map_function:nN {#2} \@@_add_default:n
2945 \prg_replicate:nn
2946 { \tl_count:n {#1} - \tl_count:n {#2} }
2947 { \@@_add_default: }
2948 }
2949 % \end{macrocode}
2950 % \end{macro}
2951 %
2952 % \subsection{Setting up expandable types}
2953 %
2954 % The approach here is not dissimilar to that for standard types, but fewer types
2955 % are supported. There is
2956 % also a need to define the per-function auxiliaries: this is done here, while
2957 % the general grabbers are dealt with later.
2958 %
2959 % \begin{macro}{\@@_add_expandable_type_+:w}
2960 % We have already checked that short arguments are before long
2961 % arguments, so \cs{l_@@_long_bool} only changes from \texttt{false}
2962 % to \texttt{true} once (and there is no need to reset it after each
2963 % argument). Continue the loop.
2964 % \begin{macrocode}
2965 \cs_new_protected:cpn { @@_add_expandable_type_+:w }
2966 {
2967 \bool_set_true:N \l_@@_long_bool
2968 \@@_prepare_signature:N
2969 }
2970 % \end{macrocode}
2971 % \end{macro}
2972 %
2973 % \begin{macro}{\@@_add_expandable_type_D:w}
2974 % \begin{macro}{\@@_add_expandable_type_D_aux:NNNn}
2975 % \begin{macro}{\@@_add_expandable_type_D_aux:NNN}
2976 % \begin{macro}{\@@_add_expandable_type_D_aux:NN}
2977 % The set up for \texttt{D}-type arguments involves constructing a
2978 % rather complex auxiliary which is used
2979 % repeatedly when grabbing. There is an auxiliary here so that the
2980 % \texttt{R}-type can share code readily: |#1| is |D| or~|R|.
2981 % The |_aux:NN| auxiliary is needed if the two delimiting tokens are
2982 % identical: in contrast to the non-expandable route, the grabber here
2983 % has to act differently for this case.
2984 % \begin{macrocode}
2985 \cs_new_protected:Npn \@@_add_expandable_type_D:w
2986 { \@@_add_expandable_type_D_aux:NNNn D }
2987 \cs_new_protected:Npn \@@_add_expandable_type_D_aux:NNNn #1#2#3#4
2988 {
2989 \@@_add_default:n {#4}
2990 \tl_if_eq:nnTF {#2} {#3}
2991 { \@@_add_expandable_type_D_aux:NN #1 #2 }
2992 { \@@_add_expandable_type_D_aux:NNN #1 #2 #3 }
2993 \@@_prepare_signature:N
2994 }
2995 \cs_new_protected:Npn \@@_add_expandable_type_D_aux:NNN #1#2#3
2996 {
2997 \bool_if:NTF \l_@@_long_bool
2998 { \cs_set:cpe }
2999 { \cs_set_nopar:cpe }
3000 { \l_@@_expandable_aux_name_tl } ##1 ##2 #2 ##3 \q_@@ ##4 #3
3001 { ##1 {##2} {##3} {##4} }
3002 \@@_add_expandable_grabber:nn {#1}
3003 {
3004 \exp_not:c { \l_@@_expandable_aux_name_tl }
3005 \exp_not:n { #2 #3 }
3006 }
3007 }
3008 \cs_new_protected:Npn \@@_add_expandable_type_D_aux:NN #1#2
3009 {
3010 \bool_if:NTF \l_@@_long_bool
3011 { \cs_set:cpe }
3012 { \cs_set_nopar:cpe }
3013 { \l_@@_expandable_aux_name_tl } ##1 #2 ##2 #2
3014 { ##1 {##2} }
3015 \@@_add_expandable_grabber:nn { #1_alt }
3016 {
3017 \exp_not:c { \l_@@_expandable_aux_name_tl }
3018 \exp_not:n {#2}
3019 }
3020 }
3021 % \end{macrocode}
3022 % \end{macro}
3023 % \end{macro}
3024 % \end{macro}
3025 % \end{macro}
3026 %
3027 % \begin{macro}{\@@_add_expandable_type_E:w}
3028 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
3029 % \begin{macro}{\@@_add_expandable_type_E_aux:n}
3030 % For each embellishment, use \cs{@@_get_grabber:NN} to obtain an
3031 % auxiliary delimited by that token and store a pair constituted of
3032 % the auxiliary and the token in \cs{l_@@_tmpb_tl}, before appending
3033 % the whole set of these pairs to the signature, and an equal number
3034 % of \cs{NoValue} markers (regardless of the default values of
3035 % arguments). Set the current argument appropriately.
3036 % \begin{macrocode}
3037 \cs_new_protected:Npn \@@_add_expandable_type_E:w #1#2
3038 {
3039 \@@_add_default_E:nn {#1} {#2}
3040 \tl_clear:N \l_@@_tmpb_tl
3041 \tl_map_function:nN {#1} \@@_add_expandable_type_E_aux:n
3042 \@@_add_expandable_grabber:nn
3043 { E \bool_if:NT \l_@@_long_bool { _long } }
3044 {
3045 { \exp_not:o \l_@@_tmpb_tl }
3046 {
3047 \prg_replicate:nn { \tl_count:n {#1} }
3048 { { \NoValue } }
3049 }
3050 }
3051 \@@_prepare_signature:N
3052 }
3053 \cs_new_protected:Npn \@@_add_expandable_type_E_aux:n #1
3054 {
3055 \@@_get_grabber:NN #1 \l_@@_tmpa_tl
3056 \tl_put_right:Ne \l_@@_tmpb_tl
3057 { \exp_not:o \l_@@_tmpa_tl \exp_not:N #1 }
3058 }
3059 % \end{macrocode}
3060 % \end{macro}
3061 % \end{macro}
3062 %
3063 % \begin{macro}{\@@_add_expandable_type_m:w}
3064 % Unlike the standard case, when working expandably each argument is always
3065 % grabbed separately.
3066 % \begin{macrocode}
3067 \cs_new_protected:Npn \@@_add_expandable_type_m:w
3068 {
3069 \@@_add_default:
3070 \@@_add_expandable_grabber:nn
3071 { m \bool_if:NT \l_@@_long_bool { _long } } { }
3072 \@@_prepare_signature:N
3073 }
3074 % \end{macrocode}
3075 % \end{macro}
3076 %
3077 % \begin{macro}{\@@_add_expandable_type_R:w}
3078 % The \texttt{R}-type is very similar to the \texttt{D}-type
3079 % argument, and so the same internals are used.
3080 % \begin{macrocode}
3081 \cs_new_protected:Npn \@@_add_expandable_type_R:w
3082 { \@@_add_expandable_type_D_aux:NNNn R }
3083 % \end{macrocode}
3084 % \end{macro}
3085 %
3086 % \begin{macro}{\@@_add_expandable_type_t:w}
3087 % An auxiliary delimited by |#1| is built now. It will be used to
3088 % test for the presence of that token.
3089 % \begin{macrocode}
3090 \cs_new_protected:Npn \@@_add_expandable_type_t:w #1
3091 {
3092 \@@_add_default:
3093 \@@_get_grabber:NN #1 \l_@@_tmpa_tl
3094 \@@_add_expandable_grabber:nn { t }
3095 {
3096 \exp_not:o \l_@@_tmpa_tl
3097 \exp_not:N #1
3098 }
3099 \@@_prepare_signature:N
3100 }
3101 % \end{macrocode}
3102 % \end{macro}
3103 %
3104 % \begin{macro}{\@@_add_expandable_grabber:nn}
3105 % This is called for all arguments to place the right grabber in the
3106 % signature.
3107 % \begin{macrocode}
3108 \cs_new_protected:Npn \@@_add_expandable_grabber:nn #1#2
3109 {
3110 \tl_put_right:Ne \l_@@_signature_tl
3111 { \exp_not:c { @@_expandable_grab_ #1 :w } #2 }
3112 }
3113 % \end{macrocode}
3114 % \end{macro}
3115 %
3116 % \begin{macro}{\@@_get_grabber:NN}
3117 % \begin{macro}{\@@_get_grabber_auxi:NN}
3118 % \begin{macro}{\@@_get_grabber_auxii:NN}
3119 % Given a token |#1|, defines an expandable function delimited by that
3120 % token and stores it in the token list~|#2|. The function is named
3121 % after the token, unless that function name is already taken by some
3122 % other grabber (this can happen in the rare case where delimiters
3123 % with different category codes are used in the same document): in
3124 % that case use a global counter to get a unique name. Since the
3125 % grabbers are not named after \pkg{xparse} commands they should not
3126 % be used to get material from the input stream.
3127 % \begin{macrocode}
3128 \cs_new_protected:Npn \@@_get_grabber:NN #1#2
3129 {
3130 \cs_set:Npn \@@_tmp:w ##1 #1 {##1}
3131 \exp_args:Nc \@@_get_grabber_auxi:NN
3132 { @@_grabber_ \token_to_str:N #1 :w } #2
3133 }
3134 \cs_new_protected:Npn \@@_get_grabber_auxi:NN #1#2
3135 {
3136 \cs_if_eq:NNTF \@@_tmp:w #1
3137 { \tl_set:Nn #2 {#1} }
3138 {
3139 \cs_if_exist:NTF #1
3140 {
3141 \int_gincr:N \g_@@_grabber_int
3142 \exp_args:Nc \@@_get_grabber_auxi:NN
3143 {
3144 @@_grabber_
3145 - \int_use:N \g_@@_grabber_int :w
3146 }
3147 #2
3148 }
3149 { \@@_get_grabber_auxii:NN #1 #2 }
3150 }
3151 }
3152 \cs_new_protected:Npn \@@_get_grabber_auxii:NN #1#2
3153 {
3154 \cs_set_eq:NN #1 \@@_tmp:w
3155 \tl_set:Nn #2 {#1}
3156 }
3157 % \end{macrocode}
3158 % \end{macro}
3159 % \end{macro}
3160 % \end{macro}
3161 %
3162 % \subsubsection{Copying a command and its internal structure}
3163 %
3164 % \begin{macrocode}
3165 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_copy:NN}%
3166 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3167 % \end{macrocode}
3168 %
3169 % \changes{v1.0h}{2021/08/30}{Added support for \cs{NewCommandCopy}}
3170 % Since the 2020-10-01 \LaTeXe{} release, support for copying, and
3171 % showing the definition of, robust commands has been available, but the
3172 % specifics of each command are implemented separately. Here we'll add
3173 % support for copying and showing \pkg{ltcmd} definitions.
3174 %
3175 % To fully support copying, we need two commands: a conditional to test
3176 % if a command is in fact a \pkg{ltcmd} command, and another command to
3177 % actually copy the command. The conditional is defined later as
3178 % \cs{__kernel_cmd_if_xparse:NTF}, so now to the copying:
3179 %
3180 % \begin{macro}{\@@_copy:NN}
3181 % \begin{macro}{\@@_set_eq_if_exist:NN,\@@_set_eq_if_exist:cc}
3182 % This macro just branches to the proper copying command by using
3183 % \cs{@@_cmd_type_cases:NnnnnnF}. The copying command takes the names
3184 % of the commands to be copied to and from, and the actual commands
3185 % as its four arguments.
3186 % \begin{macrocode}
3187 \cs_new_protected:Npn \@@_copy:NN #1 #2
3188 {
3189 \use:e
3190 {
3191 \int_set:Nn \tex_escapechar:D { 92 }
3192 \exp_not:N \@@_cmd_type_cases:NnnnnnF \exp_not:N #2
3193 { \@@_copy_command:nnNN }
3194 { \@@_copy_expandable:nnNN }
3195 { \@@_copy_optimized:nnNN }
3196 { \@@_copy_environment:nnNN }
3197 { \@@_copy_environment_end:nnNN }
3198 { \@@_cant_copy:nwn { non-ltcmd } }
3199 { \cs_to_str:N #1 } { \cs_to_str:N #2 }
3200 \exp_not:N #1 \exp_not:N #2
3201 \exp_not:N \@@_break_point:n { \cs_to_str:N #2 }
3202 \int_set:Nn \tex_escapechar:D { \int_use:N \tex_escapechar:D }
3203 }
3204 }
3205 \cs_new_protected:Npn \@@_set_eq_if_exist:NN #1 #2
3206 { \cs_if_exist:NTF #2 { \cs_set_eq:NN } { \use_none:nn } #1 #2 }
3207 \cs_generate_variant:Nn \@@_set_eq_if_exist:NN { cc }
3208 % \end{macrocode}
3209 %
3210 % \begin{macro}{\@@_cant_copy:nwn}
3211 % An utility macro similar to \cs{@@_bad_def:wn} to abort a command
3212 % copy. Contrary to \cs{@@_bad_def:wn} though, when this happens the
3213 % issue is most likely internal, because the command was already
3214 % (supposedly) correctly defined so it should be copyable. Hopefully
3215 % this macro will never be used ever, but if it does, apologise and
3216 % give the reason for the failure so the user can report.
3217 % \begin{macrocode}
3218 \cs_new_protected:Npn \@@_cant_copy:nwn #1 #2 \@@_break_point:n #3
3219 { \msg_error:nnnn { cmd } { copy-bug } {#1} {#3} }
3220 \msg_new:nnn { cmd } { copy-bug }
3221 {
3222 Error~while~copying~command~\iow_char:N\\#2:\\
3223 \str_case:nn {#1}
3224 {
3225 { non-ltcmd } { Command~is~not~a~valid~ltcmd~command. }
3226 { unknown-type } { Found~an~unknown~argument~type. }
3227 { invalid-end }
3228 { Target~command~is~not~named~\iow_char:N \\end<name>. }
3229 }
3230 }
3231 % \end{macrocode}
3232 % \end{macro}
3233 %
3234 % And, of course, add \cs{__kernel_cmd_if_xparse:NTF} and
3235 % \cs{@@_copy:NN} to \cs{@declarecommandcopylisthook}:
3236 % \begin{macrocode}
3237 \tl_gput_right:Nn \@declarecommandcopylisthook
3238 { { \__kernel_cmd_if_xparse:NTF \@@_copy:NN } }
3239 % \end{macrocode}
3240 % \end{macro}
3241 % \end{macro}
3242 %
3243 % \begin{macro}{\@@_copy_command:nnNN,\@@_copy_command:NnNNnnnn}
3244 % A normal (non-expandable) command has a pretty straightforward
3245 % structure. Its definition is stored in
3246 % \cs{\meta{cmd}\textvisiblespace code}, its defaults (if any) are
3247 % stored in \cs{\meta{cmd}\textvisiblespace defaults}, and its
3248 % top-level definition contains its signature, which can just be
3249 % copied over. \cs{@@_copy_command:nnNN} copies the command code and
3250 % defaults, and then defines the top-level command using the auxiliary
3251 % \cs{@@_copy_command:NnNNnnnn}. This macro takes the signature of
3252 % the command being copied from its top-level definition, and replaces
3253 % the named bits with the new name.
3254 % \begin{macrocode}
3255 \cs_new_protected:Npn \@@_copy_command:nnNN #1 #2 #3 #4
3256 {
3257 \cs_set_eq:cc { #1 ~ code } { #2 ~ code }
3258 \@@_set_eq_if_exist:cc { #1 ~ defaults } { #2 ~ defaults }
3259 \cs_set_protected_nopar:Npe #3
3260 { \exp_after:wN \@@_copy_command:NnNNnnnn #4 {#1} }
3261 }
3262 \cs_new:Npn \@@_copy_command:NnNNnnnn #1 #2 #3 #4 #5 #6 #7 #8
3263 {
3264 #1 \exp_not:n { {#2} }
3265 \exp_not:c { #8 ~ } \exp_not:c { #8 ~ code }
3266 \exp_not:n { {#5} {#6} {#7} }
3267 }
3268 % \end{macrocode}
3269 % \end{macro}
3270 %
3271 % \begin{macro}{\@@_copy_expandable:nnNN,\@@_copy_expandable:NnNNNNnnn}
3272 % An expandable command is slightly more complicated. Besides the
3273 % \cs{\meta{cmd}\textvisiblespace code}, and
3274 % \cs{\meta{cmd}\textvisiblespace defaults}, it also has an auxiliary
3275 % \cs{\meta{cmd}\textvisiblespace} for grabbing delimited arguments,
3276 % and possibly another auxiliary
3277 % \cs{\meta{cmd}\textvisiblespace\textvisiblespace}, if the command
3278 % has both long and short arguments. Then, its signature also has
3279 % several specific bits that are unique to that command; this is in contrast
3280 % to non-expandable commands, which use a common set of parsing functions.
3281 %
3282 % We start by copying the basics, then call
3283 % \cs{@@_copy_expandable_signature:NnNNNNnnn} to parse the signature
3284 % of the command and build up the modified copy in a temporary token list,
3285 % then we call \cs{@@_copy_expandable:NnNNNNnnn} that will copy the
3286 % top-level definition of the command, with the proper internal
3287 % renames.
3288 %
3289 % \begin{macrocode}
3290 %<latexrelease>\EndIncludeInRelease
3291 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_copy:NN}%
3292 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3293 %<latexrelease>\EndIncludeInRelease
3294 % \end{macrocode}
3295 %
3296 % \changes{v1.1c}{2023/03/12}
3297 % {Distinguish (non-expandable) document commands starting with
3298 % \cs{__cmd_start_expandable:nNNNNn}}
3299 % There's one variant: a command begins with \cs{@@_start_expandable:nNNNNn}
3300 % may still be un-expandable/protected if it's defined by
3301 % \cs{NewDocumentCommand} and friends, with empty or only m-type arguments.
3302 %
3303 % \begin{macrocode}
3304 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_copy_expandable:nnNN}%
3305 %<latexrelease> {Distinguish~non-expandable~document~commands}
3306 \cs_new_protected:Npn \@@_copy_expandable:nnNN #1 #2 #3 #4
3307 {
3308 \cs_set_eq:cc { #1 ~ code } { #2 ~ code }
3309 \@@_set_eq_if_exist:cc { #1 ~ } { #2 ~ }
3310 \@@_set_eq_if_exist:cc { #1 ~ \c_space_tl } { #2 ~ \c_space_tl }
3311 \@@_set_eq_if_exist:cc { #1 ~ defaults } { #2 ~ defaults }
3312 \exp_after:wN \@@_copy_expandable_signature:NnNNNNnnn #4 {#1} {#2}
3313 \token_if_protected_macro:NTF #4
3314 { \cs_set_protected_nopar:Npe }{ \cs_set_nopar:Npe }
3315 #3
3316 { \exp_after:wN \@@_copy_expandable:NnNNNNnnn #4 {#1} {#2} }
3317 }
3318 %<latexrelease>\EndIncludeInRelease
3319 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_copy_expandable:nnNN}%
3320 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3321 %<latexrelease>\cs_new_protected:Npn \@@_copy_expandable:nnNN #1 #2 #3 #4
3322 %<latexrelease> {
3323 %<latexrelease> \cs_set_eq:cc { #1 ~ code } { #2 ~ code }
3324 %<latexrelease> \@@_set_eq_if_exist:cc { #1 ~ } { #2 ~ }
3325 %<latexrelease> \@@_set_eq_if_exist:cc { #1 ~ \c_space_tl } { #2 ~ \c_space_tl }
3326 %<latexrelease> \@@_set_eq_if_exist:cc { #1 ~ defaults } { #2 ~ defaults }
3327 %<latexrelease> \exp_after:wN \@@_copy_expandable_signature:NnNNNNnnn #4 {#1} {#2}
3328 %<latexrelease> \cs_set_nopar:Npx #3
3329 %<latexrelease> { \exp_after:wN \@@_copy_expandable:NnNNNNnnn #4 {#1} {#2} }
3330 %<latexrelease> }
3331 %<latexrelease>\EndIncludeInRelease
3332 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_copy_expandable:nnNN}%
3333 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3334 %<latexrelease>\EndIncludeInRelease
3335 % \end{macrocode}
3336 %
3337 % \begin{macro}{\@@_copy_optimized:nnNN}
3338 % Copy the code, simply define the wrapper.
3339 % \begin{macrocode}
3340 \cs_new_protected:Npn \@@_copy_optimized:nnNN #1#2#3#4
3341 {
3342 \cs_set_eq:cc { #1 ~ code } { #2 ~ code }
3343 \token_if_protected_macro:NTF #4
3344 { \cs_set_protected_nopar:Npe }
3345 { \cs_set_nopar:Npe }
3346 #3
3347 {
3348 \exp_not:N \@@_start_optimized:
3349 \exp_not:c { #1 ~ code }
3350 }
3351 }
3352 % \end{macrocode}
3353 % \end{macro}
3354 %
3355 % \begin{macrocode}
3356 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_copy:NN (part 2)}%
3357 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3358 % \end{macrocode}
3359 %
3360 % \begin{macrocode}
3361 \cs_new:Npn \@@_copy_expandable:NnNNNNnnn #1 #2 #3 #4 #5 #6 #7 #8 #9
3362 {
3363 \exp_not:N #1 \exp_not:n { {#2} }
3364 \exp_not:c { #8 ~ }
3365 \exp_not:c
3366 {
3367 #8 ~
3368 \str_if_eq:eeT
3369 { \exp_not:c { #9 ~ \c_space_tl } } { \exp_not:N #4 }
3370 { \c_space_tl }
3371 }
3372 \exp_not:c { #8 ~ code }
3373 \str_if_eq:eeTF { \exp_not:N #6 } { ? }
3374 { ? }
3375 { \exp_not:c { #8 ~ defaults } }
3376 { \exp_not:V \l_@@_tmpa_tl }
3377 }
3378 % \end{macrocode}
3379 %
3380 % \begin{macro}{
3381 % \@@_copy_expandable_signature:NnNNNNnnn,
3382 % \@@_copy_expandable:nnN,
3383 % \@@_copy_parse_grabber:w,
3384 % }
3385 % A signature for an expandable command contains as many
3386 % \cs{expandable_grab_\meta{type}:w} as there are arguments, and what
3387 % follows this macro depends on the \meta{type}. We'll start a loop
3388 % through the signature, and at each argument grabber, we'll step the
3389 % argument count, and look for the \meta{type} with
3390 % \cs{@@_copy_parse_grabber:w} so that we know which
3391 % \cs{@@_copy_grabber_\meta{type}:w} to call next.
3392 % \begin{macrocode}
3393 \cs_new_protected:Npn \@@_copy_expandable_signature:NnNNNNnnn
3394 #1 #2 #3 #4 #5 #6 #7 #8 #9
3395 {
3396 \int_zero:N \l_@@_current_arg_int
3397 \tl_clear:N \l_@@_tmpa_tl
3398 \@@_copy_expandable:nnN {#8} {#9} #7
3399 \q_recursion_tail \q_recursion_stop
3400 }
3401 \cs_new_protected:Npn \@@_copy_expandable:nnN #1 #2 #3
3402 {
3403 \quark_if_recursion_tail_stop:n {#3}
3404 \int_incr:N \l_@@_current_arg_int
3405 \exp_after:wN \@@_copy_parse_grabber:w \token_to_str:N #3 {#1} {#2}
3406 }
3407 \use:e
3408 {
3409 \cs_new_protected:Npn \exp_not:N \@@_copy_parse_grabber:w #1
3410 \tl_to_str:n { expandable_grab_ } #2 \tl_to_str:n { :w }
3411 {
3412 \tl_put_right:Ne \exp_not:N \l_@@_tmpa_tl
3413 { \exp_not:N \exp_not:c { @@_expandable_grab_#2:w } }
3414 \exp_not:N \cs_if_exist_use:cF { @@_copy_grabber_#2:w }
3415 { \@@_cant_copy:nwn { unknown-type } }
3416 }
3417 }
3418 % \end{macrocode}
3419 %
3420 % \begin{macro}{
3421 % \@@_copy_grabber_D:w,\@@_copy_grabber_D_alt:w,
3422 % \@@_copy_grabber_R:w,\@@_copy_grabber_R_alt:w,
3423 % \@@_copy_grabber_E:w,\@@_copy_grabber_E_long:w,
3424 % \@@_copy_grabber_t:w,
3425 % \@@_copy_grabber_m:w,\@@_copy_grabber_m_long:w,
3426 % }
3427 % The most complicated is the |D|elimited argument: each argument has
3428 % a dedicated grabbing function named after the command that has to be
3429 % copied over (of the form
3430 % \cs{\meta{cmd}\textvisiblespace(arg\textvisiblespace\meta{num})}).
3431 % \begin{macrocode}
3432 \cs_new_protected:Npn \@@_copy_grabber_D:w #1 #2 #3 #4 #5
3433 {
3434 \tl_put_right:Ne \l_@@_tmpa_tl
3435 {
3436 \exp_not:c { #1 ~ (arg ~ \int_use:N \l_@@_current_arg_int ) }
3437 \exp_not:n { #4 #5 }
3438 }
3439 \cs_set_eq:cc
3440 { #1 ~ (arg ~ \int_use:N \l_@@_current_arg_int ) }
3441 { #2 ~ (arg ~ \int_use:N \l_@@_current_arg_int ) }
3442 \@@_copy_expandable:nnN {#1} {#2}
3443 }
3444 % \end{macrocode}
3445 %
3446 % |D_alt| is just a special case of |D| that uses a single delimiter
3447 % (used when both delimiters of the argument are identical):
3448 % \begin{macrocode}
3449 \cs_new_protected:Npn \@@_copy_grabber_D_alt:w #1 #2 #3 #4
3450 { \@@_copy_grabber_D:w {#1} {#2} {#3} {#4} { } }
3451 % \end{macrocode}
3452 %
3453 % As far as copying is concerned, |R| is identical to |D|:
3454 % \begin{macrocode}
3455 \cs_new_eq:NN \@@_copy_grabber_R:w \@@_copy_grabber_D:w
3456 \cs_new_eq:NN \@@_copy_grabber_R_alt:w \@@_copy_grabber_D_alt:w
3457 % \end{macrocode}
3458 %
3459 % |E| is straightforward: we just copy the embellishments over, and
3460 % increase the current argument number \cs{l_@@_current_arg_int} by
3461 % the number of embellishments (minus one because there is a
3462 % \cs{int_incr:N} down the line).
3463 % \begin{macrocode}
3464 \cs_new_protected:Npn \@@_copy_grabber_E:w #1 #2 #3 #4
3465 {
3466 \tl_put_right:Nn \l_@@_tmpa_tl { {#3} {#4} }
3467 \int_add:Nn \l_@@_current_arg_int { \tl_count:n {#4} - 1 }
3468 \@@_copy_expandable:nnN {#1} {#2}
3469 }
3470 \cs_new_eq:NN \@@_copy_grabber_E_long:w \@@_copy_grabber_E:w
3471 % \end{macrocode}
3472 %
3473 % |t| just needs copying the token to be tested for:
3474 % \begin{macrocode}
3475 \cs_new_protected:Npn \@@_copy_grabber_t:w #1 #2 #3 #4
3476 {
3477 \tl_put_right:Nn \l_@@_tmpa_tl { #3 #4 }
3478 \@@_copy_expandable:nnN {#1} {#2}
3479 }
3480 % \end{macrocode}
3481 %
3482 % And last but not least, |m| is the simplest; the grabber is just
3483 % \cs{@@_expandable_grab_m:w}, which is already added to the new
3484 % command so here we just resume the loop:
3485 % \begin{macrocode}
3486 \cs_new_protected:Npn \@@_copy_grabber_m:w { \@@_copy_expandable:nnN }
3487 \cs_new_eq:NN \@@_copy_grabber_m_long:w \@@_copy_grabber_m:w
3488 % \end{macrocode}
3489 % \end{macro}
3490 % \end{macro}
3491 % \end{macro}
3492 %
3493 % \begin{macro}{\@@_copy_environment:nnNN}
3494 % \begin{macro}{\@@_copy_environment:Nnnnnnn}
3495 % Copying an environment's \cs{begin} part is pretty much like copying
3496 % a command, except it has a longer name, and at the end we have to
3497 % copy \cs[no-index]{environment~\meta{name}} into
3498 % \cs[no-index]{\meta{name}}.
3499 % \begin{macrocode}
3500 \cs_new_protected:Npn \@@_copy_environment:nnNN #1 #2 #3 #4
3501 {
3502 \cs_set_eq:cc { environment~ #1 ~ code } { environment~ #2 ~ code }
3503 \@@_set_eq_if_exist:cc
3504 { environment~ #1 ~ defaults } { environment~ #2 ~ defaults }
3505 \cs_set_protected_nopar:cpe { environment~ #1 }
3506 { \exp_after:wN \@@_copy_environment:Nnnnnnn #4 {#1} }
3507 \cs_set_eq:cc {#1} { environment~ #1 }
3508 }
3509 \cs_new:Npn \@@_copy_environment:Nnnnnnn #1 #2 #3 #4 #5 #6 #7
3510 { #1 \exp_not:n { {#2} } {#7} \exp_not:n { {#4} {#5} {#6} } }
3511 % \end{macrocode}
3512 % \end{macro}
3513 % \end{macro}
3514 %
3515 % \begin{macro}{\@@_copy_environment_end:nnNN}
3516 % \begin{macro}{\@@_copy_environment_end_aux:nnNN}
3517 % Copying an environment's \cs{end} part is a bit trickier. We first
3518 % have to make sure that both parts are named
3519 % \cs[no-index]{end\meta{name}} (that's actually not a hard
3520 % requirement, but an environment \cs{end} command makes no sense
3521 % without the |end| in its name), and strip the leading |end| from the
3522 % strings. After that, copying is straightforward.
3523 % \begin{macrocode}
3524 \cs_new_protected:Npn \@@_copy_environment_end:nnNN #1 #2
3525 {
3526 \@@_check_end:Nn \l_@@_tmpa_tl {#1}
3527 \@@_check_end:Nn \l_@@_tmpb_tl {#2}
3528 \exp_args:Noo \@@_copy_environment_end_aux:nnNN
3529 { \l_@@_tmpa_tl } { \l_@@_tmpb_tl }
3530 }
3531 \cs_new_protected:Npn \@@_copy_environment_end_aux:nnNN #1 #2 #3 #4
3532 {
3533 \cs_set_nopar:cpe { environment~ #1 ~end }
3534 { \exp_not:c { environment~ #1 ~end~aux } }
3535 \cs_set_eq:cc
3536 { environment~ #1 ~end~aux~ } { environment~ #2 ~end~aux~ }
3537 \cs_set_eq:cc { end #1 } { environment~ #1 ~end }
3538 }
3539 % \end{macrocode}
3540 %
3541 % \begin{macro}{\@@_check_end:Nn,\@@_check_end:n,\@@_check_end:w}
3542 % To check whether an \cs{end} command is valid, we look for the
3543 % string |end| at the beginning of the command name, and if not found,
3544 % raise an error:
3545 % \begin{macrocode}
3546 \cs_new_protected:Npn \@@_check_end:Nn #1 #2
3547 {
3548 \tl_set:Ne #1 { \@@_check_end:n {#2} }
3549 \token_if_eq_meaning:NNT #1 \q_nil
3550 { \@@_cant_copy:nwn { invalid-end } }
3551 }
3552 \cs_set_protected:Npn \@@_tmp:w #1
3553 {
3554 \cs_new:Npn \@@_check_end:n ##1
3555 {
3556 \exp_after:wN \@@_check_end:w \tl_to_str:n {##1}
3557 #1 \q_mark #1 \q_stop
3558 }
3559 \cs_new:Npn \@@_check_end:w ##1 #1 ##2 #1 ##3 \q_stop
3560 { \if_meaning:w ##2 \q_mark \exp_not:N \q_nil \else: ##2 \fi: }
3561 }
3562 \exp_args:No \@@_tmp:w { \tl_to_str:n { end } }
3563 % \end{macrocode}
3564 % \end{macro}
3565 % \end{macro}
3566 % \end{macro}
3567 %
3568 % Not much to do regarding \pkg{latexrelease}: we could remove the
3569 % entries from \cs{@declarecommandcopylisthook}, but it doesn't seem
3570 % worth it.
3571 % \begin{macrocode}
3572 %<latexrelease>\EndIncludeInRelease
3573 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_copy:NN (part 2)}%
3574 %<latexrelease> {Support~\NewCommandCopy~in~ltcmd}
3575 %<latexrelease>\EndIncludeInRelease
3576 % \end{macrocode}
3577 %
3578 % \subsubsection{Showing the definition of a command}
3579 %
3580 % \begin{macrocode}
3581 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_show:N}%
3582 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3583 % \end{macrocode}
3584 %
3585 % \changes{v1.0h}{2021/08/30}{Added support for \cs{ShowCommand}}
3586 % To show the definition of a command we need more or less the same
3587 % building blocks as for copying, except that instead of making a copy,
3588 % we'll just print stuff to the terminal.
3589 %
3590 % \begin{macro}{\@@_show:N}
3591 % This macro just branches to the proper showing command by using
3592 % \cs{@@_cmd_type_cases:NnnnnnF}. The showing command takes the command
3593 % to be shown as argument.
3594 % \begin{macrocode}
3595 \cs_new_protected:Npn \@@_show:N #1
3596 {
3597 \use:e
3598 {
3599 \int_set:Nn \tex_escapechar:D { 92 }
3600 \exp_not:N \@@_cmd_type_cases:NnnnnnF \exp_not:N #1
3601 { \@@_show_command:N }
3602 { \@@_show_expandable:N }
3603 { \@@_show_optimized:N }
3604 { \@@_show_environment:N }
3605 { \@@_show_environment_end:N }
3606 { \@@_cant_copy:nwn { non-ltcmd } }
3607 \exp_not:N #1
3608 \exp_not:N \@@_break_point:n { \cs_to_str:N #1 }
3609 \int_set:Nn \tex_escapechar:D { \int_use:N \tex_escapechar:D }
3610 }
3611 }
3612 % \end{macrocode}
3613 % \end{macro}
3614 %
3615 % \begin{macro}{
3616 % \@@_show_command:N,
3617 % \@@_show_command:NnNNwN,
3618 % \@@_show_expandable:N,
3619 % \@@_show_expandable:NnNNNNnN,
3620 % \@@_show_optimized:N,
3621 % \@@_show_command_aux:NnNNn,
3622 % \@@_show_environment:N,
3623 % \@@_show:e,
3624 % }
3625 % These commands just expand the command once to reveal its innards,
3626 % then pass the type of command, the control sequence, the signature,
3627 % and the code macro to \cs{@@_show_command_aux:NnNNn}.
3628 % \begin{macrocode}
3629 \cs_new_protected:Npn \@@_show_command:N #1
3630 { \exp_after:wN \@@_show_command:NnNNwN #1 \q_@@ #1 }
3631 \cs_new_protected:Npn \@@_show_command:NnNNwN #1 #2 #3 #4 #5 \q_@@ #6
3632 {
3633 \@@_show_command_aux:NnNNn \tl_show:e
3634 { document~command } #6 #4 {#2}
3635 }
3636 \cs_new_protected:Npn \@@_show_expandable:N #1
3637 { \exp_after:wN \@@_show_expandable:NnNNNNnN #1 #1 }
3638 % \end{macrocode}
3639 %
3640 % \begin{macrocode}
3641 %<latexrelease>\EndIncludeInRelease
3642 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_show:N}%
3643 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3644 %<latexrelease>\EndIncludeInRelease
3645 % \end{macrocode}
3646 %
3647 % \changes{v1.1c}{2023/03/12}
3648 % {Distinguish (non-expandable) document commands starting with
3649 % \cs{__cmd_start_expandable:nNNNNn}}
3650 % There's one variant: a command begins with \cs{@@_start_expandable:nNNNNn}
3651 % may still be un-expandable/protected if it's defined by
3652 % \cs{NewDocumentCommand} and friends, with empty or only m-type arguments.
3653 % \begin{macrocode}
3654 %<latexrelease>\IncludeInRelease{2023/06/01}{\@@_show_expandable:NnNNNNnN}%
3655 %<latexrelease> {Distinguish~non-expandable~document~commands}
3656 \cs_new_protected:Npn \@@_show_expandable:NnNNNNnN #1 #2 #3 #4 #5 #6 #7 #8
3657 {
3658 \exp_args:NNe \@@_show_command_aux:NnNNn \tl_show:e
3659 { \token_if_protected_macro:NF #8 { expandable~ } document~command }
3660 #8 #5 {#2}
3661 }
3662 %<latexrelease>\EndIncludeInRelease
3663 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_show_expandable:NnNNNNnN}%
3664 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3665 %<latexrelease>\cs_new_protected:Npn \@@_show_expandable:NnNNNNnN #1 #2 #3 #4 #5 #6 #7 #8
3666 %<latexrelease> {
3667 %<latexrelease> \@@_show_command_aux:NnNNn \tl_show:x
3668 %<latexrelease> { expandable~document~command } #8 #5 {#2}
3669 %<latexrelease> }
3670 %<latexrelease>\EndIncludeInRelease
3671 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_show_expandable:NnNNNNnN}%
3672 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3673 %<latexrelease>\EndIncludeInRelease
3674 % \end{macrocode}
3675 %
3676 % \begin{macrocode}
3677 %<latexrelease>\IncludeInRelease{2021/11/15}{\@@_show:N (part 2)}%
3678 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3679 % \end{macrocode}
3680 %
3681 % Now just print everything in the required format. The auxiliary
3682 % \cs{@@_split_signature:n} stores a ready-to-print token list in
3683 % \cs{l_@@_tmpa_tl}, so we ust use that here:
3684 % \changes{v1.0j}{2021/13/31}
3685 % {Make \cs{ShowCommand} stop for interaction}
3686 % \changes{v1.1b}{2022/11/30}
3687 % {Don't stop for the \cs{begin} part of an environment}
3688 % \begin{macrocode}
3689 \cs_new_protected:Npn \@@_show_command_aux:NnNNn #1 #2 #3 #4 #5
3690 {
3691 \@@_split_signature:n {#5}
3692 #1
3693 {
3694 \token_to_str:N #3 = #2: \iow_newline:
3695 \tl_use:N \l_@@_tmpa_tl
3696 -> \cs_replacement_spec:N #4
3697 }
3698 }
3699 % \end{macrocode}
3700 %
3701 % Optimized functions need things done a bit differently as we need to
3702 % reconstruct the argument spec.
3703 % \begin{macrocode}
3704 \cs_new_protected:Npn \@@_show_optimized:N #1
3705 {
3706 \exp_args:Nc \@@_show_optimized:NN
3707 { \cs_to_str:N #1 \c_space_tl code }
3708 #1
3709 }
3710 \cs_new_protected:Npn \@@_show_optimized:NN #1#2
3711 {
3712 \cs_set:Npe \@@_show_optimized_aux:N ##1
3713 {
3714 \c_space_tl \c_space_tl \c_hash_str ##1 :
3715 \bool_lazy_or:nnT
3716 { \token_if_long_macro_p:N #1 }
3717 { \token_if_protected_long_macro_p:N #1 }
3718 { + } m
3719 \iow_newline:
3720 }
3721 \tl_show:e
3722 {
3723 \token_to_str:N #2 =
3724 \bool_lazy_or:nnF
3725 { \token_if_protected_macro_p:N #1 }
3726 { \token_if_protected_long_macro_p:N #1 }
3727 { expandable ~ } document~command:
3728 \iow_newline:
3729 \int_step_function:nN
3730 {
3731 \int_div_truncate:nn
3732 { \tl_count:e { \cs_parameter_spec:N #1 } }
3733 { 2 }
3734 }
3735 \@@_show_optimized_aux:N
3736 ->
3737 \cs_replacement_spec:N #1
3738 }
3739 }
3740 % \end{macrocode}
3741 %
3742 % We can reuse most of the above to show an environment, except that
3743 % we need to ensure that the proper \cs[no-index]{environment~\ldots}
3744 % are passed to \cs{@@_show_command_aux:NnNNn}. Additionally, when
3745 % |\ShowCommand\foo| is used (if |foo| is an environment), we show
3746 % |\endfoo| as well, and when |\ShowCommand\endfoo| is used, change
3747 % that to |\ShowCommand\foo| and do the same.
3748 % \changes{v1.0j}{2021/13/31}
3749 % {Make \cs{ShowCommand} stop for interaction}
3750 % \changes{v1.1b}{2022/11/30}
3751 % {Don't stop for the \cs{begin} part of an environment}
3752 % \changes{v1.3e}{2025-06-27}{Support \cs{showstream} (gh/1062)}
3753 % \begin{macrocode}
3754 \cs_new_protected:Npn \@@_show_environment:N #1
3755 {
3756 \exp_after:wN \@@_show_environment:Nnnw #1 \q_@@
3757 \tl_show:e
3758 {
3759 \token_to_str:N \end { \cs_to_str:N #1 } : \iow_newline:
3760 -> \exp_args:Nc \cs_replacement_spec:N
3761 { environment~ \cs_to_str:N #1 ~end~aux~ }
3762 }
3763 }
3764 \cs_new_protected:Npn \@@_show_environment:Nnnw #1 #2 #3 #4 \q_@@
3765 {
3766 \use:e
3767 {
3768 \@@_show_command_aux:NnNNn \@@_show:e { document~environment }
3769 { \exp_not:N \begin {#3} }
3770 \exp_not:c { environment~ #3 ~ code }
3771 {#2}
3772 }
3773 }
3774 \cs_if_exist:NTF \iow_show:e
3775 {
3776 \cs_new_protected:Npn \@@_show:e #1
3777 { \iow_show:e { > ~ #1 . \iow_newline: } }
3778 }
3779 {
3780 \cs_new_protected:Npn \@@_show:e #1
3781 { \iow_term:e { > ~ #1 . \iow_newline: } }
3782 }
3783 \cs_new_protected:Npn \@@_show_environment_end:N #1
3784 {
3785 \exp_args:NNe \@@_check_end:Nn \l_@@_tmpa_tl { \cs_to_str:N #1 }
3786 \exp_args:Nc \@@_show_environment:N { \l_@@_tmpa_tl }
3787 }
3788 % \end{macrocode}
3789 %
3790 % And, of course, add \cs{__kernel_cmd_if_xparse:NTF} and
3791 % \cs{@@_show:N} to \cs{@showcommandlisthook} and to
3792 % \cs{@showenvironmentlisthook} (\cs{@@_show:N} takes care of the
3793 % environment case as well, so both entries are identical):
3794 % \changes{v1.1b}{2022-11-29}{Add \cs{@showenvironmentlisthook}}
3795 % \begin{macrocode}
3796 \tl_gput_right:Nn \@showcommandlisthook
3797 { { \__kernel_cmd_if_xparse:NTF \@@_show:N } }
3798 \tl_gput_right:Nn \@showenvironmentlisthook
3799 { { \__kernel_cmd_if_xparse:NTF \@@_show:N } }
3800 % \end{macrocode}
3801 % \end{macro}
3802 %
3803 % \begin{macro}{\@@_split_signature:n}
3804 % Now we'll try a least-effort adventure into splitting the symbolic
3805 % user-provided signature for a command into individual parameters for
3806 % pretty-printing. A counter is used to keep track of the current
3807 % argument number, and two token lists are used: \cs{l_@@_tmpa_tl}
3808 % holds the final token list to be printed, and \cs{l_@@_tmpb_tl}
3809 % holds just the current item, so that we can make changes to an
3810 % individual item without having to dissect the whole thing (this is
3811 % used for |e|- and |E|-types).
3812 % \begin{macrocode}
3813 \cs_new_protected:Npn \@@_split_signature:n #1
3814 {
3815 \int_set:Nn \l_@@_current_arg_int { 1 }
3816 \tl_clear:N \l_@@_tmpa_tl
3817 \tl_clear:N \l_@@_tmpb_tl
3818 \@@_split_signature_loop:Nw #1 \q_recursion_tail \q_recursion_stop
3819 }
3820 % \end{macrocode}
3821 %
3822 % \begin{macro}{\@@_split_signature_loop:Nw}
3823 % This is the main chunk of the loop: it starts an item with
3824 % \cs{@@_split_start_item:}
3825 % (this adds indentation and the argument number to
3826 % \cs{l_@@_tmpb_tl}), then checks if a special token list
3827 % \cs[no-index]{c_@@_show_type_\meta{type}_tl} exists. If it doesn't,
3828 % the current argument is a \enquote{simple} type which needs no extra
3829 % processing. Otherwise, call a specific function depending on the
3830 % value of said token list.
3831 % \begin{macrocode}
3832 \cs_new_protected:Npn \@@_split_signature_loop:Nw #1
3833 {
3834 \quark_if_recursion_tail_stop:N #1
3835 \tl_if_empty:NT \l_@@_tmpb_tl { \@@_split_start_item: }
3836 \tl_if_exist:cTF { c_@@_show_type_#1_tl }
3837 {
3838 \use:c
3839 {
3840 @@_show_
3841 \if_case:w \tl_use:c { c_@@_show_type_#1_tl } \exp_stop_f:
3842 delim \or: delims \or: delims_opt \or: opt \or:
3843 e \or: E \or: prefix \or: processor \fi: :Nw
3844 } #1
3845 }
3846 { \@@_split_end_item:n {#1} \@@_split_signature_loop:Nw }
3847 }
3848 % \end{macrocode}
3849 %
3850 % \begin{macro}{
3851 % \c_@@_show_type_t_tl,
3852 % \c_@@_show_type_r_tl,\c_@@_show_type_d_tl,
3853 % \c_@@_show_type_R_tl,\c_@@_show_type_D_tl,
3854 % \c_@@_show_type_O_tl,
3855 % \c_@@_show_type_e_tl,
3856 % \c_@@_show_type_E_tl,
3857 % \c_@@_show_type_+_tl,\c_@@_show_type_!_tl,
3858 % \c_@@_show_type_>_tl,
3859 % }
3860 % The token lists \cs[no-index]{c_@@_show_type_\meta{type}_tl} exist
3861 % for nontrivial (for printing) \meta{types} that require special
3862 % parsing (like delimiters or optional arguments). Values from~0 to~7
3863 % are assigned to each type:
3864 % \begin{enumerate}
3865 % \item a single delimiter token;
3866 % \item two delimiter tokens;
3867 % \item two delimiter tokens plus a default value;
3868 % \item a default value;
3869 % \item a list of embellishments (exclusive for |e|-type);
3870 % \item embellishments plus defaults (exclusive for |E|-type);
3871 % \item simple prefixes;
3872 % \item prefixes with arguments (argument processors);
3873 % \end{enumerate}
3874 % \begin{macrocode}
3875 \cs_set_protected:Npn \@@_tmp:w #1 #2
3876 {
3877 \quark_if_nil:nF {#1}
3878 { \tl_const:cn { c_@@_show_type_#1_tl } {#2} \@@_tmp:w }
3879 }
3880 \@@_tmp:w t0 r1 d1 R2 D2 O3 e4 E5 +6 !6 >7 =7 \q_nil \q_nil
3881 % \end{macrocode}
3882 %
3883 % \begin{macro}{
3884 % \@@_show_delim:Nw,\@@_show_delims:Nw,
3885 % \@@_show_delims_opt:Nw,\@@_show_opt:Nw,
3886 % \@@_show_e:Nw,\@@_show_E:Nw,
3887 % \@@_show_prefix:Nw,\@@_show_processor:Nw,
3888 % }
3889 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
3890 % Now, based on each type we know how to act. In most cases it is
3891 % just a matter of feeding in the grabbed arguments and resuming the
3892 % loop. The embellishments require a bit more attention: the
3893 % |e|-type loops through the list of embellishments and adds each to
3894 % the token list as a separate argument. The |E|-type does more or
3895 % less the same, but uses \cs{@@_tl_mapthread_function:nnN} to map
3896 % over two lists simultaneously, adding each token and default to the
3897 % token list for printing.
3898 % \begin{macrocode}
3899 \cs_new_protected:Npn \@@_show_delim:Nw #1 #2
3900 { \@@_split_end_item:n { #1 #2 } \@@_split_signature_loop:Nw }
3901 \cs_new_protected:Npn \@@_show_delims:Nw #1 #2 #3
3902 { \@@_split_end_item:n { #1 #2 #3 } \@@_split_signature_loop:Nw }
3903 \cs_new_protected:Npn \@@_show_delims_opt:Nw #1 #2 #3 #4
3904 { \@@_split_end_item:n { #1 #2 #3 {#4} } \@@_split_signature_loop:Nw }
3905 \cs_new_protected:Npn \@@_show_opt:Nw #1 #2
3906 { \@@_split_end_item:n { #1 {#2} } \@@_split_signature_loop:Nw }
3907 \cs_new_protected:Npn \@@_show_e:Nw #1 #2
3908 {
3909 \tl_map_inline:nn {#2}
3910 {
3911 \@@_split_start_item:
3912 \@@_split_end_item:n { #1 ##1 }
3913 }
3914 \@@_split_signature_loop:Nw
3915 }
3916 \cs_set_protected:Npn \@@_tmp:w #1
3917 {
3918 \cs_new_protected:Npn \@@_show_E:Nw ##1 ##2 ##3
3919 {
3920 \cs_set_protected:Npn \@@_tmp:w ####1 ####2
3921 {
3922 \@@_split_start_item:
3923 \@@_split_end_item:n { ##1 ####1 {####2} }
3924 }
3925 \@@_tl_mapthread_function:nnN {##2}
3926 { ##3 {#1} {#1} {#1} {#1} {#1} {#1} {#1} {#1} {#1} } \@@_tmp:w
3927 \@@_split_signature_loop:Nw
3928 }
3929 }
3930 \@@_tmp:w \NoValue
3931 % \end{macrocode}
3932 %
3933 % Minor wrinkle with the prefixes: they use \cs{@@_split_add_item:n}
3934 % instead of \cs{@@_split_end_item:n} (|add| \emph{vs.} |end|) because
3935 % they are followed by an argument, so they can't end the item.
3936 % \begin{macrocode}
3937 \cs_new_protected:Npn \@@_show_prefix:Nw #1
3938 { \@@_split_add_item:n {#1} \@@_split_signature_loop:Nw }
3939 \cs_new_protected:Npn \@@_show_processor:Nw #1 #2
3940 { \@@_split_add_item:n { #1 {#2} } \@@_split_signature_loop:Nw }
3941 % \end{macrocode}
3942 %
3943 % \begin{macro}{
3944 % \@@_split_start_item:,
3945 % \@@_split_add_item:n,
3946 % \@@_split_end_item:n,
3947 % }
3948 % And now the auxiliaries that store the strings to be printed.
3949 % \cs{@@_split_start_item:} starts an item from scratch,
3950 % \cs{@@_split_add_item:n} adds tokens to an item without adding a
3951 % newline, and \cs{@@_split_end_item:n} adds tokens, terminates the
3952 % item with a newline, and steps the argument count.
3953 % \begin{macrocode}
3954 \cs_new_protected:Npn \@@_split_start_item:
3955 {
3956 \tl_set:Ne \l_@@_tmpb_tl
3957 { ~ \c_space_tl \c_hash_str \int_use:N \l_@@_current_arg_int : }
3958 }
3959 \cs_new_protected:Npn \@@_split_add_item:n #1
3960 { \tl_put_right:Ne \l_@@_tmpb_tl { \tl_to_str:n {#1} } }
3961 \cs_new_protected:Npn \@@_split_end_item:n #1
3962 {
3963 \tl_put_right:Ne \l_@@_tmpa_tl
3964 { \l_@@_tmpb_tl \tl_to_str:n {#1} \iow_newline: }
3965 \tl_clear:N \l_@@_tmpb_tl
3966 \int_incr:N \l_@@_current_arg_int
3967 }
3968 % \end{macrocode}
3969 % \end{macro}
3970 % \end{macro}
3971 % \end{macro}
3972 % \end{macro}
3973 % \end{macro}
3974 %
3975 % Not much to do regarding \pkg{latexrelease}: we could remove the
3976 % entries from \cs{@showcommandlisthook}, but it doesn't seem
3977 % worth it.
3978 % \begin{macrocode}
3979 %<latexrelease>\EndIncludeInRelease
3980 %
3981 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_show:N (part 2)}%
3982 %<latexrelease> {Support~\ShowCommand~in~ltcmd}
3983 %<latexrelease>\EndIncludeInRelease
3984 % \end{macrocode}
3985 %
3986 % \subsection{Grabbing arguments}
3987 %
3988 % All of the grabbers follow the same basic pattern. The initial
3989 % function stores in \cs{l_@@_signature_tl} the code to grab further
3990 % arguments, defines (the function in) \cs{l_@@_fn_tl} that will grab
3991 % the argument, and calls it.
3992 %
3993 % Defining \cs{l_@@_fn_tl} means determining whether to use
3994 % \cs{cs_set:Npn} or \cs{cs_set_nopar:Npn}, and for optional arguments
3995 % whether to skip spaces. Once the argument is found, \cs{l_@@_fn_tl}
3996 % calls \cs{@@_add_arg:n}, responsible for calling processors and
3997 % grabbing further arguments.
3998 %
3999 % \begin{macro}
4000 % {
4001 % \@@_grab_b:w,
4002 % \@@_grab_b_long:w,
4003 % \@@_grab_b_obey_spaces:w,
4004 % \@@_grab_b_long_obey_spaces:w,
4005 % \@@_grab_b_aux:NNw,
4006 % \@@_grab_b_end:Nw
4007 % }
4008 % \changes{v1.1a}{2022/08/10}{Track changes in \texttt{D}-type implementation}
4009 % This uses the well-tested code of \texttt{D}-type arguments,
4010 % skipping the peeking step because the \texttt{b}-type argument is
4011 % always present, and adding a cleanup stage at the end by hijacking
4012 % the signature. The clean-up consists of properly
4013 % dealing with \cs{l_@@_args_tl} and also putting back the \cs{end}
4014 % that served as an end-delimiter: this \cs{end} receives the
4015 % environment name as its argument and is run normally. The
4016 % \texttt{D}-type code stores the argument found (body of the
4017 % environment) as a brace group in \cs{l_@@_args_tl} and depending on
4018 % the presence of a prefix~|!| we trim spaces or not before adding
4019 % this braced argument into the saved \cs{l_@@_args_tl}.
4020 % The strange \verb*|\begin | control sequence is there for display
4021 % purposes only: it has to look like |\begin| in the terminal but
4022 % not to delimited arguments.
4023 % \begin{macrocode}
4024 \cs_new_protected:Npn \@@_grab_b:w
4025 { \@@_grab_b_aux:NNw \cs_set_protected_nopar:Npn \tl_trim_spaces:n }
4026 \cs_new_protected:Npn \@@_grab_b_long:w
4027 { \@@_grab_b_aux:NNw \cs_set_protected:Npn \tl_trim_spaces:n }
4028 \cs_new_protected:Npn \@@_grab_b_obey_spaces:w
4029 { \@@_grab_b_aux:NNw \cs_set_protected_nopar:Npn \exp_not:n }
4030 \cs_new_protected:Npn \@@_grab_b_long_obey_spaces:w
4031 { \@@_grab_b_aux:NNw \cs_set_protected:Npn \exp_not:n }
4032 \cs_new_protected:Npn \@@_grab_b_aux:NNw #1#2#3 \@@_run_code:
4033 {
4034 \@@_grab_D_aux:NNnNN \begin \end {#3} #1 \use_ii:nn
4035 \tl_put_left:Nn \l_@@_signature_tl { \@@_grab_b_end:Nw #2 }
4036 \tl_set_eq:NN \l_@@_saved_args_tl \l_@@_args_tl
4037 \tl_clear:N \l_@@_args_tl
4038 \exp_args:Nc \l_@@_fn_tl { begin ~ }
4039 }
4040 \cs_new_protected:Npn \@@_grab_b_end:Nw #1#2 \@@_run_code:
4041 {
4042 \tl_set:Ne \l_@@_args_tl
4043 {
4044 \exp_not:V \l_@@_saved_args_tl
4045 { \exp_after:wN #1 \l_@@_args_tl }
4046 }
4047 #2
4048 \@@_run_code:
4049 \end
4050 }
4051 % \end{macrocode}
4052 % \end{macro}
4053 %
4054 % \begin{macro}{\@@_grab_c:w}
4055 % \changes{v1.3a}{2025-01-23}{New \texttt{c}-type grabbing function}
4056 % \begin{macro}{\@@_grab_c_obey_spaces:w}
4057 % \begin{macro}{\@@_grab_c_start:n}
4058 % \begin{macro}{\@@_grab_c_first:w}
4059 % \begin{macro}{\@@_grab_c_loop:w}
4060 % Collecting an environment body verbatim shares some ideas with the
4061 % \texttt{v}-type grabber, and others with the standard \texttt{filecontents}
4062 % environment. The start is to set the end-of-line to a predictable value
4063 % and to deactivate the specials.
4064 % \begin{macrocode}
4065 \cs_new_protected:Npn \@@_grab_c:w #1 \@@_run_code:
4066 {
4067 \bool_set_false:N \l_@@_obey_spaces_bool
4068 \@@_grab_c_start:n {#1}
4069 }
4070 \cs_new_protected:Npn \@@_grab_c_obey_spaces:w #1 \@@_run_code:
4071 {
4072 \bool_set_true:N \l_@@_obey_spaces_bool
4073 \@@_grab_c_start:n {#1}
4074 }
4075 \cs_new_protected:Npn \@@_grab_c_start:n #1
4076 {
4077 \tl_set:Nn \l_@@_signature_tl {#1}
4078 \group_begin:
4079 \tl_clear:N \l_@@_v_arg_tl
4080 \tex_escapechar:D = 92 \scan_stop:
4081 \tex_endlinechar:D = `\^^M \scan_stop:
4082 \cs_set_eq:NN \do \char_set_catcode_other:N
4083 \dospecials
4084 \char_set_catcode_other:n { `\^^M }
4085 \@@_grab_c_first:w
4086 }
4087 % \end{macrocode}
4088 % \changes{v1.3c}{2025-04-12}{Correct behavior on dropping tokens}
4089 % Notice here and below that we cannot use |\token_to_str:N \end| as that
4090 % would have the wrong category codes for the letters.
4091 % \begin{macrocode}
4092 \group_begin:
4093 \char_set_catcode_other:N \^^M %
4094 \cs_new_protected:Npn \@@_grab_c_first:w #1 ^^M %
4095 { %
4096 \tl_if_blank:nTF {#1} %
4097 { %
4098 \@@_grab_c_loop:w #1 ^^M %
4099 } %
4100 { %
4101 \msg_warning:nnee { cmd } { chars-dropped-first-line } %
4102 { \exp_not:n {#1} } %
4103 { \exp_not:V \@currenvir } %
4104 \@@_grab_c_loop:w ^^M %
4105 } %
4106 } %
4107 \cs_new_protected:Npe \@@_grab_c_loop:w #1 ^^M %
4108 { %
4109 \exp_not:N \@@_grab_c_auxi:w #1 %
4110 \c_backslash_str end %
4111 \scan_stop: %
4112 } %
4113 \group_end:
4114 % \end{macrocode}
4115 % \end{macro}
4116 % \end{macro}
4117 % \end{macro}
4118 % \end{macro}
4119 % \end{macro}
4120 % \begin{macro}{\@@_grab_c_auxi:w}
4121 % \begin{macro}{\@@_grab_c_auxii:w}
4122 % \begin{macro}{\@@_grab_c_auxiii:N}
4123 % \begin{macro}{\@@_grab_c_auxiv:}
4124 % \begin{macro}{\@@_grab_c_auxv:}
4125 % \begin{macro}{\@@_grab_c_auxvi:N}
4126 % \begin{macro}{\@@_grab_c_auxvii:}
4127 % \begin{macro}{\@@_grab_c_auxviii:}
4128 % We need to see if the current line contains |\end| followed by the name
4129 % of the current environment. To do that and allow for spaces, we have to
4130 % work stepwise. First, establish if there is an |\end| at all: remember that
4131 % here we are dealing with \enquote{other} tokens. Whether these is an
4132 % |\end| or not, the tokens \emph{before} it form part of the line.
4133 % \begin{macrocode}
4134 \use:e
4135 {
4136 \cs_new_protected:Npn \exp_not:N \@@_grab_c_auxi:w
4137 #1 \c_backslash_str end #2 \scan_stop:
4138 }
4139 {
4140 \tl_put_right:Nn \l_@@_v_arg_tl {#1}
4141 \tl_if_empty:nTF {#2}
4142 {
4143 \tl_put_right:Nn \l_@@_v_arg_tl { \obeyedline }
4144 \@@_grab_c_loop:w
4145 }
4146 { \@@_grab_c_auxii:w #2 \scan_stop: }
4147 }
4148 % \end{macrocode}
4149 % There is an |\end|, so we now remove the trailing marker we needed
4150 % to do the test. This is stripped off, then we need to examine the
4151 % rest of the line one token at a time: see |verbatim.dtx| for the
4152 % inspiration. Notice that we use |^^M| here as the end marker: this allows
4153 % looping to look for multiple |\end| entries in the line.
4154 % \begin{macrocode}
4155 \group_begin:
4156 \char_set_catcode_other:N \^^M %
4157 \use:e %
4158 { %
4159 \cs_new_protected:Npe \exp_not:N \@@_grab_c_auxii:w %
4160 #1 \c_backslash_str end \scan_stop: %
4161 } %
4162 { %
4163 \tl_set:Nn \exp_not:N \l_@@_tmpa_tl
4164 { \c_backslash_str end } %
4165 \exp_not:N \@@_grab_c_auxiii:N #1 ^^M %
4166 } %
4167 % \end{macrocode}
4168 % \changes{v1.3d}{2025-04-21}{Correct a logic flaw}
4169 % Within the line, we need to collect up the tokens: if we do not find
4170 % the end-of-environment argument, we will need those to reinsert. There
4171 % are three special cases here: |^^M| (end of line: tidy up and back to
4172 % the main loop), \verb*| | (possibly skip over) and |{| (start the inner
4173 % loop). Anything else means we move back to examine the rest of the line
4174 % for any more |\end| entries.
4175 % \begin{macrocode}
4176 \cs_new_protected:Npn \@@_grab_c_auxiii:N #1 %
4177 { %
4178 \token_case_charcode:NnF #1 %
4179 { %
4180 ^^M %
4181 { \@@_grab_c_auxiv: } %
4182 \c_space_token %
4183 { %
4184 \tl_put_right:Nn \l_@@_tmpa_tl {#1} %
4185 \@@_grab_c_auxiii:N %
4186 } %
4187 \c_group_begin_token %
4188 { %
4189 \tl_set:Nn \l_@@_tmpb_tl {#1} %
4190 \@@_grab_c_auxvi:N %
4191 } %
4192 } %
4193 { %
4194 \tl_put_right:Nn \l_@@_tmpa_tl {#1} %
4195 \@@_grab_c_auxv: %
4196 } %
4197 } %
4198 \group_end:
4199 \cs_new_protected:Npn \@@_grab_c_auxiv:
4200 {
4201 \tl_put_right:Ne \l_@@_v_arg_tl
4202 {
4203 \exp_not:V \l_@@_tmpa_tl
4204 \exp_not:N \obeyedline
4205 }
4206 \@@_grab_c_loop:w
4207 }
4208 \cs_new_protected:Npn \@@_grab_c_auxv:
4209 {
4210 \tl_put_right:NV \l_@@_v_arg_tl \l_@@_tmpa_tl
4211 \@@_grab_c_loop:w
4212 }
4213 % \end{macrocode}
4214 % In the inner loop, we again have only a few special cases. First, we could
4215 % again have |^^M|, in which case we tidy up using a common auxiliary. Second,
4216 % we check for the escape char: this cannot happen inside the end of an
4217 % environment and means we loop, re-inserting the token. Finally, we have
4218 % |}|, where we need to move on to check what has been collected. Otherwise,
4219 % collect up and loop. Notice here that the inner loop needs to
4220 % collect tokens separately: this leaves any spaces after |\end| in
4221 % \cs{l_@@_tmpa_tl}, so we can test \cs{l_@@_tmpb_tl} directly.
4222 % \begin{macrocode}
4223 \group_begin:
4224 \char_set_catcode_other:N \^^M %
4225 \cs_new_protected:Npe \@@_grab_c_auxvi:N #1 %
4226 { %
4227 \exp_not:N \token_case_charcode:NnF #1 %
4228 { %
4229 ^^M %
4230 {
4231 \exp_not:N \@@_grab_c_auxvii: %
4232 \exp_not:N \@@_grab_c_auxiv: %
4233 }%
4234 \c_backslash_str %
4235 { %
4236 \exp_not:N \@@_grab_c_auxvii: %
4237 \exp_not:N \@@_grab_c_auxv: #1
4238 } %
4239 \c_group_end_token %
4240 { %
4241 \tl_put_right:Nn \exp_not:N \l_@@_tmpb_tl {#1} %
4242 \exp_not:N \@@_grab_c_auxviii: %
4243 } %
4244 } %
4245 { %
4246 \tl_put_right:Nn \exp_not:N \l_@@_tmpb_tl {#1} %
4247 \exp_not:N \@@_grab_c_auxvi:N %
4248 } %
4249 } %
4250 \group_end: %
4251 \cs_new_protected:Npn \@@_grab_c_auxvii:
4252 { \tl_put_right:NV \l_@@_tmpa_tl \l_@@_tmpb_tl }
4253 \cs_new_protected:Npn \@@_grab_c_auxviii:
4254 {
4255 \str_if_eq:eeTF { \exp_not:V \l_@@_tmpb_tl } { { \@currenvir } }
4256 { \@@_grab_c_end:w }
4257 {
4258 \@@_grab_c_auxvii:
4259 \@@_grab_c_auxv:
4260 }
4261 }
4262 % \end{macrocode}
4263 % \end{macro}
4264 % \end{macro}
4265 % \end{macro}
4266 % \end{macro}
4267 % \end{macro}
4268 % \end{macro}
4269 % \end{macro}
4270 % \end{macro}
4271 % \begin{macro}{\@@_grab_c_end:w}
4272 % \begin{macro}{\@@_grab_c_end:n}
4273 % \begin{macro}{\@@_grab_c_end_auxi:w}
4274 % \begin{macro}{\@@_grab_c_end_auxii:w}
4275 % \begin{macro}{\@@_grab_c_end_auxiii:w}
4276 % To end the collection, we clean up the last line: once again we need to
4277 % find |^^M|. Once that is done, we can warn if there is anything left
4278 % behind.
4279 % \begin{macrocode}
4280 \group_begin:
4281 \char_set_catcode_other:N \^^M %
4282 \cs_new_protected:Npn \@@_grab_c_end:w #1 ^^M %
4283 { %
4284 \tl_if_blank:nF {#1} %
4285 { %
4286 \msg_warning:nnee { cmd } { chars-dropped-last-line } %
4287 { \exp_not:n {#1} } %
4288 { \exp_not:V \@currenvir } %
4289 } %
4290 \exp_args:NNNo \group_end: %
4291 \tl_set:Nn \l_@@_v_arg_tl { \l_@@_v_arg_tl } %
4292 \@@_add_arg:e %
4293 { %
4294 \bool_if:NTF \l_@@_obey_spaces_bool %
4295 { \exp_not:V } %
4296 { \exp_args:NV \@@_grab_c_end:n } %
4297 \l_@@_v_arg_tl %
4298 } %
4299 \exp_args:NV \end \@currenvir %
4300 } %
4301 \group_end: %
4302 % \end{macrocode}
4303 % Look for line markers at each end and tidy up if required.
4304 % \begin{macrocode}
4305 \cs_new:Npn \@@_grab_c_end:n #1
4306 {
4307 \@@_grab_c_end_auxi:w \q_nil #1 \q_nil
4308 \obeyedline \obeyedline \q_nil \q_stop
4309 }
4310 \cs_new:Npn \@@_grab_c_end_auxi:w #1 \q_nil \obeyedline
4311 { \@@_grab_c_end_auxii:w #1 \q_nil }
4312 \cs_new:Npn \@@_grab_c_end_auxii:w \q_nil #1 \obeyedline \q_nil
4313 { \@@_grab_c_end_auxiii:w #1 \q_nil }
4314 \cs_new:Npn \@@_grab_c_end_auxiii:w #1 \q_nil #2 \q_stop
4315 { \exp_not:n {#1} }
4316 % \end{macrocode}
4317 % \end{macro}
4318 % \end{macro}
4319 % \end{macro}
4320 % \end{macro}
4321 % \end{macro}
4322 %
4323 % \begin{macro}
4324 % {
4325 % \@@_grab_D:w ,
4326 % \@@_grab_D_no_strip:w ,
4327 % \@@_grab_D_obey_spaces:w ,
4328 % \@@_grab_D_obey_spaces_no_strip:w ,
4329 % \@@_grab_D_long:w ,
4330 % \@@_grab_D_long_no_strip:w ,
4331 % \@@_grab_D_long_obey_spaces:w ,
4332 % \@@_grab_D_long_obey_spaces_no_strip:w
4333 % }
4334 % \changes{v1.1a}{2022/08/10}{Add support for skipping brace stripping}
4335 % \changes{v1.3a}{2025-01-23}{Auto-generate D-grabbers}
4336 % The generic delimited argument grabber. The auxiliary function does
4337 % a peek test before calling \cs{@@_grab_D_call:Nw}, so that the
4338 % optional nature of the argument works as expected.
4339 % \begin{macrocode}
4340 \tl_map_inline:nn { { } { _long } }
4341 {
4342 \tl_map_inline:nn { { } { _obey_spaces } }
4343 {
4344 \tl_map_inline:nn { { } { _no_strip } }
4345 {
4346 \tl_map_inline:nn { { } { _verb_safe } }
4347 {
4348 \cs_new_protected:cpe { @@_grab_D #1 ##1 ####1 ########1 :w }
4349 ################1################2################3
4350 \@@_run_code:
4351 {
4352 \exp_not:N \@@_grab_D_aux:NNnNNNN
4353 ################1 ################2 {################3}
4354 \str_if_eq:nnTF {#1} { _long }
4355 \cs_set_protected:Npn
4356 \cs_set_protected_nopar:Npn
4357 \str_if_eq:nnTF {##1} { _obey_spaces }
4358 { \exp_not:N \@@_peek_meaning_remove:NTF }
4359 { \exp_not:N \@@_peek_nonspace_remove:NTF }
4360 \str_if_eq:nnTF {####1} { _no_strip }
4361 { \exp_not:N \use_none:n }
4362 { \exp_not:N \use_ii:nn }
4363 \str_if_eq:nnTF {########1} { _verb_safe }
4364 { \exp_not:N \use:n }
4365 { \exp_not:N \use_none:n }
4366 }
4367 }
4368 }
4369 }
4370 }
4371 % \end{macrocode}
4372 % \begin{macro}{\@@_grab_D_aux:NNnNNNN}
4373 % \changes{v1.3a}{2025-01-23}{Extend to support \texttt{c}-type grabbing}
4374 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
4375 % \begin{macro}{\@@_grab_D_verb_safe:NN}
4376 % \changes{v1.3a}{2025-01-23}{New macro}
4377 % \begin{macro}{\@@_grab_D_aux:NNnNN}
4378 % This is a bit complicated. The idea is that, in order to check for
4379 % nested optional argument tokens (\texttt{[[...]]} and so on) the
4380 % argument needs to be grabbed without removing any braces at all. If
4381 % this is not done, then cases like |[{[}]| fail. So after testing for
4382 % an optional argument, it is collected piece-wise. Inserting a quark
4383 % prevents loss of braces, and there is then a test to see if there are
4384 % nested delimiters to handle.
4385 % \begin{macrocode}
4386 \group_begin:
4387 \char_set_catcode_other:N \^^M
4388 \cs_new_protected:Npn \@@_grab_D_aux:NNnNNNN #1#2#3#4#5#6#7
4389 {
4390 \@@_grab_D_aux:NNnNN #1#2 {#3} #4 #6
4391 #7
4392 {
4393 \group_begin:
4394 \@@_grab_D_verb_safe:NN #1#5
4395 }
4396 #5 #1
4397 {
4398 #7 { \group_end: }
4399 \@@_grab_D_call:Nw #1
4400 }
4401 {
4402 #7 { \group_end: }
4403 \@@_add_arg:n { \NoValue }
4404 }
4405 }
4406 \group_end:
4407 % \end{macrocode}
4408 % The only awkwardness here is the need to preserve the catcode of the search
4409 % token: this is done low-level for performance reasons. Only values that
4410 % are realistic are included. As this does not cover spaces when they are
4411 % skipped that has to be covered separately.
4412 % \begin{macrocode}
4413 \cs_new_protected:Npn \@@_grab_D_verb_safe:NN #1#2
4414 {
4415 \cs_set_eq:NN \do \char_set_catcode_other:N
4416 \dospecials
4417 \char_set_catcode_other:N \^^M
4418 \token_if_eq_meaning:NNT #2 \@@_peek_nonspace_remove:NTF
4419 { \char_set_catcode_space:n { `\ } }
4420 \use:c
4421 {
4422 char_set_catcode_
4423 \if_catcode:w \c_math_toggle_token #1 math_toggle \else:
4424 \if_catcode:w ^ #1 math_superscript \else:
4425 \if_catcode:w \c_math_subscript_token #1 math_subscript \else:
4426 \if_catcode:w A #1 letter \else:
4427 other \fi: \fi: \fi: \fi:
4428 :N
4429 }
4430 #1
4431 }
4432 % \end{macrocode}
4433 % Inside the \enquote{standard} grabber, there is a test to see if the
4434 % grabbed argument is entirely enclosed by braces. There are a couple of
4435 % extra factors to allow for: the argument might be entirely empty, and
4436 % spaces at the start and end of the input must be retained around a brace
4437 % group. Also notice that a \emph{blank} argument might still contain
4438 % spaces. To allow for suppression of brace stripping, the business end
4439 % is passed here as |#5|.
4440 % \begin{macrocode}
4441 \cs_new_protected:Npn \@@_grab_D_aux:NNnNN #1#2#3#4#5
4442 {
4443 \tl_set:Nn \l_@@_signature_tl {#3}
4444 \exp_after:wN #4 \l_@@_fn_tl ##1 #2
4445 {
4446 \tl_if_in:nnTF {##1} {#1}
4447 { \@@_grab_D_nested:NNnN #1 #2 {##1} #4 }
4448 {
4449 \tl_if_blank:oTF { \use_none:n ##1 }
4450 { \@@_add_arg:o { \use_none:n ##1 } }
4451 {
4452 \str_if_eq:eeTF
4453 { \exp_not:o { \use_none:n ##1 } }
4454 { { \exp_not:o { \use_ii:nnn ##1 \q_nil } } }
4455 { \@@_add_arg:o { #5 ##1 } }
4456 { \@@_add_arg:o { \use_none:n ##1 } }
4457 }
4458 }
4459 }
4460 }
4461 % \end{macrocode}
4462 % \end{macro}
4463 % \end{macro}
4464 % \end{macro}
4465 % \end{macro}
4466 %
4467 % \begin{macro}{\@@_grab_D_nested:NNnN}
4468 % \begin{macro}{\@@_grab_D_nested:w}
4469 % \begin{macro}{\l_@@_nesting_a_tl}
4470 % \begin{macro}{\l_@@_nesting_b_tl}
4471 % \begin{macro}{\q_@@}
4472 % Catching nested optional arguments means more work. The aim here is
4473 % to collect up each pair of optional tokens without \TeX{} helping out,
4474 % and without counting anything. The code above will already have
4475 % removed the leading opening token and a closing token, but the
4476 % wrong one. The aim is then to work through the material grabbed
4477 % so far and divide it up on each opening token, grabbing a closing
4478 % token to match (thus working in pairs). Once there are no opening
4479 % tokens, then there is a second check to see if there are any
4480 % opening tokens in the second part of the argument (for things
4481 % like |[][]|). Once everything has been found, the entire collected
4482 % material is added to the output as a single argument. The only tricky part
4483 % here is ensuring that any grabbing function that might run away is named
4484 % after the function currently being parsed and not after \pkg{xparse}. That
4485 % leads to some rather complex nesting! There is also a need to prevent the
4486 % loss of any braces, hence the insertion and removal of quarks along the
4487 % way.
4488 % \begin{macrocode}
4489 \tl_new:N \l_@@_nesting_a_tl
4490 \tl_new:N \l_@@_nesting_b_tl
4491 \quark_new:N \q_@@
4492 \cs_new_protected:Npn \@@_grab_D_nested:NNnN #1#2#3#4
4493 {
4494 \tl_clear:N \l_@@_nesting_a_tl
4495 \tl_clear:N \l_@@_nesting_b_tl
4496 \exp_after:wN #4 \l_@@_fn_tl ##1 #1 ##2 \q_@@ ##3 #2
4497 {
4498 \tl_put_right:No \l_@@_nesting_a_tl { \use_none:n ##1 #1 }
4499 \tl_put_right:No \l_@@_nesting_b_tl { \use_i:nn #2 ##3 }
4500 \tl_if_in:nnTF {##2} {#1}
4501 {
4502 \l_@@_fn_tl
4503 \q_nil ##2 \q_@@ \ERROR
4504 }
4505 {
4506 \tl_put_right:Ne \l_@@_nesting_a_tl
4507 { \@@_grab_D_nested:w \q_nil ##2 \q_stop }
4508 \tl_if_in:NnTF \l_@@_nesting_b_tl {#1}
4509 {
4510 \tl_set_eq:NN \l_@@_tmpa_tl \l_@@_nesting_b_tl
4511 \tl_clear:N \l_@@_nesting_b_tl
4512 \exp_after:wN \l_@@_fn_tl \exp_after:wN
4513 \q_nil \l_@@_tmpa_tl \q_nil \q_@@ \ERROR
4514 }
4515 {
4516 \tl_put_right:No \l_@@_nesting_a_tl
4517 \l_@@_nesting_b_tl
4518 \@@_add_arg:V \l_@@_nesting_a_tl
4519 }
4520 }
4521 }
4522 \l_@@_fn_tl #3 \q_nil \q_@@ \ERROR
4523 }
4524 \cs_new:Npn \@@_grab_D_nested:w #1 \q_nil \q_stop
4525 { \exp_not:o { \use_none:n #1 } }
4526 % \end{macrocode}
4527 % \end{macro}
4528 % \end{macro}
4529 % \end{macro}
4530 % \end{macro}
4531 % \end{macro}
4532 % \begin{macro}{\@@_grab_D_call:Nw}
4533 % For \texttt{D} and \texttt{R}-type arguments, to avoid losing any
4534 % braces, a token needs to be inserted before the argument to be grabbed.
4535 % If the argument runs away because the closing token is missing then this
4536 % inserted token shows up in the terminal. Ideally, |#1| would therefore be
4537 % used directly, but that is no good as it will mess up the rest of the
4538 % grabber. Instead, a copy of |#1| with an altered category code is used,
4539 % as this will look right in the terminal but will not mess up the grabber.
4540 % The only issue then is that the category code of |#1| is unknown. So there
4541 % is a quick test to ensure that the inserted token can never be matched by
4542 % the grabber. (This assumes that the open and close delimiters are not the
4543 % same character with different category codes, but that really should not
4544 % happen in any sensible document-level syntax.)
4545 % An exception is when |#1| is a control sequence token, in which case the
4546 % character-token treatment is no good because if hit with \cs{token_to_str:N}
4547 % it would add sputios tokens to the argument. In this case a different
4548 % branch is taken. The token inserted is then the same \meta{csname} as |#1|,
4549 % but with a space appended, so that the grabber don't see it as another
4550 % of the same delimiter.
4551 % \begin{macrocode}
4552 \cs_new_protected_nopar:Npn \@@_grab_D_call:Nw #1
4553 {
4554 \token_if_eq_catcode:NNTF + #1
4555 {
4556 \exp_after:wN \exp_after:wN \exp_after:wN
4557 \l_@@_fn_tl \char_generate:nn { `#1 } { 11 }
4558 }
4559 {
4560 \@@_token_if_cs:NTF #1
4561 {
4562 \exp_after:wN \l_@@_fn_tl
4563 \cs:w \cs_to_str:N #1 ~ \cs_end:
4564 }
4565 {
4566 \exp_after:wN \l_@@_fn_tl
4567 \token_to_str:N #1
4568 }
4569 }
4570 }
4571 % \end{macrocode}
4572 % \end{macro}
4573 %
4574 % \begin{macro}
4575 % {
4576 % \@@_grab_E:w, \@@_grab_E_long:w,
4577 % \@@_grab_E_obey_spaces:w, \@@_grab_E_long_obey_spaces:w
4578 % }
4579 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
4580 % \begin{macro}{\@@_grab_E:nnNN}
4581 % \begin{macro}{\@@_grab_E_loop:NnN}
4582 % \begin{macro}{\@@_grab_E_finalise:}
4583 % Everything here needs to point to a loop.
4584 % \begin{macrocode}
4585 \cs_new_protected:Npn \@@_grab_E:w #1#2 \@@_run_code:
4586 {
4587 \@@_grab_E:nnNN {#1} {#2}
4588 \cs_set_protected_nopar:Npn
4589 \@@_peek_nonspace_remove:NTF
4590 }
4591 \cs_new_protected:Npn \@@_grab_E_long:w #1#2 \@@_run_code:
4592 {
4593 \@@_grab_E:nnNN {#1} {#2}
4594 \cs_set_protected:Npn
4595 \@@_peek_nonspace_remove:NTF
4596 }
4597 \cs_new_protected:Npn \@@_grab_E_obey_spaces:w #1#2 \@@_run_code:
4598 {
4599 \@@_grab_E:nnNN {#1} {#2}
4600 \cs_set_protected_nopar:Npn
4601 \@@_peek_meaning_remove:NTF
4602 }
4603 \cs_new_protected:Npn \@@_grab_E_long_obey_spaces:w #1#2 \@@_run_code:
4604 {
4605 \@@_grab_E:nnNN {#1} {#2}
4606 \cs_set_protected:Npn
4607 \@@_peek_meaning_remove:NTF
4608 }
4609 % \end{macrocode}
4610 % A loop is needed here to allow a random ordering of keys. These are
4611 % searched for one at a time, with any not found needing to be tracked:
4612 % they can appear later. The grabbed values are held in a property list
4613 % which is then turned into an ordered list to be passed back to the user.
4614 % \begin{macrocode}
4615 \cs_new_protected:Npn \@@_grab_E:nnNN #1#2#3#4
4616 {
4617 \exp_after:wN #3 \l_@@_fn_tl ##1##2##3
4618 {
4619 \prop_put:Nnn \l_@@_tmp_prop {##1} {##3}
4620 \@@_grab_E_loop:NnN #4 { } ##2 \q_recursion_stop
4621 }
4622 \prop_clear:N \l_@@_tmp_prop
4623 \tl_set:Nn \l_@@_signature_tl {#2}
4624 \cs_set_protected:Npn \@@_grab_E_finalise:
4625 {
4626 \tl_map_inline:nn {#1}
4627 {
4628 \prop_get:NnNF \l_@@_tmp_prop {####1} \l_@@_tmpb_tl
4629 { \tl_set:Nn \l_@@_tmpb_tl { \NoValue } }
4630 \tl_put_right:Ne \l_@@_args_tl
4631 { { \exp_not:V \l_@@_tmpb_tl } }
4632 }
4633 \l_@@_signature_tl \@@_run_code:
4634 }
4635 \@@_grab_E_loop:NnN #4 { } #1 \q_recursion_tail \q_recursion_stop
4636 }
4637 \cs_new_protected:Npn \@@_grab_E_loop:NnN #1#2#3#4 \q_recursion_stop
4638 {
4639 \cs_if_eq:NNTF #3 \q_recursion_tail
4640 { \@@_grab_E_finalise: }
4641 {
4642 #1 #3
4643 { \l_@@_fn_tl #3 {#2#4} }
4644 { \@@_grab_E_loop:NnN #1 {#2#3} #4 \q_recursion_stop }
4645 }
4646 }
4647 \cs_new_protected:Npn \@@_grab_E_finalise: { }
4648 % \end{macrocode}
4649 % \end{macro}
4650 % \end{macro}
4651 % \end{macro}
4652 % \end{macro}
4653 %
4654 % \begin{macro}{\@@_grab_m:w}
4655 % \begin{macro}{\@@_grab_m_long:w}
4656 % Collecting a single mandatory argument is quite easy.
4657 % \begin{macrocode}
4658 \cs_new_protected:Npn \@@_grab_m:w #1 \@@_run_code:
4659 {
4660 \tl_set:Nn \l_@@_signature_tl {#1}
4661 \exp_after:wN \cs_set_protected_nopar:Npn \l_@@_fn_tl ##1
4662 { \@@_add_arg:n {##1} }
4663 \l_@@_fn_tl
4664 }
4665 \cs_new_protected:Npn \@@_grab_m_long:w #1 \@@_run_code:
4666 {
4667 \tl_set:Nn \l_@@_signature_tl {#1}
4668 \exp_after:wN \cs_set_protected:Npn \l_@@_fn_tl ##1
4669 { \@@_add_arg:n {##1} }
4670 \l_@@_fn_tl
4671 }
4672 % \end{macrocode}
4673 % \end{macro}
4674 % \end{macro}
4675 %
4676 % \begin{macro}{\@@_grab_m_1:w}
4677 % \begin{macro}{\@@_grab_m_2:w}
4678 % \begin{macro}{\@@_grab_m_3:w}
4679 % \begin{macro}{\@@_grab_m_4:w}
4680 % \begin{macro}{\@@_grab_m_5:w}
4681 % \begin{macro}{\@@_grab_m_6:w}
4682 % \begin{macro}{\@@_grab_m_7:w}
4683 % \begin{macro}{\@@_grab_m_8:w}
4684 % \begin{macro}{\@@_grab_m_9:w}
4685 % \begin{macro}{\@@_grab_m_aux:Nnnnnnnnn}
4686 % Grabbing 1--8 mandatory arguments is done by giving 8--1 known
4687 % arguments to a 9-argument function that stores them in
4688 % \cs{l_@@_args_tl}. For simplicity, grabbing 9 mandatory arguments
4689 % is done by grabbing 5 then 4 arguments.
4690 % \begin{macrocode}
4691 \cs_new_protected_nopar:Npn \@@_grab_m_aux:Nnnnnnnnn #1#2#3#4#5#6#7#8#9
4692 {
4693 \tl_put_right:No \l_@@_args_tl
4694 { #1 {#2} {#3} {#4} {#5} {#6} {#7} {#8} {#9} }
4695 \l_@@_signature_tl \@@_run_code:
4696 }
4697 \cs_new_protected:cpn { @@_grab_m_1:w } #1 \@@_run_code:
4698 {
4699 \tl_set:Nn \l_@@_signature_tl {#1}
4700 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4701 \l_@@_fn_tl \use_none:nnnnnnn { } { } { } { } { } { } { }
4702 }
4703 \cs_new_protected:cpn { @@_grab_m_2:w } #1 \@@_run_code:
4704 {
4705 \tl_set:Nn \l_@@_signature_tl {#1}
4706 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4707 \l_@@_fn_tl \use_none:nnnnnn { } { } { } { } { } { }
4708 }
4709 \cs_new_protected:cpn { @@_grab_m_3:w } #1 \@@_run_code:
4710 {
4711 \tl_set:Nn \l_@@_signature_tl {#1}
4712 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4713 \l_@@_fn_tl \use_none:nnnnn { } { } { } { } { }
4714 }
4715 \cs_new_protected:cpn { @@_grab_m_4:w } #1 \@@_run_code:
4716 {
4717 \tl_set:Nn \l_@@_signature_tl {#1}
4718 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4719 \l_@@_fn_tl \use_none:nnnn { } { } { } { }
4720 }
4721 \cs_new_protected:cpn { @@_grab_m_5:w } #1 \@@_run_code:
4722 {
4723 \tl_set:Nn \l_@@_signature_tl {#1}
4724 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4725 \l_@@_fn_tl \use_none:nnn { } { } { }
4726 }
4727 \cs_new_protected:cpn { @@_grab_m_6:w } #1 \@@_run_code:
4728 {
4729 \tl_set:Nn \l_@@_signature_tl {#1}
4730 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4731 \l_@@_fn_tl \use_none:nn { } { }
4732 }
4733 \cs_new_protected:cpn { @@_grab_m_7:w } #1 \@@_run_code:
4734 {
4735 \tl_set:Nn \l_@@_signature_tl {#1}
4736 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4737 \l_@@_fn_tl \use_none:n { }
4738 }
4739 \cs_new_protected:cpn { @@_grab_m_8:w } #1 \@@_run_code:
4740 {
4741 \tl_set:Nn \l_@@_signature_tl {#1}
4742 \exp_after:wN \cs_set_eq:NN \l_@@_fn_tl \@@_grab_m_aux:Nnnnnnnnn
4743 \l_@@_fn_tl \prg_do_nothing:
4744 }
4745 \cs_new_protected:cpe { @@_grab_m_9:w }
4746 {
4747 \exp_not:c { @@_grab_m_5:w }
4748 \exp_not:c { @@_grab_m_4:w }
4749 }
4750 % \end{macrocode}
4751 % \end{macro}
4752 % \end{macro}
4753 % \end{macro}
4754 % \end{macro}
4755 % \end{macro}
4756 % \end{macro}
4757 % \end{macro}
4758 % \end{macro}
4759 % \end{macro}
4760 % \end{macro}
4761 %
4762 % \begin{macro}{\@@_grab_R:w, \@@_grab_R_long:w}
4763 % \begin{macro}{\@@_grab_R_aux:NNnN}
4764 % \changes{v1.1a}{2022/08/10}{Track changes in \texttt{D}-type implementation}
4765 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
4766 % The grabber for \texttt{R}-type arguments is basically the same as
4767 % that for \texttt{D}-type ones, but always skips spaces (as it is mandatory)
4768 % and has a hard-coded error message.
4769 % \begin{macrocode}
4770 \cs_new_protected:Npn \@@_grab_R:w #1#2#3 \@@_run_code:
4771 { \@@_grab_R_aux:NNnN #1 #2 {#3} \cs_set_protected_nopar:Npn }
4772 \cs_new_protected:Npn \@@_grab_R_long:w #1#2#3 \@@_run_code:
4773 { \@@_grab_R_aux:NNnN #1 #2 {#3} \cs_set_protected:Npn }
4774 \cs_new_protected:Npn \@@_grab_R_aux:NNnN #1#2#3#4
4775 {
4776 \@@_grab_D_aux:NNnNN #1 #2 {#3} #4 \use_ii:nn
4777 \@@_peek_nonspace_remove:NTF #1
4778 { \@@_grab_D_call:Nw #1 }
4779 {
4780 \msg_error:nnee { cmd } { missing-required }
4781 { \@@_environment_or_command: }
4782 { \token_to_str:N #1 }
4783 \@@_add_arg:n { \NoValue }
4784 }
4785 }
4786 % \end{macrocode}
4787 % \end{macro}
4788 % \end{macro}
4789 %
4790 % \begin{macro}{\@@_grab_t:w}
4791 % \begin{macro}{\@@_grab_t_obey_spaces:w}
4792 % \begin{macro}{\@@_grab_t_aux:NNw}
4793 % Dealing with a token is quite easy. Check the match, remove the
4794 % token if needed and add a flag to the output.
4795 % \begin{macrocode}
4796 \cs_new_protected:Npn \@@_grab_t:w
4797 { \@@_grab_t_aux:NNw \@@_peek_nonspace_remove:NTF }
4798 \cs_new_protected:Npn \@@_grab_t_obey_spaces:w
4799 { \@@_grab_t_aux:NNw \@@_peek_meaning_remove:NTF }
4800 \cs_new_protected:Npn \@@_grab_t_aux:NNw #1#2#3 \@@_run_code:
4801 {
4802 \tl_set:Nn \l_@@_signature_tl {#3}
4803 \exp_after:wN \cs_set_protected:Npn \l_@@_fn_tl
4804 {
4805 #1 #2
4806 { \@@_add_arg:n { \BooleanTrue } }
4807 { \@@_add_arg:n { \BooleanFalse } }
4808 }
4809 \l_@@_fn_tl
4810 }
4811 % \end{macrocode}
4812 % \end{macro}
4813 % \end{macro}
4814 % \end{macro}
4815 %
4816 % \begin{variable}{\l_@@_v_arg_tl}
4817 % \begin{macrocode}
4818 \tl_new:N \l_@@_v_arg_tl
4819 % \end{macrocode}
4820 % \end{variable}
4821 %
4822 % \begin{macro}{\@@_grab_v:w}
4823 % \begin{macro}{\@@_grab_v_long:w}
4824 % \begin{macro}{\@@_grab_v_aux:w}
4825 % \begin{macro}{\@@_grab_v_group_end:}
4826 % Firstly, it is necessary to change \cs{tex_endlinechar:D} so that
4827 % newlines in different catcode regimes (e.g., \cs{ExplSyntaxOn}) are
4828 % not misinterpreted as spaces.
4829 % \changes{v1.1d}{2023/04/13}{Set \cs{tex_endlinechar:D} earlier (gh/876).}
4830 % The opening delimiter is the first non-space token, and is never
4831 % read verbatim. This is required by consistency with the case where
4832 % the preceding argument was optional and absent: then \TeX{} has
4833 % already read and tokenized that token when looking for the optional
4834 % argument. The first thing is thus to check is that this delimiter
4835 % is a character, and to distinguish the case of a left brace (in that
4836 % case, \cs{group_align_safe_end:} is needed to compensate for the
4837 % begin-group character that was just seen). Then set verbatim
4838 % catcodes with \cs{@@_grab_v_aux_catcodes:}.
4839 %
4840 % The group keep catcode changes local, and
4841 % \cs{group_align_safe_begin/end:} allow to use a character
4842 % with category code~$4$ (normally |&|) as the delimiter (all commands
4843 % do \cs{group_align_safe_begin/end:}, so there's no need to do that
4844 % again here).
4845 % It is ended by \cs{@@_grab_v_group_end:}, which smuggles
4846 % the collected argument out of the group.
4847 % \begin{macrocode}
4848 \cs_new_protected:Npn \@@_grab_v:w
4849 {
4850 \bool_set_false:N \l_@@_long_bool
4851 \@@_grab_v_aux:w
4852 }
4853 \cs_new_protected:Npn \@@_grab_v_long:w
4854 {
4855 \bool_set_true:N \l_@@_long_bool
4856 \@@_grab_v_aux:w
4857 }
4858 \cs_new_protected:Npn \@@_grab_v_aux:w #1 \@@_run_code:
4859 {
4860 \tl_set:Nn \l_@@_signature_tl {#1}
4861 \group_begin:
4862 \tex_escapechar:D = 92 \scan_stop:
4863 \tex_endlinechar:D = `\^^M \scan_stop:
4864 \tl_clear:N \l_@@_v_arg_tl
4865 \peek_remove_spaces:n
4866 {
4867 \peek_meaning_remove:NTF \c_group_begin_token
4868 {
4869 \group_align_safe_end:
4870 \@@_grab_v_bgroup:
4871 }
4872 {
4873 \peek_N_type:TF
4874 { \@@_grab_v_aux_test:N }
4875 { \@@_grab_v_aux_abort:n { } }
4876 }
4877 }
4878 }
4879 \cs_new_protected:Npn \@@_grab_v_group_end:
4880 {
4881 \exp_args:NNNo
4882 \group_end:
4883 \tl_set:Nn \l_@@_v_arg_tl { \l_@@_v_arg_tl }
4884 }
4885 % \end{macrocode}
4886 % \end{macro}
4887 % \end{macro}
4888 % \end{macro}
4889 % \end{macro}
4890 %
4891 % \begin{macro}{\@@_grab_v_aux_test:N}
4892 % \begin{macro}
4893 % {
4894 % \@@_grab_v_aux_loop:N,
4895 % \@@_grab_v_aux_loop:NN,
4896 % \@@_grab_v_aux_loop_end:
4897 % }
4898 % Check that the opening delimiter is a character, setup category codes,
4899 % then start reading tokens one by one, keeping the delimiter as an argument.
4900 % If the verbatim was not nested, we will be grabbing one character
4901 % at each step. Unfortunately, it can happen that what follows the
4902 % verbatim argument is already tokenized. Thus, we check at each step
4903 % that the next token is indeed a \enquote{nice}
4904 % character, \emph{i.e.}, is not a character with
4905 % category code $1$ (begin-group), $2$ (end-group)
4906 % or $6$ (macro parameter), nor the space character,
4907 % with category code~$10$ and character code~$32$,
4908 % nor a control sequence.
4909 % The partially built argument is stored in \cs{l_@@_v_arg_tl}.
4910 % If we ever meet a token which we cannot grab (non-N-type),
4911 % or which is not a character according to
4912 % \cs{@@_grab_v_token_if_char:NTF}, then we bail out with
4913 % \cs{@@_grab_v_aux_abort:n}. Otherwise, we stop at the first
4914 % character matching the delimiter.
4915 % \begin{macrocode}
4916 \cs_new_protected:Npn \@@_grab_v_aux_test:N #1
4917 {
4918 \@@_grab_v_token_if_char:NTF #1
4919 {
4920 \@@_grab_v_aux_put:N #1
4921 \@@_grab_v_aux_catcodes:
4922 \@@_grab_v_aux_loop:N #1
4923 }
4924 { \@@_grab_v_aux_abort:n {#1} #1 }
4925 }
4926 \cs_new_protected:Npn \@@_grab_v_aux_loop:N #1
4927 {
4928 \peek_N_type:TF
4929 { \@@_grab_v_aux_loop:NN #1 }
4930 { \@@_grab_v_aux_abort:n { } }
4931 }
4932 \cs_new_protected:Npn \@@_grab_v_aux_loop:NN #1#2
4933 {
4934 \@@_grab_v_token_if_char:NTF #2
4935 {
4936 \token_if_eq_charcode:NNTF #1 #2
4937 { \@@_grab_v_aux_loop_end: }
4938 {
4939 \@@_grab_v_aux_put:N #2
4940 \@@_grab_v_aux_loop:N #1
4941 }
4942 }
4943 { \@@_grab_v_aux_abort:n {#2} #2 }
4944 }
4945 \cs_new_protected:Npn \@@_grab_v_aux_loop_end:
4946 {
4947 \@@_grab_v_group_end:
4948 \@@_add_arg:e { \tl_tail:N \l_@@_v_arg_tl }
4949 }
4950 % \end{macrocode}
4951 % \end{macro}
4952 % \end{macro}
4953 %
4954 % \begin{variable}{\l_@@_v_nesting_int}
4955 % \begin{macrocode}
4956 \int_new:N \l_@@_v_nesting_int
4957 % \end{macrocode}
4958 % \end{variable}
4959 %
4960 % \begin{macro}{\@@_grab_v_bgroup:}
4961 % \begin{macro}{\@@_grab_v_bgroup_loop:}
4962 % \begin{macro}{\@@_grab_v_bgroup_loop:N}
4963 % If the opening delimiter is a left brace, we keep track of
4964 % how many left and right braces were encountered so far in
4965 % \cs{l_@@_v_nesting_int} (the methods used for optional
4966 % arguments cannot apply here), and stop as soon as it reaches~$0$.
4967 %
4968 % Some care was needed when removing the opening delimiter, which
4969 % has already been assigned category code~$1$: using
4970 % \cs{peek_meaning_remove:NTF} in the \cs{@@_grab_v_aux:w}
4971 % function would break within alignments. Instead, we first
4972 % convert that token to a string, and remove the result as a
4973 % normal undelimited argument.
4974 % \begin{macrocode}
4975 \cs_new_protected:Npe \@@_grab_v_bgroup:
4976 {
4977 \exp_not:N \@@_grab_v_aux_catcodes:
4978 \exp_not:n { \int_set:Nn \l_@@_v_nesting_int { 1 } }
4979 \exp_not:N \@@_grab_v_aux_put:N \iow_char:N \{
4980 \exp_not:N \@@_grab_v_bgroup_loop:
4981 }
4982 \cs_new_protected:Npn \@@_grab_v_bgroup_loop:
4983 {
4984 \peek_N_type:TF
4985 { \@@_grab_v_bgroup_loop:N }
4986 { \@@_grab_v_aux_abort:n { } }
4987 }
4988 \cs_new_protected:Npn \@@_grab_v_bgroup_loop:N #1
4989 {
4990 \@@_grab_v_token_if_char:NTF #1
4991 {
4992 \token_if_eq_charcode:NNTF \c_group_end_token #1
4993 {
4994 \int_decr:N \l_@@_v_nesting_int
4995 \int_compare:nNnTF \l_@@_v_nesting_int > 0
4996 {
4997 \@@_grab_v_aux_put:N #1
4998 \@@_grab_v_bgroup_loop:
4999 }
5000 { \@@_grab_v_aux_loop_end: }
5001 }
5002 {
5003 \token_if_eq_charcode:NNT \c_group_begin_token #1
5004 { \int_incr:N \l_@@_v_nesting_int }
5005 \@@_grab_v_aux_put:N #1
5006 \@@_grab_v_bgroup_loop:
5007 }
5008 }
5009 { \@@_grab_v_aux_abort:n {#1} #1 }
5010 }
5011 % \end{macrocode}
5012 % \end{macro}
5013 % \end{macro}
5014 % \end{macro}
5015 %
5016 % \begin{macro}{\@@_grab_v_aux_catcodes:}
5017 % \changes{v1.2g}{2025-01-21}{Store spaces and tabs as active chars}
5018 % \begin{macro}{\@@_grab_v_aux_abort:n}
5019 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5020 % The approach for short verbatim arguments is to make the end-line
5021 % character a macro parameter character: this is forbidden by the
5022 % rest of the code. Then the error branch can check what caused the
5023 % bail out and give the appropriate error message.
5024 % \begin{macrocode}
5025 %<latexrelease>\IncludeInRelease{2025/06/01}{\@@_grab_v_aux_catcodes:}%
5026 %<latexrelease> {Active~spaces~and~tabs}
5027 \cs_new_protected:Npn \@@_grab_v_aux_catcodes:
5028 {
5029 \cs_set_eq:NN \do \char_set_catcode_other:N
5030 \dospecials
5031 \char_set_catcode_active:n { `\ }
5032 \char_set_catcode_active:n { `\^^I }
5033 \bool_if:NTF \l_@@_long_bool
5034 { \char_set_catcode_other:n { \tex_endlinechar:D } }
5035 { \char_set_catcode_parameter:n { \tex_endlinechar:D } }
5036 }
5037 %<latexrelease>\EndIncludeInRelease
5038 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_grab_v_aux_catcodes:}%
5039 %<latexrelease> {Active~spaces~and~tabs}
5040 %<latexrelease>\cs_new_protected:Npn \@@_grab_v_aux_catcodes:
5041 %<latexrelease> {
5042 %<latexrelease> \cs_set_eq:NN \do \char_set_catcode_other:N
5043 %<latexrelease> \dospecials
5044 %<latexrelease> \bool_if:NTF \l_@@_long_bool
5045 %<latexrelease> { \char_set_catcode_other:n { \tex_endlinechar:D } }
5046 %<latexrelease> { \char_set_catcode_parameter:n { \tex_endlinechar:D } }
5047 %<latexrelease> }
5048 %<latexrelease>\EndIncludeInRelease
5049 \cs_new_protected:Npn \@@_grab_v_aux_abort:n #1
5050 {
5051 \@@_grab_v_group_end:
5052 \exp_after:wN \exp_after:wN \exp_after:wN
5053 \peek_meaning_remove:NTF \char_generate:nn { \tex_endlinechar:D } { 6 }
5054 {
5055 \msg_error:nneee { cmd } { verbatim-nl }
5056 { \@@_environment_or_command: }
5057 { \tl_to_str:N \l_@@_v_arg_tl }
5058 { \tl_to_str:n {#1} }
5059 \@@_add_arg:n { \NoValue }
5060 }
5061 {
5062 \msg_error:nneee { cmd } { verbatim-tokenized }
5063 { \@@_environment_or_command: }
5064 { \tl_to_str:N \l_@@_v_arg_tl }
5065 { \tl_to_str:n {#1} }
5066 \@@_add_arg:n { \NoValue }
5067 }
5068 }
5069 % \end{macrocode}
5070 % \end{macro}
5071 % \end{macro}
5072 %
5073 % \begin{macro}{\@@_grab_v_aux_put:N}
5074 % \changes{v1.2d}{2024/03/21}{Collect \cs{endlinechar} as \cs{obeyedline}}
5075 % \changes{v1.2g}{2025-01-21}{Simplify catcode handling}
5076 % Storing one token in the collected argument: everything as-is except
5077 % for end-of-lines, with \cs{exp_not:N} to handle actives.
5078 % \begin{macrocode}
5079 %<latexrelease>\IncludeInRelease{2025-06-01}{\@@_grab_v_aux_put:N}%
5080 %<latexrelease> {Use~more~std~catcodes}
5081 \cs_new_protected:Npn \@@_grab_v_aux_put:N #1
5082 {
5083 \tl_put_right:Ne \l_@@_v_arg_tl
5084 {
5085 \int_compare:nNnTF {`#1} = \tex_endlinechar:D
5086 { \exp_not:N \obeyedline }
5087 { \exp_not:N #1 }
5088 }
5089 }
5090 %<latexrelease>\EndIncludeInRelease
5091 %<latexrelease>\IncludeInRelease{2024/06/01}{\@@_grab_v_aux_put:N}%
5092 %<latexrelease> {Endlines~as~\obeyedline}
5093 %<latexrelease>\cs_new_protected:Npn \@@_grab_v_aux_put:N #1
5094 %<latexrelease> {
5095 %<latexrelease> \tl_put_right:Nx \l_@@_v_arg_tl
5096 %<latexrelease> {
5097 %<latexrelease> \token_if_active:NTF #1
5098 %<latexrelease> { \exp_not:N #1 }
5099 %<latexrelease> {
5100 %<latexrelease> \int_compare:nNnTF {`#1} = \tex_endlinechar:D
5101 %<latexrelease> { \exp_not:N \obeyedline }
5102 %<latexrelease> { \token_to_str:N #1 }
5103 %<latexrelease> }
5104 %<latexrelease> }
5105 %<latexrelease> }
5106 %<latexrelease>\EndIncludeInRelease
5107 %<latexrelease>\IncludeInRelease{2020/10/01}{\@@_grab_v_aux_put:N}%
5108 %<latexrelease> {Endlines~as~\obeyedline}
5109 %<latexrelease>\cs_new_protected:Npn \@@_grab_v_aux_put:N #1
5110 %<latexrelease> {
5111 %<latexrelease> \tl_put_right:Nx \l_@@_v_arg_tl
5112 %<latexrelease> {
5113 %<latexrelease> \token_if_active:NTF #1
5114 %<latexrelease> { \exp_not:N #1 } { \token_to_str:N #1 }
5115 %<latexrelease> }
5116 %<latexrelease> }
5117 %<latexrelease>\EndIncludeInRelease
5118 % \end{macrocode}
5119 % \end{macro}
5120 %
5121 % \begin{macro}{\@@_grab_v_token_if_char:NTF}
5122 % This function assumes that the escape character is printable.
5123 % Then the string representation of control sequences is at least
5124 % two characters, and \cs{str_tail:n} only removes the escape
5125 % character. Macro parameter characters are doubled by
5126 % \cs{tl_to_str:n}, and will also yield a non-empty result,
5127 % hence are not considered as characters.
5128 % \begin{macrocode}
5129 \cs_new_protected:Npn \@@_grab_v_token_if_char:NTF #1
5130 { \str_if_eq:eeTF { } { \str_tail:n {#1} } }
5131 % \end{macrocode}
5132 % \end{macro}
5133 %
5134 % \begin{macro}{\@@_add_arg:n, \@@_add_arg:V, \@@_add_arg:o, \@@_add_arg:e}
5135 % When an argument is found it is stored, then further arguments are
5136 % grabbed by calling \cs{l_@@_signature_tl}.
5137 % \begin{macrocode}
5138 \cs_new_protected:Npn \@@_add_arg:n #1
5139 {
5140 \tl_put_right:Nn \l_@@_args_tl { {#1} }
5141 \l_@@_signature_tl \@@_run_code:
5142 }
5143 \cs_generate_variant:Nn \@@_add_arg:n { V , o , e }
5144 % \end{macrocode}
5145 % \end{macro}
5146 %
5147 % \subsection{Grabbing arguments expandably}
5148 %
5149 % \begin{macro}[EXP]{\@@_expandable_grab_D:w}
5150 % \begin{macro}[EXP]{\@@_expandable_grab_D:NNNwNNn}
5151 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5152 % \begin{macro}[EXP]{\@@_expandable_grab_D:NNNwNNnnn}
5153 % \begin{macro}[EXP]{\@@_expandable_grab_D:Nw}
5154 % \begin{macro}[EXP]{\@@_expandable_grab_D:nnNNNwNN}
5155 % The first step is to grab the first token or group. The generic grabbers
5156 % \cs{\meta{function}}\verb*| | and \cs{\meta{function}}\verb*| | are just after \cs{q_@@}, we go and find
5157 % them (and use the long one).
5158 % \begin{macrocode}
5159 \cs_new:Npn \@@_expandable_grab_D:w #1 \q_@@ #2#3
5160 { #2 { \@@_expandable_grab_D:NNNwNNn #1 \q_@@ #2 #3 } }
5161 % \end{macrocode}
5162 % We then wish to test whether |#7|, which we just grabbed, is exactly |#2|.
5163 % A preliminary test is whether their string representations coincide, then
5164 % expand the only grabber function we have, |#1|, once: the two strings below
5165 % are equal if and only if |#7| matches |#2| exactly.\footnote{It is obvious
5166 % that if \texttt{\#7} matches \texttt{\#2} then the strings are equal. We
5167 % must check the converse. The right-hand-side of \cs{str_if_eq:onTF} does
5168 % not end with \texttt{\#3}, implying that the grabber function took
5169 % everything as its arguments. The first brace group can only be empty if
5170 % \texttt{\#7} starts with \texttt{\#2}, otherwise the brace group preceding
5171 % \texttt{\#7} would not vanish. The third brace group is empty, thus the
5172 % \cs{q_@@} that was used by our grabber \texttt{\#1} must be the one
5173 % that we inserted (not some token in \texttt{\#7}), hence the second brace
5174 % group contains the end of \texttt{\#7} followed by \texttt{\#2}. Since this
5175 % is \texttt{\#2} on the right-hand-side, and no brace can be lost there,
5176 % \texttt{\#7} must contain nothing else than its leading \texttt{\#2}.}
5177 % The preliminary test is needed as |#7| could validly contain
5178 % \tn{par} (because a later mandatory argument could be long) and our
5179 % grabber may be short. If
5180 % |#7| does not match |#2|, then the optional argument is missing, we use the
5181 % default \cs{NoValue}, and put back the argument |#7| in the input stream.
5182 %
5183 % If it does match, then interesting things need to be done. We will grab the
5184 % argument piece by piece, with the following pattern:
5185 % \begin{quote}
5186 % \meta{grabber} \Arg{tokens} \\
5187 % ~~\cs{q_nil} \Arg{piece 1} \meta{piece 2} \cs{ERROR} \cs{q_@@}\\
5188 % ~~\cs{q_nil} \meta{input stream}
5189 % \end{quote}
5190 % The \meta{grabber} will find an opening delimiter in \meta{piece 2}, take
5191 % the \cs{q_@@} as a second delimiter, and find more material delimited
5192 % by the closing delimiter in the \meta{input stream}. We then move the part
5193 % before the opening delimiter from \meta{piece 2} to \meta{piece 1}, and the
5194 % material taken from the \meta{input stream} to the \meta{piece 2}. Thus,
5195 % the argument moves gradually from the \meta{input stream} to the
5196 % \meta{piece 2}, then to the \meta{piece 1} when we have made sure to find
5197 % all opening and closing delimiters. This two-step process ensures that
5198 % nesting works: the number of opening delimiters minus closing delimiters in
5199 % \meta{piece 1} is always equal to the number of closing delimiters in
5200 % \meta{piece 2}. We stop grabbing arguments once the \meta{piece 2} contains
5201 % no opening delimiter any more, hence the balance is reached, and the final
5202 % argument is \meta{piece 1} \meta{piece 2}.
5203 % \begin{macrocode}
5204 \cs_new:Npn \@@_expandable_grab_D:NNNwNNn #1#2#3#4 \q_@@ #5#6#7
5205 {
5206 \str_if_eq:nnTF {#2} {#7}
5207 {
5208 \str_if_eq:onTF
5209 { #1 { } { } #7 #2 \q_@@ #3 }
5210 { { } {#2} { } }
5211 }
5212 { \use_ii:nn }
5213 {
5214 #1
5215 { \@@_expandable_grab_D:NNNwNNnnn #1#2#3#4 \q_@@ #5#6 }
5216 \q_nil { } #2 \ERROR \q_@@ \ERROR
5217 }
5218 { #4 { \NoValue } \q_@@ #5 #6 {#7} }
5219 }
5220 % \end{macrocode}
5221 % At this stage, |#7| is \cs{q_nil} \Arg{piece 1} \meta{more for piece 1},
5222 % and we want to concatenate all that, removing \cs{q_nil}, and keeping the
5223 % opening delimiter |#2|. Simply use \cs{use_ii:nn}. Also, |#8| is
5224 % \meta{remainder of piece 2} \cs{ERROR}, and |#9| is \cs{ERROR} \meta{more
5225 % for piece 2}. We concatenate those, replacing the two \cs{ERROR} by the
5226 % closing delimiter |#3|.
5227 % \begin{macrocode}
5228 \cs_new:Npn \@@_expandable_grab_D:NNNwNNnnn #1#2#3#4 \q_@@ #5#6#7#8#9
5229 {
5230 \exp_args:Nof \@@_expandable_grab_D:nnNNNwNN
5231 { \use_ii:nn #7 #2 }
5232 { \@@_expandable_grab_D:Nw #3 \exp_stop_f: #8 #9 }
5233 #1#2#3 #4 \q_@@ #5 #6
5234 }
5235 \cs_new:Npn \@@_expandable_grab_D:Nw #1#2 \ERROR \ERROR { #2 #1 }
5236 % \end{macrocode}
5237 % Armed with our two new \meta{pieces}, we are ready to loop. However, we
5238 % must first see if \meta{piece 2} (here |#2|) contains any opening
5239 % delimiter |#4|. Again, we expand |#3|, this time removing its whole output
5240 % with \cs{use_none:nnn}. The test is similar to \cs{tl_if_in:nnTF}. The
5241 % token list is empty if and only if |#2| does not contain the opening
5242 % delimiter. In that case, we are done, and put the argument (from which we
5243 % remove a spurious pair of delimiters coming from how we started the loop).
5244 % Otherwise, we go back to looping with
5245 % \cs{@@_expandable_grab_D:NNNwNNnnn}. The code to deal with brace stripping
5246 % is much the same as for the non-expandable case.
5247 % \begin{macrocode}
5248 \cs_new:Npn \@@_expandable_grab_D:nnNNNwNN #1#2#3#4#5#6 \q_@@ #7#8
5249 {
5250 \exp_args:No \tl_if_empty:oTF
5251 { #3 { \use_none:nnn } #2 \q_@@ #5 #4 \q_@@ #5 }
5252 {
5253 \tl_if_blank:oTF { \use_none:nn #1#2 }
5254 { \@@_put_arg_expandable:ow { \use_none:nn #1#2 } }
5255 {
5256 \str_if_eq:eeTF
5257 { \exp_not:o { \use_none:nn #1#2 } }
5258 { { \exp_not:o { \use_iii:nnnn #1#2 \q_nil } } }
5259 { \@@_put_arg_expandable:ow { \use_iii:nnn #1#2 } }
5260 { \@@_put_arg_expandable:ow { \use_none:nn #1#2 } }
5261 }
5262 #6 \q_@@ #7 #8
5263 }
5264 {
5265 #3
5266 { \@@_expandable_grab_D:NNNwNNnnn #3#4#5#6 \q_@@ #7 #8 }
5267 \q_nil {#1} #2 \ERROR \q_@@ \ERROR
5268 }
5269 }
5270 % \end{macrocode}
5271 % \end{macro}
5272 % \end{macro}
5273 % \end{macro}
5274 % \end{macro}
5275 % \end{macro}
5276 %
5277 % \begin{macro}[EXP]{\@@_expandable_grab_D_alt:w}
5278 % \begin{macro}[EXP]{\@@_expandable_grab_D_alt:NNwNNn}
5279 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5280 % \begin{macro}[EXP]{\@@_expandable_grab_D_alt:Nwn}
5281 % When the delimiters are identical, nesting is not possible and a simplified
5282 % approach is used. The test concept here is the same as for the case where
5283 % the delimiters are different but there cannot be any nesting.
5284 % \begin{macrocode}
5285 \cs_new:Npn \@@_expandable_grab_D_alt:w #1 \q_@@ #2#3
5286 { #2 { \@@_expandable_grab_D_alt:NNwNNn #1 \q_@@ #2 #3 } }
5287 \cs_new:Npn \@@_expandable_grab_D_alt:NNwNNn #1#2#3 \q_@@ #4#5#6
5288 {
5289 \str_if_eq:nnTF {#6} {#2}
5290 {
5291 \str_if_eq:onTF
5292 { #1 { } #6 #2 #2 }
5293 { { } #2 }
5294 }
5295 { \use_ii:nn }
5296 {
5297 #1
5298 { \@@_expandable_grab_D_alt:NNwn #4 #5 #3 \q_@@ }
5299 #6 \ERROR
5300 }
5301 { #3 { \NoValue } \q_@@ #4 #5 {#6} }
5302 }
5303 \cs_new:Npn \@@_expandable_grab_D_alt:NNwn #1#2#3 \q_@@ #4
5304 {
5305 \tl_if_blank:oTF { \use_none:n #4 }
5306 { \@@_put_arg_expandable:ow { \use_none:n #4 } }
5307 {
5308 \str_if_eq:eeTF
5309 { \exp_not:o { \use_none:n #4 } }
5310 { { \exp_not:o { \use_ii:nnn #4 \q_nil } } }
5311 { \@@_put_arg_expandable:ow { \use_ii:nn #4 } }
5312 { \@@_put_arg_expandable:ow { \use_none:n #4 } }
5313 }
5314 #3 \q_@@ #1 #2
5315 }
5316 % \end{macrocode}
5317 % \end{macro}
5318 % \end{macro}
5319 % \end{macro}
5320 %
5321 % \begin{macro}[EXP]{\@@_expandable_grab_E:w, \@@_expandable_grab_E_long:w}
5322 % \begin{macro}[EXP]{\@@_expandable_grab_E_aux:w}
5323 % \begin{macro}[EXP]{\@@_expandable_grab_E_test:nnw}
5324 % \begin{macro}[EXP]{\@@_expandable_grab_E_loop:nnnNNw}
5325 % \begin{macro}[EXP]{\@@_expandable_grab_E_find:w}
5326 % \begin{macro}[EXP]{\@@_expandable_grab_E_find:nnw}
5327 % \begin{macro}[EXP]{\@@_expandable_grab_E_end:nnw}
5328 % We keep track of long/short by placing the appropriate grabber as
5329 % the third token after \cs{q_@@}; it is eventually removed by the
5330 % \texttt{end:nnw} auxiliary. The \texttt{aux:w} auxiliary will be
5331 % called repeatedly with two arguments: the set
5332 % of pairs \meta{parser} \meta{token}, and the set of arguments found
5333 % so far (initially all |{\NoValue}|). At each step, grab what
5334 % follows in the input stream then call the \texttt{loop:nnnNNw}
5335 % auxiliary to compare it with each possible embellishment in turn.
5336 % This auxiliary's |#1| is what was found in the input, |#2| collects
5337 % \meta{parser} \meta{token} pairs that did not match, |#3| collects
5338 % the corresponding arguments found previously, |#4| and |#5| is the
5339 % current pair, |#6| is the remaining pairs, |#7| is empty or two
5340 % \cs{q_nil}, and |#8| is the current argument. If none of the pairs
5341 % matched (determined by \cs{quark_if_nil:NTF}) then call the
5342 % \texttt{end} auxiliary to stop looking for embellishments,
5343 % remembering to put what was grabbed in the input back where it
5344 % belongs, and storing the arguments found just before \cs{q_@@}. If
5345 % the current argument |#8| is not \cs{NoValue} or if the input |#1|
5346 % does not match |#5| (see \texttt{t}-type arguments below for a
5347 % similar \cs{str_if_eq:onTF} test) then carry on the loop.
5348 % Otherwise, we found a new embellishment: grab the corresponding
5349 % argument in the input using the \texttt{find:w} auxiliary. To avoid
5350 % losing braces around that auxiliary's argument we include a
5351 % space, which will be eliminated in the next loop through
5352 % embellishments.
5353 % \begin{macrocode}
5354 \cs_new:Npn \@@_expandable_grab_E:w #1 \q_@@ #2#3
5355 { \@@_expandable_grab_E_aux:w #1 \q_@@ #2 #3 #3 }
5356 \cs_new:Npn \@@_expandable_grab_E_long:w #1 \q_@@ #2#3
5357 { \@@_expandable_grab_E_aux:w #1 \q_@@ #2 #3 #2 }
5358 \cs_new:Npn \@@_expandable_grab_E_aux:w #1 \q_@@ #2#3#4
5359 { #2 { \@@_expandable_grab_E_test:nnw #1 \q_@@ #2 #3 #4 } }
5360 \cs_new:Npn \@@_expandable_grab_E_test:nnw #1#2#3 \q_@@ #4#5#6#7
5361 {
5362 \@@_expandable_grab_E_loop:nnnNNw {#7} { } { }
5363 #1 \q_nil \q_nil \q_nil \q_mark #2 \q_nil
5364 #3 \q_@@ #4 #5 #6
5365 }
5366 \cs_new:Npn \@@_expandable_grab_E_loop:nnnNNw
5367 #1#2#3#4#5#6 \q_nil #7 \q_mark #8
5368 {
5369 \quark_if_nil:NTF #4
5370 { \@@_expandable_grab_E_end:nnw {#1} {#3} }
5371 {
5372 \tl_if_novalue:nTF {#8}
5373 { \str_if_eq:onTF { #4 { } #1 #5 } {#5} }
5374 { \use_ii:nn }
5375 { \@@_expandable_grab_E_find:w { #2 #4 #5 #6 } {#3} ~ }
5376 {
5377 \@@_expandable_grab_E_loop:nnnNNw
5378 {#1} { #2 #4 #5 } { #3 {#8} }
5379 #6 \q_nil #7 \q_mark
5380 }
5381 }
5382 }
5383 \cs_new:Npn \@@_expandable_grab_E_find:w #1 \q_@@ #2#3#4
5384 { #4 { \@@_expandable_grab_E_find:nnw #1 \q_@@ #2 #3 #4 } }
5385 \cs_new:Npn \@@_expandable_grab_E_find:nnw #1#2#3 \q_nil #4 \q_@@ #5#6#7#8
5386 { \@@_expandable_grab_E_aux:w {#1} { #2 {#8} #3 } #4 \q_@@ #5 #6 #7 }
5387 \cs_new:Npn \@@_expandable_grab_E_end:nnw #1#2#3 \q_@@ #4#5#6
5388 { #3 #2 \q_@@ #4 #5 {#1} }
5389 % \end{macrocode}
5390 % \end{macro}
5391 % \end{macro}
5392 % \end{macro}
5393 % \end{macro}
5394 % \end{macro}
5395 % \end{macro}
5396 % \end{macro}
5397 %
5398 % \begin{macro}[EXP]{\@@_expandable_grab_m:w, \@@_expandable_grab_m_long:w}
5399 % \begin{macro}[EXP]{\@@_expandable_grab_m_aux:wNn}
5400 % The mandatory case is easy: find the auxiliary after the \cs{q_@@},
5401 % and use it directly to grab the argument, then correctly position
5402 % the argument before \cs{q_@@}.
5403 % \begin{macrocode}
5404 \cs_new:Npn \@@_expandable_grab_m:w #1 \q_@@ #2#3
5405 { #3 { \@@_expandable_grab_m_aux:wNn #1 \q_@@ #2 #3 } }
5406 \cs_new:Npn \@@_expandable_grab_m_long:w #1 \q_@@ #2#3
5407 { #2 { \@@_expandable_grab_m_aux:wNn #1 \q_@@ #2 #3 } }
5408 \cs_new:Npn \@@_expandable_grab_m_aux:wNn #1 \q_@@ #2#3#4
5409 { #1 {#4} \q_@@ #2 #3 }
5410 % \end{macrocode}
5411 % \end{macro}
5412 % \end{macro}
5413 %
5414 % \begin{macro}[EXP]{\@@_expandable_grab_R:w}
5415 % \begin{macro}[EXP]{\@@_expandable_grab_R_aux:NNNwNNn}
5416 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5417 % Much the same as for the \texttt{D}-type argument, with only the lead-off
5418 % function varying.
5419 % \begin{macrocode}
5420 \cs_new:Npn \@@_expandable_grab_R:w #1 \q_@@ #2#3
5421 { #2 { \@@_expandable_grab_R_aux:NNNwNNn #1 \q_@@ #2#3 } }
5422 \cs_new:Npn \@@_expandable_grab_R_aux:NNNwNNn #1#2#3#4 \q_@@ #5#6#7
5423 {
5424 \str_if_eq:nnTF {#7} {#2}
5425 {
5426 \str_if_eq:onTF
5427 { #1 { } { } #7 #2 \q_@@ #3 }
5428 { { } {#2} { } }
5429 }
5430 { \use_ii:nn }
5431 {
5432 #1
5433 { \@@_expandable_grab_D:NNNwNNnnn #1#2#3#4 \q_@@ #5#6 }
5434 \q_nil { } #2 \ERROR \q_@@ \ERROR
5435 }
5436 {
5437 \msg_expandable_error:nnff { cmd } { missing-required }
5438 { \exp_args:Nf \tl_trim_spaces:n { \token_to_str:N #5 } }
5439 { \tl_to_str:n {#2} }
5440 #4 { \NoValue } \q_@@ #5 #6 {#7}
5441 }
5442 }
5443 % \end{macrocode}
5444 % \end{macro}
5445 % \end{macro}
5446 %
5447 % \begin{macro}[EXP]{\@@_expandable_grab_R_alt:w}
5448 % \begin{macro}[EXP]{\@@_expandable_grab_R_alt_aux:NNwNNn}
5449 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5450 % When the delimiters are identical, nesting is not possible and a simplified
5451 % approach is used. The test concept here is the same as for the case where
5452 % the delimiters are different.
5453 % \begin{macrocode}
5454 \cs_new:Npn \@@_expandable_grab_R_alt:w #1 \q_@@ #2#3
5455 { #2 { \@@_expandable_grab_R_alt_aux:NNwNNn #1 \q_@@ #2#3 } }
5456 \cs_new:Npn \@@_expandable_grab_R_alt_aux:NNwNNn #1#2#3 \q_@@ #4#5#6
5457 {
5458 \str_if_eq:nnTF {#6} {#2}
5459 {
5460 \str_if_eq:onTF
5461 { #1 { } #6 #2 #2 }
5462 { { } #2 }
5463 }
5464 { \use_ii:nn }
5465 {
5466 #1
5467 { \@@_expandable_grab_D_alt:NNwn #4 #5 #3 \q_@@ }
5468 #6 \ERROR
5469 }
5470 {
5471 \msg_expandable_error:nnff { cmd } { missing-required }
5472 { \exp_args:Nf \tl_trim_spaces:n { \token_to_str:N #4 } }
5473 { \tl_to_str:n {#2} }
5474 #3 { \NoValue } \q_@@ #4 #5 {#6}
5475 }
5476 }
5477 % \end{macrocode}
5478 % \end{macro}
5479 % \end{macro}
5480 %
5481 % \begin{macro}[EXP]{\@@_expandable_grab_t:w}
5482 % \begin{macro}[EXP]{\@@_expandable_grab_t_aux:NNwn}
5483 % As for a \texttt{D}-type argument, here we compare the grabbed tokens using
5484 % the only parser we have in order to work out if |#2| is exactly equal to
5485 % the output of the grabber.
5486 % \begin{macrocode}
5487 \cs_new:Npn \@@_expandable_grab_t:w #1 \q_@@ #2#3
5488 { #2 { \@@_expandable_grab_t_aux:NNwn #1 \q_@@ #2 #3 } }
5489 \cs_new:Npn \@@_expandable_grab_t_aux:NNwn #1#2#3 \q_@@ #4#5#6
5490 {
5491 \str_if_eq:onTF { #1 { } #6 #2 } {#2}
5492 { #3 { \BooleanTrue } \q_@@ #4 #5 }
5493 { #3 { \BooleanFalse } \q_@@ #4 #5 {#6} }
5494 }
5495 % \end{macrocode}
5496 % \end{macro}
5497 % \end{macro}
5498 %
5499 % \begin{macro}[EXP]
5500 % {\@@_put_arg_expandable:nw, \@@_put_arg_expandable:ow}
5501 % A useful helper, to store arguments when they are ready.
5502 % \begin{macrocode}
5503 \cs_new:Npn \@@_put_arg_expandable:nw #1#2 \q_@@ { #2 {#1} \q_@@ }
5504 \cs_generate_variant:Nn \@@_put_arg_expandable:nw { o }
5505 % \end{macrocode}
5506 % \end{macro}
5507 %
5508 % \subsection{Argument processors}
5509 %
5510 % \begin{macro}{\@@_bool_reverse:N}
5511 % A simple reversal.
5512 % \begin{macrocode}
5513 \cs_new_protected:Npn \@@_bool_reverse:N #1
5514 {
5515 \bool_if:NTF #1
5516 { \tl_set:Nn \ProcessedArgument { \c_false_bool } }
5517 { \tl_set:Nn \ProcessedArgument { \c_true_bool } }
5518 }
5519 % \end{macrocode}
5520 % \end{macro}
5521 %
5522 % \begin{variable}{\l_@@_split_list_seq, \l_@@_split_list_tl}
5523 % \begin{macro}{\@@_split_list:nn}
5524 % \begin{macro}{\@@_split_list_multi:nn, \@@_split_list_multi:nV}
5525 % \begin{macro}{\@@_split_list_single:Nn}
5526 % Splitting can take place either at a single token or at a longer
5527 % identifier. To deal with single active tokens, a two-part procedure is
5528 % needed.
5529 % \begin{macrocode}
5530 \seq_new:N \l_@@_split_list_seq
5531 \tl_new:N \l_@@_split_list_tl
5532 \cs_new_protected:Npn \@@_split_list:nn #1#2
5533 {
5534 \tl_if_single:nTF {#1}
5535 {
5536 \token_if_cs:NTF #1
5537 { \@@_split_list_multi:nn {#1} {#2} }
5538 { \@@_split_list_single:Nn #1 {#2} }
5539 }
5540 { \@@_split_list_multi:nn {#1} {#2} }
5541 }
5542 \cs_new_protected:Npn \@@_split_list_multi:nn #1#2
5543 {
5544 \seq_set_split:Nnn \l_@@_split_list_seq {#1} {#2}
5545 \tl_clear:N \ProcessedArgument
5546 \seq_map_inline:Nn \l_@@_split_list_seq
5547 { \tl_put_right:Nn \ProcessedArgument { {##1} } }
5548 }
5549 \cs_generate_variant:Nn \@@_split_list_multi:nn { nV }
5550 \group_begin:
5551 \char_set_catcode_active:N \^^@
5552 \cs_new_protected:Npn \@@_split_list_single:Nn #1#2
5553 {
5554 \tl_set:Nn \l_@@_split_list_tl {#2}
5555 \group_begin:
5556 \char_set_lccode:nn { `\^^@ } { `#1 }
5557 \tex_lowercase:D
5558 {
5559 \group_end:
5560 \tl_replace_all:Nnn \l_@@_split_list_tl { ^^@ }
5561 } {#1}
5562 \@@_split_list_multi:nV {#1} \l_@@_split_list_tl
5563 }
5564 \group_end:
5565 % \end{macrocode}
5566 % \end{macro}
5567 % \end{macro}
5568 % \end{macro}
5569 % \end{variable}
5570 %
5571 % \begin{macro}{\@@_split_argument:nnn}
5572 % \begin{macro}{\@@_split_argument_aux:nnnn}
5573 % \changes{v1.3h}{2025-11-04}{Update for \cs{NoValue} adjustment}
5574 % \begin{macro}[EXP]{\@@_split_argument_aux:n}
5575 % \begin{macro}[rEXP]{\@@_split_argument_aux:wn}
5576 % Splitting to a known number of items is a special version of splitting
5577 % a list, in which the limit is hard-coded and where there will always be
5578 % exactly the correct number of output items. An auxiliary function is
5579 % used to save on working out the token list length several times.
5580 % \begin{macrocode}
5581 \cs_new_protected:Npn \@@_split_argument:nnn #1#2#3
5582 {
5583 \@@_split_list:nn {#2} {#3}
5584 \exp_args:Nf \@@_split_argument_aux:nnnn
5585 { \tl_count:N \ProcessedArgument }
5586 {#1} {#2} {#3}
5587 }
5588 \cs_new_protected:Npn \@@_split_argument_aux:nnnn #1#2#3#4
5589 {
5590 \int_compare:nNnF {#1} = { #2 + 1 }
5591 {
5592 \int_compare:nNnTF {#1} > { #2 + 1 }
5593 {
5594 \tl_set:Ne \ProcessedArgument
5595 {
5596 \exp_last_unbraced:NnNo
5597 \@@_split_argument_aux:n
5598 { #2 + 1 }
5599 \use_none_delimit_by_q_stop:w
5600 \ProcessedArgument
5601 \q_stop
5602 }
5603 \msg_error:nneee { cmd } { arg-split }
5604 { \tl_to_str:n {#3} } { \int_eval:n { #2 + 1 } }
5605 { \tl_to_str:n {#4} }
5606 }
5607 {
5608 \tl_put_right:Ne \ProcessedArgument
5609 {
5610 \prg_replicate:nn { #2 + 1 - (#1) }
5611 { { \NoValue } }
5612 }
5613 }
5614 }
5615 }
5616 % \end{macrocode}
5617 % Auxiliaries to leave exactly the correct number of arguments in
5618 % \cs{ProcessedArgument}.
5619 % \begin{macrocode}
5620 \cs_new:Npn \@@_split_argument_aux:n #1
5621 { \prg_replicate:nn {#1} { \@@_split_argument_aux:wn } }
5622 \cs_new:Npn \@@_split_argument_aux:wn #1 \use_none_delimit_by_q_stop:w #2
5623 {
5624 \exp_not:n { {#2} }
5625 #1
5626 \use_none_delimit_by_q_stop:w
5627 }
5628 % \end{macrocode}
5629 % \end{macro}
5630 % \end{macro}
5631 % \end{macro}
5632 % \end{macro}
5633 %
5634 % \begin{macro}{\@@_trim_spaces:n}
5635 % This one is almost trivial.
5636 % \begin{macrocode}
5637 \cs_new_protected:Npn \@@_trim_spaces:n #1
5638 { \tl_set:Ne \ProcessedArgument { \tl_trim_spaces:n {#1} } }
5639 % \end{macrocode}
5640 % \end{macro}
5641 %
5642 % \subsection{Conversion to key--value form}
5643 %
5644 % This is implemented as a process but with no public interfaces,
5645 % hence is treated separately from the others: it's a feature of
5646 % \pkg{ltcmd} which just happens to use the same mechanism as a processor.
5647 %
5648 % \begin{macro}{\@@_arg_to_keyvalue:nn}
5649 % \changes{v1.1a}{2022/08/10}{New internal arg-to-keyval processor}
5650 % \begin{macro}{\@@_arg_to_keyvalue_braces:nnn}
5651 % \changes{v1.3i}{2026-02-02}{Treat empty input as empty keyval list}
5652 % \begin{macro}{\@@_arg_to_keyvalue_auxi:nnn}
5653 % \begin{macro}{\@@_arg_to_keyvalue_auxii:Nnnn}
5654 % \begin{macro}{\@@_arg_to_keyvalue_auxiii:nnn}
5655 % \begin{macro}{\@@_arg_to_keyvalue_auxiv:Nnnn}
5656 % \begin{macro}{\@@_arg_to_keyvalue_auxv:nn}
5657 % \begin{macro}{\@@_arg_to_keyvalue_loop:w}
5658 % \begin{macro}{\@@_arg_to_keyvalue_loop_group:n}
5659 % \begin{macro}{\@@_arg_to_keyvalue_loop_space:w}
5660 % \begin{macro}{\@@_arg_to_keyvalue_loop_N_type:N}
5661 % \begin{macro}{\@@_arg_to_keyvalue_math:w}
5662 % \begin{macro}{\@@_arg_to_keyvalue_math_N_type:N}
5663 % \begin{macro}{\@@_arg_to_keyvalue_math_group:n}
5664 % \begin{macro}{\@@_arg_to_keyvalue_math_space:w}
5665 % \begin{macro}{\@@_arg_to_keyvalue_set_default:nn}
5666 % \begin{macro}{\@@_arg_to_keyvalue_set_keyvalue:nn}
5667 % \begin{macro}[EXP]{\@@_split_N_head_apply:Nn}
5668 % \begin{macro}[EXP]{\@@_split_N_head_apply_aux:NNw}
5669 % If the entire argument is braced, we treat as free text and return as
5670 % the value for the text key. Alternatively, if the start of the input is
5671 % |=,| then it is forced to be key--value. To avoid needing to worry about
5672 % catcodes for this, and to allow spaces around the |=|, we use a
5673 % series of steps rather than a delimited argument. We also deal with the
5674 % case where the entire argument is blank (or is an entirely empty brace
5675 % group).
5676 % \begin{macrocode}
5677 \cs_new_protected:Npn \@@_arg_to_keyvalue:nn #1#2
5678 {
5679 \tl_trim_spaces_apply:nN {#2} \@@_arg_to_keyvalue_braces:nnn
5680 {#1} {#2}
5681 }
5682 \cs_new_protected:Npn \@@_arg_to_keyvalue_braces:nnn #1#2#3
5683 {
5684 \bool_lazy_and:nnTF
5685 { \tl_if_head_is_group_p:n {#1} }
5686 { \tl_if_blank_p:o { \use_none:n #1 } }
5687 { \tl_set:Ne \ProcessedArgument { #2 = { \exp_not:n #1 } } }
5688 {
5689 \tl_if_blank:nTF {#1}
5690 { \tl_clear:N \ProcessedArgument }
5691 { \@@_arg_to_keyvalue_auxi:nnn {#1} {#2} {#3} }
5692 }
5693 }
5694 \cs_new:Npn \@@_arg_to_keyvalue_auxi:nnn #1
5695 {
5696 \tl_if_head_is_N_type:nTF {#1}
5697 { \@@_split_N_head_apply:Nn \@@_arg_to_keyvalue_auxii:Nnnn {#1} }
5698 { \@@_arg_to_keyvalue_auxv:nn }
5699 }
5700 \cs_new:Npn \@@_arg_to_keyvalue_auxii:Nnnn #1#2
5701 {
5702 \str_if_eq:eeTF { \exp_not:n {#1} } { = }
5703 { \tl_trim_spaces_apply:nN {#2} \@@_arg_to_keyvalue_auxiii:nnn }
5704 { \@@_arg_to_keyvalue_auxv:nn }
5705 }
5706 \cs_new:Npn \@@_arg_to_keyvalue_auxiii:nnn #1
5707 {
5708 \tl_if_head_is_N_type:nTF {#1}
5709 { \@@_split_N_head_apply:Nn \@@_arg_to_keyvalue_auxiv:Nnnn {#1} }
5710 { \@@_arg_to_keyvalue_auxv:nn }
5711 }
5712 \cs_new:Npn \@@_arg_to_keyvalue_auxiv:Nnnn #1#2
5713 {
5714 \str_if_eq:eeTF { \exp_not:n {#1} } { , }
5715 { \tl_set:Nn \ProcessedArgument {#2} \use_none:nn }
5716 { \@@_arg_to_keyvalue_auxv:nn }
5717 }
5718 % \end{macrocode}
5719 % The two clear-cut cases have been eliminated, and we therefore have to deal
5720 % with a search for |=| signs. We need an \enquote{action} loop here
5721 % so we do not get mislead by for example |{=}|. As the code here is for
5722 % very much predictable types of input, we hard-code what constitutes
5723 % math mode opening and closing. At the very beginning, the default
5724 % key (|#1|) and the argument as given by the user (|#2|) are placed
5725 % right after the \cs{q_@@_recursion_stop}, so that when the recursion
5726 % ends, the macros \cs{@@_arg_to_keyvalue_set_default:nn} or
5727 % \cs{@@_arg_to_keyvalue_set_keyvalue:nn} can be used to grab these
5728 % two items and set the \cs{ProcessedArgument} accordingly.
5729 % \begin{macrocode}
5730 \cs_new_protected:Npn \@@_arg_to_keyvalue_auxv:nn #1#2
5731 {
5732 \@@_arg_to_keyvalue_loop:w #2
5733 \q_@@_recursion_tail \q_@@_recursion_stop {#1} {#2}
5734 }
5735 \cs_new_protected:Npn \@@_arg_to_keyvalue_loop:w #1 \q_@@_recursion_stop
5736 {
5737 \tl_if_head_is_N_type:nTF {#1}
5738 { \@@_arg_to_keyvalue_loop_N_type:N }
5739 {
5740 \tl_if_head_is_group:nTF {#1}
5741 { \@@_arg_to_keyvalue_loop_group:n }
5742 { \@@_arg_to_keyvalue_loop_space:w }
5743 }
5744 #1 \q_@@_recursion_stop
5745 }
5746 \cs_new_protected:Npn \@@_arg_to_keyvalue_loop_group:n #1
5747 { \@@_arg_to_keyvalue_loop:w }
5748 \use:n { \cs_new_protected:Npn \@@_arg_to_keyvalue_loop_space:w } ~
5749 { \@@_arg_to_keyvalue_loop:w }
5750 \cs_new_protected:Npn \@@_arg_to_keyvalue_loop_N_type:N #1
5751 {
5752 \@@_if_recursion_tail_stop_do:Nn #1
5753 { \@@_arg_to_keyvalue_set_default:nn }
5754 \str_if_eq:nnTF {#1} { = }
5755 {
5756 \@@_use_i_delimit_by_q_recursion_stop:nw
5757 { \@@_arg_to_keyvalue_set_keyvalue:nn }
5758 }
5759 {
5760 \bool_lazy_or:nnTF
5761 { \token_if_math_toggle_p:N #1 }
5762 { \str_if_eq_p:nn {#1} { \( } }
5763 { \@@_arg_to_keyvalue_math:w }
5764 { \@@_arg_to_keyvalue_loop:w }
5765 }
5766 }
5767 \cs_new_protected:Npn \@@_arg_to_keyvalue_math:w #1 \q_@@_recursion_stop
5768 {
5769 \tl_if_head_is_N_type:nTF {#1}
5770 { \@@_arg_to_keyvalue_math_N_type:N }
5771 {
5772 \tl_if_head_is_group:nTF {#1}
5773 { \@@_arg_to_keyvalue_math_group:n }
5774 { \@@_arg_to_keyvalue_math_space:w }
5775 }
5776 #1 \q_@@_recursion_stop
5777 }
5778 \cs_new_protected:Npn \@@_arg_to_keyvalue_math_N_type:N #1
5779 {
5780 \@@_if_recursion_tail_stop_do:Nn #1
5781 { \@@_arg_to_keyvalue_set_default:nn }
5782 \bool_lazy_or:nnTF
5783 { \token_if_math_toggle_p:N #1 }
5784 { \str_if_eq_p:nn {#1} { \) } }
5785 { \@@_arg_to_keyvalue_loop:w }
5786 { \@@_arg_to_keyvalue_math:w }
5787 }
5788 \cs_new_protected:Npn \@@_arg_to_keyvalue_math_group:n #1
5789 { \@@_arg_to_keyvalue_math:w }
5790 \use:n { \cs_new_protected:Npn \@@_arg_to_keyvalue_math_space:w } ~
5791 { \@@_arg_to_keyvalue_math:w }
5792 \cs_new_protected:Npn \@@_arg_to_keyvalue_set_default:nn #1#2
5793 { \tl_set:Nn \ProcessedArgument { #1 = {#2} } }
5794 \cs_new_protected:Npn \@@_arg_to_keyvalue_set_keyvalue:nn #1#2
5795 { \tl_set:Nn \ProcessedArgument {#2} }
5796 % \end{macrocode}
5797 % A utility to allow us to grab the first \texttt{N}-type token without
5798 % risking brace stripping the rest of the input.
5799 % \begin{macrocode}
5800 \cs_new:Npn \@@_split_N_head_apply:Nn #1#2
5801 { \exp:w \if_false: { \fi: \@@_split_N_head_apply_aux:NNw #1#2 } }
5802 \cs_new:Npn \@@_split_N_head_apply_aux:NNw #1#2
5803 {
5804 \exp_after:wN \exp_end:
5805 \exp_after:wN #1 \exp_after:wN #2 \exp_after:wN { \if_false: } \fi:
5806 }
5807
5808 % \end{macrocode}
5809 % \end{macro}
5810 % \end{macro}
5811 % \end{macro}
5812 % \end{macro}
5813 % \end{macro}
5814 % \end{macro}
5815 % \end{macro}
5816 % \end{macro}
5817 % \end{macro}
5818 % \end{macro}
5819 % \end{macro}
5820 % \end{macro}
5821 % \end{macro}
5822 % \end{macro}
5823 % \end{macro}
5824 % \end{macro}
5825 % \end{macro}
5826 % \end{macro}
5827 % \end{macro}
5828 %
5829 % \subsection{Utilities}
5830 %
5831 % \begin{macro}{\@@_check_definable:nNT, \@@_check_definable_aux:nN}
5832 % Check that a token list is appropriate as a first argument of
5833 % \cs{NewDocumentCommand} and similar functions and otherwise
5834 % produce an error. First trim whitespace to allow for spaces around
5835 % the actual command to be defined. If the result has multiple
5836 % tokens, it is not a valid argument. The single token is a control
5837 % sequence exactly if its string representation has more than one
5838 % character (using \cs{token_to_str:N} rather than \cs{tl_to_str:n}
5839 % to avoid problems with macro parameter characters, and setting
5840 % \cs{tex_escapechar:D} to prevent it from being non-printable).
5841 % Finally, check for an active character: this is done by lowercasing
5842 % the token to fix its character code (arbitrarily to that of~|?|)
5843 % and comparing the result to an active~|?|. Both control sequences
5844 % and active characters are valid arguments, and non-active character
5845 % tokens are not. In all cases, the group opened to keep assignments
5846 % local must be closed.
5847 % \begin{macrocode}
5848 \cs_new_protected:Npn \@@_check_definable:nNT #1
5849 { \tl_trim_spaces_apply:nN {#1} \@@_check_definable_aux:nN }
5850 \group_begin:
5851 \char_set_catcode_active:n { `? }
5852 \cs_new_protected:Npn \@@_check_definable_aux:nN #1#2
5853 {
5854 \group_begin:
5855 \tl_if_single_token:nTF {#1}
5856 {
5857 \int_set:Nn \tex_escapechar:D { 92 }
5858 \exp_args:Ne \tl_if_empty:nTF
5859 { \exp_args:No \str_tail:n { \token_to_str:N #1 } }
5860 {
5861 \exp_args:Ne \char_set_lccode:nn
5862 { ` \str_head:n {#1} } { `? }
5863 \tex_lowercase:D { \tl_if_eq:nnTF {#1} } { ? }
5864 { \group_end: \use_iii:nnn }
5865 { \group_end: \use_i:nnn }
5866 }
5867 { \group_end: \use_iii:nnn }
5868 }
5869 { \group_end: \use_ii:nnn }
5870 {
5871 \msg_error:nnee { cmd } { not-definable }
5872 { \tl_to_str:n {#1} } { \token_to_str:N #2 }
5873 }
5874 {
5875 \msg_error:nnee { cmd } { not-one-token }
5876 { \tl_to_str:n {#1} } { \token_to_str:N #2 }
5877 }
5878 }
5879 \group_end:
5880 % \end{macrocode}
5881 % \end{macro}
5882 %
5883 % \begin{macro}{\@@_token_if_cs:NTF}
5884 % Based on the definition of \cs{@@_check_definable_aux:nN} above, but
5885 % only checks for an actual control sequence (\emph{i.e.},
5886 % \cs[no-index]{\meta{anything}}). \cs{tex_escapechar:D} is
5887 % temporarily changed to a known value and then it checks if
5888 % |\string#1| contains more than one character: if it does, it's a
5889 % control sequence. This test differs from \cs{token_if_cs:NTF} for
5890 % example in \verb|\token_if_cs:NTF \c_group_begin_token {T}{F}|,
5891 % where \cs{token_if_cs:NTF} returns false.
5892 % \begin{macrocode}
5893 \cs_new_protected:Npn \@@_token_if_cs:NTF #1
5894 {
5895 \group_begin:
5896 \int_set:Nn \tex_escapechar:D { 92 }
5897 \exp_args:Ne \tl_if_empty:nTF
5898 { \exp_args:No \str_tail:n { \token_to_str:N #1 } }
5899 { \group_end: \use_ii:nn }
5900 { \group_end: \use_i:nn }
5901 }
5902 % \end{macrocode}
5903 % \end{macro}
5904 %
5905 % \begin{macro}{\@@_tl_mapthread_function:NNN, \@@_tl_mapthread_function:nnN}
5906 % \begin{macro}{\@@_tl_mapthread_loop:w}
5907 % Analogue of \cs{seq_mapthread_function:NNN} for token lists.
5908 % \begin{macrocode}
5909 \cs_new:Npn \@@_tl_mapthread_function:NNN #1#2#3
5910 {
5911 \exp_after:wN \exp_after:wN
5912 \exp_after:wN \@@_tl_mapthread_loop:w
5913 \exp_after:wN \exp_after:wN
5914 \exp_after:wN #3
5915 \exp_after:wN #1
5916 \exp_after:wN \q_recursion_tail
5917 \exp_after:wN \q_mark
5918 #2
5919 \q_recursion_tail
5920 \q_recursion_stop
5921 }
5922 \cs_new:Npn \@@_tl_mapthread_function:nnN #1#2#3
5923 {
5924 \@@_tl_mapthread_loop:w #3
5925 #1 \q_recursion_tail \q_mark
5926 #2 \q_recursion_tail \q_recursion_stop
5927 }
5928 \cs_new:Npn \@@_tl_mapthread_loop:w #1#2#3 \q_mark #4
5929 {
5930 \quark_if_recursion_tail_stop:n {#2}
5931 \quark_if_recursion_tail_stop:n {#4}
5932 #1 {#2} {#4}
5933 \@@_tl_mapthread_loop:w #1#3 \q_mark
5934 }
5935 % \end{macrocode}
5936 % \end{macro}
5937 % \end{macro}
5938 %
5939 % \begin{macro}{\__kernel_cmd_if_xparse:NTF}
5940 % \changes{v1.3g}{2025/10/02}{Remove protected status}
5941 % \begin{macro}{\@@_cmd_type_cases:NnnnnnF}
5942 % \changes{v1.0d}{2021/04/19}{Renamed \cs{__cmd_cmd_if_xparse:NTF} to
5943 % \cs{__kernel_cmd_if_xparse:NTF} for cross-module usage}
5944 % \changes{v1.0d}{2021/07/30}{Added \cs{__cmd_cmd_type_cases:NnnnnF} for
5945 % \cs{NewCommandCopy} and \cs{ShowCommand} support}
5946 % \changes{v1.0l}{2022/03/18}{Fix \cs{__cmd_cmd_type_cases:NnnnnF}
5947 % prematurely expanding macros (gh/795)}
5948 % \changes{v1.2b}{2023/12/01}{Extend for optimized commands}
5949 % \changes{v1.2e}{2024/04/17}{Use \cs{__kernel_cs_parameter_spec:N} instead
5950 % of \cs{cs_argument_spec:N}/\cs{cs_parameter_spec:N}}
5951 % \changes{v1.3g}{2025/10/02}{Remove protected status}
5952 % \begin{macro}{\@@_cmd_if_xparse_aux:N}
5953 %
5954 % To determine whether the command is an \pkg{xparse} command check
5955 % that its |arg_spec| is empty (this also excludes non-macros) and
5956 % that its |replacement_spec| starts with either
5957 % \cs{@@_start:nNNnnn} (non-expandable command) or
5958 % \cs{@@_start_expandable:nNNNNn} (expandable command) or
5959 % \cs{@@_start_optimized:} (optimized command) or
5960 % \cs{@@_start_env:nnnnn} (environment) or
5961 % \cs[no-index]{environment~\#1~end~aux} (environment end).
5962 %
5963 % This conditional is needed in several kernel modules and is
5964 % therefore has a kernel-internal name.
5965 % \begin{macrocode}
5966 \cs_new:Npn \@@_cmd_type_cases:NnnnnnF #1 #2 #3 #4 #5 #6 #7
5967 {
5968 \exp_args:Ne \str_case_e:nnF
5969 {
5970 \exp_args:Nf \tl_if_empty:nT { \__kernel_cs_parameter_spec:N #1 }
5971 { \exp_not:N \exp_not:n { \exp_not:e { \tl_head:N #1 } } }
5972 }
5973 {
5974 { \exp_not:N \@@_start:nNNnnn } {#2}
5975 { \exp_not:N \@@_start_expandable:nNNNNn } {#3}
5976 { \exp_not:N \@@_start_optimized: } {#4}
5977 { \exp_not:N \@@_start_env:nnnnn } {#5}
5978 {
5979 \exp_after:wN \exp_not:N
5980 \cs:w environment~
5981 \exp_last_unbraced:Ne \use_none:nnn
5982 { \cs_to_str:N #1 } ~end~aux \cs_end:
5983 } {#6}
5984 }
5985 {#7}
5986 }
5987 \cs_new:Npn \__kernel_cmd_if_xparse:NTF #1
5988 {
5989 \@@_cmd_type_cases:NnnnnnF #1
5990 { } { } { } { } { } { \use_iii:nnn }
5991 \use_i:nn
5992 }
5993 % \end{macrocode}
5994 % \end{macro}
5995 % \end{macro}
5996 % \end{macro}
5997 %
5998 % \begin{macro}{\@@_peek_nonspace:NTF, \@@_peek_nonspace_remove:NTF, \@@_peek_nonspace_aux:nNNTF}
5999 % Collect spaces in a loop, and put the collected spaces back in the
6000 % false branch of a call to \cs{peek_meaning:NTF} or
6001 % \cs{peek_meaning_remove:NTF}.
6002 % \begin{macrocode}
6003 \cs_new_protected:Npn \@@_peek_nonspace:NTF
6004 { \@@_peek_nonspace_aux:nNNTF { } \@@_peek_meaning:NTF }
6005 \cs_new_protected:Npn \@@_peek_nonspace_remove:NTF
6006 { \@@_peek_nonspace_aux:nNNTF { } \@@_peek_meaning_remove:NTF }
6007 \cs_new_protected:Npn \@@_peek_nonspace_aux:nNNTF #1#2#3#4#5
6008 {
6009 \peek_meaning_remove:NTF \c_space_token
6010 { \@@_peek_nonspace_aux:nNNTF { #1 ~ } #2 #3 {#4} {#5} }
6011 { #2 #3 { #4 } { #5 #1 } }
6012 }
6013 % \end{macrocode}
6014 % \end{macro}
6015 %
6016 % \begin{macro}{\@@_peek_meaning:NTF, \@@_peek_meaning_remove:NTF}
6017 % \begin{macro}{\@@_peek_cs_check_equal:NNN, \@@_peek_meaning_aux:NNTF, \@@_peek_true_remove:NNw}
6018 % Peek ahead for a token with a given meaning. In case the search
6019 % token is a control sequence, also check that the \meta{csname} is
6020 % the same as the control sequence peeked at. This extra verification
6021 % is necessary when the command is delimited by control sequence tokens
6022 % (as opposed to character tokens), and we want the exact same
6023 % control sequence to match.
6024 % \begin{macrocode}
6025 \cs_new_protected:Npn \@@_peek_meaning:NTF
6026 { \@@_peek_meaning_aux:NNTF \c_false_bool }
6027 \cs_new_protected:Npn \@@_peek_meaning_remove:NTF
6028 { \@@_peek_meaning_aux:NNTF \c_true_bool }
6029 \cs_new_protected:Npn \@@_peek_meaning_aux:NNTF #1#2#3#4
6030 {
6031 \tl_set:Nn \l_@@_tmpa_tl {#3}
6032 \tl_set:Nn \l_@@_tmpb_tl {#4}
6033 \peek_meaning:NTF #2
6034 {
6035 \token_if_eq_meaning:NNTF #2 \c_group_begin_token
6036 { \@@_peek_true_remove:Nw #1 }
6037 {
6038 \@@_token_if_cs:NTF #2
6039 { \@@_peek_cs_check_equal:NNN #1 #2 }
6040 { \@@_peek_true_remove:Nw #1 }
6041 }
6042 }
6043 { \l_@@_tmpb_tl }
6044 }
6045 \cs_new_protected:Npn \@@_peek_cs_check_equal:NNN #1#2#3
6046 {
6047 \str_if_eq:nnTF {#2} {#3}
6048 { \@@_peek_true_remove:Nw #1 }
6049 { \l_@@_tmpb_tl }
6050 #3
6051 }
6052 \cs_new_protected:Npn \@@_peek_true_remove:Nw #1
6053 {
6054 \bool_if:NTF #1
6055 {
6056 \tex_afterassignment:D \l_@@_tmpa_tl
6057 \cs_set_eq:NN \@@_tmp:w
6058 }
6059 { \l_@@_tmpa_tl }
6060 }
6061 % \end{macrocode}
6062 % \end{macro}
6063 % \end{macro}
6064 %
6065 % \subsection{Access to the argument specification}
6066 %
6067 % \begin{macro}[EXP]{\cmd_arg_spec:N, \cmd_arg_spec:c}
6068 % \changes{v1.3g}{2025/10/02}{New function}
6069 % \begin{macro}[EXP]{\@@_arg_spec_opt:N}
6070 % \changes{v1.3g}{2025/10/02}{New function}
6071 % First we check that the command passed does exist, and is one with the
6072 % correct form. It if does then there are two cases. For optimised commands,
6073 % we can reconstruct the arg.~spec.\ from the \TeX{} parameter spec, taking
6074 % care to add |+| if required. For the non-optimized cases, the
6075 % arg.~spec.\ is stored in the top-level macro. We treat this as a |tl| and
6076 % extract the appropriate balanced text: the second item.
6077 % \begin{macrocode}
6078 \cs_new:Npn \cmd_arg_spec:N #1
6079 {
6080 \cs_if_exist:NTF #1
6081 {
6082 \__kernel_cmd_if_xparse:NTF #1
6083 {
6084 \exp_args:No \tl_if_head_eq_meaning:nNTF #1
6085 \@@_start_optimized:
6086 {
6087 \exp_args:Nc \@@_arg_spec_opt:N
6088 { \cs_to_str:N #1 \c_space_tl code }
6089 }
6090 { \tl_item:Nn #1 { 2 } }
6091 }
6092 { X }
6093 }
6094 { X }
6095 }
6096 \cs_generate_variant:Nn \cmd_arg_spec:N { c }
6097 \cs_new:Npn \@@_arg_spec_opt:N #1
6098 {
6099 \prg_replicate:nn { \str_count:e { \cs_parameter_spec:N #1 } / 2 }
6100 {
6101 \bool_lazy_or:nnT
6102 { \token_if_long_macro_p:N #1 }
6103 { \token_if_protected_long_macro_p:N #1 }
6104 { + }
6105 m
6106 }
6107 }
6108 % \end{macrocode}
6109 % \end{macro}
6110 % \end{macro}
6111 %
6112 % \subsection{Messages}
6113 %
6114 % \begin{variable}{\c_@@_ignore_def_tl}
6115 % \begin{macrocode}
6116 \tl_const:Nn \c_@@_ignore_def_tl
6117 { \\ \\ LaTeX~will~ignore~this~entire~definition. }
6118 % \end{macrocode}
6119 % \end{variable}
6120 %
6121 % \begin{macro}{\@@_environment_or_command:}
6122 % Two texts used in several messages.
6123 % \begin{macrocode}
6124 \cs_new:Npn \@@_environment_or_command:
6125 {
6126 \bool_if:NTF \l_@@_environment_bool
6127 { environment ~ ' \l_@@_environment_str ' }
6128 {
6129 command ~
6130 ' \c_backslash_str \tl_to_str:N \l_@@_function_tl '
6131 }
6132 }
6133 % \end{macrocode}
6134 % \end{macro}
6135 %
6136 % Some messages intended as errors when defining commands/environments.
6137 % \changes{v1.0f}{2021/06/04}{Normalize various error messages}
6138 % \changes{v1.2c}{2023/12/22}
6139 % {Generalize message \texttt{invalid-bang} (gh/1198)}
6140 % \changes{v1.3a}{2025-01-23}{Generalize message \texttt{arg-after-body}}
6141 % \begin{macrocode}
6142 \msg_new:nnnn { cmd } { arg-after-body }
6143 { Argument~type~'#1'~must~be~last~in~#2. }
6144 {
6145 The~'#1'~argument~type~must~come~last~but~it~is~followed~
6146 by~'#3'~in~the~argument~specification.~This~is~not~allowed.
6147 \c_@@_ignore_def_tl
6148 }
6149 \msg_new:nnnn { cmd } { bad-arg-spec }
6150 { Bad~argument~specification~'#2'~for~#1. }
6151 {
6152 The~argument~specification~provided~is~not~valid:~
6153 one~or~more~mandatory~parts~are~missing.
6154 \c_@@_ignore_def_tl
6155 }
6156 \msg_new:nnnn { cmd } { already-defined }
6157 { Command~'#1'~already~defined. }
6158 {
6159 You~have~used~#2~
6160 with~a~command~that~already~has~a~definition. \\ \\
6161 The~existing~definition~of~'#1'~will~not~be~altered.
6162 }
6163 \msg_new:nnnn { cmd } { undefined }
6164 { Command ~'#1'~undefined. }
6165 {
6166 You~have~used~#2~
6167 with~a~command~that~was~never~defined.
6168 \c_@@_ignore_def_tl
6169 }
6170 % \end{macrocode}
6171 % \changes{v1.3a}{2025-01-23}{New message \texttt{chars-dropped-first-line}}
6172 % \changes{v1.3a}{2025-01-23}{New message \texttt{chars-dropped-last-line}}
6173 % \begin{macrocode}
6174 \msg_new:nnnn { cmd } { chars-dropped-first-line }
6175 { Characters~'#1'~dropped~on~first~line~of~#2~environment. }
6176 {
6177 LaTeX~was~collecting~a~verbatim-like~environment,~and~the~characters~
6178 '#1'~were~found~after~'\begin{#2}~on~the~first~line:~this~is~not~supported.
6179 }
6180 \msg_new:nnnn { cmd } { chars-dropped-last-line }
6181 { Characters~'#1'~dropped~after~end~of~#2~environment. }
6182 {
6183 LaTeX~was~collecting~a~verbatim-like~environment,~and~the~characters~
6184 '#1'~were~found~after~\end{#2}~on~the~last~line:~this~is~not~supported.
6185 }
6186 \msg_new:nnnn { cmd } { env-already-defined }
6187 { Environment~'#1'~already~defined. }
6188 {
6189 You~have~used~\NewDocumentEnvironment
6190 with~an~environment~that~already~has~a~definition. \\ \\
6191 The~existing~definition~of~'#1'~will~not~be~altered.
6192 }
6193 \msg_new:nnnn { cmd } { env-end-already-defined }
6194 { End~of~environment~'#1'~already~defined. }
6195 {
6196 You~have~used~\NewDocumentEnvironment
6197 with~an~environment~that~already~has~a~definition~for~'end#1'. \\ \\
6198 The~existing~definition~of~'#1'~will~not~be~altered.
6199 }
6200 \msg_new:nnnn { cmd } { env-undefined }
6201 { Environment~'#1'~undefined. }
6202 {
6203 You~have~used~\RenewDocumentEnvironment
6204 with~an~environment~that~was~never~defined.
6205 \c_@@_ignore_def_tl
6206 }
6207 \msg_new:nnnn { cmd } { expandable-ending-optional }
6208 { Bad~argument~specification~'#2'~for~#1. }
6209 {
6210 Expandable~commands~must~have~a~final~mandatory~argument~
6211 (or~no~arguments~at~all).~You~cannot~have~a~terminal~optional~
6212 argument~with~expandable~commands.
6213 }
6214 \msg_new:nnnn { cmd } { long-short-mix }
6215 { Invalid~argument~prefix~'+'~in~command~'#1'. }
6216 {
6217 The~arguments~for~an~expandable~command~must~not~involve~short~
6218 arguments~after~long~arguments.~You~have~tried~to~mix~the~two~types~
6219 when~defining~'#1'.
6220 }
6221 \msg_new:nnnn { cmd } { invalid-command-arg }
6222 { Invalid~argument~type~'#2'~in~#1. }
6223 {
6224 The~letter~'#2'~can~only~be~used~in~environment~argument~
6225 specifications,~but~not~for~commands.
6226 \\ \\
6227 LaTeX~will~ignore~the~entire~definition.
6228 }
6229 \msg_new:nnnn { cmd } { invalid-expandable-arg }
6230 { Invalid~argument~type~'#2'~in~#1. }
6231 {
6232 The~letter~'#2'~specifies~an~argument~type~which~cannot~be~used~
6233 in~an~expandable~command.
6234 \c_@@_ignore_def_tl
6235 }
6236 \msg_new:nnnn { cmd } { invalid-after-optional-expandably }
6237 { Argument~'#2'~invalid~after~optional~arg~in~#1. }
6238 {
6239 The~letter~'#2'~specifies~an~argument~type~which~cannot~be~used~
6240 in~an~expandable~command~after~an~optional~argument.
6241 \c_@@_ignore_def_tl
6242 }
6243 \msg_new:nnnn { cmd } { invalid-bang }
6244 { Invalid~argument~prefix~'!'~in~#1. }
6245 {
6246 The~prefix~'!'~is~only~allowed~for~trailing~optional~arguments.~
6247 You~tried~to~apply~it~to~#2.
6248 \c_@@_ignore_def_tl
6249 }
6250 \msg_new:nnnn { cmd } { not-definable }
6251 { First~argument~of~'#2'~must~be~a~command. }
6252 {
6253 The~first~argument~of~'#2'~should~be~the~document~command~that~will~
6254 be~defined.~The~provided~argument~'#1'~is~a~character.~Perhaps~a~
6255 backslash~is~missing?
6256 \c_@@_ignore_def_tl
6257 }
6258 \msg_new:nnnn { cmd } { not-one-token }
6259 { First~argument~of~'#2'~must~be~a~command. }
6260 {
6261 The~first~argument~of~'#2'~should~be~the~document~command~that~will~
6262 be~defined.~The~provided~argument~'#1'~contains~more~than~one~
6263 token.~Perhaps~a~backslash~is~missing?
6264 \c_@@_ignore_def_tl
6265 }
6266 \msg_new:nnnn { cmd } { not-single-token }
6267 { Argument~delimiter~'#2'~invalid~in~#1. }
6268 {
6269 The~argument~specification~contains~
6270 \tl_if_empty:nTF{#2}{nothing}{'#2'}~
6271 in~a~place~
6272 where~a~single~token~is~required.
6273 \c_@@_ignore_def_tl
6274 }
6275 \msg_new:nnnn { cmd } { forbidden-group-token }
6276 { Argument~delimiter~'#2'~invalid~in~#1. }
6277 {
6278 The~argument~specification~contains~the~implicit~
6279 #3-group~token~'#2'~which~is~not~allowed~as~an~argument~delimiter.
6280 \c_@@_ignore_def_tl
6281 }
6282 \msg_new:nnnn { cmd } { processor-in-expandable }
6283 { Invalid~argument~prefix~'>'~in~command~'#1'. }
6284 {
6285 The~argument~specification~for~'#1'~contains~the~processor~function~'>{#2}'.~
6286 This~is~only~supported~for~robust~commands,~but~not~for~expandable~ones.
6287 \c_@@_ignore_def_tl
6288 }
6289 \msg_new:nnnn { cmd } { keyval-in-expandable }
6290 { Invalid~argument~prefix~'='~in~command~'#1'. }
6291 {
6292 The~argument~specification~for~'#1'~contains~a~key--value~marker~'={#2}'.~
6293 This~is~only~supported~for~robust~commands,~but~not~for~expandable~ones.
6294 \c_@@_ignore_def_tl
6295 }
6296 \msg_new:nnnn { cmd } { too-many-args }
6297 { Too~many~arguments~for~#1. }
6298 {
6299 The~argument~specification~'#2'~asks~for~more~than~9~arguments.~
6300 This~cannot~be~implemented.
6301 \c_@@_ignore_def_tl
6302 }
6303 \msg_new:nnnn { cmd } { two-markers }
6304 { Invalid~argument~prefix~'#2'~in~#1. }
6305 {
6306 The~argument~specification~provided~for~#1~has~two~'#2'~markers~applied~
6307 to~the~same~argument;~one~is~redundant.
6308 }
6309 \msg_new:nnnn { cmd } { unknown-argument-type } % should be unkown-arg-type but dep in xparse
6310 { Invalid~argument~type~'#2'~in~#1. }
6311 {
6312 The~letter~'#2'~does~not~specify~a~known~argument~type.
6313 \c_@@_ignore_def_tl
6314 }
6315 \msg_new:nnnn { cmd } { xparse-arg-type }
6316 { Invalid~argument~type~'#2'~in~#1~(requires~xparse). }
6317 {
6318 The~letter~'#2'~specifies~a~known~but~deprecated~argument~type.~
6319 If~you~really~need~it~you~have~to~load~the~xparse~package.
6320 \c_@@_ignore_def_tl
6321 }
6322 % \end{macrocode}
6323 %
6324 % Errors when using commands/environments. The \texttt{if-boolean}
6325 % message is always used in expandable errors. The
6326 % \texttt{default-loop} and \texttt{missing-required} messages can
6327 % be expandable or not expandable.
6328 % \begin{macrocode}
6329 \msg_new:nnn { cmd } { if-boolean }
6330 { Invalid~argument~{#1}~to~\iow_char:N\\IfBoolean... }
6331 \msg_new:nnnn { cmd } { default-loop }
6332 { Circular~dependency~in~defaults~of~#1. }
6333 {
6334 The~default~values~of~two~or~more~arguments~of~the~#1~
6335 depend~on~each~other~in~a~way~that~cannot~be~resolved.
6336 }
6337 \msg_new:nnnn { cmd } { missing-required }
6338 { Required~argument~missing~for~#1. }
6339 {
6340 The~#1~expects~one~of~its~arguments~to~start~with~'#2'.~
6341 LaTeX~did~not~find~this~argument~and~will~insert~a~default~value~
6342 for~further~processing.
6343 }
6344 \msg_new:nnnn { cmd } { arg-split }
6345 { Too~many~'#1'~separators~in~argument. }
6346 {
6347 LaTeX~was~asked~to~split~the~input~'#3'~
6348 at~each~occurrence~of~the~separator~'#1'~into~#2~parts.~
6349 Too~many~separators~were~found.
6350 }
6351 \msg_new:nnnn { cmd } { verbatim-nl }
6352 { Verbatim-like~#1~ended~by~end~of~line. }
6353 {
6354 The~verbatim~argument~of~the~#1~cannot~contain~more~than~one~line,~
6355 but~the~end~
6356 of~the~current~line~has~been~reached.~You~may~have~forgotten~the~
6357 closing~delimiter.
6358 \\ \\
6359 LaTeX~will~ignore~'#2'~and~you~may~get~some~additional~
6360 (low-level)~errors.
6361 }
6362 \msg_new:nnnn { cmd } { verbatim-tokenized }
6363 { Verbatim-like~#1~illegal~in~argument. }
6364 {
6365 The~#1~takes~a~verbatim~argument~and~should~therefore~normally~
6366 not~be~used~in~arguments~of~other~commands~or~environments.~
6367 LaTeX~found~an~illegal~token~ \tl_if_empty:nF {#3} { (#3)~ }
6368 after~'#2'~and~will~drop~everything~up~to~this~point.
6369 \\ \\
6370 Expect~further~(low-level)~errors.
6371 }
6372 % \end{macrocode}
6373 %
6374 % Intended more for information.
6375 % \begin{macrocode}
6376 \msg_new:nnn { cmd } { define-command } % should be just `define' but dep in xparse
6377 {
6378 Defining~command~#1~
6379 with~sig.~'#2'~\msg_line_context:.
6380 }
6381 \msg_new:nnn { cmd } { define-env }
6382 {
6383 Defining~environment~'#1'~
6384 with~sig.~'#2'~\msg_line_context:.
6385 }
6386 \msg_new:nnn { cmd } { redefine }
6387 {
6388 Redefining~command~#1~
6389 with~sig.~'#2'~\msg_line_context:.
6390 }
6391 \msg_new:nnn { cmd } { redefine-env }
6392 {
6393 Redefining~environment~'#1'~
6394 with~sig.~'#2'~\msg_line_context:.
6395 }
6396 \msg_new:nnn { cmd } { optional-mandatory }
6397 {
6398 Optional~and~mandatory~argument~with~same~delimiter~'#2'.
6399 \\ \\
6400 The~mandatory~argument~specified~with~
6401 '\str_case:nnF{#1}{ {R/r}{r'~or~'R} }{#1}'~has~the~
6402 same~delimiter~'#2'~as~an~earlier~optional~argument.~
6403 It~will~therefore~not~be~possible~to~omit~all~the~earlier~
6404 optional~arguments~when~calling~this~command.
6405 \\ \\
6406 This~may~be~intentional,~but~then~it~might~be~a~mistake.
6407 }
6408 \msg_new:nnn { cmd } { unsupported-let }
6409 {
6410 The~command~'#1'~was~undefined~but~not~the~associated~commands~
6411 '#1~code'~and/or~'#1~defaults'.~Maybe~you~tried~using~
6412 \iow_char:N\\let.~This~may~lead~to~an~infinite~loop.
6413 }
6414 % \end{macrocode}
6415 %
6416 % \subsection{User functions}
6417 %
6418 % The user functions are more or less just the internal functions
6419 % renamed.
6420 %
6421 % \begin{macro}{\BooleanFalse}
6422 % \begin{macro}{\BooleanTrue}
6423 % Design-space names for the Boolean values.
6424 % \begin{macrocode}
6425 \cs_new_eq:NN \BooleanFalse \c_false_bool
6426 \cs_new_eq:NN \BooleanTrue \c_true_bool
6427 % \end{macrocode}
6428 % \end{macro}
6429 % \end{macro}
6430 %
6431 % \begin{macro}{\NewDocumentCommand}
6432 % \begin{macro}{\RenewDocumentCommand}
6433 % \begin{macro}{\ProvideDocumentCommand}
6434 % \begin{macro}{\DeclareDocumentCommand}
6435 % The user macros are pretty simple wrappers around the internal ones.
6436 % There is however a check that the first argument is a single token,
6437 % possibly surrounded by spaces (hence the strange \cs{use:nnn}), and
6438 % is definable.
6439 % \begin{macrocode}
6440 \cs_new_protected:Npn \NewDocumentCommand #1#2#3
6441 {
6442 \@@_check_definable:nNT {#1} \NewDocumentCommand
6443 {
6444 \cs_if_exist:NTF #1
6445 {
6446 \msg_error:nnee { cmd } { already-defined }
6447 { \use:nnn \token_to_str:N #1 { } }
6448 { \token_to_str:N \NewDocumentCommand }
6449 }
6450 { \@@_declare_cmd:Nnn #1 {#2} {#3} }
6451 }
6452 }
6453 \cs_new_protected:Npn \RenewDocumentCommand #1#2#3
6454 {
6455 \@@_check_definable:nNT {#1} \RenewDocumentCommand
6456 {
6457 \cs_if_exist:NTF #1
6458 { \@@_declare_cmd:Nnn #1 {#2} {#3} }
6459 {
6460 \msg_error:nnee { cmd } { undefined }
6461 { \use:nnn \token_to_str:N #1 { } }
6462 { \token_to_str:N \RenewDocumentCommand }
6463 }
6464 }
6465 }
6466 \cs_new_protected:Npn \ProvideDocumentCommand #1#2#3
6467 {
6468 \@@_check_definable:nNT {#1} \ProvideDocumentCommand
6469 { \cs_if_exist:NF #1 { \@@_declare_cmd:Nnn #1 {#2} {#3} } }
6470 }
6471 \cs_new_protected:Npn \DeclareDocumentCommand #1#2#3
6472 {
6473 \@@_check_definable:nNT {#1} \DeclareDocumentCommand
6474 { \@@_declare_cmd:Nnn #1 {#2} {#3} }
6475 }
6476 % \end{macrocode}
6477 % \end{macro}
6478 % \end{macro}
6479 % \end{macro}
6480 % \end{macro}
6481 %
6482 % \begin{macro}{\NewDocumentEnvironment}
6483 % \changes{v1.0h}{2021/08/27}{Check for end-of-environment command}
6484 % \changes{v1.2f}{2024/07/06}{Trim spaces from envname first (gh/1399)}
6485 % \begin{macro}{\RenewDocumentEnvironment}
6486 % \begin{macro}{\ProvideDocumentEnvironment}
6487 % \begin{macro}{\DeclareDocumentEnvironment}
6488 % \begin{macro}{\@@_new_env:nnnn, \@@_renew_env:nnnn, \@@_provide_env:nnnn}
6489 % \begin{macro}{\@@_new_env:ennn, \@@_renew_env:ennn, \@@_provide_env:ennn}
6490 % Very similar for environments. Trim spaces from user-specified
6491 % \meta{envname}, do existence check then hand off to
6492 % \cs{@@_declare_env:nnnn}.
6493 % \begin{macrocode}
6494 %<latexrelease>\IncludeInRelease{2024/11/01}{\NewDocumentEnvironment}%
6495 %<latexrelease> {Trim~spaces~from~envname~first}
6496 \cs_new_protected:Npn \NewDocumentEnvironment #1#2#3#4
6497 {
6498 \@@_new_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
6499 }
6500 \cs_new_protected:Npn \RenewDocumentEnvironment #1#2#3#4
6501 {
6502 \@@_renew_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
6503 }
6504 \cs_new_protected:Npn \ProvideDocumentEnvironment #1#2#3#4
6505 {
6506 \@@_provide_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
6507 }
6508 \cs_new_protected:Npn \DeclareDocumentEnvironment #1#2#3#4
6509 {
6510 \@@_declare_env:ennn { \tl_trim_spaces:e {#1} } {#2} {#3} {#4}
6511 }
6512 % \end{macrocode}
6513 % Each of \cs[no-index]{@@_(new|renew|provide)_env:nnnn} is curried.
6514 % \begin{macrocode}
6515 \cs_new_protected:Npn \@@_new_env:nnnn #1
6516 {
6517 \cs_if_exist:cTF {#1}
6518 {
6519 \msg_error:nne { cmd } { env-already-defined } {#1}
6520 \use_none:nnn
6521 }
6522 {
6523 \cs_if_exist:cTF { end #1 }
6524 {
6525 \msg_error:nne { cmd } { env-end-already-defined } {#1}
6526 \use_none:nnn
6527 }
6528 { \@@_declare_env:nnnn {#1} }
6529 }
6530 }
6531 \cs_new_protected:Npn \@@_renew_env:nnnn #1
6532 {
6533 \cs_if_exist:cTF {#1}
6534 { \@@_declare_env:nnnn {#1} }
6535 {
6536 \msg_error:nne { cmd } { env-undefined } {#1}
6537 \use_none:nnn
6538 }
6539 }
6540 \cs_new_protected:Npn \@@_provide_env:nnnn #1
6541 {
6542 \cs_if_exist:cTF {#1}
6543 { \use_none:nnn }
6544 { \@@_declare_env:nnnn {#1} }
6545 }
6546 \cs_generate_variant:Nn \@@_new_env:nnnn { e }
6547 \cs_generate_variant:Nn \@@_renew_env:nnnn { e }
6548 \cs_generate_variant:Nn \@@_provide_env:nnnn { e }
6549 %<latexrelease>\EndIncludeInRelease
6550 %<latexrelease>\IncludeInRelease{2024/06/01}{\NewDocumentEnvironment}%
6551 %<latexrelease> {Trim~spaces~from~envname~first}
6552 %<latexrelease>\cs_new_protected:Npn \NewDocumentEnvironment #1#2#3#4
6553 %<latexrelease> {
6554 %<latexrelease> \cs_if_exist:cTF {#1}
6555 %<latexrelease> { \msg_error:nnx { cmd } { env-already-defined } {#1} }
6556 %<latexrelease> {
6557 %<latexrelease> \cs_if_exist:cTF { end #1 }
6558 %<latexrelease> { \msg_error:nnx { cmd } { env-end-already-defined } {#1} }
6559 %<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
6560 %<latexrelease> }
6561 %<latexrelease> }
6562 %<latexrelease>\cs_new_protected:Npn \RenewDocumentEnvironment #1#2#3#4
6563 %<latexrelease> {
6564 %<latexrelease> \cs_if_exist:cTF {#1}
6565 %<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
6566 %<latexrelease> { \msg_error:nnx { cmd } { env-undefined } {#1} }
6567 %<latexrelease> }
6568 %<latexrelease>\cs_new_protected:Npn \ProvideDocumentEnvironment #1#2#3#4
6569 %<latexrelease> { \cs_if_exist:cF {#1} { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} } }
6570 %<latexrelease>\cs_new_protected:Npn \DeclareDocumentEnvironment #1#2#3#4
6571 %<latexrelease> { \@@_declare_env:nnnn {#1} {#2} {#3} {#4} }
6572 %<latexrelease>\cs_undefine:N \@@_new_env:nnnn
6573 %<latexrelease>\cs_undefine:N \@@_new_env:ennn
6574 %<latexrelease>\cs_undefine:N \@@_renew_env:nnnn
6575 %<latexrelease>\cs_undefine:N \@@_renew_env:ennn
6576 %<latexrelease>\cs_undefine:N \@@_provide_env:nnnn
6577 %<latexrelease>\cs_undefine:N \@@_provide_env:ennn
6578 %<latexrelease>
6579 %<latexrelease>\EndIncludeInRelease
6580 % \end{macrocode}
6581 % \end{macro}
6582 % \end{macro}
6583 % \end{macro}
6584 % \end{macro}
6585 % \end{macro}
6586 % \end{macro}
6587 %
6588 % \begin{macro}{\NewExpandableDocumentCommand}
6589 % \begin{macro}{\RenewExpandableDocumentCommand}
6590 % \begin{macro}{\ProvideExpandableDocumentCommand}
6591 % \begin{macro}{\DeclareExpandableDocumentCommand}
6592 % The expandable versions are essentially the same as the basic
6593 % functions. The strange \cs{use:nnn} is there in case |#1| is
6594 % surrounded with spaces, as can happen with usual document catcodes
6595 % in \cs{RenewExpandableDocumentCommand} |{| |\!| |}| \ldots{}
6596 % \begin{macrocode}
6597 \cs_new_protected:Npn \NewExpandableDocumentCommand #1#2#3
6598 {
6599 \@@_check_definable:nNT {#1} \NewExpandableDocumentCommand
6600 {
6601 \cs_if_exist:NTF #1
6602 {
6603 \msg_error:nnee { cmd } { already-defined }
6604 { \use:nnn \token_to_str:N #1 { } }
6605 { \token_to_str:N \NewExpandableDocumentCommand }
6606 }
6607 { \@@_declare_expandable_cmd:Nnn #1 {#2} {#3} }
6608 }
6609 }
6610 \cs_new_protected:Npn \RenewExpandableDocumentCommand #1#2#3
6611 {
6612 \@@_check_definable:nNT {#1} \RenewExpandableDocumentCommand
6613 {
6614 \cs_if_exist:NTF #1
6615 { \@@_declare_expandable_cmd:Nnn #1 {#2} {#3} }
6616 {
6617 \msg_error:nnee { cmd } { undefined }
6618 { \use:nnn \token_to_str:N #1 { } }
6619 { \token_to_str:N \RenewExpandableDocumentCommand }
6620 }
6621 }
6622 }
6623 \cs_new_protected:Npn \ProvideExpandableDocumentCommand #1#2#3
6624 {
6625 \@@_check_definable:nNT {#1} \ProvideExpandableDocumentCommand
6626 {
6627 \cs_if_exist:NF #1
6628 { \@@_declare_expandable_cmd:Nnn #1 {#2} {#3} }
6629 }
6630 }
6631 \cs_new_protected:Npn \DeclareExpandableDocumentCommand #1#2#3
6632 {
6633 \@@_check_definable:nNT {#1} \DeclareExpandableDocumentCommand
6634 { \@@_declare_expandable_cmd:Nnn #1 {#2} {#3} }
6635 }
6636 % \end{macrocode}
6637 % \end{macro}
6638 % \end{macro}
6639 % \end{macro}
6640 % \end{macro}
6641 %
6642 % \begin{macro}{\IfBooleanT, \IfBooleanF, \IfBooleanTF}
6643 % The logical \meta{true} and \meta{false} statements are just the
6644 % normal \cs{c_true_bool} and \cs{c_false_bool} so \cs{bool_if:NTF} is
6645 % almost enough. However, this code-level function blows up badly
6646 % when passed invalid input. We want \cs{IfBooleanTF} to accept a
6647 % single (non-space) token equal to \cs{c_true_bool} or
6648 % \cs{c_false_bool}, possibly surrounded by spaces. If the input is
6649 % blank or multiple items, jump to the error and pick the false
6650 % branch. If the input, ignoring spaces (we do this by omitting
6651 % braces in the \cs{tl_if_single_token:nF} test), is not a single
6652 % token then jump to the error as well. It is then safe to compare
6653 % the token to the two booleans, picking the appropriate branch. If
6654 % neither matches, we jump to the error as well.
6655 % \begin{macrocode}
6656 \cs_new:Npn \IfBooleanTF #1
6657 {
6658 \tl_if_single:nF {#1}
6659 { \prg_break:n { \use:n } }
6660 \tl_if_single_token:nF #1
6661 { \prg_break:n { \use:n } }
6662 \token_if_eq_meaning:NNT #1 \c_true_bool
6663 { \prg_break:n { \use_ii:nnn } }
6664 \token_if_eq_meaning:NNT #1 \c_false_bool
6665 { \prg_break:n { \use_iii:nnn } }
6666 \prg_break:n { \use:n }
6667 \prg_break_point:
6668 {
6669 \msg_expandable_error:nnn { cmd } { if-boolean } {#1}
6670 \use_ii:nn
6671 }
6672 }
6673 \cs_new:Npn \IfBooleanT #1#2 { \IfBooleanTF {#1} {#2} { } }
6674 \cs_new:Npn \IfBooleanF #1 { \IfBooleanTF {#1} { } }
6675 % \end{macrocode}
6676 % \end{macro}
6677 %
6678 % We adjust here as currently there is still an older version of \cs{NoValue}
6679 % in \pkg{l3tl}.
6680 %
6681 % \begin{macro}{\NoValue}
6682 % \changes{v1.3h}{2025-11-05}{New command}
6683 % A special marker
6684 % \begin{macrocode}
6685 \cs_new_protected:Npe \NoValue { -NoValue- }
6686 % \end{macrocode}
6687 % \end{macro}
6688 % \begin{variable}{\c_novalue_tl}
6689 % \changes{v1.3h}{2025-11-05}{Update for \cs{NoValue} adjustment}
6690 % \begin{macrocode}
6691 \cs_undefine:N \c_novalue_tl
6692 \tl_const:Nn \c_novalue_tl { \NoValue }
6693 % \end{macrocode}
6694 % \end{variable}
6695 % \begin{macro}[pTF, EXP]{\tl_if_novalue:n}
6696 % \changes{v1.3h}{2025-11-05}{Update for \cs{NoValue} adjustment}
6697 % \begin{macro}[EXP]{\@@_if_novalue_aux:w}
6698 % \changes{v1.3h}{2025-11-05}{Update for \cs{NoValue} adjustment}
6699 % The first argument of \cs{@@_if_novalue_aux:w} is empty if and only if |#1|
6700 % starts with \cs{NoValue}, while the second argument is empty if |#1| is
6701 % exactly \cs{NoValue} or if it has a question mark just following the
6702 % marker. In this second case, however, the material after the first |?!|
6703 % remains and makes the emptiness test return \texttt{false}.
6704 % \begin{macrocode}
6705 %<@@=tl>
6706 \prg_gset_conditional:Npnn \tl_if_novalue:n #1
6707 { p , T , F , TF }
6708 {
6709 \@@_if_empty_if:o
6710 { \@@_if_novalue_aux:w { } #1 { } ? ! \NoValue ? ? ! }
6711 \prg_return_true:
6712 \else:
6713 \prg_return_false:
6714 \fi:
6715 }
6716 \cs_gset:Npn \@@_if_novalue_aux:w #1 \NoValue #2 ? #3 ? ! { #1 #2 }
6717 %<@@=cmd>
6718 % \end{macrocode}
6719 % \end{macro}
6720 % \end{macro}
6721 %
6722 % \begin{macro}{\IfNoValueT, \IfNoValueF, \IfNoValueTF}
6723 % Simple re-naming.
6724 % \begin{macrocode}
6725 \cs_new_eq:NN \IfNoValueF \tl_if_novalue:nF
6726 \cs_new_eq:NN \IfNoValueT \tl_if_novalue:nT
6727 \cs_new_eq:NN \IfNoValueTF \tl_if_novalue:nTF
6728 % \end{macrocode}
6729 % \end{macro}
6730 % \begin{macro}{\IfValueT, \IfValueF, \IfValueTF}
6731 % Inverted logic.
6732 % \begin{macrocode}
6733 \cs_new:Npn \IfValueF { \tl_if_novalue:nT }
6734 \cs_new:Npn \IfValueT { \tl_if_novalue:nF }
6735 \cs_new:Npn \IfValueTF #1#2#3 { \tl_if_novalue:nTF {#1} {#3} {#2} }
6736 % \end{macrocode}
6737 % \end{macro}
6738 %
6739 % \begin{macro}{\IfBlankT, \IfBlankF, \IfBlankTF}
6740 % Another simple re-naming.
6741 % \changes{v1.0k}{2022/02/19}{Added \cs{IfBlankTF} and friends}
6742 % \begin{macrocode}
6743 %<latexrelease>\IncludeInRelease{2022/06/01}%
6744 %<latexrelease> {\IfBlankTF}{Testing~for~empty~or~blank}%
6745 \cs_new_eq:NN \IfBlankF \tl_if_blank:nF
6746 \cs_new_eq:NN \IfBlankT \tl_if_blank:nT
6747 \cs_new_eq:NN \IfBlankTF \tl_if_blank:nTF
6748 %<latexrelease>\EndIncludeInRelease
6749 %<latexrelease>\IncludeInRelease{2021/11/15}%
6750 %<latexrelease> {\IfBlankTF}{Testing~for~empty~or~blank}%
6751 %<latexrelease>\cs_undefine:N \IfBlankF
6752 %<latexrelease>\cs_undefine:N \IfBlankT
6753 %<latexrelease>\cs_undefine:N \IfBlankTF
6754 %<latexrelease>
6755 %<latexrelease>\EndIncludeInRelease
6756 % \end{macrocode}
6757 % \end{macro}
6758 %
6759 % \begin{macro}{\ProcessedArgument}
6760 % Processed arguments are returned using this name, which is reserved
6761 % here although the definition will change.
6762 % \begin{macrocode}
6763 \tl_new:N \ProcessedArgument
6764 % \end{macrocode}
6765 % \end{macro}
6766 %
6767 % \begin{macro}{\ReverseBoolean, \SplitArgument, \SplitList, \TrimSpaces}
6768 % Simple copies.
6769 % \begin{macrocode}
6770 \cs_new_eq:NN \ReverseBoolean \@@_bool_reverse:N
6771 \cs_new_eq:NN \SplitArgument \@@_split_argument:nnn
6772 \cs_new_eq:NN \SplitList \@@_split_list:nn
6773 \cs_new_eq:NN \TrimSpaces \@@_trim_spaces:n
6774 % \end{macrocode}
6775 % \end{macro}
6776 %
6777 % \begin{macro}{\ProcessList}
6778 % To support \cs{SplitList}.
6779 % \changes{v1.3b}{2025-02-01}
6780 % {Use \cs{tl_map_tokens:nn} instead of \cs{tl_map_function:nN}}
6781 % \begin{macrocode}
6782 \cs_new_eq:NN \ProcessList \tl_map_tokens:nn
6783 % \end{macrocode}
6784 % \end{macro}
6785 %
6786 % Finally as promised, restore \cs{__kernel_chk_if_free_cs:N}:
6787 % \begin{macrocode}
6788 %<latexrelease>\cs_gset_eq:NN \__kernel_chk_if_free_cs:N \@@_chk_if_free_cs:N
6789 %<latexrelease>\cs_undefine:N \@@_chk_if_free_cs:N
6790 % \end{macrocode}
6791 %
6792 % \begin{macrocode}
6793 %<latexrelease>
6794 %<latexrelease>\IncludeInRelease{0000/00/00}{ltcmd}%
6795 %<latexrelease> {Document~command~parser}%
6796 %<latexrelease>
6797 %<latexrelease>\EndModuleRelease
6798 \ExplSyntaxOff
6799 % \end{macrocode}
6800 %
6801 % Now in |latexrelease| mode, redefine \cs{NewDocumentCommand} to not
6802 % complain on commands already defined.
6803 % \changes{v1.0k}{2022-02-28}
6804 % {Move latexrelease redefinitions from ltcmd.dtx}
6805 % \begin{macrocode}
6806 %<latexrelease>\@ifundefined{ExplSyntaxOff}{}{\latexrelease@postltcmd}
6807 %<latexrelease>\catcode`\^^@=\@latexrelease@catcode@null\relax
6808 %</2ekernel|latexrelease>
6809 % \end{macrocode}
6810 %
6811 % We need to stop DocStrip treating |@@| in a special way at this point.
6812 % \begin{macrocode}
6813 %<@@=>
6814 % \end{macrocode}
6815 %
6816 % \Finale
6817