%%
%% 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-2012 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
%%
%% -----------------------------------------------------------------------
\RequirePackage {expl3} [2012/08/14]
\RequirePackage {primargs} [2012/08/24]
\ProvidesExplPackage
  {morewrites} {2012/12/05} {0.2d} {Always room for a new write}
\cs_generate_variant:Nn \prop_gpop:NnNT { NV }
\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
\cs_new_eq:NN \__morewrites_tex_shipout:w   \tex_shipout:D
\int_new:N \g__morewrites_late_write_int
\prop_new:N \g__morewrites_iow_prop
\newwrite \g__morewrites_iow
\newread \g__morewrites_ior
\tl_new:N \g__morewrites_tmp_file_tl
\tl_gset:Nn \g__morewrites_tmp_file_tl { \jobname.mw }
\clist_new:N \g__morewrites_reserved_iow_clist
\int_step_inline:nnnn {0} {1} { \g__morewrites_iow - 1 }
  { \clist_gput_right:Nn \g__morewrites_reserved_iow_clist {#1} }
\clist_gput_right:Nn \g__morewrites_reserved_iow_clist {18}
\int_new:N \g__morewrites_stream_int
\cs_new_eq:NN \s__morewrites \scan_stop:
\tl_new:N  \l__morewrites_internal_tl
\cs_new_protected:Npn \__morewrites_equals_file_name:N #1
  {
    \group_begin:
      \tex_aftergroup:D #1
      \primargs_remove_equals:N \__morewrites_parse_file_name:
  }
\cs_new_protected_nopar:Npn \__morewrites_parse_file_name:
  { \primargs_get_file_name:N \group_end: }
\cs_new_protected_nopar:Npn \__morewrites_immediate:w
  { \primargs_read_x_token:N \__morewrites_immediate_ii: }
\cs_new_protected_nopar:Npn \__morewrites_immediate_ii:
  {
    \token_if_eq_meaning:NNTF \g_primargs_token \s__morewrites
      { \__morewrites_immediate_iii:N }
      { \__morewrites_tex_immediate:w }
  }
\cs_new_protected:Npn \__morewrites_immediate_iii:N #1
  {
    \tl_if_eq:nnTF { #1 } { \s__morewrites }
      { \__morewrites_immediate_iv:NN }
      { #1 }
  }
\cs_new_protected:Npn \__morewrites_immediate_iv:NN #1 #2
  {
    \exp_args:Nc #1
      {
        \exp_after:wN \__morewrites_immediate_v:w
        \token_to_str:N #2
      }
  }
\use:x
  {
    \cs_new:Npn \exp_not:N \__morewrites_immediate_v:w
        ##1 \tl_to_str:n { __morewrites } { __morewrites_immediate }
  }
\cs_new_protected:Npn \__morewrites_immediate_closeout_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w \g__morewrites_stream_int }
      { \__morewrites_immediate_closeout_aux: }
  }
\cs_new_protected_nopar:Npn \__morewrites_immediate_closeout_aux:
  {
    \prop_gpop:NVNT \g__morewrites_iow_prop \g__morewrites_stream_int \l__morewrites_internal_tl
      {
        \__morewrites_immediate_write_and_close:nn
          { \g__morewrites_stream_int } { \l__morewrites_internal_tl }
      }
  }
\cs_new_protected:Npn \__morewrites_immediate_write_and_close:nn #1#2
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w
      \g__morewrites_iow #2 \scan_stop:
    \group_begin:
      \int_set_eq:NN \tex_newlinechar:D \c_minus_one
      \tl_use:c { g__morewrites_iow_ \int_eval:n {#1} _tl }
      \tl_gclear:c { g__morewrites_iow_ \int_eval:n {#1} _tl }
    \group_end:
    \__morewrites_tex_immediate:w \__morewrites_tex_closeout:w \g__morewrites_iow
  }
\cs_new_protected:Npn \__morewrites_immediate_openout_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_immediate:w \__morewrites_tex_openout:w \g__morewrites_stream_int }
      { \__morewrites_equals_file_name:N \__morewrites_immediate_openout_aux:n }
  }
\cs_new_protected:Npn \__morewrites_immediate_openout_aux:n #1
  {
    \__morewrites_immediate_closeout_aux:
    \prop_gput:NVn \g__morewrites_iow_prop \g__morewrites_stream_int {#1}
    \tl_gclear_new:c { g__morewrites_iow_ \int_use:N \g__morewrites_stream_int _tl }
  }
\cs_new_protected:Npn \__morewrites_immediate_write_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_immediate:w \__morewrites_tex_write:w \g__morewrites_stream_int }
      { \primargs_get_general_text:N \__morewrites_immediate_write_aux:n }
  }
\cs_new_protected_nopar:Npn \__morewrites_immediate_write_aux:n
  {
    \prop_get:NVNTF \g__morewrites_iow_prop \g__morewrites_stream_int \l__morewrites_internal_tl
      { \__morewrites_immediate_write_open:n }
      { \__morewrites_immediate_write_closed:n }
  }
\cs_new_protected:Npn \__morewrites_immediate_write_closed:n #1
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_write:w
      \if_num:w \g__morewrites_stream_int < \c_zero
        -1
      \else:
        16
      \fi:
      {#1}
  }
\cs_new_protected:Npn \__morewrites_immediate_write_open:n #1
  {
    \__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_eq:NN \tex_endlinechar:D \c_minus_one
      \tex_openin:D \g__morewrites_ior \g__morewrites_tmp_file_tl \scan_stop:
      \__morewrites_immediate_write_readlines_loop:
      \tex_closein:D \g__morewrites_ior
    \group_end:
  }
\cs_new_protected_nopar:Npn \__morewrites_immediate_write_readlines_loop:
  {
    \etex_readline:D \g__morewrites_ior to \l__morewrites_internal_tl
    \ior_if_eof:NF \g__morewrites_ior
      {
        \tl_gput_right:cx
          { g__morewrites_iow_ \int_use:N \g__morewrites_stream_int _tl }
          {
            \__morewrites_tex_immediate:w \__morewrites_tex_write:w \g__morewrites_iow
              { \l__morewrites_internal_tl }
          }
        \__morewrites_immediate_write_readlines_loop:
      }
  }
\cs_new_protected:Npn \__morewrites_late:n #1
  {
    \int_gincr:N \g__morewrites_late_write_int
    \tl_const:cx
      {
        c__morewrites_late_write_
        \int_use:N \g__morewrites_late_write_int
        _tl
      }
      {
        \int_gset:Nn \exp_not:N \g__morewrites_stream_int
          { \exp_not:V \g__morewrites_stream_int }
        \exp_not:n {#1}
      }
    \exp_args:NNx \__morewrites_tex_write:w \g__morewrites_iow
      { `( \int_use:N \g__morewrites_late_write_int ) }
  }
\cs_new_protected_nopar:Npn \__morewrites_openout:w
  { \s__morewrites \primargs_get_number:N \__morewrites_openout_test:n }
\cs_new_protected:Npn \__morewrites_openout_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_openout:w \g__morewrites_stream_int }
      { \__morewrites_equals_file_name:N \__morewrites_openout_aux:n }
  }
\cs_new_protected:Npn \__morewrites_openout_aux:n #1
  { \__morewrites_late:n { \__morewrites_immediate_openout_aux:n {#1} } }
\cs_new_protected_nopar:Npn \__morewrites_write:w
  { \s__morewrites \primargs_get_number:N \__morewrites_write_test:n }
\cs_new_protected:Npn \__morewrites_write_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_write:w \g__morewrites_stream_int }
      { \primargs_get_general_text:N \__morewrites_write_aux:n }
  }
\cs_new_protected:Npn \__morewrites_write_aux:n #1
  { \__morewrites_late:n { \__morewrites_immediate_write_aux:n {#1} } }
\cs_new_protected_nopar:Npn \__morewrites_closeout:w
  { \s__morewrites \primargs_get_number:N \__morewrites_closeout_test:n }
\cs_new_protected:Npn \__morewrites_closeout_test:n #1
  {
    \int_gset:Nn \g__morewrites_stream_int {#1}
    \clist_if_in:NnTF \g__morewrites_reserved_iow_clist {#1}
      { \__morewrites_tex_closeout:w \g__morewrites_stream_int }
      { \__morewrites_closeout_aux: }
  }
\cs_new_protected_nopar:Npn \__morewrites_closeout_aux:
  { \__morewrites_late:n { \__morewrites_immediate_closeout_aux: } }
\cs_new_protected_nopar:Npn \__morewrites_before_shipout:
  {
    \__morewrites_tex_immediate:w \__morewrites_tex_openout:w \g__morewrites_iow
      \g__morewrites_tmp_file_tl \scan_stop:
  }
\cs_new_protected_nopar: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} }
      \etex_everyeof:D { `() \exp_not:N }
      \tl_gset:Nx \g__morewrites_internal_tl
        {
          \exp_after:wN \__morewrites_after_shipout_loop:ww
          \tex_input:D \g__morewrites_tmp_file_tl \c_space_tl
        }
    \group_end:
    \tl_map_inline:Nn \g__morewrites_internal_tl
      { \tl_use:c { c__morewrites_late_write_ ##1 _tl } }
  }
\cs_new:Npn \__morewrites_after_shipout_loop:ww #1 `( #2 )
  {
    \tl_if_empty:nF {#2}
      {
        {#2}
        \__morewrites_after_shipout_loop:ww
      }
  }
\IfFileExists{atbegshi.sty}
  {
    \RequirePackage{atbegshi}
    \tl_replace_once:Nnn \AtBegShi@Output
      { \AtBegShi@OrgShipout \box \AtBeginShipoutBox }
      {
        \__morewrites_before_shipout:
        \AtBegShi@OrgShipout \box \AtBeginShipoutBox
        \__morewrites_after_shipout:
      }
    \tl_replace_once:Nnn \AtBegShi@Output
      { \AtBeginShipoutOriginalShipout \box \AtBeginShipoutBox }
      {
        \__morewrites_before_shipout:
        \AtBeginShipoutOriginalShipout \box \AtBeginShipoutBox
        \__morewrites_after_shipout:
      }
  }
  {
    \int_new:N \g__morewrites_group_level_int
    \box_new:N \g__morewrites_shipout_box
    \cs_new_protected_nopar:Npn \__morewrites_shipout:w
      {
        \int_gset_eq:NN \g__morewrites_group_level_int \etex_currentgrouplevel:D
        \tex_afterassignment:D \__morewrites_shipout_i:
        \tex_global:D \tex_setbox:D \g__morewrites_shipout_box
      }
    \cs_new_protected_nopar:Npn \__morewrites_shipout_i:
      {
        \int_compare:nNnTF { \g__morewrites_group_level_int }
                         = { \etex_currentgrouplevel:D }
          { \__morewrites_shipout_ii: }
          { \tex_aftergroup:D \__morewrites_shipout_ii: }
      }
    \cs_new_protected_nopar:Npn \__morewrites_shipout_ii:
      {
        \__morewrites_before_shipout:
        \__morewrites_tex_shipout:w \tex_box:D \g__morewrites_shipout_box
        \__morewrites_after_shipout:
      }
    \AtBeginDocument { \cs_gset_eq:NN \shipout \__morewrites_shipout:w }
  }
\int_new:N \g__morewrites_at_end_int
\int_gset:Nn \g__morewrites_at_end_int { 10 }
\cs_new_protected_nopar:Npn \__morewrites_close_all:
  {
    \prop_map_function:NN \g__morewrites_iow_prop
      \__morewrites_immediate_write_and_close:nn
    \prop_gclear:N \g__morewrites_iow_prop
  }
\cs_set:Npn \__morewrites_tmp:w #1
  {
    \cs_new_protected:Npn \__morewrites_close_all_at_end:w ##1 #1
      {
        \int_gdecr:N \g__morewrites_at_end_int
        \int_compare:nNnTF \g__morewrites_at_end_int > \c_zero
          {
            \tl_if_empty:nTF {##1}
              { ##1 \__morewrites_close_all: }
              { ##1 \__morewrites_close_all_at_end:w }
          }
          { \__morewrites_close_all: ##1 }
        #1
      }
  }
\exp_args:Nc \__morewrites_tmp:w { @ @ end }
\AtEndDocument { \__morewrites_close_all_at_end:w }
\tex_countdef:D \g__morewrites_alloc_int 17 \scan_stop:
\cs_new:Npn \__morewrites_newwrite:N #1
  {
    \int_gincr:N \g__morewrites_alloc_int
    \if_num:w \g__morewrites_alloc_int = \c_sixteen
      \int_gset:Nn \g__morewrites_alloc_int { 20 }
    \fi:
    \int_set_eq:NN \allocationnumber \g__morewrites_alloc_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_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'.
