%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
% This file is written by Tanmoy Bhattacharya <tanmoy@qcd.lanl.gov> and   %
% is in the public domain for what it is worth. It is an interface to the %
% xhdvi hypertext dvi previewer written by                                %
% Arthur Smith <asmith@bigsky.chem.washington.edu> and available from     %
% ftp://snorri.cpac.washington.edu/pub/hypertex/xhdvi.tar.Z               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
% Only \hyperdef, \hyperref and \hyperREF are user callable.              %
% Usage: \hyperdef\TeXcs{category}{name}{text}                            %
% Usage: \hyperREF{url}{text}                                             %
% Usage: \hyperref{filename}{category}{name}{text}                        %
%   or   \hyperref\TeXcs{text}                                            %
%             where \TeXcs has been defined by a hyperdef.                %
%                                                                         %
%        Note: The token following \hyperref is expanded completely first.%
%              Really perverse forms like                                 %
%                \csname perverse\endcsname                               %
%              will give an error                                         %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following might be useful to other macros interfacing in.           %
% Usage: \hyper@nchor{name="#myname" href="#refer"}{text}                 %
% Usage: \hyperlink{target_name}{text}                                    %
% Usage: \hypertarget{myname}{text}                                       %
% In cases where there is a possibility of premature expansion use        %
%     \hyper@\hyperpr@ref instead of \hyperref                            %
% and \hyper@\hyperpr@def instead of \hyperdef                            %
% These macros increase enormously in size when expanded, but the expanded%
% token sequence always ends in \hyperpr@ref and \hyperpr@def respectively%
% Macro writers might want to make use of the macro \hyperv@rsion         %
% which is undefined in version 0 and is the version number otherwise     %
% \hyper@nique is exactly like \hyperdef except no special are written    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Save the current catcode of @ and redefine @ to be a letter
%
\expandafter\edef\csname hypers@fe\endcsname{\catcode
                                             `\noexpand@=\the\catcode`\@}%
