hjkl-editor 0.4.5

Front-door for the hjkl modal editor stack: vim engine + rope buffer + registers + ex commands, in one 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
//! Ex-command parser + executor.
//!
//! Parses the text after a leading `:` in the command-line prompt and
//! returns an [`ExEffect`] describing what the caller should do. Only the
//! editor-local effects (substitute, goto-line, clear-highlight) are
//! applied in-place against `Editor`; quit / save / unknown are returned
//! to the caller so the TUI loop can run them.

use std::borrow::Cow;

use hjkl_engine::Editor;

/// Vim-style ex command name table. Each entry is a full canonical name
/// plus the minimum prefix length the user must type to disambiguate it.
/// Mirrors how vim resolves `:noh` → `:nohlsearch`, `:reg` →
/// `:registers`, etc. without enumerating every alias by hand.
struct CommandDef {
    full_name: &'static str,
    min_prefix: usize,
}

#[rustfmt::skip]
static COMMAND_NAMES: &[CommandDef] = &[
    CommandDef { full_name: "bNext",      min_prefix: 2 },
    CommandDef { full_name: "bdelete",    min_prefix: 2 },
    CommandDef { full_name: "bfirst",     min_prefix: 2 },
    CommandDef { full_name: "blast",      min_prefix: 2 },
    CommandDef { full_name: "bnext",      min_prefix: 2 },
    CommandDef { full_name: "bprevious",  min_prefix: 2 },
    CommandDef { full_name: "buffers",    min_prefix: 2 },
    CommandDef { full_name: "delete",     min_prefix: 1 },
    CommandDef { full_name: "edit",       min_prefix: 1 },
    CommandDef { full_name: "files",      min_prefix: 2 },
    CommandDef { full_name: "foldindent", min_prefix: 5 },
    CommandDef { full_name: "foldsyntax", min_prefix: 5 },
    CommandDef { full_name: "global",     min_prefix: 1 },
    CommandDef { full_name: "ls",         min_prefix: 2 },
    CommandDef { full_name: "changes",    min_prefix: 7 },
    CommandDef { full_name: "jumps",      min_prefix: 5 },
    CommandDef { full_name: "marks",      min_prefix: 5 },
    CommandDef { full_name: "nohlsearch", min_prefix: 3 },
    CommandDef { full_name: "qall",       min_prefix: 2 },
    CommandDef { full_name: "quit",       min_prefix: 1 },
    CommandDef { full_name: "read",       min_prefix: 1 },
    CommandDef { full_name: "redo",       min_prefix: 3 },
    CommandDef { full_name: "registers",  min_prefix: 3 },
    CommandDef { full_name: "set",        min_prefix: 2 },
    CommandDef { full_name: "sort",       min_prefix: 3 },
    CommandDef { full_name: "substitute", min_prefix: 1 },
    CommandDef { full_name: "undo",       min_prefix: 1 },
    CommandDef { full_name: "vglobal",    min_prefix: 1 },
    CommandDef { full_name: "wall",       min_prefix: 2 },
    CommandDef { full_name: "write",      min_prefix: 1 },
    CommandDef { full_name: "wq",         min_prefix: 2 },
    CommandDef { full_name: "wqall",      min_prefix: 3 },
    CommandDef { full_name: "x",          min_prefix: 1 },
];

fn resolve_name(input: &str) -> Option<&'static str> {
    if input.is_empty() {
        return None;
    }
    if let Some(exact) = COMMAND_NAMES
        .iter()
        .find(|d| d.full_name == input)
        .map(|d| d.full_name)
    {
        return Some(exact);
    }
    let candidates: Vec<&'static str> = COMMAND_NAMES
        .iter()
        .filter(|d| d.full_name.starts_with(input) && input.len() >= d.min_prefix)
        .map(|d| d.full_name)
        .collect();
    if candidates.len() == 1 {
        Some(candidates[0])
    } else {
        None
    }
}

fn split_name(cmd: &str) -> (&str, &str) {
    let split_at = cmd
        .char_indices()
        .find(|(_, c)| !c.is_ascii_alphabetic())
        .map(|(i, _)| i)
        .unwrap_or(cmd.len());
    cmd.split_at(split_at)
}

/// Expand the leading alpha-prefix of `cmd` to its canonical command
/// name via the prefix table. Returns the input unchanged when the
/// name doesn't resolve — the caller's existing fall-through arms
/// (e.g. range-only commands) still work.
///
/// Exposed so the umbrella binary's dispatch interceptors can match
/// canonical names without duplicating alias lists.
pub fn canonical_command_name(cmd: &str) -> Cow<'_, str> {
    let (name, rest) = split_name(cmd);
    if name.is_empty() {
        return Cow::Borrowed(cmd);
    }
    match resolve_name(name) {
        Some(canon) if canon != name => Cow::Owned(format!("{canon}{rest}")),
        _ => Cow::Borrowed(cmd),
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExEffect {
    /// Nothing happened (empty input or already-applied effect).
    None,
    /// Save the current buffer to the current filename.
    Save,
    /// Save to a specific path (`:w <path>`). The caller updates its
    /// `filename` field so future `:w` writes there.
    SaveAs(String),
    /// Quit (`:q`, `:q!`, `:wq`, `:x`).
    Quit { force: bool, save: bool },
    /// Unknown command — caller should surface as an error toast.
    Unknown(String),
    /// Substitution finished — report replacement count and lines changed.
    Substituted { count: usize, lines_changed: usize },
    /// A no-op response for successful commands that don't need a side
    /// effect but should not be reported as unknown (e.g. `:noh`).
    Ok,
    /// Surface an informational message.
    Info(String),
    /// Surface an error message (syntax error, bad pattern, …).
    Error(String),
}

/// Parse and execute `input` (without the leading `:`).
pub fn run<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    input: &str,
) -> ExEffect {
    let cmd = input.trim();
    if cmd.is_empty() {
        return ExEffect::None;
    }

    // `:/pat` / `:?pat` — search-as-address with no following command.
    // Vim parses these as line addresses; with nothing trailing, the
    // cursor jumps to the matched line (forward for `/`, backward for
    // `?`). The trailing matching delimiter is optional: `:/foo`,
    // `:/foo/`, `:?foo`, and `:?foo?` are equivalent. An empty pattern
    // (`:/` / `:?`) reuses the last search like vim.
    if cmd.starts_with('/') || cmd.starts_with('?') {
        let forward = cmd.starts_with('/');
        let delim = if forward { '/' } else { '?' };
        let body = &cmd[1..];
        let pat_str: String = match body.strip_suffix(delim).unwrap_or(body) {
            "" => match editor.last_search() {
                Some(p) if !p.is_empty() => p.to_string(),
                _ => return ExEffect::Error("no previous search pattern".into()),
            },
            s => s.to_string(),
        };
        let s = editor.settings();
        let case_insensitive =
            s.ignore_case && !(s.smartcase && pat_str.chars().any(|c| c.is_uppercase()));
        let compile_src: std::borrow::Cow<'_, str> = if case_insensitive {
            std::borrow::Cow::Owned(format!("(?i){pat_str}"))
        } else {
            std::borrow::Cow::Borrowed(pat_str.as_str())
        };
        return match regex::Regex::new(&compile_src) {
            Ok(re) => {
                editor.set_search_pattern(Some(re));
                if forward {
                    editor.search_advance_forward(false);
                } else {
                    editor.search_advance_backward(true);
                }
                editor.ensure_cursor_in_scrolloff();
                editor.set_last_search(Some(pat_str), forward);
                ExEffect::Ok
            }
            Err(e) => ExEffect::Error(format!("bad search pattern: {e}")),
        };
    }

    // Strip a leading range (`5,10`, `.,$`, `'a,'b`, `%`). `range` is
    // None when the user typed no addresses; the handler defaults to
    // the command's natural scope (current line for `:s`, whole buffer
    // for `:sort` / `:g`). Resolution errors surface as ExEffect::Error.
    let (range, cmd) = match parse_range(cmd, editor) {
        Ok(pair) => pair,
        Err(e) => return ExEffect::Error(e),
    };

    // Bare line number — jump there. (Only when no range was parsed,
    // since `parse_range` already consumes a leading number as an
    // address; a bare `:5` falls through with `range = Some(5..=5)`
    // and an empty `cmd`.)
    if range.is_none() {
        if let Ok(line) = cmd.parse::<usize>() {
            editor.goto_line(line);
            return ExEffect::Ok;
        }
    } else if cmd.is_empty() {
        // `:5` jumps to line 5; `:5,10` lands on the start of the
        // range (vim's behaviour for a bare-range command).
        if let Some(r) = range {
            editor.goto_line(r.start_one_based());
            return ExEffect::Ok;
        }
    }

    // Expand the leading command-name prefix to its canonical full
    // name (`:noh` → `:nohlsearch`, `:red` → `:redo`, `:s/foo/bar/` →
    // `:substitute/foo/bar/`). The match arms below match canonical
    // names only; the table in `COMMAND_NAMES` is the single source of
    // truth for prefix resolution.
    let canon = canonical_command_name(cmd);
    let cmd: &str = canon.as_ref();

    // Bare-name terminals (`:quit`, `:write`, `:wq`, `:x`, …).
    match cmd {
        "quit" => {
            return ExEffect::Quit {
                force: false,
                save: false,
            };
        }
        "quit!" => {
            return ExEffect::Quit {
                force: true,
                save: false,
            };
        }
        "write" => return ExEffect::Save,
        "wq" | "x" => {
            return ExEffect::Quit {
                force: false,
                save: true,
            };
        }
        "qall" => {
            return ExEffect::Quit {
                force: false,
                save: false,
            };
        }
        "qall!" => {
            return ExEffect::Quit {
                force: true,
                save: false,
            };
        }
        "wqall" | "wqall!" => {
            return ExEffect::Quit {
                force: false,
                save: true,
            };
        }
        "nohlsearch" => {
            editor.set_search_pattern(None);
            return ExEffect::Ok;
        }
        "registers" => return ExEffect::Info(format_registers(editor)),
        "marks" => return ExEffect::Info(format_marks(editor)),
        "jumps" => return ExEffect::Info(format_jumps(editor)),
        "changes" => return ExEffect::Info(format_changes(editor)),
        "undo" => {
            editor.undo();
            return ExEffect::Ok;
        }
        "redo" => {
            editor.redo();
            return ExEffect::Ok;
        }
        "foldindent" => return apply_fold_indent(editor),
        "foldsyntax" => return apply_fold_syntax(editor),
        _ => {}
    }

    // `:write <path>` — save to a specific file. Caller updates filename.
    if let Some(path) = cmd.strip_prefix("write ") {
        let path = path.trim();
        if !path.is_empty() {
            return ExEffect::SaveAs(path.to_string());
        }
    }

    // `:[range]sort[!][iun]` — defaults to the whole buffer when no
    // range is given.
    if let Some(rest) = cmd.strip_prefix("sort") {
        return apply_sort(editor, range, rest);
    }

    // `:set [option ...]` — toggle / assign vim options. Range is
    // ignored (vim's `:set` doesn't accept one).
    if let Some(rest) = cmd
        .strip_prefix("set ")
        .or(if cmd == "set" { Some("") } else { None })
    {
        return apply_set(editor, rest);
    }

    // `:[range]g/pat/cmd` and inverse `:v/pat/cmd`. parse_global_prefix
    // handles canonical `global` / `vglobal` plus the bare `g` / `v`
    // single-letter forms, since canonicalization preserves a leading
    // `g`/`v` when the table can't disambiguate without a min_prefix.
    if let Some((negate, rest)) = parse_global_prefix(cmd) {
        return apply_global(editor, range, rest, negate);
    }

    // `:[range]substitute/...`. The legacy `:%s/...` form (no separate
    // range) still works because `%` is parsed by `parse_range` above.
    if let Some(rest) = cmd.strip_prefix("substitute") {
        return match parse_substitute_body(rest) {
            Ok(sub) => match apply_substitute(editor, range, sub) {
                Ok((count, lines_changed)) => ExEffect::Substituted {
                    count,
                    lines_changed,
                },
                Err(e) => ExEffect::Error(e),
            },
            Err(e) => ExEffect::Error(e),
        };
    }

    // `:[range]delete` — delete the range. Reuses :g/pat/d's row-drop loop.
    if cmd == "delete" {
        return apply_delete_range(editor, range);
    }

    // `:r path` / `:read path` — insert file contents below the
    // current line. Range is currently ignored; vim's `:Nr file`
    // semantics (insert below row N) can land later if needed.
    if let Some(path) = cmd.strip_prefix("read ") {
        return apply_read_file(editor, path.trim());
    }

    // `:[range]!cmd` — pipe rows through `cmd`, replace with stdout.
    // Without a range, `:!cmd` runs the command and surfaces stdout
    // as an Info toast (vim's `:!cmd` shows it in the message area).
    if let Some(shell_cmd) = cmd.strip_prefix('!') {
        return apply_shell_filter(editor, range, shell_cmd.trim());
    }

    ExEffect::Unknown(cmd.to_string())
}

