edamame 0.1.2

A fast TUI Markdown editor and viewer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
//! Integration tests for the vim modal-editing reducer.
//!
//! Following the project's `mouse_ops` testing convention, these treat
//! `vim_feed` as a pure function of `(VimState, EditorState, key)` and
//! assert on the resulting state — no terminal, no `App`.  CP1 covers
//! the walking skeleton: `h j k l` motion, `i a I A` Insert entries,
//! `Esc` transitions, count accumulation, and the Normal/Insert
//! passthrough contract.  CP2 adds the core motions (`w e b W E B 0 ^ $
//! gg G`), the `o`/`O` open-line entries, and `v`/`V` Visual entry.  CP3
//! adds the operator+motion reducer; CP4 adds the remaining Normal
//! primitives (`r{c} ~ >> << J u`, and `Ctrl-R` redo via the keymap).  CP6
//! adds the Visual / VisualLine operators (`d/x y c/s > < ~ J`, `o`
//! swap-ends, `v`/`V` toggle) and the line-expansion of a VisualLine
//! selection for operators and the system clipboard.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use edamame::config::{Action, KeyBindingOverrides, KeyMap, Theme};
use edamame::document::{Buffer, Selection};
use edamame::editor::vim_ops::visual_charwise_range;
use edamame::editor::{EditorState, Mode};
use edamame::input::{vim_feed, VimOutcome, VimState, VimSubMode};
use edamame::search::SearchState;

const VH: usize = 40;
const VW: usize = 80;

fn theme() -> &'static Theme {
    Box::leak(Box::new(Theme::default()))
}

/// A fresh editor in Rendered mode (vim never rests in Preview).
fn state(text: &str) -> EditorState {
    let mut st = EditorState::new(Buffer::from_str(text), theme());
    st.mode = Mode::Rendered;
    st.update_cursor_block();
    st
}

/// Like [`state`] but with an associated file path, so `:w` / `:wq`
/// resolve to a direct save rather than the Save As prompt a never-saved
/// (path-less) buffer triggers.
fn state_with_path(text: &str) -> EditorState {
    let mut buf = Buffer::for_new_file(std::path::Path::new("/tmp/edamame-vim-test.md"));
    buf.insert(0, text);
    let mut st = EditorState::new(buf, theme());
    st.mode = Mode::Rendered;
    st.update_cursor_block();
    st
}

fn ch(c: char) -> KeyEvent {
    KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)
}

fn esc() -> KeyEvent {
    KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)
}

fn key(code: KeyCode) -> KeyEvent {
    KeyEvent::new(code, KeyModifiers::NONE)
}

fn ctrl(c: char) -> KeyEvent {
    KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL)
}

fn feed(vim: &mut VimState, st: &mut EditorState, key: KeyEvent) -> VimOutcome {
    vim_feed(vim, st, key, VH, VW)
}

// ── Motion ────────────────────────────────────────────────────────────────────

#[test]
fn hjkl_move_the_cursor() {
    let mut st = state("hello\nworld");
    let mut vim = VimState::default();
    assert_eq!(st.cursor.offset, 0);

    assert_eq!(feed(&mut vim, &mut st, ch('l')), VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, 1);

    feed(&mut vim, &mut st, ch('j'));
    assert_eq!(st.cursor.offset, 7); // line 1, col 1

    feed(&mut vim, &mut st, ch('h'));
    assert_eq!(st.cursor.offset, 6);

    feed(&mut vim, &mut st, ch('k'));
    assert_eq!(st.cursor.offset, 0); // back to line 0, col 0
}

#[test]
fn bare_key_in_normal_is_consumed_without_inserting() {
    let mut st = state("ab");
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, ch('z'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "ab", "normal-mode key must not type");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn ctrl_chord_passes_through_in_normal() {
    let mut st = state("hi");
    let mut vim = VimState::default();
    let k = KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL);
    assert_eq!(feed(&mut vim, &mut st, k), VimOutcome::Passthrough);
}

#[test]
fn ctrl_backspace_and_delete_do_not_edit_in_normal() {
    // The default keymap binds Ctrl-Backspace / Ctrl-Delete to word-delete.
    // In Normal they must never mutate the buffer — they move the cursor
    // (left / right) like the plain Backspace / Delete keys instead.
    let ctrl_bs = KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL);
    let ctrl_del = KeyEvent::new(KeyCode::Delete, KeyModifiers::CONTROL);

    let mut st = state("foo bar");
    let mut vim = VimState::default();
    st.cursor.offset = 4; // start of "bar"

    assert_eq!(feed(&mut vim, &mut st, ctrl_bs), VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "foo bar",
        "Ctrl-Backspace must not edit"
    );
    assert_eq!(st.cursor.offset, 3, "Ctrl-Backspace moves left");

    assert_eq!(feed(&mut vim, &mut st, ctrl_del), VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "foo bar", "Ctrl-Delete must not edit");
    assert_eq!(st.cursor.offset, 4, "Ctrl-Delete moves right");
}

#[test]
fn ctrl_backspace_and_delete_do_not_edit_in_visual() {
    // Same guard in Visual: the chords extend the selection rather than
    // editing through it.
    let ctrl_bs = KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL);
    let ctrl_del = KeyEvent::new(KeyCode::Delete, KeyModifiers::CONTROL);

    let mut st = state("foo bar");
    let mut vim = VimState::default();
    st.cursor.offset = 4;
    feed(&mut vim, &mut st, ch('v')); // enter Visual, anchor at 4

    assert_eq!(feed(&mut vim, &mut st, ctrl_bs), VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "foo bar",
        "Ctrl-Backspace must not edit"
    );
    assert!(st.selection.is_some(), "selection is extended, not dropped");

    assert_eq!(feed(&mut vim, &mut st, ctrl_del), VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "foo bar", "Ctrl-Delete must not edit");
}

#[test]
fn motion_clears_a_lingering_selection() {
    // A mouse drag can leave a selection active; a Normal-mode motion
    // must drop it (otherwise it keeps painting under the cursor).
    let mut st = state("hello\nworld");
    let mut vim = VimState::default();
    st.selection = Some(Selection {
        anchor: 0,
        active: 3,
    });
    feed(&mut vim, &mut st, ch('l'));
    assert!(st.selection.is_none(), "motion must clear the selection");
}

#[test]
fn esc_clears_a_lingering_selection_in_normal() {
    // A mouse drag can leave a selection active while in Normal; `Esc`
    // must drop both the buffer and its paint (mirroring `Esc` in Visual).
    let mut st = state("hello\nworld");
    let mut vim = VimState::default();
    st.selection = Some(Selection {
        anchor: 0,
        active: 3,
    });
    assert_eq!(feed(&mut vim, &mut st, esc()), VimOutcome::Consumed);
    assert!(st.selection.is_none(), "Esc must clear the selection");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn horizontal_motion_skips_table_border_chrome_in_rendered_mode() {
    // `l` through a table steps cell-to-cell over the auto-managed border
    // chrome — the cursor must never land on a `|`.
    let src = "| a | b |\n|---|---|\n| 11 | 22 |\n";
    let mut st = state(src);
    let mut vim = VimState::default();
    st.cursor.offset = 2; // on 'a' in the header row
    st.update_cursor_block();

    for _ in 0..4 {
        feed(&mut vim, &mut st, ch('l'));
        let off = st
            .cursor
            .offset
            .min(st.buffer.len_chars().saturating_sub(1));
        assert_ne!(
            st.buffer.rope().char(off),
            '|',
            "rendered-mode horizontal motion must skip the table border chrome"
        );
    }
}

#[test]
fn horizontal_motion_traverses_borders_in_raw_mode() {
    // In Raw mode the borders are real, editable source, so `l` walks onto
    // the `|` character rather than skipping it.
    let src = "| a | b |\n|---|---|\n| 11 | 22 |\n";
    let mut st = state(src);
    st.mode = Mode::Raw;
    st.cursor.offset = 2; // on 'a'
    st.update_cursor_block();
    let mut vim = VimState::default();

    feed(&mut vim, &mut st, ch('l')); // -> ' '
    feed(&mut vim, &mut st, ch('l')); // -> '|'
    assert_eq!(
        st.buffer.rope().char(st.cursor.offset),
        '|',
        "raw-mode horizontal motion steps onto the border character"
    );
}

#[test]
fn j_skips_the_table_alignment_row_in_rendered_mode() {
    // `j` from the header row must skip the structural `|---|---|` row and
    // land on the first data row — the same rule the default handler's
    // MoveDown follows in a rendered view.
    let src = "| a | b |\n|---|---|\n| 11 | 22 |\n";
    let mut st = state(src); // cursor at offset 0, on the header row
    let mut vim = VimState::default();

    feed(&mut vim, &mut st, ch('j'));

    let (line, _) = st.cursor.line_col(&st.buffer);
    let line_text = st.buffer.line(line).unwrap_or_default();
    assert!(
        line_text.contains("11"),
        "expected cursor on the data row, got line {line}: {line_text:?}"
    );
}

#[test]
fn j_does_not_skip_the_alignment_row_in_raw_mode() {
    // In Raw mode every line is genuine, editable source, so `j` lands on
    // the alignment row rather than skipping it.
    let src = "| a | b |\n|---|---|\n| 11 | 22 |\n";
    let mut st = state(src);
    st.mode = Mode::Raw;
    st.update_cursor_block();
    let mut vim = VimState::default();

    feed(&mut vim, &mut st, ch('j'));

    let (line, _) = st.cursor.line_col(&st.buffer);
    let line_text = st.buffer.line(line).unwrap_or_default();
    assert!(
        line_text.contains("---"),
        "expected cursor on the alignment row, got line {line}: {line_text:?}"
    );
}

// ── Insert entries ──────────────────────────────────────────────────────────

#[test]
fn i_enters_insert_and_chars_pass_through() {
    let mut st = state("ab");
    let mut vim = VimState::default();

    assert_eq!(feed(&mut vim, &mut st, ch('i')), VimOutcome::Consumed);
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 0, "`i` inserts before the cursor");

    // In Insert, printable chars fall through to the editing pipeline —
    // vim_feed does not type them itself.
    assert_eq!(feed(&mut vim, &mut st, ch('x')), VimOutcome::Passthrough);
    assert_eq!(st.buffer.contents(), "ab");
}

#[test]
fn esc_from_insert_returns_to_normal_and_moves_left() {
    let mut st = state("ab");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('i'));
    st.cursor.offset = 1; // simulate having typed one char

    assert_eq!(feed(&mut vim, &mut st, esc()), VimOutcome::Consumed);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(st.cursor.offset, 0, "Esc moves one char left");
}

#[test]
fn esc_from_insert_at_line_start_does_not_cross_lines() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('i'));
    st.cursor.offset = 3; // start of line 1 ('c')

    feed(&mut vim, &mut st, esc());
    assert_eq!(
        st.cursor.offset, 3,
        "Esc never crosses to the previous line"
    );
}

#[test]
fn a_appends_after_the_cursor() {
    let mut st = state("ab");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('a'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 1);
}

#[test]
fn a_at_end_of_line_does_not_cross_the_newline() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // end of "ab", on the newline
    feed(&mut vim, &mut st, ch('a'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(
        st.cursor.offset, 2,
        "stays before the newline, not on line 2"
    );
}

#[test]
fn a_on_the_last_char_appends_before_the_newline() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    st.cursor.offset = 1; // on 'b', the last char of line 1
    feed(&mut vim, &mut st, ch('a'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(
        st.cursor.offset, 2,
        "insertion point after 'b', before the newline"
    );
}

#[test]
fn capital_i_moves_to_first_non_blank() {
    let mut st = state("   foo");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // inside "foo"
    feed(&mut vim, &mut st, ch('I'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 3, "first non-blank is 'f'");
}

#[test]
fn shifted_capital_letter_is_handled_like_a_bare_uppercase_char() {
    // Real terminals deliver `I` as `Char('I')` with the SHIFT modifier.
    // The reducer's Ctrl/Alt/Super guard must not swallow SHIFT, so `I`
    // still routes to the first-non-blank Insert entry.
    let mut st = state("   foo");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // inside "foo"
    let shifted_i = KeyEvent::new(KeyCode::Char('I'), KeyModifiers::SHIFT);
    assert_eq!(feed(&mut vim, &mut st, shifted_i), VimOutcome::Consumed);
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 3, "first non-blank is 'f'");
}

#[test]
fn capital_a_moves_to_line_end() {
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('A'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 3, "end of \"abc\", before the newline");
}

// ── Counts ────────────────────────────────────────────────────────────────────

#[test]
fn digits_accumulate_count_and_esc_clears_it() {
    let mut st = state("hello");
    let mut vim = VimState::default();

    assert_eq!(feed(&mut vim, &mut st, ch('3')), VimOutcome::Pending);
    assert_eq!(vim.count, Some(3));
    assert_eq!(feed(&mut vim, &mut st, ch('2')), VimOutcome::Pending);
    assert_eq!(vim.count, Some(32));

    feed(&mut vim, &mut st, esc());
    assert_eq!(vim.count, None, "Esc clears the in-progress count");
}

#[test]
fn leading_zero_is_not_a_count_digit() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    // A leading `0` is the line-start motion (consumed, no count), not a
    // digit — so it must not start a count.
    let out = feed(&mut vim, &mut st, ch('0'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(vim.count, None);
}

#[test]
fn zero_after_a_digit_extends_the_count() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('1'));
    assert_eq!(feed(&mut vim, &mut st, ch('0')), VimOutcome::Pending);
    assert_eq!(vim.count, Some(10));
}

// ── Core motions (CP2) ──────────────────────────────────────────────────────

#[test]
fn word_motions_land_on_boundaries() {
    let mut st = state("foo.bar baz");
    let mut vim = VimState::default();

    // w: 'f' → '.' → 'bar' → 'baz'.
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.cursor.offset, 3); // '.'
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.cursor.offset, 4); // 'b' of bar
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.cursor.offset, 8); // 'b' of baz

    // b walks back to word starts.
    feed(&mut vim, &mut st, ch('b'));
    assert_eq!(st.cursor.offset, 4);

    // e lands on the last char of the next word.
    st.cursor.offset = 0;
    feed(&mut vim, &mut st, ch('e'));
    assert_eq!(st.cursor.offset, 2); // 'o' of foo
}

#[test]
fn big_word_motions_ignore_punctuation() {
    let mut st = state("foo.bar baz");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('W'));
    assert_eq!(st.cursor.offset, 8, "W jumps the whole foo.bar blob");

    st.cursor.offset = 10; // 'z'
    feed(&mut vim, &mut st, ch('B'));
    assert_eq!(st.cursor.offset, 8, "B back to start of baz");

    st.cursor.offset = 0;
    feed(&mut vim, &mut st, ch('E'));
    assert_eq!(st.cursor.offset, 6, "E to the end of foo.bar");
}

#[test]
fn line_motions_zero_caret_dollar() {
    let mut st = state("  hello world\nnext");
    let mut vim = VimState::default();
    st.cursor.offset = 6; // inside "hello"

    feed(&mut vim, &mut st, ch('0'));
    assert_eq!(st.cursor.offset, 0, "0 → line start");

    feed(&mut vim, &mut st, ch('^'));
    assert_eq!(st.cursor.offset, 2, "^ → first non-blank");

    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.cursor.offset, 13, "$ → end of line before the newline");
}

#[test]
fn gg_and_capital_g_jump_document_ends() {
    let mut st = state("  first\nmiddle\nlast");
    let mut vim = VimState::default();
    st.cursor.offset = 10; // inside "middle"

    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.cursor.offset, 15, "G → first non-blank of last line");

    // gg is a two-key sequence: the first g is Pending.
    assert_eq!(feed(&mut vim, &mut st, ch('g')), VimOutcome::Pending);
    assert!(vim.pending_g);
    assert_eq!(feed(&mut vim, &mut st, ch('g')), VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, 2, "gg → first non-blank of first line");
    assert!(!vim.pending_g);
}

/// The four-line fixture the line-jump tests share.  Line 3 carries leading
/// blanks so the landing spot proves "first non-blank", not "line start".
const LINES: &str = "alpha\nbravo\n  charlie\ndelta";

#[test]
fn count_g_jumps_to_that_line() {
    let mut st = state(LINES);
    let mut vim = VimState::default();

    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(
        st.cursor.offset,
        LINES.find("charlie").unwrap(),
        "3G → first non-blank of line 3"
    );
    assert!(vim.count.is_none(), "the count is consumed");

    // `1G` is the first line — the case a repeat-count reading gets wrong.
    feed(&mut vim, &mut st, ch('1'));
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn bare_g_still_jumps_to_the_last_line() {
    // Guards the `count_of`-defaults-to-1 trap: an absent count must not
    // read as `1G`.
    let mut st = state(LINES);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.cursor.offset, LINES.find("delta").unwrap());
}

