%
%   Handles forward references.  Defines:
%
%   \dref{string}{expansion}   Defines {string} to be {expansion}; also
%      adds the string literally to the output \jobname.ref file.  Complains
%      if it is a redefinition that is different from the earlier one.
%
%   \ref{string}  Expands to the expansion of {string} if it exists, or
%      string in italics with a complaint.
%
\newread\refread\newwrite\refwrite\newif\ifknown\newtoks\knownval
%
%   Some utilities.
%
\def\testifknown#1{\expandafter\ifx\csname#1\endcsname\relax\knownfalse
   \else\knowntrue\knownval=\csname#1\endcsname\fi}%
%
%   Here's dref; write is delayed until page is shipped.
%
\def\dref#1#2{\write\refwrite{\string\expandafter\string\def\string\csname\space
      #1\string\endcsname{{#2}}}}%
%
%   Here's edref; write is immediate.
%
\def\edref#1#2{\immediate\write\refwrite{\string\expandafter\string\def
   \string\csname\space #1\string\endcsname{{#2}}}}%
%
%   Now, ref.
%
\def\ref#1{\testifknown{#1}\ifknown\the\knownval\else
   \message{/#1??/}{\it #1}\fi}%
%
%   Redo bye.
%
\def\bye{\checkref\vfill\eject\end}%
\def\checkref{\closeout\refwrite\write18{diff \jobname.ref \jobname.orf}}%
%
%   Read the old ref file.
%
\immediate\openin\refread=\jobname.ref \ifeof\refread
\message{Reference file nonexistent}\immediate\closein\refread\else
\immediate\closein\refread\immediate\write18{copy \jobname.ref \jobname.orf}%
\input \jobname.ref\fi\immediate\openout\refwrite=\jobname.ref
\write\refwrite{\string\relax}
