summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/l3experimental/xgalley/l3galley.dtx
blob: e2407f043a798b5e90be98ec0bf460d03ec92c59 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
% \iffalse
%
%% File: l3galley.dtx Copyright (C) 1999-2001, 2004-2009 Frank Mittelbach
%%                              (C) 2010-2012 The LaTeX3 Project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "l3experimental bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%%
%
%<*driver|package>
\RequirePackage{xparse}
\GetIdInfo$Id: l3galley.dtx 4388 2012-12-20 22:15:35Z joseph $
  {L3 Experimental galley code}
%</driver|package>
%<*driver>
\documentclass[full]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{l3galley} package\\ Galley code^^A
%   \thanks{This file describes v\ExplFileVersion,
%     last revised \ExplFileDate.}^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released \ExplFileDate}
%
% \maketitle
%
% \begin{documentation}
%
% \section{Introduction}
%
% In \LaTeX3 terminology a galley is a rectangular area which receives
% text and other material filling it from top. The vertically extend of
% a galley is normally not restricted: instead certain chunks are taken
% off the top of an already partially filled galley to form columns or
% similar areas on a page. This process is typically asynchronous but
% there are ways to control or change its behaviour.
%
% Examples for galleys are \enquote{the main galley}, where the
% continuous document data gets formatted into  and from which columns
% and pages are constructed, and \enquote{vertical box galleys}, such
% as the body of a minipage environment. The latter galleys are
% typically not split after formatting, though there can be exceptions.
%
% \section{Formatting layers}
%
% The present module is mainly concerned with the formatting of text
% in galleys. The mechanism by which this is achieved uses four
% (somewhat) distinct layers, some of which can be addressed using the
% templates provided here.
%
% \subsection{Layer one: external dimensions}
%
% The bottom layer of the system is the external dimensions of the
% galley. Normally only the horizontal dimension is fixed externally,
% while the vertical (filling) dimension is unspecified. The external
% dimensions are fixed when starting a new galley, and are therefore
% not modifiable within the galley.
%
% There are no templates for setting this layer directly, although the
% external values are influenced by other parts of the system (for
% example when creating minipage environments).
%
% \subsection{Layer two: internal dimensions}
%
% The second layer is the internal dimensions of the galley: the
% \emph{measure} used for paragraph text and the position of the
% paragraph relative to the edges of the galley.
%
% This layer is normally accessed by higher-level templates
% \emph{via} the object type \texttt{measure}. Changes made using
% level two templates will often extend for large parts of a document
% (up to and including the entire document).
%
% \subsection{Layer three: paragraph shape}
%
% The third layer defines the paragraph shape within the measure as
% provided by the second layer. In the absence of any specification
% for that layer the paragraph shape used will be that of a
% rectangular area of the width of the current measure.
%
% There are some restrictions imposed on the shape of a paragraph by the
% underlying \TeX{} mechanisms. For example, cut out sections in
% paragraphs can be specified from the top of the paragraph but not from
% the bottom.
%
% \subsection{Layer four: formatting inside the paragraph}
%
% The forth layer deals with the paragraph formatting aspects such as
% hyphenation and justification within the paragraph (this is sometimes
% referred to as \enquote{\texttt{h\&j}} or \enquote{\texttt{hj}}). This
% layer is somewhat distinct from the galley as such, but is handled in
% the same place as there is, internally, interaction between the different
% layers.
%
% \section{Code interfaces}
%
% \subsection{Galley layers}
%
% \begin{variable}{\l_galley_width_dim}
%   The total width of a galley, set either by the page geometry code
%   for the main vertical galley or when creating an independent galley,
%   such as a minipage.
% \end{variable}
%
% \begin{function}{\galley_level:}
%   \begin{syntax}
%     \cs{galley_level:}
%   \end{syntax}
%   Sets up a vertical box to contain a new galley level. The box should
%   be \enquote{color safe}, which is automatic for \LaTeX3 coffins but
%   must be included manually (using \cs{color_group_begin:} and
%   \cs{color_group_end:}) in \enquote{raw} vertical boxes.
% \end{function}
%
% \subsection{Measure}
%
% \begin{function}
%   {\galley_margins_set_absolute:nn, \galley_margins_set_relative:nn}
%   \begin{syntax}
%     \cs{galley_margins_set_absolute:nn} \Arg{left margin} \Arg{right margin}
%     \cs{galley_margins_set_relative:nn} \Arg{left margin} \Arg{right margin}
%   \end{syntax}
%   Sets the width of the measure to have the \meta{left margin} and
%   \meta{right margin} specified by the arguments, both of which are
%   \meta{dimension expressions}. The \texttt{relative} function will adjust
%   the text width within any existing margins, whereas the \texttt{absolute}
%   measure sets the margins based on the edges of the galley only. One or
%   both of the \meta{margins} may be negative, to specify and outdent.
% \end{function}
%
% \subsection{Between paragraphs}
% 
% \begin{variable}{\g_galley_previous_par_lines_int}
%   The number of lines in the preceding conceptual paragraph. This may not
%   correspond to the \TeX{} \tn{prevgraf} primitive value as one conceptual
%   paragraph may contain several \TeX{} \tn{par} primitive tokens.
% \end{variable}
%
% \begin{variable}{\g_galley_restore_running_tl}
%   When galley settings need to be reset at the end of a paragraph, the
%   appropriate detail should be added to this token list. It is inserted
%   immediately before the start of each paragraph, and can therefore be
%   used to clear otherwise global settings. The token list itself is
%   also cleared as part of this process.
% \end{variable}
%
% \begin{variable}{\g_galley_no_break_next_bool}
%   Indicates that no page break should be allowed between the current
%   paragraph and the next paragraph.
% \end{variable}
%
% \begin{function}{\g_galley_omit_next_indent_bool}
%   Indicates that the indent should be omitted from the start of the next
%   paragraph started.
% \end{function}
%
% \begin{variable}{\l_galley_interpar_penalty_int}
%   The \meta{penalty} for a break between paragraphs. The \meta{penalty}
%   should be in the range $-10\,000$ to $10\,000$, where $-10\,000$ forces a
%   page break, $0$ has no effect at all and  $10\,000$ forbids a page break.
%   Note that setting \cs{g_galley_no_break_next_bool} to \texttt{true}
%   will override any setting of \cs{l_galley_interpar_penalty_int}.
% \end{variable}
%
% \begin{variable}{\l_galley_interpar_vspace_skip}
%   Stretchable space to be inserted between paragraphs, set at the design
%   or template level.
% \end{variable}
%
% \begin{function}{\galley_penalty_set_single:n}
%   \begin{syntax}
%     \cs{galley_penalty_set_single:n} \Arg{penalty}
%   \end{syntax}
%   Sets the \meta{penalty} for a break between the current and next
%   paragraph on a one-off basis. This function is intended for user-level
%   adjustments to design, and takes precedent over both settings from
%   \cs{l_galley_interpar_penalty_int} and from \cs{galley_no_break_next:}.
% \end{function}
%
% \begin{function}{\galley_vspace_set_single:n}
%   \begin{syntax}
%     \cs{galley_vspace_set_single:n} \Arg{space}
%   \end{syntax}
%   Sets the \meta{space} to be inserted between the current and next
%   paragraph on a one-off basis. This function is intended for user-level
%   adjustments to design, and otherwise is analogous to
%   \cs{galley_set_vspace:n}.
% \end{function}
%
% \subsection{Paragraph shape}
%
% \begin{function}
%   {
%     \galley_parshape_set_multi:nnnN,
%     \galley_parshape_set_multi:nVVN,
%     \galley_parshape_set_single:nnnN,
%     \galley_parshape_set_single:nVVN
%   }
%   \begin{syntax}
%     \cs{galley_parshape_set_multi:nnnN} \Arg{unaltered lines} \Arg{left indents} \Arg{right indents} \meta{resume flag}
%     \cs{galley_parshape_set_single:nnnN} \Arg{unaltered lines} \Arg{left indents} \Arg{right indents} \meta{resume flag}
%   \end{syntax}
%   Sets the current paragraph shape to create an arbitrary paragraph shape.
%   The paragraph shape is set such that there are \meta{unaltered lines} which
%   have width and indent as set by the measure. The \enquote{altered} lines
%   are then defined by the comma-separated lists of \meta{left indents} and
%   \meta{right indents}. These are both indents from the edge of the measure,
%   and may be negative, and should both contain the same number of items.
%   If the \meta{resume flag} is \texttt{true}, after the last
%   altered line the paragraph shape returns to that of the measure. On the
%   other hand, if the flag is \texttt{false} then the shape of the last line
%   is retained for the rest of the paragraph.
%   For example,
%   \begin{verbatim}
%     \galley_parshape_set_multi_par:nnnN { 1 }
%       { 2 pt , 4 pt , 6 pt } { 2 pt , 4 pt , 6 pt } \c_true_bool
%   \end{verbatim}
%   would create a paragraph shape in which the first line is the full
%   width of the measure, the second line is indented by $2$\,pt on each side,
%   the third line by by $4$\,pt and the fourth line and subsequent lines by
%   $6$\,pt from the edge of the measure on each side.
%
%   The \texttt{single_par} version applies only to a single paragraph,
%   while the \texttt{multi_par} function sets the paragraph shape on an
%   ongoing basis within the scope of the current \TeX{} group.
% \end{function}
%
% \begin{function}
%   {
%     \galley_cutout_left:nn,
%     \galley_cutout_right:nn
%   }
%   \begin{syntax}
%     \cs{galley_cutout_left:nn} \Arg{unaltered lines} \Arg{indents}
%     \cs{galley_cutout_right:nn} \Arg{unaltered lines} \Arg{indents}
%   \end{syntax}
%   Adds a cutout section to the active paragraph shape, leaving
%   \meta{unaltered lines} unchanged and then applying the \meta{indents}
%   (a comma list). The cutout will be places on the left or right as indicated
%   by the function name, and will apply to exactly the number of lines
%   specified (the total of the \meta{unaltered lines} and the number of
%   entries in the \meta{indents} list). Several cutouts may be applied
%   sequentially: these act in an additive sense.
% \end{function}
%
% \subsection{Formatting inside the paragraph}
%
% The settings described here apply \enquote{inside} the paragraph, and so
% are active irrespective of any paragraph shape within the measure.
%
% \begin{variable}{\l_galley_line_left_skip, \l_galley_line_right_skip}
%   Stretchable space added to the appropriate side each line in a paragraph.
% \end{variable}
%
% \begin{variable}{\l_galley_par_begin_skip, \l_galley_par_end_skip}
%   Stretchable space added to the beginning of the first line and end of
%   the last line of a paragraph, respectively.
% \end{variable}
%
% \begin{variable}{\l_galley_par_indent_dim}
%   Fixed space added to the start of each paragraph except for those where
%   \cs{g_galley_omit_next_indent_bool} is \texttt{true}.
% \end{variable}
%
% \begin{variable}{\l_galley_last_line_fit_int}
%   Determines how the inter-word stretch is set for the last line of a
%   paragraph when
%   \begin{enumerate}
%     \item the value of \cs{l_galley_par_end_skip} contains an infinite
%       (\texttt{fil(l)}) component;
%     \item the values of \cs{l_galley_line_left_skip} and
%       \cs{l_galley_line_right_skip} do \emph{not} contain an infinite
%       (\texttt{fil(l)}) component.
%   \end{enumerate}
%   Under these circumstances, \cs{l_galley_last_line_fit_int} is active, and
%   applies as follows:
%   \begin{itemize}
%     \item when set to $0$, the last line of the paragraph is set with
%       the inter-word spacing at natural width;
%    \item when set to a $1000$ (or above), the inter-word spacing in the last
%      line is stretched by the same factor as that applied to the penultimate
%      line;
%    \item when set to~$n$ between these extremes, the inter-word spacing in
%      the last line is stretched by $n/1000$ times the factor used for the
%      penultimate line.
%   \end{itemize}
% \end{variable}
%
% \begin{function}{\galley_interword_spacing_set:N}
%   \begin{syntax}
%     \cs{galley_interword_spacing_set:N} \meta{fixed spacing bool}
%   \end{syntax}
%   Sets the inter-word spacing used based on the values supplied by the
%   current font. If the \meta{fixed spacing bool} flag is \texttt{true} then
%   no stretch is permitted between words, otherwise the stretch specified by
%   the font designer is used.
% \end{function}
%
% \subsection{Display material}
%
% Material which is set in \enquote{display-style} require additional settings
% to control the relationship with the surrounding material.
%
% \begin{function}{\galley_display_begin:, \galley_display_end:}
%   \begin{syntax}
%     \cs{galley_display_begin:}
%       \ldots
%     \cs{galley_display_end:}
%   \end{syntax}
%   Sets up a group to contain display-style material. Unlike an independent
%   galley level, settings are inherited from the surroundings. However,
%   the interaction of a display block with the paragraphs before and after it
%   can be adjusted independent of the design of text.
% \end{function}
% 
% \subsection{Hyphenation}
%
% \begin{variable}{\l_galley_hyphen_left_int}
%   THIS IS A HACK: SEE CODE!
% \end{variable}
%
% \subsection{Line breaking}
%
% \begin{variable}{\l_galley_binop_penalty_int}
%   Penalty charged if an inline math formula is broken at a
%   binary operator.
% \end{variable}
%
% \begin{variable}{\l_galley_double_hyphen_demerits_int}
%   Extra demerit charge of two (or more) lines in succession end
%   in a hyphen.
% \end{variable}
%
% \begin{variable}{\l_galley_emergency_stretch_skip}
%   Additional stretch assumed for each line if no better line breaking
%   can be found without it. This stretch is not actually added to lines,
%   so its use may result in underfull box warnings.
% \end{variable}
%
% \begin{variable}{\l_galley_final_hyphen_demerits_int}
%   Extra demerit charge if the second last line is hyphenated.
% \end{variable}
%
% \begin{variable}{\l_galley_linebreak_badness_int}
%   Boundary that if exceeded will cause \TeX{} to report an
%   underfull line.
% \end{variable}
%
% \begin{variable}{\l_galley_linebreak_fuzz_dim}
%   Boundary below which overfull lines are not reported.
% \end{variable}
%
% \begin{variable}{\l_galley_linebreak_penalty_int}
%   Extra penalty charged per line in the paragraph. By making
%   this penalty higher \TeX{} will try harder to produce compact
%   paragraphs.
% \end{variable}
%
% \begin{variable}{\l_galley_linebreak_pretolerance_int}
%   Maximum tolerance allowed for individual lines to break the
%   paragraph without attempting hyphenation.
% \end{variable}
%
% \begin{variable}{\l_galley_linebreak_tolerance_int}
%   Maximum tolerance allowed for individual lines when breaking a
%   paragraph while attempting hyphenation (if this limit can't be
%   met \cs{l_galley_emergency_stretch_skip} comes into play).
% \end{variable}
%
% \begin{variable}{\l_galley_mismatch_demerits_int}
%   Extra demerit charge if two visually incompatible lines follow
%   each other.
% \end{variable}
%
% \begin{variable}{\l_galley_relation_penalty_int}
%   Penalty charged if an inline math formula is broken at a
%   relational symbol.
% \end{variable}
%
% \begin{function}{\galley_break_line:Nn}
%   \begin{syntax}
%     \cs{galley_break_line:Nn} \meta{boolean} \Arg{dimexpr}
%   \end{syntax}
%   Breaks the current line, filling the remaining space with \texttt{fil}
%   glue. If the \meta{boolean} is \texttt{true} then a page break is possible
%   after the broken line. Vertical space as given by the \meta{dimexpr} will
%   be inserted between the broken line and the next line.
% \end{function}
%
% \subsection{Paragraph breaking}
%
% \begin{variable}{\l_galley_parbreak_badness_int}
%   Boundary that if exceeded will cause \TeX{} to report an
%   underfull vertical box.
% \end{variable}
%
% \begin{variable}{\l_galley_parbreak_fuzz_dim}
%   Boundary below which overfull vertical boxes are not reported.
% \end{variable}
%
% \begin{variable}{\l_galley_broken_penalty_int}
%   Penalty for page breaking after a hyphenated line.
% \end{variable}
%
% \begin{variable}{\l_galley_pre_display_penalty_int}
%   Penalty for breaking between immediately before display math material.
% \end{variable}
%
% \begin{variable}{\l_galley_post_display_penalty_int}
%   Penalty for breaking between immediately after display math material.
% \end{variable}
%
% \begin{function}
%   {
%     \galley_club_penalties_set:n,
%     \galley_club_penalties_set:V,
%     \galley_club_penalties_set:v,
%     \galley_display_club_penalties_set:n,
%     \galley_display_club_penalties_set:V,
%     \galley_display_club_penalties_set:v,
%     \galley_display_widow_penalties_set:n,
%     \galley_display_widow_penalties_set:V,
%     \galley_display_widow_penalties_set:v,
%     \galley_widow_penalties_set:n,
%     \galley_widow_penalties_set:V,
%     \galley_widow_penalties_set:v
%   }
%   \begin{syntax}
%     \cs{galley_club_penalties_set:n} \Arg{penalty list}
%   \end{syntax}
%   Set the penalties for breaking lines at the beginning and end of
%   (partial) paragraphs. In each case, the \meta{penalty list} is a
%   comma-separated list of penalty values. The list applies as follows:
%   \begin{itemize}
%     \item[\texttt{club}] Penalties for breaking after the first, second,
%       third, \emph{etc.}~line of the paragraph.
%     \item[\texttt{display_club}] Penalties for breaking after the first,
%       second, third, \emph{etc.}~line after a display math environment.
%     \item[\texttt{display_club}] Penalties for breaking before the last,
%       penultimate, antepenultimate, \emph{etc.}~line before a display
%       math environment.
%     \item[\texttt{widow}] Penalties for breaking before the last,
%       penultimate, antepenultimate, \emph{etc.}~line of the paragraph.
%   \end{itemize}
%   In all cases, these penalties apply in addition to the general interline
%   penalty or to any \enquote{special} line penalties.
% \end{function}
%
% \begin{function}{\galley_interline_penalty_set:n}
%   \begin{syntax}
%     \cs{galley_interline_penalty_set:n} \Arg{penalty}
%   \end{syntax}
%   Sets the standard interline penalty applied between lines of a paragraph.
%   This value is added to any (display) club or widow penalty in force.
% \end{function}
%
% \begin{function}
%   {
%     \galley_interline_penalties_set:n,
%     \galley_interline_penalties_set:V
%   }
%   \begin{syntax}
%     \cs{galley_interline_penalties_set:n} \Arg{penalty list}
%   \end{syntax}
%   Sets \enquote{special} interline penalties to be used in place of
%   the standard value, specified as a comma-separated \meta{penalty list}.
%   The \meta{penalties} apply to the first, second, third, \emph{etc.}~line
%   of the paragraph.
% \end{function}
%
% \begin{function}
%   {
%     \galley_save_club_penalties:N,
%     \galley_save_display_club_penalties:N,
%     \galley_save_display_widow_penalties:N,
%     \galley_save_interline_penalties:N,
%     \galley_save_widow_penalties:N
%   }
%   \begin{syntax}
%     \cs{galley_save_club_penalties:N} \Arg{comma list}
%   \end{syntax}
%   These functions save the current value of the appropriate penalty to the
%   comma list specified, within the current \TeX{} group.
% \end{function}
%
% \begin{function}[EXP]{\galley_interline_penalty:}
%   \begin{syntax}
%     \cs{galley_interline_penalty:}
%   \end{syntax}
%   Expands to the current interline penalty as a \meta{integer denotation}.
% \end{function}
%
% \section{Hooks and insertion points}
%
% \begin{variable}{\g_galley_par_begin_hook_tl}
%   Token list inserted at the beginning of every paragraph in horizontal mode.
%   This is inserted after any paragraph indent but before any other horizontal
%   mode material.
% \end{variable}
%
% \begin{variable}{\g_galley_par_end_hook_tl}
%   Token list inserted at the end of every paragraph in horizontal mode.
% \end{variable}
%
% \begin{variable}{\g_galley_par_reset_hook_tl}
%   Token list inserted after each paragraph. This is used for resetting
%   galley parameters, and is therefore cleared after use. It is inserted
%   in vertical mode and must not contain horizontal mode material.
% \end{variable}
%
% \begin{variable}{\g_galley_whatsit_next_tl}
%   Token list for whatsits to be inserted at the very beginning of the next
%   paragraph started.
% \end{variable}
%
% \begin{variable}{\g_galley_whatsit_previous_tl}
%   Token list for whatsits to be inserted at the very end of the last
%   paragraph started.
% \end{variable}
%
% \section{Paragraphs}
% 
% \begin{function}{\galley_par:}
%   \begin{syntax}
%     \cs{galley_par:}
%   \end{syntax}
%   Finalises the material collected as the last paragraph, inserts tokens
%   at the start of the new paragraph, setting paragraph shape and any
%   special values as required.
% \end{function}
%
% \begin{function}{\galley_par:n}
%   \begin{syntax}
%     \cs{galley_par:n} \Arg{tokens}
%   \end{syntax}
%   Adds the \meta{tokens} to the material collected for the last paragraph
%   before finalising it in the usual way. This function should
%   therefore be the \emph{first} non-expandable entry used when a function
%   needs to add tokens to the preceding paragraph.
% \end{function}
%
% \section{Information variables}
%
% Some of the variables for the galley mechanism may be of
% interest to the programmer. These should all be treated as read-only
% values and accessed only through the defined interfaces described above.
%
% \begin{variable}{\l_galley_total_left_margin_dim}
%   The total margin between the left side of the galley and the left side of
%   the text block. This may be negative if the measure is set to overlap
%   the text beyond the edge of the galley.
% \end{variable}
%
% \begin{variable}{\l_galley_total_right_margin_dim}
%   The total margin between the right side of the galley and the right side
%   of the text block. This may be negative if the measure is set to overlap
%   the text beyond the edge of the galley.
% \end{variable}
%
% \begin{variable}{\l_galley_text_width_dim}
%   The width of a line of text within the galley, taking account of
%   any margins added. This may be larger than \cs{l_galley_width_dim}
%   if the margins are negative.
% \end{variable}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3galley} Implementation}
%
% At the implementation level, there are a number of challenges which
% have to be overcome in order to make the galley easy to use at the
% designer and user levels. Inserting material into the main vertical
% list is in many ways an irreversible operation. Inserting items as
% they appear in the source is therefore not desirable. Instead,
% inserting vertical-mode material needs to be delayed until the start
% of the \enquote{next} paragraph. This is particularly notable for
% invisible items such as whatsits and specials, which will otherwise
% cause changes in spacing. Delaying insertion enables user-supplied
% settings to override design settings in a reliable fashion. This can
% be achieved as the design-level material can be ignored if a user
% value is supplied. There is a need to allow proper nesting of
% galleys, which means that all of the above needs to be set up so that
% it can be saved and restored. All of these manipulations require
% altering the meaning of the \cs{par} token, which is particularly
% awkward as \TeX{} inserts a token \emph{called} \cs{par} rather than
% one with a particular meaning. This makes moving \cs{par} to somewhere
% \enquote{safe} extremely challenging.
%
% Added to all of this complexity, there is a need to deal with
% \enquote{display-like} material. The most obvious example is the way
% lists are handled. These use \cs{par} tokens to achieve the correct
% appearance, but at the same time
% \begin{verbatim}
%   Text
%   \begin{itemize}
%     \item An item
%   \end{itemize}
%   More text
% \end{verbatim}
% should form one visual paragraph while
% \begin{verbatim}
%   Text
%   \begin{itemize}
%     \item An item
%   \end{itemize}
%
%   More text
% \end{verbatim}
% should be shown as two paragraphs. This requires an additional level
% of handling so that the \cs{par} token used to end the list in the
% first case does not start a new paragraph in a visual sense while the
% second does.
%
% Another factor to bear in mind is that \cs{tex_everypar:D} may be
% executed inside a group. For example, a paragraph starting
% \begin{verbatim}
%   {Text} here
% \end{verbatim}
% will insert the tokens such that the current group level is $1$
% higher for \enquote{Text} than for \enquote{here}. The result of this
% is that it's very important to watch how flags are set and reset. This
% can only be done reliably on a global level, which then has a knock-on
% effect on the rest of the implementation.
%
% At a \TeX{} level, settings can only apply to the current paragraph,
% but conceptually there is a need to allow for both single-paragraph
% and \enquote{running} settings. Whenever the code switches galley
% level both of these need to be correctly saved.
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%    
%    \begin{macrocode}
%<@@=galley>
%    \end{macrocode}
%
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage
  {\ExplFileName}{\ExplFileDate}{\ExplFileVersion}{\ExplFileDescription}
