% program example environment, based on tabverbatim
% by Michael Fine.  Resists many devious things.
%
% 2 March 1989	Lily Mummert (lily) at Carnegie Mellon University.
%		Created.
%

\newlength{\tabwidth}
\settowidth{\tabwidth}{\small\tt mm}
	% default is 2 characters of \tt font in \small
 
\begingroup
\catcode`\^^I=\active\def^^L{}
 
\gdef\@prog{\@verbatim
    \small	
    \def\par{\egroup\@@par\noindent
    \@tempdima 0pt\hbox\bgroup{}}\obeylines
    \let\ignorespaces\relax    % for \@gobblecr
    \catcode`\^^I\active
    \def^^I{\egroup\setbox\@tempboxa\lastbox\@tempdimb \wd\@tempboxa
    \box\@tempboxa
    \advance\@tempdima by \@tempdimb \computeskip\penalty10000
    \hskip\@tempdimb\advance\@tempdima by \@tempdimb
    \hbox\bgroup{}}\def^^L{\egroup\setbox\@tempboxa\lastbox 
    \@tempdimb\wd\@tempboxa
    \ifdim\@tempdimb=0pt\else\box\@tempboxa\fi
    {\let\par\@@par\newpage}\noindent\hbox\bgroup\@gobblecr}\relax
    \def\computeskip{\@tempcnta 0\@tempdimb 0pt\loop
        \ifdim \@tempdimb<\@tempdima
        \advance\@tempdimb by \tabwidth \advance \@tempcnta1 \repeat
    \ifdim\@tempdima=\@tempdimb \advance\@tempcnta1\fi
    \@tempdimb\@tempcnta \tabwidth \advance \@tempdimb
    by -\@tempdima}}
 
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xprog#1\end{prog}[#1|end[prog]]
|gdef|@sxprog#1\end{prog*}[#1|end[prog*]]
|endgroup

\gdef\prog{\@prog\frenchspacing\@vobeyspaces
    \@xprog\noindent\hbox\bgroup\@gobblecr}
 
\gdef\endprog{\egroup\setbox\@tempboxa\lastbox 
    \@tempdimb\wd\@tempboxa
    \ifdim\@tempdimb=0pt\else\box\@tempboxa\fi
    \let\par\@@par\endtrivlist}
 
%
% Now we define \inputprog
%
% This is similar to \prog but does not need \@xprog trickery
% to delimit the begining and end of the prog text.
 
\gdef\inputprog#1{{\@prog\frenchspacing\@vobeyspaces
    \noindent\hbox\bgroup\input{#1}\endprog}}
 
\endgroup

