1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
|
% \iffalse meta-comment
%
%% File: xpatch.dtx (C) Copyright 2012-2020 Enrico Gregorio
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version. The latest version
%% of this license is in the file
%%
%% http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "xpatch bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%
%<*driver|package>
\RequirePackage{expl3}
%</driver|package>
%<*driver>
\expandafter\def\csname ver@thumbpdf.sty\endcsname{}
\documentclass[a4paper,full]{l3doc}
\usepackage{bookmark}
%</driver>
%<*driver|package>
\GetIdInfo$Id: xpatch.dtx 0.3a 2020-03-25 12:00:00Z Enrico $
{Extending etoolbox patching commands}
%</driver|package>
%<*driver>
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \changes{v0.2}{2012/01/13}{Added \texttt{\char`\\x...bibdriver} macros; fixed a bug for
% control symbols defined with \texttt{\char`\\newcommand} and an optional argument.}
% \changes{v0.3}{2012/10/01}{Added a bunch of biblatex related commands}
% \changes{v0.3a}{2020/03/25}{Changed deprecated function}
%
% \title{^^A
% The \textsf{xpatch} package\\ Extending \textsf{etoolbox} patching
% commands^^A
% \thanks{This file describes version \ExplFileVersion,
% last revised \ExplFileDate.}^^A
% }
%
% \author{^^A
% Enrico Gregorio\thanks
% {^^A
% E-mail:
% Enrico DOT Gregorio AT univr DOT it^^A
% }^^A
% }
%
% \date{Released \ExplFileDate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Introduction}
%
% The well known \pkg{etoolbox} package provides a bunch of functions
% for patching existing commands; in particular |\patchcmd|,
% |\pretocmd| and |\apptocmd| that do a wonderful job, but suffer from
% a limitation: if some package has defined
%\begin{verbatim}
% \newcommand{\xyz}[1][x]{-#1!}
%\end{verbatim}
% where |\xyz| has an optional argument, then |\patchcmd| and siblings
% cannot be used to modify the workings of |\xyz|. The same happens
% when a command has been defined with |\DeclareRobustCommand|.
%
% The reason for this is \TeX{}nical or, better, \LaTeX{}nical. When
% \LaTeX{} performs the above definition, the expansion of |\xyz| will
% be
%\begin{verbatim}
% \@protected@testopt \xyz \\xyz {x}
%\end{verbatim}
% where |\@protected@testopt| is a macro that essentially checks
% whether we are in a ``protected'' context, so that expansion should
% not be performed all the way (in moving arguments or write
% operations), or not; in the former case it issues a protected
% version of |\xyz|, while in the latter case it expands the macro
% |\\xyz| that is a \emph{single} command (yes, with a backslash in
% its name) which contains the real definition; a way to access this
% definition is to issue the command
%\begin{verbatim}
% \expandafter\show\csname\string\xyz\endcsname
%\end{verbatim}
% which will print in the log file the message
%\begin{verbatim}
% > \\xyz=\long macro:
% [#1]->-#1!.
%\end{verbatim}
% As usual, after |->| we see the definition. In order to use
% |\patchcmd| to change the exclamation mark into a hyphen one must do
%\begin{verbatim}
% \expandafter\patchcmd\csname\string\xyz\endcsname{!}{-}{}{}
%\end{verbatim}
% (see the documentation of \pkg{etoolbox} for details about the
% arguments).
%
% A similar situation happens if |\xyz| has been defined by
%\begin{verbatim}
% \DeclareRobustCommand{\xyz}{something}
%\end{verbatim}
% A |\show\xyz| would show the cryptic
%\begin{verbatim}
% > \xyz=macro:
% ->\protect \xyz .
%\end{verbatim}
% and only a close look reveals the clever trick used by the \LaTeX{}
% team: the |\protect| is not applied to |\xyz|, but to the macro
% \verb*?\xyz ? which has a space at the end of its name! And this
% macro is the one that contains the real definition. Indeed,
%\begin{verbatim}
% \expandafter\show\csname xyz\space\endcsname
%\end{verbatim}
% produces the message
%\begin{verbatim}
% > \xyz =\long macro:
% ->something.
%\end{verbatim}
% In this case, in order to apply |\patchcmd| we must say
%\begin{verbatim}
% \expandafter\patchcmd\csname xyz\space\endcsname{s}{S}{}{}
%\end{verbatim}
% If the macro with |\DeclareRobustCommand| is defined to have an
% optional argument, say
%\begin{verbatim}
% \DeclareRobustCommand{\xyz}[1][x]{-#1!}
%\end{verbatim}
% one has to combine the two tricks:
%\begin{verbatim}
% \expandafter\patchcmd\csname\string\xyz\space\endcsname{!}{-}{}{}
%\end{verbatim}
% It's hard and error prone to remember all these tricks, so this
% package comes to the rescue.
%
% \subsection*{Caveat}
% This package is still in a preliminary version, but no relevant
% changes to the interface should be introduced in later versions. A
% different and more powerful implementation is under testing, see the
% package \pkg{regexpatch}.
%
%
% \section{Commands}
%
% The commands introduced by this package are
% \begin{itemize}
% \item |\xpatchcmd|
% \item |\xpretocmd|
% \item |\xapptocmd|
% \end{itemize}
% which have the same syntax as the similar commands provided by
% \pkg{etoolbox} and apply to all kind of commands defined by
% \begin{itemize}
% \item the \LaTeX{} kernel macros |\newcommand|, |\renewcommand|,
% |\providecommand|, but also |\newenvironment| and
% |\renewenvironment|;
% \item the \LaTeX{} kernel macro for defining robust commands
% |\DeclareRobustCommand|;
% \item the \pkg{etoolbox} macros |\newrobustcmd|, |\renewrobustcmd|,
% |\providerobustcmd|.
% \end{itemize}
% Notice that patching the definition of the environment |foo|
% requires patching |\foo| or |\endfoo|.
%
% These commands will act as the original ones if the macro to patch
% is not robust or with optional arguments.
%
% Moreover the package defines
% \begin{itemize}
% \item |\xpatchbibmacro|
% \item |\xpretobibmacro|
% \item |\xapptobibmacro|
% \end{itemize}
% that can be used to patch commands defined with \pkg{biblatex}'s
% |\newbibmacro|. Say that we have
%\begin{verbatim}
% \newbibmacro{foo.bar}[2]{#1 and #2}
%\end{verbatim}
% Then, to change |and| into |und|, we can now say
%\begin{verbatim}
% \xpatchbibmacro{foo.bar}{and}{und}{}{}
%\end{verbatim}
% \medskip
% \begin{lrbox}{0}
% \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
% \footnotesize
% Patching these macros requires resorting to the \emph{very} cryptic
%\begin{verbatim}
% \expandafter\patchcmd\csname abx@macro@\detokenize{foo.bar}\endcsname
% {and}{und}{}{}
%\end{verbatim}
% that would become an astonishing
%\begin{verbatim}
% \expandafter\patchcmd\csname\expandafter\string\csname
% abx@macro@\detokenize{foo.bar}\endcsname\endcsname
% {and}{und}{}{}
%\end{verbatim}
% if the original definition had been with an optional argument, say
%\begin{verbatim}
% \newbibmacro{foo.bar}[2][x]{#1 and #2}
%\end{verbatim}
% \end{minipage}
% \end{lrbox}
% \noindent\fbox{\usebox{0}}
%
% \medskip
%
% For \pkg{biblatex} users there are also
% \begin{itemize}
% \item |\xpatchbibdriver|
% \item |\xpretobibdriver|
% \item |\xapptobibdriver|
% \end{itemize}
% for patching commands defined with |\DeclareBibliographyDriver|. One
% could use, for patching the driver |foo|,
%\begin{verbatim}
% \makeatletter
% \patchcmd{\blx@bbx@foo}{X}{Y}{<success>}{<failure>}
% \preto{\blx@bbx@foo}{P}
% \appto{\blx@bbx@foo}{A}
% \makeatother
%\end{verbatim}
% but having a lighter interface can be handy. Since our macros use
% |\pretocmd| and |\apptocmd| for consistency, remember to always use
% the \marg{success} and \marg{failure} arguments also with
% |\xpretobibdriver| and |\xapptobibdriver|.
%
% Under the same philosophy, one can use the macros
% \begin{itemize}
% \item |\xpatchfieldformat|,\\ |\xpretofieldformat|,\\ |\xapptofieldformat|,
% \item |\xpatchnameformat|,\\ |\xpretonameformat|,\\ |\xapptonameformat|,
% \item |\xpatchlistformat|,\\ |\xpretonameformat|,\\ |\xapptonameformat|,
% \item |\xpatchindexfieldformat|,\\ |\xpretoindexfieldformat|,\\ |\xapptoindexfieldformat|,
% \item |\xpatchindexnameformat|,\\ |\xpretoindexnameformat|,\\ |\xapptoindexnameformat|,
% \item |\xpatchindexlistformat|,\\ |\xpretoindexlistformat|,\\ |\xapptoindexlistformat|,
% \end{itemize}
% for the \pkg{biblatex} internal macro defined respectively with
% \begin{flushleft}
% |\DeclareFieldFormat|,
% |\DeclareNameFormat|,
% |\DeclareListFormat|,\\
% |\DeclareIndexFieldFormat|,
% |\DeclareIndexNameFormat|,
% |\DeclareIndexListFormat|.
% \end{flushleft}
% All the eighteen |\x...format| commands take a first optional argument, with
% default value |*|, see later on.
%
%
% Finally, the package defines the commands
% \begin{itemize}
% \item |\xshowcmd|
% \item |\xshowbibmacro|
% \item |\xshowbibdriver|
% \item |\xshowfieldformat|
% \item |\xshownameformat|
% \item |\xshowlistformat|
% \item |\xshowindexfieldformat|
% \item |\xshowindexnameformat|
% \item |\xshowindexlistformat|
% \end{itemize}
% that are the analog of |\show| to see the ``real'' definition of a
% macro, be it defined with optional arguments or as a robust command;
% the |\xshowbib...| and |\xshow...format| ones are for the
% corresponding \pkg{biblatex} macros. The last six have an optional
% first argument (default value |*|).
%
%
% \section{Using the original commands}
%
% The original |\patchcmd| has still its use: suppose you want to
% modify the default for the optional argument passed to a macro: if
% the original definition is
%\begin{verbatim}
% \newcommand{\xyz}[1][x]{-#1!}
%\end{verbatim}
% then one can say
%\begin{verbatim}
% \patchcmd{\xyz}{{x}}{{y}}{}{}
%\end{verbatim}
% because of the way |\xyz| is defined, as shown before.
%
%
% \section{Syntax}
%
% |\xpatchcmd|\marg{command}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretocmd|\marg{command}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptocmd|\marg{command}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchbibmacro|\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretobibmacro|\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptobibmacro|\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchbibdriver|\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretobibdriver|\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptobibdriver|\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchfieldformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretofieldformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptofieldformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchnameformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretonameformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptonameformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchlistformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretolistformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptolistformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchindexfieldformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretoindexfieldformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptoindexfieldformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchindexnameformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretoindexnameformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptoindexnameformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xpatchindexlistformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
% \marg{success}\marg{failure}\\
% |\xpretoindexlistformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
% \marg{success}\marg{failure}\\
% |\xapptoindexlistformat|\oarg{entrytype}\marg{name}\marg{append}^^A
% \marg{success}\marg{failure}\\[\medskipamount]
% |\xshowcmd|\marg{command}\\
% |\xshowbibname|\marg{name}\\
% |\xshowbibdriver|\marg{name}\\
% |\xshowfieldformat|\oarg{entrytype}\marg{name}\\
% |\xshownameformat|\oarg{entrytype}\marg{name}\\
% |\xshowlistformat|\oarg{entrytype}\marg{name}\\
% |\xshowindexfieldformat|\oarg{entrytype}\marg{name}\\
% |\xshowindexnameformat|\oarg{entrytype}\marg{name}\\
% |\xshowindexlistformat|\oarg{entrytype}\marg{name}
%
% \medskip
%
% \begingroup\emergencystretch=.5\textwidth
% Here \meta{command} is the command's name (with the backslash),
% while \meta{name} is the string that appears as the argument to
% |\newbibmacro|, |\DeclareBibliographyDriver|, |\DeclareFieldFormat|,
% |\DeclareNameFormat|, |\DeclareListFormat|, |\DeclareIndexFieldFormat|,
% |\DeclareIndexNameFormat| or |\DeclareIndexListFormat| respectively;
% \meta{search}, \meta{replace}, \meta{prepend} and \meta{append} are
% the list of tokens that are to be used for the specific tasks;
% \meta{success} and \meta{failure} are token lists to be executed if
% the patching succeeds or fails respectively. I find it useful to use
% |\ddt| as \meta{failure}, so that \TeX{} will stop for the undefined
% control sequence when the patching fails.\par\endgroup
%
% All the |\x...format| macros have an optional argument that by
% default is |*|.
%
% It's important to remember that patching commands that have
% |@|-commands in their name or replacement text must always be
% performed between |\makeatletter| and |\makeatother|.
%
% \section{Limitations and warnings}
%
% Macros defined in devious ways might trick |\xpatchcmd| and
% siblings, although many precautions have been taken in order this
% not to happen. Always check with care.
%
% Remember that one must \emph{never} use the old trick
%\begin{verbatim}
% \let\ORIxyx\xyz
% \renewcommand{\xyz}[1][x]{+\ORIxyz[#1]?}
%\end{verbatim}
% if |\xyz| had been defined with an optional argument. For such
% things it's better to use |\xpatchcmd| and friends or employ the
% \pkg{letltxmacro} package by H.~Oberdiek, that provides
% |\LetLtxMacro| for purposes like this one.
%
% Although this package has been written with the experimental \LaTeX3
% macros, the commands \emph{can't} be used to patch commands defined
% with the \pkg{xparse} interface, in general.
%
% If a command appears to have one optional argument at the user
% level, this doesn't mean it has been defined with |\newcommand|
% directly. One should always check the definitions with |\show| and
% |\xshowcmd| before trying a patch: of course one has to know what a
% command does, in order to patch it. And, when first testing the
% patch, it's best to set |\tracingpatches|.
%
% \section{History}
%
% \begin{itemize}
% \item[Version 0.1] First public release.
% \item[Version 0.2] Added |\x...bibdriver| macros; fixed a bug for
% control symbols defined with |\newcommand| and an optional argument.
% \item[Version 0.3] Added |\x...format| macros (by kind request of the
% \pkg{biblatex} maintainers).
% \end{itemize}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{The implementation of \pkg{xpatch}}
%
% \iffalse
%<*package>
% \fi
%
%
% \begin{macrocode}
\ProvidesExplPackage
{\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
% \end{macrocode}
%
% A check to make sure that \pkg{expl3} is not too old
% \begin{macrocode}
\@ifpackagelater { expl3 } { 2011/10/09 }
{ }
{
\PackageError { xpatch } { Support~package~l3kernel~too~old. }
{
Please~install~an~up~to~date~version~of~l3kernel~
using~your~TeX~package~manager~or~from~CTAN.\\ \\
Loading~xpatch~will~abort!
}
\tex_endinput:D
}
% \end{macrocode}
%
% The \pkg{xparse} and \pkg{etoolbox} packages are required.
% \begin{macrocode}
\RequirePackage{xparse,etoolbox}
% \end{macrocode}
%
% \subsection{Utilities, variables and constants}
%
% Generate a variant of |\tl_if_in:NnT| to get the expanded second
% argument.
% \begin{macrocode}
\cs_generate_variant:Nn \tl_if_in:NnT { Nx }
% \end{macrocode}
% A boolean for the testing of robust commands.
% \begin{macrocode}
\bool_new:N \l__xpatch_protect_bool
% \end{macrocode}
% The constant |\c_backslash_str| is defined in |l3str| that's not
% loaded at the moment, so we save a bit of memory not loading it.
% \begin{macrocode}
\cs_if_exist:NF \c_backslash_str
{ \tl_const:Nx \c_backslash_str { \cs_to_str:N \\ } }
% \end{macrocode}
% A ``bizarre'' token list that's quite improbable to find in the
% replacement text of a macro.
% \begin{macrocode}
\tl_const:Nx \c__xpatch_bizarre_tl
{ \tl_to_str:n { **)-(**/**]-[** } }
% \end{macrocode}
% Internal token lists for storing the various parts of the command to be
% patched.
% \begin{macrocode}
\tl_new:N \l__xpatch_name_tl
\tl_new:N \l__xpatch_repl_tl
% \end{macrocode}
%
% \subsection{The main functions}
%
% The main function takes as first argument one of |\patchcmd|,
% |\pretocmd| or |\apptocmd|; the second argument is the command we
% want to patch.
%
% Some technical remarks. Suppose we have the following definitions:
%\begin{verbatim}
% \DeclareRobustCommand{\xaa}[1]{xaa (DeclareRobustCommand-noopt)}
% \DeclareRobustCommand{\xab}[1][x]{xab (DeclareRobustCommand-opt)}
% \newcommand{\xac}[1][]{xac (newcommand-opt)}
% \newrobustcmd\xad[1][]{xad (newrobustcmd-opt)}
% \DeclareRobustCommand{\1}[1]{1 (DeclareRobustCommand-noopt)}
% \DeclareRobustCommand{\2}[1][]{2 (DeclareRobustCommand-opt)}
% \newcommand{\3}[1][]{3 (newcommand-opt)}
% \newrobustcmd\4[1][]{4 (newrobustcmd-opt)}
%\end{verbatim}
% Then the first level expansions are, respectively,
% \iffalse
%<*mock>
% \fi
\begin{verbatim*}
+\protect \xaa +
+\protect \xab +
+\@protected@testopt \xac \\xac {}+
+\@testopt \\xad {}+
+\x@protect \1\protect \1 +
+\x@protect \2\protect \2 +
+\@protected@testopt \3\\3 {}+
+\@testopt \\4 {}+
\end{verbatim*}
% \iffalse
%</mock>
% \fi
% where the |+| is used to delimit the expansions and show the
% spaces. Remember that |\show| always adds a space after a control
% word, but not after a control symbol such as |\1|. However, in lines
% 5~and~6, \verb*?\1 ? is not a control symbol any more. So we have to
% take care of |\protect|, |\x@protect|, |\@protected@testopt| and
% |\@testopt|. But it's not simply sufficient to check for the
% presence of such a token at the start of the replacement text, or
% we'll be confused by macros such as |\linebreak|, whose replacement
% text starts with |\@testopt|. So we'll check also for the presence of
% the subsequent tokens, that depend on the macro's name. We add a
% perhaps useless ``random'' string at the beginning, as we'd like to
% ensure that the matches are exactly at the start of the replacement
% text.
% \begin{macrocode}
\cs_new:Npn \xpatch_main:NN #1 #2
{
% \end{macrocode}
% We initialize the boolean to false.
% \begin{macrocode}
\bool_set_false:N \l__xpatch_protect_bool
% \end{macrocode}
% First of all we store the command-to-patch name.
% \begin{macrocode}
\tl_set:Nx \l__xpatch_name_tl { \cs_to_str:N #2 }
% \end{macrocode}
% We store the replacement text of the command-to-patch, but adding
% the bizarre token list in front of it which consists of all category
% 12 characters, just to be sure that the matches are at the
% beginning.\footnote{This part will be reimplemented as soon as
% \pkg{l3regex} stabilizes.}
% \begin{macrocode}
\tl_set:Nx \l__xpatch_repl_tl
{ \c__xpatch_bizarre_tl \cs_replacement_spec:N #2 }
% \end{macrocode}
% We look whether the token list contains the bizarre list followed by
% |\protect| and the same name (with two spaces) which happens if |#2|
% is a control sequence defined by |\DeclareRobustCommand|, so we add
% a space to the command name.
% \begin{macrocode}
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \protect \c_space_tl
\c_backslash_str \l__xpatch_name_tl \c_space_tl \c_space_tl
}
{
\bool_set_true:N \l__xpatch_protect_bool
\tl_put_right:Nn \l__xpatch_name_tl { \c_space_tl }
}
% \end{macrocode}
% We look whether the token list contains the bizarre list followed by
% |\x@protect| which happens if |#2| is a control symbol defined by
% |\DeclareRobustCommand|, so we add a space to the command name.
% \begin{macrocode}
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \x@protect \c_space_tl
\c_backslash_str \l__xpatch_name_tl \c_backslash_str
}
{
\bool_set_true:N \l__xpatch_protect_bool
\tl_put_right:Nn \l__xpatch_name_tl { \c_space_tl }
}
% \end{macrocode}
% In both the preceding cases we have to do another check, so we set a
% boolean to true.
%
% We look whether the token list contains the bizarre list followed by
% |\@protected@testopt| which happens if |#2| is a control word with an
% optional argument (from |\newcommand|).
% \begin{macrocode}
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \@protected@testopt \c_space_tl
\c_backslash_str \l__xpatch_name_tl
\c_space_tl \c_backslash_str \c_backslash_str
}
{
\tl_put_left:Nn \l__xpatch_name_tl { \c_backslash_str }
}
% \end{macrocode}
% We look whether the token list contains the bizarre list followed by
% |\@protected@testopt| which happens if |#2| is a control symbol with an
% optional argument (from |\newcommand|).
% \begin{macrocode}
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \@protected@testopt \c_space_tl
\c_backslash_str \l__xpatch_name_tl
\c_backslash_str \c_backslash_str
}
{
\tl_put_left:Nn \l__xpatch_name_tl { \c_backslash_str }
}
% \end{macrocode}
% We look whether the token list contains the bizarre list followed by
% |\@testopt| which happens if |#2| is a command with an optional
% argument (from |\newrobustcmd|).
% \begin{macrocode}
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \@testopt \c_space_tl
\c_backslash_str \c_backslash_str \l__xpatch_name_tl
}
{
\tl_put_left:Nn \l__xpatch_name_tl { \c_backslash_str }
}
% \end{macrocode}
% In both the preceding cases, we add a backslash in front of the
% command's name.
%
% If the command-to-patch was defined by |\DeclareRobustCommand| we
% have to do another test, namely checking whether it has an optional
% argument and, in this case, adding a backslash in front of the name.
% We replicate the test for |\@protected@testopt|.
% \begin{macrocode}
\bool_if:NT \l__xpatch_protect_bool
{
\tl_set:Nx \l__xpatch_repl_tl
{ \c__xpatch_bizarre_tl
\exp_after:wN \cs_replacement_spec:N
\cs:w \l__xpatch_name_tl \cs_end: }
\tl_if_in:NxT \l__xpatch_repl_tl
{
\c__xpatch_bizarre_tl
\token_to_str:N \@protected@testopt \c_space_tl
\c_backslash_str \l__xpatch_name_tl
\c_space_tl \c_backslash_str \c_backslash_str
}
{
\tl_put_left:Nn \l__xpatch_name_tl { \c_backslash_str }
}
}
% \end{macrocode}
% Finally, we pass the real command-to-patch name to the patching
% macro.
% \begin{macrocode}
\exp_after:wN #1 \cs:w \l__xpatch_name_tl \cs_end:
% \end{macrocode}
% That's the last operation!
% \begin{macrocode}
}
% \end{macrocode}
%
% \subsection{User level commands}
%
% The user level commands.
% \begin{macrocode}
\NewDocumentCommand{\xpatchcmd}{}{ \xpatch_main:NN \patchcmd }
\NewDocumentCommand{\xpretocmd}{}{ \xpatch_main:NN \pretocmd }
\NewDocumentCommand{\xapptocmd}{}{ \xpatch_main:NN \apptocmd }
\NewDocumentCommand{\xshowcmd} {}{ \xpatch_main:NN \show }
% \end{macrocode}
%
% We generate a variant of |\xpatch_main:NN| to accept a macro's name
% as its second argument.
% \begin{macrocode}
\cs_generate_variant:Nn \xpatch_main:NN { Nc }
% \end{macrocode}
%
% Now we can define the patching macros for |\newbibmacro| defined
% commands. In case one uses a wrong name, it will remain in the hash
% space, but it shouldn't be a problem: |\tracingpatches| must be used
% when testing, and it will warn about an undefined macro or one
% equivalent to |\relax|.
% \begin{macrocode}
\NewDocumentCommand{\xpatchbibmacro} { m }
{ \xpatch_main:Nc \patchcmd { abx@macro@ \tl_to_str:n {#1} } }
\NewDocumentCommand{\xpretobibmacro} { m }
{ \xpatch_main:Nc \pretocmd { abx@macro@ \tl_to_str:n {#1} } }
\NewDocumentCommand{\xapptobibmacro} { m }
{ \xpatch_main:Nc \apptocmd { abx@macro@ \tl_to_str:n {#1} } }
\NewDocumentCommand{\xshowbibmacro} { m }
{ \xpatch_main:Nc \show { abx@macro@ \tl_to_str:n {#1} } }
% \end{macrocode}
%
% The macros for patching commands defined with |\DeclareFieldFormat|;
% all that holds for the preceding commands is valid also for the
% following groups of similar commands.
% \begin{macrocode}
\NewDocumentCommand{\xpatchfieldformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@ffd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretofieldformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@ffd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xapptofieldformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@ffd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshowfieldformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@ffd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
% |\DeclareNameFormat|:
% \begin{macrocode}
\NewDocumentCommand{\xpatchnameformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@nfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretonameformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@nfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xapptonameformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@nfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshownameformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@ffd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
% |\DeclareListFormat|:
% \begin{macrocode}
\NewDocumentCommand{\xpatchlistformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@lfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretolistformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@lfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xapptolistformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@lfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshowlistformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@lfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
% |\DeclareIndexFieldFormat|;
% \begin{macrocode}
\NewDocumentCommand{\xpatchindexfieldformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@fid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretoindexfieldformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@fid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xapptoindexfieldformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@fid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshowindexfieldformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@fid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
% |\DeclareIndexNameFormat|:
% \begin{macrocode}
\NewDocumentCommand{\xpatchindexnameformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@nid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretoindexnameformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@nid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xapptoindexnameformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@nid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshowindexnameformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@nid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
% |\DeclareIndexListFormat|:
% \begin{macrocode}
\NewDocumentCommand{\xpatchindexlistformat} { O{*} m }
{ \xpatch_main:Nc \patchcmd { abx@lid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xpretoindexlistformat} { O{*} m }
{ \xpatch_main:Nc \pretocmd { abx@lid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xappindextolistformat} { O{*} m }
{ \xpatch_main:Nc \apptocmd { abx@lid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
\NewDocumentCommand{\xshowindexlistformat} { O{*} m }
{ \xpatch_main:Nc \show { abx@lid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} } }
% \end{macrocode}
%
% Finally, the patching macros for \pkg{biblatex} drivers that don't
% need the overhead of |\xpatch_main:NN|.
% \begin{macrocode}
\NewDocumentCommand{\xpatchbibdriver} { m }
{ \exp_args:Nc \patchcmd {blx@bbx@#1} }
\NewDocumentCommand{\xpretobibdriver} { m }
{ \exp_args:Nc \pretocmd {blx@bbx@#1} }
\NewDocumentCommand{\xapptobibdriver} { m }
{ \exp_args:Nc \apptocmd {blx@bbx@#1} }
\NewDocumentCommand{\xshowbibdriver} { m }
{ \exp_args:Nc \show {blx@bbx@#1} }
% \end{macrocode}
% \iffalse
%</package>
% \fi
% \end{implementation}
%
% \PrintIndex
% \PrintChanges
|