summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl
blob: 716e45dcf5ff3b2763b7e82c1f91fb1f80616142 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
%D \module
%D   [     file=t-lua-widow-control,
%D      version=2.0.1, %%version
%D        title=lua-widow-control,
%D     subtitle=\ConTeXt module for lua-widow-control,
%D       author=Max Chernoff,
%D         date=2022-03-18, %%dashdate
%D    copyright=Max Chernoff,
%D      license=MPL-2.0+,
%D          url=https://github.com/gucci-on-fleek/lua-widow-control]
\startmodule[lua-widow-control]
\unprotect

\installnamespace{lwc}

\installcommandhandler \????lwc {lwc} \????lwc

\newdimen\lwc_emergency_stretch
\appendtoks
    \lwc_emergency_stretch=\lwcparameter{emergencystretch}
\to\everysetuplwc

\appendtoks
    \doifelse{\lwcparameter{\c!state}}\v!start{
        \ctxlua{lwc.enable_callbacks()}
    }{
        \ctxlua{lwc.disable_callbacks()}
    }
\to\everysetuplwc

\appendtoks
    \doifelse{\lwcparameter{debug}}\v!start{
        \ctxlua{lwc.debug = true}
    }{
        \ctxlua{lwc.debug = false}
    }
\to\everysetuplwc

\appendtoks
    \ctxlua{lwc.nobreak_behaviour = "\lwcparameter{nobreak}"}
\to\everysetuplwc

\newcount\lwc_max_cost
\appendtoks
    \lwc_max_cost=\lwcparameter{maxcost}
\to\everysetuplwc

\appendtoks
    % We can't just set the penalties because they will be reset automatically
    % at \\starttext.
    \startsetups[*default]
        \clubpenalty=\lwcparameter{orphanpenalty}
        \widowpenalty=\lwcparameter{widowpenalty}
        \displaywidowpenalty=\lwcparameter{widowpenalty}
        \brokenpenalty=\lwcparameter{brokenpenalty}
    \stopsetups

    \setups[*default]
\to\everysetuplwc

\define\iflwc{\ctxlua{lwc.if_lwc_enabled()}}

\ctxloadluafile{lua-widow-control}

\setuplwc[
    emergencystretch=3em,
    \c!state=\v!start,
    debug=\v!stop,
    orphanpenalty=1,
    widowpenalty=1,
    brokenpenalty=1,
    nobreak=keep,
    maxcost=2147483647,
]


% 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.
\definefontfeature[default][default][expansion=quality]
\setupalign[hz]

% Expansion of some parts of the document, such as section headings, is quite
% undesirable, so we'll disable \lwc/ for certain commands.
% We should only reenable \lwc/ at the end if it was already enabled.
\newif\iflwc_should_reenable

\define\lwc_patch_pre{%
    \iflwc%
        \lwc_should_reenabletrue%
        \setuplwc[state=stop]%
    \else%
        \lwc_should_reenablefalse
    \fi%
}

\define\lwc_patch_post{\iflwc_should_reenable%
    \setuplwc[state=start]%
\fi}

\prependtoks\lwc_patch_pre\to\everybeforesectionheadhandle % Sectioning
\prependtoks\lwc_patch_post\to\everyaftersectionheadhandle

\protect
\stopmodule