#[test]
fn count_gg_jumps_to_that_line() {
    let mut st = state(LINES);
    let mut vim = VimState::default();
    st.cursor.offset = LINES.len();

    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('g'));
    feed(&mut vim, &mut st, ch('g'));
    assert_eq!(st.cursor.offset, LINES.find("charlie").unwrap());
}

#[test]
fn count_g_clamps_past_the_end_of_the_document() {
    let mut st = state(LINES);
    let mut vim = VimState::default();
    for c in "999".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.cursor.offset, LINES.find("delta").unwrap());
}

/// A count is a *line number* here, not a repetition, so it is not subject
/// to `COUNT_CAP` — a document longer than the cap must stay reachable from
/// the keyboard, the way `:10001` already is.
#[test]
fn count_g_reaches_a_line_past_the_repeat_cap() {
    let text: String = (1..=10_005).map(|i| format!("line {i}\n")).collect();
    let mut st = state(&text);
    let mut vim = VimState::default();

    for c in "10001".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.buffer.char_to_line(st.cursor.offset), 10_000, "0-based");

    // Saturating past `u32` still lands on the last content line, like `:$`.
    for c in "99999999999".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.buffer.char_to_line(st.cursor.offset), 10_004);
}

/// The mirror of the above: lifting the cap off the accumulator must not
/// lift it off the consumers that turn a count into work.  `p` is the
/// sharpest — an uncapped `99999999p` would allocate ~100 MB.
#[test]
fn a_huge_count_is_still_capped_for_repetition() {
    let mut st = state("ab");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('l')); // yl → "a"
    assert_eq!(vim.register.text, "a");

    for c in "99999999".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    feed(&mut vim, &mut st, ch('p'));
    // COUNT_CAP (9999) copies inserted after the cursor, not 99 999 999.
    assert_eq!(st.buffer.len_chars(), 2 + 9_999);
}

#[test]
fn d_count_g_deletes_the_linewise_span() {
    // Downward: from line 1, `d2G` takes lines 1–2.
    let mut st = state(LINES);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.buffer.contents(), "  charlie\ndelta");

    // Upward: from line 3, `d1G` takes lines 1–3.
    let mut st = state(LINES);
    let mut vim = VimState::default();
    st.cursor.offset = LINES.find("charlie").unwrap();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('1'));
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.buffer.contents(), "delta");
}

#[test]
fn count_g_extends_a_visual_selection() {
    let mut st = state(LINES);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('G'));
    let sel = st.selection.as_ref().expect("visual selection live");
    assert_eq!(sel.anchor, 0);
    assert_eq!(sel.active, LINES.find("charlie").unwrap());
}

#[test]
fn lone_g_followed_by_other_key_is_a_noop() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    st.cursor.offset = 3;
    feed(&mut vim, &mut st, ch('g'));
    let out = feed(&mut vim, &mut st, ch('x'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, 3, "unknown g-command does not move");
    assert!(!vim.pending_g);
}

#[test]
fn pending_g_is_resolved_before_a_count_digit() {
    // A pending `g` must be consumed by the very next key, even a digit, so
    // it can never linger while the digit grows the count (which would let a
    // later lone `g` fire `gg`).
    let mut st = state("hello");
    let mut vim = VimState::default();
    st.cursor.offset = 3;

    feed(&mut vim, &mut st, ch('g'));
    assert!(vim.pending_g);
    let out = feed(&mut vim, &mut st, ch('5'));
    assert_eq!(
        out,
        VimOutcome::Consumed,
        "g then digit is a swallowed no-op"
    );
    assert!(!vim.pending_g, "pending_g must not dangle past the digit");
    assert_eq!(vim.count, None, "the digit does not accumulate a count");
    assert_eq!(st.cursor.offset, 3, "no motion fires");
}

// ── o / O (CP2) ───────────────────────────────────────────────────────────────

#[test]
fn o_opens_a_line_below_and_enters_insert() {
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    st.cursor.offset = 1; // inside "abc"

    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "abc\n\ndef");
    assert_eq!(st.cursor.offset, 4, "cursor on the new blank line below");
}

#[test]
fn capital_o_opens_a_line_above_and_enters_insert() {
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    let line1 = st.buffer.line_to_char(1);
    st.cursor.offset = line1 + 1; // inside "def"

    feed(&mut vim, &mut st, ch('O'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "abc\n\ndef");
    assert_eq!(
        st.cursor.offset, line1,
        "cursor on the new blank line above"
    );
}

#[test]
fn o_on_the_last_line_appends_a_new_line() {
    let mut st = state("only");
    let mut vim = VimState::default();
    st.cursor.offset = 2;

    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(st.buffer.contents(), "only\n");
    assert_eq!(
        st.cursor.offset, 5,
        "cursor on the freshly-opened last line"
    );
}

#[test]
fn open_line_is_a_single_undo_unit() {
    // `o`/`O` must record exactly one EditDelta so a later `u` reverses
    // the whole open-line in one step (Risk #2 in the plan).
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    st.cursor.offset = 1; // inside "abc"

    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(st.buffer.contents(), "abc\n\ndef");
    assert_eq!(st.history.undo_depth(), 1, "o records exactly one delta");

    // A single undo restores the original buffer in one step.
    assert!(st.history.undo(&mut st.buffer).is_some());
    assert_eq!(st.buffer.contents(), "abc\ndef");

    // `O` is likewise one unit.
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    let line1 = st.buffer.line_to_char(1);
    st.cursor.offset = line1 + 1; // inside "def"

    feed(&mut vim, &mut st, ch('O'));
    assert_eq!(st.buffer.contents(), "abc\n\ndef");
    assert_eq!(st.history.undo_depth(), 1, "O records exactly one delta");

    assert!(st.history.undo(&mut st.buffer).is_some());
    assert_eq!(st.buffer.contents(), "abc\ndef");
}

// ── Visual entry (CP2) ─────────────────────────────────────────────────────────

#[test]
fn v_enters_visual_and_anchors_the_selection() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    st.cursor.offset = 1;

    assert_eq!(feed(&mut vim, &mut st, ch('v')), VimOutcome::Consumed);
    assert_eq!(vim.sub_mode, VimSubMode::Visual);
    assert_eq!(vim.visual_anchor, Some(1));
    let sel = st.selection.expect("v installs a selection");
    assert_eq!((sel.anchor, sel.active), (1, 1));
}

#[test]
fn capital_v_enters_visual_line() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    assert_eq!(vim.sub_mode, VimSubMode::VisualLine);
    assert!(st.selection.is_some());
}

#[test]
fn motions_extend_the_selection_in_visual() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // anchor at 0
    feed(&mut vim, &mut st, ch('w')); // → start of "world" (6)

    let sel = st.selection.expect("selection persists through motion");
    assert_eq!(sel.anchor, 0, "anchor stays put");
    assert_eq!(sel.active, 6, "active follows the cursor");
    assert_eq!(st.cursor.offset, 6);
}

#[test]
fn esc_leaves_visual_and_clears_the_selection() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    assert!(st.selection.is_some());

    feed(&mut vim, &mut st, esc());
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none(), "Esc drops the visual selection");
    assert_eq!(vim.visual_anchor, None);
}

#[test]
fn hjkl_extend_the_selection_in_visual() {
    let mut st = state("hello\nworld");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // anchor at 0
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    let sel = st.selection.expect("selection persists");
    assert_eq!((sel.anchor, sel.active), (0, 2));
}

#[test]
fn arrow_keys_extend_the_selection_in_visual_like_hjkl() {
    // Arrow keys must behave exactly like `h j k l` in Visual — extend the
    // selection rather than passing through (which would clear it).
    let mut st = state("hello\nworld");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // anchor at 0

    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Right)),
        VimOutcome::Consumed,
        "Right must be consumed in Visual, not passed through"
    );
    feed(&mut vim, &mut st, key(KeyCode::Right));
    let sel = st
        .selection
        .expect("selection persists through arrow motion");
    assert_eq!((sel.anchor, sel.active), (0, 2));

    feed(&mut vim, &mut st, key(KeyCode::Down));
    let sel = st.selection.expect("selection persists across a line");
    assert_eq!(sel.anchor, 0);
    assert_eq!(sel.active, st.cursor.offset);
}

#[test]
fn arrow_keys_in_normal_pass_through() {
    // Outside Visual, arrows keep their default-handler bindings.
    let mut st = state("hello");
    let mut vim = VimState::default();
    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Right)),
        VimOutcome::Passthrough,
        "Normal-mode arrows fall through to the default keymap"
    );
}

// ── Counts drive motions (CP3) ──────────────────────────────────────────────

#[test]
fn count_repeats_a_vertical_motion() {
    let mut st = state("l0\nl1\nl2\nl3\nl4");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('j'));
    let (line, _) = st.cursor.line_col(&st.buffer);
    assert_eq!(line, 3, "3j moves down three lines");
    assert_eq!(vim.count, None, "count clears after the motion");
}

#[test]
fn count_repeats_a_word_motion() {
    let mut st = state("aa bb cc dd ee");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.cursor.offset, 9, "3w lands on the start of the 4th word");
}

// ── Operator + motion (CP3) ─────────────────────────────────────────────────

#[test]
fn dw_deletes_a_word() {
    let mut st = state("foo bar baz");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "bar baz");
    assert_eq!(st.cursor.offset, 0);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn three_dw_is_a_single_undo_unit() {
    // Risk #2: an operator must issue exactly one delta so `3dw` reverses
    // in one `u`.
    let mut st = state("foo bar baz qux");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "qux");
    assert_eq!(st.history.undo_depth(), 1, "3dw records exactly one delta");
    assert!(st.history.undo(&mut st.buffer).is_some());
    assert_eq!(st.buffer.contents(), "foo bar baz qux");
}

#[test]
fn d2w_and_2dw_both_delete_two_words() {
    // `[op][count][motion]` and `[count][op][motion]` are equivalent.
    let mut a = state("a b c d");
    let mut va = VimState::default();
    feed(&mut va, &mut a, ch('d'));
    feed(&mut va, &mut a, ch('2'));
    feed(&mut va, &mut a, ch('w'));
    assert_eq!(a.buffer.contents(), "c d");

    let mut b = state("a b c d");
    let mut vb = VimState::default();
    feed(&mut vb, &mut b, ch('2'));
    feed(&mut vb, &mut b, ch('d'));
    feed(&mut vb, &mut b, ch('w'));
    assert_eq!(b.buffer.contents(), "c d");
}

#[test]
fn counts_multiply_across_operator_and_motion() {
    // `2d3w` deletes 2*3 = 6 words.
    let mut st = state("a b c d e f g h");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "g h");
}

#[test]
fn de_deletes_to_word_end_inclusive() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('e'));
    assert_eq!(st.buffer.contents(), " bar", "de deletes 'foo' inclusively");
}

#[test]
fn dollar_delete_clears_to_end_of_line() {
    let mut st = state("hello world\nnext");
    let mut vim = VimState::default();
    st.cursor.offset = 6; // on 'w'
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.buffer.contents(), "hello \nnext");
}

#[test]
fn dw_on_last_word_does_not_join_lines() {
    // vim's rule: `dw` on the final word of a line stops at the line end.
    let mut st = state("foo\nbar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "\nbar");
}

// ── dd / yy / p (CP3) ───────────────────────────────────────────────────────

#[test]
fn dd_deletes_a_line_as_one_undo_unit() {
    let mut st = state("one\ntwo\nthree");
    let mut vim = VimState::default();
    let l1 = st.buffer.line_to_char(1);
    st.cursor.offset = l1; // on "two"
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "one\nthree");
    assert_eq!(st.history.undo_depth(), 1);
    assert!(vim.register.linewise);
    assert_eq!(vim.register.text, "two\n");
}

#[test]
fn count_dd_deletes_multiple_lines() {
    let mut st = state("a\nb\nc\nd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "c\nd");
}

#[test]
fn dd_on_last_line_removes_trailing_line_cleanly() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    let l1 = st.buffer.line_to_char(1);
    st.cursor.offset = l1;
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "one", "no stray blank line remains");
}

#[test]
fn yy_then_p_duplicates_the_line_below() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(st.buffer.contents(), "one\ntwo", "yank does not mutate");
    assert!(vim.register.linewise);
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "one\none\ntwo");
    let (line, _) = st.cursor.line_col(&st.buffer);
    assert_eq!(line, 1, "cursor lands on the pasted line below");
}

#[test]
fn yy_arms_a_yank_flash_over_the_line() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('y'));
    let flash = st.active_yank_flash().expect("yy arms a flash");
    // Covers the first line's bytes (including its trailing newline).
    assert_eq!((flash.start, flash.end), (0, 4));
}

#[test]
fn charwise_yank_arms_a_flash_over_the_span() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('e')); // ye → "abc" inclusive
    let flash = st.active_yank_flash().expect("charwise yank arms a flash");
    assert_eq!((flash.start, flash.end), (0, 3));
}

#[test]
fn visual_yank_arms_a_flash() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l')); // extend selection to cover "he"
    feed(&mut vim, &mut st, ch('y'));
    assert!(st.active_yank_flash().is_some(), "visual y arms a flash");
}

#[test]
fn capital_p_pastes_a_line_above() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    let l1 = st.buffer.line_to_char(1);
    st.cursor.offset = l1; // on "two"
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('P'));
    assert_eq!(st.buffer.contents(), "one\ntwo\ntwo");
}

#[test]
fn yy_then_p_on_last_line_inserts_below() {
    let mut st = state("only");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "only\nonly");
}

// ── Charwise yank / paste (CP3) ─────────────────────────────────────────────

#[test]
fn charwise_yank_and_paste_after_cursor() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    // yw yanks "abc" (whole word at start of line).
    feed(&mut vim, &mut st, ch('y'));
    feed(&mut vim, &mut st, ch('e')); // ye → "abc" inclusive
    assert_eq!(vim.register.text, "abc");
    assert!(!vim.register.linewise);
    assert_eq!(st.cursor.offset, 0, "yank parks the cursor at span start");
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "aabcbc", "p inserts after the cursor");
}

// ── cw / cc (CP3) ───────────────────────────────────────────────────────────

#[test]
fn cw_changes_word_and_enters_insert_keeping_trailing_space() {
    // vim special case: `cw` acts like `ce`, leaving the space after.
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), " bar", "cw leaves the trailing space");
    assert_eq!(st.cursor.offset, 0);
}

// `cw`/`cW` change to the end of the word the cursor is *in*, never running
// into the next word — the regression matrix for the `CurrentWordEnd` fix.

#[test]
fn cw_on_single_char_word_changes_only_that_word() {
    // Cursor on a single-char word is simultaneously at its start and end;
    // `cw` must change just it, not spill into the next word.
    let mut st = state("a b c");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), " b c", "cw changes only 'a'");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn cw_on_last_char_of_word_changes_only_that_char() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // last 'o' of "foo"
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "fo bar", "cw changes only the 'o'");
}

#[test]
fn cw_mid_word_changes_to_end_of_that_word() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // first 'l' of "hello"
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "he world", "cw changes 'llo'");
}

#[test]
fn c2w_changes_two_words_without_overshooting() {
    // `c2w` from a word start changes exactly two words (counting the
    // current word as the first), not three.
    let mut st = state("a b c d");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), " c d", "c2w changes 'a b' only");
}

#[test]
fn cw_on_punctuation_changes_the_punct_run() {
    // Word-class separation: `cw` on a punctuation run changes just that
    // run, not the following identifier.
    let mut st = state("a->b");
    let mut vim = VimState::default();
    st.cursor.offset = 1; // on '-'
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "ab", "cw changes the '->' run");
}

#[test]
fn capital_cw_changes_whole_bigword() {
    // `cW` ignores punctuation: on "foo.bar baz" it changes "foo.bar".
    let mut st = state("foo.bar baz");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('W'));
    assert_eq!(st.buffer.contents(), " baz", "cW changes the whole blob");
}

#[test]
fn cc_clears_the_line_keeps_it_and_enters_insert() {
    let mut st = state("one\ntwo\nthree");
    let mut vim = VimState::default();
    let l1 = st.buffer.line_to_char(1);
    st.cursor.offset = l1 + 1; // inside "two"
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('c'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(
        st.buffer.contents(),
        "one\n\nthree",
        "line kept, content cleared"
    );
    assert_eq!(st.cursor.offset, l1);
}

// ── D / C / Y (CP3) ─────────────────────────────────────────────────────────

#[test]
fn capital_d_deletes_to_end_of_line() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // on the space
    feed(&mut vim, &mut st, ch('D'));
    assert_eq!(st.buffer.contents(), "hello");
}

