%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
% This file is written by Tanmoy Bhattacharya <[email protected]> and   %
% is in the public domain for what it is worth. It inserts \specials that %
% are interpreted by (a) the xhdvi hypertex dvi previewer, (b) nextstep   %
% HyperTeXview.app dvi previewer, and (c) the dvipdf driver               %
% For more info, see http://xxx.lanl.gov/hypertex/                        %
%                or  ftp://xxx.lanl.gov/pub/hypertex/index.html           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
% Only \hyperdef, \hyperref , \href , and \hname are user callable.       %
% Usage: \hyperdef\TeXcs{category}{name}{text}                            %
% Usage: \href{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{8}%
%
% 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}%
\def\hyperstr@pblank" #1 #2\hyper@nd"{\ifx\hyper@ndefined#2\hyper@ndefined#1\else
                                   \ifx\hyper@ndefined#1\hyper@ndefined
                                   \hyperstr@pblank"#2 \hyper@nd"\else
                                   #1\hyperstr@pblank" #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\hyper@atm@ning#1->#2\hyper@nd{#2}
\def\hyperlink#1{\edef\hypert@mp{#1}%
              \edef\hypert@mp{\expandafter\hyper@atm@ning\meaning\hypert@mp
                              \hyper@nd}%
              \edef\hypert@mp"{ \expandafter\hyperstr@pquote\expandafter"%
                              \hypert@mp"\hyper@nd}%
              \edef\hypert@mp{\expandafter\hyperstr@pblank\expandafter%
                              "\hypert@mp" \hyper@nd"}%
              \hyper@nchor{href=\expandafter"\hypert@mp"}}%
%
\def\hypertarget#1{\edef\hypert@mp{#1}%
              \edef\hypert@mp{\expandafter\hyper@atm@ning\meaning\hypert@mp
                              \hyper@nd}%
              \edef\hypert@mp"{ \expandafter\hyperstr@pquote\expandafter"%
                              \hypert@mp"\hyper@nd}%
              \edef\hypert@mp{\expandafter\hyperstr@pblank\expandafter%
                              "\hypert@mp" \hyper@nd"}%
              \hyper@nchor{name=\expandafter"\hypert@mp"}}%
%
\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
                   \expandafter\hyperlink\expandafter{\the\hypert@ks}}%
%
% \def\hyperREF#1{\hyperlink{#1}}% % Disabled as per pg's suggestion
%
\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
%%%
\let\hyper@@@@=\relax
\def\hyper@@{\let\hyper@@@=\relax}%
\hyper@@
\def\hyper@{\relax\let\hyper@@@\noexpand\hyper@\noexpand}%
\def\hyperpr@ref{\hyper@@\hyperref}
\def\hyperpr@def{\hyper@@\hyperdef}

% As per pg's suggestion
\let\href\hyperlink
\let\hname\hypertarget
%
% Restore the catcode of @
%
\hypers@fe
\endinput
% A line after endinput to avoid both msdos gremlins and an incomplete
% last line