%%
%% 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
%% ---------------------------------------------------------------
%% 
\RequirePackage{l3names}
\GetIdInfo$Id: xnotes2bib.dtx 6 2008-10-31 21:00:00Z joseph $
  {Integrating notes into the bibliography (expl3 version)}
\ProvidesExplPackage
  {\filename}{\filedate}{\fileversion}{\filedescription}
\RequirePackage {expl3,keys3,xparse}
\err_file_new:Nn \c_niib_err_tlp {xnotes2bib.err}
\tlp_new:Nn \l_niib_err_option_tlp {option}
\err_interrupt_new:NNNnnn \c_niib_err_tlp \l_niib_err_option_tlp 0
  {All~package~control~should~be~given~using~the~\token_to_str:N~
   \niibsetup~\text_put_sp:~macro}
  {\err_help_return_or_X:}
  {}
\err_file_close:N \c_niib_err_tlp
\DeclareOption* {
  \err_interrupt:NNw \c_niib_err_tlp \l_niib_err_option_tlp
}
\ProcessOptions \scan_stop:
\int_new:N \g_niib_note_int
\def_new:Npn \niib_thenote: {
  \l_niib_name_tlp
  \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
}
\bool_new:N \l_niib_after_cite_bool
\bool_new:N \l_niib_before_cite_bool
\let_new:NN \niib_convert_endnotes: \scan_stop:
\let_new:NN \niib_convert_footnotes: \scan_stop:
\keys_manage_quick:n {
  niib/.cd:,
  cite/.store~in~tlp:N                     = \niib_cite:n,
  debug/.choice:,
  debug/.default:n                = true,
  debug/true/.code:n              = {
    \def:Npn \niib_debug:n ##1 {
      \err_info:nn {##1} {(xnotes2bib) \text_put_four_sp:}
    }
  },
  debug/false/.code:n             = {
    \let:NN \niib_debug:n \use_none:n
  },
  convert~endnotes/.choices:nn = {true,false} {
    \let:Nc \niib_convert_endnotes:
      {niib_convert_endnotes_ \l_keys_current_choice_tlp}
    },
  convert~endnotes/.default:n     = true,
  convert~footnotes/.choices:nn = {true,false} {
    \let:Nc \niib_convert_footnotes:
      {niib_convert_footnotes_ \l_keys_current_choice_tlp}
    },
  convert~footnotes/.default:n     = true,
  /keys/current~module:n          = niib,
  field/.store~in~tlp:c           = field,
  key~after/.store~in~tlp:c       = key_after,
  key~before/.store~in~tlp:c      = key_before,
  key~mixed/.store~in~tlp:c       = key_mixed,
  keyword/.store~in~tlp:c         = keyword,
  name/.store~in~tlp:c            = name,
  placement/.choice:              = \l_niib_placement_tlp,
  placement/.default:n            = mixed,
  placement/after/.code:n         = {
    \bool_set_true:N  \l_niib_after_cite_bool
    \bool_set_false:N \l_niib_before_cite_bool
  },
  placement/before/.code:n        = {
    \bool_set_false:N \l_niib_after_cite_bool
    \bool_set_true:N \l_niib_before_cite_bool
  },
  placement/mixed/.code:n         = {
    \bool_set_false:N \l_niib_after_cite_bool
    \bool_set_false:N \l_niib_before_cite_bool
  },
  /keys/current~module:n          = niib,
  prefix/.store~in~tlp:c          = prefix,
  presort~after/.store~in~tlp:c   = presort_after,
  presort~before/.store~in~tlp:c  = presort_before,
  presort~mixed/.store~in~tlp:c   = presort_mixed,
  record/.store~in~tlp:c          = record,
  write~key~field/.boolean:c      = write_key
}
\keys_manage_quick:n{
  /niib/.cd:,
  cite            = \cite,
  debug           = false,
  field           = note,
  key~after       = zzz,
  key~before      = aaa,
  key~mixed       = {},
  name            = Bibnote,
  placement       = mixed,
  prefix          = niib-,
  presort~after   = mn,
  presort~before  = ml,
  presort~mixed   = mm,
  record          = Misc,
  write~key~field = true}
