% ------------------------------------------------------------------------
% File: srctex.sty
%       ==========
%
% This package was written by the author of WinEdt Shell to implement
% a source file tracking and DVIWIN "SRC" specials for plain TeX.
%
% This package comes with no guarantees and no reserved rights.
% You can use or modify this file at your own risk.
% ************************************************************************
% NOTE: TeX commands are case sensitive. You have to use uppercase
% \Input and \INPUT to take advantage of this package
% (they are defined below)...
%
% This file should be placed in a directory where your TeX is looking
% for inputs.
%
% If you are writing a book with WinEdt Shell and LaTeX 2e simply
% include this file and use "\INPUT{my_chapter}" instead of
% "\input my_chapter". This way all the necessary specials for
% DVI Search and Inverse Search are inserted in your DVI File.
%
% WinEdt will be able to track Errors from your LOG file.
%
% See also the sections on "Managing Subdocuments" and
% "DVIWIN Forward and Inverse Search" in "winedt.txt"!
%
% NOTE: Some TeX implementations add the file type to the "\jobname".
% In that case the definition of the "\MainFile" should be modified to:
%
%     \def\MainFile{\jobname} instead of \def\MainFile{\jobname.tex}.
%
% To remove SRC Specials from the dvi file compile the source
% after inserting \SRCOKfalse immediately after
% the \usepackage{srcdvi} directive...
%
% This package supports nested \INPUT commands...
% ------------------------------------------------------------------------
%
\def\typeout#1{\message{^^J}\message{#1}\message{^^J}}
%
\newif\ifSRCOK \SRCOKtrue
\newcount\PAGETOP
\newcount\LASTLINE
\global\PAGETOP=1
\global\LASTLINE=-1
\def\EJECT{\SRC\eject}
\def\WinEdt#1{\typeout{:#1}} % WinEdt LOG MODE and INPUT
\def\MainFile{\jobname.tex}
\def\CurrentInput{\MainFile}
\newcount\INPSP
\global\INPSP=0
% ------------------------------------------------------------------------
\def\SRC{\ifSRCOK%
  \ifnum\inputlineno>\LASTLINE%
    \ifnum\LASTLINE<0%
      \global\PAGETOP=\inputlineno%
    \fi%
    \global\LASTLINE=\inputlineno%
    \ifnum\INPSP=0%
      \ifnum\inputlineno>\PAGETOP%
        \special{src:\the\inputlineno\CurrentInput}%
      \fi%
    \else%
      \special{src:\the\inputlineno\CurrentInput}%
    \fi%
  \fi%
\fi}
% ------------------------------------------------------------------------
\def\PUSH#1{%
\ifnum\INPSP=0 \let\INPSTACKA=\CurrentInput \else%
\ifnum\INPSP=1 \let\INPSTACKB=\CurrentInput \else%
\ifnum\INPSP=2 \let\INPSTACKC=\CurrentInput \else%
\ifnum\INPSP=3 \let\INPSTACKD=\CurrentInput \else%
\ifnum\INPSP=4 \let\INPSTACKE=\CurrentInput \else%
\ifnum\INPSP=5 \let\INPSTACKF=\CurrentInput \else%
               \let\INPSTACKX=\CurrentInput \fi\fi\fi\fi\fi\fi%
\def\CurrentInput{#1}%
\WinEdt{<+ \CurrentInput}%
\global\LASTLINE=0%
\ifSRCOK\special{src:1\CurrentInput}\fi%
\global\advance\INPSP by 1}
%
\def\POP{%
\ifnum\INPSP>0 \global\advance\INPSP by -1  \fi%
\ifnum\INPSP=0 \let\CurrentInput=\INPSTACKA \else%
\ifnum\INPSP=1 \let\CurrentInput=\INPSTACKB \else%
\ifnum\INPSP=2 \let\CurrentInput=\INPSTACKC \else%
\ifnum\INPSP=3 \let\CurrentInput=\INPSTACKD \else%
\ifnum\INPSP=4 \let\CurrentInput=\INPSTACKE \else%
\ifnum\INPSP=5 \let\CurrentInput=\INPSTACKF \else%
               \let\CurrentInput=\INPSTACKX \fi\fi\fi\fi\fi\fi%
\WinEdt{<-}%
\global\LASTLINE=\inputlineno}
% ------------------------------------------------------------------------
\def\Input#1{%
\SRC%
\PUSH{#1}%
\input #1%
\POP%
\SRC}
%
\def\INPUT#1{%
\SRC%
\PUSH{#1.tex}%
\input #1%
\POP%
\SRC}
% ------------------------------------------------------------------------
% ? Remove some of the following lines if you experience any troubles
\everypar={\SRC}
\everymath={\SRC}
\everyhbox={\SRC}
\everyvbox={\SRC}
\everydisplay={\SRC}
% ------------------------------------------------------------------------
