% NOTE THAT USING THIS FILE WILL PREVENT HYPERREF FROM WORKING.

%  REQUIRES windex.tex to do backpointers

\def\@fcwarning#1{\typeout{Fancycites Warning: #1.}}

%  REDEFINITION OF \@CITEX TO FACILITATE REFERENCE BACKPOINTERS AND SORTING

\def\@citex[#1]#2{\if@filesw\immediate\write\@auxout{\string\citation{#2}}\fi
  \@sortkeys{#2}%
  \def\@citea{}\@cite{\@for\@citeb:=#2\do
    {\indexcite{\@citeb}%
     \@citea\def\@citea{,\penalty\@m\ }\@ifundefined
       {b@\@aref{\@citeb}}{{\bf ?}\@warning
       {Citation `\@aref{\@citeb}' on page \thepage \space undefined}}%
\hbox{\csname b@\@aref{\@citeb}\endcsname}}}{#1}}

%  MAKING BACKPOINTERS FROM REFERENCES TO WHERE CITED

\def\@backcite#1{\@for\@citeb:=#1\do{\indexcite{\@citeb}}}
\def\indexcite#1{\index{\@ifundefined{b@#1}{#1}{\@nameuse{b@#1}}%
{\string\indexciteitem{#1}}}}

\let\@orig@idxitem=\@idxitem
\def\@idxitem#1#2 \par{\@orig@idxitem#1{#2}}

\def\indexciteitem#1#2{\auxout@citationpages{#1}{#2}\@indexciteitem{#1}{#2}}
\def\auxout@citationpages#1#2{\if@filesw \immediate\write\@auxout
       {\string\windcite{#1}{\@stripcom#2\@nil}}\fi}

\def\@stripcom, #1\@nil{#1}

\def\windcite#1#2{\global\@namedef{bp@#1}{#2}}

\def\enddocument{\@checkend{document}\clearpage\begingroup  
\if@filesw \immediate\closeout\@mainaux
\def\global\@namedef##1##2{}\def\newlabel{\@testdef r}%
\def\windcite{\@testdef {bp}}%
\def\bibcite{\@testdef b}\@tempswafalse \makeatletter\input \jobname.aux
\if@tempswa \@warning{Label(s) may have changed.  Rerun to get
cross-references right}\fi\fi\endgroup\deadcycles\z@\@@end}

\def\indexstyle{\@ifstar{\@emptyindex}{\@indexstyle}}
\def\@indexstyle#1{\@nameuse{is@#1}}

\def\is@citesinind{%
\def\@indexciteitem##1{\refdescriptor{\@reflabel{##1}}}%
}

\def\is@citesinbib{%
\def\@indexciteitem##1##2{}%
\def\bibitem{\@ifnextchar[{\@@lbibitem}{\@@bibitem}}%
}

\def\@@lbibitem[#1]#2#3\par{\@lbibitem[#1]{#2}#3\bibitempages{\@refpages{#2}}}
\def\@@bibitem#1#2\par{\@bibitem{#1}#2\bibitempages{\@refpages{#1}}}
\def\@refpages#1{\@ifundefined
        {bp@#1}{{\bf ?}\@fcwarning
        {Citations to #1 not indexed}}{\@nameuse{bp@#1}}}
\def\bibitempages#1{\newblock $\langle$#1$\rangle$}

\def\refdescriptor#1{reference [#1]}%
\def\@reflabel#1{\@ifundefined{b@#1}{{\bf ?}}{\@nameuse{b@#1}}}

%  MAKING CITATION KEYS PRINT IN SORTED ORDER

\def\@edefname#1{\expandafter\edef\csname #1\endcsname}

\def\@aref#1{\@nameuse{array@#1}}
\def\@aset#1#2{\@edefname{array@#1}{#2}}

\def\@refnum#1{\@ifundefined {b@\@aref{#1}}{0}{\@nameuse{b@\@aref{#1}}}}
\def\@swap#1#2{\edef\@swaptemp{\@aref{#1}}%
\@aset{#1}{\@aref{#2}}\@aset{#2}{\@swaptemp}}

\def\@carcom#1,#2\@nil{#1}
\def\@cdrcom#1,#2\@nil{#2}

\def\@sortkeys#1{\@for\@lpctri:=#1\do {\@aset{\@lpctri}{\@lpctri}}}

\newif\ifmorelist
% \loop macro used instead of \@for for inner loop of \@sortkeys in order to
% avoid exceeding parameter stack size.
% Note bizarre variation on bubble sort.  The loop counter on both inner and
% outer loop goes through entire array.  Since the sort would be correct if
% the inner loop counter would stop when it reaches the outer loop counter,
% the sort here is correct.  When the inner loop counter passes the outer
% counter and does some perverse swaps, it does not change the invariant that
% the portion of the array up to the outer loop counter is sorted.

\def\sortcitekeys{%
\def\@sortkeys##1{%
\@for\@lpctri:=##1\do {\@aset{\@lpctri}{\@lpctri}}%
\@for\@lpctri:=##1\do {%
\edef\@rest{##1,\empty,}%
\morelisttrue
\loop
\edef\@lpctrj{\expandafter\@carcom\@rest\@nil}%
\edef\@rest{\expandafter\@cdrcom\@rest\@nil}%
\ifx\@lpctrj\empty\morelistfalse\else\fi
\ifmorelist
\ifnum\@refnum{\@lpctrj}>\@refnum{\@lpctri}%
\@swap{\@lpctri}{\@lpctrj}\else\fi
\repeat
}}}