/// `:foldsyntax` / `:folds` — apply the host-supplied syntax-tree
/// block ranges as closed folds. the host calls
/// [`Editor::set_syntax_fold_ranges`] on every tree-sitter re-parse;
/// running this command consumes the latest snapshot. No-op when the
/// host hasn't pushed any ranges yet.
fn apply_fold_syntax<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
) -> ExEffect {
    let ranges = editor.syntax_fold_ranges().to_vec();
    if ranges.is_empty() {
        return ExEffect::Info("no syntax block ranges available".into());
    }
    let count = ranges.len();
    for (start, end) in ranges {
        editor.apply_fold_op(hjkl_engine::FoldOp::Add {
            start_row: start,
            end_row: end,
            closed: true,
        });
    }
    ExEffect::Info(format!("created {count} fold(s)"))
}

/// `:foldindent` / `:foldi` — derive folds from leading-whitespace runs
/// (vim's `foldmethod=indent`, fired manually because auto-fold-on-edit
/// is expensive). Each row whose successor is more deeply indented
/// becomes a fold opener; the fold extends to the row before indent
/// drops back to or below the opener's level.
fn apply_fold_indent<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
) -> ExEffect {
    let lines = editor.buffer().lines().to_vec();
    let total = lines.len();
    if total == 0 {
        return ExEffect::Ok;
    }
    let indent =
        |line: &str| -> usize { line.chars().take_while(|c| *c == ' ' || *c == '\t').count() };
    let indents: Vec<usize> = lines.iter().map(|l| indent(l)).collect();
    let blank: Vec<bool> = lines.iter().map(|l| l.trim().is_empty()).collect();
    let mut new_folds: Vec<(usize, usize)> = Vec::new();
    let mut i = 0;
    while i + 1 < total {
        if blank[i] {
            i += 1;
            continue;
        }
        let head_indent = indents[i];
        let mut j = i + 1;
        // Skip blanks adjacent to the head — they belong to the same
        // block so a fold can span across them.
        while j < total && blank[j] {
            j += 1;
        }
        if j >= total || indents[j] <= head_indent {
            i += 1;
            continue;
        }
        // We have a fold opener — walk forward until indent drops back
        // to <= head_indent on a non-blank row.
        let mut end = j;
        let mut k = j + 1;
        while k < total {
            if !blank[k] && indents[k] <= head_indent {
                break;
            }
            end = k;
            k += 1;
        }
        new_folds.push((i, end));
        // Step by one (not past `end`) so nested indented runs inside
        // the outer block also get their own fold.
        i += 1;
    }
    if new_folds.is_empty() {
        return ExEffect::Info("no indented blocks to fold".into());
    }
    let count = new_folds.len();
    for (start, end) in new_folds {
        editor.apply_fold_op(hjkl_engine::FoldOp::Add {
            start_row: start,
            end_row: end,
            closed: true,
        });
    }
    ExEffect::Info(format!("created {count} fold(s)"))
}

/// `:[range]!cmd` — pipe the range through `cmd` (or run bare with no
/// range). With a range, the rows are joined with `\n`, fed via
/// stdin to `sh -c cmd`, and replaced with stdout. Without a range
/// the command runs detached and stdout returns as an Info toast.
fn apply_shell_filter<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    range: Option<Range>,
    cmd: &str,
) -> ExEffect {
    if cmd.is_empty() {
        return ExEffect::Error(":! needs a shell command".into());
    }
    use std::io::Write;
    use std::process::{Command, Stdio};

    if range.is_none() {
        // Bare `:!cmd` — run, no buffer change, surface stdout via Info.
        let output = Command::new("sh").arg("-c").arg(cmd).output();
        return match output {
            Ok(out) if out.status.success() => {
                let stdout = String::from_utf8_lossy(&out.stdout).trim_end().to_string();
                if stdout.is_empty() {
                    ExEffect::Info(format!("`{cmd}` exited 0"))
                } else {
                    ExEffect::Info(stdout)
                }
            }
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr);
                let trimmed = stderr.trim();
                let label = if trimmed.is_empty() {
                    "no stderr".to_string()
                } else {
                    trimmed.to_string()
                };
                ExEffect::Error(format!(
                    "command exited {} ({label})",
                    out.status
                        .code()
                        .map(|c| c.to_string())
                        .unwrap_or_else(|| "?".into())
                ))
            }
            Err(e) => ExEffect::Error(format!("cannot run `{cmd}`: {e}")),
        };
    }

    // Range supplied — pipe the rows through the command.
    let scope = Range::or_default(range, Range::whole(editor));
    let mut all_lines: Vec<String> = editor.buffer().lines().to_vec();
    let total = all_lines.len();
    if total == 0 {
        return ExEffect::Ok;
    }
    let bot = scope.end.min(total - 1);
    if scope.start > bot {
        return ExEffect::Ok;
    }
    let payload = all_lines[scope.start..=bot].join("\n");
    let mut child = match Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => return ExEffect::Error(format!("cannot spawn `{cmd}`: {e}")),
    };
    if let Some(stdin) = child.stdin.as_mut() {
        match stdin.write_all(payload.as_bytes()) {
            Ok(()) => {}
            // Child closed stdin before we finished writing (e.g. `exit 5`).
            // Fall through to wait_with_output() so its actual exit status
            // wins instead of masking it as a write error.
            Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {}
            Err(e) => return ExEffect::Error(format!("cannot write to `{cmd}`: {e}")),
        }
    }
    let output = match child.wait_with_output() {
        Ok(o) => o,
        Err(e) => return ExEffect::Error(format!("`{cmd}` failed: {e}")),
    };
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let trimmed = stderr.trim();
        let label = if trimmed.is_empty() {
            "no stderr".to_string()
        } else {
            trimmed.to_string()
        };
        return ExEffect::Error(format!(
            "command exited {} ({label})",
            output
                .status
                .code()
                .map(|c| c.to_string())
                .unwrap_or_else(|| "?".into())
        ));
    }
    let stdout = match String::from_utf8(output.stdout) {
        Ok(s) => s,
        Err(_) => return ExEffect::Error("filter output was not UTF-8".into()),
    };
    let trimmed = stdout.strip_suffix('\n').unwrap_or(&stdout);
    let new_rows: Vec<String> = trimmed.split('\n').map(String::from).collect();

    editor.push_undo();
    let after: Vec<String> = all_lines.split_off(bot + 1);
    all_lines.truncate(scope.start);
    all_lines.extend(new_rows);
    all_lines.extend(after);
    editor.restore(all_lines, (scope.start, 0));
    mark_dirty_after_ex(editor);
    ExEffect::Ok
}

/// `:r file` — read `path` from disk and insert below the current
/// row. Cursor lands on the first row of the inserted content.
/// Failures (missing file, permission denied) surface as
/// `ExEffect::Error` toasts.
fn apply_read_file<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    path: &str,
) -> ExEffect {
    use hjkl_buffer::{Edit, Position};
    if path.is_empty() {
        return ExEffect::Error(":r needs a file path or `!cmd`".into());
    }
    // `:r !cmd` runs `cmd` through `sh -c` and inserts stdout. Same
    // security posture as running anything from a shell — the user
    // typed the command themselves.
    let content = if let Some(cmd) = path.strip_prefix('!') {
        let cmd = cmd.trim();
        if cmd.is_empty() {
            return ExEffect::Error(":r ! needs a shell command".into());
        }
        match std::process::Command::new("sh").arg("-c").arg(cmd).output() {
            Ok(out) if out.status.success() => match String::from_utf8(out.stdout) {
                Ok(s) => s,
                Err(_) => return ExEffect::Error("command output was not UTF-8".into()),
            },
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr);
                let trimmed = stderr.trim();
                let label = if trimmed.is_empty() {
                    "no stderr".to_string()
                } else {
                    trimmed.to_string()
                };
                return ExEffect::Error(format!(
                    "command exited {} ({label})",
                    out.status
                        .code()
                        .map(|c| c.to_string())
                        .unwrap_or_else(|| "?".into())
                ));
            }
            Err(e) => return ExEffect::Error(format!("cannot run `{cmd}`: {e}")),
        }
    } else {
        match std::fs::read_to_string(path) {
            Ok(s) => s,
            Err(e) => return ExEffect::Error(format!("cannot read `{path}`: {e}")),
        }
    };
    // Vim's `:r` inserts after the current row; trailing newline in
    // the file is dropped to avoid a stray blank tail (vim does the
    // same).
    let trimmed = content.strip_suffix('\n').unwrap_or(&content);
    editor.push_undo();
    let row = editor.cursor().0;
    let line_chars = editor
        .buffer()
        .line(row)
        .map(|l| l.chars().count())
        .unwrap_or(0);
    let insert_text = format!("\n{trimmed}");
    editor.mutate_edit(Edit::InsertStr {
        at: Position::new(row, line_chars),
        text: insert_text,
    });
    // Cursor lands on the first inserted row (row + 1) at col 0.
    editor.jump_cursor(row + 1, 0);
    mark_dirty_after_ex(editor);
    ExEffect::Ok
}

