| 1 |
%\iffalse This is patch.doc, and it's -*-LaTeX-*- text |
| 2 |
%% patch.doc/patch.tex |
| 3 |
%% (c) 1994 by David Kastrup ([email protected]) |
| 4 |
%% Version 0.91 |
| 5 |
%% |
| 6 |
% This work may be distributed and/or modified under the |
| 7 |
% conditions of the LaTeX Project Public License, either |
| 8 |
% version 1.3c of this license or (at your option) any later |
| 9 |
% version. The latest version of this license is in |
| 10 |
% http://www.latex-project.org/lppl.txt |
| 11 |
% and version 1.3c or later is part of all distributions of |
| 12 |
% LaTeX version 2005/12/01 or later. |
| 13 |
% |
| 14 |
% This work has the LPPL maintenance status `not maintained'. |
| 15 |
% |
| 16 |
% The work consists of the source file patch.doc |
| 17 |
% and the derived file patch.tex |
| 18 |
% |
| 19 |
% This file is provided marked up according to an early version of LaTeX |
| 20 |
% `doc' format markup. Unfortunatly, docstrip for that version of doc is |
| 21 |
% no longer available; therefore stripping is to be done by removing all |
| 22 |
% lines starting with a single percent sign. |
| 23 |
% You may extract a driver file for printing the documentation with |
| 24 |
% the doc style package by extracting all lines starting with |
| 25 |
% %<+driver>, while removing the %<+driver> directive. |
| 26 |
%\fi |
| 27 |
% \CheckSum{360} |
| 28 |
% \title{Using {\tt patch.tex}} |
| 29 |
% \author{David Kastrup\\[email protected]} |
| 30 |
% \date{January 22, 1994} |
| 31 |
% \maketitle |
| 32 |
% \MakeShortVerb\" |
| 33 |
% \begin{abstract} |
| 34 |
% Often one has the need to use a preestablished format, such |
| 35 |
% as~\LaTeX, but with some slight changes to some of the macros. |
| 36 |
% Of~course, it is possible to just copy the offendin maco |
| 37 |
% definitions into your application or style file, and~modify them |
| 38 |
% there. This can lead to parts of your macros being from different |
| 39 |
% versions of \LaTeX{} resp.~\TeX. |
| 40 |
% |
| 41 |
% This input file is a tool for replacing just parts of existing macros |
| 42 |
% by user defined parts. If those parts are no longer recognizable, |
| 43 |
% an~error message is produced, calling the user to~attention. |
| 44 |
% \end{abstract} |
| 45 |
% \tableofcontents |
| 46 |
% \section{User Interface} |
| 47 |
% The macros defined here will allow ``patching'' up already existing |
| 48 |
% macros. A~typical example, included from a~style file used for |
| 49 |
% typesetting Greek text (to~appear in "kdgreek") is the following: |
| 50 |
%\begin{verbatim} |
| 51 |
%\input patch.tex |
| 52 |
%\ifx\descriptionlabel\undefined\else |
| 53 |
%\gpatch\descriptionlabel#1{{#1}}{% |
| 54 |
% \bf {\grbf}} \fi |
| 55 |
%\ifx\@part\undefined\else |
| 56 |
%\gpatch\@part[#1]#2{[#1]{#2}}{% |
| 57 |
% \bf\partname~{\grbf\partname\nobreak\ }% |
| 58 |
% \bf {\grbf}} \fi |
| 59 |
%... |
| 60 |
%\endpatch |
| 61 |
%\end{verbatim} |
| 62 |
%This shows almost all features that are really there. You start by |
| 63 |
%inputting "patch.tex". This, among other things, opens a~local |
| 64 |
%group in which all of the definitions of~"patch.tex" are read~in. |
| 65 |
%In~order not to use up unnecessary hash~space, almost all local |
| 66 |
%commands are in the form~"\^^80" and upwards. For this reason, |
| 67 |
%you cannot {\em use\/} any macros of yours with such names |
| 68 |
%(Note: they are {\em |
| 69 |
%not\/} active characters, but rather regular one-letter control |
| 70 |
%sequences), and you should not patch them, but you can patch any |
| 71 |
%macros containing them, or patch them into macros. |
| 72 |
% |
| 73 |
%One macro is {\em very\/} special and must not appear in any macro |
| 74 |
%you patch, nor in any arguments to~"\patch": this is~"\@ndp@tch". |
| 75 |
% |
| 76 |
%\DescribeMacro{\patch} |
| 77 |
%\DescribeMacro{\gpatch} |
| 78 |
%You follow the "\input" by several "\patch" or "\gpatch" |
| 79 |
%commands. These commands |
| 80 |
%consist of several parts: |
| 81 |
%\begin{itemize} |
| 82 |
%\item[Optional "\bslash long"] for definition of macros possibly containing |
| 83 |
% "\par" tokens in their arguments. Note that the prefixes "\global" |
| 84 |
% and~"\outer" are not supported. "\global"ness is achieved by~usage |
| 85 |
% of~"\gpatch" as opposed to~"\patch", and is explained later, and |
| 86 |
% "\outer" macros are detestable. |
| 87 |
%\item ["\bslash patch" or "\bslash gpatch"] To be expected. |
| 88 |
%\item [\meta{Argument list}] which is the argument list the macro will |
| 89 |
% have in the patched version (usually the same as the original one). |
| 90 |
% It~is terminated by the opening brace of |
| 91 |
%\item [\meta{Argument replacement}] which is enclosed in braces. This |
| 92 |
% is fed as an argument list into the old macro, in order to get the |
| 93 |
% replacement text for the new macro. Basically this means that you |
| 94 |
% will have to use, say, "{#1}" for an undelimited parameter~"#1", |
| 95 |
% "#1@" for a delimited parameter "#1@", and "@$#!?!!" for a brace |
| 96 |
% delimited parameter "#1#{" (don't try patching any macros like |
| 97 |
% that. Presume you have never heard of~them). |
| 98 |
%\item [\meta{Substitution list}] This contains a~brace enclosed list |
| 99 |
% of~replacements. Each replacement consists of |
| 100 |
% \begin{itemize} |
| 101 |
% \item [\meta{Matching sequence}] which is a sequence of tokens not |
| 102 |
% containing any braces. If really need to match braces, you can |
| 103 |
% match by using implicit braces you get by saying something like |
| 104 |
% "\let\openbrace={". If you think of using the default implicit |
| 105 |
% braces "\bgroup" and~"\egroup", forget~it. They are used in |
| 106 |
% several macros we might want to~patch, and thus are disabled |
| 107 |
% by~default. |
| 108 |
% \item [\meta{Replacement sequence}] which is enclosed in~braces. |
| 109 |
% Again, if you want braces to appear in your replacement, you can |
| 110 |
% make that happen by using implicit braces. Matched braces, |
| 111 |
% however, you may include explicitly. |
| 112 |
% \end{itemize} |
| 113 |
%\end{itemize} |
| 114 |
%\DescribeMacro{\endpatch} |
| 115 |
%Then the patches are followed by~"\endpatch", which closes the group |
| 116 |
%opened by~"\input", thus freeing up almost all \TeX{} space |
| 117 |
%"patch.tex" took up. Since~mismatched patches can be hard to |
| 118 |
%diagnose, "\endpatch" is defined as being~"\outer". As~long as |
| 119 |
%the "\outer" nuisance prevails, we~might use it for what it~is |
| 120 |
%useful~for. |
| 121 |
% |
| 122 |
%That's all there is to it! Except for the "\global" issue we promised |
| 123 |
%to explain\dots{} |
| 124 |
%Usually you will be patching macros of some style, and there will be no |
| 125 |
%point in using~"\patch" instead of~"\gpatch". So~mostly "\gpatch" does |
| 126 |
%no harm, and will cause a~global definition. "\patch", on the other |
| 127 |
%hand, has the problem that its definitions should hold in the group |
| 128 |
%ended by~"\endpatch", but not globally. So~"\patch" cannot just make |
| 129 |
%the definitions. Instead, it collects the new definitions, and~does |
| 130 |
%them all at~"\endpatch". |
| 131 |
% |
| 132 |
%\DescribeMacro{\globaldefs} |
| 133 |
%Here is one friendly feature: If "\globaldefs" is negative, |
| 134 |
%"\gpatch"~will invoke "\patch" instead of its own behaviour, so that |
| 135 |
%local patches will be performed instead of none at~all (since the then |
| 136 |
%non-global definition would disappear at~"\endpatch"). This feature was |
| 137 |
%deemed desirable\footnote{Reason: |
| 138 |
%I~made an example file showing the effects of several |
| 139 |
%"\bslash documentstyle" commands with my Greek macro set. The Greek style |
| 140 |
%patched up some \LaTeX{} macros, and this was wanted only |
| 141 |
%locally, namely for the length of the demonstration of one particular |
| 142 |
%document style.}. |
| 143 |
%\subsection{Things to beware of (aka bugs)} |
| 144 |
%"patch.doc" is a very simple package (sort~of). It can fail for |
| 145 |
%a~number of reasons, and is not applicable in every situation. You |
| 146 |
%will have problems if |
| 147 |
%\begin{enumerate} |
| 148 |
%\item You use implicit tokens which are implicit at the time the |
| 149 |
% patches are applied. "\bgroup" and~"\egroup" are explicitly disabled |
| 150 |
% for this reason. Implicit spaces will be regarded as explicit ones |
| 151 |
% by~"\patch". Implicit macro parameter characters I~am not so sure |
| 152 |
% about. \TeX~behaves pretty wierd with regard to them anyhow. Other |
| 153 |
% implicits should do no~harm. "\patch"~will take the problematic |
| 154 |
% implicit |
| 155 |
% tokens for explicit ones while it is analyzing tokens. It analyzes |
| 156 |
% both matching and replacement tokens, and all tokens in the original |
| 157 |
% macro up to the last replacement performed. This means that, for |
| 158 |
% example, matching implicit braces in locations where you did not |
| 159 |
% intend it can lead to mismatched explicit braces and lots |
| 160 |
% of~confusion. |
| 161 |
%\item You use outer tokens in any respect. |
| 162 |
% "\patch" does not care for them. |
| 163 |
%\item When "\@ndp@tch" appears anywhere, namely in a~macro to be |
| 164 |
% patched, or in a matching or replacement sequence. It~is used for |
| 165 |
% internal purposes, and you can cause lots of confusion when |
| 166 |
% using~it. This means, for one thing, that you should not try |
| 167 |
% "\patch"ing up any macros of this package with itself. |
| 168 |
%\item And this is harsh: if the macro you patch contains "#" marks |
| 169 |
% (which originally were produced by writing "##") these will not be |
| 170 |
% reduplicated, leading to missing parameter number errors (which will |
| 171 |
% do the right thing if you ignore them, but that's ugly), or to |
| 172 |
% recombination with a following number, which is plain wrong. You can |
| 173 |
% work around this ugliness in an~ugly way: specify a~replacement |
| 174 |
% "#{##}" for any such token. I~am pretty much afraid I~know no |
| 175 |
% better way around this one. |
| 176 |
%\item Since "\bgroup" and~"\egroup" are disabled, using any macro that |
| 177 |
% in turn uses those will not work while in patch~mode. What |
| 178 |
% I~guarantee to keep working in future versions is "\let" (locally, |
| 179 |
% for making implicit tokens), |
| 180 |
% all expandable constructs (like conditionals) not |
| 181 |
% using any of the macros of this package, and~"\patch"es. |
| 182 |
% Other things are not necessarily supported in patch~mode. |
| 183 |
% Specifically, do not use "\def" or~"\gdef", since manipulation of |
| 184 |
% localness might unnecessarily be thwarted if you~do. |
| 185 |
%\item Do not change "\toks@" in any way, or use a~macro changing it. |
| 186 |
% Cannot happen really, if you listen to the last advice. |
| 187 |
%\item Do not rely on the patches either taking place or not before the |
| 188 |
% final "\endpatch"! This means, do not use a~patched macro before |
| 189 |
% "\endpatch" in ways which require specifically the patched or |
| 190 |
% the unpatched version. For example, do not patch one and the same macro |
| 191 |
% with two separate "\patch" commands in the same patch session! The |
| 192 |
% reason for this is the collecting behaviour for non-global patches. |
| 193 |
% And you never really know when a~package of yours might come handy |
| 194 |
% non-globally. |
| 195 |
%\item For similar reasons, |
| 196 |
% do not use "\input patch.tex" in a local group when you are doing |
| 197 |
% "\gpatch"es (unless you know what you are doing, as in the case of |
| 198 |
% negative~"\globaldefs"). It~might get to~being a~problem sometime. |
| 199 |
%\item Never set "\globaldefs" to a~non-zero {\em positive\/} value |
| 200 |
% while working with~"\patch", since it will interfere disastrously with |
| 201 |
% "patch.tex"'s internals, which heavily work with local groups. |
| 202 |
% Anyway, "patch.tex" is by its nature intended for local use only. |
| 203 |
%\item Just as a note aside: it is a~very bad idea to set "\globaldefs" |
| 204 |
% to non-zero values longer than strictly necessary. It is especially |
| 205 |
% a~bad idea to set it to a~negative value while {\em typesetting\/} |
| 206 |
% anything, |
| 207 |
% because the output routine (implicitly enclosed in a~group) will |
| 208 |
% never be able to get anything done permanently, and will therefore |
| 209 |
% be repeated over and~over. |
| 210 |
%\end{enumerate} |
| 211 |
% |
| 212 |
%Ok, this should be all there really is to it. If I~look that list over |
| 213 |
%again, it seems like groups really occupy my fantasy. |
| 214 |
% |
| 215 |
%Happy "\patch"ing! |
| 216 |
%\StopEventually{\PrintIndex} |
| 217 |
%\section{The Implementation} |
| 218 |
%We begin by opening our local group and making private control |
| 219 |
%sequences accessible: |
| 220 |
% \begin{macrocode} |
| 221 |
\begingroup |
| 222 |
\catcode`@=11 |
| 223 |
% \end{macrocode} |
| 224 |
%We then set "\toks@" to the initial set of~actions to~perform at the |
| 225 |
%end of the~patches. |
| 226 |
% \begin{macrocode} |
| 227 |
\toks@={\endgroup} |
| 228 |
% \end{macrocode} |
| 229 |
% \begin{macro}{\endpatch} |
| 230 |
% Rather simple. "\outer"~seems not to do much harm here. It might |
| 231 |
% help better pinpointing severe failures when using~"\patch". |
| 232 |
% \begin{macrocode} |
| 233 |
\outer\def\endpatch{\the\toks@} |
| 234 |
% \end{macrocode} |
| 235 |
% \end{macro} |
| 236 |
% \begin{macro}{\futuredef} |
| 237 |
% This is somewhat similar to "\futurelet", only not at~all. It |
| 238 |
% defines the macro which is its first argument as~code which can |
| 239 |
% by use of~"\aftergroup" reproduce the third argument~token, |
| 240 |
% and after doing that it will call the second argument. |
| 241 |
% All~arguments are removed from the input, unlike "\futurelet"'s |
| 242 |
% behaviour. |
| 243 |
% \begin{macrocode} |
| 244 |
\def\futuredef#1#2{\begingroup |
| 245 |
% \end{macrocode} |
| 246 |
% We do this locally, and set up a few things to do after the |
| 247 |
% group. Since a~"\def" is going to finish what we want, we can |
| 248 |
% insert the ``do-after'' token via "\afterassignment". |
| 249 |
% \begin{macrocode} |
| 250 |
\aftergroup\afterassignment \aftergroup#2% |
| 251 |
\aftergroup\def \aftergroup#1% |
| 252 |
\futurelet\next\^^90} |
| 253 |
% \end{macrocode} |
| 254 |
% \end{macro} |
| 255 |
% We still have the token in our input stream, but also a copy of |
| 256 |
% its meaning in "\next". We first try out some cases which will |
| 257 |
% not allow us to treat the next token as an undelimited macro |
| 258 |
% parameter. Of course we cannot distinguish explicit from implicit |
| 259 |
% tokens, but we rely on the user to explicitly unimplicate possibly |
| 260 |
% implicit characters. We will do this ourselves with the following |
| 261 |
% two ones: |
| 262 |
% \begin{macrocode} |
| 263 |
\let\bgroup\relax \let\egroup\relax |
| 264 |
% \end{macrocode} |
| 265 |
% Now we start checking all problematic cases. |
| 266 |
% "\^^90" will substitute an appropriate definition given |
| 267 |
% explicitly instead of the following token. If we can, however, we |
| 268 |
% rather use the token we get. If you wonder about this name, here |
| 269 |
% is why: we have to use hosts of macros because of various |
| 270 |
% "\futurelet"-like constructions. Patches occur just in a local group, |
| 271 |
% and should not permanently require resources. If we did not use |
| 272 |
% one-letter control sequences here, the hash table size would be |
| 273 |
% somewhat reduced, permanently, because entries never leave the |
| 274 |
% hash table. One letter control sequences, however, are not |
| 275 |
% hashed, and can disappear without a trace again. |
| 276 |
% \begin{macrocode} |
| 277 |
\def\^^90{% |
| 278 |
\ifcat\noexpand\next{% |
| 279 |
\^^91{\aftergroup{\iffalse}\fi}% |
| 280 |
\else\ifcat\noexpand\next}% |
| 281 |
\^^91{\iffalse{\fi\aftergroup}}% |
| 282 |
\else\ifcat\noexpand\next\space |
| 283 |
\^^91{\expandafter\aftergroup\space}% |
| 284 |
\else\ifcat\noexpand\next## |
| 285 |
\^^91{\aftergroup########}% |
| 286 |
\else |
| 287 |
\let\next\^^93% |
| 288 |
\fi\fi\fi\fi\next} |
| 289 |
% \end{macrocode} |
| 290 |
% "\^^91" is rather simple. We save the definition in |
| 291 |
% "\next@", and we'll call upon this after disposing of the next |
| 292 |
% token. |
| 293 |
% \begin{macrocode} |
| 294 |
\def\^^91#1{\def\next@{\endgroup{#1}}% |
| 295 |
\let\next\^^92} |
| 296 |
\def\^^92{\afterassignment\next@ \let\next= } |
| 297 |
% \end{macrocode} |
| 298 |
% "\^^93" will actively get the next token, and produce an |
| 299 |
% "\aftergroup" definition from that. It is "\long" in case we are |
| 300 |
% dealing with a "\par" token. |
| 301 |
% \begin{macrocode} |
| 302 |
\long\def\^^93#1{\endgroup{\aftergroup#1}} |
| 303 |
% \end{macrocode} |
| 304 |
% \begin{macro}{\@ndp@tch} |
| 305 |
% This is supposed to be used as a delimiter. So that we may |
| 306 |
% check for its appearance conveniently, we define it as its own |
| 307 |
% "\futuredef" expansion. |
| 308 |
% \begin{macrocode} |
| 309 |
\def\@ndp@tch{\aftergroup\@ndp@tch} |
| 310 |
% \end{macrocode} |
| 311 |
% \end{macro} |
| 312 |
% \begin{macro}{\gpatch} |
| 313 |
% This is it. If you are in doubt as to what the "\def\next{}" is |
| 314 |
% supposed to~achieve, this is~easy. We want to detect whether you |
| 315 |
% want a~long macro to be~patched. In that case, you say |
| 316 |
% "\long\gpatch"\dots. We can figure this out by comparing |
| 317 |
% "\next" to the non-long~"\empty". Of course, we do not |
| 318 |
% explicitly check for "\outer"ness. You should not even think of |
| 319 |
% wanting to make an "\outer" definition. |
| 320 |
% |
| 321 |
% We start by defining~"\gpatch", which will in the case |
| 322 |
% of~disabled "global"ness revert to~"\patch". Both~"\patch" |
| 323 |
% and~"\gpatch" define "\next" as a~macro getting one |
| 324 |
% "\@ndp@tch"-delimited argument: |
| 325 |
% it is all what should appear after a~"\def". |
| 326 |
% "\next"~should make all necessary actions for doing |
| 327 |
% the~definition. After setting~up "\next", "\^^a0"~is called, for |
| 328 |
% which we wrap up the argument list as a~separate argument. |
| 329 |
% \begin{macrocode} |
| 330 |
\def\gpatch{\ifnum\globaldefs<\z@ |
| 331 |
\expandafter\patch |
| 332 |
\else |
| 333 |
\expandafter\^^98% |
| 334 |
\fi} |
| 335 |
\long\def\^^98#1#2#{\def\next{}% |
| 336 |
\long\edef\next##1\@ndp@tch% |
| 337 |
{\ifx\next\empty\else\long\fi\gdef##1}% |
| 338 |
\^^a0#1{#2}} |
| 339 |
% \end{macrocode} |
| 340 |
% \end{macro} |
| 341 |
% \begin{macro}{\patch} |
| 342 |
% "\patch"~is a~bit more complicated, because we need to~make |
| 343 |
% additions to the current "\toks@"~list. |
| 344 |
% \begin{macrocode} |
| 345 |
\def\patch#1#2#{\def\next{}% |
| 346 |
\long\edef\next##1\@ndp@tch |
| 347 |
{\toks@ \noexpand\expandafter {\noexpand\the \toks@ |
| 348 |
\ifx\next\empty\else\long\fi \def##1}}% |
| 349 |
\^^a0#1{#2}} |
| 350 |
% \end{macrocode} |
| 351 |
% \end{macro} |
| 352 |
% |
| 353 |
% Now the real fun starts. We get the argument replacement list, |
| 354 |
% and the macro body replacement list. We define a~macro~"\^^e0" |
| 355 |
% (the "e" means error) just for knowing the proper macro name for |
| 356 |
% error messages. |
| 357 |
% |
| 358 |
% We prepare in a local |
| 359 |
% "\toks@" the definition (because we could not simply |
| 360 |
% "\aftergroup" the argument replacement, which could consist of |
| 361 |
% any number of~tokens). Note that we start yet another group, |
| 362 |
% making the definitions of "\toks@" and "\next" stable. |
| 363 |
% |
| 364 |
% \begin{macrocode} |
| 365 |
\long\def\^^a0#1#2#3#4{\begingroup |
| 366 |
\def\^^e0{#1}% |
| 367 |
\toks@{\endgroup\next#1#2}% |
| 368 |
\begingroup |
| 369 |
\aftergroup\the\aftergroup\toks@ |
| 370 |
% \end{macrocode} |
| 371 |
% We collect the replacement in a continually shrinking token list. |
| 372 |
% Do not get annoyed, but we'll use "\toks@" for that, too. |
| 373 |
% When we are through with replacements, we'll say "\endgroup", |
| 374 |
% after which the definition takes place. |
| 375 |
% |
| 376 |
% The replacement needs |
| 377 |
% to be in a token list instead of a macro in order not to break upon |
| 378 |
% encountering hash marks. The use of "\toks@" for this does not |
| 379 |
% conflict with its other uses due to the grouping. |
| 380 |
% |
| 381 |
% After that, we are ready to start the fun by expanding our macro, |
| 382 |
% and putting~"\p@check" to~work. |
| 383 |
% \begin{macrocode} |
| 384 |
\toks@{#4}% |
| 385 |
\expandafter\p@check\expandafter{#1#3}\@ndp@tch} |
| 386 |
% \end{macrocode} |
| 387 |
% \begin{macro}{\p@match} |
| 388 |
% \begin{macro}{\p@replace} |
| 389 |
% These are token lists holding the current string to be matched |
| 390 |
% next, and its replacement string. We |
| 391 |
% use token lists~1 and~2 for that. |
| 392 |
% \begin{macrocode} |
| 393 |
\toksdef\p@match=\@ne |
| 394 |
\toksdef\p@replace=\tw@ |
| 395 |
% \end{macrocode} |
| 396 |
% \end{macro} |
| 397 |
% \end{macro} |
| 398 |
% \begin{macro}{\p@check} |
| 399 |
% We check for any remaining replacements. If there are none, we |
| 400 |
% may quit. "\toks@"~is the list of |
| 401 |
% remaining substitutions. "\endgroup"~stops and makes the |
| 402 |
% definition (check~"\^^a0" to~see~why), "\^^a1"~continues. By the |
| 403 |
% way, in case of an~error, we will later use~"\next@" in order to |
| 404 |
% tell what replacements were not carried out. |
| 405 |
% \begin{macrocode} |
| 406 |
\def\p@check{\edef\next@{\the\toks@}% |
| 407 |
\ifx\empty\next@ \expandafter\endgroup |
| 408 |
\else \expandafter\^^a1\fi} |
| 409 |
% \end{macrocode} |
| 410 |
% \end{macro} |
| 411 |
% \begin{macro}{\p@first} |
| 412 |
% This sets "\p@first" to the "\futuredef" expansion of the first |
| 413 |
% token we want to~match. "\^^a2"~will rewrap the rest of~"\toks@", |
| 414 |
% and needs an (as~to~yet) unmatched closing brace for that. |
| 415 |
% It~will provide the matching open one by~itself. |
| 416 |
% \begin{macrocode} |
| 417 |
\def\^^a1{\iffalse{\fi |
| 418 |
\expandafter\futuredef \expandafter\p@first |
| 419 |
\expandafter\^^a2\the\toks@}} |
| 420 |
% \end{macrocode} |
| 421 |
% \end{macro} |
| 422 |
% Ok, we rewrap the rest to be matched into~"\p@match", and the |
| 423 |
% replacement into~"\p@replace", after which we continue with~"\^^a3". |
| 424 |
% \begin{macrocode} |
| 425 |
\def\^^a2#1#{\p@match{#1}\afterassignment\^^a3\p@replace} |
| 426 |
% \end{macrocode} |
| 427 |
% The remaining substitutions we repack into~"\toks@". Now we need to |
| 428 |
% insert the matching opening brace for the closing one we put~before. |
| 429 |
% \begin{macrocode} |
| 430 |
\def\^^a3{\afterassignment\^^a4\toks@\expandafter{\iffalse}\fi} |
| 431 |
% \end{macrocode} |
| 432 |
% We now get the first (or next) token of the original macro. |
| 433 |
% \begin{macrocode} |
| 434 |
\def\^^a4{\futuredef\next\^^a5} |
| 435 |
% \end{macrocode} |
| 436 |
% If this token matches~"\p@first", we check the rest in~"\^^a6". We |
| 437 |
% collect in~"\toks@" all tokens matched so far (excluding "\p@first"), |
| 438 |
% so that we can |
| 439 |
% reinsert them if the match cannot be completed successfully. This~is |
| 440 |
% done in a~separate group. If~"\next" turns out to |
| 441 |
% be~"\@ndp@tch", however, |
| 442 |
% this is an~error indication. It~means that the end of patchable |
| 443 |
% material was reached. We call then~"\^^e1", which quits in that |
| 444 |
% case in a somewhat unusual (but relatively fast) way, reinserting |
| 445 |
% the gotten~"\@ndp@tch". |
| 446 |
% |
| 447 |
% It does not do any harm that "\next" is contained in conditional |
| 448 |
% context. Coming from~"\futuredef", it cannot be "\let" to an |
| 449 |
% unmatched conditional, and will correctly be~skipped. |
| 450 |
% \begin{macrocode} |
| 451 |
\def\^^a5{\ifx\next\p@first |
| 452 |
\begingroup \toks@{}\expandafter\^^a6% |
| 453 |
\else\ifx\next\@ndp@tch \^^e1\fi |
| 454 |
\next \expandafter \^^a4% |
| 455 |
\fi} |
| 456 |
% \end{macrocode} |
| 457 |
% In case of an error, we give the original macro (which has not yet |
| 458 |
% been patched). And at definition time, we will show how the macro |
| 459 |
% looks like afterwards. |
| 460 |
% \begin{macrocode} |
| 461 |
\def\^^e1{\let\^^a4\endgroup |
| 462 |
\newlinechar`\^^J |
| 463 |
\toks@\expandafter{\next@}% |
| 464 |
\errorcontextlines\m@ne |
| 465 |
\errhelp{I am ignoring inapplicable patches. Go on.}% |
| 466 |
\errmessage{When trying to patch |
| 467 |
\expandafter\string\^^e0, which was defined as^^J% |
| 468 |
\expandafter\meaning\^^e0^^J% |
| 469 |
the following patches remained inapplicable:^^J% |
| 470 |
\the\toks@^^J% |
| 471 |
I will show the results of the succeeded patches as soon^^J% |
| 472 |
as I can without further comment, so do not be surprised.}% |
| 473 |
\aftergroup\show \expandafter\aftergroup\^^e0% |
| 474 |
} |
| 475 |
% \end{macrocode} |
| 476 |
% Ok, we look whether there are any tokens remaining to be matched. If |
| 477 |
% there are not, replacement can go~forth. In~that case, we end our |
| 478 |
% local group, and go over to~"\^^ac". If~there are, we continue |
| 479 |
% with~"\^^a7". |
| 480 |
% \begin{macrocode} |
| 481 |
\def\^^a6{\edef\next{\the\p@match}% |
| 482 |
\ifx\next\empty \endgroup\expandafter\^^ac% |
| 483 |
\else \expandafter\^^a7\fi} |
| 484 |
% \end{macrocode} |
| 485 |
% We get the next token to be matched into~"\next@" from~"\p@match". |
| 486 |
% We~will have to adjust "\p@match" again with a token list |
| 487 |
% reassignment. For this we need to insert an unmatched closing brace |
| 488 |
% after its expansion, which will again be matched later. |
| 489 |
% \begin{macrocode} |
| 490 |
\def\^^a7{\iffalse{\fi |
| 491 |
\expandafter\futuredef\expandafter\next@ |
| 492 |
\expandafter\^^a8\the\p@match}} |
| 493 |
% \end{macrocode} |
| 494 |
% Ok, here we gather the rest into~"\p@match" and~continue. |
| 495 |
% \begin{macrocode} |
| 496 |
\def\^^a8{\afterassignment\^^a9\p@match\expandafter{\iffalse}\fi} |
| 497 |
% \end{macrocode} |
| 498 |
% Now we get the next token from the original macro into~"\next". |
| 499 |
% \begin{macrocode} |
| 500 |
\def\^^a9{\futuredef\next\^^aa} |
| 501 |
% \end{macrocode} |
| 502 |
% In case of a match, we continue with~"\^^ab". Otherwise, if~we have |
| 503 |
% reached the illegal end of~macro ("\next" set to~"\@ndp@tch"), |
| 504 |
% we~stop. |
| 505 |
% If~we have no match legally, we reinsert~"\p@first", continue |
| 506 |
% scanning with~"\^^a4", reinserting everything matched with the help |
| 507 |
% of the "\futuredef" generated "\aftergroup"-sequence in~"\toks@". |
| 508 |
% This will hapen also if "\@ndp@tch" indicates something went |
| 509 |
% haywire, because "\@ndp@tch" cannot be in a~match sequence (if you |
| 510 |
% do not put it there, and you are not allowed to). |
| 511 |
% this we do also in the case of error, as you can easily see. |
| 512 |
% \begin{macrocode} |
| 513 |
\def\^^aa{\ifx\next@\next |
| 514 |
\expandafter\^^ab% |
| 515 |
\else \aftergroup\p@first |
| 516 |
\aftergroup\^^a4% |
| 517 |
\the\toks@\next\expandafter\endgroup\fi} |
| 518 |
% \end{macrocode} |
| 519 |
% We now have to append the "\aftergroup" sequence of the next matched |
| 520 |
% token in~"\next" to~"\toks@". After that, we can loop. |
| 521 |
% \begin{macrocode} |
| 522 |
\def\^^ab{\toks@\expandafter\expandafter\expandafter{% |
| 523 |
\expandafter\the\expandafter\toks@\next}% |
| 524 |
\^^a6} |
| 525 |
% \end{macrocode} |
| 526 |
% Ok, here follows the code if a~match {\em was\/} completed. We |
| 527 |
% insert the tokens of~"\p@replace". We~have to scan "\p@replace" |
| 528 |
% token by~token, so that implicit braces work like in the match |
| 529 |
% code sequences. We do this until "\p@replace" is exhausted. Which we |
| 530 |
% can detect by finding "\@ndp@tch". |
| 531 |
% \begin{macrocode} |
| 532 |
\def\^^ac{\expandafter\^^ad\the\p@replace\@ndp@tch} |
| 533 |
% \end{macrocode} |
| 534 |
% The following analysis loop is about as straight as it can get. |
| 535 |
% \begin{macrocode} |
| 536 |
\def\^^ad{\futuredef\next\^^ae} |
| 537 |
\def\^^ae{\ifx\next\@ndp@tch |
| 538 |
\expandafter\p@check |
| 539 |
\else |
| 540 |
\next \expandafter\^^ad% |
| 541 |
\fi} |
| 542 |
% \end{macrocode} |
| 543 |
% We end here by giving the driver file for the documentation, which |
| 544 |
% will typically be extracted using~"docstrip.sty". |
| 545 |
% \begin{macrocode} |
| 546 |
%<+driver>\documentstyle[doc]{article} |
| 547 |
%<+driver> \EnableCrossrefs |
| 548 |
%<+driver>% \DisableCrossrefs |
| 549 |
%<+driver> |
| 550 |
%<+driver> \CodelineIndex |
| 551 |
%<+driver>\begin{document} |
| 552 |
%<+driver> \DocInput{patch.doc} |
| 553 |
%<+driver>\end{document} |
| 554 |
% \end{macrocode} |
| 555 |
%\Finale |