Debian Codegraph @

texmf-dist/source/generic/collargs/collargs.edtx

raw
1 % \iffalse
2 % collargs.edtx (this is not a .dtx file; to produce a .dtx, process it with edtx2dtx)
3 %%
4 %% This file is a part of CollArgs, a TeX package providing a command which can
5 %% determine the argument scope of any command whose argument structure
6 %% conforms to xparse's argument specification, available at
7 %% https://ctan.org/pkg/advice and https://github.com/sasozivanovic/advice.
8 %%
9 %% Copyright (c) 2023- Saso Zivanovic <[email protected]>
10 %% (Sa\v{s}o \v{Z}ivanovi\'{c})
11 %%
12 %% This work may be distributed and/or modified under the conditions of the
13 %% LaTeX Project Public License, either version 1.3c of this license or (at
14 %% your option) any later version. The latest version of this license is in
15 %% https://www.latex-project.org/lppl.txt and version 1.3c or later is part of
16 %% all distributions of LaTeX version 2008 or later.
17 %%
18 %% This work has the LPPL maintenance status `maintained'.
19 %% The Current Maintainer of this work is Saso Zivanovic.
20 %%
21 %% The files belonging to this work and covered by LPPL are listed in
22 %% (<texmf>/doc/generic/collargs/)FILES.
23 %
24 % \fi
25 %
26 % \begin{macrocode}
27 %
28 % Package CollArgs provides commands |\CollectArguments| and
29 % |\CollectArgumentsRaw|, which (what a surprise!) collect the arguments
30 % conforming to the given (slightly extended) |xparse| argument specification.
31 % The package was developed to help out with automemoization (see
32 % section~\ref{sec:code:automemoization}). It started out as a few lines of
33 % code, but had grown once I realized I want automemoization to work for
34 % verbatim environments as well --- the environment-collecting code is based on
35 % Bruno Le Floch's package |cprotect| --- and had then grown some more once I
36 % decided to support the |xparse| argument specification in full detail, and to
37 % make the verbatim mode flexible enough to deal with a variety of situations.
38 %
39 % The implementation of this package does not depend on |xparse|. Perhaps this
40 % is a mistake, especially as the |xparse| code is now included in the base
41 % \hologo{LaTeX}, but the idea was to have a light-weight package (not sure
42 % this is the case anymore, given all the bells and whistles), to have its
43 % functionality available in plain \hologo{TeX} and \hologo{ConTeXt} as well
44 % (same as Memoize), and, perhaps most importantly, to have the ability to
45 % collect the arguments verbatim.
46 %
47 %
48 % \paragraph{Identification}
49 %<latex>\ProvidesPackage{collargs}[2024/03/15 v1.2.0 Collect arguments of any command]
50 %<context>%D \module[
51 %<context>%D file=t-collargs.tex,
52 %<context>%D version=1.2.0,
53 %<context>%D title=CollArgs,
54 %<context>%D subtitle=Collect arguments of any command,
55 %<context>%D author=Saso Zivanovic,
56 %<context>%D date=2024-03-15,
57 %<context>%D copyright=Saso Zivanovic,
58 %<context>%D license=LPPL,
59 %<context>%D ]
60 %<context>\writestatus{loading}{ConTeXt User Module / collargs}
61 %<context>\unprotect
62 %<context>\startmodule[collargs]
63 % \paragraph{Required packages}
64 %<latex>\RequirePackage{pgfkeys}
65 %<plain>\input pgfkeys
66 %<context>\input t-pgfkey
67 %<latex>\RequirePackage{etoolbox}
68 %<plain,context>\input etoolbox-generic
69 %<plain>\edef\resetatcatcode{\catcode`\noexpand\@\the\catcode`\@\relax}
70 %<plain>\catcode`\@11\relax
71 %
72 % \begin{macro}{\toksapp,\gtoksapp,\etoksapp,\xtoksapp}
73 % Macros for appending to a token register. We don't have to define them in
74 % \hologo{LuaTeX}, where they exist as primitives. Same as these primitives,
75 % out macros accept either a register number or a |\toksdef|fed control
76 % sequence as the (unbraced) |#1|; |#2| is the text to append.
77 \ifdefined\luatexversion
78 \else
79 \def\toksapp{\toks@cs@or@num\@toksapp}
80 \def\gtoksapp{\toks@cs@or@num\@gtoksapp}
81 \def\etoksapp{\toks@cs@or@num\@etoksapp}
82 \def\xtoksapp{\toks@cs@or@num\@xtoksapp}
83 \def\toks@cs@or@num#1#2#{%
84 % Test whether |#2| (the original |#1|) is a number or a control sequence.
85 \ifnum-2>-1#2
86 % It is a number. |\toks@cs@or@num@num| will gobble |\toks@cs@or@num@cs|
87 % below.
88 \expandafter\toks@cs@or@num@num
89 % The register control sequence in |#2| is skipped over in the false
90 % branch.
91 \fi
92 \toks@cs@or@num@cs{#1}{#2}%
93 }
94 % |#1| is one of |\@toksapp| and friends. The second macro prefixes the
95 % register number by |\toks|.
96 \def\toks@cs@or@num@cs#1#2{#1{#2}}
97 \def\toks@cs@or@num@num\toks@cs@or@num@cs#1#2{#1{\toks#2 }}
98 % Having either |\tokscs| or |\toks<number>| in |#1|, we can finally do the
99 % real job.
100 \long\def\@toksapp#1#2{#1\expandafter{\the#1#2}}%
101 \long\def\@etoksapp#1#2{#1\expandafter{\expanded{\the#1#2}}}%
102 \long\def\@gtoksapp#1#2{\global#1\expandafter{\the#1#2}}%
103 \long\def\@xtoksapp#1#2{\global#1\expandafter{\expanded{\the#1#2}}}%
104 \fi
105 % \end{macro}
106 %
107 % \begin{macro}{\CollectArguments,\CollectArgumentsRaw}
108 % |\CollectArguments| takes three arguments: the optional |#1| is the option
109 % list, processed by |pgfkeys| (given the grouping structure, these options
110 % will apply to all arguments); the mandatory |#2| is the |xparse|-style
111 % argument specification; the mandatory |#3| is the ``next'' command (or a
112 % sequence of commands). The argument list is expected to start immediately
113 % after the final argument; |\CollectArguments| parses it, effectively
114 % figuring out its extent, and then passes the entire argument list to the
115 % ``next'' command (as a single argument).
116 %
117 % |\CollectArgumentsRaw| differs only in how it takes and processes the
118 % options. For one, these should be given as a mandatory argument.
119 % Furthermore, they do not take the form of a keylist, but should deploy the
120 % ``programmer's interface.'' |#1| should thus be a sequence of invocations
121 % of the macro counterparts of the keys defined in
122 % section~\ref{sec:code:collargs:keys}, which can be recognized as starting
123 % with |\collargs| followed by a capital letter, e.g.\ |\collargsCaller|.
124 % Note that |\collargsSet| may also be used in |#1|. (The ``optional,''
125 % i.e.\ bracketed, argument of |\CollectArgumentsRaw| is in fact mandatory.)
126 \protected\def\CollectArguments{%
127 \pgf@keys@utilifnextchar[\CollectArguments@i{\CollectArgumentsRaw{}}%]
128 }
129 \def\CollectArguments@i[#1]{\CollectArgumentsRaw{\collargsSet{#1}}}
130 \protected\def\CollectArgumentsRaw#1#2#3{%
131 % This group will be closed by |\collargs@.| once we grinded through the
132 % argument specification.
133 \begingroup
134 % Initialize category code fixing; see section~\ref{sec:code:collargs:fix}
135 % for details. We have to do this before applying the settings, so that
136 % |\collargsFixFromNoVerbatim| et al can take effect.
137 \global\let\ifcollargs@last@verbatim\ifcollargs@verbatim
138 \global\let\ifcollargs@last@verbatimbraces\ifcollargs@verbatimbraces
139 \global\collargs@double@fixfalse
140 % Apply the settings.
141 \collargs@verbatim@wrap{#1}%
142 % Initialize the space-grabber.
143 \collargs@init@grabspaces
144 % Remember the code to execute after collection.
145 \def\collargs@next{#3}%
146 % Initialize the token register holding the collected arguments.
147 \ifcollargsClearArgs
148 \global\collargsArgs{}%
149 \fi
150 % Execute the central loop macro, which expects the argument specification
151 % |#2| to be delimited from the following argument tokens by a dot.
152 \collargs@#2.%
153 }
154 % \end{macro}
155 %
156 % \begin{macro}{\collargsSet}
157 % This macro processes the given keys in the |/collargs| keypath. When it is
158 % used to process options given by the end user (the optional argument to
159 % |\CollectArguments|, and the options given within the argument
160 % specification, using the new modifier |&|), its invocation should be
161 % wrapped in |\collargs@verbatim@wrap| to correctly deal with the changes of
162 % the verbatim mode.
163 \def\collargsSet#1{\pgfqkeys{/collargs}{#1}}
164 % \end{macro}
165 %
166 % \subsubsection{The keys}
167 % \label{sec:code:collargs:keys}
168 %
169 % \begin{macro}{\collargs@cs@cases}
170 % If the first argument of this auxiliary macro is a single control sequence,
171 % then the second argument is executed. If the first argument starts with a
172 % control sequence but this control sequence does not form the entire
173 % argument, the third argument is executed. Otherwise, the fourth argument
174 % is executed.
175 %
176 % This macro is defined in package CollArgs because we use it in key |caller|
177 % below, but it is really useful in package Auto, where having it we don't
178 % have to bother the end-user with a separate keys for commands and
179 % environments, but automatically detect whether the argument of |auto| and
180 % (|de|)|activate| is a command or an environment.
181 \def\collargs@cs@cases#1{\collargs@cs@cases@i#1\collargs@cs@cases@end}
182 \let\collargs@cs@cases@end\relax
183 \def\collargs@cs@cases@i{\futurelet\collargs@temp\collargs@cs@cases@ii}
184 \def\collargs@cs@cases@ii#1#2\collargs@cs@cases@end{%
185 \ifcat\noexpand\collargs@temp\relax
186 \ifx\relax#2\relax
187 \expandafter\expandafter\expandafter\@firstofthree
188 \else
189 \expandafter\expandafter\expandafter\@secondofthree
190 \fi
191 \else
192 \expandafter\@thirdofthree
193 \fi
194 }
195 \def\@firstofthree#1#2#3{#1}
196 \def\@secondofthree#1#2#3{#2}
197 \def\@thirdofthree#1#2#3{#3}
198 % \end{macro}
199 %
200 % \begin{collargskey}{caller}
201 % \begin{macro}{\collargsCaller}
202 % Every macro which grabs a part of the argument list will be accessed
203 % through the ``caller'' control sequence, so that \hologo{TeX}'s reports
204 % of any errors in the argument structure can contain a command name
205 % familiar to the author.\footnote{The idea is borrowed from package
206 % |environ|, which is in turn based on code from |amsmath|.} For example,
207 % if the argument list ``originally'' belonged to command |\foo| with
208 % argument structure |r()|, but no parentheses follow in the input, we want
209 % \hologo{TeX} to complain that |Use of \foo doesn't match its definition|.
210 % This can be achieved by setting |caller=\foo|; the default is
211 % |caller=\CollectArguments|, which is still better than seeing an error
212 % involving some random internal control sequence. It is also ok to set an
213 % environment name as the caller, see below.
214 %
215 % The key and macro defined below store the caller control sequence into
216 % |\collargs@caller|, e.g.\ when we say |caller=\foo|, we effectively
217 % execute |\def\collargs@caller{\foo}|.
218 \collargsSet{
219 caller/.code={\collargsCaller{#1}},
220 }
221 \def\collargsCaller#1{%
222 \collargs@cs@cases{#1}{%
223 \let\collargs@temp\collargs@caller@cs
224 }{%
225 \let\collargs@temp\collargs@caller@csandmore
226 }{%
227 \let\collargs@temp\collargs@caller@env
228 }%
229 \collargs@temp{#1}%
230 }
231 \def\collargs@caller@cs#1{%
232 % If |#1| is a single control sequence, just use that as the caller.
233 \def\collargs@caller{#1}%
234 }
235 \def\collargs@caller@csandmore#1{%
236 % If |#1| starts with a control sequence, we don't complain, but convert the
237 % entire |#1| into a control sequence.
238 \begingroup
239 \escapechar -1
240 \expandafter\endgroup
241 \expandafter\def\expandafter\collargs@caller\expandafter{%
242 \csname\string#1\endcsname
243 }%
244 }
245 \def\collargs@caller@env#1{%
246 % If |#1| does not start with a control sequence, we assume that is an
247 % environment name, so we prepend |start| in \hologo{ConTeXt}, and dress it
248 % up into |\begin{#1}| in \hologo{LaTeX}.
249 \expandafter\def\expandafter\collargs@caller\expandafter{%
250 \csname
251 %<context>start%
252 %<latex>begin{%
253 #1%
254 %<latex>}%
255 \endcsname
256 }%
257 }
258 \collargsCaller\CollectArguments
259 % \end{macro}
260 % \end{collargskey}
261 %
262 % \begin{macro}{\ifcollargs@verbatim,\ifcollargs@verbatimbraces}
263 % The first of these conditional
264 % signals that we're collecting the arguments in one of the verbatim
265 % modes; the second one signals the |verb| mode in particular.
266 \newif\ifcollargs@verbatim
267 \newif\ifcollargs@verbatimbraces
268 % \end{macro}
269 %
270 % \begin{collargskey}{verbatim, verb, no verbatim}
271 % \begin{macro}{\collargs@verbatim@wrap}
272 % These keys set the verbatim mode macro which will be executed by
273 % |\collargsSet| after processing all keys.
274 % The verbatim mode macros |\collargsVerbatim|, |\collargsVerb| and
275 % |\collargsNoVerbatim| are somewhat complex; we postpone their definition
276 % until section~\ref{sec:code:collargs:verbatim}. Their main effect is to
277 % set conditionals |\ifcollargs@verbatim| and |\ifcollargs@verbatimbraces|,
278 % which are be inspected by the argument type handlers --- and to make
279 % the requested category code changes, of course.
280 %
281 % Here, note that the verbatim-selection code is not executed while the
282 % keylist is being processed. Rather, the verbatim keys simply set the macro
283 % which will be executed \emph{after} the keylist is processed, and this is
284 % why processing of a keylist given by the user must be always wrapped in
285 % |\collargs@verbatim@wrap|.
286 \collargsSet{
287 verbatim/.code={\let\collargs@apply@verbatim\collargsVerbatim},
288 verb/.code={\let\collargs@apply@verbatim\collargsVerb},
289 no verbatim/.code={\let\collargs@apply@verbatim\collargsNoVerbatim},
290 }
291 \def\collargs@verbatim@wrap#1{%
292 \let\collargs@apply@verbatim\relax
293 #1%
294 \collargs@apply@verbatim
295 }
296 % \end{macro}
297 % \end{collargskey}
298 %
299 % \begin{collargskey}{fix from verbatim, fix from verb, fix from no verbatim}
300 % \begin{macro}{\collargsFixFromVerbatim,\collargsFixFromVerb,\collargsFixFromNoVerbatim}
301 % These keys and macros should be used to request a category code fix, when
302 % the offending tokenization took place prior to invoking
303 % |\CollectArguments|; see section~\ref{sec:code:collargs:fix} for details.
304 % While I assume that only |\collargsFixFromNoVerbatim| will ever be used
305 % (and it is used by |\mmz|), we provide macros for all three transitions,
306 % for completeness.\indentmacrocode
307 \collargsSet{
308 fix from verbatim/.code={\collargsFixFromVerbatim},
309 fix from verb/.code={\collargsFixFromVerb},
310 fix from no verbatim/.code={\collargsFixFromNoVerbatim},
311 }
312 % \noindentmacrocode
313 \def\collargsFixFromNoVerbatim{%
314 \global\collargs@fix@requestedtrue
315 \global\let\ifcollargs@last@verbatim\iffalse
316 }
317 \def\collargsFixFromVerbatim{%
318 \global\collargs@fix@requestedtrue
319 \global\let\ifcollargs@last@verbatim\iftrue
320 \global\let\ifcollargs@last@verbatimbraces\iftrue
321 }
322 \def\collargsFixFromVerb{%
323 \global\collargs@fix@requestedtrue
324 \global\let\ifcollargs@last@verbatim\iftrue
325 \global\let\ifcollargs@last@verbatimbraces\iffalse
326 }
327 % \end{macro}
328 % \end{collargskey}
329 %
330 % \begin{collargskey}{braces}
331 % Set the characters which are used as the grouping characters in the full
332 % verbatim mode. The user is only required to do this when multiple
333 % character pairs serve as the grouping characters. The underlying macro,
334 % |\collargsBraces|, will be defined in
335 % section~\ref{sec:code:collargs:verbatim}.
336 \collargsSet{
337 braces/.code={\collargsBraces{#1}}%
338 }
339 % \end{collargskey}
340 %
341 % \begin{collargskey}{environment}
342 % \begin{macro}{\collargsEnvironment}
343 % Set the environment name.
344 % \indentmacrocode
345 \collargsSet{
346 environment/.estore in=\collargs@b@envname
347 }
348 \def\collargsEnvironment#1{\edef\collargs@b@envname{#1}}
349 \collargsEnvironment{}
350 % \end{macro}
351 % \end{collargskey}
352 %
353 % \begin{collargskey}{begin tag, end tag, tags}
354 % \begin{macro}{\ifcollargsBeginTag,\ifcollargsEndTag,\ifcollargsAddTags}
355 % When |begin tag|\slash|end tag| is in effect, the begin\slash end-tag will
356 % be will be prepended/appended to the environment body. |tags| is a
357 % shortcut for setting |begin tag| and |end tag| simultaneously.
358 % \indentmacrocode
359 \collargsSet{
360 begin tag/.is if=collargsBeginTag,
361 end tag/.is if=collargsEndTag,
362 tags/.style={begin tag=#1, end tag=#1},
363 tags/.default=true,
364 }
365 \newif\ifcollargsBeginTag
366 \newif\ifcollargsEndTag
367 % \end{macro}
368 % \end{collargskey}
369 %
370 % \begin{collargskey}{ignore nesting}
371 % \begin{macro}{\ifcollargsIgnoreNesting}
372 % When this key is in effect, we will
373 % ignore any \cs{begin}\marg{name}s and simply grab everything up to
374 % the first \cs{end}\marg{name} (again, the markers are automatically
375 % adapted to the format).
376 \collargsSet{
377 ignore nesting/.is if=collargsIgnoreNesting,
378 }
379 \newif\ifcollargsIgnoreNesting
380 % \end{macro}
381 % \end{collargskey}
382 %
383 % \begin{collargskey}{ignore other tags}
384 % \begin{macro}{\ifcollargsIgnoreOtherTags}
385 % This key is only relevant in the
386 % non-verbatim and partial verbatim modes in \hologo{LaTeX}. When it
387 % is in effect, CollArgs checks the environment name following each
388 % |\begin| and |\end|, ignoring the tags with an environment name other
389 % than |\collargs@b@envname|.
390 \collargsSet{
391 ignore other tags/.is if=collargsIgnoreOtherTags,
392 }
393 \newif\ifcollargsIgnoreOtherTags
394 % \end{macro}
395 % \end{collargskey}
396 %
397 % \begin{collargskey}[noindex]{(append/prepend) (pre/post)processor}
398 % \begin{macro}[noindex]{\collargs(Append/Prepend)(Pre/Post)processor}
399 % \begin{collargskey}[noprint]{append preprocessor, prepend preprocessor,
400 % append postprocessor, prepend postprocessor}
401 % \begin{macro}[noprint]{\collargsAppendPreprocessor,\collargsPrependPreprocessor,\collargsAppendPostprocessor,\collargsPrependPostprocessor}
402 % These keys and
403 % macros populate the list of preprocessors,
404 % |\collargs@preprocess@arg|, and the list of post-processors,
405 % |\collargs@postprocess@arg|, executed in |\collargs@appendarg|.
406 \collargsSet{
407 append preprocessor/.code={\collargsAppendPreprocessor{#1}},
408 prepend preprocessor/.code={\collargsPrependPreprocessor{#1}},
409 append postprocessor/.code={\collargsAppendPostprocessor{#1}},
410 prepend postprocessor/.code={\collargsPrependPostprocessor{#1}},
411 }
412 \def\collargsAppendPreprocessor#1{\appto\collargs@preprocess@arg{#1}}
413 \def\collargsPrependPreprocessor#1{\preto\collargs@preprocess@arg{#1}}
414 \def\collargsAppendPostprocessor#1{\appto\collargs@postprocess@arg{#1}}
415 \def\collargsPrependPostprocessor#1{\preto\collargs@postprocess@arg{#1}}
416 % \end{macro}
417 % \end{collargskey}
418 % \end{macro}
419 % \end{collargskey}
420 %
421 % \begin{collargskey}[noindex]{clear (pre/post)processors}
422 % \begin{macro}[noindex]{\collargsClear(Pre/Post)processors}
423 % \begin{collargskey}[noprint]{clear preprocessors, clear postprocessors}
424 % \begin{macro}[noprint]{\collargsClearPreprocessors,\collargsClearPostprocessors}
425 % These keys and macros
426 % clear the pre- and post-processor lists, which are initially empty as
427 % well.
428 \def\collargs@preprocess@arg{}
429 \def\collargs@postprocess@arg{}
430 \collargsSet{
431 clear preprocessors/.code={\collargsClearPreprocessors},
432 clear postprocessors/.code={\collargsClearPostprocessors},
433 }
434 \def\collargsClearPreprocessors{\def\collargs@preprocess@arg{}}%
435 \def\collargsClearPostprocessors{\def\collargs@postprocess@arg{}}%
436 % \end{macro}
437 % \end{collargskey}
438 % \end{macro}
439 % \end{collargskey}
440 %
441 % \begin{collargskey}[noindex]{(append/prepend) expandable (pre/post)processor}
442 % \begin{macro}[noindex]{\collargs(Append/Prepend)Expandable(Pre/Post)processor}
443 % \begin{collargskey}[noprint]{append expandable preprocessor, prepend expandable preprocessor, append expandable postprocessor, prepend expandable postprocessor}
444 % \begin{macro}[noprint]{\collargsAppendExpandablePreprocessor,\collargsPrependExpandablePreprocessor,\collargsAppendExpandablePostprocessor,\collargsPrependExpandablePostprocessor}
445 % These keys and macros simplify the definition of expandable
446 % processors. Note that expandable processors are added to the same
447 % list as non-expandable processors.
448 \collargsSet{
449 append expandable preprocessor/.code={\collargsAppendExpandablePreprocessor{#1}},
450 prepend expandable preprocessor/.code={\collargsPrependExpandablePreprocessor{#1}},
451 append expandable postprocessor/.code={\collargsAppendExpandablePostprocessor{#1}},
452 prepend expandable postprocessor/.code={\collargsPrependExpandablePostprocessor{#1}},
453 }
454 \def\collargsAppendExpandablePreprocessor#1{%
455 \appto\collargs@preprocess@arg{%
456 \collargsArg\expandafter{\expanded{#1}}%
457 }%
458 }
459 \def\collargsPrependExpandablePreprocessor#1{%
460 \preto\collargs@preprocess@arg{%
461 \collargsArg\expandafter{\expanded{#1}}%
462 }%
463 }
464 \def\collargsAppendExpandablePostprocessor#1{%
465 \appto\collargs@postprocess@arg{%
466 \collargsArg\expandafter{\expanded{#1}}%
467 }%
468 }
469 \def\collargsPrependExpandablePostprocessor#1{%
470 \preto\collargs@postprocess@arg{%
471 \collargsArg\expandafter{\expanded{#1}}%
472 }%
473 }
474 % \end{macro}
475 % \end{collargskey}
476 % \end{macro}
477 % \end{collargskey}
478 %
479 %
480 % \begin{collargskey}{no delimiters}
481 % \begin{macro}{\ifcollargsNoDelimiters}
482 % When this conditional is in effect, the
483 % delimiter wrappers set by |\collargs@wrap| are ignored by
484 % |\collargs@appendarg|.
485 \collargsSet{%
486 no delimiters/.is if=collargsNoDelimiters,
487 }
488 \newif\ifcollargsNoDelimiters
489 % \end{macro}
490 % \end{collargskey}
491 %
492 % \begin{collargskey}{clear args}
493 % \begin{macro}{\ifcollargsClearArgs}
494 % When this conditional is set to |false|, the global token register
495 % |\collargsArgs| receiving the collected arguments is not cleared prior
496 % to argument collection.
497 \collargsSet{%
498 clear args/.is if=collargsClearArgs,
499 }
500 \newif\ifcollargsClearArgs
501 \collargsClearArgstrue
502 % \end{macro}
503 % \end{collargskey}
504 %
505 % \begin{collargskey}{return}
506 % \begin{macro}{\collargsReturn}
507 % Exiting |\CollectArguments|, should the next-command be followed by the
508 % braced collected arguments, collected arguments as they are, or nothing?
509 \collargsSet{%
510 return/.is choice,
511 return/braced/.code=\collargsReturnBraced,
512 return/plain/.code=\collargsReturnPlain,
513 return/no/.code=\collargsReturnNo,
514 }
515 \def\collargsReturnBraced{\def\collargsReturn{0}}
516 \def\collargsReturnPlain{\def\collargsReturn{1}}
517 \def\collargsReturnNo{\def\collargsReturn{2}}
518 \collargsReturnBraced
519 % \end{macro}
520 % \end{collargskey}
521 %
522 % \begin{collargskey}{alias}
523 % \begin{macro}{\collargsAlias}
524 %
525 \collargsSet{%
526 alias/.code 2 args=\collargsAlias{#1}{#2}%
527 }
528 \def\collargsAlias#1#2{%
529 \csdef{collargs@#1}{\collargs@@@#2}%
530 }
531 % \end{macro}
532 % \end{collargskey}
533 %
534 %
535 %
536 % \subsubsection{The central loop}
537 % \label{sec:code:collargs:central-loop}
538 %
539 % The central loop is where we grab the next \meta{token} from the argument
540 % specification and execute the corresponding argument type or modifier
541 % handler, |\collargs@|\meta{token}. The central loop consumes the argument
542 % type \meta{token}; the handler will see the remainder of the argument
543 % specification (which starts with the arguments to the argument type, if any,
544 % e.g.\ by |()| of |d()|), followed by a dot, and then the tokens list from
545 % which the arguments are to be collected. It is the responsibility of handler
546 % to preserve the rest of the argument specification and reexecute the central
547 % loop once it is finished.
548 %
549 % \begin{macro}{\collargs@}
550 % Each argument is processed in a group to allow for local settings. This
551 % group is closed by |\collargs@appendarg|.
552 \def\collargs@{%
553 \begingroup
554 \collargs@@@
555 }
556 % \end{macro}
557 %
558 % \begin{macro}{\collargs@@@}
559 % This macro is where modifier handlers reenter the central loop --- we don't
560 % want modifers to open a group, because their settings should remain in
561 % effect until the next argument. Furthermore, modifiers do not trigger
562 % category code fixes.
563 \def\collargs@@@#1{%
564 \collargs@in@{#1}{&+!>.}%
565 \ifcollargs@in@
566 \expandafter\collargs@@@iii
567 \else
568 \expandafter\collargs@@@i
569 \fi
570 #1%
571 }
572 \def\collargs@@@i#1.{%
573 % Fix the category code of the next argument token, if necessary, and then
574 % proceed with the main loop.
575 \collargs@fix{\collargs@@@ii#1.}%
576 }
577 % Reset the fix request and set the last verbatim conditionals to the current
578 % state.
579 \def\collargs@@@ii{%
580 \global\collargs@fix@requestedfalse
581 \global\let\ifcollargs@last@verbatim\ifcollargs@verbatim
582 \global\let\ifcollargs@last@verbatimbraces\ifcollargs@verbatimbraces
583 \collargs@@@iii
584 }
585 % Call the modifier or argument type handler denoted by the first token of the
586 % remainder of the argument specification.
587 \def\collargs@@@iii#1{%
588 \ifcsname collargs@#1\endcsname
589 \csname collargs@#1\expandafter\endcsname
590 \else
591 % We throw an error if the token refers to no argument type or modifier.
592 \collargs@error@badtype{#1}%
593 \fi
594 }
595 % Throwing an error stops the processing of the argument specification, and
596 % closes the group opened in |\collargs@i|.
597 \def\collargs@error@badtype#1#2.{%
598 \PackageError{collargs}{Unknown xparse argument type or modifier "#1"
599 for "\expandafter\string\collargs@caller\space"}{}%
600 \endgroup
601 }
602 % \end{macro}
603 %
604 % {\catcode`\&=11
605 % \begin{macro}{\collargs@&}
606 % We extend the |xparse| syntax with modifier |&|, which applies the given
607 % options to the following (and only the following) argument. If |&| is
608 % followed by another |&|, the options are expected to occur in the raw
609 % format, like the options given to |\CollectArgumentsRaw|. Otherwise, the
610 % options should take the form of a keylist, which will be processed by
611 % |\collargsSet|. In any case, the options should be given within the
612 % argument specification, immediately following the (single or double) |&|.
613 \csdef{collargs@&}{%
614 \futurelet\collargs@temp\collargs@amp@i
615 }
616 \def\collargs@amp@i{%
617 % In \hologo{ConTeXt}, |&| has character code ``other'' in the text.
618 %<!context>\ifx\collargs@temp&%
619 %<context>\expandafter\ifx\detokenize{&}\collargs@temp
620 \expandafter\collargs@amp@raw
621 \else
622 \expandafter\collargs@amp@set
623 \fi
624 }
625 \def\collargs@amp@raw#1#2{%
626 \collargs@verbatim@wrap{#2}%
627 \collargs@@@
628 }
629 \def\collargs@amp@set#1{%
630 \collargs@verbatim@wrap{\collargsSet{#1}}%
631 \collargs@@@
632 }
633 % \end{macro}}
634 %
635 % \begin{macro}[noindex]{\collargs@+}
636 % \MyIndex{collargs@&plus;}{\texttt{\textbackslash collargs@+}}{main}
637 % This modifier makes the next argument long, i.e.\ accept paragraph tokens.
638 \csdef{collargs@+}{%
639 \collargs@longtrue
640 \collargs@@@
641 }
642 \newif\ifcollargs@long
643 % \end{macro}
644 %
645 % \begin{macro}{\collargs@>}
646 % We can simply ignore the processor modifier. (This, |xparse|'s processor,
647 % should not be confused with CollArgs's processors, which are set using
648 % keys |append preprocessor| etc.)
649 \csdef{collargs@>}#1{\collargs@@@}
650 % \end{macro}
651 %
652 % \begin{macro}[noindex]{\collargs@!}
653 % \MyIndex{collargs@&excl;}{\texttt{\textbackslash collargs@\exclamationmark}}{main}
654 % Should we accept spaces before an optional argument following a mandatory
655 % argument (\pkg{xparse} manual, \S1.1)? By default, yes. This modifier is
656 % only applicable to types |d| and |t|, and derived types, but, unlike
657 % |xparse|, we don't bother to enforce this; when used with other types, |!|
658 % simply has no effect.
659 \csdef{collargs@!}{%
660 \collargs@grabspacesfalse
661 \collargs@@@
662 }
663 % \end{macro}
664 %
665 % \begin{macro}{\collargsArgs}
666 % This token register is where we store the collected argument tokens. All
667 % assignments to this register are global, because it needs to survive the
668 % groups opened for individual arguments.
669 \newtoks\collargsArgs
670 % \end{macro}
671 %
672 % \begin{macro}{\collargsArg}
673 % An auxiliary, but publicly available token register, used for processing
674 % the argument, and by some argument type handlers.
675 \newtoks\collargsArg
676 % \end{macro}
677 %
678 % \begin{macro}{\collargs@.}
679 % This fake argument type is used to signal the end of the argument list.
680 % Note that this really counts as an extension of the |xparse| argument
681 % specification.
682 \csdef{collargs@.}{%
683 % Close the group opened in |\collargs@|.
684 \endgroup
685 % Close the main |\CollectArguments| group, fix the category code of the next
686 % token if necessary, and execute the next-code, followed by the collected
687 % arguments in braces. Any over-grabbed spaces are reinserted into the input
688 % stream, non-verbatim.
689 \expanded{%
690 \endgroup
691 \noexpand\collargs@fix{%
692 \expandonce\collargs@next
693 \ifcase\collargsReturn\space
694 {\the\collargsArgs}%
695 \or
696 \the\collargsArgs
697 \fi
698 \collargs@spaces
699 }%
700 }%
701 }
702 % \end{macro}
703 %
704 % \subsubsection{Auxiliary macros}
705 % \label{sec:code:collargs:aux}
706 %
707 % \begin{macro}{\collargs@appendarg}
708 % This macro is used by the argument type
709 % handlers to append the collected argument to the storage
710 % (|\collargsArgs|).
711 \long\def\collargs@appendarg#1{%
712 % Temporarily store the collected argument into a token register. The
713 % processors will manipulate the contents of this register.
714 \collargsArg={#1}%
715 % This will clear the double-fix conditional, and potentially request a
716 % normal, single fix. We can do this here because this macro is only called
717 % when something is actually collected. For details, see
718 % section~\ref{sec:code:collargs:fix}.
719 \ifcollargs@double@fix
720 \collargs@cancel@double@fix
721 \fi
722 % Process the argument with user-definable preprocessors, the wrapper defined
723 % by the argument type, and user-definable postprocessors.
724 \collargs@preprocess@arg
725 \ifcollargsNoDelimiters
726 \else
727 \collargs@process@arg
728 \fi
729 \collargs@postprocess@arg
730 % Append the processed argument, preceded by any grabbed spaces (in the
731 % correct mode), to the storage.
732 \xtoksapp\collargsArgs{\collargs@grabbed@spaces\the\collargsArg}%
733 % Initialize the space-grabber.
734 \collargs@init@grabspaces
735 % Once the argument was appended to the list, we can close its group, opened
736 % by |\collargs@|.
737 \endgroup
738 }
739 % \end{macro}
740 %
741 % \begin{macro}{\collargs@wrap}
742 % This macro is used by argument type handlers to declare their delimiter
743 % wrap, like square brackets around the optional argument of type |o|. It
744 % uses |\collargs@addwrap|, defined in section~\ref{sec:code:collargs:keys},
745 % but adds to |\collargs@process@arg|, which holds the delimiter wrapper
746 % defined by the argument type handler. Note that this macro \emph{appends}
747 % a wrapper, so multiple wrappers are allowed --- this is used by type |e|
748 % handler.
749 \def\collargs@wrap#1{%
750 \appto\collargs@process@arg{%
751 \long\def\collargs@temp##1{#1}%
752 \expandafter\expandafter\expandafter\collargsArg
753 \expandafter\expandafter\expandafter{%
754 \expandafter\collargs@temp\expandafter{\the\collargsArg}%
755 }%
756 }%
757 }
758 \def\collargs@process@arg{}
759 % \end{macro}
760 %
761 % \begin{macro}{\collargs@defcollector,\collargs@defusecollector,\collargs@letusecollector}
762 % These macros streamline the usage of
763 % the ``caller'' control sequence. They are like a |\def|, but should not
764 % be given the control sequence to define, as they will automatically
765 % define the control sequence residing in |\collargs@caller|; the usage is
766 % thus |\collargs@defcollector<parameters>{<definition>}|. For example, if
767 % |\collargs@caller| holds |\foo|, |\collargs@defcollector#1{(#1)}| is
768 % equivalent to |\def\foo#1{(#1)}|. Macro |\collargs@defcollector| will
769 % only define the caller control sequence to be the collector, while
770 % |\collargs@defusecollector| will also immediately execute it.
771 \def\collargs@defcollector#1#{%
772 \ifcollargs@long\long\fi
773 \expandafter\def\collargs@caller#1%
774 }
775 \def\collargs@defusecollector#1#{%
776 \afterassignment\collargs@caller
777 \ifcollargs@long\long\fi
778 \expandafter\def\collargs@caller#1%
779 }
780 \def\collargs@letusecollector#1{%
781 \expandafter\let\collargs@caller#1%
782 \collargs@caller
783 }
784 \newif\ifcollargs@grabspaces
785 \collargs@grabspacestrue
786 % \end{macro}
787 %
788 % \begin{macro}{\collargs@init@grabspaces}
789 % The space-grabber macro
790 % |\collargs@grabspaces| should be initialized by executing this macro. If
791 % |\collargs@grabspaces| is called twice without an intermediate
792 % initialization, it will assume it is in the same position in the input
793 % stream and simply bail out.
794 \def\collargs@init@grabspaces{%
795 \gdef\collargs@gs@state{0}%
796 \gdef\collargs@spaces{}%
797 \gdef\collargs@otherspaces{}%
798 }
799 % \end{macro}
800 %
801 % \begin{macro}{\collargs@grabspaces}
802 % This auxiliary macro grabs any following
803 % spaces, and then executes the next-code given as the sole argument. The
804 % spaces will be stored into two macros, |\collargs@spaces| and
805 % |\collargs@otherspaces|, which store the spaces in the non-verbatim and the
806 % verbatim form. With the double storage, we can grab the spaces in the
807 % verbatim mode and use them non-verbatim, or vice versa. The macro takes a
808 % single argument, the code to execute after maybe grabbing the spaces.
809 %
810 \def\collargs@grabspaces#1{%
811 \edef\collargs@gs@next{\unexpanded{#1}}%
812 \ifnum\collargs@gs@state=0
813 \gdef\collargs@gs@state{1}%
814 \expandafter\collargs@gs@i
815 \else
816 \expandafter\collargs@gs@next
817 \fi
818 }
819 \def\collargs@gs@i{%
820 \futurelet\collargs@temp\collargs@gs@g
821 }
822 % We check for grouping characters even in the verbatim mode, because we might
823 % be in the partial verbatim.
824 \def\collargs@gs@g{%
825 \ifcat\noexpand\collargs@temp\bgroup
826 \expandafter\collargs@gs@next
827 \else
828 \ifcat\noexpand\collargs@temp\egroup
829 \expandafter\expandafter\expandafter\collargs@gs@next
830 \else
831 \expandafter\expandafter\expandafter\collargs@gs@ii
832 \fi
833 \fi
834 }
835 \def\collargs@gs@ii{%
836 \ifcollargs@verbatim
837 \expandafter\collargs@gos@iii
838 \else
839 \expandafter\collargs@gs@iii
840 \fi
841 }
842 % This works because the character code of a space token is always 32.
843 \def\collargs@gs@iii{%
844 \expandafter\ifx\space\collargs@temp
845 \expandafter\collargs@gs@iv
846 \else
847 \expandafter\collargs@gs@next
848 \fi
849 }
850 \expandafter\def\expandafter\collargs@gs@iv\space{%
851 \gappto\collargs@spaces{ }%
852 \xappto\collargs@otherspaces{\collargs@otherspace}%
853 \collargs@gs@i
854 }
855 % We need the space of category 12 above.
856 \begingroup\catcode`\ =12\relax\gdef\collargs@otherspace{ }\endgroup
857 \def\collargs@gos@iii#1{%
858 % Macro |\collargs@cc| recalls the ``outside'' category code of character
859 % |#1|; see section~\ref{sec:code:collargs:verbatim}.
860 \ifnum\collargs@cc{#1}=10
861 % We have a space.
862 \expandafter\collargs@gos@iv
863 \else
864 \ifnum\collargs@cc{#1}=5
865 % We have a newline.
866 \expandafter\expandafter\expandafter\collargs@gos@v
867 \else
868 \expandafter\expandafter\expandafter\collargs@gs@next
869 \fi
870 \fi
871 #1%
872 }
873 \def\collargs@gos@iv#1{%
874 \gappto\collargs@otherspaces{#1}%
875 % No matter how many verbatim spaces we collect, they equal a single
876 % non-verbatim space.
877 \gdef\collargs@spaces{ }%
878 \collargs@gs@i
879 }
880 \def\collargs@gos@v{%
881 % Only add the first newline.
882 \ifnum\collargs@gs@state=2
883 \expandafter\collargs@gs@next
884 \else
885 \expandafter\collargs@gs@vi
886 \fi
887 }
888 \def\collargs@gs@vi#1{%
889 \gdef\collargs@gs@state{2}%
890 \gappto\collargs@otherspaces{#1}%
891 \gdef\collargs@spaces{ }%
892 \collargs@gs@i
893 }
894 % \end{macro}
895 %
896 % \begin{macro}{\collargs@maybegrabspaces}
897 % This macro grabs any following spaces, but it will do so only when
898 % conditional |\ifcollargs@grabspaces|, which can be \emph{un}set by modifier
899 % |!|, is in effect. The macro is used by handlers for types |d| and |t|.
900 \def\collargs@maybegrabspaces{%
901 \ifcollargs@grabspaces
902 \expandafter\collargs@grabspaces
903 \else
904 \expandafter\@firstofone
905 \fi
906 }
907 % \end{macro}
908 %
909 % \begin{macro}{\collargs@grabbed@spaces}
910 % This macro expands to either the verbatim
911 % or the non-verbatim variant of the grabbed spaces, depending on the
912 % verbatim mode in effect at the time of expansion.
913 \def\collargs@grabbed@spaces{%
914 \ifcollargs@verbatim
915 \collargs@otherspaces
916 \else
917 \collargs@spaces
918 \fi
919 }
920 % \end{macro}
921 %
922 % \begin{macro}{\collargs@reinsert@spaces}
923 % Inserts the grabbed spaces back into the
924 % input stream, but with the category code appropriate for the verbatim mode
925 % then in effect. After the insertion, the space-grabber is initialized and
926 % the given next-code is executed in front of the inserted spaces.
927 \def\collargs@reinsert@spaces#1{%
928 \expanded{%
929 \unexpanded{%
930 \collargs@init@grabspaces
931 #1%
932 }%
933 \collargs@grabbed@spaces
934 }%
935 }
936 % \end{macro}
937 %
938 % \begin{macro}{\collargs@ifnextcat}
939 % An adaptation of |\pgf@keys@utilifnextchar|
940 % which checks whether the \emph{category} code of the next non-space
941 % character matches the category code of |#1|.
942 \long\def\collargs@ifnextcat#1#2#3{%
943 \let\pgf@keys@utilreserved@d=#1%
944 \def\pgf@keys@utilreserved@a{#2}%
945 \def\pgf@keys@utilreserved@b{#3}%
946 \futurelet\pgf@keys@utillet@token\collargs@ifncat}
947 \def\collargs@ifncat{%
948 \ifx\pgf@keys@utillet@token\pgf@keys@utilsptoken
949 \let\pgf@keys@utilreserved@c\collargsxifnch
950 \else
951 \ifcat\noexpand\pgf@keys@utillet@token\pgf@keys@utilreserved@d
952 \let\pgf@keys@utilreserved@c\pgf@keys@utilreserved@a
953 \else
954 \let\pgf@keys@utilreserved@c\pgf@keys@utilreserved@b
955 \fi
956 \fi
957 \pgf@keys@utilreserved@c}
958 {%
959 \def\:{\collargs@xifncat}
960 \expandafter\gdef\: {\futurelet\pgf@keys@utillet@token\collargs@ifncat}
961 }
962 % \end{macro}
963 %
964 % \begin{macro}{\collargs@forrange}
965 % This macro executes macro |\collargs@do| for every integer from |#1| and
966 % |#2|, both inclusive. |\collargs@do| should take a single parameter, the
967 % current number.
968 \def\collargs@forrange#1#2{%
969 \expanded{%
970 \noexpand\collargs@forrange@i{\number#1}{\number#2}%
971 }%
972 }
973 \def\collargs@forrange@i#1#2{%
974 \ifnum#1>#2 %
975 \expandafter\@gobble
976 \else
977 \expandafter\@firstofone
978 \fi
979 {%
980 \collargs@do{#1}%
981 \expandafter\collargs@forrange@i\expandafter{\number\numexpr#1+1\relax}{#2}%
982 }%
983 }
984 % \end{macro}
985 %
986 % \begin{macro}{\collargs@forranges}
987 % This macro executes macro |\collargs@do| for every integer falling into the
988 % ranges specified in |#1|. The ranges should be given as a comma-separated
989 % list of |from-to| items, e.g.\ |1-5,10-11|.
990 \def\collargs@forranges{\forcsvlist\collarg@forrange@i}
991 \def\collarg@forrange@i#1{\collarg@forrange@ii#1-}
992 \def\collarg@forrange@ii#1-#2-{\collargs@forrange{#1}{#2}}
993 % \end{macro}
994 %
995 % \begin{macro}{\collargs@percentchar}
996 % This macro holds the percent character of category 12.
997 \begingroup
998 \catcode`\%=12
999 \gdef\collargs@percentchar{%}
1000 \endgroup
1001 % \end{macro}
1002 %
1003 %
1004 % \subsubsection{The handlers}
1005 % \label{sec:code:collargs:handlers}
1006 %
1007 % \begin{macro}{\collargs@l}
1008 % We will first define the handler for the very funky argument type |l|,
1009 % which corresponds to \hologo{TeX}'s |\def\foo#1#{...}|, which grabs (into
1010 % |#1|) everything up to the first opening brace --- not because this type is
1011 % important or even recommended to use, but because the definition of the
1012 % handler is very simple, at least for the non-verbatim case.
1013 %
1014 \def\collargs@l#1.{%
1015 % Any pre-grabbed spaces in fact belong into the argument.
1016 \collargs@reinsert@spaces{\collargs@l@i#1.}%
1017 }
1018 \def\collargs@l@i{%
1019 % We request a correction of the category code of the delimiting brace if the
1020 % verbatim mode changes for the next argument; for details, see
1021 % section~\ref{sec:code:collargs:fix}.
1022 \global\collargs@fix@requestedtrue
1023 % Most handlers will branch into the verbatim and the non-verbatim part using
1024 % conditional |\ifcollargs@verbatim|. This handler is a bit special, because
1025 % it needs to distinguish verbatim and non-verbatim \emph{braces}, and
1026 % braces are verbatim only in the full verbatim mode, i.e.\ when
1027 % |\ifcollargs@verbatimbraces| is true.
1028 \ifcollargs@verbatimbraces
1029 \expandafter\collargs@l@verb
1030 \else
1031 \expandafter\collargs@l@ii
1032 \fi
1033 }
1034 % We grab the rest of the argument specification (|#1|), to be reinserted into
1035 % the token stream when we reexecute the central loop.
1036 \def\collargs@l@ii#1.{%
1037 % In the non-verbatim mode, we merely have to define and execute the
1038 % collector macro. The parameter text |##1##| (note the doubled hashes),
1039 % which will put everything up to the first opening brace into the first
1040 % argument, looks funky, but that's all.
1041 \collargs@defusecollector##1##{%
1042 % We append the collected argument, |##1|, to |\collargsArgs|, the token
1043 % register holding the collected argument tokens.
1044 \collargs@appendarg{##1}%
1045 % Back to the central loop, with the rest of the argument specification
1046 % reinserted.
1047 \collargs@#1.%
1048 }%
1049 }
1050 \def\collargs@l@verb#1.{%
1051 % In the verbatim branch, we need to grab everything up to the first opening
1052 % brace of category code 12, so we want to define the collector with
1053 % parameter text |##1{|, with the opening brace of category 12. We have
1054 % stored this token in macro |\collargs@other@bgroup|, which we now need to
1055 % expand.
1056 \expandafter\collargs@defusecollector
1057 \expandafter##\expandafter1\collargs@other@bgroup{%
1058 % Appending the argument works the same as in the non-verbatim case.
1059 \collargs@appendarg{##1}%
1060 % Reexecuting the central loop macro is a bit more involved, as we need to
1061 % reinsert the verbatim opening brace (contrary to the regular brace above,
1062 % the verbatim brace is consumed by the collector macro) back into the
1063 % token stream, behind the reinserted argument specification.
1064 \expanded{%
1065 \noexpand\collargs@\unexpanded{#1.}%
1066 \collargs@other@bgroup
1067 }%
1068 }%
1069 }
1070 % \end{macro}
1071 %
1072 % \begin{macro}{\collargs@u}
1073 % Another weird type --- |u|\meta{tokens} reads everything up to the given
1074 % \meta{tokens}, i.e.\ this is \hologo{TeX}'s
1075 % |\def\foo#1|\meta{tokens}|{...}| --- but again, simple enough to allow us
1076 % to showcase solutions to two recurring problems.
1077 %
1078 % We start by branching into the verbatim mode (full or partial) or the
1079 % non-verbatim mode.
1080 \def\collargs@u{%
1081 \ifcollargs@verbatim
1082 \expandafter\collargs@u@verb
1083 \else
1084 \expandafter\collargs@u@i
1085 \fi
1086 }
1087 % To deal with the verbatim mode, we only need to convert the above
1088 % \meta{tokens} (i.e.\ the argument of |u| in the argument specification) to
1089 % category 12, i.e.\ we have to |\detokenize| them. Then, we may proceed as in
1090 % the non-verbatim branch, |\collargs@u@ii|.
1091 \def\collargs@u@verb#1{%
1092 % The |\string| here is a temporary solution to a problem with spaces. Our
1093 % verbatim mode has them of category ``other'', but |\detokenize| produces a
1094 % space of category ``space'' behind control words. ^^A todo
1095 \expandafter\collargs@u@i\expandafter{\detokenize\expandafter{\string#1}}%
1096 }
1097 % We then reinsert any pre-grabbed spaces into the stream, but we take care not
1098 % to destroy the braces around our delimiter in the argument specification.
1099 \def\collargs@u@i#1#2.{%
1100 \collargs@reinsert@spaces{\collargs@u@ii{#1}#2.}%
1101 }
1102 \def\collargs@u@ii#1#2.{%
1103 % |#1| contains the delimiter tokens, so |##1| below will receive everything
1104 % in the token stream up to these. But we have a problem: if we defined the
1105 % collector as for the non-verbatim |l|, and the delimiter happened to be
1106 % preceded by a single brace group, we would lose the braces. For example,
1107 % if the delimiter was |-| and we received |{foo}-|, we would collect |foo-|.
1108 % We solve this problem by inserting |\collargs@empty| (with an empty
1109 % definition) into the input stream (at the end of this macro) --- this way,
1110 % the delimiter can never be preceded by a single brace group --- and then
1111 % expanding it away before appending to storage (within the argument of
1112 % |\collargs@defusecollector|).
1113 \collargs@defusecollector##1#1{%
1114 % Define the wrapper which will add the delimiter tokens (|#1|) after the
1115 % collected argument. The wrapper will be applied during argument
1116 % processing in |\collargs@appendarg| (sandwiched between used-definable
1117 % pre- and post-processors).
1118 \collargs@wrap{####1#1}%
1119 % Expand the first token in |##1|, which we know to be |\collargs@empty|,
1120 % with empty expansion.
1121 \expandafter\collargs@appendarg\expandafter{##1}%
1122 \collargs@#2.%
1123 }%
1124 % Insert |\collargs@empty| into the input stream, in front of the ``real''
1125 % argument tokens.
1126 \collargs@empty
1127 }
1128 \def\collargs@empty{}
1129 % \end{macro}
1130 %
1131 % \begin{macro}{\collargs@r}
1132 % Finally, a real argument type: required delimited argument.
1133 \def\collargs@r{%
1134 \ifcollargs@verbatim
1135 \expandafter\collargs@r@verb
1136 \else
1137 \expandafter\collargs@r@i
1138 \fi
1139 }
1140 \def\collargs@r@verb#1#2{%
1141 \expandafter\collargs@r@i\detokenize{#1#2}%
1142 }
1143 \def\collargs@r@i#1#2#3.{%
1144 % We will need to use the |\collargs@empty| trick from type |u|, but with an
1145 % additional twist: we need to insert it \emph{after} the opening delimiter
1146 % |#1|. To do this, we consume the opening delimiter by the ``outer''
1147 % collector below --- we need to use the collector so that we get a nice
1148 % error message when the opening delimiter is not present --- and have this
1149 % collector define the ``inner'' collector in the spirit of type |u|.
1150 %
1151 % The outer collector has no parameters, it just requires the presence of the
1152 % opening delimiter.
1153 \collargs@defcollector#1{%
1154 % The inner collector will grab everything up to the closing delimiter.
1155 \collargs@defusecollector####1#2{%
1156 % Append the collected argument |####1| to the list, wrapping it into the
1157 % delimiters (|#1| and |#2|), but not before expanding its first token,
1158 % which we know to be |\collargs@empty|.
1159 \collargs@wrap{#1########1#2}%
1160 \expandafter\collargs@appendarg\expandafter{####1}%
1161 \collargs@#3.%
1162 }%
1163 \collargs@empty
1164 }%
1165 % Another complication: our delimited argument may be preceded by spaces. To
1166 % replicate the argument tokens faithfully, we need to collect them before
1167 % trying to grab the argument itself.
1168 \collargs@grabspaces\collargs@caller
1169 }
1170 % \end{macro}
1171 %
1172 % \begin{macro}{\collargs@R}
1173 % Discard the default and execute |r|.
1174 \def\collargs@R#1#2#3{\collargs@r#1#2}
1175 % \end{macro}
1176 %
1177 % \begin{macro}{\collargs@d}
1178 % Optional delimited argument. Very similar to |r|.
1179 \def\collargs@d{%
1180 \ifcollargs@verbatim
1181 \expandafter\collargs@d@verb
1182 \else
1183 \expandafter\collargs@d@i
1184 \fi
1185 }
1186 \def\collargs@d@verb#1#2{%
1187 \expandafter\collargs@d@i\detokenize{#1#2}%
1188 }
1189 \def\collargs@d@i#1#2#3.{%
1190 % This macro will be executed when the optional argument is not present. It
1191 % simply closes the argument's group and reexecutes the central loop.
1192 \def\collargs@d@noopt{%
1193 \global\collargs@fix@requestedtrue
1194 \endgroup
1195 \collargs@#3.%
1196 }%
1197 % The collector(s) are exactly as for |r|.
1198 \collargs@defcollector#1{%
1199 \collargs@defusecollector####1#2{%
1200 \collargs@wrap{#1########1#2}%
1201 \expandafter\collargs@appendarg\expandafter{####1}%
1202 \collargs@#3.%
1203 }%
1204 \collargs@empty
1205 }%
1206 % This macro will check, in conjunction with |\futurelet| below, whether the
1207 % optional argument is present or not.
1208 \def\collargs@d@ii{%
1209 \ifx#1\collargs@temp
1210 \expandafter\collargs@caller
1211 \else
1212 \expandafter\collargs@d@noopt
1213 \fi
1214 }%
1215 % Whether spaces are allowed in front of this type of argument depends on the
1216 % presence of modifier |!|.
1217 \collargs@maybegrabspaces{\futurelet\collargs@temp\collargs@d@ii}%
1218 }
1219 % \end{macro}
1220 %
1221 % \begin{macro}{\collargs@D}
1222 % Discard the default and execute |d|.
1223 \def\collargs@D#1#2#3{\collargs@d#1#2}
1224 % \end{macro}
1225 %
1226 % \begin{macro}{\collargs@o}
1227 % |o| is just |d| with delimiters |[| and |]|.
1228 \def\collargs@o{\collargs@d[]}
1229 % \end{macro}
1230 %
1231 % \begin{macro}{\collargs@O}
1232 % |O| is just |d| with delimiters |[| and |]| and the discarded default.
1233 \def\collargs@O#1{\collargs@d[]}
1234 % \end{macro}
1235 %
1236 % \begin{macro}{\collargs@t}
1237 % An optional token. Similar to |d|.
1238 \def\collargs@t{%
1239 \ifcollargs@verbatim
1240 \expandafter\collargs@t@verb
1241 \else
1242 \expandafter\collargs@t@i
1243 \fi
1244 }
1245 \def\collargs@t@space{ }
1246 \def\collargs@t@verb#1{%
1247 \let\collargs@t@space\collargs@otherspace
1248 \expandafter\collargs@t@i\expandafter{\detokenize{#1}}%
1249 }
1250 \def\collargs@t@i#1{%
1251 \expandafter\ifx\space#1%
1252 \expandafter\collargs@t@s
1253 \else
1254 \expandafter\collargs@t@I\expandafter#1%
1255 \fi
1256 }
1257 \def\collargs@t@s#1.{%
1258 \collargs@grabspaces{%
1259 \ifcollargs@grabspaces
1260 \collargs@appendarg{}%
1261 \else
1262 \expanded{%
1263 \noexpand\collargs@init@grabspaces
1264 \noexpand\collargs@appendarg{\collargs@grabbed@spaces}%
1265 }%
1266 \fi
1267 \collargs@#1.%
1268 }%
1269 }
1270 \def\collargs@t@I#1#2.{%
1271 \def\collargs@t@noopt{%
1272 \global\collargs@fix@requestedtrue
1273 \endgroup
1274 \collargs@#2.%
1275 }%
1276 \def\collargs@t@opt##1{%
1277 \collargs@appendarg{#1}%
1278 \collargs@#2.%
1279 }%
1280 \def\collargs@t@ii{%
1281 \ifx#1\collargs@temp
1282 \expandafter\collargs@t@opt
1283 \else
1284 \expandafter\collargs@t@noopt
1285 \fi
1286 }%
1287 \collargs@maybegrabspaces{\futurelet\collargs@temp\collargs@t@ii}%
1288 }
1289 \def\collargs@t@opt@space{%
1290 \expanded{\noexpand\collargs@t@opt{\space}\expandafter}\romannumeral-0%
1291 }%
1292 % \end{macro}
1293 %
1294 % \begin{macro}{\collargs@s}
1295 % The optional star is just a special case of |t|.
1296 \def\collargs@s{\collargs@t*}
1297 % \end{macro}
1298 %
1299 % \begin{macro}{\collargs@m}
1300 % Mandatory argument. Interestingly, here's where things get complicated,
1301 % because we have to take care of several \hologo{TeX} quirks.
1302 \def\collargs@m{%
1303 \ifcollargs@verbatim
1304 \expandafter\collargs@m@verb
1305 \else
1306 \expandafter\collargs@m@i
1307 \fi
1308 }
1309 % The non-verbatim mode. First, collect any spaces in front of the argument.
1310 \def\collargs@m@i#1.{%
1311 \collargs@grabspaces{\collargs@m@checkforgroup#1.}%
1312 }
1313 % Is the argument in braces or not?
1314 \def\collargs@m@checkforgroup#1.{%
1315 \edef\collargs@action{\unexpanded{\collargs@m@checkforgroup@i#1.}}%
1316 \futurelet\collargs@token\collargs@action
1317 }
1318 \def\collargs@m@checkforgroup@i{%
1319 \ifcat\noexpand\collargs@token\bgroup
1320 \expandafter\collargs@m@group
1321 \else
1322 \expandafter\collargs@m@token
1323 \fi
1324 }
1325 % The argument is given in braces, so we put them back around it
1326 % (|\collargs@wrap|) when appending to the storage.
1327 \def\collargs@m@group#1.{%
1328 \collargs@defusecollector##1{%
1329 \collargs@wrap{{####1}}%
1330 \collargs@appendarg{##1}%
1331 \collargs@#1.%
1332 }%
1333 }
1334 % The argument is a single token, we append it to the storage as is.
1335 \def\collargs@m@token#1.{%
1336 \collargs@defusecollector##1{%
1337 \collargs@appendarg{##1}%
1338 \collargs@#1.%
1339 }%
1340 }
1341 % The verbatim mode. Again, we first collect any spaces in front of the
1342 % argument.
1343 \def\collargs@m@verb#1.{%
1344 \collargs@grabspaces{\collargs@m@verb@checkforgroup#1.}%
1345 }
1346 % We want to check whether we're dealing with a braced argument. We're in the
1347 % verbatim mode, but are braces verbatim as well? In other words, are we in
1348 % |verbatim| or |verb| mode? In the latter case, braces are regular, so we
1349 % redirect to the regular mode.
1350 \def\collargs@m@verb@checkforgroup{%
1351 \ifcollargs@verbatimbraces
1352 \expandafter\collargs@m@verb@checkforgroup@i
1353 \else
1354 \expandafter\collargs@m@checkforgroup
1355 \fi
1356 }
1357 % Is the argument in verbatim braces?
1358 \def\collargs@m@verb@checkforgroup@i#1.{%
1359 \def\collargs@m@verb@checkforgroup@ii{\collargs@m@verb@checkforgroup@iii#1.}%
1360 \futurelet\collargs@temp\collargs@m@verb@checkforgroup@ii
1361 }
1362 \def\collargs@m@verb@checkforgroup@iii#1.{%
1363 \expandafter\ifx\collargs@other@bgroup\collargs@temp
1364 % Yes, the argument is in (verbatim) braces.
1365 \expandafter\collargs@m@verb@group
1366 \else
1367 % We need to manually check whether the following token is a (verbatim)
1368 % closing brace, and throw an error if it is.
1369 \expandafter\ifx\collargs@other@egroup\collargs@temp
1370 \expandafter\expandafter\expandafter\collargs@m@verb@egrouperror
1371 \else
1372 % The argument is a single token.
1373 \expandafter\expandafter\expandafter\collargs@m@v@token
1374 \fi
1375 \fi
1376 #1.%
1377 }
1378 \def\collargs@m@verb@egrouperror#1.{%
1379 \PackageError{collargs}{%
1380 Argument of \expandafter\string\collargs@caller\space has an extra
1381 \iffalse{\else\string}}{}%
1382 }
1383 % A single-token verbatim argument.
1384 \def\collargs@m@v@token#1.#2{%
1385 % Is it a control sequence? (Macro |\collargs@cc| recalls the ``outside''
1386 % category code of character |#1|; see
1387 % section~\ref{sec:code:collargs:verbatim}.)
1388 \ifnum\collargs@cc{#2}=0
1389 \expandafter\collargs@m@v@token@cs
1390 \else
1391 \expandafter\collargs@m@token
1392 \fi
1393 #1.#2%
1394 }
1395 % Is it a one-character control sequence?
1396 \def\collargs@m@v@token@cs#1.#2#3{%
1397 \ifnum\collargs@cc{#3}=11
1398 \expandafter\collargs@m@v@token@cs@letter
1399 \else
1400 \expandafter\collargs@m@v@token@cs@nonletter
1401 \fi
1402 #1.#2#3%
1403 }
1404 % Store |\<token>|.
1405 \def\collargs@m@v@token@cs@nonletter#1.#2#3{%
1406 \collargs@appendarg{#2#3}%
1407 \collargs@#1.%
1408 }
1409 % Store |\| to a temporary register, we'll parse the control sequence name now.
1410 \def\collargs@m@v@token@cs@letter#1.#2{%
1411 \collargsArg{#2}%
1412 \def\collargs@tempa{#1}%
1413 \collargs@m@v@token@cs@letter@i
1414 }
1415 % Append a letter to the control sequence.
1416 \def\collargs@m@v@token@cs@letter@i#1{%
1417 \ifnum\collargs@cc{#1}=11
1418 \toksapp\collargsArg{#1}%
1419 \expandafter\collargs@m@v@token@cs@letter@i
1420 \else
1421 % Finish, returning the non-letter to the input stream.
1422 \expandafter\collargs@m@v@token@cs@letter@ii\expandafter#1%
1423 \fi
1424 }
1425 % Store the verbatim control sequence.
1426 \def\collargs@m@v@token@cs@letter@ii{%
1427 \expanded{%
1428 \unexpanded{%
1429 \expandafter\collargs@appendarg\expandafter{\the\collargsArg}%
1430 }%
1431 \noexpand\collargs@\expandonce\collargs@tempa.%
1432 }%
1433 }
1434 % The verbatim mandatory argument is delimited by verbatim braces. We have to
1435 % use the heavy machinery adapted from |cprotect|.
1436 \def\collargs@m@verb@group#1.#2{%
1437 \let\collargs@begintag\collargs@other@bgroup
1438 \let\collargs@endtag\collargs@other@egroup
1439 \def\collargs@tagarg{}%
1440 \def\collargs@commandatend{\collargs@m@verb@group@i#1.}%
1441 \collargs@readContent
1442 }
1443 % This macro appends the result given by the heavy machinery, waiting for us in
1444 % macro |\collargsArg|, to |\collargsArgs|, but not before dressing
1445 % it up (via |\collargs@wrap|) in a pair of verbatim braces.
1446 \def\collargs@m@verb@group@i{%
1447 \edef\collargs@temp{%
1448 \collargs@other@bgroup\unexpanded{##1}\collargs@other@egroup}%
1449 \expandafter\collargs@wrap\expandafter{\collargs@temp}%
1450 \expandafter\collargs@appendarg\expandafter{\the\collargsArg}%
1451 \collargs@
1452 }
1453 % \end{macro}
1454 %
1455 % \begin{macro}{\collargs@g}
1456 % An optional group: same as |m|, but we simply bail out if we don't find the
1457 % group character.
1458 \def\collargs@g{%
1459 \def\collargs@m@token{%
1460 \global\collargs@fix@requestedtrue
1461 \endgroup
1462 \collargs@
1463 }%
1464 \let\collargs@m@v@token\collargs@m@token
1465 \collargs@m
1466 }
1467 % \end{macro}
1468 %
1469 % \begin{macro}{\collargs@G}
1470 % Discard the default and execute |g|.
1471 \def\collargs@G#1{\collargs@g}
1472 % \end{macro}
1473 %
1474 % \begin{macro}{\collargs@v}
1475 % Verbatim argument. The code is executed in the group, deploying
1476 % |\collargsVerbatim|. The grouping characters are always set to braces, to
1477 % mimick |xparse| perfectly.
1478 \def\collargs@v#1.{%
1479 \begingroup
1480 \collargsBraces{{}}%
1481 \collargsVerbatim
1482 \collargs@grabspaces{\collargs@v@i#1.}%
1483 }
1484 \def\collargs@v@i#1.#2{%
1485 \expandafter\ifx\collargs@other@bgroup#2%
1486 % If the first token we see is an opening brace, use the |cprotect|
1487 % adaptation to grab the group.
1488 \let\collargs@begintag\collargs@other@bgroup
1489 \let\collargs@endtag\collargs@other@egroup
1490 \def\collargs@tagarg{}%
1491 \def\collargs@commandatend{%
1492 \edef\collargs@temp{%
1493 \collargs@other@bgroup\unexpanded{####1}\collargs@other@egroup}%
1494 \expandafter\collargs@wrap\expandafter{\collargs@temp}%
1495 \expandafter\collargs@appendarg\expandafter{\the\collargsArg}%
1496 \endgroup
1497 \collargs@#1.%
1498 }%
1499 \expandafter\collargs@readContent
1500 \else
1501 % Otherwise, the verbatim argument is delimited by two identical characters
1502 % (|#2|).
1503 \collargs@defcollector##1#2{%
1504 \collargs@wrap{#2####1#2}%
1505 \collargs@appendarg{##1}%
1506 \endgroup
1507 \collargs@#1.%
1508 }%
1509 \expandafter\collargs@caller
1510 \fi
1511 }
1512 % \end{macro}
1513 %
1514 % \begin{macro}{\collargs@b}
1515 % Environments. Here's where all hell breaks loose. We survive by adapting
1516 % some code from Bruno Le Floch's |cprotect|. We first define the
1517 % environment-related keys, then provide the handler code, and finish with
1518 % the adaptation of |cprotect|'s environment-grabbing code.
1519 %
1520 % The argument type |b| token may be followed by a braced environment name
1521 % (in the argument specification).
1522 \def\collargs@b{%
1523 \collargs@ifnextcat\bgroup\collargs@bg\collargs@bi
1524 }
1525 \def\collargs@bg#1{%
1526 \edef\collargs@b@envname{#1}%
1527 \collargs@bi
1528 }
1529 \def\collargs@bi#1.{%
1530 % Convert the environment name to verbatim if necessary.
1531 \ifcollargs@verbatim
1532 \edef\collargs@b@envname{\detokenize\expandafter{\collargs@b@envname}}%
1533 \fi
1534 % This is a format-specific macro which sets up |\collargs@begintag| and
1535 % |\collargs@endtag|.
1536 \collargs@bi@defCPTbeginend
1537 \edef\collargs@tagarg{%
1538 \ifcollargs@verbatimbraces
1539 \else
1540 \ifcollargsIgnoreOtherTags
1541 \collargs@b@envname
1542 \fi
1543 \fi
1544 }%
1545 % Run this after collecting the body.
1546 \def\collargs@commandatend{%
1547 % In \hologo{LaTeX}, we might, depending on the verbatim mode, need to
1548 % check whether the environment name is correct.
1549 %<latex>\collargs@bii
1550 % In \hologo{plainTeX} and \hologo{ConTeXt}, we can skip directly to
1551 % |\collargs@biii|.
1552 %<plain,context>\collargs@biii
1553 #1.%
1554 }%
1555 % Collect the environment body, but first, put any grabbed spaces back into
1556 % the input stream.
1557 \collargs@reinsert@spaces\collargs@readContent
1558 }
1559 %<*latex>
1560 % In \hologo{LaTeX} in the regular and the partial verbatim mode, we search for
1561 % |\begin|\slash|\end| --- as we cannot search for braces --- either as control
1562 % sequences in the regular mode, or as strings in the partial verbatim
1563 % mode. (After search, we will have to check whether the argument of
1564 % |\begin|\slash|\end| matches our environment name.) In the full verbatim
1565 % mode, we can search for the entire string |\begin|\slash|\end|\marg{name}.
1566 \def\collargs@bi@defCPTbeginend{%
1567 \edef\collargs@begintag{%
1568 \ifcollargs@verbatim
1569 \expandafter\string
1570 \else
1571 \expandafter\noexpand
1572 \fi
1573 \begin
1574 \ifcollargs@verbatimbraces
1575 \collargs@other@bgroup\collargs@b@envname\collargs@other@egroup
1576 \fi
1577 }%
1578 \edef\collargs@endtag{%
1579 \ifcollargs@verbatim
1580 \expandafter\string
1581 \else
1582 \expandafter\noexpand
1583 \fi
1584 \end
1585 \ifcollargs@verbatimbraces
1586 \collargs@other@bgroup\collargs@b@envname\collargs@other@egroup
1587 \fi
1588 }%
1589 }
1590 %</latex>
1591 %<*plain,context>
1592 % We can search for the entire |\|\meta{name}\slash|\end|\meta{name} (in
1593 % \hologo{TeX}) or |\start|\meta{name}\slash|\stop|\meta{name} (in
1594 % \hologo{ConTeXt}), either as a control sequence (in the regular mode), or as
1595 % a string (in the verbatim modes).
1596 \def\collargs@bi@defCPTbeginend{%
1597 \edef\collargs@begintag{%
1598 \ifcollargs@verbatim
1599 \expandafter\expandafter\expandafter\string
1600 \else
1601 \expandafter\expandafter\expandafter\noexpand
1602 \fi
1603 \csname
1604 %<context>start%
1605 \collargs@b@envname
1606 \endcsname
1607 }%
1608 \edef\collargs@endtag{%
1609 \ifcollargs@verbatim
1610 \expandafter\expandafter\expandafter\string
1611 \else
1612 \expandafter\expandafter\expandafter\noexpand
1613 \fi
1614 \csname
1615 %<plain>end%
1616 %<context>stop%
1617 \collargs@b@envname
1618 \endcsname
1619 }%
1620 }
1621 %</plain,context>
1622 %<*latex>
1623 % Check whether we're in front of the (braced) environment name (in
1624 % \hologo{LaTeX}), and consume it.
1625 \def\collargs@bii{%
1626 \ifcollargs@verbatimbraces
1627 \expandafter\collargs@biii
1628 \else
1629 \ifcollargsIgnoreOtherTags
1630 % We shouldn't check the name in this case, because it was already
1631 % checked, and consumed.
1632 \expandafter\expandafter\expandafter\collargs@biii
1633 \else
1634 \expandafter\expandafter\expandafter\collargs@b@checkend
1635 \fi
1636 \fi
1637 }
1638 \def\collargs@b@checkend#1.{%
1639 \collargs@grabspaces{\collargs@b@checkend@i#1.}%
1640 }
1641 \def\collargs@b@checkend@i#1.#2{%
1642 \def\collargs@temp{#2}%
1643 \ifx\collargs@temp\collargs@b@envname
1644 \else
1645 \collargs@b@checkend@error
1646 \fi
1647 \collargs@biii#1.%
1648 }
1649 \def\collargs@b@checkend@error{%
1650 \PackageError{collargs}{Environment "\collargs@b@envname" ended as
1651 "\collargs@temp"}{}%
1652 }
1653 %</latex>
1654 % This macro stores the collected body.
1655 \def\collargs@biii{%
1656 % Define the wrapper macro (|\collargs@temp|).
1657 \collargs@b@def@wrapper
1658 % Execute |\collargs@appendarg| to append the body to the list. Expand the
1659 % wrapper in |\collargs@temp| first and the body in |\collargsArg| next.
1660 \expandafter\collargs@appendarg\expandafter{\the\collargsArg}%
1661 % Reexecute the central loop.
1662 \collargs@
1663 }
1664 \def\collargs@b@def@wrapper{%
1665 %<latex>\edef\collargs@temp{{\collargs@b@envname}}%
1666 \edef\collargs@temp{%
1667 % Was the begin-tag requested?
1668 \ifcollargsBeginTag
1669 % |\collargs@begintag| is already adapted to the format and the verbatim mode.
1670 \expandonce\collargs@begintag
1671 % Add the braced environment name in \hologo{LaTeX} in the regular and
1672 % partial verbatim mode.
1673 %
1674 %<*latex>
1675 \ifcollargs@verbatimbraces\else\collargs@temp\fi
1676 %</latex>
1677 \fi
1678 % This is the body.
1679 ####1%
1680 % Rinse and repeat for the end-tag.
1681 \ifcollargsEndTag
1682 \expandonce\collargs@endtag
1683 %<*latex>
1684 \ifcollargs@verbatimbraces\else\collargs@temp\fi
1685 %</latex>
1686 \fi
1687 }%
1688 \expandafter\collargs@wrap\expandafter{\collargs@temp}%
1689 }
1690 % \end{macro}
1691 %
1692 % \begin{macro}{\collargs@readContent}
1693 % This macro, which is an adaptation of
1694 % \texttt{cprotect}'s environment-grabbing code, collects some delimited
1695 % text, leaving the result in |\collargsArg|. Before calling it, one must
1696 % define the following macros: |\collargs@begintag| and |\collargs@endtag|
1697 % are the content delimiters; |\collargs@tagarg|, if non-empty, is the token
1698 % or grouped text which must follow a delimiter to be taken into account;
1699 % |\collargs@commandatend| is the command that will be executed once the
1700 % content is collected.
1701 \def\collargs@readContent{%
1702 % Define macro which will search for the first begin-tag.
1703 \ifcollargs@long\long\fi
1704 \collargs@CPT@def\collargs@gobbleOneB\collargs@begintag{%
1705 % Assign the collected tokens into a register. The first token in |##1|
1706 % will be |\collargs@empty|, so we expand to get rid of it.
1707 \toks0\expandafter{##1}%
1708 % |cprotect| simply grabs the token following the |\collargs@begintag| with
1709 % a parameter. We can't do this, because we need the code to work in the
1710 % non-verbatim mode, as well, and we might stumble upon a brace there. So
1711 % we take a peek.
1712 \futurelet\collargs@temp\collargs@gobbleOneB@i
1713 }%
1714 % Define macro which will search for the first end-tag. We make it long if
1715 % so required (by |+|).
1716 \ifcollargs@long\long\fi
1717 \collargs@CPT@def\collargs@gobbleUntilE\collargs@endtag{%
1718 % Expand |\collargs@empty| at the start of |##1|.
1719 \expandafter\toksapp\expandafter0\expandafter{##1}%
1720 \collargs@gobbleUntilE@i
1721 }%
1722 % Initialize.
1723 \collargs@begins=0\relax
1724 \collargsArg{}%
1725 \toks0{}%
1726 % We will call |\collargs@gobbleUntilE| via the caller control sequence.
1727 \collargs@letusecollector\collargs@gobbleUntilE
1728 % We insert |\collargs@empty| to avoid the potential debracing problem.
1729 \collargs@empty
1730 }
1731 % How many begin-tags do we have opened?
1732 \newcount\collargs@begins
1733 % An auxiliary macro which |\def|s |#1| so that it will grab everything up
1734 % until |#2|. Additional parameters may be present before the definition.
1735 \def\collargs@CPT@def#1#2{%
1736 \expandafter\def\expandafter#1%
1737 \expandafter##\expandafter1#2%
1738 }
1739 % A quark quard.
1740 \def\collargs@qend{\collargs@qend}
1741 % This macro will collect the ``environment'', leaving the result in
1742 % |\collargsArg|. It expects |\collargs@begintag|, |\collargs@endtag| and
1743 % |\collargs@commandatend| to be set.
1744 \def\collargs@gobbleOneB@i{%
1745 \def\collargs@begins@increment{1}%
1746 \ifx\collargs@qend\collargs@temp
1747 % We have reached the fake begin-tag. Note that we found the end-tag.
1748 \def\collargs@begins@increment{-1}%
1749 % Gobble the quark guard.
1750 \expandafter\collargs@gobbleOneB@v
1751 \else
1752 % Append the real begin-tag to the temporary tokens.
1753 \etoksapp0{\expandonce\collargs@begintag}%
1754 \expandafter\collargs@gobbleOneB@ii
1755 \fi
1756 }%
1757 % Do we have to check the tag argument (i.e.\ the environment name after |\begin|)?
1758 \def\collargs@gobbleOneB@ii{%
1759 \expandafter\ifx\expandafter\relax\collargs@tagarg\relax
1760 \expandafter\collargs@gobbleOneB@vi
1761 \else
1762 % Yup, so let's (carefully) collect the tag argument.
1763 \expandafter\collargs@gobbleOneB@iii
1764 \fi
1765 }
1766 \def\collargs@gobbleOneB@iii{%
1767 \collargs@grabspaces{%
1768 \collargs@letusecollector\collargs@gobbleOneB@iv
1769 }%
1770 }
1771 \def\collargs@gobbleOneB@iv#1{%
1772 \def\collargs@temp{#1}%
1773 \ifx\collargs@temp\collargs@tagarg
1774 % This is the tag argument we've been waiting for!
1775 \else
1776 % Nope, this |\begin| belongs to someone else.
1777 \def\collargs@begins@increment{0}%
1778 \fi
1779 % Whatever the result was, we have to append the gobbled group to the
1780 % temporary toks.
1781 \etoksapp0{\collargs@grabbed@spaces\unexpanded{{#1}}}%
1782 \collargs@init@grabspaces
1783 \collargs@gobbleOneB@vi
1784 }
1785 \def\collargs@gobbleOneB@v#1{\collargs@gobbleOneB@vi}
1786 \def\collargs@gobbleOneB@vi{%
1787 % Store.
1788 \etoksapp\collargsArg{\the\toks0}%
1789 % Advance the begin-tag counter.
1790 \advance\collargs@begins\collargs@begins@increment\relax
1791 % Find more begin-tags, unless this was the final one.
1792 \ifnum\collargs@begins@increment=-1
1793 \else
1794 \expandafter\collargs@gobbleOneB\expandafter\collargs@empty
1795 \fi
1796 }
1797 \def\collargs@gobbleUntilE@i{%
1798 % Do we have to check the tag argument (i.e.\ the environment name after |\end|)?
1799 \expandafter\ifx\expandafter\relax\collargs@tagarg\relax
1800 \expandafter\collargs@gobbleUntilE@iv
1801 \else
1802 % Yup, so let's (carefully) collect the tag argument.
1803 \expandafter\collargs@gobbleUntilE@ii
1804 \fi
1805 }
1806 \def\collargs@gobbleUntilE@ii{%
1807 \collargs@grabspaces{%
1808 \collargs@letusecollector\collargs@gobbleUntilE@iii
1809 }%
1810 }
1811 \def\collargs@gobbleUntilE@iii#1{%
1812 \etoksapp0{\collargs@grabbed@spaces}%
1813 \collargs@init@grabspaces
1814 \def\collargs@tempa{#1}%
1815 \ifx\collargs@tempa\collargs@tagarg
1816 % This is the tag argument we've been waiting for!
1817 \expandafter\collargs@gobbleUntilE@iv
1818 \else
1819 % Nope, this |\end| belongs to someone else. Insert the end tag plus the
1820 % tag argument, and collect until the next |\end|.
1821 \expandafter\toksapp\expandafter0\expandafter{\collargs@endtag{#1}}%
1822 \expandafter\collargs@letusecollector\expandafter\collargs@gobbleUntilE
1823 \fi
1824 }
1825 \def\collargs@gobbleUntilE@iv{%
1826 % Invoke |\collargs@gobbleOneB| with the collected material, plus a fake
1827 % begin-tag and a quark guard.
1828 \ifcollargsIgnoreNesting
1829 \expandafter\collargsArg\expandafter{\the\toks0}%
1830 \expandafter\collargs@commandatend
1831 \else
1832 \expandafter\collargs@gobbleUntilE@v
1833 \fi
1834 }
1835 \def\collargs@gobbleUntilE@v{%
1836 \expanded{%
1837 \noexpand\collargs@letusecollector\noexpand\collargs@gobbleOneB
1838 \noexpand\collargs@empty
1839 \the\toks0
1840 % Add a fake begin-tag and a quark guard.
1841 \expandonce\collargs@begintag
1842 \noexpand\collargs@qend
1843 }%
1844 \ifnum\collargs@begins<0
1845 \expandafter\collargs@commandatend
1846 \else
1847 \etoksapp\collargsArg{%
1848 \expandonce\collargs@endtag
1849 \expandafter\ifx\expandafter\relax\collargs@tagarg\relax\else{%
1850 \expandonce\collargs@tagarg}\fi
1851 }%
1852 \toks0={}%
1853 \expandafter\collargs@letusecollector\expandafter\collargs@gobbleUntilE
1854 \expandafter\collargs@empty
1855 \fi
1856 }
1857 % \end{macro}
1858 %
1859 % \begin{macro}{\collargs@e}
1860 % Embellishments. Each embellishment counts as an argument, in the sense
1861 % that we will execute |\collargs@appendarg|, with all the processors, for
1862 % each embellishment separately.
1863 \def\collargs@e{%
1864 % We open an extra group, because |\collargs@appendarg| will close a group
1865 % for each embellishment.
1866 \global\collargs@fix@requestedtrue
1867 \begingroup
1868 \ifcollargs@verbatim
1869 \expandafter\collargs@e@verbatim
1870 \else
1871 \expandafter\collargs@e@i
1872 \fi
1873 }
1874 % Detokenize the embellishment tokens in the verbatim mode.
1875 \def\collargs@e@verbatim#1{%
1876 \expandafter\collargs@e@i\expandafter{\detokenize{#1}}%
1877 }
1878 % Ungroup the embellishment tokens, separating them from the rest of the
1879 % argument specification by a dot.
1880 \def\collargs@e@i#1{\collargs@e@ii#1.}
1881 % We now have embellishment tokens in |#1| and the rest of the argument
1882 % specification in |#2|. Let's grab spaces first.
1883 \def\collargs@e@ii#1.#2.{%
1884 \collargs@grabspaces{\collargs@e@iii#1.#2.}%
1885 }
1886 % What's the argument token?
1887 \def\collargs@e@iii#1.#2.{%
1888 \def\collargs@e@iv{\collargs@e@v#1.#2.}%
1889 \futurelet\collargs@temp\collargs@e@iv
1890 }
1891 % If it is a open or close group character, we surely don't have an
1892 % embellishment.
1893 \def\collargs@e@v{%
1894 \ifcat\noexpand\collargs@temp\bgroup\relax
1895 \let\collargs@marshal\collargs@e@z
1896 \else
1897 \ifcat\noexpand\collargs@temp\egroup\relax
1898 \let\collargs@marshal\collargs@e@z
1899 \else
1900 \let\collargs@marshal\collargs@e@vi
1901 \fi
1902 \fi
1903 \collargs@marshal
1904 }
1905 % We borrow the ``Does |#1| occur within |#2|?'' macro from |pgfutil-common|,
1906 % but we fix it by executing |\collargs@in@@| in a braced group. This will
1907 % prevent an |&| in an argument to function as an alignment character; the
1908 % minor price to pay is that we assign the conditional globally.
1909 \newif\ifcollargs@in@
1910 \def\collargs@in@#1#2{%
1911 \def\collargs@in@@##1#1##2##3\collargs@in@@{%
1912 \ifx\collargs@in@##2\global\collargs@in@false\else\global\collargs@in@true\fi
1913 }%
1914 {\collargs@in@@#2#1\collargs@in@\collargs@in@@}%
1915 }
1916 % Let' see whether the following token, now |#3|, is an embellishment token.
1917 \def\collargs@e@vi#1.#2.#3{%
1918 \collargs@in@{#3}{#1}%
1919 \ifcollargs@in@
1920 \expandafter\collargs@e@vii
1921 \else
1922 \expandafter\collargs@e@z
1923 \fi
1924 #1.#2.#3%
1925 }
1926 % |#3| is the current embellishment token. We'll collect its argument using
1927 % |\collargs@m|, but to do that, we have to (locally) redefine
1928 % |\collargs@appendarg| and |\collargs@|, which get called by |\collargs@m|.
1929 \def\collargs@e@vii#1.#2.#3{%
1930 % We'll have to execute the original |\collargs@appendarg| later, so let's
1931 % remember it. The temporary |\collargs@appendarg| simply stores the
1932 % collected argument into |\collargsArg| --- we'll do the processing etc.\
1933 % later.
1934 \let\collargs@real@appendarg\collargs@appendarg
1935 \def\collargs@appendarg##1{\collargsArg{##1}}%
1936 % Once |\collargs@m| is done, it will call the redefined |\collargs@| and
1937 % thereby get us back into this handler.
1938 \def\collargs@{\collargs@e@viii#1.#3}%
1939 \collargs@m#2.%
1940 }
1941 % The parameters here are as follows. |#1| are the embellishment tokens, and
1942 % |#2| is the current embellishment token; these get here via our local
1943 % redefinition of |\collargs@| in |\collargs@e@vii|. |#3| are the rest of the
1944 % argument specification, which is put behind control sequence |\collargs@| by
1945 % the |m| handler.
1946 \def\collargs@e@viii#1.#2#3.{%
1947 % Our wrapper puts the current embellishment token in front of the collected
1948 % embellishment argument. Note that if the embellishment argument was in
1949 % braces, |\collargs@m| has already set one wrapper (which will apply first).
1950 \collargs@wrap{#2##1}%
1951 % We need to get rid of the current embellishment from embellishments, not to
1952 % catch the same embellishment twice.
1953 \def\collargs@e@ix##1#2{\collargs@e@x##1}%
1954 \collargs@e@ix#1.#3.%
1955 }
1956 % When this is executed, the input stream starts with the (remaining)
1957 % embellishment tokens, followed by a dot, then the rest of the argument
1958 % specification, also followed by a dot.
1959 \def\collargs@e@x{%
1960 % Process the argument and append it to the storage.
1961 \expandafter\collargs@real@appendarg\expandafter{\the\collargsArg}%
1962 % |\collargs@real@appendarg| has closed a group, so we open it again, and
1963 % start looking for another embellishment token in the input stream.
1964 \begingroup
1965 \collargs@e@ii
1966 }
1967 % The first argument token in not an embellishment token. We finish by
1968 % consuming the list of embellishment tokens, closing the two groups opened by
1969 % this handler, and reexecuting the central loop.
1970 \def\collargs@e@z#1.{\endgroup\endgroup\collargs@}
1971 % \end{macro}
1972 %
1973 % \begin{macro}{\collargs@E}
1974 % Discard the defaults and execute |e|.
1975 \def\collargs@E#1#2{\collargs@e{#1}}
1976 % \end{macro}
1977 %
1978 %
1979 % \subsubsection{The verbatim modes}
1980 % \label{sec:code:collargs:verbatim}
1981 %
1982 % \begin{macro}{\collargsVerbatim,\collargsVerb,\collargsNoVerbatim}
1983 % These macros set the two verbatim-related
1984 % conditionals, |\ifcollargs@verbatim| and |\ifcollargs@verbatimbraces|,
1985 % and then call |\collargs@make@verbatim| to effect the requested
1986 % category code changes (among other things). A group should be opened
1987 % prior to executing either of them. After execution, they are
1988 % redefined to minimize the effort needed to enter into another mode in
1989 % an embedded group. Below, we first define all the possible transitions.
1990 \let\collargs@NoVerbatimAfterNoVerbatim\relax
1991 \def\collargs@VerbAfterNoVerbatim{%
1992 \collargs@verbatimtrue
1993 \collargs@verbatimbracesfalse
1994 \collargs@make@verbatim
1995 \collargs@after{Verb}%
1996 }
1997 \def\collargs@VerbatimAfterNoVerbatim{%
1998 \collargs@verbatimtrue
1999 \collargs@verbatimbracestrue
2000 \collargs@make@verbatim
2001 \collargs@after{Verbatim}%
2002 }
2003 \def\collargs@NoVerbatimAfterVerb{%
2004 \collargs@verbatimfalse
2005 \collargs@verbatimbracesfalse
2006 \collargs@make@other@groups
2007 \collargs@make@no@verbatim
2008 \collargs@after{NoVerbatim}%
2009 }
2010 \def\collargs@VerbAfterVerb{%
2011 \collargs@make@other@groups
2012 }
2013 \def\collargs@VerbatimAfterVerb{%
2014 \collargs@verbatimbracestrue
2015 \collargs@make@other@groups
2016 % Process the lists of grouping characters, created by
2017 % |\collargs@make@verbatim|, making these characters of category ``other''.
2018 \def\collargs@do##1{\catcode##1=12 }%
2019 \collargs@bgroups
2020 \collargs@egroups
2021 \collargs@after{Verbatim}%
2022 }%
2023 \let\collargs@NoVerbatimAfterVerbatim\collargs@NoVerbatimAfterVerb
2024 \def\collargs@VerbAfterVerbatim{%
2025 \collargs@verbatimbracesfalse
2026 \collargs@make@other@groups
2027 % Process the lists of grouping characters, created by
2028 % |\collargs@make@verbatim|, making these characters be of their normal
2029 % category.
2030 \def\collargs@do##1{\catcode##1=1 }%
2031 \collargs@bgroups
2032 \def\collargs@do##1{\catcode##1=2 }%
2033 \collargs@egroups
2034 \collargs@after{Verb}%
2035 }%
2036 \let\collargs@VerbatimAfterVerbatim\collargs@VerbAfterVerb
2037 % This macro expects |#1| to be the mode just entered (|Verbatim|, |Verb| or
2038 % |NoVerbatim|), and points macros |\collargsVerbatim|, |\collargsVerb| and
2039 % |\collargsNoVerbatim| to the appropriate transition macro.
2040 \def\collargs@after#1{%
2041 \letcs\collargsVerbatim{collargs@VerbatimAfter#1}%
2042 \letcs\collargsVerb{collargs@VerbAfter#1}%
2043 \letcs\collargsNoVerbatim{collargs@NoVerbatimAfter#1}%
2044 }
2045 % The first transition is always from the non-verbatim mode.
2046 \collargs@after{NoVerbatim}
2047 % \end{macro}
2048 %
2049 % \begin{macro}{\collargs@bgroups,\collargs@egroups}
2050 % Initialize the lists of the current grouping characters used in the
2051 % redefinitions of macros |\collargsVerbatim| and |\collargsVerb| above.
2052 % Each entry is of form |\collargs@do|\marg{character code}. These lists
2053 % will be populated by |\collargs@make@verbatim|. They may be local, as they
2054 % only used within the group opened for a verbatim environment.
2055 \def\collargs@bgroups{}%
2056 \def\collargs@egroups{}%
2057 % \end{macro}
2058 %
2059 % \begin{macro}{\collargs@cc}
2060 % This macro recalls the category code of character |#1|. In
2061 % \hologo{LuaTeX}, we simply look up the category code in the original
2062 % category code table; in other engines, we have stored the original category
2063 % code into |\collargs@cc@|\meta{character code} by
2064 % |\collargs@make@verbatim|. (Note that |#1| is a character, not a number.)
2065 \ifdefined\luatexversion
2066 \def\collargs@cc#1{%
2067 \directlua{tex.sprint(tex.getcatcode(\collargs@catcodetable@original,
2068 \the\numexpr\expandafter`\csname#1\endcsname\relax))}%
2069 }
2070 \else
2071 \def\collargs@cc#1{%
2072 \ifcsname collargs@cc@\the\numexpr\expandafter`\csname#1\endcsname\endcsname
2073 \csname collargs@cc@\the\numexpr\expandafter`\csname#1\endcsname\endcsname
2074 \else
2075 12%
2076 \fi
2077 }
2078 \fi
2079 % \end{macro}
2080 %
2081 %
2082 % \begin{macro}{\collargs@other@bgroup,\collargs@other@egroup,\collargsBraces}
2083 % Macros |\collargs@other@bgroup| and
2084 % |\collargs@other@egroup| hold the characters of category code ``other''
2085 % which will play the role of grouping characters in the full verbatim mode.
2086 % They are usually defined when entering a verbatim mode in
2087 % |\collargs@make@verbatim|, but may be also set by the user via
2088 % |\collargsBraces| (it is not even necessary to select characters which
2089 % indeed have the grouping function in the outside category code regime).
2090 % The setting process is indirect: executing |\collargsBraces| merely sets
2091 % |\collargs@make@other@groups|, which gets executed by the subsequent
2092 % |\collargsVerbatim|, |\collargsVerb| or |\collargsNoVerbatim| (either
2093 % directly or via |\collargs@make@verbatim|).
2094 \def\collargsBraces#1{%
2095 \expandafter\collargs@braces@i\detokenize{#1}\relax
2096 }
2097 \def\collargs@braces@i#1#2#3\relax{%
2098 \def\collargs@make@other@groups{%
2099 \def\collargs@other@bgroup{#1}%
2100 \def\collargs@other@egroup{#2}%
2101 }%
2102 }
2103 \def\collargs@make@other@groups{}
2104 % \end{macro}
2105 %
2106 % \begin{macro}{\collargs@catcodetable@verbatim,\catcodetable@atletter,\collargs@catcodetable@initex}
2107 % We declare several new catcode tables in \hologo{LuaTeX}, the most
2108 % important one being |\collargs@catcodetable@verbatim|, where all characters
2109 % have category code 12. We only need the other two tables in some formats:
2110 % |\collargs@catcodetable@atletter| holds the catcode in effect at the time
2111 % of loading the package, and |\collargs@catcodetable@initex| is the
2112 % \hologo{iniTeX} table.
2113 \ifdefined\luatexversion
2114 %<*latex,context>
2115 \newcatcodetable\collargs@catcodetable@verbatim
2116 %<latex>\let\collargs@catcodetable@atletter\catcodetable@atletter
2117 %<context>\newcatcodetable\collargs@catcodetable@atletter
2118 %</latex,context>
2119 %<*plain>
2120 \ifdefined\collargs@catcodetable@verbatim\else
2121 \chardef\collargs@catcodetable@verbatim=4242
2122 \fi
2123 \chardef\collargs@catcodetable@atletter=%
2124 \number\numexpr\collargs@catcodetable@verbatim+1\relax
2125 \chardef\collargs@catcodetable@initex=%
2126 \number\numexpr\collargs@catcodetable@verbatim+2\relax
2127 \initcatcodetable\collargs@catcodetable@initex
2128 %</plain>
2129 %<plain,context>\savecatcodetable\collargs@catcodetable@atletter
2130 \begingroup
2131 \@firstofone{%
2132 %<latex>\catcodetable\catcodetable@initex
2133 %<plain>\catcodetable\collargs@catcodetable@initex
2134 %<context>\catcodetable\inicatcodes
2135 \catcode`\\=12
2136 \catcode13=12
2137 \catcode0=12
2138 \catcode32=12
2139 \catcode`\%=12
2140 \catcode127=12
2141 \def\collargs@do#1{\catcode#1=12 }%
2142 \collargs@forrange{`\a}{`\z}%
2143 \collargs@forrange{`\A}{`\Z}%
2144 \savecatcodetable\collargs@catcodetable@verbatim
2145 \endgroup
2146 }%
2147 \fi
2148 % \end{macro}
2149 %
2150 % \begin{collargskey}{verbatim ranges}
2151 % \begin{macro}{\collargsVerbatimRanges,\collargs@verbatim@ranges}
2152 % This key and macro set the character ranges to which the verbatim mode
2153 % will apply (in \hologo{pdfTeX} and \hologo{XeTeX}), or which will be
2154 % inspected for grouping and comment characters (in \hologo{LuaTeX}). In
2155 % \hologo{pdfTeX}, the default value |0-255| should really remain
2156 % unchanged.
2157 \collargsSet{
2158 verbatim ranges/.store in=\collargs@verbatim@ranges,
2159 }
2160 \def\collargsVerbatimRanges#1{\def\collargs@verbatim@ranges{#1}}
2161 \def\collargs@verbatim@ranges{0-255}
2162 % \end{macro}
2163 % \end{collargskey}
2164 %
2165 % \begin{macro}{\collargs@make@verbatim}
2166 % This macro changes the category code of all characters to ``other'' ---
2167 % except the grouping characters in the partial verbatim mode. While doing
2168 % that, it also stores (unless we're in \hologo{LuaTeX}) the current category
2169 % codes into |\collargs@cc@|\meta{character code} (easily recallable by
2170 % |\collargs@cc|), redefines the ``primary'' grouping characters
2171 % |\collargs@make@other@bgroup| and |\collargs@make@other@egroup| if
2172 % necessary, and ``remembers'' the grouping characters (storing them into
2173 % |\collargs@bgroups| and |\collargs@egroups|) and the comment characters
2174 % (storing them into |\collargs@comments|).
2175 %
2176 % In \hologo{LuaTeX}, we can use catcode tables, so we change the category
2177 % codes by switching to category code table
2178 % |\collargs@catcodetable@verbatim|. In other engines, we have to change the
2179 % codes manually. In order to offer some flexibility in \hologo{XeTeX}, we
2180 % perform the change for characters in |verbatim ranges|.
2181 \ifdefined\luatexversion
2182 \def\collargs@make@verbatim{%
2183 \directlua{%
2184 for from, to in string.gmatch(
2185 "\luaescapestring{\collargs@verbatim@ranges}",
2186 "(\collargs@percentchar d+)-(\collargs@percentchar d+)"
2187 ) do
2188 for char = tex.round(from), tex.round(to) do
2189 catcode = tex.catcode[char]
2190 % For category codes 1, 2 and 14, we have to call macros
2191 % |\collargs@make@verbatim@bgroup|, |\collargs@make@verbatim@egroup|
2192 % and |\collargs@make@verbatim@comment|, same as for engines other
2193 % than \hologo{LuaTeX}.
2194 if catcode == 1 then
2195 tex.sprint(
2196 \number\collargs@catcodetable@atletter,
2197 "\noexpand\\collargs@make@verbatim@bgroup{" .. char .. "}")
2198 elseif catcode == 2 then
2199 tex.sprint(
2200 \number\collargs@catcodetable@atletter,
2201 "\noexpand\\collargs@make@verbatim@egroup{" .. char .. "}")
2202 elseif catcode == 14 then
2203 tex.sprint(
2204 \number\collargs@catcodetable@atletter,
2205 "\noexpand\\collargs@make@verbatim@comment{" .. char .. "}")
2206 end
2207 end
2208 end
2209 }%
2210 \edef\collargs@catcodetable@original{\the\catcodetable}%
2211 \catcodetable\collargs@catcodetable@verbatim
2212 % Even in \hologo{LuaTeX}, we switch between the verbatim braces regimes by
2213 % hand.
2214 \ifcollargs@verbatimbraces
2215 \else
2216 \def\collargs@do##1{\catcode##1=1\relax}%
2217 \collargs@bgroups
2218 \def\collargs@do##1{\catcode##1=2\relax}%
2219 \collargs@egroups
2220 \fi
2221 }
2222 \else
2223 % The non-\hologo{LuaTeX} version:
2224 \def\collargs@make@verbatim{%
2225 \ifdefempty\collargs@make@other@groups{}{%
2226 % The user has executed |\collargsBraces|. We first apply that setting
2227 % by executing macro |\collargs@make@other@groups|, and then disable our
2228 % automatic setting of the primary grouping characters.
2229 \collargs@make@other@groups
2230 \def\collargs@make@other@groups{}%
2231 \let\collargs@make@other@bgroup\@gobble
2232 \let\collargs@make@other@egroup\@gobble
2233 }%
2234 % Initialize the list of current comment characters. Each entry is of
2235 % form |\collargs@do|\marg{character code}. The definition must be global,
2236 % because the macro will be used only once we exit the current group (by
2237 % |\collargs@fix@cc@from@other@comment|, if at all).
2238 \gdef\collargs@comments{}%
2239 \let\collargs@do\collargs@make@verbatim@char
2240 \expandafter\collargs@forranges\expandafter{\collargs@verbatim@ranges}%
2241 }
2242 \def\collargs@make@verbatim@char#1{%
2243 % Store the current category code of the current character.
2244 \ifnum\catcode#1=12
2245 \else
2246 \csedef{collargs@cc@#1}{\the\catcode#1}%
2247 \fi
2248 \ifnum\catcode#1=1
2249 \collargs@make@verbatim@bgroup{#1}%
2250 \else
2251 \ifnum\catcode#1=2
2252 \collargs@make@verbatim@egroup{#1}%
2253 \else
2254 \ifnum\catcode#1=14
2255 \collargs@make@verbatim@comment{#1}%
2256 \fi
2257 % Change the category code of the current character (including the
2258 % comment characters).
2259 \ifnum\catcode#1=12
2260 \else
2261 \catcode#1=12\relax
2262 \fi
2263 \fi
2264 \fi
2265 }
2266 \fi
2267 % \end{macro}
2268 %
2269 % \begin{macro}{\collargs@make@verbatim@bgroup}
2270 % This macro changes the category of
2271 % the opening group character to ``other'', but only in the full verbatim
2272 % mode. Next, it populates |\collargs@bgroups|, to facilitate the potential
2273 % transition into the other verbatim mode. Finally, it executes
2274 % |\collargs@make@other@bgroup|, which stores the ``other'' variant of the
2275 % current character into |\collargs@other@bgroup|, and automatically disables
2276 % itself, so that it is only executed for the first encountered opening group
2277 % character --- unless it was already |\relax|ed at the top of
2278 % |\collargs@make@verbatim| as a consequence of the user executing
2279 % |\collargsBraces|.
2280 \def\collargs@make@verbatim@bgroup#1{%
2281 \ifcollargs@verbatimbraces
2282 \catcode#1=12\relax
2283 \fi
2284 \appto\collargs@bgroups{\collargs@do{#1}}%
2285 \collargs@make@other@bgroup{#1}%
2286 }
2287 \def\collargs@make@other@bgroup#1{%
2288 \collargs@make@char\collargs@other@bgroup{#1}{12}%
2289 \let\collargs@make@other@bgroup\@gobble
2290 }
2291 % \end{macro}
2292 %
2293 % \begin{macro}{\collargs@make@verbatim@egroup}
2294 % Ditto for the closing group character.
2295 \def\collargs@make@verbatim@egroup#1{%
2296 \ifcollargs@verbatimbraces
2297 \catcode#1=12\relax
2298 \fi
2299 \appto\collargs@egroups{\collargs@do{#1}}%
2300 \collargs@make@other@egroup{#1}%
2301 }
2302 \def\collargs@make@other@egroup#1{%
2303 \collargs@make@char\collargs@other@egroup{#1}{12}%
2304 \let\collargs@make@other@egroup\@gobble
2305 }
2306 % \end{macro}
2307 %
2308 % \begin{macro}{\collargs@make@verbatim@comment}
2309 % This macro populates
2310 % |\collargs@make@comments@other|.
2311 \def\collargs@make@verbatim@comment#1{%
2312 \gappto\collargs@comments{\collargs@do{#1}}%
2313 }
2314 % \end{macro}
2315 %
2316 % \begin{macro}{\collargs@make@no@verbatim}
2317 % This macro switches back to the non-verbatim mode: in \hologo{LuaTeX}, by
2318 % switching to the original catcode table; in other engines, by recalling the
2319 % stored category codes.
2320 \ifdefined\luatexversion
2321 \def\collargs@make@no@verbatim{%
2322 \catcodetable\collargs@catcodetable@original\relax
2323 }%
2324 \else
2325 \def\collargs@make@no@verbatim{%
2326 \let\collargs@do\collargs@make@no@verbatim@char
2327 \expandafter\collargs@forranges\expandafter{\collargs@verbatim@ranges}%
2328 }
2329 \fi
2330 \def\collargs@make@no@verbatim@char#1{%
2331 % The original category code of a characted was stored into
2332 % |\collargs@cc@|\meta{character code} by |\collargs@make@verbatim|. (We don't
2333 % use |\collargs@cc|, because we have a number.)
2334 \ifcsname collargs@cc@#1\endcsname
2335 \catcode#1=\csname collargs@cc@#1\endcsname\relax
2336 % We don't have to restore category code 12.
2337 \fi
2338 }
2339 % \end{macro}
2340 %
2341 %
2342 % \subsubsection{Transition between the verbatim and the non-verbatim mode}
2343 % \label{sec:code:collargs:fix}
2344 %
2345 % At the transition from verbatim to non-verbatim mode, and vice versa, we
2346 % sometimes have to fix the category code of the next argument token. This
2347 % happens when we have an optional argument type in one mode followed by an
2348 % argument type in another mode, but the optional argument is absent, or when
2349 % an optional, but absent, verbatim argument is the last argument in the
2350 % specification. The problem arises because the presence of optional arguments
2351 % is determined by looking ahead in the input stream; when the argument is
2352 % absent, this means that we have fixed the category code of the next token.
2353 % CollArgs addresses this issue by noting the situations where a token receives
2354 % the wrong category code, and then does its best to replace that token with
2355 % the same character of the appropriate category code.
2356 %
2357 % \begin{macro}{\ifcollargs@fix@requested}
2358 % This conditional is set, globally,
2359 % by the optional argument handlers when the argument is in fact absent,
2360 % and reset in the central loop after applying the fix if necessary.
2361 \newif\ifcollargs@fix@requested
2362 % \end{macro}
2363 %
2364 % \begin{macro}{\collargs@fix}
2365 % This macro selects the fixer appropriate to the transition between the
2366 % previous verbatim mode (determined by |\ifcollargs@last@verbatim| and
2367 % |\ifcollargs@last@verbatimbraces|) and the current verbatim mode (which
2368 % is determined by macros |\ifcollargs@verbatim| and
2369 % |\ifcollargs@verbatimbraces|); if the category code fix was not
2370 % requested (for this, we check |\ifcollargs@fix@requested|), the
2371 % macro simply executes the next-code given as the sole argument.
2372 % The name of the fixer macro has the form
2373 % |\collargs@fix@|\meta{last mode}|to|\meta{current mode}, where
2374 % the modes are given by mnemonic codes: |V| = full verbatim, |v| =
2375 % partial verbatim, and |N| = non-verbatim.
2376 \long\def\collargs@fix#1{%
2377 % Going through |\edef| + |\unexpanded| avoids doubling the hashes.
2378 \edef\collargs@fix@next{\unexpanded{#1}}%
2379 \ifcollargs@fix@requested
2380 \letcs\collargs@action{collargs@fix@%
2381 \ifcollargs@last@verbatim
2382 \ifcollargs@last@verbatimbraces V\else v\fi
2383 \else
2384 N%
2385 \fi
2386 to%
2387 \ifcollargs@verbatim
2388 \ifcollargs@verbatimbraces V\else v\fi
2389 \else
2390 N%
2391 \fi
2392 }%
2393 \else
2394 \let\collargs@action\collargs@fix@next
2395 \fi
2396 \collargs@action
2397 }
2398 % \end{macro}
2399 %
2400 % \begin{macro}{\collargs@fix@NtoN,\collargs@fix@vtov,\collargs@fix@VtoV}
2401 % \indentmacrocode
2402 % Nothing to do, continue with the next-code.
2403 \def\collargs@fix@NtoN{\collargs@fix@next}
2404 \let\collargs@fix@vtov\collargs@fix@NtoN
2405 \let\collargs@fix@VtoV\collargs@fix@NtoN
2406 % \end{macro}
2407 %
2408 % \begin{macro}{\collargs@fix@Ntov}
2409 % We do nothing for the group tokens; for other tokens, we redirect to
2410 % |\collargs@fix@NtoV|.
2411 \def\collargs@fix@Ntov{%
2412 \futurelet\collargs@temp\collargs@fix@cc@to@other@ii
2413 }
2414 \def\collargs@fix@cc@to@other@ii{%
2415 \ifcat\noexpand\collargs@temp\bgroup
2416 \let\collargs@action\collargs@fix@next
2417 \else
2418 \ifcat\noexpand\collargs@temp\egroup
2419 \let\collargs@action\collargs@fix@next
2420 \else
2421 \let\collargs@action\collargs@fix@NtoV
2422 \fi
2423 \fi
2424 \collargs@action
2425 }
2426 % \end{macro}
2427 %
2428 % \begin{macro}{\collargs@fix@NtoV}
2429 % The only complication here is that we might be in front of a control
2430 % sequence that was a result of a previous fix in the other direction.
2431 \def\collargs@fix@NtoV{%
2432 \ifcollargs@double@fix
2433 \ifcollargs@in@second@fix
2434 \expandafter\expandafter\expandafter\collargs@fix@NtoV@secondfix
2435 \else
2436 \expandafter\expandafter\expandafter\collargs@fix@NtoV@onemore
2437 \fi
2438 \else
2439 \expandafter\collargs@fix@NtoV@singlefix
2440 \fi
2441 }
2442 % This is the usual situation of a single fix. We just use |\string| on
2443 % the next token here (but note that some situations can't be saved: noone
2444 % can bring a comment back to life, or distinguish a newline and a space)
2445 \def\collargs@fix@NtoV@singlefix{%
2446 \expandafter\collargs@fix@next\string
2447 }
2448 % If this is the first fix of two, we know |#1| is a control sequence, so it is
2449 % safe to grab it.
2450 \def\collargs@fix@NtoV@onemore#1{%
2451 \collargs@do@one@more@fix{%
2452 \expandafter\collargs@fix@next\string#1%
2453 }%
2454 }
2455 % If this is the second fix of the two, we have to check whether the next token
2456 % is a control sequence, and if it is, we need to remember it. Afterwards, we
2457 % redirect to the single-fix.
2458 \def\collargs@fix@NtoV@secondfix{%
2459 \if\noexpand\collargs@temp\relax
2460 \expandafter\collargs@fix@NtoV@secondfix@i
2461 \else
2462 \expandafter\collargs@fix@NtoV@singlefix
2463 \fi
2464 }
2465 \def\collargs@fix@NtoV@secondfix@i#1{%
2466 \gdef\collargs@double@fix@cs@ii{#1}%
2467 \collargs@fix@NtoV@singlefix#1%
2468 }
2469 % \end{macro}
2470 %
2471 % \begin{macro}{\collargs@fix@vtoN}
2472 % Do nothing for the grouping tokens, redirect to |\collargs@fix@VtoN| for
2473 % other tokens.
2474 \def\collargs@fix@vtoN{%
2475 \futurelet\collargs@token\collargs@fix@vtoN@i
2476 }
2477 \def\collargs@fix@vtoN@i{%
2478 \ifcat\noexpand\collargs@token\bgroup
2479 \expandafter\collargs@fix@next
2480 \else
2481 \ifcat\noexpand\collargs@token\egroup
2482 \expandafter\expandafter\expandafter\collargs@fix@next
2483 \else
2484 \expandafter\expandafter\expandafter\collargs@fix@VtoN
2485 \fi
2486 \fi
2487 }
2488 % \end{macro}
2489 %
2490 % \begin{macro}{\collargs@fix@vtoV}
2491 % Redirect group tokens to |\collargs@fix@NtoV|, and do nothing for other
2492 % tokens.
2493 \def\collargs@fix@vtoV{%
2494 \futurelet\collargs@token\collargs@fix@vtoV@i
2495 }
2496 \def\collargs@fix@vtoV@i{%
2497 \ifcat\noexpand\collargs@token\bgroup
2498 \expandafter\collargs@fix@NtoV
2499 \else
2500 \ifcat\noexpand\collargs@token\egroup
2501 \expandafter\expandafter\expandafter\collargs@fix@NtoV
2502 \else
2503 \expandafter\expandafter\expandafter\collargs@fix@next
2504 \fi
2505 \fi
2506 }
2507 % \end{macro}
2508 %
2509 % \begin{macro}{\collargs@fix@Vtov}
2510 % Redirect group tokens to |\collargs@fix@VtoN|, and do nothing for other
2511 % tokens. |#1| is surely of category 12, so we can safely grab it.
2512 \def\collargs@fix@catcode@of@braces@fromverbatim#1{%
2513 \ifnum\catcode`#1=1
2514 \expandafter\collargs@fix@VtoN
2515 \expandafter#1%
2516 \else
2517 \ifnum\catcode`#1=2
2518 \expandafter\expandafter\expandafter\collargs@fix@cc@VtoN
2519 \expandafter\expandafter\expandafter#1%
2520 \else
2521 \expandafter\expandafter\expandafter\collargs@fix@next
2522 \fi
2523 \fi
2524 }
2525 % \end{macro}
2526 %
2527 % \begin{macro}{\collargs@fix@VtoN}
2528 % This is the only complicated part. Control sequences and comments (but not
2529 % grouping characters!) require special attention. We're fine to grab the
2530 % token right away, as we know it is of category 12.
2531 \def\collargs@fix@VtoN#1{%
2532 \ifnum\catcode`#1=0
2533 \expandafter\collargs@fix@VtoN@escape
2534 \else
2535 \ifnum\catcode`#1=14
2536 \expandafter\expandafter\expandafter\collargs@fix@VtoN@comment
2537 \else
2538 \expandafter\expandafter\expandafter\collargs@fix@VtoN@token
2539 \fi
2540 \fi
2541 #1%
2542 }
2543 % \end{macro}
2544 %
2545 % \begin{macro}{\collargs@fix@VtoN@token}
2546 % We create a new character with the current
2547 % category code behing the next-code. This works even for grouping
2548 % characters.
2549 \def\collargs@fix@VtoN@token#1{%
2550 \collargs@insert@char\collargs@fix@next{`#1}{\the\catcode`#1}%
2551 }
2552 % \end{macro}
2553 %
2554 % \begin{macro}{\collargs@fix@VtoN@comment}
2555 % This macro defines a macro which will,
2556 % when placed at a comment character, remove the tokens until the end of the
2557 % line. The code is adapted from the TeX.SE answer at
2558 % \url(https://){tex.stackexchange.com/a/10454/16819} by Bruno Le Floch.
2559 \def\collargs@defcommentstripper#1#2{%
2560 % We chuck a parameter into the following definition, to grab the (verbatim)
2561 % comment character. This is why this macro must be executed precisely
2562 % before the (verbatim) comment character.
2563 \def#1##1{%
2564 \begingroup%
2565 \escapechar=`\\%
2566 \catcode\endlinechar=\active%
2567 % We assign the ``other'' category code to comment characters. Without
2568 % this, comment characters behind the first one make trouble: there would
2569 % be no |^||^M| at the end of the line, so the comment stripper would
2570 % gobble the following line as well; in fact, it would gobble all
2571 % subsequent lines containing a comment character. We also make sure to
2572 % change the category code of \emph{all} comment characters, even if there
2573 % is usually just one.
2574 \def\collargs@do####1{\catcode####1=12 }%
2575 \collargs@comments
2576 \csname\string#1\endcsname%
2577 }%
2578 \begingroup%
2579 \escapechar=`\\%
2580 \lccode`\~=\endlinechar%
2581 \lowercase{%
2582 \expandafter\endgroup
2583 \expandafter\def\csname\string#1\endcsname##1~%
2584 }{%
2585 % I have removed |\space| from the end of the following line. We don't
2586 % want it for our application.
2587 \endgroup#2%
2588 }%
2589 }
2590 \collargs@defcommentstripper\collargs@fix@VtoN@comment{%
2591 \collargs@fix@next
2592 }
2593 % We don't need the generator any more.
2594 \let\collargs@defcommentstripper\relax
2595 % \end{macro}
2596 %
2597 % \begin{macro}{\collargs@fix@VtoN@escape}
2598 % An escape character of category code 12
2599 % is the most challenging --- and we won't get things completely right --- as
2600 % we have swim further down the input stream to create a control sequence.
2601 % This macro will throw away the verbatim escape character |#1|.
2602 \def\collargs@fix@VtoN@escape#1{%
2603 \ifcollargs@double@fix
2604 % We need to do things in a special way if we're in the double-fix
2605 % situation triggered by the previous fixing of a control sequence
2606 % (probably this very one). In that case, we can't collect it in the usual
2607 % way because the entire control sequence is spelled out in verbatim.
2608 \expandafter\collargs@fix@VtoN@escape@d
2609 \else
2610 % This here is the usual situation where the escape character was tokenized
2611 % verbatim, but the control sequence name itself will be collected (right
2612 % away) in the non-verbatim regime.
2613 \expandafter\collargs@fix@VtoN@escape@i
2614 \fi
2615 }
2616 \def\collargs@fix@VtoN@escape@i{%
2617 % The sole character forming a control symbol name may be of any category.
2618 % Temporarily redefining the category codes of the craziest characters allows
2619 % |\collargs@fix@VtoN@escape@ii| to simply grab the following
2620 % character.
2621 \begingroup
2622 \catcode`\\=12
2623 \catcode`\{=12
2624 \catcode`\}=12
2625 \catcode`\ =12
2626 \collargs@fix@VtoN@escape@ii
2627 }
2628 % The argument is the first character of the control sequence name.
2629 \def\collargs@fix@VtoN@escape@ii#1{%
2630 \endgroup
2631 \def\collargs@csname{#1}%
2632 % Only if |#1| is a letter may the control sequence name continue.
2633 \ifnum\catcode`#1=11
2634 \expandafter\collargs@fix@VtoN@escape@iii
2635 \else
2636 % In the case of a control space, we have to throw away the following
2637 % spaces.
2638 \ifnum\catcode`#1=10
2639 \expandafter\expandafter\expandafter\collargs@fix@VtoN@escape@s
2640 \else
2641 % We have a control symbol. That means that we haven't peeked ahead and
2642 % can thus skip |\collargs@fix@VtoN@escape@z|.
2643 \expandafter\expandafter\expandafter\collargs@fix@VtoN@escape@z@i
2644 \fi
2645 \fi
2646 }
2647 % We still have to collect the rest of the control sequence name. Braces have
2648 % their usual meaning again, so we have to check for them explicitly (and bail
2649 % out if we stumble upon them).
2650 \def\collargs@fix@VtoN@escape@iii{%
2651 \futurelet\collargs@temp\collargs@fix@VtoN@escape@iv
2652 }
2653 \def\collargs@fix@VtoN@escape@iv{%
2654 \ifcat\noexpand\collargs@temp\bgroup
2655 \let\collargs@action\collargs@fix@VtoN@escape@z
2656 \else
2657 \ifcat\noexpand\collargs@temp\egroup
2658 \let\collargs@action\collargs@fix@VtoN@escape@z
2659 \else
2660 \expandafter\ifx\space\collargs@temp
2661 \let\collargs@action\collargs@fix@VtoN@escape@s
2662 \else
2663 \let\collargs@action\collargs@fix@VtoN@escape@v
2664 \fi
2665 \fi
2666 \fi
2667 \collargs@action
2668 }
2669 % If we have a letter, store it and loop back, otherwise finish.
2670 \def\collargs@fix@VtoN@escape@v#1{%
2671 \ifcat\noexpand#1a%
2672 \appto\collargs@csname{#1}%
2673 \expandafter\collargs@fix@VtoN@escape@iii
2674 \else
2675 \expandafter\collargs@fix@VtoN@escape@z\expandafter#1%
2676 \fi
2677 }
2678 % Throw away the following spaces.
2679 \def\collargs@fix@VtoN@escape@s{%
2680 \futurelet\collargs@temp\collargs@fix@VtoN@escape@s@i
2681 }
2682 \def\collargs@fix@VtoN@escape@s@i{%
2683 \expandafter\ifx\space\collargs@temp
2684 \expandafter\collargs@fix@VtoN@escape@s@ii
2685 \else
2686 \expandafter\collargs@fix@VtoN@escape@z
2687 \fi
2688 }
2689 \def\collargs@fix@VtoN@escape@s@ii{%
2690 \expandafter\collargs@fix@VtoN@escape@z\romannumeral-0%
2691 }
2692 % Once we have collected the control sequence name into |\collargs@csname|, we
2693 % will create the control sequence behind the next-code. However, we have two
2694 % complications. The minor one is that |\csname| defines an unexisting control
2695 % sequence to mean |\relax|, so we have to check whether the control sequence
2696 % we will create is defined, and if not, ``undefine'' it in advance.
2697 % ^^A todo: do nicer
2698 \def\collargs@fix@VtoN@escape@z@i{%
2699 \collargs@fix@VtoN@escape@z@maybe@undefine@cs@begin
2700 \collargs@fix@VtoN@escape@z@ii
2701 }%
2702 \def\collargs@fix@VtoN@escape@z@maybe@undefine@cs@begin{%
2703 \ifcsname\collargs@csname\endcsname
2704 \@tempswatrue
2705 \else
2706 \@tempswafalse
2707 \fi
2708 }
2709 \def\collargs@fix@VtoN@escape@z@maybe@undefine@cs@end{%
2710 \if@tempswa
2711 \else
2712 \cslet{\collargs@csname}\collargs@undefined
2713 \fi
2714 }
2715 \def\collargs@fix@VtoN@escape@z@ii{%
2716 \expandafter\collargs@fix@VtoN@escape@z@maybe@undefine@cs@end
2717 \expandafter\collargs@fix@next\csname\collargs@csname\endcsname
2718 }
2719 % The second complication is much greater, but it only applies to control words
2720 % and spaces, and that's why control symbols went directly to the macro above.
2721 % Control words and spaces will only get there via a detour through the
2722 % following macro.
2723 %
2724 % The problem is that collecting the control word\slash space name peeked ahead
2725 % in the stream, so the character following the control sequence (name) is
2726 % already tokenized. We will (at least partially) address this by requesting a
2727 % ``double-fix'': until the control sequence we're about to create is consumed
2728 % into some argument, each category code fix will fix two ``tokens'' rather
2729 % than one.
2730 \def\collargs@fix@VtoN@escape@z{%
2731 \collargs@if@one@more@fix{%
2732 % Some previous fixing has requested a double fix, so let's do it.
2733 % Afterwards, redirect to the control symbol code
2734 % |\collargs@fix@VtoN@escape@z@i|. It will surely use the correct
2735 % |\collargs@csname| because we do the second fix in a group.
2736 \collargs@do@one@more@fix\collargs@fix@VtoN@escape@z@i
2737 }{%
2738 % Remember the collected control sequence. It will be used in
2739 % |\collargs@cancel@double@fix|.
2740 \collargs@fix@VtoN@escape@z@maybe@undefine@cs@begin
2741 \xdef\collargs@double@fix@cs@i{\expandonce{\csname\collargs@csname\endcsname}}%
2742 \collargs@fix@VtoN@escape@z@maybe@undefine@cs@end
2743 % Request the double-fix.
2744 \global\collargs@double@fixtrue
2745 % The complication is addressed, redirect to the control symbol finish.
2746 \collargs@fix@VtoN@escape@z@ii
2747 }%
2748 }
2749 % When we have to ``redo'' a control sequence, because it was ping-ponged back
2750 % into the verbatim mode, we cannot collect it by
2751 % |\collargs@fix@VtoN@escape@i|, because it is spelled out entirely in
2752 % verbatim. However, we have seen this control sequence before, and remembered
2753 % it, so we'll simply grab it. Another complication is that we might be either
2754 % at the ``first'' control sequence, whose fixing created all these double-fix
2755 % trouble, or at the ``second'' control sequence, if the first one was
2756 % immediately followed by another one. But we have remembered both of them:
2757 % the first one in |\collargs@fix@VtoN@escape@z|, the second one in
2758 % |\collargs@fix@NtoV@secondfix|.
2759 \def\collargs@fix@VtoN@escape@d{%
2760 \ifcollargs@in@second@fix
2761 \expandafter\collargs@fix@VtoN@escape@d@i
2762 \expandafter\collargs@double@fix@cs@ii
2763 \else
2764 \expandafter\collargs@fix@VtoN@escape@d@i
2765 \expandafter\collargs@double@fix@cs@i
2766 \fi
2767 }
2768 % We have the contents of either |\collargs@double@fix@cs@i| or
2769 % |\collargs@double@fix@cs@ii| here, a control sequence in both cases.
2770 \def\collargs@fix@VtoN@escape@d@i#1{%
2771 \expandafter\expandafter\expandafter\collargs@fix@VtoN@escape@d@ii
2772 \expandafter\string#1\relax
2773 }
2774 % We have the verbatimized control sequence name in |#2| (|#1| is the escape
2775 % character). By storing it into |\collargs@csname|, we pretend we have
2776 % collected it. By defining and executing |\collargs@fix@VtoN@escape@d@iii|,
2777 % we actually gobble it from the input stream. Finally, we reroute to
2778 % |\collargs@fix@VtoN@escape@z|.
2779 \def\collargs@fix@VtoN@escape@d@ii#1#2\relax{%
2780 \def\collargs@csname{#2}%
2781 \def\collargs@fix@VtoN@escape@d@iii#2{%
2782 \collargs@fix@VtoN@escape@z
2783 }%
2784 \collargs@fix@VtoN@escape@d@iii
2785 }
2786 % This conditional signals a double-fix request. It should be always set
2787 % globally, because it is cleared by |\collargs@double@fixfalse| in a group.
2788 \newif\ifcollargs@double@fix
2789 % This conditional signals that we're currently performing the second fix.
2790 \newif\ifcollargs@in@second@fix
2791 % Inspect the two conditionals above to decide whether we have to perform
2792 % another fix: if so, execute the first argument, otherwise the second one.
2793 % This macro is called only from |\collargs@fix@VtoN@escape@z| and
2794 % |\collargs@fix@NtoV|, because these are the only two places where we might
2795 % need the second fix, ping-ponging a control sequence between the verbatim and
2796 % the non-verbatim mode.
2797 \def\collargs@if@one@more@fix{%
2798 \ifcollargs@double@fix
2799 \ifcollargs@in@second@fix
2800 \expandafter\expandafter\expandafter\@secondoftwo
2801 \else
2802 \expandafter\expandafter\expandafter\@firstoftwo
2803 \fi
2804 \else
2805 \expandafter\@secondoftwo
2806 \fi
2807 }
2808 \def\collargs@do@one@more@fix#1{%
2809 % We perform the second fix in a group, signalling that we're performing
2810 % it.
2811 \begingroup
2812 \collargs@in@second@fixtrue
2813 % Reexecute the fixing routine, at the end, close the group and execute
2814 % the given code afterwards.
2815 \collargs@fix{%
2816 \endgroup
2817 #1%
2818 }%
2819 }
2820 % This macro is called from |\collargs@appendarg| to cancel the double-fix
2821 % request.
2822 \def\collargs@cancel@double@fix{%
2823 % |\collargs@appendarg| is only executed when something was actually
2824 % consumed. We thus know that at least one of the problematic ``tokens'' is
2825 % gone, so the double fix is not necessary anymore.
2826 \global\collargs@double@fixfalse
2827 % What we have to figure out, still, is whether both problematic ``tokens''
2828 % we consumed. If so, no more fixing is required. But if only one of them
2829 % was consumed, we need to request the normal, single, fix for the remaining
2830 % ``token''.
2831 \begingroup
2832 % This will attach the delimiters directly to the argument, so we'll see what
2833 % was actually consumed.
2834 \collargs@process@arg
2835 % We compare what was consumed when collecting the current argument with the
2836 % control word that triggered double-fixing. If they match, only the
2837 % offending control word was consumed, so we need to set the fix request to
2838 % true for the following token.
2839 \edef\collargs@temp{\the\collargsArg}%
2840 \edef\collargs@tempa{\expandafter\string\collargs@double@fix@cs@i}%
2841 \ifx\collargs@temp\collargs@tempa
2842 \global\collargs@fix@requestedtrue
2843 \fi
2844 \endgroup
2845 }
2846 % \end{macro}
2847 %
2848 %
2849 % \begin{macro}{\collargs@insert@char,\collargs@make@char}
2850 % These macros create a character of character code |#2| and category code
2851 % |#3|. The first macro inserts it into the stream behind the code in |#1|;
2852 % the second one defines the control sequence in |#1| to hold the created
2853 % character (clearly, it should not be used for categories 1 and 2).
2854 %
2855 % We use the facilities of \hologo{LuaTeX}, \hologo{XeTeX} and \hologo{LaTeX}
2856 % where possible. In the end, we only have to implement our own macros for
2857 % plain \hologo{pdfTeX}.
2858 %<!context>\ifdefined\luatexversion
2859 \def\collargs@insert@char#1#2#3{%
2860 \edef\collargs@temp{\unexpanded{#1}}%
2861 \expandafter\collargs@temp\directlua{%
2862 tex.cprint(\number#3,string.char(\number#2))}%
2863 }%
2864 \def\collargs@make@char#1#2#3{%
2865 \edef#1{\directlua{tex.cprint(\number#3,string.char(\number#2))}}%
2866 }%
2867 %<*!context>
2868 \else
2869 \ifdefined\XeTeXversion
2870 \def\collargs@insert@char#1#2#3{%
2871 \edef\collargs@temp{\unexpanded{#1}}%
2872 \expandafter\collargs@temp\Ucharcat #2 #3
2873 }%
2874 \def\collargs@make@char#1#2#3{%
2875 \edef#1{\Ucharcat#2 #3}%
2876 }%
2877 \else
2878 %<*latex>
2879 \ExplSyntaxOn
2880 \def\collargs@insert@char#1#2#3{%
2881 \edef\collargs@temp{\unexpanded{#1}}%
2882 \expandafter\expandafter\expandafter\collargs@temp\char_generate:nn{#2}{#3}%
2883 }%
2884 \def\collargs@make@char#1#2#3{%
2885 \edef#1{\char_generate:nn{#2}{#3}}%
2886 }%
2887 \ExplSyntaxOff
2888 %</latex>
2889 %<*plain>
2890 % The implementation is inspired by |expl3|'s implementation of
2891 % |\char_generate:nn|, but our implementation is not expandable, for
2892 % simplicity. We first store an (arbitrary) character |^||^@| of category
2893 % code $n$ into control sequence |\collargs@charofcat@|$n$, for every
2894 % (implementable) category code.
2895 \begingroup
2896 \catcode`\^^@=1 \csgdef{collargs@charofcat@1}{%
2897 \noexpand\expandafter^^@\iffalse}\fi}
2898 \catcode`\^^@=2 \csgdef{collargs@charofcat@2}{\iffalse{\fi^^@}
2899 \catcode`\^^@=3 \csgdef{collargs@charofcat@3}{^^@}
2900 \catcode`\^^@=4 \csgdef{collargs@charofcat@4}{^^@}
2901 % As we have grabbed the spaces already, a remaining newline should surely be
2902 % fixed into a |\par|.
2903 \csgdef{collargs@charofcat@5}{\par}
2904 \catcode`\^^@=6 \csxdef{collargs@charofcat@6}{\unexpanded{^^@}}
2905 \catcode`\^^@=7 \csgdef{collargs@charofcat@7}{^^@}
2906 \catcode`\^^@=8 \csgdef{collargs@charofcat@8}{^^@}
2907 \csgdef{collargs@charofcat@10}{\noexpand\space}
2908 \catcode`\^^@=11 \csgdef{collargs@charofcat@11}{^^@}
2909 \catcode`\^^@=12 \csgdef{collargs@charofcat@12}{^^@}
2910 \catcode`\^^@=13 \csgdef{collargs@charofcat@13}{^^@}
2911 \endgroup
2912 \def\collargs@insert@char#1#2#3{%
2913 % Temporarily change the lowercase code of |^||^@| to the requested character
2914 % |#2|.
2915 \begingroup
2916 \lccode`\^^@=#2\relax
2917 % We'll have to close the group before executing the next-code.
2918 \def\collargs@temp{\endgroup#1}%
2919 % |\collargs@charofcat@|\meta{requested category code} is f-expanded first,
2920 % leaving us to lowercase |\expandafter\collargs@temp^||^@|. Clearly,
2921 % lowercasing |\expandafter\collargs@temp| is a no-op, but lowercasing
2922 % |^||^@| gets us the requested character of the requested category.
2923 % |\expandafter| is executed next, and this gets rid of the conditional for
2924 % category codes 1 and 2.
2925 \expandafter\lowercase\expandafter{%
2926 \expandafter\expandafter\expandafter\collargs@temp
2927 \romannumeral-`0\csname collargs@charofcat@\the\numexpr#3\relax\endcsname
2928 }%
2929 }
2930 % This macro cannot not work for category code 6 (because we assign the
2931 % result to a macro), but no matter, we only use it for category code 12
2932 % anyway.
2933 \def\collargs@make@char#1#2#3{%
2934 \begingroup
2935 \lccode`\^^@=#2\relax
2936 % Define |\collargs@temp| to hold |^||^@| of the appropriate category.
2937 \edef\collargs@temp{%
2938 \csname collargs@charofcat@\the\numexpr#3\relax\endcsname}%
2939 % Preexpand the second |\collargs@temp| so that we lowercase
2940 % |\def\collargs@temp{^||^@}|, with |^||^@| of the appropriate category.
2941 \expandafter\lowercase\expandafter{%
2942 \expandafter\def\expandafter\collargs@temp\expandafter{\collargs@temp}%
2943 }%
2944 \expandafter\endgroup
2945 \expandafter\def\expandafter#1\expandafter{\collargs@temp}%
2946 }
2947 %</plain>
2948 \fi
2949 \fi
2950 %</!context>
2951 % \end{macro}
2952 %
2953 %<plain>\resetatcatcode
2954 %<context>\stopmodule
2955 %<context>\protect
2956 % \end{macrocode}
2957 %
2958 % Local Variables:
2959 % TeX-engine: luatex
2960 % TeX-master: "doc/memoize-code.tex"
2961 % TeX-auto-save: nil
2962 % End: