%%
%% This is file `notes2bib.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% notes2bib.dtx  (with options: `package')
%%  ----------------------------------------------------------------
%%  note2bib --- Integrating notes into the bibliography
%%  Maintained by Joseph Wright
%%  E-mail: joseph.wright@morningstar2.co.uk
%%  Released under the GNU General Public License
%%  See http://www.gnu.org/licenses/gpl.txt
%%  ----------------------------------------------------------------
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{notes2bib}
  [2007/09/03 v1.0a Integrating notes into the bibliography]
\RequirePackage{xspace}
\newif\ifniib@fn\niib@fnfalse
\DeclareOption{footnotes}{%
  \niib@fntrue
  \AtBeginDocument{%
    \let\footnote\bibnote%
}}
\DeclareOption{endnotes}{%
  \AtBeginDocument{%
    \let\endnote\bibnote%
    \let\endnotemark\bibnotemark%
    \let\endnotetext\bibnotetext%
    \@ifpackageloaded{endnotes}%
      {\let\theendnotes\printbibnotes}{}%
}}
\newif\ifniib@tail\niib@tailfalse
\newif\ifniib@head\niib@headfalse
\DeclareOption{tail}{\niib@tailtrue}
\DeclareOption{head}{\niib@headtrue}
\def\niib@percent{\catcode`\%=14\relax}
\DeclareOption{killpercent}{%
  \def\niib@percent{\catcode`\%=9\relax}
}
\@ifclassloaded{ltxdoc}{\ExecuteOptions{killpercent}}{}
\ProcessOptions
\ifniib@fn
  \@ifundefined{eTeXversion}{%
    \PackageWarning{notes2bib}{%
      e-TeX extensions unavailable\MessageBreak
      \protect\footnotemark and \protect\footnotetext%
      \MessageBreak unmodified}%
    }{%
    \let\niib@fnmark\footnotemark
    \let\niib@fntext\footnotetext
    \let\niib@thanks\thanks
    \RequirePackage{elatex}
    \patchcmd{\thanks}{\footnotemark}{\niib@fnmark}{%
      \patchcmd{\thanks}{\footnotetext}{\niib@fntext}{%
        \AtBeginDocument{%
          \let\footnotemark\bibnotemark
          \let\footnotetext\bibnotetext
        }%
      }%
      {\let\thanks\niib@thanks%
      \PackageWarning{notes2bib}%
        {Could not patch \protect\thanks}}%
    }{%
    \let\thanks\niib@thanks%
    \PackageWarning{notes2bib}%
      {Could not patch \protect\thanks}}%
  }%
