| 1 |
% \begin{meta-comment} |
| 2 |
% |
| 3 |
% $Id: sverb.dtx,v 1.3 1996/11/19 21:01:18 mdw Exp $ |
| 4 |
% |
| 5 |
% Verbatim typesetting done properly (ahem) |
| 6 |
% |
| 7 |
% (c) 1996 Mark Wooding |
| 8 |
% |
| 9 |
%----- Revision history ----------------------------------------------------- |
| 10 |
% |
| 11 |
% $Log: sverb.dtx,v $ |
| 12 |
% Revision 1.3 1996/11/19 21:01:18 mdw |
| 13 |
% Entered into RCS |
| 14 |
% |
| 15 |
% |
| 16 |
% \end{meta-comment} |
| 17 |
% |
| 18 |
% \begin{meta-comment} <general public licence> |
| 19 |
%% |
| 20 |
%% sverb package -- handling of verbatim text |
| 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{sverb} |
| 42 |
%<+package> [1996/05/08 1.3 Verbatim typesetting] |
| 43 |
% \end{meta-comment} |
| 44 |
% |
| 45 |
% \CheckSum{651} |
| 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} |
| 64 |
% |
| 65 |
%<*driver> |
| 66 |
\input{mdwtools} |
| 67 |
\describespackage{sverb} |
| 68 |
\mdwdoc |
| 69 |
%</driver> |
| 70 |
% |
| 71 |
% \end{meta-comment} |
| 72 |
% |
| 73 |
% \section{User guide} |
| 74 |
% |
| 75 |
% The \package{sverb} package provides some useful commands and environments |
| 76 |
% for doing things with verbatim text. I prefer this code to the standard |
| 77 |
% \package{verbatim} package (by Rainer Sch\"opf et al.)\ although I'm |
| 78 |
% biased. |
| 79 |
% |
| 80 |
% The package was written to fulfil a particular purpose: I wanted to be able |
| 81 |
% to typeset ARM assembler code, 77~columns wide, on A5~paper, with the |
| 82 |
% fields separated by \textit{tab} characters. It's grown up fairly |
| 83 |
% organically from that, and I've tidied it when I've seen the code get too |
| 84 |
% ugly. |
| 85 |
% |
| 86 |
% The current features are: |
| 87 |
% |
| 88 |
% \begin{itemize} |
| 89 |
% |
| 90 |
% \item A `listing' environment which typesets verbatim text nicely. |
| 91 |
% |
| 92 |
% \item A command to read verbatim text from an external file. |
| 93 |
% |
| 94 |
% \item Support for arbitrary-sized chunks of text without overflowing \TeX's |
| 95 |
% memory. |
| 96 |
% |
| 97 |
% \item Support for \textit{tab} characters in the verbatim text. |
| 98 |
% |
| 99 |
% \item An environment for typesetting demonstrations of \LaTeX\ markup. |
| 100 |
% |
| 101 |
% \item It all works correctly with the \package{doc} system for documenting |
| 102 |
% \LaTeX\ packages. |
| 103 |
% |
| 104 |
% \item A fairly hairy but quite powerful programmer interface to the yukky |
| 105 |
% bits of the package. |
| 106 |
% |
| 107 |
% \end{itemize} |
| 108 |
% |
| 109 |
% The interface is described in its own section, so that more timid readers |
| 110 |
% can avoid it. That said, some of the stuff in this section gets rather |
| 111 |
% technical. |
| 112 |
% |
| 113 |
% Note that this package doesn't even try to do anything with short bits of |
| 114 |
% verbatim text (as handled by the |\verb:...:| command). I have a separate |
| 115 |
% package (\package{syntax}) which does all sorts of horrible things along |
| 116 |
% those lines. |
| 117 |
% |
| 118 |
% \subsection{The \env{listing} environment} |
| 119 |
% |
| 120 |
% \DescribeEnv{listing} |
| 121 |
% The main method for typesetting verbatim text is the \env{listing} |
| 122 |
% environment. This works pretty much the same as the standard |
| 123 |
% \env{verbatim} environment, with some exceptions, which are described |
| 124 |
% below. |
| 125 |
% |
| 126 |
% So that you know exactly what you're getting, here are the rules by which |
| 127 |
% \package{sverb} decides what the verbatim text actually is: |
| 128 |
% |
| 129 |
% \begin{itemize} |
| 130 |
% |
| 131 |
% \item If there's any text, other than spaces, on the same line as the |
| 132 |
% `|\begin{listing}|', then the contents of the environment begins |
| 133 |
% immediately after the closing brace (with all leading spaces |
| 134 |
% preserved). Otherwise, the text begins on the following line. |
| 135 |
% |
| 136 |
% \item If there is any text, other than spaces, before the |
| 137 |
% `|\end{listing}|', but on the same line, this is considered to be the |
| 138 |
% last line of the text; otherwise the text is presumed to have ended |
| 139 |
% at the end of the previous line. |
| 140 |
% |
| 141 |
% \item Any text following the |\end{listing}| on the same line is thrown |
| 142 |
% away. There are good reasons for this, but they're technical. |
| 143 |
% Essentially there's nothing I can do about it. |
| 144 |
% |
| 145 |
% \end{itemize} |
| 146 |
% |
| 147 |
% \begin{figure} |
| 148 |
% \begin{demo}[w]{The \env{listing} environment} |
| 149 |
%\dots in the following code: |
| 150 |
% |
| 151 |
%\begin{listing} |
| 152 |
%init MOV R0,#200 ;Version 2.00 please |
| 153 |
% LDR R1,=&4B534154 ;Magic number (`TASK') |
| 154 |
% ADR R2,appName ;Find application name |
| 155 |
% SWI Wimp_Initialise ;Register as a WIMP task |
| 156 |
%\end{listing} |
| 157 |
% |
| 158 |
%The next step is to \dots |
| 159 |
% \end{demo} |
| 160 |
% \end{figure} |
| 161 |
% |
| 162 |
% Tab characters are supported within the environment: tab stops are set |
| 163 |
% every eighth column, although this can be modified. |
| 164 |
% |
| 165 |
% \subsubsection{Configuring the \env{listing} environment} |
| 166 |
% |
| 167 |
% The text size used in the \env{listing} environment is set by the |
| 168 |
% |\listingsize| command. By default, this is set to |\small|, although you |
| 169 |
% can redefine it in the document preamble, or it can be set in the document |
| 170 |
% class. |
| 171 |
% |
| 172 |
% The amount by which the listing text is indented is controlled by the |
| 173 |
% |\listingindent| length parameter. This is a fixed length, whose default |
| 174 |
% value is 1\,em. |
| 175 |
% |
| 176 |
% \subsubsection{Choosing a different end-text} |
| 177 |
% |
| 178 |
% \DescribeEnv{listing*} |
| 179 |
% The \env{listing} environment is terminated by the exact character sequence |
| 180 |
% `|\end{listing}|'. This isn't too much of a problem, unless you want to |
| 181 |
% include this string in the text. This is achieved by the \env{listing$*$} |
| 182 |
% environment, which allows you to specify the end-text to find as an |
| 183 |
% argument. |
| 184 |
% |
| 185 |
% For example: |
| 186 |
% |
| 187 |
% \begin{demo}{The \env{listing$*$} environment} |
| 188 |
%Type a listing as follows: |
| 189 |
% |
| 190 |
%\begin{listing*}{<end-listing*>} |
| 191 |
%\begin{listing} |
| 192 |
%This is a listing. Yes. |
| 193 |
%\end{listing} |
| 194 |
%<end-listing*> |
| 195 |
%\end{demo} |
| 196 |
% |
| 197 |
% Don't include `special' characters in your chosen end-text unless you know |
| 198 |
% what you're doing. |
| 199 |
% |
| 200 |
% \subsection{Writing text to a file} |
| 201 |
% |
| 202 |
% \DescribeEnv{verbwrite} |
| 203 |
% You can write verbatim text to a file using the \env{verbwrite} |
| 204 |
% environment. The syntax is fairly straightforward: |
| 205 |
% |
| 206 |
% \begin{quote} |
| 207 |
% \syntax{"\\begin{verbwrite}{"<file-name>"}" \dots "\\end{verbwrite}"} |
| 208 |
% \end{quote} |
| 209 |
% |
| 210 |
% The text of the environment is written to the named file. The rules about |
| 211 |
% where the text actually starts and ends are the same as for the |
| 212 |
% \env{listing} environment. |
| 213 |
% |
| 214 |
% There is also a $*$-variant, like \env{listing$*$}, which allows you to |
| 215 |
% choose the end-text. The end-text is the first argument, the filename |
| 216 |
% comes second. |
| 217 |
% |
| 218 |
% There is a restriction on the characters you can write to the file: they |
| 219 |
% must all be considered `printable' by \TeX; otherwise they will be read |
| 220 |
% back in as `\syntax{"^^"<chars>}' which isn't too good. Unfortunately, |
| 221 |
% this includes tab characters, so you can't write them.\footnote{^^A |
| 222 |
% Well, not without doing serious surgery on \TeX\ itself, anyway. } |
| 223 |
% |
| 224 |
% \iffalse [Example time... Ho hum. There is evilness here.] \fi |
| 225 |
%\begin{verbwrite*}{<end-write>}{wrdemo1.tmp} |
| 226 |
%\begin{verbwrite}{wrdemo.tmp} |
| 227 |
%This is some text written to |
| 228 |
%a file near the beginning of |
| 229 |
%the file. |
| 230 |
%\end{verbwrite} |
| 231 |
%<end-write> |
| 232 |
% |
| 233 |
% For example: \verbinput{wrdemo1.tmp} |
| 234 |
% |
| 235 |
% \input{wrdemo1.tmp} \iffalse [Now build the file ;-) ] \fi |
| 236 |
% |
| 237 |
% \subsection{The \cmd\verbinput\ command} |
| 238 |
% |
| 239 |
% \DescribeMacro{\verbinput} |
| 240 |
% You can input a pre-prepared text file exactly as it is in the input using |
| 241 |
% the |\verbinput| command. The filename is given as an argument. For |
| 242 |
% example: |
| 243 |
% |
| 244 |
% \begin{demo}{The \cmd\verbinput\ command} |
| 245 |
%\verbinput{wrdemo.tmp} |
| 246 |
% \end{demo} |
| 247 |
% |
| 248 |
% \subsection{The \env{demo} environment} |
| 249 |
% |
| 250 |
% Package authors need to document their packages, and it's common to want |
| 251 |
% to display examples showing the original text and the output side-by-side |
| 252 |
% (or, when space doesn't permit this, one above the other). Both the |
| 253 |
% \LaTeX\ book and \textit{The \LaTeX\ Companion} contain such examples. |
| 254 |
% |
| 255 |
% The \env{demo} environment allows such displays to be created easily. The |
| 256 |
% syntax of the environment is as follows: |
| 257 |
% |
| 258 |
% \begin{quote} |
| 259 |
% \syntax{"\\begin{demo}["<shape>"]{"<title>"}" \dots "\\end{demo}"} |
| 260 |
% \end{quote} |
| 261 |
% |
| 262 |
% The optional \synt{shape} argument can be either `|w|' (wide), or `|n|' |
| 263 |
% (narrow). A `wide' shape places the input and output one above the other, |
| 264 |
% while the `narrow' shape puts them side-by-side. The default shape is |
| 265 |
% `narrow'. An attractive border is drawn around the display to finish it |
| 266 |
% off nicely. |
| 267 |
% |
| 268 |
% An example: |
| 269 |
% |
| 270 |
%\begin{demo*}{<end-demo>}[w]{The \env{demo} environment} |
| 271 |
%\begin{demo}{From the \textit{\TeX book}} |
| 272 |
%\[ \sum_{p\;\rm prime} |
| 273 |
% f(p) = \int_{t>1} |
| 274 |
% f(t)\,{\rm d}\pi(t) \] |
| 275 |
%\end{demo} |
| 276 |
%<end-demo> |
| 277 |
% |
| 278 |
% \DescribeEnv{demo*} |
| 279 |
% As with the other environments created by this package, there's a |
| 280 |
% $*$-variant which takes the end-text as an argument. |
| 281 |
% |
| 282 |
% |
| 283 |
% \section{Programmer interface} |
| 284 |
% |
| 285 |
% This section describes the publicly available routines provided by the |
| 286 |
% \package{sverb} package. Routines not described here are libable to be |
| 287 |
% changed or even removed without warning, so don't use them. |
| 288 |
% |
| 289 |
% \subsection{Environment hooks} |
| 290 |
% |
| 291 |
% Each of the environments created here works in the same way. For each |
| 292 |
% environment \env{foo}, there's a main command responsible for doing the |
| 293 |
% work, called |\sv@foo|. This is given all the arguments of the normal |
| 294 |
% environment, and two more: |
| 295 |
% |
| 296 |
% \begin{itemize} |
| 297 |
% |
| 298 |
% \item The `end-text' to search for, which marks the end of the environment. |
| 299 |
% |
| 300 |
% \item Some actions to perform after the text has been read and processed. |
| 301 |
% This allows the calling macro to do some extra actions, like closing |
| 302 |
% boxes, etc. |
| 303 |
% |
| 304 |
% \end{itemize} |
| 305 |
% |
| 306 |
% All the environments do is call the main command with appropriate |
| 307 |
% arguments. |
| 308 |
% |
| 309 |
% \subsection{Reading the verbatim text} |
| 310 |
% |
| 311 |
% \DescribeMacro{\sv@read} |
| 312 |
% The main scanning routine is |\sv@read|. It is called with three |
| 313 |
% arguments: |
| 314 |
% |
| 315 |
% \begin{itemize} |
| 316 |
% |
| 317 |
% \item The end-text marking the end of the environment. |
| 318 |
% |
| 319 |
% \item The name of a macro (which must be a single token) which is called |
| 320 |
% with a line of text as its single argument. This is given each |
| 321 |
% line of text which is read from the environment in turn. |
| 322 |
% |
| 323 |
% \item A macro, or other sort of action, which is to be done when the text |
| 324 |
% has been read and processed. |
| 325 |
% |
| 326 |
% \end{itemize} |
| 327 |
% |
| 328 |
% The macro |\sv@read| assumes that the caller has already made some |
| 329 |
% provision for removing the category codes of the following text, by either |
| 330 |
% calling |\@verbatim| or using the construction |
| 331 |
% \begin{listing} |
| 332 |
%\let\do=\@makeother |
| 333 |
%\dospecials |
| 334 |
% \end{listing} |
| 335 |
% |
| 336 |
% \DescribeMacro{\sv@safespc} |
| 337 |
% Note that any space characters you read using |\sv@read| will be catcoded |
| 338 |
% as |\active|. Normally this is OK because |\obeyspaces| (or |
| 339 |
% |\@vobeyspaces|) will be in effect. If you're doing something more exotic, |
| 340 |
% like writing text to a file or building a command string, you can call |
| 341 |
% |\sv@safespc| which defines the active-space character to be a normal |
| 342 |
% whitespace-space when expanded. |
| 343 |
% |
| 344 |
% \implementation |
| 345 |
% |
| 346 |
% \section{Implementation} |
| 347 |
% |
| 348 |
% This section defines several macros and environments which allow verbatim |
| 349 |
% typing, with a high degree of configurability. OK, so this sort of |
| 350 |
% thing's been done so often before that it isn't true, but I don't really |
| 351 |
% care. |
| 352 |
% |
| 353 |
% \begin{macrocode} |
| 354 |
%<*package> |
| 355 |
% \end{macrocode} |
| 356 |
% |
| 357 |
% \subsection{Simple things} |
| 358 |
% |
| 359 |
% To help us build funny macros which involve strange and different category |
| 360 |
% codes, I'll write some simple macros which I can use while building my |
| 361 |
% complicated and clever ones. |
| 362 |
% |
| 363 |
% \begin{macro}{\@cspecials} |
| 364 |
% |
| 365 |
% This macro is used to assist the definition of some of the environments. |
| 366 |
% It makes `|\|', `|{|' and `|}|' into `other' characters, and replaces them |
| 367 |
% with `\verb"|"', `|<|' and `|>|' respectively. Note that `|[|' and `|]|' |
| 368 |
% aren't used, because they make defining commands which take optional |
| 369 |
% arguments awkward. Note that we open a group here. This should be closed |
| 370 |
% using \verb"|endgroup" at the end of the special section. |
| 371 |
% |
| 372 |
% \begin{macrocode} |
| 373 |
\def\@cspecials{% |
| 374 |
\begingroup% |
| 375 |
\catcode`|0% |
| 376 |
\catcode`<1% |
| 377 |
\catcode`>2% |
| 378 |
\catcode`\{12% |
| 379 |
\catcode`\}12% |
| 380 |
\catcode`\\12% |
| 381 |
} |
| 382 |
% \end{macrocode} |
| 383 |
% \end{macro} |
| 384 |
% |
| 385 |
% \begin{macro}{\sv@startlisting} |
| 386 |
% |
| 387 |
% This macro sets everything up nicely for a \env{listing}-type verbatim |
| 388 |
% environment. |
| 389 |
% |
| 390 |
% \begin{macrocode} |
| 391 |
\def\sv@startlisting{% |
| 392 |
\def\par{\@@par\penalty\interlinepenalty}% |
| 393 |
\@@par% |
| 394 |
\leftskip\@totalleftmargin% |
| 395 |
\obeylines% |
| 396 |
\@noligs% |
| 397 |
\let\do\@makeother\dospecials% |
| 398 |
\verbatim@font% |
| 399 |
\frenchspacing% |
| 400 |
\@vobeyspaces% |
| 401 |
\settabwidth% |
| 402 |
\catcode9\active% |
| 403 |
\lccode`\~9\lowercase{\let~\sv@vtab}% |
| 404 |
\lccode`\~13\lowercase{\let~\vinput@cr}% |
| 405 |
\interlinepenalty500% |
| 406 |
} |
| 407 |
% \end{macrocode} |
| 408 |
% |
| 409 |
% \end{macro} |
| 410 |
% |
| 411 |
% \subsection{Tab character handling} |
| 412 |
% |
| 413 |
% One of the things we want to do here is handle tab characters properly. |
| 414 |
% (Here, `properly' means `moving to the next column which is a multiple of |
| 415 |
% eight', the way these things were always meant to.) |
| 416 |
% |
| 417 |
% \begin{macro}{\settabwidth} |
| 418 |
% |
| 419 |
% The tabs used by our tabbed verbatim environments are set up by this |
| 420 |
% routine. It sets the tab width parameter |\svtab| to 8 times the width |
| 421 |
% of a |\tt| space. If you really want, you can redefine this macro. |
| 422 |
% |
| 423 |
% \begin{macrocode} |
| 424 |
\newdimen\svtab |
| 425 |
\def\settabwidth{\setbox\z@\hbox{\texttt{\space}}\svtab8\wd\z@} |
| 426 |
% \end{macrocode} |
| 427 |
% |
| 428 |
% \end{macro} |
| 429 |
% |
| 430 |
% \begin{macro}{\sv@vtab} |
| 431 |
% |
| 432 |
% Here we handle tabs inside verbatim environments. We expect each line to |
| 433 |
% be typeset as a box, using something like |
| 434 |
% |
| 435 |
% \begin{listing} |
| 436 |
%\setbox0\hbox{#1} |
| 437 |
%\leavevmode |
| 438 |
%\box0 |
| 439 |
%\par |
| 440 |
% \end{listing} |
| 441 |
% |
| 442 |
% The idea is that you make tab active, and set it to this macro. We stop |
| 443 |
% the current box, stretch it to the right width, and start another one |
| 444 |
% straight after, so nobody know the difference. The code here is straight |
| 445 |
% from Appendix~D of \textit{The \TeX book}. |
| 446 |
% |
| 447 |
% \begin{macrocode} |
| 448 |
\def\sv@vtab{% |
| 449 |
\hfill\egroup% |
| 450 |
\@tempdima\wd\z@% |
| 451 |
\divide\@tempdima\svtab% |
| 452 |
\multiply\@tempdima\svtab% |
| 453 |
\advance\@tempdima\svtab% |
| 454 |
\wd\z@\@tempdima% |
| 455 |
\leavevmode\box\z@% |
| 456 |
\setbox\z@\hbox\bgroup% |
| 457 |
} |
| 458 |
% \end{macrocode} |
| 459 |
% |
| 460 |
% \end{macro} |
| 461 |
% |
| 462 |
% \begin{macro}{\verbinput} |
| 463 |
% |
| 464 |
% We allow input from a file, by the |\verbinput| command. We display the |
| 465 |
% text pretty much the same as the \env{listing} environment below. |
| 466 |
% |
| 467 |
% We set tab and return active, and get them to do appropriate things. This |
| 468 |
% isn't actually all that hard. |
| 469 |
% |
| 470 |
% \begin{macrocode} |
| 471 |
\def\verbinput#1{% |
| 472 |
\begin{listinglist}% |
| 473 |
\listingsize% |
| 474 |
\sv@startlisting% |
| 475 |
\setbox\z@\hbox\bgroup% |
| 476 |
\input{#1}% |
| 477 |
\sv@stripspc% |
| 478 |
\egroup% |
| 479 |
\ifdim\wd\z@=\z@% |
| 480 |
\ifhmode\par\fi% |
| 481 |
\else% |
| 482 |
\leavevmode\box\z@\par% |
| 483 |
\fi% |
| 484 |
\end{listinglist}% |
| 485 |
} |
| 486 |
% \end{macrocode} |
| 487 |
% |
| 488 |
% \end{macro} |
| 489 |
% |
| 490 |
% \begin{macro}{\vinput@cr} |
| 491 |
% |
| 492 |
% This macro handles return characters while inputting text in |\verbinput|. |
| 493 |
% We just output our current box, and start another. |
| 494 |
% |
| 495 |
% \begin{macrocode} |
| 496 |
\def\vinput@cr{% |
| 497 |
\egroup% |
| 498 |
\leavevmode\box\z@% |
| 499 |
\par% |
| 500 |
\setbox\z@\hbox\bgroup% |
| 501 |
} |
| 502 |
% \end{macrocode} |
| 503 |
% |
| 504 |
% \end{macro} |
| 505 |
% |
| 506 |
% \subsection{Reading verbatim text} |
| 507 |
% |
| 508 |
% The traditional way of reading verbatim text is to use a delimited |
| 509 |
% argument, as described in the \textit{\TeX book}. This works well-ish if |
| 510 |
% the text isn't very long. A better solution would be to pick out the text |
| 511 |
% line-by-line and process it like that. So this is what we do. |
| 512 |
% |
| 513 |
% \begin{macro}{\matcher} |
| 514 |
% |
| 515 |
% For long verbatim environments, we need to be able to find the end text. |
| 516 |
% This is rather tricky. The solution here is rather horrible. The |
| 517 |
% environment picks out each line of the text at a time, as an argument, and |
| 518 |
% tests to see if it contains the text we're after. We do the test in a |
| 519 |
% particularly yukky way: we add the actual target text to the end of the |
| 520 |
% line, and inspect the text following the match to see if the match is at |
| 521 |
% the end. |
| 522 |
% |
| 523 |
% The |\matcher| macro creates a `matcher' which will test strings to see if |
| 524 |
% they contain something interesting. |
| 525 |
% |
| 526 |
% To create a matcher, say |
| 527 |
% \syntax{"\\matcher{"<cmd-name>"}{"<target>"}{"<process-cmd>"}"}. The |
| 528 |
% command \synt{cmd-name} accepts a line of text as an argument and calls |
| 529 |
% the \synt{process-cmd} with the text of the line before the match, or the |
| 530 |
% whole lot. It also sets |\@ifmatched| appropriately. |
| 531 |
% |
| 532 |
% (Having spent ages coming up with this cruft myself, I found some very |
| 533 |
% similar, but slightly better, code in Appendix~D. So I've changed mine to |
| 534 |
% match Donald's. Anyway, credit where it's due: cheers Don.) |
| 535 |
% |
| 536 |
% \begin{macrocode} |
| 537 |
\newif\if@matched |
| 538 |
\def\matcher#1#2#3{% |
| 539 |
\expandafter\def\csname\string#1$match\endcsname##1#2##2##3\end{% |
| 540 |
\ifx##2\relax% |
| 541 |
\@matchedfalse% |
| 542 |
\else% |
| 543 |
\@matchedtrue% |
| 544 |
\fi% |
| 545 |
#3{##1}% |
| 546 |
}% |
| 547 |
\expandafter\def\expandafter#1\expandafter##\expandafter1\expandafter{% |
| 548 |
\csname\string#1$match\endcsname##1#2\relax\end% |
| 549 |
}% |
| 550 |
} |
| 551 |
% \end{macrocode} |
| 552 |
% |
| 553 |
% \end{macro} |
| 554 |
% |
| 555 |
% \begin{macro}{\sv@stripspc} |
| 556 |
% |
| 557 |
% This macro strips any trailing glue in the current horizontal list. This |
| 558 |
% is fairly simple, actually: we just loop while glue is the last item. It's |
| 559 |
% slightly complicated by penalties which \TeX\ puts into the list between |
| 560 |
% the glue items, but we just remove them too. |
| 561 |
% |
| 562 |
% \begin{macrocode} |
| 563 |
\def\sv@stripspc{% |
| 564 |
\unpenalty% |
| 565 |
\ifdim\lastskip=\z@\else% |
| 566 |
\unskip\expandafter\sv@stripspc% |
| 567 |
\fi% |
| 568 |
} |
| 569 |
% \end{macrocode} |
| 570 |
% |
| 571 |
% \end{macro} |
| 572 |
% |
| 573 |
% \begin{macro}{\sv@percent} |
| 574 |
% |
| 575 |
% This macro strips a single leading percent character if there is one, and |
| 576 |
% if the \env{doc} package is loaded. We store the possibly stripped text in |
| 577 |
% |\@tempa|. |
| 578 |
% |
| 579 |
% \begin{macrocode} |
| 580 |
\begingroup |
| 581 |
\catcode`\%=12 |
| 582 |
\gdef\sv@percent#1#2\relax |
| 583 |
{\ifx\check@percent\@@undefined |
| 584 |
\ifx#1\relax\def\@tempa{}\else |
| 585 |
\def\@tempa{#1#2}\fi\else |
| 586 |
\ifx#1\relax\def\@tempa{}\else |
| 587 |
\ifx#1%\def\@tempa{#2}\else |
| 588 |
\def\@tempa{#1#2}\fi\fi\fi} |
| 589 |
\endgroup |
| 590 |
% \end{macrocode} |
| 591 |
% |
| 592 |
% \end{macro} |
| 593 |
% |
| 594 |
% \begin{macro}{\@isspaces} |
| 595 |
% |
| 596 |
% We want to avoid writing the first and last lines of the environment to the |
| 597 |
% file if there's nothing in them. To do this, we need to know whether a |
| 598 |
% piece of text contains only space characters. This macro does this, in a |
| 599 |
% rather nasty way. See the other macros below for details of how this |
| 600 |
% works. |
| 601 |
% |
| 602 |
% We define |\sv@safespc| at the same time: this makes space active and |
| 603 |
% expand to a space character which is not active. Neat, huh? |
| 604 |
% |
| 605 |
% \begin{macrocode} |
| 606 |
\lccode`\~32 |
| 607 |
\lccode`\!32 |
| 608 |
\lowercase{% |
| 609 |
\def\@isspaces#1{% |
| 610 |
\ifx#1\relax% |
| 611 |
\def\@tempb{\@tempswafalse}% |
| 612 |
\else\ifx#1~% |
| 613 |
\let\@tempb\@isspaces% |
| 614 |
\else% |
| 615 |
\def\@tempb##1\relax{}% |
| 616 |
\fi\fi% |
| 617 |
\@tempb% |
| 618 |
} |
| 619 |
\def\sv@safespc{% |
| 620 |
\catcode32\active% |
| 621 |
\def~{ }% |
| 622 |
} |
| 623 |
} |
| 624 |
% \end{macrocode} |
| 625 |
% |
| 626 |
% \end{macro} |
| 627 |
% |
| 628 |
% \begin{macro}{\sv@read} |
| 629 |
% |
| 630 |
% This macro does the main job of reading a chunk of verbatim text. You call |
| 631 |
% it like this: |
| 632 |
% |
| 633 |
% \begin{quote} |
| 634 |
% \syntax{"\\sv@read{"<end-text>"}{"<process-line-proc>"}{"<end-proc>"}"} |
| 635 |
% \end{quote} |
| 636 |
% |
| 637 |
% The \synt{end-text} is the text to find at the end of the `environment': we |
| 638 |
% stop when we find it. |
| 639 |
% |
| 640 |
% The \synt{process-line-proc} is a macro which is passed as an argument each |
| 641 |
% line which we read from the text. |
| 642 |
% |
| 643 |
% The \synt{end-proc} is a macro to call once we've finished reading all of |
| 644 |
% the text. This can tidy up an environment or close a file or whatever. |
| 645 |
% |
| 646 |
% We read the text by picking out newlines using a delimited macro. We have |
| 647 |
% to be a little clever, because newlines are active in verbatim text. |
| 648 |
% |
| 649 |
% We will also strip `|%|' signs off the beginning if the \package{doc} |
| 650 |
% package is here (\package{doc} tries to play with \LaTeX's verbatim stuff, |
| 651 |
% and doesn't understand the way we do things). |
| 652 |
% |
| 653 |
% \begin{macrocode} |
| 654 |
\def\sv@read#1#2#3{% |
| 655 |
% \end{macrocode} |
| 656 |
% |
| 657 |
% This code does all sorts of evil things, so I'll start by opening a group. |
| 658 |
% |
| 659 |
% \begin{macrocode} |
| 660 |
\begingroup% |
| 661 |
% \end{macrocode} |
| 662 |
% |
| 663 |
% So that I can spot the end-text, I'll create a matcher macro. |
| 664 |
% |
| 665 |
% \begin{macrocode} |
| 666 |
\matcher\@match{#1}\sv@read@ii% |
| 667 |
% \end{macrocode} |
| 668 |
% |
| 669 |
% So that I can identify line ends, I'll make them active. I'll also make |
| 670 |
% spaces active so that they can expand to whatever they ought to expand |
| 671 |
% to (spaces in files, or funny \verb*" " characters or whatever. |
| 672 |
% |
| 673 |
% \begin{macrocode} |
| 674 |
\catcode13\active% |
| 675 |
\catcode32\active% |
| 676 |
% \end{macrocode} |
| 677 |
% |
| 678 |
% I'll use the |\if@tempswa| flag to tell me whether I ought to output the |
| 679 |
% current line. This is a little messy, so I'll describe it later. I'll |
| 680 |
% initialise it to false because this is the correct thing to do. |
| 681 |
% |
| 682 |
% \begin{macrocode} |
| 683 |
\@tempswafalse% |
| 684 |
% \end{macrocode} |
| 685 |
% |
| 686 |
% Most of the job is done by two submacros. I'll define them in terms of |
| 687 |
% my current arguments (to save lots of token munging). The first just |
| 688 |
% extracts the next line (which ends at the next newline character) and |
| 689 |
% tries to match it. |
| 690 |
% |
| 691 |
% \begin{macrocode} |
| 692 |
\lccode`\~13\lowercase{% |
| 693 |
\def\sv@read@i##1~{\@match{##1}}% |
| 694 |
}% |
| 695 |
% \end{macrocode} |
| 696 |
% |
| 697 |
% The results of the match get passed here, along with the text of the |
| 698 |
% line up to the matched text. |
| 699 |
% |
| 700 |
% \begin{macrocode} |
| 701 |
\def\sv@read@ii##1{% |
| 702 |
% \end{macrocode} |
| 703 |
% |
| 704 |
% The first job to do is to maybe strip off percent signs from the beginning, |
| 705 |
% to keep \package{doc} happy. |
| 706 |
% |
| 707 |
% \begin{macrocode} |
| 708 |
\sv@percent##1\relax\relax% |
| 709 |
% \end{macrocode} |
| 710 |
% |
| 711 |
% Now I need to decide whether I ought to output this line. The method goes |
| 712 |
% like this: if this is the first line (|\if@tempswa| is false) or the last |
| 713 |
% (|\if@matched| is true), \emph{and} the text consists only of spaces, then |
| 714 |
% I'll ignore it. |
| 715 |
% |
| 716 |
% The first thing to do is to notice the last line -- if |\if@matched| is |
| 717 |
% true, then I'll make |\if@tempswa| false to make the first-line and |
| 718 |
% last-line cases work the same way. |
| 719 |
% |
| 720 |
% \begin{macrocode} |
| 721 |
\if@matched\@tempswafalse\fi% |
| 722 |
% \end{macrocode} |
| 723 |
% |
| 724 |
% Now if this is the first or last line, I'll examine it for spaces. This |
| 725 |
% is done in a separate macro. It will set |\if@tempswa| false if the |
| 726 |
% text contains only spaces. |
| 727 |
% |
| 728 |
% \begin{macrocode} |
| 729 |
\if@tempswa\else\@tempswatrue\expandafter\@isspaces\@tempa\relax\fi% |
| 730 |
% \end{macrocode} |
| 731 |
% |
| 732 |
% Now, if |\if@tempswa| is still true, perform the \<process-line-proc> on |
| 733 |
% the line of text. I'll provide a group, so that it doesn't upset me |
| 734 |
% too much. |
| 735 |
% |
| 736 |
% \begin{macrocode} |
| 737 |
\if@tempswa% |
| 738 |
\begingroup% |
| 739 |
\expandafter#2\expandafter{\@tempa}% |
| 740 |
\endgroup% |
| 741 |
\fi% |
| 742 |
% \end{macrocode} |
| 743 |
% |
| 744 |
% The next line won't be the first one, so I'll set the flag true in |
| 745 |
% readiness. |
| 746 |
% |
| 747 |
% \begin{macrocode} |
| 748 |
\@tempswatrue% |
| 749 |
% \end{macrocode} |
| 750 |
% |
| 751 |
% Now, if that wasn't the last line, go round again; otherwise end the group |
| 752 |
% I started ages ago, and do the user's \<end-proc>. |
| 753 |
% |
| 754 |
% \begin{macrocode} |
| 755 |
\if@matched\def\@tempa{\endgroup#3}\else\let\@tempa\sv@read@i\fi% |
| 756 |
\@tempa% |
| 757 |
}% |
| 758 |
% \end{macrocode} |
| 759 |
% |
| 760 |
% Now to start the thing up. I'll read the first line. |
| 761 |
% |
| 762 |
% \begin{macrocode} |
| 763 |
\sv@read@i% |
| 764 |
} |
| 765 |
% \end{macrocode} |
| 766 |
% |
| 767 |
% \end{macro} |
| 768 |
% |
| 769 |
% \begin{macro}{\sv@readenv} |
| 770 |
% |
| 771 |
% This macro works out an appropriate end-text for the current environment. |
| 772 |
% If you say \syntax{"\\sv@readenv{"<macro-name>"}"}, it will expand do |
| 773 |
% \begin{listinglist} \listingsize \synshorts |
| 774 |
% <macro-name>"{\\"$_{12}$"end{"$_{12}$<current-env-name>"}"$_{12}$"}"^^A |
| 775 |
% "{\\end{"<current-env-name>"}}" |
| 776 |
% \end{listinglist} |
| 777 |
% Easy, no? |
| 778 |
% |
| 779 |
% This is all done with mirrors. No, err\dots\ it's done with |
| 780 |
% |\expandafter|. |
| 781 |
% |
| 782 |
% \begin{macrocode} |
| 783 |
\begingroup |
| 784 |
\lccode`\<=`\{ |
| 785 |
\lccode`\>=`\} |
| 786 |
\lccode`\|=`\\ |
| 787 |
\lowercase{\endgroup |
| 788 |
\def\sv@readenv#1{% |
| 789 |
\expandafter\expandafter\expandafter% |
| 790 |
#1\expandafter\sv@readenv@i\@currenvir\@@% |
| 791 |
} |
| 792 |
\def\sv@readenv@i#1\@@{{|end<#1>}{\end{#1}}} |
| 793 |
} |
| 794 |
% \end{macrocode} |
| 795 |
% |
| 796 |
% \end{macro} |
| 797 |
% |
| 798 |
% \begin{macro}{\sv@verbline} |
| 799 |
% |
| 800 |
% This macro typesets a line in a verbatim way, so you can construct a real |
| 801 |
% verbatim environment from it. It's a bit tricky in the way that it catches |
| 802 |
% the last line. Don't worry about this: it's easy really. Note the |
| 803 |
% |\relax| after the |\par| -- this is because \package{doc} tries to do |
| 804 |
% clever things with |\par| to strip `|%|' signs out. |
| 805 |
% |
| 806 |
% \begin{macrocode} |
| 807 |
\def\sv@verbline#1{% |
| 808 |
\setbox\z@\hbox{#1\sv@stripspc}% |
| 809 |
\ifdim\wd\z@=\z@% |
| 810 |
\if@matched\ifhmode\par\relax\fi\else\leavevmode\par\relax\fi% |
| 811 |
\else% |
| 812 |
\leavevmode\box\z@\par\relax% |
| 813 |
\fi% |
| 814 |
} |
| 815 |
% \end{macrocode} |
| 816 |
% |
| 817 |
% \end{macro} |
| 818 |
% |
| 819 |
% \subsection{Listing environments} |
| 820 |
% |
| 821 |
% The \env{listing} environment is our equivalent of the standard |
| 822 |
% \env{verbatim} environment. We do some slightly cleverer things, though, |
| 823 |
% to make sure (for example) that even text which contains |\end{listing}| |
| 824 |
% can be typeset. |
| 825 |
% |
| 826 |
% \begin{macro}{\listinglist} |
| 827 |
% \begin{environment}{listinglist} |
| 828 |
% |
| 829 |
% This defines the layout for the \env{listing} environment. It starts a |
| 830 |
% list with the appropriate shape. It's also made into an environment, so |
| 831 |
% that the end-paragraph-environment bits work correctly. |
| 832 |
% |
| 833 |
% The |\listingindent| length parameter sets up the indentation of the |
| 834 |
% listings. If there's a |\parindent| setting, I'll line listings up with |
| 835 |
% that; otherwise I'll just choose something which looks right. |
| 836 |
% |
| 837 |
% \begin{macrocode} |
| 838 |
\newdimen\listingindent |
| 839 |
\AtBeginDocument{% |
| 840 |
\ifdim\parindent=\z@\listingindent1em\else\listingindent\parindent\fi% |
| 841 |
} |
| 842 |
% \end{macrocode} |
| 843 |
% |
| 844 |
% Now to define a size hook for the environment. This is fairly simple |
| 845 |
% stuff. |
| 846 |
% |
| 847 |
% \begin{macrocode} |
| 848 |
\ifx\listingsize\@@undefined |
| 849 |
\let\listingsize\small |
| 850 |
\fi |
| 851 |
% \end{macrocode} |
| 852 |
% |
| 853 |
% Now to define the environment itself. Suppress the indentation if we're |
| 854 |
% first thing on a new list item, so that the listing lines up with |
| 855 |
% everything else. |
| 856 |
% |
| 857 |
% \begin{macrocode} |
| 858 |
\def\listinglist{% |
| 859 |
\list{}{% |
| 860 |
\if@inlabel% |
| 861 |
\leftmargin\z@% |
| 862 |
\else% |
| 863 |
\leftmargin\listingindent% |
| 864 |
\fi% |
| 865 |
\rightmargin\z@% |
| 866 |
\labelwidth\z@% |
| 867 |
\labelsep\z@% |
| 868 |
\itemindent\z@% |
| 869 |
\listparindent\z@% |
| 870 |
\let\makelabel\relax% |
| 871 |
\parsep\z@skip% |
| 872 |
}% |
| 873 |
\parfillskip\@flushglue% |
| 874 |
\item\relax% |
| 875 |
} |
| 876 |
\let\endlistinglist\endlist |
| 877 |
% \end{macrocode} |
| 878 |
% |
| 879 |
% \end{environment} |
| 880 |
% \end{macro} |
| 881 |
% |
| 882 |
% \begin{environment}{listing} |
| 883 |
% |
| 884 |
% The \env{listing} environment is the only real verbatim-like environment we |
| 885 |
% create will all this kit, although it does the job very nicely. |
| 886 |
% |
| 887 |
% The environment indents its contents slightly, unlike \env{verbatim}, and |
| 888 |
% uses a smaller typeface in an attempt to fit 77-column text on an A5~page. |
| 889 |
% There is also a $*$-variant, which allows you to specify the terminating |
| 890 |
% text. This enables you to include absolutely any text in the environment, |
| 891 |
% including |\end{listing}|. |
| 892 |
% |
| 893 |
% First, we must define the |\listing| command. |
| 894 |
% |
| 895 |
% \begin{macrocode} |
| 896 |
\def\listing{% |
| 897 |
\listinglist% |
| 898 |
\listingsize% |
| 899 |
\sv@readenv\sv@listing% |
| 900 |
} |
| 901 |
% \end{macrocode} |
| 902 |
% |
| 903 |
% Now we define the |\@listing| command, which does most of the work. We |
| 904 |
% base the \env{listing} environment on a \env{list}. |
| 905 |
% |
| 906 |
% \begin{macrocode} |
| 907 |
\def\sv@listing#1#2{% |
| 908 |
\sv@startlisting% |
| 909 |
\sv@read{#1}\sv@verbline{\endlistinglist#2}% |
| 910 |
} |
| 911 |
% \end{macrocode} |
| 912 |
% |
| 913 |
% Now we define the starred version. The command name needs to include the |
| 914 |
% `|*|' character, so we must use |\csname|. There's some hacking here to |
| 915 |
% allow us to read the name using the appropriate catcodes for otherwise |
| 916 |
% normal characters: \LaTeX\ activates some characters and makes them typeset |
| 917 |
% themselves to suppress some ligaturing. |
| 918 |
% |
| 919 |
% \begin{macrocode} |
| 920 |
\expandafter\def\csname listing*\endcsname{% |
| 921 |
\listinglist% |
| 922 |
\listingsize% |
| 923 |
\begingroup% |
| 924 |
\@noligs% |
| 925 |
\def\@tempa##1{\endgroup\sv@listing{##1}{\end{listing*}}}% |
| 926 |
\@tempa% |
| 927 |
} |
| 928 |
% \end{macrocode} |
| 929 |
% |
| 930 |
% \end{environment} |
| 931 |
% |
| 932 |
% \begin{environment}{ignore} |
| 933 |
% |
| 934 |
% The \env{ignore} environment entirely ignores its contents. Anything at |
| 935 |
% all may be put into the environment: it is discarded utterly. |
| 936 |
% |
| 937 |
% We define some macros for defining ignoring environments, because this can |
| 938 |
% be useful for version control, possibly. |
| 939 |
% |
| 940 |
% \begin{macrocode} |
| 941 |
\def\sv@ignore#1#2{% |
| 942 |
\@bsphack% |
| 943 |
\let\do\@makeother\dospecials% |
| 944 |
\sv@read{#1}\@gobble{\@esphack#2}% |
| 945 |
} |
| 946 |
\def\ignore{\sv@readenv\sv@ignore} |
| 947 |
\def\ignoreenv#1{% |
| 948 |
\expandafter\let\csname #1\endcsname\ignore% |
| 949 |
} |
| 950 |
\def\unignoreenv#1{% |
| 951 |
\expandafter\def\csname #1\endcsname{\endgroup}% |
| 952 |
\expandafter\def\csname end#1\endcsname% |
| 953 |
{\begingroup\def\@currenvir{#1}}% |
| 954 |
} |
| 955 |
% \end{macrocode} |
| 956 |
% |
| 957 |
% \end{environment} |
| 958 |
% |
| 959 |
% \subsection{The \env{verbwrite} environment} |
| 960 |
% |
| 961 |
% The \env{verbwrite} environment allows text to be written to a file in a |
| 962 |
% verbatim way. Note that tab characters don't work, because \TeX\ refuses |
| 963 |
% to be nice. |
| 964 |
% |
| 965 |
% \begin{macro}{\sv@write} |
| 966 |
% |
| 967 |
% As seems to be traditional now, we first define a general hookable macro |
| 968 |
% which allows a caller to specify the end-text and what to do afterwards. |
| 969 |
% |
| 970 |
% \begin{macrocode} |
| 971 |
\newwrite\sv@writefile |
| 972 |
\def\sv@write#1#2{% |
| 973 |
\begingroup% |
| 974 |
\@bsphack% |
| 975 |
\let\do\@makeother\dospecials% |
| 976 |
\sv@safespc% |
| 977 |
\sv@read{#1}\sv@writeline{\sv@endwrite#2}% |
| 978 |
} |
| 979 |
\def\sv@writeline#1{% |
| 980 |
\immediate\write\sv@writefile{#1}% |
| 981 |
} |
| 982 |
\def\sv@endwrite{% |
| 983 |
\@esphack% |
| 984 |
\endgroup% |
| 985 |
} |
| 986 |
% \end{macrocode} |
| 987 |
% |
| 988 |
% \end{macro} |
| 989 |
% |
| 990 |
% \begin{environment}{verbwrite} |
| 991 |
% |
| 992 |
% Now we can define the actual environment. We define a $*$-variant which |
| 993 |
% allows the user to specify the end-text, just to make sure. |
| 994 |
% |
| 995 |
% \begin{macrocode} |
| 996 |
\def\verbwrite#1{% |
| 997 |
\immediate\openout\sv@writefile#1\relax% |
| 998 |
\sv@readenv\sv@write% |
| 999 |
} |
| 1000 |
\def\endverbwrite{\immediate\closeout\sv@writefile} |
| 1001 |
\expandafter\def\csname verbwrite*\endcsname#1#2{% |
| 1002 |
\immediate\openout\sv@writefile#2\relax% |
| 1003 |
\sv@write{#1}{\immediate\closeout\sv@writefile\end{verbwrite*}}% |
| 1004 |
} |
| 1005 |
% \end{macrocode} |
| 1006 |
% |
| 1007 |
% \end{environment} |
| 1008 |
% |
| 1009 |
% \subsection{The \env{demo} environment} |
| 1010 |
% |
| 1011 |
% By way of tying all of this together, I present an environment for |
| 1012 |
% displaying demonstrations of \LaTeX\ markup. We read the contents of the |
| 1013 |
% environment, write it to a temporary file, and read it back twice, |
| 1014 |
% typesetting it the first time and displaying it verbatim the second time. |
| 1015 |
% |
| 1016 |
% \begin{macro}{\sv@demoname} |
| 1017 |
% |
| 1018 |
% This macro expands to the filename to use for the temporary data. To |
| 1019 |
% allow the package documentation to demonstrate the \env{demo} environment |
| 1020 |
% itself, we need to keep a nesting count. This avoids too much hackery, |
| 1021 |
% which unfortunately appears to plague all of my \TeX\ code. |
| 1022 |
% |
| 1023 |
% \begin{macrocode} |
| 1024 |
\newcount\sv@nestcount |
| 1025 |
\def\sv@demoname{demo\number\[email protected]} |
| 1026 |
% \end{macrocode} |
| 1027 |
% |
| 1028 |
% \end{macro} |
| 1029 |
% |
| 1030 |
% \begin{macro}{\sv@demo} |
| 1031 |
% |
| 1032 |
% As for listing, we do all the business through a private macro. This is |
| 1033 |
% good because it means we can leave the main macro readable. The argument |
| 1034 |
% is the end-text to spot. |
| 1035 |
% |
| 1036 |
% \begin{macrocode} |
| 1037 |
\def\sv@demo#1#2{% |
| 1038 |
\@ifnextchar[{\sv@demo@i{#1}{#2}}{\sv@demo@i{#1}{#2}[n]}% |
| 1039 |
} |
| 1040 |
\def\sv@demo@i#1#2[#3]#4{% |
| 1041 |
\advance\sv@nestcount by\@ne% |
| 1042 |
\immediate\openout\sv@writefile\sv@demoname\relax% |
| 1043 |
\sv@write{#1}{% |
| 1044 |
\immediate\closeout\sv@writefile% |
| 1045 |
\sv@dodemo{#2}{#3}{#4}% |
| 1046 |
}% |
| 1047 |
} |
| 1048 |
% \end{macrocode} |
| 1049 |
% |
| 1050 |
% \end{macro} |
| 1051 |
% |
| 1052 |
% \begin{environment}{demo} |
| 1053 |
% |
| 1054 |
% This is the real environment. We provide \env{demo$*$} too, to allow the |
| 1055 |
% user to choose the end-text. |
| 1056 |
% |
| 1057 |
% \begin{macrocode} |
| 1058 |
\def\demo{\sv@readenv\sv@demo} |
| 1059 |
\expandafter\def\csname demo*\endcsname#1{\sv@demo{#1}{\end{demo*}}} |
| 1060 |
% \end{macrocode} |
| 1061 |
% |
| 1062 |
% \end{environment} |
| 1063 |
% |
| 1064 |
% \begin{macro}{\sv@dodemo} |
| 1065 |
% |
| 1066 |
% First, let's define some common bits of code in the stuff below. The |
| 1067 |
% minipages used to typeset the material has some clever stuff to avoid |
| 1068 |
% strange spacing in the output. |
| 1069 |
% |
| 1070 |
% \begin{macrocode} |
| 1071 |
\def\sv@demosmp{% |
| 1072 |
\begin{minipage}[t]{\@tempdima}% |
| 1073 |
\vskip8\p@% |
| 1074 |
\hrule\@height\z@% |
| 1075 |
\raggedright% |
| 1076 |
\vbox\bgroup% |
| 1077 |
} |
| 1078 |
\def\sv@demoemp{% |
| 1079 |
\par\unpenalty\unskip% |
| 1080 |
\egroup% |
| 1081 |
\vskip8\p@% |
| 1082 |
\hrule\@height\z@% |
| 1083 |
\end{minipage}% |
| 1084 |
} |
| 1085 |
% \end{macrocode} |
| 1086 |
% |
| 1087 |
% This is the macro which actually typesets the demonstration. |
| 1088 |
% |
| 1089 |
% \begin{macrocode} |
| 1090 |
\def\sv@dodemo#1#2#3{% |
| 1091 |
% \end{macrocode} |
| 1092 |
% |
| 1093 |
% Now work out some values. We set |\hsize| to the line width leaving 2\,em |
| 1094 |
% of space on either side. The size of the minipages is calculated depending |
| 1095 |
% on the shape of the demonstration. This is all fairly simple. |
| 1096 |
% |
| 1097 |
% \begin{macrocode} |
| 1098 |
\begingroup% |
| 1099 |
\@tempdima\linewidth% |
| 1100 |
\advance\@tempdima-2em% |
| 1101 |
\hsize\@tempdima% |
| 1102 |
\if#2w% |
| 1103 |
\advance\@tempdima-2em% |
| 1104 |
\else% |
| 1105 |
\advance\@tempdima-3em% |
| 1106 |
\divide\@tempdima2% |
| 1107 |
\fi% |
| 1108 |
% \end{macrocode} |
| 1109 |
% |
| 1110 |
% Now we open a big vertical box, and put in a header to mark off the |
| 1111 |
% demonstration. |
| 1112 |
% |
| 1113 |
% \begin{macrocode} |
| 1114 |
\par% |
| 1115 |
\setbox\z@\hbox{\strut\enspace#3\enspace\strut}% |
| 1116 |
\@tempdimb.5\dp\z@% |
| 1117 |
\advance\@tempdimb-.5\ht\z@% |
| 1118 |
\ht\z@\@tempdimb\dp\z@\@tempdimb% |
| 1119 |
\noindent\hskip1em\vtop{% |
| 1120 |
\hb@xt@\hsize{% |
| 1121 |
\hrulefill% |
| 1122 |
\raise\@tempdimb\box\z@% |
| 1123 |
\hrulefill% |
| 1124 |
}% |
| 1125 |
\nointerlineskip% |
| 1126 |
\hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}% |
| 1127 |
\nointerlineskip% |
| 1128 |
% \end{macrocode} |
| 1129 |
% |
| 1130 |
% Now we insert the output text in the first minipage. I'll force `|%|' |
| 1131 |
% to be a comment character, in case something like \package{doc} has had its |
| 1132 |
% wicked way. |
| 1133 |
% |
| 1134 |
% \begin{macrocode} |
| 1135 |
\vskip-\parskip% |
| 1136 |
\noindent\hbox{}\hskip1em% |
| 1137 |
\sv@demosmp% |
| 1138 |
\catcode`\%14\relax% |
| 1139 |
\input{\sv@demoname}% |
| 1140 |
\sv@demoemp% |
| 1141 |
% \end{macrocode} |
| 1142 |
% |
| 1143 |
% Insert some kind of separation between the two. In `wide' format, we start |
| 1144 |
% a new line, and put a ruleoff between the two. In `narrow' format, we just |
| 1145 |
% leave some space. |
| 1146 |
% |
| 1147 |
% \begin{macrocode} |
| 1148 |
\if#2w% |
| 1149 |
\vskip8\p@\hrule\vskip8\p@% |
| 1150 |
\noindent\hbox{}% |
| 1151 |
\fi% |
| 1152 |
\hskip1em% |
| 1153 |
% \end{macrocode} |
| 1154 |
% |
| 1155 |
% Now we put the verbatim copy of the text in the other minipage. |
| 1156 |
% |
| 1157 |
% \begin{macrocode} |
| 1158 |
\sv@demosmp% |
| 1159 |
\listingindent\z@% |
| 1160 |
\verbinput\sv@demoname% |
| 1161 |
\sv@demoemp% |
| 1162 |
\par% |
| 1163 |
\nointerlineskip% |
| 1164 |
\hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}% |
| 1165 |
\hrule% |
| 1166 |
}% |
| 1167 |
\endgroup% |
| 1168 |
\par% |
| 1169 |
\vskip\baselineskip% |
| 1170 |
#1% |
| 1171 |
} |
| 1172 |
% \end{macrocode} |
| 1173 |
% |
| 1174 |
% \end{macro} |
| 1175 |
% |
| 1176 |
% That's all there is. Have fun. |
| 1177 |
% |
| 1178 |
% \begin{macrocode} |
| 1179 |
%</package> |
| 1180 |
% \end{macrocode} |
| 1181 |
% |
| 1182 |
% \hfill Mark Wooding, \today |
| 1183 |
% |
| 1184 |
% \Finale |
| 1185 |
% |
| 1186 |
\endinput |