#[test]
fn capital_c_changes_to_end_of_line_and_enters_insert() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    st.cursor.offset = 6; // on 'w'
    feed(&mut vim, &mut st, ch('C'));
    assert_eq!(st.buffer.contents(), "hello ");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn capital_y_yanks_the_whole_line() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('Y'));
    assert!(vim.register.linewise);
    assert_eq!(vim.register.text, "one\n");
    assert_eq!(st.buffer.contents(), "one\ntwo", "Y does not mutate");
}

// ── x / X (CP3) ─────────────────────────────────────────────────────────────

#[test]
fn x_deletes_char_under_cursor_with_count() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "ello");
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "lo", "2x removes two chars");
    assert_eq!(vim.register.text, "el");
}

#[test]
fn x_at_line_end_does_not_cross_the_newline() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // at the newline boundary (end of "ab")
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "ab\ncd", "x at line end is a no-op");
}

#[test]
fn capital_x_deletes_char_before_cursor() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    st.cursor.offset = 3; // on second 'l'
    feed(&mut vim, &mut st, ch('X'));
    assert_eq!(st.buffer.contents(), "helo");
    assert_eq!(st.cursor.offset, 2);
}

#[test]
fn capital_x_at_line_start_is_a_noop() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    let l1 = st.buffer.line_to_char(1);
    st.cursor.offset = l1; // start of "cd"
    feed(&mut vim, &mut st, ch('X'));
    assert_eq!(st.buffer.contents(), "ab\ncd", "X at line start is a no-op");
}

// ── dj / dk linewise (CP3) ──────────────────────────────────────────────────

#[test]
fn dj_deletes_two_lines() {
    let mut st = state("a\nb\nc\nd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('j'));
    assert_eq!(
        st.buffer.contents(),
        "c\nd",
        "dj deletes the cursor line and the next"
    );
}

#[test]
fn dgg_deletes_up_to_the_first_line() {
    let mut st = state("a\nb\nc\nd");
    let mut vim = VimState::default();
    let l2 = st.buffer.line_to_char(2);
    st.cursor.offset = l2; // on "c"
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('g'));
    feed(&mut vim, &mut st, ch('g'));
    assert_eq!(st.buffer.contents(), "d", "dgg removes lines 0..=2");
}

// ── Operator cancellation (CP3) ─────────────────────────────────────────────

#[test]
fn esc_cancels_a_pending_operator() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch('d')), VimOutcome::Pending);
    assert_eq!(vim.sub_mode, VimSubMode::OperatorPending);
    feed(&mut vim, &mut st, esc());
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    assert_eq!(st.buffer.contents(), "hello");
}

#[test]
fn passthrough_chord_cancels_a_pending_operator() {
    // `d` then a `Ctrl-*` chord: the chord falls through to the default
    // handler, and the half-typed operator must not linger (otherwise the
    // next key would be read as a delete target).
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(vim.sub_mode, VimSubMode::OperatorPending);
    let ctrl_s = KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL);
    assert_eq!(feed(&mut vim, &mut st, ctrl_s), VimOutcome::Passthrough);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    // The next motion behaves as a fresh Normal-mode key, not a target.
    feed(&mut vim, &mut st, ch('l'));
    assert_eq!(st.cursor.offset, 1);
    assert_eq!(st.buffer.contents(), "hello", "no edit occurred");
}

#[test]
fn passthrough_chord_clears_a_partial_count() {
    // A count interrupted by a chord must not survive to the next key.
    let mut st = state("hello world");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    let ctrl_s = KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL);
    feed(&mut vim, &mut st, ctrl_s);
    assert_eq!(vim.count, None);
    feed(&mut vim, &mut st, ch('l'));
    assert_eq!(
        st.cursor.offset, 1,
        "the stale count did not repeat the move"
    );
}

#[test]
fn invalid_operator_target_cancels_without_editing() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    let out = feed(&mut vim, &mut st, ch('z'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(st.buffer.contents(), "hello", "an invalid target is inert");
}

// ── CP4: replace (`r{c}`) ───────────────────────────────────────────────────────

#[test]
fn r_replaces_the_char_under_the_cursor() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch('r')), VimOutcome::Pending);
    assert!(vim.pending_replace);
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "xbc");
    assert_eq!(st.cursor.offset, 0, "cursor stays on the replaced char");
    assert!(!vim.pending_replace, "replace consumed the pending state");
}

#[test]
fn r_with_count_replaces_multiple_chars() {
    let mut st = state("abcde");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('r'));
    feed(&mut vim, &mut st, ch('z'));
    assert_eq!(st.buffer.contents(), "zzzde");
    assert_eq!(
        st.cursor.offset, 2,
        "cursor lands on the last replaced char"
    );
}

#[test]
fn r_is_a_noop_when_fewer_chars_remain_than_count() {
    let mut st = state("ab");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('r'));
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "ab", "not enough room → no edit");
}

#[test]
fn r_never_replaces_across_the_newline() {
    // `2r` on the last char of a line would need a char from the next line;
    // vim refuses (a `r` never touches the newline).
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    st.cursor.offset = 1; // on 'b'
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('r'));
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(
        st.buffer.contents(),
        "ab\ncd",
        "no spill onto the next line"
    );
}

#[test]
fn r_then_esc_cancels_without_editing() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('r'));
    let out = feed(&mut vim, &mut st, esc());
    assert_eq!(out, VimOutcome::Consumed);
    assert!(!vim.pending_replace);
    assert_eq!(st.buffer.contents(), "abc", "Esc aborts the replace");
}

// ── CP4: toggle case (`~`) ───────────────────────────────────────────────────────

#[test]
fn tilde_toggles_case_and_advances() {
    let mut st = state("aBc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "ABc");
    assert_eq!(st.cursor.offset, 1, "~ advances past the toggled char");
}

#[test]
fn tilde_with_count_toggles_a_run() {
    let mut st = state("aBcD");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "AbCD");
    assert_eq!(st.cursor.offset, 3);
}

#[test]
fn tilde_clamps_to_the_line_content() {
    // A count larger than the remaining chars toggles only to the line end.
    let mut st = state("aB\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('9'));
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "Ab\ncd", "stops before the newline");
}

#[test]
fn tilde_passes_non_cased_chars_through_unchanged() {
    // The `.` in the middle has no case; it is left as-is while the cased
    // chars around it still toggle, and the cursor still advances past it.
    let mut st = state("a.B");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "A.b", "non-cased char untouched");
    assert_eq!(st.cursor.offset, 3, "cursor advances over the whole run");
}

// ── CP4: join (`J`) ──────────────────────────────────────────────────────────────

#[test]
fn j_joins_the_next_line_with_a_space() {
    let mut st = state("foo\nbar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(st.buffer.contents(), "foo bar");
    assert_eq!(st.cursor.offset, 3, "cursor lands on the join column");
}

#[test]
fn j_strips_leading_whitespace_of_the_joined_line() {
    let mut st = state("foo\n    bar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(
        st.buffer.contents(),
        "foo bar",
        "one space, indent stripped"
    );
}

#[test]
fn j_with_count_joins_multiple_lines_in_one_undo() {
    let mut st = state("a\nb\nc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(st.buffer.contents(), "a b c");
    assert_eq!(st.history.undo_depth(), 1, "3J is a single undo unit");
    st.history.undo(&mut st.buffer);
    assert_eq!(st.buffer.contents(), "a\nb\nc");
}

#[test]
fn j_on_the_last_line_is_a_noop() {
    let mut st = state("only");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(st.buffer.contents(), "only", "nothing below to join");
}

// ── CP4: indent / outdent (`>>` / `<<`) ──────────────────────────────────────────

#[test]
fn double_indent_adds_a_tab_width_of_spaces() {
    let mut st = state("foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(vim.sub_mode, VimSubMode::OperatorPending);
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    foo");
    assert_eq!(st.cursor.offset, 4, "cursor on the first non-blank");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn double_outdent_strips_a_tab_width_of_spaces() {
    let mut st = state("    foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "foo");
}

#[test]
fn indent_then_outdent_round_trips() {
    let mut st = state("foo\nbar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    foo\nbar");
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "foo\nbar");
}

#[test]
fn indent_with_count_spans_lines_in_one_undo() {
    let mut st = state("a\nb\nc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    a\n    b\n    c");
    assert_eq!(st.history.undo_depth(), 1, "3>> is a single undo unit");
}

#[test]
fn outdent_of_a_flush_line_is_a_noop() {
    let mut st = state("foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "foo");
    assert_eq!(st.history.undo_depth(), 0, "no delta recorded");
}

#[test]
fn outdent_strips_a_single_leading_tab() {
    // A leading tab counts as one indent step regardless of tab_width.
    let mut st = state("\tfoo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "foo", "the leading tab is removed");
}

#[test]
fn indent_leaves_blank_lines_within_the_range_empty() {
    // `3>>` spans a blank middle line; vim never indents an empty line, so
    // it stays empty while the surrounding content lines are indented.
    let mut st = state("a\n\nc");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(
        st.buffer.contents(),
        "    a\n\n    c",
        "blank line untouched"
    );
}

// ── CP4: undo / redo (`u` / `Ctrl-R`) ────────────────────────────────────────────

#[test]
fn u_undoes_the_last_change() {
    let mut st = state("foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    foo");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "foo", "u reverts the indent");
}

#[test]
fn ctrl_r_passes_through_for_the_keymap_to_redo() {
    // Vim claims no `Ctrl-R`; it falls through to the default keymap, which
    // CP4 binds to Redo.  The reducer must report `Passthrough`.
    let mut st = state("foo");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ctrl('r')), VimOutcome::Passthrough);
}

#[test]
fn ctrl_r_is_bound_to_redo_in_the_default_keymap() {
    // The binding fires in both default and vim mode (vim passes through to
    // this same keymap).  Ctrl-Shift-Z stays bound to Redo as well.
    let km = KeyMap::build(&KeyBindingOverrides::default()).unwrap();
    assert_eq!(km.action_for(&ctrl('r')), Some(&Action::Redo));
    let ctrl_shift_z = KeyEvent::new(
        KeyCode::Char('z'),
        KeyModifiers::CONTROL | KeyModifiers::SHIFT,
    );
    assert_eq!(km.action_for(&ctrl_shift_z), Some(&Action::Redo));
}

// ── CP5: find / paragraph / matching-pair motions ──────────────────────────────

#[test]
fn f_waits_for_a_char_then_lands_on_it() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch('f')), VimOutcome::Pending);
    assert_eq!(feed(&mut vim, &mut st, ch('o')), VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, 4); // first 'o'
}

#[test]
fn t_stops_one_char_before_the_target() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('t'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.cursor.offset, 5); // the space before 'w'
}

#[test]
fn count_finds_the_nth_occurrence() {
    let mut st = state("a.b.c.d");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('.'));
    assert_eq!(st.cursor.offset, 3); // second '.'
}

#[test]
fn find_miss_leaves_the_cursor_put() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('z'));
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn esc_cancels_a_pending_find() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('f'));
    assert!(vim.pending_find.is_some());
    feed(&mut vim, &mut st, esc());
    assert_eq!(vim.pending_find, None);
    assert_eq!(st.cursor.offset, 0);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn semicolon_replays_and_comma_reverses_the_last_find() {
    let mut st = state("a.b.c.d");
    let mut vim = VimState::default();
    // f. → first '.' (1); ; → second '.' (3); ; → third '.' (5).
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('.'));
    assert_eq!(st.cursor.offset, 1);
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 3);
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 5);
    // , reverses direction → back to the '.' at 3.
    feed(&mut vim, &mut st, ch(','));
    assert_eq!(st.cursor.offset, 3);
}

#[test]
fn semicolon_after_t_skips_the_adjacent_match() {
    // Repeating a `t` must not stay stuck one char before the same target:
    // "abc-d-e-f" — t- lands on 'c'(2); ; advances to 'd'(4) then 'e'(6).
    let mut st = state("abc-d-e-f");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('t'));
    feed(&mut vim, &mut st, ch('-'));
    assert_eq!(st.cursor.offset, 2); // one before the first '-'
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 4); // skipped the adjacent '-', one before the second
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 6); // one before the third '-'
                                     // No further '-' ahead → ; is a no-op, not a backward jump.
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 6);
}

#[test]
fn semicolon_after_capital_t_skips_the_adjacent_match() {
    // The backward form: "a-b-c-d", cursor on 'd'(6).  T- is stuck on 'd'
    // (the '-' at 5 is adjacent); ; must skip back to one after the '-' at 3.
    let mut st = state("a-b-c-d");
    st.cursor.offset = 6;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('T'));
    feed(&mut vim, &mut st, ch('-'));
    assert_eq!(st.cursor.offset, 6); // adjacent '-' → T- can't move
    feed(&mut vim, &mut st, ch(';'));
    assert_eq!(st.cursor.offset, 4); // skipped to one after the '-' at 3
}

#[test]
fn percent_jumps_between_matching_brackets() {
    let mut st = state("(a[b]c)");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('%'));
    assert_eq!(st.cursor.offset, 6); // ( → )
    feed(&mut vim, &mut st, ch('%'));
    assert_eq!(st.cursor.offset, 0); // ) → (
                                     // From the inner '[' → its ']'.
    st.cursor.offset = 2;
    feed(&mut vim, &mut st, ch('%'));
    assert_eq!(st.cursor.offset, 4);
}

#[test]
fn paragraph_motions_move_between_blank_lines() {
    let mut st = state("alpha\n\nbeta");
    let mut vim = VimState::default();
    // } → the blank line (offset 6).
    feed(&mut vim, &mut st, ch('}'));
    assert_eq!(st.cursor.offset, 6);
    // Move into the second paragraph, then { back to the blank line.
    st.cursor.offset = 7; // 'b' of beta
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('{'));
    assert_eq!(st.cursor.offset, 6);
}

#[test]
fn df_deletes_through_the_found_char_inclusive() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    // dfo → delete "hello" (through the first 'o'), one undo unit.
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(st.buffer.contents(), " world");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "hello world", "df is a single undo");
}

#[test]
fn dt_deletes_up_to_but_not_including_the_target() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('t'));
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(st.buffer.contents(), "o world"); // "hell" removed
}

#[test]
fn d_percent_deletes_the_whole_pair() {
    let mut st = state("(abc)d");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('%'));
    assert_eq!(st.buffer.contents(), "d");
}

#[test]
fn find_extends_a_visual_selection() {
    let mut st = state("foobar");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('b'));
    assert_eq!(vim.sub_mode, VimSubMode::Visual);
    let sel = st.selection.expect("visual selection present");
    assert_eq!(sel.anchor, 0);
    assert_eq!(sel.active, 3); // 'b' of "bar"
}

// ── CP6: Visual & VisualLine operators ─────────────────────────────────────────

#[test]
fn visual_charwise_delete_removes_the_highlighted_span() {
    // `v l l d` deletes the *inclusive* span — anchor through the char under
    // the cursor, as in stock vim — yanks it charwise, and returns to Normal.
    let mut st = state("hello world");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // anchor 0
    feed(&mut vim, &mut st, ch('l')); // active 1
    feed(&mut vim, &mut st, ch('l')); // active 2
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "lo world");
    assert_eq!(vim.register.text, "hel");
    assert!(!vim.register.linewise);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn visual_x_is_an_alias_for_delete() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(
        st.buffer.contents(),
        "llo",
        "`v l` covers two chars, inclusively"
    );
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_charwise_yank_leaves_the_buffer_and_parks_at_start() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    st.cursor.offset = 6; // on "world"
    feed(&mut vim, &mut st, ch('v')); // anchor 6
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(st.buffer.contents(), "hello world", "yank never mutates");
    assert_eq!(vim.register.text, "wor");
    assert!(!vim.register.linewise);
    assert_eq!(st.cursor.offset, 6, "cursor parks at the span start");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn v_alone_covers_the_char_under_the_cursor() {
    // The headline inclusivity case: `v y` in vim yanks one character, not
    // zero — the span is never empty, so `v d` / `v y` can't be no-ops.
    let mut st = state("hello");
    let mut vim = VimState::default();
    st.cursor.offset = 1;
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(vim.register.text, "e");
    assert!(!vim.register.linewise);
}

#[test]
fn visual_to_end_of_line_stops_before_the_newline() {
    // `$` parks edamame's cursor on the newline slot (vim's cursor can't go
    // there), so the inclusive extension is suppressed: `v $ y` covers the
    // line's content exactly, and matches walking `l` to the last char.
    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('$'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(vim.register.text, "abc", "the newline is never swallowed");

    let mut st = state("abc\ndef");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(vim.register.text, "abc", "`v l l` agrees with `v $`");
}

#[test]
fn a_backward_visual_selection_includes_the_anchor_char() {
    // With the cursor carried behind the anchor, the anchor's own character
    // is the high end — so it's the one the inclusive rule covers.
    let mut st = state("abcd");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // on 'c'
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('h'));
    feed(&mut vim, &mut st, ch('h'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "d");
    assert_eq!(vim.register.text, "abc");
}

#[test]
fn visual_change_deletes_and_enters_insert() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('c'));
    assert_eq!(st.buffer.contents(), "llo");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert!(st.selection.is_none());
}

