%%
%% This is file `morewrites.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% morewrites.dtx  (with options: `package')
%% 
%% Communicate any suggestions for changing this package
%% to Bruno Le Floch (blflatex@gmail.com).
%% 
%% File: morewrites.dtx Copyright (C) 2011-2018 Bruno Le Floch
%%
%% 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 has the LPPL maintenance status 'maintained'
%% and the current maintainer is Bruno Le Floch.
%% -----------------------------------------------------------------------
\RequirePackage {expl3} [2018/02/21]
\RequirePackage {primargs} [2018/12/29]
\ProvidesExplPackage
  {morewrites} {2018/12/29} {} {Always room for a new write}
\sys_if_engine_luatex:T
  {
    \cs_new_protected:Npn \morewritessetup #1 { }
    \msg_new:nnn { morewrites } { luatex }
      { The~morewrites~package~is~unnecessary~in~LuaTeX. }
    \msg_warning:nn { morewrites } { luatex }
    \tex_endinput:D
  }%
\cs_new_eq:NN \__morewrites_tex_immediate:w \tex_immediate:D
\cs_new_eq:NN \__morewrites_tex_openout:w   \tex_openout:D
\cs_new_eq:NN \__morewrites_tex_write:w     \tex_write:D
\cs_new_eq:NN \__morewrites_tex_closeout:w  \tex_closeout:D
\exp_args:NNf \cs_new_protected:Npn \__morewrites_tex_newwrite:N
  { \exp_args:NNc \exp_after:wN \exp_stop_f: { newwrite } }
\cs_generate_variant:Nn \prop_gpop:NnNT { NV }
\cs_generate_variant:Nn \prop_gput:Nnn { NVx }
\cs_generate_variant:Nn \tl_gput_right:Nn { Nv }
\tl_new:N \l__morewrites_internal_tl
\cs_new_eq:NN \__morewrites_tmp:w ?
\int_new:N \g__morewrites_later_int
\seq_new:N \g__morewrites_write_seq
\prop_new:N \g__morewrites_write_prop
\prop_new:N \g__morewrites_write_file_prop
\tl_new:N \l__morewrites_code_tl
\int_new:N \l__morewrites_user_int
\tl_new:N \l__morewrites_tstr_tl
\cs_new_eq:NN \l__morewrites_tstr_token ?
\cs_new_eq:NN \s__morewrites \scan_stop:
\newwrite \g__morewrites_iow
\newread \g__morewrites_ior
\tl_new:N \g__morewrites_tmp_file_tl
\bool_new:N \g__morewrites_tmp_file_bool
\bool_gset_false:N \g__morewrites_tmp_file_bool
\int_new:N \g__morewrites_group_level_int
\box_new:N \g__morewrites_shipout_box
\cs_new_protected:Npn \__morewrites_set_file:n #1
  {
    \bool_gset_false:N \g__morewrites_tmp_file_bool
    \tl_gset:Nn \g__morewrites_tmp_file_tl {#1}
  }
\cs_new_protected:Npn \__morewrites_empty_file:n #1
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w
      \g__morewrites_iow = #1 \scan_stop:
    \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w
      \g__morewrites_iow
  }
\prg_new_conditional:Npnn \__morewrites_if_file_trivial:n #1 { TF }
  {
    \group_begin:
      \tex_openin:D \g__morewrites_ior = #1 \scan_stop:
      \if_eof:w \g__morewrites_ior
        \cs_gset_eq:NN \__morewrites_tmp:w \prg_return_true:
      \else:
        \int_set:Nn \tex_endlinechar:D { -1 }
        \tex_readline:D \g__morewrites_ior to \l__morewrites_internal_tl
        \if_eof:w \g__morewrites_ior
          \cs_gset_eq:NN \__morewrites_tmp:w \prg_return_true:
        \else:
          \cs_gset_eq:NN \__morewrites_tmp:w \prg_return_false:
        \fi:
      \fi:
      \tex_closein:D \g__morewrites_ior
    \group_end:
    \__morewrites_tmp:w
  }
\cs_new_protected:Npn \__morewrites_chk_file:
  {
    \__morewrites_if_file_trivial:nTF { \g__morewrites_tmp_file_tl }
      { \bool_gset_true:N \g__morewrites_tmp_file_bool }
      {
        \msg_warning:nnxx { morewrites } { file-exists }
          { \g__morewrites_tmp_file_tl }
          { \g__morewrites_tmp_file_tl .mw }
        \tl_gput_right:Nn \g__morewrites_tmp_file_tl { .mw }
        \__morewrites_chk_file:
      }
  }
