% /u/sy/beebe/tex/auedindx/authidx.sty, Sun Sep 29 10:07:01 1996
% Edit by Nelson H. F. Beebe <beebe@plot79.math.utah.edu>
%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     author          = "Nelson H. F. Beebe",
%%%     version         = "1.00",
%%%     date            = "02 October 1996",
%%%     time            = "12:34:14 MDT",
%%%     filename        = "authidx.sty",
%%%     address         = "Center for Scientific Computing
%%%                        University of Utah
%%%                        Department of Mathematics, 105 JWB
%%%                        155 S 1400 E RM 233
%%%                        Salt Lake City, UT 84112-0090
%%%                        USA",
%%%     telephone       = "+1 801 581 5254",
%%%     FAX             = "+1 801 581 4148",
%%%     URL             = "http://www.math.utah.edu/~beebe",
%%%     checksum        = "08768 346 1427 13024",
%%%     email           = "beebe@math.utah.edu, beebe@acm.org,
%%%                        beebe@ieee.org (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "author/editor index, BibTeX, bibliography,
%%%                        LaTeX",
%%%     supported       = "yes",
%%%     docstring       = "This package modifies the internals of
%%%                        the LaTeX \cite{}-family macros to
%%%                        additionally provide special index entries in
%%%                        a .aei index file.
%%%
%%%                        The package can be used with LaTeX 2.09,
%%%                        LaTeX 2e, plain TeX, eplain TeX, TeXinfo,
%%%                        other plain-based TeX formats, and with
%%%                        certain limitations detailed in btxmac.tex,
%%%                        with AmSTeX.
%%%
%%%                        The .aei file can subsequently be processed
%%%                        by authidx.awk to produce a .idx-like .ida
%%%                        index file that can either be logically
%%%                        combined with the original .idx file (for a
%%%                        combined author/editor and subject index),
%%%                        or processed independently by an indexing
%%%                        program, such as makeindex, texidx, or
%%%                        texindex, to produce a separate
%%%                        author/editor index.
%%%
%%%                        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.",
%%%  }
%%% ====================================================================

%-----------------------------------------------------------------------

