]> mj.ucw.cz Git - ucwmac.git/blob - ucw-verb.tex
Let ~ be protected
[ucwmac.git] / ucw-verb.tex
1 % The UCW Macro Collection: Verbatim environments
2 % Written by Martin Mares <mj@ucw.cz> in 2010 and placed into public domain
3 % -------------------------------------------------------------------------
4
5 % We'll use internal macros of plain TeX
6 \catcode`@=11
7
8 % New \frenchspacing, which doesn't leave unwanted spaces in text.
9 \def\frenchsp@cing{\sfcode`\.\@m \sfcode`\?\@m \sfcode`\!\@m%
10 \sfcode`\:\@m \sfcode`\;\@m \sfcode`\,\@m}
11
12 % Typesetting of one verbatim word: |word| (enable by \inlineverbon)
13
14 % Set if spaces should be rendered as "bath-tub" glyphs
15 \newif\ifshowspaces
16 \showspacesfalse
17
18 \def\make@ther#1{\catcode`#1=12\relax}
19 \def\setverb@tim{\tt\frenchsp@cing\catcode`\`=13\catcode`\'=13\n@ligs%
20 \let\do=\make@ther\dospecials\obeyspaces\showsp@ces}
21
22 {\catcode`'=13
23 \catcode``=13
24 \gdef\n@ligs{\def`{\relax\lq}\def'{\relax\rq}}}
25
26 {\obeyspaces
27 \global\chardef\sp@ce=32
28 \gdef\showsp@ces{\ifshowspaces\let =\sp@ce\fi}
29 \global\let =\ }
30
31 {\catcode124=\active % Make | active
32 \gdef\ttst@rt{\begingroup\setverb@tim\let|=\endgroup}
33 \gdef\inlineverbon{\catcode124=\active\let|=\ttst@rt}
34 \gdef\inlineverboff{\catcode124=12}
35 }
36
37 % Typesetting of verbatim text with a given delimiter:
38 %       - usage: \verb%verbatim text%
39 %       - the delimiter is the first character after \verb
40 %       - remember that writing (for example) \verbz with meaning \verb
41 %         with the delimiter 'z' is possible only if \catcode`z is not 11
42
43 \def\verb{\relax\def\sv@rb##1{\uccode`~=`##1%
44 \uppercase{\catcode`~13\global\let~\endgroup}}%
45 \begingroup\setverb@tim\do\|\sv@rb}
46
47 % Typesetting of verbatim blocks:
48 %       - the block is enclosed in balanced {}'s
49 %       - leading and trailing empty lines are ignored
50 %       - all other empty lines are converted to \verbemptylineskip's
51 %       - tabs are interpreted (one tab stop per \verbtabsize columns)
52 %       - \showspacestrue works here, too, but it does not apply to tabs
53
54 \newskip\verbstartskip          % vskip before \verbatim
55 \newskip\verbendskip            % vskip after \verbatim
56 \newskip\verbinterlineskip      % between adjacent non-empty lines
57 \newskip\verbemptylineskip      % instead of every empty line
58 \newskip\verbleftskip           % left and right margin
59 \newskip\verbrightskip
60
61 \verbstartskip=3pt plus 1pt minus 0.3pt
62 \verbendskip=\verbstartskip
63 \verbinterlineskip=0pt
64 \verbemptylineskip=5pt plus 2pt
65 \verbleftskip=0in
66 \verbrightskip=0pt plus 1fil
67
68 % Can be re-defined to customize the verbatim environment
69 \def\verblocaldefs{}
70
71 % Size of a single tab
72 \newcount\verbtabsize
73 \verbtabsize=8
74
75 \newdimen\v@rbspace
76 \newdimen\t@b
77 \newdimen\t@bwidth
78
79 % When a complete line is assembled in \box0, \verbship is called to add it
80 % to the main vertical list. You can override it to get e.g. line numbering.
81 \def\verbship{%
82         \ifdim\wd0>0pt
83                 \ifdim\v@rbspace>0pt
84                         \penalty-100
85                         \vskip\v@rbspace
86                 \else
87                         \ifdim\v@rbspace=0pt
88                                 \vskip\verbinterlineskip
89                         \fi
90                 \fi
91                 \line{\hskip\verbleftskip \box0 \hskip\verbrightskip}
92                 \v@rbspace=0pt
93         \else
94                 \advance\v@rbspace by \verbemptylineskip
95         \fi
96 }
97
98 \newcount\verbcnt
99 \def\v@rbend{\par\egroup\endgroup\vskip\verbendskip}
100 \def\v@rbl{\ifnum\verbcnt>0\{\fi\global\advance\verbcnt by 1\relax}
101 \def\v@rbr{\ifnum\verbcnt>1\}\else\v@rbend\fi\global\advance\verbcnt by -1\relax}
102
103 \def\v@rbparams{%
104         \setverb@tim
105         \raggedbottom
106         \verbcnt=0
107         \v@rbspace=-1000pt
108         \catcode124=12
109         \vskip\verbstartskip
110         \let\par=\endb@x
111         \obeylines
112 }
113
114 \def\startb@x{\setbox0=\hbox\bgroup}
115 \def\endb@x{\egroup\verbship\startb@x}
116
117 {\catcode`\^^I=\active
118 \gdef\setupt@bs{\catcode`\^^I=\active
119 \setbox0=\hbox{\ }\t@bwidth=\wd0\multiply\t@bwidth by \verbtabsize
120 \def^^I{\egroup\t@b=\wd0\divide\t@b by \t@bwidth%
121 \multiply\t@b by \t@bwidth%
122 \advance\t@b by \t@bwidth\advance\t@b by -\wd0\startb@x\box0\hbox to \t@b{}}%
123 }}
124
125 {
126 \catcode`[=1\catcode`]=2\catcode123=\active\catcode125=\active
127 \gdef\verbatim[\begingroup\v@rbparams\catcode123=\active\catcode125=\active%
128 \let{=\v@rbl\let}=\v@rbr\chardef\{=123\chardef\}=125\verblocaldefs\setupt@bs\startb@x%
129 ]]
130
131 % Input file verbatim
132
133 \def\verbinput#1{\begingroup\v@rbparams\verblocaldefs\setupt@bs\startb@x\input #1 \egroup\endgroup}
134
135 % Let's hide all internal macros
136 \catcode`@=12