summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lua-widow-control/lua-widow-control.sty
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/generic/lua-widow-control/lua-widow-control.sty')
-rw-r--r--macros/luatex/generic/lua-widow-control/lua-widow-control.sty36
1 files changed, 34 insertions, 2 deletions
diff --git a/macros/luatex/generic/lua-widow-control/lua-widow-control.sty b/macros/luatex/generic/lua-widow-control/lua-widow-control.sty
index d5ef8194f1..ff3781944f 100644
--- a/macros/luatex/generic/lua-widow-control/lua-widow-control.sty
+++ b/macros/luatex/generic/lua-widow-control/lua-widow-control.sty
@@ -1,11 +1,11 @@
% lua-widow-control
% https://github.com/gucci-on-fleek/lua-widow-control
% SPDX-License-Identifier: MPL-2.0+
-% SPDX-FileCopyrightText: 2021 gucci-on-fleek
+% SPDX-FileCopyrightText: 2021 Max Chernoff
\NeedsTeXFormat{LaTeX2e}[2015/01/01] % Formats built after 2015 include \LuaTeX{}Base
\ProvidesPackage{lua-widow-control}%
- [2021/10/09 v1.0.0] %%version %%date
+ [2021/11/08 v1.1.0] %%version %%date
\setlength{\clubpenalty}{1}
\setlength{\widowpenalty}{1}
@@ -24,10 +24,42 @@
% lengthened paragraphs to not have terrible spacing.
\RequirePackage[final]{microtype}
+% Define \TeX{} wrappers for Lua functions
\newcommand{\lwcenable}{\directlua{lwc.enable_callbacks()}}
\newcommand{\lwcdisable}{\directlua{lwc.disable_callbacks()}}
+\newcommand{\iflwc}{\directlua{lwc.if_lwc_enabled()}}
% Enable \lwc/ by default when the package is loaded.
\lwcenable
+% Expansion of some parts of the document, such as section headings, is quite
+% undesirable, so we'll disable \lwc/ for certain commands.
+\RequirePackage{etoolbox}
+
+\newcommand{\lwc@patch@warning}[1]{\PackageWarning{lua-widow-control}{%
+ Patching the \protect#1 command failed%
+}}
+
+% We should only reenable \lwc/ at the end if it was already enabled.
+\newif\iflwc@should@reenable
+
+\newcommand{\lwc@patch@pre}{\iflwc%
+ \lwc@should@reenabletrue%
+ \lwcdisable%
+\fi}
+
+\newcommand{\lwc@patch@post}{\iflwc@should@reenable%
+ \lwcenable%
+\fi}
+
+\newcommand{\lwcdisablecmd}[1]{%
+ \ifdefined#1
+ \pretocmd{#1}{\lwc@patch@pre}{}{\lwc@patch@warning{#1}}%
+ \apptocmd{#1}{\lwc@patch@post}{}{\lwc@patch@warning{#1}}%
+ \fi
+}
+
+\lwcdisablecmd{\@sect} % Sectioning
+
+
\endinput