twig-sys 3.11.0

FFI bindings and native library for Twig (the Djot/Markdown/HTML/XML document engine). Used by the `twig-doc` crate.
Documentation
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
//! The authoring editor: a `Splicer` that knows how its format is SPELLED.
//!
//! ── The two layers ─────────────────────────────────────────────────────────
//! `Splicer` (`ast/splicer.zig`) is the engine: byte spans in, reparse,
//! rollback, undo. It is language-agnostic by construction and imports no
//! language module — hand it a `parse_fn` and it will edit djot, Markdown or
//! XML with the same code. What it cannot do is decide that bold is spelled
//! `**` here and `*` there.
//!
//! `Editor` is that decision, and nothing else: `Splicer` + a `*const Syntax`.
//! It hosts the gestures a caret editor actually performs — Cmd-B, H1, quote,
//! link — each of which is "consult the table, build the bytes, hand the
//! Splicer one span". The Splicer's invariant survives intact, because `Editor`
//! depends only on the `Syntax` INTERFACE and still names no format: it never
//! learns whether the table it was handed came from djot or Markdown.
//! `format.zig`'s registry is what binds the two.
//!
//! ── Why this exists ────────────────────────────────────────────────────────
//! All of this lived in `c_abi.zig`. Not by design — it accreted there because
//! the C ABI was the first (and only) caller with a caret to serve, and the
//! layer it needed didn't exist. The cost was steep: the knowledge that
//! `mailto:a@b.dev` is a `url` in Markdown but an `email` in djot could only be
//! reached through an `extern` function, could only be tested through a
//! `TwigEditor*` handle and a `TwigStatus` code, and could not be reached by
//! `twig edit` at all.
//!
//! So the C ABI's `TwigEditor` was never `Splicer` — it was always this type,
//! `{ editor, format }`, assembled by hand at the boundary. That is why this
//! module took the `Editor` name and the engine underneath was renamed to what
//! it always was: `TwigEditor` maps to `*Editor`, 1:1, and the ABI's job is
//! back to marshalling.
//!
//! ── Errors ─────────────────────────────────────────────────────────────────
//! Typed, so the ABI's mapping is mechanical and every other caller gets to
//! `switch` on something real. `error.UnsupportedFormat` is uniformly "the
//! `Syntax` table has a `null` where this gesture needed a spelling" — never a
//! hand-written per-format arm. See `syntax.zig`.

const std = @import("std");
const Allocator = std.mem.Allocator;
const Writer = std.Io.Writer;

const AST = @import("ast.zig");
const Document = @import("../document.zig");
const Span = @import("../span.zig");
const locate = @import("locate.zig");
const table_edit = @import("table_edit.zig");
const syntax_mod = @import("../syntax.zig");

pub const Splicer = @import("splicer.zig").Splicer;

/// Used by the free functions below; the public vocabularies all hang off
/// `Editor` itself (`Editor.InlineKind`, `Editor.Error`, ...).
const Syntax = syntax_mod.Syntax;
const ContainerSpelling = syntax_mod.ContainerSpelling;
/// The renderers' types, unwrapped from their optional fields — what a gesture
/// holds once it has dispatched on presence.
const RenderTextFn = @typeInfo(@FieldType(Syntax, "renderText")).optional.child;
const attrs_writer = @import("../attrs_writer.zig");
const RenderBlockFn = @typeInfo(@FieldType(Syntax, "renderBlock")).optional.child;

/// The node `setBlock`'s render path builds for a `BlockKind`.
fn blockNodeKind(kind: syntax_mod.BlockKind, level: u32) AST.Node.Kind {
    return switch (kind) {
        .paragraph => .para,
        .heading => .{ .heading = .{ .level = level } },
    };
}

/// The node an `InlineKind`/`ContainerKind` parses back as. The vocabularies
/// are named for their kinds, so this is a rename, not a mapping — and it fails
/// to compile rather than silently mis-mapping if one drifts.
///
/// It yields an `AST.KindRef` rather than a bare tag because seven of the eight
/// `InlineKind`s are now `InlineMark` family members sharing the `inline_mark`
/// tag; only `verbatim` (a text leaf, not a paired wrapper) is still a tag of
/// its own. The `@hasField` split is resolved at comptime, so a vocabulary
/// entry that matches NEITHER a mark nor a kind tag is still a compile error.
fn kindRef(kind: anytype) AST.KindRef {
    return switch (kind) {
        inline else => |k| if (@hasField(AST.InlineMark, @tagName(k)))
            .{ .mark = @field(AST.InlineMark, @tagName(k)) }
        else if (@hasField(AST.TextLeafKind, @tagName(k)))
            .{ .text_leaf = @field(AST.TextLeafKind, @tagName(k)) }
        else
            .{ .tag = @field(Splicer.KindTag, @tagName(k)) },
    };
}

/// `kindRef` for a vocabulary that is always a plain tag (`ContainerKind`) —
/// asserts that at comptime rather than leaving the caller to unwrap.
fn kindTag(kind: anytype) Splicer.KindTag {
    return switch (kindRef(kind)) {
        .tag => |t| t,
        .text_leaf => unreachable,
        .mark => unreachable,
        .markup_leaf => unreachable,
        // `kindRef` never mints one: it maps a gesture vocabulary, and no
        // gesture names a container by name.
        .container_named => unreachable,
    };
}

/// Room for the widest marker/indent a list can produce (`999. ` and friends).
const container_indent = " " ** 24;