\immediate\write16{Document Option `authidx' v1.00 for LaTeX <02 Oct 1996> and others <19 Dec 1997>.}%

%-----------------------------------------------------------------------
% Step 0:
% Make @ a `letter' character so that we can have private macros.  For
% LaTeX, this is already the case, but when this file is \input by
% other TeX formats, @ is usually of type `other'.
%
% The use of `\makeauthoreditorindex' as a temporary control sequence
% here is a kludge, but it's a reasonably simple way to accomplish
% making @ a letter (to provide ``concealed'' control sequences)
% without overwriting something (without an `@' in its name) that
% might already be defined, since we will define
% \makeauthoreditorindex below anyway.

\edef\makeauthoreditorindex{\the\catcode`@}%
\catcode`@ = 11
\let\@oldatcatcode = \makeauthoreditorindex

% We need to distinguish between LaTeX and other TeX formats,
% so a private conditional is helpful:
\ifx \if@latexformat \undefined
    \newif \if@latexformat
\fi

% We base this test on \newenvironment, rather than on \documentclass
% or \documentstyle, because AmSTeX also has \documentstyle:
\ifx \newenvironment \undefined
    \@latexformatfalse
\else                           % must be LaTeX 2e
    \@latexformattrue
\fi

\if@latexformat
    \relax
\else
    % In order for our macros below to work outside of LaTeX, we need
    % a few LaTeX-like internal macros.  However, if they are already
    % defined, we assume that they will do the right thing, and we
    % leave their definitions untouched.

    % We don't require the user to know about btxmac.tex, so we include
    % it if it has not already been read:
    \ifx \bblfilebasename \undefined
        \input btxmac
    \fi

    % Redefine plain.tex \newwrite macro not to be \outer, since we
    % need it inside other macros:
    \def \newwrite {\alloc@7\write\chardef\sixt@@n}

    % Define an output stream for the .aux file, which btxmac has
    % already opened:
    \let \@auxout = \@auxfile

    % Define \mbox since some bibliography styles need it:
    \ifx \mbox \undefined
        \def \mbox #1{\leavevmode \hbox{#1}}
    \fi%

    % Define \typeout since we need it below:
    \ifx \typeout \undefined
        \def \typeout #1{\immediate\write16{#1}}
    \fi

    % LaTeX uses \@@input for Plain TeX \input:
    \let \@@input = \input

    % Define LaTeX-style \@input which recovers from a non-existant
    % file with a warning:
    \def\@input#1%
    {%
        \openin 1 #1
        \ifeof 1
            \typeout{No file #1.}%
        \else
            \closein 1
            \relax
            \@@input #1
        \fi
    }
\fi

% This conditional is used by btxmac.tex, and even though with LaTeX,
% it is not used, we need to define it to allow TeX to properly parse
% the nested conditionals below.  We cannot place it inside a
% conditional, because of the way TeX macro expansion works (TeXbook,
% bottom of p. 211), so for non-LaTeX, this is a redefinition.
\newif \if@fileexists

% Executing \@fileswfalse turns off all file output, but that is
% unusual:
% \ifx \@fileswfalse \undefined
    \newif \if@filesw
    \@fileswtrue
% \fi

% \endinput

%-----------------------------------------------------------------------
% Step 1:
% We create a new user-level command, \makeauthoreditorindex, modelled
% on \makeindex and \makeglossary found in latex.tex (2.09) about line
% 6237 to request that the author/editor index file be written on this
% LaTeX run.

\def \makeauthoreditorindex {\if@filesw \newwrite\@authoreditorindexfile
  \immediate\openout\@authoreditorindexfile=\jobname.aei
  \def \authoreditorindex {\@bsphack\begingroup
             \def \protect ####1{\string####1\space}\@sanitize
             \@wrauthoreditorindex}\typeout
  {Writing author-editor index file \jobname.aei }\else\fi}%

%-----------------------------------------------------------------------
% Step 2:
% Define the required analogue of \@wrindex from latex.tex (2.09)
% about line 6245.

\def \@wrauthoreditorindex #1{\let\thepage\relax
   \xdef\@gtempa{\write\@authoreditorindexfile{\string
      \indexentry{#1}{\thepage}}}\endgroup\@gtempa
   \if@nobreak \ifvmode\nobreak\fi\fi\@esphack}%

%-----------------------------------------------------------------------
% Step 3:
% Define the required analogues of \index and \@index from latex.tex
% (2.09) about line 6245.  These definitions simply discard their
% argument; they will be redefined to produce output iff the user
% calls \makeauthoreditorindex.

\def \authoreditorindex {\@bsphack\begingroup \@sanitize\@authoreditorindex}%

\def \@authoreditorindex #1{\endgroup\@esphack}%

%-----------------------------------------------------------------------
% Step 4:
% We modify \@citex found in latex.tex (2.09) about line 6348 to add a
% special index entry for the citation list (#2).  User-level citation
% macros should all go through this macro, so that \cite, \shortcite,
% \citeNP, ... are all properly indexed.

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

%-----------------------------------------------------------------------
% Step 5:

\if@latexformat
    % Modify the \bibliography command defined in latex.tex (2.09) about
    % line 6358 to write a suitable \bibdata entry in the .aei file
    % so that authidx.awk need not read .aux files to find the list of
    % BibTeX files that are needed to obtain lists of author names.
    \def \bibliography #1%
    {%
      \if@filesw
        \immediate\write\@auxout{\string\bibdata{#1}}%
        \ifx \@authoreditorindexfile \undefined
        \else
          \immediate\write\@authoreditorindexfile{\string\bibdata{#1}}%
        \fi
      \fi
      \@input{\jobname.bbl}%
    }%
\else
    % Modify the btxmac.tex \bibliography command to write a suitable
    % \bibdata entry in the .aei file so that authidx.awk need not
    % read .aux files to find the list of BibTeX files that are needed
    % to obtain lists of author names.
    \def\bibliography#1{%
       \@readauxfile
       \if@filesw
         \immediate\write\@auxout{\string\bibdata{#1}}%
         \ifx \@authoreditorindexfile \undefined
         \else
           \immediate\write\@authoreditorindexfile{\string\bibdata{#1}}%
         \fi
       \fi
       \@fileexiststrue
       \@testfileexistence[\bblfilebasename]{bbl}%
       \if@fileexists
          % We just output a non-discardable item (the `whatsit' with the
          % \bibdata command).  This means that the glue that will be
          % inserted next (\parskip or \baselineskip, most likely) will be a
          % legal breakpoint.  Most likely, this is after some kind of
          % heading, where we don't want to allow a page break.  So:
          \nobreak
          \@readbblfile
       \fi
    }%