%</package>
%    \end{macrocode}
%
% \subsection{Support items}
%
% Functions or settings which are needed by the galley but perhaps also
% elsewhere.
% 
% \begin{macro}{\seq_map_function:Nc}
%   Used to allow a mapping to choose the outcome using a passed option.
%    \begin{macrocode}
\cs_generate_variant:Nn \seq_map_function:NN { Nc }
%    \end{macrocode}
% \end{macro}
%
% The default hyphenation character should be set and hyphenation should be
% enabled.
%    \begin{macrocode}
%<*initex>
\tex_defaulthyphenchar:D 45 \scan_stop:
%</initex>
%    \end{macrocode}
%    
% \subsection{Scratch variables}
%
% \begin{variable}{\l_@@_tmp_int}
% \begin{variable}{\l_@@_tmpa_seq, \l_@@_tmpb_seq}
% \begin{variable}{\l_@@_tmpa_tl}
%   Used for manipulating cutouts and paragraph shapes.
%    \begin{macrocode}
\int_new:N \l_@@_tmp_int
\seq_new:N \l_@@_tmpa_seq
\seq_new:N \l_@@_tmpb_seq
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{variable}
% \end{variable}
% \end{variable}
%
% \subsection{Galley settings}
%
% Settings for application by the galley respect the usual \TeX{} grouping and
% so are all local variables.
%
% \begin{variable}{\l_@@_parshape_multipar_bool}
%   Indicates whether the paragraph shape in use applies to one paragraph
%   only or to more than one.
%    \begin{macrocode}
\bool_new:N \l_@@_parshape_multipar_bool
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}
%   {\l_@@_parshape_left_indent_seq, \l_@@_parshape_right_indent_seq}
%   Used to convert user input comma lists into sequences for mapping, and also
%   to keep the information on paragraph shape available for the case where a
%   cutout is also active.
%    \begin{macrocode}
\seq_new:N \l_@@_parshape_left_indent_seq
\seq_new:N \l_@@_parshape_right_indent_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_galley_text_width_dim}
%   The width of the current measure: the \enquote{running} setting can be
%   inherited from \LaTeXe{}.
%    \begin{macrocode}
%<*initex>
\dim_new:N \l_galley_text_width_dim
%</initex>
%<*package>
\cs_new_eq:NN \l_galley_text_width_dim \linewidth
%</package>
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\l_galley_total_left_margin_dim, \l_galley_total_right_margin_dim}
%   Margins of the current text within the galley: these plus the galley
%   width are one way to define the measure width. See also the text width,
%   which is an alternative view (and should be in sync with this one!).
%    \begin{macrocode}
%<*initex>
\dim_new:N \l_galley_total_left_margin_dim
%</initex>
%<*package>
\cs_new_eq:NN \l_galley_total_left_margin_dim \@totalleftmargin
%</package>
\dim_new:N \l_galley_total_right_margin_dim
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\l_galley_interpar_penalty_int, \l_galley_interpar_vspace_skip}
%   Items between paragraphs at the design level.
%    \begin{macrocode}
\int_new:N \l_galley_interpar_penalty_int
\skip_new:N \l_galley_interpar_vspace_skip
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_galley_width_dim}
%   The external size of a galley is the stored in the \TeX{} primitive
%   \cs{tex_hsize:D}, which is renamed. This will only ever be reset by
%   the code constructing a new galley, for example the start of a
%   minipage. This value will be set for the main galley by the page
%   layout system.
%    \begin{macrocode}
\cs_new_eq:NN \l_galley_width_dim \tex_hsize:D
%    \end{macrocode}
% \end{variable}
%
% \subsection{Galley data structures}
%
% In contrast to settings, the data structures used by the galley are all set
% globally. To allow different galley levels to exist, a local variant is
% defined for each one to save the value when starting a new level.
%
% \begin{variable}{\g_@@_begin_level_bool, \l_@@_begin_level_bool}
%   Indicates that the galley is at the very beginning of the level, and that
%   no material has yet been set. As a result, the global version is set
%   \texttt{true} to begin with.
%    \begin{macrocode}
\bool_new:N \g_@@_begin_level_bool
\bool_new:N \l_@@_begin_level_bool
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}
%   {
%     \g_@@_cutout_left_seq,  \l_@@_cutout_left_seq,
%     \g_@@_cutout_right_seq, \l_@@_cutout_right_seq
%   }
%   To apply cutouts across paragraphs, they need to be tracked. The
%   information has to be global, so is handled here.
%    \begin{macrocode}
\seq_new:N \g_@@_cutout_left_seq
\seq_new:N \l_@@_cutout_left_seq
\seq_new:N \g_@@_cutout_right_seq
\seq_new:N \l_@@_cutout_right_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\g_galley_omit_next_indent_bool, \l_@@_omit_next_indent_bool}
%   A global flag is needed for suppressing indentation of the next
%   paragraph. This does not need a \enquote{running} version since that
%   should be handled using the \texttt{justification} object: the two
%   concepts are related but not identical. The flag here is needed in
%   cases such as the very first paragraph in a galley or immediately
%   following a heading.
%    \begin{macrocode}
\bool_new:N \g_galley_omit_next_indent_bool
\bool_new:N \l_@@_omit_next_indent_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_galley_no_break_next_bool, \l_@@_no_break_next_bool}
%   Dealing with the no-break flag is pretty much the same as the case
%   for the indent: this applies on a single paragraph basis.
%    \begin{macrocode}
\bool_new:N \g_galley_no_break_next_bool
\bool_new:N \l_@@_no_break_next_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \g_galley_par_begin_hook_tl, \l_@@_par_begin_hook_tl,
%     \g_galley_par_end_hook_tl,   \l_@@_par_end_hook_tl,
%   }
%   Hooks for user-level code: these are not used by the galley itself.
%    \begin{macrocode}
\tl_new:N \g_galley_par_begin_hook_tl
\tl_new:N \l_@@_par_begin_hook_tl
\tl_new:N \g_galley_par_end_hook_tl
\tl_new:N \l_@@_par_end_hook_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\g_galley_par_reset_hook_tl, \l_@@_par_reset_hook_tl}
%   This one is used by the galley: it happens \enquote{after} the current
%   paragraph, and is used for reset purposes.
%    \begin{macrocode}
\tl_new:N \g_galley_par_reset_hook_tl
\tl_new:N \l_@@_par_reset_hook_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\g_galley_previous_par_lines_int, \l_@@_previous_par_lines_int}
% \begin{variable}
%   {\g_@@_current_par_lines_int, \l_@@_current_par_lines_int}
%   The number of lines in the previous typeset paragraph. This is reset at
%   the start of the paragraph and \emph{added to} when each \cs{tex_par:D}
%   primitive is used: \LaTeX{} uses the primitive in places that do not end
%   a (conceptual) paragraph. There is also a set of variables to deal with
%   the current (conceptual) paragraph, so that the information can be
%   build up correctly.
%    \begin{macrocode}
\int_new:N \g_galley_previous_par_lines_int
\int_new:N \l_@@_previous_par_lines_int
\int_new:N \g_@@_current_par_lines_int
\int_new:N \l_@@_current_par_lines_int
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{variable}{\g_galley_restore_running_tl, \l_@@_restore_running_tl}
%   When a parameter is altered from the \enquote{running} value to a
%   different \enquote{current} one, there needs to be a method to restore
%   the \enquote{running} value. This is done by adding the necessary
%   assignment to a token list, which can be executed when needed. At the
%   same time, this information is itself part of the galley parameter
%   structure, and so there has to be a local save version.
%    \begin{macrocode}
\tl_new:N \g_galley_restore_running_tl
\tl_new:N \l_@@_restore_running_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \g_galley_whatsit_next_tl, \l_@@_whatsit_next_tl,
%     \g_galley_whatsit_previous_tl, \l_@@_whatsit_previous_tl
%   }
%   Whatsits only apply on a per-paragraph basis and so there is no need
%   to differentiate between current and running values. However, there
%   is a need to differentiate between whatsits that attach to the
%   previous (completed) paragraph and those that attach to the next
%   paragraph.
%    \begin{macrocode}
\tl_new:N \g_galley_whatsit_next_tl
\tl_new:N \l_@@_whatsit_next_tl
\tl_new:N \g_galley_whatsit_previous_tl
\tl_new:N \l_@@_whatsit_previous_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\g_@@_interpar_penalty_user_tl, \l_@@_interpar_penalty_user_tl}
%   The user may want to over-ride the penalty for a break between
%   paragraphs, for example to prevent a break when the overall design
%   allows one. This is handled using an additional penalty.
%    \begin{macrocode}
\tl_new:N \g_@@_interpar_penalty_user_tl
\tl_new:N \l_@@_interpar_penalty_user_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\g_@@_interpar_vspace_user_tl, \l_@@_interpar_vspace_user_tl}
%   Arbitrary vertical space can be inserted by the user on a one-off
%   basis. This is used in place of any running space between paragraphs.
%    \begin{macrocode}
\tl_new:N \g_@@_interpar_vspace_user_tl
\tl_new:N \l_@@_interpar_vspace_user_tl
%    \end{macrocode}
% \end{variable}
%
% \subsection{Independent galley levels}
%
% As well as the main vertical list, independent galleys are required
% for items such as minipages and marginal notes. Each of these galleys
% requires an independent set of global data structures. This is
% achieved by storing the data structures in \emph{local} variables. The
% later are only used to save and restore the global value, and so \TeX{}
% grouping will manage the values correctly. This implies that each
% galley level must form a group: galley levels are tied to vertical
% boxes and so this is a reasonable requirements.
%
% \begin{macro}{\@@_initialise_variables:}
%   At the start of a galley level, both the global and local variables
%   will need to be reset to standard values. For example, the measure
%   is set to the galley width and any paragraph shape is cleared.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_initialise_variables:
  {
    \bool_gset_true:N \g_@@_begin_level_bool
    \seq_gclear:N \g_@@_cutout_left_seq
    \seq_gclear:N \g_@@_cutout_right_seq
    \tl_gclear:N \g_@@_interpar_penalty_user_tl
    \tl_gclear:N \g_@@_interpar_vspace_user_tl
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \bool_gset_false:N \g_galley_no_break_next_bool
    \tl_gclear:N \g_galley_par_begin_hook_tl
    \tl_gclear:N \g_galley_par_end_hook_tl
    \tl_gclear:N \g_galley_par_reset_hook_tl
    \int_gzero:N \g_@@_current_par_lines_int
    \int_gzero:N \g_galley_previous_par_lines_int
    \tl_gclear:N \g_galley_restore_running_tl
    \tl_gclear:N \g_galley_whatsit_previous_tl
    \tl_gclear:N \g_galley_whatsit_next_tl
  }