/// 0-based, inclusive line range over the buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Range {
    start: usize,
    end: usize,
}

impl Range {
    fn whole<H: hjkl_engine::Host>(editor: &Editor<hjkl_buffer::Buffer, H>) -> Self {
        let last = editor.buffer().lines().len().saturating_sub(1);
        Self {
            start: 0,
            end: last,
        }
    }

    fn single(row: usize) -> Self {
        Self {
            start: row,
            end: row,
        }
    }

    fn start_one_based(&self) -> usize {
        self.start + 1
    }

    fn or_default(opt: Option<Self>, default: Self) -> Self {
        opt.unwrap_or(default)
    }
}

/// Single ex-mode address: `5`, `.`, `$`, `'a`. No `+/-` offset arith
/// yet — keeps the parser tight.
#[derive(Debug, Clone, Copy)]
enum Address {
    Number(usize), // 1-based, as the user typed it
    Current,
    Last,
    Mark(char),
}

/// Strip a leading address from `s` and return it plus the remainder.
/// Returns `None` when `s` doesn't start with one — the caller treats
/// that as "no range provided".
fn parse_address(s: &str) -> Option<(Address, &str)> {
    let mut chars = s.char_indices();
    let (_, first) = chars.next()?;
    match first {
        '.' => Some((Address::Current, &s[1..])),
        '$' => Some((Address::Last, &s[1..])),
        '\'' => {
            let (_, mark) = chars.next()?;
            Some((Address::Mark(mark), &s[2..]))
        }
        '0'..='9' => {
            let mut end = 1;
            for (i, c) in s.char_indices().skip(1) {
                if c.is_ascii_digit() {
                    end = i + c.len_utf8();
                } else {
                    break;
                }
            }
            let n: usize = s[..end].parse().ok()?;
            Some((Address::Number(n), &s[end..]))
        }
        _ => None,
    }
}

/// Resolve a parsed address against the current editor state. Numeric
/// addresses are clamped to the buffer; bad marks return an error.
fn resolve_address<H: hjkl_engine::Host>(
    addr: Address,
    editor: &Editor<hjkl_buffer::Buffer, H>,
) -> Result<usize, String> {
    let last = editor.buffer().lines().len().saturating_sub(1);
    match addr {
        Address::Number(n) => Ok(n.saturating_sub(1).min(last)),
        Address::Current => Ok(editor.cursor().0),
        Address::Last => Ok(last),
        Address::Mark(c) => editor
            .mark(c)
            .map(|(r, _)| r.min(last))
            .ok_or_else(|| format!("mark `{c}` not set")),
    }
}

/// Strip a leading range (`%`, `N`, `N,M`, `.,$`, `'a,'b`) from `cmd`.
/// Returns the resolved 0-based inclusive range plus the remainder.
fn parse_range<'a, H: hjkl_engine::Host>(
    cmd: &'a str,
    editor: &Editor<hjkl_buffer::Buffer, H>,
) -> Result<(Option<Range>, &'a str), String> {
    if let Some(rest) = cmd.strip_prefix('%') {
        return Ok((Some(Range::whole(editor)), rest));
    }
    let Some((start_addr, after_start)) = parse_address(cmd) else {
        return Ok((None, cmd));
    };
    let start = resolve_address(start_addr, editor)?;
    if let Some(after_comma) = after_start.strip_prefix(',') {
        let (end_addr, rest) =
            parse_address(after_comma).unwrap_or((Address::Number(start + 1), after_comma));
        let end = resolve_address(end_addr, editor)?;
        let (lo, hi) = if start <= end {
            (start, end)
        } else {
            (end, start)
        };
        return Ok((Some(Range { start: lo, end: hi }), rest));
    }
    Ok((Some(Range::single(start)), after_start))
}

/// `:[range]d` — drop every row in the range.
fn apply_delete_range<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    range: Option<Range>,
) -> ExEffect {
    use hjkl_buffer::{Edit, MotionKind, Position};
    let r = Range::or_default(range, Range::single(editor.cursor().0));
    let total = editor.buffer().row_count();
    if total == 0 {
        return ExEffect::Ok;
    }
    let bot = r.end.min(total.saturating_sub(1));
    if r.start > bot {
        return ExEffect::Ok;
    }
    editor.push_undo();
    // Delete bottom-up so row indices stay valid.
    for row in (r.start..=bot).rev() {
        if editor.buffer().row_count() == 1 {
            let line_chars = editor
                .buffer()
                .line(0)
                .map(|l| l.chars().count())
                .unwrap_or(0);
            if line_chars > 0 {
                editor.mutate_edit(Edit::DeleteRange {
                    start: Position::new(0, 0),
                    end: Position::new(0, line_chars),
                    kind: MotionKind::Char,
                });
            }
            continue;
        }
        editor.mutate_edit(Edit::DeleteRange {
            start: Position::new(row, 0),
            end: Position::new(row, 0),
            kind: MotionKind::Line,
        });
    }
    mark_dirty_after_ex(editor);
    ExEffect::Ok
}

/// Detect a `:g/pat/cmd`, `:g!/pat/cmd`, or `:v/pat/cmd` prefix.
/// Returns `(negate, body_after_prefix)` where `body_after_prefix`
/// still has the leading separator + pattern + cmd attached.
fn parse_global_prefix(cmd: &str) -> Option<(bool, &str)> {
    // After canonicalization, `:g/pat/d` arrives as "global/pat/d" and
    // `:v/pat/d` as "vglobal/pat/d". `:g!` (negate) becomes "global!".
    if let Some(rest) = cmd.strip_prefix("global!") {
        return Some((true, rest));
    }
    if let Some(rest) = cmd.strip_prefix("vglobal") {
        return Some((true, rest));
    }
    if let Some(rest) = cmd.strip_prefix("global") {
        return Some((false, rest));
    }
    None
}

/// Run `:[range]g/pat/d` (or its negated variants). Walks the rows in
/// `range` (whole buffer when None), collects matches, then drops them
/// in reverse so row indices stay valid through the cascade of deletes.
fn apply_global<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    range: Option<Range>,
    body: &str,
    negate: bool,
) -> ExEffect {
    use hjkl_buffer::{Edit, MotionKind, Position};
    let mut chars = body.chars();
    let sep = match chars.next() {
        Some(c) => c,
        None => return ExEffect::Error("empty :g pattern".into()),
    };
    if sep.is_alphanumeric() || sep == '\\' {
        return ExEffect::Error("global needs a separator, e.g. :g/foo/d".into());
    }
    let rest: String = chars.collect();
    let parts = split_unescaped(&rest, sep);
    if parts.len() < 2 {
        return ExEffect::Error("global needs /pattern/cmd".into());
    }
    let pattern = unescape(&parts[0], sep);
    let cmd = parts[1].trim();
    if cmd != "d" {
        return ExEffect::Error(format!(":g supports only `d` today, got `{cmd}`"));
    }
    let regex = match regex::Regex::new(&pattern) {
        Ok(r) => r,
        Err(e) => return ExEffect::Error(format!("bad pattern: {e}")),
    };

    editor.push_undo();
    // Identify rows to drop (newest-first so multi-line drops don't
    // shift indices under us). Default to the whole buffer when no
    // range was supplied — matches vim's `:g/pat/d` (no range = `%`).
    let scope = Range::or_default(range, Range::whole(editor));
    let row_count = editor.buffer().row_count();
    let bot = scope.end.min(row_count.saturating_sub(1));
    let mut targets: Vec<usize> = Vec::new();
    for row in scope.start..=bot {
        let line = editor.buffer().line(row).unwrap_or("");
        let matches = regex.is_match(line);
        if matches != negate {
            targets.push(row);
        }
    }
    if targets.is_empty() {
        editor.pop_last_undo();
        return ExEffect::Substituted {
            count: 0,
            lines_changed: 0,
        };
    }
    let count = targets.len();
    for row in targets.iter().rev() {
        let row = *row;
        // Last row in a 1-row buffer can't be removed (Buffer keeps
        // the one-empty-row invariant); just clear it instead.
        if editor.buffer().row_count() == 1 {
            let line_chars = editor
                .buffer()
                .line(0)
                .map(|l| l.chars().count())
                .unwrap_or(0);
            if line_chars > 0 {
                editor.mutate_edit(Edit::DeleteRange {
                    start: Position::new(0, 0),
                    end: Position::new(0, line_chars),
                    kind: MotionKind::Char,
                });
            }
            continue;
        }
        editor.mutate_edit(Edit::DeleteRange {
            start: Position::new(row, 0),
            end: Position::new(row, 0),
            kind: MotionKind::Line,
        });
    }
    mark_dirty_after_ex(editor);
    ExEffect::Substituted {
        count,
        lines_changed: count,
    }
}

/// `:set [opt ...]` body. Splits on whitespace and applies each token.
/// Bare `:set` reports the current values for the supported options.
fn apply_set<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    body: &str,
) -> ExEffect {
    let trimmed = body.trim();
    if trimmed.is_empty() {
        let s = editor.settings();
        let wrap = match s.wrap {
            hjkl_buffer::Wrap::None => "off",
            hjkl_buffer::Wrap::Char => "char",
            hjkl_buffer::Wrap::Word => "word",
        };
        let scl = match s.signcolumn {
            hjkl_engine::types::SignColumnMode::Yes => "yes",
            hjkl_engine::types::SignColumnMode::No => "no",
            hjkl_engine::types::SignColumnMode::Auto => "auto",
        };
        return ExEffect::Info(format!(
            "shiftwidth={}  tabstop={}  softtabstop={}  textwidth={}  undolevels={}  timeoutlen={}  iskeyword=\"{}\"  expandtab={}  ignorecase={}  smartcase={}  wrapscan={}  autoindent={}  smartindent={}  undobreak={}  readonly={}  wrap={}  number={}  relativenumber={}  numberwidth={}  cursorline={}  cursorcolumn={}  signcolumn={}  foldcolumn={}  colorcolumn=\"{}\"",
            s.shiftwidth,
            s.tabstop,
            s.softtabstop,
            s.textwidth,
            s.undo_levels,
            s.timeout_len.as_millis(),
            s.iskeyword,
            if s.expandtab { "on" } else { "off" },
            if s.ignore_case { "on" } else { "off" },
            if s.smartcase { "on" } else { "off" },
            if s.wrapscan { "on" } else { "off" },
            if s.autoindent { "on" } else { "off" },
            if s.smartindent { "on" } else { "off" },
            if s.undo_break_on_motion { "on" } else { "off" },
            if s.readonly { "on" } else { "off" },
            wrap,
            if s.number { "on" } else { "off" },
            if s.relativenumber { "on" } else { "off" },
            s.numberwidth,
            if s.cursorline { "on" } else { "off" },
            if s.cursorcolumn { "on" } else { "off" },
            scl,
            s.foldcolumn,
            s.colorcolumn,
        ));
    }
    for token in trimmed.split_whitespace() {
        if let Err(e) = apply_set_token(editor, token) {
            return ExEffect::Error(e);
        }
    }
    ExEffect::Ok
}

