diff options
Diffstat (limited to 'macros/generic/collargs/collargs.edtx')
-rw-r--r-- | macros/generic/collargs/collargs.edtx | 107 |
1 files changed, 69 insertions, 38 deletions
diff --git a/macros/generic/collargs/collargs.edtx b/macros/generic/collargs/collargs.edtx index 40a102c1a8..6cd39ad4ca 100644 --- a/macros/generic/collargs/collargs.edtx +++ b/macros/generic/collargs/collargs.edtx @@ -10,11 +10,11 @@ %% (Sa\v{s}o \v{Z}ivanovi\'{c}) %% %% This work may be distributed and/or modified under the conditions of the -%% LaTeX Project Public License, either version 1.3 of this license or (at your -%% option) any later version. The latest version of this license is in -%% http://www.latex-project.org/lppl.txt and version 1.3 or later is part of -%% all distributions of LaTeX version 2005/12/01 or later. -%% +%% LaTeX Project Public License, either version 1.3c of this license or (at +%% your option) any later version. The latest version of this license is in +%% https://www.latex-project.org/lppl.txt and version 1.3c or later is part of +%% all distributions of LaTeX version 2008 or later. +%% %% This work has the LPPL maintenance status `maintained'. %% The Current Maintainer of this work is Saso Zivanovic. %% @@ -46,14 +46,14 @@ % % % \paragraph{Identification} -%<latex>\ProvidesPackage{collargs}[2023/10/10 v1.0.0 Collect arguments of any command] +%<latex>\ProvidesPackage{collargs}[2024/01/02 v1.1.0 Collect arguments of any command] %<context>%D \module[ %<context>%D file=t-collargs.tex, -%<context>%D version=1.0.0, +%<context>%D version=1.1.0, %<context>%D title=CollArgs, %<context>%D subtitle=Collect arguments of any command, %<context>%D author=Saso Zivanovic, -%<context>%D date=2023-10-10, +%<context>%D date=2024-01-02, %<context>%D copyright=Saso Zivanovic, %<context>%D license=LPPL, %<context>%D ] @@ -70,24 +70,38 @@ %<plain>\catcode`\@11\relax % % \begin{macro}{\toksapp,\gtoksapp,\etoksapp,\xtoksapp} -% Our macros for appending to a token register only accept a control sequence -% defined by |\toksdef| (like |\mytoks|) but not an explicit register -% designation like |\toks0|, so we only define them if noone else did; the -% names of the macros match the \hologo{LuaTeX} primitives, so they surely -% won't be defined there. -\ifdefined\toksapp\else - \long\def\toksapp#1#2{#1\expandafter{\the#1#2}}% -\fi -\ifdefined\etoksapp\else - \long\def\etoksapp#1#2{#1\expandafter{\expanded{\the#1#2}}}% -\fi -\ifdefined\gtoksapp\else - \long\def\gtoksapp#1#2{\global#1\expandafter{\the#1#2}}% -\fi -\ifdefined\xtoksapp\else - \long\def\xtoksapp#1#2{\global#1\expandafter{\expanded{\the#1#2}}}% +% Macros for appending to a token register. We don't have to define them in +% \hologo{LuaTeX}, where they exist as primitives. Same as these primitives, +% out macros accept either a register number or a |\toksdef|fed control +% sequence as the (unbraced) |#1|; |#2| is the text to append. +\ifdefined\luatexversion +\else + \def\toksapp{\toks@cs@or@num\@toksapp} + \def\gtoksapp{\toks@cs@or@num\@gtoksapp} + \def\etoksapp{\toks@cs@or@num\@etoksapp} + \def\xtoksapp{\toks@cs@or@num\@xtoksapp} + \def\toks@cs@or@num#1#2#{% + % Test whether |#2| (the original |#1|) is a number or a control sequence. + \ifnum-2>-1#2 + % It is a number. |\toks@cs@or@num@num| will gobble |\toks@cs@or@num@cs| + % below. + \expandafter\toks@cs@or@num@num + % The register control sequence in |#2| is skipped over in the false + % branch. + \fi + \toks@cs@or@num@cs{#1}{#2}% + } + % |#1| is one of |\@toksapp| and friends. The second macro prefixes the + % register number by |\toks|. + \def\toks@cs@or@num@cs#1#2{#1{#2}} + \def\toks@cs@or@num@num\toks@cs@or@num@cs#1#2{#1{\toks#2 }} + % Having either |\tokscs| or |\toks<number>| in |#1|, we can finally do the + % real job. + \long\def\@toksapp#1#2{#1\expandafter{\the#1#2}}% + \long\def\@etoksapp#1#2{#1\expandafter{\expanded{\the#1#2}}}% + \long\def\@gtoksapp#1#2{\global#1\expandafter{\the#1#2}}% + \long\def\@xtoksapp#1#2{\global#1\expandafter{\expanded{\the#1#2}}}% \fi -\ifdefined\toks@\else\toksdef\toks@=0 \fi % \end{macro} % % \begin{macro}{\CollectArguments,\CollectArgumentsRaw} @@ -521,7 +535,19 @@ \newif\ifcollargsNoDelimiters % \end{macro} % \end{collargskey} -% + +% \begin{collargskey}{brace collected} +% \begin{macro}{\ifcollargsBraceCollected} +% When this conditional is set to false, the collected arguments are not +% enclosed in braces when passed on to \meta{next-code}. +\collargsSet{% + brace collected/.is if=collargsBraceCollected, +} +\newif\ifcollargsBraceCollected +\collargsBraceCollectedtrue +% \end{macro} +% \end{collargskey} +% % \subsubsection{The central loop} % \label{sec:code:collargs:central-loop} % @@ -678,7 +704,12 @@ \expanded{% \endgroup \noexpand\collargs@fix{% - \expandonce\collargs@next{\the\collargs@toks}% + \expandonce\collargs@next + \ifcollargsBraceCollected + {\the\collargs@toks}% + \else + \the\collargs@toks + \fi \collargs@spaces }% }% @@ -1680,7 +1711,7 @@ \collargs@CPT@def\collargs@gobbleOneB\collargs@begintag{% % Assign the collected tokens into a register. The first token in |##1| % will be |\collargs@empty|, so we expand to get rid of it. - \expandafter\toks@\expandafter{##1}% + \toks0\expandafter{##1}% % |cprotect| simply grabs the token following the |\collargs@begintag| with % a parameter. We can't do this, because we need the code to work in the % non-verbatim mode, as well, and we might stumble upon a brace there. So @@ -1692,13 +1723,13 @@ \ifcollargs@long\long\fi \collargs@CPT@def\collargs@gobbleUntilE\collargs@endtag{% % Expand |\collargs@empty| at the start of |##1|. - \expandafter\toksapp\expandafter\toks@\expandafter{##1}% + \expandafter\toksapp\expandafter0\expandafter{##1}% \collargs@gobbleUntilE@i }% % Initialize. \collargs@begins=0\relax \collargsArg{}% - \toks@{}% + \toks0{}% % We will call |\collargs@gobbleUntilE| via the caller control sequence. \collargs@letusecollector\collargs@gobbleUntilE % We insert |\collargs@empty| to avoid the potential debracing problem. @@ -1726,7 +1757,7 @@ \expandafter\collargs@gobbleOneB@v \else % Append the real begin-tag to the temporary tokens. - \etoksapp\toks@{\expandonce\collargs@begintag}% + \etoksapp0{\expandonce\collargs@begintag}% \expandafter\collargs@gobbleOneB@ii \fi }% @@ -1754,14 +1785,14 @@ \fi % Whatever the result was, we have to append the gobbled group to the % temporary toks. - \etoksapp\toks@{\collargs@grabbed@spaces\unexpanded{{#1}}}% + \etoksapp0{\collargs@grabbed@spaces\unexpanded{{#1}}}% \collargs@init@grabspaces \collargs@gobbleOneB@vi } \def\collargs@gobbleOneB@v#1{\collargs@gobbleOneB@vi} \def\collargs@gobbleOneB@vi{% % Store. - \etoksapp\collargsArg{\the\toks@}% + \etoksapp\collargsArg{\the\toks0}% % Advance the begin-tag counter. \advance\collargs@begins\collargs@begins@increment\relax % Find more begin-tags, unless this was the final one. @@ -1785,7 +1816,7 @@ }% } \def\collargs@gobbleUntilE@iii#1{% - \etoksapp\toks@{\collargs@grabbed@spaces}% + \etoksapp0{\collargs@grabbed@spaces}% \collargs@init@grabspaces \def\collargs@tempa{#1}% \ifx\collargs@tempa\collargs@tagarg @@ -1794,7 +1825,7 @@ \else % Nope, this |\end| belongs to someone else. Insert the end tag plus the % tag argument, and collect until the next |\end|. - \expandafter\toksapp\expandafter\toks@\expandafter{\collargs@endtag{#1}}% + \expandafter\toksapp\expandafter0\expandafter{\collargs@endtag{#1}}% \expandafter\collargs@letusecollector\expandafter\collargs@gobbleUntilE \fi } @@ -1802,7 +1833,7 @@ % Invoke |\collargs@gobbleOneB| with the collected material, plus a fake % begin-tag and a quark guard. \ifcollargsIgnoreNesting - \expandafter\collargsArg\expandafter{\the\toks@}% + \expandafter\collargsArg\expandafter{\the\toks0}% \expandafter\collargs@commandatend \else \expandafter\collargs@gobbleUntilE@v @@ -1812,7 +1843,7 @@ \expanded{% \noexpand\collargs@letusecollector\noexpand\collargs@gobbleOneB \noexpand\collargs@empty - \the\toks@ + \the\toks0 % Add a fake begin-tag and a quark guard. \expandonce\collargs@begintag \noexpand\collargs@qend @@ -1825,7 +1856,7 @@ \expandafter\ifx\expandafter\relax\collargs@tagarg\relax\else{% \expandonce\collargs@tagarg}\fi }% - \toks@={}% + \toks0={}% \expandafter\collargs@letusecollector\expandafter\collargs@gobbleUntilE \expandafter\collargs@empty \fi |