#[test]
fn visual_s_is_an_alias_for_change() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('s'));
    assert_eq!(st.buffer.contents(), "llo");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn visual_line_delete_removes_whole_lines_linewise() {
    // `V j d` deletes both touched lines, yanks them linewise.
    let mut st = state("alpha\nbeta\ngamma");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V')); // line 0
    feed(&mut vim, &mut st, ch('j')); // extend onto line 1
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "gamma");
    assert_eq!(vim.register.text, "alpha\nbeta\n");
    assert!(vim.register.linewise);
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_line_yank_is_linewise() {
    let mut st = state("alpha\nbeta\ngamma");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('y'));
    assert_eq!(
        st.buffer.contents(),
        "alpha\nbeta\ngamma",
        "yank never mutates"
    );
    assert_eq!(vim.register.text, "alpha\nbeta\n");
    assert!(vim.register.linewise);
}

#[test]
fn visual_line_delete_then_paste_duplicates() {
    // A VisualLine yank fills the linewise register, so `p` opens a new line.
    let mut st = state("one\ntwo\nthree");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('y')); // yank line 0 linewise
    feed(&mut vim, &mut st, ch('p')); // paste below
    assert_eq!(st.buffer.contents(), "one\none\ntwo\nthree");
}

#[test]
fn visual_indent_right_and_left_round_trip() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('>')); // indent both lines by tab_width (4)
    assert_eq!(st.buffer.contents(), "    ab\n    cd");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    // Re-select and outdent back.
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "ab\ncd");
}

#[test]
fn visual_indent_does_not_touch_the_register() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    vim.register.text = "kept".into();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(
        vim.register.text, "kept",
        "indent must not fill the register"
    );
}

#[test]
fn visual_charwise_indent_acts_on_whole_lines() {
    // `>` from charwise Visual still indents every line the span touches.
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('j')); // span from line 0 into line 1
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    ab\n    cd");
}

#[test]
fn visual_tilde_toggles_case_of_the_span() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l')); // inclusive span [0,3) → "hel"
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "HELlo");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_line_tilde_toggles_whole_lines() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('~'));
    assert_eq!(st.buffer.contents(), "AB\nCD");
}

#[test]
fn visual_join_collapses_the_selected_lines() {
    let mut st = state("one\ntwo\nthree");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('j')); // span lines 0..=2
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(st.buffer.contents(), "one two three");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_single_line_join_pulls_up_the_line_below() {
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // single-line span on line 0
    feed(&mut vim, &mut st, ch('J'));
    assert_eq!(st.buffer.contents(), "one two");
}

#[test]
fn visual_o_swaps_the_selection_ends() {
    let mut st = state("hello world");
    let mut vim = VimState::default();
    st.cursor.offset = 2;
    feed(&mut vim, &mut st, ch('v')); // anchor 2
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l')); // active 4
    feed(&mut vim, &mut st, ch('o')); // swap: cursor jumps to the anchor end
    assert_eq!(vim.sub_mode, VimSubMode::Visual);
    assert_eq!(st.cursor.offset, 2);
    let sel = st.selection.expect("selection persists through swap");
    assert_eq!((sel.anchor, sel.active), (4, 2));
    assert_eq!(vim.visual_anchor, Some(4));
    // A motion now grows the (newly active) low end.
    feed(&mut vim, &mut st, ch('h')); // active 1
    let sel = st.selection.expect("selection persists");
    assert_eq!((sel.anchor, sel.active), (4, 1));
}

#[test]
fn capital_v_then_v_switches_to_charwise_keeping_anchor() {
    let mut st = state("alpha\nbeta");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    let anchor = vim.visual_anchor;
    feed(&mut vim, &mut st, ch('v')); // VisualLine → Visual
    assert_eq!(vim.sub_mode, VimSubMode::Visual);
    assert_eq!(vim.visual_anchor, anchor, "anchor survives the toggle");
    assert!(st.selection.is_some());
}

#[test]
fn v_then_v_exits_to_normal() {
    let mut st = state("alpha");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('v')); // same key → exit
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
}

#[test]
fn capital_v_then_capital_v_exits_to_normal() {
    let mut st = state("alpha");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('V'));
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
}

#[test]
fn visual_operator_is_a_single_undo_unit() {
    // A multi-line VisualLine delete collapses to one `u`.
    let mut st = state("a\nb\nc\nd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('j')); // lines 0..=2
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "d");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(
        st.buffer.contents(),
        "a\nb\nc\nd",
        "one undo restores all three lines"
    );
}

#[test]
fn visual_line_change_clears_the_lines_and_enters_insert() {
    // `V j c` deletes both touched lines but (like `cc`) keeps one empty
    // line to type on, yanks them linewise, and enters Insert.
    let mut st = state("alpha\nbeta\ngamma");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V')); // line 0
    feed(&mut vim, &mut st, ch('j')); // extend onto line 1
    feed(&mut vim, &mut st, ch('c'));
    assert_eq!(st.buffer.contents(), "\ngamma");
    assert_eq!(vim.register.text, "alpha\nbeta\n");
    assert!(vim.register.linewise);
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert!(st.selection.is_none());
}

// ── CP6: Visual `p` / `r{c}` / `u` / `U` ────────────────────────────────────────

#[test]
fn visual_charwise_paste_replaces_the_span_with_the_register() {
    // Yank "world", select "hello", then `p` over it.
    let mut st = state("hello world");
    let mut vim = VimState::default();
    vim.register.text = "world".into();
    vim.register.linewise = false;
    feed(&mut vim, &mut st, ch('v')); // anchor 0
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l')); // inclusive span [0,5) → "hello"
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "world world");
    // The register is left untouched so it can be pasted over again.
    assert_eq!(vim.register.text, "world");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
}

#[test]
fn visual_line_paste_replaces_whole_lines_with_a_linewise_register() {
    let mut st = state("one\ntwo\nthree");
    let mut vim = VimState::default();
    vim.register.text = "X\n".into();
    vim.register.linewise = true;
    feed(&mut vim, &mut st, ch('V')); // line 0
    feed(&mut vim, &mut st, ch('j')); // extend onto line 1
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "X\nthree");
    assert_eq!(vim.register.text, "X\n", "register kept");
}

#[test]
fn visual_line_paste_of_a_charwise_register_keeps_its_own_line() {
    // A charwise register dropped over whole lines gets a trailing newline.
    let mut st = state("one\ntwo");
    let mut vim = VimState::default();
    vim.register.text = "X".into();
    vim.register.linewise = false;
    feed(&mut vim, &mut st, ch('V')); // line 0 only
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(st.buffer.contents(), "X\ntwo");
}

#[test]
fn visual_paste_with_empty_register_is_a_noop_that_leaves_visual() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(
        st.buffer.contents(),
        "hello",
        "empty register changes nothing"
    );
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
}

#[test]
fn visual_charwise_replace_fills_the_span_with_one_char() {
    // `v l l r x` replaces the inclusive [0,3) span ("hel") with "xxx".
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l')); // inclusive span [0,3)
    assert_eq!(feed(&mut vim, &mut st, ch('r')), VimOutcome::Pending);
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), "xxxlo");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(st.selection.is_none());
}

#[test]
fn visual_line_replace_preserves_newlines() {
    // `V j r *` replaces every char of both lines with '*', keeping the
    // line break between them.
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('r'));
    feed(&mut vim, &mut st, ch('*'));
    assert_eq!(st.buffer.contents(), "**\n**");
}

#[test]
fn visual_replace_cancelled_by_esc_keeps_the_selection() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('r')); // arm pending replace
    feed(&mut vim, &mut st, esc()); // cancel
    assert_eq!(st.buffer.contents(), "hello", "no edit on cancel");
    assert_eq!(vim.sub_mode, VimSubMode::Visual, "still in Visual");
    assert!(!vim.pending_replace);
    assert!(st.selection.is_some());
}

#[test]
fn visual_u_forces_lowercase_and_capital_u_forces_uppercase() {
    let mut st = state("Hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l')); // inclusive span [0,5) → "Hello"
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "hello");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);

    // Now uppercase the same span.
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('U'));
    assert_eq!(st.buffer.contents(), "HELLO");
}

#[test]
fn visual_line_set_case_covers_whole_lines() {
    let mut st = state("ab\ncd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('U'));
    assert_eq!(st.buffer.contents(), "AB\nCD");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_paste_and_replace_are_single_undo_units() {
    // VisualLine replace over two lines collapses to one `u`.
    let mut st = state("ab\ncd\nef");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('r'));
    feed(&mut vim, &mut st, ch('z'));
    assert_eq!(st.buffer.contents(), "zz\nzz\nef");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(
        st.buffer.contents(),
        "ab\ncd\nef",
        "one undo restores both lines"
    );
}

// ── CP7: text objects ──────────────────────────────────────────────────────────

#[test]
fn diw_deletes_the_inner_word() {
    // Cursor on "bar"; diw removes just the word, leaving the spaces.
    let mut st = state("foo bar baz");
    st.cursor.offset = 5;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "foo  baz");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn daw_includes_the_trailing_space() {
    let mut st = state("foo bar baz");
    st.cursor.offset = 4; // 'b' of bar
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('a'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), "foo baz");
}

#[test]
fn ciw_changes_word_and_enters_insert() {
    let mut st = state("foo bar");
    st.cursor.offset = 1; // inside "foo"
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), " bar");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn diw_is_a_single_undo_unit() {
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(st.buffer.contents(), " bar");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(
        st.buffer.contents(),
        "foo bar",
        "one undo restores the word"
    );
}

#[test]
fn di_quote_deletes_inside_quotes() {
    let mut st = state("say \"hi\" now");
    st.cursor.offset = 5; // inside "hi"
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('"'));
    assert_eq!(st.buffer.contents(), "say \"\" now");
}

#[test]
fn da_quote_deletes_quotes_and_trailing_space() {
    let mut st = state("say \"hi\" now");
    st.cursor.offset = 5;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('a'));
    feed(&mut vim, &mut st, ch('"'));
    assert_eq!(st.buffer.contents(), "say now");
}

#[test]
fn ci_paren_changes_inside_parens() {
    let mut st = state("f(arg)");
    st.cursor.offset = 3; // inside "arg"
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('('));
    assert_eq!(st.buffer.contents(), "f()");
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.cursor.offset, 2, "Insert begins between the parens");
}

#[test]
fn da_bracket_deletes_brackets_too() {
    let mut st = state("x[ab]y");
    st.cursor.offset = 2;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('a'));
    feed(&mut vim, &mut st, ch('['));
    assert_eq!(st.buffer.contents(), "xy");
}

#[test]
fn di_brace_works_from_a_closing_bracket() {
    // Cursor on the closing brace resolves to its own pair.
    let mut st = state("{ab}");
    st.cursor.offset = 3; // on '}'
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('}'));
    assert_eq!(st.buffer.contents(), "{}");
}

#[test]
fn ci_paren_picks_the_innermost_nested_pair() {
    let mut st = state("(a(b)c)");
    st.cursor.offset = 3; // 'b'
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('('));
    assert_eq!(st.buffer.contents(), "(a()c)");
}

#[test]
fn text_object_on_a_missing_pair_is_a_noop() {
    let mut st = state("abc");
    st.cursor.offset = 1;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('('));
    assert_eq!(
        st.buffer.contents(),
        "abc",
        "no enclosing pair → nothing deleted"
    );
    assert_eq!(vim.sub_mode, VimSubMode::Normal, "operator is cancelled");
}

#[test]
fn visual_inner_word_selects_the_word() {
    let mut st = state("foo bar");
    st.cursor.offset = 5; // inside "bar"
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('w'));
    let sel = st.selection.expect("selection set");
    assert_eq!(
        visual_charwise_range(&sel, &st.buffer),
        4..7,
        "the whole word is selected"
    );
    assert_eq!(
        st.cursor.offset, 6,
        "the cursor parks on the word's last char, as in vim"
    );
    // A following `d` deletes exactly the selection.
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "foo ");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn visual_inner_quote_selects_inside() {
    let mut st = state("a \"hi\" b");
    st.cursor.offset = 3; // inside "hi"
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('i'));
    feed(&mut vim, &mut st, ch('"'));
    let sel = st.selection.expect("selection set");
    assert_eq!(visual_charwise_range(&sel, &st.buffer), 3..5);
}

#[test]
fn cancelling_a_text_object_drops_the_operator() {
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    // Esc after the `i` prefix cancels with no edit.
    feed(&mut vim, &mut st, esc());
    assert_eq!(st.buffer.contents(), "foo bar");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    assert_eq!(vim.pending_text_object, None);
}

// ── Ctrl-* chord passthrough while a sub-state is pending ───────────────────────

#[test]
fn ctrl_chord_during_a_pending_text_object_passes_through() {
    // `di` then Ctrl-S: the operator is abandoned with no edit, and the
    // chord passes through so its app action (Save) still fires — matching
    // the bare-operator path where `d` then Ctrl-S also passes through.
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    assert_eq!(feed(&mut vim, &mut st, ctrl('s')), VimOutcome::Passthrough);
    assert_eq!(st.buffer.contents(), "foo bar", "no edit");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    assert_eq!(vim.pending_text_object, None);
}

#[test]
fn ctrl_chord_during_a_pending_find_passes_through() {
    // `df` then Ctrl-S: same contract for the find pending state.
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('f'));
    assert_eq!(feed(&mut vim, &mut st, ctrl('s')), VimOutcome::Passthrough);
    assert_eq!(st.buffer.contents(), "foo bar", "no edit");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    assert_eq!(vim.pending_find, None);
}

#[test]
fn ctrl_chord_during_a_pending_replace_passes_through() {
    // `r` then Ctrl-S: the replace is abandoned and the chord passes through.
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('r'));
    assert_eq!(feed(&mut vim, &mut st, ctrl('s')), VimOutcome::Passthrough);
    assert_eq!(st.buffer.contents(), "foo bar", "no replacement");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert!(!vim.pending_replace);
}

#[test]
fn non_chord_cancel_of_a_pending_text_object_is_still_consumed() {
    // `di` then `j`: `j` names no object, so it cancels — but a plain key is
    // swallowed (Consumed), not passed through, and applies no motion.
    let mut st = state("foo bar");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('i'));
    assert_eq!(feed(&mut vim, &mut st, ch('j')), VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "foo bar");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
    assert_eq!(vim.pending_op, None);
    assert_eq!(vim.pending_text_object, None);
}

#[test]
fn ctrl_chord_during_a_visual_text_object_passes_through_keeping_selection() {
    // In Visual, `vi` then Ctrl-C cancels the pending object but keeps the
    // selection and passes the chord through (so Ctrl-C copies the span).
    let mut st = state("foo bar");
    st.cursor.offset = 5;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    let before = st.selection.expect("visual selection set on entry");
    feed(&mut vim, &mut st, ch('i'));
    assert_eq!(feed(&mut vim, &mut st, ctrl('c')), VimOutcome::Passthrough);
    assert_eq!(vim.sub_mode, VimSubMode::Visual, "still in Visual");
    assert_eq!(
        st.selection.expect("selection retained"),
        before,
        "the selection is untouched by the cancel"
    );
    assert_eq!(vim.pending_text_object, None);
}

// ── CP8: search (`/ ? n N * #`) ─────────────────────────────────────────────────

/// Install an active navigate-only search for `query` on `st`.
fn with_search(st: &mut EditorState, query: &str) {
    let s = SearchState::new(query.to_owned(), None).expect("valid query");
    st.enter_search(s);
}

#[test]
fn slash_opens_a_command_line_and_enter_submits_a_forward_search() {
    let mut st = state("foo bar foo");
    let mut vim = VimState::default();

    assert_eq!(feed(&mut vim, &mut st, ch('/')), VimOutcome::Pending);
    assert!(vim.cmdline.is_some(), "`/` arms the command line");

    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('o'));
    feed(&mut vim, &mut st, ch('o'));
    let out = feed(&mut vim, &mut st, key(KeyCode::Enter));
    assert_eq!(
        out,
        VimOutcome::EnterSearch {
            forward: true,
            query: "foo".to_string()
        }
    );
    assert!(vim.cmdline.is_none(), "submit closes the command line");
}

#[test]
fn question_mark_submits_a_backward_search() {
    let mut st = state("foo bar foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('?'));
    feed(&mut vim, &mut st, ch('b'));
    feed(&mut vim, &mut st, ch('a'));
    feed(&mut vim, &mut st, ch('r'));
    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Enter)),
        VimOutcome::EnterSearch {
            forward: false,
            query: "bar".to_string()
        }
    );
}