pub const Editor = struct {
    /// The kind vocabularies, re-exported so a caller needs only this type:
    /// `twig.Editor.InlineKind`. (The `Syntax` type itself is `twig.Syntax`.)
    pub const InlineKind = syntax_mod.InlineKind;
    pub const BlockKind = syntax_mod.BlockKind;
    pub const ContainerKind = syntax_mod.ContainerKind;

    pub const Error = error{
        /// `start > end`, or a range reaching past the source.
        InvalidRange,
        /// A heading level outside 1-6.
        InvalidLevel,
        /// A destination this format cannot hold (one containing a newline).
        InvalidDestination,
        /// An info string this format's code fence cannot hold — one carrying a
        /// line end, the fence byte itself, or (where the format ends its info
        /// string at whitespace) a space.
        InvalidLanguage,
        /// A colour name outside `Syntax.MarkColors.colors` — a colour this
        /// format has no spelling for, which is not one an editor may write:
        /// the bytes would be content, not a colour.
        InvalidColor,
        /// A label this format cannot hold. For a footnote: empty, or carrying
        /// a line end or a reference bracket. For a directive: carrying a line
        /// end or a square bracket, either of which closes the `[…]` the label
        /// is written in. An EMPTY directive label is legitimate — `::name[]`
        /// — and is a different document from no label at all, which is
        /// spelled by passing `null`.
        InvalidLabel,
        /// A table shape no pipe format spells: zero columns, or zero body rows
        /// under the header.
        InvalidShape,
        /// A directive name outside the grammar every format reads one back
        /// by: an ASCII letter followed by letters, digits, `-` and `_`. A
        /// name is the whole identity of the node being written — there is no
        /// `::` without one — and it is written where a delimiter would
        /// otherwise be, so anything outside that alphabet reparses as
        /// something else. See `checkDirectiveName`.
        InvalidName,
        /// An attribute no format reads back as one: a key outside the grammar
        /// they share (an ASCII letter or `_`, then letters, digits, `-`, `_`
        /// and `:`), a BARE key with no value (djot's `{…}` has no spelling
        /// for one, so it would come back as text), or a value carrying a
        /// line end or a double quote. See `checkAttr`.
        InvalidAttribute,
        /// A formula this format cannot hold: printed on its own, the math
        /// the gesture asked for does not come back holding exactly these
        /// bytes. Markdown's `$` refuses an empty formula, one opening or
        /// closing on a space, and one holding a `$`; djot's run takes almost
        /// anything. See `insertInlineMath`.
        InvalidFormula,
        /// A destination that names the block being moved: `moveBlock`'s `to`
        /// inside the block, or at the boundary the block already sits on —
        /// a move that would change nothing, refused rather than reported as
        /// a change with no bytes in it.
        InvalidArgument,
        /// The `Syntax` table has no spelling for this gesture in this format.
        UnsupportedFormat,
        /// No block covers the offset/range this gesture needs one for.
        NoBlock,
        /// The target node has no editable span/interior, or the gesture would
        /// corrupt something it refuses to touch.
        NotEditable,
        /// The edit produced a document that no longer parses; it was rolled
        /// back and nothing changed.
        EditConflict,
    } || Allocator.Error;

    splicer: Splicer,
    /// This format's spelling. Borrowed — `format.zig`'s registry entries are
    /// static, so it outlives any editor.
    syntax: *const Syntax,

    /// `parse_ctx`/`parse_fn` are the Splicer's contract (see its doc comment);
    /// `syntax` is the table every gesture below consults. Pair them from
    /// `format.zig`'s registry rather than by hand — an entry's `parseToAst` and
    /// `syntax` are two halves of one language, and crossing them would spell
    /// djot into a Markdown document.
    pub fn init(
        allocator: Allocator,
        source_bytes: []const u8,
        parse_ctx: *const anyopaque,
        parse_fn: Splicer.ParseFn,
        syntax: *const Syntax,
    ) !Editor {
        return .{
            .splicer = try Splicer.init(allocator, source_bytes, parse_ctx, parse_fn),
            .syntax = syntax,
        };
    }

    pub fn deinit(self: *Editor) void {
        self.splicer.deinit();
    }

    pub fn sourceBytes(self: *const Editor) []const u8 {
        return self.splicer.sourceBytes();
    }

    pub fn astView(self: *const Editor) *const AST {
        return self.splicer.astView();
    }

    pub fn lastChange(self: *const Editor) ?Splicer.Change {
        return self.splicer.last_change;
    }

    /// Validate a caller-supplied byte range. `Splicer.replaceAtSpan` ASSERTS on
    /// a bad range — fine for internal callers, but a range from a C caller or a
    /// stale caret is untrusted input, so it is checked into an error here,
    /// once, before any gesture can reach the assert.
    fn checkRange(self: *const Editor, start: usize, end: usize) Error!void {
        if (start > end or end > self.sourceBytes().len) return error.InvalidRange;
    }

    /// Splice rebuilt source in over `[start, end)`. Every gesture ends here.
    fn commitSplice(self: *Editor, start: usize, end: usize, text: []const u8) Error!void {
        return self.commitSpliceChecked(start, end, text, null);
    }

    /// `commitSplice`, kept only if `check` accepts the reparse. A rejected
    /// reparse is `error.NotEditable`: the bytes parsed, and meant something
    /// other than what the gesture wrote, at this place in this document.
    fn commitSpliceChecked(self: *Editor, start: usize, end: usize, text: []const u8, check: ?Splicer.Check) Error!void {
        self.splicer.replaceAtSpanChecked(Span.init(start, end), text, check) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            error.Rejected => return error.NotEditable,
            // Anything else is the parser rejecting the edited document; the
            // splicer has already rolled it back.
            else => return error.EditConflict,
        };
    }

    // ── Capability ─────────────────────────────────────────────────────────

    /// One gesture, named with whatever kind it takes — the question
    /// `supports` answers, in the same vocabulary the gesture itself is called
    /// with. A payload here exists exactly where the gesture has a `kind`
    /// parameter, so a caller that can spell the call can spell the query.
    ///
    /// Every gesture `Editor` has appears here, because every gesture now has a
    /// FORMAT-level gate. Nine of them didn't:
    ///
    ///   * The seven TABLE gestures rebuilt a table by writing pipe syntax from
    ///     literals in `table_edit.zig`. HTML's parser lowers `<table>` to the
    ///     same `table`/`row`/`cell` nodes a pipe table produces, so the grid
    ///     extracted cleanly and the pipes were spliced over the elements —
    ///     which reparses as a paragraph, so not even `EditConflict` fired. They
    ///     read `Syntax.table_spelling` now.
    ///   * `splitBlock`'s plain-text arm separated the halves with a blank line,
    ///     which means "two blocks" only where blank lines separate blocks. In
    ///     HTML it is insignificant whitespace inside the `<p>`: one paragraph
    ///     in, one paragraph out, success reported. It reads
    ///     `Syntax.block_separator` now, and `assertCoherent` pins the other two
    ///     spellings it may need (a heading marker, a code fence) non-null
    ///     alongside it, so the answer below stays a total one rather than a
    ///     property of the caret.
    ///   * `renumberOrderedLists` rewrote `N.`/`N)` markers textually, which
    ///     finds nothing in an HTML `<ol>` and reports a successful no-op. It
    ///     gates on the ordered-list container spelling now.
    ///
    /// The table gestures get one variant EACH rather than one shared
    /// `table_edit`, which is the granularity the task and code-block families
    /// already use: they share a gate, and a toolbar still grays out seven
    /// buttons.
    pub const Gesture = union(enum) {
        wrap_range: InlineKind,
        toggle_inline: InlineKind,
        set_mark_color,
        set_block,
        toggle_block_container: ContainerKind,
        insert_thematic_break,
        toggle_code_block,
        set_code_language,
        toggle_task_item,
        set_task_checked,
        toggle_task_checked,
        insert_link,
        insert_image,
        insert_footnote,
        insert_literal,
        insert_line_break,
        split_block,
        join_blocks,
        renumber_ordered_lists,
        table_insert_row,
        table_delete_row,
        table_insert_column,
        table_delete_column,
        table_set_alignment,
        table_move_row,
        table_move_column,
        insert_table,
        insert_directive,
        set_block_attrs,
        wrap_range_attrs,
        set_node_attrs,
        move_block,
        insert_inline_math,
        insert_display_math,
    };

    /// Whether `syntax` can spell `gesture` — the toolbar's gray-out question,
    /// asked WITHOUT a document, so a caller can build its UI before it has one.
    ///
    /// This is the format half of the answer and only that half. `true` means
    /// the gesture will not fail with `error.UnsupportedFormat`; it says
    /// nothing about the caret, so a supported gesture can still report
    /// `NoBlock`, `NotEditable` or `EditConflict` at the position it is
    /// actually run. Gray out on `false`; do not assume `true` means the call
    /// will succeed.
    ///
    /// Distinct from BOTH neighbouring questions, which are easy to reach for
    /// and wrong here:
    ///
    ///   * `Syntax.authorable()` is "is there a door in" — true for HTML,
    ///     while a task box, a footnote and every table edit over it are
    ///     still unsupported. A toolbar enabled on that predicate has buttons
    ///     that fail.
    ///   * `diagnostics.zig`'s `fidelity` is "what survives a CONVERSION to this
    ///     target", which is a different table with genuinely different answers
    ///     (a smart-quote container is unauthorable in djot yet round-trips
    ///     there perfectly). Use that one for a save-as warning, this one for
    ///     an enabled/disabled button.
    ///
    /// Static — it takes the table rather than an editor — because the whole
    /// point is to answer before an `Editor` exists. `format.zig`'s `syntaxFor`
    /// gets you the table from a `Format`.
    pub fn supports(syntax: *const Syntax, gesture: Gesture) bool {
        return switch (gesture) {
            // The two inline gestures share one gate, and it is
            // `authorableDelimsFor` rather than `delimsFor`: a spelling the
            // serializer may emit but a gesture must not mint (Markdown's
            // `==mark==`) is unsupported HERE while still being written on
            // conversion. That asymmetry is `Delims.authorable`'s reason to
            // exist, so the query has to ask the same way the gesture does.
            .wrap_range, .toggle_inline => |k| syntax.authorableDelimsFor(kindRef(k)) != null,
            // A separate gate from `.toggle_inline = .mark`, and a strictly
            // narrower one: Markdown authors a highlight under
            // `ParseOptions.highlight` and can only COLOUR one under
            // `highlight_colors` on top, so a toolbar grays the palette while
            // the highlight button stays live. `assertCoherent` pins the
            // implication (a palette implies an authorable mark), not the
            // converse.
            .set_mark_color => syntax.mark_colors != null,
            // Either path: a marker alphabet to write, or a fragment renderer
            // to print a fresh node through. Each of these gestures prefers
            // the alphabet and falls back to the renderer, in that order —
            // see `setBlock`, which set the pattern — so a format that has an
            // alphabet keeps its byte-preserving path and a format with none
            // (HTML's tag pairs, AsciiDoc's `dest[text]` link) prints the node.
            .set_block => syntax.heading_marker != null or syntax.renderBlock != null,
            .toggle_block_container => |k| syntax.container_spelling.get(k) != null or syntax.renderBlock != null,
            .insert_thematic_break => syntax.thematic_break != null,
            .toggle_code_block, .set_code_language => syntax.code_fence != null or syntax.renderBlock != null,
            .toggle_task_item, .set_task_checked, .toggle_task_checked => syntax.task_marker != null,
            .insert_link => syntax.link_text_escapes != null or syntax.renderBlock != null,
            // Both halves, as `insertImage` checks them. `assertCoherent` pins
            // them null-together, so this can't disagree with `.insert_link` —
            // it is written out anyway so the query reads as the gesture does.
            .insert_image => (syntax.link_text_escapes != null and syntax.link_dest_escapes != null) or syntax.renderBlock != null,
            .insert_footnote => syntax.footnote != null,
            .insert_literal => syntax.renderText != null,
            .insert_line_break => syntax.cell_line_break != null,
            // A total answer only because `assertCoherent` pins a heading marker
            // and a code fence non-null wherever a block separator is: those are
            // the two spellings `splitBlock` reaches for once it knows which
            // block the caret is in, and without that invariant this row would
            // be true while the gesture reported unsupported in a fence.
            .split_block => syntax.block_separator != null,
            // A WIDER gate than the split's, and the pair is the clearest case
            // in this switch for why each gesture reads its own field: a join
            // writes a line break INSIDE a block, which HTML spells (a newline
            // in a `<p>` is whitespace, and the reparse gives back the one
            // paragraph the gesture claims) while it spells no blank-line
            // block separator at all. Nothing else the gesture needs comes
            // from the table — a heading's marker, the container prefix and
            // the closing markup it carries are all read out of the document —
            // so this row needs no `assertCoherent` implication behind it.
            .join_blocks => syntax.line_join != null,
            .renumber_ordered_lists => spellsOrderedMarkers(syntax),
            .table_insert_row,
            .table_delete_row,
            .table_insert_column,
            .table_delete_column,
            .table_set_alignment,
            .table_move_row,
            .table_move_column,
            .insert_table,
            => syntax.table_spelling != null,
            // Both halves, and neither implies the other here: the renderer is
            // how the bytes are written, and the claim is whether the parser
            // hands them back as the container they were. `assertCoherent`
            // pins the first onto the second, so this cannot answer true with
            // nothing to print through.
            .insert_directive => syntax.names_leaf_containers and syntax.renderBlock != null,
            // The same two halves: a shape the attributes come back in, and
            // the renderer that prints them. `assertCoherent` pins the second
            // onto the first, as for the directive.
            .set_block_attrs => syntax.block_attrs != null and syntax.renderBlock != null,
            .wrap_range_attrs => syntax.inline_attrs and syntax.renderBlock != null,
            // One half only: the run is written by a fixed algorithm
            // (`attrs_writer.writeHtmlAttrs`) into a span the parser recorded,
            // so no renderer is consulted and none has to be present.
            .set_node_attrs => syntax.node_attrs != null,
            // A block moves as its LINES, re-prefixed for the container it
            // lands in, and every prefix is read off the document — so what
            // the table has to say is only how two blocks are kept apart: a
            // blank line where `block_separator` states one, or nothing
            // where a block is a delimited fragment of its own (HTML's tag
            // pairs, which `renderBlock` is the sign of). A parse-only table
            // has neither, which is what makes XML unsupported: it has no
            // blocks for a caret to name.
            .move_block => syntax.block_separator != null or syntax.renderBlock != null,
            // Both halves, as the gesture checks them: a formula the parser
            // reads back, and the renderer that prints it. `assertCoherent`
            // pins the second onto the first, as for the directive.
            .insert_inline_math => syntax.authorableDelimsFor(.{ .text_leaf = .inline_math }) != null and syntax.renderBlock != null,
            .insert_display_math => syntax.authorableDelimsFor(.{ .text_leaf = .display_math }) != null and syntax.renderBlock != null,
        };
    }

    /// Whether this format spells an ordered list item as a NUMBERED LINE
    /// MARKER — the one question `renumberOrderedLists` needs answered, and the
    /// expression it and `supports` share so the two cannot drift.
    ///
    /// The renumber pass rewrites the numeric run of a `N.` / `N)` marker in the
    /// source. That is a gesture about a marker, so a spelling is not enough: a
    /// format could spell an ordered list some other way (`ContainerSpelling`
    /// admits a fixed `marker`), and rewriting digits in it would find none and
    /// call the no-op a success — which is precisely what an HTML `<ol>` did
    /// before this gate, where there is no container spelling at all.
    fn spellsOrderedMarkers(syntax: *const Syntax) bool {
        const sp = syntax.container_spelling.get(.ordered_list) orelse return false;
        return sp.numbered;
    }

    // ── Inline marks ───────────────────────────────────────────────────────

    /// Wrap `[start, end)` in `kind`'s delimiters — the unconditional half of
    /// the inline toolbar (always adds a mark). One pair per block the range
    /// touches; see `applyInline`.
    pub fn wrapRange(self: *Editor, span: Span, kind: InlineKind) Error!void {
        return self.applyInline(span, kind, .wrap);
    }

    /// Toggle `kind` over `[start, end)`: strip the mark where the range already
    /// IS a node of `kind` — covers its whole interior and reaches no further
    /// than its delimiters, looking through a mark of another kind that is
    /// nothing but it (`Splicer.inlineNodeCovering`) — else wrap it — a rich
    /// editor's Cmd-B. One decision per block the range touches; see
    /// `applyInline`.
    pub fn toggleInline(self: *Editor, span: Span, kind: InlineKind) Error!void {
        return self.applyInline(span, kind, .toggle);
    }

    /// Whether the gesture may REMOVE a mark it finds, or only ever adds one —
    /// the single difference between `toggleInline` and `wrapRange`, which
    /// otherwise cut the range up and reassemble it identically.
    const InlineMode = enum { wrap, toggle };

    /// One replacement inside the assembled region: `span` goes, and either
    /// `interior` takes its place (a mark being removed) or `span`'s own bytes
    /// come back wrapped in delimiters (a mark being added).
    ///
    /// `interior` aliases the pre-edit source, so an `Edit` is valid only until
    /// the splice that consumes it.
    const Edit = struct { span: Span, interior: ?[]const u8 };

    /// Both inline gestures, in one pass: cut `span` at its block boundaries,
    /// decide per piece, and splice the lot once.
    ///
    /// ── Why per block ──────────────────────────────────────────────────────
    /// A selection is a byte range; a mark is not. Wrapping the range whole put
    /// the opening delimiter in one block and the closing one in the next
    /// (`**one two\n\nthree four**`), which reparses perfectly well as two
    /// paragraphs carrying four literal asterisks — so nothing failed, and the
    /// document simply did not gain the mark the gesture reported writing.
    /// `locate.inlineHostPieces` is where the boundaries come from; the bytes
    /// between the pieces (the blank line, the next block's `- ` or `> `) are
    /// copied through untouched, so what comes out is `**one two**` and
    /// `**three four**` and a document that still says what it said.
    ///
    /// This is what every rich editor does with a multi-block selection, and it
    /// is also what makes the SECOND press work: each piece is decided on its
    /// own, so pressing Cmd-B again over the same selection finds a mark around
    /// each piece and removes both, instead of nesting a second pair around the
    /// first.
    ///
    /// ── Why still ONE splice ───────────────────────────────────────────────
    /// Every gesture here is one `commitSplice`, and that is not a stylistic
    /// preference: a splice is a reparse, an undo step, and a `Splicer.Change`
    /// the host re-anchors its caret against. N pieces spliced separately would
    /// be N reparses, N undo steps to press Cmd-Z through, and N changes whose
    /// offsets each invalidate the next piece's span. So the pieces are decided
    /// against ONE document state, assembled into one buffer, and written once
    /// — the same shape `renumberOrderedLists` uses over a whole list.
    ///
    /// The region that buffer covers is the selection UNION every mark being
    /// removed, because a removal takes its delimiters with it and those sit
    /// outside a selection of the mark's interior (see `Splicer.inlineStrip`).
    /// That is why nothing can be written until every piece has been asked.
    ///
    /// `error.NotEditable` when the range holds no inline host at all — a
    /// selection inside a code block, where `**` would be two asterisks of
    /// someone's program rather than a mark. A code SPAN is the same asterisks
    /// at a smaller scale, and a piece that cuts into one is widened to the
    /// whole of it (`locate.widenOverVerbatim`), so the mark closes around the
    /// backticks. A zero-width range is exempt from the whole business: it
    /// crosses no boundary, and inserting an empty pair for the caret to type
    /// between is a gesture in its own right.
    fn applyInline(self: *Editor, span: Span, kind: InlineKind, mode: InlineMode) Error!void {
        try self.checkRange(span.start, span.end);
        const d = self.syntax.authorableDelimsFor(kindRef(kind)) orelse return error.UnsupportedFormat;
        const allocator = self.splicer.allocator;

        var pieces: std.ArrayList(Span) = .empty;
        defer pieces.deinit(allocator);
        if (span.len() == 0) {
            // A caret spans no block, and clipping it to a host would find a
            // zero-width share and drop it — losing "open a pair here".
            try pieces.append(allocator, span);
        } else {
            try locate.inlineHostPieces(allocator, &self.splicer.doc, span, &pieces);
            if (pieces.items.len == 0) return error.NotEditable;
            // A piece that cuts into a code span widens to the whole of it:
            // `**` inside the backticks is two asterisks of code, so the
            // mark closes around the code instead — `` **`word`** `` from a
            // selection of `word`, which the next press takes off again.
            for (pieces.items) |*p| locate.widenOverVerbatim(&self.splicer.doc, p);
        }

        // Decide everything first: a removal expands its piece to the whole
        // mark, so the region being spliced is not known until the last piece
        // has answered.
        var edits: std.ArrayList(Edit) = .empty;
        defer edits.deinit(allocator);
        var region = span;
        for (pieces.items) |p| {
            // A piece widened over a code span reaches past the selection.
            region.start = @min(region.start, p.start);
            region.end = @max(region.end, p.end);
            const strip = if (mode == .toggle)
                self.splicer.inlineStrip(p, kindRef(kind), d.open, d.close) catch
                    return error.NotEditable
            else
                null;
            if (strip) |s| {
                region.start = @min(region.start, s.span.start);
                region.end = @max(region.end, s.span.end);
                try edits.append(allocator, .{ .span = s.span, .interior = s.interior });
            } else {
                try edits.append(allocator, .{ .span = p, .interior = null });
            }
        }

        const src = self.sourceBytes();
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);
        var cursor = region.start;
        for (edits.items) |e| {
            // Ascending and disjoint by construction — one piece per host, and
            // hosts do not overlap — but a removal grows its piece leftward, so
            // refuse rather than slice backwards if that ever stops holding.
            if (e.span.start < cursor) return error.EditConflict;
            try out.appendSlice(allocator, src[cursor..e.span.start]);
            if (e.interior) |interior| {
                try out.appendSlice(allocator, interior);
            } else {
                try out.appendSlice(allocator, d.open);
                try out.appendSlice(allocator, src[e.span.start..e.span.end]);
                try out.appendSlice(allocator, d.close);
            }
            cursor = e.span.end;
        }
        try out.appendSlice(allocator, src[cursor..region.end]);

        return self.commitSplice(region.start, region.end, out.items);
    }

    /// Set — or clear — the COLOUR of the `mark` the caret at `offset` is in:
    /// the second half of an authorable highlight, and the only gesture that
    /// writes a mark's colour rather than its delimiters.
    ///
    /// `color` is a name from `Syntax.MarkColors.colors` (`"red"`), or `null`
    /// to leave the highlight uncoloured. Setting one on a mark that has none
    /// inserts the prefix, setting one on a mark that has another REPLACES it,
    /// and clearing removes it — so the gesture is total over the palette plus
    /// "no colour", the way a colour picker asks it.
    ///
    /// ── Why this is not part of `toggleInline` ─────────────────────────────
    /// A colour is a property of a mark that already exists, and every gesture
    /// here is one splice: giving the toggle a colour parameter would make
    /// "highlight this" and "recolour that" the same call with two different
    /// answers to "what if there is no mark?". Authoring a coloured highlight
    /// from scratch is the two gestures in order — `toggleInline(.mark)`, then
    /// this at any offset inside the new mark, which is `start + open.len` for
    /// a range wrapped at `start`.
    ///
    /// ── What "the prefix" is ───────────────────────────────────────────────
    /// The bytes between the mark's opening delimiter and its content span,
    /// read from the SOURCE rather than from the node's attributes. That keeps
    /// the edit honest about what it is overwriting: a mark whose interior
    /// starts right after its opener has no prefix to replace, one whose
    /// interior starts later has exactly those bytes, and anything there that
    /// the palette does not spell is `error.NotEditable` rather than a splice
    /// that eats a character of the author's text.
    ///
    /// An existing prefix keeps its own SPACING — `==🔴text==` recolours tight,
    /// `==🔴 text==` recolours spaced — because that is spelling the author
    /// chose and the parser preserves. A prefix being written for the first
    /// time gets `MarkColors.space`.
    ///
    /// `error.UnsupportedFormat` where the format (or the parse config behind
    /// this editor — see `format.zig`'s `syntaxForConfig`) spells no colour,
    /// `error.InvalidColor` for a name outside the palette, `error.NotEditable`
    /// when the caret is not inside a mark. Clearing a colour a mark does not
    /// have is a no-op that reports success.
    pub fn setMarkColor(self: *Editor, offset: usize, color: ?[]const u8) Error!void {
        const mc = self.syntax.mark_colors orelse return error.UnsupportedFormat;
        // The mark's own delimiters, which the prefix sits behind. Pinned
        // non-null beside `mark_colors` by `Syntax.assertCoherent`.
        const d = self.syntax.delimsFor(.{ .mark = .mark }).?;
        const new_prefix: ?[]const u8 = if (color) |name|
            (mc.prefixFor(name) orelse return error.InvalidColor)
        else
            null;

        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const doc = &self.splicer.doc;
        const allocator = self.splicer.allocator;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.caretChain(allocator, doc, offset, &chain);

        // The innermost `mark`, so a highlight nested in emphasis (or in
        // another mark) recolours the one the caret is actually in.
        var found: ?AST.Node.Id = null;
        var i = chain.items.len;
        while (i > 0) {
            i -= 1;
            const kind = doc.ast.nodes[chain.items[i]].kind;
            if (kind == .inline_mark and kind.inline_mark == .mark) {
                found = chain.items[i];
                break;
            }
        }
        const id = found orelse return error.NotEditable;

        const node_span = doc.span(id);
        const cs = doc.contentSpan(id) orelse return error.NotEditable;
        const prefix_start = node_span.start + d.open.len;
        if (prefix_start > cs.start or cs.start > src.len) return error.NotEditable;

        // What is there now, and whether this editor recognizes it. A prefix it
        // cannot read is text as far as it knows, and text is not ours to
        // overwrite.
        const existing = src[prefix_start..cs.start];
        var spaced = false;
        if (existing.len > 0) {
            const c = mc.prefixAt(existing) orelse return error.NotEditable;
            const rest = existing[c.prefix.len..];
            if (std.mem.eql(u8, rest, mc.space)) {
                spaced = true;
            } else if (rest.len != 0) return error.NotEditable;
        }

        const prefix = new_prefix orelse
            // Clearing: the prefix goes, and the space it absorbed goes with it
            // — it was part of the spelling, not of the text.
            return if (existing.len == 0) {} else self.commitSplice(prefix_start, cs.start, "");

        // A first colour is written spaced; a replacement keeps the spacing the
        // source already had.
        const write_space = if (existing.len == 0) true else spaced;
        const buf = try allocator.alloc(u8, prefix.len + if (write_space) mc.space.len else 0);
        defer allocator.free(buf);
        @memcpy(buf[0..prefix.len], prefix);
        if (write_space) @memcpy(buf[prefix.len..], mc.space);
        return self.commitSplice(prefix_start, cs.start, buf);
    }

    // ── Block kind ─────────────────────────────────────────────────────────

    /// Convert the block at `offset` to `kind` (a `level`-N heading, or a
    /// paragraph) by rewriting its leading marker while keeping its inline
    /// content verbatim — the block half of the toolbar (H1 / Body).
    ///
    /// ON A BLANK LINE this OPENS the block instead of converting one, so
    /// "H2, then type" works from an empty line the way it works from a full
    /// one. There is no node there to rewrite — no format spells an empty
    /// paragraph, which is the same gap `splitBlock` documents — so a caller
    /// that could only convert an existing block had to spell `#` itself, and
    /// spell it per format. See `openBlockOnBlankLine`.
    ///
    /// TWO SPELLINGS, tried in this order. Where the format has a
    /// `heading_marker` the block's leading marker is rewritten and its inline
    /// bytes are kept verbatim — a `*em*` stays `*em*`. Where it has none but
    /// carries a `renderBlock`, there is no marker to rewrite (HTML's heading is
    /// a tag PAIR, level in both ends), so the block's inline children are put
    /// under a fresh node of the requested kind and the format prints it — the
    /// same tree, re-spelled, which is why a marker is preferred when there is
    /// one. A format with neither is `error.UnsupportedFormat`.
    pub fn setBlock(self: *Editor, offset: usize, kind: BlockKind, level: u32) Error!void {
        if (self.syntax.heading_marker) |marker| return self.setBlockByMarker(offset, kind, level, marker);
        if (self.syntax.renderBlock) |render| return self.setBlockByRender(offset, kind, level, render);
        return error.UnsupportedFormat;
    }

    /// `setBlock` over a format with a leading heading marker: rewrite it.
    fn setBlockByMarker(self: *Editor, offset: usize, kind: BlockKind, level: u32, marker: u8) Error!void {
        if (kind == .heading and (level < 1 or level > 6)) return error.InvalidLevel;

        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const block = locate.innermostBlock(&self.splicer.doc, offset) orelse
            return self.openBlockOnBlankLine(offset, kind, level, marker);
        const cs = self.splicer.doc.contentSpan(block) orelse return error.NotEditable;
        const content = src[cs.start..cs.end];

        // Rewrite [the block's own marker, end-of-text): the leading marker
        // (a heading) or nothing (a paragraph), plus the text — but NOT any
        // trailing newline the block span includes (Djot blocks do), so we don't
        // fuse with the next block. Rebuilding from `content_span` also
        // collapses a setext heading's underline line away for free.
        //
        // Not from the block span's start: Markdown's span for a block in a
        // quote begins at the `> ` its line carries, which is the quote's and
        // not the block's to lose. The rewrite starts at the content, or at a
        // marker in front of it on the same line.
        const block_span = self.splicer.doc.span(block);
        var start = cs.start;
        if (self.splicer.doc.markerSpan(block)) |m| {
            if (m.end <= cs.start and m.start >= locate.lineStartAt(src, cs.start)) start = m.start;
        }
        var end = block_span.end;
        if (end > block_span.start and src[end - 1] == '\n') end -= 1;
        if (end > block_span.start and src[end - 1] == '\r') end -= 1;

        const allocator = self.splicer.allocator;
        const prefix_len: usize = if (kind == .heading) level + 1 else 0; // marker*level + " "
        const buf = try allocator.alloc(u8, prefix_len + content.len);
        defer allocator.free(buf);
        if (kind == .heading) {
            @memset(buf[0..level], marker);
            buf[level] = ' ';
        }
        @memcpy(buf[prefix_len..], content);

        return self.commitSplice(start, end, buf);
    }

    /// `setBlock` over a format with no leading marker but a fragment
    /// renderer: BUILD the block and let the format print it.
    ///
    /// The block's inline children are cloned under a new `heading`/`para`
    /// node — its attributes ride along, so `<p class="lead">` becomes
    /// `<h2 class="lead">` — and the rendered fragment replaces the block's
    /// whole span. The renderer's trailing line end is dropped because a block
    /// span excludes its own; the reparse is the backstop, as everywhere.
    fn setBlockByRender(
        self: *Editor,
        offset: usize,
        kind: BlockKind,
        level: u32,
        render: RenderBlockFn,
    ) Error!void {
        if (kind == .heading and (level < 1 or level > 6)) return error.InvalidLevel;

        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const doc = &self.splicer.doc;
        const block = locate.innermostBlock(doc, offset) orelse
            return self.openBlockByRender(offset, kind, level, render);

        const allocator = self.splicer.allocator;
        var b = AST.Builder.init(allocator);
        defer b.deinit();
        var kids: std.ArrayList(AST.Node.Id) = .empty;
        defer kids.deinit(allocator);
        var child = doc.ast.nodes[block].first_child;
        while (child) |c| : (child = doc.ast.nodes[c].next_sibling) {
            try kids.append(allocator, try b.graftSubtree(&doc.ast, c));
        }
        const root = try b.addContainer(blockNodeKind(kind, level), kids.items);
        if (doc.ast.nodes[block].attrs) |ai| try b.setAttrs(root, doc.ast.attrs[ai]);

        const block_span = doc.span(block);
        return self.spliceRendered(&b, root, render, block_span.start, block_span.end);
    }

    /// `setBlockByRender` on a BLANK LINE: open an EMPTY block of the requested
    /// kind there. The same guard as `openBlockOnBlankLine` — a blank line
    /// interior to a leaf (a code body, a table) is `error.NotEditable`, and
    /// `.paragraph` is a no-op — but no quote markers to carry and no blank
    /// separation to write: a format that spells a heading as a fragment does
    /// not read line prefixes, so the fragment stands wherever it is spliced.
    fn openBlockByRender(
        self: *Editor,
        offset: usize,
        kind: BlockKind,
        level: u32,
        render: RenderBlockFn,
    ) Error!void {
        const line = try self.blankLineBetweenBlocks(offset);
        if (kind == .paragraph) return;

        var b = AST.Builder.init(self.splicer.allocator);
        defer b.deinit();
        const root = try b.addContainer(blockNodeKind(kind, level), &.{});
        return self.spliceRendered(&b, root, render, line.start, line.end);
    }

    /// Print `root` of `b` through `render` and splice it over `[start, end)`,
    /// minus the line end a block renderer writes after its block — the span
    /// being replaced excludes its own.
    fn spliceRendered(
        self: *Editor,
        b: *const AST.Builder,
        root: AST.Node.Id,
        render: RenderBlockFn,
        start: usize,
        end: usize,
    ) Error!void {
        const view = b.view(root);
        var out: Writer.Allocating = .init(self.splicer.allocator);
        defer out.deinit();
        try renderNode(self.splicer.allocator, render, &view, root, &out.writer);
        const rendered = std.mem.trimEnd(u8, out.written(), "\r\n");
        return self.commitSplice(start, end, rendered);
    }

    /// One node through `render`, with the renderer's errors folded into the
    /// editor's: a refusal to print is a content refusal, not a format one,
    /// since the renderer exists.
    fn renderNode(allocator: Allocator, render: RenderBlockFn, ast: *const AST, id: AST.Node.Id, out: *Writer) Error!void {
        render(allocator, ast, id, out) catch |err| switch (err) {
            error.OutOfMemory, error.WriteFailed => return error.OutOfMemory,
            else => return error.NotEditable,
        };
    }

    /// The line the caret is on, when it is a BLANK line lying BETWEEN a
    /// container's children — the place `setBlock` may open a block. The span
    /// is the whole line body, trailing spaces included, so a splice over it
    /// takes them along. `error.NotEditable` for a line with content, or for a
    /// blank line INTERIOR to a leaf; `openBlockOnBlankLine` below makes the
    /// same two tests, with a line prefix in the way, and says why
    /// `locate.isBlockParent` is the hinge.
    fn blankLineBetweenBlocks(self: *const Editor, offset: usize) Error!Span {
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const line_start = locate.lineStartAt(src, offset);
        const body = locate.lineBody(src[line_start..locate.lineEndAt(src, offset)]);
        if (!locate.isBlankLine(body)) return error.NotEditable;
        if (locate.lineOwningBlock(doc, offset)) |lb| {
            if (!locate.isBlockParent(doc.ast.nodes[lb.block].kind)) return error.NotEditable;
        }
        return Span.init(line_start, line_start + body.len);
    }

    /// `setBlock` where there is no block to convert: the caret sits on a BLANK
    /// LINE, so the marker is OPENED rather than rewritten.
    ///
    /// The line's own quote markers are kept and the heading marker written
    /// after them, so an H2 asked for on a quote's blank line lands inside the
    /// quote rather than ending it. They are re-emitted with a SPACE after the
    /// last `>` even when the blank line carries none, because a blank quoted
    /// line is spelled `>` and `>#` is not a quoted heading in both formats:
    /// Markdown reads it as one, djot reads the whole line as a paragraph. The
    /// space is what makes one spelling work in both, the same argument
    /// `Syntax.thematic_break` makes for blank-separating a rule.
    ///
    /// It is BLANK-SEPARATED from whatever precedes it, and that is load-bearing
    /// rather than cosmetic: djot does not let a heading interrupt a paragraph,
    /// so a `## ` written on the line directly under one is read there as the
    /// paragraph's own text — the document gains no heading and the marker shows
    /// up as literal `##`. Markdown reads the same bytes as a heading. Emitting
    /// the blank when the line above is non-blank is what makes one spelling
    /// work in both, the same argument `insertThematicBreak` makes for a rule
    /// and `Syntax.thematic_break` records for `---`.
    ///
    /// The blank carries the line's quote markers, minus the space after them —
    /// a quote's blank line is spelled `>` — so a heading opened on a quote's
    /// blank line stays inside the quote instead of ending it.
    ///
    /// `error.NotEditable` when the blank line is INTERIOR to a block rather
    /// than between blocks — a blank line in a fenced code block, or in a table.
    /// `locate.isBlockParent` is the hinge: a line owned by a container (the
    /// document, a quote, a list item) is a gap between that container's
    /// children and a block may open there, while a line owned by anything else
    /// is inside a leaf whose bytes mean something already. Writing `## ` into a
    /// code body would add no heading and corrupt the listing.
    ///
    /// `.paragraph` is a NO-OP rather than an error: a blank line already holds
    /// no block marker, so the state the caller asked for is the state it is in.
    fn openBlockOnBlankLine(
        self: *Editor,
        offset: usize,
        kind: BlockKind,
        level: u32,
        marker: u8,
    ) Error!void {
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const line_start = locate.lineStartAt(src, offset);
        const body = locate.lineBody(src[line_start..locate.lineEndAt(src, offset)]);

        // Past the line's own quote markers; what remains must be blank, or this
        // is a line with content that simply isn't a `para`/`heading` — a fence
        // line, a table row — and there is nothing here to open.
        var i: usize = 0;
        while (skipQuoteMarker(body, i)) |j| i = j;
        if (!locate.isBlankLine(body[i..])) return error.NotEditable;

        // Interior to a leaf (a code block's body, a table) rather than between
        // a container's children. `innermostBlock` reports `null` for both, and
        // only this tells them apart.
        if (locate.lineOwningBlock(doc, offset)) |lb| {
            if (!locate.isBlockParent(doc.ast.nodes[lb.block].kind)) return error.NotEditable;
        }

        if (kind == .paragraph) return;

        const allocator = self.splicer.allocator;
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // The line's quote markers, re-emitted with the space djot needs after
        // the last `>` even when the blank line carries none.
        const prefix = body[0..i];
        const needs_space = i > 0 and body[i - 1] == '>';

        // A blank line above, when the previous line has content — see above for
        // why that is correctness rather than tidiness. Inside a quote the blank
        // carries the marker WITHOUT its trailing space, which is how a quote
        // spells a blank line.
        if (precedingLineHasContent(src, line_start)) {
            try out.appendSlice(allocator, std.mem.trimEnd(u8, prefix, " "));
            try out.append(allocator, '\n');
        }

        try out.appendSlice(allocator, prefix);
        if (needs_space) try out.append(allocator, ' ');
        try out.appendNTimes(allocator, marker, level);
        try out.append(allocator, ' ');

        // The whole line body, so a blank line's trailing spaces go with it.
        return self.commitSplice(line_start, line_start + body.len, out.items);
    }

    // ── Block containers (quote / lists) ───────────────────────────────────
    // `setBlock` rewrites the leading marker of ONE block at one offset. A block
    // container is a different animal: it prefixes EVERY line of a possibly
    // multi-block range, it nests, and a list numbers its items — so it gets its
    // own gesture rather than another `BlockKind`. Everything below is line
    // surgery over the covered blocks, spliced in one shot.

    /// Toggle a block container over the blocks `[start, end)` covers.
    ///
    /// The already-in-container test walks the AST ancestors of `start` for a
    /// container of `kind`, and the toggle turns OFF only when the range covers
    /// every block that container holds — otherwise it turns ON, which is what
    /// makes a partial selection inside a quote nest (`> >`) instead of dragging
    /// the container's uncovered siblings out with it. Toggling a list kind
    /// while inside the other list kind converts in place rather than nesting.
    ///
    /// ON A BLANK LINE this OPENS an empty container instead of wrapping one,
    /// `setBlock`'s rule for the same position — see `openContainerOnBlankLine`.
    ///
    /// TWO SPELLINGS, in `setBlock`'s order. Where the format has a
    /// `ContainerSpelling` every line of the range is prefixed and the covered
    /// bytes are kept verbatim. Where it has none but carries a `renderBlock`,
    /// the container is a wrapping pair (`<blockquote>`, `<ul>` with an `<li>`
    /// per item) and the covered blocks are put under a fresh node the format
    /// prints — see `toggleBlockContainerByRender`. A format with neither is
    /// `error.UnsupportedFormat`.
    pub fn toggleBlockContainer(self: *Editor, span: Span, kind: ContainerKind) Error!void {
        try self.checkRange(span.start, span.end);
        const sp = self.syntax.container_spelling.get(kind) orelse {
            if (self.syntax.renderBlock) |render| return self.toggleBlockContainerByRender(span, kind, render);
            return error.UnsupportedFormat;
        };

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const ast = self.astView();

        const blocks = coveredBlocks(allocator, &self.splicer.doc, span.start, span.end) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            // Nothing to wrap: the caret is on a BLANK LINE, where the gesture
            // opens an empty container rather than failing.
            else => return self.openContainerOnBlankLine(span.start, kind, sp),
        };
        defer allocator.free(blocks.chain);

        const region_start = locate.lineStartAt(src, self.splicer.doc.span(blocks.first).start);
        const region_end = locate.lineEndAt(src, self.splicer.doc.span(blocks.last).end -| 1);

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // The toggle-off / convert / nest decision, all from the ancestor chain.
        if (locate.innermostOfKind(&self.splicer.doc, blocks.chain, kindTag(kind))) |target| {
            if (containerFullyCovered(&self.splicer.doc, target, region_start, region_end)) {
                const t = self.splicer.doc.span(target);
                // The container's own lines, not the range's: its span can reach
                // past the last covered block (a quote's trailing `>` line).
                const splice_start = locate.lineStartAt(src, t.start);
                const splice_end = locate.lineEndAt(src, t.end -| 1);
                switch (kind) {
                    .block_quote => try buildQuoteStrip(
                        allocator,
                        src,
                        splice_start,
                        splice_end,
                        quoteDepthAbove(ast, blocks.chain, target),
                        &out,
                    ),
                    .bullet_list, .ordered_list => try buildListRewrite(
                        allocator,
                        src,
                        &self.splicer.doc,
                        target,
                        splice_start,
                        splice_end,
                        null,
                        &out,
                    ),
                }
                return self.commitSplice(splice_start, splice_end, out.items);
            }
        }
        if (kind == .bullet_list or kind == .ordered_list) {
            const other: Splicer.KindTag = if (kind == .bullet_list) .ordered_list else .bullet_list;
            if (locate.innermostOfKind(&self.splicer.doc, blocks.chain, other)) |target| {
                if (containerFullyCovered(&self.splicer.doc, target, region_start, region_end)) {
                    const t = self.splicer.doc.span(target);
                    const splice_start = locate.lineStartAt(src, t.start);
                    const splice_end = locate.lineEndAt(src, t.end -| 1);
                    try buildListRewrite(allocator, src, &self.splicer.doc, target, splice_start, splice_end, sp, &out);
                    return self.commitSplice(splice_start, splice_end, out.items);
                }
            }
        }

        try buildContainerAdd(allocator, src, &self.splicer.doc, blocks, region_start, region_end, sp, &out);
        return self.commitSplice(region_start, region_end, out.items);
    }

    /// `toggleBlockContainer` where there is no block to wrap: the caret sits on
    /// a BLANK LINE, so the container's marker is OPENED on it and the author
    /// types into it — "bullet, then type", which is how a list most often
    /// starts.
    ///
    /// The twin of `openBlockOnBlankLine`, and it has to be: `setBlock` opening
    /// `# ` on a blank line while the quote and list gestures answered
    /// `error.NoBlock` meant a toolbar's H1 button worked on an empty line and
    /// its Quote / Bulleted / Numbered buttons were silent no-ops beside it.
    /// Everything that function reasons about applies unchanged here, so the
    /// shape is deliberately the same one:
    ///
    /// The line's own quote markers are kept and the container's marker written
    /// after them, so a bullet asked for on a quote's blank line lands inside
    /// the quote rather than ending it — re-emitted with a space after the last
    /// `>` even when the blank line carries none, since a blank quoted line is
    /// spelled `>` and `>-` is not a quoted bullet.
    ///
    /// It is BLANK-SEPARATED from whatever precedes it, and here that is
    /// load-bearing in BOTH formats rather than djot alone: an empty list item
    /// cannot interrupt a paragraph, so `- ` written on the line directly under
    /// one is read as that paragraph's own text and the document gains no list
    /// at all. (`> ` can interrupt, so a quote would survive without the blank —
    /// it gets one anyway, because a rule that holds for one of three buttons is
    /// a rule nobody can remember.)
    ///
    /// One thing `openBlockOnBlankLine` needs and this does NOT: a guard against
    /// the blank line being INTERIOR to a leaf (a fenced code block's body, a
    /// table). That function is reached whenever `locate.innermostBlock` finds
    /// nothing, and that is a narrow question — `para`/`heading`, all `setBlock`
    /// rewrites markers for — so a code block's interior lands there and has to
    /// be turned away. This is reached only when `coveredBlocks` finds nothing,
    /// and that is a broad one: a caret anywhere inside a code block resolves to
    /// the code block, which the gesture then wraps whole (`- ```…` over every
    /// line, listing intact). By the time control arrives here the offset is in
    /// no leaf at all, so there is nothing left to refuse.
    ///
    /// It TOGGLES, which is the whole gesture's name and not a bonus: a line
    /// already holding an empty container of `kind` and nothing else has that
    /// marker taken back off, because the press that made it has to un-make it.
    /// Without this, Quote pressed twice on a blank line nested `> > ` and
    /// Bulleted pressed twice failed outright — a button that cannot be
    /// un-pressed until the author types something into it. An empty marker of
    /// the OTHER list kind is CONVERTED, matching what the non-empty path does
    /// for a real list.
    ///
    /// `error.NotEditable` when the line is neither blank nor exactly one empty
    /// marker — content in no block, which no container edit fits.
    fn openContainerOnBlankLine(self: *Editor, offset: usize, kind: ContainerKind, sp: ContainerSpelling) Error!void {
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const line_start = locate.lineStartAt(src, offset);
        const body = locate.lineBody(src[line_start..locate.lineEndAt(src, offset)]);

        // The line's quote markers, and where the INNERMOST one began — the one
        // a Quote press takes back off.
        var quotes_end: usize = 0;
        var last_quote: usize = 0;
        while (skipQuoteMarker(body, quotes_end)) |j| {
            last_quote = quotes_end;
            quotes_end = j;
        }

        const marker = listMarkerAt(body, quotes_end);
        const empty_list = if (marker) |m| locate.isBlankLine(body[m.end..]) else false;
        const blank = locate.isBlankLine(body[quotes_end..]);
        if (!blank and !empty_list) return error.NotEditable;

        // `keep` is the prefix that survives the edit, `write` what follows it.
        // An empty `write` is the toggle-off direction.
        var keep: usize = if (empty_list) marker.?.start else quotes_end;
        var num_buf: [24]u8 = undefined;
        const write: []const u8 = if (kind == .block_quote) blk: {
            // A quote's own marker is what `skipQuoteMarker` has already eaten,
            // so "an empty quote" is a line that is blank once they are all
            // gone. Drop the innermost rather than nesting a second.
            if (blank and quotes_end > 0) {
                keep = last_quote;
                break :blk "";
            }
            break :blk sp.marker;
        } else if (empty_list and isOrderedMarker(body[marker.?.start]) == sp.numbered)
            ""
        else
            listMarker(sp, 1, &num_buf);

        const allocator = self.splicer.allocator;
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // A blank line above, when the previous line has content — see above for
        // why that is correctness rather than tidiness. Only when OPENING on a
        // blank line: rewriting a marker that is already there needs no
        // separation it does not already have, and adding one would make an
        // enclosing list loose.
        if (write.len > 0 and blank and precedingLineHasContent(src, line_start)) {
            try out.appendSlice(allocator, std.mem.trimEnd(u8, body[0..keep], " "));
            try out.append(allocator, '\n');
        }

        try out.appendSlice(allocator, body[0..keep]);
        // The space djot needs after the last `>` even when the blank line
        // carries none: `>-` is not a quoted bullet in either format.
        if (write.len > 0 and keep > 0 and body[keep - 1] == '>') try out.append(allocator, ' ');
        try out.appendSlice(allocator, write);

        // Toggling off can leave the prefix's own trailing space stranded on an
        // otherwise empty line; a quote's blank line is spelled `>`.
        const tidy = if (write.len == 0) std.mem.trimEnd(u8, out.items, " ") else out.items;

        // The whole line body, so a blank line's trailing spaces go with it.
        return self.commitSplice(line_start, line_start + body.len, tidy);
    }

    /// `toggleBlockContainer` over a format with no line prefix but a
    /// fragment renderer: BUILD the container and let the format print it.
    ///
    /// The same three decisions as the marker path, taken from the tree
    /// rather than from lines — there are no lines to reason about in
    /// `<ul><li>a</li><li>b</li></ul>`, and the parser has already said what
    /// nests in what:
    ///
    ///   * TOGGLE OFF when the range reaches every block a container of `kind`
    ///     holds (`coversContainer`): the container's children — for a list,
    ///     each item's children — are printed in its place. The blocks print
    ///     from the document's own tree, so a paragraph that was a tight
    ///     item's elided `<p>` comes out as a `<p>` again.
    ///   * CONVERT when the range covers the OTHER list kind whole: its items
    ///     are grafted under a fresh list of the requested kind, tightness
    ///     kept, numbering start dropped.
    ///   * WRAP otherwise: the covered blocks go under a `block_quote`, or one
    ///     `list_item` each under a list — TIGHT when every block is a
    ///     paragraph, which is the list the marker path's `- a\n- b` parses
    ///     to. A partial selection inside a container nests, as it does there.
    ///
    /// The splice replaces the covered blocks' own spans (toggle-off and
    /// convert: the container's), minus the line end a block span may carry
    /// after its closing tag, so the fragment lands where the blocks were and
    /// the line structure around them is untouched.
    fn toggleBlockContainerByRender(
        self: *Editor,
        span: Span,
        kind: ContainerKind,
        render: RenderBlockFn,
    ) Error!void {
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const ast = self.astView();

        const blocks = coveredBlocks(allocator, doc, span.start, span.end) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            // Nothing to wrap: a blank line, or an empty container — see there.
            else => return self.openContainerByRender(span.start, kind, render),
        };
        defer allocator.free(blocks.chain);

        if (locate.innermostOfKind(doc, blocks.chain, kindTag(kind))) |target| {
            if (coversContainer(ast, target, blocks)) return self.unwrapContainer(target, render);
        }
        if (kind != .block_quote) {
            const other: Splicer.KindTag = if (kind == .bullet_list) .ordered_list else .bullet_list;
            if (locate.innermostOfKind(doc, blocks.chain, other)) |target| {
                if (coversContainer(ast, target, blocks)) {
                    var b = AST.Builder.init(allocator);
                    defer b.deinit();
                    var items: std.ArrayList(AST.Node.Id) = .empty;
                    defer items.deinit(allocator);
                    var item = ast.nodes[target].first_child;
                    while (item) |i| : (item = ast.nodes[i].next_sibling) {
                        try items.append(allocator, try b.graftSubtree(ast, i));
                    }
                    const tight = switch (ast.nodes[target].kind) {
                        .bullet_list => |v| v.tight,
                        .ordered_list => |v| v.tight,
                        else => unreachable,
                    };
                    const root = try b.addContainer(containerNodeKind(kind, tight), items.items);
                    if (ast.nodes[target].attrs) |ai| try b.setAttrs(root, ast.attrs[ai]);
                    const t = doc.span(target);
                    return self.spliceRendered(&b, root, render, t.start, blockSpanEnd(src, t));
                }
            }
        }

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        var kids: std.ArrayList(AST.Node.Id) = .empty;
        defer kids.deinit(allocator);
        var all_paras = true;
        var cur: ?AST.Node.Id = blocks.first;
        while (cur) |c| : (cur = if (c == blocks.last) null else ast.nodes[c].next_sibling) {
            if (ast.nodes[c].kind != .para) all_paras = false;
            const block = try b.graftSubtree(ast, c);
            try kids.append(allocator, if (kind == .block_quote) block else try b.addContainer(.list_item, &.{block}));
        }
        const root = try b.addContainer(containerNodeKind(kind, all_paras), kids.items);
        const first = doc.span(blocks.first);
        const last = doc.span(blocks.last);
        return self.spliceRendered(&b, root, render, first.start, blockSpanEnd(src, last));
    }

    /// `toggleBlockContainerByRender` where `coveredBlocks` found nothing: the
    /// caret is on a BLANK LINE between blocks, or inside an EMPTY container.
    ///
    /// On a blank line an empty container of `kind` is OPENED — over an empty
    /// paragraph, because that is the shape a wrapped paragraph has, so the
    /// press that made it un-makes it through the ordinary toggle-off above
    /// and the author has a block to type into. `blankLineBetweenBlocks` makes
    /// `openBlockByRender`'s guard: a blank line interior to a leaf is
    /// `error.NotEditable`.
    ///
    /// Inside an empty container of `kind` — `<blockquote></blockquote>`, or
    /// an `<li></li>` that is the only item of a list of `kind`, neither of
    /// which this gesture writes but both of which a document may hold — the
    /// container is REMOVED, which is what a toggle owes the press that finds
    /// it. Anything else the caret can be in with no block around it is
    /// `error.NotEditable`.
    fn openContainerByRender(self: *Editor, offset: usize, kind: ContainerKind, render: RenderBlockFn) Error!void {
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;
        const ast = self.astView();

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, offset, &chain);
        if (emptyContainerOf(ast, chain.items, kind)) |empty| {
            const t = doc.span(empty);
            return self.commitSplice(t.start, blockSpanEnd(src, t), "");
        }

        const line = try self.blankLineBetweenBlocks(offset);
        var b = AST.Builder.init(allocator);
        defer b.deinit();
        const para = try b.addContainer(.para, &.{});
        const root = switch (kind) {
            .block_quote => try b.addContainer(.block_quote, &.{para}),
            // Loose, so the empty paragraph is printed as a `<p>` the author can
            // type into rather than elided to nothing.
            .bullet_list, .ordered_list => try b.addContainer(
                containerNodeKind(kind, false),
                &.{try b.addContainer(.list_item, &.{para})},
            ),
        };
        return self.spliceRendered(&b, root, render, line.start, line.end);
    }

    /// Print `target`'s blocks — for a list, each item's blocks — from the
    /// document's own tree and splice them over the container: the render
    /// path's toggle-off.
    fn unwrapContainer(self: *Editor, target: AST.Node.Id, render: RenderBlockFn) Error!void {
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const ast = self.astView();
        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        const is_list = std.meta.activeTag(ast.nodes[target].kind) != .block_quote;
        var child = ast.nodes[target].first_child;
        while (child) |c| : (child = ast.nodes[c].next_sibling) {
            if (is_list) {
                var block = ast.nodes[c].first_child;
                while (block) |bl| : (block = ast.nodes[bl].next_sibling) {
                    try renderNode(allocator, render, ast, bl, &out.writer);
                }
            } else {
                try renderNode(allocator, render, ast, c, &out.writer);
            }
        }
        const t = doc.span(target);
        return self.commitSplice(t.start, blockSpanEnd(src, t), std.mem.trimEnd(u8, out.written(), "\r\n"));
    }

    /// Renumber the ordered list at `offset` so its markers run `1, 2, 3, …`,
    /// with each nesting level restarting at 1 — the numbering a caret editor
    /// keeps as items are inserted, deleted, and nested, where a plain splice
    /// leaves the source numbers stale (`1. 2. 2. 3.`). A no-op that returns
    /// `error.NoBlock` when `offset` is not inside an ordered list.
    ///
    /// Which lines ARE items comes from the tree; only their LEVEL comes from
    /// indentation. The level is one left-to-right pass with a small stack of
    /// (indent column → next number), so a sub-list restarts and its parent
    /// resumes where it left off — indentation, because a nested list's depth is
    /// what the marker's column says it is, in both formats and at any width.
    /// But whether a `N.`-looking line is an item at all is a question only the
    /// parser can answer: Djot does not let a list marker interrupt a paragraph,
    /// so in `1. a\n   2. b` the second line is not a nested item but literal
    /// text inside item `a`'s paragraph — and a purely textual pass rewrote the
    /// author's own digit there. Markdown reads the same bytes as a sub-list.
    ///
    /// Only the numeric run of a `N.` / `N)` marker is rewritten; its delimiter,
    /// spacing, indentation, and every other (bullet, prose, continuation, blank)
    /// line are copied byte-for-byte.
    ///
    /// `error.UnsupportedFormat` where the format doesn't spell an ordered item
    /// as a numbered line marker (see `spellsOrderedMarkers`). An HTML `<ol>`
    /// parses into the same `ordered_list`/`list_item` nodes a Markdown one
    /// does, so the pass used to run over it, find no `N.` to rewrite, and
    /// report the silent no-op as a success.
    pub fn renumberOrderedLists(self: *Editor, offset: usize) Error!void {
        if (!spellsOrderedMarkers(self.syntax)) return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const ast = self.astView();
        const allocator = self.splicer.allocator;

        // The OUTERMOST ordered list on the descent to `offset`: renumber the
        // whole nest under it in one pass so its levels stay consistent.
        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        locate.ancestorChain(allocator, &self.splicer.doc, offset, &chain) catch
            return error.OutOfMemory;
        var outer: ?AST.Node.Id = null;
        for (chain.items) |id| {
            if (std.meta.activeTag(ast.nodes[id].kind) == .ordered_list) {
                outer = id;
                break;
            }
        }
        const list = outer orelse return error.NoBlock;

        const region_start = locate.lineStartAt(src, self.splicer.doc.span(list).start);
        const region_end = locate.lineEndAt(src, self.splicer.doc.span(list).end -| 1);

        // The line each item in the region OPENS on, ascending. An item's span
        // may start at its marker (Markdown) or at its text (Djot), but either
        // way it starts on the marker's own line, so the line start identifies it.
        var item_lines: std.ArrayList(usize) = .empty;
        defer item_lines.deinit(allocator);
        for (ast.nodes, 0..) |n, i| {
            switch (std.meta.activeTag(n.kind)) {
                .list_item, .task_list_item => {},
                else => continue,
            }
            const start = self.splicer.doc.span(@intCast(i)).start;
            if (start < region_start or start >= region_end) continue;
            try item_lines.append(allocator, locate.lineStartAt(src, start));
        }
        std.mem.sort(usize, item_lines.items, {}, std.sort.asc(usize));

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);
        try buildRenumber(allocator, src, region_start, region_end, item_lines.items, &out);

        // Identical bytes: don't spend an edit (and an undo step) on a no-op.
        if (std.mem.eql(u8, out.items, src[region_start..region_end])) return;
        return self.commitSplice(region_start, region_end, out.items);
    }

    // ── Thematic break ───────────────────────────────────────────────────────

    /// Insert a thematic break — a horizontal rule — as its own block, on the
    /// line after the block `offset` sits in.
    ///
    /// Decisions:
    ///   * It goes AFTER the caret's block rather than AT the caret. A rule is a
    ///     block, not an inline, so there is no spelling for one in the middle of
    ///     a paragraph; splitting the paragraph in two would be a different
    ///     gesture the caller didn't ask for.
    ///   * It is BLANK-SEPARATED from its neighbours unconditionally, and that is
    ///     load-bearing rather than cosmetic: Markdown reads `---` on the line
    ///     directly under a paragraph as a setext `<h2>` underline, so a rule
    ///     written flush against its predecessor silently becomes a heading and
    ///     eats it. The blank above is what makes one spelling safe in both
    ///     formats. Neither blank is added where the line is already one — the
    ///     one below so repeating the gesture doesn't accumulate them, the one
    ///     above so a caret ON a blank line between two blocks gets a rule with
    ///     one blank each side rather than two above (`a\n\n|\nb` used to give
    ///     `a\n\n\n---\n\nb`). A blank line is a separator, and one is all a
    ///     separator is; the gesture writes what is missing and nothing more.
    ///   * It inherits the caret block's QUOTE PREFIX, so a rule inside a quote
    ///     stays inside it (`> a` gains `>` and `> * * *`, not a rule that ends
    ///     the quote). Only quote markers are reproduced — a list item's indent
    ///     is not — so a rule requested inside a list lands at column zero after
    ///     the caret's ITEM, which SPLITS the list in two with the rule between.
    ///     That is a real document rather than a corrupted one (nothing is
    ///     swallowed and no item loses its marker), and it is the honest reading
    ///     of a rule at column zero, so it is allowed rather than refused.
    ///   * "The caret's block" is `locate.lineOwningBlock`, NOT
    ///     `locate.innermostBlock`. The latter only knows `para`/`heading`, so a
    ///     caret in a CODE BLOCK or a TABLE looked to it like no block at all,
    ///     and the fallback below put the rule at the caret's own line end —
    ///     inside the fence (where `---` is text, so the document gained no rule
    ///     and the code body was corrupted), or between a table's header and its
    ///     delimiter row (which stops it being a table). Losing a node is a
    ///     failure worth refusing over; here it needn't be refused, because the
    ///     rule that governs every other case already says where it goes —
    ///     AFTER the caret's block, the fence and the table included.
    ///
    /// `error.UnsupportedFormat` when the format has no thematic break. There is
    /// no `error.NoBlock`: an empty document is a legitimate place for a rule, and
    /// with no block to sit after it goes at the caret's line — ON that line
    /// when it is blank, since a blank line is where a block goes, after it
    /// otherwise.
    pub fn insertThematicBreak(self: *Editor, offset: usize) Error!void {
        const rule = self.syntax.thematic_break orelse return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const allocator = self.splicer.allocator;

        const line = try std.mem.concat(allocator, u8, &.{ rule, "\n" });
        defer allocator.free(line);
        return self.insertBlockAfter(offset, line);
    }

    /// Insert a fresh table — one header row, `rows` body rows, `cols` columns,
    /// every cell empty — as its own block after the block `offset` sits in.
    ///
    /// The placement is `insertThematicBreak`'s, decision for decision: after
    /// the caret's block rather than at the caret, blank-separated on both
    /// sides, carrying a quote's prefix on every line and landing at column
    /// zero after a list item. The blank above is load-bearing here for the
    /// same reason it is for the rule: GFM lets a table's header row be read
    /// out of the paragraph it follows, so a table written flush under prose
    /// can take the paragraph's last line as its header.
    ///
    /// The bytes are the format's own `TableSpelling`, emitted by
    /// `table_edit.emit` over a `table_edit.blank` grid — the same path every
    /// table edit re-spells through, so a table this mints is one those edits
    /// can read back. The header row is not optional, because neither pipe
    /// format has a table without one; `error.InvalidShape` for zero columns
    /// or zero body rows, the shape `tableDeleteRow` and `tableDeleteColumn`
    /// refuse to leave behind. `error.UnsupportedFormat` where the format has
    /// no table spelling, before anything is read.
    pub fn insertTable(self: *Editor, offset: usize, rows: usize, cols: usize) Error!void {
        const spelling = self.syntax.table_spelling orelse return error.UnsupportedFormat;
        if (offset > self.sourceBytes().len) return error.InvalidRange;
        if (rows == 0 or cols == 0) return error.InvalidShape;
        const allocator = self.splicer.allocator;

        var grid = table_edit.blank(allocator, cols, rows) catch |e| return mapTableErr(e);
        defer grid.deinit();
        const bytes = table_edit.emit(allocator, &grid, spelling) catch |e| return mapTableErr(e);
        defer allocator.free(bytes);
        return self.insertBlockAfter(offset, bytes);
    }

    /// Insert a LEAF DIRECTIVE — Markdown's `::name[label]{attrs}` — as its own
    /// block after the block `offset` sits in.
    ///
    /// What a name MEANS is the host application's, not twig's: this writes a
    /// named `container` with `form = .block_leaf` and asks the format to
    /// spell it. A rich-text editor's page break is `insertDirective(off,
    /// "page-break", null, &.{})`, and the same call with `"embed"` and a
    /// `src` is an embed — twig has no vocabulary of directive names and reads
    /// none back.
    ///
    /// The placement is `insertThematicBreak`'s, decision for decision, shared
    /// with it and `insertTable` as `insertBlockAfter`: after the caret's
    /// block rather than at the caret, blank-separated on both sides, a
    /// quote's prefix on every line — djot's two-line fence included — and
    /// column zero after a list item.
    ///
    /// The bytes are the format's own, through the FRAGMENT RENDERER: the node
    /// is built with `AST.Builder`, `label` becomes its single inline child
    /// (which is what the `[label]` brackets print from), `attrs` are set on
    /// it, and `Syntax.renderBlock` spells the result. So the spelling is the
    /// serializer's — `::name` in Markdown, an empty `::: name` fence in djot,
    /// `<name></name>` in HTML — and stays that way when a serializer's does.
    ///
    /// Gated on `Syntax.names_leaf_containers`, which is the narrower question
    /// than "can this format print one": every format with a renderer prints
    /// SOMETHING for a named leaf, and the gate asks whether the parser hands
    /// that back as a container carrying the name. In Markdown the answer is
    /// the parse config's — `::name` is a paragraph of colons without
    /// `ParseOptions.directives` — so an editor over a document parsed without
    /// the extension gets `error.UnsupportedFormat` here, exactly as
    /// `setMarkColor` does without `highlight_colors`.
    ///
    /// `error.InvalidName` for a name outside `checkDirectiveName`'s grammar —
    /// an ASCII letter then letters, digits, `-` and `_` — which is checked
    /// here rather than per format because a name is written where a delimiter
    /// would otherwise be, and anything else reparses as something other than
    /// the container it was meant to be. `error.InvalidLabel` for a label
    /// carrying a line end or a square bracket, either of which closes the
    /// `[…]` early. `error.InvalidRange` for an `offset` past the source.
    /// There is no `error.NoBlock` — an empty document is a legitimate place
    /// for one, as it is for a rule.
    pub fn insertDirective(
        self: *Editor,
        offset: usize,
        name: []const u8,
        label: ?[]const u8,
        attrs: []const AST.KeyVal,
    ) Error!void {
        if (!self.syntax.names_leaf_containers) return error.UnsupportedFormat;
        // `assertCoherent` pins this non-null wherever the claim above is
        // made, so the `orelse` is the compiler's requirement rather than a
        // second gate; `supports` states both halves for the same reason.
        const render = self.syntax.renderBlock orelse return error.UnsupportedFormat;
        if (offset > self.sourceBytes().len) return error.InvalidRange;
        try checkDirectiveName(name);
        // A label is written inside `[…]`, so a bracket closes it early and
        // the tail becomes ordinary text beside the directive — the same
        // reasoning `insertFootnote` applies to its own reference brackets.
        if (label) |l| {
            if (std.mem.indexOfAny(u8, l, "\r\n[]") != null) return error.InvalidLabel;
        }
        const allocator = self.splicer.allocator;

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        // A label is the container's INLINE CHILDREN, which is where every
        // serializer looks for it: Markdown prints `[` … `]` around them and
        // writes nothing at all when there are none, so "no label" is an
        // absent child rather than an empty one.
        var child: [1]AST.Node.Id = undefined;
        var children: []const AST.Node.Id = child[0..0];
        if (label) |l| {
            child[0] = try b.addLeaf(.{ .str = l });
            children = child[0..1];
        }
        const root = try b.addContainer(
            .{ .container = .{ .name = name, .form = .block_leaf } },
            children,
        );
        if (attrs.len != 0) try b.setAttrs(root, .{ .entries = attrs });

        const view = b.view(root);
        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        try renderNode(allocator, render, &view, root, &out.writer);
        // No trim here, unlike `spliceRendered`: `insertBlockAfter`
        // re-terminates every line itself and trims the renderer's trailing
        // newline on the way in.
        return self.insertBlockAfter(offset, out.written());
    }

    /// Write `body` — one or more `\n`-terminated lines — as a block of its
    /// own after the block `offset` sits in: the shared placement behind
    /// `insertThematicBreak`, `insertTable`, `insertDirective` and
    /// `insertDisplayMath`, whose doc comments own the reasoning for each
    /// decision made here.
    fn insertBlockAfter(self: *Editor, offset: usize, body: []const u8) Error!void {
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(self.splicer.allocator);
        const pos = try self.blockAfter(offset, body, &out);
        return self.commitSplice(pos, pos, out.items);
    }

    /// `insertBlockAfter`'s bytes without the splice: fills `out` with what
    /// goes in and answers where it goes.
    fn blockAfter(self: *Editor, offset: usize, body: []const u8, out: *std.ArrayList(u8)) Error!usize {
        const src = self.sourceBytes();
        const allocator = self.splicer.allocator;

        const block = if (locate.lineOwningBlock(&self.splicer.doc, offset)) |lb| lb.block else null;
        // With no block to sit after, the caret's own line is the anchor: a
        // BLANK one is a separator the block can take the place of — written at
        // its start, the blank becomes the separator below — where writing after
        // it would step past a blank only to add another above.
        const line_start = locate.lineStartAt(src, offset);
        const on_blank = block == null and
            locate.isBlankLine(locate.lineBody(src[line_start..locate.lineEndAt(src, offset)]));
        const pos = if (on_blank)
            line_start
        else
            locate.lineEndAt(src, if (block) |b| self.splicer.doc.span(b).end -| 1 else offset);
        const prefix = if (block) |b| containerPrefix(src, self.splicer.doc.span(b).start) else "";
        // A quote's blank line carries its marker but not the space after it —
        // the same rule `ContainerSpelling.blank` states for the toggle.
        const blank = std.mem.trimEnd(u8, prefix, " ");

        if (pos > 0) {
            // An unterminated last line is ended first, or the "blank" written
            // next is not a blank line but that line's own newline, and the
            // block lands flush under the paragraph — `a\n---\n`, the setext
            // heading the blank exists to prevent.
            if (src[pos - 1] != '\n') try out.append(allocator, '\n');
            // The blank above, unless the line above already is one — the
            // mirror of the rule below.
            const above = src[locate.lineStartAt(src, pos - 1)..pos];
            if (!locate.isBlankLine(locate.lineBody(above))) {
                try out.appendSlice(allocator, blank);
                try out.append(allocator, '\n');
            }
        }
        // Every line of the block takes the prefix, not just its first: a
        // quote's marker is on each line it covers.
        var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, body, "\n"), '\n');
        while (lines.next()) |l| {
            try out.appendSlice(allocator, prefix);
            try out.appendSlice(allocator, l);
            try out.append(allocator, '\n');
        }
        if (pos < src.len and !locate.isBlankLine(locate.lineBody(src[pos..locate.lineEndAt(src, pos)]))) {
            try out.appendSlice(allocator, blank);
            try out.append(allocator, '\n');
        }
        return pos;
    }

    // ── Block attributes ───────────────────────────────────────────────────

    /// Replace the attribute set of the block `offset` sits in — a paragraph
    /// or heading, the block `setBlock` rewrites — with `attrs`. REPLACE, not
    /// merge: a caller reads the node's attributes, edits the list and passes
    /// it back whole, which is the contract `Builder.setAttrs` and the C ABI's
    /// `twig_builder_set_attrs` already have, and an empty list clears them.
    ///
    /// What a key MEANS is the host's, not twig's — this is `insertDirective`'s
    /// rule applied to a block's presentation: a rich-text editor's centred
    /// paragraph is `setBlockAttrs(off, &.{.{ .key = "class", .value =
    /// "center" }})`, and twig spells the pair and interprets neither half.
    /// See `docs/proposals/presentation-as-attributes.md`.
    ///
    /// THREE SPELLINGS, chosen by `Syntax.block_attrs` and the table's fields:
    ///
    ///   * `native` with an `attr_spelling` — djot: the format spells a
    ///     block's attributes on the line BEFORE it, and the document recorded
    ///     where (`Document.attrsSpan`). That line is rewritten, inserted with
    ///     the block's own quote prefix, or removed, and the block's bytes are
    ///     not touched. The alphabet path, preferred for the reason `setBlock`
    ///     prefers a marker.
    ///   * `native` otherwise — HTML's tag, AsciiDoc's `[…]` line: the block
    ///     is rebuilt with the new set and printed through `renderBlock`, as
    ///     `setBlockByRender` prints a heading, so its bytes are re-spelled by
    ///     the format.
    ///   * `wrapped` — Markdown under `html_elements`: the block is printed
    ///     inside a container the format spells as `<div …>` around it. When
    ///     the block is already the SOLE CHILD of such a wrapper, the wrapper's
    ///     attributes are replaced instead of a second one nesting, and an
    ///     empty set unwraps it — the rule `insertLink` applies to a link
    ///     covering the range, for the same reason.
    ///
    /// `error.UnsupportedFormat` where the table makes no claim, before
    /// anything is read. `error.NoBlock` when no paragraph or heading holds
    /// `offset`; `error.InvalidRange` for an offset past the source;
    /// `error.InvalidAttribute` for a key or value no format reads back (see
    /// `checkAttr`). `error.NotEditable` where the alphabet path cannot place
    /// the line — a block that starts on a list item's marker line — or where
    /// the document's attributes came from more than one `{…}` block and no
    /// single span describes them, and where the wrap path would need a list
    /// item's continuation indent it cannot reproduce.
    pub fn setBlockAttrs(self: *Editor, offset: usize, attrs: []const AST.KeyVal) Error!void {
        const shape = self.syntax.block_attrs orelse return error.UnsupportedFormat;
        // `assertCoherent` pins this non-null wherever the claim above is
        // made, so the `orelse` is the compiler's requirement.
        const render = self.syntax.renderBlock orelse return error.UnsupportedFormat;
        if (offset > self.sourceBytes().len) return error.InvalidRange;
        for (attrs) |kv| try checkAttr(kv);
        const block = locate.innermostBlock(&self.splicer.doc, offset) orelse return error.NoBlock;
        return switch (shape) {
            .native => if (self.syntax.attr_spelling) |sp|
                self.setBlockAttrsByLine(block, attrs, sp)
            else
                self.setNodeAttrsByRender(block, attrs, render),
            .wrapped => self.setBlockAttrsByWrap(block, attrs, render),
        };
    }

    /// `setBlockAttrs` where the format spells a block's attributes as a line
    /// before it: rewrite that line in place.
    fn setBlockAttrsByLine(self: *Editor, block: AST.Node.Id, attrs: []const AST.KeyVal, sp: syntax_mod.AttrSpelling) Error!void {
        const doc = &self.splicer.doc;
        const src = self.sourceBytes();
        const allocator = self.splicer.allocator;
        const existing = doc.attrsSpan(block);
        // Attributes the node has but no single span describes — djot merges
        // consecutive `{…}` blocks into one set — are not ours to rewrite: a
        // new line would sit beside the old ones.
        if (existing == null and !doc.ast.attrsOf(block).isEmpty()) return error.NotEditable;

        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        if (attrs.len != 0) attrs_writer.write(&out.writer, .{ .entries = attrs }, sp, "") catch return error.OutOfMemory;

        if (existing) |es| {
            if (attrs.len != 0) return self.commitSplice(es.start, es.end, out.written());
            // Clearing takes the whole line when the block was alone on it —
            // a quote's marker at most beside it — so no blank line is left
            // where the attributes were.
            const ls = locate.lineStartAt(src, es.start);
            const le = locate.lineEndAt(src, es.end); // past the newline
            const prefix = containerPrefix(src, es.start);
            const before = src[ls..es.start];
            const alone = std.mem.startsWith(u8, before, prefix) and
                locate.isBlankLine(before[prefix.len..]) and
                locate.isBlankLine(locate.lineBody(src[es.end..le]));
            if (!alone) return self.commitSplice(es.start, es.end, "");
            return self.commitSplice(ls, le, "");
        }
        if (attrs.len == 0) return;
        // A fresh line above the block, carrying the block's own quote
        // prefix. A block that starts on a list item's marker line has no
        // line above it that is its own — the marker is there — so that is
        // refused rather than written before the marker, where it would be
        // the list's.
        const bs = doc.span(block).start;
        const ls = locate.lineStartAt(src, bs);
        const prefix = containerPrefix(src, bs);
        const content_start = @max(bs, ls + prefix.len);
        if (!locate.isBlankLine(src[ls + prefix.len .. content_start])) return error.NotEditable;
        var line: std.ArrayList(u8) = .empty;
        defer line.deinit(allocator);
        try line.appendSlice(allocator, prefix);
        try line.appendSlice(allocator, out.written());
        try line.append(allocator, '\n');
        return self.commitSplice(ls, ls, line.items);
    }

    /// `setBlockAttrs` over a format whose attributes live on the block's own
    /// spelling and which has no line to rewrite: rebuild the block with the
    /// new set and print it, `setBlockByRender`'s path with the kind kept.
    fn setNodeAttrsByRender(self: *Editor, block: AST.Node.Id, attrs: []const AST.KeyVal, render: RenderBlockFn) Error!void {
        const doc = &self.splicer.doc;
        const allocator = self.splicer.allocator;
        var b = AST.Builder.init(allocator);
        defer b.deinit();
        var kids: std.ArrayList(AST.Node.Id) = .empty;
        defer kids.deinit(allocator);
        var child = doc.ast.nodes[block].first_child;
        while (child) |c| : (child = doc.ast.nodes[c].next_sibling) {
            try kids.append(allocator, try b.graftSubtree(&doc.ast, c));
        }
        const root = try b.addContainer(doc.ast.nodes[block].kind, kids.items);
        if (attrs.len != 0) try b.setAttrs(root, .{ .entries = attrs });
        const span = doc.span(block);
        return self.spliceRendered(&b, root, render, span.start, span.end);
    }

    /// `setBlockAttrs` where the attributes come back on a container around
    /// the block — Markdown's `<div>`: print the block inside one, or rewrite
    /// the one it is already the sole child of.
    fn setBlockAttrsByWrap(self: *Editor, block: AST.Node.Id, attrs: []const AST.KeyVal, render: RenderBlockFn) Error!void {
        const doc = &self.splicer.doc;
        const ast = &doc.ast;
        const allocator = self.splicer.allocator;
        const block_span = doc.span(block);

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, block_span.start, &chain);
        // The wrapper: the block's parent when that is a div — a fenced
        // container that is anonymous or named `div`, and not one the parser
        // read as a directive — with the block as its only child.
        var wrapper: ?AST.Node.Id = null;
        for (chain.items, 0..) |id, i| {
            if (id != block or i == 0) continue;
            const parent = chain.items[i - 1];
            const c = switch (ast.nodes[parent].kind) {
                .container => |c| c,
                else => break,
            };
            if (c.form != .block_fenced) break;
            if (c.name.len != 0 and !std.mem.eql(u8, c.name, "div")) break;
            if (c.name.len != 0 and doc.containerOrigin(parent) == .directive) break;
            if (ast.nodes[parent].first_child != block or ast.nodes[block].next_sibling != null) break;
            wrapper = parent;
            break;
        }
        // The printed lines take the block's quote prefix; a list item's
        // continuation indent is not one `containerPrefix` reproduces.
        if (insideListItem(doc, chain.items)) return error.NotEditable;

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        const grafted = try b.graftSubtree(ast, block);
        const target = if (wrapper) |w| doc.span(w) else block_span;
        if (attrs.len == 0) {
            // Nothing to wrap in: the block alone, over the wrapper if any.
            if (wrapper == null) return;
            return self.spliceRenderedPrefixed(&b, grafted, render, target);
        }
        const root = try b.addContainer(.{ .container = .{ .name = "div", .form = .block_fenced } }, &.{grafted});
        try b.setAttrs(root, .{ .entries = attrs });
        return self.spliceRenderedPrefixed(&b, root, render, target);
    }

    /// Wrap `[start, end)` in an anonymous inline container carrying `attrs`
    /// — djot's `[text]{…}`, HTML's and Markdown's `<span …>` — or, when the
    /// range lies inside one already, REPLACE that container's attributes
    /// instead of nesting a second; an empty set there unwraps it. That is the
    /// rule `insertLink` applies to a link covering the range, for the same
    /// reason: re-styling is the common gesture, and it must not build
    /// `[[text]{.a}]{.b}`. An anonymous span and one named `span` are the
    /// same node here — HTML and Markdown hand the name back, djot does not —
    /// and a `:span[…]` the Markdown parser read as a directive is neither.
    ///
    /// The inline half of `setBlockAttrs`, with the same vocabulary rule: twig
    /// spells the pairs and interprets none. The bytes are the format's,
    /// through `renderBlock` over the container — the covered inline nodes
    /// grafted under it as `insertLinkByRender` grafts them under a link — so
    /// a mark inside the range rides along. Unwrapping splices the node's
    /// CONTENT bytes over the node, which needs no renderer and keeps them.
    ///
    /// `error.UnsupportedFormat` where `Syntax.inline_attrs` is not claimed —
    /// AsciiDoc, whose `[#id.role]#text#` keeps two keys and drops a third,
    /// and Markdown without `html_elements`; `error.InvalidRange` for a bad
    /// range; `error.InvalidAttribute` as `setBlockAttrs`; `error.NotEditable`
    /// for an empty range with no span to re-style, or a range that cuts a
    /// node it cannot slice (see `coveredInlines`).
    pub fn wrapRangeAttrs(self: *Editor, span: Span, attrs: []const AST.KeyVal) Error!void {
        if (!self.syntax.inline_attrs) return error.UnsupportedFormat;
        const render = self.syntax.renderBlock orelse return error.UnsupportedFormat;
        try self.checkRange(span.start, span.end);
        for (attrs) |kv| try checkAttr(kv);
        const doc = &self.splicer.doc;
        const ast = self.astView();
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, span.start, &chain);
        // The innermost attributed span covering the range.
        var existing: ?AST.Node.Id = null;
        var i = chain.items.len;
        while (i > 0) {
            i -= 1;
            const id = chain.items[i];
            const c = switch (ast.nodes[id].kind) {
                .container => |c| c,
                else => continue,
            };
            if (c.form != .inline_text) continue;
            if (c.name.len != 0 and !std.mem.eql(u8, c.name, "span")) continue;
            // djot's own span is anonymous and, being lightweight markup,
            // carries the `directive` origin too; the one to leave alone is a
            // NAMED span with it — Markdown's `:span[…]` directive.
            if (c.name.len != 0 and doc.containerOrigin(id) == .directive) continue;
            const sp = doc.span(id);
            if (sp.start <= span.start and sp.end >= span.end) {
                existing = id;
                break;
            }
        }

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        var kids: std.ArrayList(AST.Node.Id) = .empty;
        defer kids.deinit(allocator);
        var target = span;
        if (existing) |id| {
            target = doc.span(id);
            if (target.start == 0 and target.end == 0) return error.NotEditable;
            // djot's `{…}` follows the brackets OUTSIDE the node's span, and
            // the document recorded where; a set merged from several blocks
            // has no single span and is not ours to rewrite. HTML's and
            // Markdown's attributes sit inside the tag, inside the span — the
            // formats whose `block_attrs` path is the line rewrite are the
            // ones whose attributes lie outside, as `setBlockAttrs` reads it.
            if (doc.attrsSpan(id)) |as| {
                target = Span.init(@min(target.start, as.start), @max(target.end, as.end));
            } else if (self.syntax.block_attrs == .native and self.syntax.attr_spelling != null and !ast.attrsOf(id).isEmpty()) {
                return error.NotEditable;
            }
            if (attrs.len == 0) {
                const cs = doc.contentSpan(id) orelse return error.NotEditable;
                return self.commitSplice(target.start, target.end, src[cs.start..cs.end]);
            }
            var child = ast.nodes[id].first_child;
            while (child) |c| : (child = ast.nodes[c].next_sibling) {
                try kids.append(allocator, try b.graftSubtree(ast, c));
            }
        } else {
            if (attrs.len == 0) return;
            if (span.end == span.start) return error.NotEditable;
            const covered = try coveredInlines(allocator, doc, span.start, span.end);
            defer allocator.free(covered);
            for (covered) |piece| {
                try kids.append(allocator, if (piece.text) |t|
                    try b.addLeaf(.{ .str = t })
                else
                    try b.graftSubtree(ast, piece.node));
            }
        }
        const root = try b.addContainer(.{ .container = .{ .name = "", .form = .inline_text } }, kids.items);
        try b.setAttrs(root, .{ .entries = attrs });
        return self.spliceRendered(&b, root, render, target.start, target.end);
    }

    /// `spliceRendered` for a fragment of several lines going into a quote:
    /// every line after the first takes the quote prefix the first already
    /// sits behind, a blank line its marker alone.
    fn spliceRenderedPrefixed(self: *Editor, b: *const AST.Builder, root: AST.Node.Id, render: RenderBlockFn, target: Span) Error!void {
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const prefix = containerPrefix(src, target.start);
        const blank = std.mem.trimEnd(u8, prefix, " ");
        // A Markdown block's span starts at column zero, quote marker
        // included; the first printed line goes after that marker, which is
        // then the prefix every later line copies.
        const start = @max(target.start, locate.lineStartAt(src, target.start) + prefix.len);
        const view = b.view(root);
        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        try renderNode(allocator, render, &view, root, &out.writer);
        const rendered = std.mem.trimEnd(u8, out.written(), "\r\n");
        if (prefix.len == 0) return self.commitSplice(start, target.end, rendered);
        var text: std.ArrayList(u8) = .empty;
        defer text.deinit(allocator);
        var lines = std.mem.splitScalar(u8, rendered, '\n');
        var first = true;
        while (lines.next()) |l| {
            if (!first) {
                try text.append(allocator, '\n');
                try text.appendSlice(allocator, if (l.len == 0) blank else prefix);
            }
            first = false;
            try text.appendSlice(allocator, l);
        }
        return self.commitSplice(start, target.end, text.items);
    }

    // ── Node attributes ────────────────────────────────────────────────────

    /// Replace the attribute set of node `id` with `attrs` — an element,
    /// addressed by id rather than by offset because the caller that wants
    /// this holds a tree and not a caret: a canvas editor over an SVG names
    /// the shape it is dragging, and no byte position stands for it. Ids are
    /// the CURRENT tree's, as the splicer's node ops take them, and a
    /// successful edit reparses, so read the tree again before the next call.
    ///
    /// The run is written where the format keeps it: on the node's own
    /// opening spelling, at the span the parser recorded
    /// (`Document.attrsSpan`), as a tag interior — ` key="value"` pairs with
    /// `&`, `<`, `>` and `"` as entities. Every other byte of the node, its
    /// children included, stays where it is; a `<g>` holding a thousand paths
    /// is not re-printed to move it. A node with no attributes yet has no
    /// span, so the run is inserted right after the name, where the parser
    /// reads the first one. Replace, not merge, as `setBlockAttrs`: read the
    /// node's attributes, edit the list, pass it back whole. An empty set
    /// removes the run, and removing what is already absent is a no-op.
    ///
    /// `error.UnsupportedFormat` where `Syntax.node_attrs` is not claimed,
    /// before anything is read. `error.InvalidRange` for an id no node has;
    /// `error.InvalidAttribute` as `setBlockAttrs` (see `checkAttr`);
    /// `error.NotEditable` for a node that is not an element — a text run, a
    /// comment, a container some other format's parser made — and for an
    /// element whose attributes the parser gave no span to, which no parser
    /// in the tree does but a grafted tree could.
    pub fn setNodeAttrs(self: *Editor, id: AST.Node.Id, attrs: []const AST.KeyVal) Error!void {
        const spelling = self.syntax.node_attrs orelse return error.UnsupportedFormat;
        const doc = &self.splicer.doc;
        if (id >= doc.ast.nodes.len) return error.InvalidRange;
        for (attrs) |kv| try checkAttr(kv);
        const c = switch (doc.ast.nodes[id].kind) {
            .container => |c| c,
            else => return error.NotEditable,
        };
        if (doc.containerOrigin(id) != .element) return error.NotEditable;
        const existing = doc.attrsSpan(id);
        if (existing == null and !doc.ast.attrsOf(id).isEmpty()) return error.NotEditable;

        var out: Writer.Allocating = .init(self.splicer.allocator);
        defer out.deinit();
        attrs_writer.writeHtmlAttrs(&out.writer, .{ .entries = attrs }) catch return error.OutOfMemory;

        if (existing) |es| return self.commitSplice(es.start, es.end, out.written());
        if (attrs.len == 0) return;
        const at = doc.span(id).start + spelling.open.len + c.name.len;
        return self.commitSplice(at, at, out.written());
    }

    // ── Splitting a block ────────────────────────────────────────────────────

    /// Split the block at `offset` in two AT THE CARET, both halves the SAME
    /// KIND — Enter in the middle of a paragraph, and the gesture
    /// `insertThematicBreak` deliberately isn't.
    ///
    /// Nearly a pure INSERTION at `offset`: what is minted is the separator
    /// between the halves, and the only bytes REMOVED are the second half's
    /// leading spaces and tabs. Those are structure rather than content at the
    /// start of a block — a split at `- b| c` that kept its space would write
    /// `-  c`, setting that item's content indent to three. A code block sheds
    /// nothing, because there leading whitespace IS the content. What the
    /// separator is, is the only other thing that varies:
    ///
    ///   * A PARAGRAPH gets a blank line — `ab` -> `a`/`b`. Inside a quote the
    ///     blank carries the quote's marker and the second half its full prefix
    ///     (`> ab` -> `> a`/`>`/`> b`), so the split happens INSIDE the quote
    ///     rather than ending it.
    ///   * A paragraph in a LIST ITEM gets the item's MARKER instead of a blank,
    ///     so the second half is a sibling item and not a paragraph that ends
    ///     the list: `- this is |a list item` -> `- this is `/`- a list item`.
    ///     The marker is repeated VERBATIM, ordered numbers included, so a split
    ///     `1.` item yields two `1.` items — both formats renumber on render,
    ///     and `renumberOrderedLists` is the gesture for fixing the source when
    ///     the caller wants it fixed. A TASK item's new half is an UNCHECKED
    ///     box regardless of the original's state: splitting one done thing in
    ///     two does not make the remainder done. The marker is taken from the
    ///     END OF ANY QUOTE PREFIX rather than from the bullet, so a NESTED
    ///     item's indent rides along with it and the new sibling stays in its
    ///     own list instead of dropping to column zero and joining the
    ///     enclosing one.
    ///   * A HEADING repeats its own marker at its own level, because both
    ///     halves being the same kind is what "split" means here; `setBlock` is
    ///     how the caller demotes the second half if that is what they wanted.
    ///   * A CODE BLOCK becomes two code blocks — the first closed with a fence,
    ///     the second reopened with the opening fence line REPRODUCED VERBATIM,
    ///     so its width and its info string both survive. Splitting code is a
    ///     real request (one listing becoming two), and a consumer that doesn't
    ///     want the gesture live there can ask the tree what kind of block the
    ///     caret is in before offering it.
    ///
    /// AT A BLOCK BOUNDARY this still splits, which is what makes it Enter: at
    /// the end of a list item it opens an EMPTY sibling item (`- a|` -> `- a`/
    /// `- `), which is exactly the empty block the caller wants to type into.
    /// The paragraph case is the one place the "empty block" is unrepresentable
    /// — no format spells an empty paragraph — so `a|` gains a trailing blank
    /// line and reparses as ONE paragraph. The caret is where the next one will
    /// begin; the node appears when there is text to hold.
    ///
    /// `error.NotEditable` for the blocks where a caret-split has no honest
    /// meaning:
    ///   * A TABLE, whose structure is rows and cells rather than lines — a
    ///     newline mid-cell doesn't divide a table, it destroys one. Splitting a
    ///     table INTO TWO TABLES is a real gesture, but it is a table gesture
    ///     (it has to decide what the second table's header is), not this one.
    ///   * A SETEXT heading, whose `---` underline belongs to a block that would
    ///     no longer be under it. `setBlock` normalises one to ATX, which makes
    ///     this work; doing that silently here would rewrite the half the caller
    ///     didn't touch.
    ///   * An INDENTED code block, where a blank line is interior rather than a
    ///     separator, so the "split" would parse back as one block.
    ///
    /// `error.NoBlock` when nothing covers `offset` — an empty document has no
    /// block to divide.
    ///
    /// `error.UnsupportedFormat` when the format has no `block_separator`, and
    /// that is checked FIRST, before a single byte of source is read: the blank
    /// line every case above writes is only a divider where blank lines divide
    /// blocks. In HTML it is insignificant whitespace inside the `<p>`, so the
    /// gesture used to report success over a document it had not changed the
    /// shape of at all.
    pub fn splitBlock(self: *Editor, offset: usize) Error!void {
        const separator = self.syntax.block_separator orelse return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const doc = &self.splicer.doc;
        const found = splitTarget(doc, offset) orelse return error.NoBlock;

        const block_start = doc.span(found.block).start;
        const prefix = containerPrefix(src, block_start);
        // As in `insertThematicBreak`: a quote's blank line carries its marker
        // but not the space after it.
        const blank = std.mem.trimEnd(u8, prefix, " ");

        // When the caret already sits at a line start, the line end before it is
        // the separator's first newline — emitting another would leave a blank
        // line trailing inside the FIRST half (and, in a code block, inside its
        // body). This only decides whether the separator needs to open a line or
        // is already on one.
        const at_line_start = offset == 0 or src[offset - 1] == '\n';

        const allocator = self.splicer.allocator;
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // How many bytes after the caret the second half must SHED. Leading
        // spaces at the start of a block are structure, not content — a split at
        // `- b| c` whose second half kept its space would write `-  c`, setting
        // that item's content indent to three. This is the one place the gesture
        // isn't a pure insertion, and it is deliberately not done for a code
        // block, where leading whitespace IS the content.
        var shed: usize = 0;

        switch (splitShape(std.meta.activeTag(doc.ast.nodes[found.block].kind))) {
            .text => {
                var marker: std.ArrayList(u8) = .empty;
                defer marker.deinit(allocator);
                const in_item = try self.splitMarker(found, block_start, &marker);

                while (offset + shed < src.len and
                    (src[offset + shed] == ' ' or src[offset + shed] == '\t')) shed += 1;

                if (!at_line_start) try out.append(allocator, '\n');
                // A list item's halves stay in ONE list, so no blank line
                // between them — a blank there loosens the list, changing every
                // sibling's rendering. Everywhere else the blank IS the divider.
                if (!in_item) {
                    try out.appendSlice(allocator, blank);
                    try out.appendSlice(allocator, separator);
                }
                try out.appendSlice(allocator, prefix);
                try out.appendSlice(allocator, marker.items);
            },

            .code => {
                const fence = self.syntax.code_fence orelse return error.UnsupportedFormat;
                const open = src[locate.lineStartAt(src, block_start)..locate.lineEndAt(src, block_start)];
                // No fence on the opening line means an INDENTED code block,
                // where a blank line is interior and would not divide anything.
                const at = fenceAt(open, fence.char, fence.min) orelse return error.NotEditable;

                if (!at_line_start) try out.append(allocator, '\n');
                try out.appendSlice(allocator, prefix);
                try out.appendNTimes(allocator, fence.char, at.width);
                try out.append(allocator, '\n');
                try out.appendSlice(allocator, blank);
                try out.appendSlice(allocator, separator);
                // Verbatim from the fence character on, so width and info
                // string both survive; the prefix is re-minted, not copied.
                try out.appendSlice(allocator, prefix);
                try out.appendSlice(allocator, locate.lineBody(open)[at.start..]);
                try out.append(allocator, '\n');
            },

            .refuse => return error.NotEditable,
        }

        return self.commitSplice(offset, offset + shed, out.items);
    }

    /// Append to `out` what the second half of a split must carry to come back
    /// as the SAME KIND as the first — a list item's marker, a heading's marker,
    /// both when a heading sits in an item, or nothing for a plain paragraph.
    /// Returns whether the block is in a LIST ITEM, which is what decides
    /// whether a blank line may separate the halves.
    ///
    /// The list case reads the marker off the ITEM'S OWN first line rather than
    /// rebuilding it from `Syntax`, so `*` stays `*`, `1)` stays `1)`, and the
    /// document keeps the spelling its author chose.
    fn splitMarker(
        self: *Editor,
        found: locate.LineBlock,
        block_start: usize,
        out: *std.ArrayList(u8),
    ) Error!bool {
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const parent_tag = std.meta.activeTag(doc.ast.nodes[found.parent].kind);
        const in_item = parent_tag == .list_item or parent_tag == .task_list_item;

        if (in_item) {
            const item_start = doc.span(found.parent).start;
            const line = src[locate.lineStartAt(src, item_start)..locate.lineEndAt(src, item_start)];
            var from: usize = 0;
            while (skipQuoteMarker(line, from)) |j| from = j;
            const m = listMarkerAt(line, from) orelse return error.NotEditable;
            // From the end of the quote prefix, NOT from `m.start` — the run of
            // spaces between them is the item's NESTING DEPTH, and dropping it
            // moves the new sibling to column zero, out of its own list and into
            // the enclosing one. `listMarkerAt` puts `start` at the bullet on
            // purpose (its other callers want the indent left where it is), so
            // taking it back is this caller's job.
            try out.appendSlice(allocator, line[from..m.end]);

            // A task item's new half is an UNCHECKED box: the marker as written,
            // then this format's empty box rather than the original's state.
            if (parent_tag == .task_list_item) {
                const box = self.syntax.task_marker orelse return error.NotEditable;
                try out.appendSlice(allocator, box.unchecked);
                try out.appendSlice(allocator, box.space);
            }
        }

        if (std.meta.activeTag(doc.ast.nodes[found.block].kind) != .heading) return in_item;

        // A heading repeats its own marker. A SETEXT one has none on its first
        // line — its `---` sits UNDER the block, and would end up under the
        // second half alone — so it is refused rather than silently normalised.
        const marker = self.syntax.heading_marker orelse return error.UnsupportedFormat;
        const line = src[locate.lineStartAt(src, block_start)..locate.lineEndAt(src, block_start)];
        var i: usize = 0;
        while (skipQuoteMarker(line, i)) |j| i = j;
        if (listMarkerAt(line, i)) |m| i = m.end;
        while (i < line.len and line[i] == ' ') i += 1;
        if (i >= line.len or line[i] != marker) return error.NotEditable;

        try out.appendNTimes(allocator, marker, doc.ast.nodes[found.block].kind.heading.level);
        try out.append(allocator, ' ');
        return in_item;
    }

    // ── Joining two blocks ───────────────────────────────────────────────────

    /// Join the block at `offset` into the block BEFORE it — Backspace at the
    /// start of a block, and forward Delete at the end of the one above it.
    /// The inverse of `splitBlock`, and the reason it is a gesture rather than
    /// a host's own delete: what joins two blocks is a fact about the FORMAT,
    /// and a host that deletes the newline between them is right only for two
    /// Markdown paragraphs at the top level. In HTML that byte is the `>` of
    /// `</p>`; under a heading it leaves two blocks; after a Markdown `<div>`
    /// it deletes the blank line the div needed and breaks the div.
    ///
    /// ── The two blocks ─────────────────────────────────────────────────────
    /// **B** is the innermost `para`/`heading` covering `offset` — the block
    /// being joined UPWARD, and the one whose identity is given up. **A** is
    /// the LEAF BLOCK immediately before it in document order: the nearest
    /// preceding node holding no blocks of its own, with no other leaf block
    /// between the two. A is found by document order rather than by sibling
    /// order because the caret does not know about containers: the block
    /// visually above `below` in `above`/`<div>`/`hello`/`</div>`/`below` is
    /// `hello`, three levels down, and joining into `above` would be joining
    /// into the wrong paragraph.
    ///
    /// ── What the result is made of ─────────────────────────────────────────
    /// One splice over `[A.ce, R_end)`, assembled from four pieces:
    ///
    ///   * **The separator.** `Syntax.line_join` followed by the CONTAINER
    ///     PREFIX A's own first line sits behind (`locate.continuationPrefix`
    ///     — a quote's `> ` repeated, a list item's marker's WIDTH in spaces,
    ///     nothing at the top level). That prefix is what keeps the joined
    ///     line inside its containers in a format with no lazy continuation,
    ///     which is djot: `- a` + `below` is `- a`/`  below` and not `- a`/
    ///     `below`, which would end the list. The one exception is a heading A
    ///     with a LEADING MARKER (`# Title`, AsciiDoc's `== Title`): such a
    ///     heading is one line by its own spelling, so the separator is a
    ///     single SPACE and `# Title` + `below` is `# Title below`. A SETEXT
    ///     heading A takes the line end like everything else — its content may
    ///     already span lines, and its underline rides along in A's tail.
    ///   * **B's content**, `[B.cs, B.ce)` — its text and nothing else.
    ///   * **A's tail**, `[A.ce, span(a_top).end)`, carried PAST the joined
    ///     text: A's own closing markup (an ATX closing `#` run, a setext
    ///     underline, `</p>`) followed by the closers of every container A
    ///     sits in below the two blocks' lowest common ancestor (a Markdown
    ///     `</div>`, a djot `:::` fence). `a_top` is the LCA's own child
    ///     holding A, so this is exactly what has to be re-closed after the
    ///     text that was pulled in. A is necessarily the last leaf in those
    ///     containers, since B follows it immediately. Its trailing SEPARATOR
    ///     LINES are trimmed first — see `trimTailSeparators`, without which a
    ///     Markdown quote's own trailing `>` line travelled past the joined
    ///     text and piled up there.
    ///   * **Everything else between them vanishes**: the blank line, B's
    ///     markers (`# `, `- `), B's attribute line (djot's `{…}`,
    ///     AsciiDoc's `[…]`), B's opening tags (`<div class="center">`,
    ///     `<p class="x">`). B's ATTRIBUTES ARE DISCARDED on purpose — the
    ///     joined text is A's block, so it takes A's presentation.
    ///
    /// `R_end` is `span(B).end`, so B's own closing markup goes with it. When
    /// B's ancestor chain below the LCA passes through a DELIMITED container —
    /// one spelled with closing bytes of its own after its children, which is
    /// the `container` kind (a Markdown `<div>`, an HTML element, a djot `:::`
    /// fence, a directive) — it is the OUTERMOST of those whose span end is
    /// used instead, because B leaving it means that container's closers go
    /// too. The PREFIX containers (a quote, a list item, a list, a section, a
    /// definition item) have no closers, so whatever follows B inside them
    /// simply stays where it is: joining the first item's text out of a list
    /// leaves the other items a list.
    ///
    /// ── What is refused ────────────────────────────────────────────────────
    /// `error.NoBlock` when no `para`/`heading` covers `offset`, and when B is
    /// the document's FIRST block — there is nothing above to join into, which
    /// is the ordinary Backspace-at-the-top-of-the-document answer.
    ///
    /// `error.NotEditable` for the shapes with no honest result:
    ///   * **A is not a `para` or a `heading`** — a code block, a table, a
    ///     rule, a raw block, a reference definition. There is no text to join
    ///     into, and pulling B's prose into a fence or a table would destroy
    ///     the block a caller was standing next to.
    ///   * **Either block is in a TABLE CELL.** A cell's blocks are not the
    ///     document's lines; a newline between two of them divides nothing and
    ///     a join across a cell boundary is not a join at all.
    ///   * **B is a SETEXT heading**, whose underline is how it is spelled at
    ///     all. `setBlock` normalises one to ATX, which makes this work;
    ///     doing that silently here would rewrite the half the caller did not
    ///     point at, exactly as `splitBlock` refuses one for its own half.
    ///   * **B would have to leave a delimited container that has more content
    ///     after it.** That container's closers cannot move up past content
    ///     that is still inside it, and there is no single obvious thing to do
    ///     instead — split the container in two, or drag the rest out with B —
    ///     so this is the one shape the gesture refuses rather than guesses at.
    ///   * **The GAP between A and B holds anything but separation** — see
    ///     `gapIsClean`. The splice destroys everything between the two that
    ///     is on neither's chain, and a definition is not in the tree at all,
    ///     so nothing above can notice it. This is the guard, and it is read
    ///     off the SOURCE for that reason.
    ///
    /// `error.InvalidRange` when `offset` is past the source.
    ///
    /// `error.UnsupportedFormat` when the format has no `Syntax.line_join`,
    /// checked FIRST, before a byte of source is read: a join writes a line
    /// break INSIDE a block, which is a spelling and not a universal. It is a
    /// different gate from `splitBlock`'s `block_separator` and deliberately a
    /// wider one — HTML has no blank-line block separator and so cannot be
    /// split, while a newline inside its `<p>` is precisely what a join needs.
    pub fn joinBlocks(self: *Editor, offset: usize) Error!void {
        const join = self.syntax.line_join orelse return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const doc = &self.splicer.doc;
        const allocator = self.splicer.allocator;

        // A caret in a TABLE, before B is looked for at all. A pipe table's
        // cell holds its text directly, with no `para` under it, so the
        // `innermostBlock` below would answer `null` there and the refusal
        // would come back as `NoBlock` — "there is nothing here", about a
        // caret sitting in plain view inside a cell. `lineOwningBlock` stops
        // at the table (a `cell` is deliberately not a block parent), which
        // makes this the same position answer for every format, whether or
        // not its cells wrap their content in a block.
        if (locate.lineOwningBlock(doc, offset)) |lb| {
            if (std.meta.activeTag(doc.ast.nodes[lb.block].kind) == .table) return error.NotEditable;
        }

        const b = locate.innermostBlock(doc, offset) orelse return error.NoBlock;
        if (isSetextHeading(doc, b)) return error.NotEditable;

        var b_chain: std.ArrayList(AST.Node.Id) = .empty;
        defer b_chain.deinit(allocator);
        if (!try nodePath(allocator, doc, b, &b_chain)) return error.NoBlock;
        if (passesThroughTable(doc, b_chain.items)) return error.NotEditable;

        const a = precedingLeafBlock(doc, b) orelse return error.NoBlock;
        switch (std.meta.activeTag(doc.ast.nodes[a].kind)) {
            .para, .heading => {},
            else => return error.NotEditable,
        }

        var a_chain: std.ArrayList(AST.Node.Id) = .empty;
        defer a_chain.deinit(allocator);
        if (!try nodePath(allocator, doc, a, &a_chain)) return error.NotEditable;
        if (passesThroughTable(doc, a_chain.items)) return error.NotEditable;

        // Where the two chains diverge. Both start at the root and neither is a
        // prefix of the other — A is a leaf, so B cannot be inside it, and B is
        // not A — so this index exists in both. It is never 0: the root is the
        // first element of both chains, so the loop below runs at least once.
        // Running off the end of either chain would mean one block is inside
        // the other, which the two facts above rule out; refusing beats
        // indexing past the end should a future kind make it reachable.
        var i: usize = 0;
        while (i < a_chain.items.len and i < b_chain.items.len and
            a_chain.items[i] == b_chain.items[i]) i += 1;
        if (i >= a_chain.items.len or i >= b_chain.items.len) return error.NotEditable;
        const a_top = a_chain.items[i];
        const b_top = b_chain.items[i];

        // Everything between the two that is on neither chain is about to be
        // destroyed. This is the one guard against that, and the shapes it
        // catches are ones no walk above could have — see `gapIsClean`.
        if (!gapIsClean(doc, src, a_chain.items[0..i], a_top, b_top)) return error.NotEditable;

        // B's own closing markup goes; a delimited container it is leaving
        // takes its closers with it, and the OUTERMOST such container is the
        // one whose span bounds the removal.
        var r_end = doc.span(b).end;
        for (b_chain.items[i .. b_chain.items.len - 1]) |id| {
            // Delimited by what it WRITES, not by its kind: HTML's
            // `<blockquote>` and `<ul>`, and AsciiDoc's `____`, close with
            // markup a quote or list spelled by line prefixes never has.
            if (!writesClosers(doc, src, id)) continue;
            // Nothing may be left behind inside a container whose closers are
            // about to move up past it.
            if (lastLeafBlock(doc, id) != b) return error.NotEditable;
            r_end = doc.span(id).end;
            break;
        }

        // B leaving a PREFIX container that goes on after it leaves behind the
        // container's blank line that separated B from what followed — `>`,
        // still carrying the prefix. With B gone, that line separates A from
        // the rest of the container, and A is outside it: kept as it was, it
        // is no separator at all where A sits, and a format whose paragraphs
        // run to a blank line (djot) reads the rest of the quote into A. It
        // is taken with B and written back as a blank line at A's level.
        var separate_rest = false;
        if (b_top != b and r_end == doc.span(b).end) {
            // `lineEndAt` answers one past the line's `\n`.
            const line_start = if (r_end > 0 and src[r_end - 1] == '\n') r_end else locate.lineEndAt(src, r_end);
            const line_end = locate.lineEndAt(src, line_start);
            if (line_start < src.len and line_end < doc.span(b_top).end) {
                const line = locate.lineBody(src[line_start..line_end]);
                var k: usize = 0;
                while (skipQuoteMarker(line, k)) |j| k = j;
                if (k > 0 and std.mem.trim(u8, line[k..], " \t").len == 0) {
                    r_end = line_end;
                    separate_rest = true;
                }
            }
        }

        const a_content = blockContent(doc, a);
        const b_content = blockContent(doc, b);
        const a_tail = trimTailSeparators(src[a_content.end..doc.span(a_top).end]);
        const b_text = src[b_content.start..b_content.end];

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // A heading written with a leading marker is ONE LINE, so what
        // continues it is a space, not a line end. `markerSpan` is the whole
        // test: an HTML `<h1>` has no marker and takes the line end, and
        // `<h1>a\nb</h1>` is the one heading it should be.
        if (std.meta.activeTag(doc.ast.nodes[a].kind) == .heading and doc.markerSpan(a) != null) {
            try out.append(allocator, ' ');
            // B may be several lines; the heading is one. Each of B's line
            // ends becomes the space that continues the sentence, or `# T` +
            // `xx`/`yy` would be a heading `# T xx` with `yy` left below it
            // as a paragraph — two blocks, from a gesture that reports having
            // made one.
            try appendAsOneLine(allocator, b_text, &out);
        } else {
            try out.appendSlice(allocator, join);
            _ = try locate.continuationPrefix(allocator, doc, a_content.start, &out);
            try appendAsContinuation(allocator, self.syntax, b_text, &out);
        }
        try out.appendSlice(allocator, a_tail);

        // The removed region ran to the end of a line in every format whose
        // block span covers its own terminator (djot's does, Markdown's does
        // not), and A's tail only ends at one when A's container did. Put the
        // terminator back when B's removal took it and the tail did not supply
        // it, so the document keeps the line structure it had. `out` is never
        // empty — the separator above is at least one byte — but the index is
        // guarded rather than argued, since a `line_join` of `""` would make
        // the argument false and the panic real.
        if (r_end > a_content.end and src[r_end - 1] == '\n' and
            (out.items.len == 0 or out.items[out.items.len - 1] != '\n')) try out.append(allocator, '\n');
        if (separate_rest) {
            if (out.items.len == 0 or out.items[out.items.len - 1] != '\n') try out.append(allocator, '\n');
            var prefix: std.ArrayList(u8) = .empty;
            defer prefix.deinit(allocator);
            _ = try locate.continuationPrefix(allocator, doc, a_content.start, &prefix);
            try out.appendSlice(allocator, std.mem.trimEnd(u8, prefix.items, " "));
            try out.append(allocator, '\n');
        }

        return self.commitSplice(a_content.end, r_end, out.items);
    }

    // ── Moving a block ───────────────────────────────────────────────────────

    /// Move the block at `from` to the boundary `to` names, spelling the line
    /// prefixes of the container it lands in — a drag-and-drop, or Alt+↑/↓,
    /// in a host that has a caret and no tree.
    ///
    /// The splicer's `moveNode` reorders two nodes by their bytes, and says so:
    /// a quote's `> ` does not travel, a list item's continuation indent is
    /// not written. That is right for an XML element and wrong for every drop
    /// that restructures prose — a paragraph dragged out of a quote keeps its
    /// `> ` and is still a quote, one dragged under a list item's text lands
    /// at column zero and ends the list. Which prefix a line needs where is
    /// the per-format knowledge `toggleBlockContainer` and
    /// `locate.continuationPrefix` already hold, and this is the gesture that
    /// applies it to a move.
    ///
    /// ── The block ──────────────────────────────────────────────────────────
    /// `from` names the deepest block owning the line it is on — the
    /// `para`/`heading` `setBlock` acts on, or the code block, table or rule
    /// where there is no text block — widened to the LIST ITEM when it is the
    /// item's first block: a bullet's text is the bullet, and dragging it
    /// takes the item and everything under it. A block later in an item's tail
    /// moves alone. `error.NoBlock` on a blank line.
    ///
    /// ── The boundary ───────────────────────────────────────────────────────
    /// `to` is a position BETWEEN blocks: at or before a block's first content
    /// byte (before it), at or after its last (after it), on a blank line, or
    /// the source's length (the document's end, whether or not the last line
    /// is terminated). The block lands there and takes the prefixes of the
    /// container that boundary is inside — the innermost one, so `to` at the
    /// first content byte of a quote's first paragraph is inside the quote,
    /// and a block moved there is quoted. `error.NotEditable` for a `to`
    /// interior to a block — inside a fence, a table, a paragraph's second
    /// line — as `setBlock` answers for a blank line there;
    /// `error.InvalidArgument` for a `to` inside the block being moved, or at
    /// a boundary it already sits on, which would move nothing.
    ///
    /// A boundary at a container's edge is two boundaries — before its first
    /// block and before the container, after its last and after the
    /// container — and each is spelled:
    ///
    ///   * At the container's OPENING — at or before its marker, or at the
    ///     first byte of a delimited container's opening line — is before
    ///     the container, at its parent's level; the first content byte is
    ///     inside it. `> > a` at 0 is before both quotes, at 2 before the
    ///     inner one alone, and at 4 inside both. This is the one offset that
    ///     can name "above" a quote nothing precedes.
    ///   * A boundary the block already sits on, when the block is the
    ///     container's first or last, is read as the boundary before or after
    ///     the CONTAINER: the last block of a quote dropped at its own end —
    ///     the one offset that is both after it and after the quote — or on
    ///     the blank line after the quote, leaves the quote; its first block
    ///     dropped at its own start leaves it upward. A block between two
    ///     others has only its own boundary there, and moves nothing.
    ///
    /// Two adjustments where a list item is involved, both because a list holds
    /// items and nothing else:
    ///
    ///   * A boundary BEFORE an item's first block is before the ITEM, at the
    ///     list's level — a block dropped above a bullet's text is above the
    ///     bullet, not the bullet's new text. A boundary after an item's last
    ///     block is inside the item, which is how a block reaches an item's
    ///     tail (`to` at the end of the item's text).
    ///   * A moved ITEM is always a sibling: dropped anywhere inside another
    ///     item it lands after that item (before it, for the boundary above),
    ///     and it stays a bullet wherever else it lands — a one-item list
    ///     between two paragraphs, a quoted bullet in a quote. Nesting one
    ///     under another is `toggleBlockContainer`'s to spell.
    ///
    /// ── What is written ────────────────────────────────────────────────────
    /// One splice covering the block's old lines and the boundary. The block's
    /// lines are taken whole — stripped of every prefix its old containers put
    /// on them (the ancestors' markers on its first line, their continuation
    /// width on the rest, read off the marker spans the way
    /// `locate.continuationPrefix` reads them) and written behind the
    /// destination chain's own. The separator lines that kept it apart from
    /// its old neighbours go with it, so no doubled blank line is left where
    /// it was, and a quote or list it was the only content of goes too rather
    /// than standing empty. At the destination it is blank-separated from
    /// whatever it lands beside (`block_separator` after the container's
    /// blank-line prefix, so `>` inside a quote), except between items of a
    /// tight list — where a blank would loosen the list — and in a format that
    /// separates blocks by nothing (HTML). A format that attaches a block to
    /// an item by a line of its own (`Syntax.list_attach`, AsciiDoc's `+`)
    /// gets that line in place of the blank, and the block at column zero.
    ///
    /// Within one container the result is what `Splicer.moveNode` writes:
    /// the same lines, in the new order, with the same separators.
    ///
    /// `error.NotEditable` also when the block shares a line with something
    /// else — an HTML `<p>` written beside another on one line — since what
    /// moves is lines. `error.InvalidRange` past the source.
    pub fn moveBlock(self: *Editor, from: usize, to: usize) Error!void {
        if (!supports(self.syntax, .move_block)) return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (from > src.len or to > src.len) return error.InvalidRange;
        const doc = &self.splicer.doc;
        const allocator = self.splicer.allocator;

        const unit = movableUnit(doc, from) orelse return error.NoBlock;
        var unit_chain: std.ArrayList(AST.Node.Id) = .empty;
        defer unit_chain.deinit(allocator);
        if (!try nodePath(allocator, doc, unit, &unit_chain)) return error.NoBlock;
        const ancestors = unit_chain.items[0 .. unit_chain.items.len - 1];
        const lines = try unitLines(doc, unit, ancestors);
        // Strictly inside the block's own text — its content, or for an
        // item everything behind its marker up to its last byte, a line end
        // aside. Its edges are boundaries.
        const own = if (isListItem(doc.ast.nodes[unit].kind)) blk: {
            const sp = doc.span(unit);
            var end = sp.end;
            if (end > sp.start and src[end - 1] == '\n') end -= 1;
            break :blk Span.init(if (doc.markerSpan(unit)) |m| m.end else sp.start, end);
        } else blockContent(doc, unit);
        if (to > own.start and to < own.end) return error.InvalidArgument;

        const unit_is_item = isListItem(doc.ast.nodes[unit].kind);
        var dest = try resolveBoundary(allocator, self.syntax, doc, to, unit);

        // The prefix every moved line takes: the destination chain's, minus a
        // list item where the format attaches by a line rather than an indent.
        var dest_chain: std.ArrayList(AST.Node.Id) = .empty;
        defer dest_chain.deinit(allocator);
        if (!try nodePath(allocator, doc, dest.container, &dest_chain)) return error.NotEditable;
        for (dest_chain.items) |id| if (id == unit) return error.InvalidArgument;

        // A boundary on the separator lines that leave with the block — the
        // blank line after a quote the block was the only content of — is
        // the boundary where those lines were.
        const removal = removalLines(self.syntax, doc, unit_chain.items, lines);
        if (dest.pos > removal.start and dest.pos < removal.end) dest.pos = removal.start;
        var dest_walk: std.ArrayList(AST.Node.Id) = .empty;
        defer dest_walk.deinit(allocator);
        try prefixChain(allocator, self.syntax, doc, dest_chain.items, &dest_walk);
        var prefix: std.ArrayList(u8) = .empty;
        defer prefix.deinit(allocator);
        _ = try locate.continuationPrefixAlong(allocator, doc, dest_walk.items, &prefix);
        const blank_prefix = std.mem.trimEnd(u8, prefix.items, " \t");

        // The line that keeps the block apart from a neighbour in its new
        // container, or nothing where the format has no such line.
        var sep: std.ArrayList(u8) = .empty;
        defer sep.deinit(allocator);
        const separates = self.syntax.block_separator != null and
            !(unit_is_item and isTightList(doc.ast.nodes[dest.container].kind));
        if (separates) {
            try sep.appendSlice(allocator, blank_prefix);
            if (self.syntax.list_attach) |attach| {
                if (isListItem(doc.ast.nodes[dest.container].kind)) try sep.appendSlice(allocator, attach);
            }
            try sep.appendSlice(allocator, self.syntax.block_separator.?);
        }

        // The prefix the block's OLD containers put on its lines, to strip.
        var old_walk: std.ArrayList(AST.Node.Id) = .empty;
        defer old_walk.deinit(allocator);
        try prefixChain(allocator, self.syntax, doc, ancestors, &old_walk);
        var old_prefix: std.ArrayList(u8) = .empty;
        defer old_prefix.deinit(allocator);
        const old_cols = try locate.continuationPrefixAlong(allocator, doc, old_walk.items, &old_prefix);
        const own_start = if (doc.markerSpan(unit)) |m| m.start else blockContent(doc, unit).start;

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // A tight item's bare text takes a paragraph around it where a block
        // joins it in a format whose paragraphs are delimited: `<li>x</li>`
        // gaining a `<p>` under it is `<li><p>x</p><p>y</p></li>`, which is
        // what its list being loose means there, and what the parser reads
        // as a paragraph without its tags is exactly a paragraph whose span
        // is its content. A format that separates blocks by a line has
        // nothing to wrap — Markdown's `- x` is a tight item's spelling and a
        // loose one's alike.
        var rewrap: ?Span = null;
        var rewrapped: Writer.Allocating = .init(allocator);
        defer rewrapped.deinit();
        if (self.syntax.block_separator == null and isListItem(doc.ast.nodes[dest.container].kind)) {
            if (dest.prev) |p| if (isBareParagraph(doc, p)) {
                rewrap = try self.rewrapBlock(p, &rewrapped.writer);
            };
        }

        // Three edits, at most, applied in source order over the one region
        // they span: the removal, the rewrap, and the insertion at `pos`. The
        // kept bytes between them are copied through, so the whole move is
        // one splice — one reparse, one undo step.
        var edits: [3]MoveEdit = undefined;
        var n: usize = 0;
        edits[n] = .{ .span = removal, .kind = .removal };
        n += 1;
        if (rewrap) |r| {
            edits[n] = .{ .span = r, .kind = .rewrap };
            n += 1;
        }
        edits[n] = .{ .span = Span.init(dest.pos, dest.pos), .kind = .insert };
        n += 1;
        std.mem.sort(MoveEdit, edits[0..n], {}, MoveEdit.before);
        const region_start = edits[0].span.start;
        const region_end = edits[n - 1].span.end;

        var scratch: std.ArrayList(u8) = .empty;
        defer scratch.deinit(allocator);
        var cur = region_start;
        for (edits[0..n], 0..) |e, i| {
            if (e.span.start < cur) return error.NotEditable;
            try out.appendSlice(allocator, src[cur..e.span.start]);
            cur = e.span.end;
            switch (e.kind) {
                .removal => {},
                .rewrap => try out.appendSlice(allocator, std.mem.trimEnd(u8, rewrapped.written(), "\r\n")),
                .insert => {
                    // What precedes the insertion once the edits before it
                    // are applied, and what follows it: the two lines the
                    // separators are decided against.
                    const before = try lastLineOf(allocator, src[0..region_start], out.items, &scratch);
                    if (!before.terminated) try out.append(allocator, '\n');
                    if (separates and dest.prev != null and !before.empty and !isSeparatorLine(self.syntax, before.body)) {
                        try out.appendSlice(allocator, sep.items);
                    }
                    try self.appendMovedLines(&out, lines, ancestors, unit_is_item, dest.container, old_cols, own_start, prefix.items, blank_prefix);
                    const rest_to = if (i + 1 < n) edits[i + 1].span.start else src.len;
                    const rest_from = if (i + 1 < n) edits[i + 1].span.end else src.len;
                    const after = firstLineOf(src[e.span.end..rest_to], src[rest_from..]);
                    if (separates and dest.next != null and after.len != 0 and !isSeparatorLine(self.syntax, after)) {
                        try out.appendSlice(allocator, sep.items);
                    }
                },
            }
        }
        try out.appendSlice(allocator, src[cur..region_end]);

        // A document that did not end in a line end does not gain one for a
        // block landing at its end.
        if (region_end == src.len and src.len > 0 and src[src.len - 1] != '\n' and
            out.items.len > 0 and out.items[out.items.len - 1] == '\n') out.items.len -= 1;

        return self.commitSplice(region_start, region_end, out.items);
    }

    /// One of `moveBlock`'s edits, in source order.
    const MoveEdit = struct {
        span: Span,
        kind: enum { removal, rewrap, insert },

        /// Source order; an insertion at the start of a removal goes first,
        /// since it lands where the removed lines began.
        fn before(_: void, a: MoveEdit, b: MoveEdit) bool {
            if (a.span.start != b.span.start) return a.span.start < b.span.start;
            return a.span.end < b.span.end;
        }
    };

    /// The block's lines, each behind the destination prefix, appended to
    /// `out`. An item landing outside a list brings its list's own opening
    /// and closing lines with it — the bytes a list has apart from its items,
    /// which is `<ul>`/`</ul>` in HTML and nothing in a format whose list is
    /// spelled by its items' markers.
    fn appendMovedLines(
        self: *Editor,
        out: *std.ArrayList(u8),
        lines: Span,
        ancestors: []const AST.Node.Id,
        unit_is_item: bool,
        container: AST.Node.Id,
        old_cols: usize,
        own_start: usize,
        prefix: []const u8,
        blank_prefix: []const u8,
    ) Allocator.Error!void {
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;
        const src = doc.source;
        var wrap: ListWrap = .{};
        if (unit_is_item and !isList(doc.ast.nodes[container].kind)) {
            wrap = listWrap(doc, ancestors[ancestors.len - 1], lines);
        }
        var first = true;
        for ([_]Span{ wrap.open, lines, wrap.close }) |part| {
            var at = part.start;
            while (at < part.end) {
                const le = locate.lineEndAt(src, at);
                const body = locate.lineBody(src[at..le]);
                var strip = stripColumns(body, old_cols);
                if (first) {
                    // Its first line also sheds the markers its ancestors open
                    // there, which may reach past their continuation width —
                    // an outer item's `- ` ahead of the inner item being moved.
                    for (ancestors) |a| {
                        if (doc.markerSpan(a)) |m| {
                            if (m.start >= at and m.start < le and m.end - at > strip) strip = m.end - at;
                        }
                    }
                    if (own_start > at and own_start - at < strip) strip = own_start - at;
                }
                const rest = body[strip..];
                if (locate.isBlankLine(rest)) {
                    try out.appendSlice(allocator, blank_prefix);
                } else {
                    try out.appendSlice(allocator, prefix);
                    try out.appendSlice(allocator, rest);
                }
                try out.append(allocator, '\n');
                first = false;
                at = le;
            }
        }
    }

    /// Print `block` through the format's renderer into `w`, returning the
    /// span the print replaces. `error.UnsupportedFormat` with no renderer,
    /// which `supports` rules out for the one format this is reached in.
    fn rewrapBlock(self: *Editor, block: AST.Node.Id, w: *Writer) Error!Span {
        const render = self.syntax.renderBlock orelse return error.UnsupportedFormat;
        const doc = &self.splicer.doc;
        const allocator = self.splicer.allocator;
        var b = AST.Builder.init(allocator);
        defer b.deinit();
        const root = try b.graftSubtree(&doc.ast, block);
        const view = b.view(root);
        try renderNode(allocator, render, &view, root, w);
        return doc.span(block);
    }

    // ── Code blocks ──────────────────────────────────────────────────────────

    /// Toggle a fenced code block over the blocks `[start, end)` covers: fence
    /// them if the caret isn't in a code block, unfence the one it is in if it
    /// is. `lang` is the info string to tag the opening fence with, ignored when
    /// unfencing.
    ///
    /// Fencing is an INSERTION at the covered region's edges, not a rewrite of
    /// its lines: the body is source that already parsed where it sits, and its
    /// enclosing container's prefix is already on its lines, so leaving them
    /// alone is what keeps a fence inside a quote working (`> a` becomes
    /// `` > ``` ``/`> a`/`` > ``` ``). Only the two fence lines are minted, and
    /// they carry the containers' prefix so the block stays inside them.
    ///
    /// INSIDE A LIST ITEM that prefix is not on every line: an item holds its
    /// content by INDENTATION to its content column, and its marker is on its
    /// first line alone. So the first line's HEAD — every marker opening on it,
    /// `- `, `1. `, `> - `, a task item's `- [ ] ` — moves onto the opening
    /// fence, and the body's first line takes the continuation in its place:
    /// `- a` becomes `` - ``` ``/`  a`/`` ``` `` two columns in. The fence sits
    /// at the item's content column and the block stays the item's; a fence at
    /// column zero would have swallowed the `- ` into the code body instead. A
    /// task item's content column is its list marker's, not its box's — the box
    /// stays on the fence line and the item stays a task. The one other line
    /// rewritten is a LAZY one, a paragraph continuation that left its
    /// containers' prefix off: a code body cannot, so it is given it.
    ///
    /// Where the format attaches a block to an item by a line of its own
    /// (`Syntax.list_attach`, AsciiDoc's `+`), an attached block's lines are at
    /// column zero and fence there. The item's own first line is its principal
    /// text, where a fence is only more text, so that is `error.NotEditable`.
    ///
    /// The fence is measured, not fixed: it is one byte longer than the longest
    /// run of the fence character anywhere in the body, so fencing text that
    /// itself holds a fence nests instead of closing early. `CodeFence.min` is
    /// the floor.
    ///
    /// Unfencing peels the opening line and — when it is one — the closing fence
    /// line, leaving the interior verbatim but for its first line, which takes
    /// the opening line's head back in place of its continuation: `- ```` over
    /// `  a` is `- a` again. An item's first line is never left blank where
    /// something follows (an item opening on a blank line holds nothing past
    /// the next blank one): the body's leading blank lines are passed over,
    /// and an empty body keeps the item, empty, with a block after it in the
    /// item brought up under the marker. A Markdown INDENTED code block has no
    /// fences to peel, so it is dedented by up to four spaces a line instead;
    /// that is the same construct with a different spelling, and refusing it
    /// would make the toggle irreversible on a document that merely happens to
    /// use the older form.
    ///
    /// Unfencing is the one gesture here that can produce something other than
    /// what it removed: a code body is by definition text the parser did not
    /// read as markup, so `# x` inside a fence becomes a heading once the fence
    /// is gone. That is what unfencing MEANS, not a defect — but it is why this
    /// is a toggle over whole blocks rather than an "unwrap" that promises to
    /// give the same tree back.
    ///
    /// TWO SPELLINGS, in `setBlock`'s order: the fence above where the format
    /// has one, and where it has none but carries a `renderBlock`, a
    /// `code_block` node the format prints — see `toggleCodeBlockByRender`.
    pub fn toggleCodeBlock(self: *Editor, span: Span, lang: ?[]const u8) Error!void {
        try self.checkRange(span.start, span.end);
        const fence = self.syntax.code_fence orelse {
            if (self.syntax.renderBlock) |render| return self.toggleCodeBlockByRender(span, lang, render);
            return error.UnsupportedFormat;
        };
        if (lang) |l| try checkInfoString(fence, l);

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, span.start, &chain);

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        if (locate.innermostOfKind(doc, chain.items, .code_block)) |cb| {
            const at = std.mem.indexOfScalar(AST.Node.Id, chain.items, cb).?;
            var frame = try CodeFrame.init(allocator, self.syntax, doc, chain.items[0..at]);
            defer frame.deinit(allocator);
            const b = doc.span(cb);
            const region_start = locate.lineStartAt(src, b.start);
            const region_end = locate.lineEndAt(src, b.end -| 1);
            const end = try buildUnfence(allocator, doc, &frame, cb, region_start, region_end, fence, &out);
            return self.commitSplice(region_start, end, out.items);
        }

        const blocks = coveredBlocks(allocator, doc, span.start, span.end) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            else => return error.NoBlock,
        };
        defer allocator.free(blocks.chain);

        const region_start = locate.lineStartAt(src, doc.span(blocks.first).start);
        const region_end = locate.lineEndAt(src, doc.span(blocks.last).end -| 1);
        const at = std.mem.indexOfScalar(AST.Node.Id, blocks.chain, blocks.first) orelse return error.NoBlock;
        var frame = try CodeFrame.init(allocator, self.syntax, doc, blocks.chain[0..at]);
        defer frame.deinit(allocator);
        // An item's own first line, where the format attaches its blocks by a
        // line rather than an indent: that line is the item's principal text,
        // and a fence written after its marker is text too — refused rather
        // than mangled. See the doc comment.
        if (self.syntax.list_attach != null and frame.opensItem(doc, region_start)) return error.NotEditable;
        const width = fenceWidth(src[region_start..region_end], fence.char, fence.min);

        // The opening fence takes the first line's head — the markers opening
        // there, a list item's among them — and the body's first line takes
        // the continuation in its place. Where nothing opens on it, the fence
        // takes the continuation and the line is like any other.
        const head = frame.headLen(doc, region_start);
        if (head) |h| {
            try out.appendSlice(allocator, src[region_start .. region_start + h]);
        } else {
            try out.appendSlice(allocator, frame.prefix.items);
        }
        try out.appendNTimes(allocator, fence.char, width);
        if (lang) |l| try out.appendSlice(allocator, l);
        try out.append(allocator, '\n');
        var line_start = region_start;
        while (line_start < region_end) {
            const line_end = locate.lineEndAt(src, line_start);
            const body = locate.lineBody(src[line_start..line_end]);
            const ending = src[line_start + body.len .. line_end];
            const st = frame.strip(body);
            if (line_start == region_start and head != null) {
                try frame.writeLine(allocator, &out, 0, body[head.?..], ending);
            } else if (st.full) {
                try out.appendSlice(allocator, src[line_start..line_end]);
            } else {
                // A lazy continuation line: a paragraph may leave its
                // container's prefix off, a code body may not.
                try frame.writeLine(allocator, &out, 0, body[st.len..], ending);
            }
            line_start = line_end;
        }
        // An unterminated last line would otherwise fuse with the closing fence.
        if (out.items[out.items.len - 1] != '\n') try out.append(allocator, '\n');
        try out.appendSlice(allocator, frame.prefix.items);
        try out.appendNTimes(allocator, fence.char, width);
        try out.append(allocator, '\n');

        return self.commitSplice(region_start, region_end, out.items);
    }

    /// Retag the code block at `offset` with `lang`, or clear its info string
    /// when `lang` is null — the "language" dropdown next to a code block.
    ///
    /// Only the info string is rewritten; the fence's own width is kept, because
    /// it was measured against a body this gesture doesn't touch. An INDENTED
    /// Markdown code block is `error.NotEditable`: it has no fence, so it has
    /// nowhere to carry a language (convert it with `toggleCodeBlock` twice).
    ///
    /// Where the format has no fence but a `renderBlock`, the block is
    /// rebuilt with the new language and printed — see
    /// `setCodeLanguageByRender`.
    pub fn setCodeLanguage(self: *Editor, offset: usize, lang: ?[]const u8) Error!void {
        const fence = self.syntax.code_fence orelse {
            if (self.syntax.renderBlock) |render| return self.setCodeLanguageByRender(offset, lang, render);
            return error.UnsupportedFormat;
        };
        if (lang) |l| try checkInfoString(fence, l);

        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, offset, &chain);
        const cb = locate.innermostOfKind(doc, chain.items, .code_block) orelse return error.NoBlock;

        const line_start = locate.lineStartAt(src, doc.span(cb).start);
        const line = src[line_start..locate.lineEndAt(src, line_start)];
        const f = fenceAt(line, fence.char, fence.min) orelse return error.NotEditable;

        const info_start = line_start + f.start + f.width;
        const info_end = line_start + locate.lineBody(line).len;
        return self.commitSplice(info_start, info_end, lang orelse "");
    }

    /// `toggleCodeBlock` over a format with no fence but a fragment renderer:
    /// BUILD the code block and let the format print it, or take one apart.
    ///
    /// The fence path works on SOURCE — the covered lines go inside the fence
    /// verbatim, and unfencing gives them back — because in a lightweight
    /// format a paragraph's source is its text. Here it is not (`<p>a
    /// &amp; b</p>`), so the payload is the covered blocks' TEXT
    /// (`blockTextInto`): the selection's words, with its marks dropped, is
    /// what a listing made from prose holds. And toggling OFF puts the
    /// listing's text under a paragraph the format prints, so `a < b` reads
    /// back as those five characters and mints no markup — the one thing the
    /// fence path's "unfencing means the body is read as source" cannot
    /// promise, and the reason this direction is a render too.
    ///
    /// A paragraph in, the same paragraph out: toggle twice on plain prose and
    /// the document is what it was. Prose carrying marks loses them on the way
    /// in, which is what a code block MEANS.
    ///
    /// Nothing here is about a list item: a wrapping pair has no column to
    /// keep, where a fence has its item's content column.
    fn toggleCodeBlockByRender(self: *Editor, span: Span, lang: ?[]const u8, render: RenderBlockFn) Error!void {
        if (lang) |l| try checkLanguageToken(l);

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const doc = &self.splicer.doc;
        const ast = self.astView();

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, span.start, &chain);
        if (locate.innermostOfKind(doc, chain.items, .code_block)) |cb| {
            var b = AST.Builder.init(allocator);
            defer b.deinit();
            const text = try b.addLeaf(.{ .str = ast.nodes[cb].kind.code_block.text });
            const root = try b.addContainer(.para, &.{text});
            const t = doc.span(cb);
            return self.spliceRendered(&b, root, render, t.start, blockSpanEnd(src, t));
        }

        const blocks = coveredBlocks(allocator, doc, span.start, span.end) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            else => return error.NoBlock,
        };
        defer allocator.free(blocks.chain);

        var text: std.ArrayList(u8) = .empty;
        defer text.deinit(allocator);
        var cur: ?AST.Node.Id = blocks.first;
        while (cur) |c| : (cur = if (c == blocks.last) null else ast.nodes[c].next_sibling) {
            if (c != blocks.first) try text.append(allocator, '\n');
            try blockTextInto(allocator, ast, c, &text);
        }

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        const root = try b.addLeaf(.{ .code_block = .{ .lang = lang, .text = text.items } });
        const first = doc.span(blocks.first);
        const last = doc.span(blocks.last);
        return self.spliceRendered(&b, root, render, first.start, blockSpanEnd(src, last));
    }

    /// `setCodeLanguage` over a format with no fence but a fragment renderer:
    /// the block is rebuilt with `lang` over the same text — its attributes
    /// along — and printed in its place.
    fn setCodeLanguageByRender(self: *Editor, offset: usize, lang: ?[]const u8, render: RenderBlockFn) Error!void {
        if (lang) |l| try checkLanguageToken(l);

        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;
        const ast = self.astView();

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, offset, &chain);
        const cb = locate.innermostOfKind(doc, chain.items, .code_block) orelse return error.NoBlock;

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        const root = try b.addLeaf(.{ .code_block = .{ .lang = lang, .text = ast.nodes[cb].kind.code_block.text } });
        if (ast.nodes[cb].attrs) |ai| try b.setAttrs(root, ast.attrs[ai]);
        const t = doc.span(cb);
        return self.spliceRendered(&b, root, render, t.start, blockSpanEnd(src, t));
    }

    // ── Task list checkboxes ─────────────────────────────────────────────────
    // A checkbox is not a node an editor wraps a range in: it is a marker on an
    // ITEM, so every gesture here is addressed by a caret `offset` and edits the
    // few bytes after that item's list marker. Nothing else on the line moves —
    // in particular the box is INLINE CONTENT of the item's first paragraph, not
    // part of the marker, so adding or removing one leaves the item's
    // continuation-line indentation alone (unlike `toggleBlockContainer`, which
    // has to re-indent).

    /// Add a checkbox to the list item at `offset`, or take one away — the
    /// gesture that converts between a `list_item` and a `task_list_item`. A box
    /// is added UNCHECKED; use `setTaskChecked` to tick it.
    ///
    /// `error.NoBlock` when `offset` is in no list item, `error.NotEditable`
    /// when the item's line carries no recognizable list marker to hang a box
    /// off (a lazy continuation line).
    pub fn toggleTaskItem(self: *Editor, offset: usize) Error!void {
        const tm = self.syntax.task_marker orelse return error.UnsupportedFormat;
        const found = try self.locateTaskBox(offset);
        if (found.box) |b| return self.commitSplice(b.start, b.end + found.space, "");

        const allocator = self.splicer.allocator;
        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);
        try out.appendSlice(allocator, tm.unchecked);
        try out.appendSlice(allocator, tm.space);
        return self.commitSplice(found.at, found.at, out.items);
    }

    /// Tick or untick the task item at `offset` — a click on the checkbox.
    ///
    /// Rewrites the BOX ALONE, never the space after it, so an item spelled with
    /// unusual spacing keeps it. A no-op (no edit, no undo step) when the box is
    /// already in the requested state. `error.NotEditable` when the item has no
    /// box: minting one here would make a "set checked" call silently convert a
    /// bullet into a task, which is `toggleTaskItem`'s job to do explicitly.
    pub fn setTaskChecked(self: *Editor, offset: usize, checked: bool) Error!void {
        const tm = self.syntax.task_marker orelse return error.UnsupportedFormat;
        const found = try self.locateTaskBox(offset);
        const b = found.box orelse return error.NotEditable;
        if (found.checked == checked) return;
        return self.commitSplice(b.start, b.end, if (checked) tm.checked else tm.unchecked);
    }

    /// Flip the task item at `offset` — `setTaskChecked` against its current
    /// state, which is what a checkbox click actually is when the caller doesn't
    /// already know the state.
    pub fn toggleTaskChecked(self: *Editor, offset: usize) Error!void {
        if (self.syntax.task_marker == null) return error.UnsupportedFormat;
        const found = try self.locateTaskBox(offset);
        if (found.box == null) return error.NotEditable;
        return self.setTaskChecked(offset, !found.checked);
    }

    /// Where the checkbox on the list item at `offset` is, or would go. The
    /// shared body of the three gestures above.
    fn locateTaskBox(self: *Editor, offset: usize) Error!TaskBox {
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const tm = self.syntax.task_marker orelse return error.UnsupportedFormat;
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, offset, &chain);

        // The innermost item of EITHER kind, walked directly rather than through
        // two `innermostOfKind` calls: a plain item can gain a box and a task
        // item can lose one, so both are targets and whichever is deeper wins.
        var item: ?AST.Node.Id = null;
        var i = chain.items.len;
        while (i > 0) {
            i -= 1;
            switch (std.meta.activeTag(doc.ast.nodes[chain.items[i]].kind)) {
                .list_item, .task_list_item => {
                    item = chain.items[i];
                    break;
                },
                else => {},
            }
        }
        const id = item orelse return error.NoBlock;

        // The item's FIRST line, which is the only one a marker can open. Djot
        // starts a quoted item at its marker and Markdown at column 0, so the
        // line start is taken from the span rather than the span itself.
        const line_start = locate.lineStartAt(src, doc.span(id).start);
        const line = src[line_start..locate.lineEndAt(src, line_start)];
        var from: usize = 0;
        while (skipQuoteMarker(line, from)) |j| from = j;
        const m = listMarkerAt(line, from) orelse return error.NotEditable;

        const at = line_start + m.end;
        const rest = line[m.end..];
        const width = tm.unchecked.len;
        if (rest.len >= width and rest[0] == '[' and rest[width - 1] == ']') {
            // The box's INTERIOR, read rather than compared against the two
            // spellings: source in the wild writes `[X]` as well as `[x]`, and a
            // literal match would call the capital form "not a checkbox at all"
            // and hand the caller a second box to insert beside it.
            const inner = rest[1 .. width - 1];
            const checked = std.mem.indexOfAny(u8, inner, tm.checked_chars) != null;
            if (checked or locate.isBlankLine(inner)) return .{
                .at = at,
                .box = Span.init(at, at + width),
                // However the author spaced it — removing the box takes the
                // separator with it, ticking it leaves the separator alone.
                .space = if (rest.len > width and rest[width] == ' ') 1 else 0,
                .checked = checked,
            };
        }
        return .{ .at = at, .box = null, .space = 0, .checked = false };
    }

    // ── Tables ───────────────────────────────────────────────────────────────
    // A table is a grid, not a run of delimited text, so its edits are grid
    // surgery (add/remove/move a row or column, set a column's alignment) rather
    // than a toggle. The grid is lifted from the AST, mutated, and the whole
    // table re-spelled in one splice — see `table_edit.zig`. Every gesture is
    // addressed by a caret `offset`; the cell it lands in is the anchor.

    /// Insert an empty row below (`after`) or above the caret's row.
    pub fn tableInsertRow(self: *Editor, offset: usize, after: bool) Error!void {
        return self.tableEdit(offset, .{ .insert_row = if (after) .after else .before });
    }

    /// Delete the caret's row. `error.NotEditable` for a header row or the last
    /// body row (a table keeps a header and at least one body row).
    pub fn tableDeleteRow(self: *Editor, offset: usize) Error!void {
        return self.tableEdit(offset, .delete_row);
    }

    /// Insert an empty column right (`after`) or left of the caret's column.
    pub fn tableInsertColumn(self: *Editor, offset: usize, after: bool) Error!void {
        return self.tableEdit(offset, .{ .insert_column = if (after) .after else .before });
    }

    /// Delete the caret's column. `error.NotEditable` when it is the only one.
    pub fn tableDeleteColumn(self: *Editor, offset: usize) Error!void {
        return self.tableEdit(offset, .delete_column);
    }

    /// Set the caret's column to `alignment`.
    pub fn tableSetAlignment(self: *Editor, offset: usize, alignment: AST.Alignment) Error!void {
        return self.tableEdit(offset, .{ .set_alignment = alignment });
    }

    /// Move the caret's row one place down (`down`) or up, within the body rows.
    pub fn tableMoveRow(self: *Editor, offset: usize, down: bool) Error!void {
        return self.tableEdit(offset, .{ .move_row = if (down) .after else .before });
    }

    /// Move the caret's column one place right (`right`) or left.
    pub fn tableMoveColumn(self: *Editor, offset: usize, right: bool) Error!void {
        return self.tableEdit(offset, .{ .move_column = if (right) .after else .before });
    }

    const TableOp = union(enum) {
        insert_row: table_edit.Side,
        delete_row,
        insert_column: table_edit.Side,
        delete_column,
        set_alignment: AST.Alignment,
        move_row: table_edit.Side,
        move_column: table_edit.Side,
    };

    /// Lift the table at `offset`, apply one grid op, and splice the rebuilt
    /// table back — the shared body of every table gesture above.
    ///
    /// The spelling is fetched FIRST, before the grid is extracted and long
    /// before anything is spliced, and that ordering is the whole fix rather
    /// than a tidiness: a format can have a table this file can READ and no
    /// table it can WRITE. HTML is exactly that — `html/parser.zig` lowers
    /// `<table>/<tr>/<td>` to the same `table`/`row`/`cell` nodes a pipe table
    /// produces, so extraction succeeded and the rebuilt pipe text went over the
    /// `<table>…</table>` region, which HTML then reparsed as a paragraph. A
    /// document that still parses is one the splicer will not roll back, so the
    /// table was destroyed without an error anywhere.
    fn tableEdit(self: *Editor, offset: usize, op: TableOp) Error!void {
        const spelling = self.syntax.table_spelling orelse return error.UnsupportedFormat;
        const src = self.sourceBytes();
        if (offset > src.len) return error.InvalidRange;
        const allocator = self.splicer.allocator;

        var grid = table_edit.extract(allocator, &self.splicer.doc, offset) catch |e| return mapTableErr(e);
        defer grid.deinit();

        (switch (op) {
            .insert_row => |s| table_edit.insertRow(&grid, s),
            .delete_row => table_edit.deleteRow(&grid),
            .insert_column => |s| table_edit.insertColumn(&grid, s),
            .delete_column => table_edit.deleteColumn(&grid),
            .set_alignment => |a| table_edit.setAlignment(&grid, a),
            .move_row => |d| table_edit.moveRow(&grid, d),
            .move_column => |d| table_edit.moveColumn(&grid, d),
        }) catch |e| return mapTableErr(e);

        const bytes = table_edit.emit(allocator, &grid, spelling) catch |e| return mapTableErr(e);
        defer allocator.free(bytes);
        return self.commitSplice(grid.region.start, grid.region.end, bytes);
    }

    /// Map a `table_edit` error onto the `Editor` error set: "not in a table"
    /// reads as no block for the gesture, a refused (degenerate) edit as not
    /// editable.
    fn mapTableErr(e: table_edit.Error) Error {
        return switch (e) {
            error.NotInTable => error.NoBlock,
            error.Refused => error.NotEditable,
            error.OutOfMemory => error.OutOfMemory,
        };
    }

    // ── Links ──────────────────────────────────────────────────────────────

    /// Link `[start, end)` to `destination`, or repoint the link already there.
    ///
    /// Decisions:
    ///   * An EXISTING link covering the range has its destination REPLACED, its
    ///     text kept. Re-linking is the common gesture (fix a URL), and it keeps
    ///     the op idempotent instead of nesting `[[t](a)](b)`. Removing a link is
    ///     already `Splicer.unwrapNode`, which peels a node to its interior.
    ///   * A RANGE INSIDE an existing autolink re-points it the same way, but
    ///     there is no text to keep: an autolink's text IS its destination, so
    ///     the node is respelled whole for the new one (canonically — see below
    ///     — so a `<url>` re-pointed at a relative path becomes `[dest](dest)`,
    ///     not a broken `<>`). Without this the op reads the URL as ordinary text
    ///     and splices a link into the middle of it:
    ///     `<https<https://y.dev>://x.dev>`.
    ///
    ///     This covers a caret AND any selection the autolink contains —
    ///     including one covering it exactly. An autolink's URL is not editable
    ///     text: no part of it can host a `[`, so "link half this URL" has no
    ///     spelling, and the selection carries no text a splice could keep.
    ///
    ///     A selection that starts or ends strictly INSIDE an autolink but isn't
    ///     contained by it (`see <https://x` … `.dev> ok`) is refused with
    ///     `error.NotEditable`: half of it is real text, so there is nothing to
    ///     re-point, and any splice would rewrite the URL. Refusing beats
    ///     silently changing the caller's URL, for the same reason a newline
    ///     destination is `error.InvalidDestination`.
    ///
    ///     A range inside BOTH a link and an autolink (`[<https://x.dev>](d)`)
    ///     re-points the link, not the autolink: a link's text is separable from
    ///     its destination, so re-pointing it keeps text that re-pointing the
    ///     autolink would discard.
    ///
    ///     A range that CONTAINS an autolink whole plus text around it is
    ///     untouched by all of the above — it splices at the autolink's edges,
    ///     corrupting nothing, and the autolink stays as the new link's text.
    ///   * A link with NO TEXT gets the canonical spelling for the destination it
    ///     was given, never `[](dest)`: a childless link has nothing to render,
    ///     so consumers fall back to showing the destination and the caret has
    ///     nowhere correct to sit. Where the format can spell an autolink it gets
    ///     `<dest>`; where it can't it gets `[dest](dest)`, the destination
    ///     doubling as text so it stays visible and editable. Which destinations
    ///     autolink, and how each format spells one, is twig's knowledge — a
    ///     consumer guessing would turn `<foo>` into raw HTML (Markdown) or
    ///     literal text (both). See `Syntax.spellsAutolink`.
    ///   * A destination is escaped per format (see `writeLinkDestination`); a
    ///     newline in one is `error.InvalidDestination`, since neither format can
    ///     hold it (Djot strips it, Markdown's `<…>` form forbids it) and
    ///     silently changing the caller's URL is worse than refusing.
    ///
    /// TWO SPELLINGS, in `setBlock`'s order: `[text](dest)` from the escape
    /// alphabets where the format has them, and where it has none but carries
    /// a `renderBlock`, a `link` node over the selection's inline nodes that
    /// the format prints — see `insertLinkByRender`.
    pub fn insertLink(self: *Editor, span: Span, dest: []const u8) Error!void {
        try self.checkRange(span.start, span.end);
        if (std.mem.indexOfAny(u8, dest, "\r\n") != null) return error.InvalidDestination;
        // A format with no link spelling refuses before anything else is read.
        if (self.syntax.link_text_escapes == null) {
            if (self.syntax.renderBlock) |render| return self.insertLinkByRender(span, dest, render, .link);
            return error.UnsupportedFormat;
        }

        const start = span.start;
        const end = span.end;
        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        const ast = self.astView();

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, &self.splicer.doc, start, &chain);

        // The text to sit in the brackets, and the span the rebuilt link
        // replaces. Re-pointing an existing link rebuilds the whole node: a
        // destination is a string payload with no span of its own, so there is
        // nothing smaller to splice (see `splicer.zig`'s module doc).
        var text: []const u8 = src[start..end];
        var target = Span.init(start, end);
        var repoint = locate.innermostCovering(&self.splicer.doc, chain.items, &.{.link}, start, end);
        if (repoint == null) repoint = autolinkCovering(&self.splicer.doc, chain.items, start, end);
        // Not covered by an autolink, but still landing inside one: the range
        // runs from ordinary text into the middle of a URL (either end can be the
        // one inside). There is nothing to re-point — half the selection is real
        // text — and no way to spell the result, so refuse rather than corrupt
        // the URL.
        if (repoint == null and start != end) {
            const splits =
                (try splitsAutolink(allocator, &self.splicer.doc, start)) or
                (try splitsAutolink(allocator, &self.splicer.doc, end));
            if (splits) return error.NotEditable;
        }
        if (repoint) |id| {
            const node = ast.nodes[id];
            const rp = self.splicer.doc.span(id);
            if (rp.start == 0 and rp.end == 0) return error.NotEditable;
            // An autolink has no `[text]` half: the text it shows is the OLD
            // destination, so keeping it would spell the new link with the URL it
            // was meant to replace. Empty text sends it through the canonical
            // spelling below, exactly as a caret on bare text goes.
            text = switch (node.kind) {
                // An autolink's visible text IS its destination; the caller
                // supplies that, so the node contributes nothing.
                .text_leaf => |l| if (l.kind == .url or l.kind == .email) "" else if (self.splicer.doc.contentSpan(id)) |cs| src[cs.start..cs.end] else "",
                else => if (self.splicer.doc.contentSpan(id)) |cs| src[cs.start..cs.end] else "",
            };
            target = rp;
        }

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        // Keyed on the TEXT being empty, not the range: re-pointing an existing
        // `[](old)` is an empty range too, and it has the same childless link to
        // avoid. A non-empty range always carries text, so it never lands here.
        if (text.len == 0) {
            if (self.syntax.spellsAutolink) |spells| {
                try out.append(allocator, '<');
                try out.appendSlice(allocator, dest);
                try out.append(allocator, '>');
                // Ask about the exact bytes we would emit, so the test and the
                // output cannot disagree about what was spelled.
                if (spells(out.items)) return self.commitSplice(target.start, target.end, out.items);
                out.clearRetainingCapacity();
            }
        }

        try out.append(allocator, '[');
        if (text.len == 0) {
            // `dest` is a raw string being repurposed as text, so it needs
            // escaping for that position — unlike `text`, which is already source
            // the author (or a prior parse) spelled and which must be copied
            // through verbatim.
            try writeLinkText(allocator, self.syntax, dest, &out);
        } else {
            try out.appendSlice(allocator, text);
        }
        try out.appendSlice(allocator, "](");
        try writeLinkDestination(allocator, self.syntax, dest, &out);
        try out.append(allocator, ')');

        return self.commitSplice(target.start, target.end, out.items);
    }

    /// Spell `[start, end)` as an image pointing at `dest` — `![alt](dest)`, the
    /// selected source becoming the alt text.
    ///
    /// Shares `insertLink`'s destination spelling, which is the whole reason this
    /// belongs here rather than in a caller's format string: an image destination
    /// is the *same grammar production* as a link's, so it needs the same
    /// per-format treatment — Markdown moving a destination that holds whitespace
    /// into the `<…>` form, Djot taking it bare because `<…>` means nothing there
    /// and would link the literal characters. A caller spelling `![](my file.png)`
    /// by hand writes something Markdown does not read as an image at all, and
    /// cannot fix without reproducing `writeLinkDestination`.
    ///
    /// Simpler than a link in two ways. There is no autolink form to prefer and no
    /// re-point reasoning: an image has no bare-URL spelling, and re-pointing an
    /// existing one is `imageDestinationAt`-then-insert above this op rather than a
    /// shape to detect here. And empty text stays empty — `![](dest)` is a perfectly
    /// good image, where the `[](dest)` that `insertLink` works to avoid is a link
    /// with nothing to click.
    ///
    /// The same two spellings as `insertLink`: where the format has no
    /// alphabet but a renderer, an `image` node over the selection's inline
    /// nodes — its alt text — is printed instead (`insertLinkByRender`, with
    /// `.image`).
    pub fn insertImage(self: *Editor, span: Span, dest: []const u8) Error!void {
        try self.checkRange(span.start, span.end);
        if (std.mem.indexOfAny(u8, dest, "\r\n") != null) return error.InvalidDestination;
        if (self.syntax.link_text_escapes == null or self.syntax.link_dest_escapes == null) {
            if (self.syntax.renderBlock) |render| return self.insertLinkByRender(span, dest, render, .image);
            return error.UnsupportedFormat;
        }

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();
        // Already-parsed source, copied verbatim — the same distinction
        // `insertLink` draws between a span of the document and a raw argument.
        const text = src[span.start..span.end];

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        try out.appendSlice(allocator, "![");
        try out.appendSlice(allocator, text);
        try out.appendSlice(allocator, "](");
        try writeLinkDestination(allocator, self.syntax, dest, &out);
        try out.append(allocator, ')');

        return self.commitSplice(span.start, span.end, out.items);
    }

    /// `insertLink`/`insertImage` over a format with no link alphabet but a
    /// fragment renderer: BUILD the node over the selection and let the format
    /// print it.
    ///
    /// The alphabet path copies the selected SOURCE between `[` and `]`, which
    /// works because in a lightweight format a selection's source is text the
    /// format reads back. A renderer prints a TREE, so the selection has to be
    /// one: the inline nodes it covers (`coveredInlines`), grafted under a
    /// fresh `link` or `image`. A selection that starts or ends inside a plain
    /// text run takes the run's covered part; one that cuts through anything
    /// else — a mark, a code span, a text run whose source spells characters
    /// as entities so that a byte offset names no character in it — is
    /// `error.NotEditable`, as a range into the middle of an autolink is on the
    /// alphabet path. Refusing beats a link whose text is not what was
    /// selected.
    ///
    /// Re-pointing an existing link keeps its children and attributes and
    /// replaces its destination — the alphabet path's rule, on the tree. An
    /// empty selection gets a link whose text IS the destination, which is
    /// the alphabet path's `[dest](dest)` for a format with no autolink form;
    /// an empty selection makes an image with empty alt text, as `![](dest)`
    /// does.
    fn insertLinkByRender(
        self: *Editor,
        span: Span,
        dest: []const u8,
        render: RenderBlockFn,
        shape: enum { link, image },
    ) Error!void {
        const allocator = self.splicer.allocator;
        const doc = &self.splicer.doc;
        const ast = self.astView();

        var b = AST.Builder.init(allocator);
        defer b.deinit();
        var kids: std.ArrayList(AST.Node.Id) = .empty;
        defer kids.deinit(allocator);
        var target = span;
        var attrs: ?AST.Attrs = null;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, doc, span.start, &chain);
        const repoint = if (shape == .link)
            locate.innermostCovering(doc, chain.items, &.{.link}, span.start, span.end)
        else
            null;
        if (repoint) |id| {
            target = doc.span(id);
            if (target.start == 0 and target.end == 0) return error.NotEditable;
            if (ast.nodes[id].attrs) |ai| attrs = ast.attrs[ai];
            var child = ast.nodes[id].first_child;
            while (child) |c| : (child = ast.nodes[c].next_sibling) {
                try kids.append(allocator, try b.graftSubtree(ast, c));
            }
        } else if (span.end > span.start) {
            const covered = try coveredInlines(allocator, doc, span.start, span.end);
            defer allocator.free(covered);
            for (covered) |piece| {
                try kids.append(allocator, if (piece.text) |t|
                    try b.addLeaf(.{ .str = t })
                else
                    try b.graftSubtree(ast, piece.node));
            }
        }
        if (shape == .link and kids.items.len == 0) try kids.append(allocator, try b.addLeaf(.{ .str = dest }));

        const payload: AST.Node.Kind.Link = .{ .destination = dest, .reference = null };
        const root = try b.addContainer(switch (shape) {
            .link => .{ .link = payload },
            .image => .{ .image = payload },
        }, kids.items);
        if (attrs) |a| try b.setAttrs(root, a);
        return self.spliceRendered(&b, root, render, target.start, target.end);
    }

    // ── Footnotes ────────────────────────────────────────────────────────────

    /// Insert a footnote reference at `offset` and, unless one already exists,
    /// the matching definition at the end of the document.
    ///
    /// Decisions:
    ///   * It writes BOTH HALVES, because in neither format is half a footnote a
    ///     footnote: a bare `[^a]` with nothing defining it renders as the
    ///     literal four characters. That is also why `Syntax.footnote` is one
    ///     field holding both spellings rather than a reference entry in
    ///     `text_leaf_delims` (which is there for the serializer, and which
    ///     `assertCoherent` pins to this).
    ///   * The definition body is left EMPTY, and that parses — both parsers
    ///     read `[^a]: ` as a footnote with no children. The caller then types
    ///     into it like any other block; minting a placeholder body here would be
    ///     text the author has to delete.
    ///   * A label that is ALREADY DEFINED gets only the reference. Referring to
    ///     an existing footnote twice is an ordinary thing to want, and appending
    ///     a second definition for the same label is not — the parsers keep one
    ///     and the other becomes dead source.
    ///   * It is ONE splice, spanning the caret to the end of the document, even
    ///     though the two halves are far apart and the bytes between them are
    ///     rewritten unchanged. Two splices would be less wasteful and strictly
    ///     worse: the pair would take two undo steps to reverse, and `lastChange`
    ///     — the range a caller re-renders from — would describe only the second,
    ///     silently omitting the reference the caret is sitting in.
    ///
    /// `error.InvalidLabel` for a label that is empty or holds a line end or a
    /// reference bracket; `error.UnsupportedFormat` where the format has no
    /// footnotes.
    pub fn insertFootnote(self: *Editor, offset: usize, label: []const u8) Error!void {
        try self.checkRange(offset, offset);
        const fs = self.syntax.footnote orelse return error.UnsupportedFormat;
        if (label.len == 0) return error.InvalidLabel;
        if (std.mem.indexOfAny(u8, label, "\r\n") != null) return error.InvalidLabel;
        if (std.mem.indexOfAny(u8, label, fs.label_forbids) != null) return error.InvalidLabel;

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();

        var out: std.ArrayList(u8) = .empty;
        defer out.deinit(allocator);

        try out.appendSlice(allocator, fs.ref_open);
        try out.appendSlice(allocator, label);
        try out.appendSlice(allocator, fs.ref_close);

        if (footnoteDefined(self.astView(), label)) {
            return self.commitSplice(offset, offset, out.items);
        }

        try out.appendSlice(allocator, src[offset..]);
        // The definition is a block of its own at the end of the document, so it
        // needs a line to itself and a blank line above it.
        if (src.len > 0 and src[src.len - 1] != '\n') try out.append(allocator, '\n');
        if (!endsWithBlankLine(src)) try out.append(allocator, '\n');
        try out.appendSlice(allocator, fs.ref_open);
        try out.appendSlice(allocator, label);
        try out.appendSlice(allocator, fs.ref_close);
        try out.appendSlice(allocator, fs.def_suffix);
        try out.append(allocator, '\n');

        return self.commitSplice(offset, src.len, out.items);
    }

    // ── Math ─────────────────────────────────────────────────────────────────

    /// Insert `formula` at `offset` as an INLINE formula — Markdown's `$x$`,
    /// djot's `` $`x` ``, AsciiDoc's `stem:[x]` — a math leaf whose text is
    /// `formula` byte for byte.
    ///
    /// Decisions:
    ///   * The formula is the HOST's notation (TeX, usually), and twig reads
    ///     none of it. It goes between the format's delimiters with nothing
    ///     escaped, because a math body is read literally: `\$` inside `$…$`
    ///     is two bytes of TeX, not an escaped dollar, and `insertLiteral`'s
    ///     alphabets would write backslashes into the formula.
    ///   * The bytes are the SERIALIZER's, printed through `Syntax.renderBlock`
    ///     from a leaf built with `AST.Builder`, as `insertDirective` prints its
    ///     container. `text_leaf_delims` is the gate and not the spelling: a
    ///     fixed `Delims` pair cannot say how djot's run widens around a
    ///     backtick in the formula, and the serializer already does.
    ///   * A formula the format cannot hold is REFUSED rather than written.
    ///     Markdown's `$` will not open onto a space or close after one, has
    ///     no spelling for an empty formula, and ends at the first `$`
    ///     inside; djot's run holds nearly anything. Rather than restate each
    ///     parser's rule here, the print is parsed ON ITS OWN and must come
    ///     back holding this kind of formula with exactly these bytes —
    ///     `error.InvalidFormula` when it does not.
    ///   * And it must come back IN PLACE. The splice is kept only if the
    ///     edited document holds the formula inside the inserted bytes, so a
    ///     caret in a code span or a code block — where the bytes are code —
    ///     or flush against a `$` that Markdown pairs differently is
    ///     `error.NotEditable`, and nothing changes. This is the one check
    ///     `insertLiteral` cannot make, because a literal has no node to look
    ///     for.
    ///
    /// Gated on the format's inline-math delimiters being authorable and a
    /// renderer to print through, which `assertCoherent` pins together.
    /// Markdown authors one only under `ParseOptions.math`, so an editor over
    /// a document parsed without it gets `error.UnsupportedFormat`, exactly
    /// as `setMarkColor` does without `highlight_colors`. `error.InvalidRange`
    /// for an `offset` past the source.
    pub fn insertInlineMath(self: *Editor, offset: usize, formula: []const u8) Error!void {
        try self.checkRange(offset, offset);
        const render = try self.mathRenderer(.inline_math);
        const allocator = self.splicer.allocator;

        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        const bytes = try self.printFormula(render, .inline_math, formula, &out);
        const want: FormulaCheck = .{
            .kind = .inline_math,
            .formula = formula,
            .within = Span.init(offset, offset + bytes.len),
        };
        return self.commitSpliceChecked(offset, offset, bytes, want.check());
    }

    /// Insert `formula` as a DISPLAY formula — Markdown's `$$x$$`, djot's
    /// `` $$`x` `` — in a paragraph of its own after the block `offset` sits
    /// in.
    ///
    /// Everything `insertInlineMath` decides about the bytes holds here: the
    /// serializer prints them, the formula is written unescaped, a formula the
    /// format cannot hold is `error.InvalidFormula`, and one that does not come
    /// back in place is `error.NotEditable`.
    ///
    /// What differs is the placement, which is `insertThematicBreak`'s,
    /// decision for decision, through the `insertBlockAfter` it shares with the
    /// table and the directive: after the caret's block rather than at the
    /// caret, blank-separated on both sides, a quote's prefix on every line of
    /// a formula that runs over several, and column zero after a list item.
    /// Both formats parse display math as an inline node like any other, so
    /// nothing in either would stop one landing mid-sentence — but a display
    /// formula is set on a line of its own when rendered, and a paragraph
    /// holding only the formula is what that looks like in the source.
    ///
    /// Gated like the inline form, on the display delimiters. AsciiDoc spells
    /// display math with the same `stem:[…]` macro it uses inline and reads it
    /// back as inline, so it authors the inline form and not this one.
    pub fn insertDisplayMath(self: *Editor, offset: usize, formula: []const u8) Error!void {
        try self.checkRange(offset, offset);
        const render = try self.mathRenderer(.display_math);
        const allocator = self.splicer.allocator;

        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        const bytes = try self.printFormula(render, .display_math, formula, &out);

        var block: std.ArrayList(u8) = .empty;
        defer block.deinit(allocator);
        const pos = try self.blockAfter(offset, bytes, &block);
        const want: FormulaCheck = .{
            .kind = .display_math,
            .formula = formula,
            .within = Span.init(pos, pos + block.items.len),
        };
        return self.commitSpliceChecked(pos, pos, block.items, want.check());
    }

    /// The renderer a formula of `kind` is printed through, or
    /// `error.UnsupportedFormat` where the format does not author one.
    fn mathRenderer(self: *const Editor, kind: AST.TextLeafKind) Error!RenderBlockFn {
        if (self.syntax.authorableDelimsFor(.{ .text_leaf = kind }) == null) return error.UnsupportedFormat;
        // `assertCoherent` pins a renderer wherever the claim above is made,
        // so this is the compiler's `orelse` rather than a second gate.
        return self.syntax.renderBlock orelse error.UnsupportedFormat;
    }

    /// Print `formula` as a math leaf of `kind`, alone in a paragraph, and
    /// answer the leaf's bytes — `error.InvalidFormula` unless those bytes,
    /// parsed on their own, come back as that formula.
    fn printFormula(
        self: *Editor,
        render: RenderBlockFn,
        kind: AST.TextLeafKind,
        formula: []const u8,
        out: *Writer.Allocating,
    ) Error![]const u8 {
        const allocator = self.splicer.allocator;
        var b = AST.Builder.init(allocator);
        defer b.deinit();
        // A paragraph around the leaf, because a paragraph is what every
        // renderer is written to print; the paragraph itself adds nothing but
        // the line end trimmed below.
        const leaf = try b.addLeaf(.{ .text_leaf = .{ .kind = kind, .text = formula } });
        const root = try b.addContainer(.para, &.{leaf});
        const view = b.view(root);
        try renderNode(allocator, render, &view, root, &out.writer);
        const bytes = std.mem.trimEnd(u8, out.written(), "\r\n");

        var alone = self.splicer.parse_fn(self.splicer.parse_ctx, allocator, bytes) catch |err| switch (err) {
            error.OutOfMemory => return error.OutOfMemory,
            else => return error.InvalidFormula,
        };
        defer alone.deinit();
        const want: FormulaCheck = .{ .kind = kind, .formula = formula, .within = Span.init(0, bytes.len) };
        if (!want.holds(&alone)) return error.InvalidFormula;
        return bytes;
    }

    /// What a math gesture looks for in the reparse: a leaf of `kind` holding
    /// exactly `formula`, lying within the bytes it wrote.
    const FormulaCheck = struct {
        kind: AST.TextLeafKind,
        formula: []const u8,
        within: Span,

        fn holds(self: *const FormulaCheck, doc: *const Document) bool {
            for (doc.ast.nodes, 0..) |n, i| {
                const leaf = switch (n.kind) {
                    .text_leaf => |l| l,
                    else => continue,
                };
                if (leaf.kind != self.kind or !std.mem.eql(u8, leaf.text, self.formula)) continue;
                const sp = doc.span(@intCast(i));
                if (sp.start >= self.within.start and sp.end <= self.within.end) return true;
            }
            return false;
        }

        fn accept(ctx: *const anyopaque, doc: *const Document) bool {
            const self: *const FormulaCheck = @ptrCast(@alignCast(ctx));
            return self.holds(doc);
        }

        fn check(self: *const FormulaCheck) Splicer.Check {
            return .{ .ctx = self, .accept = accept };
        }
    };

    // ── Literal text ─────────────────────────────────────────────────────────

    /// Insert `text` at `offset` as a LITERAL run: every byte the format reads as
    /// markup is backslash-escaped so the run reparses as exactly `text`, never as
    /// emphasis, a code span, a link, an entity, or — at a line start — a heading,
    /// quote or list. This is the inverse of the serializer, which writes an
    /// already-parsed `str` verbatim: that byte survived a parse in place, whereas
    /// `text` is arbitrary input that has to be MADE safe.
    ///
    /// The escaping is positional. `Syntax.text_escapes` fires anywhere on the
    /// line (`*`, `` ` ``, `[`, `<`…); `Syntax.block_start_escapes` fires only
    /// while `offset` sits in its line's leading whitespace (`#`, `>`, `-`…),
    /// where those bytes open a block — so an inserted "5 - 3" keeps its `-` but
    /// an inserted "- item" at column zero does not become a bullet. An embedded
    /// newline in `text` re-enters that line-start zone for the bytes after it.
    ///
    /// Like the link ops, this guards the inserted run's OWN bytes; it does not
    /// reason about source already flanking `offset`. The shared
    /// splice+reparse+rollback path is the backstop: an insertion that somehow
    /// still corrupts the document yields `error.EditConflict` and changes
    /// nothing. `error.UnsupportedFormat` when the format can spell no literal
    /// (`text_escapes == null`), `error.InvalidRange` when `offset` is past the
    /// source.
    ///
    /// Two constructs a fixed byte-alphabet cannot reach, and so does not: a GFM
    /// bare-URL autolink (`https://x.com`, linkified with no delimiter to
    /// backslash) and an ordered-list marker (`1.`/`1)`, special only after a
    /// digit run, not per-byte). Both mint at most a link or a list from
    /// literal-looking text, never corruption; a caller that must suppress them
    /// does so above this op.
    ///
    /// INSIDE A CODE SPAN, a code block or a raw node the run is written in
    /// `verbatim` position instead: the format reads that body literally, so a
    /// backslash format writes the bytes as they are (an escape there would
    /// SHOW its backslash) and an entity format still escapes. The one byte no
    /// position can hold is the span's own closing delimiter, which is the same
    /// "guards its own bytes" limit as above.
    ///
    /// The engine here decides WHERE each byte lands; the format decides how a
    /// byte is written there. That split is `Syntax.renderText`: Markdown, djot
    /// and AsciiDoc share one renderer over their alphabets, and HTML — which
    /// has no backslash to spell a literal with — writes entities. The editor
    /// never sees either rule.
    pub fn insertLiteral(self: *Editor, offset: usize, text: []const u8) Error!void {
        try self.checkRange(offset, offset);
        const render = self.syntax.renderText orelse return error.UnsupportedFormat;

        const allocator = self.splicer.allocator;
        const src = self.sourceBytes();

        // At a line start iff every byte from this line's start up to `offset` is
        // leading whitespace — the same zone in which a block marker bites.
        var at_line_start = true;
        for (src[locate.lineStartAt(src, offset)..offset]) |c| {
            if (c != ' ' and c != '\t') {
                at_line_start = false;
                break;
            }
        }

        var out: Writer.Allocating = .init(allocator);
        defer out.deinit();
        const in_verbatim = try self.insideVerbatim(offset);
        writeLiteral(self.syntax, render, text, at_line_start, in_verbatim, &out.writer) catch |err| switch (err) {
            error.WriteFailed => return error.OutOfMemory,
        };

        return self.commitSplice(offset, offset, out.written());
    }

    /// Whether writing at `offset` lands inside a body the format reads
    /// LITERALLY — a code span, a code block, a raw node — where the inline and
    /// block-start alphabets mean nothing. Inclusive at both ends of the body
    /// (`content_span`), because text written flush against either delimiter
    /// still ends up inside it; a node with no interior is a leaf whose bytes
    /// ARE its delimiters, and a splice at its edge lands beside it.
    fn insideVerbatim(self: *const Editor, offset: usize) Allocator.Error!bool {
        const doc = &self.splicer.doc;
        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(self.splicer.allocator);
        try locate.ancestorChain(self.splicer.allocator, doc, offset, &chain);
        for (chain.items) |id| {
            const literal = switch (doc.ast.nodes[id].kind) {
                .code_block, .raw_block, .raw_inline => true,
                .text_leaf => |l| switch (l.kind) {
                    .verbatim, .inline_math, .display_math => true,
                    else => false,
                },
                else => false,
            };
            if (!literal) continue;
            const cs = doc.contentSpan(id) orelse continue;
            if (cs.start <= offset and offset <= cs.end) return true;
        }
        return false;
    }

    /// Insert a hard line break *inside a table cell* at `offset`, spelled the
    /// format's way (`Syntax.cell_line_break` — `<br>` for Markdown). A row is a
    /// single source line, so this is the one break the cell can hold; the
    /// spliced `<br>` reparses as a `hard_break` in cell context (see
    /// `markdown/inline.zig`), so the caller reads back a semantic node, not raw
    /// HTML. In HTML itself `<br>` is not borrowed at all — it is simply the
    /// break, and the cell restriction here understates what the format allows.
    ///
    /// Errors:
    /// - `UnsupportedFormat` — the format has no in-cell break spelling (djot,
    ///   XML). Checked first: it is a property of the format, not the caret.
    /// - `NoBlock` — `offset` is not inside a table cell. Only the in-cell
    ///   gesture is spelled today; a general (non-cell) hard break is future work.
    /// - `EditConflict` — the splice would no longer parse as the same table; the
    ///   splicer rolled it back and nothing changed (the standard contract).
    pub fn insertLineBreak(self: *Editor, offset: usize) Error!void {
        try self.checkRange(offset, offset);

        // Format capability before caret position: a format with no in-cell
        // break spelling can never satisfy this gesture, wherever the caret is.
        const spelling = self.syntax.cell_line_break orelse return error.UnsupportedFormat;

        const allocator = self.splicer.allocator;

        var chain: std.ArrayList(AST.Node.Id) = .empty;
        defer chain.deinit(allocator);
        try locate.ancestorChain(allocator, &self.splicer.doc, offset, &chain);
        if (locate.innermostOfKind(&self.splicer.doc, chain.items, .cell) == null) return error.NoBlock;

        return self.commitSplice(offset, offset, spelling);
    }
};

