%%
%% 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 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{l3names}
\GetIdInfo$Id: morewrites.dtx 0.1 2011-09-09 00:00:00Z bruno $
       {Always room for a new write}%
\ProvidesExplPackage
  {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
\RequirePackage{expl3}
\cs_new_eq:NN \mw_tex_openin:w    \tex_openin:D
\cs_new_eq:NN \mw_tex_read:w      \tex_read:D
\cs_new_eq:NN \mw_etex_readline:w \etex_readline:D
\cs_new_eq:NN \mw_tex_closein:w   \tex_closein:D
\cs_new_eq:NN \mw_tex_immediate:w \tex_immediate:D
\cs_new_eq:NN \mw_tex_openout:w   \tex_openout:D
\cs_new_eq:NN \mw_tex_write:w     \tex_write:D
\cs_new_eq:NN \mw_tex_closeout:w  \tex_closeout:D
\cs_new_eq:NN \mw_tex_shipout:w   \tex_shipout:D
\int_new:N \g_mw_late_write_int
\prop_new:N \g_mw_writes_prop
\newread \g_mw_read
\tl_const:Nn \c_mw_tmp_file_tl { \jobname.mw }
\newwrite \g_mw_write
\seq_new:N \g_mw_reserved_writes_seq
\prg_stepwise_inline:nnnn {0} {1} { \g_mw_write - 1 }
  { \seq_gput_right:Nn \g_mw_reserved_writes_seq {#1} }
\seq_gput_right:Nn \g_mw_reserved_writes_seq {18}
\tl_new:N  \l_mw_tmpa_tl
\int_new:N \g_mw_step_int
\int_new:N \g_mw_tmpa_int
\newbox \g_mw_box
\int_new:N \g_mw_stream_int
\tl_new:N  \g_mw_filename_tl
\tl_new:N  \g_mw_text_tl
\cs_new_eq:NN \mw_scan_stop: \scan_stop:
\cs_new_protected_nopar:Npn \mw_parse_number:Nw #1
  {
    \tex_afterassignment:D #1
    \tex_global:D \g_mw_stream_int
  }
\cs_new_protected_nopar:Npn \mw_parse_text:Nw #1
  {
    \group_begin:
      \tex_aftergroup:D #1
      \tex_afterassignment:D \mw_parse_text_aux:
      \tex_toks:D \c_zero =
  }
\cs_new_protected_nopar:Npn \mw_parse_text_aux:
  {
      \tl_gset:Nx \g_mw_text_tl { \tex_the:D \tex_toks:D \c_zero }
    \group_end:
  }
\cs_new_protected_nopar:Npn \mw_parse_equals_filename:Nw #1
  {
    \peek_meaning_remove:NTF =
      { \mw_parse_filename:Nw #1 } { \mw_parse_filename:Nw #1 }
  }
\cs_new_protected_nopar:Npn \mw_parse_filename:Nw #1
  {
    \group_begin:
    \group_align_safe_begin:
    \tex_aftergroup:D #1
    \tl_gclear:N \g_mw_filename_tl
    \mw_parse_filename_loop:w
  }
\cs_new_protected_nopar:Npn \mw_parse_filename_loop:w
  { \peek_gafter:Nw \mw_parse_filename_loop_test: }
\cs_new_protected_nopar:Npn \mw_parse_filename_loop_test:
  {
    \exp_after:wN \if_meaning:w \exp_not:N \g_peek_token \g_peek_token
      \exp_after:wN \use_ii:nn
    \else:
      \exp_after:wN \use_i:nn
    \fi:
      { \exp_after:wN \mw_parse_filename_loop:w }
      {
        \token_if_cs:NTF \g_peek_token
          { \mw_parse_filename_end: }
          {
            \mw_parse_filename_space_test:NTF \g_peek_token
              { \mw_parse_filename_space_do:w }
              {
                \exp_after:wN \mw_parse_filename_push:w
                \token_to_meaning:N
              }
          }
      }
  }
\cs_new_protected_nopar:Npn \mw_parse_filename_end:
  {
    \group_align_safe_end:
    \group_end:
  }
 %^^A TeXlive's behaviour (?): space with any catcode
\cs_new_protected_nopar:Npn \mw_parse_filename_space_test:NTF
  { \token_if_eq_charcode:NNTF \c_space_token }
\cs_new_protected_nopar:Npn \mw_parse_filename_space_do:w
  {
    \tex_afterassignment:D \mw_parse_filename_end:
    \cs_set_eq:NN \g_peek_token
  }
\cs_new_protected_nopar:Npn \mw_parse_filename_push:w #1 ~ %
  {
    \tl_if_in:nnTF { math ~ alignment ~ macro } { #1 }
      { \mw_parse_filename_push_three:w }
      { \mw_parse_filename_push_two:w }
  }
\cs_new_protected_nopar:Npn \mw_parse_filename_push_two:w #1 ~ #2
  {
    \tl_gput_right:Nn \g_mw_filename_tl {#2}
    \mw_parse_filename_loop:w
  }
\cs_new_protected_nopar:Npn \mw_parse_filename_push_three:w #1 ~ #2 ~ #3
  {
    \tl_gput_right:Nn \g_mw_filename_tl {#3}
    \mw_parse_filename_loop:w
  }
\cs_new:Npn \mw_prg_case_meaning:Nnn #1#2#3
  { \mw_prg_case_meaning_aux:Nw #1 #2 #1 {#3} \q_recursion_stop }
\cs_new:Npn \mw_prg_case_meaning_aux:Nw #1#2#3
  {
    \token_if_eq_meaning:NNTF #1 #2
      { \prg_case_end:nw {#3} }
      { \mw_prg_case_meaning_aux:Nw #1 }
  }
\cs_new:Npn \mw_exp_after:Nf #1
  { \exp_after:wN #1 \tex_romannumeral:D -`0 }
\cs_new_protected_nopar:Npn \mw_immediate:w
  {
    \scan_stop: %^^A?
    \mw_exp_after:Nf \mw_immediate_aux:w
  }
\cs_new_protected_nopar:Npn \mw_immediate_aux:w
  {
    \peek_meaning:NT \mw_scan_stop:
      { \mw_immediate_mw_scan_stop:N }
  }
\cs_new_protected_nopar:Npn \mw_immediate_mw_scan_stop:N #1
  {
    \str_if_eq:nnTF { #1 } { \mw_scan_stop: }
      { \mw_immediate_do:NN }
      { #1 }
  }
\cs_new_protected_nopar:Npn \mw_immediate_do:NN #1 #2
  {
    \exp_args:Nc \mw_parse_number:Nw
      {
        \exp_after:wN \mw_immediate_do_aux:w
        \token_to_str:N #2
      }
  }
\use:x
  {
    \cs_new_protected_nopar:Npn \exp_not:N \mw_immediate_do_aux:w
      ##1 \tl_to_str:n {mw_} { mw_immediate_ }
  }
\cs_new_protected_nopar:Npn \mw_immediate_closeout_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_immediate:w \mw_tex_closeout:w \g_mw_stream_int }
      { \mw_immediate_closeout_aux: }
  }
\cs_new_protected_nopar:Npn \mw_immediate_closeout_aux:
  {
    \exp_args:NNV \prop_pop:NnNT
        \g_mw_writes_prop \g_mw_stream_int \l_mw_tmpa_tl
      {
        \mw_tex_immediate:w \mw_tex_openout:w
            \g_mw_write \l_mw_tmpa_tl \scan_stop:
        \group_begin:
          \int_set_eq:NN \tex_newlinechar:D \c_minus_one
          \tl_use:c { g_mw_write_ \int_use:N \g_mw_stream_int _tl }
          \tl_gclear:c { g_mw_write_ \int_use:N \g_mw_stream_int _tl }
        \group_end:
        \mw_tex_immediate:w \mw_tex_closeout:w \g_mw_write
      }
  }
\cs_new_protected_nopar:Npn \mw_immediate_openout_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_immediate:w \mw_tex_openout:w \g_mw_stream_int }
      { \mw_parse_equals_filename:Nw \mw_immediate_openout_aux: }
  }
\cs_new_protected_nopar:Npn \mw_immediate_openout_aux:
  {
    \mw_immediate_closeout_aux:
    \prop_gput:NVV \g_mw_writes_prop \g_mw_stream_int \g_mw_filename_tl
    \tl_gclear_new:c { g_mw_write_ \int_use:N \g_mw_stream_int _tl }
  }
\cs_new_protected_nopar:Npn \mw_immediate_write_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_immediate:w \mw_tex_write:w \g_mw_stream_int }
      { \mw_parse_text:Nw \mw_immediate_write_aux: }
  }
\cs_new_protected_nopar:Npn \mw_immediate_write_aux:
  {
    \prop_if_in:NVTF \g_mw_writes_prop \g_mw_stream_int
      {
        \mw_tex_immediate:w \mw_tex_openout:w \g_mw_write
          \c_mw_tmp_file_tl \scan_stop:
        \mw_tex_immediate:w \mw_tex_write:w \g_mw_write { \g_mw_text_tl }
        \mw_tex_immediate:w \mw_tex_closeout:w \g_mw_write
        \group_begin:
          \int_set_eq:NN \tex_endlinechar:D \c_minus_one
          \mw_tex_openin:w \g_mw_read \c_mw_tmp_file_tl \scan_stop:
          \mw_immediate_write_aux_readlines:
          \mw_tex_closein:w \g_mw_read
        \group_end:
      }
      {
        \mw_tex_immediate:w \mw_tex_write:w
          \if_num:w \g_mw_stream_int < \c_zero
            -1
          \else:
            16
          \fi:
          { \g_mw_text_tl }
      }
  }
\cs_new_protected_nopar:Npn \mw_immediate_write_aux_readlines:
  {
    \mw_etex_readline:w \g_mw_read to \l_tmpa_tl
    \ior_if_eof:NF \g_mw_read
      {
        \tl_gput_right:cx
          { g_mw_write_ \int_use:N \g_mw_stream_int _tl }
          { \mw_tex_immediate:w \mw_tex_write:w \g_mw_write { \l_tmpa_tl } }
        \mw_immediate_write_aux_readlines:
      }
  }
\cs_new_protected_nopar:Npn \mw_late_aux:nN #1 #2
  {
    \int_gincr:N \g_mw_late_write_int
    \tl_const:cx { c_mw_late_write_ \int_use:N \g_mw_late_write_int _tl }
      {
        \int_gset:Nn \exp_not:N \g_mw_stream_int
          { \exp_not:V \g_mw_stream_int }
        #1
        \exp_not:N #2
      }
    \exp_args:NNx \mw_tex_write:w \g_mw_write
      { `( \int_use:N \g_mw_late_write_int ) }
  }
\cs_new_protected_nopar:Npn \mw_openout:w
  { \mw_scan_stop: \mw_parse_number:Nw \mw_openout_test: }
\cs_new_protected_nopar:Npn \mw_openout_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_openout:w \g_mw_stream_int }
      { \mw_parse_equals_filename:Nw \mw_openout_aux: }
  }
\cs_new_protected_nopar:Npn \mw_openout_aux:
  {
    \mw_late_aux:nN
      {
        \tl_gset:Nn \exp_not:N \g_mw_filename_tl
          { \exp_not:V \g_mw_filename_tl }
      }
      \mw_immediate_openout_aux:
  }
\cs_new_protected_nopar:Npn \mw_write:w
  { \mw_scan_stop: \mw_parse_number:Nw \mw_write_test: }
\cs_new_protected_nopar:Npn \mw_write_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_write:w \g_mw_stream_int }
      { \mw_parse_text:Nw \mw_write_aux: }
  }
\cs_new_protected_nopar:Npn \mw_write_aux:
  {
    \mw_late_aux:nN
      {
        \tl_gset:Nn \exp_not:N \g_mw_text_tl
          { \exp_not:V \g_mw_text_tl }
      }
      \mw_immediate_write_aux:
  }
\cs_new_protected_nopar:Npn \mw_closeout:w
  { \mw_scan_stop: \mw_parse_number:Nw \mw_closeout_test: }
\cs_new_protected_nopar:Npn \mw_closeout_test:
  {
    \seq_if_in:NVTF \g_mw_reserved_writes_seq \g_mw_stream_int
      { \mw_tex_closeout:w \g_mw_stream_int }
      { \mw_closeout_aux: }
  }
\cs_new_protected_nopar:Npn \mw_closeout_aux:
  { \mw_late_aux:nN { } \mw_immediate_closeout_aux: }
\cs_new_protected_nopar:Npn \mw_before_shipout:
  {
    \mw_tex_immediate:w \mw_tex_openout:w \g_mw_write
      \c_mw_tmp_file_tl \scan_stop:
  }
\seq_new:N \g_mw_operations_seq
\cs_new_protected_nopar:Npn \mw_after_shipout:
  {
    \mw_tex_immediate:w \mw_tex_closeout:w \g_mw_write
    \group_begin:
      \int_set_eq:NN \tex_endlinechar:D \tex_newlinechar:D
      \tl_map_inline:nn { `(0123456789) }
        { \char_set_catcode_other:n {`##1} }
      \etex_everyeof:D { `() \exp_not:N }
      \tl_gset:Nx \g_mw_operations_seq
        { \if_false: } \fi:
          \exp_after:wN \mw_after_shipout_loop:ww
          \tex_input:D \c_mw_tmp_file_tl \scan_stop:
    \group_end:
    \seq_map_inline:Nn \g_mw_operations_seq
      {
        \tl_use:c
          { c_mw_late_write_ ##1 _tl }
      }
  }
\cs_new:Npn \mw_after_shipout_loop:ww #1 `( #2 )
  {
    \tl_if_empty:nTF {#2}
      { \if_false: { \fi: } }
      {
        \exp_not:N \seq_item:n {#2}
        \mw_after_shipout_loop:ww
      }
  }
\IfFileExists{atbegshi.sty}
  {
    \RequirePackage{atbegshi}
    \tl_replace_once:Nnn \AtBegShi@Output
      { \AtBegShi@OrgShipout \box \AtBeginShipoutBox }
      {
        \mw_before_shipout:
        \AtBegShi@OrgShipout \box \AtBeginShipoutBox
        \mw_after_shipout:
      }
    \tl_replace_once:Nnn \AtBegShi@Output
      { \AtBeginShipoutOriginalShipout \box \AtBeginShipoutBox }
      {
        \mw_before_shipout:
        \AtBeginShipoutOriginalShipout \box \AtBeginShipoutBox
        \mw_after_shipout:
      }
  }
  {
    \cs_new_protected_nopar:Npn \mw_shipout:w
      {
        \int_gset_eq:NN \g_mw_tmpa_int \etex_currentgrouplevel:D
        \tex_afterassignment:D \mw_shipout_aux:
        \tex_global:D \tex_setbox:D \g_mw_box
      }
    \cs_new_protected_nopar:Npn \mw_shipout_aux:
      {
        \int_compare:nNnTF { \g_mw_tmpa_int }
                         = { \etex_currentgrouplevel:D }
          { \mw_shipout_aux_ii: }
          { \tex_aftergroup:D \mw_shipout_aux_ii: }
      }
    \cs_new_protected_nopar:Npn \mw_shipout_aux_ii:
      {
        \mw_before_shipout:
        \mw_tex_shipout:w \tex_box:D \g_mw_box
        \mw_after_shipout:
      }
    \cs_gset_eq:NN \shipout \mw_shipout:w
  }
\cs_new_protected_nopar:Npn \mw_close_all:
  {
    \prop_map_inline:Nn \g_mw_writes_prop
      {
        \mw_tex_immediate:w \mw_tex_openout:w \g_mw_write ##2 \scan_stop:
        \group_begin:
          \int_set_eq:NN \tex_newlinechar:D \c_minus_one
          \tl_use:c { g_mw_write_ ##1 _tl }
          \tl_gclear:c { g_mw_write_ ##1 _tl }
        \group_end:
        \mw_tex_immediate:w \mw_tex_closeout:w \g_mw_write
      }
    \prop_gclear:N \g_mw_writes_prop
  }
 % \IfFileExists{atveryend.sty} { \use_i:nn } { \use_ii:nn }
 %   {
 %     \RequirePackage {atveryend} [2011/06/29]
 %     \AtVeryVeryEnd { \mw_close_all: }
 %   }
 %   {
    \cs_new_protected:Npn \mw_put_at_end:Nw #1 #2 \@@end
      {
        \tl_if_empty:nTF {#2}
          { #1 \@@end }
          { #2 \mw_put_at_end:Nw #1 \@@end }
      }
    \AtEndDocument { \mw_put_at_end:Nw \mw_close_all: }
  % }
\countdef \mw_write_int 17 \scan_stop:
\cs_new:Npn \mw_newwrite:N #1
  {
    \if_num:w \mw_write_int = \c_sixteen
      \int_gset:Nn \mw_write_int { 20 }
    \fi:
    \int_set_eq:NN \allocationnumber \mw_write_int
    \int_gincr:N \mw_write_int
    \cs_undefine:N #1
    \int_const:Nn #1 { \allocationnumber }
    \wlog {\string #1=\string \write \the \allocationnumber }
  }
\cs_gset_eq:NN \immediate \mw_immediate:w
\cs_gset_eq:NN \openout   \mw_openout:w
\cs_gset_eq:NN \write     \mw_write:w
\cs_gset_eq:NN \closeout  \mw_closeout:w
\cs_gset_eq:NN \newwrite  \mw_newwrite:N
%% 
%%
%% End of file `morewrites.sty'.
