%%
%% This is file `notes2bib.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% notes2bib.dtx  (with options: `package')
%% ---------------------------------------------------------------
%% notes2bib --- 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{xparse}
\ProvidesExplPackage
  {notes2bib} {2010/03/30} {2.0c}
  {Integrating notes into the bibliography}
\RequirePackage{l3keys2e}
\tl_new:N \c_niib_file_message_tl
\tl_set:Nn \c_niib_file_message_tl {
  This~is~an~auxiliary~file~used~by~the~'notes2bib'~package.
    \iow_newline:
  This~file~may~safely~be~deleted.
    \iow_newline:
  It~will~be~recreated~as~required.
    \iow_newline:
}
\clist_new:N \g_niib_after_clist
\clist_new:N \g_niib_before_clist
\clist_new:N \g_niib_all_after_clist
\clist_new:N \g_niib_all_before_clist
\int_new:N \g_niib_note_int
\tl_new:N \l_niib_presort_tl
\tl_new:N \l_niib_sortkey_tl
\clist_new:N \g_niib_previous_after_clist
\clist_new:N \g_niib_previous_before_clist
\int_new:N \g_niib_previous_notes_int
\tl_new:N \g_niib_notes_tl
\tl_new:N \l_niib_sortkey_field_tl
\int_new:N \g_niib_total_notes_int
\cs_new_nopar:Npn \niib_cite:w { }
\keys_define:nn { notes2bib } {
  cite-function     .code:n      =
    { \AtBeginDocument { \cs_set_eq:NN \niib_cite:w #1 } }     ,
  file-name         .tl_gset_x:N = \g_niib_filename_tl         ,
  convert-endnotes  .choice:                                   ,
  convert-endnotes / false .code:n =
    { \AtBeginDocument { \niib_from_bibnote:n { endnote } } }  ,
  convert-endnotes / true .code:n  =
    { \AtBeginDocument { \niib_to_bibnote:n { endnote } } }    ,
  convert-footnotes .choice:                                   ,
  convert-footnotes / false .code:n =
    { \AtBeginDocument { \niib_from_bibnote:n { footnote } } } ,
  convert-footnotes / true .code:n =
    { \AtBeginDocument { \niib_to_bibnote:n { footnote } } }   ,
  keyword-entry     .tl_set:N    = \l_niib_keyword_tl          ,
  note-field        .tl_set:N    = \l_niib_note_field_tl       ,
  note-name         .tl_set:N    = \l_niib_note_name_tl        ,
  placement         .choice:                                   ,
  placement / after .code:n      =
    {
      \cs_set_eq:NN \niib_mark_note:n \niib_mark_note_after:n
      \tl_set_eq:NN \l_niib_presort_tl \l_niib_presort_after_tl
      \tl_set_eq:NN \l_niib_sortkey_tl \l_niib_sortkey_after_tl
    },
  placement / before .code:n     =
    {
      \cs_set_eq:NN \niib_mark_note:n \niib_mark_note_before:n
      \tl_set_eq:NN \l_niib_presort_tl \l_niib_presort_before_tl
      \tl_set_eq:NN \l_niib_sortkey_tl \l_niib_sortkey_before_tl
    },
  placement / mixed .code:n      =
    {
      \cs_set_eq:NN \niib_mark_note:n \niib_mark_note_mixed:n
      \tl_set_eq:NN \l_niib_presort_tl \l_niib_presort_mixed_tl
      \tl_set_eq:NN \l_niib_sortkey_tl \l_niib_sortkey_mixed_tl
    },
  presort-after     .tl_set:N    = \l_niib_presort_after_tl    ,
  presort-before    .tl_set:N    = \l_niib_presort_before_tl   ,
  presort-mixed     .tl_set:N    = \l_niib_presort_mixed_tl    ,
  record-type       .tl_set:N    = \l_niib_record_type_tl      ,
  sort-key-after    .tl_set:N    = \l_niib_sortkey_before_tl   ,
  sort-key-before   .tl_set:N    = \l_niib_sortkey_after_tl    ,
  sort-key-mixed    .tl_set:N    = \l_niib_sortkey_mixed_tl    ,
  use-sort-key      .bool_set:N  = \l_niib_write_sortkey_bool  ,
}
\keys_set:nn { notes2bib } {
  cite-function   = \cite              ,
  file-name       = notes2bib-\jobname ,
  keyword-entry   = note               ,
  note-field      = note               ,
  note-name       = Note               ,
  presort-after   = mn                 ,
  presort-before  = ml                 ,
  presort-mixed   = mm                 ,
  record-type     = misc               ,
  sort-key-after  = zzz                ,
  sort-key-before = aaa                ,
  use-sort-key    = true
}
\AtBeginDocument {
  \keys_define:nn { notes2bib }
    {
      cite-function     .code:n      =
        { \cs_set_eq:NN \niib_cite:w #1 }                              ,
      file-name .code:n =
        { \msg_info:nnn { notes2bib } { preamble-only } { file-name } },
      convert-endnotes / false .code:n =
        { \niib_from_bibnote:n { endnote } }                           ,
      convert-endnotes / true .code:n  =
        { \niib_to_bibnote:n { endnote } }                             ,
      convert-footnotes / false .code:n =
        { \niib_from_bibnote:n { footnote } }                          ,
      convert-footnotes / true .code:n =
        { \niib_to_bibnote:n { footnote } }                            ,
    }
}
\msg_new:nnn { notes2bib } { preamble-only }
  {The option `#1' can only be used in the preamble.}
\keys_define:nn { notes2bib } {
  cite        .meta:x = { cite-function     = \exp_not:c {#1} } ,
  debug       .code:n = { }                                     ,
  endnotes    .meta:n = { convert-footnotes = #1 }              ,
  etex        .code:n = { }                                     ,
  field       .meta:n = { note-field        = #1 }              ,
  footnotes   .meta:n = { convert-footnotes = #1 }              ,
  head        .meta:n = { placement         = before }          ,
  keyhead     .meta:n = { sort-key-before   = #1 }              ,
  keytail     .meta:n = { sort-key-after    = #1 }              ,
  keynone     .meta:n = { sort-key-mixed    = #1 }              ,
  keyword     .meta:n = { keyword-entry     = #1 }              ,
  log         .meta:n = { }                                     ,
  name        .meta:n = { note-name         = #1 }              ,
  prefix      .meta:n = { file-name         = #1 \jobname }     ,
  presorthead .meta:n = { presort-before    = #1 }              ,
  presorttail .meta:n = { presort-after     = #1 }              ,
  presortnone .meta:n = { presort-mixed     = #1 }              ,
  record      .meta:n = { record-type       = #1 }              ,
  sort        .choice:                                          ,
  sort / head .meta:n = { placement         = before }          ,
  sort / none .meta:n = { placement         = after }           ,
  sort / tail .meta:n = { placement         = mixed }           ,
  tail        .meta:n = { placement         = after }           ,
  writekey    .meta:n = { use-sort-key      = #1 }
}
\cs_new_nopar:Npn \niib_note_name: {
  \tl_use:N \l_niib_note_name_tl
  \intexpr_compare:nT { \g_niib_previous_notes_int > \c_nine }
    { \intexpr_compare:nT { \g_niib_note_int < \c_ten } { 0 } }
  \int_to_arabic:n { \g_niib_note_int }
}
\cs_new_nopar:Npn \niib_filesw: { }
\cs_new:Npn \niib_mark_note:n #1 { }
\cs_generate_variant:Nn \niib_mark_note:n { x }
\cs_new_protected:Npn \niib_mark_note_after:n #1 {
  \int_gincr:N \g_niib_total_notes_int
  \clist_gput_right:Nx \g_niib_after_clist {#1}
  \niib_mark_note_after_aux:n {#1}
}
\cs_new:Npn \niib_mark_note_after_aux:n #1 { }
\cs_new_protected:Npn \niib_mark_note_before:n #1 {
  \int_gincr:N \g_niib_total_notes_int
  \clist_gput_right:Nx \g_niib_before_clist {#1}
  \niib_cite:w {#1}
}
\cs_new_protected:Npn \niib_mark_note_mixed:n #1 {
  \int_gincr:N \g_niib_total_notes_int
  \niib_cite:w {#1}
}
\cs_new:Npn \niib_write_field:nn #1#2 {
  \c_space_tl \c_space_tl #1 \c_space_tl = \c_space_tl {#2} ,
  \iow_newline:
}
\cs_generate_variant:Nn \niib_write_field:nn { V }
\cs_new_protected_nopar:Npn \niib_write_note:nn {
  \if@filesw
    \exp_after:wN \niib_write_note_aux:nn
  \else:
    \exp_after:wN \use_none:nn
  \fi:
}
\cs_new_protected:Npn \niib_write_note_aux:nn #1#2 {
  \tl_gput_right:Nx \g_niib_notes_tl
    {
      @ \l_niib_record_type_tl
        {
          #1 , \iow_newline:
          \niib_write_field:Vn \l_niib_note_field_tl { \exp_not:n {#2} }
          \bool_if:NT \l_niib_write_sortkey_bool
            {
              \niib_write_field:Vn \l_niib_sortkey_field_tl
                { \l_niib_sortkey_tl #1 }
            }
          \niib_write_field:nn { keywords } { \l_niib_keyword_tl }
          \niib_write_field:nn { presort } { \l_niib_presort_tl }
        }
      \iow_newline:
      \iow_newline:
    }
}
\cs_generate_variant:Nn \niib_write_note:nn { x }
\cs_new_protected_nopar:Npn \niib_record_notes: {
  \if@filesw
    \exp_after:wN \niib_flush_notes_aux:
  \fi
}
\cs_new_protected_nopar:Npn \niib_flush_notes_aux: {
  \clist_if_empty:NF \g_niib_before_clist
    {
      \iow_now:Nx \@auxout
        { \NotesBeforeCitations { \exp_not:V \g_niib_before_clist } }
      \clist_gput_right:NV \g_niib_all_before_clist \g_niib_before_clist
      \clist_gclear:N \g_niib_before_clist
    }
  \clist_if_empty:NF \g_niib_after_clist
    {
      \iow_now:Nx \@auxout
        { \NotesAfterCitations { \exp_not:V \g_niib_after_clist } }
      \exp_args:NV \nocite \g_niib_after_clist
      \clist_gput_right:NV \g_niib_all_after_clist \g_niib_after_clist
      \clist_gclear:N \g_niib_after_clist
    }
}
\cs_new_protected_nopar:Npn \niib_to_bibnote:n #1 {
  \cs_set_eq:cN {#1}        \bibnote
  \cs_set_eq:cN { #1 mark } \bibnotemark
  \cs_set_eq:cN { #1 text } \bibnotetext
}
\cs_new_protected_nopar:Npn \niib_from_bibnote:n #1 {
  \cs_set_eq:cc {#1}        { niib_ #1 :w }
  \cs_set_eq:cc { #1 mark } { niib_ #1 mark:w }
  \cs_set_eq:cc { #1 text } { niib_ #1 text:w }
}
\AtBeginDocument {
  \cs_set_eq:NN \niib_endnote:w     \endnote
  \cs_set_eq:NN \niib_endnotemark:w \endnotemark
  \cs_set_eq:NN \niib_endnotetext:w \endnotetext
  \cs_set_eq:NN \niib_footnote:w     \footnote
  \cs_set_eq:NN \niib_footnotemark:w \footnotemark
  \cs_set_eq:NN \niib_footnotetext:w \footnotetext
}
\AtBeginDocument {
  \@ifpackageloaded { biblatex }
    {
      \cs_new_protected_nopar:Npn \niib_print_notes:
        {
          \cs_set_nopar:Npx \niib_create_print_notes:
            {
              \printbibliography
                [ keyword = \exp_not:V \l_niib_keyword_tl ]
            }
            \niib_create_print_notes:
        }
    }
    {
      \cs_new_protected_nopar:Npn \niib_print_notes:
        { \exp_args:NV \niib_bibliography:n \g_niib_filename_tl }
    }
}
\cs_new_nopar:Npn \niib_create_print_notes: { }
\cs_new_nopar:Npn \niib_attach_bibliography: {
  \@ifpackageloaded { biblatex }
    { \exp_args:NV \bibliography \g_niib_filename_tl }
    {
      \cs_new_eq:NN \niib_bibliography:n \bibliography
      \RenewDocumentCommand \bibliography { m }
        {
          \intexpr_compare:nTF { \g_niib_total_notes_int = \c_zero }
            { \niib_bibliography:n {##1} }
            {
              \cs_set_nopar:Npx \niib_attach_bibliography:
                {
                  \exp_not:N \niib_bibliography:n
                    {
                      \exp_not:n {##1} , \exp_not:V \g_niib_filename_tl
                    }
                }
              \niib_attach_bibliography:
            }
        }
    }
}
\@ifpackageloaded { biblatex }
  {
    \exp_args:NV \bibliography \g_niib_filename_tl
    \cs_gundefine:N \niib_attach_bibliography:
  }
  { \AtBeginDocument { \niib_attach_bibliography: } }
\AtBeginDocument {
  \@ifpackageloaded { biblatex }
    { \tl_set:Nn \l_niib_sortkey_field_tl { sortkey } }
    { \tl_set:Nn \l_niib_sortkey_field_tl { key } }
}
\AtBeginDocument {
  \@ifpackageloaded { biblatex }
    {
      \cs_set:Npn \niib_mark_note_after_aux:n #1
        {
          \AtNextCite { \@fileswfalse }
          \niib_cite:w {#1}
        }
    }
    {
      \cs_set:Npn \niib_mark_note_after_aux:n #1
        {
          \cs_set_eq:NN \niib_filesw: \if@filesw
          \@fileswfalse
          \niib_cite:w {#1}
          \cs_set_eq:NN \if@filesw \niib_filesw:
        }
    }
}
\AtBeginDocument {
  \@ifpackageloaded { cite }
    {
      \cs_set:Npn \niib_mark_note_after_aux:n #1
        {
          \cs_set_eq:NN \niib_filesw: \if@filesw
          \@fileswfalse
          \cs_set_nopar:Npn \niib_aux_hook:
            {
              \cs_set_eq:NN \if@filesw \niib_filesw:
              \cs_set_nopar:Npn \niib_aux_hook: { }
            }
          \niib_cite:w {#1}
        }
      \cs_new_nopar:Npn \niib_aux_hook: { }
      \tl_gput_right:Nn \g_niib_document_hook_tl
        {
          \cs_if_exist:NF \@restore@auxhandle
            { \tl_new:N \@restore@auxhandle }
          \tl_put_right:Nn \@restore@auxhandle { \niib_aux_hook: }
        }
    }
    { }
}
\NewDocumentCommand \bibnote { o +m } {
  \IfNoValueTF {#1}
    {
      \int_gincr:N \g_niib_note_int
      \niib_bibnote:xn { \niib_note_name: } {#2}
    }
    { \niib_bibnote:nn {#1} {#2} }
}
\cs_new_protected:Npn \niib_bibnote:nn #1#2 {
  \niib_write_note:nn {#1} {#2}
  \niib_mark_note:n {#1}
}
\cs_generate_variant:Nn \niib_bibnote:nn { x }
\NewDocumentCommand \bibnotemark { o } {
  \IfNoValueTF {#1}
    {
      \int_gincr:N \g_niib_note_int
      \niib_mark_note:x { \niib_note_name: }
    }
    { \niib_mark_note:n {#1} }
}
\NewDocumentCommand \bibnotetext { o +m } {
  \IfNoValueTF {#1}
    { \niib_write_note:xn { \niib_note_name: } {#2} }
    { \niib_write_note:nn {#1} {#2} }
}
\NewDocumentCommand \recordnotes { } {
  \niib_record_notes:
}
\NewDocumentCommand \bibnotesetup { m } {
  \keys_set:nn { notes2bib } {#1}
}
\NewDocumentCommand \printbibnotes { } {
  \niib_print_notes:
}
\NewDocumentCommand \citenote { m } {
  \niib_mark_note:n {#1}
}
\cs_new_eq:NN \flushnotestack \recordnotes
\cs_new_eq:NN \niibsetup      \bibnotesetup
\cs_new_protected_nopar:Npn \NotesAfterCitations #1 {
  \clist_gput_right:Nn \g_niib_previous_after_clist {#1}
}
\cs_new_protected_nopar:Npn \NotesBeforeCitations #1 {
  \clist_gput_right:Nn \g_niib_previous_after_clist {#1}
  \tl_gput_right:Nn \g_niib_document_hook_tl { \nocite {#1} }
}
\cs_new_protected_nopar:Npn \TotalNotes #1 {
  \int_gset:Nn \g_niib_previous_notes_int {#1}
}
\tl_gput_right:Nn \document { \g_niib_document_hook_tl }
\tl_new:N \g_niib_document_hook_tl
\AtEndDocument { \niib_record_notes: }
\AtEndDocument {
  \clist_if_eq:NNTF \g_niib_all_before_clist
    \g_niib_previous_before_clist
    {
      \clist_if_eq:NNF \g_niib_all_after_clist
        \g_niib_previous_after_clist
        { \msg_info:nn { notes2bib } { rerun } }
    }
    { \msg_info:nn { notes2bib } { rerun } }
}
\msg_new:nnn { notes2bib } { rerun } {%
  To get notes in the correct order, please run:\\%
  1) LaTeX  \\%
  2) BibTeX \\%
  3) LaTeX
}
\AtEndDocument {
  \if@filesw
    \exp_after:wN \niib_write_note_file:
  \fi:
}
\cs_new_protected_nopar:Npn \niib_write_note_file: {
  \tl_if_empty:NF \g_niib_notes_tl {
    \iow_open:Nn \g_niib_file_stream { \g_niib_filename_tl .bib }
    \iow_now:Nx \g_niib_file_stream { \c_niib_file_message_tl }
    \iow_now:Nx \g_niib_file_stream
      { \exp_not:V \g_niib_notes_tl }
    \iow_close:N \g_niib_file_stream
  }
}
\AtEndDocument {
  \intexpr_compare:nT { \g_niib_note_int > \c_zero }
    {
      \iow_now:Nx \@auxout
        { \TotalNotes { \int_to_arabic:n { \g_niib_note_int } } }
    }
}
\cs_set:Npn \thanks #1 {
  \niib_footnotemark:w
  \protected@xdef \@thanks
    {
      \@thanks
      \protect \niib_footnotetext:w [ \the \c@footnote ] {#1}
    }
}
\keys_set:nn { notes2bib } { placement = mixed }
\ProcessKeysOptions { notes2bib }
%% 
%% Copyright (C) 2007-2010 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
%%           and the derived files notes2bib.ins,
%%                                 notes2bib.pdf and
%%                                 notes2bib.sty.
%% 
%%
%% End of file `notes2bib.sty'.