// ── Block-container internals ──────────────────────────────────────────────

/// The blocks `[start, end)` touches: sibling `first`…`last` under the nearest
/// ancestor whose children are blocks. You cannot quote half a paragraph, so a
/// container op always widens to whole blocks first.
const BlockRange = struct {
    first: AST.Node.Id,
    last: AST.Node.Id,
    /// The ancestor chain down to `start`, reused for container detection.
    chain: []const AST.Node.Id,
};

/// Resolve `[start, end)` to the sibling blocks it touches. `end` is pulled back
/// off a trailing newline first: a block's span stops at its text in Markdown,
/// so a selection ending on the line break would otherwise resolve above the
/// block and drag the whole document in.
fn coveredBlocks(
    allocator: Allocator,
    doc: *const Document,
    start: usize,
    end: usize,
) !BlockRange {
    var last_off = if (end > start) end - 1 else start;
    while (last_off > start and (doc.source[last_off] == '\n' or doc.source[last_off] == '\r')) last_off -= 1;

    var chain_a: std.ArrayList(AST.Node.Id) = .empty;
    errdefer chain_a.deinit(allocator);
    try locate.ancestorChain(allocator, doc, start, &chain_a);

    var chain_b: std.ArrayList(AST.Node.Id) = .empty;
    defer chain_b.deinit(allocator);
    try locate.ancestorChain(allocator, doc, last_off, &chain_b);

    var i: usize = 0;
    while (i + 1 < chain_a.items.len and i + 1 < chain_b.items.len and
        chain_a.items[i + 1] == chain_b.items[i + 1]) : (i += 1)
    {}
    // Climb to the nearest ancestor that holds blocks: the deepest shared node
    // may be an inline (a `str`), and a container wraps blocks, not words.
    var p = i;
    while (p > 0 and !locate.isBlockParent(doc.ast.nodes[chain_a.items[p]].kind)) p -= 1;

    if (p + 1 >= chain_a.items.len) return error.NoBlock;
    const first = chain_a.items[p + 1];
    const last = if (p + 1 < chain_b.items.len) chain_b.items[p + 1] else first;
    return .{
        .first = first,
        .last = last,
        .chain = try chain_a.toOwnedSlice(allocator),
    };
}