\fi

%-----------------------------------------------------------------------
% Step 6:
% Provide an analogue of the theindex environment from LaTeX 2.09's
% article.doc, book.doc, and report.doc that works for all of them.

\def \theauthoreditorindex
{%
    \@restonecoltrue
    \if@twocolumn \@restonecolfalse \fi
    \columnseprule = \z@
    \columnsep = 35pt
    \ifx \@makeschapterhead \undefined % then NOT book/report-like style
        \twocolumn[\section*{Author/Editor Index}]%
    \else                           % must be book/report-like style
        \twocolumn[\@makeschapterhead{Author/Editor Index}]%
    \fi
    \@mkboth{AUTHOR/EDITOR INDEX}{AUTHOR/EDITOR INDEX}%
    \thispagestyle{plain}%
    \parindent = \z@
    \parskip = \z@ plus .3pt\relax
    \let \item = \@idxitem
}%

\let \endtheauthoreditorindex = \endtheindex

%-----------------------------------------------------------------------
% Step 7:
% Provide analogues of \printindex and \see from makeidx.sty, with
% \printauthoreditorindex reading its input from a .ina file created
% by an indexing program, but redefining the \theindex environment
% temporarily while the index is being processed.

\def \printauthoreditorindex
{%
  \bgroup
    \renewenvironment{theindex}%
        {\begin{theauthoreditorindex}}{\end{theauthoreditorindex}}%
    \@input {\jobname.ina}%
  \egroup
}%

\def \see #1#2{{\em see\/} #1}%

%-----------------------------------------------------------------------
% Step 8:
% Define macros for typesetting the first and other author/editor names
% and page numbers.  These may be redefined by the user.

\def \AEnamefontfirst #1{{\sc #1}}%
\def \AEnamefontrest  #1{{\rm #1}}%
\def \AEpagefontfirst #1{{\bf #1}}%
\def \AEpagefontrest  #1{{\rm #1}}%

%-----------------------------------------------------------------------
% Step 9:
% Redefine \@lbibitem and \@bibitem macros from latex.tex (2.09) about
% line 6326 to optionally generate index entries for the bibliography
% itself, and permit user control of this indexing through the
% \bibliographyindextrue and \bibliographyindexfalse commands.

\newif \ifbibliographyindex
\bibliographyindextrue

\def\@lbibitem[#1]#2{\item[\@biblabel{#1}]\if@filesw
      { \def\protect##1{\string ##1\space}\immediate
        \write\@auxout{\string\bibcite{#2}{#1}}}\fi
      \ifbibliographyindex \authoreditorindex{#2}\fi
      \ignorespaces}

\def\@bibitem#1{\item\if@filesw \immediate\write\@auxout
      {\string\bibcite{#1}{\the\c@enumi}}\fi
      \ifbibliographyindex \authoreditorindex{#1}\fi
      \ignorespaces}

%-----------------------------------------------------------------------
% Step 10:
% Define a user-customizable macro for typesetting page number
% mini-indexes in .bbl files.

\def \bibindex #1{$\{${\bf #1}$\}$}

%
%
% Restore the original catcode of commercial-at.
\catcode `\@ = \@oldatcatcode

\endinput

%================================The End================================