#[test]
fn esc_in_the_command_line_cancels_without_searching() {
    let mut st = state("foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('/'));
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(feed(&mut vim, &mut st, esc()), VimOutcome::Consumed);
    assert!(vim.cmdline.is_none(), "Esc closes the command line");
}

#[test]
fn empty_search_submit_is_a_noop() {
    let mut st = state("foo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('/'));
    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Enter)),
        VimOutcome::Consumed,
        "an empty query closes the prompt with no search"
    );
    assert!(vim.cmdline.is_none());
}

#[test]
fn command_line_typing_never_edits_the_buffer() {
    // Incsearch parks the cursor on the live focus while typing, but the
    // buffer itself must stay untouched and Esc must return the cursor
    // to the origin.
    let mut st = state("hello ell");
    let before = st.buffer.contents();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('/'));
    for c in "ell".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    assert_eq!(st.buffer.contents(), before, "no buffer edit");
    feed(&mut vim, &mut st, esc());
    assert_eq!(st.cursor.offset, 0, "Esc restores the origin");
    assert_eq!(st.buffer.contents(), before);
}

#[test]
fn star_searches_the_word_under_the_cursor_forward() {
    let mut st = state("foo bar foo");
    st.cursor.offset = 4; // on "bar"
    st.update_cursor_block();
    let mut vim = VimState::default();
    assert_eq!(
        feed(&mut vim, &mut st, ch('*')),
        VimOutcome::EnterSearch {
            forward: true,
            query: "bar".to_string()
        }
    );
}

#[test]
fn star_emits_a_query_that_decodes_back_to_the_keyword() {
    // `*` supplies buffer text where the rest of the flow expects typed
    // escape syntax, so it escapes on the way out.  A keyword run is
    // word-class only, so today that is a no-op — this pins the
    // round-trip so it stays one if `iskeyword` ever widens.
    let mut st = state("foo_bar1 baz");
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, ch('*'));
    let VimOutcome::EnterSearch { query, .. } = out else {
        panic!("expected EnterSearch, got {out:?}");
    };
    assert_eq!(edamame::search::escape::decode(&query).unwrap(), "foo_bar1");
}

#[test]
fn hash_searches_the_word_under_the_cursor_backward() {
    let mut st = state("alpha beta");
    st.cursor.offset = 7; // inside "beta"
    st.update_cursor_block();
    let mut vim = VimState::default();
    assert_eq!(
        feed(&mut vim, &mut st, ch('#')),
        VimOutcome::EnterSearch {
            forward: false,
            query: "beta".to_string()
        }
    );
}

#[test]
fn star_on_whitespace_with_no_following_word_is_a_noop() {
    let mut st = state("hi    ");
    st.cursor.offset = 3; // trailing spaces, no word after
    st.update_cursor_block();
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch('*')), VimOutcome::Consumed);
}

#[test]
fn n_and_capital_n_advance_and_retreat_the_focused_match() {
    let mut st = state("foo bar foo baz foo");
    with_search(&mut st, "foo"); // matches at 0, 8, 16
    let mut vim = VimState::default();
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 0);

    feed(&mut vim, &mut st, ch('n'));
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 1);
    assert_eq!(st.cursor.offset, 8, "cursor follows the match");

    feed(&mut vim, &mut st, ch('N'));
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 0);
    assert_eq!(st.cursor.offset, 0);

    // Retreat past the first wraps to the last.
    feed(&mut vim, &mut st, ch('N'));
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 2);
}

#[test]
fn count_drives_n() {
    let mut st = state("x y x y x y x"); // "x" at 0,4,8,12
    with_search(&mut st, "x");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('2'));
    feed(&mut vim, &mut st, ch('n'));
    assert_eq!(
        st.search.as_ref().unwrap().focused_idx,
        2,
        "2n advances twice"
    );
}

#[test]
fn n_with_no_active_search_is_an_inert_noop() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch('n')), VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, 0);
    assert!(st.search.is_none());
}

#[test]
fn esc_in_normal_dismisses_an_active_search() {
    let mut st = state("foo bar foo");
    with_search(&mut st, "foo");
    let mut vim = VimState::default();
    assert!(st.search.is_some());
    feed(&mut vim, &mut st, esc());
    assert!(
        st.search.is_none(),
        "Esc clears the search highlight (`:noh`)"
    );
}

// ── Normal mode never edits (Backspace / Delete / Enter / Tab) ───────────────────

#[test]
fn backspace_in_normal_moves_left_without_editing() {
    let mut st = state("hello");
    st.cursor.offset = 3;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, key(KeyCode::Backspace));
    assert_eq!(out, VimOutcome::Consumed, "Backspace is swallowed");
    assert_eq!(st.buffer.contents(), "hello", "no buffer edit");
    assert_eq!(st.cursor.offset, 2, "cursor moved left");
}

#[test]
fn delete_in_normal_moves_right_without_editing() {
    let mut st = state("hello");
    st.cursor.offset = 1;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, key(KeyCode::Delete));
    assert_eq!(st.buffer.contents(), "hello", "no buffer edit");
    assert_eq!(st.cursor.offset, 2, "cursor moved right");
}

#[test]
fn count_drives_backspace_in_normal() {
    let mut st = state("hello");
    st.cursor.offset = 4;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    assert_eq!(st.buffer.contents(), "hello");
    assert_eq!(st.cursor.offset, 1, "3<BS> moves left three times");
}

#[test]
fn enter_in_normal_moves_to_next_line_first_non_blank_without_editing() {
    let mut st = state("abc\n   xyz");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, key(KeyCode::Enter));
    assert_eq!(st.buffer.contents(), "abc\n   xyz", "no newline inserted");
    assert_eq!(
        st.cursor.offset, 7,
        "cursor on the first non-blank of line 2"
    );
}

#[test]
fn tab_in_normal_without_a_search_is_inert() {
    let mut st = state("hi");
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, key(KeyCode::Tab));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "hi", "Tab must not insert a tab");
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn tab_and_backtab_walk_search_matches_like_n_and_capital_n() {
    let mut st = state("foo bar foo baz foo");
    with_search(&mut st, "foo"); // matches at 0, 8, 16
    let mut vim = VimState::default();
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 0);

    feed(&mut vim, &mut st, key(KeyCode::Tab));
    assert_eq!(st.search.as_ref().unwrap().focused_idx, 1, "Tab advances");
    assert_eq!(st.cursor.offset, 8, "cursor follows the match");

    feed(&mut vim, &mut st, key(KeyCode::BackTab));
    assert_eq!(
        st.search.as_ref().unwrap().focused_idx,
        0,
        "Shift-Tab retreats"
    );
}

#[test]
fn backspace_and_delete_extend_a_visual_selection_without_editing() {
    let mut st = state("hello world");
    st.cursor.offset = 4;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v')); // enter Visual, anchor at 4
    feed(&mut vim, &mut st, key(KeyCode::Delete)); // extend right
    feed(&mut vim, &mut st, key(KeyCode::Delete));
    assert_eq!(
        st.buffer.contents(),
        "hello world",
        "Visual edit-keys don't edit"
    );
    let sel = st.selection.as_ref().expect("selection still set");
    assert_eq!((sel.anchor, sel.active), (4, 6), "selection extended right");
    feed(&mut vim, &mut st, key(KeyCode::Backspace)); // extend back left
    let sel = st.selection.as_ref().unwrap();
    assert_eq!(sel.active, 5, "Backspace pulls the active end left");
}

// ── CP9: Ex commands (`:w :q :wq :s :%s`) ──────────────────────────────────────

/// Type a full `:`-command (the leading `:`, the body, then Enter) and
/// return the outcome of the submitting Enter press.
fn ex_cmd(vim: &mut VimState, st: &mut EditorState, body: &str) -> VimOutcome {
    feed(vim, st, ch(':'));
    for c in body.chars() {
        feed(vim, st, ch(c));
    }
    feed(vim, st, key(KeyCode::Enter))
}

#[test]
fn colon_opens_the_ex_command_line() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    assert_eq!(feed(&mut vim, &mut st, ch(':')), VimOutcome::Pending);
    let cl = vim.cmdline.as_ref().expect("ex command line armed");
    assert_eq!(cl.kind.prefix(), ':');
    // Bare letters now edit the command line, not the buffer.
    assert_eq!(feed(&mut vim, &mut st, ch('w')), VimOutcome::Consumed);
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "w");
    // Esc cancels the prompt without acting.
    assert_eq!(feed(&mut vim, &mut st, esc()), VimOutcome::Consumed);
    assert!(vim.cmdline.is_none());
    assert_eq!(st.buffer.contents(), "hello", "cancelled `:` never edits");
}

#[test]
fn ex_line_address_jumps_to_that_line() {
    let mut st = state(LINES);
    let mut vim = VimState::default();

    assert_eq!(ex_cmd(&mut vim, &mut st, "3"), VimOutcome::Consumed);
    assert_eq!(st.cursor.offset, LINES.find("charlie").unwrap());

    // `:$` is the last line, clamped like `G`.
    ex_cmd(&mut vim, &mut st, "$");
    assert_eq!(st.cursor.offset, LINES.find("delta").unwrap());

    // Still an unknown command when it isn't a line address.
    assert!(matches!(
        ex_cmd(&mut vim, &mut st, "3x"),
        VimOutcome::Flash(_)
    ));
}

#[test]
fn ex_write_returns_save_outcome() {
    let mut st = state_with_path("hello");
    let mut vim = VimState::default();
    assert_eq!(ex_cmd(&mut vim, &mut st, "w"), VimOutcome::Save);
    assert!(vim.cmdline.is_none(), "prompt closes on submit");
}

#[test]
fn ex_quit_and_writequit_outcomes() {
    let mut st = state_with_path("hello");
    let mut vim = VimState::default();
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "q"),
        VimOutcome::Quit { save_first: false }
    );
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "wq"),
        VimOutcome::Quit { save_first: true }
    );
}

#[test]
fn ex_write_path_copies_saveas_repoints() {
    let mut st = state_with_path("hello");
    let mut vim = VimState::default();
    // `:w <path>` writes a copy (keeps the current file) — real vim.
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "w other.md"),
        VimOutcome::SaveCopy {
            path: std::path::PathBuf::from("other.md"),
            then_quit: false,
            force: false,
        }
    );
    // `:w! <path>` forces past the overwrite-confirmation prompt.
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "w! other.md"),
        VimOutcome::SaveCopy {
            path: std::path::PathBuf::from("other.md"),
            then_quit: false,
            force: true,
        }
    );
    // `:saveas <path>` re-points the buffer at the new path.
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "saveas other.md"),
        VimOutcome::SaveAs {
            path: Some(std::path::PathBuf::from("other.md")),
            then_quit: false,
            force: false,
        }
    );
    // `:saveas` with no argument prompts (path None).
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "saveas"),
        VimOutcome::SaveAs {
            path: None,
            then_quit: false,
            force: false,
        }
    );
    // `:wq <path>` writes a copy, then quits.
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "wq out.md"),
        VimOutcome::SaveCopy {
            path: std::path::PathBuf::from("out.md"),
            then_quit: true,
            force: false,
        }
    );
}

#[test]
fn ex_write_on_pathless_buffer_prompts() {
    // A never-saved buffer has no destination, so a bare `:w` opens the
    // Save As prompt; `:wq` does the same but also carries the quit intent.
    let mut st = state("hello");
    let mut vim = VimState::default();
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "w"),
        VimOutcome::SaveAs {
            path: None,
            then_quit: false,
            force: false,
        }
    );
    assert_eq!(
        ex_cmd(&mut vim, &mut st, "wq"),
        VimOutcome::SaveAs {
            path: None,
            then_quit: true,
            force: false,
        }
    );
}

#[test]
fn ex_empty_command_is_a_silent_noop() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch(':'));
    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Enter)),
        VimOutcome::Consumed
    );
    assert!(vim.cmdline.is_none());
    assert_eq!(st.buffer.contents(), "hello");
}

#[test]
fn ex_substitute_current_line_first_match_only() {
    let mut st = state("foo foo\nfoo foo");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "s/foo/bar/");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(
        st.buffer.contents(),
        "bar foo\nfoo foo",
        ":s replaces only the first match on the current line"
    );
}

#[test]
fn ex_substitute_current_line_global_flag() {
    let mut st = state("foo foo\nfoo foo");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "s/foo/bar/g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(
        st.buffer.contents(),
        "bar bar\nfoo foo",
        "g replaces every match on the current line only"
    );
}

#[test]
fn ex_substitute_whole_file_global() {
    let mut st = state("foo foo\nfoo foo");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "%s/foo/bar/g");
    assert_eq!(out, VimOutcome::Flash("4 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "bar bar\nbar bar");
}

#[test]
fn ex_substitute_ignore_case_flag() {
    let mut st = state("Foo foo FOO");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "%s/foo/x/gi");
    assert_eq!(out, VimOutcome::Flash("3 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "x x x");
}

#[test]
fn ex_substitute_is_a_single_undo_unit() {
    let mut st = state("foo\nfoo\nfoo");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "%s/foo/bar/g");
    assert_eq!(st.buffer.contents(), "bar\nbar\nbar");
    // One `u` reverts the whole substitution.
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(
        st.buffer.contents(),
        "foo\nfoo\nfoo",
        ":%s undoes in a single step"
    );
}

#[test]
fn ex_substitute_no_match_flashes_and_leaves_buffer() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "s/zzz/x/");
    assert_eq!(out, VimOutcome::Flash("Pattern not found: zzz".to_owned()));
    assert_eq!(st.buffer.contents(), "abc", "a no-match records no edit");
    assert!(
        !st.dirty,
        "a no-match substitution does not dirty the buffer"
    );
}

#[test]
fn ex_substitute_supports_regex() {
    let mut st = state("a1b2c3");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/[0-9]/-/g");
    assert_eq!(out, VimOutcome::Flash("3 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "a-b-c-");
}

// ── CP9: multi-line `:s` patterns ─────────────────────────────────────────────

#[test]
fn ex_substitute_pattern_can_span_a_line_break() {
    // The motivating case: collapse "two trailing spaces + break" into a
    // single space.
    let mut st = state("foo  \nbar  \nbaz");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/  \n/ /g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "foo bar baz");
}

#[test]
fn ex_substitute_newline_pattern_joins_every_line() {
    let mut st = state("a\nb\nc");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/\n//g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "abc");
    assert_eq!(st.buffer.line_count(), 1);
    assert_eq!(st.cursor.offset, 0, "cursor parks on the first line");
}

#[test]
fn ex_substitute_multiline_is_a_single_undo_unit() {
    let mut st = state("a\nb\nc");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, r"%s/\n/-/g");
    assert_eq!(st.buffer.contents(), "a-b-c");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "a\nb\nc", "one `u` restores it all");
}

#[test]
fn ex_substitute_all_lines_can_consume_the_files_final_newline() {
    // `:%s` resolves to the phantom line past the trailing newline, so
    // that newline IS in range — which is what makes the motivating case
    // work on the document's last line.
    let mut st = state("foo  \n");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/  \n/ /");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(st.buffer.contents(), "foo ");
}

#[test]
fn ex_substitute_on_one_line_has_no_break_to_match() {
    // The documented divergence from vim: `:s` resolves to a single-line
    // range, whose own break is outside it — so `:s/\n//` cannot join with
    // the next line.  Vim allows that; edamame bounds every substitution
    // to the lines it names.
    let mut st = state("a\nb");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"s/\n//");
    assert_eq!(out, VimOutcome::Flash("Pattern not found: \\n".to_owned()));
    assert_eq!(st.buffer.contents(), "a\nb");
}

#[test]
fn ex_substitute_replacement_newline_splits_a_line() {
    // The replacement side has always understood `\n`; check it still
    // does now that the pattern walk is region-wide.
    let mut st = state("a-b-c");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/-/\n/g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "a\nb\nc");
}

#[test]
fn ex_substitute_anchors_still_bind_per_line() {
    // The pattern now sees the whole range at once, so `^`/`$` would
    // anchor to the region's ends without `multi_line`.
    let mut st = state("ab\nab");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "%s/^a/X/g");
    assert_eq!(st.buffer.contents(), "Xb\nXb");

    let mut st = state("ab\nab");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "%s/b$/Y/g");
    assert_eq!(st.buffer.contents(), "aY\naY");
}

#[test]
fn ex_substitute_dot_does_not_cross_a_line_break() {
    // vim's `.` never matches a newline; `dot_matches_new_line` must stay
    // off even though the haystack now contains breaks.
    let mut st = state("a\nb");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "%s/a.b/X/");
    assert_eq!(out, VimOutcome::Flash("Pattern not found: a.b".to_owned()));
    assert_eq!(st.buffer.contents(), "a\nb");
}

