summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lua-widow-control/lua-widow-control.tex
blob: 4ff5bd993b4ef1b9f0f9db7958d877e59e04d7e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
% lua-widow-control
% https://github.com/gucci-on-fleek/lua-widow-control
% SPDX-License-Identifier: MPL-2.0+
% SPDX-FileCopyrightText: 2022 Max Chernoff

\wlog{lua-widow-control v2.0.4} %%version

\ifx\directlua\undefined
    \errmessage{%
        LuaTeX is required for this package.
        Make sure to compile with `luatex'%
    }
\fi

\input ltluatex % \LuaTeX{}Base

\clubpenalty=1
\widowpenalty=1
\displaywidowpenalty=1
\brokenpenalty=1

\newdimen\lwcemergencystretch
\lwcemergencystretch=3em

\newcount\lwcmaxcost
\lwcmaxcost=2147483647

\directlua{require "lua-widow-control"}

% Here, we enable font expansion/contraction. It isn't strictly necessary for
% \lwc/'s functionality; however, it is required for the
% lengthened paragraphs to not have terrible spacing.
\expandglyphsinfont\the\font 20 20 5
\adjustspacing=2

% Define \TeX{} wrappers for Lua functions
\def\lwcenable{\directlua{lwc.enable_callbacks()}}
\def\lwcdisable{\directlua{lwc.disable_callbacks()}}
\def\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.
\catcode`@=11

% We should only reenable \lwc/ at the end if it was already enabled.
\newif\iflwc@should@reenable

\def\lwc@patch@pre{%
    \iflwc%
        \lwc@should@reenabletrue%
        \lwcdisable%
    \else%
        \lwc@should@reenablefalse%
    \fi%
}

\def\lwc@patch@post{\iflwc@should@reenable%
    \lwcenable%
\fi}

\def\lwcdisablecmd#1{%
    \ifdefined#1
        \expandafter\def\expandafter#1\expandafter{\lwc@patch@pre #1\lwc@patch@post}
    \fi
}
\catcode`@=12

\begingroup
    \suppressoutererror=1
    \lwcdisablecmd{\beginsection} % Sectioning
\endgroup

\endinput