%%
%% This is file `widows-and-orphans.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% widows-and-orphans.dtx 
%% 
%% This is a generated file.
%% 
%% Copyright 2017-2018 Frank Mittelbach
%% 
%% 
%% This file was generated from file(s) of the LaTeX windows-and-orphans Bundle.
%% -----------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    https://www.latex-project.org/lppl/
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% windows-and-orphans Bundle. You may however distribute the Bundle
%% without such generated files.
%% 
%%
%% File: widows-and-orphans.dtx (C) Copyright 2017-2018 Frank Mittelbach


\NeedsTeXFormat{LaTeX2e}    \RequirePackage{xparse,l3keys2e}
\ProvidesExplPackage{widows-and-orphans}{2018/09/01}{v1.0a}
                    {Detecting widows and orphans (FMi)}
\tl_put_left:Nn \@makecol { \__fmwao_test_for_widows_etc: }
\bool_new:N \g__fmwao_gen_warn_bool
\bool_gset_true:N \g__fmwao_gen_warn_bool
\cs_new:Npn \__fmwao_test_for_widows_etc: {
  \int_case:nnF { \outputpenalty - \interlinepenalty }
    {
      { \widowpenalty }
        { \__fmwao_problem_identified:nn{widow}{Widow} }
      { \displaywidowpenalty }
        { \__fmwao_problem_identified:nn{widow}{Display~ widow} }
      { \clubpenalty }
        { \__fmwao_problem_identified:n{orphan} }
      { \clubpenalty + \widowpenalty }
        { \__fmwao_problem_identified:nn{orphan-widow}{} }
      { \clubpenalty + \displaywidowpenalty }
        { \__fmwao_problem_identified:nn{orphan-widow}{display} }
      { \brokenpenalty }
        { \__fmwao_problem_identified:n{hyphen} }
      { \brokenpenalty + \widowpenalty }
        { \__fmwao_problem_identified:nn{widow}{Widow} }
      { \brokenpenalty + \displaywidowpenalty }
        { \__fmwao_problem_identified:nn{widow}{Display~ widow} }
      { \brokenpenalty + \clubpenalty }
        { \__fmwao_problem_identified:n{orphan} }
      { \brokenpenalty + \clubpenalty + \widowpenalty }
        { \__fmwao_problem_identified:nn{orphan-widow}{} }
      { \brokenpenalty + \clubpenalty + \displaywidowpenalty }
        { \__fmwao_problem_identified:nn{orphan-widow}{display} }
      { \predisplaypenalty - \interlinepenalty }
        { \__fmwao_problem_identified:nn{widow}{Lonely~ display} }
    }
    { \bool_if:NF \g__fmwao_gen_warn_bool
        { \msg_error:nn{widows-and-orphans}{no-problem} } }
  \bool_gset_true:N \g__fmwao_gen_warn_bool
}

\cs_new:Npn \__fmwao_problem_identified:n #1 {
   \bool_if:NT \g__fmwao_gen_warn_bool
               { \msg_warning:nn{widows-and-orphans}{#1} }
}
\cs_new:Npn \__fmwao_problem_identified:nn #1 #2 {
   \bool_if:NT \g__fmwao_gen_warn_bool
               { \msg_warning:nnn{widows-and-orphans}{#1}{#2} }
}

\cs_new:Npn \__fmwao_this_page: { \__fmwao_some_page:n   \c@page       }
\cs_new:Npn \__fmwao_next_page: { \__fmwao_some_page:n { \c@page + 1 } }

\cs_new:Npn \__fmwao_some_page:n #1 {
  \cs_if_eq:NNTF \thepage \__fmwao_roman_thepage:
    { \int_to_roman:n } { \int_to_arabic:n }
  { #1 }
}
\cs_new_nopar:Npn \__fmwao_roman_thepage: {\csname @roman\endcsname \c@page}

\prg_new_conditional:Npnn \legacy_switch_if:n #1 {p, T , F , TF }
  { \exp_args:Nc\if_meaning:w { if#1 } \iftrue \prg_return_true:
                                        \else: \prg_return_false: \fi: }
\msg_new:nnnn {widows-and-orphans} {no-problem}
  { No~ problem~ to~ suppress~ on~ this~ page! }
  { Suppression~ of~ a~ widow~ or~ orphan~ problem~ was~ requested~
    but~ on~ the~ current~ page~ there~ doesn't~ seem~ to~ be~ any.~
    Maybe~ the~ text~ was~ changed~ and~ the~ request~ should~ get~
    (re)moved?}
\msg_new:nnnn {widows-and-orphans} {orphan}
  { Orphan~ on~ page~ \__fmwao_this_page:
    \legacy_switch_if:nT {@twocolumn}
      { \space ( \legacy_switch_if:nTF {@firstcolumn}
                                       { first~ } { (second~ } column) }
  }
  { Check~ out~ the~ page~ and~ see~ if~ you~ can~ avoid~ the~ orphan.}
\msg_new:nnnn {widows-and-orphans} {hyphen}
  { Hyphen~ in~ last~ line~ of~ page~ \__fmwao_this_page:
    \legacy_switch_if:nT {@twocolumn}
      { \space ( \legacy_switch_if:nTF {@firstcolumn}
                                       { first~ } { (second~ } column) } }
  { Check~ out~ the~ page~ and~ see~ if~ you~ can~ get~
    a~ better~ line~ break. }
\msg_new:nnnn {widows-and-orphans} {widow}
  { #1~ on~ page~
    \legacy_switch_if:nTF {@twocolumn}
      { \legacy_switch_if:nTF {@firstcolumn}
          { \__fmwao_this_page: \space (second~ }
          { \__fmwao_next_page: \space (first~  }
        column)
      }
      { \__fmwao_next_page: }
  }
  { Check~ out~ the~ page~ and~ see~ if~ you~ can~ avoid~ the~ widow.}
\msg_new:nnnn {widows-and-orphans} {orphan-widow}
  { Orphan~
    \legacy_switch_if:nTF {@twocolumn}
       { \legacy_switch_if:nTF {@firstcolumn}
           { and~ #1 widow~ on~ page~ \__fmwao_this_page: \space
             (first~ and~ second~ }
           { on~ page~ \__fmwao_this_page: \space (second~ column)~
             and~ #1 widow~ on~ page~ \__fmwao_next_page: \space (first~ }
       }
       { on~ page~ \__fmwao_this_page: \space (second~ column)~
         and~ #1 widow~ on~ page~ \__fmwao_next_page: \space (first~ }
     column)
  }
  { Check~ out~ the~ page~ and~ see~ if~ you~ can~ avoid~ both~
    orphan~ and~ widow.}

\prop_new:N \l__fmwao_penalties_prop
\cs_new:Npn \__fmwao_initialize: {
  \prop_clear:N  \l__fmwao_penalties_prop
  \prop_put:Nnn \l__fmwao_penalties_prop {10000} {break~ at~ glue}
  \__fmwao_decide_penalty:Nn \@lowpenalty  { \@lowpenalty}
  \__fmwao_decide_penalty:Nn \@medpenalty  { \@medpenalty}
  \__fmwao_decide_penalty:Nn \@highpenalty { \@highpenalty}
  \__fmwao_decide_penalty:Nn \clubpenalty
    { \clubpenalty + \interlinepenalty }
  \__fmwao_decide_penalty:Nn \widowpenalty
    { \widowpenalty + \interlinepenalty ,
      \widowpenalty + \clubpenalty  + \interlinepenalty }

  \__fmwao_decide_penalty:Nn \displaywidowpenalty
    { \displaywidowpenalty + \interlinepenalty ,
      \displaywidowpenalty + \clubpenalty + \interlinepenalty }
  \__fmwao_decide_penalty:Nn \brokenpenalty
    { \brokenpenalty + \interlinepenalty ,
      \brokenpenalty + \clubpenalty + \interlinepenalty ,
      \brokenpenalty + \widowpenalty + \interlinepenalty ,
      \brokenpenalty + \widowpenalty + \clubpenalty + \interlinepenalty ,
      \brokenpenalty + \displaywidowpenalty + \clubpenalty
                                            + \interlinepenalty }
  \__fmwao_decide_penalty:Nn \predisplaypenalty { \predisplaypenalty }
}
\int_new:N  \l__fmwao_tmp_int
\tl_new:N   \l__fmwao_tmp_tl
\bool_new:N \l__fmwao_success_bool
\cs_new:Npn \__fmwao_decide_penalty:Nn #1 #2 {
  \bool_set_false:N \l__fmwao_success_bool
  \bool_do_until:Nn \l__fmwao_success_bool
    {
     \bool_set_true:N \l__fmwao_success_bool
     \clist_map_inline:nn { #2 }
       { \int_set:Nn \l__fmwao_tmp_int {##1}
         \prop_get:NVNT
            \l__fmwao_penalties_prop \l__fmwao_tmp_int \l__fmwao_tmp_tl
            { \clist_map_break:n {\bool_set_false:N\l__fmwao_success_bool} }
       }
     \bool_if:NTF \l__fmwao_success_bool
       { \clist_map_inline:nn { #2 }
           { \int_set:Nn \l__fmwao_tmp_int {##1}
             \prop_put:NVn \l__fmwao_penalties_prop \l__fmwao_tmp_int {##1}
   }
       }
       { \int_incr:N #1 }
     }
}
\AtBeginDocument { \__fmwao_initialize: }
\keys_define:nn {fmwao} {
  ,check .choice:
  ,check / error
     .code:n = \msg_redirect_module:nnn {widows-and-orphans}{warning}{error}
  ,check / info
     .code:n = \msg_redirect_module:nnn {widows-and-orphans}{warning}{info}
  ,check / none
     .code:n = \msg_redirect_module:nnn {widows-and-orphans}{warning}{none}
  ,check / warning
     .code:n = \msg_redirect_module:nnn {widows-and-orphans}{warning}{ }
  ,orphans .choice:
  ,orphans / prevent .code:n = \int_set:Nn \clubpenalty  { 10000        }
                               \int_set:Nn \@clubpenalty { \clubpenalty }
  ,orphans / avoid   .code:n = \int_set:Nn \clubpenalty  {  5000        }
                               \int_set:Nn \@clubpenalty { \clubpenalty }
  ,orphans / default .code:n = \int_set:Nn \clubpenalty  {   150        }
                               \int_set:Nn \@clubpenalty { \clubpenalty }
   % ====================================
  ,widows .choice:
  ,widows / prevent  .code:n = \int_set:Nn \widowpenalty { 10000 }
  ,widows / avoid    .code:n = \int_set:Nn \widowpenalty {  5000 }
  ,widows / default  .code:n = \int_set:Nn \widowpenalty {   150 }
   % ====================================
  ,hyphens .choice:
  ,hyphens / prevent .code:n = \int_set:Nn \brokenpenalty { 10000 }
  ,hyphens / avoid   .code:n = \int_set:Nn \brokenpenalty {  2000 }
  ,hyphens / default .code:n = \int_set:Nn \brokenpenalty {    50 }
   % ====================================
  ,prevent-all  .code:n = \int_set:Nn \clubpenalty        { 10000   }
                          \int_set:Nn \widowpenalty       { 10000   }
                          \int_set:Nn \displaywidowpenalty{ 10000   }
                          \int_set:Nn \brokenpenalty      { 10000   }
                          \int_set:Nn \predisplaypenalty  { 10000   }
                          \int_set:Nn \@clubpenalty  { \clubpenalty }
  ,avoid-all    .code:n = \int_set:Nn \clubpenalty         { 5000   }
                          \int_set:Nn \widowpenalty        { 5000   }
                          \int_set:Nn \displaywidowpenalty { 2000   }
                          \int_set:Nn \brokenpenalty       { 2000   }
                          \int_set:Nn \@clubpenalty  { \clubpenalty }
  ,default-all  .code:n = \int_set:Nn \clubpenalty          { 150   }
                          \int_set:Nn \widowpenalty         { 150   }
                          \int_set:Nn \displaywidowpenalty  {  50   }
                          \int_set:Nn \brokenpenalty        { 100   }
                          \int_set:Nn \predisplaypenalty  { 10000   }
                          \int_set:Nn \@clubpenalty  { \clubpenalty }
}
\ProcessKeysPackageOptions{fmwao}

\NewDocumentCommand\WaOsetup{m}
  { \keys_set:nn{fmwao}{#1}  \__fmwao_initialize: \ignorespaces }

\NewDocumentCommand\WaOparameters{}{\prop_show:N \l__fmwao_penalties_prop}
\NewDocumentCommand\WaOignorenext{}
  { \bool_gset_false:N\g__fmwao_gen_warn_bool }

\endinput
%%
%% End of file `widows-and-orphans.sty'.