/// Type the body of a `:`-command that is already open (its `'<,'>` prefix
/// pre-filled by a Visual-mode `:`), then submit with Enter.
fn submit_open_ex(vim: &mut VimState, st: &mut EditorState, body: &str) -> VimOutcome {
    for c in body.chars() {
        feed(vim, st, ch(c));
    }
    feed(vim, st, key(KeyCode::Enter))
}

#[test]
fn colon_in_visual_prefills_the_visual_range() {
    let mut st = state("foo\nfoo\nfoo");
    let mut vim = VimState::default();
    // V-LINE over lines 0..=1, then `:` opens the ex prompt pre-filled with
    // the `'<,'>` range and drops the selection (as vim does).
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    assert_eq!(feed(&mut vim, &mut st, ch(':')), VimOutcome::Pending);
    let cl = vim.cmdline.as_ref().expect("ex prompt armed from Visual");
    assert_eq!(cl.input, "'<,'>", "the `'<,'>` range is pre-filled");
    assert_eq!(cl.cursor, 5, "cursor parks at the end so typing appends");
    assert_eq!(vim.sub_mode, VimSubMode::Normal, "Visual exits on `:`");
    assert!(st.selection.is_none(), "the highlight is dropped");
    assert_eq!(
        vim.last_visual_range,
        Some((0, 1)),
        "marks span the selection"
    );
}

#[test]
fn ex_substitute_over_visual_line_range() {
    let mut st = state("foo\nfoo\nfoo\nfoo");
    let mut vim = VimState::default();
    // Select lines 0..=1 linewise, then `:'<,'>s/foo/bar/g`.
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch(':'));
    let out = submit_open_ex(&mut vim, &mut st, "s/foo/bar/g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(
        st.buffer.contents(),
        "bar\nbar\nfoo\nfoo",
        ":'<,'>s only touches the selected lines"
    );
}

#[test]
fn ex_substitute_over_charwise_visual_uses_whole_lines() {
    let mut st = state("foo foo\nfoo foo\nfoo foo");
    let mut vim = VimState::default();
    // Charwise Visual from mid-line 0 into line 1; the ex range is still
    // line-oriented (whole lines 0..=1), matching vim.
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('l'));
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch(':'));
    let out = submit_open_ex(&mut vim, &mut st, "s/foo/bar/g");
    assert_eq!(out, VimOutcome::Flash("4 substitutions".to_owned()));
    assert_eq!(
        st.buffer.contents(),
        "bar bar\nbar bar\nfoo foo",
        "charwise selection substitutes over the whole touched lines"
    );
}

#[test]
fn ex_substitute_multiline_match_cannot_escape_the_visual_range() {
    // A `\n` pattern is bounded by the selection: the break after "b" is
    // the last one inside lines 0..=1, and the break after it (which would
    // pull line 2 up) is out of range.  This is the guarantee that a
    // visual-range substitute never edits text you didn't select.
    let mut st = state("a\nb\nc\nd");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch(':'));
    let out = submit_open_ex(&mut vim, &mut st, r"s/\n/-/g");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(st.buffer.contents(), "a-b\nc\nd");
}

#[test]
fn ex_write_from_visual_ignores_the_range_prefix() {
    let mut st = state_with_path("hello");
    let mut vim = VimState::default();
    // `:` in Visual auto-inserts `'<,'>`; the write/quit family ignores it and
    // acts on the whole buffer, so a Visual `:w` still just saves.
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch(':'));
    assert_eq!(submit_open_ex(&mut vim, &mut st, "w"), VimOutcome::Save);
    assert_eq!(st.buffer.contents(), "hello");
}

#[test]
fn ex_unknown_command_from_visual_keeps_the_range_in_the_error() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch(':'));
    let out = submit_open_ex(&mut vim, &mut st, "nope");
    assert_eq!(
        out,
        VimOutcome::Flash("Not an editor command: '<,'>nope".to_owned())
    );
    assert_eq!(st.buffer.contents(), "hello");
}

#[test]
fn ex_parse_error_flashes_without_editing() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, "nope");
    assert_eq!(
        out,
        VimOutcome::Flash("Not an editor command: nope".to_owned())
    );
    assert_eq!(st.buffer.contents(), "hello");
    assert_eq!(vim.sub_mode, VimSubMode::Normal);
}

#[test]
fn ex_invalid_regex_flashes() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    // `\(` translates to an unbalanced `(` — it passes the vim→regex
    // translator but the engine rejects it at compile time.
    let out = ex_cmd(&mut vim, &mut st, r"s/\(/x/");
    match out {
        VimOutcome::Flash(text) => assert!(text.starts_with("Invalid pattern:"), "got: {text}"),
        other => panic!("expected an invalid-pattern flash, got {other:?}"),
    }
    assert_eq!(st.buffer.contents(), "hello");
}

// ── CP9 follow-up: vim-syntax substitution (translator + fancy-regex) ──────────

#[test]
fn ex_substitute_uses_vim_pattern_syntax() {
    // Vim grouping/quantifiers (`\(…\)`, `\+`) and a backreference in the
    // replacement (`\1`), not the regex-crate `$1` form.
    let mut st = state("hello world");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"s/\(\w\+\) \(\w\+\)/\2 \1/");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(st.buffer.contents(), "world hello");
}

#[test]
fn ex_substitute_replacement_case_modifier() {
    // `\U\1` upcases the captured group — a vim replacement feature the plain
    // regex crate cannot express.
    let mut st = state("shout please");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"s/\(\w\+\)/\U\1/");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(st.buffer.contents(), "SHOUT please");
}

#[test]
fn ex_substitute_pattern_backreference() {
    // `\(.\)\1` (a doubled character) is impossible with the linear-time
    // regex crate; fancy-regex makes it work.
    let mut st = state("aa bb cd");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/\(.\)\1/X/g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "X X cd");
}

#[test]
fn ex_substitute_very_magic_mode() {
    let mut st = state("foofoo bar");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"s/\v(foo)+/baz/");
    assert_eq!(out, VimOutcome::Flash("1 substitution".to_owned()));
    assert_eq!(st.buffer.contents(), "baz bar");
}

#[test]
fn ex_substitute_word_boundary() {
    // `\<cat\>` matches the whole word only, not the `cat` in `category`.
    let mut st = state("cat category cat");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"%s/\<cat\>/dog/g");
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "dog category dog");
}

#[test]
fn ex_substitute_unsupported_atom_flashes() {
    let mut st = state("foobar");
    let mut vim = VimState::default();
    let out = ex_cmd(&mut vim, &mut st, r"s/foo\zsbar/X/");
    match out {
        VimOutcome::Flash(text) => {
            assert!(text.starts_with("Unsupported vim pattern:"), "got: {text}")
        }
        other => panic!("expected unsupported-pattern flash, got {other:?}"),
    }
    assert_eq!(
        st.buffer.contents(),
        "foobar",
        "rejected pattern records no edit"
    );
}

// ── Command-line history (Up/Down recall) ───────────────────────────────────────

#[test]
fn ex_history_recalls_previous_commands_with_arrows() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "w");
    ex_cmd(&mut vim, &mut st, "q");
    // Reopen the prompt and type a partial draft.
    feed(&mut vim, &mut st, ch(':'));
    feed(&mut vim, &mut st, ch('x'));
    // Up walks newest → oldest.
    feed(&mut vim, &mut st, key(KeyCode::Up));
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "q");
    feed(&mut vim, &mut st, key(KeyCode::Up));
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "w");
    feed(&mut vim, &mut st, key(KeyCode::Up)); // already oldest — no further change
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "w");
    // Down walks back, and past the newest restores the typed draft.
    feed(&mut vim, &mut st, key(KeyCode::Down));
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "q");
    feed(&mut vim, &mut st, key(KeyCode::Down));
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "x");
    assert_eq!(vim.cmdline.as_ref().unwrap().cursor, 1);
}

#[test]
fn recalled_ex_command_runs_when_submitted() {
    let mut st = state_with_path("hello");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "w");
    feed(&mut vim, &mut st, ch(':'));
    feed(&mut vim, &mut st, key(KeyCode::Up));
    assert_eq!(
        feed(&mut vim, &mut st, key(KeyCode::Enter)),
        VimOutcome::Save
    );
}

#[test]
fn ex_and_search_keep_independent_histories() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "w");
    // A search submission records into the (separate) search history.
    feed(&mut vim, &mut st, ch('/'));
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('o'));
    feed(&mut vim, &mut st, ch('o'));
    feed(&mut vim, &mut st, key(KeyCode::Enter));
    assert_eq!(vim.ex_history, vec!["w".to_owned()]);
    assert_eq!(vim.search_history, vec!["foo".to_owned()]);
    // The `:` prompt only recalls ex history, never the search query.
    feed(&mut vim, &mut st, ch(':'));
    feed(&mut vim, &mut st, key(KeyCode::Up));
    assert_eq!(vim.cmdline.as_ref().unwrap().input, "w");
}

#[test]
fn empty_ex_submit_is_not_recorded() {
    let mut st = state("hello");
    let mut vim = VimState::default();
    ex_cmd(&mut vim, &mut st, "");
    assert!(
        vim.ex_history.is_empty(),
        "an empty `:` line is not history"
    );
}

// ── CP10: markdown-aware list wiring ────────────────────────────────────────────

#[test]
fn o_continues_an_ordered_list_with_the_next_number() {
    // `o` after `1. Item` opens a fresh `2. ` item below and enters Insert.
    let mut st = state("1. Item\n");
    let mut vim = VimState::default();
    st.cursor.offset = 3; // inside "Item"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "1. Item\n2. \n");
}

#[test]
fn o_continues_a_bullet_list_with_the_same_marker() {
    let mut st = state("- one\n");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // inside "one"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "- one\n- \n");
}

#[test]
fn o_continue_is_a_single_undo_unit() {
    let mut st = state("1. a\n2. b\n");
    let mut vim = VimState::default();
    st.cursor.offset = 0;
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('o'));
    // Inserted `2. ` and renumbered `b` to `3.` inline — all one delta.
    assert_eq!(st.buffer.contents(), "1. a\n2. \n3. b\n");
    // One undo restores the original document.
    feed(&mut vim, &mut st, esc());
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "1. a\n2. b\n");
}

#[test]
fn capital_o_opens_an_item_above_a_later_list_item() {
    // `O` on the second item continues from the first, landing a new item
    // between them and renumbering the tail.
    let mut st = state("1. a\n2. b\n");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // on "2. b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('O'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "1. a\n2. \n3. b\n");
}

#[test]
fn o_outside_a_list_opens_a_plain_line() {
    let mut st = state("hello\n");
    let mut vim = VimState::default();
    st.cursor.offset = 1;
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
    assert_eq!(st.buffer.contents(), "hello\n\n");
}

#[test]
fn dd_renumbers_an_ordered_list() {
    // Deleting the middle item renumbers the survivors monotonically.
    let mut st = state("1. a\n2. b\n3. c\n");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // on "2. b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "1. a\n2. c\n");
}

#[test]
fn dd_does_not_renumber_a_bullet_list() {
    let mut st = state("- a\n- b\n- c\n");
    let mut vim = VimState::default();
    st.cursor.offset = 4; // on "- b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(st.buffer.contents(), "- a\n- c\n");
}

#[test]
fn indent_indents_a_bullet_list_item() {
    let mut st = state("- item\n");
    let mut vim = VimState::default();
    st.cursor.offset = 2; // on "item"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    - item\n");
}

#[test]
fn outdent_round_trips_a_list_indent() {
    let mut st = state("    - item\n");
    let mut vim = VimState::default();
    st.cursor.offset = 6; // on "item"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "- item\n");
}

#[test]
fn indent_indents_a_nested_ordered_list_item() {
    // `>>` on the second ordered item nests it (fresh `1.`) and renumbers
    // the outer run — the structure-aware path, not plain space-indent.
    let mut st = state("1. a\n2. b\n3. c\n");
    let mut vim = VimState::default();
    st.cursor.offset = 5; // on "2. b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "1. a\n    1. b\n2. c\n");
}

#[test]
fn indent_outside_a_list_falls_back_to_plain_spaces() {
    let mut st = state("hello\n");
    let mut vim = VimState::default();
    st.cursor.offset = 0;
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "    hello\n");
}

#[test]
fn outdent_renumbers_the_item_into_the_outer_ordered_run() {
    // De-nesting an ordered item must renumber the source so the raw markers
    // match the (already sequential) rendered numbers — the outdented item
    // carries a stale nested `1.` into the outer list otherwise.
    let mut st = state("1. a\n2. b\n    1. c\n3. d\n");
    let mut vim = VimState::default();
    st.cursor.offset = 17; // on "c" in "    1. c"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('<'));
    feed(&mut vim, &mut st, ch('<'));
    assert_eq!(st.buffer.contents(), "1. a\n2. b\n3. c\n4. d\n");
}

#[test]
fn indent_onto_an_existing_nested_run_renumbers_the_join() {
    // Nesting an item below a sibling nested run joins it as a duplicate `1.`;
    // the renumber pass must fix the nested sequence so the source matches the
    // rendered numbers.
    let mut st = state("1. a\n    1. x\n    2. y\n2. b\n");
    let mut vim = VimState::default();
    st.cursor.offset = 26; // on "b" in "2. b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('>'));
    feed(&mut vim, &mut st, ch('>'));
    assert_eq!(st.buffer.contents(), "1. a\n    1. x\n    2. y\n    3. b\n");
}

#[test]
fn dd_renumbers_an_outer_list_across_a_nested_child() {
    // Deleting an outer item whose sibling carries a nested child must still
    // renumber the outer run — the post-`dd` cursor lands on the nested child,
    // so a flat renumber would touch only the inner list and leave the outer
    // sequence stale (the rendered view shows it sequential regardless).
    let mut st = state("1. Ordered\n2. Second\n    1. nested\n3. Third\n");
    let mut vim = VimState::default();
    st.cursor.offset = 11; // on "2. Second"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(
        st.buffer.contents(),
        "1. Ordered\n    1. nested\n2. Third\n",
    );
}

#[test]
fn dd_renumbers_each_nested_sublist_under_its_own_parent() {
    // Two parents each own a nested ordered sub-list; deleting a middle
    // top-level item renumbers the outer run (3→2) while both sub-lists keep
    // restarting at 1 under their own parent.
    let mut st = state("1. a\n    1. x\n    2. y\n2. b\n3. c\n    1. p\n    2. q\n");
    let mut vim = VimState::default();
    st.cursor.offset = 23; // on "2. b"
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('d'));
    assert_eq!(
        st.buffer.contents(),
        "1. a\n    1. x\n    2. y\n2. c\n    1. p\n    2. q\n",
    );
}

// ── Live `:s` substitution preview (inccommand) ──────────────────────────────

/// Open the `:` prompt and type `body` without submitting.
fn type_ex(vim: &mut VimState, st: &mut EditorState, body: &str) {
    feed(vim, st, ch(':'));
    for c in body.chars() {
        feed(vim, st, ch(c));
    }
}

#[test]
fn typing_a_substitution_pattern_previews_matches_live() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/foo");
    let preview = st.substitute_preview.as_ref().expect("preview active");
    assert_eq!(
        preview.highlights,
        vec![0..3, 8..11],
        "every line's first match highlights while the pattern is typed"
    );
    assert_eq!(st.buffer.contents(), "foo bar\nfoo", "text untouched");
    assert!(!st.dirty, "a highlight-only preview must not dirty");
}

#[test]
fn typing_a_replacement_previews_the_substituted_text() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/foo/quux/g");
    assert_eq!(
        st.buffer.contents(),
        "quux bar\nquux",
        "the buffer shows the substituted text while typing"
    );
    assert!(!st.dirty, "the preview edit must not dirty the buffer");
    assert_eq!(
        st.history.undo_depth(),
        0,
        "no undo delta may be recorded for the preview"
    );
    let preview = st.substitute_preview.as_ref().expect("preview active");
    assert_eq!(
        preview.highlights,
        vec![0..4, 9..13],
        "the inserted segments highlight in the previewed text"
    );
}

#[test]
fn esc_reverts_the_preview_and_restores_the_view() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    st.cursor.offset = 5;
    st.update_cursor_block();
    type_ex(&mut vim, &mut st, "%s/foo/quux/g");
    assert_eq!(st.buffer.contents(), "quux bar\nquux");
    feed(&mut vim, &mut st, esc());
    assert_eq!(st.buffer.contents(), "foo bar\nfoo", "Esc reverts");
    assert_eq!(st.cursor.offset, 5, "cursor restored");
    assert!(st.substitute_preview.is_none());
    assert!(vim.cmdline.is_none());
    // No phantom undo step was recorded by the preview round-trip.
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(st.buffer.contents(), "foo bar\nfoo");
}