/// Apply a single `:set` token. Supports `name=value`, bare `name`
/// (turns booleans on), and `noname` (turns booleans off).
fn apply_set_token<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    token: &str,
) -> Result<(), String> {
    if let Some((name, value)) = token.split_once('=') {
        // String-valued options short-circuit the numeric parse.
        if matches!(name, "iskeyword" | "isk") {
            editor.set_iskeyword(value);
            return Ok(());
        }
        if matches!(name, "signcolumn" | "scl") {
            editor.settings_mut().signcolumn = match value {
                "yes" => hjkl_engine::types::SignColumnMode::Yes,
                "no" => hjkl_engine::types::SignColumnMode::No,
                "auto" => hjkl_engine::types::SignColumnMode::Auto,
                other => {
                    return Err(format!(
                        "signcolumn must be `yes`, `no`, or `auto`, got `{other}`"
                    ));
                }
            };
            return Ok(());
        }
        if matches!(name, "colorcolumn" | "cc") {
            editor.settings_mut().colorcolumn = value.to_string();
            return Ok(());
        }
        let parsed: usize = value
            .parse()
            .map_err(|_| format!("bad value `{value}` for :set {name}"))?;
        match name {
            "shiftwidth" | "sw" => {
                if parsed == 0 {
                    return Err("shiftwidth must be > 0".into());
                }
                editor.settings_mut().shiftwidth = parsed;
            }
            "tabstop" | "ts" => {
                if parsed == 0 {
                    return Err("tabstop must be > 0".into());
                }
                editor.settings_mut().tabstop = parsed;
            }
            "textwidth" | "tw" => {
                if parsed == 0 {
                    return Err("textwidth must be > 0".into());
                }
                editor.settings_mut().textwidth = parsed;
            }
            "undolevels" | "ul" => {
                editor.settings_mut().undo_levels = parsed.min(u32::MAX as usize) as u32;
            }
            "timeoutlen" | "tm" => {
                editor.settings_mut().timeout_len =
                    core::time::Duration::from_millis(parsed as u64);
            }
            "numberwidth" | "nuw" => {
                if !(1..=20).contains(&parsed) {
                    return Err(format!("numberwidth must be in range 1..=20, got {parsed}"));
                }
                editor.settings_mut().numberwidth = parsed;
            }
            "foldcolumn" | "fdc" => {
                if parsed > 12 {
                    return Err(format!("foldcolumn must be in range 0..=12, got {parsed}"));
                }
                editor.settings_mut().foldcolumn = parsed as u32;
            }
            other => return Err(format!("unknown :set option `{other}`")),
        }
        return Ok(());
    }
    // Handle toggle (name!) — must check before the `no` strip.
    if let Some(name) = token.strip_suffix('!') {
        match name {
            "number" | "nu" => {
                editor.settings_mut().number = !editor.settings().number;
            }
            "relativenumber" | "rnu" => {
                editor.settings_mut().relativenumber = !editor.settings().relativenumber;
            }
            "cursorline" | "cul" => {
                editor.settings_mut().cursorline = !editor.settings().cursorline;
            }
            "cursorcolumn" | "cuc" => {
                editor.settings_mut().cursorcolumn = !editor.settings().cursorcolumn;
            }
            other => return Err(format!("unknown :set option `{other}`")),
        }
        return Ok(());
    }
    let (name, value) = if let Some(rest) = token.strip_prefix("no") {
        (rest, false)
    } else {
        (token, true)
    };
    match name {
        "ignorecase" | "ic" => editor.settings_mut().ignore_case = value,
        "smartcase" | "scs" => editor.settings_mut().smartcase = value,
        "wrapscan" | "ws" => editor.settings_mut().wrapscan = value,
        "expandtab" | "et" => editor.settings_mut().expandtab = value,
        "autoindent" | "ai" => editor.settings_mut().autoindent = value,
        "smartindent" | "si" => editor.settings_mut().smartindent = value,
        "undobreak" => editor.settings_mut().undo_break_on_motion = value,
        "readonly" | "ro" => editor.settings_mut().readonly = value,
        "number" | "nu" => editor.settings_mut().number = value,
        "relativenumber" | "rnu" => editor.settings_mut().relativenumber = value,
        "cursorline" | "cul" => editor.settings_mut().cursorline = value,
        "cursorcolumn" | "cuc" => editor.settings_mut().cursorcolumn = value,
        "wrap" => {
            editor.settings_mut().wrap = if value {
                // Preserve `Wrap::Word` if `linebreak` already flipped
                // word-mode on; otherwise default `set wrap` to char.
                match editor.settings().wrap {
                    hjkl_buffer::Wrap::Word => hjkl_buffer::Wrap::Word,
                    _ => hjkl_buffer::Wrap::Char,
                }
            } else {
                hjkl_buffer::Wrap::None
            };
        }
        "linebreak" | "lbr" => {
            editor.settings_mut().wrap = if value {
                hjkl_buffer::Wrap::Word
            } else {
                // `nolinebreak` drops back to char wrap when wrap is on,
                // otherwise stays off.
                match editor.settings().wrap {
                    hjkl_buffer::Wrap::None => hjkl_buffer::Wrap::None,
                    _ => hjkl_buffer::Wrap::Char,
                }
            };
        }
        // Booleans we don't (yet) honour: accept silently so :set lines
        // copied from a vimrc don't error out. `foldenable` falls here.
        "foldenable" | "fen" => {}
        other => return Err(format!("unknown :set option `{other}`")),
    }
    Ok(())
}

/// `:[range]sort[!][iun]` body — `flags` is whatever followed the
/// command name (e.g. `!u`, ` un`, `i`). Sorts only the rows in `range`
/// (or the whole buffer when None).
fn apply_sort<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    range: Option<Range>,
    flags: &str,
) -> ExEffect {
    let trimmed = flags.trim();
    let mut reverse = false;
    let mut unique = false;
    let mut numeric = false;
    let mut ignore_case = false;
    for c in trimmed.chars() {
        match c {
            '!' => reverse = true,
            'u' => unique = true,
            'n' => numeric = true,
            'i' => ignore_case = true,
            ' ' | '\t' => {}
            other => return ExEffect::Error(format!("bad :sort flag `{other}`")),
        }
    }

    let mut all_lines: Vec<String> = editor.buffer().lines().to_vec();
    let total = all_lines.len();
    if total == 0 {
        return ExEffect::Ok;
    }
    let scope = Range::or_default(range, Range::whole(editor));
    let bot = scope.end.min(total - 1);
    if scope.start > bot {
        return ExEffect::Ok;
    }
    // Sort only the slice in range; keep the rest of the buffer intact.
    let mut slice: Vec<String> = all_lines[scope.start..=bot].to_vec();
    if numeric {
        // Vim's `:sort n`: extract the first decimal integer (with
        // optional leading `-`) on each line; lines with no number
        // sort first, in original order.
        slice.sort_by_key(|l| extract_leading_number(l));
    } else if ignore_case {
        slice.sort_by_key(|s| s.to_lowercase());
    } else {
        slice.sort();
    }
    if reverse {
        slice.reverse();
    }
    if unique {
        let cmp_key = |s: &str| -> String {
            if ignore_case {
                s.to_lowercase()
            } else {
                s.to_string()
            }
        };
        let mut seen = std::collections::HashSet::new();
        slice.retain(|line| seen.insert(cmp_key(line)));
    }
    // Splice the sorted slice back. `unique` may have shortened it.
    let after: Vec<String> = all_lines.split_off(bot + 1);
    all_lines.truncate(scope.start);
    all_lines.extend(slice);
    all_lines.extend(after);

    editor.push_undo();
    editor.restore(all_lines, (scope.start, 0));
    mark_dirty_after_ex(editor);
    ExEffect::Ok
}

/// Parse the first signed decimal integer from `line` for `:sort n`.
/// Lines with no leading number sort as `i64::MIN` so they cluster at
/// the top, matching vim's behaviour.
fn extract_leading_number(line: &str) -> i64 {
    let bytes = line.as_bytes();
    let mut i = 0;
    while i < bytes.len() && !bytes[i].is_ascii_digit() && bytes[i] != b'-' {
        i += 1;
    }
    if i >= bytes.len() {
        return i64::MIN;
    }
    let mut j = i;
    if bytes[j] == b'-' {
        j += 1;
    }
    let start = j;
    while j < bytes.len() && bytes[j].is_ascii_digit() {
        j += 1;
    }
    if j == start {
        return i64::MIN;
    }
    line[i..j].parse().unwrap_or(i64::MIN)
}

/// `:reg` / `:registers` — tabular dump of every non-empty register slot.
fn format_registers<H: hjkl_engine::Host>(editor: &Editor<hjkl_buffer::Buffer, H>) -> String {
    let r = editor.registers();
    let mut lines = vec!["--- Registers ---".to_string()];
    let mut push = |sel: &str, text: &str, linewise: bool| {
        if text.is_empty() {
            return;
        }
        let marker = if linewise { "L" } else { " " };
        lines.push(format!("{sel:<3} {marker} {}", display_register(text)));
    };
    push("\"\"", &r.unnamed.text, r.unnamed.linewise);
    push("\"0", &r.yank_zero.text, r.yank_zero.linewise);
    for (i, slot) in r.delete_ring.iter().enumerate() {
        let sel = format!("\"{}", i + 1);
        push(&sel, &slot.text, slot.linewise);
    }
    for (i, slot) in r.named.iter().enumerate() {
        let sel = format!("\"{}", (b'a' + i as u8) as char);
        push(&sel, &slot.text, slot.linewise);
    }
    if lines.len() == 1 {
        lines.push("(no registers set)".to_string());
    }
    lines.join("\n")
}

/// Escape control chars + truncate so a multi-line register fits a single row
/// of the toast table.
fn display_register(text: &str) -> String {
    let escaped: String = text
        .chars()
        .map(|c| match c {
            '\n' => "\\n".to_string(),
            '\t' => "\\t".to_string(),
            '\r' => "\\r".to_string(),
            c => c.to_string(),
        })
        .collect();
    const MAX: usize = 60;
    if escaped.chars().count() > MAX {
        let head: String = escaped.chars().take(MAX - 3).collect();
        format!("{head}...")
    } else {
        escaped
    }
}

/// `:marks` — list every set mark with `(line, col)`. Lines are 1-based to
/// match vim; cols are 0-based.
fn format_marks<H: hjkl_engine::Host>(editor: &Editor<hjkl_buffer::Buffer, H>) -> String {
    let mut lines = vec!["--- Marks ---".to_string(), "mark  line  col".to_string()];
    // 0.0.36: lowercase + uppercase marks share the unified
    // `Editor::marks` map. `marks()` already iterates in BTreeMap
    // (char-ordered) sequence, so no extra sort needed.
    let entries: Vec<(char, usize, usize)> =
        editor.marks().map(|(c, (r, col))| (c, r, col)).collect();
    for (c, r, col) in entries {
        lines.push(format!(" {c}    {:>4}  {col:>3}", r + 1));
    }
    if let Some((r, col)) = editor.last_jump_back() {
        lines.push(format!(" '    {:>4}  {col:>3}", r + 1));
    }
    if let Some((r, col)) = editor.last_edit_pos() {
        lines.push(format!(" .    {:>4}  {col:>3}", r + 1));
    }
    if lines.len() == 2 {
        lines.push("(no marks set)".to_string());
    }
    lines.join("\n")
}

