| 1 |
% \begin{meta-comment} |
| 2 |
% |
| 3 |
% $Id: at.dtx,v 1.3 1996/11/19 20:46:55 mdw Exp $ |
| 4 |
% |
| 5 |
% Allow @-commands |
| 6 |
% |
| 7 |
% (c) 1995 Mark Wooding |
| 8 |
% |
| 9 |
%----- Revision history ----------------------------------------------------- |
| 10 |
% |
| 11 |
% $Log: at.dtx,v $ |
| 12 |
% Revision 1.3 1996/11/19 20:46:55 mdw |
| 13 |
% Entered into RCS |
| 14 |
% |
| 15 |
% |
| 16 |
% \end{meta-comment} |
| 17 |
% |
| 18 |
% \begin{meta-comment} <general public licence> |
| 19 |
%% |
| 20 |
%% at package -- support for `@' commands' |
| 21 |
%% Copyright (c) 1996 Mark Wooding |
| 22 |
%% |
| 23 |
%% This program is free software; you can redistribute it and/or modify |
| 24 |
%% it under the terms of the GNU General Public License as published by |
| 25 |
%% the Free Software Foundation; either version 2 of the License, or |
| 26 |
%% (at your option) any later version. |
| 27 |
%% |
| 28 |
%% This program is distributed in the hope that it will be useful, |
| 29 |
%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 |
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 |
%% GNU General Public License for more details. |
| 32 |
%% |
| 33 |
%% You should have received a copy of the GNU General Public License |
| 34 |
%% along with this program; if not, write to the Free Software |
| 35 |
%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 36 |
%% |
| 37 |
% \end{meta-comment} |
| 38 |
% |
| 39 |
% \begin{meta-comment} <Package preamble> |
| 40 |
%<+package>\NeedsTeXFormat{LaTeX2e} |
| 41 |
%<+package>\ProvidesPackage{at} |
| 42 |
%<+package> [1996/05/02 1.3 @-command support (MDW)] |
| 43 |
% \end{meta-comment} |
| 44 |
% |
| 45 |
% \CheckSum{355} |
| 46 |
%% \CharacterTable |
| 47 |
%% {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 |
| 48 |
%% 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 |
| 49 |
%% Digits \0\1\2\3\4\5\6\7\8\9 |
| 50 |
%% Exclamation \! Double quote \" Hash (number) \# |
| 51 |
%% Dollar \$ Percent \% Ampersand \& |
| 52 |
%% Acute accent \' Left paren \( Right paren \) |
| 53 |
%% Asterisk \* Plus \+ Comma \, |
| 54 |
%% Minus \- Point \. Solidus \/ |
| 55 |
%% Colon \: Semicolon \; Less than \< |
| 56 |
%% Equals \= Greater than \> Question mark \? |
| 57 |
%% Commercial at \@ Left bracket \[ Backslash \\ |
| 58 |
%% Right bracket \] Circumflex \^ Underscore \_ |
| 59 |
%% Grave accent \` Left brace \{ Vertical bar \| |
| 60 |
%% Right brace \} Tilde \~} |
| 61 |
%% |
| 62 |
% |
| 63 |
% \begin{meta-comment} <driver> |
| 64 |
% |
| 65 |
%<*driver> |
| 66 |
\input{mdwtools} |
| 67 |
\describespackage{at} |
| 68 |
\aton |
| 69 |
\atlet p=\package |
| 70 |
\atdef at{\package{at}} |
| 71 |
\atdef={\mbox{-}} |
| 72 |
\atdef-{@@@=} |
| 73 |
\atlet.=\syntax |
| 74 |
\mdwdoc |
| 75 |
%</driver> |
| 76 |
% |
| 77 |
% \end{meta-comment} |
| 78 |
% |
| 79 |
% \section{User guide} |
| 80 |
% |
| 81 |
% The @at\ package is an attempt to remove a lot of tedious typing that |
| 82 |
% ends up in \LaTeX\ documents, by expanding the number of short command |
| 83 |
% names available. The new command names begin with the `|@|' character, |
| 84 |
% rather than the conventional `|\|', so you can tell them apart. |
| 85 |
% |
| 86 |
% The package provides some general commands for defining @-commands, and |
| 87 |
% then uses them to define some fairly simple ones which will be useful to |
| 88 |
% most people. |
| 89 |
% |
| 90 |
% The rules for @-command names aren't terribly complex: |
| 91 |
% \begin{itemize} |
| 92 |
% \item If the first character of the name is a letter, then the command name |
| 93 |
% consists of all characters up to, but not including, the first |
| 94 |
% nonletter. Spaces following the command name are ignored. |
| 95 |
% \item If the first character of the name is a backslash, then the @-command |
| 96 |
% name consists of the control sequence introduced by the backslash. |
| 97 |
% \item Otherwise, the command name consists only of that first character. |
| 98 |
% Spaces following the name are not ignored, unless that character |
| 99 |
% was itself a space character. |
| 100 |
% \end{itemize} |
| 101 |
% |
| 102 |
% Usually, digits are not considered to be letters. However, the |
| 103 |
% \package{at} package will consider digits to be letters if you give it the |
| 104 |
% \textsf{digits} option in the |\usepackage| command. (Note that this |
| 105 |
% only affects the \package{at} package; it won't change the characters |
| 106 |
% allowed in normal command names.) |
| 107 |
% |
| 108 |
% \DescribeMacro{\atallowdigits} |
| 109 |
% \DescribeMacro{\atdisallowdigits} |
| 110 |
% You can enable and disable digits being considered as letters dynamically. |
| 111 |
% The |\atallowdigits| command allows digits to be used as letters; |
| 112 |
% |\atdisallowdigits| prevents this. Both declarations follow \LaTeX's |
| 113 |
% usual scoping rules. Both of these commands have corresponding |
| 114 |
% environments with the same names (without the leading `|\|', obviously). |
| 115 |
% |
| 116 |
% \subsection{Defining @-commands} |
| 117 |
% |
| 118 |
% \DescribeMacro{\newatcommand} |
| 119 |
% \DescribeMacro{\renewatcommand} |
| 120 |
% The |\newatcommand| command will define a new @-command using a syntax |
| 121 |
% similar to |\newcommand|. For example, you could define |
| 122 |
% \begin{listing} |
| 123 |
%\newatcommand c[1]{\chapter{#1}} |
| 124 |
% \end{listing} |
| 125 |
% to make @.{"@c{"<name>"}"} equivalent to @.{"\\chapter{"<name>"}"}. |
| 126 |
% |
| 127 |
% A |\renewatcommand| is also provided to redefine existing commands, should |
| 128 |
% the need arise. |
| 129 |
% |
| 130 |
% \DescribeMacro{\atdef} |
| 131 |
% For \TeX\ hackers, the |\atdef| command defines @-commands using a syntax |
| 132 |
% similar to \TeX's built-in |\def|. |
| 133 |
% |
| 134 |
% As an example, the following command makes @.{"@/"<text>"/"} write its |
| 135 |
% argument \<text> in italics: |
| 136 |
% \begin{listing} |
| 137 |
%\atdef/#1/{\textit{#1}} |
| 138 |
% \end{listing} |
| 139 |
% The real implementation of the |@/|\dots|/| command is a bit more |
| 140 |
% complex, and is given in the next section. |
| 141 |
% |
| 142 |
% You can use all of \TeX's features for defining the syntax of your |
| 143 |
% command. (See chapter~20 of @/The \TeX book/ for more details.) |
| 144 |
% |
| 145 |
% \DescribeMacro{\atlet} |
| 146 |
% Since |\atdef| is provided to behave similarly to |\def|, @at\ provides |
| 147 |
% |\atlet| which works similarly to |\let|. For example you can say |
| 148 |
% \begin{listing} |
| 149 |
%\atlet!=\index |
| 150 |
% \end{listing} |
| 151 |
% to allow the short |@!| to behave exactly like |\index|. |
| 152 |
% |
| 153 |
% Note that all commands defined using these commands are robust even if you |
| 154 |
% use fragile commands in their definitions. Unless you start doing very |
| 155 |
% strange things, @-commands never need |\protect|ing. |
| 156 |
% |
| 157 |
% \subsection{Predefined @-commands} |
| 158 |
% |
| 159 |
% A small number of hopefully useful commands are provided by default. |
| 160 |
% These are described in the table below: |
| 161 |
% |
| 162 |
% \bigskip \begin{center} \begin{tabular}{lp{3in}} \hline |
| 163 |
% \bf Command & \bf Meaning \\ \hline |
| 164 |
% @.{"@@"} & Typesets an `@@' character. \\ |
| 165 |
% @.{"@/"<text>"/"} & In text (LR or paragraph) mode, typesets its |
| 166 |
% argument emphasised. In maths mode, it |
| 167 |
% always chooses italics. \\ |
| 168 |
% @.{"@*"<text>"*"} & Typesets its argument \<text> in bold. \\ |
| 169 |
% @.{"@i{"<text>"}"} & Equivalent to `@.{"\\index{"<text>"}"}'. \\ |
| 170 |
% @.{"@I{"<text>"}"} & As for |@i|, but also writes its argument |
| 171 |
% to the document. \\ \hline |
| 172 |
% \end{tabular} \end{center} \bigskip |
| 173 |
% |
| 174 |
% Package writers should not rely on any predefined @-commands -- they're |
| 175 |
% provided for users, and users should be able to redefine them without |
| 176 |
% fear of messing anything up. (This includes the `standard' commands |
| 177 |
% provided by the @at\ package, by the way. They're provided in the vague |
| 178 |
% hope that they might be useful, and as examples.) |
| 179 |
% |
| 180 |
% \implementation |
| 181 |
% |
| 182 |
% \section{Implementation} |
| 183 |
% |
| 184 |
% \begin{macrocode} |
| 185 |
%<*package> |
| 186 |
% \end{macrocode} |
| 187 |
% |
| 188 |
% \subsection{Options handling} |
| 189 |
% |
| 190 |
% We need a switch to say whether digits should be allowed. Since this |
| 191 |
% is a user thing, I'll avoid |\newif| and just define the thing by hand. |
| 192 |
% |
| 193 |
% \begin{macrocode} |
| 194 |
\def\atallowdigits{\let\ifat@digits\iftrue} |
| 195 |
\def\atdisallowdigits{\let\ifat@digits\iffalse} |
| 196 |
% \end{macrocode} |
| 197 |
% |
| 198 |
% Now define the options. |
| 199 |
% |
| 200 |
% \begin{macrocode} |
| 201 |
\DeclareOption{digits}{\atallowdigits} |
| 202 |
\DeclareOption{nodigits}{\atdisallowdigits} |
| 203 |
\ExecuteOptions{nodigits} |
| 204 |
\ProcessOptions |
| 205 |
% \end{macrocode} |
| 206 |
% |
| 207 |
% \subsection{How the commands work} |
| 208 |
% |
| 209 |
% Obviously we make the `@@' character active. It inspects the next |
| 210 |
% character (or argument, actually -- it can be enclosed in braces for |
| 211 |
% longer commands, although this is a bit futile), and builds the command |
| 212 |
% name from that. |
| 213 |
% |
| 214 |
% The |\at| command is equivalent to the active `@@' character always. |
| 215 |
% |
| 216 |
% |
| 217 |
% \subsection{Converting command names} |
| 218 |
% |
| 219 |
% We need to be able to read an @-command name, and convert it to a normal |
| 220 |
% \TeX\ control sequence. First, we declare some control sequences for |
| 221 |
% braces, which we need later. |
| 222 |
% |
| 223 |
% \begin{macrocode} |
| 224 |
\begingroup |
| 225 |
\catcode`\<1 |
| 226 |
\catcode`\>2 |
| 227 |
\catcode`\{12 |
| 228 |
\catcode`\}12 |
| 229 |
\gdef\at@lb<{> |
| 230 |
\gdef\at@rb<}> |
| 231 |
\gdef\at@spc< > |
| 232 |
\endgroup |
| 233 |
% \end{macrocode} |
| 234 |
% |
| 235 |
% I'll set up some helper routines now, to help me read the command |
| 236 |
% names. The way this works is that we |\futurelet| the token into |
| 237 |
% |\@let@token|. These routines will then sort out what to do next. |
| 238 |
% |
| 239 |
% \begin{macro}{\at@test} |
| 240 |
% |
| 241 |
% Given an |\if|\dots\ test, does its first or second argument. |
| 242 |
% |
| 243 |
% \begin{macrocode} |
| 244 |
\def\at@test#1\then{% |
| 245 |
#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi% |
| 246 |
} |
| 247 |
% \end{macrocode} |
| 248 |
% |
| 249 |
% \end{macro} |
| 250 |
% |
| 251 |
% \begin{macro}{\at@ifcat} |
| 252 |
% |
| 253 |
% Checks the category code of the current character. If it matches the |
| 254 |
% argument, it does its second argument, otherwise it does the third. |
| 255 |
% |
| 256 |
% \begin{macrocode} |
| 257 |
\def\at@ifcat#1{\at@test\ifcat#1\noexpand\@let@token\then} |
| 258 |
% \end{macrocode} |
| 259 |
% |
| 260 |
% \end{macro} |
| 261 |
% |
| 262 |
% \begin{macro}{\at@ifletter} |
| 263 |
% |
| 264 |
% This routine tests the token to see if it's a letter, and if so adds |
| 265 |
% it to the token list and does the first argument; otherwise it does the |
| 266 |
% second argument. It accepts digits as letters if the switch is turned |
| 267 |
% on. |
| 268 |
% |
| 269 |
% There's some fun later, so I'll describe this slowly. First, we compare |
| 270 |
% the category code to a letter, and if we have a match, we know we're done; |
| 271 |
% we need to pick up the letter as an argument. If the catcode is `other', |
| 272 |
% we must compare with numbers to see if it's in range. |
| 273 |
% |
| 274 |
% \begin{macrocode} |
| 275 |
\def\at@ifletter#1#2{% |
| 276 |
\at@ifcat x% |
| 277 |
{\at@ifletter@ii{#1}}% |
| 278 |
{\at@ifcat 0% |
| 279 |
{\at@ifletter@i{#1}{#2}}% |
| 280 |
{#2}% |
| 281 |
}% |
| 282 |
} |
| 283 |
% \end{macrocode} |
| 284 |
% |
| 285 |
% Right. It's `other' (so it's safe to handle as a macro argument) and we |
| 286 |
% need to know if it's a digit. This is a little tricky: I use |\if| to |
| 287 |
% compare two characters. The first character is~`1' or~`0' depending on the |
| 288 |
% `digit' switch; the second is~`1' or~`x' depending on whether it's actually |
| 289 |
% a digit. They'll only match if everything's worked out OK. |
| 290 |
% |
| 291 |
% \begin{macrocode} |
| 292 |
\def\at@ifletter@i#1#2#3{% |
| 293 |
\at@test\if% |
| 294 |
\ifat@digits1\else0\fi% |
| 295 |
\ifnum`#3<`0x\else\ifnum`#3>`9x\else1\fi\fi% |
| 296 |
\then% |
| 297 |
{\at@ifletter@ii{#1}{#3}}% |
| 298 |
{#2#3}% |
| 299 |
} |
| 300 |
% \end{macrocode} |
| 301 |
% |
| 302 |
% Right; we have the character, so add it to the list and carry on. |
| 303 |
% |
| 304 |
% \begin{macrocode} |
| 305 |
\def\at@ifletter@ii#1#2{\toks@\expandafter{\the\toks@#2}#1} |
| 306 |
% \end{macrocode} |
| 307 |
% |
| 308 |
% \end{macro} |
| 309 |
% |
| 310 |
% Now we define the command name reading routines. We have @/almost/ the |
| 311 |
% same behaviour as \TeX, although we can't support `|%|' characters for |
| 312 |
% reasons to do with \TeX's tokenising algorithm. |
| 313 |
% |
| 314 |
% \begin{macro}{\at@read@name} |
| 315 |
% |
| 316 |
% The routine which actually reads the command name works as follows: |
| 317 |
% \begin{enumerate} |
| 318 |
% \item Have a peek at the next character. If it's a left or right brace, |
| 319 |
% then use the appropriate character. |
| 320 |
% \item If the character is not a letter, just use the character (or whole |
| 321 |
% control sequence. |
| 322 |
% \item Finally, if it's a letter, keep reading letters until we find one |
| 323 |
% that wasn't. |
| 324 |
% \end{enumerate} |
| 325 |
% |
| 326 |
% First, we do some setting up and read the first character |
| 327 |
% |
| 328 |
% \begin{macrocode} |
| 329 |
\def\at@read@name#1{% |
| 330 |
\let\at@next=#1% |
| 331 |
\toks@{}% |
| 332 |
\futurelet\@let@token\at@rn@i% |
| 333 |
} |
| 334 |
% \end{macrocode} |
| 335 |
% |
| 336 |
% Next, sort out what to do, based on the category code. |
| 337 |
% |
| 338 |
% \begin{macrocode} |
| 339 |
\def\at@rn@i{% |
| 340 |
\def\@tempa{\afterassignment\at@rn@iv\let\@let@token= }% |
| 341 |
\at@ifletter% |
| 342 |
{\futurelet\@let@token\at@rn@iii}% |
| 343 |
{\at@ifcat\bgroup% |
| 344 |
{\toks@\expandafter{\at@lb}\@tempa}% |
| 345 |
{\at@ifcat\egroup% |
| 346 |
{\toks@\expandafter{\at@rb}\@tempa}% |
| 347 |
{\at@ifcat\at@spc% |
| 348 |
{\toks@{ }\@tempa}% |
| 349 |
{\at@rn@ii}% |
| 350 |
}% |
| 351 |
}% |
| 352 |
}% |
| 353 |
} |
| 354 |
% \end{macrocode} |
| 355 |
% |
| 356 |
% Most types of tokens can be fiddled using |\string|. |
| 357 |
% |
| 358 |
% \begin{macrocode} |
| 359 |
\def\at@rn@ii#1{% |
| 360 |
\toks@\expandafter{\string#1}% |
| 361 |
\at@rn@iv% |
| 362 |
} |
| 363 |
% \end{macrocode} |
| 364 |
% |
| 365 |
% We've found a letter, so we should check for another one. |
| 366 |
% |
| 367 |
% \begin{macrocode} |
| 368 |
\def\at@rn@iii{% |
| 369 |
\at@ifletter% |
| 370 |
{\futurelet\@let@token\at@rn@iii}% |
| 371 |
{\@ifnextchar.\at@rn@iv\at@rn@iv}% |
| 372 |
} |
| 373 |
% \end{macrocode} |
| 374 |
% |
| 375 |
% Finally, we need to pass the real string, as an argument, to the |
| 376 |
% macro. We make |\@let@token| relax, since it might be something which will |
| 377 |
% upset \TeX\ later, e.g., a |#| character. |
| 378 |
% |
| 379 |
% \begin{macrocode} |
| 380 |
\def\at@rn@iv{% |
| 381 |
\let\@let@token\relax% |
| 382 |
\expandafter\at@next\csname at.\the\toks@\endcsname% |
| 383 |
} |
| 384 |
% \end{macrocode} |
| 385 |
% |
| 386 |
% \end{macro} |
| 387 |
% |
| 388 |
% \begin{macro}{\at@cmdname} |
| 389 |
% |
| 390 |
% Given a control sequence, work out which @-command it came from. |
| 391 |
% |
| 392 |
% \begin{macrocode} |
| 393 |
\def\at@cmdname#1{\expandafter\at@cmdname@i\string#1\@@foo} |
| 394 |
% \end{macrocode} |
| 395 |
% |
| 396 |
% Now extract the trailing bits. |
| 397 |
% |
| 398 |
% \begin{macrocode} |
| 399 |
\def\at@cmdname@i#1.#2\@@foo{#2} |
| 400 |
% \end{macrocode} |
| 401 |
% |
| 402 |
% \end{macro} |
| 403 |
% |
| 404 |
% \begin{macro}{\at@decode} |
| 405 |
% |
| 406 |
% The |\at@decode| macro takes an extracted @-command name, and tries to |
| 407 |
% execute the correct control sequence derived from it. |
| 408 |
% |
| 409 |
% \begin{macrocode} |
| 410 |
\def\at@decode#1{% |
| 411 |
\at@test\ifx#1\relax\then{% |
| 412 |
\PackageError{at}{Unknown @-command `@\at@cmdname#1'}{% |
| 413 |
The @-command you typed wasn't recognised, so I've ignored it. |
| 414 |
}% |
| 415 |
}{% |
| 416 |
#1% |
| 417 |
}% |
| 418 |
} |
| 419 |
% \end{macrocode} |
| 420 |
% |
| 421 |
% \end{macro} |
| 422 |
% |
| 423 |
% \begin{macro}{\@at} |
| 424 |
% |
| 425 |
% We'd like a measure of compatibility with @p{amsmath}. The @-commands |
| 426 |
% provided by @p{amsmath} work only in maths mode, so this gives us a way of |
| 427 |
% distinguishing. If the control sequence |\Iat| is defined, and we're in |
| 428 |
% maths mode, we'll call that instead of doing our own thing. |
| 429 |
% |
| 430 |
% \begin{macrocode} |
| 431 |
\def\@at{% |
| 432 |
\def\@tempa{\at@read@name\at@decode}% |
| 433 |
\ifmmode\ifx\Iat\not@@defined\else% |
| 434 |
\let\@tempa\Iat% |
| 435 |
\fi\fi% |
| 436 |
\@tempa% |
| 437 |
} |
| 438 |
% \end{macrocode} |
| 439 |
% |
| 440 |
% \end{macro} |
| 441 |
% |
| 442 |
% |
| 443 |
% \subsection{Defining new commands} |
| 444 |
% |
| 445 |
% \begin{macro}{\at@buildcmd} |
| 446 |
% |
| 447 |
% First, we define a command to build these other commands: |
| 448 |
% |
| 449 |
% \begin{macrocode} |
| 450 |
\def\at@buildcmd#1#2{% |
| 451 |
\expandafter\def\csname\expandafter |
| 452 |
\@gobble\string#1@decode\endcsname##1{#2##1}% |
| 453 |
\edef#1{% |
| 454 |
\noexpand\at@read@name% |
| 455 |
\expandafter\noexpand% |
| 456 |
\csname\expandafter\@gobble\string#1@decode\endcsname% |
| 457 |
}% |
| 458 |
} |
| 459 |
% \end{macrocode} |
| 460 |
% |
| 461 |
% \end{macro} |
| 462 |
% |
| 463 |
% \begin{macro}{\newatcommand} |
| 464 |
% \begin{macro}{\renewatcommand} |
| 465 |
% \begin{macro}{\provideatcommand} |
| 466 |
% \begin{macro}{\atdef} |
| 467 |
% \begin{macro}{\atshow} |
| 468 |
% |
| 469 |
% Now we define the various operations on @-commands. |
| 470 |
% |
| 471 |
% \begin{macrocode} |
| 472 |
\at@buildcmd\newatcommand\newcommand |
| 473 |
\at@buildcmd\renewatcommand\renewcommand |
| 474 |
\at@buildcmd\provideatcommand\providecommand |
| 475 |
\at@buildcmd\atdef\def |
| 476 |
\at@buildcmd\atshow\show |
| 477 |
% \end{macrocode} |
| 478 |
% |
| 479 |
% \end{macro} |
| 480 |
% \end{macro} |
| 481 |
% \end{macro} |
| 482 |
% \end{macro} |
| 483 |
% \end{macro} |
| 484 |
% |
| 485 |
% \begin{macro}{\atlet} |
| 486 |
% |
| 487 |
% |\atlet| is rather harder than the others, because we want to allow people |
| 488 |
% to say things like @.{"\\atlet"<name>"=@"<name>}. The following hacking |
| 489 |
% does the trick. I'm trying very hard to duplicate |\let|'s behaviour with |
| 490 |
% respect to space tokens here, to avoid any surprises, although there |
| 491 |
% probably will be some differences. In particular, |\afterassignment| |
| 492 |
% won't work in any sensible way. |
| 493 |
% |
| 494 |
% First, we read the name of the @-command we're defining. We also open |
| 495 |
% a group, to stop messing other people up, and make `@@' into an `other' |
| 496 |
% token, so that it doesn't irritatingly look like its meaning as a control |
| 497 |
% sequence. |
| 498 |
% |
| 499 |
% \begin{macrocode} |
| 500 |
\def\atlet{% |
| 501 |
\begingroup% |
| 502 |
\@makeother\@% |
| 503 |
\at@read@name\atlet@i% |
| 504 |
} |
| 505 |
% \end{macrocode} |
| 506 |
% |
| 507 |
% Put the name into a scratch macro for later use. Now see if there's an |
| 508 |
% equals sign up ahead. If not, this will gobble any spaces in between the |
| 509 |
% @-command name and the argument. |
| 510 |
% |
| 511 |
% \begin{macrocode} |
| 512 |
\def\atlet@i#1{% |
| 513 |
\def\at@temp{#1}% |
| 514 |
\@ifnextchar=\atlet@ii{\atlet@ii=}% |
| 515 |
} |
| 516 |
% \end{macrocode} |
| 517 |
% |
| 518 |
% Now we gobble the equals sign (whatever catcode it is), and peek at the |
| 519 |
% next token up ahead using |\let| with no following space. |
| 520 |
% |
| 521 |
% \begin{macrocode} |
| 522 |
\def\atlet@ii#1{\afterassignment\atlet@iii\global\let\at@gnext=} |
| 523 |
% \end{macrocode} |
| 524 |
% |
| 525 |
% The control sequence |\at@gnext| is now |\let| to be whatever we want the |
| 526 |
% @-command to be, unless it's picked up an `@@' sign. If it has, we've |
| 527 |
% eaten the |@| token, so just read the name and pass it on. Otherwise, |
| 528 |
% we can |\let| the @-command directly to |\at@gnext|. There's some |
| 529 |
% nastiness here to make |\the\toks@| expand before we close the group and |
| 530 |
% restore its previous definition. |
| 531 |
% |
| 532 |
% \begin{macrocode} |
| 533 |
\def\atlet@iii{% |
| 534 |
\if @\noexpand\at@gnext% |
| 535 |
\expandafter\at@read@name\expandafter\atlet@iv% |
| 536 |
\else% |
| 537 |
\expandafter\endgroup% |
| 538 |
\expandafter\let\at@temp= \at@gnext% |
| 539 |
\fi% |
| 540 |
} |
| 541 |
% \end{macrocode} |
| 542 |
% |
| 543 |
% We've read the source @-command name, so just copy the definitions over. |
| 544 |
% |
| 545 |
% \begin{macrocode} |
| 546 |
\def\atlet@iv#1{% |
| 547 |
\expandafter\endgroup% |
| 548 |
\expandafter\let\at@temp=#1% |
| 549 |
} |
| 550 |
% \end{macrocode} |
| 551 |
% |
| 552 |
% \end{macro} |
| 553 |
% |
| 554 |
% |
| 555 |
% \subsection{Robustness of @-commands} |
| 556 |
% |
| 557 |
% We want all @-commands to be robust. We could leave them all being |
| 558 |
% fragile, although making robust @-commands would then be almost impossible. |
| 559 |
% There are two problems which we must face: |
| 560 |
% |
| 561 |
% \begin{itemize} |
| 562 |
% |
| 563 |
% \item The `|\@at|' command which scans the @-command name is (very) |
| 564 |
% fragile. I could have used |\DeclareRobustCommand| for it (and in |
| 565 |
% fact I did in an earlier version), but that doesn't help the other |
| 566 |
% problem at all. |
| 567 |
% |
| 568 |
% \item The `name' of the @-command may contain active characters or control |
| 569 |
% sequences, which will be expanded at the wrong time unless we do |
| 570 |
% something about it now. |
| 571 |
% |
| 572 |
% \end{itemize} |
| 573 |
% |
| 574 |
% We must also be careful not to introduce extra space characters into any |
| 575 |
% files written, because spaces are significant in @-commands. Finally, |
| 576 |
% we have a minor problem in that most auxiliary files are read in with |
| 577 |
% the `@@' character set to be a letter. |
| 578 |
% |
| 579 |
% \begin{macro}{\at} |
| 580 |
% |
| 581 |
% Following the example of \LaTeX's `short' command handling, we'll define |
| 582 |
% |\at| to decide what to do depending on what |\protect| looks like. If |
| 583 |
% we're typesetting, we just call |\@at| (above) and expect it to cope. |
| 584 |
% Otherwise we call |\at@protect|, which scoops up the |\fi| and the |\@at|, |
| 585 |
% and inserts other magic. |
| 586 |
% |
| 587 |
% \begin{macrocode} |
| 588 |
\def\at{\ifx\protect\@typeset@protect\else\at@protect\fi\@at} |
| 589 |
% \end{macrocode} |
| 590 |
% |
| 591 |
% \end{macro} |
| 592 |
% |
| 593 |
% \begin{macro}{\at@protect} |
| 594 |
% |
| 595 |
% Since we gobbled the |\fi| from the above, we must put that back. We then |
| 596 |
% need to do things which are more complicated. If |\protect| is behaving |
| 597 |
% like |\string|, then we do one sort of protection. Otherwise, we assume |
| 598 |
% that |\protect| is being like |\noexpand|. |
| 599 |
% |
| 600 |
% \begin{macrocode} |
| 601 |
\def\at@protect\fi#1{% |
| 602 |
\fi% |
| 603 |
\ifx\protect\string% |
| 604 |
\expandafter\at@protect@string% |
| 605 |
\else% |
| 606 |
\expandafter\at@protect@noexpand% |
| 607 |
\fi% |
| 608 |
} |
| 609 |
% \end{macrocode} |
| 610 |
% |
| 611 |
% \end{macro} |
| 612 |
% |
| 613 |
% \begin{macro}{\at@protect@string} |
| 614 |
% |
| 615 |
% When |\protect| is |\string|, we don't need to be able to recover the |
| 616 |
% original text particularly accurately -- it's for the user to look at. |
| 617 |
% Therefore, we just output a $|@|_{11}$ and use |\string| on the next |
| 618 |
% token. This must be sufficient, since we only allow multi-token command |
| 619 |
% names if the first token is a letter (code~11). |
| 620 |
% |
| 621 |
% \begin{macrocode} |
| 622 |
\def\at@protect@string{@\string} |
| 623 |
% \end{macrocode} |
| 624 |
% |
| 625 |
% \end{macro} |
| 626 |
% |
| 627 |
% \begin{macro}{\at@protect@noexpand} |
| 628 |
% |
| 629 |
% This is a little more complex, since we're still expecting to be executed |
| 630 |
% properly at some stage. However, there's a cheeky dodge we can employ |
| 631 |
% since the |\at| command is thoroughly robustified (or at least it will be |
| 632 |
% by the time we've finished this). All |\@unexpandable@protect| does |
| 633 |
% is confer repeated robustness on a fragile command. Since our command |
| 634 |
% is robust, we don't need this and we can get away with just using a |
| 635 |
% single |\noexpand|, both for the |\@at@| command and the following token |
| 636 |
% (which we must robustify, because no-one else can do it for us -- if |
| 637 |
% anyone tries, they end up using the |@\protect| command which is rather |
| 638 |
% embarassing). |
| 639 |
% |
| 640 |
% I'll give the definition, and then examine how this expands in various |
| 641 |
% cases. |
| 642 |
% |
| 643 |
% \begin{macrocode} |
| 644 |
\def\at@protect@noexpand{\noexpand\@at@ @\noexpand} |
| 645 |
\def\@at@#1{\at} |
| 646 |
% \end{macrocode} |
| 647 |
% |
| 648 |
% A few points, before we go into the main examination of the protection. |
| 649 |
% I've inserted a $|@|_{11}$ token, which is gobbled by |\@at@| when the |
| 650 |
% thing is finally expanded fully. This prevents following space tokens |
| 651 |
% in an |\input| file from being swallowed because they follow a control |
| 652 |
% sequence. (I can't use the normal $|@|_{13}$ token, because when files |
| 653 |
% like the |.aux| file are read in, |@| is given code~11 by |
| 654 |
% |\makeatletter|.) |
| 655 |
% |
| 656 |
% \setbox0\hbox{|@at@|} |
| 657 |
% Now for a description of why this works. When |\at| is expanded, it works |
| 658 |
% out that |\protect| is either |\noexpand| or |\@unexpandable@protect|, and |
| 659 |
% becomes |\at@protect@noexpand|. Because of the |\noexpand| tokens, this |
| 660 |
% stops being expanded once it reaches $\fbox{\box0}\,|@|_{11}\,x$ (where |
| 661 |
% $x$ is the token immediately following the $|@|_{13}$ character). If this |
| 662 |
% is expanded again, for example in another |\edef|, or in a |\write| or a |
| 663 |
% |\mark|, the |\@at@| wakes up, gobbles the following |@| (whatever catcode |
| 664 |
% it is -- there may be intervening |\write| and |\input| commands) and |
| 665 |
% becomes |\at|, and the whole thing can start over again. |
| 666 |
% |
| 667 |
% \end{macro} |
| 668 |
% |
| 669 |
% |
| 670 |
% \subsection{Enabling and disabling @-commands} |
| 671 |
% |
| 672 |
% \begin{macro}{\aton} |
| 673 |
% |
| 674 |
% We define the |\aton| command to enable all of our magic. We store |
| 675 |
% the old catcode in the |\atoff| command, make `@@' active, and make it |
| 676 |
% do the stuff. |
| 677 |
% |
| 678 |
% \begin{macrocode} |
| 679 |
\def\aton{% |
| 680 |
\ifnum\catcode`\@=\active\else% |
| 681 |
\edef\atoff{\catcode`\noexpand\@\the\catcode`\@}% |
| 682 |
\catcode`\@\active% |
| 683 |
\lccode`\~`\@% |
| 684 |
\lowercase{\let~\at}% |
| 685 |
\fi% |
| 686 |
} |
| 687 |
% \end{macrocode} |
| 688 |
% |
| 689 |
% \end{macro} |
| 690 |
% |
| 691 |
% \begin{macro}{\atoff} |
| 692 |
% |
| 693 |
% The |\atoff| command makes `@@' do the stuff it's meant to. We remember |
| 694 |
% the old catcode and revert to it. This is largely unnecessary. |
| 695 |
% |
| 696 |
% \begin{macrocode} |
| 697 |
\def\atoff{\catcode`\@12} |
| 698 |
% \end{macrocode} |
| 699 |
% |
| 700 |
% \end{macro} |
| 701 |
% |
| 702 |
% \begin{macro}{\makeatother} |
| 703 |
% |
| 704 |
% Now we make our active `@@' the default outside of package files. |
| 705 |
% |
| 706 |
% \begin{macrocode} |
| 707 |
\let\makeatother\aton |
| 708 |
% \end{macrocode} |
| 709 |
% |
| 710 |
% \end{macro} |
| 711 |
% |
| 712 |
% And we must make sure that the user can use all of our nice commands. |
| 713 |
% Once the document starts, we allow @-commands. |
| 714 |
% |
| 715 |
% \begin{macrocode} |
| 716 |
\AtBeginDocument{\aton} |
| 717 |
% \end{macrocode} |
| 718 |
% |
| 719 |
% \begin{macro}{\dospecials} |
| 720 |
% \begin{macro}{\@sanitize} |
| 721 |
% |
| 722 |
% We must add the `@@' character to the various specials lists. |
| 723 |
% |
| 724 |
% \begin{macrocode} |
| 725 |
\expandafter\def\expandafter\dospecials\expandafter{\dospecials\do\@} |
| 726 |
\expandafter\def\expandafter\@sanitize\expandafter{% |
| 727 |
\@sanitize\@makeother\@} |
| 728 |
% \end{macrocode} |
| 729 |
% |
| 730 |
% \end{macro} |
| 731 |
% \end{macro} |
| 732 |
% |
| 733 |
% \subsection{Default @-commands} |
| 734 |
% |
| 735 |
% We define some trivial examples to get the user going. |
| 736 |
% |
| 737 |
% \begin{macrocode} |
| 738 |
\expandafter\chardef\csname at.@\endcsname=`\@ |
| 739 |
\atdef*#1*{\ifmmode\mathbf{#1}\else\textbf{#1}\fi} |
| 740 |
\atdef/#1/{\ifmmode\mathit{#1}\else\emph{#1}\fi} |
| 741 |
\atlet i=\index |
| 742 |
\atdef I#1{#1\index{#1}} |
| 743 |
%</package> |
| 744 |
% \end{macrocode} |
| 745 |
% |
| 746 |
% \hfill Mark Wooding, \today |
| 747 |
% |
| 748 |
% \Finale |
| 749 |
% |
| 750 |
\endinput |