Debian Codegraph @

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