\msg_new:nnnn { morewrites } { file-exists }
  { File~'#1'~exists,~using~'#2'~instead. }
  {
    The~file~`#1'~exists~and~was~not~created~by~this~version~of~the~
    `morewrites'~package.~Please~move~or~delete~that~file,~or~provide~
    another~file~name~by~adding
    \\ \\
    \iow_indent:n { \iow_char:N\\morewritessetup~{~file~=~other-name~} }
    \\ \\
    to~your~source~file.~In~the~meantime,~the~file~`#2'~will~be~used.
  }
\cs_new_protected:Npn \__morewrites_equals_file:N #1
  {
    \group_begin:
      \tex_aftergroup:D \primargs_get_file_name:N
      \tex_aftergroup:D #1
      \primargs_remove_equals:N \group_end:
  }
\cs_new_protected:Npn \__morewrites_get_user:n #1
  {
    \tl_set:Nn \l__morewrites_code_tl {#1}
    \tex_afterassignment:D \l__morewrites_code_tl
    \l__morewrites_user_int =
  }
\cs_new_protected:Npn \__morewrites_user_to_tstr:NTF #1
  {
    \tl_set:NV \l__morewrites_tstr_tl \l__morewrites_user_int
    \int_compare:nNnTF { \l__morewrites_user_int } < { 19 }
      { \use_i:nn }
      { \prop_get:NVNTF #1 \l__morewrites_user_int \l__morewrites_tstr_tl }
  }
\int_new:N \l__morewrites_collect_next_int
\cs_new_protected:Npn \__morewrites_collect:x #1
  {
    \tl_set:Nx \l__morewrites_internal_tl {#1}
    \__morewrites_collect_aux:cf { l__morewrites_0_tl } { 1 }
  }
\cs_new_protected:Npn \__morewrites_collect_aux:Nn #1#2
  {
    \int_compare:nNnT {#2} > \l__morewrites_collect_next_int
      {
        \tl_clear_new:N #1
        \int_set:Nn \l__morewrites_collect_next_int {#2}
      }
    \tl_if_empty:NTF #1
      { \tl_set_eq:NN #1 \l__morewrites_internal_tl }
      {
        \tl_put_left:No \l__morewrites_internal_tl {#1}
        \tl_clear:N #1
        \__morewrites_collect_aux:cf { l__morewrites_#2_tl }
          { \int_eval:n { #2 + 1 } }
      }
  }
\cs_generate_variant:Nn \__morewrites_collect_aux:Nn { cf }
\cs_new_protected:Npn \__morewrites_collect_gput_right:N #1
  {
    \int_compare:nNnF \l__morewrites_collect_next_int = 0
      {
        \int_decr:N \l__morewrites_collect_next_int
        \tl_gput_right:Nv #1
          {
            l__morewrites_
            \int_use:N \l__morewrites_collect_next_int
            _tl
          }
        \__morewrites_collect_gput_right:N #1
      }
  }
\cs_generate_variant:Nn \__morewrites_collect_gput_right:N { c }
\cs_new:Npn \__morewrites_user_tl_name:n #1
  { g__morewrites_iow_ \int_eval:n {#1} _tl }
\cs_new_protected:Npn \__morewrites_immediate:w
  { \primargs_read_x_token:N \__morewrites_immediate_auxii: }
\cs_new_protected:Npn \__morewrites_immediate_auxii:
  {
    \token_if_eq_meaning:NNTF \g_primargs_token \s__morewrites
      { \__morewrites_immediate_auxiii:N }
      { \__morewrites_tex_immediate:w }
  }
\cs_new_protected:Npn \__morewrites_immediate_auxiii:N #1
  { \str_if_eq:nnTF { #1 } { \s__morewrites } { \use_iii:nnn } { #1 } }
\cs_new_protected:Npn \__morewrites_closeout:w
  {
    \s__morewrites
    \use_i:nn
      { \__morewrites_get_user:n { \__morewrites_closeout_later: } }
      { \__morewrites_get_user:n { \__morewrites_closeout_now: } }
  }
\cs_new_protected:Npn \__morewrites_closeout_now:
  {
    \__morewrites_user_to_tstr:NTF \g__morewrites_write_prop
      {
        \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w \l__morewrites_tstr_tl \exp_stop_f:
        \int_compare:nNnF { \l__morewrites_tstr_tl } = { \l__morewrites_user_int }
          {
            \prop_gremove:NV \g__morewrites_write_prop \l__morewrites_user_int
            \seq_gput_left:NV \g__morewrites_write_seq \l__morewrites_tstr_tl
          }
      }
      {
        \prop_gpop:NVNT \g__morewrites_write_file_prop \l__morewrites_user_int \l__morewrites_internal_tl
          { \__morewrites_closeout_now:nn { \l__morewrites_user_int } { \l__morewrites_internal_tl } }
      }
  }
\cs_new_protected:Npn \__morewrites_closeout_now:nn #1#2
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w \g__morewrites_iow = #2 \scan_stop:
    \group_begin:
      \int_set:Nn \tex_newlinechar:D { -1 }
      \tl_use:c { \__morewrites_user_tl_name:n {#1} }
      \tl_gclear:c { \__morewrites_user_tl_name:n {#1} }
    \group_end:
    \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w \g__morewrites_iow
  }
\cs_new_protected:Npn \__morewrites_openout:w
  {
    \s__morewrites
    \use_i:nn
      { \__morewrites_get_user:n { \__morewrites_openout_later:w } }
      { \__morewrites_get_user:n { \__morewrites_equals_file:N \__morewrites_openout_now:n } }
  }
\cs_new_protected:Npn \__morewrites_openout_now:n #1
  {
    \__morewrites_closeout_now:
    \int_compare:nNnTF { \l__morewrites_user_int } < { 19 }
      {
        \__morewrites_tex_immediate:w \__morewrites_tex_openout:w \l__morewrites_user_int
          = \tl_to_str:n {#1} \scan_stop:
      }
      {
        \seq_gpop:NNTF \g__morewrites_write_seq \l__morewrites_tstr_tl
          {
            \prop_gput:NVV \g__morewrites_write_prop \l__morewrites_user_int \l__morewrites_tstr_tl
            \__morewrites_tex_immediate:w \__morewrites_tex_openout:w \l__morewrites_tstr_tl \exp_stop_f:
              = \tl_to_str:n {#1} \scan_stop:
          }
          {
            \__morewrites_empty_file:n {#1}
            \prop_gput:NVx \g__morewrites_write_file_prop \l__morewrites_user_int
              { \tl_to_str:n {#1} }
            \tl_gclear_new:c { \__morewrites_user_tl_name:n { \l__morewrites_user_int } }
          }
      }
  }
\cs_new_protected:Npn \__morewrites_write:w
  {
    \s__morewrites
    \use_i:nn
      { \__morewrites_get_user:n { \__morewrites_write_later:w } }
      { \__morewrites_get_user:n { \__morewrites_write_now:w } }
  }
\cs_new_protected:Npn \__morewrites_write_now:w
  {
    \__morewrites_user_to_tstr:NTF \g__morewrites_write_prop
      { \__morewrites_tex_immediate:w \__morewrites_tex_write:w \l__morewrites_tstr_tl \exp_stop_f: }
      { \primargs_get_general_text:N \__morewrites_write_now:n }
  }
\cs_new_protected:Npn \__morewrites_write_now:n
  {
    \prop_get:NVNTF \g__morewrites_write_file_prop \l__morewrites_user_int \l__morewrites_internal_tl
      { \__morewrites_write_now_open:n }
      { \__morewrites_tex_immediate:w \__morewrites_tex_write:w 16 }
  }
\cs_new_protected:Npn \__morewrites_write_now_open:n #1
  {
    \bool_if:NF \g__morewrites_tmp_file_bool { \__morewrites_chk_file: }
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w
      \g__morewrites_iow = \g__morewrites_tmp_file_tl \scan_stop:
    \__morewrites_tex_immediate:w \__morewrites_tex_write:w
      \g__morewrites_iow {#1}
    \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w
      \g__morewrites_iow
    \group_begin:
      \int_set:Nn \tex_endlinechar:D { -1 }
      \tex_openin:D \g__morewrites_ior = \g__morewrites_tmp_file_tl \scan_stop:
      \__morewrites_write_now_loop:
      \tex_closein:D \g__morewrites_ior
      \__morewrites_collect_gput_right:c
        { \__morewrites_user_tl_name:n { \l__morewrites_user_int } }
    \group_end:
    \__morewrites_empty_file:n { \g__morewrites_tmp_file_tl }
  }
\cs_new_protected:Npn \__morewrites_write_now_loop:
  {
    \tex_readline:D \g__morewrites_ior to \l__morewrites_internal_tl
    \ior_if_eof:NF \g__morewrites_ior
      {
        \__morewrites_collect:x
          {
            \__morewrites_tex_immediate:w \__morewrites_tex_write:w
              \g__morewrites_iow { \l__morewrites_internal_tl }
          }
        \__morewrites_write_now_loop:
      }
  }
\cs_new_protected:Npn \__morewrites_later:n #1
  {
    \int_gincr:N \g__morewrites_later_int
    \tl_const:cx
      {
        c__morewrites_later_
        \int_use:N \g__morewrites_later_int
        _tl
      }
      {
        \int_set:Nn \exp_not:N \l__morewrites_user_int
          { \exp_not:V \l__morewrites_user_int }
        \exp_not:n {#1}
      }
    \exp_args:NNx \__morewrites_tex_write:w \g__morewrites_iow
      { `( \int_use:N \g__morewrites_later_int ) }
  }
