%%% Most recent mods:  19-MAY-1994 15:26:00 
%%% Improved use of \displayverbatimfont and \inlineverbatimfont

\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
     \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 don't assume Knuth's font-selectors;
                                %%% select the `large 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;
%
\catcode `\@ = \@ther           %%% commercial-at is restored to its normal
                                %%% <other> catcode (not necessarily the
                                %%% previous value --- could be improved);
%
\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.
%
\message {Verbatim environment loaded;}%
%\message {usage: ``\noexpand \verbatim \noexpand <char\noexpand > ... \noexpand
%                <char\noexpand > \noexpand \mitabrev''}%
%
\ifx \tt \undefined
     \font \displayverbatimfont = cmtt10
     \font \inlineverbatimfont = cmtt10
\else
     \let \displayverbatimfont = \tt
     \let \inlineverbatimfont = \tt
\fi
%
%%% ======================================================================== %%%
%
\begingroup
\catcode `\@ = 11
\global \catcode `\| = \active
\global \def |{\begingroup \p@ssivate \s@lidus} %%% for | ... |
\global \def \s@lidus #1|{\inlineverbatimfont #1\endgroup}
\global \def \p@ssivate 
       {\count 255 = 0
        \loop
                \ifnum \catcode \count 255 = 11 {}%
                \else  \ifnum   \catcode \count 255 = 12 {}%
                       \else    \catcode \count 255 = 12
                       \fi
                \fi
        \ifnum
                \count 255 < 255
                \advance \count 255 by 1
        \repeat
        \catcode `\| = \active
        \catcode `\  = \active
       }
\endgroup
%
% {\catcode `\ = \active\global\def {\leavevmode\kern\fontdimen2\font}}
