%%
%% 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 LaTeX Project Public License v1.3 or later
%%  See http://www.latex-project.org/lppl.txt
%%  ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{notes2bib}
  [2007/09/29 v1.1 Integrating notes into the bibliography]
\@ifundefined{eTeXversion}
  {\PackageError{notes2bib}
     {Not running under e-TeX}
     {This package requires e-TeX. Try compiling the document
      with\MessageBreak `elatex' instead of `latex'. When using
      pdfTeX, try `pdfelatex'\MessageBreak instead of `pdflatex'}%
   \endinput}
  {}
\RequirePackage{elatex}[2007/07/13]
\newif\ifniib@fn\niib@fnfalse
\DeclareOption{footnotes}{%
  \niib@fntrue
  \AtBeginDocument{%
    \let\footnote\bibnote%
  \PackageInfo{notes2bib}%
    {Converting footnotes to bibnotes}%
}}
\DeclareOption{endnotes}{%
  \AtBeginDocument{%
    \let\endnote\bibnote%
    \let\endnotemark\bibnotemark%
    \let\endnotetext\bibnotetext%
    \@ifpackageloaded{endnotes}%
      {\let\theendnotes\printbibnotes}{}%
    \PackageInfo{notes2bib}%
      {Converting endnotes to bibnotes}%
}}
\newif\ifniib@tail\niib@tailfalse
\newif\ifniib@head\niib@headfalse
\DeclareOption{tail}{\niib@tailtrue}
\DeclareOption{head}{\niib@headtrue}
\ProcessOptions
\ifniib@fn
  \let\niib@fnmark\footnotemark
  \let\niib@fntext\footnotetext
  \let\niib@thanks\thanks
  \patchcmd{\thanks}{\footnotemark}{\niib@fnmark}{%
    \patchcmd{\thanks}{\footnotetext}{\niib@fntext}{%
      \AtBeginDocument{%
        \let\footnotemark\bibnotemark
        \let\footnotetext\bibnotetext
      }%
  \PackageInfo{notes2bib}%
    {Modifying \protect\thanks\space macro}%
    }{%
    \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
  \else
    \PackageInfo{notes2bib}%
      {bibnotes will ``head'' other citations}
  \fi
\else
  \PackageInfo{notes2bib}%
    {bibnotes will ``tail'' other citations}
\fi
\AtBeginDocument{%
  \ifniib@head
    \let\delaycite\niib@delaycite
    \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}%
      \@ifundefined{autocite}{}%
        {\delaycite{autocite}}%
      \@ifundefined{supercite}{}%
        {\delaycite{supercite}}%
      }{}%
  \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}
\newrobustcmd*{\bibnote}{%
  \stepcounter{bibnote}%
  \@ifnextchar[%
    {\niib@bibnote}%
    {\niib@bibnote[\thebibnote]}%
}
\newrobustcmd*{\bibnotemark}{%
  \stepcounter{bibnote}%
  \@ifnextchar[%
    {\niib@mark}%
    {\niib@mark[\thebibnote]}%
}
\newrobustcmd*{\bibnotetext}[1][\thebibnote]{%
  \niib@text{#1}%
}
\@ifpackageloaded{biblatex}{%
  \let\printbibnotes\printbibliography%
}{%
  \newrobustcmd*{\printbibnotes}{\niib@bib%
    {\niibprefix\jobname}}%
}%
\newrobustcmd*{\flushcitestack}{%
  \ifx\niib@stack\relax\else%
    \expandafter\nocite\expandafter{\niib@stack}%
    \global\let\niib@stack\relax%
  \fi%
}
\newrobustcmd*{\delaycite}[1]{%
  \PackageWarning{notes2bib}{%
    \@nameuse{delaycite}can only be used in the document
    body\MessageBreak Not modifying citation macro \@nameuse{#1}}
}
\ifniib@tail
  \newrobustcmd*\citenote[2][\relax]{%
    \begingroup%
      \@fileswfalse%
      \ifx#1\relax%
        \niibcite{#2}%
      \else
        \niibcite[#1]{#2}%
      \fi
    \endgroup%
  }%
\else
  \newrobustcmd*\citenote{\niibcite}%
\fi
\@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
\newrobustcmd*\niib@delaycite[1]{%
  \@ifundefined{#1}{%
  \PackageWarning{notes2bib}
    {Citation macro \@nameuse{#1}undefined\MessageBreak
    \protect\delaycite\space cannot operate on this macro}
  }{%
    \@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}%
        {Modifying citation type \@nameuse{#1}}%
    }{%
      \PackageInfo{notes2bib}%
        {Already delayed \@nameuse{#1}}%
    }%
  }
}
\long\def\niib@bibnote[#1]#2{%
  \niib@text{#1}{#2}%
  \niib@cite{#1}%
}
\def\niib@mark[#1]{\niib@cite{#1}}
\ifniib@head%
  \def\niib@cite#1{%
    \niib@cntrue%
    \niibcite{#1}%
    \niib@cnfalse%
  }%
\else%
  \ifniib@tail%
    \def\niib@cite#1{%
    \ifx\niib@stack\relax%
      \xdef\niib@stack{#1}%
    \else%
      \xdef\niib@stack{\niib@stack,#1}%
    \fi%
    \begingroup
      \@fileswfalse%
      \niibcite{#1}%
    \endgroup%
    }%
  \else%
    \def\niib@cite#1{\niibcite{#1}}%
  \fi%
\fi%
\long\def\niib@text#1#2{%
  \@ifundefined{niib@out}{%
    \if@filesw%
      \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%
    \immediate\write\niib@out{%
    @\niibrecord\string{#1,^^J%
    presort = \string{\niib@presort\string},^^J%
    \niib@keyname\space= \string{\niib@key#1\string},^^J%
    \niibfield\space= \string{\unexpanded{#2}\string}^^J%
    \string}^^J}%
  \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%
  \@ifundefined{niib@out}{}%
    {\immediate\closeout\niib@out}%
}
\endinput
%%
%% End of file `notes2bib.sty'.
