%%
%% This is file `l3galley.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3galley.dtx  (with options: `package')
%% 
%% Copyright (C) 2010-2017 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.
%% 
%% File: l3galley.dtx Copyright (C) 1999-2001, 2004-2009 Frank Mittelbach
%%                              (C) 2010-2018 The LaTeX3 Project
\RequirePackage{expl3}[2018/02/21]
\@ifpackagelater{expl3}{2018/02/21}
  {}
  {%
    \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
  }
\ProvidesExplPackage{l3galley}{2018-10-31}{}
  {L3 Experimental galley code}
\int_new:N \l__galley_tmp_int
\seq_new:N \g__galley_tmpa_seq
\seq_new:N \g__galley_tmpb_seq
\seq_new:N \l__galley_tmp_seq
\tl_new:N \l__galley_tmp_tl
\bool_new:N \g__galley_cutout_active_bool
\bool_new:N \l__galley_cutout_active_bool
\seq_new:N \l__galley_cutout_left_indent_seq
\seq_new:N \l__galley_cutout_right_indent_seq
\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
\int_const:Nn \c__galley_max_penalty_int { 10000 }
\int_new:N \l__galley_group_level_int
\cs_new_protected:Npn \__galley_initialise_variables:
  {
    \int_set:Nn \l__galley_group_level_int { \tex_currentgrouplevel:D }
    \bool_gset_true:N \g__galley_begin_level_bool
    \bool_gset_false:N \g__galley_cutout_active_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:Npn \__galley_initialise_settings:
  {
    \bool_set_true:N \l__galley_parshape_multipar_bool
    \seq_clear:N \l__galley_parshape_left_indent_seq
    \seq_clear:N \l__galley_parshape_right_indent_seq
    \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:Npn \__galley_save_parameters:
  {
    \bool_set_eq:NN \l__galley_begin_level_bool
      \g__galley_begin_level_bool
    \bool_set_eq:NN \l__galley_cutout_active_bool
      \g__galley_cutout_active_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:Npn \__galley_restore_parameters:
  {
    \bool_gset_eq:NN \g__galley_begin_level_bool
      \l__galley_begin_level_bool
    \bool_gset_eq:NN \g__galley_cutout_active_bool
      \l__galley_cutout_active_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:Npn \galley_level:
  {
    \__galley_save_parameters:
    \group_begin:
      \__galley_initialise_variables:
      \__galley_initialise_settings:
      \group_insert_after:N \__galley_level_end:
  }
\cs_new_protected:Npn \__galley_level_end:
  {
      \par
      \__galley_restore_parameters:
    \group_end:
  }
\scan_new:N \s__galley_par_omit
\cs_new_protected:Npn \galley_par:
  {
    \s__galley_par_omit
    \exp_after:wN \__galley_par_auxi: \exp:w \exp_end_continue_f:w
  }
\cs_new_protected:Npn \__galley_par_auxi:
  {
    \peek_meaning:NTF \s__galley_par_omit
      { \__galley_par_aux:N }
      { \__galley_par_auxii: }
  }
\cs_new_protected:Npn \__galley_par_aux:N #1
  {
    \str_if_eq:nnF {#1} { \s__galley_par_omit }
      {
        \__galley_par_auxii:
        #1
      }
  }
\cs_new_protected:Npn \__galley_par_auxii:
  {
    \mode_if_vertical:TF
      {
        \group_begin:
          \tex_par:D
        \group_end:
      }
      {
        \g_galley_par_end_hook_tl
        \bool_if:NT \g__galley_cutout_active_bool
          { \__galley_parshape_set: }
        \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 }
        \int_gzero:N \g__galley_current_par_lines_int
        \bool_if:NF \l__galley_parshape_multipar_bool
          {
            \seq_clear:N \l__galley_parshape_left_indent_seq
            \seq_clear:N \l__galley_parshape_right_indent_seq
            \bool_set_true:N \l__galley_parshape_multipar_bool
            \__galley_parshape_set:
          }
        \__galley_par_update_cutout:
      }
    \g_galley_par_reset_hook_tl
    \tl_gclear:N \g_galley_par_reset_hook_tl
    \tex_penalty:D \c__galley_max_penalty_int
  }
\cs_new_protected:Npn \__galley_par_update_cutout:
  {
    \bool_lazy_and:nnF
      { \seq_if_empty_p:N \g__galley_cutout_left_seq }
      { \seq_if_empty_p:N \g__galley_cutout_right_seq }
      {
        \prg_replicate:nn \tex_prevgraf:D
          {
            \seq_gpop_left:NN \g__galley_cutout_left_seq  \l__galley_tmp_tl
            \seq_gpop_left:NN \g__galley_cutout_right_seq \l__galley_tmp_tl
          }
        \__galley_parshape_set:
      }
    \int_compare:nNnT \l__galley_group_level_int = \tex_currentgrouplevel:D
      {
        \bool_lazy_and:nnT
          { \seq_if_empty_p:N \g__galley_cutout_left_seq }
          { \seq_if_empty_p:N \g__galley_cutout_right_seq }
          { \bool_gset_false:N \g__galley_cutout_active_bool }
      }
  }
\cs_new_protected:Npn \galley_par:n #1
  {
    \s__galley_par_omit
    \bool_if:NF \g__galley_begin_level_bool
      {
        #1
        \galley_par:
      }
  }
\cs_set_protected:Npn \par { \galley_par: }
\tl_set:Nn \@par { \galley_par: }
\cs_new_protected: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:Npn \galley_display_end:
  {
      \par
      \__galley_restore_parameters:
    \group_end:
    \group_insert_after:N \__galley_display_par_setup:
  }
\cs_new_protected: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: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
      }
      {
        \__galley_par_set_cutout:
        \group_begin:
          \tex_par:D
        \group_end:
        \int_gadd:Nn \g__galley_current_par_lines_int \tex_prevgraf:D
        \bool_set_false:N \l__galley_tmp_bool
        \__galley_par_update_cutout:
        \__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:Npn \__galley_start_paragraph_common:N #1
  {
    \group_begin:
      \box_gset_to_last:N \g__galley_last_box
      \tex_par:D
      #1
      \tex_everypar:D { }
      \tex_noindent:D
    \group_end:
  }
\cs_new_protected:Npn \__galley_start_paragraph_std:
  {
    \__galley_start_paragraph_common:N \__galley_insert_vertical_items:
    \__galley_insert_horizontal_items:
    \__galley_restore_running_parameters:
  }
\cs_new_protected:Npn \__galley_start_paragraph_first:
  {
    \bool_gset_false:N \g__galley_begin_level_bool
    \mode_if_horizontal:TF
      { \__galley_start_paragraph_common:N \__galley_insert_vspace_first: }
      { \__galley_insert_vspace_first: }
    \__galley_insert_horizontal_items:
    \__galley_restore_running_parameters:
  }
\cs_new_protected:Npn \__galley_insert_vspace_first:
  {
    \tl_if_empty:NF \g__galley_interpar_vspace_user_tl
      { \skip_vertical:n { \g__galley_interpar_vspace_user_tl } }
  }
\cs_new_protected: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__galley_max_penalty_int }
          { \tex_penalty:D \l_galley_interpar_penalty_int }
      }
      {
        \tex_penalty:D
          \int_eval:n { \g__galley_interpar_penalty_user_tl } \exp_stop_f:
        \tl_gclear:N \g__galley_interpar_penalty_user_tl
      }
    \bool_gset_false:N \g_galley_no_break_next_bool
    \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: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: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
  {
    \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_parshape_set:
  }
\cs_new_protected:Npn \galley_margins_set_relative:nn #1#2
  {
    \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_parshape_set:
  }
\cs_new_protected:Npn \galley_parshape_set_multi:nnnN
  {
    \bool_set_true:N \l__galley_parshape_multipar_bool
    \__galley_parshape_set:nnnN
  }
\cs_new_protected: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 { 0pt }
        \seq_put_left:Nn \l__galley_parshape_right_indent_seq { 0pt }
      }
    \bool_if:NT #4
      {
        \seq_put_right:Nn \l__galley_parshape_left_indent_seq { 0pt }
        \seq_put_right:Nn \l__galley_parshape_right_indent_seq { 0pt }
      }
    \__galley_parshape_set:
  }
