summaryrefslogtreecommitdiff
path: root/macros/generic/misc/verbatim.tex
blob: 11d684db6f3b200466d70c05fd6ce80aa0a0c68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
\catcode `\@ = 11               %%% we use commercial-at as a letter throughout;
\chardef \l@tter = 11           %%% and introduce synonyms for the \catcodes for
\chardef \@ther = 12            %%%  <letter> and <other>;
\newcount \c@unt                %%% a loop-counter;
\newcount \ch@rcode             %%% this will hold the character-code of the
                                %%%  escape character;
\newif \ifd@bugging             %%% set <true> if you want to watch the
                                %%% finite-state automaton at work;
\newif \ifshewleadingspaces     %%% set <true> if you want to see leading spaces
                                %%%  shewn as inverted square cup (explicit);
\newif \ifshewembeddedspaces    %%% set <false> if you want to see embedded 
\shewembeddedspacestrue         %%%  spaces shewn as white space (implicit);
\ifd@bugging                    %%% if <debugging>, 
    \let \m@ssage = \message    %%%  \m@ssage is synonymous with \message
\else                           %%% otherwise
    \def \m@ssage #1{}%         %%%  it simply throws its parameter away;
\fi
%
\def \verbatim                  %%% the \verbatim macro takes no parameter
    {\begingroup                %%% and immediately starts a nested group
     \tracingonline = 0
     \def \n@sted               %%% within which \n@sted is defined
         {\begingroup           %%% to start a further group within which
          \let \n@sted          %%% \n@sted becomes a synonym for \endgroup
                = \endgroup  
          \@nvironment          %%% and the environment is restored to that
                                %%% which obtained two levels of nesting out;
          \ignorespaces         %%% for tidyness, we ignore any <lwsp>
         }%                     %%% which follows the escape character;
     \edef \@nvironment         %%% we initialise \@nvironment 
          {\the \font           %%% who forgot this ?????
           \parindent =         %%% to prepare to restore \parindent 
               \the \parindent 
           \parskip =           %%% and \parskip;
                 \the \parskip 
           \space               %%% and ensure that the value to be assigned to
          }%                    %%% \parskip is properly terminated;
     \displayverbatimfont       %%% we assume Knuth's font-selectors and
                                %%% select the `typewriter' font;
     \parskip = 0 pt            %%% we then set \parindent and 
     \parindent = 0 pt          %%% \parskip to 0 pt;
     \c@unt = 0                 %%% and initialise \count to 0;
     \loop                      %%% this loop checks the \catcode of each
                                %%% character code in the range 0 .. 127 
                                %%% (or 0 .. 255 for TeX V3) and if it
                                %%% is other than <letter> or <other>, as
                                %%% appropriate, saves the current value in 
                                %%% \@nvironment for subsequent restoration 
                                %%% within an inner group; it then sets the
                                %%% \catcode to either <letter> or <other>;
            \ifnum \c@unt < `\A
                   \s@ve \catcode \c@unt = \@ther
            \else  \ifnum \c@unt > `\z 
                          \s@ve \catcode \c@unt = \@ther
                   \else  \ifnum \c@unt > `\Z
                                 \ifnum \c@unt < `\a
                                        \s@ve \catcode \c@unt = \@ther
                                 \else  \s@ve \catcode \c@unt = \l@tter
                                 \fi
                          \else  \s@ve \catcode \c@unt = \l@tter
                          \fi
                   \fi
            \fi
            \advance \c@unt by 1
     \ifnum \c@unt < 256 % or 128 for the V2 sites ...
     \repeat
     \v@rbatim
    }