/// `:jumps` — list the jump-back and jump-forward lists.
/// Format mirrors vim: `jump  line  col  file` columns. Newest items
/// have the smallest `jump` number; current position is `0`.
fn format_jumps<H: hjkl_engine::Host>(editor: &Editor<hjkl_buffer::Buffer, H>) -> String {
    let (back, fwd) = editor.jump_list();
    if back.is_empty() && fwd.is_empty() {
        return "(no jumps recorded)".to_string();
    }
    let mut lines = vec![
        "--- Jump list ---".to_string(),
        " jump  line   col".to_string(),
    ];
    // jump_back: oldest at index 0, newest at the end.
    // Display as descending jump numbers: back.len() at oldest, 1 at newest, 0 = current.
    // Then jump_fwd (forward history) at negative-1, -2, …  vim shows >0 for fwd.
    // We keep it simple: back list reversed with ascending index, then fwd list.
    let back_len = back.len();
    for (i, &(row, col)) in back.iter().rev().enumerate() {
        let jump_num = i + 1;
        lines.push(format!("{jump_num:>5}  {:>4}  {:>4}", row + 1, col));
    }
    // Mark current position (not in list — just a separator).
    lines.push(format!("{:>5}  (current position)", 0));
    for (i, &(row, col)) in fwd.iter().enumerate() {
        let jump_num = -(i as isize + 1);
        lines.push(format!("{jump_num:>5}  {:>4}  {:>4}", row + 1, col));
    }
    let _ = back_len; // used above
    lines.join("\n")
}

/// `:changes` — list the change list (bounded ring of recent edit positions).
/// Newest entries have lower index numbers, matching vim's `:changes` output.
fn format_changes<H: hjkl_engine::Host>(editor: &Editor<hjkl_buffer::Buffer, H>) -> String {
    let (list, cursor) = editor.change_list();
    if list.is_empty() {
        return "(no changes recorded)".to_string();
    }
    let mut lines = vec![
        "--- Change list ---".to_string(),
        "change  line   col".to_string(),
    ];
    let len = list.len();
    // List is oldest-at-front, newest-at-back; display newest first (change 1 = most recent).
    for (display_idx, &(row, col)) in list.iter().rev().enumerate() {
        let change_num = display_idx + 1;
        // Mark the current walk position, if any. `change_list_cursor` is an
        // index into the original (oldest-first) vec; invert to display-index.
        let marker = match cursor {
            Some(c) if c == len - 1 - display_idx => " <",
            _ => "",
        };
        lines.push(format!(
            "{change_num:>6}  {:>4}  {:>4}{marker}",
            row + 1,
            col
        ));
    }
    lines.join("\n")
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Substitute {
    /// Empty string means "reuse last_search".
    pattern: String,
    replacement: String,
    global: bool,
    case_insensitive: bool,
    /// `I` flag — force case-sensitive (overrides editor ignorecase).
    case_sensitive: bool,
    /// `c` flag — confirm. Parsed, silently ignored (v1 limitation).
    #[allow(dead_code)]
    confirm: bool,
}

/// Parse the `/pat/repl/flags` tail of a substitute command. The leading
/// `s` or `%s` has already been stripped. The separator is the first
/// character after the optional scope (typically `/`), matching vim.
fn parse_substitute_body(body: &str) -> Result<Substitute, String> {
    let mut chars = body.chars();
    let sep = chars.next().ok_or_else(|| "empty substitute".to_string())?;
    if sep.is_alphanumeric() || sep == '\\' {
        return Err("substitute needs a separator, e.g. :s/foo/bar/".into());
    }
    let rest: String = chars.collect();
    let parts = split_unescaped(&rest, sep);
    if parts.len() < 2 {
        return Err("substitute needs /pattern/replacement/".into());
    }
    let pattern = unescape(&parts[0], sep);
    let replacement = unescape(&parts[1], sep);
    let flags = parts.get(2).cloned().unwrap_or_default();
    let mut global = false;
    let mut case_insensitive = false;
    let mut case_sensitive = false;
    let mut confirm = false;
    for f in flags.chars() {
        match f {
            'g' => global = true,
            'i' => case_insensitive = true,
            'I' => case_sensitive = true,
            'c' => confirm = true, // parsed, silently ignored (v1)
            other => return Err(format!("unknown substitute flag: {other}")),
        }
    }
    Ok(Substitute {
        pattern,
        replacement,
        global,
        case_insensitive,
        case_sensitive,
        confirm,
    })
}

/// Split `s` by `sep`, treating `\<sep>` as a literal occurrence.
fn split_unescaped(s: &str, sep: char) -> Vec<String> {
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            if let Some(&next) = chars.peek() {
                // Preserve the escape so regex metachars survive; only
                // collapse an escaped separator into a literal separator.
                if next == sep {
                    cur.push(sep);
                    chars.next();
                } else {
                    cur.push('\\');
                    cur.push(next);
                    chars.next();
                }
            } else {
                cur.push('\\');
            }
        } else if c == sep {
            out.push(std::mem::take(&mut cur));
        } else {
            cur.push(c);
        }
    }
    out.push(cur);
    out
}

/// Remove our `\<sep>` → `<sep>` escape. Other `\x` sequences pass
/// through so regex escape syntax (`\b`, `\d`, …) still works.
fn unescape(s: &str, _sep: char) -> String {
    s.to_string()
}

fn apply_substitute<H: hjkl_engine::Host>(
    editor: &mut Editor<hjkl_buffer::Buffer, H>,
    range: Option<Range>,
    sub: Substitute,
) -> Result<(usize, usize), String> {
    // Resolve effective pattern.  Empty string means "reuse last_search".
    let effective_pat: String = if sub.pattern.is_empty() {
        editor
            .last_search()
            .map(str::to_owned)
            .ok_or_else(|| "no previous regular expression".to_string())?
    } else {
        sub.pattern.clone()
    };

    // Case-sensitivity: `I` (force case-sensitive) > `i` > editor setting.
    let case_insensitive = if sub.case_sensitive {
        false
    } else if sub.case_insensitive {
        true
    } else {
        editor.settings().ignore_case
    };
    let pattern = if case_insensitive {
        format!("(?i){effective_pat}")
    } else {
        effective_pat.clone()
    };
    let regex = regex::Regex::new(&pattern).map_err(|e| format!("bad pattern: {e}"))?;

    editor.push_undo();

    // No range = current line only — matches vim's `:s` default.
    let scope = Range::or_default(range, Range::single(editor.cursor().0));
    let (range_start, range_end) = (scope.start, scope.end);

    let mut new_lines: Vec<String> = editor.buffer().lines().to_vec();
    let mut count = 0usize;
    let mut lines_changed = 0usize;
    let mut last_changed_row = range_start;
    let clamp = range_end.min(new_lines.len().saturating_sub(1));
    for (i, line) in new_lines[range_start..=clamp].iter_mut().enumerate() {
        let (replaced, n) = regex_replace(&regex, line, &sub.replacement, sub.global);
        if n > 0 {
            *line = replaced;
            count += n;
            lines_changed += 1;
            last_changed_row = range_start + i;
        }
    }

    if count == 0 {
        // Undo the undo push so the user doesn't see an empty undo step.
        editor.pop_last_undo();
        return Ok((0, 0));
    }

    // Apply the new content. Yank survives across loads since it's
    // owned by Editor now (was previously held by the textarea).
    editor.buffer_mut().replace_all(&new_lines.join("\n"));
    editor
        .buffer_mut()
        .set_cursor(hjkl_buffer::Position::new(last_changed_row, 0));
    mark_dirty_after_ex(editor);

    // Update last_search so n/N can repeat the same pattern.
    editor.set_last_search(Some(effective_pat), true);

    Ok((count, lines_changed))
}

/// Count-returning variant of `Regex::replace` / `replace_all`. The
/// replacement is first translated from vim's notation (`&`) to the
/// regex crate's (`$0`) so `$n` interpolation still runs.
fn regex_replace(
    regex: &regex::Regex,
    text: &str,
    replacement: &str,
    global: bool,
) -> (String, usize) {
    let matches = regex.find_iter(text).count();
    if matches == 0 {
        return (text.to_string(), 0);
    }
    let rep = expand_vim_replacement(replacement);
    let replaced = if global {
        regex.replace_all(text, rep.as_str()).into_owned()
    } else {
        regex.replace(text, rep.as_str()).into_owned()
    };
    let count = if global { matches } else { 1 };
    (replaced, count)
}

/// Translate vim-ish replacement placeholders to regex ones. For now only
/// `&` → the whole match; vim also supports `\0-\9` which the `regex`
/// crate already honours, so we leave those alone.
fn expand_vim_replacement(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut chars = input.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            if let Some(&next) = chars.peek() {
                out.push('\\');
                out.push(next);
                chars.next();
            } else {
                out.push('\\');
            }
        } else if c == '&' {
            // `&` in vim replacement == whole match, same as `$0` for `regex`.
            out.push_str("$0");
        } else {
            out.push(c);
        }
    }
    out
}

/// Called by ex-command handlers after they rewrite the buffer. Ensures
/// dirty tracking and undo bookkeeping stay consistent.
///
/// Free function rather than an inherent method because Rust's orphan
/// rules forbid `impl Editor` from outside the engine crate. Callers
/// that previously wrote `editor.mark_dirty_after_ex()` now write
/// `mark_dirty_after_ex(editor)`.
fn mark_dirty_after_ex<H: hjkl_engine::Host>(editor: &mut Editor<hjkl_buffer::Buffer, H>) {
    editor.mark_content_dirty();
}

#[cfg(test)]
mod resolver_tests {
    use super::*;

    #[test]
    fn resolves_unique_prefix_to_canonical() {
        assert_eq!(canonical_command_name("noh"), "nohlsearch");
        assert_eq!(canonical_command_name("nohl"), "nohlsearch");
        assert_eq!(canonical_command_name("nohls"), "nohlsearch");
        assert_eq!(canonical_command_name("nohlsearch"), "nohlsearch");
        assert_eq!(canonical_command_name("reg"), "registers");
        assert_eq!(canonical_command_name("red"), "redo");
        assert_eq!(canonical_command_name("u"), "undo");
        assert_eq!(canonical_command_name("e"), "edit");
        assert_eq!(canonical_command_name("se"), "set");
    }

    #[test]
    fn keeps_args_intact() {
        assert_eq!(canonical_command_name("e foo.txt"), "edit foo.txt");
        assert_eq!(canonical_command_name("w /tmp/x"), "write /tmp/x");
        assert_eq!(canonical_command_name("s/foo/bar/"), "substitute/foo/bar/");
        assert_eq!(canonical_command_name("e!"), "edit!");
        assert_eq!(canonical_command_name("q!"), "quit!");
    }