\@@_initialise_variables:
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_initialise_settings:}
%   This sets the local values of the various galley settings.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_initialise_settings:
  {
    \dim_set_eq:NN \l_galley_text_width_dim \l_galley_width_dim
    \dim_zero:N \l_galley_total_left_margin_dim
    \dim_zero:N \l_galley_total_right_margin_dim
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_save_parameters:, \@@_restore_parameters:}
%   Saving and restoring parameters is carried out by a series of copy
%   functions.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_save_parameters:
  {
    \bool_set_eq:NN \l_@@_begin_level_bool
      \g_@@_begin_level_bool
    \seq_set_eq:NN \l_@@_cutout_left_seq
      \g_@@_cutout_left_seq
    \seq_set_eq:NN \l_@@_cutout_right_seq
      \g_@@_cutout_right_seq
    \tl_set_eq:NN \l_@@_interpar_penalty_user_tl
      \g_@@_interpar_penalty_user_tl
    \tl_set_eq:NN \l_@@_interpar_vspace_user_tl
      \g_@@_interpar_vspace_user_tl
    \bool_set_eq:NN \l_@@_omit_next_indent_bool
      \g_galley_omit_next_indent_bool
    \bool_set_eq:NN \l_@@_no_break_next_bool
      \g_galley_no_break_next_bool
    \tl_set_eq:NN \l_@@_par_begin_hook_tl
      \g_galley_par_begin_hook_tl
    \tl_set_eq:NN \l_@@_par_end_hook_tl
      \g_galley_par_end_hook_tl
    \tl_set_eq:NN \l_@@_par_reset_hook_tl
      \g_galley_par_reset_hook_tl
    \int_set_eq:NN \l_@@_current_par_lines_int
      \g_@@_current_par_lines_int
    \int_set_eq:NN \l_@@_previous_par_lines_int
      \g_galley_previous_par_lines_int
    \tl_set_eq:NN \l_@@_restore_running_tl
      \g_galley_restore_running_tl
    \tl_set_eq:NN \l_@@_whatsit_previous_tl
      \g_galley_whatsit_previous_tl
    \tl_set_eq:NN \l_@@_whatsit_next_tl
      \g_galley_whatsit_next_tl
  }