\cs_new_protected:Npn \__morewrites_later_do:n #1
  { \tl_use:c { c__morewrites_later_ \int_eval:n {#1} _tl } }
\cs_new_protected:Npn \__morewrites_closeout_later:
  {
    \int_compare:nNnTF \l__morewrites_user_int < { 19 }
      { \__morewrites_tex_closeout:w \l__morewrites_user_int }
      { \__morewrites_later:n { \__morewrites_closeout_now: } }
  }
\cs_new_protected:Npn \__morewrites_openout_later:w
  {
    \int_compare:nNnTF \l__morewrites_user_int < { 19 }
      { \__morewrites_tex_openout:w \l__morewrites_user_int }
      { \__morewrites_equals_file:N \__morewrites_openout_later:n }
  }
\cs_new_protected:Npn \__morewrites_openout_later:n #1
  { \__morewrites_later:n { \__morewrites_openout_now:n {#1} } }
\cs_new_protected:Npn \__morewrites_write_later:w
  {
    \int_compare:nNnTF \l__morewrites_user_int < { 19 }
      { \__morewrites_tex_write:w \l__morewrites_user_int }
      { \primargs_get_general_text:N \__morewrites_write_later:n }
  }
\cs_new_protected:Npn \__morewrites_write_later:n #1
  { \__morewrites_later:n { \__morewrites_write_later_aux:n {#1} } }
\cs_new_protected:Npn \__morewrites_write_later_aux:n
  {
    \__morewrites_user_to_tstr:NTF \g__morewrites_write_prop
      { \__morewrites_tex_immediate:w \__morewrites_tex_write:w \l__morewrites_tstr_tl \exp_stop_f: }
      { \__morewrites_write_now:n }
  }
\cs_new_protected:Npn \__morewrites_before_shipout:
  {
    \bool_if:NF \g__morewrites_tmp_file_bool { \__morewrites_chk_file: }
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w
      \g__morewrites_iow = \g__morewrites_tmp_file_tl \scan_stop:
  }
\cs_new_protected:Npn \__morewrites_after_shipout:
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w
      \g__morewrites_iow
    \group_begin:
      \int_set_eq:NN \tex_endlinechar:D \tex_newlinechar:D
      \char_set_catcode_other:n { \tex_endlinechar:D }
      \tl_map_inline:nn { `(0123456789) }
        { \char_set_catcode_other:n {`##1} }
      \tex_everyeof:D { `() \exp_not:N }
      \tl_set:Nx \l__morewrites_internal_tl
        {
          \exp_after:wN \__morewrites_after_shipout_loop:ww
          \tex_input:D \g__morewrites_tmp_file_tl \c_space_tl
        }
      \__morewrites_empty_file:n { \g__morewrites_tmp_file_tl }
      \exp_args:NNo
    \group_end:
    \tl_map_function:nN { \l__morewrites_internal_tl } \__morewrites_later_do:n
  }
\cs_new:Npn \__morewrites_after_shipout_loop:ww #1 `( #2 )
  {
    \tl_if_empty:nF {#2}
      {
        {#2}
        \__morewrites_after_shipout_loop:ww
      }
  }
\cs_new_protected:Npn \__morewrites_shipout:w
  {
    \int_gset_eq:NN \g__morewrites_group_level_int \tex_currentgrouplevel:D
    \tex_afterassignment:D \__morewrites_shipout_i:
    \tex_global:D \tex_setbox:D \g__morewrites_shipout_box
  }
\cs_new_protected:Npn \__morewrites_shipout_i:
  {
    \int_compare:nNnTF { \g__morewrites_group_level_int }
                     = { \tex_currentgrouplevel:D }
      { \__morewrites_shipout_ii: }
      { \tex_aftergroup:D \__morewrites_shipout_ii: }
  }
\cs_new_protected:Npn \__morewrites_shipout_ii:
  {
    \__morewrites_before_shipout:
    \__morewrites_tex_shipout:w \tex_box:D \g__morewrites_shipout_box
    \__morewrites_after_shipout:
  }
\cs_gset_protected:Npn \__morewrites_tmp:w #1
  {
    \cs_if_exist:NF \__morewrites_tex_shipout:w
      { \cs_new_eq:NN \__morewrites_tex_shipout:w #1 }
    \cs_gset_eq:NN #1 \__morewrites_shipout:w
  }
\tl_map_inline:nn
  {
    \xyrealshipout@
    \org@shipout
    \PDFSYNCship@ut@ld
    \CROP@shipout
    \@soORI
    \tex_shipout:D
    \zwpl@Hship
    \o@shipout@TP
    \LL@shipout
    \Shipout
    \GXTorg@shipout
    \AtBegShi@OrgShipout
    \AtBeginShipoutOriginalShipout
    \minidocument@orig@shipout
    \shipout
  }
  {
    \str_if_eq:eeT
      { \cs_meaning:N #1 }
      { \token_to_str:N \shipout }
      { \__morewrites_tmp:w #1 }
  }
\cs_if_exist:NF \__morewrites_tex_shipout:w
  {
    \cs_new_eq:NN \__morewrites_tex_shipout:w \shipout
    \cs_gset_eq:NN \shipout \__morewrites_shipout:w
  }
\cs_new_protected:Npn \__morewrites_close_all:
  {
    \prop_map_inline:Nn \g__morewrites_write_prop
      { \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w ##2 \scan_stop: }
    \prop_gclear:N \g__morewrites_write_prop
    \prop_map_function:NN \g__morewrites_write_file_prop
      \__morewrites_closeout_now:nn
    \prop_gclear:N \g__morewrites_write_file_prop
  }
\cs_new_protected:Npn \__morewrites_close_all_at_end:nw #1#2 \@@end
  {
    \int_compare:nNnTF {#1} > 0
      { #2 \__morewrites_close_all_at_end:nw { #1 - 1 } }
      { \__morewrites_close_all: #2 }
    \@@end
  }
\AtEndDocument { \__morewrites_close_all_at_end:nw { 5 } }
\int_new:N \g__morewrites_alloc_write_int
\int_gset:Nn \g__morewrites_alloc_write_int { 18 }
\cs_new_protected:Npn \__morewrites_newwrite:N #1
  {
    \int_gincr:N \g__morewrites_alloc_write_int
    \int_set_eq:NN \allocationnumber \g__morewrites_alloc_write_int
    \cs_undefine:N #1
    \int_const:Nn #1 { \allocationnumber }
    \wlog
      {
        \token_to_str:N #1
        = \token_to_str:N \write \int_use:N \allocationnumber
      }
  }
\cs_new_protected:Npn \__morewrites_allocate:n #1
  {
    \prg_replicate:nn
      {
        \int_max:nn { 0 }
          {
            (#1) - \seq_count:N \g__morewrites_write_seq
            - \prop_count:N \g__morewrites_write_prop
          }
      }
      {
        \__morewrites_tex_newwrite:N \l__morewrites_tstr_token
        \seq_gput_right:NV \g__morewrites_write_seq \l__morewrites_tstr_token
      }
  }
\cs_new_protected:Npn \morewritessetup #1
  { \keys_set:nn { __morewrites } {#1} }
\keys_define:nn { __morewrites }
  {
    allocate .code:n = \__morewrites_allocate:n {#1} ,
    file .code:n = \__morewrites_set_file:n {#1} ,
    file .initial:n = \c_sys_jobname_str .mw
  }
\cs_gset_eq:NN \immediate \__morewrites_immediate:w
\cs_gset_eq:NN \openout   \__morewrites_openout:w
\cs_gset_eq:NN \write     \__morewrites_write:w
\cs_gset_eq:NN \closeout  \__morewrites_closeout:w
\cs_gset_eq:NN \newwrite  \__morewrites_newwrite:N
%% 
%%
%% End of file `morewrites.sty'.