#[test]
fn submitting_a_previewed_substitution_is_one_undo_unit() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/foo/bar/g");
    assert!(
        st.substitute_preview.is_some(),
        "preview active before Enter"
    );
    let out = feed(&mut vim, &mut st, key(KeyCode::Enter));
    assert_eq!(out, VimOutcome::Flash("2 substitutions".to_owned()));
    assert_eq!(st.buffer.contents(), "bar bar\nbar");
    assert!(st.dirty, "the committed substitution dirties the buffer");
    assert!(st.substitute_preview.is_none(), "preview ended on submit");
    feed(&mut vim, &mut st, ch('u'));
    assert_eq!(
        st.buffer.contents(),
        "foo bar\nfoo",
        "one `u` reverts the whole substitution — identical to a preview-less submit"
    );
}

#[test]
fn backspace_across_the_second_delimiter_walks_the_preview_back() {
    let mut st = state("a foo b");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/foo/XY");
    assert_eq!(st.buffer.contents(), "a XY b", "replacement previewed");
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    // `:%s/foo/` — replacement field present but empty: deletion preview.
    assert_eq!(st.buffer.contents(), "a  b", "deletion previewed");
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    // `:%s/foo` — back to highlight-only on the original text.
    assert_eq!(st.buffer.contents(), "a foo b");
    let preview = st.substitute_preview.as_ref().expect("highlight-only");
    assert_eq!(preview.highlights, vec![2..5]);
}

#[test]
fn visual_range_preview_touches_only_the_selected_lines() {
    let mut st = state("foo\nfoo\nfoo");
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch(':'));
    for c in "s/foo/bar/g".chars() {
        feed(&mut vim, &mut st, ch(c));
    }
    assert_eq!(
        st.buffer.contents(),
        "bar\nbar\nfoo",
        "the preview honours the '<,'> line span"
    );
    feed(&mut vim, &mut st, esc());
    assert_eq!(st.buffer.contents(), "foo\nfoo\nfoo");
}

#[test]
fn cmdline_cursor_moves_do_not_recompute_the_preview() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/foo/XY/");
    assert_eq!(st.buffer.contents(), "XY bar");
    let version = st.buffer.version();
    // Keys that leave the command line unchanged must not revert +
    // reapply the preview (each recompute costs two full reparses).
    feed(&mut vim, &mut st, key(KeyCode::Left));
    feed(&mut vim, &mut st, key(KeyCode::Right));
    feed(&mut vim, &mut st, key(KeyCode::Home));
    assert_eq!(
        st.buffer.version(),
        version,
        "a no-op cmdline key must not touch the buffer"
    );
    assert_eq!(st.buffer.contents(), "XY bar");
}

#[test]
fn substitute_preview_suppresses_the_cursor_block_raw_reveal() {
    let mut st = state("foo bar\n");
    st.mode = Mode::Rendered;
    let mut vim = VimState::default();
    // No reveal timer armed → normally revealed; the preview parks the
    // cursor on the first affected line while the user types, so the
    // reveal must be suppressed or the block flips to raw source under
    // the preview highlights.
    st.cursor_block_entered_at = None;
    assert!(st.cursor_block_revealed());
    type_ex(&mut vim, &mut st, "%s/foo/bar/");
    assert!(st.substitute_preview.is_some(), "preview active");
    st.cursor_block_entered_at = None;
    assert!(
        !st.cursor_block_revealed(),
        "an active preview must suppress the raw reveal"
    );
    feed(&mut vim, &mut st, esc());
    st.cursor_block_entered_at = None;
    assert!(st.cursor_block_revealed(), "reveal returns after Esc");
}

#[test]
fn an_invalid_or_matchless_pattern_shows_no_preview() {
    let mut st = state("abc");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, "%s/zzz/x/");
    assert!(st.substitute_preview.is_none(), "no matches → no preview");
    assert_eq!(st.buffer.contents(), "abc");
    feed(&mut vim, &mut st, esc());
    // Half-typed group: invalid regex must not preview (and not panic).
    type_ex(&mut vim, &mut st, r"%s/a\v(");
    assert!(st.substitute_preview.is_none());
    assert_eq!(st.buffer.contents(), "abc");
}

#[test]
fn preview_shows_a_multiline_substitution_live() {
    let mut st = state("foo  \nbar  \nbaz");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, r"%s/  \n/ /g");
    assert_eq!(
        st.buffer.contents(),
        "foo bar baz",
        "the joined text shows while typing"
    );
    let preview = st.substitute_preview.as_ref().expect("preview active");
    assert_eq!(preview.highlights, vec![3..4, 7..8], "the inserted spaces");
    assert!(!st.dirty);
    // Esc puts every byte back and records no undo step.
    feed(&mut vim, &mut st, esc());
    assert_eq!(st.buffer.contents(), "foo  \nbar  \nbaz");
    assert!(st.substitute_preview.is_none());
    assert!(!st.dirty);
}

#[test]
fn preview_highlights_a_multiline_pattern_before_the_replacement() {
    // Highlight-only (no second delimiter): the ranges span the break.
    let mut st = state("foo  \nbar  \nbaz");
    let mut vim = VimState::default();
    type_ex(&mut vim, &mut st, r"%s/  \n");
    let preview = st.substitute_preview.as_ref().expect("preview active");
    assert_eq!(preview.highlights, vec![3..6, 9..12]);
    assert_eq!(st.buffer.contents(), "foo  \nbar  \nbaz", "text untouched");
    assert!(!st.dirty);
}

// ── Live `/` `?` incremental search (incsearch) ───────────────────────────────

/// Open the search prompt (`/` or `?`) and type `query` without submitting.
fn type_search(vim: &mut VimState, st: &mut EditorState, prompt: char, query: &str) {
    feed(vim, st, ch(prompt));
    for c in query.chars() {
        feed(vim, st, ch(c));
    }
}

#[test]
fn typing_a_search_highlights_live_and_parks_on_the_next_match() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    type_search(&mut vim, &mut st, '/', "foo");
    let s = st.search.as_ref().expect("live session while typing");
    assert_eq!(s.matches, vec![0..3, 8..11]);
    // Forward search focuses the first match strictly after the origin.
    assert_eq!(s.focused_range(), Some(8..11));
    assert_eq!(st.cursor.offset, 8, "cursor parked on the focus");
    assert!(vim.cmdline.is_some(), "prompt still open");
}

#[test]
fn incsearch_matches_across_a_line_break() {
    let mut st = state("foo  \nbar  \nbaz");
    let mut vim = VimState::default();
    type_search(&mut vim, &mut st, '/', r"  \n");
    let s = st.search.as_ref().expect("live session while typing");
    assert_eq!(s.matches, vec![3..6, 9..12]);
}

#[test]
fn incsearch_shows_nothing_for_a_half_typed_escape() {
    // Mid-typing, `/a\` is a trailing backslash and `/\d` an unsupported
    // escape — both highlight nothing and neither may flash an error.
    let mut st = state("a1 b2");
    let mut vim = VimState::default();
    type_search(&mut vim, &mut st, '/', r"a\");
    assert!(st.search.is_none(), "incomplete escape → no highlights");
    feed(&mut vim, &mut st, esc());
    type_search(&mut vim, &mut st, '/', r"\d");
    assert!(st.search.is_none());
    // Backspacing to something valid resumes.
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    feed(&mut vim, &mut st, ch('a'));
    assert!(st.search.is_some(), "a valid query highlights again");
}

#[test]
fn backward_search_parks_on_the_previous_match() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    st.place_cursor(8); // at the start of the second "foo"
    type_search(&mut vim, &mut st, '?', "foo");
    let s = st.search.as_ref().expect("live session while typing");
    assert_eq!(s.focused_range(), Some(0..3));
    assert_eq!(st.cursor.offset, 0);
}

#[test]
fn esc_restores_view_and_prior_hlsearch_session() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    // A prior hlsearch session is live when `/` opens.
    st.enter_search(SearchState::new("bar".to_owned(), None).expect("valid"));
    st.place_cursor(5);
    st.scroll = 1;
    type_search(&mut vim, &mut st, '/', "foo");
    assert_eq!(st.search.as_ref().map(|s| s.query.as_str()), Some("foo"));
    feed(&mut vim, &mut st, esc());
    assert_eq!(
        st.search.as_ref().map(|s| s.query.as_str()),
        Some("bar"),
        "Esc restores the prior hlsearch session"
    );
    assert_eq!(st.cursor.offset, 5, "cursor restored");
    assert_eq!(st.scroll, 1, "scroll restored");
    assert!(vim.cmdline.is_none());
    assert!(vim.incsearch.is_none());
}

#[test]
fn a_matchless_query_shows_no_highlights_and_stays_at_the_origin() {
    let mut st = state("foo bar");
    let mut vim = VimState::default();
    type_search(&mut vim, &mut st, '/', "barz");
    assert!(st.search.is_none(), "no match → no highlights");
    assert_eq!(st.cursor.offset, 0);
    // Backspacing to a matching prefix resumes the live session.
    feed(&mut vim, &mut st, key(KeyCode::Backspace));
    let s = st.search.as_ref().expect("prefix matches again");
    assert_eq!(s.focused_range(), Some(4..7));
}

#[test]
fn submit_restores_the_origin_before_the_app_level_search_runs() {
    let mut st = state("foo bar\nfoo");
    let mut vim = VimState::default();
    type_search(&mut vim, &mut st, '/', "foo");
    assert_eq!(st.cursor.offset, 8, "parked on the focus while typing");
    let out = feed(&mut vim, &mut st, key(KeyCode::Enter));
    // The reducer hands the query to the App (which runs
    // `enter_vim_search`); the incsearch session ends first, restoring
    // the origin so the App resolves the same cursor-relative focus a
    // preview-less submit would.
    assert_eq!(
        out,
        VimOutcome::EnterSearch {
            forward: true,
            query: "foo".to_owned(),
        }
    );
    assert_eq!(st.cursor.offset, 0, "origin restored for the App");
    assert!(st.search.is_none(), "transient session ended");
    assert!(vim.incsearch.is_none());
}

#[test]
fn search_history_recall_updates_the_live_session() {
    let mut st = state("foo bar");
    let mut vim = VimState {
        search_history: vec!["bar".to_owned()],
        ..Default::default()
    };
    feed(&mut vim, &mut st, ch('/'));
    assert!(st.search.is_none(), "empty prompt: nothing live yet");
    feed(&mut vim, &mut st, key(KeyCode::Up));
    let s = st.search.as_ref().expect("recalled query goes live");
    assert_eq!(s.query, "bar");
    assert_eq!(s.focused_range(), Some(4..7));
}

#[test]
fn incsearch_suppresses_the_cursor_block_raw_reveal() {
    let mut st = state("foo bar\n");
    let mut vim = VimState::default();
    st.cursor_block_entered_at = None;
    assert!(st.cursor_block_revealed());
    type_search(&mut vim, &mut st, '/', "bar");
    st.cursor_block_entered_at = None;
    assert!(
        !st.cursor_block_revealed(),
        "a live search session suppresses the raw reveal like any hlsearch"
    );
    feed(&mut vim, &mut st, esc());
    st.cursor_block_entered_at = None;
    assert!(st.cursor_block_revealed());
}

// ── Table-aware motions and commands ──────────────────────────────────────────
//
// A rendered table's `|` delimiters and alignment row are auto-managed
// chrome, so the motions that mean "move within this text" stay inside the
// cursor's cell and the line-oriented commands act on the row.  Every case
// below has a Raw-mode counterpart asserting stock vim behavior there —
// Raw is hand-editable source and gets no special treatment.

/// `| alpha | bravo |` / alignment / `| one | two |`.
const TBL: &str = "| alpha | bravo |\n|---|---|\n| one | two |\n";

/// Char offset of the first occurrence of `needle` in [`TBL`].
fn tbl_at(needle: &str) -> usize {
    TBL.find(needle).expect("needle present in the fixture")
}

/// A Rendered-mode editor on [`TBL`] with the cursor at `offset`.
fn table_state(offset: usize) -> EditorState {
    let mut st = state(TBL);
    st.cursor.offset = offset;
    st.update_cursor_block();
    st
}

#[test]
fn word_motion_stops_at_the_cell_edge() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('w'));
    assert_eq!(
        st.cursor.offset,
        tbl_at("alpha") + "alpha".len(),
        "`w` must stop at the cell's content end, not cross into `bravo`"
    );
}

#[test]
fn dollar_lands_on_the_cell_end_not_the_row_end() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len());
}

#[test]
fn caret_lands_on_the_cell_start_not_the_row_start() {
    let mut st = table_state(tbl_at("alpha") + 3);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('^'));
    assert_eq!(st.cursor.offset, tbl_at("alpha"));
}

#[test]
fn find_for_a_char_in_another_cell_does_not_move() {
    // `b` only occurs in the next cell, so this is a failed find — vim
    // leaves the cursor where it was rather than moving it partway.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('b'));
    assert_eq!(st.cursor.offset, tbl_at("alpha"));
}

#[test]
fn find_within_the_cell_still_works() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('f'));
    feed(&mut vim, &mut st, ch('h'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + 3);
}

#[test]
fn dw_never_eats_the_cell_delimiter() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    feed(&mut vim, &mut st, ch('w'));
    assert!(
        st.buffer.contents().starts_with("|  | bravo |\n"),
        "got {:?}",
        st.buffer.contents().lines().next()
    );
}

#[test]
fn capital_d_stops_at_the_cell_end() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('D'));
    assert!(
        st.buffer.contents().starts_with("|  | bravo |\n"),
        "`D` must not wipe the rest of the row's delimiters"
    );
}

#[test]
fn x_at_the_cell_end_does_not_delete_the_delimiter() {
    let mut st = table_state(tbl_at("alpha") + "alpha".len());
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('x'));
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn insert_entries_land_inside_the_cell() {
    let mut st = table_state(tbl_at("alpha") + 2);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('I'));
    assert_eq!(st.cursor.offset, tbl_at("alpha"));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);

    let mut st = table_state(tbl_at("alpha") + 2);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('A'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len());
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn o_opens_a_structural_table_row() {
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('o'));
    let contents = st.buffer.contents();
    assert_eq!(contents.lines().count(), 4, "got {contents:?}");
    assert!(
        contents.lines().nth(3).is_some_and(|l| l.contains('|')),
        "the opened row must carry the table's delimiters, not be a blank line"
    );
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn dd_deletes_the_table_row_and_fills_the_register() {
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert!(!st.buffer.contents().contains("one"));
    assert!(st.buffer.contents().contains("| alpha | bravo |"));
    assert_eq!(vim.register.text, "| one | two |\n");
    assert!(vim.register.linewise);
}

#[test]
fn dd_refuses_on_the_header_and_alignment_rows() {
    for offset in [tbl_at("alpha"), tbl_at("|---|") + 2] {
        let mut st = table_state(offset);
        let mut vim = VimState::default();
        feed(&mut vim, &mut st, ch('d'));
        let out = feed(&mut vim, &mut st, ch('d'));
        assert!(
            matches!(out, VimOutcome::Flash(_)),
            "expected a refusal flash, got {out:?}"
        );
        assert_eq!(st.buffer.contents(), TBL, "the table must be untouched");
    }
}

#[test]
fn cc_clears_the_cell_and_enters_insert() {
    let mut st = table_state(tbl_at("alpha") + 2);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('c'));
    feed(&mut vim, &mut st, ch('c'));
    assert!(
        st.buffer.contents().starts_with("|  | bravo |\n"),
        "got {:?}",
        st.buffer.contents().lines().next()
    );
    assert!(st.buffer.contents().contains("| one | two |"));
    assert_eq!(vim.sub_mode, VimSubMode::Insert);
}

#[test]
fn join_and_indent_refuse_inside_a_table() {
    for keys in [vec!['J'], vec!['>', '>'], vec!['<', '<']] {
        let mut st = table_state(tbl_at("one"));
        let mut vim = VimState::default();
        let mut out = VimOutcome::Consumed;
        for k in keys.iter() {
            out = feed(&mut vim, &mut st, ch(*k));
        }
        assert!(
            matches!(out, VimOutcome::Flash(_)),
            "expected {keys:?} to refuse inside a table, got {out:?}"
        );
        assert_eq!(st.buffer.contents(), TBL);
    }
}

#[test]
fn visual_line_delete_refuses_on_a_protected_row() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert!(
        matches!(out, VimOutcome::Flash(_)),
        "expected a refusal flash, got {out:?}"
    );
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn visual_line_delete_removes_a_data_row() {
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('d'));
    assert!(!st.buffer.contents().contains("one"));
    assert!(st.buffer.contents().contains("| alpha | bravo |"));
}

#[test]
fn document_motions_still_cross_the_table() {
    // `gg` / `G` / `}` exist to leave the current context — they are never
    // confined to a cell.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(
        st.buffer.char_to_line(st.cursor.offset),
        2,
        "`G` must reach the last row rather than clamping inside the header cell"
    );
}