/// True when the range's lines cover every block `target` holds — the condition
/// for toggling the container OFF rather than nesting inside it.
///
/// The test is "are all its blocks covered?", NOT "is its span inside the
/// region?": a container's span can run past its last block, because the blank
/// `>` line continuing a quote belongs to the quote and to no paragraph in it
/// (Djot spans `> > a\n>\n` as the inner quote, ending two bytes past its only
/// paragraph). Comparing spans there reads a fully-covered quote as partial and
/// nests forever.
fn containerFullyCovered(
    doc: *const Document,
    target: AST.Node.Id,
    region_start: usize,
    region_end: usize,
) bool {
    const first = doc.ast.nodes[target].first_child orelse return false;
    var last = first;
    var cur: ?AST.Node.Id = first;
    while (cur) |c| {
        last = c;
        cur = doc.ast.nodes[c].next_sibling;
    }
    const lo = locate.lineStartAt(doc.source, doc.span(first).start);
    const hi = locate.lineEndAt(doc.source, doc.span(last).end -| 1);
    return region_start <= lo and region_end >= hi;
}

/// The node `toggleBlockContainerByRender` builds for a `ContainerKind`. A
/// list built here is decimal from 1: a fresh list has no other numbering to
/// keep, and the marker path's `1. ` says the same.
fn containerNodeKind(kind: syntax_mod.ContainerKind, tight: bool) AST.Node.Kind {
    return switch (kind) {
        .block_quote => .block_quote,
        .bullet_list => .{ .bullet_list = .{ .tight = tight } },
        .ordered_list => .{ .ordered_list = .{ .numbering = .decimal, .tight = tight, .start = null } },
    };
}