\fi
\ifniib@head
  \ifniib@tail
    \PackageWarning{notes2bib}
      {You've given both ``head'' and ``tail'' as
        options\MessageBreak
      I'm ignoring both!}
    \niib@headfalse\niib@tailfalse
  \fi
\fi
\AtBeginDocument{%
  \ifniib@head
    \delaycite{cite}%
    \@ifpackageloaded{natbib}{%
      \delaycite{citet}%
      \delaycite{citep}%
      \delaycite{citenum}%
      \delaycite{citealt}%
      \delaycite{citealp}%
      \delaycite{citetext}%
      \delaycite{citeauthor}%
      \delaycite{citeyear}%
      \delaycite{citeyearpar}%
      \delaycite{Citet}%
      \delaycite{Citep}%
      \delaycite{Citealt}%
      \delaycite{Citealp}%
      \delaycite{Citeauthor}%
    }{%
      \@ifpackageloaded{biblatex}{%
        \delaycite{Cite}%
        \delaycite{parencite}%
        \delaycite{Parencite}%
        \delaycite{footcite}%
        \delaycite{Footcite}%
        \delaycite{textcite}%
        \delaycite{Textcite}%
        \ifdefined\autocite%
          \delaycite{autocite}%
        \fi%
        \ifdefined\supercite%
          \delaycite{supercite}%
        \fi%
      }{}%
    }%
  \fi%
}
\newcommand*\bibnotename{Bibnote}
\newcounter{bibnote}
\renewcommand*{\thebibnote}{\bibnotename\the\value{bibnote}}
\newcommand*\niibprefix{niib-}
\newcommand*\niibrecord{Misc}
\newcommand*\niibfield{note}
\newcommand*\niibpresorthead{ml}
\newcommand*\niibpresortnone{mm}
\newcommand*\niibpresorttail{mn}
\newcommand*\niibkeyhead{aaa}
\newcommand*\niibkeynone{}
\newcommand*\niibkeytail{zzz}
\newcommand*\niibcite{\cite}
\DeclareRobustCommand\bibnote{%
  \stepcounter{bibnote}%
  \@ifnextchar[%
    {\niib@bibnote}%
    {\niib@bibnote[\thebibnote]}%
}
\DeclareRobustCommand\bibnotemark{%
  \stepcounter{bibnote}%
  \@ifnextchar[%
    {\niib@mark}%
    {\niib@mark[\thebibnote]}%
}
\DeclareRobustCommand\bibnotetext{%
  \@ifnextchar[%
    {\niib@text}%
    {\niib@text[\thebibnote]}%
}
\@ifpackageloaded{biblatex}{%
  \let\printbibnotes\printbibliography%
}{%
  \newcommand*{\printbibnotes}{\niib@bib%
    {\niibprefix\jobname}}%
}%
\newcommand*\flushcitestack{%
  \ifx\niib@stack\relax\else%
    \expandafter\nocite\expandafter{\niib@stack}%
    \global\let\niib@stack\relax%
  \fi%
}
\newcommand*\delaycite[1]{%
  \@ifundefined{niib@orig@#1}{%
    \expandafter\expandafter\expandafter\let\expandafter%
      \csname niib@orig@#1\expandafter\endcsname\csname #1\endcsname%
    \expandafter\renewcommand\csname #1\endcsname[2][\relax]{%
      \ifniib@cn%
        \csname niib@orig@#1\endcsname{##2}%
      \else%
        \begingroup%
          \@fileswfalse%
          \ifx##1\relax%
            \csname niib@orig@#1\endcsname{##2}%
          \else%
            \csname niib@orig@#1\endcsname[##1]{##2}%
          \fi%
        \endgroup%
        \edef\niib@temp{##2}%
        \ifx\niib@stack\relax%
          \xdef\niib@stack{\niib@temp}%
        \else%
          \xdef\niib@stack{\niib@stack,\niib@temp}%
        \fi%
      \fi%
    }%
  }{%
    \PackageInfo{notes2bib}%
      {Citation type ``#1'' already delayed}%
  }
}
\ifniib@tail
  \newcommand*\citenote[2][\relax]{%
    \begingroup%
      \@fileswfalse%
      \ifx#1\relax
        \niibcite{#2}%
      \else
        \niibcite[#1]{#2}
      \fi
    \endgroup%
  }
\else
  \newcommand*\citenote{\niibcite}
\fi
\newcommand*{\Percent}{\%\xspace}
\@ifpackageloaded{biblatex}
  {\def\niib@keyname{keysort}}
  {\def\niib@keyname{key}}
\ifniib@head
  \def\niib@presort{\niibpresorthead}
  \def\niib@key{\niibkeyhead}
\else
  \ifniib@tail
    \def\niib@presort{\niibpresorttail}
    \def\niib@key{\niibkeytail}
  \else
    \def\niib@presort{\niibpresortnone}
    \def\niib@key{\niibkeynone}
  \fi
\fi
\edef\niib@msg{%
  This is an auxiliary file used by the `notes2bib' package.^^J%
  This file may safely be deleted. It will be recreated as
  required.^^J}
\let\niib@stack\relax
\newif\ifniib@cn\niib@cnfalse
\long\def\niib@text[#1]{%
  \niib@@text{#1}%
}
\long\def\niib@bibnote[#1]{%
  \niib@citenote{#1}%
  \niib@@text{#1}%
}
\def\niib@mark[#1]{\niib@citenote{#1}}
\def\niib@citenote#1{%
  \niib@cntrue%
  \edef\niib@temp{#1}%
  \ifniib@tail%
    \begingroup%
      \@fileswfalse%
      \niibcite{#1}%
    \endgroup%
    \ifx\niib@stack\relax%
      \xdef\niib@stack{\niib@temp}%
    \else%
      \xdef\niib@stack{\niib@stack,\niib@temp}%
    \fi%
  \else%
    \expandafter\niibcite\expandafter%
      {\expandafter\niib@temp\expandafter}%
  \fi%
  \niib@cnfalse%
}%
\newcommand\niib@verbtext[2][\ignorespaces]{%
  \begingroup%
  \let\do\@makeother\dospecials%
  \catcode`\{=1\relax%
  \catcode`\}=2\relax%
  \niib@@verbtext{#1}{#2}%
}%
\newcommand\niib@@verbtext[2]{%
  \begingroup%
  \long\def\niib@tempa##1{%
    \def\niib@tempa{##1}%
    \@onelevel@sanitize\niib@tempa%
    \expandafter\endgroup%
    \expandafter\def%
    \expandafter#2%
    \expandafter{%
    \niib@tempa}%
    #1%
  }%
  \catcode`\^^M=10\relax%
  \niib@percent%
  \expandafter\endgroup%
  \niib@tempa%
}%
\newcommand*\niib@write{%
  \@bsphack%
  \begingroup%
  \niib@verbtext[\niib@@write\ignorespaces]\niib@tempa%
}%
\newcommand*\niib@@write[1]{%
  \immediate\write\niib@out{%
    @\niibrecord\string{\niib@tempb,^^J%
    presort = \string{\niib@presort\string},^^J%
    \niib@keyname\space= \string{\niib@key\niib@tempb\string},^^J%
    \niibfield\space= \string{\niib@tempa\string}^^J%
    \string}^^J}%
  \endgroup%
  \@esphack%
}%
\long\def\niib@@text#1{%
  \ifx\niib@out\@undefined%
    \newwrite\niib@out%
    \gdef\niib@stream{\niibprefix\jobname.bib}%
    \immediate\openout\niib@out\niib@stream\relax%
    \immediate\write\niib@out{\niib@msg}%
  \fi%
  \if@filesw%
    \edef\niib@tempb{#1}%
    \expandafter\niib@write%
  \fi%
}
\@ifpackageloaded{biblatex}{%
  \expandafter\bib@gadd\expandafter\blx@bibfiles\expandafter%
    {\niibprefix\jobname}%
  }{%
  \let\niib@bib\bibliography%
  \renewcommand{\bibliography}[1]{%
    \ifnum\the\value{bibnote} > \z@%
      \niib@bib{\niibprefix\jobname,#1}%
    \else%
      \niib@bib{#1}%
    \fi}}%
%%
\AtEndDocument{%
  \flushcitestack%
  \ifx\niib@out\@undefined\else%
    \immediate\closeout\niib@out%
  \fi%
}
\endinput
%%
%% End of file `notes2bib.sty'.
