Debian Codegraph @

texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty

raw
1 % lua-widow-control
2 % https://github.com/gucci-on-fleek/lua-widow-control
3 % SPDX-License-Identifier: MPL-2.0+
4 % SPDX-FileCopyrightText: 2022 Max Chernoff
5
6 % Formats built after 2015 include \LuaTeX{}Base, so this is the absolute
7 % minimum version that we will run under.
8 \NeedsTeXFormat{LaTeX2e}[2015/01/01]
9
10 % For _really_ old formats
11 \providecommand\DeclareRelease[3]{}
12 \providecommand\DeclareCurrentRelease[2]{}
13
14 \DeclareRelease{}{0000-00-00}{lua-widow-control-2022-02-22.sty}
15 \DeclareRelease{v1.1.6}{2022-02-22}{lua-widow-control-2022-02-22.sty}
16 \DeclareCurrentRelease{v3.0.1}{2024-03-11} %%version %%dashdate
17
18 % If this version of LaTeX doesn't support command hooks, then we load
19 % the last v1.1.X version of the package.
20 \providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
21 \IfFormatAtLeastTF{2020/10/01}{}{\input{lua-widow-control-2022-02-22.sty}}
22 \IfFormatAtLeastTF{2020/10/01}{}{\endinput}
23
24 \ProvidesExplPackage
25 {lua-widow-control}
26 {2024/03/11} %%slashdate
27 {v3.0.1} %%version
28 {Use Lua to remove widows and orphans}
29
30 % Message and String Constants
31 \str_const:Nn \c__lwc_name_str { lua-widow-control }
32
33 \msg_new:nnn
34 { \c__lwc_name_str }
35 { no-luatex }
36 {
37 LuaTeX~ is~ REQUIRED! \\
38 Make~ sure~ to~ compile~ your~ document~ with~ `lualatex'.
39 }
40
41 \msg_new:nnn
42 { \c__lwc_name_str }
43 { patch-failed }
44 {
45 Patching~ \c_backslash_str #1~ failed. \\
46 Please~ ensure~ that~ \c_backslash_str #1~ exists.
47 }
48
49 \msg_new:nnn
50 { \c__lwc_name_str }
51 { old-format-patch }
52 {
53 Patching~ not~ supported~ with~ old~ LaTeX. \\
54 Please~ use~ a~ LaTeX~ format~ >=~ 2021/06/01.
55 }
56
57 \msg_new:nnn
58 { \c__lwc_name_str }
59 { old-command }
60 {
61 \c_backslash_str #1~ has~ been~ REMOVED! \\
62 Please~ use~ \c_backslash_str setuplwc \c_left_brace_str #2
63 \c_right_brace_str\ instead.
64 }
65
66 % Don't let the user proceed unless they are using \LuaTeX{}.
67 \sys_if_engine_luatex:F {
68 \msg_critical:nn { \c__lwc_name_str } { no-luatex }
69 }
70
71 % Define (most of) the keys
72 \cs_generate_variant:Nn \keys_define:nn { Vn }
73
74 \keys_define:Vn { \c__lwc_name_str } {
75 emergencystretch .dim_gset:N = \g__lwc_emergencystretch_dim,
76 emergencystretch .value_required:n = true,
77 emergencystretch .initial:x = \dim_max:nn { 3em } { 30pt },
78
79 draftoffset .dim_gset:N = \g__lwc_draftoffset_dim,
80 draftoffset .value_required:n = true,
81 draftoffset .initial:x = 1in,
82
83 max-cost .int_gset:N = \g__lwc_maxcost_int,
84 max-cost .value_required:n = true,
85 max-cost .initial:x = \c_max_int,
86
87 widowpenalty .code:n = \int_gset:Nn \tex_widowpenalty:D { #1 }
88 \int_gset:Nn \tex_displaywidowpenalty:D { #1 },
89 widowpenalty .value_required:n = true,
90 widowpenalty .initial:n = 1,
91
92 orphanpenalty .code:n = \int_gset:Nn \tex_clubpenalty:D { #1 }
93 \int_gset:Nn \@clubpenalty { #1 },
94 orphanpenalty .value_required:n = true,
95 orphanpenalty .initial:n = 1,
96
97 brokenpenalty .int_gset:N = \tex_brokenpenalty:D,
98 brokenpenalty .value_required:n = true,
99 brokenpenalty .initial:n = 1,
100
101 microtype .bool_gset:N = \g__lwc_use_microtype_bool,
102 microtype .value_required:n = true,
103 microtype .initial:n = true,
104 microtype .usage:n = preamble,
105
106 disablecmds .clist_gset:N = \g__lwc_disablecmds_cl,
107 disablecmds .value_required:n = false,
108 disablecmds .initial:n = { \@sect, % LaTeX default
109 \@ssect, % LaTeX starred
110 \M@sect, % Memoir
111 \@mem@old@ssect, % Memoir Starred
112 \ttl@straight@ii, % titlesec normal
113 \ttl@top@ii, % titlesec top
114 \ttl@part@ii, % titlesec part
115 },
116 disablecmds .usage:n = preamble,
117 }
118
119 % Load the Lua code
120 \cs_if_exist:NTF \lua_load_module:n {
121 \lua_load_module:n { lua-widow-control }
122 } {
123 \lua_now:n { require "lua-widow-control" }
124 }
125
126 % Here, we enable font expansion/contraction. It isn't strictly necessary for
127 % \lwc/'s functionality; however, it is required for the
128 % lengthened paragraphs to not have terrible spacing.
129 \hook_gput_code:nnn { begindocument / before } { \c__lwc_name_str } {
130 \bool_if:NT \g__lwc_use_microtype_bool {
131 \@ifpackageloaded { microtype } {} {
132 \RequirePackage[
133 final,
134 activate = { true, nocompatibility }
135 ]
136 { microtype }
137 }
138 }
139 }
140
141 % Core Function Definitions
142 \cs_new_eq:NN \iflwc \__lwc_iflwc:
143
144 \prg_new_conditional:Nnn \__lwc_if_enabled: { T, F, TF } {
145 \__lwc_if_enabled:
146 \prg_return_true:
147 \else
148 \prg_return_false:
149 \fi
150 }
151
152 \prg_new_conditional:Nnn \__lwc_if_lmtx: { T, F, TF } {
153 \int_compare:nNnTF { \tex_luatexversion:D } > { 200 } {
154 \prg_return_true:
155 } {
156 \prg_return_false:
157 }
158 }
159
160 % Expansion of some parts of the document, such as section headings, is quite
161 % undesirable, so we'll disable \lwc/ for certain commands.
162 \int_new:N \g__lwc_disable_int
163
164 \cs_new:Npn \__lwc_patch_pre: {
165 % We should only reenable \lwc/ at the end if it was already enabled.
166 \__lwc_if_enabled:T {
167 \int_gincr:N \g__lwc_disable_int
168 \__lwc_disable:
169 }
170 }
171
172 \cs_new:Npn \__lwc_patch_post: {
173 \int_compare:nT { \g__lwc_disable_int > 0 } {
174 \__lwc_enable:
175 \int_gdecr:N \g__lwc_disable_int
176 }
177 }
178
179 \cs_new:Npn \__lwc_patch_cmd:c #1 {
180 \IfFormatAtLeastTF { 2021/06/01 } {
181 \hook_gput_code:nnn { cmd / #1 / before } { \c__lwc_name_str } {
182 \__lwc_patch_pre:
183 }
184 \hook_gput_code:nnn { cmd / #1 / after } { \c__lwc_name_str } {
185 \__lwc_patch_post:
186 }
187 } {
188 \msg_warning:nn
189 { \c__lwc_name_str }
190 { old-format-patch }
191 }
192 }
193
194 \cs_new:Npn \__lwc_patch_cmd:N #1 {
195 \__lwc_patch_cmd:c { \cs_to_str:N #1 }
196 }
197
198 \cs_new:Npn \__lwc_patch_cmd:n #1 {
199 % If the item provided is a single token, we'll assume that it's a \macro.
200 % If it is multiple tokens, we'll assume that it's a `csname`.
201 \tl_if_single:nTF { #1 } {
202 \__lwc_patch_cmd:c { \cs_to_str:N #1 }
203 } {
204 \__lwc_patch_cmd:c { #1 }
205 }
206 }
207
208 \hook_gput_code:nnn { begindocument / before } { \c__lwc_name_str } {
209 \clist_map_function:NN \g__lwc_disablecmds_cl \__lwc_patch_cmd:n
210 }
211
212 \__lwc_if_lmtx:T {
213 \int_gset:Nn \normalizelinemode {
214 \numexpression\normalizelinemode bor 2\relax
215 }
216 }
217
218 %%% Class and package-specifc patches
219
220 % KOMA-Script
221 \cs_if_exist:NT \AddtoDoHook {
222 \AddtoDoHook { heading / begingroup } { \__lwc_patch_pre: \use_none:n }
223 \AddtoDoHook { heading / endgroup } { \__lwc_patch_post: \use_none:n }
224 }
225
226 % Memoir
227 \cs_gset_nopar:Npn \pen@ltyabovepfbreak { 23 } % Issue #32
228
229 % Define some final keys
230 \keys_define:Vn { \c__lwc_name_str } {
231 enable .choice:,
232 enable / true .code:n = \__lwc_enable:,
233 enable / false .code:n = \__lwc_disable:,
234 enable .initial:n = true,
235 enable .default:n = true,
236 enable .value_required:n = false,
237
238 disable .code:n = \__lwc_disable:,
239 disable .value_forbidden:n = true,
240
241 debug .choice:,
242 debug / true .code:n = \__lwc_debug:n { true },
243 debug / false .code:n = \__lwc_debug:n { false },
244 debug .default:n = true,
245 debug .value_required:n = false,
246
247 showcolours .choice:,
248 showcolours / true .code:n = \__lwc_show_colours:n { true },
249 showcolours / false .code:n = \__lwc_show_colours:n { false },
250 showcolours .default:n = true,
251 showcolours .value_required:n = false,
252
253 showcosts .choice:,
254 showcosts / true .code:n = \__lwc_show_costs:n { true },
255 showcosts / false .code:n = \__lwc_show_costs:n { false },
256 showcosts .default:n = true,
257 showcosts .value_required:n = false,
258
259 draft .meta:n = {
260 showcolours = { #1 },
261 showcosts = { #1 },
262 },
263 draft .default:n = true,
264 draft .value_required:n = false,
265
266 nobreak .code:n = \__lwc_nobreak:n { #1 },
267 nobreak .value_required:n = true,
268 nobreak .initial:n = keep,
269
270 strict .meta:n = { emergencystretch = 0pt,
271 max-cost = 5000,
272 nobreak = warn,
273 widowpenalty = 1,
274 orphanpenalty = 1,
275 brokenpenalty = 1,
276 },
277 strict .value_forbidden:n = true,
278
279 default .meta:n = { emergencystretch = 3em,
280 max-cost = \c_max_int,
281 nobreak = keep,
282 widowpenalty = 1,
283 orphanpenalty = 1,
284 brokenpenalty = 1,
285 },
286 default .value_forbidden:n = true,
287
288 balanced .meta:n = { emergencystretch = 1em,
289 max-cost = 10000,
290 nobreak = keep,
291 widowpenalty = 500,
292 orphanpenalty = 500,
293 brokenpenalty = 500,
294 },
295 balanced .value_forbidden:n = true,
296 }
297
298 % Add the user interface for the keys
299 \IfFormatAtLeastTF { 2022-06-01 } {
300 \ProcessKeyOptions [ \c__lwc_name_str ]
301 }{
302 \RequirePackage { l3keys2e }
303 \exp_args:NV \ProcessKeysOptions { \c__lwc_name_str }
304 }
305
306 \cs_generate_variant:Nn \keys_set:nn { Vn }
307 \NewDocumentCommand \lwcsetup {m} {
308 \keys_set:Vn { \c__lwc_name_str }{ #1 }
309 }
310
311 % Legacy Commands
312 \NewDocumentCommand \lwcemergencystretch { } {
313 \msg_error:nnnnn
314 { \c__lwc_name_str }
315 { old-command }
316 { lwcemergencystretch }
317 { emergencystretch=XXXpt }
318 }
319
320 \NewDocumentCommand \lwcdisablecmd { m } {
321 \msg_error:nnxx
322 { \c__lwc_name_str }
323 { old-command }
324 { lwcdisablecmd }
325 { disablecmds={\c_backslash_str aaa,~ \c_backslash_str bbb} }
326 }
327
328 \cs_new_eq:NN \lwcenable \__lwc_enable:
329 \cs_new_eq:NN \lwcdisable \__lwc_disable:
330
331 \endinput