%%
%% This is file `pagenote.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% pagenote.dtx  (with options: `usc')
%% 
%% -----------------------------------------------------------------
%%   Author: Peter Wilson (The Herries Press)
%%   Maintainer: Will Robertson (will dot robertson at latex-project dot org)
%%   Copyright 2004 Peter R. Wilson
%% 
%%   This work may be distributed and/or modified under the
%%   conditions of the LaTeX Project Public License, either
%%   version 1.3c of this license or (at your option) any
%%   later version: <http://www.latex-project.org/lppl.txt>
%% 
%%   This work has the LPPL maintenance status "maintained".
%%   The Current Maintainer of this work is Will Robertson.
%% 
%%   This work consists of the files listed in the README file.
%% -----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pagenote}[2009/09/03 v1.1a page/end notes]
\@ifclassloaded{memoir}{}{\RequirePackage{ifmtarg}}

\newif\ifpnhaschapter
\@ifundefined{chapter}{\pnhaschapterfalse}{\pnhaschaptertrue}

\providecommand{\f@rtoc}{}

\newif\ifpnpageopt
  \pnpageoptfalse
\newif\ifpncontopt
  \pncontoptfalse

\DeclareOption{page}{\pnpageopttrue}
\DeclareOption{continuous}{\pncontopttrue}
\ProcessOptions

\ifpncontopt
  \newcounter{pagenote}
\else
  \ifpnhaschapter
    \newcounter{pagenote}[chapter]
  \else
    \newcounter{pagenote}[section]
  \fi
\fi
\renewcommand{\thepagenote}{\arabic{pagenote}}
\setcounter{pagenote}{0}
\newcounter{pnotesavechap}
  \setcounter{pnotesavechap}{-1000}

\newif\ifmakingpagenotes
  \makingpagenotesfalse

\newcommand*{\makepagenote}{%
  \newwrite\@notefile
  \immediate\openout\@notefile=\jobname.ent
  \makingpagenotestrue
  \def\pagenote{\@bsphack\begingroup
    \@sanitize
    \@wrpnote}%
  \typeout{Writing note file \jobname.ent}%
  \let\makepagenote\@empty}

\newcommand{\immediate@protected@write}[3]{%
  \begingroup
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

\ifpnpageopt
  \let\@pnwrite\protected@write
\else
  \let\@pnwrite\immediate@protected@write
\fi

\newcommand{\@wrpnote}[2][]{%
  \refstepcounter{pagenote}%
  \notenumintext{\thepagenote}%
  \ifpnhaschapter
    \ifnum\value{pnotesavechap}=\value{chapter}\else
      \setcounter{pnotesavechap}{\value{chapter}}%
      \addtonotes{\pagenotesubhead{\thechapter}{\f@rtoc}}%
    \fi
  \else
    \ifnum\value{pnotesavechap}=\value{section}\else
      \setcounter{pnotesavechap}{\value{section}}%
      \addtonotes{\pagenotesubhead{\thesection}{}}%
    \fi
  \fi
  \@pnwrite\@notefile{}
    {\string\noteentry{\thepagenote}{#1}{#2}{\thepage}}%
  \endgroup
  \@esphack}

\def\pagenote{\@bsphack\begingroup \@sanitize\@pagenote}

\newcommand{\@pagenote}[2][]{\endgroup\@esphack}

\newcommand{\addtonotes}[1]{%
  \ifmakingpagenotes
 \IfFileExists{\jobname.ent}{\@pnwrite\@notefile{}{#1}}{\pnofilewarn}%
\fi}

\newcommand{\notenumintext}[1]{%
  \textsuperscript{#1}}
\newcommand{\notenuminnotes}[1]{%
  {\normalfont #1.} }
\newcommand{\noteentry}[4]{%
  \prenoteinnotes
  \noteidinnotes{#1}{#2}\pageinnotes{#4}\noteinnotes{#3}%
  \postnoteinnotes}

\newcommand{\textinnotes}[1]{%
  [#1] }
\newcommand{\noteidinnotes}[2]{%
  \@ifmtarg{#2}{%
    \notenuminnotes{#1}}{\textinnotes{#2}}}
\providecommand*{\pagename}{page}
\newcommand{\pageinnotes}[1]{%
  \ifpnpageopt (\pagename\ #1) \fi}
\newcommand{\noteinnotes}[1]{#1}

\newcommand{\prenoteinnotes}{\par\noindent}
\newcommand{\postnoteinnotes}{\par}

\providecommand*{\notesname}{Notes}
\ifpnhaschapter
  \newcommand*{\notedivision}{\chapter{\notesname}}
\else
  \newcommand*{\notedivision}{\section{\notesname}}
\fi

\newcommand*{\printnotes}{\@ifstar{\@sprintnotes}{\@printnotes}}
\newcommand*{\pnofilewarn}{%
  \PackageWarning{pagenote}{There is no .ent file}}
\newcommand*{\@sprintnotes}{%
  \ifmakingpagenotes
  \notedivision
\IfFileExists{\jobname.ent}{%
  \immediate\closeout\@notefile
  \input{\jobname.ent}%
  \immediate\openout\@notefile=\jobname.ent%
  }{%
  \pnofilewarn
}%
\fi}

\newcommand*{\@printnotes}{%
  \ifmakingpagenotes
  \notedivision
\IfFileExists{\jobname.ent}{%
  \immediate\closeout\@notefile
  \input{\jobname.ent}%
  }{%
  \pnofilewarn
}
\fi}

\providecommand*{\chaptername}{Chapter}
\providecommand*{\sectionname}{Section}
\ifpnhaschapter
  \DeclareRobustCommand{\pagenotesubhead}[2]{%
    \subsection*{\chaptername\ #1 #2}}
\else
  \DeclareRobustCommand{\pagenotesubhead}[2]{%
    \subsection*{\sectionname\ #1}}
\fi

\endinput
%%
%% End of file `pagenote.sty'.