/// Where a splice over a block span ENDS: the span's end, minus the line end
/// some parsers fold into a block's span after its closing bytes. A rendered
/// fragment carries no trailing line end (`spliceRendered` trims it), so
/// leaving the source's in place is what keeps the next block on its own line.
fn blockSpanEnd(src: []const u8, span: Span) usize {
    var end = span.end;
    if (end > span.start and src[end - 1] == '\n') end -= 1;
    if (end > span.start and src[end - 1] == '\r') end -= 1;
    return end;
}

/// The render path's `containerFullyCovered`: true when the covered blocks
/// hold `target` whole, or reach from the FIRST leaf block under its first
/// child to the LAST under its last — the tree's statement of "every line the
/// container holds", for a format whose lines say nothing. `blocks.first` may
/// be an ancestor of that leaf (a nested list the range covers whole), which
/// is why each end tests a path and not a node.
fn coversContainer(ast: *const AST, target: AST.Node.Id, blocks: BlockRange) bool {
    // The range covers a block that holds `target` whole — a list selected
    // from outside it, the way a range over every line of one is. Both are
    // on the chain to the range's start, so the shallower one contains the
    // other.
    const first_at = std.mem.indexOfScalar(AST.Node.Id, blocks.chain, blocks.first).?;
    const target_at = std.mem.indexOfScalar(AST.Node.Id, blocks.chain, target).?;
    if (first_at <= target_at) return true;

    const first = ast.nodes[target].first_child orelse return false;
    var last = first;
    while (ast.nodes[last].next_sibling) |n| last = n;
    return onEdgePath(ast, first, blocks.first, .first) and onEdgePath(ast, last, blocks.last, .last);
}

