| 1 |
% \iffalse meta-comment |
| 2 |
% Copyright (C) 2019--2020 |
| 3 |
% by Phelype Oleinik <[email protected]> |
| 4 |
% |
| 5 |
% This work may be distributed and/or modified under the conditions of |
| 6 |
% the LaTeX Project Public License, either version 1.3c of this license |
| 7 |
% or (at your option) any later version. The latest version of this |
| 8 |
% license is in |
| 9 |
% |
| 10 |
% http://www.latex-project.org/lppl.txt |
| 11 |
% |
| 12 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 13 |
% version 2005/12/01 or later. |
| 14 |
% |
| 15 |
% This work is "maintained" (as per the LPPL maintenance status) |
| 16 |
% by Phelype Oleinik. |
| 17 |
% |
| 18 |
%<*package> |
| 19 |
\def\namedefDate{2020-06-20} |
| 20 |
\def\namedefVersion{1.0} |
| 21 |
%</package> |
| 22 |
%<*driver> |
| 23 |
\documentclass[a4paper,full,inline]{l3doc} |
| 24 |
\usepackage[T1]{fontenc} |
| 25 |
\usepackage{microtype} |
| 26 |
\usepackage{namedef} |
| 27 |
\usepackage{bookmark} |
| 28 |
\usepackage{xcolor} |
| 29 |
|
| 30 |
\def\TODO#1#{\begingroup\afterassignment\TODODO\let\tmp = } |
| 31 |
\def\TODODO{\setbox0=\vbox\bgroup\aftergroup\ENDTODO} |
| 32 |
\def\ENDTODO{\endgroup\ignorespaces} |
| 33 |
|
| 34 |
\begingroup |
| 35 |
\lccode`~=`\% |
| 36 |
\lowercase{\endgroup |
| 37 |
\def\VRBCodes{^^A |
| 38 |
\catcode`~=13 |
| 39 |
\def~{\%\global\linenofalse}} |
| 40 |
} |
| 41 |
\makeatletter |
| 42 |
\define@key{FV}{labelformat}{% |
| 43 |
\def\FV@LabelFormat{#1}% |
| 44 |
\ifx\FV@LabelFormat\FV@None |
| 45 |
\let\FV@LabelFormat\relax |
| 46 |
\fi} |
| 47 |
\def\FV@Label@ii[#1]#2\@nil{% |
| 48 |
\def\@tempa{#1}% |
| 49 |
\ifx\@tempa\empty |
| 50 |
\def\FV@LabelBegin{\FV@LabelFormat#2}% |
| 51 |
\else |
| 52 |
\def\FV@LabelBegin{\FV@LabelFormat#1}% |
| 53 |
\def\FV@LabelPositionBottomLine{\@ne}% |
| 54 |
\fi |
| 55 |
\def\FV@LabelEnd{\FV@LabelFormat#2}} |
| 56 |
\makeatother |
| 57 |
\DefineVerbatimEnvironment% |
| 58 |
{example}{Verbatim} |
| 59 |
{ |
| 60 |
gobble=3, |
| 61 |
frame=single, |
| 62 |
commandchars=|<>, |
| 63 |
codes={\VRBCodes}, |
| 64 |
numbers=left, |
| 65 |
labelformat=\rmfamily\bfseries, |
| 66 |
} |
| 67 |
\def\mshow#1{> \string#1=\meaning#1} |
| 68 |
\newif\iflineno\linenotrue%\fi |
| 69 |
\def\theFancyVerbLine{^^A |
| 70 |
\rmfamily |
| 71 |
\scriptsize |
| 72 |
\color{gray}^^A |
| 73 |
\iflineno |
| 74 |
\arabic{FancyVerbLine}^^A |
| 75 |
\else |
| 76 |
\global\addtocounter{FancyVerbLine}{-1}^^A |
| 77 |
\fi |
| 78 |
\global\linenotrue |
| 79 |
} |
| 80 |
\pdfstringdefDisableCommands{\let\footnotemark\relax} |
| 81 |
|
| 82 |
% Substitute lmtt/it by lmtt/sl |
| 83 |
\DeclareFontShape{T1}{lmtt}{lc}{it}{<->ssub*lmtt/lc/sl}{} |
| 84 |
|
| 85 |
\begin{document} |
| 86 |
\overfullrule=5pt |
| 87 |
\DocInput{\jobname.dtx} |
| 88 |
\end{document} |
| 89 |
%</driver> |
| 90 |
% \fi |
| 91 |
% |
| 92 |
% \GetFileInfo{\jobname.sty} |
| 93 |
% |
| 94 |
% \title{^^A |
| 95 |
% The \pkg{namedef} package\\ |
| 96 |
% Named parameters in \TeX{} definitions^^A |
| 97 |
% \thanks{This file has version number \namedefVersion, |
| 98 |
% last revised \namedefDate.} |
| 99 |
% } |
| 100 |
% |
| 101 |
% \author{^^A |
| 102 |
% Phelype H. Oleinik\thanks |
| 103 |
% {^^A |
| 104 |
% E-mail: |
| 105 |
% \href{mailto:[email protected]} |
| 106 |
% {phelype.oleinik\meta{at}latex-project.org}^^A |
| 107 |
% }^^A |
| 108 |
% } |
| 109 |
% |
| 110 |
% \date{Released \namedefDate} |
| 111 |
% |
| 112 |
% \maketitle |
| 113 |
% |
| 114 |
% \begin{documentation} |
| 115 |
% |
| 116 |
% \section{Introduction} |
| 117 |
% |
| 118 |
% This package provides a somewhat dubious, however useful way to make |
| 119 |
% \TeX{} definitions using \cs{def} and the like. The usual way to |
| 120 |
% define a ``hello world'' macro is |\def\hello#1{Hello #1!}|. Sure, |
| 121 |
% this is easy (for people using this package, at least), but sometimes |
| 122 |
% I stumbled upon the case where I had a macro defined with something |
| 123 |
% like |\def\macro#1#2#3#4#5#6{#6#1#4#3#5#2}| and then I had to, for |
| 124 |
% whatever reason, add another argument before |#1|. Oh, the pain. But |
| 125 |
% OK, occupational hazard. And then I change my mind and needed to |
| 126 |
% revert. This package provides a way to give a semantic meaning to the |
| 127 |
% arguments of a definition such that it becomes clearer in meaning and |
| 128 |
% easier to change when an argument's identifier defines what it means |
| 129 |
% rather than its position in a list. |
| 130 |
% |
| 131 |
% \subsection{Features} |
| 132 |
% |
| 133 |
% This package defines a macro, \cs{named}, which acts like a prefix for |
| 134 |
% \cs{def} (the same way as \cs{long} and the like), and allows one to |
| 135 |
% rewrite the \cs{hello} macro like this: |
| 136 |
% |\named\def\hello#[who]{Hello #[who]!}|. Sure, a macro with one |
| 137 |
% argument won't see much benefit from it, but as soon as that number |
| 138 |
% increases, a better description of the arguments comes in handy. |
| 139 |
% |
| 140 |
% The package also defines a macro \cs{NamedDelim} which allows the user |
| 141 |
% to change the delimiter of the named parameters to their liking. For |
| 142 |
% example, after |\NamedDelim //| the example above changes to: |
| 143 |
% |\named\def\hello#/who/{Hello #/who/!}|. The default is |
| 144 |
% |\NamedDelim []|. |
| 145 |
% |
| 146 |
% The only dependency of this package is the \LaTeX3 programming layer, |
| 147 |
% \pkg{expl3}, thus this package works in any format and engine |
| 148 |
% supported by \pkg{expl3}. In \LaTeXe, load it with |
| 149 |
% |\usepackage{namedef}|, and in other formats with |
| 150 |
% |\input namedef.sty|. |
| 151 |
% |
| 152 |
% \subsection{Feedback} |
| 153 |
% |
| 154 |
% Bugs, feature requests, or suggestions are most welcome. You can send |
| 155 |
% them by e-mail or to the source code repository at |
| 156 |
% \url{https://github.com/PhelypeOleinik/namedef}. |
| 157 |
% |
| 158 |
% \section{Usage} |
| 159 |
% |
| 160 |
% \begin{function}{\named} |
| 161 |
% \begin{syntax} |
| 162 |
% \cs{named}\meta{other~prefixes}\meta{\cs[no-index]{(e,g,x)def}}^^A |
| 163 |
%\meta{parameter text}\marg{replacement text} |
| 164 |
% \end{syntax} |
| 165 |
% The \cs{named} macro will grab the \meta{other~prefixes}, the |
| 166 |
% \cs{def} command, the \meta{parameter~text} and |
| 167 |
% \meta{replacement~text}, and will replace every ocurrence of |
| 168 |
% |#[|\meta{text}|]| by a suitable |#|\meta{number} for \TeX{} to |
| 169 |
% carry on with the definition. |
| 170 |
% |
| 171 |
% The \meta{other~prefixes} can be any combination of \cs{long}, |
| 172 |
% \cs{outer}, \cs{global}, and \cs{protected}. As for \TeX, their |
| 173 |
% relative order doesn't matter, except that \cs{named} must appear |
| 174 |
% \emph{before} any \cs{global} prefix. Other prefixes are detected |
| 175 |
% whether placed before or after \cs{named}. |
| 176 |
% |
| 177 |
% The \cs{def} command can be anything. The package will not check the |
| 178 |
% validity of the command, it will simply drag the token along until |
| 179 |
% the point where the definition is made. This allows one to use any |
| 180 |
% of \TeX's primitive \cs{def} commands as well as, for instance, |
| 181 |
% \pkg{expl3}'s \cs{cs_new:Npn} or the like. However \cs{named} will |
| 182 |
% not work with \LaTeX's \cs{newcommand} or \pkg{xparse}'s |
| 183 |
% \cs{NewDocumentCommand} and the like because their syntax is |
| 184 |
% completely different. |
| 185 |
% |
| 186 |
% The \meta{parameter~text} is the same \meta{parameter~text} that |
| 187 |
% would be used if the \cs{named} prefix wasn't used with the |
| 188 |
% difference that all |#|\meta{number} must be replaced by a |
| 189 |
% |#[|\meta{name}|]|. The characters |[| and |]| are mandatory and the |
| 190 |
% character |]| cannot appear in \meta{name}, however |[| and |]| can |
| 191 |
% still appear in the \meta{parameter~text} without restriction; the |
| 192 |
% special meaning is only applied after a parameter token (|#|). |
| 193 |
% \meta{name} can be anything that when fully expanded returns a |
| 194 |
% string of characters (anything valid in |\csname|\dots|\endcsname|). |
| 195 |
% The \meta{parameter~text} cannot, however, contain numbered |
| 196 |
% parameters anymore (|#1|,~|#2|,~\dots). |
| 197 |
% \TODO{This is not enforced... What should be done in this case?} |
| 198 |
% |
| 199 |
% The \meta{replacement~text} is also the same \meta{replacement~text} |
| 200 |
% that would be used otherwise, with all |#|\meta{number} replaced by |
| 201 |
% |#[|\meta{name}|]|. |
| 202 |
% \end{function} |
| 203 |
% |
| 204 |
% \begin{function}{\NamedDelim,\globalNamedDelim} |
| 205 |
% \begin{syntax} |
| 206 |
% \cs{NamedDelim}\meta{begin-token}\meta{end-token} |
| 207 |
% \cs{globalNamedDelim}\meta{begin-token}\meta{end-token} |
| 208 |
% \end{syntax} |
| 209 |
% These macros change the delimiter of the named parameters from the |
| 210 |
% default |#[|\meta{name}|]| to |
| 211 |
% |#|\meta{begin-token}\meta{name}\meta{end-token}. Both delimiters |
| 212 |
% must be one single non-macro token. Valid delimiters are character |
| 213 |
% tokens with catcode $3$, $4$, $7$, $8$, $11$, $12$, or $13$ |
| 214 |
% (see section~\ref{sec:delimiters}). |
| 215 |
% |
| 216 |
% \cs{globalNamedDelim} is the same as \cs{NamedDelim} except that the |
| 217 |
% effect of the former has global scope, while the latter is local to |
| 218 |
% a group. While you can use both, you should be careful not to |
| 219 |
% interleave them too much or you might exhaust \TeX's save stack. |
| 220 |
% |
| 221 |
% Delimiters are matched based on their character code (using \cs{if}) |
| 222 |
% so changes in their category code doesn't matter as long as that |
| 223 |
% change doesn't prevent the character from becoming a token or the |
| 224 |
% category code isn't \enquote{too special} :-) \quad (see above). |
| 225 |
% |
| 226 |
% The choice of delimiter is mostly ``what you like better''. Neither |
| 227 |
% the delimter tokens nor the name of the parameter make it to the |
| 228 |
% macro itself. They are just code-level abstractions. Thus the |
| 229 |
% delimiter should be something that the person writing the code can |
| 230 |
% easily distinguish from the rest of the code. For example, the code |
| 231 |
% |\NamedDelim xz \named\def\test#xyz{xyz#xyzxyz}| works, but its |
| 232 |
% readability is questionable. |
| 233 |
% \end{function} |
| 234 |
% |
| 235 |
% \subsection{Limitations} |
| 236 |
% |
| 237 |
% As already stated the command does not work (and I don't intend to |
| 238 |
% make it work) with \LaTeXe's \cs{newcommand} and its family because |
| 239 |
% a) the argument specification is by the number of arguments, so you |
| 240 |
% can't ``declare'' them as with \cs{def}, and b) because it's supposed |
| 241 |
% to be used for user-level interfaces, which usually (and preferably) |
| 242 |
% have a low argument count, so numbering shouldn't be a problem. |
| 243 |
% That said, see section~\ref{sec:newcommand}. |
| 244 |
% |
| 245 |
% For \pkg{xparse}'s \cs{NewDocumentCommand} the situation is the same. |
| 246 |
% Other than these, \cs{named} should work for whatever type of |
| 247 |
% definition that uses \TeX's \cs{def} syntax. |
| 248 |
% |
| 249 |
% Another limitation that I'd like to change in a future release (but |
| 250 |
% still don't know the best way to make the interface) is to support |
| 251 |
% definition commands which go beyond the |
| 252 |
% \meta{parameter~text}|{|\meta{replacement~text}|}| syntax. For |
| 253 |
% instance, in \pkg{expl3} a conditional that checks whether its |
| 254 |
% argument is negative can be defined like this (for integers, of |
| 255 |
% course): |
| 256 |
% \begin{Verbatim} |
| 257 |
% \prg_new_conditional:Npnn \namedef_if_negative:n #1 { T, F, TF } |
| 258 |
% { |
| 259 |
% \if_int_compare:w #1 < 0 |
| 260 |
% \prg_return_true: |
| 261 |
% \else: |
| 262 |
% \prg_return_false: |
| 263 |
% \fi: |
| 264 |
% } |
| 265 |
% \end{Verbatim} |
| 266 |
% And if one tried to use \cs{named} in that definition it would fail |
| 267 |
% because this command takes one extra argument before the |
| 268 |
% \meta{replacement~text}. Something could be done to allow one (or |
| 269 |
% more) extra argument before the \meta{replacement~text}. |
| 270 |
% |
| 271 |
% \medskip |
| 272 |
% One serious limitation is when used with definitions that expand their |
| 273 |
% argument, namely \cs{edef} and \cs{xdef}. This type of definition |
| 274 |
% expands tokens as it reads them, possibly changing their meaning |
| 275 |
% during the process. \cs{named}, however, first grabs the definition as |
| 276 |
% argument to process the named arguments before actually performing the |
| 277 |
% definition, so these ``unexpected'' changes of meaning might make the |
| 278 |
% code misbehave. While writing this manual I could think of two (and a |
| 279 |
% half) situations which will be problematic and how to work around them |
| 280 |
% (sorry, no solution for now; suggestions are welcome :). |
| 281 |
% |
| 282 |
%^^A \begingroup |
| 283 |
% \edef\hash{\string#} |
| 284 |
% \edef\¿{\csname @gobble\expandafter\endcsname\string\\} |
| 285 |
% \def\nedeft{\ttfamily\¿named\¿edef\¿test} |
| 286 |
% |
| 287 |
% \subsubsection{\nedeft\{\¿string\#[arg]\}} |
| 288 |
% \label{sec:edef wa1} |
| 289 |
% |
| 290 |
% The normal (no \cs{named}) counterpart of this one is a perfectly |
| 291 |
% valid definition: |\edef\test{\string#1}|. While expanding the |
| 292 |
% \meta{replacement~text}, \cs{string} turns the parameter token |#|$_6$ |
| 293 |
% into a character |#|$_{12}$, thus defining |\test| to expand to the |
| 294 |
% two characters |#1|. When using \cs{named}, however, the replacement |
| 295 |
% routine doesn't know what \cs{string} does to the token next to it, so |
| 296 |
% it goes on and treats |#[arg]| as one named argument only to find out |
| 297 |
% that it was never defined in the \meta{parameter~text}, so it aborts |
| 298 |
% the definition with an error. |
| 299 |
% |
| 300 |
% This will occur in the specific case where the user wishes to have the |
| 301 |
% macro expand to the characters |#[arg]|, without replacement by a |
| 302 |
% parameter. In this case the work-around is to temporarily switch the |
| 303 |
% delimiter tokens of \cs{named}'s scanner: |
| 304 |
% \begin{Verbatim} |
| 305 |
% \NamedDelim|| |
| 306 |
% \named\edef\test{\string#[arg]} |
| 307 |
% \NamedDelim[] |
| 308 |
% \end{Verbatim} |
| 309 |
% in which case the scanner will still see the |#| as a parameter token |
| 310 |
% but since it is no longer followed by a delimiter, it will be simply |
| 311 |
% passed on to the definition. Afterwards, at the time \TeX{} tries to |
| 312 |
% carry on with the definition, \cs{string} will do its thing to |#| |
| 313 |
% without further problems. |
| 314 |
% |
| 315 |
% \subsubsection{\nedeft\#[arg]\{\¿string\}\#[arg]\}} |
| 316 |
% \label{sec:edef wa2} |
| 317 |
% |
| 318 |
% This one, as the previous, works fine without \cs{named}: |
| 319 |
% |\edef\test#1{\string}#1}|. Again, when \TeX{} scans this definition, |
| 320 |
% it will expand \cs{string} which will turn the end group token |}|$_2$ |
| 321 |
% into a character |}|$_{12}$, which will have \TeX{} end the definition |
| 322 |
% with the next~|}|$_2$, after the |#1|. This only works because \TeX{} |
| 323 |
% does not grab the whole thing as argument before expanding. Which is |
| 324 |
% precisely what \cs{named} does. |
| 325 |
% |
| 326 |
% When \cs{named} grabs the definition as argument the first |}| |
| 327 |
% ends the \meta{replacement~text}, so what \cs{named} effectively sees |
| 328 |
% is |\edef\test#[arg]{\string}|, which is then processed (|#[arg]| is |
| 329 |
% replaced by |#1|) and left back in the input stream for \TeX{} to do |
| 330 |
% its thing, however the replacement |#[arg]| is never replaced: |
| 331 |
% |\edef\test#1{\string}#[arg]}|, then when \TeX{} tries to do the |
| 332 |
% definition it will complain about an ``! Illegal parameter number in |
| 333 |
% definition of |\test|.'' |
| 334 |
% |
| 335 |
% The work-around in this case is to do a dirty brace trick to lure |
| 336 |
% \cs{named} into grabbing the whole thing as argument, but which will |
| 337 |
% disappear at the time \TeX{} performs the expansion and definition. |
| 338 |
% One option is |\iffalse{\fi|: |
| 339 |
% \begin{Verbatim} |
| 340 |
% \named\edef\test#[arg]{% |
| 341 |
% \iffalse{\fi \string}#[arg]} |
| 342 |
% \end{Verbatim} |
| 343 |
% In this example \cs{named} will process everything, but at the time of |
| 344 |
% the definition the entire |\iffalse{\fi| thing expands to nothing and |
| 345 |
% the final definition becomes the same as the one without \cs{named}. |
| 346 |
% The |\iffalse{\fi| block can also be left in the definition |
| 347 |
% \emph{without} named and the result will be the same. One could argue |
| 348 |
% that using the brace hack is safer because it doesn't change the |
| 349 |
% definition, yet avoid problems when grabbing the definition as |
| 350 |
% argument in a general situation. |
| 351 |
% |
| 352 |
% \let\SVsubsec\thesubsubsection |
| 353 |
% \def\thesubsubsection{^^A |
| 354 |
% \thesubsection .\number\numexpr\value{subsubsection}-1,5} |
| 355 |
% |
| 356 |
% \subsubsection{\nedeft\#[arg]\{\¿string\{\#[arg]\}} |
| 357 |
% \label{sec:edef wa3} |
| 358 |
% |
| 359 |
% \let\thesubsubsection\SVsubsec |
| 360 |
%^^A \endgroup |
| 361 |
% |
| 362 |
% This is rather similar to the previous one, except that the brace |
| 363 |
% later-to-be-detokenized begins a group: |\edef\test#1{\string{#1}|. |
| 364 |
% Here \TeX{} also expands \cs{string} and makes |{|$_1$ a |{|$_{12}$ |
| 365 |
% which does not count in the brace balancing. \cs{named}, however, will |
| 366 |
% count it when grabbing the definition as argument and will require one |
| 367 |
% more |}|$_2$. If the code is run as is \TeX{} will probably report a |
| 368 |
% ``File ended while scanning use of \dots'' error unless there happens |
| 369 |
% to be a matching |}|$_2$ somewhere else, in which case the definition |
| 370 |
% will just go wrong. The work-around here is the same as the one |
| 371 |
% before, with |}| instead: |
| 372 |
% \begin{Verbatim} |
| 373 |
% \named\edef\test#[arg]{% |
| 374 |
% \string{#[arg]\iffalse}\fi} |
| 375 |
% \end{Verbatim} |
| 376 |
% This will ensure that \cs{named} will see the |}| it needs to grab the |
| 377 |
% definition correctly and will disappear once the definition is done. |
| 378 |
% |
| 379 |
% \subsection{Invalid delimiters} |
| 380 |
% \label{sec:delimiters} |
| 381 |
% |
| 382 |
% The delimiters that can be used should be character tokens with |
| 383 |
% catcode $3$, $4$, $7$, $8$, $11$, $12$, or $13$. Characters with |
| 384 |
% catcode $0$, $5$, $9$, $14$, and $15$ don't produce tokens to start |
| 385 |
% with, so they can't possibly be used. The remaining category codes |
| 386 |
% are currently disallowed in the code because they make the input |
| 387 |
% ambiguous or because they make the implementation more complex with no |
| 388 |
% real advantage. |
| 389 |
% |
| 390 |
% Catcodes $1$ and $2$ (begin-/end-group) cannot be used because they |
| 391 |
% become indistinguishable from the braces that delimit the |
| 392 |
% \meta{parameter~text} of the definition, so the input is ambiguous. |
| 393 |
% |
| 394 |
% Catcode $6$ (macro parameter) cannot be used because it gets hard to |
| 395 |
% distinguish a named parameter from some text surrounded by parameter |
| 396 |
% tokens. For example in: |\named\edef\foo#name#{\string#then#name#}|, |
| 397 |
% \pkg{namedef} would raise an error on |#then#| (unknown parameter) |
| 398 |
% without knowing that the first |#|$_{6}$ becomes |#|$_{12}$ and the |
| 399 |
% actual parameter is |#name#|\dots Or is it? I'm not entirely convinced |
| 400 |
% of my own argument, so this might be implemented in the future. |
| 401 |
% |
| 402 |
% Catcode $10$ (blank space) is possible but it requires a hanful of |
| 403 |
% extra precautions to avoid losing the space when passing arguments |
| 404 |
% around. Since it makes for a strange-looking syntax (our eyes are |
| 405 |
% trained to ignore spaces), this is not supported. |
| 406 |
% |
| 407 |
% \section{Boring examples} |
| 408 |
% |
| 409 |
% The following examples show two definitions each, which are the same, |
| 410 |
% but the second uses \cs{named}. The third line in each example shows |
| 411 |
% the \cs{meaning} of the defined macro. |
| 412 |
% |
| 413 |
% First the basics, replacing a numbered parameter by a named one: |
| 414 |
% \named\def\hello#[who]{Hello #[who]!} |
| 415 |
% \begin{example}[label=Basics] |
| 416 |
% \def\hello#1{Hello #1!} |
| 417 |
% \named\def\hello#[who]{Hello #[who]!} |
| 418 |
% |mshow|hello |
| 419 |
% \end{example} |
| 420 |
% \smallskip |
| 421 |
% |
| 422 |
% Prefixes can be added at will after the \cs{named} prefix: |
| 423 |
% \named\protected\long\edef\hello#[who]{Hello #[who]!} |
| 424 |
% \begin{example}[label=Prefixes] |
| 425 |
% \protected\long\edef\hello#1{Hello #1!} |
| 426 |
% \named\protected\long\edef\hello#[who]{Hello #[who]!} |
| 427 |
% |mshow|hello |
| 428 |
% \end{example} |
| 429 |
% \smallskip |
| 430 |
% |
| 431 |
%^^A Whatever prefixes put before \cs{named} will be lost. Notice that |
| 432 |
%^^A even though \cs{protected} is used, the macro itself is not defined |
| 433 |
%^^A with \cs{protected}: |
| 434 |
%^^A \protected\named\long\edef\hello#[who]{Hello #[who]!} |
| 435 |
%^^A \begin{example}[label=Wrong Prefixes] |
| 436 |
%^^A \protected \long\edef\hello#1{Hello #1!} |
| 437 |
%^^A \protected\named\long\edef\hello#[who]{Hello #[who]!} |
| 438 |
%^^A |mshow|hello |
| 439 |
%^^A \end{example} |
| 440 |
%^^A \smallskip |
| 441 |
% |
| 442 |
% This example is just to show that the named argument delimiter doesn't |
| 443 |
% interfere with the text in the macro: |
| 444 |
% \named\def\hello[#[who]]{Hello #[who]!} |
| 445 |
% \begin{example}[label=Argument delimited by \texttt{[} and \texttt{]}] |
| 446 |
% \def\hello[#1]{Hello #1!} |
| 447 |
% \named\def\hello[#[who]]{Hello #[who]!} |
| 448 |
% |mshow|hello |
| 449 |
% \end{example} |
| 450 |
% \smallskip |
| 451 |
% |
| 452 |
% However, for readability, the delimiter can be changed to something |
| 453 |
% else: |
| 454 |
% \named\def\hello[#[who]]{Hello #[who]!} |
| 455 |
% \begin{example}[label=Argument delimited by \texttt{[} and \texttt{]}, |
| 456 |
% commandchars=/<>,firstnumber=0] |
| 457 |
% \NamedDelim{|}{|} |
| 458 |
% \def\hello[#1]{Hello #1!} |
| 459 |
% \named\def\hello[#|who|]{Hello #|who|!} |
| 460 |
% /mshow/hello |
| 461 |
% \end{example} |
| 462 |
% \smallskip |
| 463 |
% |
| 464 |
% This example demonstrates multiple arguments and arbitrary |
| 465 |
% \meta{parameter~text}: |
| 466 |
% \named\def\cfbox #[color] to #[width]#[content]{% |
| 467 |
% \fbox{\hbox to #[width]{\color{#[color]}#[content]}}} |
| 468 |
% \begin{example}[label=More arguments] |
| 469 |
% \def\cfbox #1 to #2#3{\fbox{\hbox to #2{\color{#1}#3}}} |
| 470 |
% \named\def\cfbox #[color] to #[width]#[content]{% |
| 471 |
% \fbox{\hbox to #[width]{\color{#[color]}#[content]}}} |
| 472 |
% |mshow|cfbox |
| 473 |
% \end{example} |
| 474 |
% \smallskip |
| 475 |
% |
| 476 |
% \TeX's weird |#{| argument can be used as well: |
| 477 |
% \named\def\cbox #[color] to #[width]#{^^A |
| 478 |
% \hbox to #[width]\bgroup\color{#[color]}\let\next= } |
| 479 |
% \begin{example}[label=Weird arguments] |
| 480 |
% \def\cbox #1 to #2#{\hbox to #2\bgroup\color{#1}\let\next= } |
| 481 |
% \named\def\cbox #[color] to #[width]#{% |
| 482 |
% \hbox to #[width]\bgroup\color{#[color]}\let\next= } |
| 483 |
% |mshow|cbox |
| 484 |
% \end{example} |
| 485 |
% \smallskip |
| 486 |
% |
| 487 |
% {\NamedDelim XX |
| 488 |
% \named\xdef\test{\string#[arg]}} |
| 489 |
% \begin{example}[commandchars=/<>, label= |
| 490 |
% \texttt{\string\edef} workaround \ref{sec:edef wa1}] |
| 491 |
% \edef\test{\string#[arg]} |
| 492 |
% \NamedDelim || /global/linenofalse |
| 493 |
% \named\edef\test{\string#[arg]} /global/linenofalse |
| 494 |
% \NamedDelim [] |
| 495 |
% /mshow/test |
| 496 |
% \end{example} |
| 497 |
% \smallskip |
| 498 |
% |
| 499 |
% \named\edef\test#[arg]{\iffalse{\fi\string}#[arg]} |
| 500 |
% \begin{example}[label= |
| 501 |
% \texttt{\string\edef} workaround \ref{sec:edef wa2}] |
| 502 |
% \edef\test#1{\string}#1} |
| 503 |
% \named\edef\test#[arg]{\iffalse{\fi\string}#[arg]} |
| 504 |
% |mshow|test |
| 505 |
% \end{example} |
| 506 |
% \smallskip |
| 507 |
% |
| 508 |
% \named\edef\test#[arg]{\string{#[arg]\iffalse}\fi} |
| 509 |
% \begin{example}[label= |
| 510 |
% \texttt{\string\edef} workaround \ref{sec:edef wa3}] |
| 511 |
% \edef\test#1{\string{#1} |
| 512 |
% \named\edef\test#[arg]{\string{#[arg]\iffalse}\fi} |
| 513 |
% |mshow|test |
| 514 |
% \end{example} |
| 515 |
% \smallskip |
| 516 |
% |
| 517 |
% \section{Interesting\protect\footnotemark\ examples} |
| 518 |
% \footnotetext{Terms and Conditions may apply.} |
| 519 |
% |
| 520 |
% These examples shows a few more elaborate ways to use \pkg{namedef}. |
| 521 |
% |
| 522 |
% \subsection{Extended \cs{newcommand}} |
| 523 |
% \label{sec:newcommand} |
| 524 |
% |
| 525 |
% Here's an implementation to allow the syntax of \pkg{namedef} in |
| 526 |
% \cs{newcommand}. It uses \pkg{xparse} to handle optional arguments, |
| 527 |
% and uses \cs{newcommand} itself to define (possibly) optional argument |
| 528 |
% handling, so the resulting command uses \LaTeXe's command parsing |
| 529 |
% machinery. |
| 530 |
% |
| 531 |
% The syntax of the defined command is: |
| 532 |
% |
| 533 |
% |\newnamedcommand|\meta{*}\cs[no-index]{\meta{cmd}}\oarg{arg-list}^^A |
| 534 |
% [\meta{opt}=\meta{default}]\marg{definition} |
| 535 |
% |
| 536 |
% \noindent where everything is the same as in regular \cs{newcommand}, |
| 537 |
% except for the optional arguments \meta{arg-list} and |
| 538 |
% \meta{opt}=\meta{default}. \meta{arg-list} should be a |
| 539 |
% comma-separated list of named parameters as \cs{named} would expect, |
| 540 |
% and \meta{opt} is a named parameter, and \meta{default} is its default |
| 541 |
% value. The usage would be something like: |
| 542 |
% |
| 543 |
% |\newnamedcommand\foo[#[one],#[two]][#[opt]=default]%|\par |
| 544 |
% \quad|{#[one], #[two], and #[opt]}| |
| 545 |
% |
| 546 |
% \noindent which translates to: |
| 547 |
% |
| 548 |
% |\newcommand\foo[3][default]%|\par |
| 549 |
% \quad|{#2, #3, and #1}| |
| 550 |
% |
| 551 |
% First, load \pkg{xparse} and \pkg{namedef}, and define the top-level |
| 552 |
% commands to use a common |\NNC_newcommand:NnNnnn|. |
| 553 |
% |\NNC_newcommand:NnNnnn| will store the mandatory arguments in |
| 554 |
% a \texttt{seq} variable for easier access, then call |
| 555 |
% |\__NNC_newcommand:NnNn| to do the \cs{newcommand} part of the job, |
| 556 |
% and |\__NNC_named_def:nNnn| to \cs{named} part. |
| 557 |
% |\new...|, |\renew...|, and |\provide...| versions are defined, but |
| 558 |
% since a \cs{def} is used later with no checking, the behaviour is |
| 559 |
% not exactly the same as you'd get with \cs{newcommand} in this regard. |
| 560 |
% |
| 561 |
% \begin{example} |
| 562 |
% \usepackage{namedef} |
| 563 |
% \usepackage{xparse} |
| 564 |
% \ExplSyntaxOn |
| 565 |
% \seq_new:N \l__NNC_args_seq |
| 566 |
% \scan_new:N \s__NNC |
| 567 |
% \NewDocumentCommand \newnamedcommand { s m o o m } |
| 568 |
% { \NNC_newcommand:NnNnnn \newcommand {#1} #2 {#3} {#4} {#5} } |
| 569 |
% \NewDocumentCommand \renewnamedcommand { s m o o m } |
| 570 |
% { \NNC_newcommand:NnNnnn \renewcommand {#1} #2 {#3} {#4} {#5} } |
| 571 |
% \NewDocumentCommand \providenamedcommand { s m o o m } |
| 572 |
% { \NNC_newcommand:NnNnnn \providecommand {#1} #2 {#3} {#4} {#5} } |
| 573 |
% \named \cs_new_protected:Npn \NNC_newcommand:NnNnnn |
| 574 |
% #[newcmd] #[star] #[cmd] #[args] #[opt] #[defn] |
| 575 |
% { |
| 576 |
% \seq_clear:N \l__NNC_args_seq |
| 577 |
% \IfValueT {#[args]} |
| 578 |
% { \seq_set_from_clist:Nn \l__NNC_args_seq {#[args]} } |
| 579 |
% \__NNC_newcommand:NnNn #[newcmd] {#[star]} #[cmd] {#[opt]} |
| 580 |
% \__NNC_named_def:nNnn {#[star]} #[cmd] {#[opt]} {#[defn]} |
| 581 |
% } |
| 582 |
% \end{example} |
| 583 |
% |
| 584 |
% |\__NNC_newcommand:NnNn| does the \cs{newcommand} part of the job. It |
| 585 |
% takes the arguments read in by \pkg{xparse}, and translates them into |
| 586 |
% the \cs{newcommand} syntax. The number of items in the |#[args]| |
| 587 |
% parameter is counted and left within brackets, and the default value |
| 588 |
% of the optional argument is also left within another pair of brackets. |
| 589 |
% This step is executed with an empty definition because the named |
| 590 |
% parameters will cause havoc in \cs{newcommand}. |
| 591 |
% |
| 592 |
% \begin{example}[firstnumber=last] |
| 593 |
% \named \cs_new_protected:Npn \__NNC_newcommand:NnNn |
| 594 |
% #[newcmd] #[star] #[cmd] #[opt] |
| 595 |
% { |
| 596 |
% \use:x |
| 597 |
% { |
| 598 |
% \exp_not:N #[newcmd] |
| 599 |
% \IfBooleanT {#[star]} { * } |
| 600 |
% \exp_not:N #[cmd] |
| 601 |
% \seq_if_empty:NF \l__NNC_args_seq |
| 602 |
% { [ \seq_count:N \l__NNC_args_seq ] } |
| 603 |
% \IfValueT {#[opt]} { [ \__NNC_opt_value:w #[opt] \s__NNC ] } |
| 604 |
% { } |
| 605 |
% } |
| 606 |
% } |
| 607 |
% \cs_new:Npn \__NNC_opt_value:w #1 = #2 \s__NNC {#2} |
| 608 |
% \end{example} |
| 609 |
% |
| 610 |
% Now |\__NNC_named_def:nNnn| will do the \cs{named} part. |
| 611 |
% First the |#[star]| argument (if not present) becomes \cs{long}, and |
| 612 |
% then comes \cs{named} and \cs{def}. Then, if an optional argument was |
| 613 |
% given, the command we need to define is |\\foo| rather than |\foo|, so |
| 614 |
% use take care of that with \cs{token_to_str:N}, and then leave the |
| 615 |
% named parameter given for the optional argument within brackets. |
| 616 |
% If there's no optional argument, we just define |#[cmd]| (pretty |
| 617 |
% boring). Then we call \cs{seq_use:Nn} on the mandatory arguments to |
| 618 |
% lay them flat for \cs{named}, and then the parameter text (|#[defn]|), |
| 619 |
% unexpanded. |
| 620 |
% |
| 621 |
% \begin{example}[firstnumber=last] |
| 622 |
% \named \cs_new_protected:Npn \__NNC_named_def:nNnn |
| 623 |
% #[star] #[cmd] #[opt] #[defn] |
| 624 |
% { |
| 625 |
% \use:x |
| 626 |
% { |
| 627 |
% \IfBooleanF {#[star]} { \long } |
| 628 |
% \named \def |
| 629 |
% \IfValueTF {#[opt]} |
| 630 |
% { |
| 631 |
% \exp_not:c { \token_to_str:N #[cmd] } |
| 632 |
% [ \exp_not:o { \__NNC_opt_name:w #[opt] \s__NNC } ] |
| 633 |
% } |
| 634 |
% { \exp_not:N #[cmd] } |
| 635 |
% \seq_use:Nn \l__NNC_args_seq { } |
| 636 |
% { \exp_not:n {#[defn]} } |
| 637 |
% } |
| 638 |
% } |
| 639 |
% \cs_new:Npn \__NNC_opt_name:w #1 = #2 \s__NNC {#1} |
| 640 |
% \ExplSyntaxOff |
| 641 |
% \end{example} |
| 642 |
% |
| 643 |
% \end{documentation} |
| 644 |
% |
| 645 |
% \begin{implementation} |
| 646 |
% |
| 647 |
% \section{\pkg{namedef} Implementation} |
| 648 |
% |
| 649 |
% \begin{macrocode} |
| 650 |
%<*package> |
| 651 |
%<@@=namedef> |
| 652 |
% \end{macrocode} |
| 653 |
% |
| 654 |
% \subsection{Loading} |
| 655 |
% |
| 656 |
% \begin{macro}{\@@_end_package_hook:} |
| 657 |
% Load \pkg{expl3}, either through \cs{RequirePackage} or through |
| 658 |
% inputting the generic loader, depending on the format in use |
| 659 |
% (copied from Bruno Le Floch's \pkg{gtl}). |
| 660 |
% \begin{macrocode} |
| 661 |
\begingroup\expandafter\expandafter\expandafter\endgroup |
| 662 |
\expandafter\ifx\csname RequirePackage\endcsname\relax |
| 663 |
\input expl3-generic.tex |
| 664 |
\else |
| 665 |
\RequirePackage{expl3}[2018-05-15] |
| 666 |
\fi |
| 667 |
\ExplSyntaxOn |
| 668 |
\cs_if_exist:NTF \ProvidesExplPackage |
| 669 |
{ |
| 670 |
\cs_new_eq:NN \@@_end_package_hook: \prg_do_nothing: |
| 671 |
\ExplSyntaxOff |
| 672 |
\ProvidesExplPackage |
| 673 |
} |
| 674 |
{ |
| 675 |
\cs_new_eq:NN \@@_end_package_hook: \ExplSyntaxOff |
| 676 |
\group_begin: |
| 677 |
\ExplSyntaxOff |
| 678 |
\cs_set_protected:Npn \@@_tmp:w #1#2#3#4 |
| 679 |
{ |
| 680 |
\group_end: |
| 681 |
\tl_gset:cx { ver @ #1 . sty } { #2 ~ v#3 ~ #4 } |
| 682 |
\cs_if_exist_use:NF \wlog { \iow_log:x } |
| 683 |
{ Package: ~ #1 ~ #2 ~ v#3 ~ #4 } |
| 684 |
} |
| 685 |
\@@_tmp:w |
| 686 |
} |
| 687 |
{namedef} {\namedefDate} {\namedefVersion} |
| 688 |
{Named parameters in TeX definitions (PHO)} |
| 689 |
% \end{macrocode} |
| 690 |
% \end{macro} |
| 691 |
% |
| 692 |
% \subsection{Declarations} |
| 693 |
% |
| 694 |
% \begin{macro}{\flag @@_parm_count} |
| 695 |
% A flag (mis)used as a counter to keep track of the parameter number. |
| 696 |
% \begin{macrocode} |
| 697 |
\flag_new:n { @@_parm_count } |
| 698 |
% \end{macrocode} |
| 699 |
% \end{macro} |
| 700 |
% |
| 701 |
% \begin{macro}{\c_@@_prefix_tl} |
| 702 |
% A prefix to use as name space for temporary macros. |
| 703 |
% \begin{macrocode} |
| 704 |
\tl_const:Nn \c_@@_prefix_tl { namedef~parm~-> } |
| 705 |
% \end{macrocode} |
| 706 |
% \end{macro} |
| 707 |
% |
| 708 |
% \begin{macro}{\l_@@_macro_tl} |
| 709 |
% A token list to store the name of the macro meing defined for error |
| 710 |
% messages. |
| 711 |
% \begin{macrocode} |
| 712 |
\tl_new:N \l_@@_macro_tl |
| 713 |
% \end{macrocode} |
| 714 |
% \end{macro} |
| 715 |
% |
| 716 |
% \begin{macro}{\q_@@_mark,\q_@@_stop} |
| 717 |
% Quarks used throughout the package. |
| 718 |
% \begin{macrocode} |
| 719 |
\quark_new:N \q_@@_mark |
| 720 |
\quark_new:N \q_@@_stop |
| 721 |
% \end{macrocode} |
| 722 |
% \end{macro} |
| 723 |
% |
| 724 |
% \begin{macro}{\s_@@} |
| 725 |
% Scan mark used to skip code. |
| 726 |
% \begin{macrocode} |
| 727 |
\scan_new:N \s_@@ |
| 728 |
% \end{macrocode} |
| 729 |
% \end{macro} |
| 730 |
% |
| 731 |
% \begin{macro}{\@@_skip_to_scan_mark:w, \@@_skip_to_scan_mark:nw} |
| 732 |
% Consume everything up to \cs{s_@@}. |
| 733 |
% \begin{macrocode} |
| 734 |
\cs_new:Npn \@@_skip_to_scan_mark:w #1 \s_@@ { } |
| 735 |
\cs_new:Npn \@@_skip_to_scan_mark:nw #1 #2 \s_@@ {#1} |
| 736 |
% \end{macrocode} |
| 737 |
% \end{macro} |
| 738 |
% |
| 739 |
% \begin{macro}{\@@_tmp:w} |
| 740 |
% A scratch macro. |
| 741 |
% \begin{macrocode} |
| 742 |
\cs_new_eq:NN \@@_tmp:w ? |
| 743 |
% \end{macrocode} |
| 744 |
% \end{macro} |
| 745 |
% |
| 746 |
% \subsection{The top-level \cs{named} macro} |
| 747 |
% |
| 748 |
% \begin{macro}{\named} |
| 749 |
% \begin{macro}{\@@_grab_prefix:nN} |
| 750 |
% Starts scanning ahead for prefixes and the definition |
| 751 |
% command. Once finished the scanning of prefixes, call |
| 752 |
% \cs{@@_replace_named:nNnn} to do the heavy lifting. |
| 753 |
% \begin{macrocode} |
| 754 |
\cs_new_protected:Npn \named { \@@_grab_prefix:nN { } } |
| 755 |
\cs_new_protected:Npn \@@_grab_prefix:nN #1 #2 |
| 756 |
{ |
| 757 |
\@@_if_prefix:NTF #2 |
| 758 |
{ \@@_grab_prefix:nN } |
| 759 |
{ \@@_detect_prefixes:Nn \@@_kill_outer:nN } |
| 760 |
{ #1#2 } |
| 761 |
} |
| 762 |
% \end{macrocode} |
| 763 |
% \end{macro} |
| 764 |
% \end{macro} |
| 765 |
% |
| 766 |
% \begin{macro}[pTF]{\@@_if_prefix:N} |
| 767 |
% Checks against a list of valid prefixes and returns |true| or |
| 768 |
% |false| accordingly. |
| 769 |
% \begin{macrocode} |
| 770 |
\prg_new_conditional:Npnn \@@_if_prefix:N #1 { TF } |
| 771 |
{ |
| 772 |
\if_int_compare:w 0 |
| 773 |
\if_meaning:w \tex_protected:D #1 1 \fi: |
| 774 |
\if_meaning:w \tex_global:D #1 1 \fi: |
| 775 |
\if_meaning:w \tex_outer:D #1 1 \fi: |
| 776 |
\if_meaning:w \tex_long:D #1 1 \fi: |
| 777 |
\if_meaning:w \scan_stop: #1 1 \fi: |
| 778 |
= 1 \exp_stop_f: |
| 779 |
\prg_return_true: |
| 780 |
\else: |
| 781 |
\prg_return_false: |
| 782 |
\fi: |
| 783 |
} |
| 784 |
% \end{macrocode} |
| 785 |
% \end{macro} |
| 786 |
% |
| 787 |
% \begin{macro}{\@@_detect_prefixes:Nn} |
| 788 |
% \begin{macro}[rEXP]{\@@_extract_prefixes:w} |
| 789 |
% \begin{macro}[rEXP]{ |
| 790 |
% \@@_extract_protected:n, \@@_extract_protected_aux:w, |
| 791 |
% \@@_extract_long:n, \@@_extract_long_aux:w, |
| 792 |
% \@@_extract_outer:n, \@@_extract_outer_aux:w, |
| 793 |
% } |
| 794 |
% Defines a scratch macro \cs{@@_tmp:w} and queries its prefixes, then |
| 795 |
% forwards them to the the next macro to perform the parameter |
| 796 |
% replacement and definition. |
| 797 |
% |
| 798 |
% This code would be quite a lot simpler if \cs{outer} didn't exist. |
| 799 |
% First extract the meaning of \cs{@@_tmp:w}, and pass the prefixes |
| 800 |
% (before ``|macro:|'') to \cs{@@_extract_prefixes:w}, and then to |
| 801 |
% \cs{@@_extract_protected:n}, \cs{@@_extract_long:n}, and |
| 802 |
% \cs{@@_extract_outer:n} in turn to check if each of these prefixes |
| 803 |
% is there. |
| 804 |
% |
| 805 |
% \cs{global} can't be checked this way because it's different from |
| 806 |
% other prefixes in the sense that it affects the definition \emph{at |
| 807 |
% the time} of the definition, rather than at the time it is used. |
| 808 |
% I don't know if it's possible to detect a \cs{global} after it's |
| 809 |
% already consumed by \TeX. |
| 810 |
% |
| 811 |
% \TODO{\cs{afterassignment} tokens are lost. It would be nice if |
| 812 |
% it were possible to retrieve it and save until later.} |
| 813 |
% \begin{macrocode} |
| 814 |
\cs_new_protected:Npn \@@_detect_prefixes:Nn #1 #2 |
| 815 |
{ |
| 816 |
\cs_set_nopar:Npn \@@_tmp:w { } |
| 817 |
\use:x |
| 818 |
{ |
| 819 |
\exp_not:N #1 |
| 820 |
{ |
| 821 |
\exp_after:wN \exp_after:wN |
| 822 |
\exp_after:wN \@@_extract_prefixes:w |
| 823 |
\exp_after:wN \token_to_meaning:N |
| 824 |
\cs:w @@_tmp:w \cs_end: \s_@@ |
| 825 |
\exp_not:n {#2} |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
\use:x |
| 830 |
{ |
| 831 |
\cs_new:Npn \exp_not:N \@@_extract_prefixes:w ##1 |
| 832 |
\tl_to_str:n { macro: } ##2 \s_@@ |
| 833 |
{ |
| 834 |
\exp_not:N \@@_extract_protected:n {##1} |
| 835 |
\exp_not:N \@@_extract_long:n {##1} |
| 836 |
\exp_not:N \@@_extract_outer:n {##1} |
| 837 |
} |
| 838 |
} |
| 839 |
\cs_set_protected:Npn \@@_tmp:w #1 #2 |
| 840 |
{ |
| 841 |
\use:x |
| 842 |
{ |
| 843 |
\cs_new:cpn { @@_extract_#1:n } ####1 |
| 844 |
{ |
| 845 |
\exp_not:c { @@_extract_#1_aux:w } ####1 |
| 846 |
\token_to_str:N #2 \scan_stop: \token_to_str:N #2 \s_@@ |
| 847 |
} |
| 848 |
\cs_new:cpn { @@_extract_#1_aux:w } ####1 |
| 849 |
\token_to_str:N #2 ####2 \token_to_str:N #2 ####3 \s_@@ |
| 850 |
{ |
| 851 |
\exp_not:N \if_meaning:w \scan_stop: ####2 |
| 852 |
\exp_not:N \else: |
| 853 |
\exp_not:c { tex_#1:D } |
| 854 |
\exp_not:N \fi: |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
\@@_tmp:w { protected } { \protected } |
| 859 |
\@@_tmp:w { long } { \long } |
| 860 |
\@@_tmp:w { outer } { \outer } |
| 861 |
% \end{macrocode} |
| 862 |
% \end{macro} |
| 863 |
% \end{macro} |
| 864 |
% \end{macro} |
| 865 |
% |
| 866 |
% \subsection{Main routine} |
| 867 |
% |
| 868 |
% \begin{macro}{\@@_kill_outer:nN} |
| 869 |
% \begin{macro}{\@@_start:nNp, \@@_replace_named:nNnn} |
| 870 |
% \begin{macro}{\@@_replace_parameter:Nn, \@@_parameter_output:nnw} |
| 871 |
% \begin{macro}{\@@_handle_parameter:nN} |
| 872 |
% \begin{macro}{\@@_define:nnnN} |
| 873 |
% Here we play dirty: abuse the fact that \cs{exp_not:N} temporarily |
| 874 |
% makes the \cs{noexpand}ed control sequence temporarily equal to |
| 875 |
% \cs{relax}. But rather than using it in an \cs{edef} or whatnot, |
| 876 |
% hit \cs{exp_not:N} with \cs{exp_after:wN}, and then quickly grab |
| 877 |
% it with \cs{@@_start:nNp}, so it's safe to grab it, even if it's |
| 878 |
% \cs{outer}. If that wasn't bad enough, do it once again to make |
| 879 |
% it equal to \cs{relax} in the scope of \cs{@@_replace_named:nNnn} |
| 880 |
% so that it doesn't blow up. |
| 881 |
% |
| 882 |
% \TODO{Currently |\named\def{\foo}{bar}| works, but perhaps it |
| 883 |
% shouldn't.} |
| 884 |
% \begin{macrocode} |
| 885 |
\cs_new_protected:Npn \@@_kill_outer:nN #1 |
| 886 |
{ |
| 887 |
\cs_set:Npn \@@_tmp:w { \@@_start:nNp {#1} } |
| 888 |
\exp_after:wN \@@_tmp:w \exp_not:N |
| 889 |
} |
| 890 |
\cs_new_protected:Npn \@@_start:nNp #1 #2 #3 # |
| 891 |
{ |
| 892 |
\group_begin: |
| 893 |
\int_set:Nn \tex_escapechar:D { `\\ } |
| 894 |
\exp_after:wN \cs_set_eq:NN \exp_not:N #2 \scan_stop: |
| 895 |
\@@_replace_named:nNnn {#1} #2 {#3} |
| 896 |
} |
| 897 |
% \end{macrocode} |
| 898 |
% |
| 899 |
% Here the actual replacement of named parameters by numbered ones |
| 900 |
% takes place. A group is started to revert the flag and all the |
| 901 |
% defined temporary macros. |
| 902 |
% |
| 903 |
% \cs{@@_replace_parameter:Nn} \cs{@@_in_parameter:nN} starts |
| 904 |
% replacing a dummy macro in the generic parameter replacement routine |
| 905 |
% by the macro which counts the parameters and aliases the named |
| 906 |
% parameters with numbered ones. Finally it starts |
| 907 |
% \cs{@@_replace_parm:Nn}, which scans the \meta{parameter~text} for |
| 908 |
% the named parameters and replaces them by numbered ones. The second |
| 909 |
% output argument of \cs{@@_replace_parm:Nn} is a list of definitions |
| 910 |
% which assign a number to each named parameter so that they can be |
| 911 |
% used in the next step. |
| 912 |
% |
| 913 |
% \cs{@@_replace_parameter:Nn} \cs{@@_in_replacement:nN} then starts |
| 914 |
% by replacing the same dummy macro by one which will replace the |
| 915 |
% named parameter by its number. Again \cs{@@_replace_parm:Nn} is |
| 916 |
% started, and its output is the already-processed part of the |
| 917 |
% \meta{replacement~text}. |
| 918 |
% |
| 919 |
% The output of both steps is inserted after \cs{@@_define:nnnN} (it's |
| 920 |
% missing two arguments in the definition of |
| 921 |
% \cs{@@_replace_named:nNnn}). After all that is done, all the named |
| 922 |
% parameters were replaced by numbered ones, so \cs{@@_define:nnnN} |
| 923 |
% can do its thing. |
| 924 |
% |
| 925 |
% A final quark is put in the input stream for recovery from errors. |
| 926 |
% In a successful run this quark is removed by \cs{@@_define:nnnN}. |
| 927 |
% \begin{macrocode} |
| 928 |
\cs_new_protected:Npn \@@_replace_named:nNnn #1 #2 #3 #4 |
| 929 |
{ |
| 930 |
\tl_set:Nx \l_@@_macro_tl { \token_to_str:N #2 } |
| 931 |
\@@_replace_parameter:Nn \@@_in_parameter:nN {#3} |
| 932 |
\@@_replace_parameter:Nn \@@_in_replacement:nN {#4} |
| 933 |
\@@_define:nnnN {#1} #2 |
| 934 |
\s_@@ |
| 935 |
} |
| 936 |
\cs_new_protected:Npn \@@_define:nnnN #1 #2 #3 #4 |
| 937 |
{ |
| 938 |
\group_end: |
| 939 |
#3#4#2{#1} |
| 940 |
} |
| 941 |
\cs_new_protected:Npn \@@_replace_parameter:Nn #1 #2 |
| 942 |
{ |
| 943 |
\cs_set_eq:NN \@@_handle_parameter:nN #1 |
| 944 |
\@@_replace_parm:Nn \@@_parameter_output:nnw {#2} |
| 945 |
} |
| 946 |
\cs_new_eq:NN \@@_handle_parameter:nN ? |
| 947 |
\cs_new_protected:Npn \@@_parameter_output:nnw #1 #2 |
| 948 |
#3 \@@_define:nnnN |
| 949 |
{ #2 #3 \@@_define:nnnN {#1} } |
| 950 |
% \end{macrocode} |
| 951 |
% \end{macro} |
| 952 |
% \end{macro} |
| 953 |
% \end{macro} |
| 954 |
% \end{macro} |
| 955 |
% \end{macro} |
| 956 |
% |
| 957 |
% \begin{macro}{\@@_in_parameter:nN, \@@_in_replacement:nN} |
| 958 |
% These two functions handle the named parameters when they are found |
| 959 |
% in the \meta{parameter~text} and \meta{replacement~text}, |
| 960 |
% respectively. |
| 961 |
% |
| 962 |
% \cs{@@_in_parameter:nN} checks if the named parameter already exists |
| 963 |
% (with \cs{relax} being |true|) and, in such case, throws an error |
| 964 |
% and inserts the number already set for that named parameter. |
| 965 |
% Otherwise the parameter is \cs{let} to |
| 966 |
% \cs{relax} so that if it is found later an error is issued. Setting |
| 967 |
% a macro to \cs{relax} is an expandable way to define it (the same |
| 968 |
% approach as in \pkg{l3flag}). After that, |
| 969 |
% the \cs{flag @@_parm_count} is raised once and its height is used as |
| 970 |
% the parameter number. The current parameter tokens and the parameter |
| 971 |
% number are flushed to the first (left) output slot, and a definition |
| 972 |
% |\cs_set:cpn {|\cs{c_@@_prefix_tl}~\meta{name}|} {|\meta{number}|}| |
| 973 |
% is appended to the second (right) output slot so that the names can |
| 974 |
% be used in the \meta{replacement~text}. |
| 975 |
% |
| 976 |
% In case of a repeated parameter it is tricky to do anything |
| 977 |
% sensible. In a normal definition, when \TeX{} sees a repeated |
| 978 |
% parameter number (like in |\def\foo#1#1{}|) it just uses the wrong |
| 979 |
% number to a parameter number not yet taken, or ignores the parameter |
| 980 |
% if there's already nine before that. However here we can't guess |
| 981 |
% the name of the next parameter, so we can't do much. The easiest |
| 982 |
% way out is to just use the same parameter number as before and go |
| 983 |
% on with out job: at the end, \TeX{} will complain again about this. |
| 984 |
% \begin{macrocode} |
| 985 |
\cs_new:Npn \@@_in_parameter:nN #1 |
| 986 |
{ |
| 987 |
\if_cs_exist:w \c_@@_prefix_tl #1 \cs_end: |
| 988 |
\exp_after:wN \use_i:nn |
| 989 |
\else: |
| 990 |
\exp_after:wN \use_ii:nn |
| 991 |
\fi: |
| 992 |
{ \msg_expandable_error:nnn { namedef } { repeated-parm } {#1} } |
| 993 |
{ |
| 994 |
\exp_after:wN \use_none:n \cs:w \c_@@_prefix_tl #1 \cs_end: |
| 995 |
\flag_raise:n { @@_parm_count } |
| 996 |
} |
| 997 |
\exp_args:Nf \@@_append_output:nnNwnn |
| 998 |
{ \flag_height:n { @@_parm_count } } |
| 999 |
{#1} |
| 1000 |
} |
| 1001 |
% \end{macrocode} |
| 1002 |
% |
| 1003 |
% \cs{@@_in_replacement:nN} also checks if the named parameter exists, |
| 1004 |
% however now it will be \emph{not} be \cs{relax}, but the number |
| 1005 |
% defined earlier, so \cs{cs_if_exist:cTF} can be safely used. If the |
| 1006 |
% parameter does not exist it was never declared in the |
| 1007 |
% \meta{parameter~text} (somewhat like |\def#1{#2}|), then raise an |
| 1008 |
% error and abort. |
| 1009 |
% Otherwise just flush |#|\meta{number}. |
| 1010 |
% \begin{macrocode} |
| 1011 |
\cs_new:Npn \@@_in_replacement:nN #1 #2 |
| 1012 |
{ |
| 1013 |
\cs_if_exist:cTF { \c_@@_prefix_tl #1 } |
| 1014 |
{ |
| 1015 |
\exp_args:Nf \@@_append_output:nnNwnn |
| 1016 |
{ \use:c { \c_@@_prefix_tl #1 } } |
| 1017 |
{ } |
| 1018 |
} |
| 1019 |
{ |
| 1020 |
\msg_expandable_error:nnn { namedef } { unknown-parm } {#1} |
| 1021 |
\exp_args:Ne \@@_append_output:nnNwnn |
| 1022 |
{ #2 \@@_begin_name_token: #1 \@@_end_name_token: } |
| 1023 |
{ \cs_end: { } \use_none:nn } |
| 1024 |
} |
| 1025 |
#2 |
| 1026 |
} |
| 1027 |
% \end{macrocode} |
| 1028 |
% \end{macro} |
| 1029 |
% |
| 1030 |
% \subsection{Scanning routine} |
| 1031 |
% |
| 1032 |
% \begin{macro}[EXP]{\@@_replace_parm:Nn} |
| 1033 |
% \begin{macro}[EXP]{\@@_replace_loop:w} |
| 1034 |
% \begin{macro}[EXP]{\@@_replace_end:wnn, \@@_flush:nw} |
| 1035 |
% \begin{macro}[EXP]{\@@_append_output:nnNwnn} |
| 1036 |
% \begin{macro}[EXP]{\@@_abort_definition:w} |
| 1037 |
% \cs{@@_replace_parm:Nn} uses the same looping principle as in |
| 1038 |
% \pkg{l3tl}'s \cs{__tl_act:NNNnn}. It scans the input (here, the |
| 1039 |
% \meta{parameter~text} and \meta{replacement~text}, separately) token |
| 1040 |
% by token, differentiating spaces, braced tokens (groups), and |
| 1041 |
% \enquote{normal} tokens. |
| 1042 |
% \begin{macrocode} |
| 1043 |
\cs_new:Npn \@@_replace_parm:Nn #1 #2 |
| 1044 |
{ |
| 1045 |
\exp_after:wN #1 |
| 1046 |
\exp:w |
| 1047 |
\@@_replace_loop:w #2 |
| 1048 |
\q_@@_mark \q_@@_stop { } { } |
| 1049 |
} |
| 1050 |
\cs_new:Npn \@@_replace_loop:w #1 \q_@@_stop |
| 1051 |
{ |
| 1052 |
\tl_if_head_is_N_type:nTF {#1} |
| 1053 |
{ \@@_replace_normal:N } |
| 1054 |
{ |
| 1055 |
\tl_if_head_is_group:nTF {#1} |
| 1056 |
{ \@@_replace_group:n } |
| 1057 |
{ \@@_replace_space:w } |
| 1058 |
} |
| 1059 |
#1 \q_@@_stop |
| 1060 |
} |
| 1061 |
\cs_new:Npn \@@_replace_end:wnn \q_@@_stop #1 #2 |
| 1062 |
{ \exp_end: {#1} {#2} } |
| 1063 |
\cs_new:Npn \@@_flush:nw #1 |
| 1064 |
#2 \q_@@_stop #3 #4 |
| 1065 |
{ \@@_replace_loop:w #2 \q_@@_stop { #3 #1 } {#4} } |
| 1066 |
% \end{macrocode} |
| 1067 |
% |
| 1068 |
% \cs{@@_append_output:nnNwnn} takes three arguments (a parameter |
| 1069 |
% number, a parameter name, and a parameter token) and the two output |
| 1070 |
% slots as |#5| and |#6|. It appends the parameter token and number |
| 1071 |
% to the first output slot, and a definition |
| 1072 |
% |\cs_set:cpn {|\cs{c_@@_prefix_tl}~\meta{name}|} {|\meta{number}|}| |
| 1073 |
% to the second output slot. |
| 1074 |
% \begin{macrocode} |
| 1075 |
\cs_new:Npn \@@_append_output:nnNwnn #1 #2 #3 |
| 1076 |
#4 \q_@@_stop #5 #6 |
| 1077 |
{ |
| 1078 |
\@@_replace_loop:w #4 \q_@@_stop |
| 1079 |
{ #5 #3#1 } |
| 1080 |
{ #6 \cs_set:cpn { \c_@@_prefix_tl #2 } {#1} } |
| 1081 |
} |
| 1082 |
% \end{macrocode} |
| 1083 |
% |
| 1084 |
% This macro doesn't really abort the definition at the time it's |
| 1085 |
% called because it's called from within an |f|-expansion context, so |
| 1086 |
% an attempt to end that prematurely will hardly end well. Instead |
| 1087 |
% it hijacks the process by inserting |
| 1088 |
% \cs{@@_skip_to_scan_mark:w} in the second output slot, so that |
| 1089 |
% the definition end as soon as the scanning ends. |
| 1090 |
% \TODO{Can be simplified further.} |
| 1091 |
% \begin{macrocode} |
| 1092 |
\cs_new:Npn \@@_abort_definition:w |
| 1093 |
#1 \q_@@_stop #2 #3 |
| 1094 |
{ |
| 1095 |
\@@_replace_loop:w #1 \q_@@_stop |
| 1096 |
{#2} { #3 \group_end: \@@_skip_to_scan_mark:w } |
| 1097 |
} |
| 1098 |
% \end{macrocode} |
| 1099 |
% \end{macro} |
| 1100 |
% \end{macro} |
| 1101 |
% \end{macro} |
| 1102 |
% \end{macro} |
| 1103 |
% \end{macro} |
| 1104 |
% |
| 1105 |
% \begin{macro}[EXP]{\@@_replace_normal:N} |
| 1106 |
% \begin{macro}[EXP]{\@@_replace_group:n, \@@_flush_group:nnw} |
| 1107 |
% \begin{macro}[EXP]{\@@_replace_space:w} |
| 1108 |
% Spaces are just passed through: they aren't parameter tokens nor |
| 1109 |
% valid delimiters, so need no special treatment. |
| 1110 |
% |
| 1111 |
% Braced tokens are recursively scanned by \cs{@@_replace_parm:Nn}, |
| 1112 |
% and the output is flushed inside a pair of braces (explicit catcode |
| 1113 |
% $1$ and $2$ tokens are normalised to |{|_$1$ and |}|_$2$, |
| 1114 |
% respectively) |
| 1115 |
% \TODO{Since \cs{__tl_act:NNNnn} isn't used anymore, this doesn't |
| 1116 |
% have to be |f|-expandable, so this restriciton can probably be |
| 1117 |
% lifted.} |
| 1118 |
% |
| 1119 |
% The remaining tokens are examined for their meaning. If the token |
| 1120 |
% is the quark \cs{q_@@_mark}, the scanning stops; if the token is a |
| 1121 |
% parameter token, what follows is examined with \cs{@@_grab_parm:Nw} |
| 1122 |
% to check if a replacement should be done; otherwise it's fluhsed to |
| 1123 |
% the output. |
| 1124 |
% \begin{macrocode} |
| 1125 |
\cs_new:Npn \@@_replace_normal:N #1 |
| 1126 |
{ |
| 1127 |
\token_if_eq_meaning:NNTF \q_@@_mark #1 |
| 1128 |
{ \@@_replace_end:wnn } |
| 1129 |
{ |
| 1130 |
\token_if_parameter:NTF #1 |
| 1131 |
{ \@@_grab_parm:Nw } |
| 1132 |
{ \@@_flush:nw } |
| 1133 |
{#1} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
\cs_new:Npn \@@_replace_group:n #1 |
| 1137 |
{ \@@_replace_parm:Nn \@@_flush_group:nnw {#1} } |
| 1138 |
\cs_new:Npn \@@_flush_group:nnw #1 #2 |
| 1139 |
{ \@@_flush:nw { {#1} } } |
| 1140 |
\exp_last_unbraced:NNo |
| 1141 |
\cs_new:Npn \@@_replace_space:w \c_space_tl |
| 1142 |
{ \@@_flush:nw { ~ } } |
| 1143 |
% \end{macrocode} |
| 1144 |
% \end{macro} |
| 1145 |
% \end{macro} |
| 1146 |
% \end{macro} |
| 1147 |
% |
| 1148 |
% \subsection{Parsing a parameter} |
| 1149 |
% |
| 1150 |
% \begin{macro}[EXP]{\@@_grab_parm:Nw} |
| 1151 |
% \begin{macro}[EXP]{\@@_grab_parm_aux:NNw, \@@_grab_parm_noop:NNw} |
| 1152 |
% \begin{macro}[EXP]{\@@_grab_parm_scan:NNw} |
| 1153 |
% \begin{macro}[EXP]{\@@_grab_parm_loop:nw, \@@_grab_parm_end:nw} |
| 1154 |
% \begin{macro}[EXP]{ |
| 1155 |
% \@@_parm_get_normal:nN, |
| 1156 |
% \@@_parm_get_group:nn, |
| 1157 |
% \@@_parm_get_space:nw |
| 1158 |
% } |
| 1159 |
% These macros are the final pieces of the parameter replacement |
| 1160 |
% routine. \cs{@@_grab_parm:Nw} checks if the next token in the stream |
| 1161 |
% is a valid |N|-type. If it is, then \cs{@@_grab_parm_aux:NNw} checks |
| 1162 |
% if its character code is equal to \cs{@@_begin_name_token:}, and if |
| 1163 |
% it is, then call \cs{@@_grab_parm_scan:NNw} to scan ahead for the |
| 1164 |
% named parameter. In all other cases, the tokens grabbed are not |
| 1165 |
% named parameters, so they are flushed to the output. |
| 1166 |
% \begin{macrocode} |
| 1167 |
\cs_new:Npn \@@_grab_parm:Nw #1 #2 \q_@@_stop |
| 1168 |
{ |
| 1169 |
\tl_if_head_is_N_type:nTF {#2} |
| 1170 |
{ \@@_grab_parm_aux:NNw } |
| 1171 |
{ \@@_flush:nw } |
| 1172 |
#1 #2 \q_@@_stop |
| 1173 |
} |
| 1174 |
\cs_new:Npn \@@_grab_parm_aux:NNw #1 #2 |
| 1175 |
{ |
| 1176 |
\exp_args:No \token_if_eq_charcode:NNTF |
| 1177 |
{ \@@_begin_name_token: } #2 |
| 1178 |
{ \@@_grab_parm_scan:NNw } |
| 1179 |
{ \@@_grab_parm_noop:NNw } |
| 1180 |
#1 #2 |
| 1181 |
} |
| 1182 |
% \end{macrocode} |
| 1183 |
% |
| 1184 |
% Here we have to take care not to flush \cs{q_@@_mark} to the output. |
| 1185 |
% \begin{macrocode} |
| 1186 |
\cs_new:Npn \@@_grab_parm_noop:NNw #1 #2 |
| 1187 |
{ |
| 1188 |
\token_if_eq_meaning:NNTF \q_@@_mark #2 |
| 1189 |
{ \@@_flush:nw { #1 } #2 } |
| 1190 |
{ \@@_flush:nw { #1 #2 } } |
| 1191 |
} |
| 1192 |
% \end{macrocode} |
| 1193 |
% |
| 1194 |
% Here's the actual scanning routine. It would be a lot faster to |
| 1195 |
% just define a delimiter macro with the right tokens, however this |
| 1196 |
% would have two consequences: first, missing delimiters would be |
| 1197 |
% rather catastrophic, and second, the catcode of the |end| delimiter |
| 1198 |
% would need to match. With a manual scanning, we can kill off those |
| 1199 |
% two items at the cost of some performance. |
| 1200 |
% |
| 1201 |
% The scanning routine is pretty standard: a looping macro, an output |
| 1202 |
% slot, the tokens to be scanned, \cs{q_@@_stop} to delimit the whole |
| 1203 |
% thing (\cs{q_@@_mark} is redundant here: the one from the main |
| 1204 |
% scanning rountine is already in place), and the parameter token |
| 1205 |
% safely stored at the end: |
| 1206 |
% \begin{macrocode} |
| 1207 |
\cs_new:Npn \@@_grab_parm_scan:NNw #1 #2 #3 \q_@@_stop |
| 1208 |
{ \@@_grab_parm_loop:nw { } #3 \q_@@_stop {#1} } |
| 1209 |
\cs_new:Npn \@@_grab_parm_loop:nw #1 #2 \q_@@_stop |
| 1210 |
{ |
| 1211 |
\tl_if_head_is_N_type:nTF {#2} |
| 1212 |
{ \@@_parm_get_normal:nN } |
| 1213 |
{ |
| 1214 |
\tl_if_head_is_group:nTF {#2} |
| 1215 |
{ \@@_parm_get_group:nn } |
| 1216 |
{ \@@_parm_get_space:nw } |
| 1217 |
} |
| 1218 |
{#1} #2 \q_@@_stop |
| 1219 |
} |
| 1220 |
% \end{macrocode} |
| 1221 |
% |
| 1222 |
% If the end of the token list was reached (signalled by |
| 1223 |
% \cs{q_@@_mark}), the |end| delimiter is missing. If so, raise an |
| 1224 |
% error and recover as gracefully as possible. Otherwise, if the |
| 1225 |
% current token is the same character as the \cs{@@_end_name_token:}, |
| 1226 |
% then the scaning is complete. |
| 1227 |
% \begin{macrocode} |
| 1228 |
\cs_new:Npn \@@_parm_get_normal:nN #1 #2 |
| 1229 |
{ |
| 1230 |
\token_if_eq_meaning:NNTF \q_@@_mark #2 |
| 1231 |
{ |
| 1232 |
\msg_expandable_error:nn { namedef } { missing-end } |
| 1233 |
\@@_grab_parm_end:nw {#1} #2 |
| 1234 |
} |
| 1235 |
{ |
| 1236 |
\exp_args:No \token_if_eq_charcode:NNTF |
| 1237 |
{ \@@_end_name_token: } #2 |
| 1238 |
{ \@@_grab_parm_end:nw {#1} } |
| 1239 |
{ \@@_grab_parm_loop:nw {#1#2} } |
| 1240 |
} |
| 1241 |
} |
| 1242 |
\cs_new:Npn \@@_parm_get_group:nn #1 #2 |
| 1243 |
{ \@@_grab_parm_loop:nw { #1{#2} } } |
| 1244 |
\cs_new:Npn \@@_parm_get_space:nw #1 ~ |
| 1245 |
{ \@@_grab_parm_loop:nw { #1~ } } |
| 1246 |
\cs_new:Npn \@@_grab_parm_end:nw #1 #2 \q_@@_stop #3 |
| 1247 |
{ \@@_handle_parameter:nN {#1} #3 #2 \q_@@_stop } |
| 1248 |
% \end{macrocode} |
| 1249 |
% \end{macro} |
| 1250 |
% \end{macro} |
| 1251 |
% \end{macro} |
| 1252 |
% \end{macro} |
| 1253 |
% \end{macro} |
| 1254 |
% |
| 1255 |
% \subsection{Changing delimiters} |
| 1256 |
% |
| 1257 |
% \begin{macro}[EXP]{\@@_begin_name_token:, \@@_end_name_token:} |
| 1258 |
% These two hold the delimiters for named parameters. They are |
| 1259 |
% initialised here so that we can use \cs{named} (just to show off) |
| 1260 |
% ahead, and they are redefined every time \cs{NamedDelim} is used. |
| 1261 |
% \begin{macrocode} |
| 1262 |
\cs_new:Npn \@@_begin_name_token: { [ } |
| 1263 |
\cs_new:Npn \@@_end_name_token: { ] } |
| 1264 |
% \end{macrocode} |
| 1265 |
% \end{macro} |
| 1266 |
% |
| 1267 |
% \begin{macro}{\NamedDelim, \globalNamedDelim} |
| 1268 |
% \begin{macro}{\@@_named_delim_set:Nnn} |
| 1269 |
% \begin{macro}{\@@_check_delimiter:n} |
| 1270 |
% At this point everything for the \cs{named} macro is set up, |
| 1271 |
% so we can start using it. Now just some syntactic sugar to |
| 1272 |
% allow the modification of the named argument delimiters. |
| 1273 |
% |
| 1274 |
% Both \cs{NamedDelim} and \cs{globalNamedDelim} take two arguments, |
| 1275 |
% an initial and final delimiters for the named argument. Both |
| 1276 |
% delimters should be single non-control sequence tokens. Some of |
| 1277 |
% these restrictions could be lifted, but it's not really necessary |
| 1278 |
% because the choice of delimiter should not influence the working of |
| 1279 |
% the code, only the readability. A code with |\NamedDelim[]| and |
| 1280 |
% |\def\test#[1][#[2]]{[#[1]][#[2]]}| should work without problems; |
| 1281 |
% the only restriction is that \cs{@@_end_name_token:} (\emph{i.e.,} |
| 1282 |
% the second argument of \cs{NamedDelim}) cannot appear in the |
| 1283 |
% parameter name. |
| 1284 |
% \begin{macrocode} |
| 1285 |
\cs_new_protected:Npn \NamedDelim |
| 1286 |
{ \@@_named_delim_set:Nnn \cs_set:Npn } |
| 1287 |
\cs_new_protected:Npn \globalNamedDelim |
| 1288 |
{ \@@_named_delim_set:Nnn \cs_gset:Npn } |
| 1289 |
\named \cs_new_protected:Npn \@@_named_delim_set:Nnn |
| 1290 |
#[def] #[begin] #[end] |
| 1291 |
{ |
| 1292 |
\tl_trim_spaces_apply:nN {#[begin]} \@@_check_delimiter:n |
| 1293 |
\tl_trim_spaces_apply:nN {#[end]} \@@_check_delimiter:n |
| 1294 |
#[def] \@@_begin_name_token: {#[begin]} |
| 1295 |
#[def] \@@_end_name_token: {#[end]} |
| 1296 |
\s_@@ |
| 1297 |
} |
| 1298 |
% \end{macrocode} |
| 1299 |
% |
| 1300 |
% Here the \meta{token} is checked against a bunch of forbidden cases. |
| 1301 |
% \begin{macrocode} |
| 1302 |
\named \cs_new_protected:Npn \@@_check_delimiter:n #[token] |
| 1303 |
{ |
| 1304 |
% \end{macrocode} |
| 1305 |
% It can't be empty (nor a space: they were trimmed above): |
| 1306 |
% \begin{macrocode} |
| 1307 |
\tl_if_empty:nT {#[token]} |
| 1308 |
{ |
| 1309 |
\msg_error:nn { namedef } { blank-delim } |
| 1310 |
\@@_skip_to_scan_mark:w |
| 1311 |
} |
| 1312 |
% \end{macrocode} |
| 1313 |
% It can't be multiple tokens: |
| 1314 |
% \begin{macrocode} |
| 1315 |
\tl_if_single_token:nF {#[token]} |
| 1316 |
{ |
| 1317 |
\msg_error:nnn { namedef } { multi-token-delim } {#[token]} |
| 1318 |
\@@_skip_to_scan_mark:w |
| 1319 |
} |
| 1320 |
% \end{macrocode} |
| 1321 |
% It can't be an implicit begin- or end-group token: |
| 1322 |
% \begin{macrocode} |
| 1323 |
\bool_lazy_or:nnT |
| 1324 |
{ \token_if_group_begin_p:N #[token] } |
| 1325 |
{ \token_if_group_end_p:N #[token] } |
| 1326 |
{ |
| 1327 |
\msg_error:nnx { namedef } { group-delim } |
| 1328 |
{ \cs_to_str:N #[token] } |
| 1329 |
\@@_skip_to_scan_mark:w |
| 1330 |
} |
| 1331 |
% \end{macrocode} |
| 1332 |
% It can't be a parameter token: |
| 1333 |
% \TODO{Maybe it can, but maybe it will be ambiguous. Must check.} |
| 1334 |
% \begin{macrocode} |
| 1335 |
\token_if_parameter:NT #[token] |
| 1336 |
{ |
| 1337 |
\msg_error:nnx { namedef } { param-delim } |
| 1338 |
{ \cs_to_str:N #[token] } |
| 1339 |
\@@_skip_to_scan_mark:w |
| 1340 |
} |
| 1341 |
% \end{macrocode} |
| 1342 |
% It can't be a control sequence: |
| 1343 |
% \TODO{It probably can, but I'm not sure I want to allow this.} |
| 1344 |
% \begin{macrocode} |
| 1345 |
\token_if_cs:NT #[token] |
| 1346 |
{ |
| 1347 |
\msg_error:nnx { namedef } { macro-delim } |
| 1348 |
{ \c_backslash_str \cs_to_str:N #[token] } |
| 1349 |
\@@_skip_to_scan_mark:w |
| 1350 |
} |
| 1351 |
} |
| 1352 |
% \end{macrocode} |
| 1353 |
% \end{macro} |
| 1354 |
% \end{macro} |
| 1355 |
% \end{macro} |
| 1356 |
% |
| 1357 |
% \subsection{Messages} |
| 1358 |
% |
| 1359 |
% Now we define the messages used throughout the package. |
| 1360 |
% |
| 1361 |
% \begin{macrocode} |
| 1362 |
\msg_new:nnn { namedef } { repeated-parm } |
| 1363 |
{ |
| 1364 |
Parameter~\iow_char:N\#[#1]~duplicated~in~ |
| 1365 |
definition~of~\l_@@_macro_tl. |
| 1366 |
} |
| 1367 |
\msg_new:nnn { namedef } { unknown-parm } |
| 1368 |
{ |
| 1369 |
Unknown~parameter~\iow_char:N\#[#1]~in~ |
| 1370 |
definition~of~\l_@@_macro_tl. |
| 1371 |
} |
| 1372 |
\msg_new:nnn { namedef } { multi-token-delim } |
| 1373 |
{ |
| 1374 |
Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~ |
| 1375 |
Delimiters~for~named~parameters~must~be~single~tokens. |
| 1376 |
} |
| 1377 |
\msg_new:nnn { namedef } { macro-delim } |
| 1378 |
{ |
| 1379 |
Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~ |
| 1380 |
Delimiters~for~named~parameters~can't~be~control~sequence~nor~ |
| 1381 |
active~characters. |
| 1382 |
} |
| 1383 |
\msg_new:nnn { namedef } { group-delim } |
| 1384 |
{ |
| 1385 |
Invalid~\iow_char:N\\named~parameter~delimiter~`\iow_char:N\\#1'.~ |
| 1386 |
Delimiters~for~named~parameters~can't~be~ |
| 1387 |
begin-/end-group~character~tokens. |
| 1388 |
} |
| 1389 |
\msg_new:nnn { namedef } { blank-delim } |
| 1390 |
{ |
| 1391 |
Invalid~\iow_char:N\\named~parameter~delimiter.~ |
| 1392 |
Delimiters~for~named~parameters~can't~be~empty~nor~space~tokens. |
| 1393 |
} |
| 1394 |
\msg_new:nnn { namedef } { param-delim } |
| 1395 |
{ |
| 1396 |
Invalid~\iow_char:N\\named~parameter~delimiter.~ |
| 1397 |
Delimiters~for~named~parameters~can't~be~parameter~tokens. |
| 1398 |
} |
| 1399 |
\msg_new:nnn { namedef } { missing-end } |
| 1400 |
{ |
| 1401 |
Missing~\@@_end_name_token:\iow_char:N\ inserted~in~ |
| 1402 |
definition~of~\l_@@_macro_tl. |
| 1403 |
} |
| 1404 |
% \end{macrocode} |
| 1405 |
% |
| 1406 |
% Now execute the end package hook (in \LaTeX{} it is |
| 1407 |
% \cs{prg_do_nothing:}, but in plain \TeX{} it does \cs{ExplSyntaxOff}). |
| 1408 |
% |
| 1409 |
% \begin{macrocode} |
| 1410 |
\@@_end_package_hook: |
| 1411 |
% \end{macrocode} |
| 1412 |
% |
| 1413 |
% \iffalse |
| 1414 |
%</package> |
| 1415 |
% \fi |
| 1416 |
% |
| 1417 |
% \end{implementation} |
| 1418 |
% |
| 1419 |
%^^A \par\CodedocExplainEXP |
| 1420 |
%^^A \par\CodedocExplainREXP |
| 1421 |
%^^A \par\CodedocExplainTF |
| 1422 |
% |
| 1423 |
% \PrintIndex |
| 1424 |
% |
| 1425 |
\endinput |