\cs_new_protected:Npn \galley_cutout_left:nn
  { \__galley_cutout:nnn { left } }
\cs_new_protected:Npn \galley_cutout_right:nn
  { \__galley_cutout:nnn { right } }
\cs_new_protected:Npn \__galley_cutout:nnn #1#2#3
  {
    \seq_gset_from_clist:Nn \g__galley_tmpa_seq {#3}
    \prg_replicate:nn {#2}
      { \seq_gput_left:Nn \g__galley_tmpa_seq { 0pt } }
    \seq_if_empty:cTF { g__galley_cutout_ #1 _seq }
      { \seq_gset_eq:cN { g__galley_cutout_ #1 _seq } \g__galley_tmpa_seq }
      {
        \int_compare:nNnTF
          { \seq_count:c { g__galley_cutout_ #1 _seq } } >
            { \seq_count:N \g__galley_tmpa_seq }
          { \__galley_cutout:cN { g__galley_cutout_ #1 _seq } \g__galley_tmpa_seq }
          { \__galley_cutout:Nc \g__galley_tmpa_seq { g__galley_cutout_ #1 _seq } }
        \seq_gset_eq:cN { g__galley_cutout_ #1 _seq } \l__galley_tmp_seq
      }
    \seq_if_empty:cF { g__galley_cutout_ #1 _seq }
      { \bool_gset_true:N \g__galley_cutout_active_bool }
  }
\cs_new_protected:Npn \__galley_cutout:NN #1#2
  {
    \seq_clear:N \l__galley_tmp_seq
    \seq_map_inline:Nn #1
      {
        \seq_gpop_left:NNF #2 \l__galley_tmp_tl
          { \tl_set:Nn \l__galley_tmp_tl { 0pt } }
        \seq_put_right:Nx \l__galley_tmp_seq
          { \dim_eval:n { ##1 + \l__galley_tmp_tl } }
      }
  }
\cs_generate_variant:Nn \__galley_cutout:NN { c , Nc }
\cs_new_protected:Npn \__galley_parshape_set:
  {
    \__galley_parshape_set_margins:
    \__galley_parshape_set_indents:
    \__galley_parshape_set_cutouts:
  }
\cs_new_protected:Npn \__galley_parshape_set_margins:
  {
    \bool_lazy_and:nnTF
      { \dim_compare_p:nNn \l_galley_total_left_margin_dim  = \c_zero_dim }
      { \dim_compare_p:nNn \l_galley_total_right_margin_dim = \c_zero_dim }
      { \tex_parshape:D 0 \exp_stop_f: }
      {
        \tex_parshape:D
          1 \exp_stop_f:
          \l_galley_total_left_margin_dim
          \l_galley_text_width_dim
      }
  }
\cs_new_protected:Npn \__galley_parshape_set_indents:
  {
    \bool_lazy_and:nnF
      { \seq_if_empty_p:N \l__galley_parshape_left_indent_seq }
      { \seq_if_empty_p:N \l__galley_parshape_right_indent_seq }
      {
        \__galley_parshape_set_indents:NN
          \l__galley_parshape_left_indent_seq
          \l__galley_parshape_right_indent_seq
      }
  }
\cs_new_protected:Npn \__galley_parshape_set_indents:NN #1#2
  {
    \tex_parshape:D
      \int_min:nn { \seq_count:N #1 } { \seq_count:N #2 }
      \exp_stop_f:
      \seq_mapthread_function:NNN #1 #2
        \__galley_parshape_set_indents:nn
  }
\cs_new:Npn \__galley_parshape_set_indents:nn #1#2
  {
    \dim_eval:n { \l_galley_total_left_margin_dim + #1 } \exp_stop_f:
    \dim_eval:n { \l_galley_text_width_dim - ( #1 + #2 ) } \exp_stop_f:
  }
\cs_new_protected:Npn \__galley_parshape_set_cutouts:
  {
    \bool_lazy_and:nnF
      { \seq_if_empty_p:N \g__galley_cutout_left_seq }
      { \seq_if_empty_p:N \g__galley_cutout_right_seq }
      {
        \__galley_parshape_set_cutouts:n { left }
        \__galley_parshape_set_cutouts:n { right }
        \int_set:Nn \l__galley_tmp_int
          {
              \seq_count:N \l__galley_cutout_left_indent_seq
            - \seq_count:N \l__galley_cutout_right_indent_seq
          }
        \int_compare:nNnTF \l__galley_tmp_int > 0
          { \__galley_parshape_set_cutouts:N \l__galley_cutout_right_indent_seq }
          { \__galley_parshape_set_cutouts:N \l__galley_cutout_left_indent_seq }
        \__galley_parshape_set_indents:NN
          \l__galley_cutout_left_indent_seq
          \l__galley_cutout_right_indent_seq
      }
  }
\cs_new_protected:Npn \__galley_parshape_set_cutouts:n #1
  {
    \seq_if_empty:cTF { g__galley_cutout_ #1 _seq }
      {
        \seq_set_eq:cc { l__galley_cutout_ #1 _indent_seq }
          { l__galley_parshape_ #1 _indent_seq }
      }
      {
        \seq_gset_eq:Nc \g__galley_tmpa_seq { g__galley_cutout_ #1 _seq }
        \seq_gset_eq:Nc \g__galley_tmpb_seq { l__galley_parshape_ #1 _indent_seq }
        \seq_get_right:NNF \g__galley_tmpb_seq \l__galley_tmp_tl
          { \tl_clear:N \l__galley_tmp_tl }
        \tl_if_empty:NT \l__galley_tmp_tl
         { \tl_set:Nn \l__galley_tmp_tl { 0pt } }
        \int_set:Nn \l__galley_tmp_int
          {
              \seq_count:N \g__galley_tmpa_seq
            - \seq_count:N \g__galley_tmpb_seq
            + 1
          }
        \int_compare:nNnT \l__galley_tmp_int > 0
          {
            \prg_replicate:nn
              { \l__galley_tmp_int }
              { \seq_gput_right:NV \g__galley_tmpb_seq \l__galley_tmp_tl }
          }
        \__galley_cutout:NN \g__galley_tmpb_seq \g__galley_tmpa_seq
        \seq_set_eq:cN { l__galley_cutout_ #1 _indent_seq } \l__galley_tmp_seq
      }
  }
\cs_new_protected:Npn \__galley_parshape_set_cutouts:N #1
  {
    \seq_get_right:NNF #1 \l__galley_tmp_tl
      { \tl_clear:N \l__galley_tmp_tl }
    \tl_if_empty:NT \l__galley_tmp_tl
      { \tl_set:Nn \l__galley_tmp_tl { 0pt } }
    \prg_replicate:nn { \int_abs:n \l__galley_tmp_int }
      { \seq_put_right:NV #1 \l__galley_tmp_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_penalty_add_single:n #1
  {
    \tl_gset:Nx \g__galley_interpar_penalty_user_tl
      {
        \int_eval:n
          {
            \tl_if_empty:NF \g__galley_interpar_penalty_user_tl
              { \g__galley_interpar_penalty_user_tl + }
            #1
          }
      }
  }
\cs_new_protected:Npn \galley_vspace_set_single:n #1
  { \tl_gset:Nx \g__galley_interpar_vspace_user_tl { \skip_eval:n {#1} } }
\cs_new_protected:Npn \galley_vspace_add_single:n #1
  {
    \tl_gset:Nx \g__galley_interpar_vspace_user_tl
      {
        \skip_eval:n
          {
            \tl_if_empty:NF \g__galley_interpar_vspace_user_tl
              { \g__galley_interpar_vspace_user_tl + }
            #1
          }
      }
  }
\cs_new_protected:Npn \galley_vspace_max_single:n #1
  {
    \tl_if_empty:NTF \g__galley_interpar_vspace_user_tl
      { \galley_vspace_set_single:n {#1} }
      {
        \dim_compare:nNnT
          { \tex_glueexpr:D \g__galley_interpar_vspace_user_tl }
            < { \tex_glueexpr:D (#1) \scan_stop: }
          { \galley_vspace_set_single: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 \tex_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 3 \tex_font:D }
          = \c_zero_dim
          { \tex_spaceskip:D \c_zero_dim }
          { \tex_spaceskip:D \tex_fontdimen:D 2 \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
      { \__kernel_msg_error:nn { galley } { no-line-to-end } }
      {
        \tex_unskip:D
        \bool_if:NF #1
          { \tex_vadjust:D { \tex_penalty:D \c__galley_max_penalty_int } }
        \dim_compare:nNnF {#2} = \c_zero_dim
          { \tex_vadjust:D { \skip_vertical:n {#2} } }
        \tex_penalty:D \c__galley_max_penalty_int
        \tex_hfil:D
        \tex_penalty:D - \c__galley_max_penalty_int
      }
  }
\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
  {
    \tex_displaywidowpenalties:D
      \int_eval:n { \clist_count:n {#1} + 1 } \exp_stop_f:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      0 \exp_stop_f:
  }
\cs_generate_variant:Nn \galley_display_widow_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_widow_penalties_set:n #1
  {
    \tex_widowpenalties:D
      \int_eval:n { \clist_count:n {#1} + 1 } \exp_stop_f:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      0 \exp_stop_f:
  }
\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
  {
    \tex_clubpenalties:D
      \int_eval:n { \clist_count:n {#1} + 1 } \exp_stop_f:
      \clist_map_function:nN {#1} \__galley_set_aux:n
      0 \exp_stop_f:
    \__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 { \tex_interlinepenalties:D 0 } = 0
      {
        \tex_interlinepenalties:D 1 = \int_eval:n {#1} \exp_stop_f:
        \__galley_calc_interline_penalties:
      }
      {
        \cs_set:Npn \__galley_set_interline_penalty_auxii:n ##1
          {
            \int_eval:n
              {
                \tex_interlinepenalties:D ##1
                - \tex_interlinepenalties:D \tex_interlinepenalties:D 0
                + #1
              }
              \exp_stop_f:
          }
        \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
  {
    \tex_interlinepenalties:D
      \tex_interlinepenalties:D 0 \exp_stop_f:
      \int_step_function:nN {#1}
        \__galley_set_interline_penalty_auxi:n
      \int_step_function:nnN { #1 + 1 } { \tex_interlinepenalties:D 0 - 1 }
        \__galley_set_interline_penalty_auxii:n
      \int_eval:n {#2} \exp_stop_f:
  }
\cs_new:Npn \__galley_set_interline_penalty_auxi:n #1
  { \tex_interlinepenalties:D \int_eval:n {#1} \exp_stop_f: }
\cs_new:Npn \__galley_set_interline_penalty_auxii:n #1 { }
\cs_new_protected:Npn \__galley_calc_interline_penalties:
  {
    \exp_args:Nff \__galley_calc_interline_penalties:nn
      {
        \int_max:nn
          { \clist_count:N \l_galley_club_penalties_clist + 1 }
          {
            \int_max:nn
              { \clist_count:N \l_galley_line_penalties_clist + 1 }
              { \tex_clubpenalties:D 0 }
          }
      }
      { \clist_count:N \l_galley_line_penalties_clist }
  }
\cs_new_protected:Npn \__galley_calc_interline_penalties:nn #1#2
  {
    \tex_interlinepenalties:D #1 ~
      \int_step_function:nN {#2}
        \__galley_calc_interline_penalties_auxi:n
      \int_step_function:nnN { #2 + 1 } { #1 - 1 }
        \__galley_calc_interline_penalties_auxii:n
      \tex_interlinepenalties:D \tex_interlinepenalties:D 0 \exp_stop_f:
  }
\cs_new:Npn \__galley_calc_interline_penalties_auxi:n #1
  {
    \int_eval:n
      {
        \clist_item:Nn \l_galley_line_penalties_clist {#1}
        + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
        - \tex_clubpenalties:D #1 ~
      }
      \exp_stop_f:
  }
\cs_new:Npn \__galley_calc_interline_penalties_auxii:n #1
  {
    \int_eval:n
      {
        \tex_interlinepenalties:D \tex_interlinepenalties:D 0 \exp_stop_f:
        + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
        - \tex_clubpenalties:D #1 ~
      }
      \exp_stop_f:
  }
\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:nN { \tex_clubpenalties:D 0 - 1 }
          \__galley_save_display_club_penalties:n
      }
  }
\cs_new:Npn \__galley_save_display_club_penalties:n #1
  { \int_value:w \tex_clubpenalties:D \int_eval:n {#1} , }
\cs_new_protected:Npn \galley_save_display_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nN { \tex_displaywidowpenalties:D 0 - 1 }
          \__galley_save_display_widow_penalties:n
      }
  }
\cs_new:Npn \__galley_save_display_widow_penalties:n #1
  {
    \int_value:w \tex_displaywidowpenalties:D
      \int_eval:n {#1} ,
  }
\cs_new_protected:Npn \galley_save_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nN { \tex_widowpenalties:D 0 - 1 }
          \__galley_save_widow_penalties:n
      }
  }
\cs_new:Npn \__galley_save_widow_penalties:n #1
  { \int_value:w \tex_widowpenalties:D \int_eval:n {#1} , }
\cs_new_protected:Npn \galley_interline_penalty:
  { \int_value:w \tex_interlinepenalties:D \tex_interlinepenalties:D 0 \exp_stop_f: }
\__kernel_msg_new:nnn { galley } { no-line-to-end }
  { There's~no~line~here~to~end. }
\cs_set:Npn \clearpage
  {
    \mode_if_vertical:T
      {
        \int_compare:nNnT \@dbltopnum = { -1 }
          {
            \dim_compare:nNnT \tex_pagetotal:D < \topskip
              { \tex_hbox:D { } }
          }
      }
    \newpage
    \galley_penalty_set_single:n { -\@Mi }
  }
\cs_set_protected:Npn \nobreak
  { \bool_gset_true:N \g_galley_no_break_next_bool }
\cs_set_protected:Npn \noindent
  {
    \tex_noindent:D
    \bool_gset_false:N \g_galley_omit_next_indent_bool
  }
\DeclareRobustCommand \vspace
  {
    \@ifstar
      \@vspace
      \@vspace
  }
\cs_set:Npn \@vspace #1 { \galley_vspace_add_single:n {#1} }
\DeclareRobustCommand \\
  {
    \@ifstar
      { \cs_set_eq:NN \reserved@e \c_true_bool }
      { \cs_set_eq:NN \reserved@e \c_false_bool }
    \@xnewline
  }
\cs_set:Npn \@xnewline
  {
    \@ifnextchar [ % ]
      { \@newline }
      { \@newline [ 0pt ] }
  }
\cs_set:Npn \@newline [ #1 ]
  { \galley_break_line:Nn \reserved@e {#1} }
\DeclareRobustCommand \newline
  { \galley_break_line:Nn \c_true_bool { 0pt } }
\cs_set_eq:NN \@@par \galley_par:
\cs_set_protected:Npn \addpenalty { \galley_penalty_add_single:n }
\cs_set_protected:Npn \addvspace  { \galley_vspace_max_single:n }
\cs_set_protected: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
      { 1 }
      { \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 { \cs_to_str:N \\ }
%% 
%%
%% End of file `l3galley.sty'.
