%%
%% \iffalse filename: endnotes.dtx \fi
%%
%<*doc>
\input driver
\thisis{endnotes}{Endnotes}

The usual \LaTeX\ endnote option stores the unexecuted text of notes and
then executes them all at the end of the document. This does not work for legal
citations because the content of the body text interacts with the content of the
endnotes: An inline citation in body text can change the appearance of citations
in the notes, for example.
Instead, it is necessary to typeset endnotes as they are encountered and store
them to a vertical box that can be displayed upon request.

To use endnotes, include the option \opt{endnote} with this package. This will
automatically convert all footnotes to endnotes, including citations using
|\note|. (Specifically, all footnote texts will be executed with the macro
|\HiEndNote| described below). The following macros may further be used to
control the content of the endnotes.

%</doc>
%<*package>
%
% If endnotes are not used, then define the user commands to do nothing.
%
%    \begin{macrocode}
\let\HiEndNote\@gobble
\let\HiENDelay\@gobble
\let\HiENSection\relax
\def\hi@useendnotes{%
%    \end{macrocode}
%
% The subsequent code is only run if endnotes are requested.
%
% |\hi@endnotes| is the vertical box where endnotes will be stored until used.
%    \begin{macrocode}
    \newbox\hi@endnotes
%    \end{macrocode}
%
% |\@footnotetext| is redefined to run |\HiEndNote|, adding the note text to
% the vertical box. Note that, as described in \sec{state}, the |\@footnotetext|
% macro will be decorated for citation count tracking with |\AtBeginDocument|,
% so the tracking macros need not be added here.
%
%    \begin{macrocode}
    \long\def\@footnotetext##1{%
        \HiEndNote{%
            \reset@font\normalsize
            \parskip\z@
            \HiENFont
            \protected@edef\@currentlabel{%
               \csname p@footnote\endcsname\@thefnmark
            }%
            \color@begingroup
            \@makefntext{\ignorespaces##1}%
            \color@endgroup
        }%
    }%
%    \end{macrocode}
%
%</package>
%<*doc>

\MacroSpec\HiEndNote \marg{text} \\
Adds arbitrary text to the endnote list. The text is
set in vertical mode.

%</doc>
%<*package>
%
%    \begin{macrocode}
    \long\def\HiEndNote##1{%
        \global\setbox\hi@endnotes\vbox{%
            \ifvoid\hi@endnotes\else
                \prevdepth\dp\hi@endnotes
                \unvbox\hi@endnotes
            \fi
            \ifx\hi@endnotes@delayed\relax\else
                \hi@endnotes@delayed
                \global\let\hi@endnotes@delayed\relax
            \fi
            ##1%
        }%
    }
%    \end{macrocode}
%
%</package>
%<*doc>

\MacroSpec\HiENDelay\marg{text}\\
reserves a certain text for future adding to the endnote
list. The text is actually added only if other endnote text is added before
|\HiENDelay| is invoked a second time.
This is useful for adding chapter or section heading markers to and endnote list
conditionally. If the chapter contains no endnotes, then the chapter heading
will never be inserted into the endnote list.

Again, the text is set in vertical mode.

%</doc>
%<*package>
%
%    \begin{macrocode}
    \def\HiENDelay##1{%
        \global\def\hi@endnotes@delayed{##1}%
    }
    \let\hi@endnotes@delayed\relax
%    \end{macrocode}
%
%</package>
%<*doc>

\MacroSpec\HiENSection
Inserts text into the endnote list (using |\HiENDelay|) indicating where a
section heading occurred. The section number is inserted automatically, preceded
by ``Section.''

%</doc>
%<*package>
%
%    \begin{macrocode}
    \def\HiENSection{%
        \HiENDelay{%
            \vskip .7\baselineskip
            {\noindent\large\textbf{Section \@currentlabel}\par}%
            \penalty\@M
            \vskip .3\baselineskip
        }%
    }
%    \end{macrocode}
%
%</package>
%<*doc>

\DescribeMacro\HiENFont
|\HiENFont| can be defined to set the font for endnotes. It must be defined
before endnotes are added, since once they are added the notes have been
typeset and cannot be changed.

%</doc>
%<*package>
%
%    \begin{macrocode}
    \def\HiENFont{}%
%    \end{macrocode}
%
%</package>
%<*doc>

\DescribeMacro\theendnotes
|\theendnotes| emits the list of endnotes. If the macro is not called, it will
be automatically executed at the end of the document with a warning.

%</doc>
%<*package>
%
%    \begin{macrocode}
    \def\theendnotes{%
        \setbox\hi@endnotes\vtop{\unvbox\hi@endnotes}%
        \@tempskipa\baselineskip
        \multiply\@tempskipa\m@ne
        \advance\@tempskipa\prevdepth
        \advance\@tempskipa\ht\hi@endnotes
        \multiply\@tempskipa\m@ne
        \ifdim\@tempskipa>\z@ \vskip\@tempskipa \fi
        \unvbox\hi@endnotes
    }
    \AtEndDocument{%
        \ifvoid\hi@endnotes\else
            \PackageWarning\hi@pkgname{%
                Endnotes not displayed;\MessageBreak inserting them%
            }%
            \vskip\baselineskip
            \footnoterule
            \theendnotes
        \fi
    }%
}% End of |\hi@useendnotes|
%    \end{macrocode}
%</package>