\catcode`\@=11
%
% Check if the file is already included
%
\ifx\hyperd@ne\hyper@ndefined
 \global\let\hyperd@ne=\relax
\else
 \errhelp{hyperbasics.tex needs to be included only once outside
          of any {...} or \begingroup...\endgroup. You have tried to
          include it more than once. If the previous include was indeed
          outside any groupings, continue and all will be well.}%
 \errmessage{Input this file only once!}%
 \endinput
\fi
%
% Version number
%
\def\hyperv@rsion{3}%
%
% Check and input a previous .hrf file if it exists
%
\newread\hyperf@le
\def\hyperf@lename{\jobname.hrf}%
\immediate\openin\hyperf@le\hyperf@lename\relax
\ifeof\hyperf@le\relax
 \immediate\closein\hyperf@le\relax
\else
 \immediate\closein\hyperf@le\relax
 \input \hyperf@lename
\fi
%
% Open a new .hrf file
%
\newwrite\hyperf@le
\immediate\openout\hyperf@le\hyperf@lename
%%%%
% MAIN SECTION
%%%%
%
% define a token register
%
\newtoks\hypert@ks
%
% Define a convenient macro to hold the character #
%
\edef\hypert@mp{\catcode`\noexpand\#=\the\catcode`\#}%
\catcode`\#=12
\def\hyperh@sh{#}%
\hypert@mp
\let\hypert@mp=\relax
\let\hyper@nd=\relax
\def\hyperstr@pquote"#1"#2\hyper@nd{\ifx\hyper@ndefined#2\hyper@ndefined#1\else
                                    \ifx\hyper@ndefined#1\hyper@ndefined
                                    \hyperstr@pquote#2"\hyper@nd\else
                                    #1\hyperstr@pquote"#2"\hyper@nd\fi\fi}%
%
\long\def\hyper@nchor#1#2{\edef\hyperm@cro{html:<A #1>}%
                          \special\expandafter{\hyperm@cro}%
                          {#2}\special{html:</A>}}%
%
\def\hyperlink#1{\edef\hypert@mp{#1}%
                 \hyper@nchor{href="\expandafter\hyperstr@pquote
                 \expandafter"\hypert@mp"\hyper@nd"}}%
%
\def\hypertarget#1{\edef\hypert@mp{#1}%
                   \hyper@nchor{name="\expandafter\hyperstr@pquote
                   \expandafter"\hypert@mp"\hyper@nd"}}%
%
\def\hyperref{\afterassignment\hyperr@f\let\hyperp@ram}
\def\hyperr@f{\ifx\hyperp@ram{\iffalse}\fi
               \expandafter\expandafter\expandafter\hyperr@@
               \expandafter{%
              \else
               \iffalse}\fi
               \ifx\hyperp@ram\hyper@ndefined
                 \message{Undefined reference}%
                 \def\hyperp@r@m{{}{undefined}{}}%
               \else
                 \edef\hyperp@r@m{\hyperp@ram}%
               \fi
               \expandafter\expandafter\expandafter\hyperr@@
               \expandafter\hyperp@r@m
              \fi}%
% refer to #1, \hyperh@sh#2.#3 or #1\hyperh@sh#2.#3
% depending on what is blank/nonblank
\def\hyperr@@#1#2#3{\ifx\hyper@ndefined#1\hyper@ndefined
                    \hypert@ks\expandafter{\hyperh@sh#2.#3}%
                    \else
                     \ifx\hyper@ndefined#2#3\hyper@ndefined
                      \hypert@ks{#1}%
                     \else
                      \def\hypert@mp{#1}%
                      \hypert@ks\expandafter\expandafter\expandafter
                      {\expandafter\hypert@mp\hyperh@sh#2.#3}%
                     \fi
                    \fi
                    \hyperlink{\the\hypert@ks}}%
%
\def\hyperREF#1{\hyperlink{#1}}%
%
\def\hyperdef#1#2#3{{\global\escapechar=`\\\relax
                     \edef\hypert@mp{\hyperstr@pquote"#2.#3"\hyper@nd}%
                     \expandafter\ifx\csname hyperd@\meaning\hypert@mp
                     \endcsname
                     \relax
                     \expandafter\gdef\csname hyperd@\meaning\hypert@mp
                     \endcsname{}%
                     \gdef#1{{}{\hyperstr@pquote"#2"\hyper@nd}%
                               {\hyperstr@pquote"#3"\hyper@nd}}%
                     \immediate\write\hyperf@le{\def\noexpand#1{#1}}%
                     \xdef\hypert@mp{\global\let\noexpand\hypert@mp=\relax
                                     \noexpand\hypertarget{\hypert@mp}}%
                     \global\hypert@ks={\hypert@mp}%
                     \else
                     \message\expandafter{'\hypert@mp' duplicate}%
                     \global\let\hypert@mp=\relax
                     \global\hypert@ks={\hyperdef{#1}{#2}{#3@}}%
                     \fi}\the\hypert@ks}%

\def\hyper@nique#1#2#3#4{\global\escapechar=`\\\relax
                     \edef\hypert@mp{\hyperstr@pquote"#2.#3"\hyper@nd}%
                     \expandafter\ifx\csname hyperd@\meaning\hypert@mp
                     \endcsname
                     \relax
                     \gdef#1{{}{\hyperstr@pquote"#2"\hyper@nd}%
                               {\hyperstr@pquote"#3"\hyper@nd}}%
                     \global\let\hypert@mp=\relax
                     #4%
                     \else
                     \global\let\hypert@mp=\relax
                     \hyper@nique{#1}{#2}{#3@}{#4}%
                     \fi
                     }%

%%% 
% protection macros
%%%
\def\hyper@@{\let\hyper@@@=\relax}%
\hyper@@
\def\hyper@{\relax\let\hyper@@@\noexpand\hyper@\noexpand}%
\def\hyperpr@ref{\hyper@@\hyperref}
\def\hyperpr@def{\hyper@@\hyperdef}

%
% Restore the catcode of @
%
\hypers@fe
\endinput
% A line after endinput to avoid both msdos gremlins and an incomplete
% last line