    #[test]
    fn ambiguous_prefix_falls_through() {
        // `f` matches foldindent + foldsyntax, both min_prefix=5, so the
        // shorter input doesn't reach either's threshold and falls
        // through unchanged. `fold` likewise.
        assert_eq!(canonical_command_name("f"), "f");
        assert_eq!(canonical_command_name("fold"), "fold");
        // `r` is min_prefix=1 for read; `red` is exactly redo's
        // min_prefix; `reg` is registers'.
        assert_eq!(canonical_command_name("r"), "read");
        assert_eq!(canonical_command_name("red"), "redo");
        assert_eq!(canonical_command_name("reg"), "registers");
    }

    #[test]
    fn unknown_command_passes_through_unchanged() {
        assert_eq!(canonical_command_name("foobar"), "foobar");
        assert_eq!(canonical_command_name(""), "");
    }

    #[test]
    fn resolves_multi_buffer_commands() {
        assert_eq!(canonical_command_name("bd"), "bdelete");
        assert_eq!(canonical_command_name("bn"), "bnext");
        assert_eq!(canonical_command_name("ls"), "ls");
        assert_eq!(canonical_command_name("bd!"), "bdelete!");
        assert_eq!(canonical_command_name("bp"), "bprevious");
        assert_eq!(canonical_command_name("wa"), "wall");
        assert_eq!(canonical_command_name("qa"), "qall");
        assert_eq!(canonical_command_name("wqa"), "wqall");
    }
}