\cs_new_protected_nopar:Npn \@@_restore_parameters:
  {
    \bool_gset_eq:NN \g_@@_begin_level_bool
      \l_@@_begin_level_bool
    \seq_gset_eq:NN \g_@@_cutout_left_seq
      \l_@@_cutout_left_seq
    \seq_gset_eq:NN \g_@@_cutout_right_seq
      \l_@@_cutout_right_seq
    \tl_gset_eq:NN \g_@@_interpar_penalty_user_tl
      \l_@@_interpar_penalty_user_tl
    \tl_gset_eq:NN \g_@@_interpar_vspace_user_tl
      \l_@@_interpar_vspace_user_tl
    \bool_gset_eq:NN \g_galley_omit_next_indent_bool
      \l_@@_omit_next_indent_bool
    \bool_gset_eq:NN \g_galley_no_break_next_bool
      \l_@@_no_break_next_bool
    \tl_gset_eq:NN \g_galley_par_begin_hook_tl
      \l_@@_par_begin_hook_tl
    \tl_gset_eq:NN \g_galley_par_end_hook_tl
      \l_@@_par_end_hook_tl
    \tl_gset_eq:NN \g_galley_par_reset_hook_tl
      \l_@@_par_reset_hook_tl
    \int_gset_eq:NN \g_@@_current_par_lines_int
      \l_@@_current_par_lines_int
    \int_gset_eq:NN \g_galley_previous_par_lines_int
      \l_@@_previous_par_lines_int
    \tl_gset_eq:NN \g_galley_restore_running_tl
      \l_@@_restore_running_tl
    \tl_gset_eq:NN \g_galley_whatsit_previous_tl
      \l_@@_whatsit_previous_tl
    \tl_gset_eq:NN \g_galley_whatsit_next_tl
      \l_@@_whatsit_next_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\galley_level:}
% \begin{macro}[aux]{\@@_level_end:}
%   Galley levels are created by saving all of the current global
%   settings, starting a group then initialising both the local and global
%   variables.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_level:
  {
    \@@_save_parameters:
    \group_begin:
      \@@_initialise_variables:
      \@@_initialise_settings:
      \group_insert_after:N \@@_level_end:
  }
