% File: CITESIDX.STY
% (Based on fancycites.sty and windex.tex)
% Author: Frank Buchholz, Dortmund, Germany, Dez. 1992
%
% Main purpose:
% 	Add reference-page-list to bibliography-items.
%	This is done by using the index-facility.
%
% USAGE:
% Switch index-commands on:
%	\makeindex
%
% User selection:
% Put indexed terms into the margin (not ready):
%	\indexmark{margin}
% Mark indexed terms with a vertical rule:
%	\indexmark{rule}
%
% User selection:
% Put references of cites into index
%	\indexstyle{citesinind}
% Put references of cites into bibliography (default)
%	\indexstyle{citesinbib}
%
% Switch sorting of numeric keys on:
%	\sortcitekeys
%
% Put some text in front of bibliography 
% (Use '\\[\parskip]' instead of '\par'):
%	\bibintro{<Text>}
%
% Run LaTeX to create the <file>.idx
% Run BiBTeX if neccesary.
% Run 'makeindex -s citesidx.isty <file>' to create <file>.ind
% Run LaTeX twice to get correct references (Maybe you need even more, if you
% are using crossrefs in the bibliography.)

\typeout{Document style option `citesidx' - Frank Buchholz, Dez. 1992}

% REDEFINITION OF \@wrindex#1 TO CALL \@indexmark#1
% see LATEX.TEX

\def\@wrindex#1{%
  \@indexmark{#1}%
  \let\thepage\relax%
  \edef\@tempa{\write\@indexfile{%
    \string\indexentry{#1}{\thepage}}}%
  \expandafter\endgroup\@tempa%
  \if@nobreak \ifvmode\nobreak\fi\fi\@esphack}

% REDEFINITION OF \@citex[#1]#2 TO FACILITATE REFERENCE BACKPOINTERS AND SORTING
% see LATEX.TEX

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

% REDEFINITION OF \enddocument TO DEFINE \windcite
% see LATEX.TEX

% \enddocument is the user's command for ending the manuscript file.
\def\enddocument{\@checkend{document}\clearpage\begingroup  
  \if@filesw \immediate\closeout\@mainaux
    \def\global\@namedef##1##2{}%
    \def\newlabel{\@testdef r}%
    \def\windcite{\@testdef {bp}}% new
    \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}

% REDEFINITION OF \@idxitem#1#2
% see ARTICLE.STY, REPORT.STY, BOOK.STY
 
%\let\@orig@idxitem=\@idxitem
%\def\@idxitem#1#2 \par{\@orig@idxitem#1{#2}}

% DEFINITION OF \printindex
% see MAKEIDX.STY

\def\printindex{\@input{\jobname.ind}}

%%% NEW COMMANDS

% WARNING MESSAGE

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

% MAKING BACKPOINTERS FROM REFERENCES TO WHERE CITED

% not used ???
%\def\@backcite#1{\@for\@citeb:=#1\do{\indexcite{\@citeb}}}

% used by \@citex[#1]#2
% used by \@backcite#1
\def\indexcite#1{\index{\@ifundefined{b@#1}{#1}{\@nameuse{b@#1}}%
  \string @\string\indexciteitem{#1}}}

% used by \indexcite#1
\def\indexciteitem#1#2{%
  \auxout@citationpages{#1}{#2}%
  \@indexciteitem{#1}{#2}}

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

% used by \auxout@citationpages#1#2
\def\@stripcom, #1\@nil{#1}

% Read References from aux-file
\def\windcite#1#2{\global\@namedef{bp@#1}{#2}}

% USER SELECTION OF INDEXSTYLE

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

% Select style: Cites in index
\def\is@citesinind{%
  \def\@indexciteitem##1{\refdescriptor{\@reflabel{##1}}}
}

% Select Style: Cites in bibliography
\def\is@citesinbib{%
  \def\@indexciteitem##1##2{}% Ignore 
  % see LATEX.TEX
  \def\bibitem{\@ifnextchar[{\@@lbibitem}{\@@bibitem}}%
}

% Default style
\is@citesinbib

% used by \is@citesinbib
\def\@@lbibitem[#1]#2#3\par{\@lbibitem[#1]{#2}#3\bibitempages{\@refpages{#2}}}
\def\@@bibitem#1#2\par{\@bibitem{#1}#2\bibitempages{\@refpages{#1}}}

% used by \@@lbibitem[#1]#2#3\par
\def\@refpages#1{%
  \@ifundefined{bp@#1}
    {{\bf -}\@fcwarning{Citations to #1 not indexed}}
    {\@nameuse{bp@#1}}}

% used by \@@lbibitem[#1]#2#3\par
% used by \@@bibitem#1#2\par
\def\bibitempages#1{\newblock [#1]}

% used by \bibitempages#1 
% see ARTICLE.STY, REPORT.STY, BOOK.STY
\def\newblock{\hskip .11em plus .33em minus -.07em}%

% used by \is@citesinind
\def\refdescriptor#1{Referenz [#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.

% Switch sorting of numeric keys on
\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}
}}

% PUT SOME TEXT IN FRONT OF THE BIBLIOGRAPHY

\def\bibintro#1{%
  \let\@origthebib=\thebibliography
  \let\@origlist=\list
  \def\thebibliography{\def\list{#1\par\bigskip\@origlist}\@origthebib}%
}

% USER SELECTION: MARK INDEXED TERMS

\def\indexmark{\@ifstar{\@emptyindex}{\@selectindexmark}}
\def\@selectindexmark#1{\@nameuse{is@index#1}}

% Put indexed terms into the margin:
\def\is@indexmargin{%
  \def\@indexmark##1{\marginpar{\fbox{\parbox[t]{\marginparwidth}{\tiny ##1}}}}
} 	

% Mark indexed terms with a vertical rule:
\def\is@indexrule{%
  \def\@indexmark##1{\unskip\hbox{\vrule depth0pt width1pt height 2ex}}
}

% no default
\def\@indexmark#1{}				

% Make all characters ``other'' so that par box doesn't give errors
% used by \is@indexmargin
\def\@makeallother{%
\@makeother a\@makeother n\@makeother A\@makeother N%
\@makeother b\@makeother o\@makeother B\@makeother O%
\@makeother c\@makeother p\@makeother C\@makeother P%
\@makeother d\@makeother q\@makeother D\@makeother Q%
\@makeother e\@makeother r\@makeother E\@makeother R%
\@makeother f\@makeother s\@makeother F\@makeother S%
\@makeother g\@makeother t\@makeother G\@makeother T%
\@makeother h\@makeother u\@makeother H\@makeother U%
\@makeother i\@makeother v\@makeother I\@makeother V%
\@makeother j\@makeother w\@makeother J\@makeother W%
\@makeother k\@makeother x\@makeother K\@makeother X%
\@makeother l\@makeother y\@makeother L\@makeother Y%
\@makeother m\@makeother z\@makeother M\@makeother Z%
\@makeother /\@makeother !\@makeother <\@makeother >%
\@makeother \$\@makeother [\@makeother ]%
%\@makeother\ 
\@makeother\\\@makeother\$\@makeother\&%
\@makeother\#\@makeother\^\@makeother\^^K\@makeother\_\@makeother\^^A%
\@makeother\%\@makeother\~}

