Debian Codegraph @

texmf-dist/source/latex/mdwtools/doafter.dtx

raw
1 % \begin{meta-comment}
2 %
3 % $Id: doafter.dtx,v 1.2 1996/11/19 20:49:08 mdw Exp $
4 %
5 % Insert tokens to be read after a group has been processed
6 %
7 % (c) 1996 Peter Schmitt and Mark Wooding
8 %
9 %----- Revision history -----------------------------------------------------
10 %
11 % $Log: doafter.dtx,v $
12 % Revision 1.2 1996/11/19 20:49:08 mdw
13 % Entered into RCS
14 %
15 %
16 % \end{meta-comment}
17 %
18 % \begin{meta-comment} <general public licence>
19 %%
20 %% doafter package -- insert a token really after a group
21 %% Copyright (c) 1996 Peter Schmitt and Mark Wooding
22 %<*package>
23 %%
24 %% This program is free software; you can redistribute it and/or modify
25 %% it under the terms of the GNU General Public License as published by
26 %% the Free Software Foundation; either version 2 of the License, or
27 %% (at your option) any later version.
28 %%
29 %% This program is distributed in the hope that it will be useful,
30 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
31 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 %% GNU General Public License for more details.
33 %%
34 %% You should have received a copy of the GNU General Public License
35 %% along with this program; if not, write to the Free Software
36 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 %</package>
38 %%
39 % \end{meta-comment}
40 %
41 % \begin{meta-comment} <Package preamble>
42 %<+latex2e>\NeedsTeXFormat{LaTeX2e}
43 %<+latex2e>\ProvidesPackage{doafter}
44 %<+latex2e> [1996/05/08 1.2 Aftergroup hacking (PS/MDW)]
45 % \end{meta-comment}
46 %
47 % \CheckSum{259}
48 %\iffalse
49 %<*package>
50 %\fi
51 %% \CharacterTable
52 %% {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
53 %% 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
54 %% Digits \0\1\2\3\4\5\6\7\8\9
55 %% Exclamation \! Double quote \" Hash (number) \#
56 %% Dollar \$ Percent \% Ampersand \&
57 %% Acute accent \' Left paren \( Right paren \)
58 %% Asterisk \* Plus \+ Comma \,
59 %% Minus \- Point \. Solidus \/
60 %% Colon \: Semicolon \; Less than \<
61 %% Equals \= Greater than \> Question mark \?
62 %% Commercial at \@ Left bracket \[ Backslash \\
63 %% Right bracket \] Circumflex \^ Underscore \_
64 %% Grave accent \` Left brace \{ Vertical bar \|
65 %% Right brace \} Tilde \~}
66 %%
67 %\iffalse
68 %</package>
69 %\fi
70 %
71 % \begin{meta-comment} <driver>
72 %
73 %<*driver>
74 \input{mdwtools}
75 \describespackage{doafter}
76 \author{Peter Schmitt\thanks{%
77 Peter came up with the basic implementation after I posed the problem
78 in the \texttt{comp.text.tex} newsgroup. I fixed some really piddly little
79 things, to improve it a bit, wrote the documentation, and turned the code
80 into a nice \package{doc}ced package. Then Peter gave me an updated
81 version, and I upgraded this from memory. Then he gave me some more tweaks
82 which I haven't incorporated.}
83 \and Mark Wooding}
84 \def\author#1{}
85 \mdwdoc
86 %</driver>
87 %
88 % \end{meta-comment}
89 %
90 % \section{Description}
91 %
92 % \subsection{What it's all about}
93 %
94 % \DescribeMacro{\doafter}
95 % It's common for the \TeX\ primitive |\aftergroup| to be used to `tidy up'
96 % after a group. For example, \LaTeX's colour handling uses this to insert
97 % appropriate |\special|s when the scope of a colour change ends. This
98 % causes several problems, though; for example, extra grouping must be added
99 % within boxes to ensure that the |\special|s don't `leak' out of their box
100 % and appear in odd places in the document. \LaTeX\ usually solves this
101 % problem by reading the box contents as an argument, although this isn't
102 % particularly desirable. The |\doafter| macro provided here will solve the
103 % problem in a different way, by allowing a macro to regain control after
104 % all the |\aftergroup| things have been processed.
105 %
106 % The macro works like this:
107 % \begin{grammar}
108 % <doafter-cmd> ::= \[[
109 % "\\doafter" <token> <group>
110 % \]]
111 % \end{grammar}
112 % The \<token> can be any token you like, except an explicit braces, since
113 % it's read as an undelimited macro argument. The \<group> is a normal
114 % \TeX\ group, surrounded by either implicit or explicit braces, or by
115 % |\begingroup| and |\endgroup| tokens. Once the final closing token of the
116 % \<group> is read, and any tokens saved up by |\aftergroup| have been
117 % processed, the \<token> is inserted and processed. Under normal
118 % circumstances, this will be a macro.
119 %
120 % There are some subtle problems with the current implementation, which you
121 % may need to be aware of:
122 %
123 % \begin{itemize}
124 %
125 % \item Since we're inserting things after all the |\aftergroup| tokens,
126 % those tokens might read something they're not expecting if they try
127 % to look ahead at the text after the group (e.g., with |\futurelet|).
128 % This is obviously totally unavoidable.
129 %
130 % \item Implicit braces (like |\bgroup| and |\egroup|) inserted using
131 % |\aftergroup| may be turned into \emph{explicit} $|{|_1$ and $|}|_2$
132 % characters within a |\doafter| group. This can cause probems under
133 % very specialised circumstances. The names |\bgroup| and |\egroup|
134 % are treated specially, and they will work normally (remaining as
135 % implicit braces). This should minimise problems caused by this
136 % slight difference. (This only applies to the last |\aftergroup|
137 % token in a group.)
138 %
139 % \item To handle the |\aftergroup| tokens properly, |\doafter| has to insert
140 % some |\aftergroup| tokens of its own. It will then process the
141 % other tokens some more, and set them up to be read again. This does
142 % mean that after the group ends, some assignments and other `stomach
143 % operations' will be performed, which may cause problems in
144 % alignments and similar places.
145 %
146 % \end{itemize}
147 %
148 %
149 % \subsection{Package options}
150 %
151 % There are a fair few \textsf{docstrip} options provided by this packge:
152 %
153 % \begin{description}
154 % \item [driver] extracts the documentation driver. This isn't usually
155 % necessary.
156 % \item [package] extracts the code as a standalone package, formatted for
157 % either \LaTeXe\ or Plain~\TeX.
158 % \item [latex2e] inserts extra identification code for a \LaTeXe\ package.
159 % \item [plain] inserts some extra code for a Plain \TeX\ package.
160 % \item [macro] just extracts the raw code, for inclusion in another package.
161 % \item [test] extracts some code for testing the current implementation.
162 % \end{description}
163 %
164 %
165 % \implementation
166 %
167 % \section{Implementation}
168 %
169 % \subsection{The main macro}
170 %
171 % We start outputting code here. If this is a Plain~\TeX\ package, we must
172 % make \lit{@} into a letter.
173 %
174 % \begin{macrocode}
175 %<*macro|package>
176 %<+plain>\catcode`\@=11
177 % \end{macrocode}
178 %
179 % \begin{macro}{\doafter}
180 %
181 % The idea is to say \syntax{"\\doafter" <token> <group>} and expect the
182 % \synt{token} to be processed after the group has finished its stuff,
183 % even if it contains |\aftergroup| things. My eternal gratitude goes to
184 % Peter Schmitt, who came up with most of the solution implemented here;
185 % I've just tidied up some very minor niggles and things later.
186 %
187 % Let's start with some preamble. I'll save the (hopefully) primitive
188 % |\aftergroup| in a different token.
189 %
190 % \begin{macrocode}
191 \let\@@aftergroup\aftergroup
192 % \end{macrocode}
193 %
194 % Now to define the `user' interface. It takes a normal undelimited
195 % argument, although this must be a single token; otherwise eveything will
196 % go wrong. It assumes that the token following is some kind of group
197 % opening thing (an explicit or implicit character with catcode~1, or
198 % a |\begingroup| token). To make this work, I'll save the token,
199 % together with an |\@@aftergroup| (to save an |\expandafter| later) in
200 % a temporary macro which no-one will mind me using, and then look ahead at
201 % the beginning-group token.
202 %
203 % \begin{macrocode}
204 \def\doafter#1{%
205 \def\@tempa{\@@aftergroup#1}%
206 \afterassignment\doafter@i\let\@let@token%
207 }
208 % \end{macrocode}
209 %
210 % I now have the token in |\@let@token|, so I'll put that in. I'll then
211 % make |\aftergroup| do my thing rather than the normal thing, and queue
212 % the tokens |\@prepare@after| and the |\doafter| argument for later use.
213 %
214 % \begin{macrocode}
215 \def\doafter@i{%
216 \@let@token%
217 \let\aftergroup\@my@aftergroup%
218 \@@aftergroup\@prepare@after\@tempa%
219 }
220 % \end{macrocode}
221 %
222 % \end{macro}
223 %
224 % \begin{macro}{\@my@aftergroup}
225 %
226 % Now the cleverness begins. We keep two macros (Peter's original used
227 % count registers) which keep counts of the numbers of |\aftergroup|s,
228 % both locally and globally. Let's call the local counter~$n$ and the
229 % global one $N$. Every time we get a call to our |\aftergroup| hack,
230 % we set~$n := n+1$ and~$N := n$, and leave the token given to us for later
231 % processing. When we actually process an |\aftergroup| token properly,
232 % set~$N := N-1$ to indicate that it's been handled; when they're all done,
233 % we'll have $N=n$, which is exactly what we'd have if there weren't any
234 % to begin with.
235 %
236 % \begin{macrocode}
237 \def\ag@cnt@local{0 }
238 \let\ag@cnt@global\ag@cnt@local
239 % \end{macrocode}
240 %
241 % Now we come to the definition of my version of |\aftergroup|. I'll just
242 % add the token |\@after@token| before every |\aftergroup| token I find.
243 % This means there's two calls to |\aftergroup| for every one the user makes,
244 % but these things aren't all that common, so it's OK really. I'll also
245 % bump the local counter, and synchronise them.
246 %
247 % \begin{macrocode}
248 \def\@my@aftergroup{%
249 \begingroup%
250 \count@\ag@cnt@local%
251 \advance\count@\@ne%
252 \xdef\ag@cnt@global{\the\count@\space}%
253 \endgroup%
254 \let\ag@cnt@local\ag@cnt@global%
255 \@@aftergroup\@after@token\@@aftergroup%
256 }
257 % \end{macrocode}
258 %
259 % \end{macro}
260 %
261 % Now what does |\@after@token| we inserted above actually do? Well, this
262 % is more exciting. There are actually two different variants of the
263 % macro, which are used at different times.
264 %
265 % \begin{macro}{\@after@token}
266 %
267 % The default |\@after@token| starts a group, which will `catch'
268 % |\aftergroup| tokens which I throw at it. I put the two counters into
269 % some scratch count registers. (There's a slight problem here: Plain \TeX\
270 % only gives us one. For the sake of evilness I'll use |\clubpenalty| as the
271 % other one. Eeeek.) I then redefine |\@after@token| to the second
272 % variant, and execute it. The |\@start@after@group| macro starts the
273 % group, because this code is shared with |\@prepare@after| below.
274 %
275 % \begin{macrocode}
276 \def\@after@token{%
277 \@start@after@group%
278 \@after@token%
279 }
280 \def\@start@after@group{%
281 \begingroup%
282 \count@\ag@cnt@global%
283 \clubpenalty\ag@cnt@local%
284 \let\@after@token\@after@token@i%
285 }
286 % \end{macrocode}
287 %
288 % \end{macro}
289 %
290 % \begin{macro}{\@after@token@i}
291 %
292 % I have $|\count@| = N$ and $|\@tempcnta| = n$. I'll decrement~$N$,
293 % and if I have $N = n$, I know that this is the last token to do, so I
294 % must insert an |\@after@all| after the token. This will close the group,
295 % and maybe insert the original |\doafter| token if appropriate.
296 %
297 % \begin{macrocode}
298 \def\@after@token@i{%
299 \advance\count@\m@ne%
300 \ifnum\count@=\clubpenalty%
301 \global\let\ag@cnt@global\ag@cnt@local%
302 \expandafter\@after@aftertoken\expandafter\@after@all%
303 \else%
304 \expandafter\@@aftergroup%
305 \fi%
306 }
307 % \end{macrocode}
308 %
309 % Finally, establish a default meaning for |\@after@all|.
310 %
311 % \begin{macrocode}
312 \let\@after@all\endgroup
313 % \end{macrocode}
314 %
315 % \end{macro}
316 %
317 % \begin{macro}{\@prepare@after}
318 %
319 % If this group is handled by |\doafter|, then the first |\aftergroup| token
320 % isn't |\@after@token|; it's |\@prepare@after|.
321 %
322 % There are some extra cases to deal with:
323 % \begin{itemize}
324 % \item If $N=n$ then there were no |\aftergroup| tokens, so we have an easy
325 % job. I'll just let the token do its stuff directly.
326 % \item Otherwise, $N>n$, and there are |\aftergroup| tokens. I'll open
327 % the group, and let |\@after@token| do all the handling.
328 % \end{itemize}
329 %
330 % \begin{macrocode}
331 \def\@prepare@after{%
332 \ifx\ag@cnt@local\ag@cnt@global\else%
333 \expandafter\@prepare@after@i%
334 \fi%
335 }
336 \def\@prepare@after@i#1{%
337 \@start@after@group%
338 \def\@after@all{\@@aftergroup#1\endgroup}%
339 }
340 % \end{macrocode}
341 %
342 % \end{macro}
343 %
344 % \begin{macro}{\@after@aftertoken}
345 %
346 % This is where all the difficulty lies. The next token in the stream is
347 % an |\aftergroup| one, which could be more or less anything. We have an
348 % argument, which is some code to do \emph{after} the token has been
349 % |\aftergroup|ed.
350 %
351 % If the token is anything other than a brace (i.e., an explicit character
352 % of category~1 or~2) then I have no problem; I can scoop up the token with
353 % an undelimited macro argument. But the only way I can decide if this token
354 % is a brace (nondestructively) is with |\futurelet|, which makes the token
355 % implicit, so I can't decide whether it's really dangerous.
356 %
357 % There is a possible way of doing this\footnote{Due to Peter Schmitt,
358 % again.} which relates to nobbling the offending token with |\string| and
359 % sifting through the results. The problem here involves scooping up all the
360 % tokens of a |\string|ed control sequence, which may turn out to be
361 % `|\csname\endcsname|' or something equally horrid.
362 %
363 % The solution I've used is much simpler: I'll change |\bgroup| and |\egroup|
364 % to stop them from being implicit braces before comparing.
365 %
366 % \begin{macrocode}
367 \def\@after@aftertoken#1{%
368 \let\bgroup\relax\let\egroup\relax%
369 \toks@{#1}%
370 \futurelet\@let@token\@after@aftertoken@i%
371 }
372 \def\@after@aftertoken@i{%
373 \ifcat\noexpand\@let@token{%
374 \@@aftergroup{%
375 \else\ifcat\noexpand\@let@token}%
376 \@@aftergroup}%
377 \else%
378 \def\@tempa##1{\@@aftergroup##1\the\toks@}%
379 \expandafter\expandafter\expandafter\@tempa%
380 \fi\fi%
381 }
382 % \end{macrocode}
383 %
384 % \end{macro}
385 %
386 %
387 % Phew!
388 %
389 % \begin{macrocode}
390 %<+plain>\catcode`\@=12
391 %</macro|package>
392 % \end{macrocode}
393 %
394 % \subsection{Test code}
395 %
396 % The following code gives |\doafter| a bit of a testing. It's based on
397 % the test suite I gave to comp.text.tex, although it's been improved a
398 % little since then.
399 %
400 % The first thing to do is define a control sequence with an \lit{@} sign
401 % in its name, so we can test catcode changes. This also hides an
402 % |\aftergroup| within a macro, making life more difficult for prospective
403 % implementations.
404 %
405 % \begin{macrocode}
406 %<*test>
407 \catcode`\@=11
408 \def\at@name{\aftergroup\saynine}
409 \def\saynine{\say{ix}}
410 \catcode`\@=12
411 % \end{macrocode}
412 %
413 % Now define a command to write a string to the terminal. The name will
414 % probably be familiar to REXX hackers.
415 %
416 % \begin{macrocode}
417 \def\say{\immediate\write16}
418 % \end{macrocode}
419 %
420 % Test one: This is really easy; it just tests that the thing works at all.
421 % If your implementation fails this, it's time for a major rethink.
422 %
423 % \begin{macrocode}
424 \say{Test one... (1--2)}
425 \def\saytwo{\say{ii}}
426 \doafter\saytwo{\say{i}}
427 % \end{macrocode}
428 %
429 % Test two: Does |\aftergroup| work?
430 %
431 % \begin{macrocode}
432 \say{Test two... (1--4)}
433 \def\saythree{\say{iii}}
434 \def\sayfour{\say{iv}}
435 \doafter\sayfour{\say{i}\aftergroup\saythree\say{ii}}
436 % \end{macrocode}
437 %
438 % Test three: Test braces and |\iffalse| working as they should. Several
439 % proposed solutions based on |\write|ing the group to a file get upset by
440 % this test, although I forgot to include it in the torture test. It also
441 % tests whether literal braces can be |\aftergroup|ed properly. (Added a new
442 % test here, making sure that |\bgroup| is left as an implicit token.)
443 %
444 % \begin{macrocode}
445 \say{Test three... (1--4, `\string\bgroup', 5)}
446 \def\sayfive{\say{v}}
447 \doafter\sayfive{%
448 \say{i}%
449 \aftergroup\say%
450 \aftergroup{%
451 \aftergroup\romannumeral\aftergroup3%
452 \aftergroup}%
453 \iffalse}\fi%
454 \aftergroup\def%
455 \aftergroup\sayfouretc%
456 \aftergroup{%
457 \aftergroup\say%
458 \aftergroup{%
459 \aftergroup i%
460 \aftergroup v%
461 \aftergroup}%
462 \aftergroup\say%
463 \aftergroup{%
464 \aftergroup\string%
465 \aftergroup\bgroup%
466 \aftergroup}%
467 \aftergroup}%
468 \aftergroup\sayfouretc%
469 \say{ii}%
470 }
471 % \end{macrocode}
472 %
473 % Test four: Make sure the implementation isn't leaking things. This just
474 % makes sure that |\aftergroup| is its normal reasonable self.
475 %
476 % \begin{macrocode}
477 \say{Test four... (1--3)}
478 {\say{i}\aftergroup\saythree\say{ii}}
479 % \end{macrocode}
480 %
481 % Test five: Nesting, aftergroup, catcodes, grouping. This is the `torture'
482 % test I gave to comp.text.tex, slightly corrected (oops) and amended. It
483 % ensures that nested groups and |\doafter|s work properly (the latter is
484 % actually more likely than might be imagined).
485 %
486 % \begin{macrocode}
487 \say{Test five... (1--14)}
488 \def\sayten{\say{x}}
489 \def\saythirteen{\say{xiii}}
490 \def\sayfourteen{\say{xiv}}
491 \doafter\sayfourteen\begingroup%
492 \say{i}%
493 {\say{ii}\aftergroup\sayfour\say{iii}}%
494 \def\saynum{\say{viii}}%
495 \doafter\sayten{%
496 \say{v}%
497 \def\saynum{\say{vii}}%
498 \catcode`\@=11%
499 \aftergroup\saynum%
500 \say{vi}%
501 \at@name%
502 \saynum%
503 }%
504 \say{xi}%
505 \aftergroup\saythirteen%
506 \say{xii}%
507 \endgroup
508 \end
509 %</test>
510 % \end{macrocode}
511 %
512 % That's it. All present and correct.
513 %
514 % \Finale
515 %
516 \endinput