\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
        {\l_niib_prefix_tlp \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 {
      @\l_niib_record_tlp{
        #1,^^J
        \text_put_sp: \text_put_sp: \l_niib_field_tlp \text_put_sp:
          =~{\exp_not:n{#2}},^^J
        \bool_if:NT \l_niib_write_key_bool {
          \text_put_sp: \text_put_sp: \l_niib_keyname_tlp \text_put_sp:
            =~{\niib_key:#1},^^J
        }
        \text_put_sp: \text_put_sp: keywords~
          =~{\l_niib_keyword_tlp},^^J
        \text_put_sp: \text_put_sp: presort~
          =~{\niib_presort:},^^J
      }^^J
    }
  \fi
}
\NewDocumentCommand {\flushnotestack} {} {
  \clist_if_empty:NF \g_niib_before_cite_clist {
    \if@filesw
      \iow_expanded:Nn \@auxout {
        \token_to_str:N \niibbeforecite {
          \g_niib_before_cite_clist
        }
      }
    \fi
  }
  \clist_if_empty:NF \g_niib_after_cite_clist {
    \nocite { \g_niib_after_cite_clist}
    \if@filesw
      \iow_expanded:Nn \@auxout {
        \token_to_str:N \niibaftercite {\g_niib_after_cite_clist}
      }
    \fi
  }
  \clist_gclear:N \g_niib_after_cite_clist
}
\NewDocumentCommand {\niibsetup} {m} {\keys_manage:n {/niib/.cd:,#1}}
\clist_new:N \g_niib_before_cite_old_clist
\clist_new:N \g_niib_after_cite_old_clist
\tlp_new:N \g_niib_document_tlp
\tlp_gput_right:Nn \document {\g_niib_document_tlp}
\NewDocumentCommand {\niibbeforecite} {m} {
  \clist_gput_right:Nn \g_niib_before_cite_old_clist {#1}
  \tlp_gput_right:Nn \g_niib_document_tlp {\nocite{#1}}
}
\NewDocumentCommand {\niibaftercite} {m} {
  \clist_gput_right:Nn \g_niib_after_cite_old_clist {#1}
}
\int_new:N \g_niib_totalnotes_int
\NewDocumentCommand {\niibtotalnotes} {m} {
  \int_gset:Nn \g_niib_totalnotes_int {#1}
}
\def_new:Npn \niib_notemark:n #1 {
  \niib_debug:n {Creating~citation~for~note~`#1'}
  \bool_if:NT \l_niib_before_cite_bool {
    \niib_debug:n {Note~`#1'~before~real~citations}
    \clist_gput_right:Nx \g_niib_before_cite_clist {#1}
  }
  \bool_if:NTF \l_niib_after_cite_bool {
    \niib_debug:n {Note~`#1'~after~real~citations}
    \clist_gput_right:Nx \g_niib_after_cite_clist {#1}
    \niib_after_cite:n {#1}
  }{
    \niib_cite:n {#1}
  }
}
\clist_new:N \g_niib_before_cite_clist
\clist_new:N \g_niib_after_cite_clist
\def_new:Npn \niib_key: {
  \bool_if:NTF \l_niib_before_cite_bool {
    \tlp_use:N \l_niib_key_before_tlp
  }{
    \bool_if:NTF \l_niib_after_cite_bool {
      \tlp_use:N \l_niib_key_after_tlp
    }{
      \tlp_use:N \l_niib_key_mixed_tlp
    }
  }
}
\def_new:Npn \niib_presort: {
  \bool_if:NTF \l_niib_before_cite_bool {
    \tlp_use:N \l_niib_presort_before_tlp
  }{
    \bool_if:NTF \l_niib_after_cite_bool {
      \tlp_use:N \l_niib_presort_after_tlp
    }{
      \tlp_use:N \l_niib_presort_mixed_tlp
    }
  }
}
\def_new:Npn \niib_convert_endnotes_false: {
  \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:Npn \niib_convert_endnotes_true: {
  \niib_debug:n {Converting~endnotes~to~bibnotes}
  \let:NN \endnote \bibnote
  \let:NN \endnotemark \bibnotemark
  \let:NN \endnotetext \bibnotetext
}
\def_new:Npn \niib_convert_footnotes_false: {
  \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:Npn \niib_convert_footnotes_true: {
  \niib_debug:n {Converting~footnotes~to~bibnotes}
  \let:NN \footnote \bibnote
  \let:NN \footnotemark \bibnotemark
  \let:NN \footnotetext \bibnotetext
}
\def:Npn \thanks #1 {
  \niib_saved_footnotemark:
  \protected@xdef \@thanks {
    \@thanks
    \protect \niib_saved_footnotetext: [\the\c@footnote] {#1}
  }
}
\tlp_new:N \l_niib_keyname_tlp
\AtBeginDocument{
  \err_info:nn {
    (xnotes2bib) \text_put_sp: Beginning~\token_to_str:N
    \AtBeginDocument \text_put_sp: tasks
  } {}
  \@ifpackageloaded {biblatex} {
    \tlp_set:Nn \l_niib_keyname_tlp {keysort}
    \gappto \blx@bibfiles {,\niib_prefix\c_job_name_tlp}
    \let_new:NN \printbibnotes \printbibliography
    \def_new:Npn \niib_after_cite:n #1 {
      \AtNextCite {\@fileswfalse}
      \niib_cite:n {#1}
    }
  }{
    \tlp_gset:Nn \l_niib_keyname_tlp {key}
    \let_new:NN \niib_biliography:n \bibliography
    \def:Npn \bibliography #1 {
      \int_compare:nNnTF {\g_niib_note_int} = {\c_zero} {
        \niib_biliography:n {#1}
      }{
        \niib_biliography:n {
          #1, \l_niib_prefix_tlp \c_job_name_tlp
        }
      }
    }
    \NewDocumentCommand {\printbibnotes} {} {
      \niib_biliography:n {\l_niib_prefix_tlp\c_job_name_tlp}
    }
    \newif \ifniib@filesw
    \@ifpackageloaded {cite}{
      \def_new:Npn \niib_after_cite:n #1 {
         \let:NN \ifniib@filesw \if@filesw
         \@fileswfalse
         \def:Npn \niib_auxhandle: {
           \let:NN \if@filesw \ifniib@filesw
           \let:NN \niib_auxhandle: \scan_stop:
         }
         \niib_cite:n {#1}
      }
      \tlp_gput_right:Nn \g_niib_document_tlp {
        \cs_if_exist:NTF \@restore@auxhandle{
          \tlp_put_right:Nn \@restore@auxhandle {\niib_auxhandle:}
        }{
          \def_new:Npn \@restore@auxhandle {\niib_auxhandle:}
        }
      }
      \let_new:NN \niib_auxhandle: \scan_stop:
    }{
      \def_new:Npn \niib_after_cite:n #1 {
         \let:NN \ifniib@filesw \if@filesw
         \@fileswfalse
         \niib_cite:n {#1}
         \let:NN \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:
  \keys_manage_quick:n {
    /niib/.cd:,
    convert~endnotes/true/.code:n   = {\niib_convert_endnotes_true:},
    convert~endnotes/false/.code:n  = {\niib_convert_endnotes_false:},
    convert~footnotes/true/.code:n  = {\niib_convert_footnotes_true:},
    convert~footnotes/false/.code:n = {\niib_convert_footnotes_false:},
  }
  \err_info:nn {
    (xnotes2bib) \text_put_sp: Finished~\token_to_str:N
    \AtBeginDocument \text_put_sp: tasks
  } {}
}
\def_new:Npn \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:
    }
  }{
    \clist_if_empty:cTF {g_niib_#1cite_clist}{
      \niib_rerun_log:
    }{
      \bool_set_false:N \l_tmpa_bool
      \def:Npn \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:Npn \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:
      }
    }
  }
}
\let_new:NN \niib_rerun_test:n \use_none_i:n
\def_new:Npn \niib_rerun_log: {
  \err_info:nn {
    (xnotes2bib) \text_put_sp: To~get~notes~in~the~correct~order,~
    please \err_newline: rerun~LaTeX,~(re)run~BibTeX~on~the~file~
    \c_job_name_tlp.aux \err_newline: and~rerun~LaTeX~again~afterwards
  } {(xnotes2bib) \text_put_sp:}
  \io_put_log:x {
    REQ:3:latex:REQ \iow_newline:
    REQ:2:bibtex:REQ \iow_newline:
    REQ:1:latex:REQ \iow_newline:
  }
  \let \niib_rerun_log: \scan_stop:
}
\AtEndDocument{
  \niib_rerun_check:n {before}
  \niib_rerun_check:n {after}
  \flushnotestack
  \iow_expanded:Nn \@auxout {
    \token_to_str:N \niibtotalnotes {\int_to_arabic:n \g_niib_note_int}
  }
  \num_compare:nNnF {\g_niib_totalnotes_int} = {\g_niib_note_int} {
    \niib_rerun_log:
  }
  \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'.