#[test]
fn a_counted_line_jump_still_crosses_the_table() {
    // `{count}G` inherits `G`'s unscoped classification.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('3'));
    feed(&mut vim, &mut st, ch('G'));
    assert_eq!(st.buffer.char_to_line(st.cursor.offset), 2);
}

// ── Raw mode keeps stock vim behavior ─────────────────────────────────────────

/// A Raw-mode editor on [`TBL`] with the cursor at `offset`.
fn raw_table_state(offset: usize) -> EditorState {
    let mut st = table_state(offset);
    st.mode = Mode::Raw;
    st.update_cursor_block();
    st
}

#[test]
fn raw_mode_dd_deletes_the_raw_line() {
    // No structural protection in Raw — `dd` on the header deletes that
    // source line, exactly as it would on any other text.
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('d'));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert!(!st.buffer.contents().contains("alpha"));
}

#[test]
fn raw_mode_word_motion_crosses_the_delimiter() {
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('w'));
    assert!(
        st.cursor.offset > tbl_at("alpha") + "alpha".len(),
        "Raw mode gets no cell scoping — `w` walks onto the `|`"
    );
}

#[test]
fn raw_mode_dollar_reaches_the_row_end() {
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('$'));
    // The whole row is ordinary text in Raw, so `$` reaches its end rather
    // than stopping at the first cell's content (offset 7 in Rendered).
    assert_eq!(st.cursor.offset, "| alpha | bravo |".len());
}

#[test]
fn raw_mode_o_opens_a_plain_line() {
    let mut st = raw_table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('o'));
    assert_eq!(
        st.buffer.contents(),
        "| alpha | bravo |\n|---|---|\n| one | two |\n\n",
        "Raw `o` inserts a bare newline, not a table row"
    );
}

#[test]
fn raw_mode_join_and_indent_are_not_refused() {
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, ch('J'));
    assert_eq!(out, VimOutcome::Consumed);
    assert!(st
        .buffer
        .contents()
        .starts_with("| alpha | bravo | |---|---|"));
}

// ── Structural refusals: the routes that reach a protected row ────────────────
//
// The cell clamp shapes the ordinary keystroke; these are the ways a range
// gets to a header or alignment row *without* a cell to clamp against.  Each
// one corrupted the table silently before `range_breaks_a_table` guarded the
// two operator funnels.

/// Feed each char of `keys` and return the last outcome.
fn feed_keys(vim: &mut VimState, st: &mut EditorState, keys: &str) -> VimOutcome {
    let mut out = VimOutcome::Consumed;
    for k in keys.chars() {
        out = feed(vim, st, ch(k));
    }
    out
}

#[test]
fn a_counted_dd_cannot_delete_the_header() {
    // `2dd` on the header used to bypass the refusal entirely and leave
    // `| one | two |` standing alone as paragraph text.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "2dd");
    assert!(
        matches!(out, VimOutcome::Flash(_)),
        "expected a refusal flash, got {out:?}"
    );
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn dj_cannot_slice_the_header_off_the_table() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "dj");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn cc_cannot_blank_the_alignment_row() {
    // `cc` there fell through to a plain linewise change, which split the
    // table into two paragraphs.
    let mut st = table_state(tbl_at("|---|") + 2);
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "cc");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), TBL);
    assert_eq!(vim.sub_mode, VimSubMode::Normal, "no Insert on a refusal");
}

#[test]
fn a_visual_line_selection_leaving_the_table_still_protects_the_header() {
    // Anchor on the header, then move the cursor *out* of the table: the
    // guard keys on the selection, not on where the cursor ended up.
    let src = "para\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = src.find("alpha").expect("fixture");
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "Vkd");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), src);
}

#[test]
fn a_visual_line_selection_covering_the_whole_table_may_delete_it() {
    // Deleting a table outright is a legitimate edit — there is no
    // half-table left to be broken, so this must *not* be refused.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "Vjjd");
    assert_eq!(st.buffer.contents().trim(), "");
}

#[test]
fn a_charwise_visual_selection_stops_at_the_cell_edge() {
    // `l` steps cell-to-cell in Normal, but a charwise Visual highlight
    // that crossed the `|` would promise an edit the structural guard then
    // refuses — so here it stops on the cell's last character, however many
    // times `l` is pressed, and the delete it promised goes through.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    for _ in 0..9 {
        feed(&mut vim, &mut st, ch('l'));
    }
    assert_eq!(
        st.cursor.offset,
        tbl_at("alpha") + "alpha".len() - 1,
        "the cursor must rest on the cell's last char, not the append slot"
    );
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed, "the highlight was all in-cell");
    assert_eq!(
        st.buffer.contents(),
        "|  | bravo |\n|---|---|\n| one | two |\n"
    );
}

/// The mirror of the above: `h` can't reverse out of the cell either.
#[test]
fn a_charwise_visual_selection_stops_at_the_cell_start() {
    let mut st = table_state(tbl_at("bravo") + 2);
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    for _ in 0..9 {
        feed(&mut vim, &mut st, ch('h'));
    }
    assert_eq!(st.cursor.offset, tbl_at("bravo"));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| alpha | vo |\n|---|---|\n| one | two |\n"
    );
}

/// `$` parks on the cell's append slot — the padding space before the `|`.
/// A charwise selection opened there covers that space, and the edit takes
/// it: the guard permits the range (it measures the cell untrimmed), so `d`
/// would leave `alpha` abutting its delimiter.  `v` anchors one grapheme
/// back instead.
#[test]
fn opening_visual_on_the_cell_append_slot_anchors_on_the_last_char() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len());
    feed(&mut vim, &mut st, ch('v'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len() - 1);
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| alph | bravo |\n|---|---|\n| one | two |\n"
    );
}

/// `V`→`v` is the other door into charwise Visual, and it inherits a cursor
/// `$` may have parked on the append slot.  The span here runs from the
/// cell's first char (where `V` anchored) to its last, so `d` clears the
/// content and leaves both padding spaces — without the pull-back it would
/// take the trailing one too and leave `| | bravo |`.
#[test]
fn toggling_visual_line_to_charwise_pulls_the_cursor_off_the_append_slot() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len());
    feed(&mut vim, &mut st, ch('v'));
    assert_eq!(st.cursor.offset, tbl_at("alpha") + "alpha".len() - 1);
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "|  | bravo |\n|---|---|\n| one | two |\n"
    );
}

/// The same toggle, with `r` — which overwrites rather than deletes, so an
/// unpulled cursor would replace the padding space itself and leave the
/// cell's text touching the `|`.
#[test]
fn toggling_visual_line_to_charwise_protects_the_padding_from_replace() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('$'));
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('r'));
    let out = feed(&mut vim, &mut st, ch('z'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| zzzzz | bravo |\n|---|---|\n| one | two |\n",
        "the space before the delimiter survives"
    );
}

/// `o` puts the append slot on the *anchor* end instead, so the pull-back
/// has to reach both ends of the span — each against its own cell.
#[test]
fn toggling_visual_line_to_charwise_pulls_the_anchor_too() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('V'));
    feed(&mut vim, &mut st, ch('$'));
    feed(&mut vim, &mut st, ch('o')); // anchor ← append slot, cursor ← cell start
    feed(&mut vim, &mut st, ch('v'));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "|  | bravo |\n|---|---|\n| one | two |\n"
    );
}

/// The clamp is horizontal only, so the range guard is still the thing that
/// catches a selection which left the cell by another route — here `j`,
/// which steps to the cell below and drags the row's `|`s and newlines into
/// the span.
#[test]
fn a_charwise_visual_delete_reaching_another_row_is_refused() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('j'));
    let out = feed(&mut vim, &mut st, ch('d'));
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn a_charwise_visual_delete_inside_one_cell_still_works() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "vlld");
    assert!(
        st.buffer.contents().starts_with("| ha | bravo |\n"),
        "got {:?}",
        st.buffer.contents().lines().next()
    );
}

#[test]
fn a_visual_replace_cannot_overwrite_a_delimiter() {
    // `l` can no longer carry the selection out of the cell, so the span
    // that reaches a delimiter comes from `j` — the vertical step the cell
    // clamp deliberately leaves alone.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('v'));
    feed(&mut vim, &mut st, ch('j'));
    feed(&mut vim, &mut st, ch('r'));
    let out = feed(&mut vim, &mut st, ch('z'));
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn yy_on_a_protected_row_is_never_refused() {
    // Yank mutates nothing, so the guard must not fire on it.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "yy");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(vim.register.text, "| alpha | bravo |\n");
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn join_refuses_on_the_line_above_a_table() {
    // `J` there merges the paragraph onto the header row.
    let src = "para\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, ch('J'));
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), src);
}

// ── Paste ─────────────────────────────────────────────────────────────────────

#[test]
fn a_yanked_row_pasted_on_the_header_lands_below_the_alignment_row() {
    // `dd` fills the register with a data row; the ordinary linewise paste
    // would drop it between the header and the alignment row.
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "dd");
    st.cursor.offset = tbl_at("alpha");
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(
        st.buffer.contents(),
        "| alpha | bravo |\n|---|---|\n| one | two |\n",
        "the row must come back below the alignment row"
    );
}

#[test]
fn pasting_prose_into_a_table_is_refused() {
    // Yank a paragraph line, then try to drop it between two table rows.
    let src = "para\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "yy");
    st.cursor.offset = src.find("one").expect("fixture");
    st.update_cursor_block();
    let out = feed(&mut vim, &mut st, ch('p'));
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), src);
}

#[test]
fn raw_mode_paste_keeps_the_plain_linewise_landing_spot() {
    // No table awareness in Raw: yank the data row, then `p` on the header
    // drops it right below, above the alignment row, as stock vim would.
    let mut st = raw_table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "yy");
    st.cursor.offset = tbl_at("alpha");
    st.update_cursor_block();
    feed(&mut vim, &mut st, ch('p'));
    assert_eq!(
        st.buffer.contents(),
        "| alpha | bravo |\n| one | two |\n|---|---|\n| one | two |\n"
    );
}

#[test]
fn raw_mode_counted_dd_deletes_the_raw_lines() {
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "2dd");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "| one | two |\n");
}

#[test]
fn a_multibyte_cell_clamps_on_char_boundaries() {
    // `cell_scope` converts `table_edit`'s byte offsets to char offsets;
    // a cell of multibyte text is where an off-by-a-byte would panic or
    // land the cursor mid-character.
    let src = "| héllo wörld | b |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 2; // on the `h`
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('$'));
    assert_eq!(st.cursor.offset, 13, "`$` stops past the cell's last char");

    // `D` from the cell's first char clears the cell and nothing else.
    let mut st = state(src);
    st.cursor.offset = 2;
    st.update_cursor_block();
    let mut vim = VimState::default();
    feed(&mut vim, &mut st, ch('D'));
    assert_eq!(
        st.buffer.contents(),
        "|  | b |\n|---|---|\n| one | two |\n",
        "`D` clears the cell without touching the delimiters"
    );
}

// ── The guard must not over-refuse ────────────────────────────────────────────

#[test]
fn a_counted_dd_over_data_rows_only_is_allowed() {
    let src = "| a | b |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |\n";
    let mut st = state(src);
    st.cursor.offset = src.find('1').expect("fixture");
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "2dd");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| a | b |\n|---|---|",
        "both data rows go; the last line takes the trailing newline with it"
    );
}

#[test]
fn ordinary_cell_edits_are_never_refused() {
    // `diw`, `ciw`, `x` and `dw` all stay inside the cell, so none of them
    // may trip the structural guard.
    for (keys, expected) in [
        ("diw", "|  | bravo |"),
        ("ciw", "|  | bravo |"),
        ("dw", "|  | bravo |"),
        ("x", "| lpha | bravo |"),
    ] {
        let mut st = table_state(tbl_at("alpha"));
        let mut vim = VimState::default();
        let out = feed_keys(&mut vim, &mut st, keys);
        assert_eq!(out, VimOutcome::Consumed, "{keys} must not be refused");
        assert_eq!(
            st.buffer.contents().lines().next(),
            Some(expected),
            "{keys} left the wrong row"
        );
    }
}

#[test]
fn the_alignment_row_stays_hand_editable() {
    // No cell scope there, and `x` must keep working so a user can retype
    // the dashes — only whole-row loss is refused.
    let mut st = table_state(tbl_at("|---|") + 2);
    let mut vim = VimState::default();
    let out = feed(&mut vim, &mut st, ch('x'));
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| alpha | bravo |\n|--|---|\n| one | two |\n"
    );
}

#[test]
fn edits_outside_a_table_are_untouched_by_the_guard() {
    let src = "para one\npara two\n\n| a | b |\n|---|---|\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "dd");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), "para two\n\n| a | b |\n|---|---|\n");
}

// ── Register shape vs. selection shape (Visual `p`) ───────────────────────────
//
// A Visual paste reconciles the register's shape with the selection's before
// it writes, so the guard has to be asked about the payload as it will land.
// Asking about the register's own flags let each mismatch through.

#[test]
fn a_row_register_cannot_be_pasted_into_a_cell() {
    // `dd` fills the register linewise with a whole row; dropping that over a
    // charwise selection spliced the `|` and the newline into mid-cell.
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "dd");
    assert!(vim.register.linewise);
    let remaining = st.buffer.contents();

    st.cursor.offset = tbl_at("alpha");
    st.update_cursor_block();
    let out = feed_keys(&mut vim, &mut st, "vlp");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), remaining);
}

#[test]
fn a_prose_register_cannot_replace_a_whole_row() {
    // The mirror case: a charwise register grows a trailing newline when it
    // lands over a VisualLine selection, so the row it replaces stops being a
    // table row at all.
    let src = "para\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    // Yank `para` charwise, then select the data row linewise and paste.
    feed_keys(&mut vim, &mut st, "v$y");
    assert!(!vim.register.linewise);
    st.cursor.offset = src.find("one").expect("fixture");
    st.update_cursor_block();
    let out = feed_keys(&mut vim, &mut st, "Vp");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), src);
}

#[test]
fn a_row_register_still_replaces_a_selected_row() {
    // The shapes agree here, so the paste must go through.
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "yy");
    let out = feed_keys(&mut vim, &mut st, "Vp");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn ordinary_text_still_pastes_inside_a_cell() {
    // A charwise register over a charwise selection only widens the cell.
    let mut st = table_state(tbl_at("one"));
    let mut vim = VimState::default();
    feed_keys(&mut vim, &mut st, "vly"); // yank "on"
    assert!(!vim.register.linewise);
    st.cursor.offset = tbl_at("alpha");
    st.update_cursor_block();
    let out = feed_keys(&mut vim, &mut st, "vp");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| onlpha | bravo |\n|---|---|\n| one | two |\n"
    );
}

// ── Normal-mode `r{c}` ────────────────────────────────────────────────────────

#[test]
fn a_counted_replace_cannot_overwrite_a_delimiter() {
    // `7rx` on `alpha` reached past the cell and turned `alpha |` into
    // `xxxxxxx`, leaving a one-cell header over a two-cell alignment row.
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "7rx");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), TBL);
}

#[test]
fn a_replace_inside_the_cell_still_works() {
    let mut st = table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "3rx");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| xxxha | bravo |\n|---|---|\n| one | two |\n"
    );
}

#[test]
fn raw_mode_counted_replace_is_unscoped() {
    // Raw is hand-editable source: no table awareness, so the delimiter is
    // just another character.
    let mut st = raw_table_state(tbl_at("alpha"));
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "7rx");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "| xxxxxxx bravo |\n|---|---|\n| one | two |\n"
    );
}

// ── The `J` guard's span ──────────────────────────────────────────────────────

#[test]
fn a_counted_join_that_never_reaches_the_table_is_allowed() {
    // `2J` makes one join, exactly as bare `J` does, so it stops a line short
    // of the header — the guard used to over-reach by one line and refuse it.
    let src = "para one\npara two\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "2J");
    assert_eq!(out, VimOutcome::Consumed);
    assert_eq!(
        st.buffer.contents(),
        "para one para two\n| alpha | bravo |\n|---|---|\n| one | two |\n"
    );
}

#[test]
fn a_counted_join_that_does_reach_the_table_is_refused() {
    // `3J` makes two joins, so it pulls the header row up onto the prose.
    let src = "para one\npara two\n| alpha | bravo |\n|---|---|\n| one | two |\n";
    let mut st = state(src);
    st.cursor.offset = 0;
    st.update_cursor_block();
    let mut vim = VimState::default();
    let out = feed_keys(&mut vim, &mut st, "3J");
    assert!(matches!(out, VimOutcome::Flash(_)), "got {out:?}");
    assert_eq!(st.buffer.contents(), src);
}