/// Whether `node` lies on the path of first (or last) children descending
/// from `from`, `from` itself included.
fn onEdgePath(ast: *const AST, from: AST.Node.Id, node: AST.Node.Id, edge: enum { first, last }) bool {
    var cur = from;
    while (true) {
        if (cur == node) return true;
        cur = ast.nodes[cur].first_child orelse return false;
        if (edge == .last) {
            while (ast.nodes[cur].next_sibling) |n| cur = n;
        }
    }
}

/// The empty container of `kind` the caret sits in, if the innermost node on
/// `chain` is one — or is an empty item that is the ONLY child of one — so
/// that `openContainerByRender` can take it back off. `null` otherwise.
fn emptyContainerOf(ast: *const AST, chain: []const AST.Node.Id, kind: syntax_mod.ContainerKind) ?AST.Node.Id {
    if (chain.len == 0) return null;
    const deepest = chain[chain.len - 1];
    if (ast.nodes[deepest].first_child != null) return null;
    const tag = std.meta.activeTag(ast.nodes[deepest].kind);
    if (tag == kindTag(kind)) return deepest;
    if (tag == .list_item and chain.len >= 2) {
        const list = chain[chain.len - 2];
        const only = ast.nodes[list].first_child == deepest and ast.nodes[deepest].next_sibling == null;
        if (only and std.meta.activeTag(ast.nodes[list].kind) == kindTag(kind)) return list;
    }
    return null;
}

/// The TEXT of a subtree, for a code block's payload: every text payload in
/// order, a break as a line end, and a line end between sibling blocks. This
/// is `select.textOf` with the breaks kept as breaks rather than folded to
/// spaces — a listing is the one place a paragraph's line structure is the
/// content, so a `<br>` in a poem becomes the line end it displayed as.
fn blockTextInto(allocator: Allocator, ast: *const AST, id: AST.Node.Id, out: *std.ArrayList(u8)) Allocator.Error!void {
    switch (ast.nodes[id].kind) {
        .str => |t| try out.appendSlice(allocator, t),
        .text_leaf => |l| try out.appendSlice(allocator, l.text),
        .smart_punctuation => |v| try out.appendSlice(allocator, v.ascii()),
        .raw_inline => |v| try out.appendSlice(allocator, v.text),
        .code_block => |v| try out.appendSlice(allocator, v.text),
        .raw_block => |v| try out.appendSlice(allocator, v.text),
        .non_breaking_space => try out.append(allocator, ' '),
        .soft_break, .hard_break => try out.append(allocator, '\n'),
        else => {
            var child = ast.nodes[id].first_child;
            var first = true;
            while (child) |c| : (child = ast.nodes[c].next_sibling) {
                if (!first and ast.nodes[c].kind.level() == .block) try out.append(allocator, '\n');
                try blockTextInto(allocator, ast, c, out);
                first = false;
            }
        },
    }
}

/// How many quotes enclose `target` on the chain — the number of `>` markers to
/// step over before the one that belongs to `target`.
fn quoteDepthAbove(ast: *const AST, chain: []const AST.Node.Id, target: AST.Node.Id) usize {
    var depth: usize = 0;
    for (chain) |id| {
        if (id == target) break;
        if (std.meta.activeTag(ast.nodes[id].kind) == .block_quote) depth += 1;
    }
    return depth;
}

// ── Join internals ─────────────────────────────────────────────────────────

/// The interior of a BLOCK, for the formats whose parsers record one and for
/// the ones that do not — `Editor.joinBlocks` needs both halves of a block's
/// bytes (its content, and the markup around it) and AsciiDoc's parser records
/// no `content_span` at all.
///
/// The fallback is the marker: a block with a leading marker holds its content
/// from the marker's end to its own end, and one with neither a content span
/// nor a marker is all content. That is a reconstruction, not a guess — it is
/// the same relation `Document.node_content_spans` states for the parsers that
/// do fill it in, read off `node_marker_spans` instead.
fn blockContent(doc: *const Document, id: AST.Node.Id) Span {
    if (doc.contentSpan(id)) |c| return c;
    const sp = doc.span(id);
    if (doc.markerSpan(id)) |m| {
        if (m.end >= sp.start and m.end <= sp.end) return Span.init(m.end, sp.end);
    }
    // Neither: the INLINE CHILDREN are where the text is, and the block's own
    // span is not. AsciiDoc is the format that forces this — a paragraph's
    // span opens at its METADATA (`[.lead]`, a `.Title` line), which is markup
    // and not content, and nothing records where it ends. Handing the whole
    // span back spliced those bytes into the middle of the joined line, so
    // `above` + `[.lead]`/`below` read `above`/`[.lead]`/`below` with the
    // attribute line now literal text. The first child's start and the last
    // child's end are the same relation `node_content_spans` states, read off
    // the tree where no span table answers.
    var it = doc.children(id);
    const first = it.next() orelse return sp;
    var last = first.id;
    while (it.next()) |child| last = child.id;
    const start = doc.span(first.id).start;
    const end = doc.span(last).end;
    if (start >= sp.start and end <= sp.end and start <= end) return Span.init(start, end);
    return sp;
}

/// A's tail with its trailing SEPARATOR LINES removed — what is left is the
/// closing markup and the closers `joinBlocks`'s doc comment says travel past
/// the joined text, and nothing else.
///
/// A container's span can reach past its last block: a Markdown `block_quote`
/// covers its own trailing marker lines (`> a\n>\n` is `0..5`), so the tail of
/// `a` there is `"\n>\n"` — a `>` line that, written after the joined text,
/// left a stray quote line below it, and piled one up per join. Those lines
/// separate; they close nothing.
///
/// WHOLE LINES only, which is the whole care needed here: `</div>` ends in a
/// `>`, and a byte-wise trim over the same alphabet would eat it and unclose
/// the div. The line end this drops is put back by `joinBlocks`'s
/// trailing-terminator rule wherever the removal took one.
fn trimTailSeparators(tail: []const u8) []const u8 {
    var end = tail.len;
    while (std.mem.lastIndexOfScalar(u8, tail[0..end], '\n')) |nl| {
        if (!isSeparatorRun(tail[nl + 1 .. end])) break;
        end = nl;
    }
    return tail[0..end];
}

/// Whether `bytes` are only what SEPARATES two blocks: line ends, spaces, tabs
/// and quote markers. A `>` is separation because a quote's marker is on every
/// line of it, blank ones included.
fn isSeparatorRun(bytes: []const u8) bool {
    for (bytes) |c| switch (c) {
        ' ', '\t', '\r', '\n', '>' => {},
        else => return false,
    };
    return true;
}

/// Whether the region between A's and B's topmost ancestors below the LCA
/// holds nothing the join would destroy. `joinBlocks`'s one data-loss guard.
///
/// The splice writes `[A.ce, R_end)` out of A's tail and B's content alone, so
/// whatever else lies in there is gone. Neither walk that found A and B can
/// see all of it. `precedingLeafBlock` steps over any node HOLDING NO LEAF
/// BLOCK — an empty `<div>`, a bare `>`, a bare `-`, an empty djot `:::`
/// fence — and a Markdown LINK REFERENCE DEFINITION (`[ref]: /zed`), a
/// FOOTNOTE DEFINITION (`[^n]: note`) and a djot CAPTION (`^ caption`) are not
/// in the tree at all: Markdown keeps a definition as a lookup table and not
/// as a node, so no tree walk could be written that sees one. `alpha` +
/// `[ref]: /zed` + `beta` joined to `alpha\nbeta\n` and the definition, along
/// with every link that resolved through it, was simply gone.
///
/// So the region is read twice, once against each thing that can be true of
/// it:
///
///   * **No node that HOLDS BLOCKS may overlap it** other than the two
///     blocks' COMMON ANCESTORS, which necessarily do. `holdsBlocks` is the
///     same classifier `precedingLeafBlock` descends by, and that is why it is
///     the right one: a node in the gap holding a leaf block WOULD BE A, so
///     what can be in there is exactly what that walk stepped over. It is
///     also why a bare `>` is refused where the `>` of a quote holding both
///     blocks is not — the bytes are identical and the tree is not. Inline
///     nodes are passed over: HTML keeps the newline between two elements as
///     a `str`, and the byte rule below is what judges those bytes.
///   * **What is left may only SEPARATE**, by `isSeparatorRun` — with B's own
///     PREAMBLE peeled off the right first. That preamble is the run of
///     non-blank whole lines directly above B with no blank line between: a
///     djot attribute line (`{.center}`) sits there, outside the span the
///     parser gives B, and B's attributes are discarded by design. A
///     definition is a block, so a blank line divides it from B and the peel
///     never reaches one — and the node check above holds it anyway.
///
/// A's extent reaching PAST B's start is false here too. Nothing produces it
/// now that djot's section spans are right, and it would make A's tail a slice
/// running through B.
fn gapIsClean(
    doc: *const Document,
    src: []const u8,
    common: []const AST.Node.Id,
    a_top: AST.Node.Id,
    b_top: AST.Node.Id,
) bool {
    const start = doc.span(a_top).end;
    const b_start = doc.span(b_top).start;
    if (start > b_start or b_start > src.len) return false;

    for (doc.ast.nodes, 0..) |_, idx| {
        const id: AST.Node.Id = @intCast(idx);
        const sp = doc.span(id);
        if (sp.start >= b_start or sp.end <= start) continue;
        if (!holdsBlocks(doc, id)) continue;
        if (std.mem.indexOfScalar(AST.Node.Id, common, id) == null) return false;
    }

    var end = b_start;
    while (end > start and src[end - 1] == '\n') {
        const line_start = locate.lineStartAt(src, end - 1);
        if (line_start < start) break;
        if (isSeparatorRun(src[line_start .. end - 1])) break;
        end = line_start;
    }
    return isSeparatorRun(src[start..end]);
}

/// B's content written as ONE LINE — what a marker heading A can hold. Each of
/// B's line ends becomes a single space, and the CONTINUATION PREFIX behind it
/// (a list item's indent, a quote's `> `) goes with it, since a heading's one
/// line sits behind its own prefix already.
fn appendAsOneLine(
    allocator: Allocator,
    text: []const u8,
    out: *std.ArrayList(u8),
) Allocator.Error!void {
    var rest = text;
    while (std.mem.indexOfScalar(u8, rest, '\n')) |nl| {
        try out.appendSlice(allocator, std.mem.trimEnd(u8, rest[0..nl], " \t\r"));
        try out.append(allocator, ' ');
        rest = rest[nl + 1 ..];
        var k: usize = 0;
        while (k < rest.len and (rest[k] == ' ' or rest[k] == '\t' or rest[k] == '>')) k += 1;
        rest = rest[k..];
    }
    try out.appendSlice(allocator, rest);
}

/// B's content written as a CONTINUATION LINE of A, which is one byte more
/// than verbatim in exactly one shape.
///
/// A line of `=` or `-` under a paragraph is a SETEXT UNDERLINE, and it is the
/// only line-start construct that rewrites the block ABOVE it rather than
/// opening one of its own: `above` + `===` joined to `above\n===\n` reparsed
/// as a single heading and no paragraph at all — B's text became A's spelling.
/// A backslash on its first byte is what `insertLiteral` writes for the same
/// bytes at the same position, and `\` before ASCII punctuation is that
/// character in every format stating the alphabet, so the gate is
/// `block_start_escapes` being stated at all.
///
/// Nothing else is escaped, and that is deliberate: `#`, `>`, `-` + space, a
/// table's `|` all open a BLOCK of their own as a continuation line, so the
/// join merely fails to merge two blocks into one — it does not rewrite A.
fn appendAsContinuation(
    allocator: Allocator,
    syntax: *const Syntax,
    text: []const u8,
    out: *std.ArrayList(u8),
) Allocator.Error!void {
    if (syntax.block_start_escapes != null and isUnderlineLine(text)) {
        const lead = text.len - std.mem.trimStart(u8, text, " \t").len;
        try out.appendSlice(allocator, text[0..lead]);
        try out.append(allocator, '\\');
        try out.appendSlice(allocator, text[lead..]);
        return;
    }
    try out.appendSlice(allocator, text);
}

/// Whether `text`'s FIRST line is a run of `=` or a run of `-` and nothing
/// else — a setext underline, whichever level it spells.
fn isUnderlineLine(text: []const u8) bool {
    const first = text[0 .. std.mem.indexOfScalar(u8, text, '\n') orelse text.len];
    const trimmed = std.mem.trim(u8, first, " \t\r");
    if (trimmed.len == 0) return false;
    for (trimmed) |c| if (c != trimmed[0]) return false;
    return trimmed[0] == '=' or trimmed[0] == '-';
}

/// Whether `id` is a heading spelled by an UNDERLINE rather than by a marker —
/// Markdown's setext form, the one `Editor.splitBlock` refuses for its own half
/// and `joinBlocks` refuses for B.
///
/// Three conditions, and all three are needed. No recorded marker rules out
/// every ATX-ish heading (`# x`, AsciiDoc's `== x`). Content starting where the
/// block does rules out a WRAPPING spelling — an HTML `<h1>a</h1>` records no
/// marker either, and its content starts after the opening tag. Content ending
/// before the block does is the underline itself.
fn isSetextHeading(doc: *const Document, id: AST.Node.Id) bool {
    if (std.meta.activeTag(doc.ast.nodes[id].kind) != .heading) return false;
    if (doc.markerSpan(id) != null) return false;
    const content = doc.contentSpan(id) orelse return false;
    const sp = doc.span(id);
    return content.start == sp.start and content.end < sp.end;
}

/// Whether a node on `chain` is a table. A table's cells hold paragraphs, so a
/// `para` inside one is a perfectly ordinary leaf block to the walks below —
/// and joining one into the cell above it, or into the paragraph before the
/// table, is not a join of two lines but a hole in a grid. Stated as a chain
/// test rather than as a kind test for exactly that reason: what is refused is
/// the POSITION, not the node.
fn passesThroughTable(doc: *const Document, chain: []const AST.Node.Id) bool {
    for (chain) |id| {
        if (std.meta.activeTag(doc.ast.nodes[id].kind) == .table) return true;
    }
    return false;
}

/// The path of node ids from the root down to `target`, appended to `out`.
///
/// A tree walk rather than `locate.ancestorChain`, which descends by OFFSET: two
/// nodes can share a start byte (a Markdown `list_item` and its first `para`
/// both begin at column zero), so an offset names a path and not a node.
/// `joinBlocks` already holds the node, and what it needs is that node's own
/// ancestors — for the lowest common ancestor, which is what decides how much
/// of A's closing markup has to travel.
fn nodePath(
    allocator: Allocator,
    doc: *const Document,
    target: AST.Node.Id,
    out: *std.ArrayList(AST.Node.Id),
) Allocator.Error!bool {
    try out.append(allocator, doc.ast.root);
    if (target == doc.ast.root) return true;
    if (try descendTo(allocator, doc, doc.ast.root, target, out)) return true;
    out.clearRetainingCapacity();
    return false;
}

fn descendTo(
    allocator: Allocator,
    doc: *const Document,
    id: AST.Node.Id,
    target: AST.Node.Id,
    out: *std.ArrayList(AST.Node.Id),
) Allocator.Error!bool {
    var it = doc.children(id);
    while (it.next()) |child| {
        try out.append(allocator, child.id);
        if (child.id == target) return true;
        if (try descendTo(allocator, doc, child.id, target, out)) return true;
        _ = out.pop();
    }
    return false;
}

/// True for a node that HOLDS BLOCKS, and so is a container the leaf walks
/// below descend into rather than a block they stop at.
///
/// `contentModel` is the classifier rather than a hand-kept list, because it is
/// already the exhaustive answer to this exact question and a new kind has to
/// declare one. It puts a `table` on the container side — its rows hold cells
/// which hold paragraphs — which is why `passesThroughTable` exists rather than
/// a "is a table a leaf?" special case here.
fn holdsBlocks(doc: *const Document, id: AST.Node.Id) bool {
    return doc.ast.nodes[id].kind.contentModel() == .blocks;
}

/// The last LEAF BLOCK inside `root`'s subtree in document order, or `null` when
/// it holds none — how `joinBlocks` asks whether B is the last thing in a
/// delimited container it is about to pull out of.
fn lastLeafBlock(doc: *const Document, root: AST.Node.Id) ?AST.Node.Id {
    if (!holdsBlocks(doc, root)) return root;
    var last: ?AST.Node.Id = null;
    var it = doc.children(root);
    while (it.next()) |child| {
        if (lastLeafBlock(doc, child.id)) |leaf| last = leaf;
    }
    return last;
}

/// The leaf block immediately before `target` in DOCUMENT ORDER — the block a
/// caret at the start of `target` has visually above it, wherever in the tree
/// that is. `null` when `target` is the document's first block.
///
/// Document order rather than sibling order is the whole point: the block above
/// a paragraph following a `<div>` is the div's LAST paragraph, and the block
/// above the first paragraph INSIDE the div is whatever preceded the div.
/// Sibling order answers "the div" to the first and "nothing" to the second,
/// and both are the wrong block to join into.
fn precedingLeafBlock(doc: *const Document, target: AST.Node.Id) ?AST.Node.Id {
    var state: PrecedingLeaf = .{ .doc = doc, .target = target };
    scanLeaves(&state, doc.ast.root);
    return if (state.found) state.last else null;
}

const PrecedingLeaf = struct {
    doc: *const Document,
    target: AST.Node.Id,
    last: ?AST.Node.Id = null,
    found: bool = false,
};