%
\def \v@rbatim #1%              %%%
    {\if  #1                    %%%
         \let \n@xt = \v@rb@tim %%%
     \else                      %%% 
         \def \n@xt             %%%
                {\v@rb@tim #1}% %%%
     \fi                        %%%
     \n@xt                      %%%
    }                           %%%
%
\def \v@rb@tim #1%
    {\ch@rcode = `#1            %%%
     \edef \@nvironment         %%% and append code to \@nvironment 
        {\@nvironment           %%% to make the escape character active;
         \catcode \the \ch@rcode 
                = \active 
         \space                 %%% <space> separates list items in \@nvironment
        }%                      
     \catcode \ch@rcode         %%% the escape character is made active;
                = \active 
     \uccode `\^^M = \ch@rcode  %%% and the upper-case code of <return> is made
                                %%% equal to the character-code of the escape
                                %%% character; this is necessary because only
                                %%% <return> can be guaranteed to be active at
                                %%% this point, and we need an active character
                                %%% to form the primary operand of \def;
                                %%% the \@x's below are \expandafter's,
                                %%% and the effect is to upper-case <return>
                                %%% (yielding the escape character), then \def
                                %%% (an active instance of) this character as
                                %%% \n@sted, which has been defined above;
%
     \@x \uppercase \@x {\@x \def \r@turn {\n@sted}}%
%
     \uccode `\^^M = 0          %%% the upper-case code of <return> is then re-
                                %%% instated (not strictly true; it is set to 0,
                                %%% which is assumed to be its previous value
                                %%% --- could be improved here); 
     \@ctivecr                  %%% <return> is made active;
     \@ctivespace               %%% and so is <space> (to avoid space-elision);
                                %%% finally, the finite-state automaton which 
    }%                          %%% processes <space>s is set to <????>;
                                %%% this ends the definition of \verbatim.
%
\def \s@ve #1#2=#3%             %%% \s@ve minimises the \catcode restoration
    {\ifnum #1#2 = #3%          %%% work of \@nvironment by saving only the
                                %%% \catcode of characters whose \catcode
                                %%% is to be changed; it then changes the
                                %%% \catcode of those characters.
     \else \edef \@nvironment
                     {\@nvironment #1\the #2=\the #1#2 }%
           #1#2 = #3%
     \fi
    }%
                                %%% the code which follows implements the finite
                                %%% state automaton which determines whether 
                                %%% <space>s are ignored, shewn explicitly or 
                                %%% implied, and which ensures that blank
                                %%% lines are reproduced correctly.
%
\def \v@id {\futurelet \n@xt \v@idifspace}%
\def \l@ad {\l@adingspace \futurelet \n@xt \l@adifspace}%
\def \sk@p {\vskip \baselineskip \futurelet \n@xt \l@adifspace}%
\def \emb@d {\emb@ddedspace}%
\def \sh@wspace {\char 32\relax}%
\def \h@despace {\leavevmode \kern \fontdimen 2 \font}%
\def \l@adingspace {\ifshewleadingspaces \sh@wspace \else \h@despace \fi}%
\def \emb@ddedspace {\ifshewembeddedspaces \sh@wspace \else \h@despace \fi}%
\def \v@idifspace {\testn@xt {\afterassignment \v@id}}%
\def \l@adifspace {\testn@xt {\afterassignment \sk@p}}%
%
                                %%% \testn@xt provides a common look-ahead for
                                %%% \v@idifspace and \l@adifspace, and also
\def \testn@xt #1%              %%% implements some essential debugging hooks.
    {\ifx  \n@xt \sp@c@         
           \m@ssage {Next character is a space}%        
           \let \n@xt = \relax
     \else \ifx  \n@xt \r@t@rn
                 \m@ssage {Next character is a return}%
                 \def \n@xt {#1\let \n@xt = }%
           \else \m@ssage {Next character is \meaning \n@xt}%
                 \let \n@xt = \relax
                 \@x \let \sp@ce = \emb@d
           \fi
     \fi
     \n@xt
    }%
%
                                %%% We next tamper with the \catcode of <space>
                                %%% and <return>, while defining macros and
                                %%% synonyms which require them to be active;
                                %%% the \catcode is then restored to its default
                                %%% (not necessarily the previous value --- 
                                %%% could be improved). \@ctivespace makes 
                                %%% <space> active, then defines <space> as 
\catcode `\ = \active%          %%% \v@id with a synonym \sp@c@.  This code is
\def\sp@ce{ }%                  %%% used by the finite-state automaton.
\def\@ctivespace%
{\catcode`\ =\active\def {\v@id}\let\sp@c@= }\catcode`\ =10\relax%
\catcode`\ =10\relax%
%               
\catcode `\^^M = \active %      %%% <return> is made active;
\def \r@turn {^^M}%             %%% \r@turn defined as an active <return>;
\let \r@t@rn = ^^M%             %%% \r@t@rn is made a synonym;
\def \@ctivecr %                %%% and \@ctivecr is defined to
    {\catcode `\^^M = \active % %%% make <return> active, then
     \def ^^M%                  %%% define <return> to manipulate the 
                                %%% finite-state automaton and ...
         {\@x \def \sp@ce {\l@ad}%
          \@x \let \@x \sp@c@ \@x =\sp@ce %
%
          \endgraf %            %%% insert a \par primitive (for blank lines).
%
          \futurelet \n@xt \l@adifspace %
         }%
     \let \r@t@rn = ^^M%        %%% \r@t@rn is synonymous with active <return>
    }%
\catcode `\^^M = 5 %            %%% finally, the \catcode of <return> is 
                                %%% restored to its normal value;
%
                                %%% the \AfterGroup macro is intended for
                                %%% use within a nested normal environment,
                                %%% and causes (a concealed macro defined as)
                                %%% its parameter text to be inserted into
                                %%% TeX's input stream when the nested normal
                                %%% group terminates.
%
\def \AfterGroup #1{\global \def \@ftergroup {#1}\aftergroup \@ftergroup}%
%
\let \@x = \expandafter         %%% \@x is a brief synonym for \expandafter;
%
%
\def \mitabrev                  %%% and \mitabrev defined as the closure for
    {\endgroup \endgroup}%      %%% \verbatim; any other name could be used,
                                %%% as the code performs no look-ahead for
                                %%% any particular string.
%
                                %%% Finally we announce to the world that we
                                %%% have been loaded, and gives some clues as
                                %%% to the usage.
%

\catcode `\Ý = \active
\catcode `\| = \active

\def Ý{\begingroup 
       \def \s@lidus ##1Ý{\inlineverbatimfont ##1\endgroup}%
       \p@ssivate \activesp@ces \s@lidus
      }

\def |{\begingroup 

       \def \s@lidus ##1|{\inlineverbatimfont ##1\endgroup}%
       \p@ssivate \activesp@ces \s@lidus
      }

\def \p@ssivate 
       {\tracingonline = 0
        \c@unt = 0
	\loop
		\ifnum 	\catcode \c@unt = 11 {}%
		\else 	\ifnum	\catcode \c@unt = 12 {}%
			\else	\catcode \c@unt = 12
			\fi
		\fi
	\ifnum
		\c@unt < 255
		\advance \c@unt by 1
	\repeat
	\catcode `\Ý = \active
	\catcode `\| = \active
       }

\begingroup
\catcode `\ = \active\gdef\activesp@ce{\def {\phantom{\char`\ }}}%
\endgroup

\def \activesp@ces {\catcode `\ = \active \activesp@ce}

\catcode `\@ = \@ther           %%% commercial-at is restored to its normal
                                %%% <other> catcode (not necessarily the
                                %%% previous value --- could be improved);

\message {Verbatim environment loaded;}%
\message {usage: ``\noexpand \verbatim <char> ... <char> \noexpand \mitabrev''}%
\message {less functional short forms: \string Ý...\string Ý & 
                                                \string |...\string |}%

%%% ======================================================================== %%%