86f6a31e |
1 | % \begin{meta-comment} |
2 | % |
3a9729b4 |
3 | % $Id: sverb.dtx,v 1.2 2003/09/05 16:09:30 mdw Exp $ |
86f6a31e |
4 | % |
5 | % Verbatim typesetting done properly (ahem) |
6 | % |
7 | % (c) 1996 Mark Wooding |
8 | % |
9 | %----- Revision history ----------------------------------------------------- |
10 | % |
11 | % $Log: sverb.dtx,v $ |
3a9729b4 |
12 | % Revision 1.2 2003/09/05 16:09:30 mdw |
13 | % Make listing formatting more hookable. Add colour support, and `splitverb' |
14 | % environment. |
15 | % |
86f6a31e |
16 | % Revision 1.1 2002/02/03 20:49:03 mdw |
17 | % Checkin for new build system. |
18 | % |
19 | % Revision 1.3 1996/11/19 21:01:18 mdw |
20 | % Entered into RCS |
21 | % |
22 | % |
23 | % \end{meta-comment} |
24 | % |
25 | % \begin{meta-comment} <general public licence> |
26 | %% |
27 | %% sverb package -- handling of verbatim text |
28 | %% Copyright (c) 1996 Mark Wooding |
29 | %% |
30 | %% This program is free software; you can redistribute it and/or modify |
31 | %% it under the terms of the GNU General Public License as published by |
32 | %% the Free Software Foundation; either version 2 of the License, or |
33 | %% (at your option) any later version. |
34 | %% |
35 | %% This program is distributed in the hope that it will be useful, |
36 | %% but WITHOUT ANY WARRANTY; without even the implied warranty of |
37 | %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
38 | %% GNU General Public License for more details. |
39 | %% |
40 | %% You should have received a copy of the GNU General Public License |
41 | %% along with this program; if not, write to the Free Software |
42 | %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
43 | %% |
44 | % \end{meta-comment} |
45 | % |
46 | % \begin{meta-comment} <Package preamble> |
47 | %<+package>\NeedsTeXFormat{LaTeX2e} |
48 | %<+package>\ProvidesPackage{sverb} |
3a9729b4 |
49 | %<+package> [2003/09/04 1.4 Verbatim typesetting] |
50 | %<+colour>\NeedsTeXFormat{LaTeX2e} |
51 | %<+colour>\ProvidesPackage{svcolour} |
52 | %<+colour> [2003/09/04 1.4 Colour support for sverb] |
53 | %<+color>\NeedsTeXFormat{LaTeX2e} |
54 | %<+color>\ProvidesPackage{svcolor} |
55 | %<+color> [2003/09/04 1.4 Fix for people who can't spell] |
56 | %<+split>\NeedsTeXFormat{LaTeX2e} |
57 | %<+split>\ProvidesPackage{svsplit} |
58 | %<+split> [2003/09/04 1.4 Verbatim, but with line breaking] |
86f6a31e |
59 | % \end{meta-comment} |
60 | % |
3a9729b4 |
61 | % \CheckSum{1009} |
86f6a31e |
62 | %% \CharacterTable |
63 | %% {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 |
64 | %% 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 |
65 | %% Digits \0\1\2\3\4\5\6\7\8\9 |
66 | %% Exclamation \! Double quote \" Hash (number) \# |
67 | %% Dollar \$ Percent \% Ampersand \& |
68 | %% Acute accent \' Left paren \( Right paren \) |
69 | %% Asterisk \* Plus \+ Comma \, |
70 | %% Minus \- Point \. Solidus \/ |
71 | %% Colon \: Semicolon \; Less than \< |
72 | %% Equals \= Greater than \> Question mark \? |
73 | %% Commercial at \@ Left bracket \[ Backslash \\ |
74 | %% Right bracket \] Circumflex \^ Underscore \_ |
75 | %% Grave accent \` Left brace \{ Vertical bar \| |
76 | %% Right brace \} Tilde \~} |
77 | %% |
78 | % |
79 | % \begin{meta-comment} |
80 | % |
81 | %<*driver> |
82 | \input{mdwtools} |
83 | \describespackage{sverb} |
3a9729b4 |
84 | \describespackage{svcolour} |
85 | \describespackage{svsplit} |
86f6a31e |
86 | \mdwdoc |
87 | %</driver> |
88 | % |
89 | % \end{meta-comment} |
90 | % |
91 | % \section{User guide} |
92 | % |
93 | % The \package{sverb} package provides some useful commands and environments |
94 | % for doing things with verbatim text. I prefer this code to the standard |
95 | % \package{verbatim} package (by Rainer Sch\"opf et al.)\ although I'm |
96 | % biased. |
97 | % |
98 | % The package was written to fulfil a particular purpose: I wanted to be able |
99 | % to typeset ARM assembler code, 77~columns wide, on A5~paper, with the |
100 | % fields separated by \textit{tab} characters. It's grown up fairly |
101 | % organically from that, and I've tidied it when I've seen the code get too |
102 | % ugly. |
103 | % |
104 | % The current features are: |
105 | % |
106 | % \begin{itemize} |
107 | % |
108 | % \item A `listing' environment which typesets verbatim text nicely. |
109 | % |
110 | % \item A command to read verbatim text from an external file. |
111 | % |
112 | % \item Support for arbitrary-sized chunks of text without overflowing \TeX's |
113 | % memory. |
114 | % |
115 | % \item Support for \textit{tab} characters in the verbatim text. |
116 | % |
117 | % \item An environment for typesetting demonstrations of \LaTeX\ markup. |
118 | % |
119 | % \item It all works correctly with the \package{doc} system for documenting |
120 | % \LaTeX\ packages. |
121 | % |
122 | % \item A fairly hairy but quite powerful programmer interface to the yukky |
123 | % bits of the package. |
124 | % |
125 | % \end{itemize} |
126 | % |
127 | % The interface is described in its own section, so that more timid readers |
128 | % can avoid it. That said, some of the stuff in this section gets rather |
129 | % technical. |
130 | % |
131 | % Note that this package doesn't even try to do anything with short bits of |
132 | % verbatim text (as handled by the |\verb:...:| command). I have a separate |
133 | % package (\package{syntax}) which does all sorts of horrible things along |
134 | % those lines. |
135 | % |
136 | % \subsection{The \env{listing} environment} |
137 | % |
138 | % \DescribeEnv{listing} |
139 | % The main method for typesetting verbatim text is the \env{listing} |
140 | % environment. This works pretty much the same as the standard |
141 | % \env{verbatim} environment, with some exceptions, which are described |
142 | % below. |
143 | % |
144 | % So that you know exactly what you're getting, here are the rules by which |
145 | % \package{sverb} decides what the verbatim text actually is: |
146 | % |
147 | % \begin{itemize} |
148 | % |
149 | % \item If there's any text, other than spaces, on the same line as the |
150 | % `|\begin{listing}|', then the contents of the environment begins |
151 | % immediately after the closing brace (with all leading spaces |
152 | % preserved). Otherwise, the text begins on the following line. |
153 | % |
154 | % \item If there is any text, other than spaces, before the |
155 | % `|\end{listing}|', but on the same line, this is considered to be the |
156 | % last line of the text; otherwise the text is presumed to have ended |
157 | % at the end of the previous line. |
158 | % |
159 | % \item Any text following the |\end{listing}| on the same line is thrown |
160 | % away. There are good reasons for this, but they're technical. |
161 | % Essentially there's nothing I can do about it. |
162 | % |
163 | % \end{itemize} |
164 | % |
165 | % \begin{figure} |
166 | % \begin{demo}[w]{The \env{listing} environment} |
167 | %\dots in the following code: |
168 | % |
169 | %\begin{listing} |
170 | %init MOV R0,#200 ;Version 2.00 please |
171 | % LDR R1,=&4B534154 ;Magic number (`TASK') |
172 | % ADR R2,appName ;Find application name |
173 | % SWI Wimp_Initialise ;Register as a WIMP task |
174 | %\end{listing} |
175 | % |
176 | %The next step is to \dots |
177 | % \end{demo} |
178 | % \end{figure} |
179 | % |
180 | % Tab characters are supported within the environment: tab stops are set |
181 | % every eighth column, although this can be modified. |
182 | % |
183 | % \subsubsection{Configuring the \env{listing} environment} |
184 | % |
3a9729b4 |
185 | % \DescribeMacro\listingsize |
86f6a31e |
186 | % The text size used in the \env{listing} environment is set by the |
3a9729b4 |
187 | % |\listingsize| command. By default, this is set to |\footnotesize|, |
188 | % although you can redefine it in the document preamble, or it can be set in |
189 | % the document class. You can put other declarations (e.g., colours) here if |
190 | % you like. |
86f6a31e |
191 | % |
3a9729b4 |
192 | % \DescribeMacro\listingindent |
86f6a31e |
193 | % The amount by which the listing text is indented is controlled by the |
194 | % |\listingindent| length parameter. This is a fixed length, whose default |
195 | % value is 1\,em. |
196 | % |
3a9729b4 |
197 | % \DescribeMacro\listinghook |
198 | % \DescribeMacro\svafter |
199 | % \DescribeMacro\svline |
200 | % \DescribeMacro\svdoline |
201 | % \DescribeEnv{listinglist} |
202 | % The |\listinghook| command is called by the \env{listing} environment (and |
203 | % |\verbinput| and \env{demo}) to set up the formatting of the listing. It |
204 | % can do any setting up it likes, and may configure |\svline| and |\svafter| |
205 | % as necessary. The macro |\svline| is run once for each line of verbatim |
206 | % text, with the line gathered into a box register, the number of which is |
207 | % given as an argument. The macro |\svafter| is called when processing has |
208 | % finished. |
209 | % |
210 | % The default setting for |\listinghook| is (similar to) |
211 | %\begin{listing} |
212 | %\newcommand{\listinghook}{% |
213 | % \par% |
214 | % \begin{listinglist}% |
215 | % \listingsize% |
216 | % \renewcommand{\svline}{\listingline}% |
217 | % \renewcommand{\svafter}{\end{listinglist}}% |
218 | %} |
219 | %\end{listing} |
220 | % (see the source for the true definition). The default |\listingline| macro |
221 | % just writes out the line using |\svdoline|, which is a simple no-nonsense |
222 | % macro which just writes the text. As an example, you could say |
223 | %\begin{listing} |
224 | %\renewcommand{\listingline}{\leavevmode\llap{\strut\vrule\space}\svdoline} |
225 | %\end{listing} |
226 | % to put a rule down the left-hand side of your listings. |
227 | % |
228 | % The \env{listinglist} environment is a relatively straightforward |
229 | % \env{list}-based environment which sets pu the indentation of a listing. |
230 | % Feel free to redefine it. |
231 | % |
86f6a31e |
232 | % \subsubsection{Choosing a different end-text} |
233 | % |
234 | % \DescribeEnv{listing*} |
235 | % The \env{listing} environment is terminated by the exact character sequence |
236 | % `|\end{listing}|'. This isn't too much of a problem, unless you want to |
237 | % include this string in the text. This is achieved by the \env{listing$*$} |
238 | % environment, which allows you to specify the end-text to find as an |
239 | % argument. |
240 | % |
241 | % For example: |
242 | % |
243 | % \begin{demo}{The \env{listing$*$} environment} |
244 | %Type a listing as follows: |
245 | % |
246 | %\begin{listing*}{<end-listing*>} |
247 | %\begin{listing} |
248 | %This is a listing. Yes. |
249 | %\end{listing} |
250 | %<end-listing*> |
251 | %\end{demo} |
252 | % |
253 | % Don't include `special' characters in your chosen end-text unless you know |
254 | % what you're doing. |
255 | % |
256 | % \subsection{Writing text to a file} |
257 | % |
258 | % \DescribeEnv{verbwrite} |
259 | % You can write verbatim text to a file using the \env{verbwrite} |
260 | % environment. The syntax is fairly straightforward: |
261 | % |
262 | % \begin{quote} |
263 | % \syntax{"\\begin{verbwrite}{"<file-name>"}" \dots "\\end{verbwrite}"} |
264 | % \end{quote} |
265 | % |
266 | % The text of the environment is written to the named file. The rules about |
267 | % where the text actually starts and ends are the same as for the |
268 | % \env{listing} environment. |
269 | % |
270 | % There is also a $*$-variant, like \env{listing$*$}, which allows you to |
271 | % choose the end-text. The end-text is the first argument, the filename |
272 | % comes second. |
273 | % |
274 | % There is a restriction on the characters you can write to the file: they |
275 | % must all be considered `printable' by \TeX; otherwise they will be read |
276 | % back in as `\syntax{"^^"<chars>}' which isn't too good. Unfortunately, |
277 | % this includes tab characters, so you can't write them.\footnote{^^A |
278 | % Well, not without doing serious surgery on \TeX\ itself, anyway. } |
279 | % |
280 | % \iffalse [Example time... Ho hum. There is evilness here.] \fi |
281 | %\begin{verbwrite*}{<end-write>}{wrdemo1.tmp} |
282 | %\begin{verbwrite}{wrdemo.tmp} |
283 | %This is some text written to |
284 | %a file near the beginning of |
285 | %the file. |
286 | %\end{verbwrite} |
287 | %<end-write> |
288 | % |
289 | % For example: \verbinput{wrdemo1.tmp} |
290 | % |
291 | % \input{wrdemo1.tmp} \iffalse [Now build the file ;-) ] \fi |
292 | % |
293 | % \subsection{The \cmd\verbinput\ command} |
294 | % |
295 | % \DescribeMacro{\verbinput} |
296 | % You can input a pre-prepared text file exactly as it is in the input using |
297 | % the |\verbinput| command. The filename is given as an argument. For |
298 | % example: |
299 | % |
300 | % \begin{demo}{The \cmd\verbinput\ command} |
301 | %\verbinput{wrdemo.tmp} |
302 | % \end{demo} |
303 | % |
304 | % \subsection{The \env{demo} environment} |
305 | % |
306 | % Package authors need to document their packages, and it's common to want |
307 | % to display examples showing the original text and the output side-by-side |
308 | % (or, when space doesn't permit this, one above the other). Both the |
309 | % \LaTeX\ book and \textit{The \LaTeX\ Companion} contain such examples. |
310 | % |
311 | % The \env{demo} environment allows such displays to be created easily. The |
312 | % syntax of the environment is as follows: |
313 | % |
314 | % \begin{quote} |
315 | % \syntax{"\\begin{demo}["<shape>"]{"<title>"}" \dots "\\end{demo}"} |
316 | % \end{quote} |
317 | % |
318 | % The optional \synt{shape} argument can be either `|w|' (wide), or `|n|' |
319 | % (narrow). A `wide' shape places the input and output one above the other, |
320 | % while the `narrow' shape puts them side-by-side. The default shape is |
321 | % `narrow'. An attractive border is drawn around the display to finish it |
322 | % off nicely. |
323 | % |
324 | % An example: |
325 | % |
326 | %\begin{demo*}{<end-demo>}[w]{The \env{demo} environment} |
327 | %\begin{demo}{From the \textit{\TeX book}} |
328 | %\[ \sum_{p\;\rm prime} |
329 | % f(p) = \int_{t>1} |
330 | % f(t)\,{\rm d}\pi(t) \] |
331 | %\end{demo} |
332 | %<end-demo> |
333 | % |
334 | % \DescribeEnv{demo*} |
335 | % As with the other environments created by this package, there's a |
336 | % $*$-variant which takes the end-text as an argument. |
337 | % |
3a9729b4 |
338 | % \DescribeMacro\demohook |
339 | % The |\demohook| does the same job for \env{demo} environments as |
340 | % |\listinghook| does for \env{listing}s. The default version just says |
341 | %\begin{listing} |
342 | %\newcommand{\demohook}{\setlength{\listingindent}{0pt}\listinghook} |
343 | %\end{listing} |
344 | % (near enough), which turns off the indentation for the listing (which would |
345 | % otherwise look rather odd). |
346 | % |
86f6a31e |
347 | % |
348 | % \section{Programmer interface} |
349 | % |
350 | % This section describes the publicly available routines provided by the |
351 | % \package{sverb} package. Routines not described here are libable to be |
352 | % changed or even removed without warning, so don't use them. |
353 | % |
354 | % \subsection{Environment hooks} |
355 | % |
356 | % Each of the environments created here works in the same way. For each |
357 | % environment \env{foo}, there's a main command responsible for doing the |
358 | % work, called |\sv@foo|. This is given all the arguments of the normal |
359 | % environment, and two more: |
360 | % |
361 | % \begin{itemize} |
362 | % |
363 | % \item The `end-text' to search for, which marks the end of the environment. |
364 | % |
365 | % \item Some actions to perform after the text has been read and processed. |
366 | % This allows the calling macro to do some extra actions, like closing |
367 | % boxes, etc. |
368 | % |
369 | % \end{itemize} |
370 | % |
371 | % All the environments do is call the main command with appropriate |
372 | % arguments. |
373 | % |
374 | % \subsection{Reading the verbatim text} |
375 | % |
376 | % \DescribeMacro{\sv@read} |
377 | % The main scanning routine is |\sv@read|. It is called with three |
378 | % arguments: |
379 | % |
380 | % \begin{itemize} |
381 | % |
382 | % \item The end-text marking the end of the environment. |
383 | % |
384 | % \item The name of a macro (which must be a single token) which is called |
385 | % with a line of text as its single argument. This is given each |
386 | % line of text which is read from the environment in turn. |
387 | % |
388 | % \item A macro, or other sort of action, which is to be done when the text |
389 | % has been read and processed. |
390 | % |
391 | % \end{itemize} |
392 | % |
393 | % The macro |\sv@read| assumes that the caller has already made some |
394 | % provision for removing the category codes of the following text, by either |
395 | % calling |\@verbatim| or using the construction |
396 | % \begin{listing} |
397 | %\let\do=\@makeother |
398 | %\dospecials |
399 | % \end{listing} |
400 | % |
401 | % \DescribeMacro{\sv@safespc} |
402 | % Note that any space characters you read using |\sv@read| will be catcoded |
403 | % as |\active|. Normally this is OK because |\obeyspaces| (or |
404 | % |\@vobeyspaces|) will be in effect. If you're doing something more exotic, |
405 | % like writing text to a file or building a command string, you can call |
406 | % |\sv@safespc| which defines the active-space character to be a normal |
407 | % whitespace-space when expanded. |
408 | % |
3a9729b4 |
409 | % \section{Colour support} |
410 | % |
411 | % There's now a little colour support in \package{sverb}. To use it, give |
412 | % the \textsf{colour} (or \textsf{color}) package option, or load the |
413 | % \package{svcolour} package. |
414 | % |
415 | % \DescribeMacro\svcolourline |
416 | % Say \syntax{"\\svcolourline["<model>"]{"<colour>"}{"<box>"}"} to typeset |
417 | % \<box> against a background of the given colour. This is a good thing to |
418 | % put in your |\listingline| command. |
419 | %\begin{demo}{Coloured listings} |
420 | %\renewcommand{\listingline} |
421 | % {\svcolourline[rgb]{1, 0.8, 0.9}} |
422 | %Consider, for example, this more |
423 | %complicated program. |
424 | %\begin{listing} |
425 | %#include <stdio.h> |
426 | % |
427 | %int main(void) |
428 | %{ |
429 | % puts("Hello, world!"); |
430 | % return (0); |
431 | %} |
432 | %\end{listing} |
433 | %\end{demo} |
434 | % For coloured text rather than background, put a |\color| command in |
435 | % |\listinghook| itself. |
436 | % |
437 | % \section{The \package{svsplit} package} |
438 | % |
439 | % A new toy! |
440 | % |
441 | % \DescribeEnv{splitverb} |
442 | % \DescribeEnv{splitverb*} |
443 | % \DescribeMacro\svsplitchars |
444 | % The \env{splitverb} environment typesets verbatim material very slowly. On |
445 | % the plus side, however, it does know how to do simple line-breaking. It |
446 | % will break lines at spaces or tabs, or after any character listed in |
447 | % |\svsplitchars|. Continuation lines have the same initial intentation as |
448 | % the original. If a line has no `good' breaking point, it's broken as late |
449 | % as possible, and a little hyphen is inserted. |
450 | %\begin{demo}[w]{The \env{splitverb} environment} |
451 | %\begin{multicols}{2} |
452 | %\begin{splitverb} |
453 | %The \package{url} package is rather fine at splitting up long URLs such as |
454 | % \url{http://www.excessus.demon.co.uk/tex} |
455 | %though it can't do its thing in the midst of verbatim text. It |
456 | %also doesn't cope when |
457 | % allthespacesinalongphrasehavemysteriouslydisappeared! |
458 | %\end{splitverb} |
459 | %\end{multicols} |
460 | %\end{demo} |
461 | % |
86f6a31e |
462 | % \implementation |
463 | % |
464 | % \section{Implementation} |
465 | % |
466 | % This section defines several macros and environments which allow verbatim |
467 | % typing, with a high degree of configurability. OK, so this sort of |
468 | % thing's been done so often before that it isn't true, but I don't really |
469 | % care. |
470 | % |
471 | % \begin{macrocode} |
472 | %<*package> |
473 | % \end{macrocode} |
474 | % |
3a9729b4 |
475 | % \subsection{Options processing} |
476 | % |
477 | % Notice options, load package. |
478 | % |
479 | % \begin{macrocode} |
480 | \newif\ifsv@colour\sv@colourfalse |
481 | \DeclareOption{colour}{\sv@colourtrue} |
482 | \DeclareOption{color}{\sv@colourtrue} |
483 | \ProcessOptions |
484 | % \end{macrocode} |
485 | % |
86f6a31e |
486 | % \subsection{Simple things} |
487 | % |
488 | % To help us build funny macros which involve strange and different category |
489 | % codes, I'll write some simple macros which I can use while building my |
490 | % complicated and clever ones. |
491 | % |
492 | % \begin{macro}{\@cspecials} |
493 | % |
494 | % This macro is used to assist the definition of some of the environments. |
495 | % It makes `|\|', `|{|' and `|}|' into `other' characters, and replaces them |
496 | % with `\verb"|"', `|<|' and `|>|' respectively. Note that `|[|' and `|]|' |
497 | % aren't used, because they make defining commands which take optional |
498 | % arguments awkward. Note that we open a group here. This should be closed |
499 | % using \verb"|endgroup" at the end of the special section. |
500 | % |
501 | % \begin{macrocode} |
502 | \def\@cspecials{% |
503 | \begingroup% |
504 | \catcode`|0% |
505 | \catcode`<1% |
506 | \catcode`>2% |
507 | \catcode`\{12% |
508 | \catcode`\}12% |
509 | \catcode`\\12% |
510 | } |
511 | % \end{macrocode} |
512 | % \end{macro} |
513 | % |
3a9729b4 |
514 | % \begin{macro}{\sv@addtobox} |
515 | % |
516 | % Add stuff to a horizontal box. |
517 | % |
518 | % \begin{macrocode} |
519 | \def\sv@addtobox#1#2{\setbox#1\hbox{\unhbox#1\box#2}} |
520 | % \end{macrocode} |
521 | % |
522 | % \end{macro} |
523 | % |
524 | % \begin{macro}{\sv@emptybox} |
525 | % |
526 | % Clear out a horizontal box. |
527 | % |
528 | % \begin{macrocode} |
529 | \def\sv@emptybox#1{\setbox#1\hbox{}} |
530 | % \end{macrocode} |
531 | % |
532 | % \end{macro} |
533 | % |
86f6a31e |
534 | % \begin{macro}{\sv@startlisting} |
535 | % |
536 | % This macro sets everything up nicely for a \env{listing}-type verbatim |
537 | % environment. |
538 | % |
539 | % \begin{macrocode} |
540 | \def\sv@startlisting{% |
541 | \def\par{\@@par\penalty\interlinepenalty}% |
542 | \@@par% |
543 | \leftskip\@totalleftmargin% |
544 | \obeylines% |
545 | \@noligs% |
546 | \let\do\@makeother\dospecials% |
547 | \verbatim@font% |
548 | \frenchspacing% |
549 | \@vobeyspaces% |
550 | \settabwidth% |
551 | \catcode9\active% |
552 | \lccode`\~9\lowercase{\let~\sv@vtab}% |
553 | \lccode`\~13\lowercase{\let~\vinput@cr}% |
554 | \interlinepenalty500% |
555 | } |
556 | % \end{macrocode} |
557 | % |
558 | % \end{macro} |
559 | % |
560 | % \subsection{Tab character handling} |
561 | % |
562 | % One of the things we want to do here is handle tab characters properly. |
563 | % (Here, `properly' means `moving to the next column which is a multiple of |
564 | % eight', the way these things were always meant to.) |
565 | % |
566 | % \begin{macro}{\settabwidth} |
567 | % |
568 | % The tabs used by our tabbed verbatim environments are set up by this |
569 | % routine. It sets the tab width parameter |\svtab| to 8 times the width |
570 | % of a |\tt| space. If you really want, you can redefine this macro. |
571 | % |
572 | % \begin{macrocode} |
573 | \newdimen\svtab |
574 | \def\settabwidth{\setbox\z@\hbox{\texttt{\space}}\svtab8\wd\z@} |
575 | % \end{macrocode} |
576 | % |
577 | % \end{macro} |
578 | % |
579 | % \begin{macro}{\sv@vtab} |
580 | % |
3a9729b4 |
581 | % Here we handle tabs inside verbatim environments. We expect to be inside |
582 | % |\box|~0. This is padded to the correct width and contributed to |\box|~2; |
583 | % |\box|~0 is then cleared and re-entered. |
86f6a31e |
584 | % |
585 | % The idea is that you make tab active, and set it to this macro. We stop |
586 | % the current box, stretch it to the right width, and start another one |
3a9729b4 |
587 | % straight after, so nobody knows the difference. The code here is straight |
86f6a31e |
588 | % from Appendix~D of \textit{The \TeX book}. |
589 | % |
590 | % \begin{macrocode} |
591 | \def\sv@vtab{% |
592 | \hfill\egroup% |
593 | \@tempdima\wd\z@% |
594 | \divide\@tempdima\svtab% |
595 | \multiply\@tempdima\svtab% |
596 | \advance\@tempdima\svtab% |
597 | \wd\z@\@tempdima% |
3a9729b4 |
598 | \sv@addtobox\tw@\z@% |
86f6a31e |
599 | \setbox\z@\hbox\bgroup% |
600 | } |
601 | % \end{macrocode} |
602 | % |
603 | % \end{macro} |
604 | % |
605 | % \begin{macro}{\verbinput} |
606 | % |
607 | % We allow input from a file, by the |\verbinput| command. We display the |
608 | % text pretty much the same as the \env{listing} environment below. |
609 | % |
610 | % We set tab and return active, and get them to do appropriate things. This |
611 | % isn't actually all that hard. |
612 | % |
613 | % \begin{macrocode} |
3a9729b4 |
614 | \def\verbinput{\listinghook\@ifstar{\verbinput@\@input}{\verbinput@\input}} |
615 | \def\verbinput@#1#2{% |
86f6a31e |
616 | \sv@startlisting% |
617 | \setbox\z@\hbox\bgroup% |
3a9729b4 |
618 | #1{#2}% |
86f6a31e |
619 | \sv@stripspc% |
620 | \egroup% |
3a9729b4 |
621 | \sv@addtobox\tw@\z@% |
622 | \ifdim\wd\tw@=\z@\listingline\tw@\fi% |
623 | \svafter% |
86f6a31e |
624 | } |
625 | % \end{macrocode} |
626 | % |
627 | % \end{macro} |
628 | % |
629 | % \begin{macro}{\vinput@cr} |
630 | % |
631 | % This macro handles return characters while inputting text in |\verbinput|. |
632 | % We just output our current box, and start another. |
633 | % |
634 | % \begin{macrocode} |
635 | \def\vinput@cr{% |
636 | \egroup% |
3a9729b4 |
637 | \sv@addtobox\tw@\z@% |
638 | \listingline\tw@% |
639 | \sv@emptybox\tw@% |
86f6a31e |
640 | \setbox\z@\hbox\bgroup% |
641 | } |
642 | % \end{macrocode} |
643 | % |
644 | % \end{macro} |
645 | % |
646 | % \subsection{Reading verbatim text} |
647 | % |
648 | % The traditional way of reading verbatim text is to use a delimited |
649 | % argument, as described in the \textit{\TeX book}. This works well-ish if |
650 | % the text isn't very long. A better solution would be to pick out the text |
651 | % line-by-line and process it like that. So this is what we do. |
652 | % |
653 | % \begin{macro}{\matcher} |
654 | % |
655 | % For long verbatim environments, we need to be able to find the end text. |
656 | % This is rather tricky. The solution here is rather horrible. The |
657 | % environment picks out each line of the text at a time, as an argument, and |
658 | % tests to see if it contains the text we're after. We do the test in a |
659 | % particularly yukky way: we add the actual target text to the end of the |
660 | % line, and inspect the text following the match to see if the match is at |
661 | % the end. |
662 | % |
663 | % The |\matcher| macro creates a `matcher' which will test strings to see if |
664 | % they contain something interesting. |
665 | % |
666 | % To create a matcher, say |
667 | % \syntax{"\\matcher{"<cmd-name>"}{"<target>"}{"<process-cmd>"}"}. The |
668 | % command \synt{cmd-name} accepts a line of text as an argument and calls |
669 | % the \synt{process-cmd} with the text of the line before the match, or the |
670 | % whole lot. It also sets |\@ifmatched| appropriately. |
671 | % |
672 | % (Having spent ages coming up with this cruft myself, I found some very |
673 | % similar, but slightly better, code in Appendix~D. So I've changed mine to |
674 | % match Donald's. Anyway, credit where it's due: cheers Don.) |
675 | % |
676 | % \begin{macrocode} |
677 | \newif\if@matched |
678 | \def\matcher#1#2#3{% |
679 | \expandafter\def\csname\string#1$match\endcsname##1#2##2##3\end{% |
680 | \ifx##2\relax% |
681 | \@matchedfalse% |
682 | \else% |
683 | \@matchedtrue% |
684 | \fi% |
685 | #3{##1}% |
686 | }% |
687 | \expandafter\def\expandafter#1\expandafter##\expandafter1\expandafter{% |
688 | \csname\string#1$match\endcsname##1#2\relax\end% |
689 | }% |
690 | } |
691 | % \end{macrocode} |
692 | % |
693 | % \end{macro} |
694 | % |
695 | % \begin{macro}{\sv@stripspc} |
696 | % |
697 | % This macro strips any trailing glue in the current horizontal list. This |
698 | % is fairly simple, actually: we just loop while glue is the last item. It's |
699 | % slightly complicated by penalties which \TeX\ puts into the list between |
700 | % the glue items, but we just remove them too. |
701 | % |
702 | % \begin{macrocode} |
703 | \def\sv@stripspc{% |
704 | \unpenalty% |
705 | \ifdim\lastskip=\z@\else% |
706 | \unskip\expandafter\sv@stripspc% |
707 | \fi% |
708 | } |
709 | % \end{macrocode} |
710 | % |
711 | % \end{macro} |
712 | % |
713 | % \begin{macro}{\sv@percent} |
714 | % |
715 | % This macro strips a single leading percent character if there is one, and |
716 | % if the \env{doc} package is loaded. We store the possibly stripped text in |
717 | % |\@tempa|. |
718 | % |
719 | % \begin{macrocode} |
720 | \begingroup |
721 | \catcode`\%=12 |
722 | \gdef\sv@percent#1#2\relax |
723 | {\ifx\check@percent\@@undefined |
724 | \ifx#1\relax\def\@tempa{}\else |
725 | \def\@tempa{#1#2}\fi\else |
726 | \ifx#1\relax\def\@tempa{}\else |
727 | \ifx#1%\def\@tempa{#2}\else |
728 | \def\@tempa{#1#2}\fi\fi\fi} |
729 | \endgroup |
730 | % \end{macrocode} |
731 | % |
732 | % \end{macro} |
733 | % |
734 | % \begin{macro}{\@isspaces} |
735 | % |
736 | % We want to avoid writing the first and last lines of the environment to the |
737 | % file if there's nothing in them. To do this, we need to know whether a |
738 | % piece of text contains only space characters. This macro does this, in a |
739 | % rather nasty way. See the other macros below for details of how this |
740 | % works. |
741 | % |
742 | % We define |\sv@safespc| at the same time: this makes space active and |
743 | % expand to a space character which is not active. Neat, huh? |
744 | % |
745 | % \begin{macrocode} |
746 | \lccode`\~32 |
747 | \lccode`\!32 |
748 | \lowercase{% |
749 | \def\@isspaces#1{% |
750 | \ifx#1\relax% |
751 | \def\@tempb{\@tempswafalse}% |
752 | \else\ifx#1~% |
753 | \let\@tempb\@isspaces% |
754 | \else% |
755 | \def\@tempb##1\relax{}% |
756 | \fi\fi% |
757 | \@tempb% |
758 | } |
759 | \def\sv@safespc{% |
760 | \catcode32\active% |
761 | \def~{ }% |
762 | } |
763 | } |
764 | % \end{macrocode} |
765 | % |
766 | % \end{macro} |
767 | % |
768 | % \begin{macro}{\sv@read} |
769 | % |
770 | % This macro does the main job of reading a chunk of verbatim text. You call |
771 | % it like this: |
772 | % |
773 | % \begin{quote} |
774 | % \syntax{"\\sv@read{"<end-text>"}{"<process-line-proc>"}{"<end-proc>"}"} |
775 | % \end{quote} |
776 | % |
777 | % The \synt{end-text} is the text to find at the end of the `environment': we |
778 | % stop when we find it. |
779 | % |
780 | % The \synt{process-line-proc} is a macro which is passed as an argument each |
781 | % line which we read from the text. |
782 | % |
783 | % The \synt{end-proc} is a macro to call once we've finished reading all of |
784 | % the text. This can tidy up an environment or close a file or whatever. |
785 | % |
786 | % We read the text by picking out newlines using a delimited macro. We have |
787 | % to be a little clever, because newlines are active in verbatim text. |
788 | % |
789 | % We will also strip `|%|' signs off the beginning if the \package{doc} |
790 | % package is here (\package{doc} tries to play with \LaTeX's verbatim stuff, |
791 | % and doesn't understand the way we do things). |
792 | % |
793 | % \begin{macrocode} |
794 | \def\sv@read#1#2#3{% |
795 | % \end{macrocode} |
796 | % |
797 | % This code does all sorts of evil things, so I'll start by opening a group. |
798 | % |
799 | % \begin{macrocode} |
800 | \begingroup% |
801 | % \end{macrocode} |
802 | % |
803 | % So that I can spot the end-text, I'll create a matcher macro. |
804 | % |
805 | % \begin{macrocode} |
806 | \matcher\@match{#1}\sv@read@ii% |
807 | % \end{macrocode} |
808 | % |
809 | % So that I can identify line ends, I'll make them active. I'll also make |
810 | % spaces active so that they can expand to whatever they ought to expand |
811 | % to (spaces in files, or funny \verb*" " characters or whatever. |
812 | % |
813 | % \begin{macrocode} |
814 | \catcode13\active% |
815 | \catcode32\active% |
816 | % \end{macrocode} |
817 | % |
818 | % I'll use the |\if@tempswa| flag to tell me whether I ought to output the |
819 | % current line. This is a little messy, so I'll describe it later. I'll |
820 | % initialise it to false because this is the correct thing to do. |
821 | % |
822 | % \begin{macrocode} |
823 | \@tempswafalse% |
824 | % \end{macrocode} |
825 | % |
826 | % Most of the job is done by two submacros. I'll define them in terms of |
827 | % my current arguments (to save lots of token munging). The first just |
828 | % extracts the next line (which ends at the next newline character) and |
829 | % tries to match it. |
830 | % |
831 | % \begin{macrocode} |
832 | \lccode`\~13\lowercase{% |
833 | \def\sv@read@i##1~{\@match{##1}}% |
834 | }% |
835 | % \end{macrocode} |
836 | % |
837 | % The results of the match get passed here, along with the text of the |
838 | % line up to the matched text. |
839 | % |
840 | % \begin{macrocode} |
841 | \def\sv@read@ii##1{% |
842 | % \end{macrocode} |
843 | % |
844 | % The first job to do is to maybe strip off percent signs from the beginning, |
845 | % to keep \package{doc} happy. |
846 | % |
847 | % \begin{macrocode} |
848 | \sv@percent##1\relax\relax% |
849 | % \end{macrocode} |
850 | % |
851 | % Now I need to decide whether I ought to output this line. The method goes |
852 | % like this: if this is the first line (|\if@tempswa| is false) or the last |
853 | % (|\if@matched| is true), \emph{and} the text consists only of spaces, then |
854 | % I'll ignore it. |
855 | % |
856 | % The first thing to do is to notice the last line -- if |\if@matched| is |
857 | % true, then I'll make |\if@tempswa| false to make the first-line and |
858 | % last-line cases work the same way. |
859 | % |
860 | % \begin{macrocode} |
861 | \if@matched\@tempswafalse\fi% |
862 | % \end{macrocode} |
863 | % |
864 | % Now if this is the first or last line, I'll examine it for spaces. This |
865 | % is done in a separate macro. It will set |\if@tempswa| false if the |
866 | % text contains only spaces. |
867 | % |
868 | % \begin{macrocode} |
869 | \if@tempswa\else\@tempswatrue\expandafter\@isspaces\@tempa\relax\fi% |
870 | % \end{macrocode} |
871 | % |
872 | % Now, if |\if@tempswa| is still true, perform the \<process-line-proc> on |
873 | % the line of text. I'll provide a group, so that it doesn't upset me |
874 | % too much. |
875 | % |
876 | % \begin{macrocode} |
877 | \if@tempswa% |
878 | \begingroup% |
879 | \expandafter#2\expandafter{\@tempa}% |
880 | \endgroup% |
881 | \fi% |
882 | % \end{macrocode} |
883 | % |
884 | % The next line won't be the first one, so I'll set the flag true in |
885 | % readiness. |
886 | % |
887 | % \begin{macrocode} |
888 | \@tempswatrue% |
889 | % \end{macrocode} |
890 | % |
891 | % Now, if that wasn't the last line, go round again; otherwise end the group |
892 | % I started ages ago, and do the user's \<end-proc>. |
893 | % |
894 | % \begin{macrocode} |
895 | \if@matched\def\@tempa{\endgroup#3}\else\let\@tempa\sv@read@i\fi% |
896 | \@tempa% |
897 | }% |
898 | % \end{macrocode} |
899 | % |
900 | % Now to start the thing up. I'll read the first line. |
901 | % |
902 | % \begin{macrocode} |
903 | \sv@read@i% |
904 | } |
905 | % \end{macrocode} |
906 | % |
907 | % \end{macro} |
908 | % |
909 | % \begin{macro}{\sv@readenv} |
910 | % |
911 | % This macro works out an appropriate end-text for the current environment. |
912 | % If you say \syntax{"\\sv@readenv{"<macro-name>"}"}, it will expand do |
913 | % \begin{listinglist} \listingsize \synshorts |
914 | % <macro-name>"{\\"$_{12}$"end{"$_{12}$<current-env-name>"}"$_{12}$"}"^^A |
915 | % "{\\end{"<current-env-name>"}}" |
916 | % \end{listinglist} |
917 | % Easy, no? |
918 | % |
919 | % This is all done with mirrors. No, err\dots\ it's done with |
920 | % |\expandafter|. |
921 | % |
922 | % \begin{macrocode} |
923 | \begingroup |
924 | \lccode`\<=`\{ |
925 | \lccode`\>=`\} |
926 | \lccode`\|=`\\ |
927 | \lowercase{\endgroup |
3a9729b4 |
928 | \def\sv@readenv#1{\expandafter\sv@readenv@i\expandafter{\@currenvir}{#1}} |
929 | \def\sv@readenv@i#1#2{#2{|end<#1>}{\end{#1}}} |
86f6a31e |
930 | } |
931 | % \end{macrocode} |
932 | % |
933 | % \end{macro} |
934 | % |
935 | % \begin{macro}{\sv@verbline} |
936 | % |
937 | % This macro typesets a line in a verbatim way, so you can construct a real |
938 | % verbatim environment from it. It's a bit tricky in the way that it catches |
939 | % the last line. Don't worry about this: it's easy really. Note the |
940 | % |\relax| after the |\par| -- this is because \package{doc} tries to do |
941 | % clever things with |\par| to strip `|%|' signs out. |
942 | % |
943 | % \begin{macrocode} |
944 | \def\sv@verbline#1{% |
3a9729b4 |
945 | \sv@emptybox\tw@% |
86f6a31e |
946 | \setbox\z@\hbox{#1\sv@stripspc}% |
3a9729b4 |
947 | \sv@addtobox\tw@\z@% |
948 | \if1\ifdim\wd\tw@=\z@\if@matched0\else1\fi\else1\fi% |
949 | \svline\tw@\relax% |
86f6a31e |
950 | \fi% |
951 | } |
952 | % \end{macrocode} |
953 | % |
954 | % \end{macro} |
955 | % |
956 | % \subsection{Listing environments} |
957 | % |
958 | % The \env{listing} environment is our equivalent of the standard |
959 | % \env{verbatim} environment. We do some slightly cleverer things, though, |
960 | % to make sure (for example) that even text which contains |\end{listing}| |
961 | % can be typeset. |
962 | % |
3a9729b4 |
963 | % \begin{macro}{\listinghook} |
964 | % |
965 | % Set everything up as required. This is here for customization. The |
966 | % underlying machinery doesn't mess with this directly, but assumes that |
967 | % |\svline| and |\svafter| are set up appropriately. |
968 | % |
969 | % \begin{macrocode} |
970 | \def\listinghook{% |
971 | \par% |
972 | \begingroup |
973 | \listinglist% |
974 | \listingsize% |
975 | \let\svline\listingline% |
976 | \def\svafter{\endlistinglist\endgroup}% |
977 | } |
978 | % \end{macrocode} |
979 | % |
980 | % \end{macro} |
981 | % |
86f6a31e |
982 | % \begin{macro}{\listinglist} |
983 | % \begin{environment}{listinglist} |
984 | % |
985 | % This defines the layout for the \env{listing} environment. It starts a |
986 | % list with the appropriate shape. It's also made into an environment, so |
987 | % that the end-paragraph-environment bits work correctly. |
988 | % |
989 | % The |\listingindent| length parameter sets up the indentation of the |
990 | % listings. If there's a |\parindent| setting, I'll line listings up with |
991 | % that; otherwise I'll just choose something which looks right. |
992 | % |
993 | % \begin{macrocode} |
994 | \newdimen\listingindent |
995 | \AtBeginDocument{% |
996 | \ifdim\parindent=\z@\listingindent1em\else\listingindent\parindent\fi% |
997 | } |
998 | % \end{macrocode} |
999 | % |
1000 | % Now to define a size hook for the environment. This is fairly simple |
1001 | % stuff. |
1002 | % |
1003 | % \begin{macrocode} |
1004 | \ifx\listingsize\@@undefined |
3a9729b4 |
1005 | \let\listingsize\footnotesize |
86f6a31e |
1006 | \fi |
1007 | % \end{macrocode} |
1008 | % |
1009 | % Now to define the environment itself. Suppress the indentation if we're |
1010 | % first thing on a new list item, so that the listing lines up with |
1011 | % everything else. |
1012 | % |
1013 | % \begin{macrocode} |
1014 | \def\listinglist{% |
1015 | \list{}{% |
1016 | \if@inlabel% |
1017 | \leftmargin\z@% |
1018 | \else% |
1019 | \leftmargin\listingindent% |
1020 | \fi% |
1021 | \rightmargin\z@% |
1022 | \labelwidth\z@% |
1023 | \labelsep\z@% |
1024 | \itemindent\z@% |
1025 | \listparindent\z@% |
1026 | \let\makelabel\relax% |
1027 | \parsep\z@skip% |
1028 | }% |
1029 | \parfillskip\@flushglue% |
1030 | \item\relax% |
1031 | } |
1032 | \let\endlistinglist\endlist |
1033 | % \end{macrocode} |
1034 | % |
1035 | % \end{environment} |
1036 | % \end{macro} |
1037 | % |
3a9729b4 |
1038 | % \begin{macro}{\svline} |
1039 | % \begin{macro}{\svdoline} |
1040 | % \begin{macro}{\listingline} |
1041 | % |
1042 | % The simple spit-out-a-line macro. |
1043 | % |
1044 | % \begin{macrocode} |
1045 | \def\svdoline#1{\leavevmode\box#1\par} |
1046 | \let\svline\svdoline |
1047 | \let\listingline\svline |
1048 | % \end{macrocode} |
1049 | % |
1050 | % \end{macro} |
1051 | % \end{macro} |
1052 | % \end{macro} |
1053 | % |
1054 | % \begin{macro}{\svafter} |
1055 | % |
1056 | % This is called when the machinery finishes. A default is set for safety's |
1057 | % sake. |
1058 | % |
1059 | % \begin{macrocode} |
1060 | \let\svafter\relax |
1061 | % \end{macrocode} |
1062 | % |
1063 | % \end{macro} |
1064 | % |
86f6a31e |
1065 | % \begin{environment}{listing} |
1066 | % |
1067 | % The \env{listing} environment is the only real verbatim-like environment we |
1068 | % create will all this kit, although it does the job very nicely. |
1069 | % |
1070 | % The environment indents its contents slightly, unlike \env{verbatim}, and |
1071 | % uses a smaller typeface in an attempt to fit 77-column text on an A5~page. |
1072 | % There is also a $*$-variant, which allows you to specify the terminating |
1073 | % text. This enables you to include absolutely any text in the environment, |
1074 | % including |\end{listing}|. |
1075 | % |
1076 | % First, we must define the |\listing| command. |
1077 | % |
1078 | % \begin{macrocode} |
3a9729b4 |
1079 | \def\listing{\listinghook\sv@readenv\sv@listing} |
86f6a31e |
1080 | % \end{macrocode} |
1081 | % |
1082 | % Now we define the |\@listing| command, which does most of the work. We |
1083 | % base the \env{listing} environment on a \env{list}. |
1084 | % |
1085 | % \begin{macrocode} |
3a9729b4 |
1086 | \def\sv@listing#1#2{\sv@startlisting\sv@read{#1}\sv@verbline{\svafter#2}} |
86f6a31e |
1087 | % \end{macrocode} |
1088 | % |
1089 | % Now we define the starred version. The command name needs to include the |
1090 | % `|*|' character, so we must use |\csname|. There's some hacking here to |
1091 | % allow us to read the name using the appropriate catcodes for otherwise |
1092 | % normal characters: \LaTeX\ activates some characters and makes them typeset |
1093 | % themselves to suppress some ligaturing. |
1094 | % |
1095 | % \begin{macrocode} |
1096 | \expandafter\def\csname listing*\endcsname{% |
3a9729b4 |
1097 | \listinghook\begingroup\@noligs\listing@star% |
86f6a31e |
1098 | } |
3a9729b4 |
1099 | \def\listing@star#1{\endgroup\sv@listing{#1}{\end{listing*}}} |
86f6a31e |
1100 | % \end{macrocode} |
1101 | % |
1102 | % \end{environment} |
1103 | % |
1104 | % \begin{environment}{ignore} |
1105 | % |
1106 | % The \env{ignore} environment entirely ignores its contents. Anything at |
1107 | % all may be put into the environment: it is discarded utterly. |
1108 | % |
1109 | % We define some macros for defining ignoring environments, because this can |
1110 | % be useful for version control, possibly. |
1111 | % |
1112 | % \begin{macrocode} |
1113 | \def\sv@ignore#1#2{% |
1114 | \@bsphack% |
1115 | \let\do\@makeother\dospecials% |
1116 | \sv@read{#1}\@gobble{\@esphack#2}% |
1117 | } |
1118 | \def\ignore{\sv@readenv\sv@ignore} |
1119 | \def\ignoreenv#1{% |
1120 | \expandafter\let\csname #1\endcsname\ignore% |
1121 | } |
1122 | \def\unignoreenv#1{% |
1123 | \expandafter\def\csname #1\endcsname{\endgroup}% |
1124 | \expandafter\def\csname end#1\endcsname% |
1125 | {\begingroup\def\@currenvir{#1}}% |
1126 | } |
1127 | % \end{macrocode} |
1128 | % |
1129 | % \end{environment} |
1130 | % |
1131 | % \subsection{The \env{verbwrite} environment} |
1132 | % |
1133 | % The \env{verbwrite} environment allows text to be written to a file in a |
1134 | % verbatim way. Note that tab characters don't work, because \TeX\ refuses |
1135 | % to be nice. |
1136 | % |
1137 | % \begin{macro}{\sv@write} |
1138 | % |
1139 | % As seems to be traditional now, we first define a general hookable macro |
1140 | % which allows a caller to specify the end-text and what to do afterwards. |
1141 | % |
1142 | % \begin{macrocode} |
1143 | \newwrite\sv@writefile |
1144 | \def\sv@write#1#2{% |
1145 | \begingroup% |
1146 | \@bsphack% |
1147 | \let\do\@makeother\dospecials% |
1148 | \sv@safespc% |
1149 | \sv@read{#1}\sv@writeline{\sv@endwrite#2}% |
1150 | } |
1151 | \def\sv@writeline#1{% |
1152 | \immediate\write\sv@writefile{#1}% |
1153 | } |
1154 | \def\sv@endwrite{% |
1155 | \@esphack% |
1156 | \endgroup% |
1157 | } |
1158 | % \end{macrocode} |
1159 | % |
1160 | % \end{macro} |
1161 | % |
1162 | % \begin{environment}{verbwrite} |
1163 | % |
1164 | % Now we can define the actual environment. We define a $*$-variant which |
1165 | % allows the user to specify the end-text, just to make sure. |
1166 | % |
1167 | % \begin{macrocode} |
1168 | \def\verbwrite#1{% |
1169 | \immediate\openout\sv@writefile#1\relax% |
1170 | \sv@readenv\sv@write% |
1171 | } |
1172 | \def\endverbwrite{\immediate\closeout\sv@writefile} |
1173 | \expandafter\def\csname verbwrite*\endcsname#1#2{% |
1174 | \immediate\openout\sv@writefile#2\relax% |
1175 | \sv@write{#1}{\immediate\closeout\sv@writefile\end{verbwrite*}}% |
1176 | } |
1177 | % \end{macrocode} |
1178 | % |
1179 | % \end{environment} |
1180 | % |
1181 | % \subsection{The \env{demo} environment} |
1182 | % |
1183 | % By way of tying all of this together, I present an environment for |
1184 | % displaying demonstrations of \LaTeX\ markup. We read the contents of the |
1185 | % environment, write it to a temporary file, and read it back twice, |
1186 | % typesetting it the first time and displaying it verbatim the second time. |
1187 | % |
1188 | % \begin{macro}{\sv@demoname} |
1189 | % |
1190 | % This macro expands to the filename to use for the temporary data. To |
1191 | % allow the package documentation to demonstrate the \env{demo} environment |
1192 | % itself, we need to keep a nesting count. This avoids too much hackery, |
1193 | % which unfortunately appears to plague all of my \TeX\ code. |
1194 | % |
1195 | % \begin{macrocode} |
1196 | \newcount\sv@nestcount |
1197 | \def\sv@demoname{demo\number\sv@nestcount.tmp} |
1198 | % \end{macrocode} |
1199 | % |
1200 | % \end{macro} |
1201 | % |
1202 | % \begin{macro}{\sv@demo} |
1203 | % |
1204 | % As for listing, we do all the business through a private macro. This is |
1205 | % good because it means we can leave the main macro readable. The argument |
1206 | % is the end-text to spot. |
1207 | % |
1208 | % \begin{macrocode} |
1209 | \def\sv@demo#1#2{% |
1210 | \@ifnextchar[{\sv@demo@i{#1}{#2}}{\sv@demo@i{#1}{#2}[n]}% |
1211 | } |
1212 | \def\sv@demo@i#1#2[#3]#4{% |
1213 | \advance\sv@nestcount by\@ne% |
1214 | \immediate\openout\sv@writefile\sv@demoname\relax% |
1215 | \sv@write{#1}{% |
1216 | \immediate\closeout\sv@writefile% |
1217 | \sv@dodemo{#2}{#3}{#4}% |
1218 | }% |
1219 | } |
1220 | % \end{macrocode} |
1221 | % |
1222 | % \end{macro} |
1223 | % |
1224 | % \begin{environment}{demo} |
1225 | % |
1226 | % This is the real environment. We provide \env{demo$*$} too, to allow the |
1227 | % user to choose the end-text. |
1228 | % |
1229 | % \begin{macrocode} |
3a9729b4 |
1230 | \def\demo{\let\@demohook\demohook\sv@readenv\sv@demo} |
1231 | \expandafter\def\csname demo*\endcsname#1% |
1232 | {\let\@demohook\demohook\sv@demo{#1}{\end{demo*}}} |
86f6a31e |
1233 | % \end{macrocode} |
1234 | % |
1235 | % \end{environment} |
1236 | % |
3a9729b4 |
1237 | % \begin{macro}{\demohook} |
1238 | % |
1239 | % Like |\listinghook|. So much so that we just call it, but first ensure |
1240 | % that the indent is zero (otherwise it looks really odd!). |
1241 | % |
1242 | % \begin{macrocode} |
1243 | \def\demohook{\listingindent\z@\listinghook} |
1244 | % \end{macrocode} |
1245 | % |
1246 | % \end{macro} |
1247 | % |
86f6a31e |
1248 | % \begin{macro}{\sv@dodemo} |
1249 | % |
1250 | % First, let's define some common bits of code in the stuff below. The |
1251 | % minipages used to typeset the material has some clever stuff to avoid |
1252 | % strange spacing in the output. |
1253 | % |
1254 | % \begin{macrocode} |
1255 | \def\sv@demosmp{% |
1256 | \begin{minipage}[t]{\@tempdima}% |
1257 | \vskip8\p@% |
1258 | \hrule\@height\z@% |
1259 | \raggedright% |
1260 | \vbox\bgroup% |
1261 | } |
1262 | \def\sv@demoemp{% |
1263 | \par\unpenalty\unskip% |
1264 | \egroup% |
1265 | \vskip8\p@% |
1266 | \hrule\@height\z@% |
1267 | \end{minipage}% |
1268 | } |
1269 | % \end{macrocode} |
1270 | % |
1271 | % This is the macro which actually typesets the demonstration. |
1272 | % |
1273 | % \begin{macrocode} |
1274 | \def\sv@dodemo#1#2#3{% |
1275 | % \end{macrocode} |
1276 | % |
1277 | % Now work out some values. We set |\hsize| to the line width leaving 2\,em |
1278 | % of space on either side. The size of the minipages is calculated depending |
1279 | % on the shape of the demonstration. This is all fairly simple. |
1280 | % |
1281 | % \begin{macrocode} |
1282 | \begingroup% |
1283 | \@tempdima\linewidth% |
1284 | \advance\@tempdima-2em% |
1285 | \hsize\@tempdima% |
1286 | \if#2w% |
1287 | \advance\@tempdima-2em% |
1288 | \else% |
1289 | \advance\@tempdima-3em% |
1290 | \divide\@tempdima2% |
1291 | \fi% |
1292 | % \end{macrocode} |
1293 | % |
1294 | % Now we open a big vertical box, and put in a header to mark off the |
1295 | % demonstration. |
1296 | % |
1297 | % \begin{macrocode} |
1298 | \par% |
1299 | \setbox\z@\hbox{\strut\enspace#3\enspace\strut}% |
1300 | \@tempdimb.5\dp\z@% |
1301 | \advance\@tempdimb-.5\ht\z@% |
1302 | \ht\z@\@tempdimb\dp\z@\@tempdimb% |
1303 | \noindent\hskip1em\vtop{% |
1304 | \hb@xt@\hsize{% |
1305 | \hrulefill% |
1306 | \raise\@tempdimb\box\z@% |
1307 | \hrulefill% |
1308 | }% |
1309 | \nointerlineskip% |
1310 | \hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}% |
1311 | \nointerlineskip% |
1312 | % \end{macrocode} |
1313 | % |
1314 | % Now we insert the output text in the first minipage. I'll force `|%|' |
1315 | % to be a comment character, in case something like \package{doc} has had its |
1316 | % wicked way. |
1317 | % |
1318 | % \begin{macrocode} |
1319 | \vskip-\parskip% |
1320 | \noindent\hbox{}\hskip1em% |
1321 | \sv@demosmp% |
1322 | \catcode`\%14\relax% |
3a9729b4 |
1323 | \@input{\sv@demoname}% |
86f6a31e |
1324 | \sv@demoemp% |
1325 | % \end{macrocode} |
1326 | % |
1327 | % Insert some kind of separation between the two. In `wide' format, we start |
1328 | % a new line, and put a ruleoff between the two. In `narrow' format, we just |
1329 | % leave some space. |
1330 | % |
1331 | % \begin{macrocode} |
1332 | \if#2w% |
1333 | \vskip8\p@\hrule\vskip8\p@% |
1334 | \noindent\hbox{}% |
1335 | \fi% |
1336 | \hskip1em% |
1337 | % \end{macrocode} |
1338 | % |
1339 | % Now we put the verbatim copy of the text in the other minipage. |
1340 | % |
1341 | % \begin{macrocode} |
1342 | \sv@demosmp% |
3a9729b4 |
1343 | \@demohook% |
1344 | \verbinput@\@input\sv@demoname% |
86f6a31e |
1345 | \sv@demoemp% |
1346 | \par% |
1347 | \nointerlineskip% |
1348 | \hb@xt@\hsize{\vrule\@height5\p@\hfil\vrule\@height5\p@}% |
1349 | \hrule% |
1350 | }% |
1351 | \endgroup% |
1352 | \par% |
1353 | \vskip\baselineskip% |
1354 | #1% |
1355 | } |
1356 | % \end{macrocode} |
1357 | % |
1358 | % \end{macro} |
1359 | % |
3a9729b4 |
1360 | % \subsection{Loading the colour package} |
1361 | % |
1362 | % If requested, we load the \package{svcolour} package here. This ensures |
1363 | % that it can patch this code if it needs to. |
1364 | % |
1365 | % \begin{macrocode} |
1366 | \ifsv@colour |
1367 | \RequirePackage{svcolour} |
1368 | \fi |
1369 | % \end{macrocode} |
1370 | % |
86f6a31e |
1371 | % That's all there is. Have fun. |
1372 | % |
1373 | % \begin{macrocode} |
1374 | %</package> |
1375 | % \end{macrocode} |
1376 | % |
3a9729b4 |
1377 | % \subsection{The \package{svcolour} package} |
1378 | % |
1379 | % This is in a separate package to avoid dragging in the \package{color} |
1380 | % package if it's unwanted. |
1381 | % |
1382 | % I prefer English spellings. Here's a trivial redirection for Americans. |
1383 | % |
1384 | % \begin{macrocode} |
1385 | %<*color> |
1386 | \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{svcolour}} |
1387 | \ProcessOptions |
1388 | \RequirePackage{svcolour} |
1389 | %</color> |
1390 | % \end{macrocode} |
1391 | % |
1392 | % And now we can start the thing properly. |
1393 | % |
1394 | % \begin{macrocode} |
1395 | %<*colour> |
1396 | \RequirePackage{color} |
1397 | % \end{macrocode} |
1398 | % |
1399 | % \begin{macro}{\@snarfcolour} |
1400 | % |
1401 | % Reading a colour specification is something we'll need to do a few times, |
1402 | % so an abstraction is useful. Its single argument is a continuation to |
1403 | % which we pass a colour-spec acceptable to the |\color| command. (This is |
1404 | % the same code as found in the \package{mdwtab} package. Remember to keep |
1405 | % them in step.) |
1406 | % |
1407 | % \begin{macrocode} |
1408 | \def\@snarfcolour#1{% |
1409 | \@ifnextchar[{\@snarfcolour@i{#1}}{\@snarfcolour@ii{#1}{}}% |
1410 | } |
1411 | \def\@snarfcolour@i#1[#2]{\@snarfcolour@ii{#1}{[#2]}} |
1412 | \def\@snarfcolour@ii#1#2#3{#1{#2{#3}}} |
1413 | % \end{macrocode} |
1414 | % |
1415 | % \end{macro} |
1416 | % |
1417 | % \begin{macro}{\svcolourline} |
1418 | % \begin{macro}{\svcolorline} |
1419 | % |
1420 | % Snarf the option, and plot the coloured bar. Note the penalties which are |
1421 | % meant to stick the glue and leaders onto the colour specials. |
1422 | % |
1423 | % \begin{macrocode} |
1424 | \def\svcolourline{\@snarfcolour\svcl@i} |
1425 | \def\svcl@i#1#2{% |
1426 | \skip@\wd#2% |
1427 | \advance\skip@\parfillskip% |
1428 | \advance\skip@.2em% |
1429 | \strut% |
1430 | \kern.2em% |
1431 | \begingroup\color#1\nobreak\leaders\vrule\hskip\skip@\endgroup% |
1432 | \nobreak\hskip-\skip@% |
1433 | \kern.2em% |
1434 | \box#2% |
1435 | \nobreak\hskip-\rightskip\vadjust{}% |
1436 | \par% |
1437 | } |
1438 | \let\svcolorline\svcolourline |
1439 | % \end{macrocode} |
1440 | % |
1441 | % \end{macro} |
1442 | % \end{macro} |
1443 | % |
1444 | % Done! |
1445 | % |
1446 | % \begin{macrocode} |
1447 | %</colour> |
1448 | % \end{macrocode} |
1449 | % |
1450 | % \subsection{The \package{svsplit} package} |
1451 | % |
1452 | % \begin{macrocode} |
1453 | %<*split> |
1454 | \RequirePackage{sverb} |
1455 | % \end{macrocode} |
1456 | % |
1457 | % \begin{environment}{splitverb} |
1458 | % \begin{environment}{splitverb*} |
1459 | % |
1460 | % The basic environments are simple enough. |
1461 | % |
1462 | % \begin{macrocode} |
1463 | \def\splitverb{\listinghook\sv@readenv\splitverb@} |
1464 | \expandafter\def\csname splitverb*\endcsname% |
1465 | {\listinghook\begingroup\@noligs\svsplit@star} |
1466 | \def\svsplit@star#1{\endgroup\splitverb@{#1}{\end{splitverb*}}} |
1467 | % \end{macrocode} |
1468 | % |
1469 | % \end{environment} |
1470 | % \end{environment} |
1471 | % |
1472 | % \begin{macro}{\splitverb@} |
1473 | % |
1474 | % Even this isn't so bad, really. |
1475 | % |
1476 | % \begin{macrocode} |
1477 | \def\splitverb@#1#2{\sv@startlisting\sv@read{#1}\svsplit@line{\svafter#2}} |
1478 | % \end{macrocode} |
1479 | % |
1480 | % \end{macro} |
1481 | % |
1482 | % \begin{macro}{\svsplit@line} |
1483 | % |
1484 | % For the sake of readability (and maybe saving a few tokens), we define some |
1485 | % synonyms for \TeX's scratch registers. |\svsplit@remain| will be a |
1486 | % |\global| register containing the remaining horizontal space on the line; |
1487 | % |\svsplit@indent| is a local register containing the amount of initial |
1488 | % whitespace on the line. |
1489 | % |
1490 | % \begin{macrocode} |
1491 | \dimendef\svsplit@remain=1 |
1492 | \dimendef\svsplit@indent=2 |
1493 | % \end{macrocode} |
1494 | % |
1495 | % The switch |\svsplit@| is set if we've found a good place to split the |
1496 | % current line. |
1497 | % |
1498 | % \begin{macrocode} |
1499 | \newif\ifsvsplit@ |
1500 | % \end{macrocode} |
1501 | % |
1502 | % And finally a delimiter. This is the same one I use everywhere else. |
1503 | % |
1504 | % \begin{macrocode} |
1505 | \def\q@delim{\q@delim} |
1506 | % \end{macrocode} |
1507 | % |
1508 | % \begin{macrocode} |
1509 | \begingroup |
1510 | \catcode`\~=\active \lccode`\~=32 |
1511 | \catcode`\!=\active \lccode`\!=9 |
1512 | \lowercase{\endgroup |
1513 | % \end{macrocode} |
1514 | % |
1515 | % So far, so good. The |\svsplit@line| macro is given a line of text. We |
1516 | % initialize |\svtab| to be a \emph{single} space, |\svsplit@remain| to be |
1517 | % the text width, and |\svsplit@indent| to zero. Then we embark on the first |
1518 | % loop, which attempts to find the width of the leading whitespace. |
1519 | % |
1520 | % \begin{macrocode} |
1521 | \def\svsplit@line#1{% |
1522 | \divide\svtab8% |
1523 | \global\svsplit@remain\linewidth% |
1524 | \svsplit@indent\z@% |
1525 | \sv@emptybox\tw@% |
1526 | \let\next@\svsplit@findindent% |
1527 | \next@#1\q@delim% |
1528 | } |
1529 | % \end{macrocode} |
1530 | % |
1531 | % A straightforward tail-recursive loop finds out how much whitespace there |
1532 | % is at the start of the current line. Note that |\next@| is already set up |
1533 | % for the optimized case of continuing the loop. Also, if we reach the end |
1534 | % then this is a blank line, so only emit something if we didn't see the |
1535 | % end-marker. This is the only place we need to check for this. |
1536 | % |
1537 | % \begin{macrocode} |
1538 | \def\svsplit@findindent#1{% |
1539 | \ifx~#1% |
1540 | \advance\svsplit@indent\svtab% |
1541 | \else\ifx!#1% |
1542 | \dimen@8\svtab% |
1543 | \divide\svsplit@indent\dimen@% |
1544 | \multiply\svsplit@indent\dimen@% |
1545 | \advance\svsplit@indent\dimen@% |
1546 | \else\ifx\q@delim#1% |
1547 | \if@matched\else\svline\tw@\fi% |
1548 | \let\next@\relax% |
1549 | \else% |
1550 | \def\next@{\svsplit@scanline{#1}}% |
1551 | \fi\fi\fi% |
1552 | \next@% |
1553 | } |
1554 | % \end{macrocode} |
1555 | % |
1556 | % Now we have to actually scan the line to find breakpoints. We build the |
1557 | % current unbreakable chunk in |\box|~0. When we find a breakpoint, we close |
1558 | % the box, maybe stretch it to take into account trailing space, and attach |
1559 | % it to |\box|~2, which is gathering the current line. If |\svsplit@remain| |
1560 | % hits zero then we flush |\box|~2 to the output and continue on the next |
1561 | % line with a (more-or-less) clean slate. |
1562 | % |
1563 | % If there's no breakpoint then we're hosed. In that case, we just insert a |
1564 | % (|\normalfont|) hyphen and eject what we've got. |
1565 | % |
1566 | % Note that this assumes that the indentation will fit. If not, then we're |
1567 | % deeply stuffed. |
1568 | % |
1569 | % \begin{macrocode} |
1570 | \def\svsplit@scanline{% |
1571 | \svsplit@false% |
1572 | \let\next@\svsplit@char% |
1573 | \setbox\z@\hbox\bgroup% |
1574 | \kern\svsplit@indent% |
1575 | \global\advance\svsplit@remain-\svsplit@indent% |
1576 | \next@% |
1577 | } |
1578 | % \end{macrocode} |
1579 | % |
1580 | % Scanning a character isn't so bad, if we take it a step at a time. |
1581 | % |
1582 | % \begin{macrocode} |
1583 | \def\svsplit@char#1{% |
1584 | % \end{macrocode} |
1585 | % |
1586 | % If the character is a space or a tab, then we call |\svsplit@space| which |
1587 | % knows about adding breakable whitespace. For tabs, this involves computing |
1588 | % the correct tab size. |
1589 | % |
1590 | % \begin{macrocode} |
1591 | \ifx~#1% |
1592 | \svsplit@space\svtab% |
1593 | \else\ifx!#1% |
1594 | \@tempdima\linewidth% |
1595 | \advance\@tempdima-\svsplit@remain% |
1596 | \@tempdimb\@tempdima% |
1597 | \dimen@8\svtab% |
1598 | \divide\@tempdimb\dimen@% |
1599 | \multiply\@tempdimb\dimen@% |
1600 | \advance\@tempdimb\dimen@% |
1601 | \advance\@tempdimb-\@tempdima% |
1602 | \svsplit@space\@tempdimb% |
1603 | % \end{macrocode} |
1604 | % |
1605 | % We might have reached the end of the line. If so, then we finish off. |
1606 | % |
1607 | % \begin{macrocode} |
1608 | \else\ifx\q@delim#1% |
1609 | \let\next@\svsplit@done% |
1610 | % \end{macrocode} |
1611 | % |
1612 | % Otherwise it's a normal character. If there's not enough space then force |
1613 | % a break. |
1614 | % |
1615 | % \begin{macrocode} |
1616 | \else% |
1617 | \ifdim\svsplit@remain<2\svtab% |
1618 | \ifsvsplit@\else\normalfont-\svsplit@break\fi% |
1619 | \svsplit@eject% |
1620 | \fi% |
1621 | % \end{macrocode} |
1622 | % |
1623 | % Insert the character and decrement the distance-left register. |
1624 | % |
1625 | % \begin{macrocode} |
1626 | #1% |
1627 | \global\advance\svsplit@remain-\svtab% |
1628 | % \end{macrocode} |
1629 | % |
1630 | % Now we see if it's a breakable-after character and if so mark it as being |
1631 | % breakable. |
1632 | % |
1633 | % \begin{macrocode} |
1634 | \def\temp@##1#1##2\q@delim% |
1635 | {\ifx\q@delim##2\q@delim\else\svsplit@break\fi}% |
1636 | \expandafter\temp@\svsplitchars#1\q@delim% |
1637 | % \end{macrocode} |
1638 | % |
1639 | % And with that, we're done. |
1640 | % |
1641 | % \begin{macrocode} |
1642 | \fi\fi\fi% |
1643 | \next@% |
1644 | } |
1645 | % \end{macrocode} |
1646 | % |
1647 | % Our next macro is the break-insertion subroutine, which is quite easy. |
1648 | % |
1649 | % \begin{macrocode} |
1650 | \def\svsplit@break{% |
1651 | \egroup% |
1652 | \sv@addtobox\tw@\z@% |
1653 | \svsplit@true% |
1654 | \setbox\z@\hbox\bgroup% |
1655 | } |
1656 | % \end{macrocode} |
1657 | % |
1658 | % Now we add space to the current box. The argument is a dimen register. |
1659 | % |
1660 | % \begin{macrocode} |
1661 | \def\svsplit@space#1{% |
1662 | \ifdim\svsplit@remain>#1\kern#1\global\advance\svsplit@remain-#1\fi% |
1663 | \svsplit@break% |
1664 | \ifdim\svsplit@remain>#1\else\svsplit@eject\fi% |
1665 | } |
1666 | % \end{macrocode} |
1667 | % |
1668 | % We now come to a slightly involved piece of code, which is how to flush out |
1669 | % a line, and then fix up the registers for the next line correctly. |
1670 | % |
1671 | % \begin{macrocode} |
1672 | \def\svsplit@eject{% |
1673 | \egroup% |
1674 | \svline\tw@% |
1675 | \sv@emptybox\tw@% |
1676 | \svsplit@false% |
1677 | \setbox\z@\hbox\bgroup% |
1678 | \kern\svsplit@indent% |
1679 | \global\svsplit@remain\linewidth% |
1680 | \global\advance\svsplit@remain-\svsplit@indent% |
1681 | \global\advance\svsplit@remain-\wd\z@% |
1682 | \unhbox\z@% |
1683 | } |
1684 | % \end{macrocode} |
1685 | % |
1686 | % Finally, how to finish the line and go home. |
1687 | % |
1688 | % \begin{macrocode} |
1689 | \def\svsplit@done{% |
1690 | \egroup% |
1691 | \sv@addtobox\tw@\z@% |
1692 | \svline\tw@% |
1693 | } |
1694 | % \end{macrocode} |
1695 | % |
1696 | % End the |\lowercase| hack. |
1697 | % |
1698 | % \begin{macrocode} |
1699 | } |
1700 | % \end{macrocode} |
1701 | % |
1702 | % \end{macro} |
1703 | % |
1704 | % Finally, set the breakable characters to something plausible. |
1705 | % |
1706 | % \begin{macrocode} |
1707 | \def\svsplitchars{:/.} |
1708 | % \end{macrocode} |
1709 | % |
1710 | % And with that, we're done! |
1711 | % |
1712 | % \begin{macrocode} |
1713 | %</split> |
1714 | % \end{macrocode} |
1715 | % |
86f6a31e |
1716 | % \hfill Mark Wooding, \today |
1717 | % |
1718 | % \Finale |
1719 | % |
1720 | \endinput |