fn scanLeaves(state: *PrecedingLeaf, id: AST.Node.Id) void {
    if (id == state.target) {
        state.found = true;
        return;
    }
    if (!holdsBlocks(state.doc, id)) {
        state.last = id;
        return;
    }
    var it = state.doc.children(id);
    while (it.next()) |child| {
        scanLeaves(state, child.id);
        if (state.found) return;
    }
}

/// What `Editor.splitBlock` does with a block of a given kind. Having ONE
/// exhaustive switch answer this — rather than a "can I split it?" predicate
/// beside the switch that builds the separator — is what keeps `splitTarget`
/// and the builder from drifting apart about which kinds are splittable.
const SplitShape = enum { text, code, refuse };

/// Spelled out rather than left to an `else`, so a new `Kind` is a compile error
/// here and gets an answer on purpose — this switch is the checklist.
fn splitShape(tag: locate.KindTag) SplitShape {
    return switch (tag) {
        // The line-owning text blocks: a blank line divides them, unless a list
        // item's marker has to be repeated instead.
        .para, .heading => .text,
        .code_block => .code,

        // Structure whose parts are not lines, or containers a split would have
        // to descend into rather than divide.
        .table,
        .doc,
        .section,
        .block_quote,
        .bullet_list,
        .ordered_list,
        .task_list,
        .definition_list,
        .line_block,
        .list_item,
        .task_list_item,
        .definition_list_item,
        .term,
        .definition,
        .line,
        .row,
        .cell,
        .column,
        .caption,
        .footnote,
        .reference,
        .citation,
        .substitution,
        .container,
        // Blocks with no interior to divide: a rule is one line, and metadata
        // and raw blocks are inert islands whose bytes are not ours to
        // punctuate.
        .thematic_break,
        .metadata,
        .raw_block,
        // Inlines. `lineOwningBlock` cannot return one — it stops at a block
        // parent's child — but naming them is what keeps this exhaustive, and a
        // refusal is the right answer if that ever changes.
        .str,
        .soft_break,
        .hard_break,
        .non_breaking_space,
        .text_leaf,
        .raw_inline,
        .smart_punctuation,
        .link,
        .image,
        .inline_mark,
        .markup_leaf,
        .processing_instruction,
        => .refuse,
    };
}

/// The block `Editor.splitBlock` should divide for a caret at `offset`, which is
/// not simply "the block at `offset`" — because of where a caret at the END of a
/// block actually lands.
///
/// Spans are half-open, so a caret at a block's end is outside it. Whether it is
/// outside EVERYTHING depends on the format, and the two authorable ones
/// disagree: djot's `list_item` covers its trailing newline (`[0,6)` for
/// `- one\n`), while Markdown's stops before it (`[0,5)`). So for
/// `- one|\n- two\n` the caret at 5 is inside djot's item but, in Markdown, in
/// the GAP between two items — inside the `bullet_list` and inside no item at
/// all. A lookup that trusts the deepest hit gets the LIST, which is not
/// splittable, and "Enter at the end of an item" — the single most common way
/// this gesture is asked for — fails on the format where lists are commonest.
///
/// Retrying one byte back is therefore not a null-check: the answer at `offset`
/// can be non-null and still wrong. What makes it wrong is that it isn't
/// splittable, so that is what the retry keys on. A caret in any gap resolves to
/// the block that just ENDED, which is where the text cursor visually sits.
///
/// When neither position yields a splittable block the ORIGINAL is returned, so
/// a genuine refusal (a caret in a table) still reports `NotEditable` against
/// the block the caller actually pointed at rather than `NoBlock`.
fn splitTarget(doc: *const Document, offset: usize) ?locate.LineBlock {
    const at = locate.lineOwningBlock(doc, offset);
    if (at) |lb| {
        if (splitShape(std.meta.activeTag(doc.ast.nodes[lb.block].kind)) != .refuse) return lb;
    }
    if (offset > 0) {
        if (locate.lineOwningBlock(doc, offset - 1)) |back| {
            if (splitShape(std.meta.activeTag(doc.ast.nodes[back.block].kind)) != .refuse) return back;
        }
    }
    return at;
}

/// Advance past one `>` quote marker — its optional indent, the `>`, and the one
/// optional space after it — or `null` if `line[i..]` doesn't start one.
/// Whether the line ENDING at `line_start` carries content — anything past its
/// own quote markers. `false` at the start of the source, where there is no
/// preceding line to run into.
///
/// What `openBlockOnBlankLine` consults to decide whether it must mint a blank
/// separator: a marker written flush under a paragraph line is that paragraph's
/// text in djot, not a heading.
fn precedingLineHasContent(src: []const u8, line_start: usize) bool {
    if (line_start == 0) return false;
    const prev = locate.lineBody(src[locate.lineStartAt(src, line_start - 1)..line_start]);
    var i: usize = 0;
    while (skipQuoteMarker(prev, i)) |j| i = j;
    return !locate.isBlankLine(prev[i..]);
}

/// The marker that opens `sp`'s `ordinal`-th item — `sp.marker` for a fixed
/// spelling, the ordinal itself for a numbered one, which is why `buf` is the
/// caller's (the returned slice borrows it).
///
/// Shared by `buildContainerAdd`, which writes one per covered block, and
/// `openContainerOnBlankLine`, which writes exactly one: `1. ` and `- ` have to
/// be spelled the same by both, and two `{d}. ` format strings is one too many.
fn listMarker(sp: ContainerSpelling, ordinal: u32, buf: *[24]u8) []const u8 {
    if (!sp.numbered) return sp.marker;
    return std.fmt.bufPrint(buf, "{d}. ", .{ordinal}) catch unreachable;
}

/// Whether the byte opening a list marker (`listMarkerAt`'s `start`) opens an
/// ORDERED one — a digit, or the `(` of `(1)`. The complement is a `-`/`*`/`+`
/// bullet. Lets `openContainerOnBlankLine` tell "the same button again" from
/// "the other list button" against `ContainerSpelling.numbered`.
fn isOrderedMarker(c: u8) bool {
    return c == '(' or c == '.' or (c >= '0' and c <= '9');
}

fn skipQuoteMarker(line: []const u8, i: usize) ?usize {
    var j = i;
    var indent: usize = 0;
    while (j < line.len and line[j] == ' ' and indent < 3) : (indent += 1) j += 1;
    if (j >= line.len or line[j] != '>') return null;
    j += 1;
    if (j < line.len and line[j] == ' ') j += 1;
    return j;
}

/// The `[start, end)` of a list marker on `line`, scanning from `from` —
/// `start` at the bullet/first digit (so the indent before it stays put, keeping
/// an enclosing container's prefix intact) and `end` past the marker's trailing
/// spaces. `null` if the line doesn't open a list item.
///
/// `from` is how a caller skips a prefix the marker sits after: the checkbox
/// gestures pass the end of the line's quote markers, so `> - [ ] a` is found.
/// The container builders pass 0, which is the whole line.
fn listMarkerAt(line: []const u8, from: usize) ?struct { start: usize, end: usize } {
    var j: usize = from;
    while (j < line.len and (line[j] == ' ' or line[j] == '\t')) j += 1;
    const start = j;
    if (j >= line.len) return null;
    if (line[j] == '-' or line[j] == '*' or line[j] == '+') {
        j += 1;
    } else if (line[j] == '.') {
        // AsciiDoc's ordered marker: a run of dots, its depth its nesting.
        while (j < line.len and line[j] == '.') j += 1;
    } else {
        if (line[j] == '(') j += 1;
        var digits: usize = 0;
        while (j < line.len and line[j] >= '0' and line[j] <= '9') : (digits += 1) j += 1;
        if (digits == 0) return null;
        if (j >= line.len or (line[j] != '.' and line[j] != ')')) return null;
        j += 1;
    }
    // A marker must be followed by whitespace (or end the line): `-x` is a
    // paragraph starting with a hyphen, not a bullet.
    if (j < line.len and line[j] != ' ' and line[j] != '\n' and line[j] != '\r') return null;
    while (j < line.len and line[j] == ' ') j += 1;
    return .{ .start = start, .end = j };
}

/// True if one of the covered blocks begins on `[line_start, line_end)` — the
/// test for "this line opens a new list item". Djot starts a quoted block at its
/// text (after `> `), Markdown at the line start; either way it lands on the
/// block's first line, which is all this asks.
fn blockStartsOnLine(doc: *const Document, blocks: BlockRange, line_start: usize, line_end: usize) bool {
    var cur: ?AST.Node.Id = blocks.first;
    while (cur) |id| {
        const s = doc.span(id).start;
        if (s >= line_start and s < line_end) return true;
        if (id == blocks.last) break;
        cur = doc.ast.nodes[id].next_sibling;
    }
    return false;
}

/// True if one of `list`'s items begins on `[line_start, line_end)`.
fn itemStartsOnLine(doc: *const Document, list: AST.Node.Id, line_start: usize, line_end: usize) bool {
    var it = doc.children(list);
    while (it.next()) |item| {
        const s = doc.span(item.id).start;
        if (s >= line_start and s < line_end) return true;
    }
    return false;
}

/// Wrap every line of `[region_start, region_end)` in `kind`'s prefix, one item
/// per covered block. The lines already carry any enclosing container's prefix,
/// so prefixing at column 0 nests naturally (`> a` -> `> > a`).
fn buildContainerAdd(
    allocator: Allocator,
    src: []const u8,
    doc: *const Document,
    blocks: BlockRange,
    region_start: usize,
    region_end: usize,
    sp: ContainerSpelling,
    out: *std.ArrayList(u8),
) !void {
    var ordinal: u32 = 1;
    var cont: []const u8 = sp.cont;
    var line_start = region_start;
    while (line_start < region_end) {
        const line_end = locate.lineEndAt(src, line_start);
        const line = src[line_start..line_end];
        const body = locate.lineBody(line);

        if (locate.isBlankLine(body)) {
            // A blank line inside the region: mark it for a quote (else the quote
            // ends here), leave it bare for a list (it separates items).
            if (sp.blank.len > 0) {
                try out.appendSlice(allocator, sp.blank);
                try out.appendSlice(allocator, line[body.len..]);
            } else {
                try out.appendSlice(allocator, line);
            }
            line_start = line_end;
            continue;
        }

        if (blockStartsOnLine(doc, blocks, line_start, line_end)) {
            var num_buf: [24]u8 = undefined;
            const marker = listMarker(sp, ordinal, &num_buf);
            if (sp.numbered) cont = container_indent[0..@min(marker.len, container_indent.len)];
            try out.appendSlice(allocator, marker);
            try out.appendSlice(allocator, line);
            ordinal += 1;
        } else {
            try out.appendSlice(allocator, cont);
            try out.appendSlice(allocator, line);
        }
        line_start = line_end;
    }
}

/// Strip the quote marker `target` contributes from each of its lines, leaving
/// any outer quote levels untouched: `depth` is how many quotes enclose it, so
/// the marker removed is the `depth`-th + 1 on every line. That's what makes
/// toggling off a nested quote peel exactly one level (`> > a` -> `> a`).
fn buildQuoteStrip(
    allocator: Allocator,
    src: []const u8,
    region_start: usize,
    region_end: usize,
    depth: usize,
    out: *std.ArrayList(u8),
) !void {
    var line_start = region_start;
    while (line_start < region_end) {
        const line_end = locate.lineEndAt(src, line_start);
        const line = src[line_start..line_end];

        var keep: usize = 0;
        var d: usize = 0;
        while (d < depth) : (d += 1) keep = skipQuoteMarker(line, keep) orelse break;
        if (d == depth) {
            if (skipQuoteMarker(line, keep)) |after| {
                try out.appendSlice(allocator, line[0..keep]);
                try out.appendSlice(allocator, line[after..]);
                line_start = line_end;
                continue;
            }
        }
        // A line with no marker at this level (a lazy continuation) is already
        // outside the level being removed — pass it through untouched.
        try out.appendSlice(allocator, line);
        line_start = line_end;
    }
}

/// Rewrite the list `target`'s item markers: `sp == null` removes the list
/// (toggle off), otherwise it converts one list kind to the other in place. The
/// text before a marker (an enclosing quote's `> `, a nesting indent) is kept
/// verbatim; a block's continuation lines are re-indented to the new marker's
/// width so they stay attached to their item.
///
/// Removing a list has to keep its items separate BLOCKS: a tight `- a\n- b\n`
/// would strip to `a\nb\n`, which is one two-line paragraph, not two. So a blank
/// line is injected between items that had none — the structure the items had is
/// what survives, not their tightness.
fn buildListRewrite(
    allocator: Allocator,
    src: []const u8,
    doc: *const Document,
    target: AST.Node.Id,
    region_start: usize,
    region_end: usize,
    sp: ?ContainerSpelling,
    out: *std.ArrayList(u8),
) !void {
    var ordinal: u32 = 1;
    var old_width: usize = 0;
    var new_width: usize = 0;
    var seen_item = false;
    var last_blank = true;
    var line_start = region_start;
    while (line_start < region_end) {
        const line_end = locate.lineEndAt(src, line_start);
        const line = src[line_start..line_end];
        const body = locate.lineBody(line);

        if (locate.isBlankLine(body)) {
            try out.appendSlice(allocator, line);
            last_blank = true;
            line_start = line_end;
            continue;
        }

        if (itemStartsOnLine(doc, target, line_start, line_end)) {
            // Only when the list is going away: a conversion keeps the items as
            // items, so it must not loosen a tight list.
            if (sp == null and seen_item and !last_blank) try out.append(allocator, '\n');
            const m = listMarkerAt(line, 0) orelse {
                try out.appendSlice(allocator, line);
                line_start = line_end;
                continue;
            };
            var num_buf: [24]u8 = undefined;
            const marker: []const u8 = if (sp) |s|
                (if (s.numbered)
                    std.fmt.bufPrint(&num_buf, "{d}. ", .{ordinal}) catch unreachable
                else
                    s.marker)
            else
                "";
            try out.appendSlice(allocator, line[0..m.start]);
            try out.appendSlice(allocator, marker);
            try out.appendSlice(allocator, line[m.end..]);
            old_width = m.end - m.start;
            new_width = marker.len;
            ordinal += 1;
            seen_item = true;
            last_blank = false;
        } else {
            // A continuation line: swap the old marker's indent for the new one's
            // so the line stays inside its item.
            var j: usize = 0;
            while (j < line.len and j < old_width and line[j] == ' ') j += 1;
            try out.appendSlice(allocator, container_indent[0..@min(new_width, container_indent.len)]);
            try out.appendSlice(allocator, line[j..]);
            last_blank = false;
        }
        line_start = line_end;
    }
}

/// One left-to-right rewrite of `[region_start, region_end)` that renumbers
/// ordered-list items by their indentation depth — the body of
/// [`Editor.renumberOrderedLists`], where its reasoning lives.
fn buildRenumber(
    allocator: Allocator,
    src: []const u8,
    region_start: usize,
    region_end: usize,
    item_lines: []const usize,
    out: *std.ArrayList(u8),
) !void {
    // A small stack of (indent column, next number), one entry per open nesting
    // level. Documents don't nest lists dozens deep; 32 is plenty and keeps this
    // allocation-free. A level deeper than 32 just isn't renumbered (copied).
    var cols: [32]usize = undefined;
    var nums: [32]u32 = undefined;
    var depth: usize = 0;

    // `item_lines` ascends and so does the walk, so one cursor answers "does an
    // item open here" for every line without a search.
    var next_item: usize = 0;

    var line_start = region_start;
    while (line_start < region_end) {
        const line_end = locate.lineEndAt(src, line_start);
        const line = src[line_start..line_end];
        while (next_item < item_lines.len and item_lines[next_item] < line_start) next_item += 1;
        const opens_item = next_item < item_lines.len and item_lines[next_item] == line_start;
        // Past any `>` markers first. A quoted list's items sit BEHIND a prefix,
        // and scanning from column zero finds `>` where it wants a bullet or a
        // digit — so every line of `> 1. a` failed the marker test, the whole
        // region was copied verbatim, and the gesture reported success having
        // changed nothing. `splitMarker` skips the same way for the same reason.
        var from: usize = 0;
        while (skipQuoteMarker(line, from)) |j| from = j;
        const m = if (opens_item) listMarkerAt(line, from) else null;
        const numbered = if (m) |mm| isNumberedMarker(line[mm.start..mm.end]) else false;
        if (numbered) {
            const mm = m.?;
            // Leading whitespace measured from AFTER the quote prefix, not from
            // column zero: the prefix's own width is not this list's nesting
            // depth, and counting it would put a quoted top-level item at a
            // deeper level than an unquoted one.
            const indent = mm.start - from;
            // Drop levels deeper than this item; then resume this level or open it.
            while (depth > 0 and cols[depth - 1] > indent) depth -= 1;
            var number: u32 = 1;
            if (depth > 0 and cols[depth - 1] == indent) {
                number = nums[depth - 1];
                nums[depth - 1] += 1;
            } else if (depth < cols.len) {
                cols[depth] = indent;
                nums[depth] = 2; // this item is 1; its next sibling will be 2
                depth += 1;
            }
            // Emit: indentation, an optional `(`, the new number, then the
            // delimiter and everything after it verbatim.
            try out.appendSlice(allocator, line[0..mm.start]);
            var d = mm.start;
            if (d < line.len and line[d] == '(') {
                try out.append(allocator, '(');
                d += 1;
            }
            var num_buf: [16]u8 = undefined;
            const digits = std.fmt.bufPrint(&num_buf, "{d}", .{number}) catch unreachable;
            try out.appendSlice(allocator, digits);
            var k = d;
            while (k < line.len and line[k] >= '0' and line[k] <= '9') k += 1;
            try out.appendSlice(allocator, line[k..]);
        } else {
            // A bullet item, a blank line, or prose — including prose that LOOKS
            // like a marker but opens no item: verbatim. A bullet doesn't disturb
            // the ordered counters at other columns, and neither does prose.
            try out.appendSlice(allocator, line);
        }
        line_start = line_end;
    }
}

/// Whether a marker (as returned by `listMarkerAt`) is an ordered one — a run of
/// digits, allowing a leading `(` for the `(1)` form — rather than a bullet.
fn isNumberedMarker(marker: []const u8) bool {
    var j: usize = 0;
    if (j < marker.len and marker[j] == '(') j += 1;
    return j < marker.len and marker[j] >= '0' and marker[j] <= '9';
}

/// The run of QUOTE MARKERS opening the line `at` sits on — the prefix a new
/// block written beside it must repeat to stay in the same containers.
///
/// Quote markers only, because a quote's marker is on EVERY line it covers
/// while a list item's is on its first line alone — a list item holds its
/// content by indentation of the marker's width, which this cannot see from one
/// line. `insertThematicBreak` accepts landing at column zero (it splits the
/// list, corrupting nothing); `toggleCodeBlock`, where the same gap would pull
/// the item's marker into the code body, reads the item's column off the tree
/// instead — see `CodeFrame`.
fn containerPrefix(src: []const u8, at: usize) []const u8 {
    const line_start = locate.lineStartAt(src, at);
    const line = src[line_start..locate.lineEndAt(src, at)];
    var i: usize = 0;
    while (skipQuoteMarker(line, i)) |j| i = j;
    return line[0..i];
}

/// Whether container `id` ends in markup of its own — bytes after its last
/// child that are not whitespace: `</blockquote>`, `:::`, `____`. A container
/// spelled by line prefixes ends where its last child does.
fn writesClosers(doc: *const Document, src: []const u8, id: AST.Node.Id) bool {
    var last: ?AST.Node.Id = null;
    var c = doc.ast.nodes[id].first_child;
    while (c) |ch| : (c = doc.ast.nodes[ch].next_sibling) last = ch;
    const end = doc.span(id).end;
    const tail_start = if (last) |l| doc.span(l).end else doc.span(id).start;
    if (tail_start >= end) return false;
    for (src[tail_start..end]) |byte| {
        if (!std.ascii.isWhitespace(byte)) return true;
    }
    return false;
}

/// Whether `chain` passes through a list item — the containers `containerPrefix`
/// cannot reproduce, and so the ones a gesture that relies on it must refuse.
fn insideListItem(doc: *const Document, chain: []const AST.Node.Id) bool {
    for (chain) |id| {
        switch (std.meta.activeTag(doc.ast.nodes[id].kind)) {
            .list_item, .task_list_item => return true,
            else => {},
        }
    }
    return false;
}

// ── Move internals ─────────────────────────────────────────────────────────

/// Where `Editor.moveBlock` puts a block: the container whose prefixes it
/// takes, the blocks it lands between (either may be absent), and the line
/// start — or the source's end — its lines are written at.
const Dest = struct {
    container: AST.Node.Id,
    prev: ?AST.Node.Id,
    next: ?AST.Node.Id,
    pos: usize,
};

/// A node `moveBlock` descends INTO looking for the block at an offset,
/// rather than stopping at: the line-owning containers, and a list, whose
/// items own lines even though the list itself is not on `isBlockParent`'s
/// list.
fn descendsInto(kind: AST.Node.Kind) bool {
    return locate.isBlockParent(kind) or switch (kind) {
        .bullet_list, .ordered_list, .task_list => true,
        else => false,
    };
}

fn isListItem(kind: AST.Node.Kind) bool {
    return switch (kind) {
        .list_item, .task_list_item => true,
        else => false,
    };
}

fn isList(kind: AST.Node.Kind) bool {
    return switch (kind) {
        .bullet_list, .ordered_list, .task_list => true,
        else => false,
    };
}

/// The lines a list has apart from its items, as whole lines: what opens it
/// before its first item's line and closes it after its last item's, each
/// empty where the list is spelled by its items' markers alone.
const ListWrap = struct { open: Span = Span.init(0, 0), close: Span = Span.init(0, 0) };

fn listWrap(doc: *const Document, list: AST.Node.Id, item_lines: Span) ListWrap {
    if (!isList(doc.ast.nodes[list].kind)) return .{};
    const src = doc.source;
    const sp = doc.span(list);
    if (sp.end <= sp.start) return .{};
    const first = doc.ast.nodes[list].first_child orelse return .{};
    var last = first;
    while (doc.ast.nodes[last].next_sibling) |n| last = n;
    const lo = locate.lineStartAt(src, sp.start);
    const hi = locate.lineEndAt(src, sp.end - 1);
    const first_line = locate.lineStartAt(src, doc.span(first).start);
    const last_sp = doc.span(last);
    const last_line = if (last_sp.end > last_sp.start) locate.lineEndAt(src, last_sp.end - 1) else item_lines.end;
    return .{
        .open = Span.init(lo, @max(lo, @min(first_line, item_lines.start))),
        .close = Span.init(@min(hi, @max(last_line, item_lines.end)), hi),
    };
}

/// A paragraph the parser read without any markup around it — its span is
/// its content — which is how a tight HTML `<li>x</li>` holds its text.
fn isBareParagraph(doc: *const Document, id: AST.Node.Id) bool {
    if (doc.ast.nodes[id].kind != .para) return false;
    const content = doc.contentSpan(id) orelse return false;
    const sp = doc.span(id);
    return content.start == sp.start and content.end == sp.end;
}

fn isTightList(kind: AST.Node.Kind) bool {
    return switch (kind) {
        .bullet_list => |l| l.tight,
        .ordered_list => |l| l.tight,
        .task_list => |l| l.tight,
        else => false,
    };
}

/// An inline node — what a list item holds DIRECTLY in a format that gives
/// its text no paragraph (AsciiDoc, a tight HTML `<li>`), and so what the
/// descent may land on under an item. Its text stands for the item's first
/// block.
fn isInlineKind(kind: AST.Node.Kind) bool {
    return switch (kind) {
        .str,
        .soft_break,
        .hard_break,
        .non_breaking_space,
        .text_leaf,
        .raw_inline,
        .smart_punctuation,
        .link,
        .image,
        .inline_mark,
        .substitution,
        .reference,
        => true,
        .container => |c| c.form == .inline_text,
        else => false,
    };
}

/// The block `moveBlock` moves for a caret at `offset`: the deepest block
/// owning the caret's line, widened to its list item when it is the item's
/// first block (or the item's own text). `null` on a blank line.
fn movableUnit(doc: *const Document, offset: usize) ?AST.Node.Id {
    if (doc.ast.nodes.len == 0) return null;
    var cur = doc.ast.root;
    while (true) {
        const child = locate.caretChildContaining(doc, cur, offset) orelse {
            // Inside an item's own marker: the item.
            if (isListItem(doc.ast.nodes[cur].kind)) {
                if (doc.markerSpan(cur)) |m| if (offset < m.end) return cur;
            }
            return null;
        };
        const kind = doc.ast.nodes[child].kind;
        if (descendsInto(kind)) {
            cur = child;
            continue;
        }
        if (isListItem(doc.ast.nodes[cur].kind)) {
            if (isInlineKind(kind) or doc.ast.nodes[cur].first_child == child) return cur;
        } else if (isInlineKind(kind)) return null;
        return child;
    }
}

/// The whole lines `unit` owns, trailing separator lines trimmed —
/// `error.NotEditable` when it shares its first or last line with something
/// that is not a container prefix, since lines are what move.
fn unitLines(doc: *const Document, unit: AST.Node.Id, ancestors: []const AST.Node.Id) Editor.Error!Span {
    const src = doc.source;
    const sp = doc.span(unit);
    if (sp.end <= sp.start) return error.NotEditable;
    const start = locate.lineStartAt(src, sp.start);
    var end = locate.lineEndAt(src, sp.end - 1);
    while (end > start) {
        const ls = locate.lineStartAt(src, end - 1);
        if (ls <= start) break;
        if (!isSeparatorRun(locate.lineBody(src[ls..end]))) break;
        end = ls;
    }
    if (!lineHeadIsPrefix(doc, ancestors, start, sp.start)) return error.NotEditable;
    if (sp.end < end and !locate.isBlankLine(locate.lineBody(src[sp.end..end]))) return error.NotEditable;
    return Span.init(start, end);
}

/// Whether the bytes `[line_start, at)` are all container prefix: blanks,
/// quote markers, or inside a marker one of `chain`'s nodes opens there.
fn lineHeadIsPrefix(doc: *const Document, chain: []const AST.Node.Id, line_start: usize, at: usize) bool {
    var i = line_start;
    scan: while (i < at) : (i += 1) {
        switch (doc.source[i]) {
            ' ', '\t', '>' => continue :scan,
            else => {},
        }
        for (chain) |id| {
            if (doc.markerSpan(id)) |m| {
                if (i >= m.start and i < m.end) continue :scan;
            }
        }
        return false;
    }
    return true;
}

/// Resolve `to` to the boundary `moveBlock` lands on — see its doc comment
/// for the rules, and `Dest` for the answer. `unit` is the block moving,
/// which is what tells a boundary it already sits on from one it does not.
fn resolveBoundary(
    allocator: Allocator,
    syntax: *const Syntax,
    doc: *const Document,
    to: usize,
    unit: AST.Node.Id,
) Editor.Error!Dest {
    const src = doc.source;
    const unit_is_item = isListItem(doc.ast.nodes[unit].kind);
    var chain: std.ArrayList(AST.Node.Id) = .empty;
    defer chain.deinit(allocator);
    try chain.append(allocator, doc.ast.root);
    var container = chain.items[chain.items.len - 1];
    var prev: ?AST.Node.Id = null;
    var next: ?AST.Node.Id = null;
    const Side = enum { before, after, gap };
    var side: Side = .gap;

    // The source's length is the document's end, whether or not the last
    // line is terminated: after the last top-level block, and not inside
    // it or any container it closes.
    const at_end = to == src.len and src.len > 0 and src[src.len - 1] != '\n';
    var anchor: ?AST.Node.Id = null;
    if (!at_end) {
        while (locate.caretChildContaining(doc, chain.items[chain.items.len - 1], to)) |child| {
            if (!descendsInto(doc.ast.nodes[child].kind)) {
                anchor = child;
                break;
            }
            try chain.append(allocator, child);
        }
        container = chain.items[chain.items.len - 1];
    }

    if (anchor) |a| {
        // A block holding opaque text — a fence, a raw block — is interior
        // from its first byte to its last: the end of its body's last line
        // is inside the fence, not after the block.
        const content = if (doc.ast.nodes[a].kind.contentModel() == .text) blk: {
            const sp = doc.span(a);
            var end = sp.end;
            if (end > sp.start and src[end - 1] == '\n') end -= 1;
            break :blk Span.init(sp.start, end);
        } else blockContent(doc, a);
        if (to <= content.start) {
            side = .before;
        } else if (to >= content.end) {
            side = .after;
        } else return error.NotEditable;
        const inline_anchor = isInlineKind(doc.ast.nodes[a].kind);
        if (side == .before) {
            // Between two inlines of an item's text is inside that text.
            if (inline_anchor and doc.ast.nodes[container].first_child != a) return error.NotEditable;
            prev = prevSibling(doc, container, a);
            next = a;
        } else {
            var n = doc.ast.nodes[a].next_sibling;
            if (inline_anchor) {
                if (n != null and isInlineKind(doc.ast.nodes[n.?].kind)) return error.NotEditable;
                n = null;
                var it = doc.children(container);
                var seen = false;
                while (it.next()) |c| {
                    if (seen and !isInlineKind(doc.ast.nodes[c.id].kind)) {
                        n = c.id;
                        break;
                    }
                    if (c.id == a) seen = true;
                }
            }
            prev = a;
            next = n;
        }
    } else {
        var it = doc.children(container);
        while (it.next()) |c| {
            const sp = doc.span(c.id);
            if (sp.start < to) {
                prev = c.id;
            } else if (next == null) next = c.id;
        }
        if (at_end and prev != null) side = .after;
    }

    // At a container's opening — at or before its marker, or its opening
    // line's first byte where it has lines of its own — is BEFORE the
    // container, at its parent's level: the boundary inside it is the first
    // content byte, and this is the one offset that can name the other when
    // nothing precedes the container. The outermost such container on
    // `to`'s line, so `> > a` at 0 is before both quotes and at 2 before
    // the inner one alone.
    const to_line = locate.lineStartAt(src, to);
    for (chain.items[1..], 1..) |c, depth| {
        // A list opens where its first item does; its edge is the list-edge
        // rule's below, which knows an item joins it there.
        if (isList(doc.ast.nodes[c].kind)) continue;
        const open = if (doc.markerSpan(c)) |m| m.start else doc.span(c).start;
        if (to > open or locate.lineStartAt(src, open) != to_line) continue;
        chain.items.len = depth;
        container = chain.items[depth - 1];
        side = .before;
        prev = prevSibling(doc, container, c);
        next = c;
        break;
    }

    // The list adjustments, and then the climb: a boundary the block already
    // sits on — after itself, before itself — is, inside a container, the
    // same boundary as the one after or before the CONTAINER, read at the
    // parent's level, which is the reading that moves it. At the top level
    // it moves nothing. The adjustments run again after the climb, since it
    // can land at an item's head or a list's edge.
    var climbed = false;
    while (true) {
        // Inside a list item: a boundary before its first block is before the
        // item, and a moved item is a sibling wherever it is dropped.
        if (isListItem(doc.ast.nodes[container].kind) and chain.items.len >= 2) {
            const item = container;
            const at_head = prev == null or (next != null and next.? == doc.ast.nodes[item].first_child);
            if (at_head or unit_is_item) {
                _ = chain.pop();
                container = chain.items[chain.items.len - 1];
                if (at_head) {
                    side = .before;
                    prev = prevSibling(doc, container, item);
                    next = item;
                } else {
                    side = .after;
                    prev = item;
                    next = doc.ast.nodes[item].next_sibling;
                }
            }
        }

        // At a list's edge, a block that is not an item is beside the LIST:
        // the list contributes no prefix, so the boundary is the same place,
        // but its neighbours are the list's own, which is what tells a block
        // already there from one that moves.
        if (!unit_is_item and isList(doc.ast.nodes[container].kind) and chain.items.len >= 2) {
            const list = container;
            if (next != null and next.? == doc.ast.nodes[list].first_child) {
                _ = chain.pop();
                container = chain.items[chain.items.len - 1];
                side = .before;
                prev = prevSibling(doc, container, list);
                next = list;
            } else if (next == null) {
                _ = chain.pop();
                container = chain.items[chain.items.len - 1];
                side = .after;
                prev = list;
                next = doc.ast.nodes[list].next_sibling;
            }
        }

        const after_self = prev != null and prev.? == unit;
        const before_self = next != null and next.? == unit;
        if (!after_self and !before_self) break;
        // Only the container's last block is also after the container, and
        // only its first is also before it; for any other the boundary is
        // one it sits on and nothing else.
        const at_edge = if (after_self) doc.ast.nodes[unit].next_sibling == null else doc.ast.nodes[container].first_child == unit;
        if (climbed or !at_edge or chain.items.len < 2) return error.InvalidArgument;
        const c = container;
        _ = chain.pop();
        container = chain.items[chain.items.len - 1];
        if (after_self) {
            side = .after;
            prev = c;
            next = doc.ast.nodes[c].next_sibling;
        } else {
            side = .before;
            prev = prevSibling(doc, container, c);
            next = c;
        }
        climbed = true;
    }

    const pos = switch (side) {
        .before => blk: {
            const sp = doc.span(next.?);
            const ls = locate.lineStartAt(src, sp.start);
            if (!lineHeadIsPrefix(doc, chain.items, ls, sp.start)) return error.NotEditable;
            break :blk ls;
        },
        .after => blk: {
            const sp = doc.span(prev.?);
            if (sp.end <= sp.start) return error.NotEditable;
            const le = locate.lineEndAt(src, sp.end - 1);
            if (sp.end < le and !locate.isBlankLine(locate.lineBody(src[sp.end..le]))) return error.NotEditable;
            break :blk le;
        },
        .gap => blk: {
            const ls = locate.lineStartAt(src, to);
            if (!isSeparatorLine(syntax, locate.lineBody(src[ls..locate.lineEndAt(src, to)]))) return error.NotEditable;
            break :blk ls;
        },
    };
    return .{ .container = container, .prev = prev, .next = next, .pos = pos };
}

fn prevSibling(doc: *const Document, parent: AST.Node.Id, id: AST.Node.Id) ?AST.Node.Id {
    var prev: ?AST.Node.Id = null;
    var it = doc.children(parent);
    while (it.next()) |c| {
        if (c.id == id) return prev;
        prev = c.id;
    }
    return null;
}

