Debian Codegraph @

texmf-dist/source/latex/regexpatch/regexpatch.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: regexpatch.dtx (C) Copyright 2012-2020 Enrico Gregorio
4 %%
5 %% It may be distributed and/or modified under the conditions of the
6 %% LaTeX Project Public License (LPPL), either version 1.3c of this
7 %% license or (at your option) any later version. The latest version
8 %% of this license is in the file
9 %%
10 %% http://www.latex-project.org/lppl.txt
11 %%
12 %% This file is part of the "regexpatch bundle" (The Work in LPPL)
13 %% and all files in that bundle must be distributed together.
14 %%
15 %% The released version of this bundle is available from CTAN.
16 %%
17 %
18 %<*driver|package>
19 \RequirePackage{expl3}
20 %</driver|package>
21 %<*driver>
22 \expandafter\def\csname [email protected]\endcsname{}
23 \documentclass[a4paper,full]{l3doc}
24 \usepackage{bookmark}
25 \newcommand{\sv}{[\texttt{*}]}
26 %</driver>
27 %<*driver|package>
28 \GetIdInfo$Id: regexpatch.dtx 0.2f 2021-03-21 12:00:00Z Enrico $
29 {Extending etoolbox patching commands}
30 %</driver|package>
31 %<*driver>
32 \begin{document}
33 \DocInput{\jobname.dtx}
34 \end{document}
35 %</driver>
36 % \fi
37 %
38 % \title{^^A
39 % The \textsf{regexpatch} package^^A
40 % \thanks{This file describes version \ExplFileVersion,
41 % last revised \ExplFileDate.}^^A
42 % \\ {\normalsize Replacing \textsf{etoolbox} patching
43 % commands}^^A
44 % }
45 %
46 % \author{^^A
47 % Enrico Gregorio\thanks
48 % {^^A
49 % E-mail:
50 % Enrico DOT Gregorio AT univr DOT it^^A
51 % }^^A
52 % }
53 %
54 % \date{Released \ExplFileDate}
55 %
56 % \maketitle
57 %
58 % \changes{v0.2f}{2021/03/21}{Replaced deprecated commands}
59 % \changes{v0.2e}{2020/10/06}{Replaced deprecated commands}
60 % \changes{v0.2d}{2018/05/02}{Fixed silly typo}
61 % \changes{v0.2c}{2018/03/31}{Removed loading of \texttt{l3regex}}
62 % \changes{v0.2b}{2016/05/15}{Fix for removed function}
63 % \changes{v0.2a}{2015/05/20}{Fixed silly mistake}
64 % \changes{v0.2}{2013/03/28}{Polished code and documentation}
65 % \changes{v0.2}{2013/03/28}{Additional biblatex related macros}
66 % \changes{v0.1c}{2013/03/25}{Replaced obsolete command
67 % \texttt{\protect\string\protect\prg_case_str:onn}}
68 % \changes{v0.1c}{2013/03/25}{Replaced \texttt{\protect\string\protect\msg_term:x}
69 % with \texttt{\protect\string\protect\msg_term:n}}
70 % \changes{v0.1b}{2012/07/24}{Fixed bugs in the `bibmacro' functions}
71 % \changes{v0.1a}{2012/06/07}{Fixed bugs in success/failure code}
72 % \changes{v0.1}{2012-04-15}{First public release}
73 % \begin{documentation}
74 %
75 % \begin{quotation}\small
76 % \begin{center}\bfseries\large
77 % Important preliminary notice
78 % \end{center}
79 % \medskip
80 %
81 % This is an experimental version and it might cease to work if the
82 % commands in the package \pkg{l3regex} are modified. When that
83 % \LaTeX3 package will be declared stable, this package will replace
84 % \pkg{xpatch} and calling |\usepackage{regexpatch}| will load the
85 % main package. Use at own risk.
86 % \end{quotation}
87 %
88 % \section{Introduction}
89 %
90 % The well known \pkg{etoolbox} package provides a bunch of functions
91 % for patching existing commands; in particular |\patchcmd|,
92 % |\pretocmd| and |\apptocmd| that do a wonderful job, but suffer from
93 % a limitation: if some package has defined
94 %\begin{verbatim}
95 % \newcommand{\xyz}[1][x]{-#1!}
96 %\end{verbatim}
97 % where |\xyz| has an optional argument, then |\patchcmd| and siblings
98 % cannot be used to modify the workings of |\xyz|. The same happens
99 % when a command has been defined with |\DeclareRobustCommand|.
100 %
101 % The reason for this is \TeX{}nical or, better, \LaTeX{}nical. When
102 % \LaTeX{} performs the above definition, the expansion of |\xyz| will
103 % be
104 %\begin{verbatim}
105 % \@protected@testopt \xyz \\xyz {x}
106 %\end{verbatim}
107 % where |\@protected@testopt| is a macro that essentially checks
108 % whether we are in a ``protected'' context, so that expansion should
109 % not be performed all the way (in moving arguments or write
110 % operations), or not; in the former case it issues a protected
111 % version of |\xyz|, while in the latter case it expands the macro
112 % |\\xyz| that is a \emph{single} command (yes, with a backslash in
113 % its name) which contains the real definition; a way to access this
114 % definition is to issue the command
115 %\begin{verbatim}
116 % \expandafter\show\csname\string\xyz\endcsname
117 %\end{verbatim}
118 % which will print in the log file the message
119 %\begin{verbatim}
120 % > \\xyz=\long macro:
121 % [#1]->-#1!.
122 %\end{verbatim}
123 % As usual, after |->| we see the definition. In order to use
124 % |\patchcmd| to change the exclamation mark into a hyphen one must do
125 %\begin{verbatim}
126 % \expandafter\patchcmd\csname\string\xyz\endcsname{!}{-}{}{}
127 %\end{verbatim}
128 % (see the documentation of \pkg{etoolbox} for details about the
129 % arguments).
130 %
131 % A similar situation happens if |\xyz| has been defined by
132 %\begin{verbatim}
133 % \DeclareRobustCommand{\xyz}{something}
134 %\end{verbatim}
135 % A |\show\xyz| instruction would show the cryptic
136 %\begin{verbatim}
137 % > \xyz=macro:
138 % ->\protect \xyz .
139 %\end{verbatim}
140 % and only a close look reveals the clever trick used by the \LaTeX{}
141 % team: the |\protect| is not applied to |\xyz|, but to the macro
142 % \verb*?\xyz ? which has a space at the end of its name! And this
143 % macro is the one that contains the real definition. Indeed,
144 %\begin{verbatim}
145 % \expandafter\show\csname xyz\space\endcsname
146 %\end{verbatim}
147 % produces the message
148 %\begin{verbatim}
149 % > \xyz =\long macro:
150 % ->something.
151 %\end{verbatim}
152 % In this case, in order to apply |\patchcmd| we must say
153 %\begin{verbatim}
154 % \expandafter\patchcmd\csname xyz\space\endcsname{s}{S}{}{}
155 %\end{verbatim}
156 % If the macro with |\DeclareRobustCommand| is defined to have an
157 % optional argument, say
158 %\begin{verbatim}
159 % \DeclareRobustCommand{\xyz}[1][x]{-#1!}
160 %\end{verbatim}
161 % one has to combine the two tricks:
162 %\begin{verbatim}
163 % \expandafter\patchcmd\csname\string\xyz\space\endcsname{!}{-}{}{}
164 %\end{verbatim}
165 % It's hard and error prone to remember all of these tricks, so this
166 % package comes to the rescue.
167 %
168 % The package is now completely independent of \pkg{etoolbox}. It
169 % doesn't feature commands analogous to |\preto| and |\appto| that, in
170 % the author's opinion, are a bit dangerous, since somebody might
171 % apply them to commands defined with |\DeclareRobustCommand| or
172 % |\newrobustcmd|, with the obvious problems.
173 %
174 % The \pkg{regexpatch} package uses many features of the \LaTeX3
175 % experimental packages, in particular of \pkg{l3regex}. This has a
176 % clear advantage: we can have a *-variant of |\xpatchcmd| that does a
177 % ``replace~all'' which can avoid multiple uses of |\patchcmd| on the
178 % same macro. Moreover there's a very powerful |\regexpatchcmd|
179 % function that uses regular expression syntax for search and replace
180 % which can even patch commands defined under different category code
181 % setup.
182 %
183 % For example, let's see how the \LaTeX{} kernel defines |\strip@pt|:
184 %\begin{verbatim}
185 % \begingroup
186 % \catcode`P=12
187 % \catcode`T=12
188 % \lowercase{
189 % \def\x{\def\rem@pt##1.##2PT{##1\ifnum##2>\z@.##2\fi}}}
190 % \expandafter\endgroup\x
191 % \def\strip@pt{\expandafter\rem@pt\the}
192 %\end{verbatim}
193 % The same result can be obtained by
194 %\begin{verbatim}
195 % \begingroup\def\defrem@pt{\endgroup
196 % \def\rem@pt##1.##2pt{##1\ifnum##2>\z@.##2\fi}}
197 % \regexpatchcmd{\defrem@pt}{pt}{\cOp\cOt}{}{}
198 % \defrem@pt
199 % \def\strip@pt{\expandafter\rem@pt\the}
200 %\end{verbatim}
201 % Perhaps not so striking, but the pattern seems to be more intuitive;
202 % however the package supplies also a function for patching the
203 % parameter text of a macro:
204 %\begin{verbatim}
205 % \def\rem@pt#1.#2pt{#1\ifnum#2>\z@.#2\fi}
206 % \xpatchparametertext{\rem@pt}{pt}{\cO p \cO t}{}{}
207 %\end{verbatim}
208 % Of course, reading the manual of \pkg{l3regex} is necessary for
209 % being able to exploit the full power of |\regexpatchcmd| or
210 % |\xpatchparametertext|; in this case, `|\cO p|' (the space in between
211 % is optional) specifies a character `|p|' with category code `other'.
212 % Actually neither the |\cO| escape is necessary, as all letters in a
213 % replacement text in the context of regular expressions has category
214 % code 12 by default, but clarity is often to be preferred to
215 % efficiency.
216 %
217 %
218 % \section{Important notices}
219 %
220 % If the command to be patched contains `|@|-commands' in its
221 % replacement text, \emph{always} ensure that the patching code is
222 % enclosed between |\makeatletter| and |\makeatother|; this is
223 % different from what \pkg{etoolbox} requires. It's recommended to
224 % turn on |\tracingxpatches| when testing a patch, to get maximum
225 % information.
226 %
227 % Some people like to add informative messages to the \meta{failure}
228 % code in the patching commands. Usually I'm lazy and don't do it;
229 % when testing I find it better to trace the patchings or add |\ddt|
230 % to the \meta{failure} code. Adding warnings to the \meta{success}
231 % code is annoying for the user.
232 %
233 %
234 % \section{Acknowledgment}
235 %
236 % This package would not exist without the \pkg{l3regex} package
237 % and Bruno Le Floch. Some parts of \pkg{l3regex} were added just
238 % because I asked for them while developing the present package.
239 % Thanks also to Joseph Wright and all the \LaTeX3 team.
240 %
241 %
242 % \section{Commands}
243 %
244 % The main commands introduced by this package are
245 % \begin{itemize}
246 % \item |\xpretocmd|
247 % \item |\xapptocmd|
248 % \item |\xpatchcmd|
249 % \item |\regexpatchcmd|
250 % \end{itemize}
251 % which have the same syntax as the similar commands provided by
252 % \pkg{etoolbox} and apply to all kind of commands defined by
253 % \begin{itemize}
254 % \item the \LaTeX{} kernel macros |\newcommand|, |\renewcommand|,
255 % |\providecommand|, but also |\newenvironment| and
256 % |\renewenvironment|;
257 % \item the \LaTeX{} kernel macro for defining robust commands
258 % |\DeclareRobustCommand|;
259 % \item the \pkg{etoolbox} macros |\newrobustcmd|, |\renewrobustcmd|,
260 % |\providerobustcmd|.
261 % \end{itemize}
262 % Notice that patching the definition of the environment |foo|
263 % requires patching |\foo| or |\endfoo|.
264 %
265 % These commands will act as the original ones if the macro to patch
266 % is not robust or with optional arguments.
267 %
268 % There is also added functionality that \pkg{etoolbox} doesn't
269 % provide (at least easily for the first command):
270 % \begin{itemize}
271 % \item |\xpatchoptarg|
272 % \item |\xpatchparametertext|
273 % \item |\checkifpatchable|
274 % \end{itemize}
275 %
276 % Moreover the package defines
277 % \begin{itemize}
278 % \item |\xpretobibmacro|
279 % \item |\xapptobibmacro|
280 % \item |\xpatchbibmacro|
281 % \item |\regexpatchbibmacro|
282 % \end{itemize}
283 % that can be used to patch commands defined with \pkg{biblatex}'s
284 % |\newbibmacro|. Say that we have
285 %\begin{verbatim}
286 % \newbibmacro{foo.bar}[2]{#1 and #2}
287 %\end{verbatim}
288 % Then, to change |and| into |und|, we can now say
289 %\begin{verbatim}
290 % \xpatchbibmacro{foo.bar}{and}{und}{}{}
291 %\end{verbatim}
292 % \medskip
293 % \begin{lrbox}{0}
294 % \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
295 % \footnotesize^^A
296 % Patching these macros with \pkg{etoolbox} requires
297 % resorting to the \emph{very} cryptic
298 %\begin{verbatim}
299 % \expandafter\patchcmd\csname abx@macro@\detokenize{foo.bar}\endcsname
300 % {and}{und}{}{}
301 %\end{verbatim}
302 % that would become an astonishing
303 %\begin{verbatim}
304 % \expandafter\patchcmd\csname\expandafter\string\csname
305 % abx@macro@\detokenize{foo.bar}\endcsname\endcsname
306 % {and}{und}{}{}
307 %\end{verbatim}
308 % if the original definition had been with an optional argument, say
309 %\begin{verbatim}
310 % \newbibmacro{foo.bar}[2][x]{#1 and #2}
311 %\end{verbatim}
312 % \end{minipage}
313 % \end{lrbox}
314 % \noindent\fbox{\usebox{0}}
315 %
316 % \medskip
317 %
318 % For \pkg{biblatex} users there are also
319 % \begin{itemize}
320 % \item |\xpretobibdriver|
321 % \item |\xapptobibdriver|
322 % \item |\xpatchbibdriver|
323 % \item |\regexpatchbibdriver|
324 % \end{itemize}
325 % for patching commands defined with |\DeclareBibliographyDriver|. One
326 % could use, for patching the driver |foo|,
327 %\begin{verbatim}
328 % \makeatletter
329 % \patchcmd{\blx@bbx@foo}{X}{Y}{success}{failure}
330 % \preto{\blx@bbx@foo}{P}
331 % \appto{\blx@bbx@foo}{A}
332 % \makeatother
333 %\end{verbatim}
334 % but having a lighter interface can be handy. Since our macros use
335 % |\pretocmd| and |\apptocmd| for consistency, remember to always use
336 % the |{success}| and |{failure}| arguments also with
337 % |\xpretobibdriver| and |\xapptobibdriver|.
338 %
339 % Under the same philosophy, one can use the macros
340 % \begin{itemize}
341 % \item |\xpatchfieldformat|,\\ |\xpretofieldformat|,\\ |\xapptofieldformat|,
342 % \item |\xpatchnameformat|,\\ |\xpretonameformat|,\\ |\xapptonameformat|,
343 % \item |\xpatchlistformat|,\\ |\xpretonameformat|,\\ |\xapptonameformat|,
344 % \item |\xpatchindexfieldformat|,\\ |\xpretoindexfieldformat|,\\ |\xapptoindexfieldformat|,
345 % \item |\xpatchindexnameformat|,\\ |\xpretoindexnameformat|,\\ |\xapptoindexnameformat|,
346 % \item |\xpatchindexlistformat|,\\ |\xpretoindexlistformat|,\\ |\xapptoindexlistformat|,
347 % \end{itemize}
348 % for the \pkg{biblatex} internal macro defined respectively with
349 % \begin{flushleft}
350 % |\DeclareFieldFormat|,
351 % |\DeclareNameFormat|,
352 % |\DeclareListFormat|,\\
353 % |\DeclareIndexFieldFormat|,
354 % |\DeclareIndexNameFormat|,
355 % |\DeclareIndexListFormat|.
356 % \end{flushleft}
357 % All the eighteen |\x...format| commands take a first optional argument, with
358 % default value |*|, see later on.
359 %
360 % Finally, the package defines the commands
361 % \begin{itemize}
362 % \item |\xshowcmd|
363 % \item |\xshowbibmacro|
364 % \item |\xshowbibdriver|
365 % \item |\xshowfieldformat|
366 % \item |\xshownameformat|
367 % \item |\xshowlistformat|
368 % \item |\xshowindexfieldformat|
369 % \item |\xshowindexnameformat|
370 % \item |\xshowindexlistformat|
371 % \item |\tracingxpatches|
372 % \end{itemize}
373 % The first three are the analog of |\show| to see the ``real''
374 % definition of a macro, be it defined with optional arguments or as a
375 % robust command; the |bib| ones are for the corresponding
376 % \pkg{biblatex} macros. The last one takes an optional argument for
377 % activating and deactivating the tracing system. So
378 %\begin{verbatim}
379 % \tracingxpatches
380 %\end{verbatim}
381 % will activate it (it's equivalent to |\tracingxpatches[1]|), while
382 %\begin{verbatim}
383 % \tracingxpatches[0]
384 %\end{verbatim}
385 % will stop issuing messages.
386 %
387 %
388 % \section{Syntax}
389 %
390 % Here is the formal syntax of the commands.
391 %
392 % \medskip
393 % {\small\noindent
394 % |\xpatchcmd|\marg{command}\marg{search}\marg{replace}^^A
395 % \marg{success}\marg{failure}\\
396 % |\xpretocmd|\marg{command}\marg{prepend}^^A
397 % \marg{success}\marg{failure}\\
398 % |\xapptocmd|\marg{command}\marg{append}^^A
399 % \marg{success}\marg{failure}\\[\medskipamount]
400 % |\xpatchbibmacro|\marg{name}\marg{search}\marg{replace}^^A
401 % \marg{success}\marg{failure}\\
402 % |\xpretobibmacro|\marg{name}\marg{prepend}^^A
403 % \marg{success}\marg{failure}\\
404 % |\xapptobibmacro|\marg{name}\marg{append}^^A
405 % \marg{success}\marg{failure}\\[\medskipamount]
406 % |\xpatchbibdriver|\marg{name}\marg{search}\marg{replace}^^A
407 % \marg{success}\marg{failure}\\
408 % |\xpretobibdriver|\marg{name}\marg{prepend}^^A
409 % \marg{success}\marg{failure}\\
410 % |\xapptobibdriver|\marg{name}\marg{append}^^A
411 % \marg{success}\marg{failure}\\[\medskipamount]
412 % |\xpatchfieldformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
413 % \marg{success}\marg{failure}\\
414 % |\xpretofieldformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
415 % \marg{success}\marg{failure}\\
416 % |\xapptofieldformat|\oarg{entrytype}\marg{name}\marg{append}^^A
417 % \marg{success}\marg{failure}\\[\medskipamount]
418 % |\xpatchnameformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
419 % \marg{success}\marg{failure}\\
420 % |\xpretonameformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
421 % \marg{success}\marg{failure}\\
422 % |\xapptonameformat|\oarg{entrytype}\marg{name}\marg{append}^^A
423 % \marg{success}\marg{failure}\\[\medskipamount]
424 % |\xpatchlistformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
425 % \marg{success}\marg{failure}\\
426 % |\xpretolistformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
427 % \marg{success}\marg{failure}\\
428 % |\xapptolistformat|\oarg{entrytype}\marg{name}\marg{append}^^A
429 % \marg{success}\marg{failure}\\[\medskipamount]
430 % |\xpatchindexfieldformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
431 % \marg{success}\marg{failure}\\
432 % |\xpretoindexfieldformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
433 % \marg{success}\marg{failure}\\
434 % |\xapptoindexfieldformat|\oarg{entrytype}\marg{name}\marg{append}^^A
435 % \marg{success}\marg{failure}\\[\medskipamount]
436 % |\xpatchindexnameformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
437 % \marg{success}\marg{failure}\\
438 % |\xpretoindexnameformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
439 % \marg{success}\marg{failure}\\
440 % |\xapptoindexnameformat|\oarg{entrytype}\marg{name}\marg{append}^^A
441 % \marg{success}\marg{failure}\\[\medskipamount]
442 % |\xpatchindexlistformat|\oarg{entrytype}\marg{name}\marg{search}\marg{replace}^^A
443 % \marg{success}\marg{failure}\\
444 % |\xpretoindexlistformat|\oarg{entrytype}\marg{name}\marg{prepend}^^A
445 % \marg{success}\marg{failure}\\
446 % |\xapptoindexlistformat|\oarg{entrytype}\marg{name}\marg{append}^^A
447 % \marg{success}\marg{failure}\\[\medskipamount]
448 % |\xshowcmd|\sv\marg{command}\\
449 % |\xshowbibname|\marg{name}\\
450 % |\xshowbibdriver|\marg{name}\\[\medskipamount]
451 % |\xpatchoptarg|\marg{name}\marg{replace}\\
452 % |\xpatchparametertext|\marg{name}\marg{search}\marg{replace}^^A
453 % \marg{success}\marg{failure}\\
454 % |\checkifpatchable|\marg{name}\\
455 % |\tracingxpatches|\oarg{number}
456 %
457 % }
458 %
459 % \medskip
460 %
461 % \begingroup\emergencystretch=.5\textwidth
462 % Here \meta{command} is the command's name (with the backslash),
463 % while \meta{name} is the string that appears as the argument to
464 % |\newbibmacro|, |\DeclareBibliographyDriver|, |\DeclareFieldFormat|,
465 % |\DeclareNameFormat|, |\DeclareListFormat|, |\DeclareIndexFieldFormat|,
466 % |\DeclareIndexNameFormat| or |\DeclareIndexListFormat| respectively;
467 % \meta{search}, \meta{replace}, \meta{prepend} and \meta{append} are
468 % the list of tokens that are to be used for the specific tasks;
469 % \meta{success} and \meta{failure} are token lists to be executed if
470 % the patching succeeds or fails respectively. I find it useful to use
471 % |\ddt| as \meta{failure}, so that \TeX{} will stop for the undefined
472 % control sequence when the patching fails.\par\endgroup
473 %
474 % All the |\x...format| macros have an optional argument that by
475 % default is |*|.
476 %
477 % In the commands whose name contains the string |regex|, both
478 % \meta{search} and \meta{replace} are understood to represent regular
479 % expressions. Check with the documentation of \pkg{l3regex} for
480 % details.
481 %
482 % The *-variants of the |patch| type commands means that the
483 % replacement is performed on \emph{all} matches. With
484 % |\xshowcmd*\foo| one gets all information on |\foo|, as if the
485 % tracing system were activated, including the default optional
486 % argument, if existent. So it's best to use it before trying
487 % |\xpatchoptarg| (and all the other commands, of course).
488 %
489 % A curiosity about optional arguments: if one defines
490 %\begin{verbatim}
491 % \newcommand{\foo}[1][{bar}]{-#1-}
492 %\end{verbatim}
493 % then the braces around |bar| are stripped off. So with
494 %\begin{verbatim}
495 % \newcommand{\foo}[1][{\itshape bar}]{-#1-}
496 %\end{verbatim}
497 % all text following the call of |\foo| without an optional argument
498 % would be set in italics; one needs \emph{two} sets of braces, in
499 % this case. However,
500 %\begin{verbatim}
501 % \xpatchoptarg{\foo}{{\itshape bar}}
502 %\end{verbatim}
503 % would \emph{not} strip the braces.
504 %
505 % It's important to remember that patching commands that have |@| in
506 % their name must \emph{always} be performed between |\makeatletter|
507 % and |\makeatother|.
508 %
509 % \section{Examples}
510 %
511 % From \texttt{http://tex.stackexchange.com/a/42894}: the series
512 % of successive patches for changing the three occurrences of
513 % |\mathcode| in |\@addligto| into |\Umathcodenum| can become
514 %\begin{verbatim}
515 % \xpatchcmd*{\@addligto}{\mathcode}{\Umathcodenum}{}{}
516 %\end{verbatim}
517 % while the code
518 %\begin{verbatim}
519 % \expandafter\patchcmd\csname mathligsoff \endcsname
520 % {\mathcode}{\Umathcodenum}{}{}
521 %\end{verbatim}
522 % needed without \pkg{regexpatch} can become
523 %\begin{verbatim}
524 % \xpatchcmd\mathligsoff{\mathcode}{\Umathcodenum}{}{}
525 %\end{verbatim}
526 %
527 % Another one: changing the space reserved for the theorem number in
528 % the `List of theorems' provided by \pkg{ntheorem} could be obtained
529 % with \pkg{etoolbox}'s |\patchcmd| by
530 %\begin{verbatim}
531 % \patchcmd{\thm@@thmline}{2.3em}{5em}{}{}
532 % \patchcmd{\thm@@thmline@name}{2.3em}{5em}{}{}
533 % \patchcmd{\thm@@thmline@noname}{2.3em}{5em}{}{}
534 %\end{verbatim}
535 % if the |hyperref| option is not used, but a long series of patches
536 % would be needed with the option, as |2.3em| appears three times in
537 % each macro. With \pkg{regexpatch} one can do independently of the
538 % option:
539 %\begin{verbatim}
540 % \xpatchcmd*{\thm@@thmline}{2.3em}{5em}{}{}
541 % \xpatchcmd*{\thm@@thmline@name}{2.3em}{5em}{}{}
542 % \xpatchcmd*{\thm@@thmline@noname}{2.3em}{5em}{}{}
543 %\end{verbatim}
544 %
545 % A user asked how to patch the |rubric| environment in the `CurVe'
546 % class in order to avoid the repetition of the rubric's title on
547 % continuation pages. The environment is based on |longtable| and the
548 % task is to remove the |\endhead| material, which is delimited by
549 % |\endfirsthead| and |\endhead|. Instead of
550 %\begin{verbatim}
551 % \patchcmd{\rubric}
552 % {\endfirsthead\@rubrichead{#1\@continuedname}\\*[\rubricspace]\endhead}
553 % {\endfirsthead}{}{}
554 %\end{verbatim}
555 % one can more simply exploit regular expressions:
556 %\begin{verbatim}
557 % \makeatletter % the replacement text has @-commands
558 % \regexpatchcmd{\rubric}
559 % { \c{endfirsthead} .* \c{endhead} }
560 % { \c{endfirsthead} }{}{}
561 % \makeatother
562 %\end{verbatim}
563 %
564 % Assume you want to insert a patch in the argument of a command; with
565 % the traditional method this is possible provided the patch text
566 % doesn't contain |#|. Here's an example
567 %\begin{verbatim}
568 % \makeatletter
569 % \usepackage{etoolbox} % for \ifdef
570 % \ifdef{\H@old@part}
571 % {
572 % \regexpatchcmd{\H@old@part}
573 % {$} % regex representing the end
574 % {\c{gdef}\c{cont@name@part}\cB\{\cP\#2\cE\}} % replacement
575 % {}{}%
576 % }
577 % {
578 % \regexpatchcmd{\@part}
579 % {$}
580 % {\c{gdef}\c{cont@name@part}\cB\{\cP\#2\cE\}}
581 % {}{}%
582 % }
583 % \makeatother
584 %\end{verbatim}
585 % We want to add |\gdef\cont@name@part{#2}| at the end of the
586 % replacement text, distinguishing when \pkg{hyperref} is loaded or
587 % not. So we patch the command by doing just what's requested. The
588 % example is a bit contrived, as using |\ifdefined| instead of the
589 % argument form wrapper would allow the traditional |\apptocmd|.
590 % However, other applications may be foreseen.
591 %
592 % A problem raised on |comp.text.tex| in 2008 was to extract the
593 % number from the name of the file being typeset; the name was of the
594 % form |lecture15.tex| and the question was how to define a macro
595 % |\lecturenumber| that acted on |\jobname| to do its work. The
596 % obvious
597 %\begin{verbatim}
598 % \def\lecturenumber{\expandafter\extractnumber\jobname;}
599 % \def\extractnumber lecture#1;{#1}
600 %\end{verbatim}
601 % doesn't work because the characters produced by |\jobname| all have
602 % category code 12 (spaces have 10, as usual). A nifty solution was
603 % provided by David Kastrup:
604 %\begin{verbatim}
605 % \begingroup
606 % \escapechar=-1
607 % \expandafter\endgroup
608 % \expandafter\def\expandafter\extractnumber\string\lecture#1;{#1}
609 %\end{verbatim}
610 % Now with |\xpatchparametertext| one can do
611 %\begin{verbatim}
612 % \def\lecturenumber{\expandafter\extractnumber\jobname;}
613 % \def\extractnumber lecture#1;{#1}
614 % \xpatchparametertext\extractnumber{lecture}{lecture}{}{}
615 %\end{verbatim}
616 % recalling that the substitution performed by \pkg{l3regex} uses
617 % category code 12 characters by default. The command can be
618 % generalized to accept any (fixed) prefix:
619 %\begin{verbatim}
620 % \def\setupfilenumber#1{%
621 % \def\filenumber{\expandafter\extractnumber\jobname;}%
622 % \def\extractnumber#1##1;{##1}%
623 % \xpatchparametertext\extractnumber{#1}{#1}{}{}}
624 % \setupfilenumber{lecture}
625 %\end{verbatim}
626 % where the prefix is passed to |\setupfilenumber| and the macro to
627 % use is |\filenumber|.
628 %
629 % A proper \LaTeX3 definition might be
630 %\begin{verbatim}
631 % \NewDocumentCommand{\setupfilenumber}{ m }
632 % {
633 % \group_begin:
634 % \cs_set:Npx \filenumber_aux:
635 % {
636 % \group_end:
637 % \cs_set:Npn \exp_not:N \filenumber_extract:w
638 % \tl_to_str:n { #1 } ####1 ; { ####1 }
639 % }
640 % \filenumber_aux:
641 % }
642 % \NewDocumentCommand{\filenumber} {}
643 % {
644 % \exp_after:wN \filenumber_extract:w \c_job_name_tl ;
645 % }
646 %\end{verbatim}
647 %
648 % \end{documentation}
649 %
650 % \begin{implementation}
651 %
652 % \section{The implementation of \pkg{regexpatch}}
653 %
654 % \iffalse
655 %<*package>
656 % \fi
657 %
658 % The usual starting stuff.
659 % \begin{macrocode}
660 \ProvidesExplPackage
661 {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
662 \@ifpackagelater { expl3 } { 2012/01/19 }
663 { }
664 {
665 \PackageError { regexpatch } { Support~package~l3kernel~too~old. }
666 {
667 Please~install~an~up~to~date~version~of~l3kernel~
668 using~your~TeX~package~manager~or~from~CTAN.\\ \\
669 Loading~regexpatch~will~abort!
670 }
671 \tex_endinput:D
672 }
673 \RequirePackage{xparse}
674 % \end{macrocode}
675 %
676 % \subsection{Variables}
677 %
678 % We define a bunch of variables: some booleans and token lists. The
679 % first tells us when the macro to patch has been defined by
680 % |\DeclareRobustCommand|, the second if it has an optional argument,
681 % the third if it's patchable, that is it can be reconstructed from
682 % its decomposition under the current category code regime. The last
683 % boolean is used for the tracing system: if true, messages about
684 % patching are issued.
685 % \begin{macrocode}
686 \bool_new:N \l_xpatch_protect_bool
687 \bool_new:N \l_xpatch_optional_bool
688 \bool_new:N \l_xpatch_patchable_bool
689 \bool_new:N \l_xpatch_tracing_bool
690 % \end{macrocode}
691 % The token list variables contain various items regarding the macro
692 % to patch: the name, the first level replacement text (we distinguish
693 % it from the `real' replacement text), the prefixes, the argument
694 % spec and the `real' replacement text.
695 % \begin{macrocode}
696 \tl_new:N \l_xpatch_name_tl
697 \tl_new:N \l_xpatch_repl_tl
698 \tl_new:N \l_xpatch_prefix_tl
699 \tl_new:N \l_xpatch_arg_tl
700 \tl_new:N \l_xpatch_replacement_tl
701 \tl_new:N \l_xpatch_type_tl % for debugging messages
702 % \end{macrocode}
703 % A variant for checking a regex match so that we can give the second
704 % argument as a token list.
705 % \begin{macrocode}
706 \prg_generate_conditional_variant:Nnn \regex_match:nn {nV} { T,F,TF }
707 % \end{macrocode}
708 %
709 % \subsection{Functions}
710 %
711 % The function |\xpatch_main_check:N| is responsible for telling us
712 % what kind of macro we're patching. Only one of the first four tests
713 % can be true; if none is, the macro is not `special' and can be
714 % patched without doing anything particular to get its
715 % `real~name'. The check consists in matching with a suitable regex
716 % at the start of the replacement text (which is in detokenized
717 % form). If the macro passes one of the first two tests, it can still
718 % have an optional argument, so a supplementary test is needed.
719 %
720 % Some technical remarks. Suppose we have the following definitions:
721 %\begin{verbatim}
722 % \DeclareRobustCommand{\xaa}[1]{xaa (DeclareRobustCommand-noopt)}
723 % \DeclareRobustCommand{\xab}[1][x]{xab (DeclareRobustCommand-opt)}
724 % \newcommand{\xac}[1][]{xac (newcommand-opt)}
725 % \newrobustcmd\xad[1][]{xad (newrobustcmd-opt)}
726 % \DeclareRobustCommand{\1}[1]{1 (DeclareRobustCommand-noopt)}
727 % \DeclareRobustCommand{\2}[1][]{2 (DeclareRobustCommand-opt)}
728 % \newcommand{\3}[1][]{3 (newcommand-opt)}
729 % \newrobustcmd\4[1][]{4 (newrobustcmd-opt)}
730 %\end{verbatim}
731 % Then the first level expansions are, respectively,
732 %\begin{verbatim*}
733 %+\protect \xaa +
734 %+\protect \xab +
735 %+\@protected@testopt \xac \\xac {}+
736 %+\@testopt \\xad {}+
737 %+\x@protect \1\protect \1 +
738 %+\x@protect \2\protect \2 +
739 %+\@protected@testopt \3\\3 {}+
740 %+\@testopt \\4 {}+
741 %\end{verbatim*}
742 % where the |+| is used to delimit the expansions and show the
743 % spaces. Remember that |\show| always adds a space after a control
744 % word, but not after a control symbol such as |\1|. However, in lines
745 % 5~and~6, \verb*?\1 ? is not a control symbol any more. So we have to
746 % take care of |\protect|, |\x@protect|, |\@protected@testopt| and
747 % |\@testopt|. But it's not simply sufficient to check for the
748 % presence of such a token at the start of the replacement text, or
749 % we'll be confused by macros such as |\linebreak|, whose replacement
750 % text starts with |\@testopt|. So we'll check also for the presence
751 % of the subsequent tokens, that depend on the macro's name. If the
752 % macro is recognized to have an optional argument, its default value
753 % is stored in |\tl_xpatch_repl_tl| (that we wouldn't use any more) to
754 % be shown by |\xshowcmd*| or when the tracing system is active: we
755 % throw away everything except what's contained between the final pair
756 % of braces.
757 % \begin{macrocode}
758 \cs_new_protected:Npn \xpatch_main_check:N #1
759 {
760 \bool_set_false:N \l_xpatch_protect_bool
761 \bool_set_false:N \l_xpatch_optional_bool
762 \tl_set:Nf \l_xpatch_name_tl { \cs_to_str:N #1 }
763 \tl_set:Nf \l_xpatch_repl_tl { \cs_replacement_spec:N #1 }
764 \tl_clear:N \l_xpatch_type_tl
765 \regex_match:nVT % \DeclareRobustCommand<control word>
766 {^\\protect\ \\\u{l_xpatch_name_tl}\ \ }
767 \l_xpatch_repl_tl
768 {
769 \bool_set_true:N \l_xpatch_protect_bool
770 \tl_put_right:Nx \l_xpatch_name_tl { \c_space_tl }
771 \tl_set:Nn \l_xpatch_type_tl { DRCw }
772 }
773 \regex_match:nVT % \DeclareRobustCommand<control symbol>
774 {^\\x@protect\ \\\u{l_xpatch_name_tl}\\}
775 \l_xpatch_repl_tl
776 {
777 \bool_set_true:N \l_xpatch_protect_bool
778 \tl_put_right:Nx \l_xpatch_name_tl { \c_space_tl }
779 \tl_set:Nn \l_xpatch_type_tl { DRCs }
780 }
781 \regex_match:nVT % \newcommand<control word> with opt arg
782 {^\\@protected@testopt\ \\\u{l_xpatch_name_tl}\ \\\\}
783 \l_xpatch_repl_tl
784 {
785 \bool_set_true:N \l_xpatch_optional_bool
786 \tl_put_left:Nx \l_xpatch_name_tl { \c_backslash_str }
787 \tl_set:Nn \l_xpatch_type_tl { ncw+o }
788 }
789 \regex_match:nVT % \newcommand<control symbol> with opt arg
790 {^\\@protected@testopt\ \\\u{l_xpatch_name_tl}\\\\}
791 \l_xpatch_repl_tl
792 {
793 \bool_set_true:N \l_xpatch_optional_bool
794 \tl_put_left:Nx \l_xpatch_name_tl { \c_backslash_str }
795 \tl_set:Nn \l_xpatch_type_tl { ncs+o }
796 }
797 \regex_match:nVT % \newrobustcmd<any cs> with opt arg
798 {^\\@testopt\ \\\\\u{l_xpatch_name_tl}}
799 \l_xpatch_repl_tl
800 {
801 \bool_set_true:N \l_xpatch_optional_bool
802 \tl_put_left:Nx \l_xpatch_name_tl { \c_backslash_str }
803 \tl_set:Nn \l_xpatch_type_tl { nrc+o }
804 }
805 \bool_if:NT \l_xpatch_protect_bool
806 {
807 \tl_set:Nf \l_xpatch_repl_tl
808 { \exp_after:wN \cs_replacement_spec:N
809 \cs:w \l_xpatch_name_tl \cs_end: }
810 \regex_match:nVT % \DeclareRobustCommand<any cs> with opt arg
811 {^\\@protected@testopt\ \\\u{l_xpatch_name_tl}\ \\\\}
812 \l_xpatch_repl_tl
813 {
814 \bool_set_true:N \l_xpatch_optional_bool
815 \tl_put_left:Nx \l_xpatch_name_tl { \c_backslash_str }
816 \tl_put_right:Nn \l_xpatch_type_tl { +o }
817 }
818 }
819 \bool_if:NT \l_xpatch_optional_bool
820 {
821 \regex_replace_once:nnN { .*? \{ (.*) \} \Z } { \1 }
822 \l_xpatch_repl_tl
823 }
824 }
825 % \end{macrocode}
826 % We use the information gathered with |\xpatch_main_check:N| to
827 % perform the patch; the macro to patch is |#2|, the function to
828 % execute is |#1|; in case the macro's name is misspelled, the
829 % following arguments will be ignored because they have already been
830 % absorbed. The main function is |\xpatch_main_four:NNnnnn|, where the
831 % |four| refers to the number of braced arguments for the |patch| and
832 % |regexpatch| type macros; we define also a |three| function for
833 % |preto| and |appto| macros, and a |zero| function for the |show|
834 % macros. We also define the variants taking a name as their second
835 % argument.
836 % \begin{macrocode}
837 \cs_new_protected:Npn \xpatch_main_four:NNnnnn #1 #2 #3 #4 #5 #6
838 {
839 \cs_if_exist:NTF #2
840 {
841 \xpatch_main_check:N #2
842 \bool_if:NT \l_xpatch_tracing_bool
843 { \xpatch_message_cstype:N #2 }
844 \exp_after:wN #1 \cs:w \l_xpatch_name_tl \cs_end: {#3}{#4}{#5}{#6}
845 }
846 {
847 \iow_term:x
848 {
849 xpatch~message: ^^J
850 `\token_to_str:N #2'~is~undefined;~
851 I'll~ignore~the~request.
852 }
853 }
854 }
855 \cs_new_protected:Npn \xpatch_main_three:NNnnn #1 #2 #3 #4 #5
856 {
857 \xpatch_main_four:NNnnnn #1 #2 { #3 } { #4 } { #5 } { }
858 }
859 \cs_new_protected:Npn \xpatch_main_zero:NN #1 #2
860 {
861 \xpatch_main_four:NNnnnn #1 #2 { } { } { } { }
862 }
863 \cs_generate_variant:Nn \xpatch_main_zero:NN {Nc}
864 \cs_generate_variant:Nn \xpatch_main_three:NNnnn {Nc}
865 \cs_generate_variant:Nn \xpatch_main_four:NNnnnn {Nc}
866 % \end{macrocode}
867 %
868 % Now we define the patching functions. We get all the parts in which
869 % a macro can be split: prefixes, parameter text and replacement text;
870 % the name is already available. The token lists |\l_xpatch_X_tl|
871 % will contain the prefix or parameter text or replacement text of
872 % |#1| first in `detokenized' and then in `tokenized' form.
873 % \begin{macrocode}
874 \cs_new_protected:Npn \xpatch_get_all:N #1
875 {
876 \tl_set:Nf \l_xpatch_prefix_tl { \cs_prefix_spec:N #1 }
877 \tl_set_rescan:Nnx \l_xpatch_prefix_tl { } \l_xpatch_prefix_tl
878 \tl_set:Nf \l_xpatch_arg_tl { \cs_argument_spec:N #1 }
879 \tl_set_rescan:Nnx \l_xpatch_arg_tl { } \l_xpatch_arg_tl
880 \tl_set:Nf \l_xpatch_replacement_tl { \cs_replacement_spec:N #1 }
881 \tl_set_rescan:Nnx \l_xpatch_replacement_tl { } \l_xpatch_replacement_tl
882 }
883 % \end{macrocode}
884 % After possible modifications to the replacement text, we can call
885 % |\xpatch_rebuild:N| to redo the definition of |#1|; we can also use
886 % it for checking if |#1| is patchable. Of course we need to use
887 % |\tex_def:D| at this point. Apologies to the developers of \LaTeX3
888 % that recommend never using |:D| functions.
889 % \begin{macrocode}
890 \cs_new_protected:Npn \xpatch_rebuild:N #1
891 {
892 \use:x
893 {
894 \exp_not:V \l_xpatch_prefix_tl
895 \tex_def:D % unavoidable
896 \exp_not:N #1
897 \exp_not:V \l_xpatch_arg_tl
898 { \exp_not:V \l_xpatch_replacement_tl }
899 }
900 }
901 % \end{macrocode}
902 % To check if |#1| is patchable, we rebuild it as |\xpatch_tmpa:w| and
903 % look whether |#1| and |\xpatch_tmpa:w| are the same. This is always
904 % the first thing to do, so we put |\xpatch_get_all:N| here; |#1| is
905 % the macro to patch.
906 % \begin{macrocode}
907 \cs_new_protected:Npn \xpatch_check_patchable:N #1
908 {
909 \cs_if_exist:NTF #1
910 {
911 \xpatch_get_all:N #1
912 \xpatch_rebuild:N \xpatch_tmpa:w
913 \cs_if_eq:NNTF #1 \xpatch_tmpa:w
914 {
915 \bool_set_true:N \l_xpatch_patchable_bool
916 \xpatch_message:n
917 {
918 Macro `\token_to_str:N #1'~is~patchable
919 }
920 }
921 {
922 \bool_set_false:N \l_xpatch_patchable_bool
923 \xpatch_message:n
924 {
925 Macro `\token_to_str:N #1'~is~NOT~patchable\\
926 (Check~if~it~contains~`@'~commands)
927 }
928 }
929 }
930 {
931 \bool_set_false:N \l_xpatch_patchable_bool
932 \xpatch_message:n
933 {
934 Macro `\token_to_str:N #1'~doesn't~exist.
935 }
936 }
937 }
938 % \end{macrocode}
939 %
940 % Defining the internal versions of |\xpretocmd| and |\xapptocmd| is
941 % easy: we check if the command is patchable and, if so, we prepend or append
942 % the second argument to the replacement text and rebuild the
943 % macro, then we execute the \meta{success} code. If the patch isn't
944 % possible we just execute the \meta{failure} code.
945 % \begin{macrocode}
946 \cs_new_protected:Npn \xpatch_pretocmd:Nnnn #1 #2 #3 #4
947 {
948 \xpatch_check_patchable:N #1
949 \bool_if:NTF \l_xpatch_patchable_bool
950 {
951 \tl_put_left:Nn \l_xpatch_replacement_tl { #2 }
952 \xpatch_rebuild:N #1
953 #3
954 }
955 {
956 #4
957 }
958 }
959 \cs_new_protected:Npn \xpatch_apptocmd:Nnnn #1 #2 #3 #4
960 {
961 \xpatch_check_patchable:N #1
962 \bool_if:NTF \l_xpatch_patchable_bool
963 {
964 \tl_put_right:Nn \l_xpatch_replacement_tl { #2 }
965 \xpatch_rebuild:N #1
966 #3
967 }
968 {
969 #4
970 }
971 }
972 % \end{macrocode}
973 %
974 % Substituting tokens in the replacement text is a bit harder, but not
975 % conceptually different. First the internal version of
976 % |\regexpatchcmd(*)|: check if |#1| is patchable, do the replacement
977 % if possible; beware that characters in the replacement string are of
978 % category 12 by default. We use |\regex_replace_all:nnNTF| and
979 % |\regex_replace_once:nnNTF| in order to pass correctly the success
980 % or failure arguments.
981 % \changes{v0.1a}{2012/06/07}{Fixed bugs in success/failure code with regex...nnNTF}
982 % \begin{macrocode}
983 \cs_new_protected:Npn \xpatch_regexpatchcmd_all:Nnnnn #1 #2 #3 #4 #5
984 {
985 \xpatch_check_patchable:N #1
986 \bool_if:NTF \l_xpatch_patchable_bool
987 {
988 \regex_replace_all:nnNTF { #2 } { #3 } \l_xpatch_replacement_tl
989 { \xpatch_rebuild:N #1 #4 }
990 { #5 }
991 }
992 {
993 #5
994 }
995 }
996 \cs_new_protected:Npn \xpatch_regexpatchcmd_once:Nnnnn #1 #2 #3 #4 #5
997 {
998 \xpatch_check_patchable:N #1
999 \bool_if:NTF \l_xpatch_patchable_bool
1000 {
1001 \regex_replace_once:nnNTF { #2 } { #3 } \l_xpatch_replacement_tl
1002 { \xpatch_rebuild:N #1 #4 }
1003 { #5 }
1004 }
1005 {
1006 #5
1007 }
1008 }
1009 % \end{macrocode}
1010 %
1011 % Thanks to the features of \pkg{l3regex}, we can also implement
1012 % directly the analog of |\patchcmd|, but also with a `replace all'
1013 % version.
1014 % \begin{macrocode}
1015 \cs_new_protected:Npn \xpatch_patchcmd_once:Nnnnn #1 #2 #3 #4 #5
1016 {
1017 \xpatch_check_patchable:N #1
1018 \bool_if:NTF \l_xpatch_patchable_bool
1019 {
1020 \tl_set:Nn \l_tmpa_tl { #2 }
1021 \tl_set:Nn \l_tmpb_tl { #3 }
1022 \regex_replace_once:nnNTF
1023 { \u{l_tmpa_tl} }
1024 { \u{l_tmpb_tl} }
1025 \l_xpatch_replacement_tl
1026 { \xpatch_rebuild:N #1 #4 }
1027 { #5 }
1028 }
1029 {
1030 #5
1031 }
1032 }
1033 \cs_new_protected:Npn \xpatch_patchcmd_all:Nnnnn #1 #2 #3 #4 #5
1034 {
1035 \xpatch_check_patchable:N #1
1036 \bool_if:NTF \l_xpatch_patchable_bool
1037 {
1038 \tl_set:Nn \l_tmpa_tl { #2 }
1039 \tl_set:Nn \l_tmpb_tl { #3 }
1040 \regex_replace_all:nnNTF
1041 { \u{l_tmpa_tl} }
1042 { \u{l_tmpb_tl} }
1043 \l_xpatch_replacement_tl
1044 { \xpatch_rebuild:N #1 #4 }
1045 { #5 }
1046 }
1047 {
1048 #5
1049 }
1050 }
1051 % \end{macrocode}
1052 %
1053 % Now the tracing system.
1054 % \begin{macrocode}
1055 \cs_new_protected:Npn \xpatch_message:n #1
1056 {
1057 \bool_if:NT \l_xpatch_tracing_bool
1058 {
1059 \iow_term:x { xpatch~message ^^J ~ #1 }
1060 }
1061 }
1062 \cs_new:Npn \xpatch_message_cstype:N #1
1063 {
1064 \str_case:onF { \l_xpatch_type_tl }
1065 {
1066 { DRCw } {
1067 \xpatch_message:n
1068 {
1069 `\token_to_str:N #1'~is~a~control~word~defined~
1070 with~\token_to_str:N \DeclareRobustCommand
1071 }
1072 }
1073 { DRCw+o } {
1074 \xpatch_message:n
1075 {
1076 `\token_to_str:N #1'~is~a~control~word~defined~
1077 with~`\token_to_str:N \DeclareRobustCommand'~
1078 and~a~default~optional~argument~`\l_xpatch_repl_tl'
1079 }
1080 }
1081 { DRCs } {
1082 \xpatch_message:n
1083 {
1084 `\token_to_str:N #1'~is~a~control~symbol~defined~
1085 with~`\token_to_str:N \DeclareRobustCommand'
1086 }
1087 }
1088 { DRCs+o } {
1089 \xpatch_message:n
1090 {
1091 `\token_to_str:N #1'~is~a~control~symbol~defined~
1092 with~`\token_to_str:N \DeclareRobustCommand'~
1093 and~a~default~optional~argument~`\l_xpatch_repl_tl'
1094 }
1095 }
1096 { ncw+o } {
1097 \xpatch_message:n
1098 {
1099 `\token_to_str:N #1'~is~a~control~word~defined~
1100 with~`\token_to_str:N \newcommand'~
1101 and~a~default~optional~argument~`\l_xpatch_repl_tl'
1102 }
1103 }
1104 { ncs+o } {
1105 \xpatch_message:n
1106 {
1107 `\token_to_str:N #1'~is~a~control~symbol~defined~
1108 with~`\token_to_str:N \newcommand'~
1109 and~a~default~optional~argument~`\l_xpatch_repl_tl'
1110 }
1111 }
1112 { nrc+o } {
1113 \xpatch_message:n
1114 {
1115 `\token_to_str:N #1'~is~a~control~sequence~defined~
1116 with~`\token_to_str:N \newrobustcmd'~
1117 and~a~default~optional~argument~`\l_xpatch_repl_tl'
1118 }
1119 }
1120 }
1121 {
1122 \xpatch_message:n
1123 {
1124 `\token_to_str:N #1'~is~not~especially~defined
1125 }
1126 }
1127 }
1128 % \end{macrocode}
1129 %
1130 % \subsection{The user level functions}
1131 %
1132 % Here are the functions for patching usual macros; the *-variants for
1133 % |\xpatchcmd| and |\regexpatchcmd| do a `replace all'. All arguments
1134 % are declared `long' with |+m| because we may need |\par| in them.
1135 % \changes{v0.1a}{2012/06/07}{Fixed bug in success/failure code (missing final \texttt{n})}
1136 % \begin{macrocode}
1137 \NewDocumentCommand{\xshowcmd} { s +m }
1138 {
1139 \IfBooleanT{#1}
1140 {
1141 \group_begin:
1142 \bool_set_true:N \l_xpatch_tracing_bool
1143 }
1144 \xpatch_main_zero:NN \cs_show:N #2
1145 \IfBooleanT{#1}
1146 {
1147 \group_end:
1148 }
1149 }
1150 \NewDocumentCommand{\xpretocmd}{ +m +m +m +m }
1151 { \xpatch_main_three:NNnnn \xpatch_pretocmd:Nnnn #1 {#2} {#3} {#4} }
1152 \NewDocumentCommand{\xapptocmd}{ +m +m +m +m }
1153 { \xpatch_main_three:NNnnn \xpatch_apptocmd:Nnnn #1 {#2} {#3} {#4} }
1154 \NewDocumentCommand{\regexpatchcmd}{ s +m +m +m +m +m }
1155 {
1156 \IfBooleanTF{#1}
1157 { \xpatch_main_four:NNnnnn \xpatch_regexpatchcmd_all:Nnnnn #2 {#3}{#4}{#5}{#6} }
1158 { \xpatch_main_four:NNnnnn \xpatch_regexpatchcmd_once:Nnnnn #2 {#3}{#4}{#5}{#6} }
1159 }
1160 \NewDocumentCommand{\xpatchcmd}{ s +m +m +m +m +m }
1161 {
1162 \IfBooleanTF{#1}
1163 { \xpatch_main_four:NNnnnn \xpatch_patchcmd_all:Nnnnn #2 {#3}{#4}{#5}{#6} }
1164 { \xpatch_main_four:NNnnnn \xpatch_patchcmd_once:Nnnnn #2 {#3}{#4}{#5}{#6} }
1165 }
1166 % \end{macrocode}
1167 %
1168 % The functions for patching \pkg{biblatex} related macros that are
1169 % given by name and we'll use the already defined variants.
1170 % \begin{macrocode}
1171 \NewDocumentCommand{\xshowbibmacro} { s +m }
1172 {
1173 \IfBooleanT{#1}
1174 {
1175 \group_begin:
1176 \bool_set_true:N \l_xpatch_tracing_bool
1177 }
1178 \xpatch_main_zero:Nc \cs_show:N { abx@macro@ \tl_to_str:n {#2} }
1179 \IfBooleanT{#1}
1180 {
1181 \group_end:
1182 }
1183 }
1184 \NewDocumentCommand{\xpretobibmacro} { +m +m +m +m }
1185 {
1186 \xpatch_main_three:Ncnnn \xpatch_pretocmd:Nnnn
1187 { abx@macro@ \tl_to_str:n {#1} } {#2}{#3}{#4}
1188 }
1189 \NewDocumentCommand{\xapptobibmacro} { +m +m +m +m }
1190 {
1191 \xpatch_main_three:Ncnnn \xpatch_apptocmd:Nnnn
1192 { abx@macro@ \tl_to_str:n {#1} } {#2}{#3}{#4}
1193 }
1194 \NewDocumentCommand{\xpatchbibmacro} { s +m +m +m +m +m }
1195 {
1196 \IfBooleanTF{#1}
1197 {
1198 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1199 { abx@macro@ \tl_to_str:n {#2} } {#3}{#4}{#5}{#6}
1200 }
1201 {
1202 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1203 { abx@macro@ \tl_to_str:n {#2} } {#3}{#4}{#5}{#6}
1204 }
1205 }
1206 \NewDocumentCommand{\regexpatchbibmacro} { s +m +m +m +m +m }
1207 {
1208 \IfBooleanTF{#1}
1209 {
1210 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1211 { abx@macro@ \tl_to_str:n {#2} } {#3}{#4}{#5}{#6}
1212 }
1213 {
1214 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1215 { abx@macro@ \tl_to_str:n {#2} } {#3}{#4}{#5}{#6}
1216 }
1217 }
1218 \NewDocumentCommand{\xshowbibdriver} { s +m }
1219 {
1220 \IfBooleanT{#1}
1221 {
1222 \group_begin:
1223 \bool_set_true:N \l_xpatch_tracing_bool
1224 }
1225 \xpatch_main_zero:Nc \cs_show:N { blx@bbx@#2 }
1226 \IfBooleanT{#1}
1227 {
1228 \group_end:
1229 }
1230 }
1231 \NewDocumentCommand{\xpretobibdriver} { +m +m +m +m }
1232 { \exp_args:Nc \xpatch_pretocmd:Nnnn {blx@bbx@#1} {#2}{#3}{#4} }
1233 \NewDocumentCommand{\xapptobibdriver} { +m +m +m +m }
1234 { \exp_args:Nc \xpatch_apptocmd:Nnnn {blx@bbx@#1} {#2}{#3}{#4} }
1235 \NewDocumentCommand{\xpatchbibdriver} { s +m +m +m +m +m }
1236 {
1237 \IfBooleanTF{#1}
1238 { \exp_args:Nc \xpatch_patchcmd_all:Nnnnn {blx@bbx@#2} {#3}{#4}{#5}{#6} }
1239 { \exp_args:Nc \xpatch_patchcmd_once:Nnnnn {blx@bbx@#2} {#3}{#4}{#5}{#6} }
1240 }
1241 \NewDocumentCommand{\regexpatchbibdriver} { s +m +m +m +m +m }
1242 {
1243 \IfBooleanTF{#1}
1244 { \exp_args:Nc \xpatch_regexpatchcmd_all:Nnnnn {blx@bbx@#2} {#3}{#4}{#5}{#6} }
1245 { \exp_args:Nc \xpatch_regexpatchcmd_once:Nnnnn {blx@bbx@#2} {#3}{#4}{#5}{#6} }
1246 }
1247 % \end{macrocode}
1248 % \changes{v0.2c}{2018/03/31}{Fixed wrong function name in \texttt{\protect\string\protect\xpatchbibmacro}}
1249 % \changes{v0.2a}{2015/05/20}{Fixed silly mistake}
1250 % Other \pkg{biblatex} related macros, added by request of the
1251 % maintainers.
1252 % \changes{v0.2}{2013/03/28}{Additional biblatex related macros}
1253 % \begin{macrocode}
1254 \NewDocumentCommand{\xshowfieldformat} { s O{*} +m }
1255 {
1256 \IfBooleanT{#1}
1257 {
1258 \group_begin:
1259 \bool_set_true:N \l_xpatch_tracing_bool
1260 }
1261 \xpatch_main_zero:Nc \cs_show:N { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} }
1262 \IfBooleanT{#1}
1263 {
1264 \group_end:
1265 }
1266 }
1267 \NewDocumentCommand{\xpretofieldformat} { s O{*} +m +m +m +m }
1268 {
1269 \IfBooleanTF{#1}
1270 {
1271 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1272 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1273 }
1274 {
1275 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1276 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1277 }
1278 }
1279 \NewDocumentCommand{\xapptofieldformat} { s O{*} +m +m +m +m }
1280 {
1281 \IfBooleanTF{#1}
1282 {
1283 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1284 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1285 }
1286 {
1287 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1288 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1289 }
1290 }
1291 \NewDocumentCommand{\xpatchfieldformat} { s O{*} +m +m +m +m +m }
1292 {
1293 \IfBooleanTF{#1}
1294 {
1295 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1296 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1297 }
1298 {
1299 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1300 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1301 }
1302 }
1303 \NewDocumentCommand{\regexpatchfieldformat} { s O{*} +m +m +m +m +m }
1304 {
1305 \IfBooleanTF{#1}
1306 {
1307 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1308 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1309 }
1310 {
1311 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1312 { abx@ffd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1313 }
1314 }
1315
1316 \NewDocumentCommand{\xshownameformat} { s O{*} +m }
1317 {
1318 \IfBooleanT{#1}
1319 {
1320 \group_begin:
1321 \bool_set_true:N \l_xpatch_tracing_bool
1322 }
1323 \xpatch_main_zero:Nc \cs_show:N { abx@nfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} }
1324 \IfBooleanT{#1}
1325 {
1326 \group_end:
1327 }
1328 }
1329 \NewDocumentCommand{\xpretonameformat} { s O{*} +m +m +m +m }
1330 {
1331 \IfBooleanTF{#1}
1332 {
1333 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1334 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1335 }
1336 {
1337 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1338 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1339 }
1340 }
1341 \NewDocumentCommand{\xapptonameformat} { s O{*} +m +m +m +m }
1342 {
1343 \IfBooleanTF{#1}
1344 {
1345 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1346 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1347 }
1348 {
1349 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1350 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1351 }
1352 }
1353 \NewDocumentCommand{\xpatchnameformat} { s O{*} +m +m +m +m +m }
1354 {
1355 \IfBooleanTF{#1}
1356 {
1357 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1358 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1359 }
1360 {
1361 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1362 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1363 }
1364 }
1365 \NewDocumentCommand{\regexpatchnameformat} { s O{*} +m +m +m +m +m }
1366 {
1367 \IfBooleanTF{#1}
1368 {
1369 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1370 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1371 }
1372 {
1373 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1374 { abx@nfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1375 }
1376 }
1377
1378 \NewDocumentCommand{\xshowlistformat} { s O{*} +m }
1379 {
1380 \IfBooleanT{#1}
1381 {
1382 \group_begin:
1383 \bool_set_true:N \l_xpatch_tracing_bool
1384 }
1385 \xpatch_main_zero:Nc \cs_show:N { abx@lfd@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} }
1386 \IfBooleanT{#1}
1387 {
1388 \group_end:
1389 }
1390 }
1391 \NewDocumentCommand{\xpretolistformat} { s O{*} +m +m +m +m }
1392 {
1393 \IfBooleanTF{#1}
1394 {
1395 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1396 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1397 }
1398 {
1399 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1400 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1401 }
1402 }
1403 \NewDocumentCommand{\xapptolistformat} { s O{*} +m +m +m +m }
1404 {
1405 \IfBooleanTF{#1}
1406 {
1407 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1408 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1409 }
1410 {
1411 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1412 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1413 }
1414 }
1415 \NewDocumentCommand{\xpatchlistformat} { s O{*} +m +m +m +m +m }
1416 {
1417 \IfBooleanTF{#1}
1418 {
1419 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1420 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1421 }
1422 {
1423 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1424 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1425 }
1426 }
1427 \NewDocumentCommand{\regexpatchlistformat} { s O{*} +m +m +m +m +m }
1428 {
1429 \IfBooleanTF{#1}
1430 {
1431 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1432 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1433 }
1434 {
1435 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1436 { abx@lfd@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1437 }
1438 }
1439
1440 \NewDocumentCommand{\xshowindexfieldformat} { s O{*} +m }
1441 {
1442 \IfBooleanT{#1}
1443 {
1444 \group_begin:
1445 \bool_set_true:N \l_xpatch_tracing_bool
1446 }
1447 \xpatch_main_zero:Nc \cs_show:N { abx@fid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} }
1448 \IfBooleanT{#1}
1449 {
1450 \group_end:
1451 }
1452 }
1453 \NewDocumentCommand{\xpretoindexfieldformat} { s O{*} +m +m +m +m }
1454 {
1455 \IfBooleanTF{#1}
1456 {
1457 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1458 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1459 }
1460 {
1461 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1462 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1463 }
1464 }
1465 \NewDocumentCommand{\xapptoindexfieldformat} { s O{*} +m +m +m +m }
1466 {
1467 \IfBooleanTF{#1}
1468 {
1469 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1470 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1471 }
1472 {
1473 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1474 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1475 }
1476 }
1477 \NewDocumentCommand{\xpatchindexfieldformat} { s O{*} +m +m +m +m +m }
1478 {
1479 \IfBooleanTF{#1}
1480 {
1481 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1482 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1483 }
1484 {
1485 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1486 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1487 }
1488 }
1489 \NewDocumentCommand{\regexpatchindexfieldformat} { s O{*} +m +m +m +m +m }
1490 {
1491 \IfBooleanTF{#1}
1492 {
1493 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1494 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1495 }
1496 {
1497 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1498 { abx@fid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1499 }
1500 }
1501
1502 \NewDocumentCommand{\xshowindexnameformat} { s O{*} +m }
1503 {
1504 \IfBooleanT{#1}
1505 {
1506 \group_begin:
1507 \bool_set_true:N \l_xpatch_tracing_bool
1508 }
1509 \xpatch_main_zero:Nc \cs_show:N { abx@nid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} }
1510 \IfBooleanT{#1}
1511 {
1512 \group_end:
1513 }
1514 }
1515 \NewDocumentCommand{\xpretoindexnameformat} { s O{*} +m +m +m +m }
1516 {
1517 \IfBooleanTF{#1}
1518 {
1519 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1520 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1521 }
1522 {
1523 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1524 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1525 }
1526 }
1527 \NewDocumentCommand{\xapptoindexnameformat} { s O{*} +m +m +m +m }
1528 {
1529 \IfBooleanTF{#1}
1530 {
1531 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1532 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1533 }
1534 {
1535 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1536 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1537 }
1538 }
1539 \NewDocumentCommand{\xpatchindexnameformat} { s O{*} +m +m +m +m +m }
1540 {
1541 \IfBooleanTF{#1}
1542 {
1543 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1544 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1545 }
1546 {
1547 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1548 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1549 }
1550 }
1551 \NewDocumentCommand{\regexpatchindexnameformat} { s O{*} +m +m +m +m +m }
1552 {
1553 \IfBooleanTF{#1}
1554 {
1555 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1556 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1557 }
1558 {
1559 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1560 { abx@nid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1561 }
1562 }
1563
1564 \NewDocumentCommand{\xshowindexlistformat} { s O{*} +m }
1565 {
1566 \IfBooleanT{#1}
1567 {
1568 \group_begin:
1569 \bool_set_true:N \l_xpatch_tracing_bool
1570 }
1571 \xpatch_main_zero:Nc \cs_show:N { abx@lid@ \tl_to_str:n {#1} @ \tl_to_str:n {#2} }
1572 \IfBooleanT{#1}
1573 {
1574 \group_end:
1575 }
1576 }
1577 \NewDocumentCommand{\xpretoindexlistformat} { s O{*} +m +m +m +m }
1578 {
1579 \IfBooleanTF{#1}
1580 {
1581 \xpatch_main_three:Ncnnn \xpatch_pretocmd_all:Nnnn
1582 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1583 }
1584 {
1585 \xpatch_main_three:Ncnnn \xpatch_pretocmd_once:Nnnn
1586 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1587 }
1588 }
1589 \NewDocumentCommand{\xapptoindexlistformat} { s O{*} +m +m +m +m }
1590 {
1591 \IfBooleanTF{#1}
1592 {
1593 \xpatch_main_three:Ncnnn \xpatch_apptocmd_all:Nnnn
1594 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1595 }
1596 {
1597 \xpatch_main_three:Ncnnn \xpatch_apptocmd_once:Nnnn
1598 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}
1599 }
1600 }
1601 \NewDocumentCommand{\xpatchindexlistformat} { s O{*} +m +m +m +m +m }
1602 {
1603 \IfBooleanTF{#1}
1604 {
1605 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_all:Nnnnn
1606 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1607 }
1608 {
1609 \xpatch_main_four:Ncnnnn \xpatch_patchcmd_once:Nnnnn
1610 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1611 }
1612 }
1613 \NewDocumentCommand{\regexpatchindexlistformat} { s O{*} +m +m +m +m +m }
1614 {
1615 \IfBooleanTF{#1}
1616 {
1617 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_all:Nnnnn
1618 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1619 }
1620 {
1621 \xpatch_main_four:Ncnnnn \xpatch_regexpatchcmd_once:Nnnnn
1622 { abx@lid@ \tl_to_str:n {#2} @ \tl_to_str:n {#3} } {#4}{#5}{#6}{#7}
1623 }
1624 }
1625 % \end{macrocode}
1626 % A macro to check if the macro is patchable. It just prints a message
1627 % on the terminal and in the log file.
1628 % \begin{macrocode}
1629 \NewDocumentCommand{\checkpatchable}{ +m }
1630 {
1631 \group_begin:
1632 \bool_set_true:N \l_xpatch_tracing_bool
1633 \xpatch_check_patchable:N #1
1634 \group_end:
1635 }
1636 % \end{macrocode}
1637 % The last user level command: a macro for changing the optional
1638 % argument in a macro that has one.
1639 % \begin{macrocode}
1640 \cs_generate_variant:Nn \xpatch_get_all:N {c}
1641 \cs_generate_variant:Nn \xpatch_rebuild:N {c}
1642 \NewDocumentCommand{\xpatchoptarg}{ +m +m }
1643 {
1644 \xpatch_main_check:N #1
1645 \bool_if:NTF \l_xpatch_optional_bool
1646 {
1647 % \end{macrocode}
1648 % We have a macro with optional argument; so we strip off
1649 % the first backslash from the name and proceed.
1650 % \begin{macrocode}
1651 \tl_set:Nx \l_xpatch_name_tl { \tl_tail:V \l_xpatch_name_tl }
1652 % \end{macrocode}
1653 % Gather the prefix (it is |\protected| when |#1| has been defined
1654 % with |\newrobustcmd|).
1655 % \begin{macrocode}
1656 \tl_set:Nx \l_xpatch_prefix_tl { \cs_prefix_spec:N #1 }
1657 \tl_clear:N \l_xpatch_prefix_tl
1658 \tl_set_rescan:Nnx \l_xpatch_prefix_tl { } \l_xpatch_prefix_tl
1659 % \end{macrocode}
1660 % Get the replacement text in tokenized form: the control sequences
1661 % have spaces in their names, so we can't rely on
1662 % |\cs_replacement_spec:N| because the spaces would be lost.
1663 % \begin{macrocode}
1664 \tl_set_eq:Nc \l_xpatch_replacement_tl { \l_xpatch_name_tl }
1665 % \end{macrocode}
1666 % Now we have to change the last item in the token list: we just store
1667 % the new optional argument in a token list variable and do a regex
1668 % substitution, based on the fact that the replacement text consists
1669 % of control sequences, an open brace, the optional argument and a
1670 % closed brace, so we anchor at the end of the token list.
1671 % \begin{macrocode}
1672 \tl_set:Nn \l_tmpa_tl { { #2 } }
1673 \regex_replace_once:nnN { \cB. .* \cE. \Z} { \u{l_tmpa_tl} }
1674 \l_xpatch_replacement_tl
1675 % \end{macrocode}
1676 % Now we rebuild the control sequence.
1677 % \begin{macrocode}
1678 \xpatch_rebuild:c { \l_xpatch_name_tl }
1679 }
1680 % \end{macrocode}
1681 % If the macro hasn't an optional argument we issue a message.
1682 % \begin{macrocode}
1683 {
1684 \group_begin:
1685 \bool_set_true:N \l_xpatch_tracing_bool
1686 \xpatch_message:n
1687 {
1688 Macro~`\token_to_str:N #1'~ has~no~optional~argument~
1689 or~it~has~been~defined~with~`xparse'~and~operating~
1690 on~such~commands~is~(still)~not~supported
1691 }
1692 \group_end:
1693 }
1694 }
1695 % \end{macrocode}
1696 % Just one more thing: enabling or disabling the tracing system.
1697 % \begin{macrocode}
1698 \NewDocumentCommand{\tracingxpatches}{ O{1} }
1699 {
1700 \int_compare:nTF { #1 > 0 }
1701 { \bool_set_true:N \l_xpatch_tracing_bool }
1702 { \bool_set_false:N \l_xpatch_tracing_bool }
1703 }
1704 % \end{macrocode}
1705 % One more thing: patching the parameter text!
1706 % \begin{macrocode}
1707 \NewDocumentCommand{\xpatchparametertext}{ +m +m +m +m +m }
1708 {
1709 \xpatch_check_patchable:N #1
1710 \bool_if:NTF \l_xpatch_patchable_bool
1711 {
1712 \regex_replace_once:nnN { #2 } { #3 } \l_xpatch_arg_tl
1713 \xpatch_rebuild:N #1
1714 #4
1715 }
1716 {
1717 #5
1718 }
1719 }
1720 % \end{macrocode}
1721 %
1722 % \iffalse
1723 %</package>
1724 % \fi
1725 % \end{implementation}
1726 %
1727 % \PrintIndex
1728 % \PrintChanges