%%
%% This is file `index.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% index.dtx  (with options: `style')
%% 
%% David M. Jones
%% dmjones@alum.mit.edu
%% 
%% Copyright 2024, 2025 David M. Jones
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   https://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is David M. Jones.
%% 

\NeedsTeXFormat{LaTeX2e}[2024/11/01]

\ProvidesPackage{index}[2025/02/06 v4.04 Improved index support (dmj)]
\def\disableindex#1{%
    \@for\@tempa:=#1\do{%
        \@namedef{disable@\@tempa}{}%
        \@ifundefined{tf@\@tempa}{}{%
            \PackageWarningNoLine{index}{It's too late to disable
                the `\@tempa' index;\MessageBreak
                \jobname.\@tempa\space has already
                been opened for output. You \MessageBreak
                should put the \string\disableindex\space command
                before\MessageBreak
                the declaration of the `\@tempa' index}%
        }%
    }%
}
\newif\if@newindex

\def\newindex{%
    \@tempswafalse
    \@ifnextchar[{\@tempswatrue\x@newindex}{\x@newindex[thepage]}%
}

\def\x@newindex[#1]{%
    \@ifstar {\@tempswafalse\y@newindex{#1}}
             {\y@newindex{#1}}%
}

\def\y@newindex#1#2{%
    \@ifundefined{idx@#2}%
        {\@newindextrue\def@index{#1}{#2}}%
        {%
            \@latexerr{Index type `\string#2' already defined}\@ehc
            \expandafter\@gobble\@gobbletwo
        }%
}

\def\renewindex{%
    \@tempswafalse
    \@ifnextchar[{\@tempswatrue\x@renewindex}{\x@renewindex[thepage]}%
}

\def\x@renewindex[#1]{%
    \@ifstar {\@tempswafalse\y@renewindex{#1}}
             {\y@renewindex{#1}}%
}

\def\y@renewindex#1#2{%
    \@ifundefined{idx@#2}%
        {%
            \@newindextrue
            \@latexerr{Index type `\string#2' not defined}\@ehc
        }%
        {\@newindexfalse}%
    \def@index{#1}{#2}%
}
\@onlypreamble\newindex
\@onlypreamble\renewindex
\@onlypreamble\disableindex
\def\def@index#1#2#3#4{%
    \@namedef{idx@#2}{#3:#4:#1}%
    \expandafter\let\csname if@immediate@#2\endcsname\if@tempswa
    \if@filesw
        \if@newindex
            \expandafter\newtoks\csname idxtitle@#2\endcsname
        \fi
        \@ifundefined{disable@#2}{%
            \if@newindex
                \expandafter\newwrite\csname tf@#2\endcsname
            \else
                \immediate\closeout\@nameuse{tf@#2}%
            \fi
            \immediate\openout\@nameuse{tf@#2}\jobname.#3 %
            \PackageInfo{index}{Writing index file \jobname.#3}%
        }
        {\PackageInfo{index}{Index `#2' disabled -- not opening
                      \jobname.#3}}%
    \fi
    \expandafter\csname idxtitle@#2\endcsname
}
\def\@first#1:#2:#3\@nil{#1}

\def\@second#1:#2:#3\@nil{#2}

\def\@third#1:#2:#3\@nil{#3}
\def\@nearverbatim{\expandafter\strip@prefix\meaning}
\edef\makeindex{%
    \noexpand\newindex{default}{idx}{ind}{\indexname}%
}
\newif\if@silentindex\@silentindextrue

\newif\if@addtoindex\@addtoindextrue

\newif\ifproofmode\proofmodefalse
\protected\def\index{\protect\p@index}

\def\p@index{%
    \if@silentindex\@bsphack\fi
    \@ifstar{\@silentindexfalse\@xindex}{\@silentindextrue\@xindex}%
}

\def\@xindex{\@ifnextchar[{\@index}{\@index[default]}}
\def\@index[#1]{%
    \ifx\index\@gobble
        \@addtoindexfalse
    \fi
    \def\@tempf{%
        \begingroup
            \@sanitize
            \@@index{#1}%
    }%
    \if@addtoindex
        \@ifundefined{idx@#1}%
            {%
              \def\@tempf{%
                  \@latexerr{Index type `\string#1' undefined}%
                  \@ehc
                  \@silentindextrue
                  \@gobble
              }%
            }%
            {}%
    \fi
    \@tempf
}

\def\@@index#1#2{%
    \endgroup
    \if@addtoindex
        \if@filesw\@wrindex{#1}{#2}\fi
        \ifproofmode\@showidx{#2}\fi
    \fi
    \if@silentindex
        \expandafter\@esphack
    \else
        \@silentindextrue#2%
    \fi
}

\def\@wrindex#1#2{%
    \begingroup
        \def\@tempa{#2}%
        \edef\@tempb{\@nameuse{idx@#1}}%
        \edef\@tempb{\expandafter\@third\@tempb\@nil}%
        \csname if@immediate@#1\endcsname \else
            \expandafter\let\csname\@tempb\endcsname\relax
        \fi
        \edef\@tempa{%
           \write\@auxout{%
              \string\@writefile{#1}{%
                  \string\indexentry{\@nearverbatim\@tempa}%
                                    {\@nameuse{\@tempb}}%
              }%
           }%
        }%
    \expandafter\endgroup\@tempa
    \if@nobreak\ifvmode\nobreak\fi\fi
}
\providecommand{\seename}{see}

\providecommand*{\see}[2]{\emph{\seename} #1}

\@ifclassloaded{article}{%

    \renewenvironment{theindex}{%
        \edef\indexname{\the\@nameuse{idxtitle@\@indextype}}%
        \if@twocolumn
            \@restonecolfalse
        \else
            \@restonecoltrue
        \fi
        \columnseprule \z@
        \columnsep 35\p@
        \twocolumn[%
            \section*{\indexname}%
            \ifx\index@prologue\@empty\else
                \index@prologue
                \bigskip
            \fi
        ]%
        \@mkboth{\MakeUppercase\indexname}%
                {\MakeUppercase\indexname}%
        \thispagestyle{plain}%
        \parindent\z@
        \parskip\z@ \@plus .3\p@\relax
        \let\item\@idxitem
    }{%
        \if@restonecol
            \onecolumn
        \else
            \clearpage
        \fi
    }
}{%
    \renewenvironment{theindex}{%
        \edef\indexname{\the\@nameuse{idxtitle@\@indextype}}%
        \if@twocolumn
            \@restonecolfalse
        \else
            \@restonecoltrue
        \fi
        \columnseprule \z@
        \columnsep 35\p@
        \twocolumn[%
            \@makeschapterhead{\indexname}%
            \ifx\index@prologue\@empty\else
                \index@prologue
                \bigskip
            \fi
        ]%
        \@mkboth{\MakeUppercase\indexname}%
                {\MakeUppercase\indexname}%
        \thispagestyle{plain}%
        \parindent\z@
        \parskip\z@ \@plus .3\p@\relax
        \let\item\@idxitem
    }{%
        \if@restonecol
            \onecolumn
        \else
            \clearpage
        \fi
    }
}

\def\printindex{\@ifnextchar[{\@printindex}{\@printindex[default]}}

\def\@printindex[#1]{%
    \@ifnextchar[{\@print@index[#1]}{\@print@index[#1][]}%
}

\long\def\@print@index[#1][#2]{%
    \def\@indextype{#1}%
    \long\def\index@prologue{#2}%
    \@ifundefined{idx@#1}%
        {\@latexerr{Index type `\string#1' undefined}\@ehc}%
        {%
            \edef\@tempa{\@nameuse{idx@#1}}%
            \edef\@tempb{%
                \jobname.\expandafter\@second\@tempa\@nil
            }%
            \edef\@tempc{%
                \jobname.\expandafter\@first\@tempa\@nil
            }%
            \InputIfFileExists{\@tempb}{}{%
                \typeout{No file \@tempb: Run makeindex -o
                    \@tempb \space \@tempc}%
            }%
        }%
}
\def\@indexstar@{\index*}
\def\idx@activehat{%
    \relax
    \ifmmode\expandafter\sp\else\expandafter\@indexstar@\fi
}

\def\idx@activebar{%
    \relax
    \ifmmode\expandafter\sb\else\expandafter\index\fi
}
\newif\if@shortindexing

\begingroup

    \catcode`\^\active
    \catcode`\_\active

    \gdef\shortindexingon{%
        \@shortindexingtrue
        \chardef\old@idxhatcode\catcode`\^\relax
        \chardef\old@idxbarcode\catcode`\_\relax
        \catcode`\^\active
        \catcode`\_\active
        \let\old@idxhat ^%
        \let\old@idxbar _%
        \let^\idx@activehat
        \let_\idx@activebar
    }

    \gdef\shortindexingoff{%
        \if@shortindexing
            \@shortindexingfalse
            \let^\old@idxhat
            \let_\old@idxbar
            \catcode`\^\old@idxhatcode
            \catcode`\_\old@idxbarcode
        \fi
    }

\endgroup
\newinsert\@indexbox

\dimen\@indexbox\maxdimen
\begingroup
    \catcode`\@\active
    \expandafter\gdef\csname\string @sanitizeat\endcsname
        {\def @{\char`\@}}
\endgroup
\newtoks\indexproofstyle

\indexproofstyle{\footnotesize\reset@font\ttfamily}

\def\@showidx#1{%
    \insert\@indexbox{%
        \@sanitizeat
        \the\indexproofstyle
        \hsize\marginparwidth
        \hangindent\marginparsep \parindent\z@
        \everypar{}\let\par\@@par \parfillskip\@flushglue
        \lineskip\normallineskip
        \baselineskip .8\normalbaselineskip\sloppy
        \raggedright \leavevmode
        \vrule \@height .7\normalbaselineskip \@width \z@\relax#1\relax
        \vrule \@height\z@ \@depth.3\normalbaselineskip \@width\z@\relax
    }%
    \ifhmode\penalty\@M \hskip\z@skip\fi
}

\def\@leftidx{\hskip-\marginparsep \hskip-\marginparwidth}

\def\@rightidx{\hskip\columnwidth \hskip\marginparsep}

\def\@mkidx{%
    \vbox to \z@{%
        \rlap{%
            \if@twocolumn
                \if@firstcolumn \@leftidx \else \@rightidx \fi
            \else
                \if@twoside
                    \ifodd\c@page \@rightidx \else \@leftidx \fi
                \else
                    \@rightidx
                \fi
            \fi
            \box\@indexbox
        }%
        \vss
    }%
}

\def\raggedbottom{%
    \def\@textbottom{\vskip\z@ plus.0001fil}%
    \let\@texttop\@mkidx
}

\def\flushbottom{\let\@textbottom\relax \let\@texttop\@mkidx}

\let\@texttop\@mkidx
\CheckCommand\addtocontents[2]{%
  \protected@write\@auxout
      {\let\label\@gobble \let\index\@gobble \let\glossary\@gobble}%
      {\string\@writefile{#1}{#2}}%
}

\renewcommand{\addtocontents}[2]{%
    \protected@write\@auxout
      {\let\label\@gobble \let\glossary\@gobble}%
      {\string\@writefile{#1}{#2}}%
}
\let\old@starttoc\@starttoc

\renewcommand{\@starttoc}[1]{%
    \begingroup
        \@addtoindexfalse
        \old@starttoc{#1}%
    \endgroup
}
\endinput
%%
%% End of file `index.sty'.