#[cfg(all(test, feature = "crossterm"))]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    use hjkl_engine::Editor;
    use hjkl_engine::types::{DefaultHost, Options};

    fn new(content: &str) -> Editor {
        let mut e = Editor::new(
            hjkl_buffer::Buffer::new(),
            DefaultHost::new(),
            Options::default(),
        );
        e.set_content(content);
        e
    }

    fn type_keys<H: hjkl_engine::Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, keys: &str) {
        for c in keys.chars() {
            let ev = match c {
                '\n' => KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE),
                '\x1b' => KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE),
                ch => KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE),
            };
            e.handle_key(ev);
        }
    }

    #[test]
    fn substitute_current_line() {
        let mut e = new("foo foo\nfoo foo");
        let effect = run(&mut e, "s/foo/bar/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "bar foo");
        assert_eq!(e.buffer().lines()[1], "foo foo");
    }

    #[test]
    fn substitute_current_line_global() {
        let mut e = new("foo foo\nfoo");
        run(&mut e, "s/foo/bar/g");
        assert_eq!(e.buffer().lines()[0], "bar bar");
        assert_eq!(e.buffer().lines()[1], "foo");
    }

    #[test]
    fn substitute_whole_buffer_global() {
        let mut e = new("foo\nfoo foo\nbar");
        let effect = run(&mut e, "%s/foo/xyz/g");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 3,
                lines_changed: 2
            }
        );
        assert_eq!(e.buffer().lines()[0], "xyz");
        assert_eq!(e.buffer().lines()[1], "xyz xyz");
        assert_eq!(e.buffer().lines()[2], "bar");
    }

    #[test]
    fn substitute_zero_matches_reports_zero() {
        let mut e = new("hello");
        let effect = run(&mut e, "s/xyz/abc/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 0,
                lines_changed: 0
            }
        );
        assert_eq!(e.buffer().lines()[0], "hello");
    }

    #[test]
    fn substitute_respects_case_insensitive_flag() {
        let mut e = new("Foo");
        let effect = run(&mut e, "s/foo/bar/i");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "bar");
    }

    #[test]
    fn substitute_accepts_case_sensitive_flag() {
        let mut e = new("Foo foo");
        e.settings_mut().ignore_case = true;
        // `I` overrides editor's ignorecase — only lowercase matches.
        let effect = run(&mut e, "s/foo/bar/I");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "Foo bar");
    }

    #[test]
    fn substitute_confirm_flag_accepted_not_error() {
        let mut e = new("foo");
        // `c` was previously an error; now it's silently ignored.
        let effect = run(&mut e, "s/foo/bar/c");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "bar");
    }

    #[test]
    fn substitute_empty_pattern_reuses_last_search() {
        let mut e = new("hello world");
        e.set_last_search(Some("world".to_string()), true);
        let effect = run(&mut e, "s//planet/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "hello planet");
    }

    #[test]
    fn substitute_accepts_alternate_separator() {
        let mut e = new("/usr/local/bin");
        run(&mut e, "s#/usr#/opt#");
        assert_eq!(e.buffer().lines()[0], "/opt/local/bin");
    }

    #[test]
    fn substitute_ampersand_in_replacement() {
        let mut e = new("foo");
        run(&mut e, "s/foo/[&]/");
        assert_eq!(e.buffer().lines()[0], "[foo]");
    }

    #[test]
    fn goto_line() {
        let mut e = new("a\nb\nc\nd");
        run(&mut e, "3");
        assert_eq!(e.cursor().0, 2);
    }

    #[test]
    fn quit_and_force_quit() {
        let mut e = new("");
        assert_eq!(
            run(&mut e, "q"),
            ExEffect::Quit {
                force: false,
                save: false
            }
        );
        assert_eq!(
            run(&mut e, "q!"),
            ExEffect::Quit {
                force: true,
                save: false
            }
        );
        assert_eq!(
            run(&mut e, "wq"),
            ExEffect::Quit {
                force: false,
                save: true
            }
        );
    }

    #[test]
    fn qall_and_wqall_dispatch() {
        let mut e = new("");
        // :qa (prefix → qall) — quit all, no force, no save
        assert_eq!(
            run(&mut e, "qa"),
            ExEffect::Quit {
                force: false,
                save: false
            }
        );
        // :qa! (prefix → qall!) — quit all, force, no save
        assert_eq!(
            run(&mut e, "qa!"),
            ExEffect::Quit {
                force: true,
                save: false
            }
        );
        // :qall — same as :qa
        assert_eq!(
            run(&mut e, "qall"),
            ExEffect::Quit {
                force: false,
                save: false
            }
        );
        // :qall! — same as :qa!
        assert_eq!(
            run(&mut e, "qall!"),
            ExEffect::Quit {
                force: true,
                save: false
            }
        );
        // :wqa (prefix → wqall) — quit all, no force, save
        assert_eq!(
            run(&mut e, "wqa"),
            ExEffect::Quit {
                force: false,
                save: true
            }
        );
        // :wqall — same as :wqa
        assert_eq!(
            run(&mut e, "wqall"),
            ExEffect::Quit {
                force: false,
                save: true
            }
        );
        // :wqall! — force=false, save=true (mirrors :wq! behaviour; force not
        // threaded through ExEffect::Quit for the write path)
        assert_eq!(
            run(&mut e, "wqall!"),
            ExEffect::Quit {
                force: false,
                save: true
            }
        );
    }

    #[test]
    fn write_returns_save() {
        let mut e = new("");
        assert_eq!(run(&mut e, "w"), ExEffect::Save);
    }

    #[test]
    fn write_path_returns_save_as() {
        let mut e = new("");
        assert_eq!(
            run(&mut e, "w /tmp/foo.txt"),
            ExEffect::SaveAs("/tmp/foo.txt".to_string())
        );
    }

    #[test]
    fn noh_is_ok() {
        let mut e = new("");
        assert_eq!(run(&mut e, "noh"), ExEffect::Ok);
    }

    #[test]
    fn registers_lists_unnamed_and_named() {
        let mut e = new("hello world");
        // `yw` populates `"` and `"0`; `"ayw` also fills `"a`.
        type_keys(&mut e, "yw");
        type_keys(&mut e, "\"ayw");
        let info = match run(&mut e, "reg") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(info.starts_with("--- Registers ---"));
        assert!(info.contains("\"\""));
        assert!(info.contains("\"0"));
        assert!(info.contains("\"a"));
        // Alias resolves to same command.
        assert_eq!(run(&mut e, "registers"), ExEffect::Info(info));
    }

    #[test]
    fn registers_empty_state() {
        let mut e = new("hi");
        let info = match run(&mut e, "reg") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(info.contains("(no registers set)"));
    }

    #[test]
    fn marks_lists_user_and_special() {
        let mut e = new("alpha\nbeta\ngamma");
        type_keys(&mut e, "ma");
        type_keys(&mut e, "jjmb");
        // `iX<Esc>` produces a last_edit_pos.
        type_keys(&mut e, "iX");
        let info = match run(&mut e, "marks") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(info.starts_with("--- Marks ---"));
        assert!(info.contains(" a "));
        assert!(info.contains(" b "));
        assert!(info.contains(" . "));
    }

    #[test]
    fn undo_alias_reverses_last_change() {
        let mut e = new("hello");
        type_keys(&mut e, "Aworld\x1b");
        assert_eq!(e.buffer().lines()[0], "helloworld");
        assert_eq!(run(&mut e, "undo"), ExEffect::Ok);
        assert_eq!(e.buffer().lines()[0], "hello");
        // Short alias.
        type_keys(&mut e, "Awow\x1b");
        assert_eq!(e.buffer().lines()[0], "hellowow");
        assert_eq!(run(&mut e, "u"), ExEffect::Ok);
        assert_eq!(e.buffer().lines()[0], "hello");
    }

    #[test]
    fn redo_alias_reapplies_undone_change() {
        let mut e = new("hi");
        type_keys(&mut e, "Athere\x1b");
        assert_eq!(e.buffer().lines()[0], "hithere");
        run(&mut e, "undo");
        assert_eq!(e.buffer().lines()[0], "hi");
        assert_eq!(run(&mut e, "redo"), ExEffect::Ok);
        assert_eq!(e.buffer().lines()[0], "hithere");
        // Short alias.
        run(&mut e, "u");
        assert_eq!(run(&mut e, "red"), ExEffect::Ok);
        assert_eq!(e.buffer().lines()[0], "hithere");
    }

    #[test]
    fn marks_empty_state() {
        let mut e = new("hi");
        let info = match run(&mut e, "marks") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(info.contains("(no marks set)"));
    }

    #[test]
    fn sort_alphabetical() {
        let mut e = new("banana\napple\ncherry");
        assert_eq!(run(&mut e, "sort"), ExEffect::Ok);
        assert_eq!(
            e.buffer().lines(),
            vec!["apple".to_string(), "banana".into(), "cherry".into()]
        );
    }

    #[test]
    fn sort_reverse_with_bang() {
        let mut e = new("apple\nbanana\ncherry");
        run(&mut e, "sort!");
        assert_eq!(
            e.buffer().lines(),
            vec!["cherry".to_string(), "banana".into(), "apple".into()]
        );
    }

    #[test]
    fn sort_unique() {
        let mut e = new("foo\nbar\nfoo\nbaz\nbar");
        run(&mut e, "sort u");
        assert_eq!(
            e.buffer().lines(),
            vec!["bar".to_string(), "baz".into(), "foo".into()]
        );
    }

    #[test]
    fn sort_numeric() {
        let mut e = new("10\n2\n100\n7");
        run(&mut e, "sort n");
        assert_eq!(
            e.buffer().lines(),
            vec!["2".to_string(), "7".into(), "10".into(), "100".into()]
        );
    }

    #[test]
    fn sort_ignore_case() {
        let mut e = new("Banana\napple\nCherry");
        run(&mut e, "sort i");
        assert_eq!(
            e.buffer().lines(),
            vec!["apple".to_string(), "Banana".into(), "Cherry".into()]
        );
    }

    #[test]
    fn sort_undo_restores_original_order() {
        let mut e = new("c\nb\na");
        run(&mut e, "sort");
        assert_eq!(e.buffer().lines()[0], "a");
        e.undo();
        assert_eq!(
            e.buffer().lines(),
            vec!["c".to_string(), "b".into(), "a".into()]
        );
    }

    #[test]
    fn sort_rejects_unknown_flag() {
        let mut e = new("a\nb");
        match run(&mut e, "sortz") {
            ExEffect::Error(msg) => assert!(msg.contains("z")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn range_sort_partial() {
        // `:2,4sort` sorts rows 1..=3 (1-based 2..=4) only.
        let mut e = new("z\nc\nb\na\nx");
        run(&mut e, "2,4sort");
        assert_eq!(
            e.buffer().lines(),
            vec![
                "z".to_string(),
                "a".into(),
                "b".into(),
                "c".into(),
                "x".into(),
            ]
        );
    }

    #[test]
    fn range_substitute_partial() {
        let mut e = new("foo\nfoo\nfoo\nfoo");
        // `:2,3s/foo/bar/` only replaces lines 2 and 3.
        let effect = run(&mut e, "2,3s/foo/bar/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 2,
                lines_changed: 2
            }
        );
        assert_eq!(
            e.buffer().lines(),
            vec!["foo".to_string(), "bar".into(), "bar".into(), "foo".into(),]
        );
    }

    #[test]
    fn range_delete_drops_lines() {
        let mut e = new("a\nb\nc\nd\ne");
        run(&mut e, "2,4d");
        assert_eq!(e.buffer().lines(), vec!["a".to_string(), "e".into()]);
    }

    #[test]
    fn percent_substitute_still_works() {
        let mut e = new("foo\nfoo");
        let effect = run(&mut e, "%s/foo/bar/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 2,
                lines_changed: 2
            }
        );
        assert_eq!(e.buffer().lines(), vec!["bar".to_string(), "bar".into()]);
    }

    #[test]
    fn dot_dollar_addresses_resolve() {
        let mut e = new("a\nb\nc\nd");
        e.jump_cursor(1, 0);
        // `.,$d` deletes from the current row to the bottom.
        run(&mut e, ".,$d");
        assert_eq!(e.buffer().lines(), vec!["a".to_string()]);
    }

    #[test]
    fn mark_address_resolves() {
        let mut e = new("a\nb\nc\nd\ne");
        // Set marks `a` on row 1, `b` on row 3.
        e.jump_cursor(1, 0);
        type_keys(&mut e, "ma");
        e.jump_cursor(3, 0);
        type_keys(&mut e, "mb");
        run(&mut e, "'a,'bd");
        assert_eq!(e.buffer().lines(), vec!["a".to_string(), "e".into()]);
    }

    #[test]
    fn range_global_partial() {
        let mut e = new("foo\nfoo\nbar\nfoo\nfoo");
        // Only delete `foo` lines in rows 2..=4.
        run(&mut e, "2,4g/foo/d");
        assert_eq!(
            e.buffer().lines(),
            vec!["foo".to_string(), "bar".into(), "foo".into()]
        );
    }

    #[test]
    fn bare_line_number_jumps() {
        let mut e = new("a\nb\nc\nd");
        run(&mut e, "3");
        assert_eq!(e.cursor().0, 2);
    }

    #[test]
    fn set_shiftwidth_changes_indent_step() {
        let mut e = new("hello");
        // Default: shiftwidth = 2.
        run(&mut e, "set sw=4");
        assert_eq!(e.settings().shiftwidth, 4);
        // Indent uses the new value: `>>` prepends 4 spaces now.
        type_keys(&mut e, ">>");
        assert_eq!(e.buffer().lines()[0], "    hello");
    }

    #[test]
    fn set_tabstop_stored() {
        let mut e = new("");
        run(&mut e, "set tabstop=4");
        assert_eq!(e.settings().tabstop, 4);
    }

    #[test]
    fn set_ignorecase_affects_substitute() {
        let mut e = new("Hello");
        // Plain :s/h/X/ misses on the lowercase pattern.
        let effect = run(&mut e, "s/h/X/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 0,
                lines_changed: 0
            }
        );
        run(&mut e, "set ignorecase");
        assert!(e.settings().ignore_case);
        let effect = run(&mut e, "s/h/X/");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "Xello");
    }

    #[test]
    fn set_no_prefix_disables_boolean() {
        let mut e = new("x");
        run(&mut e, "set ic");
        assert!(e.settings().ignore_case);
        run(&mut e, "set noic");
        assert!(!e.settings().ignore_case);
    }

    #[test]
    fn set_zero_shiftwidth_errors() {
        let mut e = new("x");
        match run(&mut e, "set sw=0") {
            ExEffect::Error(msg) => assert!(msg.contains("shiftwidth")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn set_unknown_option_errors() {
        let mut e = new("x");
        match run(&mut e, "set bogus") {
            ExEffect::Error(msg) => assert!(msg.contains("bogus")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn bare_set_reports_current_values() {
        // 0.1.0: `Editor::new(buf, host, Options::default())` uses
        // SPEC-faithful vim defaults (shiftwidth=8, ts=8). The pre-0.1.0
        // `Editor::new(KeybindingMode)` defaulted to `Settings::default`
        // (sqeel-derived shiftwidth=2). Asserting against the new
        // SPEC defaults.
        let mut e = new("x");
        match run(&mut e, "set") {
            ExEffect::Info(msg) => {
                assert!(msg.contains("shiftwidth=4"), "got: {msg}");
                assert!(msg.contains("ignorecase=off"), "got: {msg}");
                assert!(msg.contains("wrap=off"), "got: {msg}");
            }
            other => panic!("expected Info, got {other:?}"),
        }
    }

    #[test]
    fn set_wrap_flips_to_char_mode() {
        let mut e = new("x");
        run(&mut e, "set wrap");
        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Char);
    }

    #[test]
    fn set_nowrap_resets() {
        let mut e = new("x");
        run(&mut e, "set wrap");
        run(&mut e, "set nowrap");
        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::None);
    }

    #[test]
    fn set_linebreak_flips_to_word_mode() {
        let mut e = new("x");
        run(&mut e, "set linebreak");
        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Word);
    }

    #[test]
    fn set_wrap_after_linebreak_keeps_word_mode() {
        let mut e = new("x");
        run(&mut e, "set linebreak");
        run(&mut e, "set wrap");
        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Word);
    }

    #[test]
    fn set_nolinebreak_drops_to_char_when_wrap_on() {
        let mut e = new("x");
        run(&mut e, "set linebreak");
        run(&mut e, "set nolinebreak");
        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Char);
    }

    #[test]
    fn foldsyntax_applies_host_supplied_ranges() {
        let mut e = new("a\nb\nc\nd\ne");
        e.set_syntax_fold_ranges(vec![(0, 2), (3, 4)]);
        match run(&mut e, "foldsyntax") {
            ExEffect::Info(msg) => assert!(msg.contains("2 fold")),
            other => panic!("expected Info, got {other:?}"),
        }
        let folds = e.buffer().folds();
        assert_eq!(folds.len(), 2);
        assert!(folds.iter().any(|f| f.start_row == 0 && f.end_row == 2));
        assert!(folds.iter().any(|f| f.start_row == 3 && f.end_row == 4));
    }

    #[test]
    fn foldsyntax_no_ranges_reports_info() {
        let mut e = new("a\nb");
        match run(&mut e, "foldsyntax") {
            ExEffect::Info(msg) => assert!(msg.contains("no syntax block")),
            other => panic!("expected Info, got {other:?}"),
        }
    }

    #[test]
    fn foldsyntax_short_alias() {
        let mut e = new("a\nb\nc");
        e.set_syntax_fold_ranges(vec![(0, 2)]);
        assert!(matches!(run(&mut e, "folds"), ExEffect::Info(_)));
        assert_eq!(e.buffer().folds().len(), 1);
    }

    #[test]
    fn foldindent_creates_fold_for_indented_block() {
        let mut e = new("SELECT *\n  FROM t\n  WHERE x = 1\nORDER BY id");
        match run(&mut e, "foldindent") {
            ExEffect::Info(msg) => assert!(msg.contains("1 fold")),
            other => panic!("expected Info, got {other:?}"),
        }
        let folds = e.buffer().folds();
        assert_eq!(folds.len(), 1);
        assert_eq!(folds[0].start_row, 0);
        assert_eq!(folds[0].end_row, 2);
        assert!(folds[0].closed);
    }

    #[test]
    fn foldindent_no_blocks_reports_info() {
        let mut e = new("a\nb\nc");
        match run(&mut e, "foldindent") {
            ExEffect::Info(msg) => assert!(msg.contains("no indented blocks")),
            other => panic!("expected Info, got {other:?}"),
        }
        assert!(e.buffer().folds().is_empty());
    }

    #[test]
    fn foldindent_handles_nested_blocks() {
        let mut e = new("outer\n  mid\n    inner1\n    inner2\n  back\noutmost");
        run(&mut e, "foldindent");
        let folds = e.buffer().folds();
        // Outer block 0..=4 + inner block 1..=3 (mid → inner runs).
        assert_eq!(folds.len(), 2);
        assert_eq!(folds[0].start_row, 0);
        assert_eq!(folds[0].end_row, 4);
        assert_eq!(folds[1].start_row, 1);
        assert_eq!(folds[1].end_row, 3);
    }

    #[test]
    fn foldindent_skips_blanks_inside_block() {
        let mut e = new("head\n  body1\n\n  body2\nfoot");
        run(&mut e, "foldindent");
        let folds = e.buffer().folds();
        assert_eq!(folds.len(), 1);
        assert_eq!(folds[0].start_row, 0);
        assert_eq!(folds[0].end_row, 3);
    }

    #[test]
    fn foldindent_short_alias() {
        let mut e = new("a\n  b\nc");
        assert!(matches!(run(&mut e, "foldi"), ExEffect::Info(_)));
        assert_eq!(e.buffer().folds().len(), 1);
    }

    #[test]
    fn read_file_inserts_below_current_row() {
        // Write a temp file with two rows.
        let dir = std::env::temp_dir();
        let path = dir.join(format!("hjkl_read_{}.sql", std::process::id()));
        std::fs::write(&path, "SELECT 1;\nSELECT 2;\n").unwrap();
        let mut e = new("alpha\nbeta");
        e.jump_cursor(0, 0);
        let cmd = format!("r {}", path.display());
        assert_eq!(run(&mut e, &cmd), ExEffect::Ok);
        assert_eq!(
            e.buffer().lines(),
            vec![
                "alpha".to_string(),
                "SELECT 1;".into(),
                "SELECT 2;".into(),
                "beta".into(),
            ]
        );
        // Cursor sits on the first inserted row.
        assert_eq!(e.cursor(), (1, 0));
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn shell_filter_replaces_range() {
        let mut e = new("c\nb\na");
        // `:%!sort` reorders the whole buffer alphabetically.
        assert_eq!(run(&mut e, "%!sort"), ExEffect::Ok);
        assert_eq!(
            e.buffer().lines(),
            vec!["a".to_string(), "b".into(), "c".into()]
        );
    }

    #[test]
    fn shell_filter_partial_range() {
        let mut e = new("head\ngamma\nbeta\nalpha\ntail");
        // `:2,4!sort` should reorder rows 2..=4 only.
        run(&mut e, "2,4!sort");
        assert_eq!(
            e.buffer().lines(),
            vec![
                "head".to_string(),
                "alpha".into(),
                "beta".into(),
                "gamma".into(),
                "tail".into(),
            ]
        );
    }

    #[test]
    fn shell_filter_undo_restores() {
        let mut e = new("c\nb\na");
        let before: Vec<String> = e.buffer().lines().to_vec();
        run(&mut e, "%!sort");
        e.undo();
        assert_eq!(e.buffer().lines(), before);
    }

    #[test]
    fn shell_command_no_range_returns_info() {
        let mut e = new("buffer stays put");
        match run(&mut e, "!echo from-shell") {
            ExEffect::Info(msg) => assert!(msg.contains("from-shell")),
            other => panic!("expected Info, got {other:?}"),
        }
        // Buffer unchanged.
        assert_eq!(e.buffer().lines()[0], "buffer stays put");
    }

    #[test]
    fn shell_filter_failing_command_errors() {
        let mut e = new("a\nb");
        match run(&mut e, "%!exit 5") {
            ExEffect::Error(msg) => assert!(msg.contains("exited 5"), "msg was: {msg}"),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn shell_bang_empty_command_errors() {
        let mut e = new("a");
        match run(&mut e, "!") {
            ExEffect::Error(msg) => assert!(msg.contains("shell command")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn read_bang_inserts_command_stdout() {
        let mut e = new("alpha\nbeta");
        e.jump_cursor(0, 0);
        // `echo` is portable — outputs a trailing newline that
        // apply_read_file strips.
        assert_eq!(run(&mut e, "r !echo hello"), ExEffect::Ok);
        assert_eq!(
            e.buffer().lines(),
            vec!["alpha".to_string(), "hello".into(), "beta".into()]
        );
    }

    #[test]
    fn read_bang_failing_command_errors() {
        let mut e = new("hi");
        match run(&mut e, "r !exit 7") {
            ExEffect::Error(msg) => assert!(msg.contains("exited 7")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn read_bang_empty_command_errors() {
        let mut e = new("hi");
        match run(&mut e, "r !") {
            ExEffect::Error(msg) => assert!(msg.contains("shell command")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn read_file_alias_read_works() {
        let dir = std::env::temp_dir();
        let path = dir.join(format!("hjkl_read_alias_{}.sql", std::process::id()));
        std::fs::write(&path, "x").unwrap();
        let mut e = new("");
        let cmd = format!("read {}", path.display());
        run(&mut e, &cmd);
        assert_eq!(e.buffer().lines(), vec!["".to_string(), "x".into()]);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn read_file_missing_path_errors() {
        let mut e = new("a");
        match run(&mut e, "r /nonexistent/path/sqeel_test_xyzzy") {
            ExEffect::Error(msg) => assert!(msg.contains("cannot read")),
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn read_file_undo_restores() {
        let dir = std::env::temp_dir();
        let path = dir.join(format!("hjkl_read_undo_{}.sql", std::process::id()));
        std::fs::write(&path, "ins\n").unwrap();
        let mut e = new("a\nb");
        e.jump_cursor(0, 0);
        run(&mut e, &format!("r {}", path.display()));
        assert_eq!(e.buffer().lines().len(), 3);
        e.undo();
        assert_eq!(e.buffer().lines(), vec!["a".to_string(), "b".into()]);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn unknown_command() {
        let mut e = new("");
        match run(&mut e, "blargh") {
            ExEffect::Unknown(cmd) => assert_eq!(cmd, "blargh"),
            other => panic!("expected Unknown, got {other:?}"),
        }
    }

    #[test]
    fn bad_substitute_pattern() {
        let mut e = new("hi");
        match run(&mut e, "s/[unterminated/foo/") {
            ExEffect::Error(_) => {}
            other => panic!("expected Error, got {other:?}"),
        }
    }

    #[test]
    fn substitute_escaped_separator() {
        let mut e = new("a/b/c");
        let effect = run(&mut e, "s/\\//-/g");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 2,
                lines_changed: 1
            }
        );
        assert_eq!(e.buffer().lines()[0], "a-b-c");
    }

    #[test]
    fn global_delete_drops_matching_rows() {
        let mut e = new("keep1\nDROP1\nkeep2\nDROP2\nkeep3");
        let effect = run(&mut e, "g/DROP/d");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 2,
                lines_changed: 2
            }
        );
        assert_eq!(
            e.buffer().lines(),
            &[
                "keep1".to_string(),
                "keep2".to_string(),
                "keep3".to_string()
            ]
        );
    }

    #[test]
    fn global_negated_drops_non_matching_rows() {
        let mut e = new("keep1\nother\nkeep2");
        let effect = run(&mut e, "v/keep/d");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 1,
                lines_changed: 1
            }
        );
        assert_eq!(
            e.buffer().lines(),
            &["keep1".to_string(), "keep2".to_string()]
        );
    }

    #[test]
    fn global_with_regex_pattern() {
        let mut e = new("foo bar\nbaz qux\nfoo baz\nbaz");
        // Drop lines starting with "foo".
        let effect = run(&mut e, r"g/^foo/d");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 2,
                lines_changed: 2
            }
        );
        assert_eq!(
            e.buffer().lines(),
            &["baz qux".to_string(), "baz".to_string()]
        );
    }

    #[test]
    fn global_no_matches_reports_zero() {
        let mut e = new("hello\nworld");
        let effect = run(&mut e, "g/xyz/d");
        assert_eq!(
            effect,
            ExEffect::Substituted {
                count: 0,
                lines_changed: 0
            }
        );
        assert_eq!(e.buffer().lines().len(), 2);
    }

    #[test]
    fn global_unsupported_command_errors_out() {
        let mut e = new("foo\nbar");
        let effect = run(&mut e, "g/foo/p");
        assert!(matches!(effect, ExEffect::Error(_)));
    }

    #[test]
    fn format_jumps_empty() {
        let e = new("hello");
        let info = match run(&mut { e }, "jumps") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert_eq!(info, "(no jumps recorded)");
    }

    #[test]
    fn format_jumps_with_entries() {
        let mut e = new("line1\nline2\nline3\nline4\nline5");
        // `gg` and `G` are "big jump" motions that push the jumplist.
        type_keys(&mut e, "gg");
        type_keys(&mut e, "G");
        type_keys(&mut e, "gg");
        let info = match run(&mut e, "jumps") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(info.contains("--- Jump list ---"), "header missing: {info}");
        assert!(info.contains("jump"), "column header missing: {info}");
    }

    #[test]
    fn format_changes_empty() {
        let e = new("hello");
        let info = match run(&mut { e }, "changes") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert_eq!(info, "(no changes recorded)");
    }

    #[test]
    fn format_changes_with_edits() {
        let mut e = new("hello\nworld");
        // Insert something to populate the change list.
        type_keys(&mut e, "Afoo\x1b");
        type_keys(&mut e, "jAbar\x1b");
        let info = match run(&mut e, "changes") {
            ExEffect::Info(s) => s,
            other => panic!("expected Info, got {other:?}"),
        };
        assert!(
            info.contains("--- Change list ---"),
            "header missing: {info}"
        );
        assert!(info.contains("change"), "column header missing: {info}");
    }

    #[test]
    fn slash_pat_search_address_jumps_forward() {
        // `:/foo` is a search-as-address with no following command —
        // vim moves the cursor to the next forward match.
        let mut e = new("alpha\nbeta\nfoo here\ndelta\nfoo also");
        // cursor starts at (0, 0); first forward match is row 2.
        let effect = run(&mut e, "/foo");
        assert_eq!(effect, ExEffect::Ok);
        assert_eq!(e.cursor().0, 2);
        // Trailing delimiter is optional but accepted.
        e.goto_line(1);
        let effect = run(&mut e, "/foo/");
        assert_eq!(effect, ExEffect::Ok);
        assert_eq!(e.cursor().0, 2);
    }

    #[test]
    fn question_pat_search_address_jumps_backward() {
        // `:?foo` searches strictly backward from the cursor.
        let mut e = new("foo first\nbeta\nfoo middle\ndelta\nlast");
        e.goto_line(5); // cursor at row 4
        let effect = run(&mut e, "?foo");
        assert_eq!(effect, ExEffect::Ok);
        assert_eq!(
            e.cursor().0,
            2,
            "?foo from row 4 must land on previous match (row 2)"
        );
        // Trailing matching delimiter accepted.
        e.goto_line(5);
        let effect = run(&mut e, "?foo?");
        assert_eq!(effect, ExEffect::Ok);
        assert_eq!(e.cursor().0, 2);
    }

    #[test]
    fn search_address_persists_direction_for_n() {
        // After `:?foo`, `last_search_forward` must be false so a
        // subsequent `n` keeps moving backward.
        let mut e = new("foo a\nbeta\nfoo b\ndelta\nfoo c");
        e.goto_line(5); // cursor row 4
        run(&mut e, "?foo");
        assert_eq!(e.last_search(), Some("foo"));
        assert!(!e.last_search_forward(), "?foo must persist backward dir");

        // And `:/foo` flips it back to forward.
        run(&mut e, "/foo");
        assert!(e.last_search_forward(), "/foo must persist forward dir");
    }

    #[test]
    fn empty_search_address_reuses_last_pattern() {
        let mut e = new("alpha\nfoo a\nbar\nfoo b");
        // Seed last_search by jumping to row 1 with `/foo`.
        run(&mut e, "/foo");
        assert_eq!(e.cursor().0, 1);
        // `:/` with empty pattern repeats — should advance past row 1
        // to the next match at row 3.
        e.search_advance_forward(true); // step off the current match
        e.goto_line(1); // back to row 0
        let effect = run(&mut e, "/");
        assert_eq!(effect, ExEffect::Ok);
        assert_eq!(e.cursor().0, 1, "/ alone should re-find first match");
    }

    #[test]
    fn empty_search_address_with_no_history_errors() {
        let mut e = new("foo");
        let effect = run(&mut e, "?");
        assert!(matches!(effect, ExEffect::Error(_)));
    }
}