%%
%% This is file `l3galley.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3galley.dtx  (with options: `package')
%% 
%% EXPERIMENTAL CODE
%% 
%% Do not distribute this file without also distributing the
%% source files specified above.
%% 
%% Do not distribute a modified version of this file.
%% 
%% File: l3galley.dtx Copyright (C) 1999-2001, 2004-2009 Frank Mittelbach
%%                              (C) 2010-2014 The LaTeX3 Project
%%
%% 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 file is part of the "l3experimental bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%%
\RequirePackage{expl3}[2014/09/15]
\@ifpackagelater{expl3}{2014/09/15}
  {}
  {%
    \PackageError{l3galley}{Support package l3kernel too old}
      {%
        Please install an up to date version of l3kernel\MessageBreak
        using your TeX package manager or from CTAN.\MessageBreak
        \MessageBreak
        Loading l3galley will abort!%
      }%
    \endinput
  }
\GetIdInfo$Id: l3galley.dtx 5437 2014-10-17 12:24:43Z joseph $
  {L3 Experimental galley code}
\ProvidesExplPackage
  {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
\cs_generate_variant:Nn \seq_map_function:NN { Nc }
\int_new:N \l__galley_tmp_int
\seq_new:N \l__galley_tmpa_seq
\seq_new:N \l__galley_tmpb_seq
\tl_new:N \l__galley_tmp_tl
\bool_new:N \l__galley_parshape_multipar_bool
\seq_new:N \l__galley_parshape_left_indent_seq
\seq_new:N \l__galley_parshape_right_indent_seq
\cs_new_eq:NN \l_galley_text_width_dim \linewidth
\cs_new_eq:NN \l_galley_total_left_margin_dim \@totalleftmargin
\dim_new:N \l_galley_total_right_margin_dim
\int_new:N \l_galley_interpar_penalty_int
\skip_new:N \l_galley_interpar_vspace_skip
\cs_new_eq:NN \l_galley_width_dim \tex_hsize:D
\bool_new:N \g__galley_begin_level_bool
\bool_new:N \l__galley_begin_level_bool
\seq_new:N \g__galley_cutout_left_seq
\seq_new:N \l__galley_cutout_left_seq
\seq_new:N \g__galley_cutout_right_seq
\seq_new:N \l__galley_cutout_right_seq
\bool_new:N \g_galley_omit_next_indent_bool
\bool_new:N \l__galley_omit_next_indent_bool
\bool_new:N \g_galley_no_break_next_bool
\bool_new:N \l__galley_no_break_next_bool
\tl_new:N \g_galley_par_begin_hook_tl
\tl_new:N \l__galley_par_begin_hook_tl
\tl_new:N \g_galley_par_end_hook_tl
\tl_new:N \l__galley_par_end_hook_tl
\tl_new:N \g_galley_par_reset_hook_tl
\tl_new:N \l__galley_par_reset_hook_tl
\int_new:N \g_galley_previous_par_lines_int
\int_new:N \l__galley_previous_par_lines_int
\int_new:N \g__galley_current_par_lines_int
\int_new:N \l__galley_current_par_lines_int
\tl_new:N \g_galley_restore_running_tl
\tl_new:N \l__galley_restore_running_tl
\tl_new:N \g_galley_whatsit_next_tl
\tl_new:N \l__galley_whatsit_next_tl
\tl_new:N \g_galley_whatsit_previous_tl
\tl_new:N \l__galley_whatsit_previous_tl
\tl_new:N \g__galley_interpar_penalty_user_tl
\tl_new:N \l__galley_interpar_penalty_user_tl
\tl_new:N \g__galley_interpar_vspace_user_tl
\tl_new:N \l__galley_interpar_vspace_user_tl
\cs_new_protected_nopar:Npn \__galley_initialise_variables:
  {
    \bool_gset_true:N \g__galley_begin_level_bool
    \seq_gclear:N \g__galley_cutout_left_seq
    \seq_gclear:N \g__galley_cutout_right_seq
    \tl_gclear:N \g__galley_interpar_penalty_user_tl
    \tl_gclear:N \g__galley_interpar_vspace_user_tl
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \bool_gset_false:N \g_galley_no_break_next_bool
    \tl_gclear:N \g_galley_par_begin_hook_tl
    \tl_gclear:N \g_galley_par_end_hook_tl
    \tl_gclear:N \g_galley_par_reset_hook_tl
    \int_gzero:N \g__galley_current_par_lines_int
    \int_gzero:N \g_galley_previous_par_lines_int
    \tl_gclear:N \g_galley_restore_running_tl
    \tl_gclear:N \g_galley_whatsit_previous_tl
    \tl_gclear:N \g_galley_whatsit_next_tl
  }
\__galley_initialise_variables:
\cs_new_protected_nopar:Npn \__galley_initialise_settings:
  {
    \dim_set_eq:NN \l_galley_text_width_dim \l_galley_width_dim
    \dim_zero:N \l_galley_total_left_margin_dim
    \dim_zero:N \l_galley_total_right_margin_dim
  }
\cs_new_protected_nopar:Npn \__galley_save_parameters:
  {
    \bool_set_eq:NN \l__galley_begin_level_bool
      \g__galley_begin_level_bool
    \seq_set_eq:NN \l__galley_cutout_left_seq
      \g__galley_cutout_left_seq
    \seq_set_eq:NN \l__galley_cutout_right_seq
      \g__galley_cutout_right_seq
    \tl_set_eq:NN \l__galley_interpar_penalty_user_tl
      \g__galley_interpar_penalty_user_tl
    \tl_set_eq:NN \l__galley_interpar_vspace_user_tl
      \g__galley_interpar_vspace_user_tl
    \bool_set_eq:NN \l__galley_omit_next_indent_bool
      \g_galley_omit_next_indent_bool
    \bool_set_eq:NN \l__galley_no_break_next_bool
      \g_galley_no_break_next_bool
    \tl_set_eq:NN \l__galley_par_begin_hook_tl
      \g_galley_par_begin_hook_tl
    \tl_set_eq:NN \l__galley_par_end_hook_tl
      \g_galley_par_end_hook_tl
    \tl_set_eq:NN \l__galley_par_reset_hook_tl
      \g_galley_par_reset_hook_tl
    \int_set_eq:NN \l__galley_current_par_lines_int
      \g__galley_current_par_lines_int
    \int_set_eq:NN \l__galley_previous_par_lines_int
      \g_galley_previous_par_lines_int
    \tl_set_eq:NN \l__galley_restore_running_tl
      \g_galley_restore_running_tl
    \tl_set_eq:NN \l__galley_whatsit_previous_tl
      \g_galley_whatsit_previous_tl
    \tl_set_eq:NN \l__galley_whatsit_next_tl
      \g_galley_whatsit_next_tl
  }
\cs_new_protected_nopar:Npn \__galley_restore_parameters:
  {
    \bool_gset_eq:NN \g__galley_begin_level_bool
      \l__galley_begin_level_bool
    \seq_gset_eq:NN \g__galley_cutout_left_seq
      \l__galley_cutout_left_seq
    \seq_gset_eq:NN \g__galley_cutout_right_seq
      \l__galley_cutout_right_seq
    \tl_gset_eq:NN \g__galley_interpar_penalty_user_tl
      \l__galley_interpar_penalty_user_tl
    \tl_gset_eq:NN \g__galley_interpar_vspace_user_tl
      \l__galley_interpar_vspace_user_tl
    \bool_gset_eq:NN \g_galley_omit_next_indent_bool
      \l__galley_omit_next_indent_bool
    \bool_gset_eq:NN \g_galley_no_break_next_bool
      \l__galley_no_break_next_bool
    \tl_gset_eq:NN \g_galley_par_begin_hook_tl
      \l__galley_par_begin_hook_tl
    \tl_gset_eq:NN \g_galley_par_end_hook_tl
      \l__galley_par_end_hook_tl
    \tl_gset_eq:NN \g_galley_par_reset_hook_tl
      \l__galley_par_reset_hook_tl
    \int_gset_eq:NN \g__galley_current_par_lines_int
      \l__galley_current_par_lines_int
    \int_gset_eq:NN \g_galley_previous_par_lines_int
      \l__galley_previous_par_lines_int
    \tl_gset_eq:NN \g_galley_restore_running_tl
      \l__galley_restore_running_tl
    \tl_gset_eq:NN \g_galley_whatsit_previous_tl
      \l__galley_whatsit_previous_tl
    \tl_gset_eq:NN \g_galley_whatsit_next_tl
      \l__galley_whatsit_next_tl
  }
\cs_new_protected_nopar:Npn \galley_level:
  {
    \__galley_save_parameters:
    \group_begin:
      \__galley_initialise_variables:
      \__galley_initialise_settings:
      \group_insert_after:N \__galley_level_end:
  }
\cs_new_protected_nopar:Npn \__galley_level_end:
  {
      \par
      \__galley_restore_parameters:
    \group_end:
  }
\__scan_new:N \s__par_omit
\cs_new_protected_nopar:Npn \galley_par:
  {
    \s__par_omit
    \exp_after:wN \__galley_par_auxi: \tex_romannumeral:D - `0
  }
\cs_new_protected:Npn \__galley_par_auxi:
  {
    \peek_meaning:NTF \s__par_omit
      { \__galley_par_aux:N }
      { \__galley_par_auxii: }
  }
\cs_new_protected:Npn \__galley_par_aux:N #1
  {
    \str_if_eq_x:nnF {#1} { \s__par_omit }
      {
        \__galley_par_auxii:
        #1
      }
  }
\cs_new_protected_nopar:Npn \__galley_par_auxii:
  {
    \mode_if_vertical:TF
      {
        \group_begin:
          \tex_par:D
        \group_end:
      }
      {
        \g_galley_par_end_hook_tl
        \group_begin:
          \tex_par:D
        \group_end:
        \int_gset:Nn \g_galley_previous_par_lines_int
          { \tex_prevgraf:D + \g__galley_current_par_lines_int }
        \__galley_parshape_reset:
        \int_gzero:N \g__galley_current_par_lines_int
      }
    \g_galley_par_reset_hook_tl
    \tl_gclear:N \g_galley_par_reset_hook_tl
    \tex_penalty:D \c_ten_thousand
  }
\cs_new_protected:Npn \galley_par:n #1
  {
    \s__par_omit
    \bool_if:nF \g__galley_begin_level_bool
      {
        #1
        \galley_par:
      }
  }
\cs_set_protected_nopar:Npn \par { \galley_par: }
\tl_set:Nn \@par { \galley_par: }
\cs_new_protected_nopar:Npn \galley_display_begin:
  {
    \group_begin:
      \__galley_save_parameters:
      \mode_if_vertical:TF
        {
          \__galley_display_penalty:N \l_galley_display_begin_par_penalty_tl
          \__galley_display_vspace:N  \l_galley_display_begin_par_vspace_tl
        }
        {
          \__galley_display_penalty:N \l_galley_display_begin_penalty_tl
          \__galley_display_vspace:N  \l_galley_display_begin_vspace_tl
        }
      \par
  }
\cs_new_protected:Npn \__galley_display_penalty:N #1
  {
     \tl_if_empty:NF #1
       {
         \tl_gput_right:Nx \g_galley_restore_running_tl
           {
             \int_gset:Nn \exp_not:N \g_galley_penalty_int
               { \int_use:N \g_galley_penalty_int }
           }
         \int_gset:Nn \g_galley_penalty_int {#1}
       }
  }
\cs_new_protected:Npn \__galley_display_vspace:N #1
  {
     \tl_if_empty:NF #1
       {
         \tl_gput_right:Nx \g_galley_restore_running_tl
           {
             \skip_gset:Nn \exp_not:N \g_galley_vspace_skip
               { \skip_use:N \g_galley_vspace_skip }
           }
         \skip_gset:Nn \g_galley_vspace_int {#1}
       }
  }
\cs_new_protected_nopar:Npn \galley_display_end:
  {
      \par
      \__galley_restore_parameters:
    \group_end:
    \group_insert_after:N \__galley_display_par_setup:
  }
\cs_new_protected_nopar:Npn \__galley_display_par_setup:
  {
    \bool_gset_false:N \g_galley_omit_next_indent_bool
    \cs_set_eq:NN \par \__galley_display_par:
  }
\cs_new_protected_nopar:Npn \__galley_display_par:
  {
    \cs_set_eq:NN \par \galley_par:
    \mode_if_vertical:TF
      {
        \par
        \bool_gset_false:N \g_galley_omit_next_indent_bool
        \__galley_display_penalty:N \l_galley_display_end_par_penalty_tl
        \__galley_display_vspace:N  \l_galley_display_end_par_vspace_tl
      }
      {
        \group_begin:
          \tex_par:D
        \group_end:
        \int_gadd:Nn \g__galley_current_par_lines_int \tex_prevgraf:D
        \__galley_display_penalty:N \l_galley_display_end_penalty_tl
        \__galley_display_vspace:N  \l_galley_display_end_vspace_tl
      }
  }
\cs_undefine:N \everypar
\newtoks \everypar
\AtBeginDocument
  {
    \tex_everypar:D
      {
        \bool_if:NTF \g__galley_begin_level_bool
          { \__galley_start_paragraph_first: }
          { \__galley_start_paragraph_std: }
        \tex_the:D \everypar
      }
  }
\box_new:N \g__galley_last_box
\cs_new_protected_nopar:Npn \__galley_start_paragraph_std:
  {
    \group_begin:
      \box_gset_to_last:N \g__galley_last_box
      \tex_par:D
      \__galley_insert_vertical_items:
      \tex_everypar:D { }
      \tex_noindent:D
    \group_end:
    \__galley_insert_horizontal_items:
    \__galley_restore_running_parameters:
  }
\cs_new_protected_nopar:Npn \__galley_start_paragraph_first:
  {
    \bool_gset_false:N \g__galley_begin_level_bool
    \mode_if_horizontal:TF
      {
        \group_begin:
          \box_gset_to_last:N \g__galley_last_box
          \tex_par:D
          \__galley_insert_vspace:
          \tex_everypar:D { }
          \tex_noindent:D
        \group_end:
      }
      { \__galley_insert_vspace: }
    \__galley_insert_horizontal_items:
    \__galley_restore_running_parameters:
  }
\cs_new_protected_nopar:Npn \__galley_insert_vertical_items:
  {
    \g_galley_whatsit_previous_tl
    \tl_gclear:N \g_galley_whatsit_previous_tl
    \tl_if_empty:NTF \g__galley_interpar_penalty_user_tl
      {
        \bool_if:NTF \g_galley_no_break_next_bool
          { \tex_penalty:D \c_ten_thousand }
          { \tex_penalty:D \l_galley_interpar_penalty_int }
      }
      {
        \tex_penalty:D
          \__int_eval:w \g__galley_interpar_penalty_user_tl \__int_eval_end:
        \tl_gclear:N \g__galley_interpar_penalty_user_tl
      }
    \bool_gset_false:N \g_galley_no_break_next_bool
    \__galley_insert_vspace:
  }
\cs_new_protected_nopar:Npn \__galley_insert_vspace:
  {
    \tl_if_empty:NTF \g__galley_interpar_vspace_user_tl
      { \skip_vertical:N \l_galley_interpar_vspace_skip }
      {
        \skip_vertical:n { \g__galley_interpar_vspace_user_tl }
        \tl_gclear:N \g__galley_interpar_vspace_user_tl
      }
  }
\cs_new_protected_nopar:Npn \__galley_insert_horizontal_items:
  {
    \g_galley_whatsit_next_tl
    \tl_gclear:N \g_galley_whatsit_next_tl
    \bool_if:NF \g_galley_omit_next_indent_bool
      {
        \box_if_empty:NF \g__galley_last_box
          { \hbox_to_wd:nn \l_galley_par_indent_dim { } }
      }
    \skip_horizontal:N \l_galley_par_begin_skip
    \g_galley_par_begin_hook_tl
    \bool_gset_false:N \g_galley_omit_next_indent_bool
  }
\cs_new_protected_nopar:Npn \__galley_restore_running_parameters:
  {
    \g_galley_restore_running_tl
    \tl_gclear:N \g_galley_restore_running_tl
  }
\dim_new:N \l__galley_total_left_margin_dim
\dim_new:N \l__galley_total_right_margin_dim
\cs_new_protected:Npn \galley_margins_set_absolute:nn #1#2
  {
    \__galley_save_margins:
    \dim_set:Nn \l_galley_total_left_margin_dim  {#1}
    \dim_set:Nn \l_galley_total_right_margin_dim {#2}
    \dim_set:Nn \l_galley_text_width_dim
      {
          \l_galley_width_dim
        - \l_galley_total_left_margin_dim
        - \l_galley_total_right_margin_dim
      }
    \__galley_set_measure:
  }
\cs_new_protected:Npn \galley_margins_set_relative:nn #1#2
  {
    \__galley_save_margins:
    \dim_add:Nn \l_galley_total_left_margin_dim  {#1}
    \dim_add:Nn \l_galley_total_right_margin_dim {#2}
    \dim_set:Nn \l_galley_text_width_dim
      {
          \l_galley_width_dim
        - \l_galley_total_left_margin_dim
        - \l_galley_total_right_margin_dim
      }
    \__galley_set_measure:
  }
\cs_new_protected_nopar:Npn \__galley_save_margins:
  {
    \dim_set_eq:NN \l__galley_total_left_margin_dim
      \l_galley_total_left_margin_dim
    \dim_set_eq:NN \l__galley_total_right_margin_dim
      \l_galley_total_right_margin_dim
  }
\cs_new_protected_nopar:Npn \__galley_set_measure:
  {
    \int_compare:nNnTF \tex_parshape:D = \c_zero
      { \__galley_parshape_measure: }
      {
        \dim_sub:Nn \l__galley_total_left_margin_dim
          \l_galley_total_left_margin_dim
        \dim_sub:Nn \l__galley_total_right_margin_dim
          \l_galley_total_right_margin_dim
        \tex_parshape:D
          \tex_parshape:D
          \int_step_function:nnnN
            \c_one \c_one \tex_parshape:D
            \__galley_set_measure_aux:n
      }
  }
\cs_new:Npn \__galley_set_measure_aux:n #1
  {
    \__dim_eval:w
      \etex_parshapeindent:D #1 - \l__galley_total_left_margin_dim
    \__dim_eval_end:
    \__dim_eval:w
      \etex_parshapelength:D #1
        + \l__galley_total_left_margin_dim + \l__galley_total_right_margin_dim
    \__dim_eval_end:
  }
\cs_new_protected_nopar:Npn \__galley_parshape_reset:
  {
    \int_compare:nNnF \tex_parshape:D = \c_zero
      {
        \bool_if:nTF
          {
               \seq_if_empty_p:N \g__galley_cutout_left_seq
            && \seq_if_empty_p:N \g__galley_cutout_right_seq
          }
          {
            \bool_if:NF \l__galley_parshape_multipar_bool
              { \__galley_parshape_measure: }
          }
          {
            \prg_replicate:nn \g_galley_previous_par_lines_int
              {
                \seq_gpop:NN \g__galley_cutout_left_seq \l__galley_tmp_tl
                \seq_gpop:NN \g__galley_cutout_right_seq \l__galley_tmp_tl
              }
            \bool_if:NTF \l__galley_parshape_multipar_bool
              { \__galley_parshape_set: }
              { \__galley_parshape_measure: }
            \__galley_cutout_set:Nn \g__galley_cutout_left_seq { left }
            \__galley_cutout_set:Nn \g__galley_cutout_right_seq { right }
          }
      }
  }
\cs_new_protected_nopar:Npn \__galley_parshape_measure:
  {
    \tex_parshape:D
      \c_one
      \l_galley_total_left_margin_dim
      \l_galley_text_width_dim
  }
\cs_new_protected_nopar:Npn \galley_parshape_set_multi:nnnN
  {
    \bool_set_true:N \l__galley_parshape_multipar_bool
    \__galley_parshape_set:nnnN
  }
\cs_new_protected_nopar:Npn \galley_parshape_set_single:nnnN
  {
    \bool_set_false:N \l__galley_parshape_multipar_bool
    \__galley_parshape_set:nnnN
  }
\cs_generate_variant:Nn \galley_parshape_set_multi:nnnN  { nVV }
\cs_generate_variant:Nn \galley_parshape_set_single:nnnN { nVV }
\cs_new_protected:Npn \__galley_parshape_set:nnnN #1#2#3#4
  {
    \seq_set_from_clist:Nn \l__galley_parshape_left_indent_seq {#2}
    \seq_set_from_clist:Nn \l__galley_parshape_right_indent_seq {#3}
    \prg_replicate:nn {#1}
      {
        \seq_put_left:Nn \l__galley_parshape_left_indent_seq { 0 pt }
        \seq_put_left:Nn \l__galley_parshape_right_indent_seq { 0 pt }
      }
    \bool_if:NT #4
      {
        \seq_put_right:Nn \l__galley_parshape_left_indent_seq { 0 pt }
        \seq_put_right:Nn \l__galley_parshape_right_indent_seq { 0 pt }
      }
    \__galley_parshape_set:
  }
\cs_new_protected_nopar:Npn \__galley_parshape_set:
  {
    \tex_parshape:D
      \__int_eval:w
        \int_min:nn
          { \seq_count:N \l__galley_parshape_left_indent_seq }
          { \seq_count:N \l__galley_parshape_right_indent_seq }
      \__int_eval_end:
      \seq_mapthread_function:NNN
        \l__galley_parshape_left_indent_seq
        \l__galley_parshape_right_indent_seq
        \__galley_parshape_set:nn
  }
\cs_new:Npn \__galley_parshape_set:nn #1#2
  {
    \__dim_eval:w \l_galley_total_left_margin_dim + ( #1 ) \__dim_eval_end:
    \__dim_eval:w
      \l_galley_text_width_dim - ( ( #1 ) + ( #2 ) )
    \__dim_eval_end:
  }
\cs_new_protected_nopar:Npn \galley_cutout_left:nn
  { \__galley_cutout:nnn { left } }
\cs_new_protected_nopar:Npn \galley_cutout_right:nn
  { \__galley_cutout:nnn { right } }
\cs_new_protected:Npn \__galley_cutout:nnn #1#2#3
  {
    \seq_set_from_clist:Nn \l__galley_tmpa_seq {#3}
    \prg_replicate:nn {#2}
      { \seq_put_left:Nn \l__galley_tmpa_seq { 0 pt } }
    \__galley_cutout_set:Nn \l__galley_tmpa_seq {#1}
    \seq_clear:N \l__galley_tmpb_seq
    \int_compare:nNnTF
      { \seq_count:N \l__galley_tmpa_seq } >
        { \seq_count:c { g__galley_cutout_ #1 _ seq }  }
      {
        \seq_map_inline:Nn \l__galley_tmpa_seq
          {
            \__galley_cutout_store:nn
              { \seq_gpop:cNF { g__galley_cutout_ #1 _ seq } }
              {##1}
          }
      }
      {
        \seq_map_inline:cn { g__galley_cutout_ #1 _ seq }
          {
            \__galley_cutout_store:nn
              { \seq_pop:NNF \l__galley_tmpa_seq }
              {##1}
          }
      }
    \seq_gset_eq:cN { g__galley_cutout_ #1 _ seq } \l__galley_tmpb_seq
  }
\cs_new_protected:Npn \__galley_cutout_store:nn #1#2
  {
    #1 \l__galley_tmp_tl
      { \tl_set:Nn \l__galley_tmp_tl { 0 pt } }
    \seq_put_right:Nx \l__galley_tmpb_seq
      { \dim_eval:n { \l__galley_tmp_tl + #2 } }
  }
\cs_new_protected:Npn \__galley_cutout_set:Nn #1#2
  {
    \int_zero:N \l__galley_tmp_int
    \tl_clear:N \l__galley_tmp_tl
    \seq_map_function:Nc #1 { __galley_cutout_ #2 :n }
    \int_compare:nNnTF \tex_parshape:D > \l__galley_tmp_int
      {
        \int_step_function:nnnN
          { \l__galley_tmp_int + \c_one }
          \c_one
          \tex_parshape:D
          \__galley_cutout_end:n
      }
      {
        \int_incr:N \l__galley_tmp_int
        \int_compare:nNnTF \tex_parshape:D = \c_zero
          {
            \tl_put_right:Nn \l__galley_tmp_tl
             { \l__galley_total_left_margin_dim \l_galley_text_width_dim }
          }
          {
            \tl_put_right:Nx \l__galley_tmp_tl
              {
                \dim_eval:n { \etex_parshapeindent:D \tex_parshape:D }
                \c_space_tl
                \dim_eval:n { \etex_parshapelength:D \tex_parshape:D }
                \c_space_tl
              }
          }
      }
    \tex_parshape:D
      \l__galley_tmp_int
      \l__galley_tmp_tl
  }
\cs_new_protected:Npn \__galley_cutout_left:n #1
  {
    \int_incr:N \l__galley_tmp_int
    \tl_put_right:Nx \l__galley_tmp_tl
      {
        \int_compare:nNnTF \l__galley_tmp_int > \tex_parshape:D
          {
            \dim_eval:n { \l__galley_total_left_margin_dim + ( #1 ) }
            \c_space_tl
            \dim_eval:n { \l_galley_text_width_dim - ( #1 ) }
            \c_space_tl
          }
          {
            \dim_eval:n { \etex_parshapeindent:D \l__galley_tmp_int + ( #1 ) }
            \c_space_tl
            \dim_eval:n { \etex_parshapelength:D \l__galley_tmp_int - ( #1 ) }
            \c_space_tl
          }
      }
  }
\cs_new_protected:Npn \__galley_cutout_right:n #1
  {
    \int_incr:N \l__galley_tmp_int
    \tl_put_right:Nx \l__galley_tmp_tl
      {
        \int_compare:nNnTF \l__galley_tmp_int > \tex_parshape:D
          {
            \dim_use:N \l__galley_total_left_margin_dim
            \c_space_tl
            \dim_eval:n { \l_galley_text_width_dim - ( #1 ) }
            \c_space_tl
          }
          {
            \dim_eval:n { \etex_parshapeindent:D \l__galley_tmp_int }
            \c_space_tl
            \dim_eval:n { \etex_parshapelength:D \l__galley_tmp_int - ( #1 ) }
            \c_space_tl
          }
      }
  }
\cs_new_protected:Npn \__galley_cutout_end:n #1
  {
    \int_incr:N \l__galley_tmp_int
    \tl_put_right:Nx \l__galley_tmp_tl
      {
        \dim_eval:n { \etex_parshapeindent:D #1 }
        \c_space_tl
        \dim_eval:n { \etex_parshapelength:D #1 }
        \c_space_tl
      }
  }
\cs_new_protected:Npn \galley_penalty_set_single:n #1
  { \tl_gset:Nx \g__galley_interpar_penalty_user_tl { \int_eval:n {#1} } }
\cs_new_protected:Npn \galley_vspace_set_single:n #1
  { \tl_gset:Nx \g__galley_interpar_vspace_user_tl { \skip_eval:n {#1} } }
\dim_set:Nn \parskip \c_zero_dim
\cs_undefine:N \parskip
\skip_new:N \parskip
\cs_new_eq:NN \l_galley_line_left_skip  \tex_leftskip:D
\cs_new_eq:NN \l_galley_line_right_skip \tex_rightskip:D
\dim_new:N \l_galley_par_begin_skip
\cs_new_eq:NN \l_galley_par_end_skip   \tex_parfillskip:D
\cs_new_eq:NN \l_galley_par_indent_dim \tex_parindent:D
\cs_new_eq:NN \l_galley_last_line_fit_int \etex_lastlinefit:D
\cs_new_protected:Npn \galley_interword_spacing_set:N #1
  {
    \bool_if:NTF #1
      { % TODO Hook for font changes required!
        \dim_compare:nNnTF { \tex_fontdimen:D \c_three \tex_font:D }
          = \c_zero_dim
          { \tex_spaceskip:D \c_zero_dim }
          { \tex_spaceskip:D \tex_fontdimen:D \c_two \tex_font:D }
      }
      { \tex_spaceskip:D \c_zero_dim }
  }
\int_new:N \l_galley_hyphen_left_int
\int_set:Nn \l_galley_hyphen_left_int { \tex_lefthyphenmin:D }
\cs_new_eq:NN \l_galley_binop_penalty_int          \tex_binoppenalty:D
\cs_new_eq:NN \l_galley_double_hyphen_demerits_int \tex_doublehyphendemerits:D
\cs_new_eq:NN \l_galley_emergency_stretch_skip     \tex_emergencystretch:D
\cs_new_eq:NN \l_galley_final_hyphen_demerits_int  \tex_finalhyphendemerits:D
\cs_new_eq:NN \l_galley_linebreak_badness_int      \tex_hbadness:D
\cs_new_eq:NN \l_galley_linebreak_fuzz_dim         \tex_hfuzz:D
\cs_new_eq:NN \l_galley_linebreak_penalty_int      \tex_linepenalty:D
\cs_new_eq:NN \l_galley_linebreak_pretolerance_int \tex_pretolerance:D
\cs_new_eq:NN \l_galley_mismatch_demerits_int      \tex_adjdemerits:D
\cs_new_eq:NN \l_galley_relation_penalty_int       \tex_relpenalty:D
\cs_new_eq:NN \l_galley_linebreak_tolerance_int    \tex_tolerance:D
\cs_new_protected:Npn \galley_break_line:Nn #1#2
  {
    \mode_if_vertical:TF
      { \__msg_kernel_error:nn { galley } { no-line-to-end } }
      {
        \tex_unskip:D
        \bool_if:NF #1
          { \tex_vadjust:D { \tex_penalty:D \c_ten_thousand } }
        \dim_compare:nNnF {#2} = \c_zero_dim
          { \tex_vadjust:D { \skip_vertical:n {#2} } }
        \tex_penalty:D \c_ten_thousand
        \tex_hfil:D
        \tex_penalty:D -\c_ten_thousand
      }
  }
\cs_new_eq:NN \l_galley_broken_penalty_int       \tex_brokenpenalty:D
\cs_new_eq:NN \l_galley_interline_penalty_int    \tex_interlinepenalty:D
\cs_new_eq:NN \l_galley_parbreak_badness_int     \tex_vbadness:D
\cs_new_eq:NN \l_galley_parbreak_fuzz_dim        \tex_vfuzz:D
\cs_new_eq:NN \l_galley_post_display_penalty_int \tex_postdisplaypenalty:D
\cs_new_eq:NN \l_galley_pre_display_penalty_int  \tex_predisplaypenalty:D
\clist_new:N \l_galley_club_penalties_clist
\clist_new:N \l_galley_line_penalties_clist
\cs_new_protected:Npn \galley_display_widow_penalties_set:n #1
  {
    \etex_displaywidowpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      \c_zero
  }
\cs_generate_variant:Nn \galley_display_widow_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_widow_penalties_set:n #1
  {
    \etex_widowpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      \c_zero
  }
\cs_generate_variant:Nn \galley_widow_penalties_set:n { V , v }
\cs_new:Npn \__galley_set_aux:n #1 { \int_eval:n {#1} ~ }
\cs_new_protected:Npn \galley_club_penalties_set:n #1
  {
    \clist_set:Nn \l_galley_club_penalties_clist {#1}
    \__galley_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_club_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_interline_penalties_set:n #1
  {
    \clist_set:Nn \l_galley_line_penalties_clist {#1}
    \__galley_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_interline_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_display_club_penalties_set:n #1
  {
    \etex_clubpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      \c_zero
    \__galley_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_display_club_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_interline_penalty_set:n #1
  {
    \int_compare:nNnTF { \etex_interlinepenalties:D \c_zero } = \c_zero
      {
        \etex_interlinepenalties:D \c_one \__int_eval:w #1 \__int_eval_end:
        \__galley_calc_interline_penalties:
      }
      {
        \cs_set:Npn \__galley_set_interline_penalty_auxii:n ##1
          {
            \__int_eval:w
              \etex_interlinepenalties:D ##1
               - \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
               + #1
            \__int_eval_end:
          }
        \exp_args:Nf \__galley_set_interline_penalty:nn
          { \clist_count:N \l_galley_line_penalties_clist } {#1}
      }
  }
\cs_new_protected:Npn \__galley_set_interline_penalty:nn #1#2
  {
    \etex_interlinepenalties:D
      \etex_interlinepenalties:D \c_zero
      \int_step_function:nnnN \c_one \c_one {#1}
        \__galley_set_interline_penalty_auxi:n
      \int_step_function:nnnN { #1 + \c_one } \c_one
        { \etex_interlinepenalties:D \c_zero - \c_one }
        \__galley_set_interline_penalty_auxii:n
      \__int_eval:w #2 \__int_eval_end:
  }
\cs_new:Npn \__galley_set_interline_penalty_auxi:n #1
  { \etex_interlinepenalties:D \__int_eval:w #1 \__int_eval_end: }
\cs_new:Npn \__galley_set_interline_penalty_auxii:n #1 { }
\cs_new_protected_nopar:Npn \__galley_calc_interline_penalties:
  {
    \exp_args:Nff \__galley_calc_interline_penalties:nn
      {
        \int_max:nn
          {
              \clist_count:N \l_galley_club_penalties_clist
            + \c_one
          }
          {
            \int_max:nn
              {
                  \clist_count:N \l_galley_line_penalties_clist
                + \c_one
              }
              { \etex_clubpenalties:D \c_zero }
          }
      }
      { \clist_count:N \l_galley_line_penalties_clist }
  }
\cs_new_protected:Npn \__galley_calc_interline_penalties:nn #1#2
  {
    \etex_interlinepenalties:D #1 ~
      \int_step_function:nnnN \c_one \c_one {#2}
        \__galley_calc_interline_penalties_auxi:n
      \int_step_function:nnnN { #2 + \c_one } \c_one { #1 - \c_one }
        \__galley_calc_interline_penalties_auxii:n
      \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
  }
\cs_new:Npn \__galley_calc_interline_penalties_auxi:n #1
  {
    \__int_eval:w
        \clist_item:Nn \l_galley_line_penalties_clist {#1}
      + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
      - \etex_clubpenalties:D #1 ~
    \__int_eval_end:
  }
\cs_new:Npn \__galley_calc_interline_penalties_auxii:n #1
  {
    \__int_eval:w
        \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
      + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
      - \etex_clubpenalties:D #1 ~
    \__int_eval_end:
  }
\cs_new_protected:Npn \galley_save_club_penalties:N #1
  { \clist_set_eq:NN #1 \l_galley_club_penalties_clist }
\cs_new_protected:Npn \galley_save_interline_penalties:N #1
  { \clist_set_eq:NN #1 \l_galley_line_penalties_clist }
\cs_new_protected:Npn \galley_save_display_club_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_clubpenalties:D \c_zero - \c_one }
          \__galley_save_display_club_penalties:n
      }
  }
\cs_new:Npn \__galley_save_display_club_penalties:n #1
  { \int_use:N \etex_clubpenalties:D \__int_eval:w #1 \__int_eval_end: , }
\cs_new_protected:Npn \galley_save_display_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_displaywidowpenalties:D \c_zero - \c_one }
          \__galley_save_display_widow_penalties:n
      }
  }
\cs_new:Npn \__galley_save_display_widow_penalties:n #1
  { \int_use:N \etex_displaywidowpenalties:D \__int_eval:w #1 \__int_eval_end: , }
\cs_new_protected:Npn \galley_save_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_widowpenalties:D \c_zero - \c_one }
          \__galley_save_widow_penalties:n
      }
  }
\cs_new:Npn \__galley_save_widow_penalties:n #1
  { \int_use:N \etex_widowpenalties:D \__int_eval:w #1 \__int_eval_end: , }
\cs_new_protected_nopar:Npn \galley_interline_penalty:
  { \int_use:N \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero }
\__msg_kernel_new:nnn { galley } { no-line-to-end }
  { There's~no~line~here~to~end. }
\RenewDocumentCommand \clearpage { }
  {
    \mode_if_vertical:T
      {
        \int_compare:nNnT \@dbltopnum = \c_minus_one
          {
            \dim_compare:nNnT \tex_pagetotal:D < \topskip
              { \tex_hbox:D { } }
          }
      }
    \newpage
    \tl_gput_right:Nn \g_galley_whatsit_next_tl
      { \iow_shipout:Nx \c_minus_one { } }
    \tex_vbox:D { }
    \galley_penalty_set_single:n { -\@Mi }
  }
\RenewDocumentCommand \nobreak { }
  { \bool_gset_true:N \g_galley_no_break_next_bool }
\RenewDocumentCommand \noindent { }
  {
    \tex_noindent:D
    \bool_gset_false:N \g_galley_omit_next_indent_bool
  }
\RenewDocumentCommand \vspace { s m }
  {
    \IfBooleanTF #1
      { \galley_vspace_set_single:n {#2} }
      { \galley_vspace_set_single:n {#2} }
  }
\RenewDocumentCommand \\ { s O { 0 pt } }
  { \galley_break_line:Nn #1 {#2} }
\RenewDocumentCommand \newline { }
  { \galley_break_line:Nn \c_true_bool { 0 pt } }
\cs_set_eq:cN { @ @ par } \galley_par:
\cs_set_protected_nopar:Npn \@afterheading
  {
    \bool_gset_true:N \g_galley_no_break_next_bool
    \if@afterindent
    \else
      \bool_gset_true:N \g_galley_omit_next_indent_bool
    \fi
  }
\cs_set_protected:Npn \@hangfrom #1
  {
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \leavevmode
    \setbox \@tempboxa = \hbox { {#1} }
    \galley_parshape_set_single:nnnN
      \c_one
      { \box_wd:N \@tempboxa }
      \c_zero_dim
      \c_false_bool
    \bool_gset_true:N \g_galley_no_break_next_bool
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \box \@tempboxa
  }
\cs_set_eq:Nc \@normalcr { \token_to_str:N \\ }
%% 
%%
%% End of file `l3galley.sty'.
