%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% verbasef.sty
%
% This stands for VERBatim Automatic Splitting of External Files.
%
% Copyright (c) 1994 by Paul A. Thompson, All Rights Reserved.
% Copy freely as per the GNU Copyleft.
%
% Requires:
%   The New Font Selection Scheme (NFSS) as implemented in LaTeX2e
%   VERBATIM.STY  :::  Rainer Schoepf
%   VRBEXIN.STY   :::  You 
%                      However, it is based on vrbinput.sty, a style by 
%                      Bernd Raichle which must be revised as discussed below
%                      in the section labeled IMPORTANT
%   HERE.STY      :::  David Carlisle
%
% There is a ifsubstr macro used below which is based on code from Donald
% Arseneau. 
%
% verbasef allows you to input (subsections of a) file, print them in
% verbatim mode, while automatically breaking up the inputted lines into
% pieces of a given length, which are output as figures.  These figures are
% posted using the [H] specification, which forces LaTeX to place the figure
% at the spot of invocation, rather than floating the figures to the top of
% the next page.
% 
% Options for the verbasef specification include:
%  o numbering of input lines
%  o differential number of lines for the first page
%  o specification of the font for the verbatim output
%  o specification of the font for the optional line numbering
%  o specification of line numbers as labels 
%    To use the labelling feature, non-standard approaches must be used.
%    A line number will be inserted into the main .aux file with label foo,
%    if the specification 
%   
%    Vzzlabel|foo|
%
%    is inserted AFTER the line which is to be indicated.  Thus, if we have 
%    a file with the following
%    
%    a line
%    b line
%    Vzzlabel|fooa|
%    c line
%    Vzzlabel|foob|
%    d line
%    Vzzlabel|fooc|
%    e line
%    f line
%    Vzzlabel|food|
% 
%    these lines will be found in the .aux file.
%    \newlabel{fooa}{{2}{1}}
%    \newlabel{foob}{{3}{1}}
%    \newlabel{fooc}{{4}{1}}
%    \newlabel{food}{{6}{1}}
%
%    They can be used as any other label, for instance 
%
%    Using the labeling feature, pay attention to Line \ref{food} (found on
%    Page \pageref{food}) to understand this radically new method.
%
%    Any lines with the Vzzlabel specification will be ignored in the 
%    line counts.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% USING THE styLE 
%
% verbasef is an adaptation of VRBSUBFILE, from Norman Walsh.  
% I fully acknowledge Mr./Dr. Walsh as the imprimature of the code ensconced
% herein, but have included other features as indicated above.
%
% Comments and suggestions always welcome.
%   Paul A. Thompson
%   Department of Psychiatry, Data Analysis and Statistics Section
%   Case Western Reserve University
%   Cleveland, OH    44106
%
%   AudioNet: (216) 844-8946
%   InterNet: pat@po.cwru.edu
%
% User interface:
%   VautoSubF[#1]#2#3#4#5#6
%     Input lines #2 to #3 of file #4.  If present, #1 indicates which lines
%     should be numbered.  For example, if #1 is 5, lines 5, 10, 15, etc.
%     will be numbered.  By default, lines are not numbered at all.
%     #5 is figure caption, and #6 is figure label.
%
%   VautoSfFont[#1]#2
%     Use font #2 for verbatim input lines and, if present, use font #1 for
%     line numbers.  By default, #2 is \tt and #1 is \rm\tiny.
%   
%   VautoPl#1
%     Placement of the figure (either H or some other placement character)
%     By default, placement is H (immediate here, here.sty) 
%     (This currently does not function.  All placement is done using the 
%     H specification.)
%
%   VautoLines[#1]{#2}
%     #2 is number of lines per page.  If #1 is included, it is the number of
%     lines for the first page.  If not, #1=#2
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In order to use the verbasef.sty file, VRBINPUT.STY must be MODIFIED
% and given a NEW NAME.  Here are instructions for modification:
%
% 1. Obtain vrbinput.sty from the standard distribution channels.  The file 
%    should have the following characteristics:
%
%    \fileversion{v1.0b}
%    \filedate{91/06/30}
%    \docdate{91/08/05}
%
% 2. Copy vrbinput.sty to a file vrbexin.sty.  THIS MUST BE DONE PRIOR TO
%    EDITING ANY FILE.
% 
% 3. Edit the file vrbexin.sty.  Please don't edit the file vrbinput.sty.
%    Change Line 90 from 
%    \verbatim@startline 
%    to 
%    \verbatim@start 
%    as shown in the fragment of code from Lines 89-92 below.
%
%  \def\verbatim@readfile#1{%
%     \verbatim@start
%     \openin\verbatim@in@stream #1\relax
%     \ifeof\verbatim@in@stream
%
%
% 4. VERY IMPORTANT.  CHANGE ALL OCCURRANCES of vrbinput, in titles, typeout
%    sections and credits to vrbexin.  Remove all references to the original
%    author, Bernd Raichle, as he is not the author of the style file vrbexin.
%    You may substitute my name for Mr. Raichle if you wish.
%
% 5. Place this .sty file in a place searched by TeX, on the TEXINPUT path.
%
% After I can figure out the docstrip utility, I will do this stuff myself.
%
% Note: As is standard practice in the TeX community, any modifications in 
% .sty files must be given new names.  Thus, it is certainly vitally important
% that the modifications in vrbinput.sty be done in the file named
% vrbexin.sty, not in the original which I did not write.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
% IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT    IMPORTANT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\fileversion{v1.1}
\def\filedate{94/04/31}
\typeout{Style-Option: `verbasef'
   \fileversion \space <\filedate>  (ndw)}
\@ifundefined{verbatim@@@}{\input{vrbexin.sty}\input{verbatim.sty}}{}%
\@ifundefined{@HHfloat}{\input{here.sty}}{}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newcounter{Vauto@LinePage}%3 % lines/page counter
\newcounter{Vauto@NP}%        % Number of pages
\newcounter{Vauto@Fignum}%    % Figure number - retain over figures
\newcounter{Vauto@Pagea}%     % # lines page 1
\newcounter{Vauto@LineNo}%    % current line number
\newcounter{Vauto@LineMod}%   % modulo to print on
\newcounter{Vauto@LineLoop}%  % loop count in line number (1..Mod)
\newcounter{Vauto@LineStart}% % first line to print
\newcounter{Vauto@LineCount}% % number of lines to print
\newcounter{Vauto@NotLabel}%  % Printable - not a label
\newcounter{Vauto@PrintFlag}% % print this line?
%
%%%% Default table placement, and macros to change table placement
\def\Vauto@Plx{H}\def\Vauto@Pl#1{\def\Vauto@Plx{#1}}\def\VautoPl{\Vauto@Pl}
%%%% Default verbatim font and verbatim line number font
\def\Vauto@sfFont{\tt}\def\Vauto@sfLNF{\rm\tiny}%
%
%%%% Macros to change fonts
\def\Vauto@sbf[#1]#2{\def\Vauto@sfFont{#2}\def\Vauto@sfLNF{#1}}
\def\Vauto@svf#1{\def\Vauto@sfFont{#1}}%
\def\VautoSfFont{
  \@ifnextchar[{\Vauto@sbf}{\Vauto@svf}%]
}
%%%% Macros to set number of lines per figure
\def\Vauto@LPP{46}\def\Vauto@LPPa{46}
\def\Vauto@TwoLPP[#1]#2{\def\Vauto@LPP{#2}\def\Vauto@LPPa{#1}}
\def\Vauto@OneLPP#1{\def\Vauto@LPP{#1}\def\Vauto@LPPa{#1}}
\def\VautoLines{
  \@ifnextchar[{\Vauto@TwoLPP}{\Vauto@OneLPP}%]
}
\def\VautoSubFF{\@ifnextchar[{\Vauto@sbf}{\Vauto@svf}%]
}
\def\xxparse Vzzlabel|#1|{
%\typeout{Here is that label: #1}
\immediate\write\@mainaux{\string\newlabel{#1}{{\theVauto@LineNo}{\thepage}}}
}
\def\ifsubstr#1#2#3#4%% #1=string #2=substring, #3=yes, #4=no
{ \def\tempa##1#2##2\over_##3\end{%
  \ifx##3% can't parse
  \setcounter{Vauto@NotLabel}{1}%
  \else% can parse
  \setcounter{Vauto@NotLabel}{0}%
  \xxparse #1%
  \fi%
}%
\tempa#1\over_\over#2\over___\end
}
%
%%Macro which sets beginfigure and endfigure constructions
%
\def\Vauto@etab#1#2#3{%  {caption}{label}{prtype}%
  \ifnum#3<3% 1 or 2 qualifies here
    \begingroup \normalsize\rm\selectfont%
      \ifnum\theVauto@NP=1 \caption{#1}\label{#2}%
        \setcounter{Vauto@Fignum}{\value{figure}}%
      \else \vspace{2em}%
          \centerline{Figure \theVauto@Fignum: #1 (cont.)}%
      \fi%
    \endgroup%
    \end{figure}% 
    \def\Vauto@PrCnt{\Vauto@LPP}%
    \ifnum#3=2 \setcounter{figure}{\value{Vauto@Fignum}}\fi%
  \fi%
  \ifnum#3>1% 2 or 3 qualifies here
    \begin{figure}[H]%
    \addtocounter{Vauto@NP}{1}%
    \setcounter{Vauto@LinePage}{0}%
    \setcounter{Vauto@PrintFlag}{1}%
  \fi%
  \catcode`\^^M9%
}%
%
%%%% Internal macro that does the work...
\def\Vauto@sf#1#2#3{\begingroup%
  \def\verbatim@start{\Vauto@etab{#2}{#3}{3}}
  \def\verbatim@finish{\Vauto@etab{#2}{#3}{1}}
  \def\verbatim@font{\Vauto@sfFont\selectfont}
  \setcounter{Vauto@LineNo}{0}%
  \setcounter{Vauto@LineLoop}{0}%
  \parskip=0pt%
  \def\verbatim@processline{%
    \edef\subz{\noexpand\ifsubstr{\the\verbatim@line}{Vzzlabel}{1}{0}}\subz
    \ifnum\theVauto@NotLabel=1%
      \addtocounter{Vauto@LineNo}{1}%
      \addtocounter{Vauto@LineLoop}{1}%
      \setcounter{Vauto@PrintFlag}{1}%
      \ifnum\theVauto@LineStart>0% we haven't reached the first line yet...
        \setcounter{Vauto@PrintFlag}{0}%
        \addtocounter{Vauto@LineStart}{-1}%
      \else%
        \ifnum\theVauto@LineCount=0% we're out of the "printable" region
          \setcounter{Vauto@PrintFlag}{0}%
        \else%
          \addtocounter{Vauto@LineCount}{-1}% decrease the line count
        \fi%
      \fi%
      % debugging...
      %\typeout{Start: \theVauto@LineStart, Count: \theVauto@LineCount,
      %  Print: \theVauto@PrintFlag, Loop: \theVauto@LineLoop,
      %  Mod: \theVauto@LineMod}%
      \ifnum\theVauto@PrintFlag=1%
        \leavevmode%
        \ifnum\theVauto@LineLoop=\theVauto@LineMod%
        \llap{{\Vauto@sfLNF\selectfont \theVauto@LineNo}%
            \ \hskip\@totalleftmargin}%
          \setcounter{Vauto@LineLoop}{0}%
          \else%
          \llap{\phantom{\Vauto@sfLNF\selectfont 0}\ %
                \hskip\@totalleftmargin}%
          \fi%
        \the\verbatim@line\par%
        \addtocounter{Vauto@LinePage}{1}%
      \else%
        \ifnum\theVauto@LineLoop=\theVauto@LineMod%
          \setcounter{Vauto@LineLoop}{0}%
        \fi%
      \fi%
      \ifnum\theVauto@LinePage=\Vauto@PrCnt \Vauto@etab{#2}{#3}{2}\fi%
    \fi% End of the loop printing non-labels
  }
  \verbatiminput{#1} \endgroup}%
%
\def\Vauto@sfP#1#2#3#4#5{%
\begingroup
  \def\Vauto@PrCnt{\Vauto@LPPa}
  \setcounter{Vauto@LineMod}{-1}%
  \setcounter{Vauto@LineStart}{#1}%
  \setcounter{Vauto@LineCount}{#2}%
  \addtocounter{Vauto@LineCount}{-\theVauto@LineStart}%
  \addtocounter{Vauto@LineCount}{1}%
  \addtocounter{Vauto@LineStart}{-1}%
  \Vauto@sf{#3}{#4}{#5}%
  \catcode`\^^M`\^^M
\endgroup
}%
\def\Vauto@sfN[#1]#2#3#4#5#6{%
\begingroup
  \def\Vauto@PrCnt{\Vauto@LPPa}
  \setcounter{Vauto@LineMod}{#1}%
  \setcounter{Vauto@LineStart}{#2}%
  \setcounter{Vauto@LineCount}{#3}%
  \addtocounter{Vauto@LineCount}{-\theVauto@LineStart}%
  \addtocounter{Vauto@LineCount}{1}%
  \addtocounter{Vauto@LineStart}{-1}%
  \Vauto@sf{#4}{#5}{#6}%
  \catcode`\^^M`\^^M
\endgroup
}%
\def\VautoSubF{
  \@ifnextchar[{\Vauto@sfN}{\Vauto@sfP}%]
}%
%
%%%% EOF