/// The lines `moveBlock` removes: the block's own, plus the separator lines
/// that kept it apart from its neighbours inside its parent — the ones after
/// it, or when nothing but separators follows it there, those before it too,
/// so that no doubled blank is left and a container's trailing `>` line does
/// not outlive its last block. The parent is climbed first through every
/// prefix-spelled container the block is the only content of (a quote whose
/// one paragraph leaves, a list whose one item does), since such a container
/// has no lines of its own and is gone once its content is.
fn removalLines(syntax: *const Syntax, doc: *const Document, chain: []const AST.Node.Id, lines: Span) Span {
    const src = doc.source;
    var r: usize = chain.len - 1;
    while (r > 0) {
        const parent = chain[r - 1];
        if (parent == doc.ast.root or !spelledByPrefix(doc, parent)) break;
        if (doc.ast.nodes[parent].first_child != chain[r] or doc.ast.nodes[chain[r]].next_sibling != null) break;
        r -= 1;
    }
    // The climbed container's own lines, where it has any beyond the
    // block's (a delimited list's `<ul>`/`</ul>`).
    var lo = lines.start;
    var hi = lines.end;
    if (r < chain.len - 1) {
        const rsp = doc.span(chain[r]);
        if (rsp.end > rsp.start) {
            lo = @min(lo, locate.lineStartAt(src, rsp.start));
            hi = @max(hi, locate.lineEndAt(src, rsp.end - 1));
        }
    }
    const parent = chain[r - 1];
    var parent_start: usize = 0;
    var parent_end: usize = src.len;
    if (parent != doc.ast.root) {
        // Its interior, where the parent records one: a delimited
        // container's closing line is the parent's own, not something that
        // follows the block inside it.
        const psp = doc.contentSpan(parent) orelse doc.span(parent);
        if (psp.end > psp.start) {
            parent_start = locate.lineStartAt(src, psp.start);
            parent_end = @max(locate.lineEndAt(src, psp.end - 1), hi);
        }
    }
    var after = hi;
    while (after < parent_end) {
        const le = locate.lineEndAt(src, after);
        if (!isSeparatorLine(syntax, locate.lineBody(src[after..le]))) break;
        after = le;
    }
    var before = lo;
    if (after >= parent_end) {
        while (before > parent_start) {
            const ls = locate.lineStartAt(src, before - 1);
            if (ls < parent_start) break;
            if (!isSeparatorLine(syntax, locate.lineBody(src[ls..before]))) break;
            before = ls;
        }
    }
    return Span.init(before, after);
}

/// Whether `parent` goes when its only child does: a container spelled by
/// prefixes on the child's lines and nothing else (a `>` quote, a
/// marker-prefixed item), or a list, whose lines are its items' spelling.
/// A delimited container (`____`, `:::`, `<blockquote>`) has lines of its
/// own and stands when emptied — it may carry attributes a move must not
/// drop.
fn spelledByPrefix(doc: *const Document, parent: AST.Node.Id) bool {
    return switch (doc.ast.nodes[parent].kind) {
        // A list is its items' spelling either way: a marker-spelled one has
        // no lines of its own, and a delimited one (`<ul>`) travels with its
        // only item — see `listWrap`.
        .bullet_list, .ordered_list, .task_list => true,
        .block_quote, .list_item, .task_list_item => doc.markerSpan(parent) != null,
        .section => true,
        else => false,
    };
}

/// `chain` as the prefix walk should see it, appended to `out`: whole, or
/// without its list items where the format attaches a block to an item by a
/// line of its own rather than by the item's indent (`Syntax.list_attach`).
fn prefixChain(
    allocator: Allocator,
    syntax: *const Syntax,
    doc: *const Document,
    chain: []const AST.Node.Id,
    out: *std.ArrayList(AST.Node.Id),
) Allocator.Error!void {
    for (chain) |id| {
        if (syntax.list_attach != null and isListItem(doc.ast.nodes[id].kind)) continue;
        try out.append(allocator, id);
    }
}

/// `isSeparatorRun` for a whole line, plus the format's attach line where it
/// has one: AsciiDoc's `+` separates an item's text from an attached block
/// the way a blank line separates two paragraphs, and travels the same way.
fn isSeparatorLine(syntax: *const Syntax, body: []const u8) bool {
    if (isSeparatorRun(body)) return true;
    const attach = syntax.list_attach orelse return false;
    return std.mem.eql(u8, std.mem.trim(u8, body, " \t\r"), attach);
}

/// How many bytes of `body` are prefix inside the first `cols` columns:
/// blanks and quote markers, a tab advancing to its stop. Stops at the first
/// byte that is neither, so a lazy continuation line sheds nothing.
fn stripColumns(body: []const u8, cols: usize) usize {
    var i: usize = 0;
    var col: usize = 0;
    while (i < body.len and col < cols) : (i += 1) {
        switch (body[i]) {
            ' ', '>' => col += 1,
            '\t' => col += 4 - col % 4,
            else => break,
        }
    }
    return i;
}

/// The line that ends the text `a ++ b`: its body (with a line end taken
/// off), whether that line end was there, and whether the text was empty.
/// `scratch` holds the body where it straddles the two slices.
const LastLine = struct { body: []const u8, terminated: bool, empty: bool };

fn lastLineOf(allocator: Allocator, a: []const u8, b: []const u8, scratch: *std.ArrayList(u8)) Allocator.Error!LastLine {
    if (a.len == 0 and b.len == 0) return .{ .body = "", .terminated = true, .empty = true };
    var text_a = a;
    var text_b = b;
    var terminated = false;
    if (text_b.len > 0) {
        if (text_b[text_b.len - 1] == '\n') {
            terminated = true;
            text_b = text_b[0 .. text_b.len - 1];
        }
    } else if (text_a[text_a.len - 1] == '\n') {
        terminated = true;
        text_a = text_a[0 .. text_a.len - 1];
    }
    if (std.mem.lastIndexOfScalar(u8, text_b, '\n')) |nl| {
        return .{ .body = std.mem.trimEnd(u8, text_b[nl + 1 ..], "\r"), .terminated = terminated, .empty = false };
    }
    const a_tail = if (std.mem.lastIndexOfScalar(u8, text_a, '\n')) |nl| text_a[nl + 1 ..] else text_a;
    if (text_b.len == 0) return .{ .body = std.mem.trimEnd(u8, a_tail, "\r"), .terminated = terminated, .empty = false };
    scratch.clearRetainingCapacity();
    try scratch.appendSlice(allocator, a_tail);
    try scratch.appendSlice(allocator, text_b);
    return .{ .body = std.mem.trimEnd(u8, scratch.items, "\r"), .terminated = terminated, .empty = false };
}

/// The first line of the text `a ++ b`, without its line end — a whole
/// slice of one or the other, since the separator decision only needs to
/// know whether it is content, and a line that starts content in `a` and
/// runs into `b` is content already.
fn firstLineOf(a: []const u8, b: []const u8) []const u8 {
    const text = if (a.len > 0) a else b;
    const line = text[0 .. std.mem.indexOfScalar(u8, text, '\n') orelse text.len];
    return std.mem.trimEnd(u8, line, "\r");
}

// ── Code-fence internals ───────────────────────────────────────────────────

/// How many fence characters a fence over `body` needs: one more than the
/// longest run of `char` anywhere in it, floored at `min`. That is what lets a
/// code block hold a code block — the outer fence simply outgrows the inner one.
fn fenceWidth(body: []const u8, char: u8, min: usize) usize {
    var longest: usize = 0;
    var run: usize = 0;
    for (body) |c| {
        if (c != char) {
            run = 0;
            continue;
        }
        run += 1;
        if (run > longest) longest = run;
    }
    return @max(min, longest + 1);
}

/// The fence opening `line`: where its first character sits and how many there
/// are, after any quote prefix and up to three spaces of indentation. `null`
/// when the line opens no fence — which is not a malformed document but the
/// ordinary shape of a Markdown INDENTED code block.
fn fenceAt(line: []const u8, char: u8, min: usize) ?struct { start: usize, width: usize } {
    var i: usize = 0;
    while (skipQuoteMarker(line, i)) |j| i = j;
    var indent: usize = 0;
    while (i < line.len and line[i] == ' ' and indent < 3) : (indent += 1) i += 1;
    const start = i;
    while (i < line.len and line[i] == char) i += 1;
    return if (i - start >= min) .{ .start = start, .width = i - start } else null;
}

/// Where a code block's lines sit: the containers around it, as
/// `Editor.toggleCodeBlock` strips them off a line it reads and writes them
/// onto a line it mints.
///
/// A quote's marker is on every line it holds, but a list item's is on its
/// first line alone — the rest are held by INDENTATION to the item's content
/// column. So a line has two shapes here: the first line of a block that
/// opens a container, whose HEAD is the markers opening on it (`- `, `> `,
/// `> 1. `), and every other, which carries the continuation `prefix` (`  `,
/// `> `, `>    `). Fencing moves the head onto the opening fence and gives the
/// body's first line the continuation in its place; unfencing does the
/// reverse.
const CodeFrame = struct {
    ancestors: []const AST.Node.Id,
    /// What a line inside the containers opens with where nothing opens on
    /// it: a quote's marker again, a list item's content column in spaces.
    prefix: std.ArrayList(u8) = .empty,
    /// `prefix`'s width in columns.
    cols: usize = 0,

    /// The frame of a block whose ancestors are `ancestors`, outermost first.
    ///
    /// This is `locate.continuationPrefixAlong` but for two things, both
    /// because a code body is read to the column where a paragraph is read
    /// loosely:
    ///
    ///   * A TASK item's content column is its list marker's, not its
    ///     checkbox's: the box is the first paragraph's, and a fence written
    ///     after it opens a block at the item's content column like any other.
    ///     A body at the box's column would be indented past it — its closing
    ///     fence four columns in, where it closes nothing.
    ///   * A list item is left out where the format attaches a block to an
    ///     item by a line of its own (`Syntax.list_attach`) — the block's
    ///     lines are at column zero there, as `moveBlock` writes them.
    fn init(allocator: Allocator, syntax: *const Syntax, doc: *const Document, ancestors: []const AST.Node.Id) Allocator.Error!CodeFrame {
        var self: CodeFrame = .{ .ancestors = ancestors };
        errdefer self.prefix.deinit(allocator);
        const src = doc.source;
        for (ancestors) |id| {
            const kind = doc.ast.nodes[id].kind;
            if (!frames(kind)) continue;
            if (syntax.list_attach != null and isListItem(kind)) continue;
            const m = doc.markerSpan(id) orelse continue;
            var end = m.end;
            if (kind == .task_list_item) {
                if (listMarkerAt(src[m.start..m.end], 0)) |lm| end = m.start + lm.end;
            }
            const start_col = locate.columnOf(src, m.start);
            const end_col = locate.columnOf(src, end);
            while (self.cols < start_col) : (self.cols += 1) try self.prefix.append(allocator, ' ');
            if (end_col > self.cols) {
                if (kind == .block_quote) {
                    try self.prefix.appendSlice(allocator, src[m.start..end]);
                } else {
                    try self.prefix.appendNTimes(allocator, ' ', end_col - self.cols);
                }
                self.cols = end_col;
            }
        }
        return self;
    }

    fn deinit(self: *CodeFrame, allocator: Allocator) void {
        self.prefix.deinit(allocator);
    }

    /// The containers that put a prefix on the lines they hold — the ones a
    /// frame is made of. Not every node with a marker: an AsciiDoc
    /// admonition's `NOTE: ` is its paragraph's, and a fence written after
    /// it would be that paragraph's text.
    fn frames(kind: AST.Node.Kind) bool {
        return switch (kind) {
            .block_quote, .list_item, .task_list_item, .definition_list_item, .definition, .footnote => true,
            else => false,
        };
    }

    /// What a blank line inside the containers carries: a quote's `>`, and
    /// nothing for a list item — see `locate.blankLinePrefix`.
    fn blank(self: *const CodeFrame) []const u8 {
        return std.mem.trimEnd(u8, self.prefix.items, " \t");
    }

    /// How many bytes of the line starting at `at` are markers opening there
    /// — through the last of them, a task item's checkbox included, since it
    /// stays on the item's first line — or null where none opens and the line
    /// carries only the continuation.
    fn headLen(self: *const CodeFrame, doc: *const Document, at: usize) ?usize {
        const le = locate.lineEndAt(doc.source, at);
        const body_len = locate.lineBody(doc.source[at..le]).len;
        var n: ?usize = null;
        for (self.ancestors) |a| {
            if (!frames(doc.ast.nodes[a].kind)) continue;
            const m = doc.markerSpan(a) orelse continue;
            if (m.start >= at and m.start < le) n = @min(body_len, @max(n orelse 0, m.end - at));
        }
        return n;
    }

    /// Whether a list item's marker opens on the line starting at `at` — the
    /// block there is the item's first, and the line its only marker.
    fn opensItem(self: *const CodeFrame, doc: *const Document, at: usize) bool {
        const le = locate.lineEndAt(doc.source, at);
        for (self.ancestors) |a| {
            if (!isListItem(doc.ast.nodes[a].kind)) continue;
            const m = doc.markerSpan(a) orelse continue;
            if (m.start >= at and m.start < le) return true;
        }
        return false;
    }

    /// How much of `body` is the continuation prefix: `len` bytes of blanks
    /// and quote markers up to `cols` columns, stopping at anything else (a
    /// lazy line), and `full` when all `cols` were there. A tab that crosses
    /// the prefix's edge is taken whole and its columns past the edge are
    /// `pad` — content indentation to write back as spaces.
    const Strip = struct { len: usize, pad: usize, full: bool };

    fn strip(self: *const CodeFrame, body: []const u8) Strip {
        var i: usize = 0;
        var col: usize = 0;
        while (i < body.len and col < self.cols) : (i += 1) {
            switch (body[i]) {
                ' ', '>' => col += 1,
                '\t' => col += 4 - col % 4,
                else => break,
            }
        }
        return .{ .len = i, .pad = col -| self.cols, .full = col >= self.cols };
    }

    /// `rest` behind the continuation prefix and `pad` spaces, or behind the
    /// blank prefix where it is blank, then `ending`.
    fn writeLine(self: *const CodeFrame, allocator: Allocator, out: *std.ArrayList(u8), pad: usize, rest: []const u8, ending: []const u8) Allocator.Error!void {
        if (locate.isBlankLine(rest)) {
            try out.appendSlice(allocator, self.blank());
        } else {
            try out.appendSlice(allocator, self.prefix.items);
            try out.appendNTimes(allocator, ' ', pad);
            try out.appendSlice(allocator, rest);
        }
        try out.appendSlice(allocator, ending);
    }
};

/// The interior of the code block `cb` occupying `[region_start, region_end)`,
/// with its framing removed — the body of `Editor.toggleCodeBlock`'s unfence
/// half, where its reasoning lives — written to `out`. Returns where the
/// splice ends, which is past `region_end` in the one case below that takes
/// the blank lines after the block.
///
/// Where markers open on the fence line (`frame.headLen`), they are not
/// framing but the containers': they go onto the first line of the body in
/// place of its continuation, which is what keeps a list item's marker when
/// its first block is the one unfenced. Where the item's first line would be
/// left blank, that is not a place to stop: an item whose first line is
/// blank holds nothing past a blank line after it, so the body's leading
/// blank lines are skipped to its first line of content, and an EMPTY body
/// takes the blank lines after the block with it, so that a block following
/// in the item is still the item's.
fn buildUnfence(
    allocator: Allocator,
    doc: *const Document,
    frame: *const CodeFrame,
    cb: AST.Node.Id,
    region_start: usize,
    region_end: usize,
    fence: syntax_mod.CodeFence,
    out: *std.ArrayList(u8),
) !usize {
    const src = doc.source;
    const head = frame.headLen(doc, region_start);
    const first_end = locate.lineEndAt(src, region_start);
    const first_body = locate.lineBody(src[region_start..first_end]);
    const opening = first_body[head orelse frame.strip(first_body).len ..];

    if (fenceAt(opening, fence.char, fence.min) == null) {
        // No opening fence: a Markdown indented code block, whose framing IS its
        // indentation. Four spaces is the marker; a line indented further keeps
        // the rest, which is the indentation the code itself carried.
        var line_start = region_start;
        while (line_start < region_end) {
            const line_end = locate.lineEndAt(src, line_start);
            const body = locate.lineBody(src[line_start..line_end]);
            const ending = src[line_start + body.len .. line_end];
            const on_head = line_start == region_start and head != null;
            const st: CodeFrame.Strip = if (on_head) .{ .len = head.?, .pad = 0, .full = true } else frame.strip(body);
            var rest = body[st.len..];
            var pad = st.pad;
            var indent: usize = @min(pad, 4);
            pad -= indent;
            while (indent < 4 and rest.len > 0 and rest[0] == ' ') : (indent += 1) rest = rest[1..];
            if (on_head) {
                try appendHeadLine(allocator, out, src[region_start .. region_start + head.?], pad, rest, ending);
            } else {
                try frame.writeLine(allocator, out, pad, rest, ending);
            }
            line_start = line_end;
        }
        return region_end;
    }

    // Fenced: drop the opening line, and the closing one when there IS one — an
    // unterminated fence at the end of the document has no closing line, and its
    // last line is content that must survive.
    var body_end = region_end;
    const last_start = locate.lineStartAt(src, region_end -| 1);
    if (last_start >= first_end) {
        const last = locate.lineBody(src[last_start..region_end]);
        if (fenceAt(last[frame.strip(last).len..], fence.char, fence.min) != null) body_end = last_start;
    }
    body_end = @max(body_end, first_end);
    const h = head orelse {
        // Nothing opens on the fence line: the interior already carries its
        // containers' prefix, and is kept verbatim.
        try out.appendSlice(allocator, src[first_end..body_end]);
        return region_end;
    };
    const head_bytes = src[region_start .. region_start + h];
    const item = frame.opensItem(doc, region_start);

    // The line the head goes onto: the body's first, or in an item its first
    // with content.
    var line_start = first_end;
    if (item) {
        while (line_start < body_end) {
            const le = locate.lineEndAt(src, line_start);
            const body = locate.lineBody(src[line_start..le]);
            if (!locate.isBlankLine(body[frame.strip(body).len..])) break;
            line_start = le;
        }
    }
    if (line_start >= body_end) {
        // An empty body. Nothing is left of the block — but where the fence
        // line held a list item's marker, the item is kept, empty, and a block
        // after this one in the item is brought up under it.
        if (!item) return region_end;
        try out.appendSlice(allocator, std.mem.trimEnd(u8, head_bytes, " \t"));
        try out.append(allocator, '\n');
        var end = region_end;
        if (doc.ast.nodes[cb].next_sibling != null) {
            while (end < src.len) {
                const le = locate.lineEndAt(src, end);
                const body = locate.lineBody(src[end..le]);
                if (!locate.isBlankLine(body[frame.strip(body).len..])) break;
                end = le;
            }
        }
        return end;
    }
    const le = locate.lineEndAt(src, line_start);
    const body = locate.lineBody(src[line_start..le]);
    const st = frame.strip(body);
    try appendHeadLine(allocator, out, head_bytes, st.pad, body[st.len..], src[line_start + body.len .. le]);
    try out.appendSlice(allocator, src[le..@max(le, body_end)]);
    return region_end;
}

/// `rest` behind the head of a block's first line — where the line's markers
/// are — and `pad` spaces; a blank `rest` leaves the head without its
/// trailing blanks.
fn appendHeadLine(allocator: Allocator, out: *std.ArrayList(u8), head: []const u8, pad: usize, rest: []const u8, ending: []const u8) Allocator.Error!void {
    if (locate.isBlankLine(rest)) {
        try out.appendSlice(allocator, std.mem.trimEnd(u8, head, " \t"));
    } else {
        try out.appendSlice(allocator, head);
        try out.appendNTimes(allocator, ' ', pad);
        try out.appendSlice(allocator, rest);
    }
    try out.appendSlice(allocator, ending);
}

/// Refuse an info string this format's fence cannot carry back out: a line end
/// (which would end the fence line), the fence character itself (which would
/// widen or close the fence), and whatever else the format forbids — Markdown
/// ends its info string at whitespace, so a space there would come back
/// truncated rather than broken, which is worse.
fn checkInfoString(fence: syntax_mod.CodeFence, lang: []const u8) Editor.Error!void {
    if (std.mem.indexOfAny(u8, lang, "\r\n") != null) return error.InvalidLanguage;
    if (std.mem.indexOfScalar(u8, lang, fence.char) != null) return error.InvalidLanguage;
    if (std.mem.indexOfAny(u8, lang, fence.info_forbids) != null) return error.InvalidLanguage;
}

/// Refuse a directive name that would not come back as the name of a
/// container. The grammar is `markdown/attributes.zig`'s `scanName` — an ASCII
/// letter, then letters, digits, `-` and `_` — minus the colon that one also
/// admits, and it is checked HERE, once, rather than per format, because a
/// name is written where a delimiter would otherwise be in every spelling of
/// one. The failures are not subtle and they differ per format, which is
/// exactly why the editor takes the intersection:
///
///   * A SPACE: Markdown reads `::a b` as a paragraph holding an INLINE
///     directive named `a`, djot reads `::: a b` as a paragraph of literal
///     colons, and HTML truncates `<a b>` to a tag named `a` with an
///     attribute. Three different wrong answers from one gesture.
///   * A BRACKET or a brace: `::]{ ` is no container at all, in any of them.
///   * A LEADING DIGIT: Markdown's own `scanName` requires a letter, so
///     `::1x` is a paragraph — the rule that keeps `:30` from being a name.
///   * A COLON is the one Markdown itself would accept, and djot would not:
///     its class grammar has none, so `::: a:b` reparses as a paragraph. A
///     format-neutral gesture cannot mint a name only some formats read.
///
/// Case is not checked, because it is not a refusal: HTML folds a tag name, so
/// an upper-case name comes back lower-cased there while every other format
/// keeps it. That is a spelling difference, not a lost node.
/// The grammar an attribute must fit for every format to read it back: a key
/// that is an ASCII letter or `_` followed by letters, digits, `-`, `_` and
/// `:` (the intersection of djot's, Markdown's and HTML's attribute names), a
/// value — a BARE key is HTML's alone; djot's `{…}` has no spelling for one
/// and reads the whole block as text — and no line end or double quote in
/// it, the two bytes a quoted value cannot hold in every spelling at once.
fn checkAttr(kv: AST.KeyVal) Editor.Error!void {
    if (kv.key.len == 0) return error.InvalidAttribute;
    if (!std.ascii.isAlphabetic(kv.key[0]) and kv.key[0] != '_') return error.InvalidAttribute;
    for (kv.key[1..]) |c| {
        if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != ':') return error.InvalidAttribute;
    }
    const value = kv.value orelse return error.InvalidAttribute;
    if (std.mem.indexOfAny(u8, value, "\r\n\"") != null) return error.InvalidAttribute;
}

fn checkDirectiveName(name: []const u8) Editor.Error!void {
    if (name.len == 0) return error.InvalidName;
    if (!std.ascii.isAlphabetic(name[0])) return error.InvalidName;
    for (name[1..]) |c| {
        if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_') return error.InvalidName;
    }
}

// ── Task-checkbox internals ────────────────────────────────────────────────

/// Where a list item's checkbox is, or would go. `box` is the brackets and what
/// is between them and NOTHING else — `space` counts the separator after it
/// separately, because ticking a box must not touch the author's spacing while
/// removing one must take the separator with it.
const TaskBox = struct {
    at: usize,
    box: ?Span,
    space: usize,
    checked: bool,
};

// ── Footnote internals ─────────────────────────────────────────────────────

/// Whether `label` already has a DEFINITION in this tree.
///
/// A linear scan of `ast.nodes` rather than a walk from the root, because a
/// footnote definition is attached to no parent: both parsers resolve them by
/// label, not by position, so the node is a detached root that a child walk
/// never reaches.
/// `checkInfoString` for the render path, which has no fence to measure
/// against: a language is ONE TOKEN in every format that spells one — a
/// `class="language-…"` splits on whitespace as an info string ends at it —
/// so any whitespace is `error.InvalidLanguage`. Whatever else the token
/// holds is the renderer's to escape, and the reparse's to check.
fn checkLanguageToken(lang: []const u8) Editor.Error!void {
    for (lang) |c| if (std.ascii.isWhitespace(c)) return error.InvalidLanguage;
}

/// One inline node a selection covers: the node whole, or — for a `str` the
/// selection starts or ends inside — the covered part of its text.
const InlinePiece = struct { node: AST.Node.Id, text: ?[]const u8 };

/// The inline nodes `[start, end)` covers, in order: the children of the
/// deepest node containing both ends, from the one holding `start` to the one
/// holding `end - 1`, each of which must be inline-level. Caller frees.
///
/// The ends may fall inside a `str`, and only a `str`: its covered part is a
/// slice of its text, which is exact only where the text is the source
/// byte-for-byte (`span.len == text.len`) — a run spelling `&amp;` has no
/// character at every byte, and slicing it would put an entity's tail in the
/// link. Anything else the range cuts through is `error.NotEditable`, and so
/// is a range whose ends sit in nothing (`error.NoBlock`) or that crosses a
/// block boundary.
fn coveredInlines(allocator: Allocator, doc: *const Document, start: usize, end: usize) Editor.Error![]InlinePiece {
    const ast = &doc.ast;
    var chain_a: std.ArrayList(AST.Node.Id) = .empty;
    defer chain_a.deinit(allocator);
    try locate.ancestorChain(allocator, doc, start, &chain_a);
    var chain_b: std.ArrayList(AST.Node.Id) = .empty;
    defer chain_b.deinit(allocator);
    try locate.ancestorChain(allocator, doc, end - 1, &chain_b);

    var i: usize = 0;
    while (i + 1 < chain_a.items.len and i + 1 < chain_b.items.len and
        chain_a.items[i + 1] == chain_b.items[i + 1]) : (i += 1)
    {}
    const common = chain_a.items[i];

    var pieces: std.ArrayList(InlinePiece) = .empty;
    errdefer pieces.deinit(allocator);

    // The range IS one node (`<em>this</em>` selected whole): that node.
    const common_span = doc.span(common);
    if (common_span.start >= start and common_span.end <= end) {
        if (ast.nodes[common].kind.level() != .@"inline") return error.NotEditable;
        try pieces.append(allocator, .{ .node = common, .text = null });
        return pieces.toOwnedSlice(allocator);
    }
    // Both ends in one text run: its covered part, and nothing else.
    if (ast.nodes[common].kind == .str) {
        try pieces.append(allocator, .{ .node = common, .text = try strSlice(doc, common, start, end) });
        return pieces.toOwnedSlice(allocator);
    }
    if (i + 1 >= chain_a.items.len or i + 1 >= chain_b.items.len) return error.NoBlock;
    const first = chain_a.items[i + 1];
    const last = chain_b.items[i + 1];
    var cur: ?AST.Node.Id = first;
    while (cur) |c| : (cur = if (c == last) null else ast.nodes[c].next_sibling) {
        if (ast.nodes[c].kind.level() != .@"inline") return error.NotEditable;
        const sp = doc.span(c);
        const cut = (c == first and sp.start < start) or (c == last and sp.end > end);
        try pieces.append(allocator, .{
            .node = c,
            .text = if (cut) try strSlice(doc, c, @max(sp.start, start), @min(sp.end, end)) else null,
        });
    }
    return pieces.toOwnedSlice(allocator);
}

/// The part of `str` node `id`'s text that `[start, end)` covers, where the
/// text is its source byte-for-byte; `error.NotEditable` for any other node,
/// or a run whose source spells a character some other way.
fn strSlice(doc: *const Document, id: AST.Node.Id, start: usize, end: usize) Editor.Error![]const u8 {
    const text = switch (doc.ast.nodes[id].kind) {
        .str => |t| t,
        else => return error.NotEditable,
    };
    const sp = doc.span(id);
    if (sp.end - sp.start != text.len) return error.NotEditable;
    return text[start - sp.start .. end - sp.start];
}

fn footnoteDefined(ast: *const AST, label: []const u8) bool {
    for (ast.nodes) |n| {
        if (std.meta.activeTag(n.kind) != .footnote) continue;
        if (std.mem.eql(u8, n.kind.footnote.label, label)) return true;
    }
    return false;
}

/// Whether `src` already ends in a blank line — so appending a block needs only
/// a line of its own, not a separator too. An EMPTY document counts: there is
/// nothing above for the new block to fuse with.
fn endsWithBlankLine(src: []const u8) bool {
    if (src.len == 0) return true;
    if (src[src.len - 1] != '\n') return false;
    const last = locate.lineStartAt(src, src.len - 1);
    return locate.isBlankLine(locate.lineBody(src[last..]));
}

// ── Link internals ─────────────────────────────────────────────────────────
// `toggleInline` can't spell a link: its delimiters are a fixed `(open, close)`
// pair, and a link's closing half carries a payload (`](dest)`). Hence a
// dedicated gesture with a destination argument — and with the escaping that
// payload needs.

/// Write `dest` into `out` spelled so the format parses it back byte-for-byte.
///
/// This is the sharp edge of the whole gesture, and it is NOT one escape table:
///
///   * Markdown ends a destination at the first space — `[t](a b)` is not a link
///     at all, it is literal text — so a destination holding whitespace has to
///     move into the `<…>` form, where `<`/`>`/`\` are what need escaping.
///   * Djot takes spaces literally and gives `<…>` NO meaning: `[t](<a b>)`
///     links to the seven characters `<a b>`. Wrapping there would corrupt the
///     URL rather than protect it.
///
/// That difference is `DestEscapes.angle`: non-null means the format HAS an
/// angle form to escape into. The algorithm is the same either way, which is why
/// it lives here once and the alphabets live in `syntax.zig`.
///
/// Both formats honour a backslash escape inside the destination, which is what
/// keeps an unbalanced `)` from closing the link early.
fn writeLinkDestination(
    allocator: Allocator,
    syntax: *const Syntax,
    dest: []const u8,
    out: *std.ArrayList(u8),
) !void {
    const de = syntax.link_dest_escapes orelse return error.UnsupportedFormat;
    const angle: ?[]const u8 = if (de.angle) |a|
        (if (std.mem.indexOfAny(u8, dest, " \t") != null) a.escapes else null)
    else
        null;

    if (angle != null) try out.append(allocator, '<');
    const escapes = angle orelse de.plain;
    for (dest) |c| {
        if (std.mem.indexOfScalar(u8, escapes, c) != null) try out.append(allocator, '\\');
        try out.append(allocator, c);
    }
    if (angle != null) try out.append(allocator, '>');
}

fn writeLinkText(
    allocator: Allocator,
    syntax: *const Syntax,
    text: []const u8,
    out: *std.ArrayList(u8),
) !void {
    const escapes = syntax.link_text_escapes orelse return error.UnsupportedFormat;
    for (text) |c| {
        if (std.mem.indexOfScalar(u8, escapes, c) != null) try out.append(allocator, '\\');
        try out.append(allocator, c);
    }
}

/// Write `text` through `render`, one run per `TextPosition`. The engine's half
/// of `Editor.insertLiteral`: it decides which bytes sit at a LINE START —
/// `at_line_start` seeds that zone for the first line; a `\n` re-enters it, and
/// spaces/tabs (a block marker tolerates up to three leading spaces) hold it, so
/// `\n  # h` still hands the `#` over in `block_start` position — and which sit
/// in a verbatim body, where the whole run goes over at once. Every run given
/// to `render` lies in exactly one position, so the format spells bytes and
/// never re-derives where a line starts.
fn writeLiteral(
    syntax: *const Syntax,
    render: RenderTextFn,
    text: []const u8,
    at_line_start: bool,
    in_verbatim: bool,
    out: *Writer,
) Writer.Error!void {
    if (in_verbatim) return render(syntax, text, .verbatim, out);
    var rest = text;
    var block_pos = at_line_start;
    while (rest.len > 0) {
        const nl = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len - 1;
        const line = rest[0 .. nl + 1];
        rest = rest[nl + 1 ..];
        var body = line;
        if (block_pos) {
            // The leading whitespace and the byte that ends it: the run in
            // which a block marker bites.
            var i: usize = 0;
            while (i < line.len and (line[i] == ' ' or line[i] == '\t')) i += 1;
            const zone = @min(i + 1, line.len);
            try render(syntax, line[0..zone], .block_start, out);
            body = line[zone..];
        }
        if (body.len > 0) try render(syntax, body, .inline_text, out);
        // A line end re-enters the zone for whatever follows it.
        block_pos = line[line.len - 1] == '\n';
    }
}

/// The innermost autolink — the `<https://x.dev>` / `<a@b.dev>` form — on the
/// chain that wholly contains `[start, end)`.
///
/// Both node kinds are matched in both formats because the split is not the one
/// the names suggest — it follows the FORMAT, not just the destination.
/// `<mailto:a@b.dev>` parses as a `url` in Markdown and an `email` in djot, so
/// picking one kind per format would miss half the autolinks it was meant to
/// catch.
fn autolinkCovering(doc: *const Document, chain: []const AST.Node.Id, start: usize, end: usize) ?AST.Node.Id {
    const id = locate.innermostCovering(doc, chain, &.{.text_leaf}, start, end) orelse return null;
    const l = doc.ast.nodes[id].kind.text_leaf;
    return if (l.kind == .url or l.kind == .email) id else null;
}

/// Whether writing at `pos` would land STRICTLY INSIDE an autolink's URL — an
/// autolink covers `pos`, and `pos` is neither of its edges. A splice at an edge
/// is safe (it lands beside the node); one strictly inside rewrites the URL
/// itself, which is never what any caller meant. See `Editor.insertLink`.
///
/// Builds its own chain because the caller's is rooted at `start`, and the offset
/// that lands inside can be `end` (a selection running from ordinary text into
/// the middle of a URL).
fn splitsAutolink(allocator: Allocator, doc: *const Document, pos: usize) Allocator.Error!bool {
    var chain: std.ArrayList(AST.Node.Id) = .empty;
    defer chain.deinit(allocator);
    try locate.ancestorChain(allocator, doc, pos, &chain);
    const id = autolinkCovering(doc, chain.items, pos, pos) orelse return false;
    const span = doc.span(id);
    return span.start < pos and pos < span.end;
}

test {
    _ = @import("editor_test.zig");
}