\chardef\escape=0
\chardef\subscript=8
\chardef\letter=11
\chardef\other=12
%\chardef\active=13		 % is defined in Plain already

\chardef\atcode=\catcode`\@
\chardef\uscode=\catcode`\_

\catcode`\@=\letter
\catcode`\_=\letter
\font\tentex=cmtex10		% typewriter extended ASCII 10pt
\let\ttex=\tentex		% only for PLAIN with base size 10pt

\def\setup_verbatim{%
   \def\do##1{\catcode`##1\other}\dospecials
   \parskip\z@skip \parindent\z@
   \obeylines \obeyspaces \obeytabs \frenchspacing
   \ttex
   }

\let\tab=\space
\begingroup
   \catcode`\^^I=\active%	% Attention: no tabs!
   \gdef\obeytabs{\catcode`\^^I=\active\def^^I{\tab}}
   \global\let^^I=\tab% 	% if an active tab appears in a \write
\endgroup
\let\origvert=|
\chardef\vbar=`\|

\catcode`\|=\active

\def|{%
   \leavevmode
   \hbox\bgroup
      \let\par\space \setup_verbatim
      \let|\egroup
   }
\newif\if@print

\def\begin_verbatim{%
   \endgraf
   \bigbreak
   \begingroup
      \setup_verbatim \catcode`\|\other
      \@printtrue
      \ignore_rest_line
   }
\let\end_verbatim=\endgroup		% internal command !
\begingroup
   \obeylines%		% ^^M is active! ==> every line must end with %
   \gdef\ignore_rest_line#1^^M{\set_next_line}%
   \gdef\set_next_line#1^^M{\do_set{#1}}%
\endgroup

\def\do_set#1{%
   \endgraf
   \check_print{#1}%
   \if@print  \indent \print_char#1\end_line\end_line
   \else  \let\set_next_line\end_verbatim
   \fi
   \set_next_line
   }
\let\end_line=\relax
\begingroup
\obeyspaces\obeytabs
\gdef\check_print#1{\cut_at_tab#1^^I\end_line}
\gdef\cut_at_tab#1^^I#2\end_line{\check_first_part#1 \end_line}% blank !
\gdef\check_first_part#1 #2\end_line{\do_check{#1}}
\endgroup
\def\do_check#1{%
   \def\@line{#1}%
   \ifx \@line\end_verbatim_line  \@printfalse
   \fi
   }
\newcount\char_count  \char_count\z@

\def\print_char#1#2\end_line{%
   \print_first_char{#1}%
   \print_rest_of_line{#2}%
   }
{\obeytabs\gdef\@tab{^^I}}

\def\print_first_char#1{%
   \def\@char{#1}%
   \advance \char_count\@ne
   \ifx \@char\@tab  \print_tab
   \else  \@char
   \fi
   }
\newcount\count_mod_viii
\def\mod_viii#1{%
   \count@ #1\relax  \count_mod_viii\count@
   \divide \count@ 8\relax
   \multiply \count@ 8\relax
   \advance \count_mod_viii -\count@
   }
\def\print_tab{%
   \loop  \space \mod_viii\char_count
      \ifnum \count_mod_viii>\z@
	 \advance \char_count\@ne
   \repeat
   }
\def\print_rest_of_line#1{%
   \def\@line{#1}%
   \ifx \@line\empty  \char_count\z@
	\def\next##1\end_line{\relax}%
   \else  \let\next\print_char
   \fi
   \next#1\end_line
   }
{\catcode`\/=\escape  \catcode`\\=\other % / is temporary escape char
   /gdef/beginprog{%
      /gdef/end_verbatim_line{\endprog}%
      /begin_verbatim
      }
   /gdef/beginverbatim{%
      /gdef/end_verbatim_line{\endverbatim}%
      /begin_verbatim
      }
}					 % here \endgroup can't be used
\newcount\sectno  \sectno=\@ne

\newtoks\title  \title{}
\def\toc_file_name{\jobname.toc }      % <--- Space!!
\newwrite\toc_file

\immediate\openout\toc_file=\toc_file_name
\immediate\write\toc_file{\relax}

\let\tocline=\relax
\headline{%
   \tenrm
   \S\topmark \qquad \the\title
   \hfil
   \main_title \qquad \tenrm \folio
   }
\footline{\hfil}
\vbox to \vsize{}

\output={%
   \setbox\z@ \vbox{\unvbox\@cclv}%    % throw away the first page
   \global\output{\plainoutput}%       % restore Plain output routine
   }
\def\chap#1.{%
   \endgraf
   \mark{\number\sectno}%        % gather information for headline
   \vfill\supereject
   \begingroup
      \def\protect##1{\string ##1 }%   % <--- Space!!
      \edef\write_toc_sectno{\write\toc_file{\tocline{\number\sectno}}}%
      \write_toc_sectno
      \write\toc_file{{#1}{\folio}}%
   \endgroup
   \gdef\main_title{#1}%         % more information for headline
   \noindent {\bf \number\sectno.\quad #1.}%
   \global\advance \sectno\@ne
   \endgraf
   \medskip  \nobreak
   \everypar{%                         % remove next parindent
      \setbox0\lastbox
      \global\everypar{}%
      }%
   }
\def\sect{%
   \endgraf
   \mark{\number\sectno}%        % gather information for headline
   \vskip 2pc plus 1pc minus 6dd  \goodbreak
   \noindent {\bf \number\sectno.}\quad \ignorespaces
   \global\advance \sectno\@ne
   }
\def\byebye{\par\vfil\supereject\end}    % original definition of \bye
\def\bye{%
   \endgraf
   \vfill \supereject
   \immediate\closeout\toc_file
   \message{Table of contents:}%
   \leftskip\z@skip  \rightskip\z@skip
   \parskip\z@skip  \parindent\z@  \parfillskip\z@skip
   \normalbaselines
   \headline{\hfil}%
   \footline{\hfil \folio \hfil}%
   \def\HeaderToc{\rightline{Section\hbox to\thr@@ em{\hss Page}}}%
   \def\tocline##1##2##3{%
      \line{%
	 \ignorespaces ##2%
	 \leaders\hbox to .5em{.\hfil}\hfil
	 \ ##1%
	 \hbox to \thr@@ em{\hss ##3}%
	 }%
       }
   \pageno\m@ne        % table of contents gets roman page numbers
   \topofcontents      % may redefine \HeaderToc and \tocline!
   \HeaderToc
   \catcode`\@\letter  % @ and _ are letters (for reading private macros)
   \catcode`\_\letter
   \input \toc_file_name
   \catcode`\@\atcode  % @ and _ are no more letters
   \catcode`\_\uscode
   \botofcontents
   \byebye		       % print the contents page(s) and terminate
   }

\font\titlefont=cmr7 scaled\magstep4	% title on the contents page
\font\ttitlefont=cmtt10 scaled\magstep2 % typewriter type in title
\def\topofcontents{
   \null \vfill
   \centerline{\let\tt\ttitlefont \titlefont \the\title}
   \vskip 1cm
   }
\def\botofcontents{
   \vfill\vfill
   }
\catcode`\@=\atcode
\catcode`\_=\uscode

\endinput

.
