5 % Insert tokens to be read after a group has been processed
7 % (c) 1996 Peter Schmitt and Mark Wooding
11 % \begin{meta-comment} <general public licence>
13 %% doafter package -- insert a token really after a group
14 %% Copyright (c) 1996 Peter Schmitt and Mark Wooding
17 %% This program is free software; you can redistribute it and/or modify
18 %% it under the terms of the GNU General Public License as published by
19 %% the Free Software Foundation; either version 2 of the License, or
20 %% (at your option) any later version.
22 %% This program is distributed in the hope that it will be useful,
23 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
24 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 %% GNU General Public License for more details.
27 %% You should have received a copy of the GNU General Public License
28 %% along with this program; if not, write to the Free Software
29 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 % \begin{meta-comment} <Package preamble>
35 %<+latex2e>\NeedsTeXFormat{LaTeX2e}
36 %<+latex2e>\ProvidesPackage{doafter}
37 %<+latex2e> [1996/05/08 1.2 Aftergroup hacking]
45 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
46 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
47 %% Digits \0\1\2\3\4\5\6\7\8\9
48 %% Exclamation \! Double quote \" Hash (number) \#
49 %% Dollar \$ Percent \% Ampersand \&
50 %% Acute accent \' Left paren \( Right paren \)
51 %% Asterisk \* Plus \+ Comma \,
52 %% Minus \- Point \. Solidus \/
53 %% Colon \: Semicolon \; Less than \<
54 %% Equals \= Greater than \> Question mark \?
55 %% Commercial at \@ Left bracket \[ Backslash \\
56 %% Right bracket \] Circumflex \^ Underscore \_
57 %% Grave accent \` Left brace \{ Vertical bar \|
58 %% Right brace \} Tilde \~}
64 % \begin{meta-comment} <driver>
68 \describespackage{doafter}
69 \author{Peter Schmitt\thanks{%
70 Peter came up with the basic implementation after I posed the problem
71 in the \texttt{comp.text.tex} newsgroup. I fixed some really piddly little
72 things, to improve it a bit, wrote the documentation, and turned the code
73 into a nice \package{doc}ced package. Then Peter gave me an updated
74 version, and I upgraded this from memory. Then he gave me some more tweaks
75 which I haven't incorporated.}
83 % \section{Description}
85 % \subsection{What it's all about}
87 % \DescribeMacro{\doafter}
88 % It's common for the \TeX\ primitive |\aftergroup| to be used to `tidy up'
89 % after a group. For example, \LaTeX's colour handling uses this to insert
90 % appropriate |\special|s when the scope of a colour change ends. This
91 % causes several problems, though; for example, extra grouping must be added
92 % within boxes to ensure that the |\special|s don't `leak' out of their box
93 % and appear in odd places in the document. \LaTeX\ usually solves this
94 % problem by reading the box contents as an argument, although this isn't
95 % particularly desirable. The |\doafter| macro provided here will solve the
96 % problem in a different way, by allowing a macro to regain control after
97 % all the |\aftergroup| things have been processed.
99 % The macro works like this:
101 % <doafter-cmd> ::= \[[
102 % "\\doafter" <token> <group>
105 % The \<token> can be any token you like, except an explicit braces, since
106 % it's read as an undelimited macro argument. The \<group> is a normal
107 % \TeX\ group, surrounded by either implicit or explicit braces, or by
108 % |\begingroup| and |\endgroup| tokens. Once the final closing token of the
109 % \<group> is read, and any tokens saved up by |\aftergroup| have been
110 % processed, the \<token> is inserted and processed. Under normal
111 % circumstances, this will be a macro.
113 % There are some subtle problems with the current implementation, which you
114 % may need to be aware of:
118 % \item Since we're inserting things after all the |\aftergroup| tokens,
119 % those tokens might read something they're not expecting if they try
120 % to look ahead at the text after the group (e.g., with |\futurelet|).
121 % This is obviously totally unavoidable.
123 % \item Implicit braces (like |\bgroup| and |\egroup|) inserted using
124 % |\aftergroup| may be turned into \emph{explicit} $|{|_1$ and $|}|_2$
125 % characters within a |\doafter| group. This can cause probems under
126 % very specialised circumstances. The names |\bgroup| and |\egroup|
127 % are treated specially, and they will work normally (remaining as
128 % implicit braces). This should minimise problems caused by this
129 % slight difference. (This only applies to the last |\aftergroup|
132 % \item To handle the |\aftergroup| tokens properly, |\doafter| has to insert
133 % some |\aftergroup| tokens of its own. It will then process the
134 % other tokens some more, and set them up to be read again. This does
135 % mean that after the group ends, some assignments and other `stomach
136 % operations' will be performed, which may cause problems in
137 % alignments and similar places.
142 % \subsection{Package options}
144 % There are a fair few \textsf{docstrip} options provided by this packge:
146 % \begin{description}
147 % \item [driver] extracts the documentation driver. This isn't usually
149 % \item [package] extracts the code as a standalone package, formatted for
150 % either \LaTeXe\ or Plain~\TeX.
151 % \item [latex2e] inserts extra identification code for a \LaTeXe\ package.
152 % \item [plain] inserts some extra code for a Plain \TeX\ package.
153 % \item [macro] just extracts the raw code, for inclusion in another package.
154 % \item [test] extracts some code for testing the current implementation.
160 % \section{Implementation}
162 % \subsection{The main macro}
164 % We start outputting code here. If this is a Plain~\TeX\ package, we must
165 % make \lit{@} into a letter.
169 %<+plain>\catcode`\@=11
172 % \begin{macro}{\doafter}
174 % The idea is to say \syntax{"\\doafter" <token> <group>} and expect the
175 % \synt{token} to be processed after the group has finished its stuff,
176 % even if it contains |\aftergroup| things. My eternal gratitude goes to
177 % Peter Schmitt, who came up with most of the solution implemented here;
178 % I've just tidied up some very minor niggles and things later.
180 % Let's start with some preamble. I'll save the (hopefully) primitive
181 % |\aftergroup| in a different token.
184 \let\@@aftergroup\aftergroup
187 % Now to define the `user' interface. It takes a normal undelimited
188 % argument, although this must be a single token; otherwise eveything will
189 % go wrong. It assumes that the token following is some kind of group
190 % opening thing (an explicit or implicit character with catcode~1, or
191 % a |\begingroup| token). To make this work, I'll save the token,
192 % together with an |\@@aftergroup| (to save an |\expandafter| later) in
193 % a temporary macro which no-one will mind me using, and then look ahead at
194 % the beginning-group token.
198 \def\@tempa{\@@aftergroup#1}%
199 \afterassignment\doafter@i\let\@let@token%
203 % I now have the token in |\@let@token|, so I'll put that in. I'll then
204 % make |\aftergroup| do my thing rather than the normal thing, and queue
205 % the tokens |\@prepare@after| and the |\doafter| argument for later use.
210 \let\aftergroup\@my@aftergroup%
211 \@@aftergroup\@prepare@after\@tempa%
217 % \begin{macro}{\@my@aftergroup}
219 % Now the cleverness begins. We keep two macros (Peter's original used
220 % count registers) which keep counts of the numbers of |\aftergroup|s,
221 % both locally and globally. Let's call the local counter~$n$ and the
222 % global one $N$. Every time we get a call to our |\aftergroup| hack,
223 % we set~$n := n+1$ and~$N := n$, and leave the token given to us for later
224 % processing. When we actually process an |\aftergroup| token properly,
225 % set~$N := N-1$ to indicate that it's been handled; when they're all done,
226 % we'll have $N=n$, which is exactly what we'd have if there weren't any
230 \def\ag@cnt@local{0 }
231 \let\ag@cnt@global\ag@cnt@local
234 % Now we come to the definition of my version of |\aftergroup|. I'll just
235 % add the token |\@after@token| before every |\aftergroup| token I find.
236 % This means there's two calls to |\aftergroup| for every one the user makes,
237 % but these things aren't all that common, so it's OK really. I'll also
238 % bump the local counter, and synchronise them.
241 \def\@my@aftergroup{%
243 \count@\ag@cnt@local%
245 \xdef\ag@cnt@global{\the\count@\space}%
247 \let\ag@cnt@local\ag@cnt@global%
248 \@@aftergroup\@after@token\@@aftergroup%
254 % Now what does |\@after@token| we inserted above actually do? Well, this
255 % is more exciting. There are actually two different variants of the
256 % macro, which are used at different times.
258 % \begin{macro}{\@after@token}
260 % The default |\@after@token| starts a group, which will `catch'
261 % |\aftergroup| tokens which I throw at it. I put the two counters into
262 % some scratch count registers. (There's a slight problem here: Plain \TeX\
263 % only gives us one. For the sake of evilness I'll use |\clubpenalty| as the
264 % other one. Eeeek.) I then redefine |\@after@token| to the second
265 % variant, and execute it. The |\@start@after@group| macro starts the
266 % group, because this code is shared with |\@prepare@after| below.
273 \def\@start@after@group{%
275 \count@\ag@cnt@global%
276 \clubpenalty\ag@cnt@local%
277 \let\@after@token\@after@token@i%
283 % \begin{macro}{\@after@token@i}
285 % I have $|\count@| = N$ and $|\@tempcnta| = n$. I'll decrement~$N$,
286 % and if I have $N = n$, I know that this is the last token to do, so I
287 % must insert an |\@after@all| after the token. This will close the group,
288 % and maybe insert the original |\doafter| token if appropriate.
291 \def\@after@token@i{%
292 \advance\count@\m@ne%
293 \ifnum\count@=\clubpenalty%
294 \global\let\ag@cnt@global\ag@cnt@local%
295 \expandafter\@after@aftertoken\expandafter\@after@all%
297 \expandafter\@@aftergroup%
302 % Finally, establish a default meaning for |\@after@all|.
305 \let\@after@all\endgroup
310 % \begin{macro}{\@prepare@after}
312 % If this group is handled by |\doafter|, then the first |\aftergroup| token
313 % isn't |\@after@token|; it's |\@prepare@after|.
315 % There are some extra cases to deal with:
317 % \item If $N=n$ then there were no |\aftergroup| tokens, so we have an easy
318 % job. I'll just let the token do its stuff directly.
319 % \item Otherwise, $N>n$, and there are |\aftergroup| tokens. I'll open
320 % the group, and let |\@after@token| do all the handling.
324 \def\@prepare@after{%
325 \ifx\ag@cnt@local\ag@cnt@global\else%
326 \expandafter\@prepare@after@i%
329 \def\@prepare@after@i#1{%
331 \def\@after@all{\@@aftergroup#1\endgroup}%
337 % \begin{macro}{\@after@aftertoken}
339 % This is where all the difficulty lies. The next token in the stream is
340 % an |\aftergroup| one, which could be more or less anything. We have an
341 % argument, which is some code to do \emph{after} the token has been
344 % If the token is anything other than a brace (i.e., an explicit character
345 % of category~1 or~2) then I have no problem; I can scoop up the token with
346 % an undelimited macro argument. But the only way I can decide if this token
347 % is a brace (nondestructively) is with |\futurelet|, which makes the token
348 % implicit, so I can't decide whether it's really dangerous.
350 % There is a possible way of doing this\footnote{Due to Peter Schmitt,
351 % again.} which relates to nobbling the offending token with |\string| and
352 % sifting through the results. The problem here involves scooping up all the
353 % tokens of a |\string|ed control sequence, which may turn out to be
354 % `|\csname\endcsname|' or something equally horrid.
356 % The solution I've used is much simpler: I'll change |\bgroup| and |\egroup|
357 % to stop them from being implicit braces before comparing.
360 \def\@after@aftertoken#1{%
361 \let\bgroup\relax\let\egroup\relax%
363 \futurelet\@let@token\@after@aftertoken@i%
365 \def\@after@aftertoken@i{%
366 \ifcat\noexpand\@let@token{%
368 \else\ifcat\noexpand\@let@token}%
371 \def\@tempa##1{\@@aftergroup##1\the\toks@}%
372 \expandafter\expandafter\expandafter\@tempa%
383 %<+plain>\catcode`\@=12
387 % \subsection{Test code}
389 % The following code gives |\doafter| a bit of a testing. It's based on
390 % the test suite I gave to comp.text.tex, although it's been improved a
393 % The first thing to do is define a control sequence with an \lit{@} sign
394 % in its name, so we can test catcode changes. This also hides an
395 % |\aftergroup| within a macro, making life more difficult for prospective
401 \def\at@name{\aftergroup\saynine}
402 \def\saynine{\say{ix}}
406 % Now define a command to write a string to the terminal. The name will
407 % probably be familiar to REXX hackers.
410 \def\say{\immediate\write16}
413 % Test one: This is really easy; it just tests that the thing works at all.
414 % If your implementation fails this, it's time for a major rethink.
417 \say{Test one... (1--2)}
418 \def\saytwo{\say{ii}}
419 \doafter\saytwo{\say{i}}
422 % Test two: Does |\aftergroup| work?
425 \say{Test two... (1--4)}
426 \def\saythree{\say{iii}}
427 \def\sayfour{\say{iv}}
428 \doafter\sayfour{\say{i}\aftergroup\saythree\say{ii}}
431 % Test three: Test braces and |\iffalse| working as they should. Several
432 % proposed solutions based on |\write|ing the group to a file get upset by
433 % this test, although I forgot to include it in the torture test. It also
434 % tests whether literal braces can be |\aftergroup|ed properly. (Added a new
435 % test here, making sure that |\bgroup| is left as an implicit token.)
438 \say{Test three... (1--4, `\string\bgroup', 5)}
439 \def\sayfive{\say{v}}
444 \aftergroup\romannumeral\aftergroup3%
448 \aftergroup\sayfouretc%
461 \aftergroup\sayfouretc%
466 % Test four: Make sure the implementation isn't leaking things. This just
467 % makes sure that |\aftergroup| is its normal reasonable self.
470 \say{Test four... (1--3)}
471 {\say{i}\aftergroup\saythree\say{ii}}
474 % Test five: Nesting, aftergroup, catcodes, grouping. This is the `torture'
475 % test I gave to comp.text.tex, slightly corrected (oops) and amended. It
476 % ensures that nested groups and |\doafter|s work properly (the latter is
477 % actually more likely than might be imagined).
480 \say{Test five... (1--14)}
482 \def\saythirteen{\say{xiii}}
483 \def\sayfourteen{\say{xiv}}
484 \doafter\sayfourteen\begingroup%
486 {\say{ii}\aftergroup\sayfour\say{iii}}%
487 \def\saynum{\say{viii}}%
490 \def\saynum{\say{vii}}%
498 \aftergroup\saythirteen%
505 % That's it. All present and correct.