%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     filename        = "bibref.sty",
%%%     version         = "1.0beta",
%%%     date            = "29 September 1995",
%%%     time            = "14:36:27 EDT",
%%%     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        = "21394 117 377 3498",
%%%     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}}%
        \afterassignment\@bibref\count@\reserved@a\relax\@nil
    }%
}

\def\@bibref#1\@nil{%
    \if\relax#1%
        (page~\reserved@a)%
    \else
        (pages~\reserved@a)%
    \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
