%%
%% This is file `xnotes2bib.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% xnotes2bib.dtx  (with options: `package')
%% ---------------------------------------------------------------
%% The notes2bib bundle --- Integrating notes into the
%%   bibliography
%% Maintained by Joseph Wright
%% E-mail: joseph.wright@morningstar2.co.uk
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ---------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\begingroup
  \@ifundefined{eTeXversion}{%
    \PackageError{xnotes2bib}
      {Not running under e-TeX}
      {This package requires e-TeX: enable the extensions!}%
    \endgroup\endinput
  }{%
    \endgroup
  }
\RequirePackage{l3names}
\ProvidesExplPackage
  {xnotes2bib}{2008/09/08}{0.0}
  {Integrating notes into the bibliography (expl3 version)}
\RequirePackage{expl3,xparse}
\DeclareOption*{
  \PackageWarning{xnotes2bib}{
    All~package~control~should~be~given~using~the\MessageBreak
    \token_to_string:N\niibsetup\space macro
  }
}
\ProcessOptions\scan_stop:
\int_new:N \g_niib_note_int
\def_new:NNn \niib_thenote: 0 {
  \niib_notename:
  \num_compare:nNnT {\g_niib_totalnotes_int} > {\c_nine}{
    \num_compare:nNnF {\g_niib_note_int} > {\c_nine}{
      0
    }
  }
  \int_to_arabic:n \g_niib_note_int
}
\RequirePackage{keyval}
\clist_new:N\g_niib_KV_bool_clist
\clist_put_left:Nn\g_niib_KV_bool_clist{true,false}
\def_new:NNn\niib_KV_bool:nT 2 {
  \define@key{niib}{#1}[true]{
    \clist_if_in:cnTF{g_niib_KV_bool_clist}{##1}{
      #2
    }{
      \PackageError{xnotes2bib}{
        Unknown~choice~`##1'
      }{
        The~Boolean~option~`#1'~accepts~only~the\MessageBreak
        values~true~and~false
      }
    }
  }
}
\def_new:NNn\niib_KV_choice:nnT 3 {
  \cs_if_free:cTF{g_niib_KV_#1_clist}{
    \clist_new:c{g_niib_KV_#1_clist}
  }{
    \clist_gclear:c{g_niib_KV_#1_clist}
  }
  \clist_put_left:cn{g_niib_KV_#1_clist}{#2}
  \define@key{niib}{#1}{
    \clist_if_in:cnTF{g_niib_KV_#1_clist}{##1}{
      #3
    }{
      \PackageError{xnotes2bib}{
        Unknown~choice~`##1'
      }{
        The~option~`#1'~accepts~only~the~values:\MessageBreak
        #2
      }
    }
  }
}
\def_new:NNn\niib_KV_cs_n:n 1 {
  \define@key{niib}{#1}{
    \def:cNn{niib_#1:n} 0 {##1}
  }
}
\def_new:NNn\niib_KV_string:n 1 {
  \define@key{niib}{#1}{
    \def:cNn{niib_#1:} 0 {##1}
  }
}
\niib_KV_cs_n:n{cite}
\niib_KV_string:n{field}
\niib_KV_string:n{key~after}
\niib_KV_string:n{key~before}
\niib_KV_string:n{key~mixed}
\niib_KV_string:n{notename}
\niib_KV_string:n{prefix}
\niib_KV_string:n{presort~after}
\niib_KV_string:n{presort~before}
\niib_KV_string:n{presort~mixed}
\niib_KV_string:n{record}
\let_new:NN\niib_convert_endnotes:\scan_stop:
\let_new:NN\niib_convert_footnotes:\scan_stop:
\niib_KV_bool:nT{convert~endnotes}{
  \let:Nc\niib_convert_endnotes:{niib_convert_endnotes_#1:}
}
\niib_KV_bool:nT{convert~footnotes}{
  \let:Nc\niib_convert_footnotes:{niib_convert_footnotes_#1:}
}
\niib_KV_bool:nT{debug}{
  \let:Nc\niib_debug:n{niib_debug_#1:n}
}
\def_new:NNn\niib_debug_true:n 1 {
  \PackageInfo{xnotes2bib}{#1}
}
\let_new:NN\niib_debug_false:n\use_none:n
\bool_new:N\l_niib_after_bool
\bool_new:N\l_niib_before_bool
\niib_KV_choice:nnT{placement}{after,before,mixed}{
  \bool_set_false:N\l_niib_before_bool
  \bool_set_false:N\l_niib_after_bool
  \cs_if_really_exist:cT{l_niib_#1_bool}{
    \bool_set_true:c{l_niib_#1_bool}
  }
}
\setkeys{niib}{
  cite             = \cite,
  debug            = false,
  field            = note,
  key~after        = zzz,
  key~before       = aaa,
  key~mixed        = {},
  notename         = Bibnote,
  placement        = mixed,
  prefix           = niib-,
  presort~after    = mn,
  presort~before   = ml,
  presort~mixed    = mm,
  record           = Misc}
\NewDocumentCommand{\bibnote}{o>{P}m}{
  \IfNoValueTF{#1}{
    \int_gincr:N \g_niib_note_int
    \bibnotetext{#2}
    \bibnotemark[\niib_thenote:]
  }{
    \bibnotetext[#1]{#2}
    \bibnotemark[#1]
  }
}
\NewDocumentCommand{\bibnotemark}{o}{
  \IfNoValueTF{#1}{
    \int_gincr:N \g_niib_note_int
    \niib_notemark:n{\niib_thenote:}
  }{
    \niib_notemark:n{#1}
  }
}
\NewDocumentCommand{\bibnotetext}{O{\niib_thenote:}>{P}m}{
  \if@filesw
    \cs_if_free:NT \g_niib_out_stream {
      \iow_new:N \g_niib_out_stream
      \iow_open:Nn \g_niib_out_stream
        {\niib_prefix:\c_job_name_tlp.bib}
      \iow_expanded:Nn \g_niib_out_stream
        {This~is~an~auxiliary~file~used~by~the~`xnotes2bib'~
         package.^^J This~file~may~safely~be~deleted.^^J It~
         will~be~recreated~as~required.^^J}
    }
    \niib_debug:n{
      Writing~text:\MessageBreak #2\MessageBreak to~database~
      for~note~`#1'
    }
    \iow_expanded:Nn \g_niib_out_stream {
      @\niib_record:{
        #1,^^J
        \space\space \niib_field:\space
          =~{\exp_not:n{#2}},^^J
        \space\space \niib_keyname:\space
          =~{#1}            ,^^J
        \space\space presort~
          =~{\niib_presort:},^^J
      }^^J
    }
  \fi
}
\NewDocumentCommand{\flushnotestack}{}{
  \clist_if_empty:NF\g_niib_beforecite_clist{
    \if@filesw
      \iow_expanded:Nn\@auxout{
        \token_to_string:N\niibbeforecite{
          \g_niib_beforecite_clist
        }
      }
    \fi
  }
  \clist_if_empty:NF\g_niib_aftercite_clist{
    \nocite{\g_niib_aftercite_clist}
    \if@filesw
      \iow_expanded:Nn\@auxout{
        \token_to_string:N\niibaftercite{\g_niib_aftercite_clist}
      }
    \fi
  }
  \clist_gclear:N\g_niib_aftercite_clist
}
\NewDocumentCommand{\niibsetup}{m}{\setkeys{niib}{#1}}
\clist_new:N\g_niib_oldbeforecite_clist
\clist_new:N\g_niib_oldaftercite_clist
\tlp_new:N\g_niib_document_tlp
\tlp_put_right:Nn\document{\g_niib_document_tlp}
\NewDocumentCommand{\niibbeforecite}{m}{
  \clist_gput_right:Nn\g_niib_oldbeforecite_clist{#1}
  \tlp_gput_right:Nn\g_niib_document_tlp{\nocite{#1}}
}
\NewDocumentCommand{\niibaftercite}{m}{
  \clist_gput_right:Nn\g_niib_oldaftercite_clist{#1}
}
\int_new:N \g_niib_totalnotes_int
\NewDocumentCommand{\niibtotalnotes}{m}{
  \int_gset:Nn \g_niib_totalnotes_int {#1}
}
\def_new:NNn \niib_notemark:n 1 {
  \niib_debug:n{Creating~citation~for~note~`#1'}
  \bool_if:NT\l_niib_before_bool{
    \niib_debug:n{Note~`#1'~before~real~citations}
    \clist_put_right:Nx\g_niib_beforecite_clist{#1}
  }
  \bool_if:NTF\l_niib_after_bool{
    \niib_debug:n{Note~`#1'~after~real~citations}
    \clist_put_right:Nx\g_niib_aftercite_clist{#1}
    \niib_aftercite:n{#1}
  }{
    \niib_cite:n{#1}
  }
}
\clist_new:N\g_niib_beforecite_clist
\clist_new:N\g_niib_aftercite_clist
\def_new:NNn\niib_key: 0 {
  \bool_if:NTF\l_niib_before_bool{
    \cs_use:c{niib_key~before:}
  }{
    \bool_if:NTF\l_niib_after_bool{
      \cs_use:c{niib_key~after:}
    }{
      \cs_use:c{niib_within~key:}
    }
  }
}
\def_new:NNn\niib_presort: 0 {
  \bool_if:NTF\l_niib_before_bool{
    \cs_use:c{niib_presort~before:}
  }{
    \bool_if:NTF\l_niib_after_bool{
      \cs_use:c{niib_presort~after:}
    }{
      \cs_use:c{niib_within~presort:}
    }
  }
}
\def_new:NNn\niib_convert_endnotes_false: 0 {
  \niib_debug:n{Restoring~normal~endnotes}
  \let:NN\endnote\niib_saved_endnote:
  \let:NN\endnotemark\niib_saved_endnotemark:
  \let:NN\endnotetext\niib_saved_endnotetext:
}
\def_new:NNn\niib_convert_endnotes_true: 0 {
  \niib_debug:n{Converting~endnotes~to~bibnotes}
  \let:NN\endnote\bibnote
  \let:NN\endnotemark\bibnotemark
  \let:NN\endnotetext\bibnotetext
}
\def_new:NNn\niib_convert_footnotes_false: 0 {
  \niib_debug:n{Restoring~normal~footnotes}
  \let:NN\footnote\niib_saved_footnote:
  \let:NN\footnotemark\niib_saved_footnotemark:
  \let:NN\footnotetext\niib_saved_footnotetext:
}
\def_new:NNn\niib_convert_footnotes_true: 0 {
  \niib_debug:n{Converting~footnotes~to~bibnotes}
  \let:NN\footnote\bibnote
  \let:NN\footnotemark\bibnotemark
  \let:NN\footnotetext\bibnotetext
}
\def:NNn\thanks 1 {
  \niib_saved_footnotemark:
  \protected@xdef\@thanks{\@thanks
  \protect\niib_saved_footnotetext:[\the\c@footnote]{#1}}
}
\AtBeginDocument{
  \PackageInfo{xnotes2bib}{
    Beginning~\token_to_string:N\AtBeginDocument\space tasks
  }
  \@ifpackageloaded{biblatex}{
    \def_new:NNn \niib_keyname: 0 {keysort}
    \gappto\blx@bibfiles{,\niib_prefix\c_job_name_tlp}
    \let_new:NN\printbibnotes\printbibliography
    \def_new:NNn\niib_aftercite:n 1 {
      \AtNextCite{\@fileswfalse}
      \niib_cite:n{#1}
    }
  }{
    \def_new:NNn \niib_keyname: 0 {key}
    \let_new:NN\niib_biliography:n\bibliography
    \def:NNn\bibliography 1 {
      \int_compare:nNnTF{\g_niib_note_int}={\c_zero}{
        \niib_biliography:n{#1}
      }{
        \niib_biliography:n{#1,\niib_prefix:\c_job_name_tlp}
      }
    }
    \NewDocumentCommand{\printbibnotes}{}{
      \niib_biliography:n{\niib_prefix:\c_job_name_tlp}
    }
    \newif\ifniib@filesw
    \@ifpackageloaded{cite}{
      \def_new:NNn\niib_aftercite:n 1 {
         \let:NN\ifniib@filesw\if@filesw
         \@fileswfalse
         \def:NNn{\niib_auxhandle:} 0 {
           \let:NN\if@filesw\ifniib@filesw
           \let:NN\niib_auxhandle:\scan_stop:
         }
         \niib_cite:n{#1}
      }
      \cs_if_exist:NTF\@restore@auxhandle{
        \tlp_put_right:Nn\@restore@auxhandle{\niib_auxhandle:}
      }{
        \def_new:NNn\@restore@auxhandle 0 {\niib_auxhandle:}
      }
      \let_new:NN\niib_auxhandle:\scan_stop:
    }{
      \def_new:NNn\niib_aftercite:n 1 {
         \let\ifniib@filesw\if@filesw
         \@fileswfalse
         \niib_cite:n{#1}
         \let\if@filesw\ifniib@filesw
      }
    }
  }
  \let_new:NN\niib_saved_footnote:\footnote
  \let_new:NN\niib_saved_footnotemark:\footnotemark
  \let_new:NN\niib_saved_footnotetext:\footnotetext
  \let_new:NN\niib_saved_endnote:\endnote
  \let_new:NN\niib_saved_endnotemark:\endnotemark
  \let_new:NN\niib_saved_endnotetext:\endnotetext
  \niib_convert_endnotes:
  \niib_convert_footnotes:
  \niib_KV_bool:nT{convert~endnotes}{
    \cs_use:c{niib_convert_endnotes_#1:}
  }
  \niib_KV_bool:nT{convert~footnotes}{
    \cs_use:c{niib_convert_footnotes_#1:}
  }
  \PackageInfo{xnotes2bib}{
    Finished~\token_to_string:N\AtBeginDocument\space tasks
  }
}
\def_new:NNn\niib_rerun_check:n 1 {
  \clist_if_empty:cTF {g_niib_old#1cite_clist}{
    \clist_if_empty:cF {g_niib_#1cite_clist}{
      \niib_rerun_log:n {#1}
    }
  }{
    \clist_if_empty:cTF {g_niib_#1cite_clist}{
      \niib_rerun_log:n {#1}
    }{
      \bool_set_false:N\l_tmpa_bool
      \def:NNn\niib_rerun_test:n 1 {
        \clist_if_in:cnTF{g_niib_old#1cite_clist}{##1}{
          \bool_set_true:N\l_tmpa_bool
          \clist_map_break:w
        }{
        }
      }
      \clist_map_function:cN{g_niib_#1cite_clist}
        \niib_rerun_test:n
      \def:NNn\niib_rerun_test:n 1 {
        \clist_if_in:cnTF{g_niib_#1cite_clist}{##1}{
          \bool_set_true:N\l_tmpa_bool
          \clist_map_break:w
        }{
        }
      }
      \clist_map_function:cN{g_niib_old#1cite_clist}
        \niib_rerun_test:n
      \bool_if:NF\l_tmpa_bool{
        \niib_rerun_log:n {#1}
      }
    }
  }
}
\def:NNn\niib_rerun_test:n 0 {}
\def_new:NNn\niib_rerun_log:n 1 {
  \PackageInfo{xnotes2bib}{
    To~get~`#1'~notes~in~the~correct~order,~please\MessageBreak
    rerun~LaTeX,~(re)run~BibTeX~on~the~file~\c_job_name_tlp.aux
    \MessageBreak and~rerun~LaTeX~again~afterwards
  }
  \io_put_log:x {
    REQ:3:latex:REQ\iow_newline:
    REQ:2:bibtex:REQ\iow_newline:
    REQ:1:latex:REQ\iow_newline:
  }
}
\AtEndDocument{
  \niib_rerun_check:n{before}
  \niib_rerun_check:n{after}
  \flushnotestack
  \iow_expanded:Nn\@auxout{
    \token_to_string:N\niibtotalnotes{
      \int_to_arabic:n \g_niib_note_int
    }
  }
  \num_compare:nNnF{\g_niib_totalnotes_int}={\g_niib_note_int}{
    \niib_rerun_log:n
  }
  \cs_if_free:NF \g_niib_out_stream{
    \iow_close:N \g_niib_out_stream
  }
}
%% 
%% Copyright (C) 2007-2008 by
%%   Joseph Wright <joseph.wright@morningstar2.co.uk>
%% 
%% It may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License (LPPL), either version 1.3c of
%% this license or (at your option) any later version.  The latest
%% version of this license is in the file:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%%   Joseph Wright.
%% 
%% This work consists of the file  notes2bib.dtx
%%                                 xnotes2bib.dtx
%%           and the derived files notes2bib.pdf,
%%                                 notes2bib.sty,
%%                                 notes2bib.ins,
%%                                 xnotes2bib.pdf,
%%                                 xnotes2bib.sty and
%%                                 xnotes2bib.ins.
%% 
%%
%% End of file `xnotes2bib.sty'.
