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
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
|
% \iffalse meta-comment
%
% Copyright (C) 2020-2023
% Frank Mittelbach, Phelype Oleinik, The LaTeX Project
%
% This file is part of the LaTeX base system.
% -------------------------------------------
%
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This file has the LPPL maintenance status "maintained".
%
% The list of all files belonging to the LaTeX base distribution is
% given in the file `manifest.txt'. See also `legal.txt' for additional
% information.
%
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
%
% \fi
%
% \iffalse
%
%%% From File: lthooks.dtx
%
% \begin{macrocode}
\def\lthooksversion{v1.1f}
\def\lthooksdate{2023/06/16}
% \end{macrocode}
%
%<*driver>
\documentclass{l3doc}
\providecommand\InternalDetectionOff{}
\providecommand\InternalDetectionOn{}
\EnableCrossrefs
\CodelineIndex
\begin{document}
\DocInput{lthooks.dtx}
\end{document}
%</driver>
%
% \fi
%
%
% \providecommand\hook[1]{\texttt{#1}}
%
% \providecommand\fmi[1]{\marginpar{\footnotesize FMi: #1}}
% \providecommand\fmiinline[1]{\begin{quote}\itshape\footnotesize FMi: #1\end{quote}}
% \providecommand\pho[1]{\marginpar{\footnotesize PhO: #1}}
% \providecommand\phoinline[1]{\begin{quote}\itshape\footnotesize PhO: #1\end{quote}}
%
%
%
% \title{\LaTeX{}'s hook management\thanks{This module has version
% \lthooksversion\ dated \lthooksdate, \copyright\ \LaTeX\
% Project.}}
%
% \author{Frank Mittelbach\thanks{Code improvements for speed and other goodies by Phelype Oleinik}}
%
% \maketitle
%
%
% \tableofcontents
%
% \section{Introduction}
%
% Hooks are points in the code of commands or environments where it
% is possible to add processing code into existing commands. This
% can be done by different packages that do not know about each
% other and to allow for hopefully safe processing it is necessary
% to sort different chunks of code added by different packages into
% a suitable processing order.
%
% This is done by the packages adding chunks of code (via
% \cs{AddToHook}) and labeling their code with some label by
% default using the package name as a label.
%
% At \verb=\begin{document}= all code for a hook is then sorted
% according to some rules (given by \cs{DeclareHookRule}) for fast
% execution without processing overhead. If the hook code is
% modified afterwards (or the rules are changed),
% a new version for fast processing is generated.
%
% Some hooks are used already in the preamble of the document. If
% that happens then the hook is prepared for execution (and sorted)
% already at that point.
%
%
% \section{Package writer interface}
%
% The hook management system is offered as a set of CamelCase
% commands for traditional \LaTeXe{} packages (and for use in the
% document preamble if needed) as well as \texttt{expl3} commands
% for modern packages, that use the L3 programming layer of
% \LaTeX{}. Behind the scenes, a single set of data structures is
% accessed so that packages from both worlds can coexist and access
% hooks in other packages.
%
%
%
% \subsection{\LaTeXe\ interfaces}
%
% \subsubsection{Declaring hooks}
%
% With a few exceptions, hooks have to be declared before they can
% be used. The exceptions are the generic hooks for commands and
% environments (executed at \cs{begin} and \cs{end}), and the
% hooks run when loading files (see section~\ref{sec:generic}).
%
% \begin{function}{\NewHook}
% \begin{syntax}
% \cs{NewHook} \Arg{hook}
% \end{syntax}
% Creates a new \meta{hook}.
% If this hook is declared within a package it is suggested
% that its name is always structured as follows:
% \meta{package-name}\texttt{/}\meta{hook-name}. If necessary you
% can further subdivide the name by adding more \texttt{/} parts.
% If a hook name is already taken, an error is raised and the hook
% is not created.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\NewReversedHook}
% \begin{syntax}
% \cs{NewReversedHook} \Arg{hook}
% \end{syntax}
% Like \cs{NewHook} declares a new \meta{hook}.
% the difference is that the code chunks for this hook are in
% reverse order by default (those added last are executed first).
% Any rules for the hook are applied after the default ordering.
% See sections~\ref{sec:order} and \ref{sec:reversed-order}
% for further details.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\NewMirroredHookPair}
% \begin{syntax}
% \cs{NewMirroredHookPair} \Arg{hook-1} \Arg{hook-2}
% \end{syntax}
% A shorthand for
% \cs{NewHook}\Arg{hook-1}\cs{NewReversedHook}\Arg{hook-2}.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
%
% \begin{function}{\NewHookWithArguments}
% \begin{syntax}
% \cs{NewHookWithArguments} \Arg{hook} \Arg{number}
% \end{syntax}
% Creates a new \meta{hook} whose code takes \meta{number} arguments,
% and otherwise works exactly like \cs{NewHook}.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\NewReversedHookWithArguments}
% \begin{syntax}
% \cs{NewReversedHookWithArguments} \Arg{hook} \Arg{number}
% \end{syntax}
% Like \cs{NewReversedHook}, but creates a hook whose code takes
% \meta{number} arguments.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\NewMirroredHookPairWithArguments}
% \begin{syntax}
% \cs{NewMirroredHookPairWithArguments} \Arg{hook-1} \Arg{hook-2} \Arg{number}
% \end{syntax}
% A shorthand for
% \cs{NewHookWithArguments}\Arg{hook-1}\Arg{number}\\
% \cs{NewReversedHookWithArguments}\Arg{hook-2}\Arg{number}.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
%
% \subsubsection{Special declarations for generic hooks}
%
% The declarations here should normally not be used. They are available
% to provide support for special use cases mainly involving
% generic command hooks.
%
% \changes{v1.0p}{2021/08/20}{Documentation updates for generic hook commands (gh/638)}
%
% \begin{function}{\DisableGenericHook}
% \begin{syntax}
% \cs{DisableGenericHook} \Arg{hook}
% \end{syntax}
% After this declaration\footnotemark{} the \meta{hook} is no longer
% usable: Any further attempt to add code to it will result in an
% error and any use, e.g., via \cs{UseHook}, will simply do nothing.
%
% This is intended to be used with generic command hooks (see
% \texttt{ltcmdhooks-doc}) as depending on the definition of the
% command such generic hooks may be unusable. If that is known, a
% package developer can disable such hooks up front.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}\footnotetext{In the 2020/06 release this command was
% called \cs{DisableHook}, but that name was misleading as it
% shouldn't be used to disable non-generic hooks.}
%
%
% \begin{function}{\ActivateGenericHook}
% \begin{syntax}
% \cs{ActivateGenericHook} \Arg{hook}
% \end{syntax}
% This declaration activates a generic hook provided by a package/class
% (e.g., one used in code with \cs{UseHook} or
% \cs{UseOneTimeHook}) without it being explicitly declared with
% \cs{NewHook}).
% This command undoes the effect of \cs{DisableGenericHook}.
% If the hook is already activated, this command does nothing.
%
% See section~\ref{sec:generic-hooks} for a discussion of when this
% declaration is appropriate.
% \end{function}
%
%
%
%
%
% \subsubsection{Using hooks in code}
%
%
% \begin{function}{\UseHook}
% \begin{syntax}
% \cs{UseHook} \Arg{hook}
% \end{syntax}
% Execute the code stored in the \meta{hook}.
%
% Before \verb=\begin{document}= the fast execution code for a hook
% is not set up, so in order to use a hook there it is explicitly
% initialized first. As that involves assignments using a hook at
% those times is not 100\% the same as using it after
% \verb=\begin{document}=.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
% \begin{function}{\UseHookWithArguments}
% \begin{syntax}
% \cs{UseHookWithArguments} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
% \end{syntax}
% Execute the code stored in the \meta{hook} and pass the arguments
% \Arg{arg_1} through \Arg{arg_n} to the \meta{hook}. Otherwise, it
% works exactly like \cs{UseHook}.
% The \meta{number} should be the number of arguments declared for
% the hook. If the hook is not declared, this command does nothing
% and it will remove \meta{number} items from the input.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
% \begin{function}{\UseOneTimeHook}
% \begin{syntax}
% \cs{UseOneTimeHook} \Arg{hook}
% \end{syntax}
% Some hooks are only used (and can be only used) in one place, for
% example, those in \verb=\begin{document}= or
% \verb=\end{document}=. From that point onwards, adding to the hook
% through a defined \cs[no-index]{\meta{addto-cmd}} command (e.g.,
% \cs{AddToHook} or \cs{AtBeginDocument}, etc.\@) would have no
% effect (as would the use of such a command inside the hook code
% itself). It is therefore customary to redefine
% \cs{\meta{addto-cmd}} to simply process its argument, i.e.,
% essentially make it behave like \cs{@firstofone}.
%
% \cs{UseOneTimeHook} does that: it records that the hook has been
% consumed and any further attempt to add to it will result in
% executing the code to be added immediately.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally. See
% section~\ref{sec:default-label} for details.
%
% Using \cs{UseOneTimeHook} several times with the same
% \Arg{hook} means that it only executes the first time it is used.
% For example, if it is used in a command that can be called several times
% then the hook executes during only the \emph{first} invocation of that
% command; this allows its use as an \enquote{initialization hook}.
%
% Mixing \cs{UseHook} and \cs{UseOneTimeHook} for the same
% \Arg{hook} should be avoided, but if this is done then neither will execute
% after the first \cs{UseOneTimeHook}.
% \end{function}
%
% \begin{function}{\UseOneTimeHookWithArguments}
% \begin{syntax}
% \cs{UseOneTimeHookWithArguments} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
% \end{syntax}
% Works exactly like \cs{UseOneTimeHook}, but passes arguments
% \Arg{arg_1} through \Arg{arg_n} to the \meta{hook}.
% The \meta{number} should be the number of arguments declared for
% the hook. If the hook is not declared, this command does nothing
% and it will remove \meta{number} items from the input.
%
% It should be noted that after a one-time hook is used, it is no
% longer possible to use \cs{AddToHookWithArguments} or similar with
% that hook. \cs{AddToHook} continues to work as normal.
% Section~\ref{sec:hook-args} explains hooks with arguments.
% \end{function}
%
%
% \subsubsection{Updating code for hooks}
%
% \begin{function}{\AddToHook}
% \begin{syntax}
% \cs{AddToHook} \Arg{hook}\oarg{label}\Arg{code}
% \end{syntax}
% Adds \meta{code} to the \meta{hook} labeled by \meta{label}.
% When the optional argument \meta{label} is not provided, the
% \meta{default label} is used (see section~\ref{sec:default-label}).
% If \cs{AddToHook} is used in a package/class, the
% \meta{default label} is the package/class name, otherwise it is
% \hook{top-level} (the \hook{top-level} label is treated
% differently: see section~\ref{sec:top-level}).
%
% If there already exists code under the \meta{label} then the new
% \meta{code} is appended to the existing one (even if this is a reversed hook).
% If you want to replace existing code under the
% \meta{label}, first apply \cs{RemoveFromHook}.
%
% The hook doesn't have to exist for code to be added to
% it. However, if it is not declared, then obviously the
% added \meta{code} will never be executed. This
% allows for hooks to work regardless of package loading order and
% enables packages to add to hooks from other packages without
% worrying whether they are actually used in the current document.
% See section~\ref{sec:querying}.
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\AddToHookWithArguments}
% \begin{syntax}
% \cs{AddToHookWithArguments} \Arg{hook}\oarg{label}\Arg{code}
% \end{syntax}
% Works exactly like \cs{AddToHook}, except that the \meta{code} can
% access the arguments passed to the hook using \verb|#1|, \verb|#2|,
% \ldots, \verb|#n| (up to the number of arguments declared for the
% hook). If the \meta{code} should contain \emph{parameter tokens}
% (\verb|#|) that are not supposed to be understood as the arguments
% of the hook, such tokens should be doubled. For example, with
% \cs{AddToHook} one can write:
%\begin{verbatim}
% \AddToHook{myhook}{\def\foo#1{Hello, #1!}}
%\end{verbatim}
% but to achieve the same with \cs{AddToHookWithArguments}, one should
% write:
%\begin{verbatim}
% \AddToHookWithArguments{myhook}{\def\foo##1{Hello, ##1!}}
%\end{verbatim}
% because in the latter case, \verb|#1| refers to the first argument
% of the hook \hook{myhook}.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\RemoveFromHook}
% \begin{syntax}
% \cs{RemoveFromHook} \Arg{hook}\oarg{label}
% \end{syntax}
% Removes any code labeled by \meta{label} from the \meta{hook}.
% When the optional argument \meta{label} is not provided, the
% \meta{default label} is used (see section~\ref{sec:default-label}).
%
% If there is no code under the \meta{label} in the \meta{hook},
% or if the \meta{hook} does not exist, a warning is issued when
% you attempt to \cs{RemoveFromHook}, and the command is ignored.
% \cs{RemoveFromHook} should be used only when you know exactly what
% labels are in a hook. Typically this will be when some code gets added to a hook
% by a package, then later this code is removed by that same package.
% If you want to prevent the execution of code from another
% package, use the |voids| rule instead (see section~\ref{sec:rules}).
%
% If the optional \meta{label} argument is \texttt{*}, then all code chunks are
% removed. This is rather dangerous as it may well drop code from other
% packages (that one may not know about); it should therefore not be used
% in packages but only in document preambles!
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% \end{function}
%
% \medskip
%
% In contrast to the \texttt{voids} relationship between two labels
% in a \cs{DeclareHookRule} this is a destructive operation as the
% labeled code is removed from the hook data structure, whereas the
% relationship setting can be undone by providing a different
% relationship later.
%
% A useful application for this declaration inside the document body
% is when one wants to temporarily add code to hooks and later remove
% it again, e.g.,
%\begin{verbatim}
% \AddToHook{env/quote/before}{\small}
% \begin{quote}
% A quote set in a smaller typeface
% \end{quote}
% ...
% \RemoveFromHook{env/quote/before}
% ... now back to normal for further quotes
%\end{verbatim}
% Note that you can't cancel the setting with
%\begin{verbatim}
% \AddToHook{env/quote/before}{}
%\end{verbatim}
% because that only \enquote{adds} a further empty chunk of code to
% the hook. Adding \cs{normalsize} would work but that means the hook
% then contained \cs{small}\cs{normalsize} which means two font size
% changes for no good reason.
%
% The above is only needed if one wants to typeset several quotes in a
% smaller typeface. If the hook is only needed once then
% \cs{AddToHookNext} is simpler, because it resets itself after one use.
%
%
% \begin{function}{\AddToHookNext}
% \begin{syntax}
% \cs{AddToHookNext} \Arg{hook}\Arg{code}
% \end{syntax}
% Adds \meta{code} to the next invocation of the \meta{hook}.
% The code is executed after the normal hook code has finished and
% it is executed only once, i.e. it is deleted after it was used.
%
% Using this declaration is a global operation, i.e., the code is
% not lost even if the declaration is used inside a group and the
% next invocation of the hook happens after the end of that group.
% If the declaration is
% used several times before the hook is executed then all code is
% executed in the order in which it was declared.\footnotemark
%
% If this declaration is used with a one-time hook then the code
% is only ever used if the declaration comes before the hook’s
% invocation. This is because, in contrast
% to \cs{AddToHook}, the code in this declaration is not
% executed immediately in the case when the invocation of the hook
% has already happened---in other words, this code will truly execute
% only on the next invocation of the hook (and in the case of a
% one-time hook there is no such \enquote{next invocation}).
% This gives you a choice: should my code execute
% always, or should it execute only at the point where the
% one-time hook is used (and not at all if this is impossible)? For
% both of these possibilities there are use cases.
%
% It is possible to nest this declaration using the same hook (or
% different hooks): e.g.,
% \begin{quote}
% \cs{AddToHookNext}\Arg{hook}\verb={=\meta{code-1}^^A
% \cs{AddToHookNext}\Arg{hook}\Arg{code-2}\verb=}=
% \end{quote}
% will execute \meta{code-1} next time the \meta{hook} is used and at
% that point puts \meta{code-2} into the \meta{hook} so that it gets
% executed on following time the hook is run.
%
% A hook doesn't have to exist for code to be added to it. This
% allows for hooks to work regardless of package loading
% order.
% See section~\ref{sec:querying}.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}\footnotetext{There is
% no mechanism to reorder such code chunks (or delete them).}
%
% \begin{function}{\AddToHookNextWithArguments}
% \begin{syntax}
% \cs{AddToHookNextWithArguments} \Arg{hook}\Arg{code}
% \end{syntax}
% Works exactly like \cs{AddToHookNext}, but the \meta{code} can
% contain references to the arguments of the \meta{hook} as described
% for \cs{AddToHookWithArguments} above.
% Section~\ref{sec:hook-args} explains hooks with arguments.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
%
%
% \begin{function}{\ClearHookNext}
% \begin{syntax}
% \cs{ClearHookNext}\Arg{hook}
% \end{syntax}
% Normally \cs{AddToHookNext} is only used when you know precisely
% where it will apply and why you want some extra code at that
% point. However, there are a few use cases in which such a
% declaration needs to be canceled, for example, when
% discarding a page with \cs{DiscardShipoutBox} (but even then not
% always), and in such situations \cs{ClearHookNext} can be
% used.
% \end{function}
%
%
%
%
%
% \subsubsection{Hook names and default labels}
% \label{sec:default-label}
%
% It is best practice to use \cs{AddToHook} in packages or classes
% \emph{without specifying a \meta{label}} because then the package
% or class name is automatically used, which is helpful if rules are
% needed, and avoids mistyping the \meta{label}.
%
% Using an explicit \meta{label} is only necessary in very specific
% situations, e.g., if you want to add several chunks of code into a
% single hook and have them placed in different parts of the hook
% (by providing some rules).
%
% The other case is when you develop a larger package with several
% sub-packages. In that case you may want to use the same
% \meta{label} throughout the sub-packages in order to avoid
% that the labels change if you internally reorganize your code.
%
% Except for \cs{UseHook}, \cs{UseOneTimeHook} and \cs{IfHookEmptyTF}
% (and their \pkg{expl3} interfaces \cs{hook_use:n},
% \cs{hook_use_once:n} and \cs{hook_if_empty:nTF}), all \meta{hook}
% and \meta{label} arguments are processed in the same way: first,
% spaces are trimmed around the argument, then it is fully expanded
% until only character tokens remain. If the full expansion of the
% \meta{hook} or \meta{label} contains a non-expandable non-character
% token, a low-level \TeX{} error is raised (namely, the \meta{hook} is
% expanded using \TeX's \cs{csname}\ldots\cs{endcsname}, as such,
% Unicode characters are allowed in \meta{hook} and \meta{label}
% arguments). The arguments of \cs{UseHook}, \cs{UseOneTimeHook}, and
% \cs{IfHookEmptyTF} are
% processed much in the same way except that spaces are not trimmed
% around the argument, for better performance.
%
% It is not enforced, but highly recommended that the hooks defined by
% a package, and the \meta{labels} used to add code to other hooks
% contain the package name to easily identify the source of the code
% chunk and to prevent clashes. This should be the standard practice,
% so this hook management code provides a shortcut to refer to the
% current package in the name of a \meta{hook} and in a \meta{label}.
% If the \meta{hook} name or the \meta{label} consist just of a single dot
% (|.|), or starts with a dot followed by a slash (|./|) then the dot
% denotes the \meta{default label} (usually the current package or class
% name---see~\cs{SetDefaultHookLabel}).
% A \enquote{|.|} or \enquote{|./|} anywhere else in a \meta{hook} or in
% \meta{label} is treated literally and is not replaced.
%
% For example,
% inside the package \texttt{mypackage.sty}, the default label is
% \texttt{mypackage}, so the instructions:
% \begin{verbatim}
% \NewHook {./hook}
% \AddToHook {./hook}[.]{code} % Same as \AddToHook{./hook}{code}
% \AddToHook {./hook}[./sub]{code}
% \DeclareHookRule{begindocument}{.}{before}{babel}
% \AddToHook {file/foo.tex/after}{code}
% \end{verbatim}
% are equivalent to:
% \begin{verbatim}
% \NewHook {mypackage/hook}
% \AddToHook {mypackage/hook}[mypackage]{code}
% \AddToHook {mypackage/hook}[mypackage/sub]{code}
% \DeclareHookRule{begindocument}{mypackage}{before}{babel}
% \AddToHook {file/foo.tex/after}{code} % unchanged
% \end{verbatim}
%
% The \meta{default label} is automatically set equal to the name of the
% current package or class at the time the package is loaded. If the
% hook command is used outside of a package, or the current file wasn't
% loaded with \cs{usepackage} or \cs{documentclass}, then the
% \texttt{top-level} is used as the \meta{default label}. This may have
% exceptions---see \cs{PushDefaultHookLabel}.
%
% This syntax is available in all \meta{label} arguments and most
% \meta{hook} arguments, both in the \LaTeXe{} interface, and the \LaTeX3
% interface described in section~\ref{sec:l3hook-interface}.
%
% Note, however, that the replacement of |.| by the \meta{default label}
% takes place when the hook command is executed, so actions that are
% somehow executed after the package ends will have the wrong
% \meta{default label} if the dot-syntax is used. For that reason,
% this syntax is not available in \cs{UseHook} (and \cs{hook_use:n})
% because the hook is most of the time used outside of the package file
% in which it was defined. This syntax is also not available in the hook
% conditionals \cs{IfHookEmptyTF} (and \cs{hook_if_empty:nTF}), because these
% conditionals are used in some performance-critical parts of the hook
% management code, and because they are usually used to refer to other
% package's hooks, so the dot-syntax doesn't make much sense.
%
% In some cases, for example in large packages, one may want to separate
% the code in logical parts, but still use the main package name as the
% \meta{label}, then the \meta{default label} can be set using
% \cs{PushDefaultHookLabel}\verb={...}=\,\ldots\cs{PopDefaultHookLabel}
% or \cs{SetDefaultHookLabel}\verb={...}=.
%
% \begin{function}{\PushDefaultHookLabel,\PopDefaultHookLabel}
% \begin{syntax}
% \cs{PushDefaultHookLabel} \Arg{default label}
% \quad \meta{code}
% \cs{PopDefaultHookLabel}
% \end{syntax}
% \cs{PushDefaultHookLabel} sets the current \meta{default label} to
% be used in \meta{label} arguments, or when replacing a leading
% ``|.|'' (see above). \cs{PopDefaultHookLabel} reverts the
% \meta{default label} to its previous value.
%
% Inside a package or class, the \meta{default label} is equal to the
% package or class name, unless explicitly changed. Everywhere else,
% the \meta{default label} is |top-level| (see
% section~\ref{sec:top-level}) unless explicitly changed.
%
% The effect of \cs{PushDefaultHookLabel} holds until the next
% \cs{PopDefaultHookLabel}. \cs{usepackage} (and \cs{RequirePackage}
% and \cs{documentclass}) internally use
% \begin{quote}
% \cs{PushDefaultHookLabel}\Arg{package name} \\
% \null \quad \meta{package code} \\
% \cs{PopDefaultHookLabel}
% \end{quote}
% to set the \meta{default label} for the package or class file.
% Inside the \meta{package code} the \meta{default label} can also be
% changed with \cs{SetDefaultHookLabel}. \cs{input} and other
% file input-related commands from the \LaTeX{} kernel do not use
% \cs{PushDefaultHookLabel}, so code within files loaded by these
% commands does \emph{not} get a dedicated \meta{label}! (that is, the
% \meta{default label} is the current active one when the file was
% loaded.)
%
% Packages that provide their own package-like interfaces
% (Ti\textit{k}Z's \cs{usetikzlibrary}, for example) can use
% \cs{PushDefaultHookLabel} and \cs{PopDefaultHookLabel} to set
% dedicated labels and to emulate \cs{usepackage}-like hook behavior
% within those contexts.
%
% The |top-level| label is treated differently, and is reserved to the
% user document, so it is not allowed to change the
% \meta{default label} to |top-level|.
% \end{function}
%
% \begin{function}{\SetDefaultHookLabel}
% \begin{syntax}
% \cs{SetDefaultHookLabel} \Arg{default label}
% \end{syntax}
% Similarly to \cs{PushDefaultHookLabel},
% sets the current \meta{default label} to
% be used in \meta{label} arguments, or when replacing a leading
% ``|.|''. The effect holds until the label is changed again or until
% the next \cs{PopDefaultHookLabel}. The difference between
% \cs{PushDefaultHookLabel} and \cs{SetDefaultHookLabel} is that the
% latter does not save the current \meta{default label}.
%
% This command is useful when a large package is composed of several
% smaller packages, but all should have the same \meta{label}, so
% \cs{SetDefaultHookLabel} can be used at the beginning of each
% package file to set the correct label.
%
% \cs{SetDefaultHookLabel} is not allowed in the main document, where
% the \meta{default label} is |top-level| and there is no
% \cs{PopDefaultHookLabel} to end its effect.
% It is also not allowed to change the \meta{default label} to
% |top-level|.
% \end{function}
%
% \subsubsection{The \texttt{top-level} label}
% \label{sec:top-level}
%
% The |top-level| label, assigned to code added from the main document,
% is different from other labels. Code added to hooks (usually
% \cs{AtBeginDocument}) in the preamble is almost always to change
% something defined by a package, so it should go at the very end of the
% hook.
%
% Therefore, code added in the |top-level| is always executed at the end
% of the hook, regardless of where it was declared. If the hook is
% reversed (see \cs{NewReversedHook}), the |top-level| chunk is executed
% at the very beginning instead.
%
% Rules regarding |top-level| have no effect: if a user wants to have a
% specific set of rules for a code chunk, they should use a different
% label to said code chunk, and provide a rule for that label instead.
%
% The |top-level| label is exclusive for the user, so trying to add code
% with that label from a package results in an error.
%
% \subsubsection{Defining relations between hook code}
% \label{sec:rules}
%
% The default assumption is that code added to hooks by different
% packages are independent and the order in which they are executed is
% irrelevant. While this is true in many cases it is obviously false
% in others.
%
% Before the hook management system was introduced
% packages had to take elaborate precaution to determine of some other
% package got loaded as well (before or after) and find some ways to
% alter its behavior accordingly. In addition is was often the user's
% responsibility to load packages in the right order so that code
% added to hooks got added in the right order and some cases even
% altering the loading order wouldn't resolve the conflicts.
%
% With the new hook management system it is now possible to define
% rules (i.e., relationships) between code chunks added by different
% packages and explicitly describe in which order they should be
% processed.
%
% \begin{function}{\DeclareHookRule}
% \begin{syntax}
% \cs{DeclareHookRule} \Arg{hook}\Arg{label1}\Arg{relation}\Arg{label2}
% \end{syntax}
% Defines a relation between \meta{label1} and \meta{label2} for a
% given \meta{hook}. If \meta{hook} is \texttt{??} this defines a default
% relation for all hooks that use the two labels, i.e., that have
% chunks of code labeled with \meta{label1} and \meta{label2}.
% Rules specific to a given hook take precedence over default
% rules that use \texttt{??} as the \meta{hook}.
%
% Currently, the supported relations are the following:
% \begin{itemize}
%
% \item[\texttt{before} or \texttt{\string<}]
%
% Code for \meta{label1} comes before code for \meta{label2}.
%
% \item[\texttt{after} or \texttt{\string>}]
% Code for \meta{label1} comes after code for \meta{label2}.
%
% \item[\texttt{incompatible-warning}]
%
% Only code for either \meta{label1} or \meta{label2} can appear
% for that hook (a way to say that two packages---or parts of
% them---are incompatible). A warning is raised if both labels
% appear in the same hook.
%
% \item[\texttt{incompatible-error}]
%
% Like \texttt{incompatible-error} but instead of a warning a
% \LaTeX{} error is raised, and the code for both labels are
% dropped from that hook until the conflict is resolved.
%
% \item[\texttt{voids}]
%
% Code for \meta{label1} overwrites code for \meta{label2}. More
% precisely, code for \meta{label2} is dropped for that
% hook. This can be used, for example if one package is a
% superset in functionality of another one and therefore wants to
% undo code in some hook and replace it with its own version.
%
% \item[\texttt{unrelated}]
%
% The order of code for \meta{label1} and \meta{label2} is
% irrelevant. This rule is there to undo an incorrect rule
% specified earlier.
%
% \end{itemize}
% There can only be a single relation between two labels for a
% given hook,
% i.e., a later \cs{DeclareHookrule} overwrites any previous
% declaration.
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
%
% \end{function}
%
%
% \begin{function}{\ClearHookRule}
% \begin{syntax}
% \cs{ClearHookRule}\Arg{hook}\Arg{label1}\Arg{label2}
% \end{syntax}
% Syntactic sugar for saying that \meta{label1} and \meta{label2}
% are unrelated for the given \meta{hook}.
% \end{function}
%
%
%
% \begin{function}{\DeclareDefaultHookRule}
% \begin{syntax}
% \cs{DeclareDefaultHookRule}\Arg{label1}\Arg{relation}\Arg{label2}
% \end{syntax}
% This sets up a relation between \meta{label1} and \meta{label2}
% for all hooks unless overwritten by a specific rule for a hook.
% Useful for cases where one package has a specific relation to
% some other package, e.g., is \texttt{incompatible} or always
% needs a special ordering \texttt{before} or \texttt{after}.
% (Technically it is just a shorthand for using \cs{DeclareHookRule}
% with \texttt{??} as the hook name.)
%
% Declaring default rules is only supported in the document
% preamble.\footnotemark{}
%
% The \meta{label} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}\footnotetext{Trying to do so, e.g., via
% \cs{DeclareHookRule} with \texttt{??} has bad side-effects and
% is not supported (though not explicitly caught for performance
% reasons).}
%
%
%
% \subsubsection{Querying hooks}
% \label{sec:querying}
%
% Simpler data types, like token lists, have three possible states; they
% can:
% \begin{itemize}
% \item exist and be empty;
% \item exist and be non-empty; and
% \item not exist (in which case emptiness doesn't apply);
% \end{itemize}
% Hooks are a bit more complicated:
% a hook may exist or not, and independently it may or may not be empty.
% This means that even a hook that doesn't exist may be non-empty and
% it can also be disabled.
%
% This seemingly strange state may happen when, for example, package~$A$
% defines hook \hook{A/foo}, and package $B$ adds some code to that
% hook. However, a document may load package $B$ before package $A$, or
% may not load package $A$ at all. In both cases some code is added to
% hook \hook{A/foo} without that hook being defined yet, thus that
% hook is said to be non-empty, whereas it doesn't exist. Therefore,
% querying the existence of a hook doesn't imply its emptiness, neither
% does the other way around.
%
% Given that code or rules can be added to a hook even if it doesn't
% physically exist yet, means that a querying its existence has no
% real use case (in contrast to other variables that can only be
% update if they have already been declared). For that reason only the
% test for emptiness has a public interface.
%
% A hook is said to be empty when no code was added to it, either to
% its permanent code pool, or to its ``next'' token list. The hook
% doesn't need to be declared to have code added to its code pool.
% A hook is said to exist when it was declared with \cs{NewHook} or
% some variant thereof. Generic hooks such as \hook{file} and \hook{env} hooks are
% automatically declared when code is added to them.
%
% \begin{function}[EXP]{\IfHookEmptyTF}
% \begin{syntax}
% \cs{IfHookEmptyTF} \Arg{hook} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{hook} is empty (\emph{i.e.}, no code was added to
% it using either \cs{AddToHook} or \cs{AddToHookNext}) or such code
% was removed again (via \cs{RemoveFromHook}), and
% branches to either \meta{true code} or \meta{false code} depending
% on the result.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
%
%
% \subsubsection{Displaying hook code}
%
% If one has to adjust the code execution in a hook using a hook
% rule it is helpful to get some information about the code
% associated with a hook, its current order and the existing rules.
%
% \begin{function}{\ShowHook,\LogHook}
% \begin{syntax}
% \cs{ShowHook} \Arg{hook}
% \cs{LogHook} \Arg{hook}
% \end{syntax}
% Displays information about the \meta{hook} such as
% \begin{itemize}
% \item
% the code chunks (and their labels) added to it,
% \item
% any rules set up to order them,
% \item
% the computed order in which the chunks are executed,
% \item
% any code executed on the next invocation only.
% \end{itemize}
% \end{function}
%
% \cs{LogHook} prints the information to the |.log| file, and
% \cs{ShowHook} prints them to the terminal/command window and starts
% \TeX's prompt (only in \cs{errorstopmode}) to wait for user action.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
%
%^^A % Code for the listing below:
%^^A \NewHook{example-hook}
%^^A \AddToHook{example-hook}{[code from 'top-level']}
%^^A \AddToHook{example-hook}[foo]{[code from package 'foo']}
%^^A \AddToHook{example-hook}[bar]{[from package 'bar']}
%^^A \AddToHook{example-hook}[baz]{[package 'baz' is here]}
%^^A \AddToHookNext{example-hook}{[one-time code]}
%^^A \DeclareHookRule{example-hook}{baz}{before}{foo}
%^^A \DeclareDefaultHookRule{bar}{after}{baz}
%^^A \ShowHook{example-hook}
%
% \def\theFancyVerbLine{\textcolor[gray]{0.5}{%^^A
% \sffamily\tiny\arabic{FancyVerbLine}}}
%
% \bigskip
% Suppose a hook \texttt{example-hook} whose output of
% \cs{ShowHook}|{example-hook}| is:
% \begin{verbatim}[numbers=left]
% -> The hook 'example-hook':
% > Code chunks:
% > foo -> [code from package 'foo']
% > bar -> [from package 'bar']
% > baz -> [package 'baz' is here]
% > Document-level (top-level) code (executed last):
% > -> [code from 'top-level']
% > Extra code for next invocation:
% > -> [one-time code]
% > Rules:
% > foo|baz with relation >
% > baz|bar with default relation <
% > Execution order (after applying rules):
% > baz, foo, bar.
% \end{verbatim}
%
% In the listing above, lines~3 to~5 show the three code chunks added
% to the hook and their respective labels in the format
% \begin{quote}
% \quad \meta{label}\verb| -> |\meta{code}
% \end{quote}
%
% Line~7 shows the code chunk added by the user in the main document
% (labeled |top-level|) in the format
% \begin{quote}
% \quad\verb|Document-level (top-level) code (executed |%^^A
% \meta{first\texttt{\string|}last}\verb|):|\\
% \quad\verb| -> |\meta{\texttt{top-level} code}
% \end{quote}
% This code will be either the first or last code executed by the hook
% (|last| if the hook is normal, |first| if it is reversed). This
% chunk is not affected by rules and does not take part in sorting.
%
% Line~9 shows the code chunk for the next execution of the hook in
% the format
% \begin{quote}
% \quad \verb|-> |\meta{next-code}
% \end{quote}
% This code will be used and disappear at the next
% \verb|\UseHook{example-hook}|, in contrast to the chunks mentioned
% earlier, which can only be removed from that hook by doing
% \verb|\RemoveFromHook{|\meta{label}|}[example-hook]|.
%
% Lines~11 and~12 show the rules declared that affect this hook in the
% format
% \begin{quote}
% \quad \meta{label-1}\verb+|+\meta{label-2}| with |%^^A
% \meta{\texttt{default}?}| relation |\meta{relation}
% \end{quote}
% which means that the \meta{relation} applies to \meta{label-1} and
% \meta{label-2}, in that order, as detailed in \cs{DeclareHookRule}.
% If the relation is \texttt{default} it means that this rule applies
% to \meta{label-1} and \meta{label-2} in \emph{all} hooks, (unless
% overridden by a non-default relation).
%
% Finally, line~14 lists the labels in the hook after sorting;
% that is, in the order they will be executed when the hook is used.
%
%
% \subsubsection{Debugging hook code}
%
% \begin{function}{\DebugHooksOn,\DebugHooksOff}
% \begin{syntax}
% \cs{DebugHooksOn}
% \end{syntax}
% Turn the debugging of hook code on or off. This displays most changes
% made to the hook data structures. The output is rather coarse and
% not really intended for normal use.
% \end{function}
%
%
% \subsection{L3 programming layer (\texttt{expl3}) interfaces}
% \label{sec:l3hook-interface}
%
%
% This is a quick summary of the \LaTeX3 programming interfaces for
% use with packages written in \texttt{expl3}. In contrast to the
% \LaTeXe{} interfaces they always use mandatory arguments only, e.g.,
% you always have to specify the \meta{label} for a code chunk. We
% therefore suggest to use the declarations discussed in the previous
% section even in \texttt{expl3} packages, but the choice is yours.
%
%
% \begin{function}{
% \hook_new:n,
% \hook_new_reversed:n,
% \hook_new_pair:nn
% }
% \begin{syntax}
% \cs{hook_new:n} \Arg{hook}
% \cs{hook_new_reversed:n} \Arg{hook}
% \cs{hook_new_pair:nn} \Arg{hook-1} \Arg{hook-2}
% \end{syntax}
% Creates a new \meta{hook} with normal or reverse ordering of code
% chunks. \cs{hook_new_pair:nn} creates a pair of such hooks with
% \Arg{hook-2} being a reversed hook.
% If a hook name is already taken, an error is raised and the hook
% is not created.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{
% \hook_new_with_args:nn,
% \hook_new_reversed_with_args:nn,
% \hook_new_pair_with_args:nnn
% }
% \begin{syntax}
% \cs{hook_new_with_args:nn} \Arg{hook} \Arg{number}
% \cs{hook_new_reversed_with_args:nn} \Arg{hook} \Arg{number}
% \cs{hook_new_pair_with_args:nnn} \Arg{hook-1} \Arg{hook-2} \Arg{number}
% \end{syntax}
% Creates a new \meta{hook} with normal or reverse ordering of code
% chunks, that takes \meta{number} arguments from the input stream
% when it is used. \cs{hook_new_pair_with_args:nn} creates a pair of
% such hooks with \Arg{hook-2} being a reversed hook.
% If a hook name is already taken, an error is raised and the hook
% is not created.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
%
%
% \begin{function}{\hook_disable_generic:n}
% \begin{syntax}
% \cs{hook_disable_generic:n} \Arg{hook}
% \end{syntax}
% Marks \Arg{hook} as disabled. Any further attempt to add code to
% it or declare it, will result in an error and any call to
% \cs{hook_use:n} will simply do nothing.
%
% This declaration is intended for use with generic hooks that are
% known not to work (see \texttt{ltcmdhooks-doc}) if they receive
% code.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\hook_activate_generic:n}
% \begin{syntax}
% \cs{hook_activate_generic:n} \Arg{hook}
% \end{syntax}
% This is like \cs{hook_new:n} but it does nothing if the hook was previously
% declared with \cs{hook_new:n}. This declaration should be used
% only in special situations, e.g., when a command from another package
% needs to be altered and it is not clear whether a
% generic \hook{cmd} hook (for that command) has been previously
% explicitly declared.
%
% Normally \cs{hook_new:n} should be used instead of this.
% \end{function}
%
%
%
%
% \begin{function}{\hook_use:n,\hook_use:nnw}
% \begin{syntax}
% \cs{hook_use:n} \Arg{hook}
% \cs{hook_use:nnw} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
% \end{syntax}
% Executes the \Arg{hook} code followed (if set up) by the code for next
% invocation only, then empties that next invocation code.
% \cs{hook_use:nnw} should be used for hooks declared with arguments,
% and should be followed by as many brace groups as the declared
% number of arguments.
% The \meta{number} should be the number of arguments declared for
% the hook. If the hook is not declared, this command does nothing
% and it will remove \meta{number} items from the input.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
% \begin{function}{\hook_use_once:n,\hook_use_once:nnw}
% \begin{syntax}
% \cs{hook_use_once:n} \Arg{hook}
% \cs{hook_use_once:nnw} \Arg{hook} \Arg{number} \Arg{arg_1} \ldots \Arg{arg_n}
% \end{syntax}
% Changes the \Arg{hook} status so that from now on any addition to
% the hook code is executed immediately. Then execute any
% \Arg{hook} code already set up.
% \cs{hook_use_once:nnw} should be used for hooks declared with arguments,
% and should be followed by as many brace groups as the declared
% number of arguments.
% The \meta{number} should be the number of arguments declared for
% the hook. If the hook is not declared, this command does nothing
% and it will remove \meta{number} items from the input.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
% \begin{function}{
% \hook_gput_code:nnn,
% \hook_gput_code_with_args:nnn
% }
% \begin{syntax}
% \cs{hook_gput_code:nnn} \Arg{hook} \Arg{label} \Arg{code}
% \cs{hook_gput_code_with_args:nnn} \Arg{hook} \Arg{label} \Arg{code}
% \end{syntax}
% Adds a chunk of \meta{code} to the \meta{hook} labeled
% \meta{label}. If the label already exists the \meta{code} is
% appended to the already existing code.
%
% If \cs{hook_gput_code_with_args:nnn} is used, the \meta{code}
% can access the arguments passed to \cs{hook_use:nnw}
% (or~\cs{hook_use_once:nnw}) with \verb|#1|, \verb|#2|, \ldots,
% \verb|#n| (up to the number of arguments declared for the hook).
% In that case, if an actual parameter token should be added to the
% code, it should be doubled.
%
% If code is added to an external \meta{hook} (of the kernel or
% another package) then the convention is to use the package name
% as the \meta{label} not some internal module name or some other
% arbitrary string.
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{
% \hook_gput_next_code:nn,
% \hook_gput_next_code_with_args:nn,
% }
% \begin{syntax}
% \cs{hook_gput_next_code:nn} \Arg{hook} \Arg{code}
% \end{syntax}
% Adds a chunk of \meta{code} for use only in the next invocation of the
% \meta{hook}. Once used it is gone.
%
% If \cs{hook_gput_next_code_with_args:nn} is used, the \meta{code}
% can access the arguments passed to \cs{hook_use:nnw}
% (or~\cs{hook_use_once:nnw}) with \verb|#1|, \verb|#2|, \ldots,
% \verb|#n| (up to the number of arguments declared for the hook).
% In that case, if an actual parameter token should be added to the
% code, it should be doubled.
%
% This is simpler than \cs{hook_gput_code:nnn}, the code is simply
% appended to the hook in the order of declaration at the very end,
% i.e., after all standard code for the hook got executed.
% Thus if one needs to undo what the standard does one has to do
% that as part of \meta{code}.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
%
% \begin{function}{\hook_gclear_next_code:n}
% \begin{syntax}
% \cs{hook_gclear_next_code:n} \Arg{hook}
% \end{syntax}
% Undo any earlier \cs{hook_gput_next_code:nn}.
% \end{function}
%
%
%
% \begin{function}{\hook_gremove_code:nn}
% \begin{syntax}
% \cs{hook_gremove_code:nn} \Arg{hook} \Arg{label}
% \end{syntax}
% Removes any code for \meta{hook} labeled \meta{label}.
%
% If there is no code under the \meta{label} in the \meta{hook},
% or if the \meta{hook} does not exist, a warning is issued when
% you attempt to use \cs{hook_gremove_code:nn}, and the command is ignored.
%
% If the second argument is \texttt{*}, then all code chunks are
% removed. This is rather dangerous as it drops code from other
% packages one may not know about, so think twice before using
% that!
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% \end{function}
%
%
% \begin{function}{\hook_gset_rule:nnnn}
% \begin{syntax}
% \cs{hook_gset_rule:nnnn} \Arg{hook} \Arg{label1} \Arg{relation} \Arg{label2}
% \end{syntax}
% Relate \meta{label1} with \meta{label2} when used in \meta{hook}.
% See \cs{DeclareHookRule} for the allowed \meta{relation}s.
% If \meta{hook} is \texttt{??} a default rule is specified.
%
% The \meta{hook} and \meta{label} can be specified using the
% dot-syntax to denote the current package name.
% See section~\ref{sec:default-label}.
% The dot-syntax is parsed in both \meta{label} arguments, but it
% usually makes sense to be used in only one of them.
% \end{function}
%
% \begin{function}[pTF]{\hook_if_empty:n}
% \begin{syntax}
% \cs{hook_if_empty:nTF} \Arg{hook} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{hook} is empty (\emph{i.e.}, no code was added to
% it using either \cs{AddToHook} or \cs{AddToHookNext}), and
% branches to either \meta{true code} or \meta{false code} depending
% on the result.
%
% The \meta{hook} \emph{cannot} be specified using the dot-syntax.
% A leading |.| is treated literally.
% \end{function}
%
%
% \begin{function}{\hook_show:n,\hook_log:n}
% \begin{syntax}
% \cs{hook_show:n} \Arg{hook}
% \cs{hook_log:n} \Arg{hook}
% \end{syntax}
% Displays information about the \meta{hook} such as
% \begin{itemize}
% \item
% the code chunks (and their labels) added to it,
% \item
% any rules set up to order them,
% \item
% the computed order in which the chunks are executed,
% \item
% any code executed on the next invocation only.
% \end{itemize}
%
% \cs{hook_log:n} prints the information to the |.log| file, and
% \cs{hook_show:n} prints them to the terminal/command window and starts
% \TeX's prompt (only if \cs{errorstopmode}) to wait for user action.
%
% The \meta{hook} can be specified using the dot-syntax to denote
% the current package name. See section~\ref{sec:default-label}.
% \end{function}
%
% \begin{function}{\hook_debug_on:,\hook_debug_off:}
% \begin{syntax}
% \cs{hook_debug_on:}
% \end{syntax}
% Turns the debugging of hook code on or off. This displays changes
% to the hook data.
% \end{function}
%
%
%
% \subsection{On the order of hook code execution} \label{sec:order}
%
% Chunks of code for a \meta{hook} under different labels are supposed
% to be independent if there are no special rules set up that
% define a relation between the chunks. This means that you can't
% make assumptions about the order of execution!
%
% Suppose you have the following declarations:
%\begin{verbatim}
% \NewHook{myhook}
% \AddToHook{myhook}[packageA]{\typeout{A}}
% \AddToHook{myhook}[packageB]{\typeout{B}}
% \AddToHook{myhook}[packageC]{\typeout{C}}
%\end{verbatim}
% then executing the hook with \cs{UseHook} will produce the
% typeout \texttt{A} \texttt{B} \texttt{C} in that order. In other
% words, the execution order is computed to be \texttt{packageA},
% \texttt{packageB}, \texttt{packageC} which you can verify with
% \cs{ShowHook}\texttt{\{myhook\}}:
%\begin{verbatim}
% -> The hook 'myhook':
% > Code chunks:
% > packageA -> \typeout {A}
% > packageB -> \typeout {B}
% > packageC -> \typeout {C}
% > Document-level (top-level) code (executed last):
% > ---
% > Extra code for next invocation:
% > ---
% > Rules:
% > ---
% > Execution order:
% > packageA, packageB, packageC.
%\end{verbatim}
% The reason is that the code chunks are internally saved in a property list
% and the initial order of such a property list is the order in
% which key-value pairs got added. However, that is only true if
% nothing other than adding happens!
%
% Suppose, for example, you want to replace the code chunk for
% \texttt{packageA}, e.g.,
%\begin{verbatim}
% \RemoveFromHook{myhook}[packageA]
% \AddToHook{myhook}[packageA]{\typeout{A alt}}
%\end{verbatim}
% then your order becomes \texttt{packageB},
% \texttt{packageC}, \texttt{packageA} because the label got removed
% from the property list and then re-added (at its end).
%
% While that may not be too surprising, the execution order is
% also sometimes altered if you add a redundant rule, e.g. if you specify
%\begin{verbatim}
% \DeclareHookRule{myhook}{packageA}{before}{packageB}
%\end{verbatim}
% instead of the previous lines we get
%\begin{verbatim}
% -> The hook 'myhook':
% > Code chunks:
% > packageA -> \typeout {A}
% > packageB -> \typeout {B}
% > packageC -> \typeout {C}
% > Document-level (top-level) code (executed last):
% > ---
% > Extra code for next invocation:
% > ---
% > Rules:
% > packageB|packageA with relation >
% > Execution order (after applying rules):
% > packageA, packageC, packageB.
%\end{verbatim}
% As you can see the code chunks are still in the same order, but
% in the execution order for the labels \texttt{packageB} and
% \texttt{packageC} have
% swapped places.
% The reason is that, with the rule there are two orders that
% satisfy it, and the algorithm for sorting happened to pick a
% different one compared to the case without rules (where it
% doesn't run at all as there is nothing to resolve).
% Incidentally, if we had instead specified the redundant rule
%\begin{verbatim}
% \DeclareHookRule{myhook}{packageB}{before}{packageC}
%\end{verbatim}
% the execution order would not have changed.
%
% In summary: it is not possible to rely on the order of execution
% unless there are rules that partially or fully define the order
% (in which you can rely on them being fulfilled).
%
%
% \subsection{The use of \enquote{reversed} hooks} \label{sec:reversed-order}
%
% You may have wondered why you can declare a \enquote{reversed} hook
% with \cs{NewReversedHook} and what that does exactly.
%
% In short: the execution order of a reversed hook (without any
% rules!) is exactly reversed to the order you would have gotten for
% a hook declared with \cs{NewHook}.
%
% This is helpful if you have a pair of hooks where you expect to see
% code added that involves grouping, e.g., starting an environment
% in the first and closing that environment in the second hook.
% To give a somewhat contrived example\footnote{there are simpler
% ways to achieve the same effect.}, suppose there is a package
% adding the following:
%\begin{verbatim}
% \AddToHook{env/quote/before}[package-1]{\begin{itshape}}
% \AddToHook{env/quote/after} [package-1]{\end{itshape}}
%\end{verbatim}
% As a result, all quotes will be in italics.
% Now suppose further that another |package-too| makes the quotes
% also in blue and therefore adds:
%\begin{verbatim}
% \usepackage{color}
% \AddToHook{env/quote/before}[package-too]{\begin{color}{blue}}
% \AddToHook{env/quote/after} [package-too]{\end{color}}
%\end{verbatim}
% Now if the \hook{env/quote/after} hook would be a normal hook we
% would get the same execution order in both hooks, namely:
%\begin{verbatim}
% package-1, package-too
%\end{verbatim}
% (or vice versa) and as a result, would get:
%\begin{verbatim}
% \begin{itshape}\begin{color}{blue} ...
% \end{itshape}\end{color}
%\end{verbatim}
% and an error message saying that \verb=\begin{color}= was ended by
% \verb=\end{itshape}=.
% With \hook{env/quote/after} declared as a reversed hook the
% execution order is reversed and so all environments are closed in
% the correct sequence and \cs{ShowHook} would give us the
% following output:
%\begin{verbatim}
% -> The hook 'env/quote/after':
% > Code chunks:
% > package-1 -> \end {itshape}
% > package-too -> \end {color}
% > Document-level (top-level) code (executed first):
% > ---
% > Extra code for next invocation:
% > ---
% > Rules:
% > ---
% > Execution order (after reversal):
% > package-too, package-1.
%\end{verbatim}
%
% The reversal of the execution order happens before applying any
% rules, so if you alter the order you will probably have to alter
% it in both hooks, not just in one, but that depends on the use case.
%
%
%
%
% \subsection{Difference between \enquote{normal} and
% \enquote{one-time} hooks}
% \label{sec:onetime-hooks}
%
% When executing a hook a developer has the choice of using
% either \cs{UseHook} or \cs{UseOneTimeHook} (or their \pkg{expl3}
% equivalents \cs{hook_use:n} and \cs{hook_use_once:n}).
% This choice affects how \cs{AddToHook} is handled after the hook
% has been executed for the first time.
%
% With normal hooks adding code via \cs{AddToHook} means that the
% code chunk is added to the hook data structure and then used each time
% \cs{UseHook} is called.
%
% With one-time hooks it this is handled slightly differently:
% After \cs{UseOneTimeHook} has been called, any further attempts to
% add code to the hook via \cs{AddToHook} will simply execute the
% \meta{code} immediately.
%
% This has some consequences one needs to be aware of:
% \begin{itemize}
% \item
%
% If \meta{code} is added to a normal hook after the hook was
% executed and it is never executed again for one or the other
% reason, then this new \meta{code} will never be executed.
%
% \item
%
% In contrast if that happens with a one-time hook the \meta{code} is
% executed immediately.
%
% \end{itemize}
% In particular this means that construct such as
%\begin{quote}
% \cs{AddToHook}\verb={myhook}=\\
% \phantom{\cs{AddToHook}}\verb={= \meta{code-1}
% \cs{AddToHook}\verb={myhook}=\Arg{code-2}
% \meta{code-3} \verb=}=
%\end{quote}
% works for one-time hooks\footnote{This is sometimes used with
% \cs{AtBeginDocument} which is why it is supported.} (all three
% code chunks are executed one after another), but it makes little
% sense with a normal hook, because with a normal hook the first time
% \verb=\UseHook{myhook}= is executed it would
% \begin{itemize}
% \item
% execute \meta{code-1},
% \item
% then execute \verb=\AddToHook{myhook}{code-2}= which adds the
% code chunk \meta{code-2} to the hook for use on the next invocation,
% \item
% and finally execute \meta{code-3}.
% \end{itemize}
% The second time \cs{UseHook} is called it would execute the
% above and in addition \meta{code-2} as that was added as a code
% chunk to the hook in the meantime. So each time the hook is used
% another copy of \meta{code-2} is added and so that code chunk
% is executed $\meta{\# of invocations} -1$ times.
%
%
%
% \subsection{Generic hooks provided by packages}
% \label{sec:generic-hooks}
%
% \changes{v1.0p}{2021/08/20}{Section on generic hooks added (gh/638)}
%
% The hook management system also implements a category of hooks
% that are called \enquote{Generic Hooks}. Normally a hook has to
% be explicitly declared before it can be used in code. This
% ensures that different packages are not using the same hook name
% for unrelated purposes---something that would result in absolute
% chaos. However, there are a number of \enquote{standard} hooks
% where it is unreasonable to declare them beforehand, e.g, each
% and every command has (in theory) an associated \texttt{before}
% and \texttt{after} hook. In such cases, i.e., for command,
% environment or file hooks, they can be used simply by adding code
% to them with \cs{AddToHook}. For more specialized generic hooks,
% e.g., those provided by \pkg{babel}, you have to additionally
% enable them with \cs{ActivateGenericHook} as explained below.
%
% The generic hooks provided by \LaTeX{} are those for
% \hook{cmd},
% \hook{env},
% \hook{file},
% \hook{include}
% \hook{package}, and
% \hook{class},
% and all these are available out of the box: you only have to
% use \cs{AddToHook} to
% add code to them, but you don't have to add \cs{UseHook} or
% \cs{UseOneTimeHook} to your code, because this is already done for
% you (or, in the case of \hook{cmd} hooks, the command’s code is
% patched at \verb=\begin{document}=, if necessary).
%
% However, if you want to provide further generic hooks in your own
% code, the situation is slightly different. To do this you should
% use \cs{UseHook} or \cs{UseOneTimeHook}, but
% \emph{without declaring the hook} with \cs{NewHook}. As
% mentioned earlier, a call to \cs{UseHook} with an undeclared hook
% name does nothing. So as an additional setup step, you need to
% explicitly activate your generic hook. Note that a generic hook
% produced in this way is always a normal hook.
%
% For a truly generic hook, with a variable part in the hook name,
% such upfront activation would be difficult or impossible, because
% you typically do not know what kind of variable parts may come up
% in real documents.
%
% For example, \pkg{babel} provides hooks such as
% \hook{babel/\meta{language}/afterextras}. However, language
% support in \pkg{babel} is often done through external language
% packages. Thus doing the activation for all languages inside the
% core \pkg{babel} code is not a viable approach. Instead it needs
% to be done by each language package (or by the user who wants to
% use a particular hook).
%
% Because the hooks are not declared with \cs{NewHook} their names
% should be carefully chosen to ensure that they are (likely to be)
% unique. Best practice is to include the package or
% command name, as was done in the \pkg{babel} example above.
%
% Generic hooks defined in this way are always normal hooks (i.e.,
% you can't implement reversed hooks this way). This is a
% deliberate limitation, because it speeds up the processessing
% conciderably.
%
%
% \subsection{Hooks with arguments}
% \label{sec:hook-args}
%
% Sometimes it is necessary to pass contextual information to a hook,
% and, for one reason or another, it is not feasible to store such
% information in macros. To serve this purpose, hooks can be
% declared with arguments, so that the programmer can pass along the
% data necessary for the code in the hook to function properly.
%
% A hook with arguments works mostly like a regular hook, and most
% commands that work for regular hooks, also work for hooks that take
% arguments. The differences are when the hook is declared
% (\cs{NewHookWithArguments} is used instead of \cs{NewHook}), then
% code can be added with both \cs{AddToHook} and
% \cs{AddToHookWithArguments}, and when the hook is used
% (\cs{UseHookWithArguments} instead of \cs{UseHook}).
%
% \medskip
%
% A hook with arguments must be declared as such (before it is first
% used, as all regular hooks) using
% \cs{NewHookWithArguments}\Arg{hook}\Arg{number}. All code added to
% that hook can then use \verb|#1| to access the first argument,
% \verb|#2| to access the second, and so forth up to the number of
% arguments declared. However, it is still possible to add code with
% references to the arguments of a hook that was not yet declared
% (we will discuss that later). At their core, hooks are macros, so
% \TeX's limit of 9~arguments applies, and a low-level \TeX{} error
% is raised if you try to reference an argument number that doesn't
% exist.
%
% \medskip
%
% To use a hook with arguments, just write
% \cs{UseHookWithArguments}\Arg{hook}\Arg{number} followed by a
% braced list of the arguments. For example, if the hook \hook{test}
% takes three arguments, write:
%\begin{verbatim}
% \UseHookWithArguments{test}{3}{arg-1}{arg-2}{arg-3}
%\end{verbatim}
% then, in the \meta{code} of the hook, all instances of \verb|#1|
% will be replaced by \verb|arg-1|, \verb|#2| by \verb|arg-2| and so
% on. If, at the point of usage, the programmer provides more
% arguments than the hook is declared to take, the excess arguments
% are simply ignored by the hook. Behaviour is
% unpredictable\footnote
% {The hook \emph{will} take the declared number of arguments, and
% what will happen depends on what was grabbed, and what the hook
% code does with its arguments.}
% if too few arguments are provided. If the hook isn't declared,
% \meta{number} arguments are removed from the input stream.
%
% \medskip
%
% Adding code to a hook with arguments can be done with
% \cs{AddToHookWithArguments} as well as with the regular
% \cs{AddToHook}, to achieve different outcomes. The main difference
% when it comes to adding code to a hook, in this case, is firstly
% the possibility of accessing a hook's arguments, of course, and
% second, how parameter tokens (\verb|#|$_6$) are treated.
%
% Using \cs{AddToHook} in a hook that takes arguments will work as it
% does for all other hooks. This allows a package developer to add
% arguments to a hook that otherwise had none without having to worry
% about compatibility. This means that, for example:
%\begin{verbatim}
% \AddToHook{test}{\def\foo#1{Hello, #1!}}
%\end{verbatim}
% will define the same macro \cs[no-index]{foo} regardless if the
% hook \hook{test} takes arguments or not.
%
% Using \cs{AddToHookWithArguments} allows the \meta{code} added to
% access the arguments of the hook with \verb|#1|, \verb|#2|, and so
% forth, up to the number of the arguments declared in the hook.
% This means that if one wants to add a \verb|#|$_6$ to the
% \meta{code} that token must be doubled in the input. The same
% definition from above, using \cs{AddToHookWithArguments}, needs to
% be rewritten:
%\begin{verbatim}
% \AddToHookWithArguments{test}{\def\foo##1{Hello, ##1!}}
%\end{verbatim}
%
% Extending the above example to use the hook arguments, we could
% rewrite something like (now from declaration to usage, to get the
% whole picture):
%\begin{verbatim}
% \NewHookWithArguments{test}{1}
% \AddToHookWithArguments{test}{%
% \typeout{Defining foo with "#1"}
% \def\foo##1{Hello, ##1! Some text after: #1}%
% }
% \UseHook{test}{Howdy!}
% \ShowCommand\foo
%\end{verbatim}
% Running the code above prints in the terminal:
%\begin{verbatim}
% Defining foo with "Howdy!"
% > \foo=macro:
% #1->Hello, #1! Some text after: Howdy!.
%\end{verbatim}
% Note how \verb|##1| in the call to \cs{AddToHookWithArguments}
% became \verb|#1|, and the \verb|#1| was replaced by the argument
% passed to the hook. Should the hook be used again, with a
% different argument, the definition would naturally change.
%
% \bigskip
%
% It is possible to add code referencing a hook's arguments before
% such hook is declared and the number of hooks is fixed. However,
% if some code is added to the hook, that references more arguments
% than will be declared for the hook, there will be a low-level
% \TeX{} error about an \enquote{Illegal parameter number} at the
% time the hook is declared, which will be hard to track down because
% at that point \TeX{} can't know whence the offending code came
% from. Thus it is important that package writers explicitly
% document how many arguments (if any) each hook can take, so users
% of those packages know how many arguments can be referenced, and
% equally important, what each argument means.
%
% \subsection{Private \LaTeX{} kernel hooks}
%
% There are a few places where it is absolutely essential for
% \LaTeX{} to function correctly that code is executed in a precisely
% defined order. Even that could have been implemented with the
% hook management (by adding various rules to ensure the
% appropriate ordering with respect to other code added by
% packages). However, this makes every document unnecessary
% slow, because there has to be sorting even though the result is
% predetermined. Furthermore it forces package writers to
% unnecessarily add such rules if they add further code to the hook
% (or break \LaTeX{}).
%
% For that reason such code is not using the hook management, but
% instead private kernel commands directly before or after a public
% hook with the following naming
% convention: \cs{@kernel@before@\meta{hook}} or
% \cs{@kernel@after@\meta{hook}}. For example, in
% \cs{enddocument} you find
%\begin{verbatim}
% \UseHook{enddocument}%
% \@kernel@after@enddocument
%\end{verbatim}
% which means first the user/package-accessible \hook{enddocument}
% hook is executed and then the internal kernel hook. As their name
% indicates these kernel commands should not be altered by third-party
% packages, so please refrain from that in the interest of
% stability and instead use the public hook next to it.\footnote{As
% with everything in \TeX{} there is not enforcement of this rule,
% and by looking at the code it is easy to find out how the kernel
% adds to them. The main reason of this section is therefore to say
% \enquote{please don't do that, this is unconfigurable code!}}
%
%
%
% \subsection{Legacy \LaTeXe{} interfaces}
%
% \newcommand\onetimetext{This is a one-time hook, so after it
% is executed, all further
% attempts to add code to it will execute such code immediately
% (see section~\ref{sec:onetime-hooks}).}
%
% \LaTeXe{} offered a small number of hooks together with commands to
% add to them. They are listed here and are retained for backwards
% compatibility.
%
% With the new hook management, several additional hooks have been added
% to \LaTeX\ and more will follow. See the next section for what
% is already available.
%
%
% \begin{function}{\AtBeginDocument}
% \begin{syntax}
% \cs{AtBeginDocument} \oarg{label} \Arg{code}
% \end{syntax}
% If used without the optional argument \meta{label}, it works essentially
% like before, i.e., it is adding \meta{code} to the hook
% \hook{begindocument}
% (which is executed inside \verb=\begin{document}=).
% However, all code added this way is labeled with the label
% \hook{top-level} (see section~\ref{sec:top-level})
% if done outside of a package or class or with the
% package/class name if called inside such a file
% (see section~\ref{sec:default-label}).
%
% This way one can add code to the hook using
% \cs{AddToHook} or \cs{AtBeginDocument} using a different label
% and explicitly order the code chunks as necessary, e.g., run some
% code before or after another package's code. When using the
% optional argument the call is equivalent to running
% \cs{AddToHook} \texttt{\{begindocument\}} \oarg{label}
% \Arg{code}.
%
% \cs{AtBeginDocument} is a wrapper around the \hook{begindocument}
% hook (see section~\ref{sec:begindocument-hooks}), which is a
% one-time hook. As such, after the \hook{begindocument} hook is
% executed at \verb=\begin{document}= any attempt to add \meta{code}
% to this hook with \cs{AtBeginDocument} or with \cs{AddToHook} will
% cause that \meta{code} to execute immediately instead.
% See section~\ref{sec:onetime-hooks} for more on one-time hooks.
%
% For important packages with known order requirement we may over
% time add rules to the kernel (or to those packages) so that they
% work regardless of the loading-order in the document.
% \end{function}
%
% \begin{function}{\AtEndDocument}
% \begin{syntax}
% \cs{AtEndDocument} \oarg{label} \Arg{code}
% \end{syntax}
% Like \cs{AtBeginDocument} but for the \hook{enddocument} hook.
% \end{function}
%
% \bigskip
%
% The few hooks that existed previously in \LaTeXe{} used internally
% commands such as \cs{@begindocumenthook} and packages sometimes
% augmented them directly rather than working through
% \cs{AtBeginDocument}. For that reason there is currently support
% for this, that is, if the system detects that such an internal
% legacy hook command contains code it adds it to the new hook
% system under the label \texttt{legacy} so that it doesn't get
% lost.
%
% However, over time the remaining cases of direct usage need
% updating because in one of the future release of \LaTeX{} we will
% turn this legacy support off, as it does unnecessary slow down
% the processing.
%
%
% \section{\LaTeXe{} commands and environments augmented by hooks}
%
% In this section we describe the standard hooks that are now
% offered by \LaTeX{}, or give pointers to other documents in which
% they are described. This section will grow over time (and
% perhaps eventually move to usrguide3).
%
% \subsection{Generic hooks}
% \label{sec:generic}
%
% As stated earlier, with the exception of generic hooks, all hooks must
% be declared with \cs{NewHook} before they can be used.
% All generic hooks have names of the form
% \enquote{\meta{type}/\meta{name}/\meta{position}}, where \meta{type}
% is from the predefined list shown below, and \meta{name} is the variable
% part whose meaning will depend on the \meta{type}. The last component,
% \meta{position}, has more complex possibilities:
% it can always be |before| or |after|; for |env| hooks, it can also be |begin|
% or |end|; and for |include| hooks it can also be |end|. Each specific
% hook is documented below, or in \texttt{ltcmdhooks-doc.pdf} or
% \texttt{ltfilehook-doc.pdf}.
%
% The generic hooks provided by \LaTeX{} belong to one of the six types:
% \begin{description}
% \item[env] Hooks executed before and after environments --
% \meta{name} is the name of the environment, and available values
% for \meta{position} are |before|, |begin|, |end|, and |after|;
% \item[cmd] Hooks added to and executed before and after commands --
% \meta{name} is the name of the command, and available values
% for \meta{position} are |before| and |after|;
% \item[file] Hooks executed before and after reading a file --
% \meta{name} is the name of the file (with extension), and
% available values for \meta{position} are |before| and |after|;
% \item[package] Hooks executed before and after loading packages --
% \meta{name} is the name of the package, and available values for
% \meta{position} are |before| and |after|;
% \item[class] Hooks executed before and after loading classes --
% \meta{name} is the name of the class, and available values for
% \meta{position} are |before| and |after|;
% \item[include] Hooks executed before and after \cs{include}d files --
% \meta{name} is the name of the included file (without the |.tex|
% extension), and available values for \meta{position} are |before|,
% |end|, and |after|.
% \end{description}
%
% Each of the hooks above are detailed in the following sections and
% in linked documentation.
% ^^A^^A^^A \pho{Wouldn't it be better to document all hooks here?}
%
% \subsubsection{Generic hooks for all environments}
%
% Every environment \meta{env} has now four associated hooks coming
% with it:
% \begin{description}
% \item[\hook{env/\meta{env}/before}]
%
% This hook is executed as part of \cs{begin} as the very first
% action, in particular prior to starting the environment group.
% Its scope is therefore not restricted by the environment.
%
% \item[\hook{env/\meta{env}/begin}]
%
% This hook is executed as part of \cs{begin} directly in front
% of the code specific to the environment start (e.g., the
% second argument of \cs{newenvironment}). Its scope is the
% environment body.
%
% \item[\hook{env/\meta{env}/end}]
%
% This hook is executed as part of \cs{end} directly in front of the
% code specific to the end of the environment (e.g., the third
% argument of \cs{newenvironment}).
%
% \item[\hook{env/\meta{env}/after}]
%
% This hook is executed as part of \cs{end} after the
% code specific to the environment end and after the environment
% group has ended.
% Its scope is therefore not restricted by the environment.
%
% The hook is implemented as a reversed hook so if two packages
% add code to \hook{env/\meta{env}/before} and to
% \hook{env/\meta{env}/after} they can add surrounding
% environments and the order of closing them happens in the
% right sequence.
%
% \end{description}
% Generic environment hooks are never one-time hooks even with
% environments that are supposed to appear only once in a
% document.\footnote{Thus if one adds code to such hooks after the
% environment has been processed, it will only be executed if the
% environment appears again and if that doesn't happen the code
% will never get executed.} In contrast to other hooks there is
% also no need to declare them using \cs{NewHook}.
%
% The hooks are only executed if \cs{begin}\Arg{env} and
% \cs{end}\Arg{env} is used. If the environment code is executed
% via low-level calls to \cs[no-index]{\meta{env}} and \cs[no-index]{end\meta{env}}
% (e.g., to avoid the environment grouping) they are not
% available. If you want them available in code using this method,
% you would need to add them yourself, i.e., write something like
%\begin{verbatim}
% \UseHook{env/quote/before}\quote
% ...
% \endquote\UseHook{env/quote/after}
%\end{verbatim}
% to add the outer hooks, etc.
%
%
% Largely for compatibility with existing packages, the following
% four commands are also available to set the environment hooks; but for
% new packages we recommend directly using the hook names and
% \cs{AddToHook}.
%
%
% \begin{function}{\BeforeBeginEnvironment}
% \begin{syntax}
% \cs{BeforeBeginEnvironment} \oarg{label} \Arg{env} \Arg{code}
% \end{syntax}
% This declaration adds to the \hook{env/\meta{env}/before} hook
% using the \meta{label}. If \meta{label} is not given, the
% \meta{default label} is used (see section~\ref{sec:default-label}).
% \end{function}
%
% \begin{function}{\AtBeginEnvironment}
% \begin{syntax}
% \cs{AtBeginEnvironment} \oarg{label} \Arg{env} \Arg{code}
% \end{syntax}
% This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/begin} hook.
% \end{function}
%
% \begin{function}{\AtEndEnvironment}
% \begin{syntax}
% \cs{AtEndEnvironment} \oarg{label} \Arg{env} \Arg{code}
% \end{syntax}
% This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/end} hook.
% \end{function}
%
% \begin{function}{\AfterEndEnvironment}
% \begin{syntax}
% \cs{AfterEndEnvironment} \oarg{label} \Arg{env} \Arg{code}
% \end{syntax}
% This is like \cs{BeforeBeginEnvironment} but it adds to the \hook{env/\meta{env}/after} hook.
% \end{function}
%
%
% \subsubsection{Generic hooks for commands}
%
% Similar to environments there are now (at least in theory) two
% generic hooks available for any \LaTeX{} command. These are
% \begin{description}
% \item[\hook{cmd/\meta{name}/before}]
%
% This hook is executed at the very start of the command
% execution.
%
% \item[\hook{cmd/\meta{name}/after}]
% This hook is executed at the very end of the command body. It is
% implemented as a reversed hook.
% \end{description}
% In practice there are restrictions and especially the
% \hook{after} hook works only with a subset of commands. Details
% about these restrictions are documented in
% \texttt{ltcmdhooks-doc.pdf} or with code in
% \texttt{ltcmdhooks-code.pdf}.
%
%
%
%
% \subsubsection{Generic hooks provided by file loading operations}
%
% There are several hooks added to \LaTeX{}'s process of loading
% file via its high-level interfaces such as \cs{input},
% \cs{include}, \cs{usepackage}, \cs{RequirePackage}, etc. These
% are documented in \texttt{ltfilehook-doc.pdf} or with code in
% \texttt{ltfilehook-code.pdf}.
%
%
%
% \subsection{Hooks provided by \cs{begin}\texttt{\{document\}}}
% \label{sec:begindocument-hooks}
%
% Until 2020 \cs{begin}\texttt{\{document\}} offered exactly one
% hook that one could add to using
% \cs{AtBeginDocument}. Experiences over the years have shown that
% this single hook in one place was not enough and as part of
% adding the general hook management system a number of additional
% hooks have been added at this point. The places for these hooks have
% been chosen to provide the same support as offered by external
% packages, such as \pkg{etoolbox} and others that augmented
% \cs{document} to gain better control.
%
% Supported are now the following hooks (all of them one-time hooks):
% \begin{description}
%
%
% \item[\hook{begindocument/before}]
%
% This hook is executed at the very start of \cs{document}, one can
% think of it as a hook for code at the end of the preamble
% section and this is how it is used by \pkg{etoolbox}'s
% \cs{AtEndPreamble}.
%
% \onetimetext
%
% \item[\hook{begindocument}]
%
% This hook is added to when using \cs{AtBeginDocument} and it is
% executed after the \texttt{.aux} file as be read in and most
% initialization are done, so they can be altered and inspected by
% the hook code. It is followed by a small number of further
% initializations that shouldn't be altered and are therefore
% coming later.
%
% The hook should not be used to add material for typesetting as
% we are still in \LaTeX's initialization phase and not in the
% document body. If such material needs to be added to the document
% body use the next hook instead.
%
% \onetimetext
%
% \item[\hook{begindocument/end}]
%
% This hook is executed at the end of the \cs{document} code in
% other words at the beginning of the document body. The only
% command that follows it is \cs{ignorespaces}.
%
% \onetimetext
%
% \end{description}
% The generic hooks executed by \cs{begin} also exist, i.e.,
% \hook{env/document/before} and \hook{env/document/begin}, but
% with this special environment it is better use the dedicated
% one-time hooks above.
%
%
%
%
% \subsection{Hooks provided by \cs{end}\texttt{\{document\}}}
%
% \LaTeXe{} has always provided \cs{AtEndDocument} to add code to the
% \verb=\end{document}=, just in front of the code that
% is normally executed there. While this was a big improvement over
% the situation in \LaTeX\,2.09, it was not flexible enough for a
% number of use cases and so packages, such as \pkg{etoolbox},
% \pkg{atveryend} and others patched \cs{enddocument} to add
% additional points where code could be hooked into.
%
% Patching using packages is always problematical as leads to
% conflicts (code availability, ordering of patches, incompatible
% patches, etc.). For this reason a number of additional hooks
% have been added to the \cs{enddocument} code to allow packages
% to add code in various places in a controlled way without the
% need for overwriting or patching the core code.
%
% Supported are now the following hooks (all of them one-time hooks):
% \begin{description}
%
% \item[\hook{enddocument}]
%
% The hook associated with \cs{AtEndDocument}. It is immediately
% called at the beginning of \cs{enddocument}.
%
% When this hook is executed there may be still unprocessed
% material (e.g., floats on the deferlist) and the hook may add
% further material to be typeset. After it, \cs{clearpage} is
% called to ensure that all such material gets typeset. If there
% is nothing waiting the \cs{clearpage} has no effect.
%
% \onetimetext
%
% \item[\hook{enddocument/afterlastpage}]
%
% As the name indicates this hook should not receive code that
% generates material for further pages. It is the right place to
% do some final housekeeping and possibly write out some
% information to the \texttt{.aux} file (which is still open at
% this point to receive data, but since there will be no more pages you
% need to write to it using \cs{immediate}\cs{write}). It is also the
% correct place to
% set up any testing code to be run when the \texttt{.aux} file
% is re-read in the next step.
%
%
% After this hook has been executed the \texttt{.aux} file is
% closed for writing and then read back in to do some tests
% (e.g., looking for missing references or duplicated labels, etc.).
%
% \onetimetext
%
% \item[\hook{enddocument/afteraux}]
%
% At this point, the \texttt{.aux} file has been reprocessed and so
% this is a possible place for final checks and display of
% information to the user. However, for the latter you might
% prefer the next hook, so that your information is displayed after the
% (possibly longish) list of files if that got requested via \cs{listfiles}.
%
% \onetimetext
%
% \item[\hook{enddocument/info}]
%
% This hook is meant to receive code that write final information
% messages to the terminal. It follows immediately after the
% previous hook (so both could have been combined, but then
% packages adding further code would always need to also supply
% an explicit rule to specify where it should go.
%
% This hook already contains some code added by the kernel (under
% the labels \texttt{kernel/filelist} and
% \texttt{kernel/warnings}), namely the list of files when
% \cs{listfiles} has been used and the warnings for duplicate
% labels, missing references, font substitutions etc.
%
% \onetimetext
%
% \item[\hook{enddocument/end}]
%
% Finally, this hook is executed just in front of the final call
% to \cs{@{}@end}.
%
% \onetimetext % is it even possible to add code after this one?
%
% \end{description}
%
%
% There is also the hook \hook{shipout/lastpage}. This hook is
% executed as part of the last \cs{shipout} in the document to
% allow package to add final \cs{special}'s to that page. Where
% this hook is executed in relation to those from the above list
% can vary from document to document. Furthermore to determine correctly
% which of the \cs{shipout}s is the last one, \LaTeX{} needs to be run
% several times, so initially it might get executed on the wrong
% page. See section~\ref{sec:shipout} for where to find the details.
%
%
% It is in also possible to use the generic \hook{env/document/end}
% hook which is executed by \cs{end}, i.e., just in front of the
% first hook above. Note however that the other generic \cs{end}
% environment hook, i.e., \hook{env/document/after} will never get
% executed, because by that time \LaTeX{} has finished the document
% processing.
%
%
%
%
% \subsection{Hooks provided by \cs{shipout} operations}
% \label{sec:shipout}
%
% There are several hooks and mechanisms added to \LaTeX{}'s
% process of generating pages. These are documented in
% \texttt{ltshipout-doc.pdf} or with code in
% \texttt{ltshipout-code.pdf}.
%
%
% \subsection{Hooks provided for paragraphs}
% \label{sec:para}
%
% The paragraph processing has been augmented to include a number of
% internal and public hooks. These are documented in
% \texttt{ltpara-doc.pdf} or with code in
% \texttt{ltpara-code.pdf}.
%
%
%
% \subsection{Hooks provided in NFSS commands}
%
% In languages that need to support for more than one script in
% parallel (and thus several sets of fonts, e.g., supporting both Latin and
% Japanese fonts), NFSS font commands such as \cs{sffamily} need
% to switch both the Latin family to ``Sans Serif'' and in addition
% alter a second set of fonts.
%
% To support this, several NFSS commands have hooks to which
% such support can be added.
% \begin{description}
%
% \item[\hook{rmfamily}]
%
% After \cs{rmfamily} has done its initial checks and prepared a
% font series update, this hook is executed before \cs{selectfont}.
%
% \item[\hook{sffamily}]
%
% This is like the \hook{rmfamily} hook, but for the \cs{sffamily} command.
%
% \item[\hook{ttfamily}]
%
% This is like the \hook{rmfamily} hook, but for the \cs{ttfamily} command.
%
% \item[\hook{normalfont}]
%
% The \cs{normalfont} command resets the font encoding, family, series
% and shape to their document defaults. It then executes this
% hook and finally calls \cs{selectfont}.
%
% \item[\hook{expand@font@defaults}]
%
% The internal \cs{expand@font@defaults} command expands and
% saves the current defaults for the meta families (rm/sf/tt) and
% the meta series (bf/md). If the NFSS machinery has been
% augmented, e.g., for Chinese or Japanese fonts, then further
% defaults may need to be set at this point. This can be done in
% this hook which is executed at the end of this macro.
%
% \item[\hook{bfseries/defaults}, \hook{bfseries}]
%
% If the \cs{bfdefault} was explicitly changed by the user, its
% new value is used to set the bf series defaults for the meta
% families (rm/sf/tt) when \cs{bfseries} is called. The
% \hook{bfseries/defaults} hook allows further adjustments to be made
% in this case. This hook is only executed if such a change is
% detected. In contrast, the \hook{bfseries} hook is always
% executed just before \cs{selectfont} is called to change to the
% new series.
%
%
% \item[\hook{mdseries/defaults}, \hook{mdseries}]
%
% These two hooks are like the previous ones but they are in the
% \cs{mdseries} command.
%
% \item[\hook{selectfont}]
%
% This hook is executed inside \cs{selectfont}, after the current
% values for \textit{encoding}, \textit{family}, \textit{series},
% \textit{shape}, and \textit{size} are evaluated and the new font
% is selected (and if necessary loaded). After the hook has
% executed, NFSS will still do any updates necessary for a new
% \textit{size} (such as changing the size of \cs{strut}) and any
% updates necessary to a change in \textit{encoding}.
%
% This hook is intended for use cases where, in parallel to a
% change in the main font, some other fonts need to be altered
% (e.g., in CJK processing where you may need to deal with several
% different alphabets).
%
% \end{description}
%
%
%
% \subsection{Hook provided by the mark mechanism}
%
% See \texttt{ltmarks-doc.pdf} for details.
% \begin{description}
%
% \item[\hook{insertmark}]
%
% This hook allows for a special setup while \cs{InsertMark}
% inserts a mark. It is executed in group so local changes only
% apply to the mark being inserted.
%
% \end{description}
%
% \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
%
%
% \section{The Implementation}
%
%
% \begin{macrocode}
%<@@=hook>
% \end{macrocode}
%
% \changes{v1.0i}{2021/03/18}{Use \cs{NewModuleRelease}.}
% \changes{v1.0n}{2021/05/24}{Use \cs{msg_...} instead of \cs{__kernel_msg...}}
%
% \begin{macrocode}
%<*2ekernel|latexrelease>
\ExplSyntaxOn
%<latexrelease>\NewModuleRelease{2020/10/01}{lthooks}
%<latexrelease> {The~hook~management~system}
% \end{macrocode}
%
% \subsection{Debugging}
%
% \begin{macro}{\g_@@_debug_bool}
% Holds the current debugging state.
% \begin{macrocode}
\bool_new:N \g_@@_debug_bool
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hook_debug_on:,\hook_debug_off:}
% \begin{macro}{\@@_debug:n}
% \begin{macro}{\@@_debug_gset:}
% Turns debugging on and off by redefining \cs{@@_debug:n}.
% \begin{macrocode}
\cs_new_eq:NN \@@_debug:n \use_none:n
\cs_new_protected:Npn \hook_debug_on:
{
\bool_gset_true:N \g_@@_debug_bool
\@@_debug_gset:
}
\cs_new_protected:Npn \hook_debug_off:
{
\bool_gset_false:N \g_@@_debug_bool
\@@_debug_gset:
}
\cs_new_protected:Npn \@@_debug_gset:
{
\cs_gset_protected:Npx \@@_debug:n ##1
{ \bool_if:NT \g_@@_debug_bool {##1} }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \subsection{Borrowing from internals of other kernel modules}
%
%
% \begin{macro}[EXP]{\@@_str_compare:nn}
% Private copy of \cs{__str_if_eq:nn}
% \InternalDetectionOff
% \begin{macrocode}
\cs_new_eq:NN \@@_str_compare:nn \__str_if_eq:nn
% \end{macrocode}
% \InternalDetectionOn
% \end{macro}
%
% \subsection{Declarations}
%
% \begin{macro}{\l_@@_tmpa_bool}
% Scratch boolean used throughout the package.
% \begin{macrocode}
\bool_new:N \l_@@_tmpa_bool
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_@@_return_tl,\l_@@_tmpa_tl,\l_@@_tmpb_tl}
% Scratch variables used throughout the package.
% \begin{macrocode}
\tl_new:N \l_@@_return_tl
\tl_new:N \l_@@_tmpa_tl
\tl_new:N \l_@@_tmpb_tl
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g_@@_all_seq}
% In a few places we need a list of all hook names ever defined so
% we keep track if them in this sequence.
% \begin{macrocode}
\seq_new:N \g_@@_all_seq
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_@@_cur_hook_tl}
% Stores the name of the hook currently being sorted.
% \begin{macrocode}
\tl_new:N \l_@@_cur_hook_tl
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\l_@@_work_prop}
% A property list holding a copy of the
% \cs[no-index]{g_@@_\meta{hook}_code_prop} of the hook being sorted
% to work on, so that changes don't act destructively on the hook data
% structure.
% \begin{macrocode}
\prop_new:N \l_@@_work_prop
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g_@@_used_prop}
% All hooks that receive code (for use in debugging display).
% \begin{macrocode}
\prop_new:N \g_@@_used_prop
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\g_@@_hook_curr_name_tl,\g_@@_name_stack_seq}
% Default label used for hook commands, and a stack to keep track of
% packages within packages.
% \begin{macrocode}
\tl_new:N \g_@@_hook_curr_name_tl
\seq_new:N \g_@@_name_stack_seq
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tmp:w}
% Temporary macro for generic usage.
% \begin{macrocode}
\cs_new_eq:NN \@@_tmp:w ?
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c_@@_empty_tl}
% \begin{macro}{\c_@@_nine_parameters_tl}
% An empty token list, and one containing nine parameters.
% \changes{v1.1a}{2023/04/06}
% {Add auxiliary token lists (hook-args).}
% \begin{macrocode}
\tl_const:Nn \c_@@_empty_tl { }
\tl_const:Nn \c_@@_nine_parameters_tl { #1#2#3#4#5#6#7#8#9 }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[int]{
% \tl_gremove_once:Nx,
% \tl_show:x,
% \tl_log:x,
% \tl_set:Ne,
% \cs_replacement_spec:c,
% \prop_put:Nne,
% \str_count:e
% }
% Some variants of \pkg{expl3} functions.
% \fmiinline{should probably be moved to expl3}
% \begin{macrocode}
\cs_generate_variant:Nn \tl_gremove_once:Nn { Nx }
\cs_generate_variant:Nn \tl_show:n { x }
\cs_generate_variant:Nn \tl_log:n { x }
\cs_generate_variant:Nn \tl_set:Nn { Ne }
\cs_generate_variant:Nn \cs_replacement_spec:N { c }
\cs_generate_variant:Nn \prop_put:Nnn { Nne }
\cs_generate_variant:Nn \str_count:n { e }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\s_@@_mark}
% Scan mark used for delimited arguments.
% \begin{macrocode}
\scan_new:N \s_@@_mark
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \@@_use_none_delimit_by_s_mark:w,
% \@@_use_i_delimit_by_s_mark:nw
% }
% Removes tokens until the next \cs{s_@@_mark}.
% \changes{v1.1a}{2023/04/06}
% {Use standard naming scheme (hook-args).}
% \begin{macrocode}
\cs_new:Npn \@@_use_none_delimit_by_s_mark:w #1 \s_@@_mark { }
\cs_new:Npn \@@_use_i_delimit_by_s_mark:nw #1 #2 \s_@@_mark {#1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tl_set:cn}
% Private copies of a few \pkg{expl3} functions. \pkg{l3debug} will
% only add debugging to the public names, not to these copies, so we
% don't have to use \cs{debug_suspend:} and \cs{debug_resume:}
% everywhere.
%
% Functions like \cs{@@_tl_set:Nn} have to be redefined, rather than
% copied because in \pkg{expl3} they use
% \cs[no-index]{__kernel_tl_(g)set:Nx}, which is also patched by
% \pkg{l3debug}.
% \changes{v1.0h}{2021/01/07}{Manually define some \pkg{l3tl} commands
% to work around \pkg{expl3} changes}
% \changes{v1.1a}{2023/04/06}
% {Clean-up unused variants (hook-args).}
% \begin{macrocode}
\cs_new_protected:Npn \@@_tl_set:cn #1#2
{ \cs_set_nopar:cpx {#1} { \__kernel_exp_not:w {#2} } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tl_gset:Nn,\@@_tl_gset:Nx,
% \@@_tl_gset:cn,\@@_tl_gset:co,\@@_tl_gset:cx}
% Same as above.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tl_gset:Nn #1#2
{ \cs_gset_nopar:Npx #1 { \__kernel_exp_not:w {#2} } }
\cs_new_protected:Npn \@@_tl_gset:Nx #1#2
{ \cs_gset_nopar:Npx #1 {#2} }
\cs_generate_variant:Nn \@@_tl_gset:Nn { c, co }
\cs_generate_variant:Nn \@@_tl_gset:Nx { c }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \@@_tl_gput_right:Nn,
% \@@_tl_gput_right:Ne,
% \@@_tl_gput_right:cn,
% }
% Same as above.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tl_gput_right:Nn #1#2
{ \@@_tl_gset:Nx #1 { \__kernel_exp_not:w \exp_after:wN { #1 #2 } } }
\cs_generate_variant:Nn \@@_tl_gput_right:Nn { Ne, cn }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tl_gput_left:Nn}
% Same as above.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tl_gput_left:Nn #1#2
{
\@@_tl_gset:Nx #1
{ \__kernel_exp_not:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tl_gset_eq:NN}
% Same as above.
% \begin{macrocode}
\cs_new_eq:NN \@@_tl_gset_eq:NN \tl_gset_eq:NN
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tl_gclear:N,\@@_tl_gclear:c}
% Same as above.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tl_gclear:N #1
{ \@@_tl_gset_eq:NN #1 \c_empty_tl }
\cs_generate_variant:Nn \@@_tl_gclear:N { c }
% \end{macrocode}
% \end{macro}
%
%
% \subsection{Providing new hooks}
%
% \subsubsection{The data structures of a hook}
%
% \DescribeMacro{\g_@@_\meta{hook}_code_prop}
% \DescribeMacro{\@@\textvisiblespace\meta{hook}}
% \DescribeMacro{\g_@@_\meta{hook}_reversed_tl}
% \DescribeMacro{\g_@@_\meta{hook}_declared_tl}
% \DescribeMacro{\g_@@_\meta{hook}_parameter_tl}
% \DescribeMacro{\@@_next\textvisiblespace\meta{hook}}
% \DescribeMacro{\@@_toplevel\textvisiblespace\meta{hook}}
% Hooks have a name (called \meta{hook} in the description below)
% and for each hook we have to
% provide a number of data structures. These are
% \begin{description}
% \item[\cs{g_@@_\meta{hook}_code_prop}] A property list holding the code
% for the hook in separate chunks. The keys are by default the
% package names that add code to the hook, but it is possible
% for packages to define other keys.
%
% \item[{\cs[no-index]{g_@@_\meta{hook}_rule_\meta{label1}\string|\meta{label2}_tl}}]
% A token list holding the relation between \meta{label1} and
% \meta{label2} in the \meta{hook}. The \meta{labels} are lexically
% (reverse) sorted to ensure that two labels always point to the same
% token list. For global rules, the \meta{hook} name is \texttt{??}.
%
% \item[\cs{@@\textvisiblespace\meta{hook}}] The code that is actually executed
% when the hook is called in the document is stored in this token
% list. It is constructed from the code chunks applying the
% information.
% This token list is named like that so that in case of an error
% inside the hook, the reported token list in the error is shorter,
% and to make it simpler to normalize hook names in
% \cs{@@_make_name:n}.
%
% \item[\cs{g_@@_\meta{hook}_reversed_tl}] Some hooks are
% \enquote{reversed}. This token list stores a |-| for such hook
% so that it can be identified. The |-| character is used because
% $\meta{reversed}1$ is $+1$ for normal hooks and $-1$ for reversed
% ones.
%
% \item[{\cs[no-index]{g_@@_\meta{hook}_declared_tl}}] This token
% list serves as a marker for the hook being officially declared. Its
% existence is tested to raise an error in case another declaration
% is attempted.
%
% \item[{\cs[no-index]{c_@@_\meta{hook}_parameter_tl}}] This token
% list stores the parameter text for a declared hook (its existence
% almost completely intersects the token list above), which is used
% for managing hooks with arguments.
%
% \item[\cs{@@_toplevel\textvisiblespace\meta{hook}}] This token list stores the code
% inserted in the hook from the user's document, in the |top-level|
% label. This label is special, and doesn't participate in sorting.
% Instead, all code is appended to it and executed after (or before,
% if the hook is reversed) the normal
% hook code, but before the |next| code chunk.
%
% \item[\cs{@@_next\textvisiblespace\meta{hook}}] Finally there is
% extra code (normally empty) that is used on the next invocation
% of the hook (and then deleted). This can be used to define some
% special behavior for a single occasion from within the document.
% This token list follows the same naming scheme than the main
% \cs{@@\textvisiblespace\meta{hook}} token list. It is called
% \cs{@@_next\textvisiblespace\meta{hook}} rather than
% \cs[no-index]{@@\textvisiblespace next_\meta{hook}} because
% otherwise a hook whose name is |next_|\meta{hook} would clash
% with the next code-token list of the hook called \meta{hook}.
%
% \end{description}
%
%
% \subsubsection{On the existence of hooks}
% \label{sec:existence}
%
% A hook may be in different states of existence. Here we give an
% overview of the internal commands to set up hooks and explain how the
% different states are distinguished. The actual implementation
% then follows in subsequent sections.
%
% One problem we have to solve is that we need to be able to add
% code to hooks (e.g., with \cs{AddToHook}) even if that code has
% not yet been declared. For example, one package needs to write
% into a hook of another package, but that package may not get
% loaded, or is loaded only later. Another problem is that most hooks,
% but not the generic hooks, require a declaration.
%
% We therefore distinguish the following states for a hook, which
% are managed by four different tests: structure existence
% (\cs{@@_if_structure_exist:nTF}), creation
% (\cs{@@_if_usable:nTF}), declaration (\cs{@@_if_declared:nTF})
% and disabled or not (\cs{@@_if_disabled:nTF})
% \begin{description}
% \setlist[itemize]{leftmargin=5cm,format=\cs}
% \item[not existing]
%
% Nothing is known about the hook so far. This state can be
% detected with \cs{@@_if_structure_exist:nTF}
% (which uses the false branch).
%
% In this state the hook can be declared, disabled, rules can be
% defined or code could be added to it, but it is not possible
% to use the hook (with \cs{UseHook}).
% \item[basic data structure set up]
%
% A hook is this state when its basic data structure has been
% set up (using \cs{@@_init_structure:n}). The data structure setup happens
% automatically when commands such as \cs{AddToHook} are used
% and the hook is at that point in state \enquote{not existing}.
%
% In this state the four tests give the following results:
% \begin{itemize}
% \item [@@_if_structure_exist:nTF] returns |true|.
% \item [@@_if_usable:nTF] returns |false|.
% \item [@@_if_declared:nTF] returns |false|.
% \item [@@_if_disabled:nTF] returns |false|.
% \end{itemize}
%
% The allowed actions are the same as in the \enquote{not
% existing} state.
%
% \item[declared]
%
% A hook is in this state it is not disabled and was explicitly declared (e.g.,
% with \cs{NewHook}). In this case the four tests give the
% following results:
% \begin{itemize}
% \item [@@_if_structure_exist:nTF] returns |true|.
% \item [@@_if_usable:nTF] returns |true|.
% \item [@@_if_declared:nTF] returns |true|.
% \item [@@_if_disabled:nTF] returns |false|.
% \end{itemize}
%
% \item[usable]
%
% A hook is in this state if it is not disabled, was not
% explicitly declared but nevertheless is allowed to be used
% (with \cs{UseHook} or \cs{hook_use:n}). This state is only
% possible for generic hooks as they do not need to be
% declared. Therefore such hooks move directly from state
% \enquote{not existing} to \enquote{usable} the moment a
% declaration such as \cs{AddToHook} wants to add to the hook
% data structure. In this state the tests give the following
% results:
% \begin{itemize}
% \item [@@_if_structure_exist:nTF] returns |true|.
% \item [@@_if_usable:nTF] returns |true|.
% \item [@@_if_declared:nTF] returns |false|.
% \item [@@_if_disabled:nTF] returns |false|.
% \end{itemize}
%
%
% \item[disabled]
%
% A generic hook in any state is moved to this state when
% \cs{DisableGenericHook} is used. This changes the tests to give the
% following results:
% \begin{itemize}
% \item [@@_if_structure_exist:nTF] \emph{unchanged}.
% \item [@@_if_usable:nTF] returns |false|.
% \item [@@_if_declared:nTF] returns |true|.
% \item [@@_if_disabled:nTF] returns |true|.
% \end{itemize}
% The structure test is unchanged (if the hook was unknown before it is
% |false|, otherwise |true|). The usable test returns |false| so that
% any \cs{UseHook} will bypass the hook from now on. The
% declared test returns true so that any further \cs{NewHook}
% generates an error and the disabled test returns true so that
% \cs{AddToHook} can return an error.
% \fmiinline{maybe it should do this only after begin document?}
%
% \end{description}
%
%
%
%
% \subsubsection{Setting hooks up}
%
%
% \begin{macro}{
% \hook_new:n,
% \hook_new_with_args:nn
% }
% \begin{macro}{@@_new:nn}
% The \cs{hook_new:n} declaration declares a new hook and expects
% the hook \meta{name} as its argument, e.g.,
% \hook{begindocument}.
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_new_with_args:nn} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_with_args:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_new:n #1
{ \@@_normalize_hook_args:Nn \@@_new:nn {#1} { 0 } }
\cs_new_protected:Npn \hook_new_with_args:nn #1 #2
{ \@@_normalize_hook_args:Nn \@@_new:nn {#1} {#2} }
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_new:nn #1 #2
{
% \end{macrocode}
% We check if the hook was already \emph{explicitly} declared with
% \cs{hook_new:n}, and if it already exists we complain, otherwise set
% the \enquote{created} flag for the hook so that it errors next time
% \cs{hook_new:n} is used.
% \changes{v1.1d}{2023/05/21}
% {Changes to allow support arguments in cmd hooks (cmd-args).}
% \begin{macrocode}
\@@_if_declared:nTF {#1}
{ \msg_error:nnn { hooks } { exists } {#1} }
{
\tl_new:c { g_@@_#1_declared_tl }
\cs_undefine:c { @@~#1 }
\cs_undefine:c { c_@@_#1_parameter_tl }
\@@_make_usable:nn {#1} {#2}
% \end{macrocode}
% In case there is already code in a hook, but it's undeclared, run
% \cs{@@_update_hook_code:n} to make it ready to be executed (see test
% \texttt{lthooks-034}).
% \changes{v1.1a}{2023/04/06}
% {Update hook code after declaring.}
% \begin{macrocode}
\@@_update_hook_code:n {#1}
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_with_args:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \hook_new:n #1
%<latexrelease> { \@@_normalize_hook_args:Nn \@@_new:n {#1} }
%<latexrelease>\cs_undefine:N \@@_new:nn
%<latexrelease>\cs_gset_protected:Npn \@@_new:n #1
%<latexrelease> {
%<latexrelease> \@@_if_declared:nTF {#1}
%<latexrelease> { \msg_error:nnn { hooks } { exists } {#1} }
%<latexrelease> {
%<latexrelease> \tl_new:c { g_@@_#1_declared_tl }
%<latexrelease> \@@_make_usable:n {#1}
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \hook_new_with_args:nn #1 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@@_make_usable:nn}
%
% This initializes all hook data structures for the hook but if
% used on its own doesn't mark the hook as declared (as
% \cs{hook_new:n} does, so a later \cs{hook_new:n} on that hook will
% not result in an error. This command is internally used by
% \cs{hook_gput_code:nnn} when adding code to a generic hook.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_make_usable:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_make_usable:nn #1 #2
{
% \end{macrocode}
% Now we check if the hook's data structure can be safely created
% without \pkg{expl3} raising errors, then
% we add the hook name to the list of all hooks and
% allocate the necessary data structures for the new hook,
% otherwise just do nothing.
% \begin{macrocode}
\@@_if_usable:nF {#1}
{
\seq_gput_right:Nn \g_@@_all_seq {#1}
% \end{macrocode}
% Here we'll define the \cs[no-index]{c_@@_\meta{hook}_parameter_tl}
% to hold a run of parameters up to the number of arguments of the
% hook (\verb|#2|).
% \begin{macrocode}
\__kernel_cs_parm_from_arg_count:nnF
{ \tl_const:cn { c_@@_#1_parameter_tl } } {#2}
{
\msg_error:nnnn { hooks } { too-many-args } {#1} {#2}
\tl_const:cx { c_@@_#1_parameter_tl }
{ \exp_not:V \c_@@_nine_parameters_tl }
}
% \end{macrocode}
% After that, use \cs{@@_normalise_cs_args:nn} to correct the number
% of parameters of the macros
% \cs[no-index]{@@_toplevel\textvisiblespace\meta{hook}} and
% \cs[no-index]{@@_next\textvisiblespace\meta{hook}}. We need to be
% able to add
% code with arguments to a hook without prior knowledge of the number
% of arguments of that hook, so \pkg{lthooks} assumes~9 until the
% hook is properly declared and the number of arguments is known.
% \cs{@@_normalise_cs_args:nn} does the normalisation by using the
% \cs[no-index]{c_@@_\meta{hook}_parameter_tl} defined just above.
% \begin{macrocode}
\@@_normalise_cs_args:nn { _toplevel } {#1}
\@@_normalise_cs_args:nn { _next } {#1}
% \end{macrocode}
% This is only used by the actual code of the current hook, so
% declare it normally:
% \begin{macrocode}
\@@_code_gset:nn {#1} { }
% \end{macrocode}
% Now ensure that the base data structure for the hook exists:
% \begin{macrocode}
\@@_init_structure:n {#1}
% \end{macrocode}
% The call to \cs{@@_normalise_code_pool:n} will correct any improper
% reference to arguments that don't exist in the hook, raising a
% low-level \TeX{} error and doubling the offending parameter tokens.
% It has to be done after \cs{@@_init_structure:n} because it
% operates on \cs[no-index]{g_@@_\meta{hook}_code_prop}.
% \begin{macrocode}
\@@_normalise_code_pool:n {#1}
% \end{macrocode}
% The \cs{g_@@_\meta{hook}_labels_clist} holds the sorted list of
% labels (once it got sorted). This is used only for debugging.
% These are defined conditionally, in case \cs{@@_make_usable:nn}
% is being used to redefine a hook.
% \changes{v1.1d}{2023/05/21}
% {Changes to allow support arguments in cmd hooks (cmd-args).}
% \begin{macrocode}
\clist_if_exist:cF { g_@@_#1_labels_clist }
{
\clist_new:c { g_@@_#1_labels_clist }
% \end{macrocode}
% Some hooks should reverse the default order of code chunks. To
% signal this we have a token list which is empty for normal hooks
% and contains a \verb=-= for reversed hooks.
% \begin{macrocode}
\tl_new:c { g_@@_#1_reversed_tl }
}
% \end{macrocode}
% The above is all in L3 convention, but we also provide an
% interface to legacy \LaTeXe{} hooks of the form \cs{@...hook},
% e.g., \cs{@begindocumenthook}.
% there have been a few of them and they have been added to
% using \cs{g@addto@macro}. If there exists such a macro matching
% the name of the new hook, i.e.,
% \verb+\@+\meta{hook-name}\texttt{hook} and it is not empty then
% we add its contents as a code chunk under the label \texttt{legacy}.
% \begin{quote}
% \textbf{Warning: this support will vanish in future releases!}
% \end{quote}
%
% \begin{macrocode}
\@@_include_legacy_code_chunk:n {#1}
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_make_usable:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_make_usable:nn
%<latexrelease>\cs_gset_protected:Npn \@@_make_usable:n #1
%<latexrelease> {
%<latexrelease> \tl_if_exist:cF { @@~#1 }
%<latexrelease> {
%<latexrelease> \seq_gput_right:Nn \g_@@_all_seq {#1}
%<latexrelease> \tl_new:c { @@~#1 }
%<latexrelease> \@@_init_structure:n {#1}
%<latexrelease> \clist_new:c { g_@@_#1_labels_clist }
%<latexrelease> \tl_new:c { g_@@_#1_reversed_tl }
%<latexrelease> \@@_include_legacy_code_chunk:n {#1}
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_init_structure:n}
% This function declares the basic data structures for a hook
% without explicit declaring the hook itself. This is needed to
% allow adding to undeclared hooks. Here it is unnecessary to
% check whether all variables exist, since all three are declared
% at the same time (either all of them exist, or none).
%
% It creates the hook code pool
% (\cs[no-index]{g_@@_\meta{hook}_code_prop}) and the |top-level|
% and |next| token lists. A hook is initialized with
% \cs{@@_init_structure:n} the first time anything is added to it.
% Initializing a hook just with \cs{@@_init_structure:n} will not
% make it usable with \cs{hook_use:n}.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_init_structure:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_init_structure:n #1
{
\@@_if_structure_exist:nF {#1}
{
\prop_new:c { g_@@_#1_code_prop }
\@@_toplevel_gset:nn {#1} { }
\@@_next_gset:nn {#1} { }
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_init_structure:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_init_structure:n #1
%<latexrelease> {
%<latexrelease> \@@_if_structure_exist:nF {#1}
%<latexrelease> {
%<latexrelease> \prop_new:c { g_@@_#1_code_prop }
%<latexrelease> \tl_new:c { @@_toplevel~#1 }
%<latexrelease> \tl_new:c { @@_next~#1 }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \hook_new_reversed:n,
% \hook_new_reversed_with_args:nn
% }
% \begin{macro}{\@@_new_reversed:nn}
%
% Declare a new hook. The default ordering of code chunks is
% reversed, signaled by setting the token list to a minus sign.
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_new_reversed_with_args:nn} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_reversed_with_args:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_new_reversed:n #1
{ \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} { 0 } }
\cs_new_protected:Npn \hook_new_reversed_with_args:nn #1 #2
{ \@@_normalize_hook_args:Nn \@@_new_reversed:nn {#1} {#2} }
\cs_new_protected:Npn \@@_new_reversed:nn #1 #2
{
\@@_if_declared:nTF {#1}
{ \msg_error:nnn { hooks } { exists } {#1} }
{
\@@_new:nn {#1} {#2}
\tl_gset:cn { g_@@_#1_reversed_tl } { - }
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_reversed_with_args:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \hook_new_reversed:n #1
%<latexrelease> { \@@_normalize_hook_args:Nn \@@_new_reversed:n {#1} }
%<latexrelease>\cs_undefine:N \@@_new_reversed:nn
%<latexrelease>\cs_gset_protected:Npn \@@_new_reversed:n #1
%<latexrelease> {
%<latexrelease> \@@_new:n {#1}
%<latexrelease> \tl_gset:cn { g_@@_#1_reversed_tl } { - }
%<latexrelease> }
%<latexrelease>\cs_undefine:N \@@_new_reversed:nn
%<latexrelease>\cs_gset_protected:Npn \hook_new_reversed_with_args:nn #1 #2 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hook_new_pair:nn,\hook_new_pair_with_args:nnn}
% A shorthand for declaring a normal and a (matching) reversed hook in one go.
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_new_pair_with_args:nnn} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_new_pair_with_args:nnn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_new_pair:nn #1#2
{ \@@_normalize_hook_args:Nnn \@@_new_pair:nnn {#1} {#2} { 0 } }
\cs_new_protected:Npn \hook_new_pair_with_args:nnn #1#2#3
{ \@@_normalize_hook_args:Nnn \@@_new_pair:nnn {#1} {#2} {#3} }
\cs_new_protected:Npn \@@_new_pair:nnn #1 #2 #3
{
\@@_if_declared:nTF {#1}
{ \msg_error:nnn { hooks } { exists } {#1} }
{
\@@_if_declared:nTF {#2}
{ \msg_error:nnn { hooks } { exists } {#2} }
{
\@@_new:nn {#1} {#3}
\@@_new_reversed:nn {#2} {#3}
}
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_new_pair_with_args:nnn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \hook_new_pair:nn #1#2
%<latexrelease> {
%<latexrelease> \hook_new:n {#1}
%<latexrelease> \hook_new_reversed:n {#2}
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \hook_new_pair_with_args:nnn #1#2#3
%<latexrelease> { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_include_legacy_code_chunk:n}
% The \LaTeX{} legacy concept for hooks uses with hooks the
% following naming scheme in the code: \cs{@...hook}.
%
% If this macro is not empty we add it under the label
% \texttt{legacy} to the current hook and then empty it globally.
% This way packages or classes directly manipulating commands such
% as \cs{@begindocumenthook} still get their hook data added.
% \begin{quote}
% \textbf{Warning: this support will vanish in future releases!}
% \end{quote}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_include_legacy_code_chunk:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_include_legacy_code_chunk:n #1
{
% \end{macrocode}
% If the macro doesn't exist (which is the usual case) then nothing
% needs to be done.
% \begin{macrocode}
\tl_if_exist:cT { @#1hook }
{
% \end{macrocode}
% Of course if the legacy hook exists but is empty, there is no need
% to add anything under \texttt{legacy} the legacy label.
% \begin{macrocode}
\tl_if_empty:cF { @#1hook }
{
% \end{macrocode}
% Here we set \cs{@@_replacing_args_false:} because no legacy code
% will reference hook arguments.
% \changes{v1.1b}{2023/04/16}
% {\cs{@@_replacing_args_false:} in
% \cs{@@_include_legacy_code_chunk:n}.}
% \begin{macrocode}
\@@_replacing_args_false:
\use:e
{
\@@_hook_gput_code_do:nnn {#1} { legacy }
{ \exp_not:v { @#1hook } }
}
\@@_replacing_args_reset:
% \end{macrocode}
% Once added to the hook, we need to clear it otherwise it might
% get added again later if the hook data gets updated.
% \begin{macrocode}
\@@_tl_gclear:c { @#1hook }
}
}
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_include_legacy_code_chunk:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_include_legacy_code_chunk:n #1
%<latexrelease> {
%<latexrelease> \tl_if_exist:cT { @#1hook }
%<latexrelease> {
%<latexrelease> \tl_if_empty:cF { @#1hook }
%<latexrelease> {
%<latexrelease> \exp_args:Nnnv \@@_hook_gput_code_do:nnn
%<latexrelease> {#1} { legacy } { @#1hook }
%<latexrelease> \@@_tl_gclear:c { @#1hook }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Disabling and providing hooks}
%
% \changes{v1.0p}{2021/08/20}{Renames of generic hook commands (gh/638)}
%
% \begin{macro}{\hook_disable_generic:n}
% \begin{macro}{\@@_disable:n}
% \begin{macro}[pTF]{\@@_if_disabled:n}
%
% Disables a hook by creating its
% \cs[no-index]{g_@@_\meta{hook}_declared_tl} so that the hook
% errors when used with \cs{hook_new:n}, then it undefines
% \cs{@@\textvisiblespace\meta{hook}} so that it may not be executed.
%
% This does not clear any code that may be already stored in the
% hook's structure, but doesn't allow adding more code.
% \cs{@@_if_disabled:nTF} uses that specific combination to check
% if the hook is disabled.
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/06/01}{\hook_disable_generic:n}
%<latexrelease> {Disable~hooks}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \hook_disable_generic:n #1
{ \@@_normalize_hook_args:Nn \@@_disable:n {#1} }
\cs_new_protected:Npn \@@_disable:n #1
{
\tl_gclear_new:c { g_@@_#1_declared_tl }
\cs_undefine:c { @@~#1 }
}
\prg_new_conditional:Npnn \@@_if_disabled:n #1 { p, T, F, TF }
{
\bool_lazy_and:nnTF
{ \tl_if_exist_p:c { g_@@_#1_declared_tl } }
{ ! \cs_if_exist_p:c { @@~#1 } }
{ \prg_return_true: }
{ \prg_return_false: }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_disable_generic:n}
%<latexrelease> {Disable~hooks}
%<latexrelease>
%<latexrelease>\cs_new_protected:Npn \hook_disable_generic:n #1 {}
%<latexrelease>
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hook_activate_generic:n}
% \begin{macro}{\@@_activate_generic:n}
% The \cs{hook_activate_generic:n} declaration declares a new hook if it
% wasn't declared already, in which case it only checks that the
% already existing hook is not a reversed hook.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_activate_generic:n}
%<latexrelease> {Providing~hooks}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \hook_activate_generic:n #1
{ \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { } }
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_activate_generic:nn #1 #2
{
% \end{macrocode}
% If the hook to be activated was disabled we warn (for now --- this
% may change).
% \begin{macrocode}
\@@_if_disabled:nTF {#1}
{ \msg_warning:nnn { hooks } { activate-disabled } {#1} }
% \end{macrocode}
% Otherwise we check if the hook is not declared, and if it isn't,
% figure out if it's reversed or not, then declare it accordingly.
% \begin{macrocode}
{
\@@_if_declared:nF {#1}
{
\tl_new:c { g_@@_#1_declared_tl }
\@@_make_usable:nn {#1} { 0 }
\tl_gset:cx { g_@@_#1_reversed_tl }
{ \@@_if_generic_reversed:nT {#1} { - } }
% \end{macrocode}
% Reflect that we have activated the generic hook and set its
% execution code.
% \changes{v1.0v}{2022/06/15}{Ensure that a newly activated generic hook
% gets its execution code set}
% \begin{macrocode}
\@@_update_hook_code:n {#1}
}
}
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/06/01}{\hook_activate_generic:n}
%<latexrelease> {Providing~hooks}
%<latexrelease>\cs_gset_protected:Npn \@@_activate_generic:nn #1 #2
%<latexrelease> {
%<latexrelease> \@@_if_disabled:nTF {#1}
%<latexrelease> { \msg_warning:nnn { hooks } { activate-disabled } {#1} }
%<latexrelease> {
%<latexrelease> \@@_if_declared:nF {#1}
%<latexrelease> {
%<latexrelease> \tl_new:c { g_@@_#1_declared_tl }
%<latexrelease> \@@_make_usable:n {#1}
%<latexrelease> \tl_gset:cx { g_@@_#1_reversed_tl }
%<latexrelease> { \@@_if_generic_reversed:nT {#1} { - } }
%<latexrelease> \@@_update_hook_code:n {#1}
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_activate_generic:n}
%<latexrelease> {Providing~hooks}
%<latexrelease>\cs_gset_protected:Npn \hook_activate_generic:n #1 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Parsing a label}
%
% \begin{macro}[EXP]{\@@_parse_label_default:n}
% This macro checks if a label was given (not \cs{c_novalue_tl}), and
% if so, tries to parse the label looking for a leading \verb|.| to
% replace by \cs{@@_currname_or_default:}.
% \begin{macrocode}
\cs_new:Npn \@@_parse_label_default:n #1
{
\tl_if_novalue:nTF {#1}
{ \@@_currname_or_default: }
{ \tl_trim_spaces_apply:nN {#1} \@@_parse_dot_label:n }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_parse_dot_label:n}
% \begin{macro}[EXP]{
% \@@_parse_dot_label:w,
% \@@_parse_dot_label_cleanup:w,
% \@@_parse_dot_label_aux:w
% }
% Start by checking if the label is empty, which raises an error, and
% uses the fallback value. If not,
% split the label at a \verb|./|, if any, and check if no tokens are
% before the \verb|./|, or if the only character is a \verb|.|.
% If these requirements are fulfilled, the leading
% \verb|.| is replaced with \cs{@@_currname_or_default:}. Otherwise
% the label is returned unchanged.
% \begin{macrocode}
\cs_new:Npn \@@_parse_dot_label:n #1
{
\tl_if_empty:nTF {#1}
{
\msg_expandable_error:nn { hooks } { empty-label }
\@@_currname_or_default:
}
{
\str_if_eq:nnTF {#1} { . }
{ \@@_currname_or_default: }
{ \@@_parse_dot_label:w #1 ./ \s_@@_mark }
}
}
\cs_new:Npn \@@_parse_dot_label:w #1 ./ #2 \s_@@_mark
{
\tl_if_empty:nTF {#1}
{ \@@_parse_dot_label_aux:w #2 \s_@@_mark }
{
\tl_if_empty:nTF {#2}
{ \@@_make_name:n {#1} }
{ \@@_parse_dot_label_cleanup:w #1 ./ #2 \s_@@_mark }
}
}
\cs_new:Npn \@@_parse_dot_label_cleanup:w #1 ./ \s_@@_mark {#1}
\cs_new:Npn \@@_parse_dot_label_aux:w #1 ./ \s_@@_mark
{ \@@_currname_or_default: / \@@_make_name:n {#1} }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_currname_or_default:}
% This uses \cs{g_@@_hook_curr_name_tl} if it is set, otherwise it tries
% \cs{@currname}. If neither is set, it raises an error and uses the
% fallback value \verb|label-missing|.
% \begin{macrocode}
\cs_new:Npn \@@_currname_or_default:
{
\tl_if_empty:NTF \g_@@_hook_curr_name_tl
{
\tl_if_empty:NTF \@currname
{
\msg_expandable_error:nnn { latex2e } { should-not-happen }
{ Empty~default~label. }
\@@_make_name:n { label-missing }
}
{ \@currname }
}
{ \g_@@_hook_curr_name_tl }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_make_name:n,\@@_make_name:w}
% This provides a standard sanitization of a hook's name.
% It uses \cs{cs:w} to build a control sequence out of the hook name,
% then uses \cs{cs_to_str:N} to get the string representation of that,
% without the escape character. \cs{cs:w}-based expansion is used
% instead of |e|-based because Unicode characters don't behave well
% inside \cs{expanded}. The macro adds the \cs[no-index]{@@\textvisiblespace} prefix to the
% hook name to reuse the hook's code token list to build the csname
% and avoid leaving \enquote{public} control sequences defined
% (as~\cs[no-index]{relax}) in TeX's memory.
% \begin{macrocode}
\cs_new:Npn \@@_make_name:n #1
{
\exp_after:wN \exp_after:wN \exp_after:wN \@@_make_name:w
\exp_after:wN \token_to_str:N \cs:w @@~ #1 \cs_end:
}
\exp_last_unbraced:NNNNo
\cs_new:Npn \@@_make_name:w #1 \tl_to_str:n { @@~ } { }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_normalize_hook_args:Nn}
% \begin{macro}{\@@_normalize_hook_args:Nnn}
% \begin{macro}{\@@_normalize_hook_rule_args:Nnnnn}
% \begin{macro}{\@@_normalize_hook_args_aux:Nn}
% This is the standard route for normalizing hook and label arguments. The main
% macro does the entire operation within a group so that csnames made
% by \cs{@@_make_name:n} are wiped off before continuing. This means
% that this function cannot be used for \cs{hook_use:n}!
% \begin{macrocode}
\cs_new_protected:Npn \@@_normalize_hook_args_aux:Nn #1 #2
{
\group_begin:
\use:e
{
\group_end:
\exp_not:N #1 #2
}
}
\cs_new_protected:Npn \@@_normalize_hook_args:Nn #1 #2
{
\@@_normalize_hook_args_aux:Nn #1
{ { \@@_parse_label_default:n {#2} } }
}
\cs_new_protected:Npn \@@_normalize_hook_args:Nnn #1 #2 #3
{
\@@_normalize_hook_args_aux:Nn #1
{
{ \@@_parse_label_default:n {#2} }
{ \@@_parse_label_default:n {#3} }
}
}
\cs_new_protected:Npn \@@_normalize_hook_rule_args:Nnnnn #1 #2 #3 #4 #5
{
\@@_normalize_hook_args_aux:Nn #1
{
{ \@@_parse_label_default:n {#2} }
{ \@@_parse_label_default:n {#3} }
{ \tl_trim_spaces:n {#4} }
{ \@@_parse_label_default:n {#5} }
}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_curr_name_push:n,\@@_curr_name_push_aux:n}
% \begin{macro}{\@@_curr_name_pop:}
% \begin{macro}{\@@_end_document_label_check:}
% The token list \cs{g_@@_hook_curr_name_tl} stores the name of the
% current package/file to be used as the default label in hooks.
% Providing a consistent interface is tricky because packages can
% be loaded within packages, and some packages may not use
% \cs{SetDefaultHookLabel} to change the default label (in which
% case \cs{@currname} is used).
%
% To pull that one off, we keep a stack that contains the default
% label for each level of input. The bottom of the stack contains the
% default label for the |top-level| (this stack should never go
% empty). If we're building the format, set the default label to be
% |top-level|:
% \begin{macrocode}
\tl_gset:Nn \g_@@_hook_curr_name_tl { top-level }
% \end{macrocode}
%
% Then, in case we're in \pkg{latexrelease} we push something on
% the stack to support roll forward. But in some rare cases,
% \pkg{latexrelease} may be loaded inside another package (notably
% \pkg{platexrelease}), so we'll first push the |top-level| entry:
% \changes{v1.0i}{2021/03/18}
% {Only add \texttt{top-level} if not already there.}
% \begin{macrocode}
%<latexrelease>\seq_if_empty:NT \g_@@_name_stack_seq
%<latexrelease> { \seq_gput_right:Nn \g_@@_name_stack_seq { top-level } }
% \end{macrocode}
% then we dissect the \cs{@currnamestack}, adding \cs{@currname} to
% the stack:
% \changes{v1.0f}{2020/11/24}{Support for roll forward (gh/434)}
% \begin{macrocode}
%<latexrelease>\cs_set_protected:Npn \@@_tmp:w #1 #2 #3
%<latexrelease> {
%<latexrelease> \quark_if_recursion_tail_stop:n {#1}
%<latexrelease> \seq_gput_right:Nn \g_@@_name_stack_seq {#1}
%<latexrelease> \@@_tmp:w
%<latexrelease> }
%<latexrelease>\exp_after:wN \@@_tmp:w \@currnamestack
%<latexrelease> \q_recursion_tail \q_recursion_tail
%<latexrelease> \q_recursion_tail \q_recursion_stop
% \end{macrocode}
% and finally set the default label to be the \cs{@currname}:
% \changes{v1.0i}{2021/03/18}
% {Remove the (empty) \enquote{top-level} from \cs{@currnamestack}.}
% \begin{macrocode}
%<latexrelease>\tl_gset:Nx \g_@@_hook_curr_name_tl { \@currname }
%<latexrelease>\seq_gpop_right:NN \g_@@_name_stack_seq \l_@@_tmpa_tl
% \end{macrocode}
%
% Two commands keep track of the stack: when a file is input,
% \cs{@@_curr_name_push:n} pushes the current default label onto the
% stack and sets the new default label (all in one go):
% \begin{macrocode}
\cs_new_protected:Npn \@@_curr_name_push:n #1
{ \exp_args:Nx \@@_curr_name_push_aux:n { \@@_make_name:n {#1} } }
\cs_new_protected:Npn \@@_curr_name_push_aux:n #1
{
\tl_if_blank:nTF {#1}
{ \msg_error:nn { hooks } { no-default-label } }
{
\str_if_eq:nnTF {#1} { top-level }
{
\msg_error:nnnnn { hooks } { set-top-level }
{ to } { PushDefaultHookLabel } {#1}
}
{
\seq_gpush:NV \g_@@_name_stack_seq \g_@@_hook_curr_name_tl
\tl_gset:Nn \g_@@_hook_curr_name_tl {#1}
}
}
}
% \end{macrocode}
% and when an input is over, the topmost item of the stack is popped,
% since that label will not be used again, and \cs{g_@@_hook_curr_name_tl}
% is updated to equal the now topmost item of the stack:
% \begin{macrocode}
\cs_new_protected:Npn \@@_curr_name_pop:
{
\seq_gpop:NNTF \g_@@_name_stack_seq \l_@@_return_tl
{ \tl_gset_eq:NN \g_@@_hook_curr_name_tl \l_@@_return_tl }
{ \msg_error:nn { hooks } { extra-pop-label } }
}
% \end{macrocode}
%
% At the end of the document we want to check if there was no
% \cs{@@_curr_name_push:n} without a matching \cs{@@_curr_name_pop:}
% (not a critical error, but it might indicate that something else is
% not quite right):
% \begin{macrocode}
\tl_gput_right:Nn \@kernel@after@enddocument@afterlastpage
{ \@@_end_document_label_check: }
\cs_new_protected:Npn \@@_end_document_label_check:
{
\seq_gpop:NNT \g_@@_name_stack_seq \l_@@_return_tl
{
\msg_error:nnx { hooks } { missing-pop-label }
{ \g_@@_hook_curr_name_tl }
\tl_gset_eq:NN \g_@@_hook_curr_name_tl \l_@@_return_tl
\@@_end_document_label_check:
}
}
% \end{macrocode}
%
% The token list \cs{g_@@_hook_curr_name_tl} is but a mirror of the
% top of the stack.
%
% \begin{macro}{\@@_set_default_hook_label:n,\@@_set_default_label:n}
% Now define a wrapper that replaces the top of the stack with the
% argument, and updates \cs{g_@@_hook_curr_name_tl} accordingly.
% \begin{macrocode}
\cs_new_protected:Npn \@@_set_default_hook_label:n #1
{
\seq_if_empty:NTF \g_@@_name_stack_seq
{
\msg_error:nnnnn { hooks } { set-top-level }
{ for } { SetDefaultHookLabel } {#1}
}
{ \exp_args:Nx \@@_set_default_label:n { \@@_make_name:n {#1} } }
}
\cs_new_protected:Npn \@@_set_default_label:n #1
{
\str_if_eq:nnTF {#1} { top-level }
{
\msg_error:nnnnn { hooks } { set-top-level }
{ to } { SetDefaultHookLabel } {#1}
}
{ \tl_gset:Nn \g_@@_hook_curr_name_tl {#1} }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Adding or removing hook code}
%
% \begin{macro}{\hook_gput_code:nnn,\hook_gput_code_with_args:nnn}
% \begin{macro}{
% \@@_gput_code:nnn,
% \@@_gput_code_store:nnn,
% \@@_hook_gput_code_do:nnn,
% \@@_prop_gput_labeled_cleanup:nnn,
% \@@_prop_gput_labeled_do:Nnnn
% }
%
% With \cs{hook_gput_code:nnn}\Arg{hook}\Arg{label}\Arg{code} a
% chunk of \meta{code} is added to an existing \meta{hook} labeled
% with \meta{label}.
% \changes{v1.0o}{2021/07/22}{Do not queue removals (gh/625)}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_gput_code_with_args:nnn} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gput_code:nnn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_gput_code:nnn #1 #2 #3
{
\@@_replacing_args_false:
\@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
\@@_replacing_args_reset:
}
\cs_new_protected:Npn \hook_gput_code_with_args:nnn #1 #2 #3
{
\@@_replacing_args_true:
\@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} {#3}
\@@_replacing_args_reset:
}
% \end{macrocode}
%
% If \cs{AddToHookWithArguments} was used, do some sanity checking,
% and if it's not possible to use arguments at this point, fall back
% to regular \cs{AddToHook} by using \cs{@@_replacing_args_false:}.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
\cs_new_protected:Npn \@@_gput_code:nnn #1 #2 #3
{
\@@_chk_args_allowed:nn {#1} { AddToHook }
% \end{macrocode}
% Then check if the code should be executed immediately, rather than
% stored:
% \changes{v1.0r}{2021/09/06}{Use dedicated conditional (gh/606)}
% \begin{macrocode}
\@@_if_execute_immediately:nTF {#1}
{
% \end{macrocode}
% \cs{AddToHookWithArguments} can't be used on one-time hooks (that
% were already used).
% \begin{macrocode}
\@@_if_replacing_args:TF
{
\msg_error:nnnn { hooks } { one-time-args }
{#1} { AddToHook }
}
{ }
\use:n
}
{ \@@_gput_code_store:nnn {#1} {#2} }
{#3}
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_gput_code_store:nnn #1 #2 #3
{
% \end{macrocode}
% Then check if the hook is usable.
% \begin{macrocode}
\@@_if_usable:nTF {#1}
% \end{macrocode}
% If so we simply add (or append) the new code to the property list
% holding different chunks for the hook. At \verb=\begin{document}=
% this is then sorted into a token list for fast execution.
% \begin{macrocode}
{
\@@_hook_gput_code_do:nnn {#1} {#2} {#3}
% \end{macrocode}
% However, if there is an update within the document we need to alter
% this execution code which is done by
% \cs{@@_update_hook_code:n}. In the preamble this does nothing.
% \begin{macrocode}
\@@_update_hook_code:n {#1}
}
% \end{macrocode}
%
% If the hook is not usable, before giving up, check if it's not
% disabled and otherwise try to declare it as a generic hook, if its
% name matches one of the valid patterns.
% \begin{macrocode}
{
\@@_if_disabled:nTF {#1}
{ \msg_error:nnn { hooks } { hook-disabled } {#1} }
{ \@@_try_declaring_generic_hook:nnn {#1} {#2} {#3} }
}
}
% \end{macrocode}
%
% This macro will unconditionally add a chunk of code to the given hook.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
\cs_new_protected:Npn \@@_hook_gput_code_do:nnn #1 #2 #3
{
% \end{macrocode}
% However, first some debugging info if debugging is enabled:
% \begin{macrocode}
\@@_debug:n{\iow_term:x{****~ Add~ to~
\@@_if_usable:nF {#1} { undeclared~ }
hook~ #1~ (#2)
\on@line\space <-~ \tl_to_str:n{#3}} }
% \end{macrocode}
% Then try to get the code chunk labeled \verb=#2= from the hook.
% If there's code already there, then append \verb=#3= to that,
% otherwise just put \verb=#3=. If the current label is |top-level|,
% the code is added to a dedicated token list
% \cs{@@_toplevel\textvisiblespace\meta{hook}} that goes at the end of the
% hook (or at the beginning, for a reversed hook), just before
% \cs[no-index]{@@_next\textvisiblespace\meta{hook}}.
% \begin{macrocode}
\str_if_eq:nnTF {#2} { top-level }
{
\str_if_eq:eeTF { top-level } { \@@_currname_or_default: }
{
% \end{macrocode}
% If the hook's basic structure does not exist, we need to declare it
% with \cs{@@_init_structure:n}.
% \begin{macrocode}
\@@_init_structure:n {#1}
% \end{macrocode}
% Then append to the \verb|_toplevel| container for the hook.
% \begin{macrocode}
\@@_cs_gput_right:nnn { _toplevel } {#1} {#3}
}
{ \msg_error:nnn { hooks } { misused-top-level } {#1} }
}
{
% \end{macrocode}
% When adding to the code pool, we have to double hashes if
% \cs{AddToHook} was used (\verb|replacing_args| is false), so that
% later it is turned into a single parameter token, rather than a
% parameter to the hook macro.
% \begin{macrocode}
\exp_args:Nx \@@_prop_gput_labeled_cleanup:nnn
{
\@@_if_replacing_args:TF
{ \exp_not:n }
{ \@@_double_hashes:n }
{#3}
}
{#1} {#2}
}
}
% \end{macrocode}
%
% Adds code to a hook's code pool.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
\cs_new_protected:Npn \@@_prop_gput_labeled_cleanup:nnn #1 #2 #3
{
\tl_set:Nn \l_@@_return_tl {#1}
\@@_if_replacing_args:TF
{
\@@_if_usable:nT {#2}
{
\@@_set_normalise_fn:nn {#2}
{ Invalid~code~added~\msg_line_context: }
\@@_normalise_fn:nn {#3} {#1}
\prop_get:NnN \l_@@_work_prop {#3} \l_@@_return_tl
}
}
{ }
\exp_args:NcV \@@_prop_gput_labeled_do:Nnn
{ g_@@_#2_code_prop } \l_@@_return_tl {#3}
}
\cs_new_protected:Npn \@@_prop_gput_labeled_do:Nnn #1 #2 #3
{
\prop_get:NnNTF #1 {#3} \l_@@_return_tl
{ \prop_gput:Nno #1 {#3} { \l_@@_return_tl #2 } }
{ \prop_gput:Nnn #1 {#3} {#2} }
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gput_code:nnn}
%<latexrelease> {Providing~hooks}
%<latexrelease>\cs_gset_protected:Npn \hook_gput_code:nnn #1 #2
%<latexrelease> { \@@_normalize_hook_args:Nnn \@@_gput_code:nnn {#1} {#2} }
%<latexrelease>\cs_gset_protected:Npn \@@_gput_code:nnn #1 #2 #3
%<latexrelease> {
%<latexrelease> \@@_if_execute_immediately:nTF {#1}
%<latexrelease> {#3}
%<latexrelease> {
%<latexrelease> \@@_if_usable:nTF {#1}
%<latexrelease> {
%<latexrelease> \@@_hook_gput_code_do:nnn {#1} {#2} {#3}
%<latexrelease> \@@_update_hook_code:n {#1}
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \@@_if_disabled:nTF {#1}
%<latexrelease> { \msg_error:nnn { hooks } { hook-disabled } {#1} }
%<latexrelease> { \@@_try_declaring_generic_hook:nnn {#1} {#2} {#3} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \@@_hook_gput_code_do:nnn #1 #2 #3
%<latexrelease> {
%<latexrelease> \@@_debug:n{\iow_term:x{****~ Add~ to~
%<latexrelease> \@@_if_usable:nF {#1} { undeclared~ }
%<latexrelease> hook~ #1~ (#2)
%<latexrelease> \on@line\space <-~ \tl_to_str:n{#3}} }
%<latexrelease> \str_if_eq:nnTF {#2} { top-level }
%<latexrelease> {
%<latexrelease> \str_if_eq:eeTF { top-level } { \@@_currname_or_default: }
%<latexrelease> {
%<latexrelease> \@@_init_structure:n {#1}
%<latexrelease> \@@_tl_gput_right:cn { @@_toplevel~#1 } {#3}
%<latexrelease> }
%<latexrelease> { \msg_error:nnn { hooks } { misused-top-level } {#1} }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \prop_get:cnNTF { g_@@_#1_code_prop } {#2} \l_@@_return_tl
%<latexrelease> {
%<latexrelease> \prop_gput:cno { g_@@_#1_code_prop } {#2}
%<latexrelease> { \l_@@_return_tl #3 }
%<latexrelease> }
%<latexrelease> { \prop_gput:cnn { g_@@_#1_code_prop } {#2} {#3} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \hook_gput_code_with_args:nnn #1#2#3 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_chk_args_allowed:nn}
% This macro checks if it is possible to add code with references to
% a hook's arguments for hook \verb|#1|. It only does something if
% the function being run is \verb|replacing_args|. This macro will
% error if the hook is declared and takes no arguments, then it will
% set \cs{@@_replacing_args_false:} so that the macro which called it
% will add the code normally.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_chk_args_allowed:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_chk_args_allowed:nn #1 #2
{
\@@_if_replacing_args:TF
{
\@@_if_declared:nT {#1}
{ \tl_if_empty:cT { c_@@_#1_parameter_tl } { \use_ii:nn } }
\use_none:n
{
\msg_error:nnnn { hooks } { without-args } {#1} {#2}
\@@_replacing_args_false:
}
}
{ }
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_chk_args_allowed:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_chk_args_allowed:nn
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_gput_undeclared_hook:nnn}
% Often it may happen that a package $A$ defines a hook \verb=foo=,
% but package $B$, that adds code to that hook, is loaded before $A$.
% In such case we need to add code to the hook before its declared.
% An implicitly declared hook doesn't have arguments (in principle),
% so use \cs{c_false_bool} here.
% \begin{macrocode}
\cs_new_protected:Npn \@@_gput_undeclared_hook:nnn #1 #2 #3
{
\@@_init_structure:n {#1}
\@@_hook_gput_code_do:nnn {#1} {#2} {#3}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_try_declaring_generic_hook:nnn,
% \@@_try_declaring_generic_next_hook:nn}
%
% These entry-level macros just pass the arguments along to the
% common \cs{@@_try_declaring_generic_hook:nNNnn} with the right
% functions to execute when some action is to be taken.
%
% The wrapper \cs{@@_try_declaring_generic_hook:nnn} then defers
% \cs{hook_gput_code:nnn} if the generic hook was declared, or to
% \cs{@@_gput_undeclared_hook:nnn} otherwise (the hook was tested for
% existence before, so at this point if it isn't generic, it doesn't
% exist).
%
% The wrapper \cs{@@_try_declaring_generic_next_hook:nn} for
% next-execution hooks does the same: it defers the code to
% \cs{hook_gput_next_code:nn} if the generic hook was declared, or
% to \cs{@@_gput_next_do:nn} otherwise.
% \changes{v1.0p}{2021/08/25}{Standardise generic hook names (gh/648)}
% \changes{v1.1d}{2023/05/21}
% {Changes to allow support arguments in cmd hooks (cmd-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_try_declaring_generic_hook:nnn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_try_declaring_generic_hook:nnn #1
{
\@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
\@@_gput_code:nnn
\@@_gput_undeclared_hook:nnn
{#1}
}
\cs_new_protected:Npn \@@_try_declaring_generic_next_hook:nn #1
{
\@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
\@@_gput_next_code:nn
\@@_gput_next_do:nn
{#1}
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2021/11/15}{\@@_try_declaring_generic_hook:nnn}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_gset_protected:Npn \@@_try_declaring_generic_hook:nnn #1
%<latexrelease> {
%<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
%<latexrelease> \hook_gput_code:nnn
%<latexrelease> \@@_gput_undeclared_hook:nnn
%<latexrelease> {#1}
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \@@_try_declaring_generic_next_hook:nn #1
%<latexrelease> {
%<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
%<latexrelease> \hook_gput_next_code:nn
%<latexrelease> \@@_gput_next_do:nn
%<latexrelease> {#1}
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_try_declaring_generic_hook:nnn}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_new_protected:Npn \@@_try_declaring_generic_hook:nnn #1
%<latexrelease> {
%<latexrelease> \@@_try_declaring_generic_hook:nNNnn {#1}
%<latexrelease> \hook_gput_code:nnn \@@_gput_undeclared_hook:nnn
%<latexrelease> }
%<latexrelease>\cs_new_protected:Npn \@@_try_declaring_generic_next_hook:nn #1
%<latexrelease> {
%<latexrelease> \@@_try_declaring_generic_hook:nNNnn {#1}
%<latexrelease> \hook_gput_next_code:nn \@@_gput_next_do:nn
%<latexrelease> }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_try_declaring_generic_hook:nNNnn,
% \@@_try_declaring_generic_hook_split:nNNnn}
%
% \cs{@@_try_declaring_generic_hook:nNNnn}
% now splits the hook name
% at the first \texttt{/} (if any) and first checks if it is a
% file-specific hook (they require some normalization) using
% \cs{@@_if_file_hook:wTF}. If not then check it is one of a
% predefined set for generic names. We also split off the second
% component to see if we have to make a reversed hook. In either case
% the function returns \meta{true} for a generic hook and \meta{false}
% in other cases.
%
% \changes{v1.0s}{2021/09/28}
% {Correct usage of older \cs{@@_if_file_hook:wTF} (gh/675)}
% \begin{macrocode}
%<latexrelease>\cs_new_protected:Npn \@@_try_declaring_generic_hook:nNNnn #1
%<latexrelease> {
%<latexrelease> \@@_if_file_hook:wTF #1 / \s_@@_mark
%<latexrelease> {
%<latexrelease> \exp_args:Ne \@@_try_declaring_generic_hook_split:nNNnn
%<latexrelease> { \exp_args:Ne \@@_file_hook_normalize:n {#1} }
%<latexrelease> }
%<latexrelease> { \@@_try_declaring_generic_hook_split:nNNnn {#1} }
%<latexrelease> }
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\cs_new_protected:Npn \@@_try_declaring_generic_hook_split:nNNnn #1 #2 #3
%<latexrelease> {
%<latexrelease> \@@_try_declaring_generic_hook:wnTF #1 / / / \scan_stop: {#1}
%<latexrelease> { #2 }
%<latexrelease> { #3 } {#1}
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[TF]{\@@_try_declaring_generic_hook:wn}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_try_declaring_generic_hook:wn}
%<latexrelease> {Hooks~with~args}
\prg_new_protected_conditional:Npnn \@@_try_declaring_generic_hook:wn
#1 / #2 / #3 / #4 \scan_stop: #5 { TF }
{
\@@_if_generic:nTF {#5}
{
\@@_if_usable:nF {#5}
{
% \end{macrocode}
% If the hook doesn't exist yet we check if it is a \texttt{cmd}
% hook and if so we attempt patching the command in addition to
% declaring the hook.
%
% For some commands this will not be possible, in which case
% \cs{@@_patch_cmd_or_delay:Nnn} (defined in \texttt{ltcmdhooks})
% will generate an appropriate error message.
% \changes{v1.1d}{2023/05/21}
% {Changes to allow support arguments in cmd hooks (cmd-args).}
% \begin{macrocode}
\str_if_eq:nnT {#1} { cmd }
{
\@@_try_put_cmd_hook:n {#5}
\@@_make_usable:nn {#5} { 9 }
\use_none:nnn
}
% \end{macrocode}
%
% Declare the hook always even if it can't really be used (error
% message generated elsewhere).
%
% Here we use \cs{@@_make_usable:nn}, so that a \cs{hook_new:n} is
% still possible later. Generic hooks (except \hook{cmd} hooks) take
% no arguments, so use zero as the second argument.
% \begin{macrocode}
\@@_make_usable:nn {#5} { 0 }
}
\@@_if_generic_reversed:nT {#5}
{ \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
\prg_return_true:
}
{
% \end{macrocode}
%
% Generic hooks are all named \meta{type}/\meta{name}/\meta{place},
% where \meta{type} and \meta{place} are predefined
% (\cs{c_@@_generic_\meta{type}/./\meta{place}_tl}), and \meta{name}
% is the variable component. Older releases had some hooks with the
% \meta{name} in the third part, so the code below supports that
% syntax for a while, with a warning.
%
% The \cs{exp_after:wN} |...| \cs{exp:w} trick is there to remove the
% conditional structure inserted by
% \cs{@@_try_declaring_generic_hook:wnTF} and thus allow access to the
% tokens that follow it, as is needed to keep things going.
%
% When the deprecation cycle ends, the lines below should all be
% replaced by \cs{prg_return_false:}.
% \begin{macrocode}
\@@_if_deprecated_generic:nTF {#5}
{
\@@_deprecated_generic_warn:n {#5}
\exp_after:wN \@@_declare_deprecated_generic:NNn
\exp:w % \exp_end:
}
{ \prg_return_false: }
}
}
% \end{macrocode}
%
% \begin{macro}{
% \@@_deprecated_generic_warn:Nn,
% \@@_deprecated_generic_warn:Nw,
% }
% \cs{@@_deprecated_generic_warn:n} will issue a deprecation warning
% for a given hook, and mark that hook such that the warning will not
% be issued again (multiple warnings can be issued, but only once per
% hook).
% \begin{macrocode}
\cs_new_protected:Npn \@@_deprecated_generic_warn:n #1
{ \@@_deprecated_generic_warn:w #1 \s_@@_mark }
\cs_new_protected:Npn \@@_deprecated_generic_warn:w
#1 / #2 / #3 \s_@@_mark
{
\if_cs_exist:w @@~#1/#2/#3 \cs_end: \else:
\msg_warning:nnnnn { hooks } { generic-deprecated } {#1} {#2} {#3}
\fi:
\cs_gset_eq:cN { @@~#1/#2/#3 } \scan_stop:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \@@_do_deprecated_generic:Nn,
% \@@_do_deprecated_generic:Nw,
% \@@_declare_deprecated_generic:NNw,
% \@@_declare_deprecated_generic:NNw,
% }
% Now that the user has been told about the deprecation, we proceed by
% swapping \meta{name} and \meta{place} and adding the code to the
% correct hook.
% \begin{macrocode}
\cs_new_protected:Npn \@@_do_deprecated_generic:Nn #1 #2
{ \@@_do_deprecated_generic:Nw #1 #2 \s_@@_mark }
\cs_new_protected:Npn \@@_do_deprecated_generic:Nw #1
#2 / #3 / #4 \s_@@_mark
{ #1 { #2 / #4 / #3 } }
\cs_new_protected:Npn \@@_declare_deprecated_generic:NNn #1 #2 #3
{ \@@_declare_deprecated_generic:NNw #1 #2 #3 \s_@@_mark }
\cs_new_protected:Npn \@@_declare_deprecated_generic:NNw #1 #2
#3 / #4 / #5 \s_@@_mark
{
\@@_try_declaring_generic_hook:wnTF #3 / #5 / #4 / \scan_stop:
{ #3 / #5 / #4 }
#1 #2 { #3 / #5 / #4 }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\@@_try_declaring_generic_hook:wn}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\prg_new_protected_conditional:Npnn \@@_try_declaring_generic_hook:wn
%<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
%<latexrelease> {
%<latexrelease> \@@_if_generic:nTF {#5}
%<latexrelease> {
%<latexrelease> \@@_if_usable:nF {#5}
%<latexrelease> {
%<latexrelease> \str_if_eq:nnT {#1} { cmd }
%<latexrelease> { \@@_try_put_cmd_hook:n {#5} }
%<latexrelease> \@@_make_usable:n {#5}
%<latexrelease> }
%<latexrelease> \@@_if_generic_reversed:nT {#5}
%<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
%<latexrelease> \prg_return_true:
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \@@_if_deprecated_generic:nTF {#5}
%<latexrelease> {
%<latexrelease> \@@_deprecated_generic_warn:n {#5}
%<latexrelease> \exp_after:wN \@@_declare_deprecated_generic:NNn
%<latexrelease> \exp:w % \exp_end:
%<latexrelease> }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/06/01}{\@@_try_declaring_generic_hook:wn}
%<latexrelease> {Support~cmd~hooks}
%<latexrelease>\prg_new_protected_conditional:Npnn \@@_try_declaring_generic_hook:wn
%<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
%<latexrelease> {
%<latexrelease> \tl_if_empty:nTF {#2}
%<latexrelease> { \prg_return_false: }
%<latexrelease> {
%<latexrelease> \prop_if_in:NnTF \c_@@_generics_prop {#1}
%<latexrelease> {
%<latexrelease> \@@_if_usable:nF {#5}
%<latexrelease> {
%<latexrelease> \str_if_eq:nnT {#1} { cmd }
%<latexrelease> { \@@_try_put_cmd_hook:n {#5} }
%<latexrelease> \@@_make_usable:n {#5}
%<latexrelease> }
%<latexrelease> \prop_if_in:NnTF \c_@@_generics_reversed_ii_prop {#2}
%<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
%<latexrelease> {
%<latexrelease> \prop_if_in:NnT \c_@@_generics_reversed_iii_prop {#3}
%<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
%<latexrelease> }
%<latexrelease> \prg_return_true:
%<latexrelease> }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_try_declaring_generic_hook:wn}
%<latexrelease> {Support~cmd~hooks}
%<latexrelease>\prg_new_protected_conditional:Npnn \@@_try_declaring_generic_hook:wn
%<latexrelease> #1 / #2 / #3 / #4 \scan_stop: #5 { TF }
%<latexrelease> {
%<latexrelease> \tl_if_empty:nTF {#2}
%<latexrelease> { \prg_return_false: }
%<latexrelease> {
%<latexrelease> \prop_if_in:NnTF \c_@@_generics_prop {#1}
%<latexrelease> {
%<latexrelease> \@@_if_declared:nF {#5} { \hook_new:n {#5} }
%<latexrelease> \prop_if_in:NnTF \c_@@_generics_reversed_ii_prop {#2}
%<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
%<latexrelease> {
%<latexrelease> \prop_if_in:NnT \c_@@_generics_reversed_iii_prop {#3}
%<latexrelease> { \tl_gset:cn { g_@@_#5_reversed_tl } { - } }
%<latexrelease> }
%<latexrelease> \prg_return_true:
%<latexrelease> }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[pTF]{\@@_if_file_hook:w}
% \cs{@@_if_file_hook:wTF} checks if the argument is a valid
% file-specific hook (not, for example, |file/before|, but
% |file/foo.tex/before|). If it is a file-specific hook, then it
% executes the \meta{true} branch, otherwise \meta{false}.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\@@_if_file_hook:w}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_if_file_hook:w}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\prg_new_conditional:Npnn \@@_if_file_hook:w
%<latexrelease> #1 / #2 / #3 \s_@@_mark { TF }
%<latexrelease> {
%<latexrelease> \str_if_eq:nnTF {#1} { file }
%<latexrelease> {
%<latexrelease> \bool_lazy_or:nnTF
%<latexrelease> { \tl_if_empty_p:n {#3} }
%<latexrelease> { \str_if_eq_p:nn {#3} { / } }
%<latexrelease> { \prg_return_false: }
%<latexrelease> {
%<latexrelease> \prop_if_in:NnTF \c_@@_generics_file_prop {#2}
%<latexrelease> { \prg_return_true: }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_file_hook_normalize:n}
% \begin{macro}[EXP]{\@@_strip_double_slash:n,\@@_strip_double_slash:w}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\@@_file_hook_normalize:n}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% When a file-specific hook is found, before being declared it is
% lightly normalized by \cs{@@_file_hook_normalize:n}. The current
% implementation just replaces two consecutive slashes (|//|) by a
% single one, to cope with simple cases where the user did something
% like \verb|\def\input@path{{./mypath/}}|, in which case a hook would
% have to be \verb|\AddToHook{file/./mypath//file.tex/after}|.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_file_hook_normalize:n}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_new:Npn \@@_file_hook_normalize:n #1
%<latexrelease> { \@@_strip_double_slash:n {#1} }
%<latexrelease>\cs_new:Npn \@@_strip_double_slash:n #1
%<latexrelease> { \@@_strip_double_slash:w #1 // \s_@@_mark }
% \end{macrocode}
% This function is always called after testing if the argument is a
% file hook with \cs{@@_if_file_hook:wTF}, so we can assume it has
% three parts (it is either \verb|file/.../before| or
% \verb|file/.../after|), so we use \verb|#1/#2/#3 //| instead of just
% \verb|#1 //| to prevent losing a slash if the file name is empty.
% \changes{v1.0h}{2021/01/07}{Assume hook name has at least three
% nonempty parts (gh/464)}
% \begin{macrocode}
%<latexrelease>\cs_new:Npn \@@_strip_double_slash:w #1/#2/#3 // #4 \s_@@_mark
%<latexrelease> {
%<latexrelease> \tl_if_empty:nTF {#4}
%<latexrelease> { #1/#2/#3 }
%<latexrelease> { \@@_strip_double_slash:w #1/#2/#3 / #4 \s_@@_mark }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{
% \c_@@_generic_cmd/./before_tl,\c_@@_generic_cmd/./after_tl,
% \c_@@_generic_env/./before_tl,\c_@@_generic_env/./after_tl,
% \c_@@_generic_file/./before_tl,\c_@@_generic_file/./after_tl,
% \c_@@_generic_package/./before_tl,\c_@@_generic_package/./after_tl,
% \c_@@_generic_class/./before_tl,\c_@@_generic_class/./after_tl,
% \c_@@_generic_include/./before_tl,\c_@@_generic_include/./after_tl,
% \c_@@_generic_env/./begin_tl,\c_@@_generic_env/./end_tl,
% \c_@@_generic_include/./end_tl
% }
% Token lists defining the possible generic hooks. We don't provide
% any user interface to this as this is meant to be static.
% \begin{description}
% \item[\texttt{cmd}]
% The generic hooks used for commands.
% \item[\texttt{env}]
% The generic hooks used in \cs{begin} and \cs{end}.
% \item[\texttt{file}, \texttt{package}, \texttt{class}, \texttt{include}]
% The generic hooks used when loading a file
% \end{description}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\c_@@_generics_prop}
%<latexrelease> {Standardise~generic~hook~names}
\clist_map_inline:nn { cmd , env , file , package , class , include }
{
\tl_const:cn { c_@@_generic_#1/./before_tl } { + }
\tl_const:cn { c_@@_generic_#1/./after_tl } { - }
}
\tl_const:cn { c_@@_generic_env/./begin_tl } { + }
\tl_const:cn { c_@@_generic_env/./end_tl } { + }
% \end{macrocode}
%
% \changes{v1.0t}{2022/04/01}{Support generic \texttt{include/.../excluded} hooks}
% \begin{macrocode}
\tl_const:cn { c_@@_generic_include/./end_tl } { - }
\tl_const:cn { c_@@_generic_include/./excluded_tl } { + }
% \end{macrocode}
%
% Deprecated generic hooks:
% \begin{macrocode}
\clist_map_inline:nn { file , package , class , include }
{
\tl_const:cn { c_@@_deprecated_#1/./before_tl } { }
\tl_const:cn { c_@@_deprecated_#1/./after_tl } { }
}
\tl_const:cn { c_@@_deprecated_include/./end_tl } { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_generics_prop}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\prop_const_from_keyval:Nn \c_@@_generics_prop
%<latexrelease> {cmd=,env=,file=,package=,class=,include=}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c_@@_generics_reversed_ii_prop,
% \c_@@_generics_reversed_iii_prop,
% \c_@@_generics_file_prop}
% The following generic hooks are supposed to use reverse ordering
% (the |ii| and |iii| names are kept for the deprecation cycle):
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\c_@@_generics_reversed_ii_prop}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_generics_reversed_ii_prop}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\prop_const_from_keyval:Nn \c_@@_generics_reversed_ii_prop {after=,end=}
%<latexrelease>\prop_const_from_keyval:Nn \c_@@_generics_reversed_iii_prop {after=}
%<latexrelease>\prop_const_from_keyval:Nn \c_@@_generics_file_prop {before=,after=}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{
% \c_@@_parameter_cmd/./before_tl,\c_@@_parameter_cmd/./after_tl,
% }
% Token lists defining the number of arguments for a given type of
% generic hook.
% \changes{v1.1d}{2023/05/21}
% {Token lists added (cmd-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\c_@@_parameter_cmd/./before_tl}
%<latexrelease> {Hooks~with~args}
% \end{macrocode}
%
% \hook{cmd} hooks are declared with 9 arguments because they have a
% variable number of arguments (depending on the command they are
% attached to), so we use the maximum here.
% \begin{macrocode}
\tl_const:cn { c_@@_parameter_cmd/./before_tl } { #1#2#3#4#5#6#7#8#9 }
\tl_const:cn { c_@@_parameter_cmd/./after_tl } { #1#2#3#4#5#6#7#8#9 }
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_parameter_cmd/./before_tl}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hook_gremove_code:nn}
% \begin{macro}{\@@_gremove_code:nn}
%
% With \cs{hook_gremove_code:nn}\Arg{hook}\Arg{label} any code
% for \meta{hook} stored under \meta{label} is removed.
% \changes{v1.0o}{2021/07/22}{Do not queue removals (gh/625)}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gremove_code:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_gremove_code:nn #1 #2
{ \@@_normalize_hook_args:Nnn \@@_gremove_code:nn {#1} {#2} }
\cs_new_protected:Npn \@@_gremove_code:nn #1 #2
{
% \end{macrocode}
% First check that the hook code pool exists. \cs{@@_if_usable:nTF}
% isn't used here because it should be possible to remove code from a
% hook before its defined (see section~\ref{sec:querying}).
% \begin{macrocode}
\@@_if_structure_exist:nTF {#1}
{
% \end{macrocode}
% Then remove the chunk and run \cs{@@_update_hook_code:n} so
% that the execution token list reflects the change if we are after
% \verb=\begin{document}=.
%
% If all code is to be removed, clear the code pool
% \cs{g_@@_\meta{hook}_code_prop}, the top-level code
% \cs{@@_toplevel\textvisiblespace\meta{hook}}, and the next-execution code
% \cs{@@_next\textvisiblespace\meta{hook}}.
% \begin{macrocode}
\str_if_eq:nnTF {#2} {*}
{
\prop_gclear:c { g_@@_#1_code_prop }
\@@_toplevel_gset:nn {#1} { }
\@@_next_gset:nn {#1} { }
}
{
% \end{macrocode}
% If the label is |top-level| then clear the token list, as all code
% there is under the same label.
% \begin{macrocode}
\str_if_eq:nnTF {#2} { top-level }
{ \@@_toplevel_gset:nn {#1} { } }
{
\prop_gpop:cnNF { g_@@_#1_code_prop } {#2} \l_@@_return_tl
{ \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
}
}
% \end{macrocode}
% Finally update the code, if the hook exists.
% \begin{macrocode}
\@@_if_usable:nT {#1}
{ \@@_update_hook_code:n {#1} }
}
% \end{macrocode}
%
% If the code pool for this hook doesn't exist, show a warning:
% \begin{macrocode}
{
\@@_if_deprecated_generic:nTF {#1}
{
\@@_deprecated_generic_warn:n {#1}
\@@_do_deprecated_generic:Nn \@@_gremove_code:nn {#1} {#2}
}
{ \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gremove_code:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \@@_gremove_code:nn #1 #2
%<latexrelease> {
%<latexrelease> \@@_if_structure_exist:nTF {#1}
%<latexrelease> {
%<latexrelease> \str_if_eq:nnTF {#2} {*}
%<latexrelease> {
%<latexrelease> \prop_gclear:c { g_@@_#1_code_prop }
%<latexrelease> \@@_tl_gclear:c { @@_toplevel~#1 }
%<latexrelease> \@@_tl_gclear:c { @@_next~#1 }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \str_if_eq:nnTF {#2} { top-level }
%<latexrelease> { \@@_tl_gclear:c { @@_toplevel~#1 } }
%<latexrelease> {
%<latexrelease> \prop_gpop:cnNF { g_@@_#1_code_prop } {#2} \l_@@_return_tl
%<latexrelease> { \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \@@_if_usable:nT {#1}
%<latexrelease> { \@@_update_hook_code:n {#1} }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \@@_if_deprecated_generic:nTF {#1}
%<latexrelease> {
%<latexrelease> \@@_deprecated_generic_warn:n {#1}
%<latexrelease> \@@_do_deprecated_generic:Nn \@@_gremove_code:nn {#1} {#2}
%<latexrelease> }
%<latexrelease> { \msg_warning:nnnn { hooks } { cannot-remove } {#1} {#2} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_cs_gput_right:nnn}
% \begin{macro}{\@@_cs_gput_right_fast:nnn,\@@_cs_gput_right_slow:nnn}
% \begin{macro}{\@@_code_gset_auxi:nnnn,\@@_code_gset_auxi:eeen}
% This macro is used to append code to the \verb|toplevel| and
% \verb|next| token lists, trating them correctly depending on their
% number of arguments, and depending if the code being added should
% have parameter tokens understood as parameters, or doubled to be
% stored as parameter tokens.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \changes{v1.1e}{2023/06/06}
% {Short-circuit when the hook is declared without args (gh1078).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_cs_gput_right:nnn}
%<latexrelease> {Hooks~with~args}
% \end{macrocode}
%
% Check if the current hook is declared and takes no arguments. In
% this case, we short-circuit and use the simpler and much faster
% approach that doesn't require hash-doubling.
% \begin{macrocode}
\cs_new_protected:Npn \@@_cs_gput_right:nnn #1 #2
{
\if:w T
\@@_if_declared:nF {#2} { F }
\tl_if_empty:cF { c_@@_#2_parameter_tl } { F }
T
\exp_after:wN \@@_cs_gput_right_fast:nnn
\else:
\exp_after:wN \@@_cs_gput_right_slow:nnn
\fi:
{#1} {#2}
}
\cs_new_protected:Npn \@@_cs_gput_right_fast:nnn #1 #2 #3
{ \cs_gset:cpx { @@#1~#2 } { \exp_not:v { @@#1~#2 } \exp_not:n {#3} } }
\cs_new_protected:Npn \@@_cs_gput_right_slow:nnn #1 #2 #3
{
% \end{macrocode}
% The auxiliary \cs{@@_code_gset_auxi:eeen} just does the assignment
% at the end. Its first argument is the parameter text of the macro,
% which is chosen here depending if
% \cs[no-index]{c_@@_\meta{hook}_parameter_tl} exists, if the hook is
% declared, and if it's a generic hook.
% \begin{macrocode}
\cs_if_exist:cF { @@#1~#2 }
{ \@@_code_gset_aux:nnn {#1} {#2} { } }
\@@_code_gset_auxi:eeen
{
\@@_if_declared:nTF {#2}
{ \tl_use:c { c_@@_#2_parameter_tl } }
{
\@@_if_generic:nTF {#2}
{ \@@_generic_parameter:n {#2} }
{ \c_@@_nine_parameters_tl }
}
}
% \end{macrocode}
% Here we take the existing code in the macro, expand it with as many
% arguments as it takes, then double the hashes so the code can be
% reused. \pho{Maybe can be improved. The case of adding to an empty
% cs can be optimised by quickly checking \cs{cs_replacement_spec}.}
% \begin{macrocode}
{
\exp_args:NNo \exp_args:No \@@_double_hashes:n
{
\cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
{ \@@_braced_cs_parameter:n { @@#1~#2 } }
}
}
% \end{macrocode}
% Now the new code: if we are replacing arguments, then hashes are
% left untouched, otherwise they are doubled.
% \begin{macrocode}
{
\@@_if_replacing_args:TF
{ \exp_not:n }
{ \@@_double_hashes:n }
{#3}
}
% \end{macrocode}
% And finally, the csname which we'll define with all the above.
% \begin{macrocode}
{ @@#1~#2 }
}
% \end{macrocode}
%
% And as promised, the auxiliary that does the definition.
% \begin{macrocode}
\cs_new_protected:Npn \@@_code_gset_auxi:nnnn #1 #2 #3 #4
{ \cs_gset:cpn {#4} #1 { #2 #3 } }
\cs_generate_variant:Nn \@@_code_gset_auxi:nnnn { eeen }
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_cs_gput_right:nnn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_cs_gput_right:nnn
%<latexrelease>\cs_undefine:N \@@_cs_gput_right_fast:nnn
%<latexrelease>\cs_undefine:N \@@_cs_gput_right_slow:nnn
%<latexrelease>\cs_undefine:N \@@_code_gset_auxi:nnnn
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{
% \@@_code_gset:nn,\@@_code_gset:ne,
% \@@_toplevel_gset:nn,
% \@@_next_gset:nn,
% \@@_code_gset_aux:nnn
% }
% These macros define
% \cs[no-index]{@@\meta{type}\textvisiblespace\meta{hook}} (with
% \meta{type} being \verb|_next|, \verb|_toplevel|, or empty) with the
% given code and the parameters stored in
% \cs[no-index]{c_@@_\meta{hook}_parameter_tl} (or none, if that
% doesn't exist).
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_code_gset:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_code_gset:nn
{ \@@_code_gset_aux:nnn { } }
\cs_new_protected:Npn \@@_toplevel_gset:nn
{ \@@_code_gset_aux:nnn { _toplevel } }
\cs_new_protected:Npn \@@_next_gset:nn
{ \@@_code_gset_aux:nnn { _next } }
\cs_new_protected:Npn \@@_code_gset_aux:nnn #1 #2 #3
{
\cs_gset:cpn { @@#1~#2 \exp_last_unbraced:Ne }
{ \@@_parameter:n {#2} }
{#3}
}
\cs_generate_variant:Nn \@@_code_gset:nn { ne }
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_code_gset:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_code_gset:nn
%<latexrelease>\cs_undefine:N \@@_toplevel_gset:nn
%<latexrelease>\cs_undefine:N \@@_next_gset:nn
%<latexrelease>\cs_undefine:N \@@_code_gset_aux:nnn
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_normalise_cs_args:nn}
% This macro normalises the parameters of the macros
% \cs[no-index]{@@\meta{type}\textvisiblespace\meta{hook}} to take the
% right number of arguments after a hook is declared. At this point
% we know \cs[no-index]{c_@@_\meta{hook}_parameter_tl} exists, so use
% that to count the arguments and use that as \meta{parameter text}
% for the newly (re)defined macro.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_normalise_cs_args:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_normalise_cs_args:nn #1 #2
{
\cs_if_exist:cT { @@#1~#2 }
{
\@@_code_gset_auxi:eeen
{ \tl_use:c { c_@@_#2_parameter_tl } }
{
\exp_args:NNo \exp_args:No \@@_double_hashes:n
{
\cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
{ \@@_braced_cs_parameter:n { @@#1~#2 } }
}
}
{ }
{ @@#1~#2 }
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_normalise_cs_args:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_normalise_cs_args:nn
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_normalise_code_pool:n}
% \begin{macro}{\@@_set_normalise_fn:nn}
% This one's a bit of a hack. It takes a hook, and iterates over its
% code pool (\cs[no-index]{g_@@_\meta{hook}_code_prop}), redefining
% each code label to use only valid arguments. This is used when, for
% example, a code is added referencing arguments \verb|#1| and
% \verb|#2|, but the hook has only \verb|#1|. In this example, every
% reference to \verb|#2| is changed to \verb|##2|. This is done
% because otherwise \TeX{} will throw a low-level error every time
% some change happens to the hook (code is added, a rule is set, etc),
% which can get quite repetitive for no good reason.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_normalise_code_pool:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_normalise_code_pool:n #1
{
% \end{macrocode}
% First, call \cs{@@_set_normalise_fn:nn} with the hook name to set
% everything up, then we'll loop over the
% hook's code pool applying the normalisation above. After that's
% done, copy the temporary property list back to the hook's.
% \begin{macrocode}
\@@_set_normalise_fn:nn {#1} { Offending~label:~'##1' }
\prop_clear:N \l_@@_work_prop
\prop_map_function:cN { g_@@_#1_code_prop } \@@_normalise_fn:nn
\prop_gset_eq:cN { g_@@_#1_code_prop } \l_@@_work_prop
}
% \end{macrocode}
%
% The sole purpose of this function is to define
% \cs{@@_normalise_fn:nn}, which will then do the correcting of the
% code being added to the hook.
% \begin{macrocode}
\cs_new_protected:Npn \@@_set_normalise_fn:nn #1 #2
{
% \end{macrocode}
% To start, we define two auxiliary token lists.
% \cs[no-index]{l_@@_tmpb_tl} contains:
%\begin{verbatim}
% {\c__hook_hashes_tl 1}
% {\c__hook_hashes_tl 2}
% ...
% {\c__hook_hashes_tl 9}
%\end{verbatim}
% \begin{macrocode}
\cs_set:Npn \@@_tmp:w ##1##2##3##4##5##6##7##8##9 { }
\tl_set:Ne \l_@@_tmpb_tl
{ \@@_braced_cs_parameter:n { @@_tmp:w } }
\group_begin:
\@@_tl_set:cn { c_@@_hash_tl } { \exp_not:N \c_@@_hashes_tl }
\use:e
{
\group_end:
\tl_set:Nn \exp_not:N \l_@@_tmpb_tl { \l_@@_tmpb_tl }
}
% \end{macrocode}
% And \cs[no-index]{l_@@_tmpa_tl} contains:
%\begin{verbatim}
% {\c__hook_hash_tl 1}
% {\c__hook_hash_tl 2}
% ...
% {\c__hook_hash_tl <n>}
%\end{verbatim}
% with \meta{n} being the number of arguments declared for the hook.
% \begin{macrocode}
\exp_last_unbraced:NNf
\cs_set:Npn \@@_tmp:w { \@@_parameter:n {#1} } { }
\tl_set:Ne \l_@@_tmpa_tl { \@@_braced_cs_parameter:n { @@_tmp:w } }
% \end{macrocode}
% Now this function does the fun part. It is meant to be used with
% \cs{prop_map_function:NN}, taking a label name in \verb|##1| and the
% code stored in that label in \verb|##2|.
% \begin{macrocode}
\cs_gset_protected:Npx \@@_normalise_fn:nn ##1 ##2
{
% \end{macrocode}
% Here we'll define two auxiliary macros: the first one throws an
% error when it detects an invalid argument reference. It piggybacks
% on \TeX's low-level \enquote{Illegal parameter number} error, but it
% defines a weirdly-named control sequence so that the error comes out
% nicely formatted. For example, if the label \enquote{badpkg} adds
% some code that references argument \verb|#3| in the hook
% \enquote{foo}, which takes only two arguments, the error will be:
%\begin{verbatim}
% ! Illegal parameter number in definition of hook 'foo'.
% (hooks) Offending label: 'badpkg'.
% <to be read again>
% 3
%\end{verbatim}
% At the point of this definition, the error is raised if the code
% happens to reference an invalid argument. If it was possible to
% detect that this definition raised no error, the next step would be
% unnecessary. We'll do all this in a group so this weird definition
% doesn't leak out, and set \cs{tex_escapechar:D} to $-1$ so this hack
% shows up extra nice in the case of an error.
% \begin{macrocode}
\group_begin:
\int_set:Nn \tex_escapechar:D { -1 }
\cs_set:cpn
{
hook~'#1'. ^^J
(hooks) \prg_replicate:nn { 13 } { ~ }
#2 % more message text
}
\exp_not:v { c_@@_#1_parameter_tl }
{##2}
\group_end:
% \end{macrocode}
% This next macro, with a much less fabulous name, takes always nine
% arguments, and it just transfers the code \verb|##2| under the label
% \verb|##1| to the temporary property list. The first \meta{n}
% arguments are taken from \cs[no-index]{l_@@_tmpa_tl}, and the other
% $9-\meta{n}$ taken from \cs[no-index]{l_@@_tmpb_tl} (which contains
% twice as many \verb|#| tokens as the former). Then,
% \cs{@@_double_hashes:n} is used to double non-argument hashes, and
% expand the \cs{c_@@_hash_tl} and \cs{c_@@_hashes_tl} to the actual
% parameter tokens.
% \begin{macrocode}
\cs_set:Npn \exp_not:N \@@_tmp:w
\exp_not:V \c_@@_nine_parameters_tl
{
\prop_put:Nne \exp_not:N \l_@@_work_prop
{##1} { \exp_not:N \@@_double_hashes:n {##2} }
}
% \end{macrocode}
% This next macro, with a much less fabulous name, takes always nine
% arguments, and it just transfers the code \verb|##2| under the label
% \verb|##1| to the temporary property list. The first \meta{n}
% arguments are taken from \cs[no-index]{l_@@_tmpa_tl}, and the other
% $9-\meta{n}$ taken from \cs[no-index]{l_@@_tmpb_tl} (which contains
% twice as many \verb|#| tokens as the former). Then,
% \cs{@@_double_hashes:n} is used to double non-argument hashes, and
% expand the \cs{c_@@_hash_tl} and \cs{c_@@_hashes_tl} to the actual
% parameter tokens.
% \begin{macrocode}
\exp_not:N \@@_tmp:w
\exp_not:V \l_@@_tmpa_tl
\exp_args:No \exp_not:o
{ \exp_after:wN \@@_tmp:w \l_@@_tmpb_tl }
}
}
\cs_new_eq:NN \@@_normalise_fn:nn ?
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_normalise_code_pool:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_normalise_code_pool:n
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_cs_if_empty:c}
% Check if the expansion of a control sequence is empty by looking at
% its replacement text.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_cs_if_empty:c}
%<latexrelease> {Hooks~with~args}
\prg_new_conditional:Npnn \@@_cs_if_empty:c #1 { p, T, F, TF }
{
\if:w \scan_stop: \@@_replacement_spec:c {#1} \scan_stop:
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
\cs_new:Npn \@@_replacement_spec:c #1
{
\exp_args:Nc \token_if_macro:NT {#1}
{ \cs_replacement_spec:c {#1} }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_cs_if_empty:c}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_cs_if_empty:c
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_braced_cs_parameter:n}
% \begin{macro}{\@@_braced_hidden_loop:w}
% \begin{macro}{\@@_cs_parameter_count:N}
% \begin{macro}{\@@_cs_parameter_count:w,\@@_cs_end:w}
% Looks at the \meta{parameter text} of a control sequence, and
% returns a run of \enquote{hidden} braced parameters for that macro.
% This works as long as the macros take a simple run of zero to nine
% arguments. The parameters are \enquote{hidden} because the
% parameter tokens are returned inside \cs{c_@@_hash_tl} instead of
% explicitly, so that \cs{@@_double_hashes:n} won't touch these.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_braced_cs_parameter:n}
%<latexrelease> {Hooks~with~args}
\cs_new:Npn \@@_braced_cs_parameter:n #1
{
\exp_last_unbraced:Ne \@@_braced_hidden_loop:w
{ \exp_args:Nc \@@_cs_parameter_count:N {#1} } ? \s_@@_mark
}
\cs_new:Npn \@@_braced_hidden_loop:w #1
{
\if:w ? #1
\@@_use_i_delimit_by_s_mark:nw
\fi:
{ \exp_not:N \c_@@_hash_tl #1 }
\@@_braced_hidden_loop:w
}
\cs_new:Npn \@@_cs_parameter_count:N #1
{
\exp_last_unbraced:Nf \@@_cs_parameter_count:w
{ \token_if_macro:NT #1 { \cs_parameter_spec:N #1 } }
? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
? \@@_cs_end:w ? \@@_cs_end:w ? \@@_cs_end:w
\s_@@_mark
}
\cs_new:Npn \@@_cs_parameter_count:w #1#2 #3#4 #5#6 #7#8
{ #2 #4 #6 #8 \@@_cs_parameter_count:w }
\cs_new:Npn \@@_cs_end:w #1 \s_@@_mark { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% This function can't be undefined when rolling back because it's used
% at the end of this module to adequate the hook data structures to
% previous versions.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_braced_cs_parameter:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_braced_parameter:n}
% \begin{macro}{\@@_braced_real_loop:w}
% This one is used in simpler cases, where no special handling of
% hashes is required. This is used only inside
% \cs{@@_initialize_hook_code:n}, so it assumes
% \cs[no-index]{c_@@_\meta{hook}_parameter_tl} is defined, but should
% work otherwise.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_braced_parameter:n}
%<latexrelease> {Hooks~with~args}
\cs_new:Npn \@@_braced_parameter:n #1
{
\if_case:w
\int_eval:n
{ \exp_args:Nv \str_count:n { c_@@_#1_parameter_tl } / 3 }
\exp_stop_f:
\or: {##1}
\or: {##1} {##2}
\or: {##1} {##2} {##3}
\or: {##1} {##2} {##3} {##4}
\or: {##1} {##2} {##3} {##4} {##5}
\or: {##1} {##2} {##3} {##4} {##5} {##6}
\or: {##1} {##2} {##3} {##4} {##5} {##6} {##7}
\or: {##1} {##2} {##3} {##4} {##5} {##6} {##7} {##8}
\or: {##1} {##2} {##3} {##4} {##5} {##6} {##7} {##8} {##9}
\else:
\msg_expandable_error:nnn { latex2e } { should-not-happen }
{ Invalid~parameter~spec. }
\fi:
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_braced_parameter:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_braced_parameter:n
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parameter:n}
% This is just a shortcut to \verb|e|- or \verb|f|-expand to the
% \meta{parameter text} of the hook.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_parameter:n}
%<latexrelease> {Hooks~with~args}
\cs_new:Npn \@@_parameter:n #1
{
\cs:w c_@@_
\tl_if_exist:cTF { c_@@_#1_parameter_tl }
{ #1_parameter } { empty }
_tl \cs_end:
}
\cs_new:Npn \@@_generic_parameter:n #1
{ \@@_generic_parameter:w #1 / / / \s_@@_mark }
\cs_new:Npn \@@_generic_parameter:w #1 / #2 / #3 / #4 \s_@@_mark
{
\cs_if_exist_use:cF { c_@@_parameter_#1/./#3_tl }
{ \c_@@_empty_tl }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_parameter:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_parameter:n
%<latexrelease>\cs_undefine:N \@@_generic_parameter:n
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \subsection{Setting rules for hooks code}
%
% \begin{macro}{
% \g_@@_??_code_prop,
% \@@~??,
% \g_@@_??_reversed_tl,
% \c_@@_??_parameter_tl,
% }
%
% Initially these variables simply used an empty ``label'' name (not
% two question marks). This was a bit unfortunate, because then
% \texttt{l3doc} complains about \verb=__= in the middle of a
% command name when trying to typeset the documentation. However
% using a ``normal'' name such as \texttt{default} has the
% disadvantage of that being not really distinguishable from a real
% hook name. I now have settled for \texttt{??} which needs some
% gymnastics to get it into the csname, but since this is used a
% lot, the code should be fast, so this is not done with \texttt{c}
% expansion in the code later on.
%
% \cs{@@\textvisiblespace??} isn't used, but it has to be defined to
% trick the code into thinking that \texttt{??} is actually a hook.
% \begin{macrocode}
\prop_new:c { g_@@_??_code_prop }
\prop_new:c { @@~?? }
% \end{macrocode}
%
% Default rules are always given in normal ordering (never in
% reversed ordering). If such a rule is applied to a reversed
% hook it behaves as if the rule is reversed (e.g.,
% \texttt{after} becomes \texttt{before})
% because those rules are applied first and then the order is reversed.
% \begin{macrocode}
\tl_new:c { g_@@_??_reversed_tl }
% \end{macrocode}
%
% The parameter text for the \enquote{default} hook is empty.
% \changes{v1.1a}{2023/04/06}
% {Token list added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\c_@@_??_parameter_tl}
%<latexrelease> {Hooks~with~args}
\tl_const:cn { c_@@_??_parameter_tl } { }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\c_@@_??_parameter_tl}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:c { c_@@_??_parameter_tl }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hook_gset_rule:nnnn}
% \begin{macro}{\@@_gset_rule:nnnn}
% With
% \cs{hook_gset_rule:nnnn}\Arg{hook}\Arg{label1}\Arg{relation}\Arg{label2}
% a relation is defined between the two code labels for the given
% \meta{hook}. The special hook \texttt{??} stands for \emph{any}
% hook, which sets a default rule (to be used if no other relation
% between the two hooks exist).
% \begin{macrocode}
\cs_new_protected:Npn \hook_gset_rule:nnnn #1#2#3#4
{
\@@_normalize_hook_rule_args:Nnnnn \@@_gset_rule:nnnn
{#1} {#2} {#3} {#4}
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2022/06/01}{\@@_gset_rule:nnnn}
%<latexrelease> {Refuse~setting~rule~for~one-time~hooks}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_gset_rule:nnnn #1#2#3#4
{
\@@_if_deprecated_generic:nT {#1}
{
\@@_deprecated_generic_warn:n {#1}
\@@_do_deprecated_generic:Nn \@@_gset_rule:nnnn {#1}
{#2} {#3} {#4}
\@@_use_none_delimit_by_s_mark:w
}
\@@_if_execute_immediately:nT {#1}
{
\msg_error:nnnnnn { hooks } { rule-too-late }
{#1} {#2} {#3} {#4}
\@@_use_none_delimit_by_s_mark:w
}
% \end{macrocode}
% First we ensure the basic data structure of the hook exists:
% \begin{macrocode}
\@@_init_structure:n {#1}
% \end{macrocode}
% Then we clear any previous relationship between both labels.
% \begin{macrocode}
\@@_rule_gclear:nnn {#1} {#2} {#4}
% \end{macrocode}
% Then we call the function to handle the given rule. Throw an error if the
% rule is invalid.
% \begin{macrocode}
\cs_if_exist_use:cTF { @@_rule_#3_gset:nnn }
{
{#1} {#2} {#4}
\@@_update_hook_code:n {#1}
}
{
\msg_error:nnnnnn { hooks } { unknown-rule }
{#1} {#2} {#3} {#4}
}
\s_@@_mark
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_gset_rule:nnnn}
%<latexrelease> {Refuse~setting~rule~for~one-time~hooks}
%<latexrelease>\cs_new_protected:Npn \@@_gset_rule:nnnn #1#2#3#4
%<latexrelease> {
%<latexrelease> \@@_if_deprecated_generic:nT {#1}
%<latexrelease> {
%<latexrelease> \@@_deprecated_generic_warn:n {#1}
%<latexrelease> \@@_do_deprecated_generic:Nn \@@_gset_rule:nnnn {#1}
%<latexrelease> {#2} {#3} {#4}
%<latexrelease> \exp_after:wN \use_none:nnnnnnnnn \use_none:n
%<latexrelease> }
%<latexrelease> \@@_init_structure:n {#1}
%<latexrelease> \@@_rule_gclear:nnn {#1} {#2} {#4}
%<latexrelease> \cs_if_exist_use:cTF { @@_rule_#3_gset:nnn }
%<latexrelease> {
%<latexrelease> {#1} {#2} {#4}
%<latexrelease> \@@_update_hook_code:n {#1}
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \msg_error:nnnnnn { hooks } { unknown-rule }
%<latexrelease> {#1} {#2} {#3} {#4}
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_rule_before_gset:nnn, \@@_rule_after_gset:nnn,
% \@@_rule_<_gset:nnn, \@@_rule_>_gset:nnn}
% Then we add the new rule. We need to normalize the rules here to
% allow for faster processing later. Given a pair of labels
% $l_A$ and $l_B$, the rule $l_A>l_B$ is the same as $l_B<l_A$
% only presented differently. But by normalizing the
% forms of the rule to a single representation, say, $l_B<l_A$, reduces
% the time spent looking for the rules later considerably.
%
% Here we do that normalization by using \cs[no-index]{(pdf)strcmp} to
% lexically sort labels $l_A$ and $l_B$ to a fixed order. This order
% is then enforced every time these two labels are used together.
%
% Here we use \cs{@@_label_pair:nn}~\Arg{hook}~\Arg{l_A}~\Arg{l_B}
% to build a string \texttt{$l_B$\string|$l_A$} with a fixed order, and
% use \cs{@@_label_ordered:nnTF} to apply the correct rule to the pair
% of labels, depending if it was sorted or not.
% \begin{macrocode}
\cs_new_protected:Npn \@@_rule_before_gset:nnn #1#2#3
{
\@@_tl_gset:cx { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
{ \@@_label_ordered:nnTF {#2} {#3} { < } { > } }
}
\cs_new_eq:cN { @@_rule_<_gset:nnn } \@@_rule_before_gset:nnn
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_rule_after_gset:nnn #1#2#3
{
\@@_tl_gset:cx { g_@@_#1_rule_ \@@_label_pair:nn {#3} {#2} _tl }
{ \@@_label_ordered:nnTF {#3} {#2} { < } { > } }
}
\cs_new_eq:cN { @@_rule_>_gset:nnn } \@@_rule_after_gset:nnn
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_rule_voids_gset:nnn}
% This rule removes (clears, actually) the code from label |#3| if
% label |#2| is in the hook |#1|.
% \begin{macrocode}
\cs_new_protected:Npn \@@_rule_voids_gset:nnn #1#2#3
{
\@@_tl_gset:cx { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
{ \@@_label_ordered:nnTF {#2} {#3} { -> } { <- } }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \@@_rule_incompatible-error_gset:nnn,
% \@@_rule_incompatible-warning_gset:nnn,
% }
% These relations make an error/warning if labels |#2| and |#3| appear
% together in hook |#1|.
% \begin{macrocode}
\cs_new_protected:cpn { @@_rule_incompatible-error_gset:nnn } #1#2#3
{ \@@_tl_gset:cn { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
{ xE } }
\cs_new_protected:cpn { @@_rule_incompatible-warning_gset:nnn } #1#2#3
{ \@@_tl_gset:cn { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl }
{ xW } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_rule_unrelated_gset:nnn, \@@_rule_gclear:nnn}
% Undo a setting. \cs{@@_rule_unrelated_gset:nnn} doesn't need to do anything,
% since we use \cs{@@_rule_gclear:nnn} before setting any rule.
% \begin{macrocode}
\cs_new_protected:Npn \@@_rule_unrelated_gset:nnn #1#2#3 { }
\cs_new_protected:Npn \@@_rule_gclear:nnn #1#2#3
{ \cs_undefine:c { g_@@_#1_rule_ \@@_label_pair:nn {#2} {#3} _tl } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_label_pair:nn}
% Ensure that the lexically greater label comes first.
% \begin{macrocode}
\cs_new:Npn \@@_label_pair:nn #1#2
{
\if_case:w \@@_str_compare:nn {#1} {#2} \exp_stop_f:
#1 | #1 % 0
\or: #1 | #2 % +1
\else: #2 | #1 % -1
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_label_ordered:nn}
% Check that labels |#1| and |#2| are in the correct order (as
% returned by \cs{@@_label_pair:nn}) and if so return true, else
% return false.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_label_ordered:nn #1#2 { TF }
{
\if_int_compare:w \@@_str_compare:nn {#1} {#2} > 0 \exp_stop_f:
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_label_case:nnnnn}
% To avoid doing the string comparison twice in \cs{@@_initialize_single:NNn}
% (once with \cs{str_if_eq:nn} and again with \cs{@@_label_ordered:nn}),
% we use a three-way branching macro that will compare |#1| and |#2|
% and expand to \cs{use_i:nnn} if they are equal, \cs{use_ii:nn} if
% |#1| is lexically greater, and \cs{use_iii:nn} otherwise.
% \begin{macrocode}
\cs_new:Npn \@@_if_label_case:nnnnn #1#2
{
\cs:w use_
\if_case:w \@@_str_compare:nn {#1} {#2}
i \or: ii \else: iii \fi: :nnn
\cs_end:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_update_hook_code:n}
% Before \verb=\begin{document}= this does nothing, in the body it
% reinitializes the hook code using the altered data.
% \begin{macrocode}
\cs_new_eq:NN \@@_update_hook_code:n \use_none:n
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_initialize_all:}
% Initialize all known hooks (at \verb=\begin{document}=), i.e.,
% update the fast execution token lists to hold the necessary code
% in the right order.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_all:}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_initialize_all:
{
% \end{macrocode}
% First we change \cs{@@_update_hook_code:n} which so far was a
% no-op to now initialize one hook. This way any later updates to
% the hook will run that code and also update the execution token
% list.
% \begin{macrocode}
\cs_gset_eq:NN \@@_update_hook_code:n \@@_initialize_hook_code:n
% \end{macrocode}
% Now we loop over all hooks that have been defined and update each
% of them. Here we have to determine if the hook has arguments so
% that auxiliaries know what to do with hashes. We look at
% \cs[no-index]{c_@@_\meta{hook}_parameter_tl}, if it has any
% parameters, and set \verb|replacing_args| accordingly.
% \begin{macrocode}
\@@_debug:n { \prop_gclear:N \g_@@_used_prop }
\seq_map_inline:Nn \g_@@_all_seq
{
\tl_if_empty:cTF { c_@@_##1_parameter_tl }
{ \@@_replacing_args_false: }
{ \@@_replacing_args_true: }
\@@_update_hook_code:n {##1}
\@@_replacing_args_reset:
}
% \end{macrocode}
% If we are debugging we show results hook by hook for all hooks
% that have data.
% \begin{macrocode}
\@@_debug:n
{
\iow_term:x { ^^J All~initialized~(non-empty)~hooks: }
\prop_map_inline:Nn \g_@@_used_prop
{
\iow_term:x
{ ^^J ~ ##1 ~ -> ~ \cs_replacement_spec:c { @@~##1 } ~ }
}
}
% \end{macrocode}
% After all hooks are initialized we change the ``use'' to just
% call the hook code and not initialize it (as it was done in the
% preamble.
% \begin{macrocode}
\@@_post_initialization_defs:
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_all:}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_initialize_all:
%<latexrelease> {
%<latexrelease> \cs_gset_eq:NN \@@_update_hook_code:n \@@_initialize_hook_code:n
%<latexrelease> \@@_debug:n { \prop_gclear:N \g_@@_used_prop }
%<latexrelease> \seq_map_inline:Nn \g_@@_all_seq
%<latexrelease> { \@@_update_hook_code:n {##1} }
%<latexrelease> \@@_debug:n
%<latexrelease> {
%<latexrelease> \iow_term:x{^^JAll~ initialized~ (non-empty)~ hooks:}
%<latexrelease> \prop_map_inline:Nn \g_@@_used_prop
%<latexrelease> {
%<latexrelease> \iow_term:x
%<latexrelease> { ^^J ~ ##1 ~ -> ~ \cs_replacement_spec:c { @@~##1 } ~ }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \cs_gset_eq:NN \hook_use:n \@@_use_initialized:n
%<latexrelease> \cs_gset_eq:NN \@@_preamble_hook:n \use_none:n
%<latexrelease> }
%<@@=>
%<latexrelease>\cs_gset_eq:NN \@expl@@@initialize@all@@
%<latexrelease> \__hook_initialize_all:
%<@@=hook>
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_initialize_hook_code:n}
% Initializing or reinitializing the fast execution hook code. In
% the preamble this is selectively done in case a hook gets used
% and at \verb=\begin{document}= this is done for all hooks and
% afterwards only if the hook code changes.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_hook_code:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_initialize_hook_code:n #1
{
\@@_debug:n
{ \iow_term:x { ^^J Update~code~for~hook~'#1' \on@line :^^J } }
% \end{macrocode}
% This does the sorting and the updates.
% First thing we do is to check if a legacy hook macro exists and
% if so we add it to the hook under the label \texttt{legacy}. This
% might make the hook non-empty so we have to do this before
% the then following test.
% \begin{macrocode}
\@@_include_legacy_code_chunk:n {#1}
% \end{macrocode}
% If there aren't any code
% chunks for the current hook, there is no point in even starting
% the sorting routine so we make a quick test for that and in that
% case just update \cs{@@\textvisiblespace\meta{hook}} to hold the |top-level| and
% |next| code chunks. If there are code chunks we call
% \cs{@@_initialize_single:NNn} and pass to it ready made csnames
% as they are needed several times inside. This way we save a bit
% on processing time if we do that up front.
% \changes{v1.0u}{2022/05/17}{Refuse sorting one-time hooks (gh/818).}
% \begin{macrocode}
\@@_if_usable:nT {#1}
{
\prop_if_empty:cTF { g_@@_#1_code_prop }
{
\@@_code_gset:ne {#1}
{
% \end{macrocode}
% The hook may take arguments, so we add a run of braced parameters
% after the \verb|_next| and \verb|_toplevel| macros, so that the
% arguments passed to the hook are forwarded to them.
% \begin{macrocode}
\exp_not:c { @@_toplevel~#1 } \@@_braced_parameter:n {#1}
\exp_not:c { @@_next~#1 } \@@_braced_parameter:n {#1}
}
}
{
% \end{macrocode}
% By default the algorithm sorts the code chunks and then saves the
% result in a token list for fast execution; this is done by adding the code chunks
% one after another, using \cs{tl_gput_right:NV}. When we sort code for
% a reversed hook, all we have to do is to add the code chunks in
% the opposite order into the token list. So all we have to do
% in preparation is to change two definitions that are used later on.
% \begin{macrocode}
\@@_if_reversed:nTF {#1}
{ \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_left:Nn
\cs_set_eq:NN \@@_clist_gput:NV \clist_gput_left:NV }
{ \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_right:Nn
\cs_set_eq:NN \@@_clist_gput:NV \clist_gput_right:NV }
% \end{macrocode}
%
% When sorting, some relations (namely \verb|voids|) need to
% act destructively on the code property lists to remove code that
% shouldn't appear in the sorted hook token list, so we make a copy
% of the code property list that we can safely work on without
% changing the main one.
% \begin{macrocode}
\prop_set_eq:Nc \l_@@_work_prop { g_@@_#1_code_prop }
\@@_initialize_single:ccn
{ @@~#1 } { g_@@_#1_labels_clist } {#1}
% \end{macrocode}
% For debug display we want to keep track of those hooks that
% actually got code added to them, so we record that in plist. We
% use a plist to ensure that we record each hook name only once,
% i.e., we are only interested in storing the keys and the value is
% arbitrary.
% \begin{macrocode}
\@@_debug:n
{ \exp_args:NNx \prop_gput:Nnn \g_@@_used_prop {#1} { } }
}
}
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_hook_code:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_initialize_hook_code:n #1
%<latexrelease> {
%<latexrelease> \@@_debug:n
%<latexrelease> { \iow_term:x { ^^J Update~code~for~hook~'#1' \on@line :^^J } }
%<latexrelease> \@@_include_legacy_code_chunk:n {#1}
%<latexrelease> \@@_if_usable:nT {#1}
%<latexrelease> {
%<latexrelease> \prop_if_empty:cTF { g_@@_#1_code_prop }
%<latexrelease> {
%<latexrelease> \@@_tl_gset:co { @@~#1 }
%<latexrelease> {
%<latexrelease> \cs:w @@_toplevel~#1 \exp_after:wN \cs_end:
%<latexrelease> \cs:w @@_next~#1 \cs_end:
%<latexrelease> }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \@@_if_reversed:nTF {#1}
%<latexrelease> { \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_left:Nn
%<latexrelease> \cs_set_eq:NN \@@_clist_gput:NV \clist_gput_left:NV }
%<latexrelease> { \cs_set_eq:NN \@@_tl_gput:Nn \@@_tl_gput_right:Nn
%<latexrelease> \cs_set_eq:NN \@@_clist_gput:NV \clist_gput_right:NV }
%<latexrelease> \prop_set_eq:Nc \l_@@_work_prop { g_@@_#1_code_prop }
%<latexrelease> \@@_initialize_single:ccn
%<latexrelease> { @@~#1 } { g_@@_#1_labels_clist } {#1}
%<latexrelease> \@@_debug:n
%<latexrelease> { \exp_args:NNx \prop_gput:Nnn \g_@@_used_prop {#1} { } }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[EXP]{\@@_tl_csname:n,\@@_seq_csname:n}
% It is faster to pass a single token and expand it when necessary
% than to pass a bunch of character tokens around.
% \fmiinline{note to myself: verify}
% \begin{macrocode}
\cs_new:Npn \@@_tl_csname:n #1 { l_@@_label_#1_tl }
\cs_new:Npn \@@_seq_csname:n #1 { l_@@_label_#1_seq }
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\l_@@_labels_seq,\l_@@_labels_int,\l_@@_front_tl,
% \l_@@_rear_tl,\l_@@_label_0_tl}
%
% For the sorting I am basically implementing Knuth's algorithm for
% topological sorting as given in TAOCP volume 1 pages 263--266.
% For this algorithm we need a number of local variables:
% \begin{itemize}
% \item
% List of labels used in the current hook to label code chunks:
% \begin{macrocode}
\seq_new:N \l_@@_labels_seq
% \end{macrocode}
% \item
% Number of labels used in the current hook. In Knuth's algorithm
% this is called $N$:
% \begin{macrocode}
\int_new:N \l_@@_labels_int
% \end{macrocode}
% \item
% The sorted code list to be build is managed using two pointers
% one to the front of the queue and one to the rear. We model this
% using token list pointers. Knuth calls them $F$ and $R$:
% \begin{macrocode}
\tl_new:N \l_@@_front_tl
\tl_new:N \l_@@_rear_tl
% \end{macrocode}
% \item
% The data for the start of the queue is kept in this token list,
% it corresponds to what Don calls \texttt{QLINK[0]} but since we
% aren't manipulating individual words in memory it is slightly
% differently done:
% \begin{macrocode}
\tl_new:c { \@@_tl_csname:n { 0 } }
% \end{macrocode}
%
% \end{itemize}
% \end{macro}
%
%
% \begin{macro}{\@@_initialize_single:NNn,\@@_initialize_single:ccn}
%
% \cs{@@_initialize_single:NNn} implements the sorting of the code
% chunks for a hook and saves the result in the token list for fast
% execution (\verb=#4=). The arguments are \meta{hook-code-plist},
% \meta{hook-code-tl}, \meta{hook-top-level-code-tl},
% \meta{hook-next-code-tl},
% \meta{hook-ordered-labels-clist} and \meta{hook-name} (the latter
% is only used for debugging---the \meta{hook-rule-plist} is accessed
% using the \meta{hook-name}).
%
% The additional complexity compared to Don's algorithm is that we
% do not use simple positive integers but have arbitrary
% alphanumeric labels. As usual Don's data structures are chosen in
% a way that one can omit a lot of tests and I have mimicked that as
% far as possible. The result is a restriction I do not test for at
% the moment: a label can't be equal to the number 0!
% \fmiinline{Needs checking for, just in case ... maybe}
%
% ^^A #1 <- \@@~#1
% ^^A #2 <- \g_@@_#1_labels_clist
% ^^A #3 <- #1
%
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_initialize_single:NNn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_initialize_single:NNn #1#2#3
{
% \end{macrocode}
% Step T1: Initialize the data structure \ldots
% \begin{macrocode}
\seq_clear:N \l_@@_labels_seq
\int_zero:N \l_@@_labels_int
% \end{macrocode}
%
% Store the name of the hook:
% \begin{macrocode}
\tl_set:Nn \l_@@_cur_hook_tl {#3}
% \end{macrocode}
%
% We loop over the property list holding the code and record all
% the labels listed there. Only the rules for those labels are of interest
% to us. While we are at it we count them (which gives us the $N$
% in Knuth's algorithm). The prefix |label_| is added to the variables
% to ensure that labels named |front|, |rear|, |labels|, or |return|
% don't interact with our code.
% \begin{macrocode}
\prop_map_inline:Nn \l_@@_work_prop
{
\int_incr:N \l_@@_labels_int
\seq_put_right:Nn \l_@@_labels_seq {##1}
\@@_tl_set:cn { \@@_tl_csname:n {##1} } { 0 }
\seq_clear_new:c { \@@_seq_csname:n {##1} }
}
% \end{macrocode}
% Steps T2 and T3: Here we sort the relevant rules into the data structure\ldots
%
% This loop constitutes a square matrix of the labels in
% \cs{l_@@_work_prop} in the
% vertical and the horizontal directions. However, since the rule
% $l_A\meta{rel}l_B$ is the same as $l_B\meta{rel}^{-1}l_A$ we can cut
% the loop short at the diagonal of the matrix (\emph{i.e.}, when
% both labels are equal), saving a good amount of time. The way the
% rules were set up (see the implementation of \cs{@@_rule_before_gset:nnn}
% above) ensures that we have no rule in the ignored side of the
% matrix, and all rules are seen. The rules are applied in
% \cs{@@_apply_label_pair:nnn}, which takes the properly-ordered pair
% of labels as argument.
% \begin{macrocode}
\prop_map_inline:Nn \l_@@_work_prop
{
\prop_map_inline:Nn \l_@@_work_prop
{
\@@_if_label_case:nnnnn {##1} {####1}
{ \prop_map_break: }
{ \@@_apply_label_pair:nnn {##1} {####1} }
{ \@@_apply_label_pair:nnn {####1} {##1} }
{#3}
}
}
% \end{macrocode}
% Now take a breath, and look at the data structures that have
% been set up:
% \begin{macrocode}
\@@_debug:n { \@@_debug_label_data:N \l_@@_work_prop }
% \end{macrocode}
%
%
% Step T4:
% \begin{macrocode}
\tl_set:Nn \l_@@_rear_tl { 0 }
\tl_set:cn { \@@_tl_csname:n { 0 } } { 0 }
\seq_map_inline:Nn \l_@@_labels_seq
{
\int_compare:nNnT { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
{
\tl_set:cn { \@@_tl_csname:n { \l_@@_rear_tl } }{##1}
\tl_set:Nn \l_@@_rear_tl {##1}
}
}
\tl_set_eq:Nc \l_@@_front_tl { \@@_tl_csname:n { 0 } }
% \end{macrocode}
%
% \begin{macrocode}
\@@_tl_gclear:N #1
\clist_gclear:N #2
% \end{macrocode}
%
% The whole loop gets combined in steps T5--T7:
% \begin{macrocode}
\bool_while_do:nn { ! \str_if_eq_p:Vn \l_@@_front_tl { 0 } }
{
% \end{macrocode}
% This part is step T5:
% \begin{macrocode}
\int_decr:N \l_@@_labels_int
\prop_get:NVN \l_@@_work_prop \l_@@_front_tl \l_@@_return_tl
\exp_args:NNV \@@_tl_gput:Nn #1 \l_@@_return_tl
% \end{macrocode}
%
% \begin{macrocode}
\@@_clist_gput:NV #2 \l_@@_front_tl
\@@_debug:n{ \iow_term:x{Handled~ code~ for~ \l_@@_front_tl} }
% \end{macrocode}
%
% This is step T6, except that we don't use a pointer $P$ to move
% through the successors, but instead use \verb=##1= of the mapping
% function.
% \begin{macrocode}
\seq_map_inline:cn { \@@_seq_csname:n { \l_@@_front_tl } }
{
\tl_set:cx { \@@_tl_csname:n {##1} }
{ \int_eval:n
{ \cs:w \@@_tl_csname:n {##1} \cs_end: - 1 }
}
\int_compare:nNnT
{ \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
{
\tl_set:cn { \@@_tl_csname:n { \l_@@_rear_tl } } {##1}
\tl_set:Nn \l_@@_rear_tl {##1}
}
}
% \end{macrocode}
% and here is step T7:
% \begin{macrocode}
\tl_set_eq:Nc \l_@@_front_tl
{ \@@_tl_csname:n { \l_@@_front_tl } }
% \end{macrocode}
%
% This is step T8: If we haven't moved the code for all labels
% (i.e., if \cs{l_@@_labels_int} is still greater than zero) we
% have a loop and our partial order can't be flattened out.
% \begin{macrocode}
}
\int_compare:nNnF \l_@@_labels_int = 0
{
\iow_term:x{====================}
\iow_term:x{Error:~ label~ rules~ are~ incompatible:}
% \end{macrocode}
%
% This is not really the information one needs in the error case
% but it will do for now \ldots \fmiinline{improve output on a rainy day}
% \begin{macrocode}
\@@_debug_label_data:N \l_@@_work_prop
\iow_term:x{====================}
}
% \end{macrocode}
% After we have added all hook code to \verb=#1=, we finish it off
% by adding extra code for the |top-level| (\verb=#2=) and for one
% time execution (\verb=#3=). These should normally be empty. The
% |top-level| code is added with \cs{@@_tl_gput:Nn} as that might
% change for a reversed hook (then |top-level| is the very first code
% chunk added). The |next| code is always added last (to the right).
% The hook may take arguments, so we add a run of braced parameters
% after the \verb|_next| and \verb|_toplevel| macros, so that the
% arguments passed to the hook are forwarded to them.
% \begin{macrocode}
\exp_args:NNe \@@_tl_gput:Nn #1
{ \exp_not:c { @@_toplevel~#3 } \@@_braced_parameter:n {#3} }
\@@_tl_gput_right:Ne #1
{ \exp_not:c { @@_next~#3 } \@@_braced_parameter:n {#3} }
\use:e
{
\cs_gset:cpn { @@~#3 } \use:c { c_@@_#3_parameter_tl }
{ \exp_not:V #1 }
}
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_generate_variant:Nn \@@_initialize_single:NNn { cc }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_initialize_single:NNn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \@@_initialize_single:NNn #1#2#3
%<latexrelease> {
%<latexrelease> \seq_clear:N \l_@@_labels_seq
%<latexrelease> \int_zero:N \l_@@_labels_int
%<latexrelease> \tl_set:Nn \l_@@_cur_hook_tl {#3}
%<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
%<latexrelease> {
%<latexrelease> \int_incr:N \l_@@_labels_int
%<latexrelease> \seq_put_right:Nn \l_@@_labels_seq {##1}
%<latexrelease> \@@_tl_set:cn { \@@_tl_csname:n {##1} } { 0 }
%<latexrelease> \seq_clear_new:c { \@@_seq_csname:n {##1} }
%<latexrelease> }
%<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
%<latexrelease> {
%<latexrelease> \prop_map_inline:Nn \l_@@_work_prop
%<latexrelease> {
%<latexrelease> \@@_if_label_case:nnnnn {##1} {####1}
%<latexrelease> { \prop_map_break: }
%<latexrelease> { \@@_apply_label_pair:nnn {##1} {####1} }
%<latexrelease> { \@@_apply_label_pair:nnn {####1} {##1} }
%<latexrelease> {#3}
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \@@_debug:n { \@@_debug_label_data:N \l_@@_work_prop }
%<latexrelease> \tl_set:Nn \l_@@_rear_tl { 0 }
%<latexrelease> \tl_set:cn { \@@_tl_csname:n { 0 } } { 0 }
%<latexrelease> \seq_map_inline:Nn \l_@@_labels_seq
%<latexrelease> {
%<latexrelease> \int_compare:nNnT { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
%<latexrelease> {
%<latexrelease> \tl_set:cn { \@@_tl_csname:n { \l_@@_rear_tl } }{##1}
%<latexrelease> \tl_set:Nn \l_@@_rear_tl {##1}
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \tl_set_eq:Nc \l_@@_front_tl { \@@_tl_csname:n { 0 } }
%<latexrelease> \@@_tl_gclear:N #1
%<latexrelease> \clist_gclear:N #2
%<latexrelease> \bool_while_do:nn { ! \str_if_eq_p:Vn \l_@@_front_tl { 0 } }
%<latexrelease> {
%<latexrelease> \int_decr:N \l_@@_labels_int
%<latexrelease> \prop_get:NVN \l_@@_work_prop \l_@@_front_tl \l_@@_return_tl
%<latexrelease> \exp_args:NNV \@@_tl_gput:Nn #1 \l_@@_return_tl
%<latexrelease> \@@_clist_gput:NV #2 \l_@@_front_tl
%<latexrelease> \@@_debug:n{ \iow_term:x{Handled~ code~ for~ \l_@@_front_tl} }
%<latexrelease> \seq_map_inline:cn { \@@_seq_csname:n { \l_@@_front_tl } }
%<latexrelease> {
%<latexrelease> \tl_set:cx { \@@_tl_csname:n {##1} }
%<latexrelease> { \int_eval:n
%<latexrelease> { \cs:w \@@_tl_csname:n {##1} \cs_end: - 1 }
%<latexrelease> }
%<latexrelease> \int_compare:nNnT
%<latexrelease> { \cs:w \@@_tl_csname:n {##1} \cs_end: } = 0
%<latexrelease> {
%<latexrelease> \tl_set:cn { \@@_tl_csname:n { \l_@@_rear_tl } } {##1}
%<latexrelease> \tl_set:Nn \l_@@_rear_tl {##1}
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \tl_set_eq:Nc \l_@@_front_tl
%<latexrelease> { \@@_tl_csname:n { \l_@@_front_tl } }
%<latexrelease> }
%<latexrelease> \int_compare:nNnF \l_@@_labels_int = 0
%<latexrelease> {
%<latexrelease> \iow_term:x{====================}
%<latexrelease> \iow_term:x{Error:~ label~ rules~ are~ incompatible:}
%<latexrelease> \@@_debug_label_data:N \l_@@_work_prop
%<latexrelease> \iow_term:x{====================}
%<latexrelease> }
%<latexrelease> \exp_args:NNo \@@_tl_gput:Nn #1 { \cs:w @@_toplevel~#3 \cs_end: }
%<latexrelease> \@@_tl_gput_right:No #1 { \cs:w @@_next~#3 \cs_end: }
%<latexrelease> }
%<latexrelease>\cs_generate_variant:Nn \@@_tl_gput_right:Nn { No }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@@_tl_gput:Nn,\@@_clist_gput:NV}
% These append either on the right (normal hook) or on the left
% (reversed hook). This is setup up in
% \cs{@@_initialize_hook_code:n}, elsewhere their behavior is undefined.
% \begin{macrocode}
\cs_new:Npn \@@_tl_gput:Nn { \ERROR }
\cs_new:Npn \@@_clist_gput:NV { \ERROR }
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@@_apply_label_pair:nnn,\@@_label_if_exist_apply:nnnF}
%
% This is the payload of steps T2 and T3 executed in the loop described
% above. This macro assumes |#1| and |#2| are ordered, which means that
% any rule pertaining the pair |#1| and |#2| is
% \cs{g_@@_\meta{hook}_rule_\#1\string|\#2_tl}, and not
% \cs{g_@@_\meta{hook}_rule_\#2\string|\#1_tl}. This also saves a great deal
% of time since we only need to check the order of the labels once.
%
% The arguments here are \meta{label1}, \meta{label2}, \meta{hook}, and
% \meta{hook-code-plist}. We are about to apply the next rule and
% enter it into the data structure. \cs{@@_apply_label_pair:nnn} will
% just call \cs{@@_label_if_exist_apply:nnnF} for the \meta{hook}, and
% if no rule is found, also try the \meta{hook} name \texttt{??}
% denoting a default hook rule.
%
% \cs{@@_label_if_exist_apply:nnnF} will check if the rule exists for
% the given hook, and if so call \cs{@@_apply_rule:nnn}.
% \begin{macrocode}
\cs_new_protected:Npn \@@_apply_label_pair:nnn #1#2#3
{
% \end{macrocode}
% Extra complication: as we use default rules and local hook specific
% rules we first have to check if there is a local rule and if that
% exist use it. Otherwise check if there is a default rule and use
% that.
% \begin{macrocode}
\@@_label_if_exist_apply:nnnF {#1} {#2} {#3}
{
% \end{macrocode}
% If there is no hook-specific rule we check for a default one and
% use that if it exists.
% \begin{macrocode}
\@@_label_if_exist_apply:nnnF {#1} {#2} { ?? } { }
}
}
\cs_new_protected:Npn \@@_label_if_exist_apply:nnnF #1#2#3
{
\if_cs_exist:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end:
% \end{macrocode}
% What to do precisely depends on the type of rule we have
% encountered. If it is a \texttt{before} rule it will be handled by the
% algorithm but other types need to be managed differently. All
% this is done in \cs{@@_apply_rule:nnnN}.
% \begin{macrocode}
\@@_apply_rule:nnn {#1} {#2} {#3}
\exp_after:wN \use_none:n
\else:
\use:nn
\fi:
}
% \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\@@_apply_rule:nnn}
% This is the code executed in steps T2 and T3 while looping through
% the matrix This is part of step T3. We are about to apply the next
% rule and enter it into the data structure. The arguments are
% \meta{label1}, \meta{label2}, \meta{hook-name}, and \meta{hook-code-plist}.
% \begin{macrocode}
\cs_new_protected:Npn \@@_apply_rule:nnn #1#2#3
{
\cs:w @@_apply_
\cs:w g_@@_#3_reversed_tl \cs_end: rule_
\cs:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end: :nnn \cs_end:
{#1} {#2} {#3}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_apply_rule_<:nnn,\@@_apply_rule_>:nnn}
% The most common cases are \texttt{\string<} and \texttt{\string>} so we handle
% that first. They are relations $\prec$ and $\succ$ in TAOCP, and
% they dictate sorting.
% \begin{macrocode}
\cs_new_protected:cpn { @@_apply_rule_<:nnn } #1#2#3
{
\@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
\tl_set:cx { \@@_tl_csname:n {#2} }
{ \int_eval:n{ \cs:w \@@_tl_csname:n {#2} \cs_end: + 1 } }
\seq_put_right:cn{ \@@_seq_csname:n {#1} }{#2}
}
\cs_new_protected:cpn { @@_apply_rule_>:nnn } #1#2#3
{
\@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
\tl_set:cx { \@@_tl_csname:n {#1} }
{ \int_eval:n{ \cs:w \@@_tl_csname:n {#1} \cs_end: + 1 } }
\seq_put_right:cn{ \@@_seq_csname:n {#2} }{#1}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_apply_rule_xE:nnn,\@@_apply_rule_xW:nnn}
% These relations make two labels incompatible within a hook.
% |xE| makes raises an error if the labels are found in the same
% hook, and |xW| makes it a warning.
% \begin{macrocode}
\cs_new_protected:cpn { @@_apply_rule_xE:nnn } #1#2#3
{
\@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
\msg_error:nnnnnn { hooks } { labels-incompatible }
{#1} {#2} {#3} { 1 }
\use:c { @@_apply_rule_->:nnn } {#1} {#2} {#3}
\use:c { @@_apply_rule_<-:nnn } {#1} {#2} {#3}
}
\cs_new_protected:cpn { @@_apply_rule_xW:nnn } #1#2#3
{
\@@_debug:n { \@@_msg_pair_found:nnn {#1} {#2} {#3} }
\msg_warning:nnnnnn { hooks } { labels-incompatible }
{#1} {#2} {#3} { 0 }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_apply_rule_->:nnn,\@@_apply_rule_<-:nnn}
% If we see \texttt{\detokenize{->}} we have to drop code for label
% \verb=#3= and carry on. We could do a little better and drop
% everything for that label since it doesn't matter where we put
% such empty code. However that would complicate the algorithm a
% lot with little gain.\footnote{This also has the advantage that
% the result of the sorting doesn't change, as it might otherwise do
% (for unrelated chunks) if we aren't careful.} So we still
% unnecessarily try to sort it in and depending on the rules that
% might result in a loop that is otherwise resolved. If that turns
% out to be a real issue, we can improve the code.
%
% Here the code is removed from \cs{l_@@_cur_hook_tl} rather than
% \verb=#3= because the latter may be \texttt{??}, and the default
% hook doesn't store any code. Removing it instead from \cs{l_@@_cur_hook_tl}
% makes the default rules \verb=->= and \verb=<-= work properly.
% \begin{macrocode}
\cs_new_protected:cpn { @@_apply_rule_->:nnn } #1#2#3
{
\@@_debug:n
{
\@@_msg_pair_found:nnn {#1} {#2} {#3}
\iow_term:x{--->~ Drop~ '#2'~ code~ from~
\iow_char:N \\ g_@@_ \l_@@_cur_hook_tl _code_prop ~
because~ of~ '#1' }
}
\prop_put:Nnn \l_@@_work_prop {#2} { }
}
\cs_new_protected:cpn { @@_apply_rule_<-:nnn } #1#2#3
{
\@@_debug:n
{
\@@_msg_pair_found:nnn {#1} {#2} {#3}
\iow_term:x{--->~ Drop~ '#1'~ code~ from~
\iow_char:N \\ g_@@_ \l_@@_cur_hook_tl _code_prop ~
because~ of~ '#2' }
}
\prop_put:Nnn \l_@@_work_prop {#1} { }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{
% \@@_apply_-rule_<:nnn,
% \@@_apply_-rule_>:nnn,
% \@@_apply_-rule_<-:nnn,
% \@@_apply_-rule_->:nnn,
% \@@_apply_-rule_xW:nnn,
% \@@_apply_-rule_xE:nnn,
% }
% Reversed rules.
% \begin{macrocode}
\cs_new_eq:cc { @@_apply_-rule_<:nnn } { @@_apply_rule_>:nnn }
\cs_new_eq:cc { @@_apply_-rule_>:nnn } { @@_apply_rule_<:nnn }
\cs_new_eq:cc { @@_apply_-rule_<-:nnn } { @@_apply_rule_<-:nnn }
\cs_new_eq:cc { @@_apply_-rule_->:nnn } { @@_apply_rule_->:nnn }
\cs_new_eq:cc { @@_apply_-rule_xE:nnn } { @@_apply_rule_xE:nnn }
\cs_new_eq:cc { @@_apply_-rule_xW:nnn } { @@_apply_rule_xW:nnn }
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_msg_pair_found:nnn}
% A macro to avoid moving this many tokens around.
% \begin{macrocode}
\cs_new_protected:Npn \@@_msg_pair_found:nnn #1#2#3
{
\iow_term:x{~ \str_if_eq:nnTF {#3} {??} {default} {~normal} ~
rule~ \@@_label_pair:nn {#1} {#2}:~
\use:c { g_@@_#3_rule_ \@@_label_pair:nn {#1} {#2} _tl } ~
found}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_debug_label_data:N}
%
% \begin{macrocode}
\cs_new_protected:Npn \@@_debug_label_data:N #1 {
\iow_term:x{Code~ labels~ for~ sorting:}
\iow_term:x{~ \seq_use:Nnnn\l_@@_labels_seq {~and~}{,~}{~and~} }
\iow_term:x{^^J Data~ structure~ for~ label~ rules:}
\prop_map_inline:Nn #1
{
\iow_term:x{~ ##1~ =~ \tl_use:c{ \@@_tl_csname:n {##1} }~ ->~
\seq_use:cnnn{ \@@_seq_csname:n {##1} }{~->~}{~->~}{~->~}
}
}
\iow_term:x{}
}
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\hook_show:n,\hook_log:n}
% \begin{macro}{\@@_log_line:x,\@@_log_line_indent:x}
% \begin{macro}{\@@_log:nN}
% This writes out information about the hook given in its argument
% onto the \texttt{.log} file and the terminal, if \cs{show_hook:n} is
% used. Internally both share the same structure, except that at the
% end, \cs{hook_show:n} triggers \TeX's prompt.
% \begin{macrocode}
\cs_new_protected:Npn \hook_log:n #1
{
\cs_set_eq:NN \@@_log_cmd:x \iow_log:x
\@@_normalize_hook_args:Nn \@@_log:nN {#1} \tl_log:x
}
\cs_new_protected:Npn \hook_show:n #1
{
\cs_set_eq:NN \@@_log_cmd:x \iow_term:x
\@@_normalize_hook_args:Nn \@@_log:nN {#1} \tl_show:x
}
\cs_new_protected:Npn \@@_log_line:x #1
{ \@@_log_cmd:x { >~#1 } }
\cs_new_protected:Npn \@@_log_line_indent:x #1
{ \@@_log_cmd:x { >~\@spaces #1 } }
% \end{macrocode}
%
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_log:nN}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_log:nN #1 #2
{
\@@_if_deprecated_generic:nT {#1}
{
\@@_deprecated_generic_warn:n {#1}
\@@_do_deprecated_generic:Nn \@@_log:nN {#1} #2
\exp_after:wN \use_none:nnnnnnnnn \use_none:nnnnn
}
\@@_preamble_hook:n {#1}
\@@_log_cmd:x
{
^^J ->~The~
\@@_if_generic:nT {#1} { generic~ }
hook~'#1'
\@@_if_disabled:nF {#1}
{
\exp_args:Nf \@@_print_args:nn {#1}
{
\int_eval:n
{ \str_count:e { \@@_parameter:n {#1} } / 3 }
}
}
:
}
% \end{macrocode}
%
% \begin{macrocode}
\@@_if_usable:nF {#1}
{ \@@_log_line:x { The~hook~is~not~declared. } }
\@@_if_disabled:nT {#1}
{ \@@_log_line:x { The~hook~is~disabled. } }
\hook_if_empty:nTF {#1}
{ #2 { The~hook~is~empty } }
{
\@@_log_line:x { Code~chunks: }
\prop_if_empty:cTF { g_@@_#1_code_prop }
{ \@@_log_line_indent:x { --- } }
{
\prop_map_inline:cn { g_@@_#1_code_prop }
{
\exp_after:wN \cs_set:Npn \exp_after:wN \@@_tmp:w
\c_@@_nine_parameters_tl {##2}
\@@_log_line_indent:x
{ ##1~->~\cs_replacement_spec:N \@@_tmp:w }
}
}
% \end{macrocode}
%
% If there is code in the |top-level| token list, print it:
% \begin{macrocode}
\@@_log_line:x
{
Document-level~(top-level)~code
\@@_if_usable:nT {#1}
{ ~(executed~\@@_if_reversed:nTF {#1} {first} {last} ) } :
}
\@@_log_line_indent:x
{
\@@_cs_if_empty:cTF { @@_toplevel~#1 }
{ --- }
{ -> ~ \cs_replacement_spec:c { @@_toplevel~#1 } }
}
% \end{macrocode}
%
% \begin{macrocode}
\@@_log_line:x { Extra~code~for~next~invocation: }
\@@_log_line_indent:x
{
\@@_cs_if_empty:cTF { @@_next~#1 }
{ --- }
% \end{macrocode}
%
% If the token list is not empty we want to display it but without
% the first tokens (the code to clear itself) so we call a helper
% command to get rid of them.
% \begin{macrocode}
{
-> ~ \exp_last_unbraced:Nf \@@_log_next_code:w
{ \cs_replacement_spec:c { @@_next~#1 } }
}
}
% \end{macrocode}
%
% Loop through the rules in a hook and for every rule found, print it.
% If no rule is there, print |---|. The boolean \cs{l_@@_tmpa_bool}
% here indicates if the hook has no rules.
% \begin{macrocode}
\@@_log_line:x { Rules: }
\bool_set_true:N \l_@@_tmpa_bool
\@@_list_rules:nn {#1}
{
\bool_set_false:N \l_@@_tmpa_bool
\@@_log_line_indent:x
{
##2~ with~
\str_if_eq:nnT {##3} {??} { default~ }
relation~ ##1
}
}
\bool_if:NT \l_@@_tmpa_bool
{ \@@_log_line_indent:x { --- } }
% \end{macrocode}
%
% When the hook is declared (that is, the sorting algorithm is applied
% to that hook) and not empty
% \begin{macrocode}
\bool_lazy_and:nnTF
{ \@@_if_usable_p:n {#1} }
{ ! \hook_if_empty_p:n {#1} }
{
\@@_log_line:x
{
Execution~order
\bool_if:NTF \l_@@_tmpa_bool
{ \@@_if_reversed:nT {#1} { ~(after~reversal) } }
{ ~(after~
\@@_if_reversed:nT {#1} { reversal~and~ }
applying~rules)
} :
}
#2 % \tl_show:n
{
\@spaces
\clist_if_empty:cTF { g_@@_#1_labels_clist }
{ --- }
{ \clist_use:cn { g_@@_#1_labels_clist } { ,~ } }
}
}
{
\@@_log_line:x { Execution~order: }
#2
{
\@spaces Not~set~because~the~hook~ \@@_if_usable:nTF {#1}
{ code~pool~is~empty }
{ is~\@@_if_disabled:nTF {#1} {disabled} {undeclared} }
}
}
}
}
%<latexrelease>\EndIncludeInRelease
%
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_log:nN}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \@@_log:nN #1 #2
%<latexrelease> {
%<latexrelease> \@@_if_deprecated_generic:nT {#1}
%<latexrelease> {
%<latexrelease> \@@_deprecated_generic_warn:n {#1}
%<latexrelease> \@@_do_deprecated_generic:Nn \@@_log:nN {#1} #2
%<latexrelease> \exp_after:wN \use_none:nnnnnnnnn \use_none:nnnnn
%<latexrelease> }
%<latexrelease> \@@_preamble_hook:n {#1}
%<latexrelease> \@@_log_cmd:x
%<latexrelease> { ^^J ->~The~ \@@_if_generic:nT {#1} { generic~ } hook~'#1': }
%<latexrelease> \@@_if_usable:nF {#1}
%<latexrelease> { \@@_log_line:x { The~hook~is~not~declared. } }
%<latexrelease> \@@_if_disabled:nT {#1}
%<latexrelease> { \@@_log_line:x { The~hook~is~disabled. } }
%<latexrelease> \hook_if_empty:nTF {#1}
%<latexrelease> { #2 { The~hook~is~empty } }
%<latexrelease> {
%<latexrelease> \@@_log_line:x { Code~chunks: }
%<latexrelease> \prop_if_empty:cTF { g_@@_#1_code_prop }
%<latexrelease> { \@@_log_line_indent:x { --- } }
%<latexrelease> {
%<latexrelease> \prop_map_inline:cn { g_@@_#1_code_prop }
%<latexrelease> { \@@_log_line_indent:x { ##1~->~\tl_to_str:n {##2} } }
%<latexrelease> }
%<latexrelease> \@@_log_line:x
%<latexrelease> {
%<latexrelease> Document-level~(top-level)~code
%<latexrelease> \@@_if_usable:nT {#1}
%<latexrelease> { ~(executed~\@@_if_reversed:nTF {#1} {first} {last} ) } :
%<latexrelease> }
%<latexrelease> \@@_log_line_indent:x
%<latexrelease> {
%<latexrelease> \tl_if_empty:cTF { @@_toplevel~#1 }
%<latexrelease> { --- }
%<latexrelease> { -> ~ \exp_args:Nv \tl_to_str:n { @@_toplevel~#1 } }
%<latexrelease> }
%<latexrelease> \@@_log_line:x { Extra~code~for~next~invocation: }
%<latexrelease> \@@_log_line_indent:x
%<latexrelease> {
%<latexrelease> \tl_if_empty:cTF { @@_next~#1 }
%<latexrelease> { --- }
%<latexrelease> { ->~ \exp_args:Nv \@@_log_next_code:n { @@_next~#1 } }
%<latexrelease> }
%<latexrelease> \@@_log_line:x { Rules: }
%<latexrelease> \bool_set_true:N \l_@@_tmpa_bool
%<latexrelease> \@@_list_rules:nn {#1}
%<latexrelease> {
%<latexrelease> \bool_set_false:N \l_@@_tmpa_bool
%<latexrelease> \@@_log_line_indent:x
%<latexrelease> {
%<latexrelease> ##2~ with~
%<latexrelease> \str_if_eq:nnT {##3} {??} { default~ }
%<latexrelease> relation~ ##1
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \bool_if:NT \l_@@_tmpa_bool
%<latexrelease> { \@@_log_line_indent:x { --- } }
%<latexrelease> \bool_lazy_and:nnTF
%<latexrelease> { \@@_if_usable_p:n {#1} }
%<latexrelease> { ! \hook_if_empty_p:n {#1} }
%<latexrelease> {
%<latexrelease> \@@_log_line:x
%<latexrelease> {
%<latexrelease> Execution~order
%<latexrelease> \bool_if:NTF \l_@@_tmpa_bool
%<latexrelease> { \@@_if_reversed:nT {#1} { ~(after~reversal) } }
%<latexrelease> { ~(after~
%<latexrelease> \@@_if_reversed:nT {#1} { reversal~and~ }
%<latexrelease> applying~rules)
%<latexrelease> } :
%<latexrelease> }
%<latexrelease> #2 % \tl_show:n
%<latexrelease> {
%<latexrelease> \@spaces
%<latexrelease> \clist_if_empty:cTF { g_@@_#1_labels_clist }
%<latexrelease> { --- }
%<latexrelease> { \clist_use:cn { g_@@_#1_labels_clist } { ,~ } }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> {
%<latexrelease> \@@_log_line:x { Execution~order: }
%<latexrelease> #2
%<latexrelease> {
%<latexrelease> \@spaces Not~set~because~the~hook~ \@@_if_usable:nTF {#1}
%<latexrelease> { code~pool~is~empty }
%<latexrelease> { is~\@@_if_disabled:nTF {#1} {disabled} {undeclared} }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macro}{\@@_log_next_code:n}
% To display the code for next invocation only (i.e., from
% \cs{AddToHookNext} we have to remove the string
% \cs{@@_clear_next:n}\Arg{hook}, so the simplest is to use a macro
% delimited by a \verb|}|$_12$.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_log_next_code:n}
%<latexrelease> {Hooks~with~args}
\exp_last_unbraced:NNNNo
\cs_new:Npn \@@_log_next_code:w #1 \c_right_brace_str { }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_log_next_code:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset:Npn \@@_log_next_code:n #1
%<latexrelease> { \exp_args:No \tl_to_str:n { \use_none:nn #1 } }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_print_args:n}
% Pretty-prints the number of arguments of a hook.
% \begin{macrocode}
\cs_new:Npn \@@_print_args:nn #1 #2
{
\int_compare:nNnT {#2} > { 0 }
{
\@@_if_declared:nT {#1} { \use_none:nnn }
\@@_if_cmd_hook:nT {#1}
{ \use_i:nnn { ~ (unknown ~ } }
\use:n { ~ (#2 ~ }
argument \int_compare:nNnT {#2} > { 1 } { s } )
}
}
% \end{macrocode}
% \end{macro}
%
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_list_rules:nn}
% \begin{macro}{\@@_list_one_rule:nnn,\@@_list_if_rule_exists:nnnF}
% This macro takes a \meta{hook} and an \meta{inline function} and
% loops through each pair of \meta{labels} in the \meta{hook}, and if
% there is a relation between this pair of \meta{labels}, the
% \meta{inline function} is executed with |#1|${}={}$\meta{relation},
% |#2|${}={}$\meta{label_1}\verb=|=\meta{label_2},
% and |#3|${}={}$\meta{hook} (the latter may be the argument |#1| to
% \cs{@@_list_rules:nn}, or \texttt{??} if it is a default rule).
% \begin{macrocode}
\cs_new_protected:Npn \@@_list_rules:nn #1 #2
{
\cs_set_protected:Npn \@@_tmp:w ##1 ##2 ##3 {#2}
\prop_map_inline:cn { g_@@_#1_code_prop }
{
\prop_map_inline:cn { g_@@_#1_code_prop }
{
\@@_if_label_case:nnnnn {##1} {####1}
{ \prop_map_break: }
{ \@@_list_one_rule:nnn {##1} {####1} }
{ \@@_list_one_rule:nnn {####1} {##1} }
{#1}
}
}
}
% \end{macrocode}
%
% These two are quite similar to \cs{@@_apply_label_pair:nnn} and
% \cs{@@_label_if_exist_apply:nnnF}, respectively, but rather than
% applying the rule, they pass it to the \meta{inline function}.
% \begin{macrocode}
\cs_new_protected:Npn \@@_list_one_rule:nnn #1#2#3
{
\@@_list_if_rule_exists:nnnF {#1} {#2} {#3}
{ \@@_list_if_rule_exists:nnnF {#1} {#2} { ?? } { } }
}
\cs_new_protected:Npn \@@_list_if_rule_exists:nnnF #1#2#3
{
\if_cs_exist:w g_@@_ #3 _rule_ #1 | #2 _tl \cs_end:
\exp_args:Nv \@@_tmp:w
{ g_@@_ #3 _rule_ #1 | #2 _tl } { #1 | #2 } {#3}
\exp_after:wN \use_none:nn
\fi:
\use:n
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_debug_print_rules:n}
% A shorthand for debugging that prints similar to \cs{prop_show:N}.
% \begin{macrocode}
\cs_new_protected:Npn \@@_debug_print_rules:n #1
{
\iow_term:n { The~hook~#1~contains~the~rules: }
\cs_set_protected:Npn \@@_tmp:w ##1
{
\@@_list_rules:nn {#1}
{
\iow_term:x
{
> ##1 {####2} ##1 => ##1 {####1}
\str_if_eq:nnT {####3} {??} { ~(default) }
}
}
}
\exp_args:No \@@_tmp:w { \use:nn { ~ } { ~ } }
}
% \end{macrocode}
% \end{macro}
%
%
%
%
% \subsection{Specifying code for next invocation}
%
% \begin{macro}{\hook_gput_next_code:nn}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_gput_next_code_with_args:nn} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_gput_next_code:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_gput_next_code:nn #1 #2
{
\@@_replacing_args_false:
\@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
\@@_replacing_args_reset:
}
\cs_new_protected:Npn \hook_gput_next_code_with_args:nn #1 #2
{
\@@_replacing_args_true:
\@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} {#2}
\@@_replacing_args_reset:
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_gput_next_code:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \hook_gput_next_code:nn #1
%<latexrelease> { \@@_normalize_hook_args:Nn \@@_gput_next_code:nn {#1} }
%<latexrelease>\cs_gset_protected:Npn \hook_gput_next_code_with_args:nn #1 #2 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_gput_next_code:nn}
% \begin{macrocode}
\cs_new_protected:Npn \@@_gput_next_code:nn #1 #2
{
\@@_if_disabled:nTF {#1}
{ \msg_error:nnn { hooks } { hook-disabled } {#1} }
{
\@@_if_structure_exist:nTF {#1}
{ \@@_gput_next_do:nn }
{ \@@_try_declaring_generic_next_hook:nn }
{#1} {#2}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_gput_next_do:nn}
% Start by sanity-checking with \cs{@@_chk_args_allowed:nn}.
% Then check if the ``next code'' token list is empty: if so we need
% to add a \cs{tl_gclear:c} to clear it, so the code lasts for one
% usage only. The token list is cleared early so that nested usages
% don't get lost. \cs{tl_gclear:c} is used instead of
% \cs{tl_gclear:N} in case the hook is used in an expansion-only
% context, so the token list doesn't expand before \cs{tl_gclear:N}:
% that would make an infinite loop. Also in case the main code token
% list is empty, the hook code has to be updated to add the next
% execution token list.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \changes{v1.1c}{2023/04/19}
% {Initialise hook structure when adding 'next' code (gh/1052).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_gput_next_do:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_gput_next_do:nn #1
{
\@@_init_structure:n {#1}
\@@_chk_args_allowed:nn {#1} { AddToHookNext }
\@@_cs_if_empty:cT { @@~#1 }
{ \@@_update_hook_code:n {#1} }
\@@_cs_if_empty:cT { @@_next~#1 }
{ \@@_next_gset:nn {#1} { \@@_clear_next:n {#1} } }
\@@_cs_gput_right:nnn { _next } {#1}
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_gput_next_do:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_gput_next_do:nn #1
%<latexrelease> {
%<latexrelease> \exp_args:Nc \@@_gput_next_do:Nnn
%<latexrelease> { @@_next~#1 } {#1}
%<latexrelease> }
%<latexrelease>\cs_gset_protected:Npn \@@_gput_next_do:Nnn #1 #2
%<latexrelease> {
%<latexrelease> \tl_if_empty:cT { @@~#2 }
%<latexrelease> { \@@_update_hook_code:n {#2} }
%<latexrelease> \tl_if_empty:NT #1
%<latexrelease> { \@@_tl_gset:Nn #1 { \@@_clear_next:n {#2} } }
%<latexrelease> \@@_tl_gput_right:Nn #1
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hook_gclear_next_code:n}
% Discard anything set up for next invocation of the hook.
% \changes{v1.0o}{2021/07/27}{Macro made public}
% \begin{macrocode}
\cs_new_protected:Npn \hook_gclear_next_code:n #1
{ \@@_normalize_hook_args:Nn \@@_clear_next:n {#1} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_clear_next:n}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_clear_next:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_clear_next:n #1
{ \@@_next_gset:nn {#1} { } }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_clear_next:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_clear_next:n #1
%<latexrelease> { \cs_gset_eq:cN { @@_next~#1 } \c_empty_tl }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \subsection{Using the hook}
%
% \begin{macro}{\hook_use:n}
% \begin{macro}[EXP]{\@@_use_initialized:n}
% \begin{macro}{\@@_preamble_hook:n}
% \cs{hook_use:n} as defined here is used in the preamble, where
% hooks aren't initialized by default. \cs{@@_use_initialized:n} is
% also defined, which is the non-\tn{protected} version for use within
% the document. Their definition is identical, except for the
% \cs{@@_preamble_hook:n} (which wouldn't hurt in the expandable
% version, but it would be an unnecessary extra expansion).
%
% \cs{@@_use_initialized:n} holds the expandable definition while in
% the preamble. \cs{@@_preamble_hook:n} initializes the hook in the
% preamble, and is redefined to \cs{use_none:n} at |\begin{document}|.
%
% Both versions do the same thing internally: they check that the hook exists as
% given, and if so they use it as quickly as possible.
%
% At |\begin{document}|, all hooks are initialized, and any change in
% them causes an update, so \cs{hook_use:n} can be made expandable.
% This one is better not protected so that it can expand into nothing
% if containing no code. Also important in case of generic hooks that
% we do not generate a \cs[no-index]{relax} as a side effect of
% checking for a csname. In contrast to the \TeX{} low-level
% \verb=\csname ...\endcsname= construct \cs{tl_if_exist:c} is
% careful to avoid this.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_use:n #1
{
\@@_preamble_hook:n {#1}
\@@_use_initialized:n {#1}
}
\cs_new:Npn \@@_use_initialized:n #1
{
\if_cs_exist:w @@~#1 \cs_end:
\cs:w @@~#1 \use_i:nn
\fi:
\use_none:n
\cs_end:
}
\cs_new_protected:Npn \@@_preamble_hook:n #1
{
\if_cs_exist:w @@~#1 \cs_end:
\@@_initialize_hook_code:n {#1}
\fi:
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\hook_use:n}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_new_protected:Npn \hook_use:n #1
%<latexrelease> {
%<latexrelease> \tl_if_exist:cT { @@~#1 }
%<latexrelease> {
%<latexrelease> \@@_preamble_hook:n {#1}
%<latexrelease> \cs:w @@~#1 \cs_end:
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\cs_new:Npn \@@_use_initialized:n #1
%<latexrelease> {
%<latexrelease> \if_cs_exist:w @@~#1 \cs_end:
%<latexrelease> \cs:w @@~#1 \exp_after:wN \cs_end:
%<latexrelease> \fi:
%<latexrelease> }
%<latexrelease>\cs_new_protected:Npn \@@_preamble_hook:n #1
%<latexrelease> { \@@_initialize_hook_code:n {#1} }
%<latexrelease>\cs_new:Npn \hook_use:nnw #1 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use:n}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_new_protected:Npn \hook_use:n #1
%<latexrelease> {
%<latexrelease> \tl_if_exist:cTF { @@~#1 }
%<latexrelease> {
%<latexrelease> \@@_preamble_hook:n {#1}
%<latexrelease> \cs:w @@~#1 \cs_end:
%<latexrelease> }
%<latexrelease> { \@@_use:wn #1 / \s_@@_mark {#1} }
%<latexrelease> }
%<latexrelease>\cs_new:Npn \@@_use_initialized:n #1
%<latexrelease> {
%<latexrelease> \if_cs_exist:w @@~#1 \cs_end:
%<latexrelease> \else:
%<latexrelease> \@@_use_undefined:w
%<latexrelease> \fi:
%<latexrelease> \cs:w @@~#1 \@@_use_end:
%<latexrelease> }
%<latexrelease>\cs_new:Npn \@@_use_undefined:w #1 #2 @@~#3 \@@_use_end:
%<latexrelease> {
%<latexrelease> #1 % fi
%<latexrelease> \@@_use:wn #3 / \s_@@_mark {#3}
%<latexrelease> }
%<latexrelease>\cs_new_protected:Npn \@@_preamble_hook:n #1
%<latexrelease> { \@@_initialize_hook_code:n {#1} }
%<latexrelease>\cs_new_eq:NN \@@_use_end: \cs_end:
%<latexrelease>\cs_new:Npn \hook_use:nnw #1 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\hook_use:nnw}
% \begin{macro}[EXP]{\@@_use_initialized:nnw}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_use:nnw} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use:nnw}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_use:nnw #1
{
\@@_preamble_hook:n {#1}
\@@_use_initialized:nnw {#1}
}
\cs_new:Npn \@@_use_initialized:nnw #1 #2
{
\cs:w
\if_cs_exist:w @@~#1 \cs_end:
@@~#1
\else:
use_none: \prg_replicate:nn {#2} { n }
\fi:
\cs_end:
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use:nnw}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset:Npn \hook_use:nnw #1 #2
%<latexrelease> { \use:c { use_none: \prg_replicate:nn {#2} { n } } }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@@_post_initialization_defs:}
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_post_initialization_defs:}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_post_initialization_defs:
{
\cs_gset_eq:NN \hook_use:n \@@_use_initialized:n
\cs_gset_eq:NN \hook_use:nnw \@@_use_initialized:nnw
\cs_gset_eq:NN \@@_preamble_hook:n \use_none:n
\cs_gset_eq:NN \@@_post_initialization_defs: \prg_do_nothing:
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_post_initialization_defs:}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_post_initialization_defs:
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[EXP]{\@@_use:wn}
% \begin{macro}{\@@_try_file_hook:n,\@@_if_usable_use:n}
% \cs{@@_use:wn} does a quick check to test if the current hook is a
% file hook: those need a special treatment. If it is not, the hook
% does not exist. If it is, then \cs{@@_try_file_hook:n} is called,
% and checks that the current hook is a file-specific hook using
% \cs{@@_if_file_hook:wTF}. If it's not, then it's a generic |file/|
% hook and is used if it exist.
%
% If it is a file-specific hook, it passes through the same
% normalization as during declaration, and then it is used if defined.
% \cs{@@_if_usable_use:n} checks if the hook exist, and calls
% \cs{@@_preamble_hook:n} if so, then uses the hook.
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}{\@@_use:wn}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use:wn}
%<latexrelease> {Standardise~generic~hook~names}
%<latexrelease>\cs_new:Npn \@@_use:wn #1 / #2 \s_@@_mark #3
%<latexrelease> {
%<latexrelease> \str_if_eq:nnTF {#1} { file }
%<latexrelease> { \@@_try_file_hook:n {#3} }
%<latexrelease> { } % Hook doesn't exist
%<latexrelease> }
% \end{macrocode}
%
% \changes{v1.0s}{2021/09/28}
% {Correct usage of older \cs{@@_if_file_hook:wTF} (gh/675)}
% \begin{macrocode}
%<latexrelease>\cs_new_protected:Npn \@@_try_file_hook:n #1
%<latexrelease> {
%<latexrelease> \@@_if_file_hook:wTF #1 / \s_@@_mark
%<latexrelease> {
%<latexrelease> \exp_args:Ne \@@_if_usable_use:n
%<latexrelease> { \exp_args:Ne \@@_file_hook_normalize:n {#1} }
%<latexrelease> }
%<latexrelease> { \@@_if_usable_use:n {#1} } % file/ generic hook (e.g. file/before)
%<latexrelease> }
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\cs_new_protected:Npn \@@_if_usable_use:n #1
%<latexrelease> {
%<latexrelease> \tl_if_exist:cT { @@~#1 }
%<latexrelease> {
%<latexrelease> \@@_preamble_hook:n {#1}
%<latexrelease> \cs:w @@~#1 \cs_end:
%<latexrelease> }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hook_use_once:n,\hook_use_once:nnw}
% For hooks that can and should be used only once we have a special
% use command that further inhibits the hook from getting more code
% added to it. This has the effect that any
% further code added to the hook is executed immediately rather
% than stored in the hook.
%
% The code needs some gymnastics to prevent space trimming from the
% hook name, since \cs{hook_use:n} and \cs{hook_use_once:n} are
% documented to not trim spaces.
%
% \changes{v1.0r}{2021/09/06}{Clean up after \cs{UseOneTimeHook} (gh/606)}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{hook_use_once:nnw} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_use_once:nnw}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \hook_use_once:n #1
{
\@@_if_execute_immediately:nF {#1}
{ \@@_normalize_hook_args:Nn \@@_use_once:nn { \use:n {#1} } { 0 } }
}
\cs_new_protected:Npn \hook_use_once:nnw #1 #2
{
\@@_if_execute_immediately:nF {#1}
{ \@@_normalize_hook_args:Nn \@@_use_once:nn { \use:n {#1} } {#2} }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_use_once:nnw}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \hook_use_once:n #1
%<latexrelease> {
%<latexrelease> \@@_if_execute_immediately:nF {#1}
%<latexrelease> { \@@_normalize_hook_args:Nn \@@_use_once:n { \use:n {#1} } }
%<latexrelease> }
%<latexrelease>\cs_gset:Npn \hook_use_once:nnw #1 #2
%<latexrelease> { \use:c { use_none: \prg_replicate:nn {#2} { n } } }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macro}{\@@_use_once:nn}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_use_once:nn}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_use_once:nn #1 #2
{
\@@_preamble_hook:n {#1}
\@@_use_once_set:n {#1}
% \end{macrocode}
% When a hook has arguments, the call to \cs{@@_use_initialized:n},
% should be the very last thing to happen, otherwise the arguments
% grabbed will be wrong. So, to clean up after the hook we need to
% cheat a bit and sneak the cleanup code at the end of the hook,
% along with the next execution code.
% \begin{macrocode}
\@@_replacing_args_false:
\@@_cs_gput_right:nnn { _next } {#1} { \@@_use_once_clear:n {#1} }
\@@_replacing_args_reset:
\@@_if_usable:nTF {#1}
{ \@@_use_initialized:n {#1} }
{
\int_compare:nNnT {#2} > { 0 }
{ \use:c { use_none: \prg_replicate:nn {#2} { n } } }
}
}
%<latexrelease>\EndIncludeInRelease
%
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use_once:nn}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_gset_protected:Npn \@@_use_once:n #1
%<latexrelease> {
%<latexrelease> \@@_preamble_hook:n {#1}
%<latexrelease> \@@_use_once_set:n {#1}
%<latexrelease> \@@_use_initialized:n {#1}
%<latexrelease> \@@_use_once_clear:n {#1}
%<latexrelease> }
%<latexrelease>\cs_undefine:N \@@_use_once:nn
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_use_once_set:n}
% \begin{macro}{\@@_use_once_clear:n}
% \cs{@@_use_once_set:n} is used before the actual hook code is
% executed so that any usage of \cs{AddToHook} inside the hook causes
% the code to execute immediately. Setting
% \cs[no-index]{g_@@_\meta{hook}_reversed_tl} to |I| prevents further
% code from being added to the hook. \cs{@@_use_once_clear:n} then
% clears the hook so that any further call to \cs{hook_use:n} or
% \cs{hook_use_once:n} will expand to nothing.
% \changes{v1.0r}{2021/09/06}{Clean up after \cs{UseOneTimeHook} (gh/606)}
% \changes{v1.0u}{2022/05/13}{Check if prop exists to avoid l3debug error}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_use_once_clear:n}
%<latexrelease> {Hooks~with~args}
\cs_new_protected:Npn \@@_use_once_set:n #1
{ \@@_tl_gset:cn { g_@@_#1_reversed_tl } { I } }
\cs_new_protected:Npn \@@_use_once_clear:n #1
{
\@@_code_gset:nn {#1} { }
\@@_next_gset:nn {#1} { }
\@@_toplevel_gset:nn {#1} { }
\prop_gclear_new:c { g_@@_#1_code_prop }
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_use_once_clear:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \@@_use_once_clear:n #1
%<latexrelease> {
%<latexrelease> \@@_tl_gclear:c { @@~#1 }
%<latexrelease> \@@_tl_gclear:c { @@_next~#1 }
%<latexrelease> \@@_tl_gclear:c { @@_toplevel~#1 }
%<latexrelease> \prop_gclear_new:c { g_@@_#1_code_prop }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_execute_immediately:n}
% To check whether the code being added should be executed immediately
% (that is, if the hook is a one-time hook), we check if
% \cs[no-index]{g_@@_\meta{hook}_reversed_tl} is |I|. The gymnastics
% around \cs{if:w} is there to allow the |reversed| token list to be
% empty.
% \changes{v1.0r}{2021/09/06}{Macro added (gh/606)}
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_execute_immediately:n #1 { T, F, TF }
{
\exp_after:wN \@@_use_none_delimit_by_s_mark:w
\if:w I
\if_cs_exist:w g_@@_#1_reversed_tl \cs_end:
\cs:w g_@@_#1_reversed_tl \exp_after:wN \cs_end:
\fi:
X
\s_@@_mark \prg_return_true:
\else:
\s_@@_mark \prg_return_false:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Querying a hook}
%
% Simpler data types, like token lists, have three possible states; they
% can exist and be empty, exist and be non-empty, and they may not
% exist, in which case emptiness doesn't apply (though
% \cs{tl_if_empty:N} returns false in this case).
%
% Hooks are a bit more complicated: they have several other states as
% discussed in \ref{sec:existence}.
% A hook may exist or not, and either way it may or may not be empty
% (even a hook that doesn't exist may be non-empty) or may be disabled.
%
% A hook is said to be empty when no code was added to it, either to
% its permanent code pool, or to its ``next'' token list. The hook
% doesn't need to be declared to have code added to its code pool
% (it may happen that a package $A$ defines a hook \hook{foo}, but
% it's loaded after package $B$, which adds some code to that hook.
% In this case it is important that the code added by package $B$ is
% remembered until package $A$ is loaded).
%
% All other states can only be queried with internal tests as the
% different states are irrelevant for package code.
%
% \begin{macro}[pTF]{\hook_if_empty:n}
% Test if a hook is empty (that is, no code was added to that hook).
% A \meta{hook} being empty means that all three of its
% \cs{g_@@_\meta{hook}_code_prop}, its
% \cs{@@_toplevel\textvisiblespace\meta{hook}} and its
% \cs{@@_next\textvisiblespace\meta{hook}} are empty.
% \changes{v1.1a}{2023/04/06}
% {Changes to add hook arguments (hook-args).}
% \changes{v1.1c}{2023/04/19}
% {Simpler and faster version (gh/1052).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\hook_if_empty:n}
%<latexrelease> {Hooks~with~args}
\prg_new_conditional:Npnn \hook_if_empty:n #1 { p , T , F , TF }
{
\if:w
T
\prop_if_exist:cT { g_@@_#1_code_prop }
{ \prop_if_empty:cF { g_@@_#1_code_prop } { F } }
\@@_cs_if_empty:cF { @@_toplevel~#1 } { F }
\@@_cs_if_empty:cF { @@_next~#1 } { F }
T
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\hook_if_empty:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\prg_new_conditional:Npnn \hook_if_empty:n #1 { p , T , F , TF }
%<latexrelease> {
%<latexrelease> \@@_if_structure_exist:nTF {#1}
%<latexrelease> {
%<latexrelease> \bool_lazy_and:nnTF
%<latexrelease> { \prop_if_empty_p:c { g_@@_#1_code_prop } }
%<latexrelease> {
%<latexrelease> \bool_lazy_and_p:nn
%<latexrelease> { \tl_if_empty_p:c { @@_toplevel~#1 } }
%<latexrelease> { \tl_if_empty_p:c { @@_next~#1 } }
%<latexrelease> }
%<latexrelease> { \prg_return_true: }
%<latexrelease> { \prg_return_false: }
%<latexrelease> }
%<latexrelease> { \prg_return_true: }
%<latexrelease> }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[pTF]{\@@_if_usable:n}
% A hook is usable if the
% token list that stores the sorted code for that hook,
% \cs[no-index]{@@\textvisiblespace\meta{hook}}, exists. The property
% list \cs[no-index]{g_@@_\meta{hook}_code_prop} cannot be used here
% because often it is necessary to add code to a hook without knowing
% if such hook was already declared, or even if it will ever be
% (for example, in case the package that defines it isn't loaded).
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_usable:n #1 { p , T , F , TF }
{
\cs_if_exist:cTF { @@~#1 }
{ \prg_return_true: }
{ \prg_return_false: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_structure_exist:n}
%
% An internal check if the hook has already its basic internal
% structure set up with
% \cs{@@_init_structure:n}. This means that the hook was already used somehow
% (a code chunk or rule was added to it), but it still wasn't declared
% with \cs{hook_new:n}.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_structure_exist:n #1 { p , T , F , TF }
{
\prop_if_exist:cTF { g_@@_#1_code_prop }
{ \prg_return_true: }
{ \prg_return_false: }
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[pTF]{\@@_if_declared:n}
%
% Internal test to check if the hook was officially declared with
% \cs{hook_new:n} or a variant.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_declared:n #1 { p, T, F, TF }
{
\tl_if_exist:cTF { g_@@_#1_declared_tl }
{ \prg_return_true: }
{ \prg_return_false: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_reversed:n}
% An internal conditional that checks if a hook is reversed.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_reversed:n #1 { p , T , F , TF }
{
\exp_after:wN \@@_use_none_delimit_by_s_mark:w
\if:w - \cs:w g_@@_#1_reversed_tl \cs_end:
\s_@@_mark \prg_return_true:
\else:
\s_@@_mark \prg_return_false:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_generic:n}
% \begin{macro}[pTF]{\@@_if_deprecated_generic:n}
% An internal conditional that checks if a name belongs to a generic
% hook. The deprecated version needs to check if |#3| is empty to
% avoid returning true on \hook{file/before}, for example.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_generic:n #1 { T, TF }
{ \@@_if_generic:w #1 / / / \s_@@_mark }
\cs_new:Npn \@@_if_generic:w #1 / #2 / #3 / #4 \s_@@_mark
{
\cs_if_exist:cTF { c_@@_generic_#1/./#3_tl }
{ \prg_return_true: }
{ \prg_return_false: }
}
\prg_new_conditional:Npnn \@@_if_deprecated_generic:n #1 { T, TF }
{ \@@_if_deprecated_generic:w #1 / / / \s_@@_mark }
\cs_new:Npn \@@_if_deprecated_generic:w #1 / #2 / #3 / #4 \s_@@_mark
{
\cs_if_exist:cTF { c_@@_deprecated_#1/./#2_tl }
{
\tl_if_empty:nTF {#3}
{ \prg_return_false: }
{ \prg_return_true: }
}
{ \prg_return_false: }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_cmd_hook:n}
% \begin{macro}[pTF]{\@@_if_cmd_hook:w}
% An internal conditional that checks if a given hook is a valid
% generic \hook{cmd} hook.
% \changes{v1.1d}{2023/05/21}
% {Changes to allow support arguments in cmd hooks (cmd-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\@@_if_cmd_hook:n}
%<latexrelease> {Hooks~with~args}
\prg_new_conditional:Npnn \@@_if_cmd_hook:n #1 { T }
{ \@@_if_cmd_hook:w #1 / / / \s_@@_mark }
\cs_new:Npn \@@_if_cmd_hook:w #1 / #2 / #3 / #4 \s_@@_mark
{
\if:w Y
\str_if_eq:nnF {#1} { cmd } { N }
\tl_if_exist:cF { c_@@_generic_#1/./#3_tl } { N }
Y
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\@@_if_cmd_hook:n}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_undefine:N \@@_if_cmd_hook:nT
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[pTF]{\@@_if_generic_reversed:n}
% An internal conditional that checks if a name belongs to a generic
% reversed hook.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_generic_reversed:n #1 { T }
{ \@@_if_generic_reversed:w #1 / / / \scan_stop: }
\cs_new:Npn \@@_if_generic_reversed:w #1 / #2 / #3 / #4 \scan_stop:
{
\if_charcode:w - \cs:w c_@@_generic_#1/./#3_tl \cs_end:
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_replacing_args:TF}
% \begin{macro}[EXP]{\@@_misused_if_replacing_args:nn}
% \begin{macro}{\@@_replacing_args_true:}
% \begin{macro}{\@@_replacing_args_false:}
% \begin{macro}{\@@_replacing_args_reset:}
% \begin{macro}{\g_@@_replacing_stack_seq}
% An internal conditional that checks if the code being added to the
% hook contains arguments.
% \changes{v1.1a}{2023/04/06}
% {Macro added (hook-args).}
% \begin{macrocode}
\seq_new:N \g_@@_replacing_stack_seq
\cs_new:Npn \@@_misused_if_replacing_args:nn #1 #2
{
\msg_expandable_error:nnn { latex2e } { should-not-happen }
{ Misused~\@@_if_replacing_args:. }
}
\cs_new:Npn \@@_if_replacing_args:TF
{ \@@_misused_if_replacing_args:nn }
\cs_new_protected:Npn \@@_replacing_args_true:
{
\seq_gpush:No \g_@@_replacing_stack_seq
{ \@@_if_replacing_args:TF }
\cs_set:Npn \@@_if_replacing_args:TF { \use_i:nn }
}
\cs_new_protected:Npn \@@_replacing_args_false:
{
\seq_gpush:No \g_@@_replacing_stack_seq
{ \@@_if_replacing_args:TF }
\cs_set:Npn \@@_if_replacing_args:TF { \use_ii:nn }
}
\cs_new_protected:Npn \@@_replacing_args_reset:
{
\seq_gpop:NN \g_@@_replacing_stack_seq \l_@@_return_tl
\cs_gset_eq:NN \@@_if_replacing_args:TF \l_@@_return_tl
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Messages}
%
% Hook errors are LaTeX kernel errors:
% \begin{macrocode}
\prop_gput:Nnn \g_msg_module_type_prop { hooks } { LaTeX }
% \end{macrocode}
% \changes{v1.0q}{2021/08/27}{Internal message name changes}
% And so are kernel errors (this should move elsewhere eventually).
% \begin{macrocode}
\prop_gput:Nnn \g_msg_module_type_prop { latex2e } { LaTeX }
\prop_gput:Nnn \g_msg_module_name_prop { latex2e } { kernel }
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { labels-incompatible }
{
Labels~'#1'~and~'#2'~are~incompatible
\str_if_eq:nnF {#3} {??} { ~in~hook~'#3' } .~
\int_compare:nNnTF {#4} = { 1 }
{ The~ code~ for~ both~ labels~ will~ be~ dropped. }
{ You~ may~ see~ errors~ later. }
}
{ LaTeX~found~two~incompatible~labels~in~the~same~hook.~
This~indicates~an~incompatibility~between~packages. }
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { exists }
{ Hook~'#1'~ has~ already~ been~ declared. }
{ There~ already~ exists~ a~ hook~ declaration~ with~ this~
name.\\
Please~ use~ a~ different~ name~ for~ your~ hook.}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{too-many-args}
%<latexrelease> {Hooks~with~args}
% \end{macrocode}
%
% \changes{v1.1a}{2023/04/06}
% {Messages 'too-many-args', 'without-args' and 'one-time-args' added (hook-args).}
% \begin{macrocode}
\msg_new:nnnn { hooks } { too-many-args }
{ Too~many~arguments~for~hook~'#1'. }
{
You~tried~to~declare~a~hook~with~#2~arguments,~but~a~
hook~can~only~have~up~to~nine.~LaTeX~will~define~this~
hook~with~nine~arguments.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { without-args }
{ Hook~'#1'~has~no~arguments. }
{
You~tried~to~use~\iow_char:N\\#2WithArguments~
on~a~hook~that~takes~no~arguments.\\
Check~the~usage~of~the~hook~or~use~\iow_char:N\\#2~instead.\\
\\
LaTeX~will~use~\iow_char:N\\#2.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { one-time-args }
{ You~can't~have~arguments~in~used~one-time~hook~'#1'. }
{
You~tried~to~use~\iow_char:N\\#2WithArguments~
on~a~one-time~hook~that~has~already~been~used.~
You~have~to~add~the~code~before~the~hook~is~used,~
or~add~the~code~without~arguments~using~\iow_char:N\\#2~instead.\\
\\
LaTeX~will~use~\iow_char:N\\#2.
}
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{too-many-args}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { hook-disabled }
{ Cannot~add~code~to~disabled~hook~'#1'. }
{
The~hook~'#1'~you~tried~to~add~code~to~was~previously~disabled~
with~\iow_char:N\\hook_disable_generic:n~or~\iow_char:N\\DisableGenericHook,~so~
it~cannot~have~code~added~to~it.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { empty-label }
{
Empty~code~label~\msg_line_context:.~
Using~'\@@_currname_or_default:'~instead.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { no-default-label }
{
Missing~(empty)~default~label~\msg_line_context:. \\
This~command~was~ignored.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { unknown-rule }
{
Unknown~ relationship~ '#3'~
between~ labels~ '#2'~ and~ '#4'~
\str_if_eq:nnF {#1} {??} { ~in~hook~'#1' }. ~
Perhaps~ a~ misspelling?
}
{
The~ relation~ used~ not~ known~ to~ the~ system.~ Allowed~ values~ are~
'before'~ or~ '<',~
'after'~ or~ '>',~
'incompatible-warning',~
'incompatible-error',~
'voids'~ or~
'unrelated'.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { rule-too-late }
{
Sorting~rule~for~'#1'~hook~applied~too~late.\\
Try~setting~this~rule~earlier.
}
{
You~tried~to~set~the~ordering~of~hook~'#1'~using\\
\ \ \iow_char:N\\DeclareHookRule{#1}{#2}{#3}{#4}\\
but~hook~'#1'~was~already~used~as~a~one-time~hook,~
thus~sorting~is\\
no~longer~possible.~Declare~the~rule~
before~the~hook~is~used.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnnn { hooks } { misused-top-level }
{
Illegal~use~of~\iow_char:N \\AddToHook{#1}[top-level]{...}.\\
'top-level'~is~reserved~for~the~user's~document.
}
{
The~'top-level'~label~is~meant~for~user~code~only,~and~should~only~
be~used~(sparingly)~in~the~main~document.~Use~the~default~label~
'\@@_currname_or_default:'~for~this~\@cls@pkg,~or~another~
suitable~label.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { set-top-level }
{
You~cannot~change~the~default~label~#1~'top-level'.~Illegal \\
\use:nn { ~ } { ~ } \iow_char:N \\#2{#3} \\
\msg_line_context:.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { extra-pop-label }
{
Extra~\iow_char:N \\PopDefaultHookLabel. \\
This~command~will~be~ignored.
}
\msg_new:nnn { hooks } { missing-pop-label }
{
Missing~\iow_char:N \\PopDefaultHookLabel. \\
The~label~'#1'~was~pushed~but~never~popped.~Something~is~wrong.
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { latex2e } { should-not-happen }
{
This~should~not~happen.~#1 \\
Please~report~at~https://github.com/latex3/latex2e.
}
% \end{macrocode}
%
%
%
% \begin{macrocode}
\msg_new:nnn { hooks } { activate-disabled }
{
Cannot~ activate~ hook~ '#1'~ because~ it~ is~ disabled!
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { cannot-remove }
{
Cannot~remove~chunk~'#2'~from~hook~'#1'~because~
\@@_if_structure_exist:nTF {#1}
{ it~does~not~exist~in~that~hook. }
{ the~hook~does~not~exist. }
}
% \end{macrocode}
%
% \begin{macrocode}
\msg_new:nnn { hooks } { generic-deprecated }
{
Generic~hook~'#1/#2/#3'~is~deprecated. \\
Use~hook~'#1/#3/#2'~instead.
}
% \end{macrocode}
%
%
% \subsection{\LaTeXe{} package interface commands}
%
%
%
% \begin{macro}{\NewHook,\NewReversedHook,\NewMirroredHookPair}
% Declaring new hooks \ldots
% \begin{macrocode}
\NewDocumentCommand \NewHook { m }
{ \hook_new:n {#1} }
\NewDocumentCommand \NewReversedHook { m }
{ \hook_new_reversed:n {#1} }
\NewDocumentCommand \NewMirroredHookPair { mm }
{ \hook_new_pair:nn {#1}{#2} }
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{
% \NewHookWithArguments,
% \NewReversedHookWithArguments,
% \NewMirroredHookPairWithArguments
% }
% Declaring new hooks with arguments\ldots
% \changes{v1.1a}{2023/04/06}
% {Add \cs{NewHookWithArguments} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\NewHookWithArguments}
%<latexrelease> {Hooks~with~args}
\NewDocumentCommand \NewHookWithArguments { mm }
{ \hook_new_with_args:nn {#1} {#2} }
\NewDocumentCommand \NewReversedHookWithArguments { mm }
{ \hook_new_reversed_with_args:nn {#1} {#2} }
\NewDocumentCommand \NewMirroredHookPairWithArguments { mmm }
{ \hook_new_pair_with_args:nnn {#1} {#2} {#3} }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\NewHookWithArguments}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \NewHookWithArguments #1 #2 { }
%<latexrelease>\cs_new_protected:Npn \NewReversedHookWithArguments #1 #2 { }
%<latexrelease>\cs_new_protected:Npn \NewMirroredHookPairWithArguments #1 #2 #3 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/06/01}{\ActivateGenericHook}
%<latexrelease> {Providing~hooks}
% \end{macrocode}
%
% \begin{macro}{\ActivateGenericHook}
% Providing new hooks \ldots
% \changes{v1.0m}{2021/04/29}{Add \cs{ProvideHook} etc.}
% \changes{v1.0o}{2021/08/02}{Change name}
% \begin{macrocode}
\NewDocumentCommand \ActivateGenericHook { m }
{ \hook_activate_generic:n {#1} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DisableGenericHook}
% Disabling a generic hook.
% \changes{v1.0o}{2021/08/02}{Change name}
% \begin{macrocode}
\NewDocumentCommand \DisableGenericHook { m }
{ \hook_disable_generic:n {#1} }
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2020/10/01}{\ActivateGenericHook}
%<latexrelease> {Providing~hooks}
%<latexrelease>\def \ActivateGenericHook #1 { }
%<latexrelease>\def \DisableGenericHook #1 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
%
%
% \begin{macro}{\AddToHook,\AddToHookWithArguments}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{AddToHookWithArguments} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\AddToHookWithArguments}
%<latexrelease> {Hooks~with~args}
\NewDocumentCommand \AddToHook { m o +m }
{ \hook_gput_code:nnn {#1} {#2} {#3} }
\NewDocumentCommand \AddToHookWithArguments { m o +m }
{ \hook_gput_code_with_args:nnn {#1} {#2} {#3} }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\AddToHookWithArguments}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \AddToHookWithArguments #1 #2 #3 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AddToHookNext,\AddToHookNextWithArguments}
% \changes{v1.1a}{2023/04/06}
% {Add \cs{AddToHookNextWithArguments} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\AddToHookNextWithArguments}
%<latexrelease> {Hooks~with~args}
\NewDocumentCommand \AddToHookNext { m +m }
{ \hook_gput_next_code:nn {#1} {#2} }
\NewDocumentCommand \AddToHookNextWithArguments { m +m }
{ \hook_gput_next_code_with_args:nn {#1} {#2} }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\AddToHookNextWithArguments}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new_protected:Npn \AddToHookNextWithArguments #1 #2 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClearHookNext}
% \changes{v1.0o}{2021/07/27}{Macro added}
% \begin{macrocode}
\NewDocumentCommand \ClearHookNext { m }
{ \hook_gclear_next_code:n {#1} }
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\RemoveFromHook}
% \begin{macrocode}
\NewDocumentCommand \RemoveFromHook { m o }
{ \hook_gremove_code:nn {#1} {#2} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\SetDefaultHookLabel}
% \begin{macro}{\PushDefaultHookLabel}
% \begin{macro}{\PopDefaultHookLabel}
% Now define a wrapper that replaces the top of the stack with the
% argument, and updates \cs{g_@@_hook_curr_name_tl} accordingly.
% \begin{macrocode}
\NewDocumentCommand \SetDefaultHookLabel { m }
{ \@@_set_default_hook_label:n {#1} }
%
% The label is only automatically updated with \cs{@onefilewithoptions}
% (\cs{usepackage} and \cs{documentclass}), but some packages, like
% Ti\emph{k}Z, define package-like interfaces, like
% \cs{usetikzlibrary} that are wrappers around \cs{input}, so they
% inherit the default label currently in force (usually |top-level|,
% but it may change if loaded in another package). To provide a
% package-like behavior also for hooks in these files, we provide
% high-level access to the default label stack.
% \begin{macrocode}
\NewDocumentCommand \PushDefaultHookLabel { m }
{ \@@_curr_name_push:n {#1} }
\NewDocumentCommand \PopDefaultHookLabel { }
{ \@@_curr_name_pop: }
% \end{macrocode}
%
% The current label stack holds the labels for all files but the
% current one (more or less like \cs{@currnamestack}), and the current
% label token list, \cs{g_@@_hook_curr_name_tl}, holds the label for
% the current file. However \cs{@pushfilename} happens before
% \cs{@currname} is set, so we need to look ahead to get the
% \cs{@currname} for the label. \pkg{expl3} also requires the current
% file in \cs{@pushfilename}, so here we abuse
% \cs{@expl@push@filename@aux@@@@} to do \cs{@@_curr_name_push:n}.
% \begin{macrocode}
\cs_gset_protected:Npn \@expl@push@filename@aux@@@@ #1#2#3
{
\@@_curr_name_push:n {#3}
\str_gset:Nx \g_file_curr_name_str {#3}
#1 #2 {#3}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
%
% \begin{macro}{
% \UseHook,
% \UseOneTimeHook,
% \UseHookWithArguments,
% \UseOneTimeHookWithArguments,
% }
% Avoid the overhead of \pkg{xparse} and its protection that we
% don't want here (since the hook should vanish without trace if empty)!
% \changes{v1.1a}{2023/04/06}
% {Add \cs{UseHookWithArguments} (hook-args).}
% \begin{macrocode}
%<latexrelease>\IncludeInRelease{2023/06/01}{\UseHookWithArguments}
%<latexrelease> {Hooks~with~args}
\cs_new:Npn \UseHook { \hook_use:n }
\cs_new:Npn \UseOneTimeHook { \hook_use_once:n }
\cs_new:Npn \UseHookWithArguments { \hook_use:nnw }
\cs_new:Npn \UseOneTimeHookWithArguments { \hook_use_once:nnw }
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2020/10/01}{\UseHookWithArguments}
%<latexrelease> {Hooks~with~args}
%<latexrelease>\cs_new:Npn \UseHookWithArguments #1 #2 { }
%<latexrelease>\cs_new:Npn \UseOneTimeHookWithArguments #1 #2 { }
%<latexrelease>\EndIncludeInRelease
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\ShowHook,\LogHook}
% \begin{macrocode}
\cs_new_protected:Npn \ShowHook { \hook_show:n }
\cs_new_protected:Npn \LogHook { \hook_log:n }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DebugHooksOn,\DebugHooksOff}
%
% \begin{macrocode}
\cs_new_protected:Npn \DebugHooksOn { \hook_debug_on: }
\cs_new_protected:Npn \DebugHooksOff { \hook_debug_off: }
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\DeclareHookRule}
%
% \begin{macrocode}
\NewDocumentCommand \DeclareHookRule { m m m m }
{ \hook_gset_rule:nnnn {#1}{#2}{#3}{#4} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DeclareDefaultHookRule}
% This declaration is only supported before \verb=\begin{document}=.
% \begin{macrocode}
\NewDocumentCommand \DeclareDefaultHookRule { m m m }
{ \hook_gset_rule:nnnn {??}{#1}{#2}{#3} }
\@onlypreamble\DeclareDefaultHookRule
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClearHookRule}
% A special setup rule that removes an existing relation.
% Basically {@@_rule_gclear:nnn} plus fixing the property list for debugging.
% \fmiinline{Needs perhaps an L3 interface, or maybe it should get dropped?}
% \begin{macrocode}
\NewDocumentCommand \ClearHookRule { m m m }
{ \hook_gset_rule:nnnn {#1}{#2}{unrelated}{#3} }
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[EXP]{\IfHookEmptyTF}
% Here we avoid the overhead of \pkg{xparse}, since \cs{IfHookEmptyTF}
% is used in \cs{end} (that is, every \LaTeX{} environment). As a
% further optimization, use \cs{let} rather than \cs{def} to avoid one
% expansion step.
% \begin{macrocode}
\cs_new_eq:NN \IfHookEmptyTF \hook_if_empty:nTF
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP,int]{\IfHookExistsTF}
% Marked for removal and no longer documented in the doc section!
% \phoinline{\cs{IfHookExistsTF} is used in \texttt{jlreq.cls},
% \texttt{pxatbegshi.sty}, \texttt{pxeverysel.sty},
% \texttt{pxeveryshi.sty}, so the public name may be an alias of the
% internal conditional for a while. Regardless, those packages' use for
% \cs{IfHookExistsTF} is not really correct and can be changed.}
% \begin{macrocode}
\cs_new_eq:NN \IfHookExistsTF \@@_if_usable:nTF
% \end{macrocode}
% \end{macro}
%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \subsection{Deprecated that needs cleanup at some point}
%
% \changes{v1.0p}{2021/08/20}{Added deprecation warnings for
% old generic hook commands (gh/638)}
%
% \begin{macro}[int]{
% \hook_disable:n,
% \hook_provide:n,
% \hook_provide_reversed:n,
% \hook_provide_pair:nn,
% \@@_activate_generic_reversed:n,
% \@@_activate_generic_pair:nn,
% }
% Deprecated.
% \begin{macrocode}
\cs_new_protected:Npn \hook_disable:n
{
\@@_deprecated_warn:nn
{ hook_disable:n }
{ hook_disable_generic:n }
\hook_disable_generic:n
}
\cs_new_protected:Npn \hook_provide:n
{
\@@_deprecated_warn:nn
{ hook_provide:n }
{ hook_activate_generic:n }
\hook_activate_generic:n
}
\cs_new_protected:Npn \hook_provide_reversed:n
{
\@@_deprecated_warn:nn
{ hook_provide_reversed:n }
{ hook_activate_generic:n }
\@@_activate_generic_reversed:n
}
\cs_new_protected:Npn \hook_provide_pair:nn
{
\@@_deprecated_warn:nn
{ hook_provide_pair:nn }
{ hook_activate_generic:n }
\@@_activate_generic_pair:nn
}
\cs_new_protected:Npn \@@_activate_generic_reversed:n #1
{ \@@_normalize_hook_args:Nn \@@_activate_generic:nn {#1} { - } }
\cs_new_protected:Npn \@@_activate_generic_pair:nn #1#2
{ \hook_activate_generic:n {#1} \@@_activate_generic_reversed:n {#2} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{
% \DisableHook,
% \ProvideHook,
% \ProvideReversedHook,
% \ProvideMirroredHookPair,
% }
% Deprecated.
% \begin{macrocode}
\cs_new_protected:Npn \DisableHook
{
\@@_deprecated_warn:nn
{ DisableHook }
{ DisableGenericHook }
\hook_disable_generic:n
}
\cs_new_protected:Npn \ProvideHook
{
\@@_deprecated_warn:nn
{ ProvideHook }
{ ActivateGenericHook }
\hook_activate_generic:n
}
\cs_new_protected:Npn \ProvideReversedHook
{
\@@_deprecated_warn:nn
{ ProvideReversedHook }
{ ActivateGenericHook }
\@@_activate_generic_reversed:n
}
\cs_new_protected:Npn \ProvideMirroredHookPair
{
\@@_deprecated_warn:nn
{ ProvideMirroredHookPair }
{ ActivateGenericHook }
\@@_activate_generic_pair:nn
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_deprecated_warn:nn}
% Warns about a deprecation, telling what should be used instead.
% \begin{macrocode}
\cs_new_protected:Npn \@@_deprecated_warn:nn #1 #2
{ \msg_warning:nnnn { hooks } { deprecated } {#1} {#2} }
\msg_new:nnn { hooks } { deprecated }
{
Command~\iow_char:N\\#1~is~deprecated~and~will~be~removed~in~a~
future~release. \\ \\
Use~\iow_char:N\\#2~instead.
}
% \end{macrocode}
% \end{macro}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \subsection{Internal commands needed elsewhere}
%
% Here we set up a few horrible (but consistent) \LaTeXe{} names to
% allow for internal commands to be used outside this module. We
% have to unset the \texttt{@\/@} since we want double ``at'' sign
% in place of double underscores.
%
% \begin{macrocode}
%<@@=>
% \end{macrocode}
%
% \begin{macro}[int]{
% \@expl@@@initialize@all@@,
% \@expl@@@hook@curr@name@pop@@
% }
%
% \InternalDetectionOff
% \begin{macrocode}
\cs_new_eq:NN \@expl@@@initialize@all@@
\__hook_initialize_all:
% \end{macrocode}
%
% \begin{macrocode}
\cs_new_eq:NN \@expl@@@hook@curr@name@pop@@
\__hook_curr_name_pop:
% \end{macrocode}
% \InternalDetectionOn
% \end{macro}
%
% Rolling back here doesn't undefine the interface commands as they
% may be used in packages without rollback functionality. So we
% just make them do nothing which may or may not work depending on
% the code usage.
% \changes{v1.0d}{2020/10/04}{Definition \cs{AddToHookNext} was supposed
% to be for \cs{AddToHook} vice versa (gh/401)}
% \begin{macrocode}
%
%<latexrelease>\IncludeInRelease{0000/00/00}{lthooks}
%<latexrelease> {The~hook~management}%
%<latexrelease>
%<latexrelease>\def \NewHook#1{}
%<latexrelease>\def \NewReversedHook#1{}
%<latexrelease>\def \NewMirroredHookPair#1#2{}
%<latexrelease>
%<latexrelease>\def \DisableGenericHook #1{}
%<latexrelease>
%<latexrelease>\long\def\AddToHookNext#1#2{}
%<latexrelease>
%<latexrelease>\def\AddToHook#1{\@gobble@AddToHook@args}
%<latexrelease>\providecommand\@gobble@AddToHook@args[2][]{}
%<latexrelease>
%<latexrelease>\def\RemoveFromHook#1{\@gobble@RemoveFromHook@arg}
%<latexrelease>\providecommand\@gobble@RemoveFromHook@arg[1][]{}
%<latexrelease>
%<latexrelease>\def \UseHook #1{}
%<latexrelease>\def \UseOneTimeHook #1{}
%<latexrelease>\def \ShowHook #1{}
%<latexrelease>\let \DebugHooksOn \@empty
%<latexrelease>\let \DebugHooksOff\@empty
%<latexrelease>
%<latexrelease>\def \DeclareHookRule #1#2#3#4{}
%<latexrelease>\def \DeclareDefaultHookRule #1#2#3{}
%<latexrelease>\def \ClearHookRule #1#2#3{}
% \end{macrocode}
% If the hook management is not provided we make the test for existence
% false and the test for empty true in the hope that this is most
% of the time reasonable. If not a package would need to guard
% against running in an old kernel.
% \begin{macrocode}
%<latexrelease>\long\def \IfHookExistsTF #1#2#3{#3}
%<latexrelease>\long\def \IfHookEmptyTF #1#2#3{#2}
%<latexrelease>
%<latexrelease>\EndModuleRelease
% \end{macrocode}
%
% \begin{macrocode}
%<@@=hook>
% \end{macrocode}
%
% \changes{v1.1a}{2023/04/06}
% {Add dedicated rollback code to revert data structures (hook-args).}
% \begin{macrocode}
%<latexrelease>\cs:w @@_rollback_tidying: \cs_end:
%<latexrelease>\bool_lazy_and:nnT
%<latexrelease> { \int_compare_p:nNn { \sourceLaTeXdate } > { 20230600 } }
%<latexrelease> { \int_compare_p:nNn { \requestedLaTeXdate } < { 20230601 } }
%<latexrelease> {
%<latexrelease> \cs_gset_protected:Npn \@@_rollback_tidying:
%<latexrelease> {
%<latexrelease> \@latex@error { Rollback~code~executed~twice }
%<latexrelease> {
%<latexrelease> Something~went~wrong~(unless~this~was~
%<latexrelease> done~on~purpose~in~a~testing~environment).
%<latexrelease> }
%<latexrelease> \use_none:nnnn
%<latexrelease> }
%<latexrelease> \cs_set:Npn \@@_tmp:w #1 #2
%<latexrelease> {
%<latexrelease> \@@_tl_gset:cx { @@#1~#2 }
%<latexrelease> {
%<latexrelease> \exp_args:No \exp_not:o
%<latexrelease> {
%<latexrelease> \cs:w @@#1~#2 \exp_last_unbraced:Ne \cs_end:
%<latexrelease> { \@@_braced_cs_parameter:n { @@#1~#2 } }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> }
%<latexrelease> \seq_map_inline:Nn \g_@@_all_seq
%<latexrelease> {
%<latexrelease> \exp_after:wN \cs_gset_nopar:Npn
%<latexrelease> \cs:w g_@@_#1_code_prop \exp_args:NNo \exp_args:No
%<latexrelease> \cs_end: { \cs:w g_@@_#1_code_prop \cs_end: }
%<latexrelease> \@@_tmp:w { _toplevel } {#1}
%<latexrelease> \@@_tmp:w { _next } {#1}
%<latexrelease> }
%<latexrelease> }
\ExplSyntaxOff
%</2ekernel|latexrelease>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=>
% \end{macrocode}
%
% \Finale
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
^^A Needed for emacs
^^A
^^A Local Variables:
^^A mode: latex
^^A coding: utf-8-unix
^^A End:
|