%    \end{macrocode}
%   At the end of the level, the global values are restored using the
%   saved \emph{local} versions, hence the position of the close-of-group
%   instruction. As this code can be inserted automatically, at the point
%   of use only the start of a galley level needs to be marked up: the end
%   must come in a fixed location. All of this relies on the the
%   \enquote{color safe} group used inside a box.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_level_end:
  {
      \par
      \@@_restore_parameters:
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{The \cs{par} token}
%
% \begin{variable}{\s__par_omit}
%   Used to indicate that a paragraph should be omitted.
%    \begin{macrocode}
\__scan_new:N \s__par_omit
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\galley_par:}
% \begin{macro}[aux]{\@@_par_auxi:, \@@_par_auxii:}
% \begin{macro}[aux]{\@@_par_aux:N}
%   The idea here is to expand the next token in exactly the same way as \TeX{}
%   would do anyway. The \texttt{f}-type expansion will ignore any protection,
%   but will stop at a scan marker. Thus the code can test for an
%   \enquote{omit paragraph} marker.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_par:
  {
    \s__par_omit
    \exp_after:wN \@@_par_auxi: \tex_romannumeral:D - `0
  }
\cs_new_protected:Npn \@@_par_auxi:
  {
    \peek_meaning:NTF \s__par_omit
      { \@@_par_aux:N }
      { \@@_par_auxii: }
  }
\cs_new_protected:Npn \@@_par_aux:N #1
  {
    \str_if_eq_x:nnF {#1} { \s__par_omit }
      {
        \@@_par_auxii:
        #1
      }
  }
%    \end{macrocode}
%   No marker, so really insert a paragraph: the \cs{tex_par:D} is inside a
%   group to preserve some dynamic settings (for example
%   \cs{etex_interlinepenalties:D}). In vertical mode, that means just
%   inserting the primitive.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_par_auxii:
  {
    \mode_if_vertical:TF
      {
        \group_begin:
          \tex_par:D
        \group_end:  
      }
%    \end{macrocode}
%   In horizontal mode, the paragraph shape is set \enquote{just in time}
%   before inserting \cs{tex_par:D}.  Once the
%   paragraph has been typeset, the number of lines is \emph{added} to the
%   running total. It's possible that the conceptual paragraph contains
%   display-like material, and simply setting the number of lines equal to
%   \cs{tex_prevgraf:D} would \enquote{lose} these.
%    \begin{macrocode}
      {
        \g_galley_par_end_hook_tl
        \group_begin:    
          \tex_par:D
        \group_end:
        \int_gset:Nn \g_galley_previous_par_lines_int
          { \tex_prevgraf:D + \g_@@_current_par_lines_int }
        \@@_parshape_reset:
        \int_gzero:N \g_@@_current_par_lines_int
      }
    \g_galley_par_reset_hook_tl
    \tl_gclear:N \g_galley_par_reset_hook_tl
%    \end{macrocode}
%   The non-breaking penalty is needed here as within the \cs{tex_everypar:D}
%   hook there is an additional \cs{tex_par:D}. This leads to an extra
%   \cs{tex_parskip:D}, which will leave an unwanted break-point here
%   otherwise.
%    \begin{macrocode}
    \tex_penalty:D \c_ten_thousand
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\galley_par:n}
%   Inserts tokens such that they are appended to the end of the last
%   paragraph, using the paragraph-omitting system.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_par:n #1
  {
    \s__par_omit
    \bool_if:nF \g_@@_begin_level_bool
      {
        #1
        \galley_par:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\par}
%   The meaning of the token \cs{par} itself starts off as a standard
%   paragraph.
%    \begin{macrocode}
\cs_set_protected_nopar:Npn \par { \galley_par: }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@par}
%   \LaTeXe{} requires a \enquote{long term} version of \cs{par}, which is
%   stored as \cs{@par}. Things are done a bit differently by \LaTeX3 and
%   so this will only be needed in package mode.
%    \begin{macrocode}
%<*package>
\tl_set:Nn \@par { \galley_par: }
%</package>
%    \end{macrocode}
% \end{macro}
%
% \subsection{Display levels}
%
% \begin{macro}{\galley_display_begin:, \galley_display_end:}
% \begin{macro}[aux]{\@@_display_penalty:N, \@@_display_vspace:N}
% \begin{macro}[aux]{\@@_display_par_setup:, \@@_display_par:}
%   Display items within the galley are a bit like galley levels: they
%   may have different paragraph settings to the main part of the galley.
%   On the other hand, unlike independent galleys they should inherit the
%   settings from the surrounding material. They may also start and end with
%   special spacing values.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_display_begin:
  {
    \group_begin:
      \@@_save_parameters:
      \mode_if_vertical:TF
        {
          \@@_display_penalty:N \l_galley_display_begin_par_penalty_tl
          \@@_display_vspace:N  \l_galley_display_begin_par_vspace_tl
        }
        {
          \@@_display_penalty:N \l_galley_display_begin_penalty_tl
          \@@_display_vspace:N  \l_galley_display_begin_vspace_tl
        }
      \par
  }
%    \end{macrocode}
%   Two short-cuts for setting up any special penalty or vertical space.
%   The idea is that the standard value is saved to the \enquote{restore}
%   token list, before setting up the value to the special value needed
%   in this one case.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_display_penalty:N #1
  {
     \tl_if_empty:NF #1
       {
         \tl_gput_right:Nx \g_galley_restore_running_tl
           {
             \int_gset:Nn \exp_not:N \g_galley_penalty_int
               { \int_use:N \g_galley_penalty_int }
           }
         \int_gset:Nn \g_galley_penalty_int {#1}
       }
  }
\cs_new_protected:Npn \@@_display_vspace:N #1
  {
     \tl_if_empty:NF #1
       {
         \tl_gput_right:Nx \g_galley_restore_running_tl
           {
             \skip_gset:Nn \exp_not:N \g_galley_vspace_skip
               { \skip_use:N \g_galley_vspace_skip }
           }
         \skip_gset:Nn \g_galley_vspace_int {#1}
       }
  }
%    \end{macrocode}
%   The \cs{par} token at the end of the display needs to go in at the same
%   group level as the text, hence this function cannot be placed using
%   \cs{group_insert_after:N}. Resetting the meaning of the \cs{par} token
%   needs to be carried out after the group used for the environment.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_display_end:
  {
      \par
      \@@_restore_parameters:
    \group_end:
    \group_insert_after:N \@@_display_par_setup:
  }
%    \end{macrocode}
%   The method used here is to assume that the next piece of horizontal
%   mode material will follow on from the displayed output without an
%   intervening \cs{par} token (probably a blank line). The meaning of the
%   \cs{par} token is then altered so that a check can be made to see if
%   this assumption was correct.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_display_par_setup:
  {
    \bool_gset_false:N \g_galley_omit_next_indent_bool
    \cs_set_eq:NN \par \@@_display_par:
  }
%    \end{macrocode}
%   The \enquote{special} meaning of the paragraph token starts by putting
%   things back to normal: there should never need to be more than one
%   special paragraph marker in one group. If \TeX{} is in vertical mode,
%   then there has been a paragraph token inserted, most likely by a
%   blank line. Thus the next piece of material is a separate conceptual
%   paragraph from the display. In that case, the assumption from above is
%   undone and the indent is turned back on. On the other hand, for the
%   case where \TeX{} is in horizontal mode then a \cs{tex_par:D} primitive
%   is required in the same way as in \cs{galley_standard_par:}.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_display_par:
  {
    \cs_set_eq:NN \par \galley_par:
    \mode_if_vertical:TF
      {
        \par
        \bool_gset_false:N \g_galley_omit_next_indent_bool
        \@@_display_penalty:N \l_galley_display_end_par_penalty_tl
        \@@_display_vspace:N  \l_galley_display_end_par_vspace_tl
      }
      {
        \group_begin:
          \tex_par:D
        \group_end:
        \int_gadd:Nn \g_@@_current_par_lines_int \tex_prevgraf:D
        \@@_display_penalty:N \l_galley_display_end_penalty_tl
        \@@_display_vspace:N  \l_galley_display_end_vspace_tl
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Insertions using \cs{tex_everypar:D}}
%
% The key to the entire galley mechanism is hooking into the
% \cs{tex_everypar:D} token register. This requires that the original
% is moved out of the way, with appropriate hooks left attached for
% further modification by other modules and by the user. This is all
% done such that there is no danger of accidentally deactivating the
% galley mechanism.
%
% \begin{macro}{\everypar}
%   When used on top of \LaTeXe{} the original primitive name needs to be
%   available without the risk of completely overwriting the new
%   mechanism. This is implemented as a token register in case low-level
%   \TeX{} is used. The \TeX{} primitive is set here as otherwise the
%   \LaTeXe{} \cs{@nodocument} is never removed from the register.
%   This precaution is not be needed for a stand-alone format.
%    \begin{macrocode}
%<*initex>
\tex_everypar:D % TEMP
  {
    \bool_if:NTF \g_@@_begin_level_bool
      { \@@_start_paragraph_first: }
      { \@@_start_paragraph_std: }
  }
%</initex>
%<*package>
\cs_undefine:N \everypar
\newtoks \everypar
\AtBeginDocument
  {
    \tex_everypar:D
      {
        \bool_if:NTF \g_@@_begin_level_bool
          { \@@_start_paragraph_first: }
          { \@@_start_paragraph_std: }
        \tex_the:D \everypar
      }
  }
%</package>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The galley mechanism}
%
% \begin{variable}{\g_@@_last_box}
%   A temporary box to hold the box inserted by \TeX{} when a paragraph
%   is inserted with an indent. The galley actually inserts the space
%   (\emph{i.e.}~\cs{tex_parindent:D} is globally zero), but there is
%   still an empty box to test for.
%    \begin{macrocode}
\box_new:N \g_@@_last_box
%    \end{macrocode}
% \end{variable}
%
% The \enquote{start of paragraph} routines are fired by \cs{tex_everypar:D}.
% This can take place within a group in a construction such as
% \begin{verbatim}
%   ... end of last par.
%
%   {\Large Start} of par
% \end{verbatim}
% and so anything applied here must be done globally.
%
% \begin{macro}[int]{\@@_start_paragraph_std:}
%   The routine at the start of a paragraph starts by removing any
%   (empty) indent box from the vertical list. As there may be vertical
%   mode items still to insert, a \cs{tex_par:D} primitive is used to
%   get back into vertical mode before they are tidied up. To get back
%   again to horizontal mode, \cs{tex_noindent:D} can be used. To avoid
%   an infinite loop, \cs{tex_everypar:D} is locally cleared before doing
%   that. Back in horizontal mode, the horizontal mode items can be
%   tidied up before sorting out any items which have been set on a
%   single-paragraph basis.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_start_paragraph_std:
  {
    \group_begin:
      \box_gset_to_last:N \g_@@_last_box
      \tex_par:D
      \@@_insert_vertical_items:
      \tex_everypar:D { }
      \tex_noindent:D
    \group_end:
    \@@_insert_horizontal_items:
    \@@_restore_running_parameters:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_start_paragraph_first:}
%   For the very first paragraph in a galley, the code needs to avoid
%   adding any unnecessary vertical items at the top as it will interfere with
%   vertical positioning in \cs{tex_vtop:D}.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_start_paragraph_first:
  {
    \bool_gset_false:N \g_@@_begin_level_bool
    \mode_if_horizontal:TF
      {
        \group_begin:
          \box_gset_to_last:N \g_@@_last_box
          \tex_par:D
          \@@_insert_vspace:
          \tex_everypar:D { }
          \tex_noindent:D
        \group_end:
      }
      { \@@_insert_vspace: }
    \@@_insert_horizontal_items:
    \@@_restore_running_parameters:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_insert_vertical_items, \@@_insert_vspace:}
%   The aim here is to insert the vertical items such that they attach to
%   the correct place. This function is used as part of the \cs{tex_everypar:D}
%   mechanism, meaning that the immediately-preceding item on the vertical
%   list is the \cs{tex_parskip:D}, always zero-length but an implicit
%   penalty. So any whatsits \enquote{attached} to the previous paragraph
%   should stay glued on.  After the whatsits, a penalty for
%   breaking will be inserted. This will be the user penalty if supplied,
%   or the running penalty unless the no-break flag is set. Finally,
%   the inter-paragraph space is applied.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_insert_vertical_items:
  {
    \g_galley_whatsit_previous_tl
    \tl_gclear:N \g_galley_whatsit_previous_tl
    \tl_if_empty:NTF \g_@@_interpar_penalty_user_tl
      {
        \bool_if:NTF \g_galley_no_break_next_bool
          { \tex_penalty:D \c_ten_thousand }
          { \tex_penalty:D \l_galley_interpar_penalty_int }
      }
      {
        \tex_penalty:D
          \__int_eval:w \g_@@_interpar_penalty_user_tl \__int_eval_end:
        \tl_gclear:N \g_@@_interpar_penalty_user_tl
      }
    \bool_gset_false:N \g_galley_no_break_next_bool
    \@@_insert_vspace:
  }
%    \end{macrocode}
%   Inserting vertical space is set up as a separate function as it comes
%   up in a few places. The idea here is that any user-set space will
%   override the design value, and only one space is ever inserted.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_insert_vspace:
  {
    \tl_if_empty:NTF \g_@@_interpar_vspace_user_tl
      { \skip_vertical:N \l_galley_interpar_vspace_skip }
      {
        \skip_vertical:n { \g_@@_interpar_vspace_user_tl }
        \tl_gclear:N \g_@@_interpar_vspace_user_tl
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_insert_horizontal_items:}
%   Horizontal mode objects start with the whatsits for the next paragraph. An
%   indent is then included if the removed box was not void.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_insert_horizontal_items:
  {
    \g_galley_whatsit_next_tl
    \tl_gclear:N \g_galley_whatsit_next_tl
    \bool_if:NF \g_galley_omit_next_indent_bool
      {
        \box_if_empty:NF \g_@@_last_box
          { \hbox_to_wd:nn \l_galley_par_indent_dim { } }
      }
    \skip_horizontal:N \l_galley_par_begin_skip
    \g_galley_par_begin_hook_tl
    \bool_gset_false:N \g_galley_omit_next_indent_bool
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_restore_running_parameters:}
%   Restoring the ongoing parameters just means using the token list
%   variable in which the appropriate assignments are stored. The
%   list can then be cleared.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_restore_running_parameters:
  {
    \g_galley_restore_running_tl
    \tl_gclear:N \g_galley_restore_running_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Measure}
% 
% \begin{variable}{\l_@@_total_left_margin_dim, \l_@@_total_right_margin_dim}
%   Used to set the measure, first by providing a place to save the existing
%   values, then allowing calculation of the difference between old and new
%   settings.
%    \begin{macrocode}
\dim_new:N \l_@@_total_left_margin_dim
\dim_new:N \l_@@_total_right_margin_dim
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}
%   {\galley_margins_set_absolute:nn, \galley_margins_set_relative:nn}
% \begin{macro}[aux]{\@@_save_margins:, \@@_set_measure:}
% \begin{macro}[aux, rEXP]{\@@_set_measure_aux:n}
%   Setting the measure is just a question of adjusting margins, either
%   in a relative or absolute sense.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_margins_set_absolute:nn #1#2
  {
    \@@_save_margins:
    \dim_set:Nn \l_galley_total_left_margin_dim  {#1}
    \dim_set:Nn \l_galley_total_right_margin_dim {#2}
    \dim_set:Nn \l_galley_text_width_dim
      {
          \l_galley_width_dim
        - \l_galley_total_left_margin_dim
        - \l_galley_total_right_margin_dim
      }
    \@@_set_measure:
  }
\cs_new_protected:Npn \galley_margins_set_relative:nn #1#2
  {
    \@@_save_margins:
    \dim_add:Nn \l_galley_total_left_margin_dim  {#1}
    \dim_add:Nn \l_galley_total_right_margin_dim {#2}
    \dim_set:Nn \l_galley_text_width_dim
      {
          \l_galley_width_dim
        - \l_galley_total_left_margin_dim
        - \l_galley_total_right_margin_dim
      }
    \@@_set_measure:  
  }
%    \end{macrocode}
%   Saves the previous margin state so that it can be used for calculation
%   during the update.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_save_margins:
  {
    \dim_set_eq:NN \l_@@_total_left_margin_dim
      \l_galley_total_left_margin_dim
    \dim_set_eq:NN \l_@@_total_right_margin_dim
      \l_galley_total_right_margin_dim
  }
%    \end{macrocode}
%   Setting the measure first requires a quick test to see if there is any
%   existing shape: if not, just use the values directly. Assuming we do need
%   to allow for the existing shape, the idea is to preserve whatever shape
%   has already been applied and adjust the edges. This can be done by
%   iterating though the existing shape, recovering the values and applying
%   the changes.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_set_measure:
  {
    \int_compare:nNnTF \tex_parshape:D = \c_zero
      { \@@_parshape_measure: }
      { 
        \dim_sub:Nn \l_@@_total_left_margin_dim
          \l_galley_total_left_margin_dim
        \dim_sub:Nn \l_@@_total_right_margin_dim
          \l_galley_total_right_margin_dim
        \tex_parshape:D
          \tex_parshape:D
          \int_step_function:nnnN
            \c_one \c_one \tex_parshape:D
            \@@_set_measure_aux:n
      }
  }
\cs_new:Npn \@@_set_measure_aux:n #1
  {
    \__dim_eval:w
      \etex_parshapeindent:D #1 - \l_@@_total_left_margin_dim
    \__dim_eval_end:
    \__dim_eval:w
      \etex_parshapelength:D #1
        + \l_@@_total_left_margin_dim + \l_@@_total_right_margin_dim
    \__dim_eval_end:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\@@_parshape_reset:}
%   Test for paragraph shapes to be removed: a multi-step procedure! First,
%   check to see if there is a shape at all. If there is, the second check
%   looks for cutouts. If there are no cutouts to worry about, the final
%   check is whether the current shape applies to one paragraph or on an
%   ongoing sense.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_parshape_reset:
  {
    \int_compare:nNnF \tex_parshape:D = \c_zero
      {
        \bool_if:nTF
          { 
               \seq_if_empty_p:N \g_@@_cutout_left_seq
            && \seq_if_empty_p:N \g_@@_cutout_right_seq 
          }
          {
            \bool_if:NF \l_@@_parshape_multipar_bool
              { \@@_parshape_measure: }
          }
%    \end{macrocode}
%   If there was a cutout active, the \enquote{done} part is removed from
%   the tracking sequences. The \enquote{normal} paragraph shape is then
%   reapplied before removing the cutout parts. This is done using separate
%   loops as in general that is as easy as trying to work out all of the
%   parts in advance (the same number of loops would still be needed).
%    \begin{macrocode}
          {
            \prg_replicate:nn \g@@_previous_par_lines_int
              {
                \seq_gpop:NN \g_@@_cutout_left_seq \l_@@_tmp_tl
                \seq_gpop:NN \g_@@_cutout_right_seq \l_@@_tmp_tl
              }
            \bool_if:NTF \l_@@_parshape_multipar_bool
              { \@@_parshape_set: }
              { \@@_parshape_measure: }
            \@@_cutout_set:Nn \g_@@_cutout_left_seq { left }
            \@@_cutout_set:Nn \g_@@_cutout_right_seq { right } 
          }
      }    
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}[int]{\@@_parshape_measure:}
%   Sets the parshape to the full measure at the current time: used in a few
%   places, so worth spinning out.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_parshape_measure:
  {
    \tex_parshape:D
      \c_one
      \l_galley_total_left_margin_dim
      \l_galley_text_width_dim
  }
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Paragraph shape}
%
% \begin{macro}
%   {
%     \galley_parshape_set_multi:nnnN,
%     \galley_parshape_set_multi:nVVN,
%     \galley_parshape_set_single:nnnN,
%     \galley_parshape_set_single:nVVN
%   }
%   Setting the paragraph shape is mainly a question of converting the input.
%   First, though, a flag is set.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_parshape_set_multi:nnnN
  {
    \bool_set_true:N \l_@@_parshape_multipar_bool
    \@@_parshape_set:nnnN
  }
\cs_new_protected_nopar:Npn \galley_parshape_set_single:nnnN
  {
    \bool_set_false:N \l_@@_parshape_multipar_bool
    \@@_parshape_set:nnnN
  }
\cs_generate_variant:Nn \galley_parshape_set_multi:nnnN  { nVV }
\cs_generate_variant:Nn \galley_parshape_set_single:nnnN { nVV }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_parshape_set:nnnN}
% \begin{macro}[int]{\@@_parshape_set:}
% \begin{macro}[aux, rEXP]{\@@_parshape_set:nn}
%   Setting the paragraph shape starts by converting the two input lists into
%   sequences. The shape is then set using a mapping with allowance for the
%   unaltered lines and the possibility of resuming the measure. The unaltered
%   lines are added to the sequences as this information may be needed
%   elsewhere (if a cutout is active), and this is the most convenient way to
%   deal with it. Everything ends up stored in the two sequences for possible
%   re-application if there are cutouts.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parshape_set:nnnN #1#2#3#4
  {
    \seq_set_from_clist:Nn \l_@@_parshape_left_indent_seq {#2}
    \seq_set_from_clist:Nn \l_@@_parshape_right_indent_seq {#3}
    \prg_replicate:nn {#1}
      {
        \seq_put_left:Nn \l_@@_parshape_left_indent_seq { 0 pt }
        \seq_put_left:Nn \l_@@_parshape_right_indent_seq { 0 pt }
      }
    \bool_if:NT #4
      {
        \seq_put_right:Nn \l_@@_parshape_left_indent_seq { 0 pt }
        \seq_put_right:Nn \l_@@_parshape_right_indent_seq { 0 pt }
      }  
    \@@_parshape_set:
  }
\cs_new_protected_nopar:Npn \@@_parshape_set:
  {
    \tex_parshape:D
      \__int_eval:w
        \int_min:nn
          { \seq_count:N \l_@@_parshape_left_indent_seq }
          { \seq_count:N \l_@@_parshape_right_indent_seq }
      \__int_eval_end:
      \seq_mapthread_function:NNN
        \l_@@_parshape_left_indent_seq
        \l_@@_parshape_right_indent_seq
        \@@_parshape_set:nn
  }
\cs_new:Npn \@@_parshape_set:nn #1#2
  {
    \__dim_eval:w \l_galley_total_left_margin_dim + ( #1 ) \__dim_eval_end:
    \__dim_eval:w
      \l_galley_text_width_dim - ( ( #1 ) + ( #2 ) )
    \__dim_eval_end:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \subsection{Cutouts}
% 
% Cutouts are another way of looking at paragraph shapes, but apply to
% a fixed number of lines within a galley. As such, they require separate
% handling from the measure and shape.
% 
% \begin{macro}{\galley_cutout_left:nn, \galley_cutout_right:nn}
% \begin{macro}[int]{\@@_cutout:nnn}
% \begin{macro}[aux]{\@@_cutout_store:nn}
% \begin{macro}[int]{\@@_cutout_set:Nn}
% \begin{macro}[aux]{\@@_cutout_left:n, \@@_cutout_right:n, \@@_cutout_end:n}
%   Setting up cutouts on the two sides of the paragraph is more or less
%   the same idea with one or two very specific points of difference. As such,
%   the two interface functions both use the same implementation.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_cutout_left:nn
  { \@@_cutout:nnn { left } }
\cs_new_protected_nopar:Npn \galley_cutout_right:nn
  { \@@_cutout:nnn { right } }
%    \end{macrocode}
%   The approach in the main function is first to construct a single sequence
%   which contains details of all of the altered lines. This sequence is then
%   used by \cs{@@_cutout_set:Nn}, which actually applies the cutout to the
%   current paragraph shape. One that is done, information on the cutout just
%   applied is merged with the global cutout store: this needs to allow for
%   different cutout lengths. The best way to do that is to loop over whichever
%   list is longer, build a new list of the result and the copy this back to
%   the store.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_cutout:nnn #1#2#3
  {
    \seq_set_from_clist:Nn \l_@@_tmpa_seq {#3}
    \prg_replicate:nn {#2}
      { \seq_put_left:Nn \l_@@_tmpa_seq { 0 pt } }
    \@@_cutout_set:Nn \l_@@_tmpa_seq {#1}
    \seq_clear:N \l_@@_tmpb_seq
    \int_compare:nNnTF
      { \seq_count:N \l_@@_tmpa_seq } >
        { \seq_count:c { g_@@_cutout_ #1 _ seq }  }
      {
        \seq_map_inline:Nn \l_@@_tmpa_seq
          { 
            \@@_cutout_store:nn
              { \seq_gpop:cNF { g_@@_cutout_ #1 _ seq } }
              {##1}
          }
      }
      {
        \seq_map_inline:cn { g_@@_cutout_ #1 _ seq }
          { 
            \@@_cutout_store:nn
              { \seq_pop:NNF \l_@@_tmpa_seq }
              {##1}
          }
      }
    \seq_gset_eq:cN { g_@@_cutout_ #1 _ seq } \l_@@_tmpb_seq
  }
\cs_new_protected:Npn \@@_cutout_store:nn #1#2
  {
    #1 \l_@@_tmp_tl
      { \tl_set:Nn \l_@@_tmp_tl { 0 pt } }
    \seq_put_right:Nx \l_@@_tmpb_seq
      { \dim_eval:n { \l_@@_tmp_tl + #2 } }
  }
%    \end{macrocode}
%   Actually applying a cutout is done by using the sequence containing the
%   cutout shape plus the current paragraph shape to construct a token list
%   of the required indents and line lengths. That is done in a
%   non-expansion way to allow for the two different kinds of mapping (to the
%   sequence and to the parshape). At the end of the process, there is a need
%   to allow for the last line of \tn{parshape}: depending on any existing
%   shape and the length of the cutout, this might be the measure or a
%   repeat of the last values in the shape.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_cutout_set:Nn #1#2
  {
    \int_zero:N \l_@@_tmp_int
    \tl_clear:N \l_@@_tmp_tl
    \seq_map_function:Nc #1 { _@@_cutout_ #2 :n }
    \int_compare:nNnTF \tex_parshape:D > \l_@@_tmp_int
      {
        \int_step_function:nnnN
          { \l_@@_tmp_int + \c_one }
          \c_one
          \tex_parshape:D
          \@@_cutout_end:n
      }
      {
        \int_incr:N \l_@@_tmp_int
        \int_compare:nNnTF \tex_parshape:D = \c_zero
          {
            \tl_put_right:Nn \l_@@_tmp_tl
             { \l@@_total_left_margin_dim \l@@_text_width_dim }
          }
          {
            \tl_put_right:Nx \l_@@_tmp_tl
              {
                \dim_eval:n { \etex_parshapeindent:D \tex_parshape:D }
                \c_space_tl
                \dim_eval:n { \etex_parshapelength:D \tex_parshape:D }
                \c_space_tl
              }
          }
      }
    \tex_parshape:D
      \l_@@_tmp_int
      \l_@@_tmp_tl
  }
%    \end{macrocode}
%   The only place where the position of the cutout matters is in working out
%   the indent and line lengths. That's achieved using two separate auxiliaries
%   and selection by name.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_cutout_left:n #1
  {
    \int_incr:N \l_@@_tmp_int
    \tl_put_right:Nx \l_@@_tmp_tl
      {
        \int_compare:nNnTF \l_@@_tmp_int > \tex_parshape:D
          {
            \dim_eval:n { \l@@_total_left_margin_dim + ( #1 ) }
            \c_space_tl
            \dim_eval:n { \l@@_text_width_dim - ( #1 ) }
            \c_space_tl
          }
          {
            \dim_eval:n { \etex_parshapeindent:D \l_@@_tmp_int + ( #1 ) }
            \c_space_tl
            \dim_eval:n { \etex_parshapelength:D \l_@@_tmp_int - ( #1 ) }
            \c_space_tl
          } 
      }
  }
\cs_new_protected:Npn \@@_cutout_right:n #1
  {
    \int_incr:N \l_@@_tmp_int
    \tl_put_right:Nx \l_@@_tmp_tl
      { 
        \dim_eval:n { \etex_parshapeindent:D \l_@@_tmp_int }
        \c_space_tl
        \dim_eval:n { \etex_parshapelength:D \l_@@_tmp_int - ( #1 ) }
        \c_space_tl
      }
  }
%    \end{macrocode}
%   An auxiliary to \enquote{recycle} the paragraph shape which is already
%   active \emph{if} the cutout is shorter.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_cutout_end:n #1
  {
    \int_incr:N \l_@@_tmp_int
    \tl_put_right:Nx \l_@@_tmp_tl
      { 
        \dim_eval:n { \etex_parshapeindent:D #1 }
        \c_space_tl
        \dim_eval:n { \etex_parshapelength:D #1 }
        \c_space_tl
      }    
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Between paragraphs}
%
% \begin{macro}{\galley_penalty_set_single:n, \galley_vspace_set_single:n}
%   User supplied penalties and spaces only apply for a single paragraph.
%   In both cases, the input values need to be checked for the correct
%   form but are stored as token lists. The \texttt{x}-type expansion
%   deals with this nicely.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_penalty_set_single:n #1
  { \tl_gset:Nx \g_@@_interpar_penalty_user_tl { \int_eval:n {#1} } }
\cs_new_protected:Npn \galley_vspace_set_single:n #1
  { \tl_gset:Nx \g_@@_interpar_vspace_user_tl { \skip_eval:n {#1} } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\parskip}
%   For the package, the \cs{parskip} primitive is moved out of the
%   way as the code above is handling things.
%    \begin{macrocode}
%<*package>
\dim_set:Nn \parskip \c_zero_dim
\cs_undefine:N \parskip
\skip_new:N \parskip
%</package>
%    \end{macrocode}
% \end{macro}
%
% \subsection{Formatting inside the paragraph}
%
% Justification is more complex than is necessarily desirable as the various
% \TeX{} parameters here interact in ways which mean that clear separation
% between different areas is not so easy.
%
% \begin{variable}
%   {
%     \l_galley_line_left_skip,
%     \l_galley_line_right_skip,
%     \l_galley_par_begin_skip,
%     \l_galley_par_end_skip,
%     \l_galley_par_indent_dim
%   }
%   The variables for setting paragraph shape: essentially, these are
%   the \TeX{} set.
%    \begin{macrocode}
\cs_new_eq:NN \l_galley_line_left_skip  \tex_leftskip:D
\cs_new_eq:NN \l_galley_line_right_skip \tex_rightskip:D
\dim_new:N \l_galley_par_begin_skip
\cs_new_eq:NN \l_galley_par_end_skip   \tex_parfillskip:D
\cs_new_eq:NN \l_galley_par_indent_dim \tex_parindent:D
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_galley_last_line_fit_int}
%   One from \eTeX{}.
%    \begin{macrocode}
\cs_new_eq:NN \l_galley_last_line_fit_int \etex_lastlinefit:D
%    \end{macrocode}
% \end{variable}
%
% \subsection{Inter-word spacing}
%
% Setting the spacing between words and between sentences is important
% for achieving the correct output from ragged and centered output. At
% the same time, as far as possible the aim is to retain the spacing
% specified by the font designer and not to use arbitrary values
% (\emph{cf.}~the approach in \emph{The \TeX{}book}, p.~101).
%
% \begin{macro}{\galley_interword_spacing_set:N}
%   The approach taken to setting a fixed space is to use the information
%   from the current font to set the spacing. This means that only
%   \cs{tex_spacefactor:D} needs to be set, while \cs{tex_xspacefactor:D}
%   is left alone. However, this is only necessary for fonts which have
%   a stretch component to the inter-word spacing in the first place,
%   \emph{i.e.}~monospaced fonts require no changes. The code therefore
%   checks whether there is any stretch, and if there is uses the fixed
%   component to set \cs{tex_spaceskip:D}. If there is a stretch component
%   (non-zero \cs{tex_fontdimen:D} \texttt{3}), then the \cs{teX_spaceskip:D}
%   is set to the fixed component from the font.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_interword_spacing_set:N #1
  {
    \bool_if:NTF #1
      { % TODO Hook for font changes required!
        \dim_compare:nNnTF { \tex_fontdimen:D \c_three \tex_font:D }
          = \c_zero_dim
          { \tex_spaceskip:D \c_zero_dim }
          { \tex_spaceskip:D \tex_fontdimen:D \c_two \tex_font:D }
      }
      { \tex_spaceskip:D \c_zero_dim }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Hyphenation}
%
% \begin{variable}{\l_galley_hyphen_left_int}
%   Currently something of a hack: this links in with language and fonts,
%   so is not so straight-forward to handle.
%    \begin{macrocode}
\int_new:N \l_galley_hyphen_left_int
%<*package>
\int_set:Nn \l_galley_hyphen_left_int { \tex_lefthyphenmin:D }
%</package>
%    \end{macrocode}
% \end{variable}
%
% \subsection{Line breaking}
%
% \begin{variable}
%   {
%     \l_galley_binop_penalty_int,
%     \l_galley_double_hyphen_demerits_int,
%     \l_galley_emergency_stretch_skip,
%     \l_galley_final_hyphen_demerits_int,
%     \l_galley_linebreak_badness_int,
%     \l_galley_linebreak_fuzz_dim,
%     \l_galley_linebreak_penalty_int,
%     \l_galley_linebreak_pretolerance_int,
%     \l_galley_linebreak_tolerance_int,
%     \l_galley_mismatch_demerits_int,
%     \l_galley_relation_penalty_int
%   }
%   All \TeX{} primitives renamed.
%    \begin{macrocode}
\cs_new_eq:NN \l_galley_binop_penalty_int          \tex_binoppenalty:D
\cs_new_eq:NN \l_galley_double_hyphen_demerits_int \tex_doublehyphendemerits:D
\cs_new_eq:NN \l_galley_emergency_stretch_skip     \tex_emergencystretch:D
\cs_new_eq:NN \l_galley_final_hyphen_demerits_int  \tex_finalhyphendemerits:D
\cs_new_eq:NN \l_galley_linebreak_badness_int      \tex_hbadness:D
\cs_new_eq:NN \l_galley_linebreak_fuzz_dim         \tex_hfuzz:D
\cs_new_eq:NN \l_galley_linebreak_penalty_int      \tex_linepenalty:D
\cs_new_eq:NN \l_galley_linebreak_pretolerance_int \tex_pretolerance:D
\cs_new_eq:NN \l_galley_mismatch_demerits_int      \tex_adjdemerits:D
\cs_new_eq:NN \l_galley_relation_penalty_int       \tex_relpenalty:D
\cs_new_eq:NN \l_galley_linebreak_tolerance_int    \tex_tolerance:D
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\galley_break_line:Nn}
%   Terminating a line early without a new paragraph requires a few steps.
%   First, any skips are removed, then any additional space to add is
%   places on the surrounding vertical list. Finally, the current line
%   is ended, using a penalty to prevents an overfull line ending |\\| giving
%   a totally-blank one in the output. The boolean argument is used to indicate
%   that a break is allowed after the blank line.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_break_line:Nn #1#2
  {
    \mode_if_vertical:TF
      { \__msg_kernel_error:nn { galley } { no-line-to-end } }
      {
        \tex_unskip:D
        \bool_if:NF #1
          { \tex_vadjust:D { \tex_penalty:D \c_ten_thousand } }
        \dim_compare:nNnF {#2} = \c_zero_dim
          { \tex_vadjust:D { \skip_vertical:n {#2} } }
        \tex_penalty:D \c_ten_thousand
        \tex_hfil:D
        \tex_penalty:D -\c_ten_thousand
      }
  }
%    \end{macrocode}
%\end{macro}
%
% \subsection{Paragraph breaking}
%
% \begin{variable}
%   {
%     \l_galley_broken_penalty_int,
%     \l_galley_interline_penalty_int,
%     \l_galley_parbreak_badness_int,
%     \l_galley_parbreak_fuzz_dim,
%     \l_galley_post_display_penalty_int,
%     \l_galley_pre_display_penalty_int
%
%   }
%   \TeX{} primitives renamed cover \emph{some} of this.
%    \begin{macrocode}
\cs_new_eq:NN \l_galley_broken_penalty_int       \tex_brokenpenalty:D
\cs_new_eq:NN \l_galley_interline_penalty_int    \tex_interlinepenalty:D
\cs_new_eq:NN \l_galley_parbreak_badness_int     \tex_vbadness:D
\cs_new_eq:NN \l_galley_parbreak_fuzz_dim        \tex_vfuzz:D
\cs_new_eq:NN \l_galley_post_display_penalty_int \tex_postdisplaypenalty:D
\cs_new_eq:NN \l_galley_pre_display_penalty_int  \tex_predisplaypenalty:D
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\l_galley_club_penalties_clist, \l_galley_line_penalties_clist}
%   These are used to keep a track of information which cannot be
%   extracted out of the primitives due to the overlapping nature of
%   the meanings.
%    \begin{macrocode}
\clist_new:N \l_galley_club_penalties_clist
\clist_new:N \l_galley_line_penalties_clist
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \galley_display_widow_penalties_set:n,
%     \galley_display_widow_penalties_set:V,
%     \galley_display_widow_penalties_set:v,
%     \galley_widow_penalties_set:n,
%     \galley_widow_penalties_set:V,
%     \galley_widow_penalties_set:v
%   }
% \begin{macro}[aux]{\@@_set_aux:n}
%   By far the easiest penalties to deal with are those for widows. These
%   work exactly as the names imply, with the display version only used
%   immediately before display math, and the standard penalty used at the end
%   of a paragraph. Thus there is only the need to convert the argument into
%   the correct form, and add a $0$ penalty at the end to nullify the effect of
%   repeating the last value.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_display_widow_penalties_set:n #1
  {
    \etex_displaywidowpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \@@_set_aux:n
      \c_zero
  }
\cs_generate_variant:Nn \galley_display_widow_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_widow_penalties_set:n #1
  {
    \etex_widowpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \@@_set_aux:n
      \c_zero
  }
\cs_generate_variant:Nn \galley_widow_penalties_set:n { V , v }
\cs_new:Npn \@@_set_aux:n #1 { \int_eval:n {#1} ~ }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {
%     \galley_club_penalties_set:n,
%     \galley_club_penalties_set:V,
%     \galley_club_penalties_set:v,
%     \galley_interline_penalties_set:n,
%     \galley_interline_penalties_set:V,
%     \galley_interline_penalties_set:v
%   }
%   Setting club or special line penalties is easy, as these are handled
%   mainly by the interline set up function. The two concepts are essentially
%   the same, but having two takes makes some special effects easier to
%   carry out.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_club_penalties_set:n #1
  {
    \clist_set:Nn \l_galley_club_penalties_clist {#1}
    \@@_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_club_penalties_set:n { V , v }
\cs_new_protected:Npn \galley_interline_penalties_set:n #1
  {
    \clist_set:Nn \l_galley_line_penalties_clist {#1}
    \@@_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_interline_penalties_set:n { V , v }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \galley_display_club_penalties_set:n,
%     \galley_display_club_penalties_set:V,
%     \galley_display_club_penalties_set:v
%   }
%   Setting the display club penalties means first setting the primitive,
%   then recalculating the interline array to allow for these new values.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_display_club_penalties_set:n #1
  {
    \etex_clubpenalties:D
      \__int_eval:w \clist_count:n {#1} + \c_one \__int_eval_end:
      \clist_map_function:nN {#1} \@@_set_aux:n
      \c_zero
    \@@_calc_interline_penalties:
  }
\cs_generate_variant:Nn \galley_display_club_penalties_set:n { V , v }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\galley_interline_penalty_set:n}
% \begin{macro}[aux]{\@@_set_interline_penalty:nn}
% \begin{macro}[aux]
%   {
%     \@@_set_interline_penalty_auxi:n,
%     \@@_set_interline_penalty_auxii:n
%   }
%   Dealing with the general interline penalty is handled in one shot.
%   The idea is that for lines with no special penalty, the old general
%   penalty is removed and the new one is added. If there is currently
%   no shape set, then after adding the general interline value the
%   generic build system is invoked (in case the
%   \cs{etex_interlinepenalties:D} has accidentally been cleared).
%    \begin{macrocode}
\cs_new_protected:Npn \galley_interline_penalty_set:n #1
  {
    \int_compare:nNnTF { \etex_interlinepenalties:D \c_zero } = \c_zero
      {
        \etex_interlinepenalties:D \c_one \__int_eval:w #1 \__int_eval_end:
        \@@_calc_interline_penalties:
      }
      {
        \cs_set:Npn \@@_set_interline_penalty_auxii:n ##1
          {
            \__int_eval:w
              \etex_interlinepenalties:D ##1
               - \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
               + #1
            \__int_eval_end:
          }
        \exp_args:Nf \@@_set_interline_penalty:nn
          { \clist_count:N \l_galley_line_penalties_clist } {#1}
      }
  }
\cs_new_protected:Npn \@@_set_interline_penalty:nn #1#2
  {
    \etex_interlinepenalties:D
      \etex_interlinepenalties:D \c_zero
      \int_step_function:nnnN \c_one \c_one {#1}
        \@@_set_interline_penalty_auxi:n
      \int_step_function:nnnN { #1 + \c_one } \c_one
        { \etex_interlinepenalties:D \c_zero - \c_one }
        \@@_set_interline_penalty_auxii:n
      \__int_eval:w #2 \__int_eval_end:
  }
\cs_new:Npn \@@_set_interline_penalty_auxi:n #1
  { \etex_interlinepenalties:D \__int_eval:w #1 \__int_eval_end: }
\cs_new:Npn \@@_set_interline_penalty_auxii:n #1 { }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[int]{\@@_calc_interline_penalties:}
% \begin{macro}[aux]{\@@_calc_interline_penalties:nn}
% \begin{macro}[aux]
%   {
%     \@@_calc_interline_penalties_auxi:n,
%     \@@_calc_interline_penalties_auxii:n
%   }
%   The underlying interline penalty array has to deal with club penalties,
%   display club penalties and any special line penalties, and include
%   the general interline penalty. These requirements lead to a rather
%   complex requirement on how many lines to deal with. This is needed twice,
%   so an \texttt{f}-type expansion is used to make life a little less
%   complex.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_calc_interline_penalties:
  {
    \exp_args:Nff \@@_calc_interline_penalties:nn
      {
        \int_max:nn
          {
              \clist_count:N \l_galley_club_penalties_clist
            + \c_one
          }
          {
            \int_max:nn
              {
                  \clist_count:N \l_galley_line_penalties_clist
                + \c_one
              }
              { \etex_clubpenalties:D \c_zero }
          }
      }
      { \clist_count:N \l_galley_line_penalties_clist }
  }
%    \end{macrocode}
%   The idea is now to calculate the correct penalties. Two auxiliary functions
%   are used: one for any \enquote{special penalty} lines and a second for
%   normal lines. At the end of the process, the standard interline
%   penalty is always included.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_calc_interline_penalties:nn #1#2
  {
    \etex_interlinepenalties:D #1 ~
      \int_step_function:nnnN \c_one \c_one {#2}
        \@@_calc_interline_penalties_auxi:n
      \int_step_function:nnnN { #2 + \c_one } \c_one { #1 - \c_one }
        \@@_calc_interline_penalties_auxii:n
      \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
  }
\cs_new:Npn \@@_calc_interline_penalties_auxi:n #1
  {
    \__int_eval:w
        \clist_item:Nn \l_galley_line_penalties_clist {#1}
      + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
      - \etex_clubpenalties:D #1 ~
    \__int_eval_end:
  }
\cs_new:Npn \@@_calc_interline_penalties_auxii:n #1
  {
    \__int_eval:w
        \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero
      + 0 \clist_item:Nn \l_galley_club_penalties_clist {#1}
      - \etex_clubpenalties:D #1 ~
    \__int_eval_end:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {
%     \galley_save_club_penalties:N,
%     \galley_save_interline_penalties:N,
%     \galley_save_display_club_penalties:N,
%     \galley_save_display_widow_penalties:N,
%     \galley_save_widow_penalties:N
%   }
% \begin{macro}[aux]
%   {
%     \@@_save_display_club_penalties:n,
%     \@@_save_display_widow_penalties:n,
%     \@@_save_widow_penalties:n
%   }
% \begin{macro}{\galley_interline_penalty:}
%   Saving the array penalties varies in complexity depending on how they are
%   stored internally. The first two are easy: these are simply copies.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_save_club_penalties:N #1
  { \clist_set_eq:NN #1 \l_galley_club_penalties_clist }
\cs_new_protected:Npn \galley_save_interline_penalties:N #1
  { \clist_set_eq:NN #1 \l_galley_line_penalties_clist }
%    \end{macrocode}
%   These all require appropriate mappings, using the fact that
%   \cs{clist_set:Nx} will tidy up the excess comma.
%    \begin{macrocode}
\cs_new_protected:Npn \galley_save_display_club_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_clubpenalties:D \c_zero - \c_one }
          \@@_save_display_club_penalties:n
      }
  }
\cs_new:Npn \@@_save_display_club_penalties:n #1
  { \int_use:N \etex_clubpenalties:D \__int_eval:w #1 \__int_eval_end: , }
\cs_new_protected:Npn \galley_save_display_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_displaywidowpenalties:D \c_zero - \c_one }
          \@@_save_display_widow_penalties:n
      }
  }
\cs_new:Npn \@@_save_display_widow_penalties:n #1
  { \int_use:N \etex_displaywidowpenalties:D \__int_eval:w #1 \__int_eval_end: , }
\cs_new_protected:Npn \galley_save_widow_penalties:N #1
  {
    \clist_set:Nx #1
      {
        \int_step_function:nnnN \c_one \c_one
          { \etex_widowpenalties:D \c_zero - \c_one }
          \@@_save_widow_penalties:n
      }
  }
\cs_new:Npn \@@_save_widow_penalties:n #1
  { \int_use:N \etex_widowpenalties:D \__int_eval:w #1 \__int_eval_end: , }
%    \end{macrocode}
%   This one is not an array, but is stored in a primitive, so there is
%   a simple conversion. The general interline penalty is always the
%   last value in the primitive array.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \galley_interline_penalty:
  { \int_use:N \etex_interlinepenalties:D \etex_interlinepenalties:D \c_zero }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Messages}
%
%    \begin{macrocode}
\__msg_kernel_new:nnn { galley } { no-line-to-end }
  { There's~no~line~here~to~end. }
%    \end{macrocode}
%
% \subsection{\LaTeXe{} functions}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \begin{macro}{\clearpage}
%   The \tn{clearpage} macro needs to place material into the correct
%   structures rather than directly onto the main vertical list. Other
%   than that it is the same as the \LaTeXe{} version.
%    \begin{macrocode}
\RenewDocumentCommand \clearpage { }
  {
    \mode_if_vertical:T
      {
        \int_compare:nNnT \@dbltopnum = \c_minus_one
          {
            \dim_compare:nNnT \tex_pagetotal:D < \topskip
              { \tex_hbox:D { } }
          }
      }
    \newpage
    \tl_gput_right:Nn \g_galley_whatsit_next_tl
      { \iow_shipout:Nx \c_minus_one { } }
    \tex_vbox:D { }
    \galley_penalty_set_single:n { -\@Mi }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\nobreak}
% \begin{macro}{\noindent}
% \begin{macro}{\vspace}
% In package mode, some of \LaTeXe{}'s functions are re-implemented using
% the galley system. Not all of the optional arguments currently work!
%    \begin{macrocode}
\RenewDocumentCommand \nobreak { }
  { \bool_gset_true:N \g_galley_no_break_next_bool }
%    \end{macrocode}
%  The \tn{noindent} primitive will causes problems, as it is used by
%  \LaTeXe{} documents to implicitly leave vertical mode as well as to
%  prevent indentation. Rather than patch \emph{every} place where we
%  need leave vertical mode, at the moment we stick with the primitive as
%  well as setting the galley flag.
%    \begin{macrocode}
\RenewDocumentCommand \noindent { }
  {
    \tex_noindent:D
    \bool_gset_false:N \g_galley_omit_next_indent_bool
  }
\RenewDocumentCommand \vspace { s m }
  {
    \IfBooleanTF #1
      { \galley_vspace_set_single:n {#2} }
      { \galley_vspace_set_single:n {#2} }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\\}
% \begin{macro}{\newline}
%   These functions pass their arguments straight through to the internal
%   implementation (which is currently just the \LaTeXe{} one recoded).
%    \begin{macrocode}
\RenewDocumentCommand \\ { s O { 0 pt } }
  { \galley_break_line:Nn #1 {#2} }
\RenewDocumentCommand \newline { }
  { \galley_break_line:Nn \c_true_bool { 0 pt } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{\LaTeXe{} fixes}
%
% Purely for testing, some internal \LaTeXe{} functions are altered to work
% with the mechanism here. This material is not comprehensive: additions are
% made as-needed for test purposes.
%
% \begin{macro}{\@@par}
%   The primitive is moved as otherwise the clever skipping code will fail.
%    \begin{macrocode}
\cs_set_eq:cN { @ @ par } \galley_par:
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@afterheading}
%   Set some flags and hope for the best!
%    \begin{macrocode}
\cs_set_protected_nopar:Npn \@afterheading
  {
    \bool_gset_true:N \g_galley_no_break_next_bool
    \if@afterindent
    \else
      \bool_gset_true:N \g_galley_omit_next_indent_bool
    \fi
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@hangfrom}
%   The \cs{tex_hangindent:D} primitive is no longer used, so the paragraph
%   shape is set in a different way. As a result, the label is part of the
%   same paragraph as the main body, hence the need to leave vertical mode.
%    \begin{macrocode}
\cs_set_protected:Npn \@hangfrom #1
  {
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \leavevmode
    \setbox \@tempboxa = \hbox { {#1} }
    \galley_parshape_set_single:nnnN
      \c_one
      { \box_wd:N \@tempboxa }
      \c_zero_dim
      \c_false_bool
    \bool_gset_true:N \g_galley_no_break_next_bool
    \bool_gset_true:N \g_galley_omit_next_indent_bool
    \box \@tempboxa
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@normalcr}
%   This is needed as \cs{@parboxrestore} sets |\\| equal to \cs{@normalcr},
%   and the new definition must be used
%    \begin{macrocode}
\cs_set_eq:Nc \@normalcr { \token_to_str:N \\ }
%    \end{macrocode}
%\end{macro}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex