%%  8 JUN 93: this file needed to run Johannes Braams' article in TTN
%%            2,3 (Ch.)


%%  7 JUL 93: This file renamed  ttn2n3-gather.sty  to show its
%%            relationship with TTN 2,3 files (Ch.)

% gather.sty

%
% The following macro hackery I use to extract the macrocode
% from the source of the article in order to use it later on.
%
\newwrite\codefile     % To gather all the code together
\newwrite\tmpfile      % Temporary file for typesetting the code
%
{\catcode`\%=12 \gdef\comment{%% }}
\chardef\other=12
\def\myverbatim{\begingroup
   \catcode`\\=\other\catcode`\&=\other\catcode`\$=\other
   \catcode`\{=\other\catcode`\}=\other\catcode`\#=\other
   \catcode`\^=\other\catcode`\~=\other\catcode`\%=\other
   \catcode`\"=\other\catcode`\<=\other\catcode`\>=\other
   \catcode`\_=\other\catcode`\|=\other \obeyspaces}
% Macro to write code to the codefile *and* to the temporary file
\def\spaces{\space\space\space\space}
%
% DOUBLE DANGEROUS BEND::
% I make the ^^M an active character
% I use it to write macrocode line by line to the files.
\begingroup
 \catcode`\^^M\active% Hide all ^^M's from TeX...
%
% I also have to temporarily change some other category codes,
% in order to be able to scan for the line containing
% the string "\end{code}"
%
 \begingroup%
   \catcode`\[=1  \catcode`\]=2  \catcode`\|=0%
   \catcode`\{=12 \catcode`\}=12 \catcode`\\=12%
   |gdef|writeit#1^^M[|endgroup|def|tempa[#1]|def|tempb[\end{code}]%
               |ifx|tempa|tempb%
                   |def|next[|end[code]]%
               |else%
                 |def|next[|immediate|write|codefile[#1]%
                           |immediate|write|tmpfile[|spaces#1]^^M]%
               |fi|next]%
 |endgroup%
% Here is a new LaTeX environment which can contain TeX macrocode
% The code will be written to two files, the file to gather all code
% in and the temporary fill that is \input at the end of the
% environment. In that file the code will be in a normal verbatim
% environment.
 \gdef\code{\bgroup\catcode`\^^M\active%
     \def^^M{\myverbatim\writeit}%
     \immediate\openout\tmpfile=tmp%
     \immediate\write\tmpfile{\string\begin{verbatim}}}%
% And an environment to typeset the examples
 \gdef\example{\bgroup\catcode`\^^M\active%
     \def^^M{\myverbatim\writeit}%
     \immediate\openout\tmpfile=tmp%
     \immediate\openout\codefile=tmpb%
     \immediate\write\tmpfile{\string\begin{verbatim}}}%
\endgroup%         Now the ^^M is no longer active
\def\endcode{\egroup
   \immediate\write\tmpfile{\string\end{verbatim}}
   \immediate\closeout\tmpfile
   \input{tmp}}
\def\endexample{\egroup
   \immediate\write\tmpfile{\string\end{verbatim}}
   \immediate\closeout\tmpfile
   \immediate\closeout\codefile
   \leavevmode
   \begin{minipage}[c]{.45\textwidth}\small\input{tmp}\end{minipage}%
   \hfil
   \begin{minipage}[c]{.45\textwidth}\small\input{tmpb}\end{minipage}}
\def\startgather#1{%
   \immediate\openout\codefile=#1
   \immediate\write\codefile{\comment File  : #1.tex}
   \immediate\write\codefile{\comment Author: J.L. Braams}
   {\let~\space
   \immediate\write\codefile{\comment Date  : \today}}
}
\def\endgather{%
   \immediate\write\codefile{\string\endinput}
   \immediate\write\codefile{\comment the rest of this file is ignored.}
   \immediate\closeout\codefile}
 \def\startexamples{\let\code\example\let\endcode\endexample
   \def\spaces{}}