%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     filename        = "bibref.sty",
%%%     version         = "1.1beta",
%%%     date            = "21 January 1996",
%%%     time            = "20:56:49 EST",
%%%     author          = "David M. Jones",
%%%     address         = "MIT Laboratory for Computer Science
%%%                        Room NE43-316
%%%                        545 Technology Square
%%%                        Cambridge, MA 02139
%%%                        USA",
%%%     telephone       = "(617) 253-5936",
%%%     FAX             = "(617) 253-3480",
%%%     checksum        = "34585 145 470 4184",
%%%     email           = "dmjones@theory.lcs.mit.edu",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "",
%%%     supported       = "yes",
%%%     abstract        = "",
%%%     docstring       = "The checksum field above contains a CRC-16
%%%                        checksum as the first value, followed by the
%%%                        equivalent of the standard UNIX wc (word
%%%                        count) utility output of lines, words, and
%%%                        characters.  This is produced by Robert
%%%                        Solovay's checksum utility.",
%%%  }
%%% ====================================================================

%%  This is an experimental package to support back references from
%%  reference sections to the pages where individual items are cited.
%%  I.e., the reference section would
%%
%%      Zofia Adamowicz.  A sharp version of the bounded
%%      {Matijasevich} conjecture and the end-extension problem.
%%      Journal of Symbolic Logic}, 57(2):597--616, June 1992.  (pages
%%      1, 3-5, 54)
%%
%%  if that item were cited on pages 1, 3, 4, 5 and 54.
%%
%%  To use this feature, you'll need to make a small change to your
%%  BibTeX bibliography style file.  In the standard styles, all that
%%  is needed is to modify the function fin.entry as follows:
%%
%%      FUNCTION {fin.entry}
%%      { add.period$
%%        write$
%%          "\bibref{" cite$ "}" * * write$ % added line
%%        newline$
%%      }
%%
%%  Then load the bibref package by adding
%%
%%      \usepackage{bibref}
%%
%%  to your document preamble, and run LaTeX as normal.  After running
%%  LaTeX, invoke makeindex as follows:
%%
%%      makeindex -s bibref.ist -o <jobname>.bnd <jobname>.bdx
%%
%%  The next time you run LaTeX, the back-references should magically
%%  appear.
%%
%%  See plaintst.tex and xplain.bst for an example.

\RequirePackage{index}

\newindex{@bib}{bdx}{bnd}{}

\newcommand{\biblabel}[2]{%
    \global\@namedef{br@#1}{#2}%
}

\newcommand{\bibref}[1]{%
    \unskip\space
    \@ifundefined{br@#1}{%
%        (\textbf{??})%
    }{%
        \edef\reserved@a{\@nameuse{br@#1}}%
%%
%%  *sigh*  Another clever hack killed by an ugly fact.
%%
%%        \afterassignment\@bibref\count@\reserved@a\relax\@nil
        \expandafter\@bibref\expandafter{\reserved@a}%
    }%
}

\newcommand{\@bibref}[1]{%
%%
%%  \@tempswatrue means there's only one page number
%%
    \@tempswatrue
%%
%%  First we look for a comma.  If there is one, we set @tempswafalse
%%
    \lookfor@comma#1,\@nil
%%
%%  If @tempswa is still true, it means there wasn't a comma, so now
%%  we check for a dash.
%%
    \if@tempswa \lookfor@dash#1-\@nil \fi
    \if@tempswa
        (page~\reserved@a)%
    \else
        (pages~\reserved@a)%
    \fi
}

%%  If there's no comma, #2 will be empty, otherwise, there was a
%%  comma, and we should invert @tempswa

\def\lookfor@comma#1,#2\@nil{%
    \if!#2!\else\@tempswafalse\fi
}

\def\lookfor@dash#1-#2\@nil{%
    \if!#2!\else\@tempswafalse\fi
}

\let\@@cite\cite

\renewcommand{\cite}[2][]{%
    \@for\@citea:=#2\do{%
        \edef\reserved@a{%
            \noexpand\index[@bib]{\@citea}%
        }%
        \reserved@a
    }%
    \def\reserved@a{#1}%
    \ifx\reserved@a\@empty
        \@@cite{#2}%
    \else
        \@@cite[#1]{#2}%
    \fi
}

\renewcommand{\bibliography}[1]{%
    \if@filesw
        \immediate\write\@auxout{\string\bibdata{#1}}%
      \fi
      \@input@{\jobname.bnd}%
      \@input@{\jobname.bbl}%
}

\endinput
