reedline 0.50.0

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

/// Stateful editor executing changes to the underlying [`LineBuffer`]
///
/// In comparison to the state-less [`LineBuffer`] the [`Editor`] keeps track of
/// the undo/redo history and has facilities for cut/copy/yank/paste
pub struct Editor {
    line_buffer: LineBuffer,
    cut_buffer: Box<dyn Clipboard>,
    #[cfg(feature = "system_clipboard")]
    system_clipboard: Box<dyn Clipboard>,
    edit_stack: EditStack<LineBuffer>,
    last_undo_behavior: UndoBehavior,
    edit_mode: PromptEditMode,
    /// Set when [`sync_edit_mode`](Self::sync_edit_mode) adopts a new rest
    /// policy without committing the cursor; cleared at the commit boundary.
    /// Lets the pre-paint sweep settle a command-less mode transition that
    /// would otherwise never re-normalize under the new policy.
    policy_unsettled: bool,
    /// When `true`, a grapheme left/right motion under a block caret (vi normal)
    /// crosses line terminators — `l` at a line's end lands on the next line's
    /// first grapheme, `h` at column 0 on the previous line's last. When `false`
    /// the motion is clamped to the current line (vim's default `h`/`l`). Bar
    /// carets (emacs, vi insert) always cross regardless, since a bar may rest in
    /// the gap around a `\n`. Defaults to `true`.
    cross_line_cursor: bool,
}

enum OperatorVerb {
    Cut,
    Copy,
    /// Cut, but a `LineWise` span keeps its line terminators so one blank line
    /// remains — vi's change operator (`cc`/`cj`/`cgg`). Identical to `Cut`
    /// for `CharWise` spans.
    Change,
    Erase,
}

/// Build a word [`MotionTarget`] — the shared shape the legacy `*Word*` command
/// sugar lowers to. The emacs-flavored bindings pass [`WordKind::Unicode`]
/// (UAX-29, proven equivalent to the old `*_index` scans); the big-WORD sugar
/// passes [`WordKind::LongWord`].
fn word_target(kind: WordKind, edge: WordEdge, direction: Direction) -> MotionTarget {
    MotionTarget::Word {
        kind,
        edge,
        direction,
    }
}

impl Default for Editor {
    fn default() -> Self {
        Editor {
            line_buffer: LineBuffer::new(),
            cut_buffer: get_local_clipboard(),
            #[cfg(feature = "system_clipboard")]
            system_clipboard: get_system_clipboard(),
            edit_stack: EditStack::new(),
            last_undo_behavior: UndoBehavior::CreateUndoPoint,
            edit_mode: PromptEditMode::Default,
            policy_unsettled: false,
            cross_line_cursor: true,
        }
    }
}

impl Editor {
    /// Get the current [`LineBuffer`]
    pub const fn line_buffer(&self) -> &LineBuffer {
        &self.line_buffer
    }

    /// Mutable [`LineBuffer`] access, no undo.
    pub(crate) fn line_buffer_mut(&mut self) -> &mut LineBuffer {
        &mut self.line_buffer
    }

    /// Set the [`LineBuffer`] with undo behavior.
    pub(crate) fn set_line_buffer(&mut self, line_buffer: LineBuffer, undo_behavior: UndoBehavior) {
        self.line_buffer = line_buffer;
        self.update_undo_state(undo_behavior);
    }

    pub(crate) fn run_edit_command(&mut self, command: &EditCommand) {
        match command {
            EditCommand::MoveToStart { select } => self.move_to_start(*select),
            EditCommand::MoveToLineStart { select } => self.move_to_line_start(*select),
            EditCommand::MoveToLineNonBlankStart { select } => {
                self.move_to_line_non_blank_start(*select)
            }
            EditCommand::MoveToEnd { select } => self.move_to_end(*select),
            EditCommand::MoveToLineEnd { select } => self.move_to_line_end(*select),
            EditCommand::MoveToPosition { position, select } => {
                self.move_head_to(*position, *select)
            }
            EditCommand::MoveLineUp { select } => self.move_line_up(*select),
            EditCommand::MoveLineDown { select } => self.move_line_down(*select),
            EditCommand::MoveLeft { select } => self.move_left(*select),
            EditCommand::MoveRight { select } => self.move_right(*select),
            EditCommand::MoveWordLeft { select } => self.move_word_left(*select),
            EditCommand::MoveBigWordLeft { select } => self.move_big_word_left(*select),
            EditCommand::MoveWordRight { select } => self.move_word_right(*select),
            EditCommand::MoveWordRightStart { select } => self.move_word_right_start(*select),
            EditCommand::MoveBigWordRightStart { select } => {
                self.move_big_word_right_start(*select)
            }
            EditCommand::MoveWordRightEnd { select } => self.move_word_right_end(*select),
            EditCommand::MoveBigWordRightEnd { select } => self.move_big_word_right_end(*select),
            EditCommand::Move(t) => {
                let head = self.resolve_head(*t);
                self.move_head_to(head, false);
            }
            // A destination-shaped target has no travel direction for `Span`'s
            // `op_end` to bake inclusivity from: whether its landing grapheme is
            // covered turns on which side of the *anchor* it falls, which only
            // `put_cursor` can see. Helix lowers its own (`gs`) through
            // `put_cursor` in select mode too, so take that path either way.
            EditCommand::Extend(t) if t.direction().is_none() => {
                let head = self.resolve_head(*t);
                self.move_head_to(head, true);
            }
            EditCommand::Extend(t) => match self.caret_extent() {
                SelectionExtent::CoverLanding => {
                    let head = self.resolve_head(*t);
                    self.move_head_to(head, true);
                }
                SelectionExtent::Span => {
                    let geom = self.caret_geometry();
                    let origin = self.motion_origin(*t);
                    let op_end = resolve_motion(self.get_buffer(), origin, *t, geom).op_end;
                    let next =
                        self.line_buffer
                            .cursor()
                            .extend_span(self.get_buffer(), op_end, geom);
                    self.place(next);
                }
            },
            EditCommand::Select(t) => match self.caret_extent() {
                SelectionExtent::CoverLanding => {
                    let origin = self.insertion_point();
                    self.place(Cursor::point(origin));
                    let head = self.resolve_head(*t);
                    self.move_head_to(head, true);
                }
                SelectionExtent::Span => {
                    let geom = self.caret_geometry();
                    let origin = self.insertion_point();
                    let selection = resolve_selection(self.get_buffer(), origin, *t, geom);
                    let selection =
                        if self.edit_mode.rest_policy().is_block() || selection.is_empty() {
                            selection
                        } else {
                            let buf = self.get_buffer();
                            if selection.head() >= selection.anchor() {
                                selection.move_head(prev_grapheme_boundary(buf, selection.head()))
                            } else {
                                Cursor::new(
                                    prev_grapheme_boundary(buf, selection.anchor()),
                                    selection.head(),
                                )
                            }
                        };
                    self.place(selection);
                }
            },
            EditCommand::CollapseSelection(direction) => {
                let cursor = self.line_buffer.cursor();
                let pos = match direction {
                    Direction::Backward => cursor.start(),
                    Direction::Forward => cursor.end(),
                };
                self.place(Cursor::point(pos));
            }
            EditCommand::Cut {
                target,
                granularity,
            } => {
                let sel = operator_span(
                    self.get_buffer(),
                    self.insertion_point(),
                    *target,
                    self.caret_geometry(),
                );
                self.operate(sel, OperatorVerb::Cut, *granularity);
            }
            EditCommand::Copy {
                target,
                granularity,
            } => {
                let sel = operator_span(
                    self.get_buffer(),
                    self.insertion_point(),
                    *target,
                    self.caret_geometry(),
                );
                self.operate(sel, OperatorVerb::Copy, *granularity);
            }
            EditCommand::Change {
                target,
                granularity,
            } => {
                let sel = operator_span(
                    self.get_buffer(),
                    self.insertion_point(),
                    *target,
                    self.caret_geometry(),
                );
                self.operate(sel, OperatorVerb::Change, *granularity);
            }
            EditCommand::Erase(t) => {
                let sel = operator_span(
                    self.get_buffer(),
                    self.insertion_point(),
                    *t,
                    self.caret_geometry(),
                );
                self.operate(sel, OperatorVerb::Erase, Granularity::CharWise);
            }
            EditCommand::InsertChar(c) => self.insert_char(*c),
            EditCommand::Complete => {}
            EditCommand::InsertString(str) => self.insert_str(str),
            EditCommand::InsertNewline => self.insert_newline(),
            EditCommand::InsertNewlineAbove => self.insert_newline_above(),
            EditCommand::InsertNewlineBelow => self.insert_newline_below(),
            EditCommand::ReplaceChar(chr) => self.replace_char(*chr),
            EditCommand::ReplaceChars(n_chars, str) => self.replace_chars(*n_chars, str),
            EditCommand::Backspace => self.backspace(),
            EditCommand::Delete => self.delete(),
            EditCommand::CutChar => self.cut_char(),
            EditCommand::CutCharLeft => self.cut_char_left(),
            EditCommand::BackspaceWord => self.line_buffer.delete_word_left(),
            EditCommand::DeleteWord => self.line_buffer.delete_word_right(),
            EditCommand::Clear => self.line_buffer.clear(),
            EditCommand::ClearToLineEnd => self.line_buffer.clear_to_line_end(),
            EditCommand::CutCurrentLine => self.cut_current_line(),
            EditCommand::CutFromStart => self.cut_from_start(),
            EditCommand::CutFromStartLinewise { leave_blank_line } => {
                self.cut_from_start_linewise(*leave_blank_line)
            }
            EditCommand::CutFromLineStart => self.cut_from_line_start(),
            EditCommand::CutFromLineNonBlankStart => self.cut_from_line_non_blank_start(),
            EditCommand::CutToEnd => self.cut_from_end(),
            EditCommand::CutToEndLinewise { leave_blank_line } => {
                self.cut_from_end_linewise(*leave_blank_line)
            }
            EditCommand::CutToLineEnd => self.cut_to_line_end(),
            EditCommand::KillLine => self.kill_line(),
            EditCommand::CutWordLeft => self.cut_word_left(),
            EditCommand::CutBigWordLeft => self.cut_big_word_left(),
            EditCommand::CutWordRight => self.cut_word_right(),
            EditCommand::CutBigWordRight => self.cut_big_word_right(),
            EditCommand::CutWordRightToNext => self.cut_word_right_to_next(),
            EditCommand::CutBigWordRightToNext => self.cut_big_word_right_to_next(),
            EditCommand::PasteCutBufferBefore => self.insert_cut_buffer_before(),
            EditCommand::PasteCutBufferAfter => self.insert_cut_buffer_after(),
            EditCommand::PasteAtSelectionEdge { direction, count } => {
                self.paste_at_selection_edge(*direction, *count)
            }
            EditCommand::UppercaseWord => self.line_buffer.uppercase_word(),
            EditCommand::LowercaseWord => self.line_buffer.lowercase_word(),
            EditCommand::SwitchcaseChar => self.line_buffer.switchcase_char(),
            EditCommand::CapitalizeChar => self.line_buffer.capitalize_char(),
            EditCommand::SwapWords => self.line_buffer.swap_words(),
            EditCommand::SwapGraphemes => self.line_buffer.swap_graphemes(),
            EditCommand::Undo => self.undo(),
            EditCommand::Redo => self.redo(),
            EditCommand::CutRightUntil(c) => self.cut_right_until_char(*c, false, true),
            EditCommand::CutRightBefore(c) => self.cut_right_until_char(*c, true, true),
            EditCommand::MoveRightUntil { c, select } => {
                self.move_right_until_char(*c, false, true, *select)
            }
            EditCommand::MoveRightBefore { c, select } => {
                self.move_right_until_char(*c, true, true, *select)
            }
            EditCommand::CutLeftUntil(c) => self.cut_left_until_char(*c, false, true),
            EditCommand::CutLeftBefore(c) => self.cut_left_until_char(*c, true, true),
            EditCommand::MoveLeftUntil { c, select } => {
                self.move_left_until_char(*c, false, true, *select)
            }
            EditCommand::MoveLeftBefore { c, select } => {
                self.move_left_until_char(*c, true, true, *select)
            }
            EditCommand::SelectAll => self.select_all(),
            #[cfg(feature = "helix")]
            EditCommand::SelectLine => self.select_line(),
            EditCommand::CutSelection { granularity } => {
                self.cut_selection_to_cut_buffer(*granularity)
            }
            #[cfg(feature = "helix")]
            EditCommand::EraseSelection => self.erase_selection(),
            EditCommand::CopySelection => self.copy_selection_to_cut_buffer(),
            EditCommand::LowercaseSelection => self.lowercase_selection(),
            EditCommand::UppercaseSelection => self.uppercase_selection(),
            EditCommand::SwitchcaseSelection => self.switchcase_selection(),
            EditCommand::Paste => self.paste_cut_buffer(),
            EditCommand::CopyFromStart => self.copy_from_start(),
            EditCommand::CopyFromStartLinewise => self.copy_from_start_linewise(),
            EditCommand::CopyFromLineStart => self.copy_from_line_start(),
            EditCommand::CopyFromLineNonBlankStart => self.copy_from_line_non_blank_start(),
            EditCommand::CopyToEnd => self.copy_from_end(),
            EditCommand::CopyToEndLinewise => self.copy_from_end_linewise(),
            EditCommand::CopyToLineEnd => self.copy_to_line_end(),
            EditCommand::CopyWordLeft => self.copy_word_left(),
            EditCommand::CopyBigWordLeft => self.copy_big_word_left(),
            EditCommand::CopyWordRight => self.copy_word_right(),
            EditCommand::CopyBigWordRight => self.copy_big_word_right(),
            EditCommand::CopyWordRightToNext => self.copy_word_right_to_next(),
            EditCommand::CopyBigWordRightToNext => self.copy_big_word_right_to_next(),
            EditCommand::CopyRightUntil(c) => self.copy_right_until_char(*c, false, true),
            EditCommand::CopyRightBefore(c) => self.copy_right_until_char(*c, true, true),
            EditCommand::CopyLeftUntil(c) => self.copy_left_until_char(*c, false, true),
            EditCommand::CopyLeftBefore(c) => self.copy_left_until_char(*c, true, true),
            EditCommand::CopyCurrentLine => {
                let range = self.line_buffer.current_line_range();
                let copy_slice = &self.line_buffer.get_buffer()[range];
                if !copy_slice.is_empty() {
                    self.cut_buffer.set(copy_slice, Granularity::LineWise);
                }
            }
            EditCommand::CopyLeft => {
                let insertion_offset = self.line_buffer.insertion_point();
                if insertion_offset > 0 {
                    let left_index = self.line_buffer.grapheme_left_index();
                    let copy_range = left_index..insertion_offset;
                    self.cut_buffer.set(
                        &self.line_buffer.get_buffer()[copy_range],
                        Granularity::CharWise,
                    );
                }
            }
            EditCommand::CopyRight => {
                let insertion_offset = self.line_buffer.insertion_point();
                let right_index = self.line_buffer.grapheme_right_index();
                if right_index > insertion_offset {
                    let copy_range = insertion_offset..right_index;
                    self.cut_buffer.set(
                        &self.line_buffer.get_buffer()[copy_range],
                        Granularity::CharWise,
                    );
                }
            }
            EditCommand::SwapCursorAndAnchor => self
                .line_buffer
                .set_cursor(self.line_buffer.cursor().flip()),
            #[cfg(feature = "system_clipboard")]
            EditCommand::CutSelectionSystem => self.cut_selection_to_system(),
            #[cfg(feature = "system_clipboard")]
            EditCommand::CopySelectionSystem => self.copy_selection_to_system(),
            #[cfg(feature = "system_clipboard")]
            EditCommand::PasteSystem => self.paste_from_system(),
            EditCommand::CutInsidePair { left, right } => self.cut_inside_pair(*left, *right),
            EditCommand::CopyInsidePair { left, right } => self.copy_inside_pair(*left, *right),
            EditCommand::CutAroundPair { left, right } => self.cut_around_pair(*left, *right),
            EditCommand::CopyAroundPair { left, right } => self.copy_around_pair(*left, *right),
            EditCommand::CutTextObject { text_object } => self.cut_text_object(*text_object),
            EditCommand::CopyTextObject { text_object } => self.copy_text_object(*text_object),
        }
        let leaves_selection = matches!(command.edit_type(), EditType::MoveCursor { select: true })
            || matches!(command, EditCommand::PasteAtSelectionEdge { .. })
            || (matches!(
                command,
                EditCommand::CopySelection
                    | EditCommand::LowercaseSelection
                    | EditCommand::UppercaseSelection
                    | EditCommand::SwitchcaseSelection
            ) && self.edit_mode.retains_selection_after_edit());
        if !leaves_selection {
            self.clear_selection();
        }

        self.commit_cursor();

        let new_undo_behavior = match (command, command.edit_type()) {
            (_, EditType::MoveCursor { .. }) => UndoBehavior::MoveCursor,
            (EditCommand::InsertChar(c), EditType::EditText) => UndoBehavior::InsertCharacter(*c),
            (EditCommand::Delete, EditType::EditText) => {
                let deleted_char = self.edit_stack.current().grapheme_right().chars().next();
                UndoBehavior::Delete(deleted_char)
            }
            (EditCommand::Backspace, EditType::EditText) => {
                let deleted_char = self.edit_stack.current().grapheme_left().chars().next();
                UndoBehavior::Backspace(deleted_char)
            }
            (_, EditType::UndoRedo | EditType::NoOp) => UndoBehavior::NoOp,
            (_, _) => UndoBehavior::CreateUndoPoint,
        };

        self.update_undo_state(new_undo_behavior);
    }

    pub(crate) fn clear_selection(&mut self) {
        // Collapse to the caret (the visible position), not merely drop the
        // anchor: under `Block` the stored head sits on the far edge, so dropping
        // the anchor alone would strand the cursor one grapheme past where it
        // shows. Collapsing to `point(caret)` keeps it put; the commit boundary
        // re-widens it under the active policy.
        let caret = self.line_buffer.insertion_point();
        self.line_buffer.set_cursor(Cursor::point(caret));
    }

    fn operate(&mut self, selection: Cursor, verb: OperatorVerb, granularity: Granularity) {
        // `register` is the span the cut buffer keeps; `delete` is the span that
        // leaves the buffer. They coincide except for a linewise Cut/Copy of the
        // *last* line: the deletion eats the preceding terminator so no blank line
        // is stranded, but the register must hold only the line's content —
        // otherwise a later linewise paste re-introduces that newline as a
        // spurious leading blank line.
        let (register, delete) = match granularity {
            Granularity::CharWise => {
                let r = selection.start()..selection.end();
                (r.clone(), r)
            }
            Granularity::LineWise => {
                let buf = self.get_buffer();
                let s = line::start_of_line(buf, selection.start());
                match verb {
                    // Change keeps the line terminators: only the lines'
                    // content is consumed, so one blank line remains for the
                    // re-entered insert mode. Register and deletion coincide.
                    OperatorVerb::Change => {
                        let r = s..line::end_of_line(buf, selection.end());
                        (r.clone(), r)
                    }
                    // Cut/Copy/Erase consume whole lines including the trailing
                    // `\n`. On the last line (no trailing `\n`) the *deletion*
                    // eats the whole preceding terminator instead so no stray
                    // blank line is left — 2 bytes for a `\r\n`, so the `\r` is
                    // not orphaned (e.g. a CRLF history entry "ab\r\ncd" + `dd`
                    // → "ab"; the buffer can carry CR, see `LineBuffer`'s
                    // line-ending contract). The *register* keeps just `s..e` so
                    // a later linewise paste does not gain a leading blank line.
                    _ => {
                        let e = line::start_of_next_line(buf, selection.end()).unwrap_or(buf.len());
                        let delete_start = if e == buf.len() && s > 0 {
                            if buf[..s].ends_with("\r\n") {
                                s - 2
                            } else {
                                s - 1
                            }
                        } else {
                            s
                        };
                        (s..e, delete_start..e)
                    }
                }
            }
        };

        match verb {
            OperatorVerb::Cut => {
                self.copy_range_with(register, granularity);
                self.line_buffer.clear_range_safe(delete.clone());
                self.line_buffer.set_insertion_point(delete.start);
            }
            // Change's register and deletion coincide, so one range suffices.
            OperatorVerb::Change => self.cut_range_with(delete, granularity),
            OperatorVerb::Copy => self.copy_range_with(register, granularity),
            OperatorVerb::Erase => {
                self.line_buffer.clear_range_safe(delete.clone());
                self.line_buffer.set_insertion_point(delete.start);
            }
        }
    }

    /// Plant or clear a selection anchor explicitly — retained only for test
    /// setup. Production selections open through [`move_head_to`](Self::move_head_to)
    /// (`put_cursor`) or the `Block` min-width-1 commit, neither of which needs
    /// this.
    #[cfg(test)]
    fn update_selection_anchor(&mut self, select: bool) {
        if select {
            if self.line_buffer.selection_anchor().is_none() {
                self.line_buffer
                    .set_selection_anchor(Some(self.insertion_point()));
            }
        } else {
            self.clear_selection();
        }
    }

    /// Set the current edit mode
    pub fn set_edit_mode(&mut self, mode: PromptEditMode) {
        // Called on every repaint, so skip the work when nothing relevant moved.
        // `commit_cursor` depends only on the rest policy, and the cursor is
        // already committed under the old one; re-normalize only when the policy
        // actually changes (e.g. Vi insert → normal tightens to `OnGrapheme`).
        let policy_changed = mode.rest_policy() != self.edit_mode.rest_policy();
        self.edit_mode = mode;
        // `sync_edit_mode` may have already adopted this policy without
        // committing (a command-less transition), so `policy_changed` can read
        // false here even though the cursor still owes a settle.
        if policy_changed || self.policy_unsettled {
            self.commit_cursor();
        }
    }

    /// Whether a rest-policy change is awaiting a commit (see
    /// [`policy_unsettled`](Self::policy_unsettled) field).
    pub(crate) fn policy_unsettled(&self) -> bool {
        self.policy_unsettled
    }

    /// Set whether a block-caret left/right motion crosses line terminators (see
    /// the [`cross_line_cursor`](Self::cross_line_cursor) field).
    pub(crate) fn set_cross_line_cursor(&mut self, cross: bool) {
        self.cross_line_cursor = cross;
    }

    /// Adopt `mode`'s rest policy *without* committing the cursor.
    ///
    /// Called at the parse seam, before the events a mode transition emitted
    /// are run, so those commands resolve under the new [`RestPolicy`] (e.g.
    /// the Esc→normal grapheme step-back reads `OnGrapheme`). The cursor is
    /// deliberately left where insert mode put it: the emitted commands move
    /// and commit it under the new policy, and any no-command transition is
    /// settled by the pre-paint `set_edit_mode`. Committing here would pull a
    /// caret at the line end back a grapheme, double-stepping the Esc move.
    pub fn sync_edit_mode(&mut self, mode: PromptEditMode) {
        if mode.rest_policy() != self.edit_mode.rest_policy() {
            self.policy_unsettled = true;
        }
        self.edit_mode = mode;
    }

    /// Normalize the cursor at the single commit boundary: clamp + grapheme-snap
    /// (universal), then apply the active mode's [`RestPolicy`]. Total and
    /// idempotent, so it is safe to call after any state change — including ones
    /// that move the cursor outside the command path (e.g. history navigation),
    /// which must still settle a vi-normal caret off the line end.
    pub(crate) fn commit_cursor(&mut self) {
        let committed = commit(
            self.line_buffer.get_buffer(),
            self.line_buffer.cursor(),
            self.edit_mode.rest_policy(),
        );
        self.line_buffer.set_cursor(committed);
        self.policy_unsettled = false;
    }

    /// Plain head move with no `put_cursor` geometry — retained only for test
    /// setup. Production motions route through [`move_head_to`](Self::move_head_to)
    /// so selections get the inclusive anchor-flip.
    #[cfg(test)]
    fn move_to_position(&mut self, position: usize, select: bool) {
        self.line_buffer.move_head(position, select);
    }

    /// Resolve a motion target to the byte the caret should land on.
    ///
    /// The origin is always the visible caret (Helix `move_horizontally` does the
    /// same: `range.cursor()` is the origin for both Move and Extend). The
    /// returned target is fed to [`Cursor::put_cursor`], which places the head and
    /// flips the anchor as needed — so there is no head-vs-caret origin split.
    fn resolve_head(&self, target: MotionTarget) -> usize {
        let buf = self.line_buffer.get_buffer();
        // Origin is the visible cursor position — `insertion_point()` already
        // resolves that per policy (head for Between, caret for Block).
        let origin = self.insertion_point();
        let head = resolve_motion(buf, origin, target, self.caret_geometry()).head;
        // Only a block-caret grapheme step needs a line policy at the edges; every
        // other target's line-crossing is already fixed by `resolve_motion`, and a
        // bar caret (`Between`) moves freely across the terminator either way.
        // `BlockOverNewline` opts out: the terminator is a cell it may rest on, so
        // the raw step is already the landing and clamping or skipping would put
        // the newline out of reach.
        if let MotionTarget::Grapheme(direction) = target {
            if self.caret_geometry() == CaretGeometry::Block
                && !self.edit_mode.rest_policy().covers_terminator()
            {
                return self.grapheme_line_policy(buf, origin, head, direction);
            }
        }
        head
    }

    /// The block-caret line policy for one grapheme step (`h`/`l` in vi
    /// normal/visual): per [`cross_line_cursor`](Self::cross_line_cursor), either
    /// clamp the landing to the current line, or cross the terminator onto a real
    /// cell on the adjacent line. `origin` is the step's start, `head` its raw
    /// one-grapheme landing.
    ///
    /// This is a *movement-landing* transform only. Operator spans (`d`/`c`/`y`)
    /// deliberately bypass it — they resolve straight through `resolve_motion` and
    /// delete the literal grapheme range, which must not skip the `\n` (e.g. `dl`
    /// deletes the char under the caret, never the line break). So the flag steers
    /// where the caret *rests*, not how far an operator reaches.
    fn grapheme_line_policy(
        &self,
        buf: &str,
        origin: usize,
        head: usize,
        direction: Direction,
    ) -> usize {
        if !self.cross_line_cursor {
            // vim-strict: the caret may not leave the current line.
            return match direction {
                Direction::Backward => head.max(line::start_of_line(buf, origin)),
                Direction::Forward => head.min(line::end_of_line(buf, origin)),
            };
        }
        // Cross the terminator so the caret lands on a real cell, not the `\n`.
        // Forward: skip onto the next line's first grapheme. Backward: step once
        // more onto the previous line's last grapheme — unless that line is *also*
        // a terminator (an empty line), where column 0 is the only cell.
        let is_terminator = |pos: usize| buf[pos..].starts_with(['\r', '\n']);
        if !is_terminator(head) {
            return head;
        }
        match direction {
            Direction::Forward => next_grapheme_boundary(buf, head),
            Direction::Backward => {
                let back = prev_grapheme_boundary(buf, head);
                if is_terminator(back) {
                    head // previous line is empty — rest on its column 0
                } else {
                    back
                }
            }
        }
    }

    /// Caret geometry of the active mode: [`CaretGeometry::Block`] for vi normal
    /// / visual (an inclusive motion lands *on* a grapheme and the operator eats
    /// it), [`CaretGeometry::Bar`] for emacs / vi insert (`Between`, resting on
    /// the trailing boundary). Drives the forward word-end landing and operator
    /// inclusivity in [`resolve_motion`] and the selection extension in
    /// [`Cursor::put_cursor`].
    fn caret_geometry(&self) -> CaretGeometry {
        if self.edit_mode.rest_policy() == RestPolicy::Between {
            CaretGeometry::Bar
        } else {
            CaretGeometry::Block
        }
    }

    /// Place the caret on the grapheme at `target` via [`Cursor::put_cursor`]
    /// (Helix's central op), collapsing the selection unless `select` keeps the
    /// anchor, then normalize at the commit boundary (RestPolicy snap and
    /// selection bookkeeping). The per-mode geometry (inclusive block vs
    /// exclusive bar) rides on [`caret_geometry`](Self::caret_geometry), so
    /// inclusivity is carried by the range itself — there is no
    /// `selection_inclusive` side-channel to maintain.
    ///
    /// The sink for [`CoverLanding`](SelectionExtent::CoverLanding) placement:
    /// every `Move`, and every `Extend` under that extent, funnels here after
    /// its target is resolved via [`resolve_motion`]. [`SelectionExtent::Span`]
    /// extension goes around it through [`Cursor::extend_span`].
    fn move_head_to(&mut self, target: usize, select: bool) {
        let next = self.line_buffer.cursor().put_cursor(
            self.line_buffer.get_buffer(),
            target,
            Movement::select(select),
            self.caret_geometry(),
        );
        self.place(next);
    }

    /// Install an already-placed [`Cursor`] and normalize it at the commit
    /// boundary — the shared tail of every placement strategy ([`put_cursor`]'s
    /// `CoverLanding` via [`move_head_to`](Self::move_head_to), [`extend_span`]'s
    /// `Span`). The strategy decides *where* the caret goes; `place` is *how* it
    /// lands: `set_cursor` then [`commit_cursor`](Self::commit_cursor).
    ///
    /// [`put_cursor`]: Cursor::put_cursor
    /// [`extend_span`]: Cursor::extend_span
    fn place(&mut self, next: Cursor) {
        self.line_buffer.set_cursor(next);
        self.commit_cursor();
    }

    /// The active mode's selection model: how `Extend` places the head (vi-visual
    /// `CoverLanding` vs bar/helix `Span`). Orthogonal to [`caret_geometry`](Self::caret_geometry).
    fn caret_extent(&self) -> SelectionExtent {
        self.edit_mode.selection_extent()
    }

    /// Lower a [`MotionTarget`] onto the cursor (the `Move`/`Extend` path):
    /// resolve the head per the active policy, then place it — collapsing the
    /// selection unless `select` keeps the anchor. The shared sink the legacy
    /// `MoveWord*` sugar funnels through, so they behave identically to an
    /// equivalent `Move`/`Extend` command.
    fn apply_move(&mut self, target: MotionTarget, select: bool) {
        let head = self.resolve_head(target);
        self.move_head_to(head, select);
    }

    /// Lower an operator over a [`MotionTarget`] onto the buffer (the
    /// `Cut`/`Copy` path) at char-wise granularity. The shared sink the legacy
    /// `CutWord*`/`CopyWord*` sugar funnels through: `operator_span`'s `op_end`
    /// already encodes inclusivity, so the consumed range matches the old
    /// hand-built `insertion_point..*_index` ranges.
    fn apply_operator(&mut self, target: MotionTarget, verb: OperatorVerb) {
        let sel = operator_span(
            self.get_buffer(),
            self.insertion_point(),
            target,
            self.caret_geometry(),
        );
        self.operate(sel, verb, Granularity::CharWise);
    }

    pub(crate) fn move_line_up(&mut self, select: bool) {
        if let Some(target) = self.line_buffer.line_up_target() {
            self.move_head_to(target, select);
        }
        self.update_undo_state(UndoBehavior::MoveCursor);
    }

    pub(crate) fn move_line_down(&mut self, select: bool) {
        if let Some(target) = self.line_buffer.line_down_target() {
            self.move_head_to(target, select);
        }
        self.update_undo_state(UndoBehavior::MoveCursor);
    }

    /// Get the text of the current [`LineBuffer`]
    pub fn get_buffer(&self) -> &str {
        self.line_buffer.get_buffer()
    }

    /// Edit the [`LineBuffer`] in an undo-safe manner.
    pub fn edit_buffer<F>(&mut self, func: F, undo_behavior: UndoBehavior)
    where
        F: FnOnce(&mut LineBuffer),
    {
        self.update_undo_state(undo_behavior);
        func(&mut self.line_buffer);
    }

    /// Set the text of the current [`LineBuffer`] given the specified [`UndoBehavior`]
    /// Insertion point update to the end of the buffer.
    pub(crate) fn set_buffer(&mut self, buffer: String, undo_behavior: UndoBehavior) {
        self.line_buffer.set_buffer(buffer);
        // History navigation replaces the buffer outside the command path, so
        // normalize the cursor here too (e.g. Vi normal must not sit past the end).
        self.commit_cursor();
        self.update_undo_state(undo_behavior);
    }

    pub(crate) fn insertion_point(&self) -> usize {
        // The visible / edit position is policy-dependent: a `Between` (bar)
        // cursor sits at the head; a `Block` cursor sits at the caret (its left
        // edge). This is the one place that distinction lives — motions, edits
        // and callers all read it from here.
        let cursor = self.line_buffer.cursor();
        if self.edit_mode.rest_policy() == RestPolicy::Between {
            cursor.head()
        } else {
            cursor.caret(self.line_buffer.get_buffer())
        }
    }

    /// The edge `target` departs from — the origin an `Extend` resolves against,
    /// as opposed to [`insertion_point`](Self::insertion_point)'s *where the
    /// cursor is*.
    ///
    /// A forward block selection puts the caret on the near edge of its head
    /// grapheme and the head on the far edge, so forward travel departs from the
    /// head and backward travel from the caret. Departing from the caret both
    /// ways makes an exclusive forward motion a no-op — it lands on the very
    /// boundary the previous `Extend` parked the head on.
    ///
    /// Which way `target` travels is read against the *visible* cursor, so the
    /// answer never depends on the edge being picked. Under `Between` both edges
    /// are the head, so this is a no-op there.
    fn motion_origin(&self, target: MotionTarget) -> usize {
        let reference = self.insertion_point();
        match target.direction() {
            Some(Direction::Forward) => self.line_buffer.cursor().head(),
            _ => reference,
        }
    }

    /// Where completion treats the cursor: the *end* of the word under it.
    ///
    /// A caret cursor rests *on* a grapheme and [`Self::insertion_point`]
    /// reports that grapheme's start, so completing there strands it
    /// (`foo` -> `foobaro`). Complete at its far edge instead — where insert
    /// mode sits, since leaving insert steps one grapheme back.
    pub(crate) fn completion_point(&self) -> usize {
        let pos = self.insertion_point();
        if self.edit_mode.rest_policy() == RestPolicy::Between {
            return pos;
        }
        let buf = self.line_buffer.get_buffer();
        // Grapheme-aware: `pos + 1` would split a multi-byte grapheme.
        // Yields `buf.len()` at the end, so no separate guard.
        let next = next_grapheme_boundary(buf, pos);
        // No current policy rests the caret *on* a line terminator (`Block`
        // widens backward off it), but a Helix-faithful policy that does would
        // otherwise pull the newline into the completed span and join the two
        // lines. The word ends at the caret there.
        if buf[pos..next].starts_with(['\n', '\r']) {
            pos
        } else {
            next
        }
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.line_buffer.is_empty()
    }

    pub(crate) fn is_cursor_at_first_line(&self) -> bool {
        self.line_buffer.is_cursor_at_first_line()
    }

    pub(crate) fn is_cursor_at_last_line(&self) -> bool {
        self.line_buffer.is_cursor_at_last_line()
    }

    pub(crate) fn is_cursor_at_buffer_end(&self) -> bool {
        let buf = self.get_buffer();
        let cursor = self.line_buffer.cursor();
        // An active selection is never a clean end-of-buffer point. Completing a
        // history hint (or appending) here would run through `delete_selection`
        // and clobber the selection — so report `false`, matching the old
        // caret-based check, which a forward selection's caret (one inward from
        // `len`) already failed.
        if !cursor.is_empty() {
            return false;
        }
        if self.caret_geometry() == CaretGeometry::Block {
            // Cell caret (vi normal): the resting point sits *on* the last
            // grapheme, one inward from `len`. "At the end" means that cell is the
            // final one — nothing lies to its right. (A bare `head == len` check
            // never holds here, which is why a history hint stopped completing in
            // normal mode after the cursor became the single source of truth.)
            next_grapheme_boundary(buf, cursor.head()) == buf.len()
        } else {
            // Bar caret (emacs / vi insert): at the end iff the head rests past
            // the last grapheme.
            cursor.head() == buf.len()
        }
    }

    pub(crate) fn reset_undo_stack(&mut self) {
        self.edit_stack.reset();
    }

    pub(crate) fn move_to_start(&mut self, select: bool) {
        self.move_head_to(0, select);
    }

    pub(crate) fn move_to_end(&mut self, select: bool) {
        self.move_head_to(self.line_buffer.len(), select);
    }

    /// Place the edit point *past the last grapheme* (at `len`) so the next
    /// insert appends rather than splitting. A block caret rests one grapheme
    /// inward from the end, so a plain insert there lands *before* the final
    /// character — accepting a trailing history hint must append instead. Does
    /// not commit, so the following `InsertString` reads this position.
    pub(crate) fn prepare_append_at_buffer_end(&mut self) {
        // Collapse to a bare point, don't just move the head: a resting block
        // caret is anchored (a helix "cursor" is a 1-wide selection), and
        // `set_insertion_point` would keep that anchor. The append then runs
        // through `insert_str`'s `delete_selection` and eats the covered
        // grapheme -- accepting the hint "-add" on "ssh" gave "ss-add".
        self.line_buffer
            .set_cursor(Cursor::point(self.line_buffer.len()));
    }

    pub(crate) fn move_to_line_start(&mut self, select: bool) {
        self.move_head_to(self.line_buffer.line_start_index(), select);
    }

    pub(crate) fn move_to_line_non_blank_start(&mut self, select: bool) {
        self.move_head_to(self.line_buffer.line_non_blank_start_index(), select);
    }

    pub(crate) fn move_to_line_end(&mut self, select: bool) {
        self.move_head_to(self.line_buffer.find_current_line_end(), select);
    }

    fn undo(&mut self) {
        let val = self.edit_stack.undo();
        self.line_buffer = val.clone();
    }

    fn redo(&mut self) {
        let val = self.edit_stack.redo();
        self.line_buffer = val.clone();
    }

    pub(crate) fn update_undo_state(&mut self, undo_behavior: UndoBehavior) {
        if matches!(undo_behavior, UndoBehavior::NoOp) {
            self.last_undo_behavior = UndoBehavior::NoOp;
            return;
        }
        if !undo_behavior.create_undo_point_after(&self.last_undo_behavior) {
            self.edit_stack.undo();
        }
        self.edit_stack.insert(self.line_buffer.clone());
        self.last_undo_behavior = undo_behavior;
    }

    // The dedicated `*Linewise` cut/copy methods below back the legacy public
    // `EditCommand` variants only — every builtin binding now lowers through
    // `operate` + `Granularity::LineWise` (with the `Change` verb covering the
    // `leave_blank_line` flavor). Linewise span fixes belong in `operate` /
    // `core_editor::line`, not here.

    fn cut_current_line(&mut self) {
        let deletion_range = self.line_buffer.current_line_range();

        let cut_slice = &self.line_buffer.get_buffer()[deletion_range.clone()];
        if !cut_slice.is_empty() {
            self.cut_buffer.set(cut_slice, Granularity::LineWise);
            self.line_buffer.set_insertion_point(deletion_range.start);
            self.line_buffer.clear_range(deletion_range);
        }
    }

    fn cut_from_start(&mut self) {
        let insertion_offset = self.line_buffer.insertion_point();
        if insertion_offset > 0 {
            self.cut_buffer.set(
                &self.line_buffer.get_buffer()[..insertion_offset],
                Granularity::CharWise,
            );
            self.line_buffer.clear_to_insertion_point();
        }
    }

    fn cut_from_start_linewise(&mut self, leave_blank_line: bool) {
        let insertion_offset = self.line_buffer.insertion_point();
        let end_offset = self.line_buffer.get_buffer()[insertion_offset..]
            .find('\n')
            .map_or(self.line_buffer.len(), |offset| {
                // When leave_blank_line is true, we do **not** add 1 to the offset
                // So there will remain an empty line after the operation
                if leave_blank_line {
                    insertion_offset + offset
                } else {
                    insertion_offset + offset + 1
                }
            });
        if end_offset > 0 {
            self.cut_buffer.set(
                &self.line_buffer.get_buffer()[..end_offset],
                Granularity::LineWise,
            );
            self.line_buffer.clear_range(..end_offset);
            self.line_buffer.move_to_start();
        }
    }

    fn cut_from_line_start(&mut self) {
        let previous_offset = self.line_buffer.insertion_point();
        self.line_buffer.move_to_line_start();
        let deletion_range = self.line_buffer.insertion_point()..previous_offset;
        let cut_slice = &self.line_buffer.get_buffer()[deletion_range.clone()];
        if !cut_slice.is_empty() {
            self.cut_buffer.set(cut_slice, Granularity::CharWise);
            self.line_buffer.clear_range(deletion_range);
        }
    }

    fn cut_from_line_non_blank_start(&mut self) {
        let cursor_pos = self.line_buffer.insertion_point();
        self.line_buffer.move_to_line_non_blank_start();
        let other_pos = self.line_buffer.insertion_point();
        let deletion_range = min(cursor_pos, other_pos)..max(cursor_pos, other_pos);
        self.cut_range(deletion_range);
    }

    fn cut_from_end(&mut self) {
        let cut_slice = &self.line_buffer.get_buffer()[self.line_buffer.insertion_point()..];
        if !cut_slice.is_empty() {
            self.cut_buffer.set(cut_slice, Granularity::CharWise);
            self.line_buffer.clear_to_end();
        }
    }

    fn cut_from_end_linewise(&mut self, leave_blank_line: bool) {
        let buf = self.line_buffer.get_buffer();
        let len = buf.len();
        let nl = buf[..self.line_buffer.insertion_point()].rfind('\n');
        // The register keeps content from the line start only (no leading
        // terminator) so a later linewise paste gains no blank line. The
        // deletion eats the preceding terminator when not leaving a blank line —
        // the whole `\r\n` for CRLF (see `LineBuffer`'s line-ending contract).
        // Same register/delete split as `operate`.
        let register_start = nl.map_or(0, |offset| offset + 1);
        let delete_start = nl.map_or(0, |offset| {
            if leave_blank_line {
                offset + 1
            } else if buf[..offset].ends_with('\r') {
                offset - 1
            } else {
                offset
            }
        });

        if delete_start < len {
            let register_slice = &self.line_buffer.get_buffer()[register_start..];
            if !register_slice.is_empty() {
                self.cut_buffer.set(register_slice, Granularity::LineWise);
            }
            self.line_buffer.set_insertion_point(delete_start);
            self.line_buffer.clear_to_end();
        }
    }

    fn cut_to_line_end(&mut self) {
        let cut_slice = &self.line_buffer.get_buffer()
            [self.line_buffer.insertion_point()..self.line_buffer.find_current_line_end()];
        if !cut_slice.is_empty() {
            self.cut_buffer.set(cut_slice, Granularity::CharWise);
            self.line_buffer.clear_to_line_end();
        }
    }

    fn kill_line(&mut self) {
        if self.line_buffer.insertion_point() == self.line_buffer.find_current_line_end() {
            self.cut_char()
        } else {
            self.cut_to_line_end()
        }
    }

    fn cut_word_left(&mut self) {
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Backward),
            OperatorVerb::Cut,
        );
    }

    fn cut_big_word_left(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Backward),
            OperatorVerb::Cut,
        );
    }

    fn cut_word_right(&mut self) {
        // emacs `M-d`: consume to the current word's trailing boundary (no skip).
        // Under a bar caret the operator span runs `origin..trailing`, matching
        // the old `insertion_point..word_right_index`.
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::End, Direction::Forward),
            OperatorVerb::Cut,
        );
    }

    fn cut_big_word_right(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::End, Direction::Forward),
            OperatorVerb::Cut,
        );
    }

    fn cut_word_right_to_next(&mut self) {
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Forward),
            OperatorVerb::Cut,
        );
    }

    fn cut_big_word_right_to_next(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Forward),
            OperatorVerb::Cut,
        );
    }

    fn cut_char(&mut self) {
        if self.line_buffer.selection_anchor().is_some() {
            self.cut_selection_to_cut_buffer(Granularity::CharWise);
        } else {
            let insertion_offset = self.line_buffer.insertion_point();
            let next_char = self.line_buffer.grapheme_right_index();
            self.cut_range(insertion_offset..next_char);
        }
    }

    fn insert_cut_buffer_before(&mut self) {
        self.delete_selection();
        insert_clipboard_content_before(&mut self.line_buffer, self.cut_buffer.deref_mut())
    }

    fn insert_cut_buffer_after(&mut self) {
        // After replacing a selection the cursor already sits at the deletion
        // point, so it must NOT skip a grapheme; only the plain no-selection `p`
        // steps past the grapheme under the cursor before inserting.
        let had_selection = self.line_buffer.selection_anchor().is_some();
        self.delete_selection();
        match self.cut_buffer.get() {
            (content, Granularity::CharWise) => {
                if !had_selection {
                    self.line_buffer.move_right();
                }
                self.line_buffer.insert_str(&content);
            }
            (mut content, Granularity::LineWise) => {
                if !content.ends_with('\n') {
                    content.push('\n');
                }
                let ip = self.line_buffer.insertion_point();
                match line::start_of_next_line(self.line_buffer.get_buffer(), ip) {
                    // A line exists below: insert at its start so the pasted lines
                    // land between current and next — i.e. below the current line.
                    Some(next) => {
                        self.line_buffer.set_insertion_point(next);
                        self.line_buffer.insert_str(&content);
                    }
                    // Last line: no line below, so append after the current line's
                    // terminator. Drop the trailing `\n` so no blank line is added,
                    // otherwise the paste would land *above* (like `P`).
                    None => {
                        let trimmed = content.strip_suffix('\n').unwrap_or(&content);
                        if self.line_buffer.is_empty() {
                            // No current line to append below — insert as-is so an
                            // empty buffer (e.g. after `dd` on the only line) does
                            // not gain a leading blank line.
                            self.line_buffer.insert_str(trimmed);
                        } else {
                            self.line_buffer.set_insertion_point(self.line_buffer.len());
                            self.line_buffer.insert_str(&format!("\n{trimmed}"));
                        }
                    }
                }
            }
        }
    }

    fn paste_at_selection_edge(&mut self, direction: Direction, count: usize) {
        let at = match direction {
            Direction::Forward => self.line_buffer.cursor().end(),
            Direction::Backward => self.line_buffer.cursor().start(),
        };

        match self.cut_buffer.get() {
            (content, Granularity::CharWise) if !content.is_empty() => {
                let to_paste = content.repeat(count);
                self.line_buffer.set_cursor(Cursor::point(at));
                self.line_buffer.insert_str(&to_paste);
                self.line_buffer
                    .set_cursor(Cursor::new(at, at + to_paste.len()));
            }
            // no consumer for linewise paste yet
            _ => (),
        }
    }

    fn move_right_until_char(
        &mut self,
        c: char,
        before_char: bool,
        current_line: bool,
        select: bool,
    ) {
        // Route through `move_head_to` so a selecting search opens the selection
        // via `put_cursor`; the old `update_selection_anchor` + raw `set_head`
        // path dropped the anchor when starting a selection from a point.
        let Some(found) = self.line_buffer.find_char_right(c, current_line) else {
            // Miss: no movement; only settle the selection per `select`.
            if !select {
                self.clear_selection();
            }
            return;
        };
        let target = if before_char {
            self.line_buffer.grapheme_left_index_from_pos(found)
        } else {
            found
        };
        self.move_head_to(target, select);
    }

    fn move_left_until_char(
        &mut self,
        c: char,
        before_char: bool,
        current_line: bool,
        select: bool,
    ) {
        // See `move_right_until_char`.
        let Some(found) = self.line_buffer.find_char_left(c, current_line) else {
            if !select {
                self.clear_selection();
            }
            return;
        };
        let target = if before_char {
            found + c.len_utf8()
        } else {
            found
        };
        self.move_head_to(target, select);
    }

    fn cut_right_until_char(&mut self, c: char, before_char: bool, current_line: bool) {
        if let Some(index) = self.line_buffer.find_char_right(c, current_line) {
            // Saving the section of the string that will be deleted to be
            // stored into the buffer
            let extra = if before_char { 0 } else { c.len_utf8() };
            let cut_slice =
                &self.line_buffer.get_buffer()[self.line_buffer.insertion_point()..index + extra];

            if !cut_slice.is_empty() {
                self.cut_buffer.set(cut_slice, Granularity::CharWise);

                if before_char {
                    self.line_buffer.delete_right_before_char(c, current_line);
                } else {
                    self.line_buffer.delete_right_until_char(c, current_line);
                }
            }
        }
    }

    fn cut_left_until_char(&mut self, c: char, before_char: bool, current_line: bool) {
        if let Some(index) = self.line_buffer.find_char_left(c, current_line) {
            // Saving the section of the string that will be deleted to be
            // stored into the buffer
            let extra = if before_char { c.len_utf8() } else { 0 };
            let cut_slice =
                &self.line_buffer.get_buffer()[index + extra..self.line_buffer.insertion_point()];

            if !cut_slice.is_empty() {
                self.cut_buffer.set(cut_slice, Granularity::CharWise);

                if before_char {
                    self.line_buffer.delete_left_before_char(c, current_line);
                } else {
                    self.line_buffer.delete_left_until_char(c, current_line);
                }
            }
        }
    }

    fn replace_char(&mut self, character: char) {
        // Visual `r`: replace every grapheme in the selection with `character`,
        // preserving line terminators — vim's `r` over a selection.
        if let Some((start, end)) = self.get_selection() {
            use unicode_segmentation::UnicodeSegmentation;
            let replacement: String = self.line_buffer.get_buffer()[start..end]
                .graphemes(true)
                .map(|g| {
                    if g == "\n" || g == "\r\n" || g == "\r" {
                        g.to_string()
                    } else {
                        character.to_string()
                    }
                })
                .collect();
            self.line_buffer.replace_range(start..end, &replacement);
            self.line_buffer.set_cursor(Cursor::point(start));
            return;
        }
        // Anchor the in-place replace on the caret: under a Block/visual cursor
        // head is one grapheme past the caret, so deleting+inserting without
        // collapsing first would clear two graphemes and corrupt the buffer.
        self.line_buffer.collapse_to_caret();
        let insertion_point = self.line_buffer.insertion_point();
        self.line_buffer.delete_right_grapheme();

        self.line_buffer.insert_char(character);
        self.line_buffer.set_insertion_point(insertion_point);
    }

    fn replace_chars(&mut self, n_chars: usize, string: &str) {
        // See `replace_char`: collapse the selection so the deletes start at the
        // caret rather than overshooting from the head.
        self.line_buffer.collapse_to_caret();
        for _ in 0..n_chars {
            self.line_buffer.delete_right_grapheme();
        }

        self.line_buffer.insert_str(string);
    }

    fn move_left(&mut self, select: bool) {
        let head = self.resolve_head(MotionTarget::Grapheme(Direction::Backward));
        self.move_head_to(head, select);
    }

    fn move_right(&mut self, select: bool) {
        let head = self.resolve_head(MotionTarget::Grapheme(Direction::Forward));
        self.move_head_to(head, select);
    }

    fn select_all(&mut self) {
        let end = self.line_buffer.len();
        self.line_buffer.set_cursor(Cursor::new(0, end));
    }

    /// Helix `x`: snap out to whole lines, or take one more when already there.
    ///
    /// The "already there" test is what no composition of existing commands can
    /// express: [`Select`](EditCommand::Select) re-anchors at the origin and
    /// [`Extend`](EditCommand::Extend) keeps its anchor, so neither can move
    /// both edges to line boundaries *and* notice they were there already.
    #[cfg(feature = "helix")]
    fn select_line(&mut self) {
        let buf = self.line_buffer.get_buffer();
        let cursor = self.line_buffer.cursor();
        let (start, end) = (cursor.start(), cursor.end());
        let first = line::start_of_line(buf, start);
        // The last byte covered, which for a point is the position itself. Taken
        // off `end` since that is the exclusive edge and may already be the next
        // line's first byte.
        let last = if end > start {
            prev_grapheme_boundary(buf, end)
        } else {
            start
        };
        // `None` at an unterminated last line, where the buffer end is the edge.
        let after = line::start_of_next_line(buf, last).unwrap_or(buf.len());
        let head = if start == first && end == after {
            line::start_of_next_line(buf, after).unwrap_or(buf.len())
        } else {
            after
        };
        self.place(Cursor::new(first, head));
    }

    #[cfg(feature = "system_clipboard")]
    fn cut_selection_to_system(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let cut_slice = &self.line_buffer.get_buffer()[start..end];
            self.system_clipboard.set(cut_slice, Granularity::CharWise);
            self.cut_range(start..end);
            self.clear_selection();
        }
    }

    fn cut_selection_to_cut_buffer(&mut self, granularity: Granularity) {
        if let Some((start, end)) = self.get_selection() {
            let sel = Cursor::new(start, end);
            self.operate(sel, OperatorVerb::Cut, granularity);
            self.clear_selection();
        }
    }

    /// Helix `Alt-d`: drop the selection without touching the cut buffer.
    ///
    /// `OperatorVerb::Erase` is the register-free deletion the motion-shaped
    /// `Erase` already uses; only the span differs.
    #[cfg(feature = "helix")]
    fn erase_selection(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let sel = Cursor::new(start, end);
            self.operate(sel, OperatorVerb::Erase, Granularity::CharWise);
            self.clear_selection();
        }
    }

    #[cfg(feature = "system_clipboard")]
    fn copy_selection_to_system(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let cut_slice = &self.line_buffer.get_buffer()[start..end];
            self.system_clipboard.set(cut_slice, Granularity::CharWise);
        }
    }

    fn copy_selection_to_cut_buffer(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let cut_slice = &self.line_buffer.get_buffer()[start..end];
            self.cut_buffer.set(cut_slice, Granularity::CharWise);
        }
    }

    fn lowercase_selection(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let lowercase_slice = self.line_buffer.get_buffer()[start..end].to_ascii_lowercase();
            self.line_buffer.replace_range(start..end, &lowercase_slice);
        }
    }

    fn uppercase_selection(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let uppercase_slice = self.line_buffer.get_buffer()[start..end].to_ascii_uppercase();
            self.line_buffer.replace_range(start..end, &uppercase_slice);
        }
    }

    fn switchcase_selection(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            let switchcase_slice = self.line_buffer.get_buffer()[start..end]
                .chars()
                .map(|ch| {
                    if ch.is_ascii_lowercase() {
                        ch.to_ascii_uppercase()
                    } else {
                        ch.to_ascii_lowercase()
                    }
                })
                .collect::<String>();
            self.line_buffer
                .replace_range(start..end, &switchcase_slice);
        }
    }

    /// If a selection is active returns the selected range, otherwise None.
    /// The range is guaranteed to be ascending.
    pub fn get_selection(&self) -> Option<(usize, usize)> {
        // `None` exactly when the cursor is empty (head == anchor): with the
        // collapsed `Cursor` storage, `selection_anchor()` is derived from
        // `!is_empty()`, so an anchor on the head is simply no selection.
        self.line_buffer.selection_anchor()?;
        let cursor = self.line_buffer.cursor();

        // Inclusivity is geometric (widened by put_cursor).
        Some((cursor.start(), cursor.end().min(self.line_buffer.len())))
    }

    /// The one-grapheme cell the caret rests on inside the active selection,
    /// or `None` when there is no selection, the cell falls outside the
    /// selected range, or the caret geometry is a bar (emacs / vi insert rest
    /// *between* graphemes, so no cell belongs to the cursor).
    ///
    /// This is the cell a distinct cursor style may claim: helix renders its
    /// primary cursor with an own style *inside* the selection, and a flat
    /// selection style (e.g. reverse video) painted over the whole range can
    /// otherwise swallow the terminal cursor entirely.
    pub(crate) fn selection_head_cell(&self) -> Option<(usize, usize)> {
        let (from, to) = self.get_selection()?;
        if self.caret_geometry() != CaretGeometry::Block {
            return None;
        }
        let cell_start = self.insertion_point();
        let cell_end = next_grapheme_boundary(self.get_buffer(), cell_start);
        (cell_start < to && cell_end > from).then_some((cell_start, cell_end))
    }

    fn delete_selection(&mut self) {
        if let Some((start, end)) = self.get_selection() {
            self.line_buffer.clear_range_safe(start..end);
            self.clear_selection();
        }
    }

    fn backspace(&mut self) {
        if self.line_buffer.selection_anchor().is_some() {
            self.delete_selection();
        } else {
            self.line_buffer.delete_left_grapheme();
        }
    }

    /// Cut the grapheme left of the caret into the cut buffer (vi `X`).
    ///
    /// Intended for vi normal mode only, where there is no selection, since
    /// visual `X` is dispatched as a linewise `CutSelection`. Bound directly
    /// elsewhere, a selection could exist, so clear it first: `X` always means
    /// "delete the grapheme before the caret," and clearing avoids leaving a
    /// stale anchor pointing into the mutated buffer. The cut is clamped to
    /// the current line so `X` never crosses a terminator (unconditional,
    /// unlike the `cross_line_cursor`-gated clamp in `resolve_head`).
    fn cut_char_left(&mut self) {
        self.clear_selection();
        let cur_pos = self.line_buffer.insertion_point();
        let left_index = self.line_buffer.grapheme_left_index();
        if left_index < cur_pos && left_index >= self.line_buffer.current_line_range().start {
            self.cut_range(left_index..cur_pos);
        }
    }

    fn delete(&mut self) {
        if self.line_buffer.selection_anchor().is_some() {
            self.delete_selection();
        } else {
            self.line_buffer.delete_right_grapheme();
        }
    }

    fn move_word_left(&mut self, select: bool) {
        self.apply_move(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Backward),
            select,
        );
    }

    fn move_big_word_left(&mut self, select: bool) {
        self.apply_move(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Backward),
            select,
        );
    }

    fn move_word_right(&mut self, select: bool) {
        // emacs M-f: end of current word, no skip.
        self.apply_move(
            word_target(WordKind::Unicode, WordEdge::End, Direction::Forward),
            select,
        );
    }

    fn move_word_right_start(&mut self, select: bool) {
        self.apply_move(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Forward),
            select,
        );
    }

    fn move_big_word_right_start(&mut self, select: bool) {
        self.apply_move(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Forward),
            select,
        );
    }

    fn move_word_right_end(&mut self, select: bool) {
        // vi-`e` lands *on* the word's last grapheme regardless of the active
        // caret, so it resolves the word-end with `inclusive = true` (block
        // reading) rather than the mode's geometry — distinct from emacs `M-f`,
        // which rests on the trailing boundary. (Unbound.)
        self.move_head_to(self.word_end_on_grapheme(WordKind::Unicode), select);
    }

    fn move_big_word_right_end(&mut self, select: bool) {
        // vi-`E` on-char — see `move_word_right_end`.
        self.move_head_to(self.word_end_on_grapheme(WordKind::LongWord), select);
    }

    /// Forward word-end resolved with block (on-grapheme) geometry, whatever the
    /// active caret. Backs the vi-`e`/`E`-style `*RightEnd` commands, whose
    /// landing is the word's last grapheme rather than its trailing boundary —
    /// so it asks the motion resolver for the block reading (`block = true`)
    /// directly instead of the mode's geometry.
    fn word_end_on_grapheme(&self, kind: WordKind) -> usize {
        let target = word_target(kind, WordEdge::End, Direction::Forward);
        resolve_motion(
            self.get_buffer(),
            self.insertion_point(),
            target,
            CaretGeometry::Block,
        )
        .head
    }

    fn insert_char(&mut self, c: char) {
        self.delete_selection();
        self.line_buffer.insert_char(c);
    }

    fn insert_str(&mut self, str: &str) {
        self.delete_selection();
        self.line_buffer.insert_str(str);
    }

    fn insert_newline(&mut self) {
        self.delete_selection();
        self.line_buffer.insert_newline();
    }

    /// Collapse first: `set_insertion_point` is `set_head`, so on an anchored
    /// cursor (every helix one) the stale anchor dragged the caret back onto
    /// the old line.
    fn insert_newline_above(&mut self) {
        self.clear_selection();
        let index = self.line_buffer.find_char_left('\n', false).unwrap_or(0);
        self.line_buffer.set_insertion_point(index);
        self.line_buffer.insert_newline();
    }

    fn insert_newline_below(&mut self) {
        self.clear_selection();
        let index = self
            .line_buffer
            .find_char_right('\n', false)
            .unwrap_or(self.line_buffer.len());
        self.line_buffer.set_insertion_point(index);
        self.line_buffer.insert_newline();
    }

    #[cfg(feature = "system_clipboard")]
    fn paste_from_system(&mut self) {
        self.delete_selection();
        insert_clipboard_content_before(&mut self.line_buffer, self.system_clipboard.deref_mut());
    }

    fn paste_cut_buffer(&mut self) {
        self.delete_selection();
        insert_clipboard_content_before(&mut self.line_buffer, self.cut_buffer.deref_mut());
    }

    fn cut_range(&mut self, range: Range<usize>) {
        self.cut_range_with(range, Granularity::CharWise);
    }

    fn cut_range_with(&mut self, range: Range<usize>, granularity: Granularity) {
        if range.start <= range.end {
            self.copy_range_with(range.clone(), granularity);
            self.line_buffer.clear_range_safe(range.clone());
            self.line_buffer.set_insertion_point(range.start);
        }
    }

    fn copy_range(&mut self, range: Range<usize>) {
        self.copy_range_with(range, Granularity::CharWise);
    }

    fn copy_range_with(&mut self, range: Range<usize>, granularity: Granularity) {
        if range.start < range.end {
            let slice = &self.line_buffer.get_buffer()[range];
            self.cut_buffer.set(slice, granularity);
        }
    }

    /// Delete text strictly between matching `open_char` and `close_char`.
    fn cut_inside_pair(&mut self, open_char: char, close_char: char) {
        if let Some(range) = self
            .line_buffer
            .range_inside_current_pair(open_char, close_char)
            .or_else(|| {
                self.line_buffer
                    .range_inside_next_pair(open_char, close_char)
            })
        {
            self.cut_range(range)
        }
    }

    /// Return the range of the word under the cursor.
    /// A word consists of a sequence of letters, digits and underscores,
    /// separated with white space.
    /// A block of whitespace under the cursor is also treated as a word.
    ///
    /// `text_object_scope` Inner includes only the word itself
    /// while Around also includes trailing whitespace,
    /// or preceding whitespace if there is no trailing whitespace.
    fn word_text_object_range(&self, text_object_scope: TextObjectScope) -> Range<usize> {
        self.line_buffer
            .current_whitespace_range()
            .unwrap_or_else(|| {
                let word_range = self.line_buffer.current_word_range();
                match text_object_scope {
                    TextObjectScope::Inner => word_range,
                    TextObjectScope::Around => {
                        self.line_buffer.expand_range_with_whitespace(word_range)
                    }
                }
            })
    }

    /// Return the range of the WORD under the cursor.
    /// A WORD consists of a sequence of non-blank characters, separated with white space.
    /// A block of whitespace under the cursor is also treated as a word.
    ///
    /// `text_object_scope` Inner includes only the word itself
    /// while Around also includes trailing whitespace,
    /// or preceding whitespace if there is no trailing whitespace.
    fn big_word_text_object_range(&self, text_object_scope: TextObjectScope) -> Range<usize> {
        self.line_buffer
            .current_whitespace_range()
            .unwrap_or_else(|| {
                let big_word_range = self.line_buffer.current_big_word_range();
                match text_object_scope {
                    TextObjectScope::Inner => big_word_range,
                    TextObjectScope::Around => self
                        .line_buffer
                        .expand_range_with_whitespace(big_word_range),
                }
            })
    }

    /// Returns `Some(Range<usize>)` for range inside the character pair in `pair_group`
    /// at or surrounding the cursor, the next pair if no pairs in `pair_group`
    /// surround the cursor, or `None` if there are no pairs from `pair_group` found.
    ///
    /// `text_object_scope` [`TextObjectScope::Inner`] includes only the range inside the pair
    /// whereas [`TextObjectScope::Around`] also includes the surrounding pair characters
    ///
    /// If multiple pair types exist, returns the innermost pair that surrounds
    /// the cursor. Handles empty pair as zero-length ranges inside pair.
    /// For asymmetric pairs like `(` `)` the search is multi-line, however,
    /// for symmetric pairs like `"` `"` the search is restricted to the current line.
    fn matching_pair_group_text_object_range(
        &self,
        text_object_scope: TextObjectScope,
        matching_pair_group: &[(char, char)],
    ) -> Option<Range<usize>> {
        self.line_buffer
            .range_inside_current_pair_in_group(matching_pair_group)
            .or_else(|| {
                self.line_buffer
                    .range_inside_next_pair_in_group(matching_pair_group)
            })
            .and_then(|pair_range| match text_object_scope {
                TextObjectScope::Inner => Some(pair_range),
                TextObjectScope::Around => self.expand_range_to_include_pair(pair_range),
            })
    }

    /// Returns `Some(Range<usize>)` for range inside brackets (`()`, `[]`, `{}`)
    /// at or surrounding the cursor, the next pair of brackets if no brackets
    /// surround the cursor, or `None` if there are no brackets found.
    ///
    /// `text_object_scope` [`TextObjectScope::Inner`] includes only the range inside the pair
    /// whereas [`TextObjectScope::Around`] also includes the surrounding pair characters
    ///
    /// If multiple bracket types exist, returns the innermost pair that surrounds
    /// the cursor. Handles empty brackets as zero-length ranges inside brackets.
    /// Includes brackets that span multiple lines.
    fn bracket_text_object_range(
        &self,
        text_object_scope: TextObjectScope,
    ) -> Option<Range<usize>> {
        const BRACKET_PAIRS: &[(char, char)] = &[('(', ')'), ('[', ']'), ('{', '}')];
        self.matching_pair_group_text_object_range(text_object_scope, BRACKET_PAIRS)
    }

    /// Returns `Some(Range<usize>)` for the range inside quotes (`""`, `''` or `\`\`\`)
    /// at the cursor, the next pair of quotes if the cursor is not within quotes,
    /// or `None` if there are no quotes found.
    ///
    /// Quotes are restricted to the current line.
    ///
    /// `text_object_scope` [`TextObjectScope::Inner`] includes only the range inside the pair
    /// whereas [`TextObjectScope::Around`] also includes the surrounding pair characters
    ///
    /// If multiple quote types exist, returns the innermost pair that surrounds
    /// the cursor. Handles empty quotes as zero-length ranges inside quote.
    fn quote_text_object_range(&self, text_object_scope: TextObjectScope) -> Option<Range<usize>> {
        const QUOTE_PAIRS: &[(char, char)] = &[('"', '"'), ('\'', '\''), ('`', '`')];
        self.matching_pair_group_text_object_range(text_object_scope, QUOTE_PAIRS)
    }

    /// Get the bounds for a text object operation
    fn text_object_range(&self, text_object: TextObject) -> Option<Range<usize>> {
        match text_object.object_type {
            TextObjectType::Word => Some(self.word_text_object_range(text_object.scope)),
            TextObjectType::BigWord => Some(self.big_word_text_object_range(text_object.scope)),
            TextObjectType::Brackets => self.bracket_text_object_range(text_object.scope),
            TextObjectType::Quote => self.quote_text_object_range(text_object.scope),
        }
    }

    fn cut_text_object(&mut self, text_object: TextObject) {
        if let Some(range) = self.text_object_range(text_object) {
            self.cut_range(range);
        }
    }

    fn copy_text_object(&mut self, text_object: TextObject) {
        if let Some(range) = self.text_object_range(text_object) {
            self.copy_range(range);
        }
    }

    pub(crate) fn copy_from_start(&mut self) {
        let insertion_offset = self.line_buffer.insertion_point();
        if insertion_offset > 0 {
            self.cut_buffer.set(
                &self.line_buffer.get_buffer()[..insertion_offset],
                Granularity::CharWise,
            );
        }
    }

    pub(crate) fn copy_from_start_linewise(&mut self) {
        let insertion_point = self.line_buffer.insertion_point();
        let end_offset = self.line_buffer.get_buffer()[insertion_point..]
            .find('\n')
            .map_or(self.line_buffer.len(), |offset| insertion_point + offset);
        if end_offset > 0 {
            self.cut_buffer.set(
                &self.line_buffer.get_buffer()[..end_offset],
                Granularity::LineWise,
            );
        }
        self.line_buffer.move_to_start();
    }

    pub(crate) fn copy_from_line_start(&mut self) {
        let previous_offset = self.line_buffer.insertion_point();
        let start_offset = {
            let temp_pos = self.line_buffer.insertion_point();
            self.line_buffer.move_to_line_start();
            let start = self.line_buffer.insertion_point();
            self.line_buffer.set_insertion_point(temp_pos);
            start
        };
        let copy_range = start_offset..previous_offset;
        self.copy_range(copy_range);
    }

    pub(crate) fn copy_from_line_non_blank_start(&mut self) {
        let cursor_pos = self.line_buffer.insertion_point();
        self.line_buffer.move_to_line_non_blank_start();
        let other_pos = self.line_buffer.insertion_point();
        self.line_buffer.set_insertion_point(cursor_pos);
        let copy_range = min(cursor_pos, other_pos)..max(cursor_pos, other_pos);
        self.copy_range(copy_range);
    }

    pub(crate) fn copy_from_end(&mut self) {
        let copy_range = self.line_buffer.insertion_point()..self.line_buffer.len();
        self.copy_range(copy_range);
    }

    pub(crate) fn copy_from_end_linewise(&mut self) {
        self.line_buffer.move_to_line_start();
        let copy_range = self.line_buffer.insertion_point()..self.line_buffer.len();
        if copy_range.start < copy_range.end {
            let slice = &self.line_buffer.get_buffer()[copy_range];
            self.cut_buffer.set(slice, Granularity::LineWise);
        }
    }

    pub(crate) fn copy_to_line_end(&mut self) {
        let copy_range =
            self.line_buffer.insertion_point()..self.line_buffer.find_current_line_end();
        self.copy_range(copy_range);
    }

    pub(crate) fn copy_word_left(&mut self) {
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Backward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_big_word_left(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Backward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_word_right(&mut self) {
        // emacs forward-word end (no skip) — mirrors `cut_word_right`.
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::End, Direction::Forward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_big_word_right(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::End, Direction::Forward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_word_right_to_next(&mut self) {
        self.apply_operator(
            word_target(WordKind::Unicode, WordEdge::Start, Direction::Forward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_big_word_right_to_next(&mut self) {
        self.apply_operator(
            word_target(WordKind::LongWord, WordEdge::Start, Direction::Forward),
            OperatorVerb::Copy,
        );
    }

    pub(crate) fn copy_right_until_char(&mut self, c: char, before_char: bool, current_line: bool) {
        if let Some(index) = self.line_buffer.find_char_right(c, current_line) {
            let extra = if before_char { 0 } else { c.len_utf8() };
            let copy_range = self.line_buffer.insertion_point()..index + extra;
            self.copy_range(copy_range);
        }
    }

    pub(crate) fn copy_left_until_char(&mut self, c: char, before_char: bool, current_line: bool) {
        if let Some(index) = self.line_buffer.find_char_left(c, current_line) {
            let extra = if before_char { c.len_utf8() } else { 0 };
            let copy_range = index + extra..self.line_buffer.insertion_point();
            self.copy_range(copy_range);
        }
    }

    /// Copy text strictly between matching `open_char` and `close_char`.
    fn copy_inside_pair(&mut self, open_char: char, close_char: char) {
        if let Some(range) = self
            .line_buffer
            .range_inside_current_pair(open_char, close_char)
            .or_else(|| {
                self.line_buffer
                    .range_inside_next_pair(open_char, close_char)
            })
        {
            self.copy_range(range);
        }
    }

    /// Expand the range to include `open_char` and `close_char`
    fn expand_range_to_include_pair(&self, range: Range<usize>) -> Option<Range<usize>> {
        let start = self.line_buffer.grapheme_left_index_from_pos(range.start);
        let end = self.line_buffer.grapheme_right_index_from_pos(range.end);

        Some(start..end)
    }

    /// Delete text around matching `open_char` and `close_char` (including the pair characters).
    fn cut_around_pair(&mut self, open_char: char, close_char: char) {
        if let Some(around_range) = self
            .line_buffer
            .range_inside_current_pair(open_char, close_char)
            .or_else(|| {
                self.line_buffer
                    .range_inside_next_pair(open_char, close_char)
            })
            .and_then(|range| self.expand_range_to_include_pair(range))
        {
            self.cut_range(around_range);
        }
    }

    /// Copy text around matching `open_char` and `close_char` (including the pair characters).
    fn copy_around_pair(&mut self, open_char: char, close_char: char) {
        if let Some(around_range) = self
            .line_buffer
            .range_inside_current_pair(open_char, close_char)
            .or_else(|| {
                self.line_buffer
                    .range_inside_next_pair(open_char, close_char)
            })
            .and_then(|range| self.expand_range_to_include_pair(range))
        {
            self.copy_range(around_range);
        }
    }
}

fn insert_clipboard_content_before(line_buffer: &mut LineBuffer, clipboard: &mut dyn Clipboard) {
    match clipboard.get() {
        (content, Granularity::CharWise) => {
            line_buffer.insert_str(&content);
        }
        (mut content, Granularity::LineWise) => {
            // TODO: Simplify that?
            line_buffer.move_to_line_start();
            line_buffer.move_line_up();
            if !content.ends_with('\n') {
                // TODO: Make sure platform requirements are met
                content.push('\n');
            }
            line_buffer.insert_str(&content);
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::prompt::PromptViMode;
    use crate::{Direction, FindStop, WordEdge, WordKind};
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    fn editor_with(buffer: &str) -> Editor {
        let mut editor = Editor::default();
        editor.set_buffer(buffer.to_string(), UndoBehavior::CreateUndoPoint);
        editor
    }

    fn vi_editor(buffer: &str, vi_mode: PromptViMode) -> Editor {
        let mut editor = editor_with(buffer);
        editor.set_edit_mode(PromptEditMode::Vi(vi_mode));
        editor
    }

    // The Vi-normal cursor invariant ("cursor never rests past the last
    // grapheme") is enforced by the `RestPolicy` commit boundary in
    // `run_edit_command`, not by a per-command clamp. These cover the
    // behavioural scenarios from nushell/reedline#1069 by driving real
    // `EditCommand`s through that boundary.

    #[test]
    fn vi_normal_clamps_cursor_off_the_end() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        // rests on the last grapheme 'o' (byte 4), not past it (byte 5)
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn vi_normal_clamps_to_line_end() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: false });
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn vi_insert_does_not_clamp_off_the_end() {
        let mut editor = vi_editor("hello", PromptViMode::Insert);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        // insert mode's caret may sit past the last grapheme
        assert_eq!(editor.insertion_point(), 5);
    }

    #[test]
    fn vi_normal_empty_buffer_stays_at_zero() {
        let mut editor = vi_editor("", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        assert_eq!(editor.insertion_point(), 0);
    }

    #[test]
    fn vi_normal_within_bounds_is_unchanged() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 2,
            select: false,
        });
        assert_eq!(editor.insertion_point(), 2);
    }

    #[test]
    fn vi_normal_clamps_onto_multibyte_grapheme() {
        let mut editor = vi_editor("café", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        // 'é' is 2 bytes, so the last grapheme starts at byte 3, not 4
        assert_eq!(editor.insertion_point(), "caf".len());
    }

    // ======================================================================
    // FLIP SAFETY NET — gates the cursor-as-truth flip (storage follows Helix)
    //
    // INVARIANT masters (`net_*`): pin `insertion_point()` / `get_selection()`
    // values the storage swap must preserve byte-for-byte. These MUST stay
    // green through the flip — they are the proof the swap was faithful.
    // (#694/#893 and the inclusive-cut cases are already pinned by the tests
    // above; these cover the gaps: Between-mode resting, no-anchor/backward
    // selection, and the bare-block-vs-deliberate-selection distinction.)
    // ======================================================================

    #[rstest]
    #[case(PromptViMode::Insert, "hello", 5)] // Between: caret may rest at len
    #[case(PromptViMode::Normal, "hello", 4)] // OnGrapheme: onto the last grapheme
    #[case(PromptViMode::Insert, "café", 5)] // multibyte, insert rests at len
    #[case(PromptViMode::Normal, "café", 3)] // multibyte, normal on last grapheme
    #[case(PromptViMode::Insert, "", 0)]
    #[case(PromptViMode::Normal, "", 0)]
    fn net_insertion_point_at_end(
        #[case] mode: PromptViMode,
        #[case] buf: &str,
        #[case] expect: usize,
    ) {
        let mut editor = vi_editor(buf, mode);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        assert_eq!(editor.insertion_point(), expect);
    }

    #[test]
    fn net_insertion_point_emacs_rests_at_len() {
        // Default/Emacs is `Between`: the caret may sit past the last grapheme.
        let mut editor = editor_with("hello");
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        assert_eq!(editor.insertion_point(), 5);
    }

    #[test]
    fn net_get_selection_none_without_anchor() {
        // A bare cursor (no anchor planted) is not a selection.
        let editor = vi_editor("hello", PromptViMode::Normal);
        assert_eq!(editor.get_selection(), None);
    }

    #[test]
    fn net_get_selection_backward_is_ordered() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.move_to_position(3, false);
        editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        // head left of anchor; get_selection returns an ordered (start, end).
        assert_eq!(editor.get_selection(), Some((1, 4)));
    }

    // --- selection_head_cell: the cell a cursor style may claim -------------

    #[test]
    fn head_cell_is_the_last_grapheme_of_a_forward_selection() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.move_to_position(1, false);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((1, 3)), "setup");
        assert_eq!(editor.selection_head_cell(), Some((2, 3)));
    }

    #[test]
    fn head_cell_is_the_first_grapheme_of_a_backward_selection() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.move_to_position(2, false);
        editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        assert_eq!(editor.get_selection(), Some((1, 3)), "setup");
        assert_eq!(editor.selection_head_cell(), Some((1, 2)));
    }

    #[test]
    fn head_cell_is_grapheme_wide() {
        let mut editor = vi_editor("a\u{1f44d}b", PromptViMode::Normal);
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((0, 5)), "setup");
        assert_eq!(editor.selection_head_cell(), Some((1, 5)));
    }

    #[test]
    fn no_head_cell_without_a_selection() {
        let editor = vi_editor("hello", PromptViMode::Normal);
        assert_eq!(editor.selection_head_cell(), None);
    }

    #[test]
    fn no_head_cell_under_a_bar_caret() {
        // Vi insert rests *between* graphemes: a backward selection has the
        // caret boundary touching the range, but no cell belongs to the
        // cursor, so the whole range keeps the selection style.
        let mut editor = vi_editor("hello", PromptViMode::Insert);
        editor.move_to_position(2, false);
        editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        assert!(editor.get_selection().is_some(), "setup");
        assert_eq!(editor.selection_head_cell(), None);
    }

    // BEHAVIOR(E): we follow helix — a bare cursor and a 1-grapheme selection
    // render the SAME (the 1-wide block IS the cursor), so we deliberately do
    // NOT distinguish them. The editor-level invariant kept here is only that a
    // deliberate selection reports a range. "A bare cursor is not highlighted"
    // is a *painter* invariant (helix rule: highlight = range minus the 1-wide
    // cursor cell), pinned when we touch the render side of the flip.
    //
    // NOTE: the exact range value is BEHAVIOR the flip may shift — the inclusive
    // `+1` goes away with `selection_inclusive`. The stable selection invariants
    // are the cut-result tests above (buffer + cut content), not raw ranges.
    #[test]
    fn net_deliberate_selection_reports_a_range() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.move_to_position(1, false);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((1, 3)));
    }

    // Esc-from-insert is lowered (in the Vi machine) to a backward grapheme
    // step, and the engine relays the new rest policy via `sync_edit_mode`
    // *before* that step runs. These replicate that seam sequence — insert
    // caret, `sync_edit_mode` (no commit), then the step — to pin the timing:
    // the step must read `OnGrapheme`, must not double-step a caret sitting at
    // the line end, and must not cross the line under the cell-caret policy.

    /// Helper: caret in insert at `at`, then the Esc seam (policy relayed
    /// without committing) followed by the backward grapheme step.
    ///
    /// Pins the line-clamped path (`cross_line_cursor = false`): these tests
    /// observe the seam timing through the at-line-edge behavior, which is only
    /// stable when the cell caret can't cross the newline. Cross-line wrapping
    /// (now the default) is covered by its own tests.
    fn esc_back_from_insert(buffer: &str, at: usize) -> Editor {
        let mut editor = vi_editor(buffer, PromptViMode::Insert);
        editor.set_cross_line_cursor(false);
        editor.line_buffer.set_insertion_point(at);
        editor.sync_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(
            Direction::Backward,
        )));
        editor
    }

    #[test]
    fn esc_back_steps_one_within_line() {
        // caret on the last 'c' (as after `i`): steps back onto the first 'c'
        let editor = esc_back_from_insert("aa bb cc", 7);
        assert_eq!(editor.insertion_point(), 6);
    }

    #[test]
    fn esc_back_at_line_end_does_not_double_step() {
        // caret appended past the end (as after `A`): the relay must NOT commit
        // and pull it back, or the step would land on 6 instead of the last 'c'
        let editor = esc_back_from_insert("aa bb cc", 8);
        assert_eq!(editor.insertion_point(), 7);
    }

    #[test]
    fn esc_back_at_line_start_stays_in_line() {
        // caret at column 0 of the second line: the cell-caret can't cross the
        // newline, so it stays put rather than jumping onto the line above
        let editor = esc_back_from_insert("ab\ncd", 3);
        assert_eq!(editor.insertion_point(), 3);
    }

    #[test]
    fn esc_back_on_trailing_empty_line_stays() {
        // the `cc`/`S`-then-Esc shape: caret on a blank last line stays there
        let editor = esc_back_from_insert("a\n\n", 3);
        assert_eq!(editor.insertion_point(), 3);
    }

    // The commit boundary also fires on the two state changes that bypass
    // `run_edit_command`: buffer replacement (history navigation) and edit-mode
    // transitions (e.g. Esc into Vi normal). Both were clamped by #1069 too.

    #[test]
    fn vi_normal_set_buffer_clamps_cursor() {
        // history navigation replaces the buffer (cursor lands at the end)
        let mut editor = vi_editor("", PromptViMode::Normal);
        editor.set_buffer("hello".to_string(), UndoBehavior::CreateUndoPoint);
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn vi_normal_set_buffer_clamps_multibyte() {
        let mut editor = vi_editor("", PromptViMode::Normal);
        editor.set_buffer("café".to_string(), UndoBehavior::CreateUndoPoint);
        assert_eq!(editor.insertion_point(), "caf".len());
    }

    #[test]
    fn entering_vi_normal_clamps_cursor() {
        // simulates Esc: the cursor sits past the end in insert mode, then the
        // mode flips to normal and the commit-on-mode-change pulls it back
        let mut editor = vi_editor("hello", PromptViMode::Insert);
        editor.run_edit_command(&EditCommand::MoveToEnd { select: false });
        assert_eq!(editor.insertion_point(), 5);
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn entering_vi_insert_does_not_move_cursor() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 4,
            select: false,
        });
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Insert));
        assert_eq!(editor.insertion_point(), 4);
    }

    // Selections built by selecting motions still cut the right bytes after the
    // commit boundary runs on every move — including across a multibyte grapheme.

    #[test]
    fn vi_normal_selection_cut_is_inclusive() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 0,
            select: false,
        });
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        // head on 'l' (byte 2); Vi-normal selection is inclusive → covers [0,3)
        assert_eq!(editor.get_selection(), Some((0, 3)));
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "lo");
        assert_eq!(editor.cut_buffer.get().0, "hel");
    }

    #[test]
    fn vi_normal_selection_cut_spans_multibyte_grapheme() {
        let mut editor = vi_editor("caféx", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 0,
            select: false,
        });
        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        // head on 'é' (byte 3); inclusive end extends over both bytes of é → 5
        assert_eq!(editor.get_selection(), Some((0, 5)));
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "x");
        assert_eq!(editor.cut_buffer.get().0, "café");
    }

    // Regression for #893: a single selecting move must extend the selection by
    // exactly one grapheme, not two. The default (exclusive) policy means the
    // selection end is the head — the cursor-as-range model has no place for the
    // off-by-one that produced the two-char grab.
    #[test]
    fn shift_select_grabs_one_grapheme_per_step() {
        let mut editor = editor_with("hello");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((0, 1)));
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((0, 2)));
    }

    #[test]
    fn shift_select_one_grapheme_over_multibyte() {
        let mut editor = editor_with("café"); // 'é' is 2 bytes at [3,5)
        editor.move_to_position(3, false);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        assert_eq!(editor.get_selection(), Some((3, 5))); // one grapheme, not two
    }

    #[test]
    fn select_all_captures_inclusivity_at_plant_time() {
        // `select_all` plants its anchor outside the motion path; it must still
        // capture inclusivity, so a later mode switch (vi normal → insert here)
        // can't shrink the selection by the final grapheme.
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::SelectAll);
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Insert));
        assert_eq!(editor.get_selection(), Some((0, 5)));
    }

    // --- granularity gate -------------------------------------------------
    //
    // dd/dgg/dG/yy (and the cgg/cG blank-line variant) currently lower to
    // dedicated linewise commands. The granularity axis will re-lower them
    // through the operator verbs; these golden masters pin the exact buffer,
    // cursor, cut content, and — crucially — the `Granularity::LineWise` register
    // tag (what makes paste linewise) so the re-lowering stays behavior-preserving.
    // Buffer "aaa\nbbb\nccc": a@0..3, \n@3, b@4..7, \n@7, c@8..11; cursor in "bbb".

    fn linewise_editor() -> Editor {
        let mut editor = editor_with("aaa\nbbb\nccc");
        editor.move_to_position(5, false);
        editor
    }

    #[test]
    fn cut_current_line_is_linewise() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::CutCurrentLine);
        assert_eq!(editor.get_buffer(), "aaa\nccc");
        assert_eq!(editor.insertion_point(), 4);
        let (content, mode) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\n");
        assert!(matches!(mode, Granularity::LineWise));
    }

    // --- explicit-granularity target (Phase 2 step 3 makes these pass) ----
    //
    // The new vocab: `dd` = `Cut(LineEdge, LineWise)`, `dgg` = `Cut(BufferEdge(Bwd),
    // LineWise)`, `dG` = `Cut(BufferEdge(Fwd), LineWise)`. `operate` must snap a
    // LineWise span out to whole lines (incl. the `dG` leading-\n fixup) and tag
    // the register `LineWise`. These mirror the dedicated-command golden masters
    // above. (`operate` ignores granularity until step 3, so they start red.)

    #[test]
    fn cut_lineedge_linewise_matches_current_line() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\nccc");
        assert_eq!(editor.insertion_point(), 4);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\n");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_bufferedge_back_linewise_cuts_through_current_line() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::BufferEdge(Direction::Backward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "ccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb\n");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_bufferedge_fwd_linewise_eats_leading_newline() {
        // the `dG` fixup: reaching buffer end consumes the *preceding* \n so no
        // stray blank line is left.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::BufferEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa");
        assert_eq!(editor.insertion_point(), 3);
        let (content, gran) = editor.cut_buffer.get();
        // The buffer-end fixup eats the *preceding* `\n` from the deletion only;
        // the register keeps content (no leading `\n`) so paste stays blank-safe.
        assert_eq!(content, "bbb\nccc");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn copy_lineedge_linewise_tags_register_nondestructively() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Copy {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\nbbb\nccc"); // unchanged
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\n");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_lineedge_charwise_stays_charwise() {
        // CharWise must NOT snap: `d$` from mid-line cuts to the line end only.
        let mut editor = linewise_editor(); // cursor 5, inside "bbb"
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\nb\nccc"); // removed "bb"
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bb");
        assert_eq!(gran, Granularity::CharWise);
    }

    #[test]
    fn cut_line_down_linewise_deletes_current_and_next() {
        // `dj` from "bbb" deletes bbb + ccc, linewise.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Line(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa");
        let (content, gran) = editor.cut_buffer.get();
        // Register keeps the line content only — no leading `\n` — so a linewise
        // paste does not gain a spurious blank line.
        assert_eq!(content, "bbb\nccc");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_line_up_linewise_deletes_current_and_prev() {
        // `dk` from "bbb" deletes aaa + bbb, linewise.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Line(Direction::Backward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "ccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb\n");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_line_down_on_last_line_cuts_only_that_line() {
        // `dj` on the last line: the motion stays put (no line below), so the
        // linewise snap consumes just the current line — including its
        // *leading* `\n` (the buffer-end fixup), leaving no stray blank line.
        let mut editor = editor_with("aaa\nbbb\nccc");
        editor.move_to_position(9, false); // inside "ccc"
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Line(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\nbbb");
        let (content, gran) = editor.cut_buffer.get();
        // Register keeps content only; the leading `\n` is eaten from the
        // deletion alone, keeping a later linewise paste blank-safe.
        assert_eq!(content, "ccc");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn dd_on_last_line_then_paste_leaves_no_blank_line() {
        // Regression: a linewise cut of the last line stored the deletion span
        // (with its eaten leading `\n`) in the register, so a later linewise
        // paste re-introduced that newline as a spurious blank line.
        let mut editor = vi_editor("ab\ncd", PromptViMode::Normal);
        editor.line_buffer.set_insertion_point(3); // on "cd"
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "ab");
        assert_eq!(editor.cut_buffer.get().0, "cd"); // content only
        editor.run_edit_command(&EditCommand::PasteCutBufferBefore);
        assert_eq!(editor.get_buffer(), "cd\nab"); // no leading blank line
    }

    #[test]
    fn word_operator_never_splits_a_combining_grapheme() {
        // Regression: NFD "aé" = 'a' + 'e' + U+0301 (combining acute). The
        // combining mark classifies differently, so the word-start boundary lands
        // mid-grapheme; `dw` must floor to a grapheme boundary rather than cut
        // mid-cluster and strand the combining mark at the buffer start.
        let mut editor = vi_editor("ae\u{0301}", PromptViMode::Normal);
        editor.line_buffer.set_insertion_point(0);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Word {
                kind: WordKind::Word,
                edge: WordEdge::Start,
                direction: Direction::Forward,
            },
            granularity: Granularity::CharWise,
        });
        let buf = editor.get_buffer();
        assert!(
            !buf.starts_with('\u{0301}'),
            "word operator orphaned a combining mark: {buf:?}"
        );
    }

    #[test]
    fn paste_after_over_selection_does_not_skip_a_grapheme() {
        // Regression: paste-after replaced the selection then `move_right`,
        // skipping the first remaining grapheme, so the register landed one
        // grapheme too late ("hello" + select "hel" + register "xyz" → "lxyzo").
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.cut_buffer.set("xyz", Granularity::CharWise);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 0,
            select: false,
        });
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        assert_eq!(editor.get_selection(), Some((0, 3))); // "hel"
        editor.run_edit_command(&EditCommand::PasteCutBufferAfter);
        assert_eq!(editor.get_buffer(), "xyzlo");
    }

    #[test]
    fn paste_after_linewise_on_last_line_lands_below() {
        // Regression: `p` on the last line fell back to the line start (no line
        // below), pasting *above* like `P`.
        let mut editor = editor_with("ab");
        editor.cut_buffer.set("cd", Granularity::LineWise);
        editor.line_buffer.set_insertion_point(0);
        editor.run_edit_command(&EditCommand::PasteCutBufferAfter);
        assert_eq!(editor.get_buffer(), "ab\ncd"); // below, not "cd\nab"
    }

    #[test]
    fn paste_after_linewise_into_empty_buffer_has_no_blank_line() {
        // Regression (introduced by the last-line paste fix): `dd` on the only
        // line empties the buffer, and `p` must not prepend a blank line.
        let mut editor = editor_with("");
        editor.cut_buffer.set("ab", Granularity::LineWise);
        editor.run_edit_command(&EditCommand::PasteCutBufferAfter);
        assert_eq!(editor.get_buffer(), "ab");
    }

    #[test]
    fn paste_after_linewise_middle_line_lands_below() {
        let mut editor = editor_with("a\nb");
        editor.cut_buffer.set("X", Granularity::LineWise);
        editor.line_buffer.set_insertion_point(0); // on line "a"
        editor.run_edit_command(&EditCommand::PasteCutBufferAfter);
        assert_eq!(editor.get_buffer(), "a\nX\nb");
    }

    #[test]
    fn visual_replace_char_replaces_whole_selection() {
        let mut editor = vi_editor("hello", PromptViMode::Normal);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 0,
            select: false,
        });
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        assert_eq!(editor.get_selection(), Some((0, 3))); // "hel"
        editor.run_edit_command(&EditCommand::ReplaceChar('x'));
        assert_eq!(editor.get_buffer(), "xxxlo");
    }

    #[test]
    fn cut_line_up_on_first_line_cuts_only_that_line() {
        // `dk` on the first line: no line above, so only the current line goes.
        let mut editor = editor_with("aaa\nbbb\nccc");
        editor.move_to_position(1, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Line(Direction::Backward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "bbb\nccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\n");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn cut_line_down_on_single_line_buffer_empties_it() {
        let mut editor = editor_with("aaa");
        editor.move_to_position(1, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Line(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "");
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa");
        assert_eq!(gran, Granularity::LineWise);
    }

    // --- the Change verb (vi linewise change: `cc`/`cj`/`cgg`/`cG`) ---------
    //
    // Change is Cut with the LineWise snap keeping the line terminators: the
    // spanned lines' *content* is consumed and one blank line remains for the
    // re-entered insert mode. The register is tagged LineWise like vim's.

    #[test]
    fn change_lineedge_linewise_blanks_current_line() {
        // `cc` from "bbb": content gone, blank line kept, cursor at its start.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\n\nccc");
        assert_eq!(editor.insertion_point(), 4);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bbb");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn change_line_down_blanks_current_and_next() {
        // `cj` from "bbb": bbb + ccc collapse into one blank line.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::Line(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\n");
        assert_eq!(editor.insertion_point(), 4);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\nccc");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn change_line_up_blanks_current_and_prev() {
        // `ck` from "bbb": aaa + bbb collapse into one blank line.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::Line(Direction::Backward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "\nccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn change_bufferedge_back_matches_legacy_leave_blank_command() {
        // `cgg` — must reproduce `CutFromStartLinewise { leave_blank_line: true }`
        // (the golden master above) exactly.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::BufferEdge(Direction::Backward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "\nccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn change_bufferedge_fwd_matches_legacy_leave_blank_command() {
        // `cG` — must reproduce `CutToEndLinewise { leave_blank_line: true }`:
        // no buffer-end fixup; the preceding `\n` stays so a blank line remains.
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::BufferEdge(Direction::Forward),
            granularity: Granularity::LineWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\n");
        assert_eq!(editor.insertion_point(), 4);
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\nccc");
        assert_eq!(gran, Granularity::LineWise);
    }

    #[test]
    fn change_charwise_behaves_like_cut() {
        // For CharWise spans Change and Cut are the same operator.
        let mut editor = linewise_editor(); // cursor 5, inside "bbb"
        editor.run_edit_command(&EditCommand::Change {
            target: MotionTarget::LineEdge(Direction::Forward),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "aaa\nb\nccc"); // removed "bb"
        let (content, gran) = editor.cut_buffer.get();
        assert_eq!(content, "bb");
        assert_eq!(gran, Granularity::CharWise);
    }

    #[test]
    fn cut_from_start_linewise_cuts_through_current_line() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::CutFromStartLinewise {
            leave_blank_line: false,
        });
        assert_eq!(editor.get_buffer(), "ccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, mode) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb\n");
        assert!(matches!(mode, Granularity::LineWise));
    }

    #[test]
    fn cut_from_start_linewise_leave_blank_keeps_empty_line() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::CutFromStartLinewise {
            leave_blank_line: true,
        });
        assert_eq!(editor.get_buffer(), "\nccc");
        assert_eq!(editor.insertion_point(), 0);
        let (content, mode) = editor.cut_buffer.get();
        assert_eq!(content, "aaa\nbbb");
        assert!(matches!(mode, Granularity::LineWise));
    }

    #[test]
    fn cut_to_end_linewise_cuts_from_current_line() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::CutToEndLinewise {
            leave_blank_line: false,
        });
        assert_eq!(editor.get_buffer(), "aaa");
        assert_eq!(editor.insertion_point(), 3);
        let (content, mode) = editor.cut_buffer.get();
        // Register holds content only (no leading `\n`); the deletion alone eats
        // the preceding terminator, so a later linewise paste stays blank-safe.
        assert_eq!(content, "bbb\nccc");
        assert!(matches!(mode, Granularity::LineWise));
    }

    #[test]
    fn copy_current_line_is_linewise_and_nondestructive() {
        let mut editor = linewise_editor();
        editor.run_edit_command(&EditCommand::CopyCurrentLine);
        assert_eq!(editor.get_buffer(), "aaa\nbbb\nccc"); // unchanged
        let (content, mode) = editor.cut_buffer.get();
        assert_eq!(content, "bbb\n");
        assert!(matches!(mode, Granularity::LineWise));
    }

    #[rstest]
    #[case("abc def ghi", 11, "abc def ")]
    #[case("abc def-ghi", 11, "abc def-")]
    #[case("abc def.ghi", 11, "abc ")]
    fn test_cut_word_left(#[case] input: &str, #[case] position: usize, #[case] expected: &str) {
        let mut editor = editor_with(input);
        editor.line_buffer.set_insertion_point(position);

        editor.cut_word_left();

        assert_eq!(editor.get_buffer(), expected);
    }

    #[rstest]
    #[case("abc def ghi", 11, "abc def ")]
    #[case("abc def-ghi", 11, "abc ")]
    #[case("abc def.ghi", 11, "abc ")]
    #[case("abc def gh ", 11, "abc def ")]
    fn test_cut_big_word_left(
        #[case] input: &str,
        #[case] position: usize,
        #[case] expected: &str,
    ) {
        let mut editor = editor_with(input);
        editor.line_buffer.set_insertion_point(position);

        editor.cut_big_word_left();

        assert_eq!(editor.get_buffer(), expected);
    }

    #[rstest]
    #[case("hello world", 0, 'l', 1, false, "lo world")]
    #[case("hello world", 0, 'l', 1, true, "llo world")]
    #[ignore = "Deleting two consecutive chars is not implemented correctly and needs the multiplier explicitly."]
    #[case("hello world", 0, 'l', 2, false, "o world")]
    #[case("hello world", 0, 'h', 1, false, "hello world")]
    #[case("hello world", 0, 'l', 3, true, "ld")]
    #[case("hello world", 4, 'o', 1, true, "hellorld")]
    #[case("hello world", 4, 'w', 1, false, "hellorld")]
    #[case("hello world", 4, 'o', 1, false, "hellrld")]
    fn test_cut_right_until_char(
        #[case] input: &str,
        #[case] position: usize,
        #[case] search_char: char,
        #[case] repeat: usize,
        #[case] before_char: bool,
        #[case] expected: &str,
    ) {
        let mut editor = editor_with(input);
        editor.line_buffer.set_insertion_point(position);
        for _ in 0..repeat {
            editor.cut_right_until_char(search_char, before_char, true);
        }
        assert_eq!(editor.get_buffer(), expected);
    }

    #[rstest]
    #[case("abc", 1, 'X', "aXc")]
    #[case("abc", 1, '🔄', "a🔄c")]
    #[case("a🔄c", 1, 'X', "aXc")]
    #[case("a🔄c", 1, '🔀', "a🔀c")]
    fn test_replace_char(
        #[case] input: &str,
        #[case] position: usize,
        #[case] replacement: char,
        #[case] expected: &str,
    ) {
        let mut editor = editor_with(input);
        editor.line_buffer.set_insertion_point(position);

        editor.replace_char(replacement);

        assert_eq!(editor.get_buffer(), expected);
    }

    #[test]
    fn visual_replace_char_replaces_only_the_caret_grapheme() {
        // Regression: under a Block/visual cursor head sits one grapheme past
        // the caret. `replace_char` paired the caret-based insertion point with
        // a head-based delete, clearing two graphemes ("hello" -> "lxlo").
        // Collapsing to the caret first keeps it a single-grapheme replace.
        let mut editor = vi_editor("hello", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToStart { select: false });
        editor.update_selection_anchor(true); // Block covers 'h': caret 0, head 1
        editor.replace_char('x');
        assert_eq!(editor.get_buffer(), "xello");
    }

    #[test]
    fn visual_move_line_does_not_panic_across_line_boundary() {
        // Regression: `move_line_*` measured the grapheme column from the caret
        // while `current_line_range` used the head; a selection straddling a
        // line boundary made `range.start > caret` and panicked on the slice.
        let mut editor = vi_editor("a\nbc", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToStart { select: false });
        editor.update_selection_anchor(true);
        // Drive vertical moves with the selection active — must not panic.
        editor.run_edit_command(&EditCommand::MoveLineDown { select: true });
        editor.run_edit_command(&EditCommand::MoveLineDown { select: true });
        editor.run_edit_command(&EditCommand::MoveLineUp { select: true });
    }

    #[test]
    fn linewise_cut_last_line_eats_whole_crlf_terminator() {
        // Regression (#10): cutting the last line must consume the whole
        // *preceding* terminator. On a CRLF buffer — reachable via a recalled
        // Windows history entry or `EditCommand::InsertString` — stepping back a
        // single byte left an orphan `\r` ("ab\r\ncd" + linewise cut → "ab\r").
        let mut editor = editor_with("ab\r\ncd");
        editor.operate(
            Cursor::point(5), // on 'd', the last line
            OperatorVerb::Cut,
            Granularity::LineWise,
        );
        assert_eq!(editor.get_buffer(), "ab");

        // The lone-LF case is unchanged.
        let mut editor = editor_with("ab\ncd");
        editor.operate(Cursor::point(4), OperatorVerb::Cut, Granularity::LineWise);
        assert_eq!(editor.get_buffer(), "ab");
    }

    #[test]
    fn cut_to_end_linewise_eats_whole_crlf_terminator() {
        // Sibling of #10 on the CutToEndLinewise path (a public EditCommand):
        // stepping back to the `\n` would orphan the `\r` of a CRLF terminator.
        let mut editor = editor_with("x\r\ncd");
        editor.line_buffer.set_insertion_point(3); // on 'c', the second line
        editor.cut_from_end_linewise(false);
        assert_eq!(editor.get_buffer(), "x");
        assert!(!editor.get_buffer().contains('\r'));
    }

    fn selected_text(editor: &Editor) -> String {
        let c = editor.line_buffer().cursor();
        editor.get_buffer()[c.start()..c.end()].to_string()
    }

    #[test]
    fn visual_move_word_right_end_select_covers_last_grapheme() {
        // #9: a selecting word-end motion must cover the word's last grapheme
        // (inclusive block geometry), not stop one grapheme short.
        let mut editor = vi_editor("foo bar", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToStart { select: false });
        editor.run_edit_command(&EditCommand::MoveWordRightEnd { select: true });
        assert_eq!(selected_text(&editor), "foo");
    }

    #[test]
    fn visual_move_to_line_start_after_end_keeps_anchor_grapheme() {
        // #12: extend from 'd' to the line end, then back to the line start. The
        // grapheme the selection started on ('d') must stay covered on reversal
        // (vim keeps "abc d"), which needs the put_cursor anchor-flip.
        let mut editor = vi_editor("abc def", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 4, // on 'd'
            select: false,
        });
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: true });
        editor.run_edit_command(&EditCommand::MoveToLineStart { select: true });
        assert_eq!(selected_text(&editor), "abc d");
    }

    #[test]
    fn visual_line_jk_keeps_anchor_grapheme_covered() {
        // #13: vertical visual motion must keep the grapheme the selection
        // started on covered, even across a direction reversal. "x\ny\nz",
        // select 'y' (byte 2), then j/k/k. The old raw `set_head` path dropped
        // the anchor; routing through put_cursor keeps it.
        let mut editor = vi_editor("x\ny\nz", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 2, // 'y'
            select: false,
        });
        editor.update_selection_anchor(true);
        editor.run_edit_command(&EditCommand::MoveLineDown { select: true });
        editor.run_edit_command(&EditCommand::MoveLineUp { select: true });
        editor.run_edit_command(&EditCommand::MoveLineUp { select: true });

        let c = editor.line_buffer().cursor();
        assert!(
            c.start() <= 2 && 2 < c.end(),
            "byte 2 ('y', the anchor) must stay covered; selection was {:?}",
            c.start()..c.end()
        );
    }

    #[test]
    fn visual_line_jk_preserves_caret_column() {
        // Starting visual at a word end (caret one grapheme before the trailing
        // space), j then k must return the caret to its column — the column is
        // the caret's, not the head's, which under a Block cursor sits one
        // grapheme further on (onto the space) and drifts the selection by a
        // grapheme on every vertical move. "ab cd\nef gh": 'b' is byte 1.
        let mut editor = vi_editor("ab cd\nef gh", PromptViMode::Visual);
        editor.run_edit_command(&EditCommand::MoveToPosition {
            position: 1, // 'b', the end of "ab"
            select: false,
        });
        editor.update_selection_anchor(true);
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveLineDown { select: true });
            editor.run_edit_command(&EditCommand::MoveLineUp { select: true });
            assert_eq!(
                editor.insertion_point(),
                1,
                "caret drifted off 'b' after a j/k round-trip"
            );
        }
    }

    #[test]
    fn select_until_char_in_bar_mode_opens_selection() {
        // Regression: `MoveRightUntil { select: true }` from a point in a
        // bar-caret mode (emacs / vi-insert) must open a selection. The old
        // `update_selection_anchor` + raw `set_head` path anchored on a point
        // (empty cursor) and the move then collapsed it, dropping the anchor.
        let mut editor = editor_with("This is a test!"); // default = Between (bar)
        editor.line_buffer.set_insertion_point(0);
        editor.run_edit_command(&EditCommand::MoveRightUntil {
            c: 's',
            select: true,
        });
        // 's' is byte 3; a bar selection is exclusive, covering bytes [0, 3).
        assert_eq!(editor.get_selection(), Some((0, 3)));

        // The backward form likewise opens a selection.
        let mut editor = editor_with("This is a test!");
        editor
            .line_buffer
            .set_insertion_point(editor.line_buffer.len());
        editor.run_edit_command(&EditCommand::MoveLeftUntil {
            c: 'T',
            select: true,
        });
        assert!(editor.get_selection().is_some());
    }

    fn str_to_edit_commands(s: &str) -> Vec<EditCommand> {
        s.chars().map(EditCommand::InsertChar).collect()
    }

    #[test]
    fn test_undo_insert_works_on_work_boundaries() {
        let mut editor = editor_with("This is  a");
        for cmd in str_to_edit_commands(" test") {
            editor.run_edit_command(&cmd);
        }
        assert_eq!(editor.get_buffer(), "This is  a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is  a");
        editor.run_edit_command(&EditCommand::Redo);
        assert_eq!(editor.get_buffer(), "This is  a test");
    }

    #[test]
    fn test_undo_backspace_works_on_word_boundaries() {
        let mut editor = editor_with("This is  a test");
        for _ in 0..6 {
            editor.run_edit_command(&EditCommand::Backspace);
        }
        assert_eq!(editor.get_buffer(), "This is  ");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is  a");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is  a test");
    }

    #[test]
    fn test_undo_delete_works_on_word_boundaries() {
        let mut editor = editor_with("This  is a test");
        editor.line_buffer.set_insertion_point(0);
        for _ in 0..7 {
            editor.run_edit_command(&EditCommand::Delete);
        }
        assert_eq!(editor.get_buffer(), "s a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "is a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This  is a test");
    }

    #[test]
    fn test_undo_insert_with_newline() {
        let mut editor = editor_with("This is a");
        for cmd in str_to_edit_commands(" \n test") {
            editor.run_edit_command(&cmd);
        }
        assert_eq!(editor.get_buffer(), "This is a \n test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a \n");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a");
    }

    #[test]
    fn test_undo_backspace_with_newline() {
        let mut editor = editor_with("This is a \n test");
        for _ in 0..8 {
            editor.run_edit_command(&EditCommand::Backspace);
        }
        assert_eq!(editor.get_buffer(), "This is ");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a \n");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a \n test");
    }

    #[test]
    fn test_undo_backspace_with_crlf() {
        let mut editor = editor_with("This is a \r\n test");
        for _ in 0..8 {
            editor.run_edit_command(&EditCommand::Backspace);
        }
        assert_eq!(editor.get_buffer(), "This is ");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a \r\n");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This is a \r\n test");
    }

    #[test]
    fn test_cut_char_left_cuts_char_and_puts_in_buffer() {
        let mut editor = editor_with("hello");
        editor.line_buffer.set_insertion_point(3);
        editor.run_edit_command(&EditCommand::CutCharLeft);
        assert_eq!(editor.get_buffer(), "helo");
        assert_eq!(editor.cut_buffer.get().0, "l");
    }

    #[test]
    fn test_cut_char_left_at_beginning_of_line() {
        let starting_line = "This is a single line test";
        let mut editor = editor_with(starting_line);
        editor.line_buffer.set_insertion_point(0);
        editor.run_edit_command(&EditCommand::CutCharLeft);
        assert_eq!(editor.get_buffer(), starting_line);
    }

    #[test]
    fn test_cut_char_left_at_beginning_of_2nd_line() {
        let starting_line = "This is a \r\nmulti-line test";
        let mut editor = editor_with(starting_line);
        editor.line_buffer.set_insertion_point(12);
        editor.run_edit_command(&EditCommand::CutCharLeft);
        assert_eq!(editor.get_buffer(), starting_line);
    }

    #[test]
    fn test_cut_char_left_clears_stale_selection() {
        let mut editor = editor_with("hello");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.line_buffer.set_insertion_point(2);
        editor.update_selection_anchor(true);
        editor.run_edit_command(&EditCommand::MoveRight { select: true });
        // stale selection present
        editor.run_edit_command(&EditCommand::CutCharLeft);
        assert_eq!(editor.get_buffer(), "helo");
        assert_eq!(editor.cut_buffer.get().0, "l");
        assert!(editor.get_selection().is_none());
    }

    #[test]
    fn test_cut_selection_linewise_single_line() {
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.line_buffer.set_insertion_point(2);
        editor.update_selection_anchor(true);
        // Select "llo" (positions 2..5 inclusive in normal mode)
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::LineWise,
        });
        // X in visual mode should cut the entire line
        assert_eq!(editor.get_buffer(), "");
    }

    #[test]
    fn test_cut_selection_linewise_multi_line() {
        let mut editor = editor_with("first\nsecond\nthird");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        // Place cursor in "second", select a portion
        editor.line_buffer.set_insertion_point(8); // 's' of "second"
        editor.update_selection_anchor(true);
        for _ in 0..2 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::LineWise,
        });
        // X in visual mode should cut the entire line(s) covered by the selection
        assert_eq!(editor.get_buffer(), "first\nthird");
    }

    #[test]
    fn test_cut_selection_linewise_spanning_two_lines() {
        let mut editor = editor_with("first\nsecond\nthird");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        // Select from end of "first" to beginning of "second"
        editor.line_buffer.set_insertion_point(3); // in "first"
        editor.update_selection_anchor(true);
        for _ in 0..6 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::LineWise,
        });
        // Should cut both "first\n" and "second\n"
        assert_eq!(editor.get_buffer(), "third");
    }

    #[test]
    fn test_undo_delete_with_newline() {
        let mut editor = editor_with("This \n is a test");
        editor.line_buffer.set_insertion_point(0);
        for _ in 0..8 {
            editor.run_edit_command(&EditCommand::Delete);
        }
        assert_eq!(editor.get_buffer(), "s a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "is a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "\n is a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This \n is a test");
    }

    #[test]
    fn test_undo_delete_with_crlf() {
        // CLRF delete is a special case, since the first character of the
        // grapheme is \r rather than \n
        let mut editor = editor_with("This \r\n is a test");
        editor.line_buffer.set_insertion_point(0);
        for _ in 0..8 {
            editor.run_edit_command(&EditCommand::Delete);
        }
        assert_eq!(editor.get_buffer(), "s a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "is a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "\r\n is a test");
        editor.run_edit_command(&EditCommand::Undo);
        assert_eq!(editor.get_buffer(), "This \r\n is a test");
    }

    #[test]
    fn test_swap_cursor_and_anchor() {
        let mut editor = editor_with("This is some test content");
        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        assert_eq!(editor.line_buffer().selection_anchor(), Some(0));
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.get_selection(), Some((0, 3)));

        editor.run_edit_command(&EditCommand::SwapCursorAndAnchor);
        assert_eq!(editor.line_buffer().selection_anchor(), Some(3));
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.get_selection(), Some((0, 3)));

        editor.run_edit_command(&EditCommand::SwapCursorAndAnchor);
        assert_eq!(editor.line_buffer().selection_anchor(), Some(0));
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.get_selection(), Some((0, 3)));
    }

    /// Drive a single block-caret grapheme motion from `start` in vi normal mode
    /// and return the resulting insertion point.
    #[cfg(test)]
    fn normal_mode_step(buf: &str, start: usize, cross: bool, cmd: &EditCommand) -> usize {
        let mut e = editor_with(buf);
        e.set_cross_line_cursor(cross);
        e.line_buffer.set_insertion_point(start);
        e.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        e.run_edit_command(cmd);
        e.insertion_point()
    }

    #[test]
    fn cross_line_cursor_on_crosses_newline() {
        let r = &EditCommand::MoveRight { select: false };
        let l = &EditCommand::MoveLeft { select: false };
        // "ab\ncd": l at end of line 1 ('b'=1) lands on line 2's first char ('c'=3);
        // h at line 2's start ('c'=3) lands on line 1's last char ('b'=1).
        assert_eq!(normal_mode_step("ab\ncd", 1, true, r), 3);
        assert_eq!(normal_mode_step("ab\ncd", 3, true, l), 1);
        // `\r\n` is one grapheme: crossing skips the whole terminator.
        // "ab\r\ncd": 'b'=1, 'c'=4.
        assert_eq!(normal_mode_step("ab\r\ncd", 1, true, r), 4);
        assert_eq!(normal_mode_step("ab\r\ncd", 4, true, l), 1);
    }

    #[test]
    fn cross_line_cursor_off_clamps_to_line() {
        let r = &EditCommand::MoveRight { select: false };
        let l = &EditCommand::MoveLeft { select: false };
        // Opt-out (vim default): the motion stops at the line edge instead of
        // crossing — `l` from 'b' does not reach line 2's 'c' (3), `h` from 'c'
        // does not reach line 1's 'b' (1).
        assert_ne!(normal_mode_step("ab\ncd", 1, false, r), 3);
        assert_ne!(normal_mode_step("ab\ncd", 3, false, l), 1);
    }

    #[test]
    fn append_at_buffer_end_appends_past_block_caret() {
        // Regression: accepting a history hint in vi normal mode must append
        // *after* the last char. The block caret rests on the last grapheme, so
        // a plain insert would split it ("abc" + "def" -> "abdefc").
        let mut editor = editor_with("abc");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: false });
        editor.prepare_append_at_buffer_end();
        editor.run_edit_command(&EditCommand::InsertString("def".into()));
        assert_eq!(editor.get_buffer(), "abcdef");
        // Multibyte last grapheme must not be split either.
        let mut editor = editor_with("caf\u{e9}");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: false });
        editor.prepare_append_at_buffer_end();
        editor.run_edit_command(&EditCommand::InsertString("X".into()));
        assert_eq!(editor.get_buffer(), "caf\u{e9}X");
    }

    #[test]
    fn cursor_at_buffer_end_holds_on_last_grapheme_in_normal_mode() {
        // Regression: in vi normal mode the resting cursor sits *on* the last
        // grapheme (OnGrapheme pulls the head back), so `caret()` is one inward
        // from `len`. "At buffer end" must still hold there, or a history hint
        // never completes in normal mode (it did before the cursor refactor).
        let mut editor = editor_with("abc");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: false });
        assert!(editor.is_cursor_at_buffer_end());
        // Multibyte: resting on `é` (a 2-byte grapheme) must report end too.
        let mut editor = editor_with("caf\u{e9}");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::MoveToLineEnd { select: false });
        assert!(editor.is_cursor_at_buffer_end());
        // Not at the end: resting on the first char of a multi-char buffer.
        let mut editor = editor_with("abc");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.run_edit_command(&EditCommand::MoveToLineStart { select: false });
        assert!(!editor.is_cursor_at_buffer_end());
        // An active selection reaching the end is NOT a clean end point: a hint
        // completing here would delete the selection. (vi visual extending to len.)
        let mut editor = editor_with("abc");
        editor.line_buffer.set_insertion_point(0);
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.update_selection_anchor(true);
        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        assert!(!editor.is_cursor_at_buffer_end());
    }

    #[test]
    fn test_vi_normal_mode_inclusive_selection() {
        let mut editor = editor_with("This is some test content");
        editor.line_buffer.set_insertion_point(0);
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.update_selection_anchor(true);

        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }
        assert_eq!(editor.line_buffer().selection_anchor(), Some(0));
        assert_eq!(editor.insertion_point(), 3);
        // In Vi normal mode, selection should be inclusive (include character at position 3)
        assert_eq!(editor.get_selection(), Some((0, 4)));
    }

    #[test]
    fn test_vi_normal_mode_inclusive_selection_backward() {
        let mut editor = editor_with("This is some test content");
        editor.line_buffer.set_insertion_point(4); // Start at position 4 ('i')
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.update_selection_anchor(true);

        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        }
        // Inclusivity is geometric now: the anchor flips onto the far edge of
        // its grapheme (4 → 5) so the char at 4 stays covered, instead of an
        // anchor-stays-at-4 + query-time `+1`. The selected span is unchanged.
        assert_eq!(editor.line_buffer().selection_anchor(), Some(5));
        assert_eq!(editor.insertion_point(), 1); // cursor at position 1 ('h')
                                                 // In Vi normal mode, selection should be inclusive from cursor to anchor+1
                                                 // So it should select from position 1 to 5 (inclusive of char at position 4)
        assert_eq!(editor.get_selection(), Some((1, 5)));
    }

    #[test]
    fn test_vi_normal_mode_cut_selection_backward() {
        let mut editor = editor_with("This is some test content");

        editor.line_buffer.set_insertion_point(4); // Start at position 4 (' ')
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));
        editor.update_selection_anchor(true);

        for _ in 0..3 {
            editor.run_edit_command(&EditCommand::MoveLeft { select: true });
        }

        // Should select "his " (from position 1 to 5, inclusive of char at position 4)
        assert_eq!(editor.get_selection(), Some((1, 5)));

        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });

        // After cutting, should have "Tis some test content" (removed "his ")
        assert_eq!(editor.get_buffer(), "Tis some test content");
        assert_eq!(editor.insertion_point(), 1); // cursor should be at start of cut
    }

    #[test]
    fn test_vi_visual_mode_c_command() {
        // Test the exact scenario: select in visual mode, then press 'c'
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        // Start at position 0, enter visual mode by selecting
        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        // Move right 4 characters to select "hello" (from pos 0 to pos 4)
        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        // In vi normal mode, this should be inclusive selection
        // So we should select "hello" (positions 0-4, inclusive of position 4)
        assert_eq!(editor.get_selection(), Some((0, 5))); // should include character at position 4

        // Now simulate pressing 'c' - this should cut the selection
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });

        // Should have " world" left (removed "hello")
        assert_eq!(editor.get_buffer(), " world");
        assert_eq!(editor.insertion_point(), 0);
    }

    #[test]
    fn test_vi_normal_mode_c_command_with_selection() {
        // Test the exact issue: c command in vi normal mode with selection
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        // Start at position 0, create selection by moving cursor
        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        // Move right to select "hello" (positions 0-4, should be inclusive of pos 4)
        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        // In vi normal mode, selection should include character at cursor position
        assert_eq!(editor.get_selection(), Some((0, 5))); // inclusive selection

        // Now simulate pressing 'c' - this should cut the selection
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });

        // Should have " world" left (removed "hello" including the 'o')
        assert_eq!(editor.get_buffer(), " world");
        assert_eq!(editor.insertion_point(), 0);
    }

    #[cfg(feature = "system_clipboard")]
    mod without_system_clipboard {
        use super::*;
        #[test]
        fn test_cut_selection_system() {
            let mut editor = editor_with("This is a test!");
            // Build the whole-buffer selection head-first: under the unified
            // Cursor model, anchoring where the head already sits makes an empty
            // cursor that the next head move would collapse, so move the head to
            // the start first, then drop the anchor at the end.
            editor.line_buffer.set_insertion_point(0);
            editor
                .line_buffer
                .set_selection_anchor(Some(editor.line_buffer.len()));
            editor.run_edit_command(&EditCommand::CutSelectionSystem);
            assert!(editor.line_buffer.get_buffer().is_empty());
        }
        #[test]
        fn test_copypaste_selection_system() {
            let s = "This is a test!";
            let mut editor = editor_with(s);
            // Head-first selection build; see `test_cut_selection_system`.
            editor.line_buffer.set_insertion_point(0);
            editor
                .line_buffer
                .set_selection_anchor(Some(editor.line_buffer.len()));
            editor.run_edit_command(&EditCommand::CopySelectionSystem);
            editor.run_edit_command(&EditCommand::PasteSystem);
            pretty_assertions::assert_eq!(editor.line_buffer.len(), s.len() * 2);
        }
    }

    #[test]
    fn test_cut_inside_brackets() {
        let mut editor = editor_with("foo(bar)baz");
        editor.move_to_position(5, false); // Move inside brackets
        editor.cut_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo()baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar");

        // Test with cursor outside brackets
        let mut editor = editor_with("foo(bar)baz");
        editor.move_to_position(0, false);
        editor.cut_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo()baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar");

        // Test with no matching brackets
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(4, false);
        editor.cut_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo bar baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "");
    }

    #[test]
    fn test_cut_inside_quotes() {
        let mut editor = editor_with("foo\"bar\"baz");
        editor.move_to_position(5, false); // Move inside quotes
        editor.cut_inside_pair('"', '"');
        assert_eq!(editor.get_buffer(), "foo\"\"baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar");

        // Test with cursor outside quotes
        let mut editor = editor_with("foo\"bar\"baz");
        editor.move_to_position(0, false);
        editor.cut_inside_pair('"', '"');
        assert_eq!(editor.get_buffer(), "foo\"\"baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar");

        // Test with no matching quotes
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(4, false);
        editor.cut_inside_pair('"', '"');
        assert_eq!(editor.get_buffer(), "foo bar baz");
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn test_cut_inside_nested() {
        let mut editor = editor_with("foo(bar(baz)qux)quux");
        editor.move_to_position(8, false); // Move inside inner brackets
        editor.cut_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo(bar()qux)quux");
        assert_eq!(editor.insertion_point(), 8);
        assert_eq!(editor.cut_buffer.get().0, "baz");

        editor.move_to_position(4, false); // Move inside outer brackets
        editor.cut_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo()quux");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar()qux");
    }

    #[test]
    fn test_yank_inside_brackets() {
        let mut editor = editor_with("foo(bar)baz");
        editor.move_to_position(5, false); // Move inside brackets
        editor.copy_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo(bar)baz"); // Buffer shouldn't change
        assert_eq!(editor.insertion_point(), 5); // Cursor should return to original position

        // Test yanked content by pasting
        editor.paste_cut_buffer();
        assert_eq!(editor.get_buffer(), "foo(bbarar)baz");

        // Test with cursor outside brackets
        let mut editor = editor_with("foo(bar)baz");
        editor.move_to_position(0, false);
        editor.copy_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo(bar)baz");
        assert_eq!(editor.insertion_point(), 0);
    }

    #[test]
    fn test_yank_inside_quotes() {
        let mut editor = editor_with("foo\"bar\"baz");
        editor.move_to_position(5, false); // Move inside quotes
        editor.copy_inside_pair('"', '"');
        assert_eq!(editor.get_buffer(), "foo\"bar\"baz"); // Buffer shouldn't change
        assert_eq!(editor.insertion_point(), 5); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, "bar");

        // Test with no matching quotes
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(4, false);
        editor.copy_inside_pair('"', '"');
        assert_eq!(editor.get_buffer(), "foo bar baz");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "");
    }

    #[test]
    fn test_yank_inside_nested() {
        let mut editor = editor_with("foo(bar(baz)qux)quux");
        editor.move_to_position(8, false); // Move inside inner brackets
        editor.copy_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo(bar(baz)qux)quux"); // Buffer shouldn't change
        assert_eq!(editor.insertion_point(), 8);
        assert_eq!(editor.cut_buffer.get().0, "baz");

        // Test yanked content by pasting
        editor.paste_cut_buffer();
        assert_eq!(editor.get_buffer(), "foo(bar(bazbaz)qux)quux");

        editor.move_to_position(4, false); // Move inside outer brackets
        editor.copy_inside_pair('(', ')');
        assert_eq!(editor.get_buffer(), "foo(bar(bazbaz)qux)quux");
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.cut_buffer.get().0, "bar(bazbaz)qux");
    }

    #[test]
    fn test_kill_line() {
        let mut editor = editor_with("foo\nbar");
        editor.move_to_position(1, false);
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "f\nbar"); // Just cut until the end of line
        assert_eq!(editor.insertion_point(), 1); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, "oo");
        // continue kill line at current position.
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "fbar"); // Just cut the new line character
        assert_eq!(editor.insertion_point(), 1);
        assert_eq!(editor.cut_buffer.get().0, "\n");

        // Test when editor start with newline character point.
        let mut editor = editor_with("foo\nbar");
        editor.move_to_position(3, false);
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "foobar"); // Just cut the new line character
        assert_eq!(editor.insertion_point(), 3); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, "\n");
        // continue kill line at current position.
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "foo"); // Just cut until line end.
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.cut_buffer.get().0, "bar");
        // continue kill line, all remains the same.
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "foo");
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.cut_buffer.get().0, "bar");
    }

    #[test]
    fn test_kill_line_with_windows_newline() {
        let mut editor = editor_with("foo\r\nbar");
        editor.move_to_position(1, false);
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "f\r\nbar"); // Just cut until the end of line
        assert_eq!(editor.insertion_point(), 1); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, "oo");
        // continue kill line at current position.
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "fbar"); // Just cut the new line character
        assert_eq!(editor.insertion_point(), 1);
        assert_eq!(editor.cut_buffer.get().0, "\r\n");

        let mut editor = editor_with("foo\r\nbar");
        editor.move_to_position(3, false);
        editor.kill_line();
        assert_eq!(editor.get_buffer(), "foobar"); // Just cut the newline
        assert_eq!(editor.insertion_point(), 3); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, "\r\n");
    }

    #[test]
    fn test_vi_normal_mode_shift_select_right_c_command() {
        // Test vi normal mode inclusive selection with cut operation
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.line_buffer().selection_anchor(), Some(0));
        assert_eq!(editor.get_selection(), Some((0, 5))); // inclusive selection

        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });

        assert_eq!(editor.get_buffer(), " world");
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, "hello");
    }

    #[test]
    fn test_vi_mode_selection_calculation_bug() {
        // Test selection calculation preserves original mode after mode switch
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        assert_eq!(editor.get_selection(), Some((0, 5))); // inclusive in normal mode

        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Insert));

        assert_eq!(editor.get_selection(), Some((0, 5))); // still inclusive after mode switch
    }

    #[test]
    fn test_vi_c_command_mode_switch_bug_fix() {
        // Test vi 'c' command selection behavior with mode switching
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        assert_eq!(editor.get_selection(), Some((0, 5))); // inclusive selection

        // Simulate vi 'c' command: mode switches to insert then cuts selection
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Insert));
        editor.run_edit_command(&EditCommand::CutSelection {
            granularity: Granularity::CharWise,
        });

        assert_eq!(editor.get_buffer(), " world");
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, "hello");
    }

    #[test]
    fn test_vi_x_command_with_shift_selection() {
        // Test that 'x' (cut char) works with shift+selection in vi normal mode
        let mut editor = editor_with("hello world");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Normal));

        editor.line_buffer.set_insertion_point(0);
        editor.update_selection_anchor(true);

        for _ in 0..4 {
            editor.run_edit_command(&EditCommand::MoveRight { select: true });
        }

        assert_eq!(editor.get_selection(), Some((0, 5))); // inclusive selection

        // Simulate vi 'x' command - should cut the selection, not just one character
        editor.run_edit_command(&EditCommand::CutChar);

        assert_eq!(editor.get_buffer(), " world");
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, "hello");
    }

    #[rstest]
    #[case("hello world test", 7, "hello  test", 6, "world")] // cursor inside word
    #[case("hello world test", 6, "hello  test", 6, "world")] // cursor at start of word
    #[case("hello world test", 10, "hello  test", 6, "world")] // cursor at end of word
    fn test_cut_inside_word(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    #[case("hello world test", 7, "world")] // cursor inside word
    #[case("hello world test", 6, "world")] // cursor at start of word
    #[case("hello world test", 10, "world")] // cursor at end of word
    fn test_yank_inside_word(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_yank: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.copy_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.get_buffer(), input); // Buffer shouldn't change
        assert_eq!(editor.insertion_point(), cursor_pos); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, expected_yank);
    }

    #[rstest]
    #[case("hello world test", 7, "hello test", 6, "world ")] // word with following space
    #[case("hello world", 7, "hello", 5, " world")] // word at end, gets preceding space
    #[case("word test", 2, "test", 0, "word ")] // first word with following space
    #[case("hello word", 7, "hello", 5, " word")] // last word gets preceding space
    // Edge cases at end of string
    #[case("word", 2, "", 0, "word")] // single word, no whitespace
    #[case(" word", 2, "", 0, " word")] // word with only leading space
    // Edge cases with punctuation boundaries
    #[case("word.", 2, ".", 0, "word")] // word followed by punctuation
    #[case(".word", 2, ".", 1, "word")] // word preceded by punctuation
    #[case("(word)", 2, "()", 1, "word")] // word surrounded by punctuation
    #[case("hello,world", 2, ",world", 0, "hello")] // word followed by punct+word
    #[case("hello,world", 7, "hello,", 6, "world")] // word preceded by word+punct
    fn test_cut_around_word(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Around,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    #[case("hello world test", 7, "world ")] // word with following space
    #[case("hello world", 7, " world")] // word at end, gets preceding space
    #[case("word test", 2, "word ")] // first word with following space
    fn test_yank_around_word(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_yank: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.copy_text_object(TextObject {
            scope: TextObjectScope::Around,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.get_buffer(), input); // Buffer shouldn't change
        assert_eq!(editor.insertion_point(), cursor_pos); // Cursor should return to original position
        assert_eq!(editor.cut_buffer.get().0, expected_yank);
    }

    #[rstest]
    #[case("hello big-word test", 10, "hello  test", 6, "big-word")] // big word with punctuation
    #[case("hello BIGWORD test", 10, "hello  test", 6, "BIGWORD")] // simple big word
    #[case("test@example.com file", 8, " file", 0, "test@example.com")] //cursor on email address
    #[case("test@example.com file", 17, "test@example.com ", 17, "file")] // cursor at end of "file"
    fn test_cut_inside_big_word(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::BigWord,
        });

        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    #[case("hello-world test", 2, "-world test", 0, "hello")] // cursor on "hello"
    #[case("hello-world test", 5, "helloworld test", 5, "-")] // cursor on "-"
    #[case("hello-world test", 8, "hello- test", 6, "world")] // cursor on "world"
    #[case("a-b-c test", 0, "-b-c test", 0, "a")] // single char "a"
    #[case("a-b-c test", 2, "a--c test", 2, "b")] // single char "b"
    fn test_cut_inside_word_with_punctuation(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    #[case("foo bar baz", 0, 3, EditCommand::LowercaseSelection, "foo bar baz")]
    #[case("Foo Bar Baz", 0, 7, EditCommand::LowercaseSelection, "foo bar Baz")]
    #[case("FOO BAR BAZ", 0, 11, EditCommand::LowercaseSelection, "foo bar baz")]
    #[case("foo bar baz", 0, 3, EditCommand::UppercaseSelection, "FOO bar baz")]
    #[case("Foo Bar Baz", 0, 7, EditCommand::UppercaseSelection, "FOO BAR Baz")]
    #[case("FOO BAR BAZ", 0, 11, EditCommand::UppercaseSelection, "FOO BAR BAZ")]
    #[case("foo bar baz", 0, 3, EditCommand::SwitchcaseSelection, "FOO bar baz")]
    #[case("Foo Bar Baz", 0, 7, EditCommand::SwitchcaseSelection, "fOO bAR Baz")]
    #[case("FOO BAR BAZ", 0, 11, EditCommand::SwitchcaseSelection, "foo bar baz")]
    fn test_lower_upper_switchcase_selection(
        #[case] input: &str,
        #[case] selection_start: usize,
        #[case] selection_end: usize,
        #[case] command: EditCommand,
        #[case] expected_buffer: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(selection_start, false);
        editor.move_to_position(selection_end, true);
        editor.run_edit_command(&command);
        assert_eq!(editor.get_buffer(), expected_buffer);
    }

    #[rstest]
    #[case("hello-world test", 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "-world test", "hello")] // small word gets just "hello"
    #[case("hello-world test", 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::BigWord }, " test", "hello-world")] // big word gets "hello-word"
    #[case("test@example.com", 6, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "test@", "example.com")] // small word in email (UAX#29 extends across punct)
    #[case("test@example.com", 6, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::BigWord }, "", "test@example.com")] // big word gets entire email
    fn test_word_vs_big_word_comparison(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] text_object: TextObject,
        #[case] expected_buffer: &str,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(text_object);
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    // Test inside operations (iw) at word boundaries
    #[case("hello world", 0, "hello")] // start of first word
    #[case("hello world", 4, "hello")] // end of first word
    #[case("hello world", 6, "world")] // start of second word
    #[case("hello world", 10, "world")] // end of second word
    // Test at exact word boundaries with punctuation
    #[case("hello-world", 4, "hello")] // just before punctuation
    #[case("hello-world", 5, "-")] // on punctuation
    #[case("hello-world", 6, "world")] // just after punctuation
    fn test_cut_inside_word_boundaries(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    // Test around operations (aw) at word boundaries
    #[case("hello world", 0, "hello ")] // start of first word
    #[case("hello world", 4, "hello ")] // end of first word
    #[case("hello world", 6, " world")] // start of second word (gets preceding space)
    #[case("hello world", 10, " world")] // end of second word
    #[case("word", 0, "word")] // single word, no whitespace
    #[case("word ", 0, "word ")] // word with trailing space
    #[case(" word", 1, " word")] // word with leading space
    fn test_cut_around_word_boundaries(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Around,
            object_type: TextObjectType::Word,
        });
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    fn test_cut_text_object_unicode_safety() {
        let mut editor = editor_with("hello 🦀end");
        editor.move_to_position(10, false); // Position after the emoji
        editor.move_to_position(6, false); // Move to the emoji

        editor.cut_text_object(TextObject {
            scope: TextObjectScope::Inner,
            object_type: TextObjectType::Word,
        }); // Cut the emoji

        assert!(editor.line_buffer.is_valid()); // Should not panic or be invalid
    }

    #[rstest]
    // Test operations when cursor is IN WHITESPACE (middle of spaces)
    #[case("hello world test", 5, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "helloworld test", 5, " ")] // single space
    #[case("hello  world", 6, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "helloworld", 5, "  ")] // multiple spaces, cursor on second
    #[case("hello   world", 7, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "helloworld", 5, "   ")] // multiple spaces, cursor on middle
    #[case("   hello", 1, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "hello", 0, "   ")] // leading spaces, cursor on middle
    #[case("hello   ", 7, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "hello", 5, "   ")] // trailing spaces, cursor on middle
    #[case("hello\tworld", 5, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "helloworld", 5, "\t")] // tab character
    #[case("hello\nworld", 5, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "helloworld", 5, "\n")] // newline character
    #[case("hello world test", 5, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::BigWord }, "helloworld test", 5, " ")] // single space (big word)
    #[case("hello  world", 6, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::BigWord }, "helloworld", 5, "  ")] // multiple spaces (big word)
    #[case("  ", 0, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "", 0, "  ")] // only whitespace at start
    #[case("  ", 1, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "", 0, "  ")] // only whitespace at end
    #[case("hello  ", 5, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "hello", 5, "  ")] // trailing whitespace at string end
    #[case("  hello", 0, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Word }, "hello", 0, "  ")] // leading whitespace at string start
    fn test_text_object_in_whitespace(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] text_object: TextObject,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(text_object);
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    // Test text object jumping behavior in various scenarios
    // Cursor inside empty pairs should operate on current pair (cursor stays, nothing cut)
    #[case(r#"foo()bar"#, 4, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Brackets }, "foo()bar", 4, "")] // inside empty brackets
    #[case(r#"foo""bar"#, 4, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Quote }, "foo\"\"bar", 4, "")] // inside empty quotes
    // Cursor outside pairs should jump to next pair (even if empty)
    #[case(r#"foo ()bar"#, 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Brackets }, "foo ()bar", 5, "")] // jump to empty brackets
    #[case(r#"foo ""bar"#, 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Quote }, "foo \"\"bar", 5, "")] // jump to empty quote
    #[case(r#"foo (content)bar"#, 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Brackets }, "foo ()bar", 5, "content")] // jump to non-empty brackets
    #[case(r#"foo "content"bar"#, 2, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Quote }, "foo \"\"bar", 5, "content")] // jump to non-empty quotes
    // Cursor between pairs should jump to next pair
    #[case(r#"(first) (second)"#, 8, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Brackets }, "(first) ()", 9, "second")] // between brackets
    #[case(r#""first" "second""#, 8, TextObject { scope: TextObjectScope::Inner, object_type: TextObjectType::Quote }, "\"first\"\"second\"", 7, " ")] // between quotes
    // Around scope should include the pair characters
    #[case(r#"foo (bar)"#, 2, TextObject { scope: TextObjectScope::Around, object_type: TextObjectType::Brackets }, "foo ", 4, "(bar)")] // around includes parentheses
    #[case(r#"foo "bar""#, 2, TextObject { scope: TextObjectScope::Around, object_type: TextObjectType::Quote }, "foo ", 4, "\"bar\"")] // around includes quotes
    fn test_text_object_jumping_behavior(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] text_object: TextObject,
        #[case] expected_buffer: &str,
        #[case] expected_cursor: usize,
        #[case] expected_cut: &str,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        editor.cut_text_object(text_object);
        assert_eq!(editor.get_buffer(), expected_buffer);
        assert_eq!(editor.insertion_point(), expected_cursor);
        assert_eq!(editor.cut_buffer.get().0, expected_cut);
    }

    #[rstest]
    // Test bracket_text_object_range with Inner scope - just the content inside brackets
    #[case("foo(bar)baz", 5, TextObjectScope::Inner, Some(4..7))] // cursor inside brackets
    #[case("foo[bar]baz", 5, TextObjectScope::Inner, Some(4..7))] // square brackets
    #[case("foo{bar}baz", 5, TextObjectScope::Inner, Some(4..7))] // square brackets
    #[case("foo()bar", 4, TextObjectScope::Inner, Some(4..4))] // empty brackets
    #[case("(nested[inner]outer)", 8, TextObjectScope::Inner, Some(8..13))] // nested, innermost
    #[case("(nested[mixed{inner}brackets]outer)", 8, TextObjectScope::Inner, Some(8..28))] // nested, innermost
    #[case("next(nested[mixed{inner}brackets]outer)", 0, TextObjectScope::Inner, Some(5..38))] // next nested mixed
    #[case("foo (bar)baz", 0, TextObjectScope::Inner, Some(5..8))] // next pair from line start
    #[case("    (bar)baz", 1, TextObjectScope::Inner, Some(5..8))] // next pair from whitespace
    #[case("foo(bar)baz", 2, TextObjectScope::Inner, Some(4..7))] // next pair from word
    #[case("foo(bar\nbaz)qux", 8, TextObjectScope::Inner, Some(4..11))] // multi-line brackets
    #[case("foo\n(bar\nbaz)qux", 0, TextObjectScope::Inner, Some(5..12))] // next multi-line brackets
    #[case("foo\n(bar\nbaz)qux", 3, TextObjectScope::Around, Some(4..13))] // next multi-line brackets
    #[case("{hello}", 3, TextObjectScope::Around, Some(0..7))] // includes curly brackets
    #[case("foo()bar", 4, TextObjectScope::Around, Some(3..5))] // around empty brackets
    #[case("(nested(inner)outer)", 8, TextObjectScope::Around, Some(7..14))] // nested around includes delimiters
    #[case("start(nested(inner)outer)", 2, TextObjectScope::Around, Some(5..25))] // Next outer nested pair
    #[case("(mixed{nested)brackets", 1, TextObjectScope::Inner, Some(1..13))] // mixed nesting
    #[case("(unclosed(nested)brackets", 1, TextObjectScope::Inner, Some(10..16))] // unclosed bracket, find next closed
    #[case("no brackets here", 5, TextObjectScope::Inner, None)] // no brackets found
    #[case("(unclosed", 1, TextObjectScope::Inner, None)] // unclosed bracket
    #[case("(mismatched}", 1, TextObjectScope::Inner, None)] // mismatched brackets
    fn test_bracket_text_object_range(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] scope: TextObjectScope,
        #[case] expected: Option<std::ops::Range<usize>>,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);
        let result = editor.bracket_text_object_range(scope);
        assert_eq!(result, expected);
    }

    #[rstest]
    // Test quote_text_object_range with Inner scope - just the content inside quotes
    #[case(r#"foo"bar"baz"#, 5, TextObjectScope::Inner, Some(4..7))] // cursor inside double quotes
    #[case("foo'bar'baz", 5, TextObjectScope::Inner, Some(4..7))] // single quotes
    #[case("foo`bar`baz", 5, TextObjectScope::Inner, Some(4..7))] // backticks
    #[case(r#"foo""bar"#, 4, TextObjectScope::Inner, Some(4..4))] // empty quotes
    #[case(r#""nested'inner'outer""#, 8, TextObjectScope::Inner, Some(8..13))] // nested, innermost
    #[case(r#""nested`mixed'inner'backticks`outer""#, 8, TextObjectScope::Inner, Some(8..29))] // nested, innermost
    #[case(r#"next"nested'mixed`inner`quotes'outer""#, 0, TextObjectScope::Inner, Some(5..36))] // next nested mixed
    #[case(r#"foo "bar"baz"#, 0, TextObjectScope::Inner, Some(5..8))] // next pair
    #[case(r#"foo"bar"baz"#, 2, TextObjectScope::Inner, Some(4..7))] // next from inside word
    #[case(r#"foo"bar"baz"#, 4, TextObjectScope::Around, Some(3..8))] // around includes quotes
    #[case(r#"foo"bar"baz"#, 3, TextObjectScope::Around, Some(3..8))] // around on opening quote
    #[case(r#"foo"bar"baz"#, 2, TextObjectScope::Around, Some(3..8))] // around next quotes
    #[case(r#"foo""bar"#, 4, TextObjectScope::Around, Some(3..5))] // around empty quotes
    #[case(r#"foo""bar"#, 1, TextObjectScope::Around, Some(3..5))] // around empty quotes
    #[case(r#""nested"inner"outer""#, 8, TextObjectScope::Around, Some(7..14))] // nested around includes delimiters
    #[case(r#"start"nested'inner'outer""#, 2, TextObjectScope::Around, Some(5..25))] // Next outer nested pair
    #[case("no quotes here", 5, TextObjectScope::Inner, None)] // no quotes found
    #[case(r#"foo"bar"#, 1, TextObjectScope::Inner, None)] // unclosed quote
    #[case("foo'bar\nbaz'qux", 5, TextObjectScope::Inner, None)] // quotes don't span multiple lines
    #[case("foo'bar\nbaz'qux", 0, TextObjectScope::Inner, None)] // quotes don't span multiple lines
    #[case("foobar\n`baz`qux", 6, TextObjectScope::Inner, None)] // quotes don't span multiple lines
    #[case("foo\n(bar\nbaz)qux", 0, TextObjectScope::Inner, None)] // next multi-line brackets
    #[case("foo\n(bar\nbaz)qux", 3, TextObjectScope::Around, None)] // next multi-line brackets
    fn test_quote_text_object_range(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] scope: TextObjectScope,
        #[case] expected: Option<std::ops::Range<usize>>,
    ) {
        let mut editor = editor_with(input);
        editor.line_buffer.set_insertion_point(cursor_pos);
        let result = editor.quote_text_object_range(scope);
        assert_eq!(result, expected);
    }

    #[rstest]
    // Test edge cases and complex scenarios for both bracket and quote text objects
    #[case("", 0, TextObjectScope::Inner, None, None)] // empty buffer
    #[case("a", 0, TextObjectScope::Inner, None, None)] // single character
    #[case("()", 1, TextObjectScope::Inner, Some(1..1), None)] // empty brackets, cursor inside
    #[case(r#""""#, 1, TextObjectScope::Inner, None, Some(1..1))] // empty quotes, cursor inside
    #[case("([{}])", 3, TextObjectScope::Inner, Some(3..3), None)] // deeply nested brackets
    #[case(r#""'`text`'""#, 5, TextObjectScope::Inner, None, Some(3..7))] // deeply nested quotes
    #[case("(text) and [more]", 5, TextObjectScope::Around, Some(0..6), None)] // multiple bracket types
    #[case(r#""text" and 'more'"#, 5, TextObjectScope::Around, None, Some(0..6))] // multiple quote types
    fn test_text_object_edge_cases(
        #[case] input: &str,
        #[case] cursor_pos: usize,
        #[case] scope: TextObjectScope,
        #[case] expected_bracket: Option<std::ops::Range<usize>>,
        #[case] expected_quote: Option<std::ops::Range<usize>>,
    ) {
        let mut editor = editor_with(input);
        editor.move_to_position(cursor_pos, false);

        let bracket_result = editor.bracket_text_object_range(scope);
        let quote_result = editor.quote_text_object_range(scope);

        assert_eq!(bracket_result, expected_bracket);
        assert_eq!(quote_result, expected_quote);
    }

    // --- MotionTarget verbs (Move / Extend / Cut / Copy / Erase) ---
    //
    // These drive the public verbs through the full lowering
    // (`MotionTarget` -> `resolve_motion`) in the default (emacs)
    // editor, proving the substrate in isolation before any keymap emits it.

    /// `w` as a target: small-word start, forward.
    fn word_start_fwd() -> MotionTarget {
        MotionTarget::Word {
            kind: WordKind::Word,
            edge: WordEdge::Start,
            direction: Direction::Forward,
        }
    }

    #[test]
    fn move_word_forward_lands_on_next_word_start() {
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Move(word_start_fwd()));
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.get_selection(), None); // Move collapses — no selection
    }

    #[test]
    fn move_grapheme_right_steps_over_multibyte() {
        let mut editor = editor_with("café"); // 'é' is 2 bytes: graphemes at 0,1,2,3, len 5
        editor.move_to_position(3, false);
        editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(
            Direction::Forward,
        )));
        assert_eq!(editor.insertion_point(), 5); // one grapheme, two bytes
    }

    // --- an operator span is always safe to slice with ---
    //
    // `MotionTarget::Position` is the one target that can name a byte the caller
    // picked rather than one a motion produced, so it can land *inside* a
    // grapheme. An operator consumes its span before the commit boundary would
    // normalize anything, so without the `recohere` in `operator_span` these
    // panic on a non-char-boundary slice rather than failing an assertion.
    //
    // `recohere` expands outward, thus the straddled grapheme is consumed whole
    // from either side rather than truncated.

    #[test]
    fn cut_to_a_position_inside_a_grapheme_takes_it_whole() {
        let mut editor = editor_with("café"); // c0 a1 f2, 'é' spans [3, 5)
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Position(4), // inside the 'é'
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "");
        let (content, _) = editor.cut_buffer.get();
        assert_eq!(content, "café");
    }

    #[test]
    fn cut_back_to_a_position_inside_a_grapheme_takes_it_whole() {
        let mut editor = editor_with("café");
        editor.move_to_position(5, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Position(4),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "caf");
        let (content, _) = editor.cut_buffer.get();
        assert_eq!(content, "é");
    }

    #[test]
    fn cut_to_a_position_past_the_buffer_stops_at_the_end() {
        let mut editor = editor_with("café");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Position(99),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "");
    }

    #[test]
    fn extend_word_forward_keeps_anchor_at_origin() {
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
        assert_eq!(editor.insertion_point(), 4);
        assert_eq!(editor.get_selection(), Some((0, 4))); // anchor stays at the origin
    }

    #[test]
    fn vi_visual_extend_word_covers_landing() {
        // `CoverLanding`: vi visual sweeps the grapheme the motion lands *on*, so
        // `Extend(w)` over "foo bar" selects "foo b" (vim's inclusive visual).
        let mut editor = editor_with("foo bar baz");
        editor.set_edit_mode(PromptEditMode::Vi(PromptViMode::Visual));
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
        assert_eq!(editor.get_selection(), Some((0, 5)));
        assert_eq!(editor.insertion_point(), 4);
    }

    #[test]
    fn emacs_extend_word_is_exclusive_span() {
        // The contrast that proves the axis is real: the *same* `Extend(w)` from
        // the *same position in a `Span` (bar) mode stops at the boundary "foo "
        // instead of sweeping the landing grapheme.
        let mut editor = editor_with("foo bar baz");
        editor.set_edit_mode(PromptEditMode::Emacs);
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
        assert_eq!(editor.get_selection(), Some((0, 4)))
    }

    #[test]
    fn emacs_extend_word_twice_grows_span() {
        // A second `Extend` resolves the next motion from the live head and grows
        // the existing Span — not from a collapsed or retreated caret. "foo bar
        // baz": 0 → "foo " (0,4) → "foo bar " (0,8), anchor pinned at the origin.
        let mut editor = editor_with("foo bar baz");
        editor.set_edit_mode(PromptEditMode::Emacs);
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
        assert_eq!(editor.get_selection(), Some((0, 4)));
        editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
        assert_eq!(editor.get_selection(), Some((0, 8)));
    }

    #[test]
    fn vi_visual_still_stops_before_the_newline() {
        // The reason `Block` and `BlockOverNewline` are separate: vim's visual
        // `l` does not step onto the terminator.
        let mut editor = vi_editor("ab\ncd", PromptViMode::Visual);
        editor.line_buffer.set_cursor(Cursor::new(1, 2));
        editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(
            Direction::Forward,
        )));
        assert_eq!(editor.get_selection(), Some((3, 4)));
    }

    #[rstest]
    #[case(EditCommand::InsertNewlineBelow, 1)]
    #[case(EditCommand::InsertNewlineAbove, 5)]
    fn open_line_is_unchanged_for_a_bar_caret(#[case] command: EditCommand, #[case] caret: usize) {
        // Pinned so the collapse added for helix cannot regress the bar modes.
        let mut editor = editor_with("abc\ndef");
        editor.move_to_position(caret, false);
        editor.run_edit_command(&command);
        assert_eq!(editor.get_buffer(), "abc\n\ndef");
        assert_eq!(editor.insertion_point(), 4);
    }

    /// Helix-only editor behaviour. One gate for the whole block so it
    /// lifts in a single edit once helix stops being feature gated.
    #[cfg(feature = "helix")]
    mod helix {
        use super::*;
        use pretty_assertions::assert_eq;

        fn helix_editor(buffer: &str) -> Editor {
            let mut editor = editor_with(buffer);
            editor.set_edit_mode(PromptEditMode::Helix(crate::PromptHelixMode::Normal));
            editor
        }

        #[test]
        fn resting_cursor_cell_is_the_whole_head_cell() {
            // A 1-wide resting selection IS the cursor: with a cursor style
            // configured it claims the entire range and no selection style
            // remains.
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(
                Direction::Forward,
            )));
            assert_eq!(editor.get_selection(), Some((1, 2)), "setup");
            assert_eq!(editor.selection_head_cell(), Some((1, 2)));
        }

        #[test]
        fn head_cell_tracks_the_extending_head() {
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)), "setup");
            assert_eq!(editor.insertion_point(), 3, "setup: head on the space");
            assert_eq!(editor.selection_head_cell(), Some((3, 4)));
        }

        #[test]
        fn helix_select_re_anchors_at_each_word() {
            // Successive `Select(w)` tile the line — "foo " then "bar " — instead
            // of growing from the origin like `Extend` does above.
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)));
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((4, 8)));
        }

        // --- `Extend` grows on every press (helix select `l` / `w` / `h`) ---
        //
        // These drive the editor rather than asserting the emitted event, which
        // is why they catch what the keybinding tests cannot: `Extend` resolved
        // its motion from the caret, a grapheme behind the head, so an exclusive
        // forward motion landed on the boundary the previous press had already
        // parked the head on and froze there.
        //
        // Every case presses more than once on purpose. At rest the caret and the
        // head grapheme coincide, so the first press is correct even when no
        // later one can move.

        fn helix_select_editor(buffer: &str) -> Editor {
            let mut editor = editor_with(buffer);
            editor.set_edit_mode(PromptEditMode::Helix(crate::PromptHelixMode::Select));
            editor
        }

        #[test]
        fn helix_extend_grapheme_forward_grows_on_every_press() {
            let mut editor = helix_select_editor("hello");
            editor.move_to_position(2, false);
            let l = EditCommand::Extend(MotionTarget::Grapheme(Direction::Forward));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((2, 3)));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((2, 4)));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((2, 5)));
        }

        #[test]
        fn helix_extend_word_forward_grows_on_every_press() {
            // Unlike `Select(w)`, which re-anchors and tiles, `Extend(w)` keeps
            // the anchor and sweeps whole words into one selection.
            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)));
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 8)));
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 11)));
        }

        #[test]
        fn helix_extend_grapheme_backward_flips_the_anchor_then_grows() {
            // The backward path was already correct — for a backward range the
            // caret *is* the head, so it never had the origin split. Pinned so
            // the forward fix cannot regress it.
            let mut editor = helix_select_editor("hello");
            editor.move_to_position(2, false);
            let h = EditCommand::Extend(MotionTarget::Grapheme(Direction::Backward));
            editor.run_edit_command(&h);
            assert_eq!(editor.get_selection(), Some((1, 3)));
            editor.run_edit_command(&h);
            assert_eq!(editor.get_selection(), Some((0, 3)));
            // At the buffer start there is nowhere left to go.
            editor.run_edit_command(&h);
            assert_eq!(editor.get_selection(), Some((0, 3)));
        }

        #[test]
        fn helix_extend_hops_a_whole_multibyte_grapheme() {
            // "cafe\u{301}x" is c0 a1 f2 e+combining[3,6) x6 — the head must clear
            // the combining mark in one step rather than land inside it.
            let mut editor = helix_select_editor("cafe\u{301}x");
            editor.move_to_position(0, false);
            let l = EditCommand::Extend(MotionTarget::Grapheme(Direction::Forward));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((0, 1)));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((0, 2)));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((0, 3)));
            editor.run_edit_command(&l);
            assert_eq!(editor.get_selection(), Some((0, 6)));
        }

        // --- `gs` (goto first non-blank) ---

        #[test]
        fn helix_extend_line_start_non_blank_forward_covers_the_landing() {
            // From inside the indent `gs` travels forward, so the selection must
            // hold the 'f' it lands on. Extending via `Span` stopped at (0, 4).
            let mut editor = helix_select_editor("    foo");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Extend(MotionTarget::LineStartNonBlank));
            assert_eq!(editor.get_selection(), Some((0, 5)));
        }

        #[test]
        fn helix_extend_line_start_non_blank_backward_reaches_the_indent_end() {
            // Pinned separately: a backward range covers its target at the low
            // end, so this direction never widens.
            let mut editor = helix_select_editor("    foo");
            editor.move_to_position(6, false);
            editor.run_edit_command(&EditCommand::Extend(MotionTarget::LineStartNonBlank));
            assert_eq!(editor.get_selection(), Some((4, 7)));
        }

        #[test]
        fn helix_extend_line_start_non_blank_is_idempotent() {
            // The anchor governs the widening, not the caret, so repeated
            // presses must neither creep forward nor shrink back.
            let mut editor = helix_select_editor("    foo");
            editor.move_to_position(0, false);
            let gs = EditCommand::Extend(MotionTarget::LineStartNonBlank);
            editor.run_edit_command(&gs);
            assert_eq!(editor.get_selection(), Some((0, 5)));
            editor.run_edit_command(&gs);
            assert_eq!(editor.get_selection(), Some((0, 5)));
        }

        #[test]
        fn helix_move_line_start_non_blank_lands_on_the_first_non_blank() {
            // Normal mode collapses onto the target.
            let mut editor = helix_editor("    foo");
            editor.move_to_position(6, false);
            editor.run_edit_command(&EditCommand::Move(MotionTarget::LineStartNonBlank));
            assert_eq!(editor.insertion_point(), 4);
        }

        #[test]
        fn helix_move_line_start_non_blank_stays_on_a_blank_line() {
            // `LineBuffer::line_non_blank_start_index` settles for the
            // terminator, so lowering `gs` onto it would move here.
            let mut editor = helix_editor("   \nfoo");
            editor.move_to_position(1, false);
            editor.run_edit_command(&EditCommand::Move(MotionTarget::LineStartNonBlank));
            assert_eq!(editor.insertion_point(), 1);
        }

        // --- operations that keep the selection ---

        #[test]
        fn helix_yank_leaves_the_selection_standing() {
            // Helix yanks without collapsing, so the same span stays operable.
            let mut editor = helix_editor("abc def");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)), "setup");
            editor.run_edit_command(&EditCommand::CopySelection);
            assert_eq!(editor.get_selection(), Some((0, 4)));
            assert_eq!(editor.cut_buffer.get().0, "abc ");
        }

        #[test]
        fn helix_case_change_leaves_the_selection_standing() {
            let mut editor = helix_editor("abc def");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::SwitchcaseSelection);
            assert_eq!(editor.get_buffer(), "ABC def");
            assert_eq!(editor.get_selection(), Some((0, 4)));
        }

        #[test]
        fn vi_yank_still_collapses_the_selection() {
            // The contrast that gives the two above their meaning. Not `None`:
            // under `Block` the collapsed point re-widens, so a dropped
            // selection still reads as a one-grapheme cursor.
            let mut editor = vi_editor("abc def", PromptViMode::Visual);
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(
                editor.get_selection(),
                Some((0, 5)),
                "setup: vim's inclusive visual sweeps onto the `b`"
            );
            editor.run_edit_command(&EditCommand::CopySelection);
            assert_eq!(editor.get_selection(), Some((4, 5)));
        }

        // --- `Alt-d` (erase without yanking) ---

        /// The register is seeded with text the erase does *not* delete, so a
        /// `Cut` in its place would visibly overwrite it.
        #[test]
        fn helix_erase_selection_leaves_the_cut_buffer_alone() {
            let mut editor = helix_editor("abc def");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::CopySelection);
            assert_eq!(editor.cut_buffer.get().0, "abc ", "setup");

            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::Select(MotionTarget::BufferEdge(
                Direction::Forward,
            )));
            editor.run_edit_command(&EditCommand::EraseSelection);
            assert_eq!(editor.get_buffer(), "abc ");
            assert_eq!(
                editor.cut_buffer.get().0,
                "abc ",
                "erase must not fill the register"
            );
        }

        #[test]
        fn helix_cut_selection_still_fills_the_cut_buffer() {
            // The contrast that gives the test above its meaning.
            let mut editor = helix_editor("abc def");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::CopySelection);

            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::Select(MotionTarget::BufferEdge(
                Direction::Forward,
            )));
            editor.run_edit_command(&EditCommand::CutSelection {
                granularity: Granularity::CharWise,
            });
            assert_eq!(editor.get_buffer(), "abc ");
            assert_eq!(editor.cut_buffer.get().0, "def");
        }

        #[test]
        fn helix_erase_selection_takes_only_the_selection() {
            let mut editor = helix_editor("abc def");
            editor.move_to_position(0, false);
            // `w` selects "abc " as a small-word start motion.
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)), "setup");
            editor.run_edit_command(&EditCommand::EraseSelection);
            assert_eq!(editor.get_buffer(), "def");
        }

        // --- `x` (line selection) ---

        #[test]
        fn helix_select_line_snaps_out_to_the_whole_line() {
            // "ab\ncd\nef": line 2 is bytes 3..6, terminator included.
            let mut editor = helix_editor("ab\ncd\nef");
            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::SelectLine);
            assert_eq!(editor.get_selection(), Some((3, 6)));
        }

        #[test]
        fn helix_select_line_takes_one_more_line_on_repeat() {
            // The press that composition cannot reproduce: growing needs the
            // command to notice the selection already spans whole lines.
            let mut editor = helix_editor("ab\ncd\nef");
            editor.move_to_position(0, false);
            let x = EditCommand::SelectLine;
            editor.run_edit_command(&x);
            assert_eq!(editor.get_selection(), Some((0, 3)));
            editor.run_edit_command(&x);
            assert_eq!(editor.get_selection(), Some((0, 6)));
            editor.run_edit_command(&x);
            assert_eq!(editor.get_selection(), Some((0, 8)));
        }

        #[test]
        fn helix_select_line_stops_at_the_last_line() {
            // The final line is unterminated, so the buffer end is its edge and
            // a further press has nowhere to grow.
            let mut editor = helix_editor("ab\ncd");
            editor.move_to_position(4, false);
            let x = EditCommand::SelectLine;
            editor.run_edit_command(&x);
            assert_eq!(editor.get_selection(), Some((3, 5)));
            editor.run_edit_command(&x);
            assert_eq!(editor.get_selection(), Some((3, 5)));
        }

        #[test]
        fn helix_select_line_from_a_terminator_keeps_its_own_line() {
            // Helix rests *on* the terminator, so `x` there must select the line
            // that terminator ends, not the one after it.
            let mut editor = helix_editor("ab\ncd\nef");
            editor.move_to_position(2, false);
            editor.run_edit_command(&EditCommand::SelectLine);
            assert_eq!(editor.get_selection(), Some((0, 3)));
        }

        #[test]
        fn helix_select_line_covers_an_empty_line_whole() {
            // "ab\n\ncd": the blank line is just its terminator at 3.
            let mut editor = helix_editor("ab\n\ncd");
            editor.move_to_position(3, false);
            editor.run_edit_command(&EditCommand::SelectLine);
            assert_eq!(editor.get_selection(), Some((3, 4)));
        }

        /// `b` as a target: small-word start, backward.
        fn word_start_bwd() -> MotionTarget {
            MotionTarget::Word {
                kind: WordKind::Word,
                edge: WordEdge::Start,
                direction: Direction::Backward,
            }
        }

        /// `e` as a target: small-word end, forward.
        fn word_end_fwd() -> MotionTarget {
            MotionTarget::Word {
                kind: WordKind::Word,
                edge: WordEdge::End,
                direction: Direction::Forward,
            }
        }

        // --- every select motion, from an already-grown selection ---
        //
        // "foo bar baz" is f0 o1 o2 _3 b4 a5 r6 _7 b8 a9 z10, len 11.
        //
        // One `Extend(w)` first, so each case starts from (0, 4) rather than a
        // resting cursor: that is where the caret and the head diverge, and a
        // motion resolved from the wrong one shows up as a frozen selection.
        // Two presses each, since the first can succeed where the second cannot.

        #[rstest]
        #[case::word_start(word_start_fwd(), (0, 8), (0, 11))]
        #[case::word_end(word_end_fwd(), (0, 7), (0, 11))]
        #[case::line_end(MotionTarget::LineEdge(Direction::Forward), (0, 11), (0, 11))]
        #[case::buffer_end(MotionTarget::BufferEdge(Direction::Forward), (0, 11), (0, 11))]
        #[case::find_on(find('a', Direction::Forward, FindStop::On), (0, 6), (0, 10))]
        #[case::find_before(find('a', Direction::Forward, FindStop::Before), (0, 5), (0, 9))]
        fn helix_extend_forward_targets_keep_growing(
            #[case] target: MotionTarget,
            #[case] after_one: (usize, usize),
            #[case] after_two: (usize, usize),
        ) {
            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            assert_eq!(editor.get_selection(), Some((0, 4)));
            editor.run_edit_command(&EditCommand::Extend(target));
            assert_eq!(editor.get_selection(), Some(after_one));
            editor.run_edit_command(&EditCommand::Extend(target));
            assert_eq!(editor.get_selection(), Some(after_two));
        }

        // --- backward targets that land on the anchor ---
        //
        // Each of these drives the head onto the anchor itself, so `extend_span`
        // writes an *empty* cursor and only the commit boundary's min-width-1
        // rule widens it back onto a grapheme. Pinned because that dependency is
        // invisible at the call site: drop the rest policy and these collapse to
        // a bare point rather than a helix cursor.

        #[rstest]
        #[case::word_start(word_start_bwd())]
        #[case::line_start(MotionTarget::LineEdge(Direction::Backward))]
        #[case::buffer_start(MotionTarget::BufferEdge(Direction::Backward))]
        fn helix_extend_backward_onto_the_anchor_stays_one_grapheme(#[case] target: MotionTarget) {
            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            editor.run_edit_command(&EditCommand::Extend(target));
            assert_eq!(editor.get_selection(), Some((0, 1)));
        }

        // --- backward targets that cross the anchor ---
        //
        // Anchored at 4 and grown to 8 ("bar "), so a backward target has to pass
        // *through* the anchor. `flip_anchor` then hops the anchor to the far edge
        // of its grapheme (4 -> 5) to keep `b` covered, which is the one place the
        // block reversal rule is observable from the outside.

        #[rstest]
        #[case::line_start(MotionTarget::LineEdge(Direction::Backward), (5, 0))]
        #[case::buffer_start(MotionTarget::BufferEdge(Direction::Backward), (5, 0))]
        fn helix_extend_backward_across_the_anchor_flips_it(
            #[case] target: MotionTarget,
            #[case] expected: (usize, usize),
        ) {
            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            assert_eq!(editor.line_buffer.cursor(), Cursor::new(4, 8));
            editor.run_edit_command(&EditCommand::Extend(target));
            let (anchor, head) = expected;
            assert_eq!(editor.line_buffer.cursor(), Cursor::new(anchor, head));
            // The anchor moved, but the grapheme it started on is still covered.
            assert_eq!(editor.get_selection(), Some((0, 5)));
        }

        #[test]
        fn helix_extend_backward_shrinks_without_crossing_the_anchor() {
            // The contrast with the flip cases above: `b` and `h` stop short of
            // the anchor, so it stays put and the selection only narrows.
            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            editor.run_edit_command(&EditCommand::Extend(word_start_bwd()));
            assert_eq!(editor.line_buffer.cursor(), Cursor::new(4, 5));

            let mut editor = helix_select_editor("foo bar baz");
            editor.move_to_position(4, false);
            editor.run_edit_command(&EditCommand::Extend(word_start_fwd()));
            editor.run_edit_command(&EditCommand::Extend(MotionTarget::Grapheme(
                Direction::Backward,
            )));
            assert_eq!(editor.line_buffer.cursor(), Cursor::new(4, 6));
        }

        // --- the newline as a cell (helix `l` / `h`) ---
        //
        // "ab\ncd" is a0 b1 \n2 c3 d4.

        #[rstest]
        #[case(Direction::Forward, 1, (2, 3))]
        #[case(Direction::Backward, 3, (2, 3))]
        fn helix_grapheme_step_rests_on_the_newline(
            #[case] direction: Direction,
            #[case] from: usize,
            #[case] expected: (usize, usize),
        ) {
            let mut editor = helix_editor("ab\ncd");
            editor.line_buffer.set_cursor(Cursor::new(from, from + 1));
            editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(direction)));
            assert_eq!(editor.get_selection(), Some(expected));
        }

        #[test]
        fn helix_cut_on_the_newline_joins_the_lines() {
            let mut editor = helix_editor("ab\ncd");
            editor.line_buffer.set_cursor(Cursor::new(1, 2));
            editor.run_edit_command(&EditCommand::Move(MotionTarget::Grapheme(
                Direction::Forward,
            )));
            editor.run_edit_command(&EditCommand::CutSelection {
                granularity: Granularity::CharWise,
            });
            assert_eq!(editor.get_buffer(), "abcd");
        }

        // --- open line (`o` / `O`) ---
        //
        // "abc\ndef" is a0 b1 c2 \n3 d4 e5 f6; after either insert the buffer is
        // "abc\n\ndef", whose new empty line is the second `\n` at byte 4.

        #[rstest]
        #[case(EditCommand::InsertNewlineBelow, 1)]
        #[case(EditCommand::InsertNewlineAbove, 5)]
        fn open_line_lands_on_the_new_line_from_an_anchored_cursor(
            #[case] command: EditCommand,
            #[case] caret: usize,
        ) {
            // The helix regression: a resting block cursor is always anchored, and
            // the line-edge seek used to move only its head, so the stale anchor
            // pulled the caret back onto the original line.
            let mut editor = helix_editor("abc\ndef");
            editor.line_buffer.set_cursor(Cursor::new(caret, caret + 1));
            editor.run_edit_command(&command);
            assert_eq!(editor.get_buffer(), "abc\n\ndef");
            assert_eq!(editor.insertion_point(), 4);
        }

        #[rstest]
        #[case(EditCommand::InsertNewlineBelow, 1)]
        #[case(EditCommand::InsertNewlineAbove, 5)]
        fn open_line_then_opens_above_stack(#[case] open: EditCommand, #[case] caret: usize) {
            // Opening N lines means one seeking open plus N-1 opens *above* it:
            // only the first has a line edge to find. See the negative case below.
            let mut editor = helix_editor("abc\ndef");
            editor.line_buffer.set_cursor(Cursor::new(caret, caret + 1));
            editor.run_edit_command(&open);
            editor.run_edit_command(&EditCommand::InsertNewlineAbove);
            editor.run_edit_command(&EditCommand::InsertNewlineAbove);
            assert_eq!(editor.get_buffer(), "abc\n\n\n\ndef");
            assert_eq!(editor.insertion_point(), 4);
        }

        #[test]
        fn repeating_open_below_does_not_stack() {
            // The second seek finds no `\n` past the blank line just made, so it
            // appends at the buffer end rather than stacking.
            let mut editor = helix_editor("abc\ndef");
            editor.line_buffer.set_cursor(Cursor::new(1, 2));
            for _ in 0..3 {
                editor.run_edit_command(&EditCommand::InsertNewlineBelow);
            }
            assert_eq!(editor.get_buffer(), "abc\n\ndef\n\n");
        }

        #[test]
        fn helix_collapse_selection_forward_lands_after_it() {
            // The collapse lands as a point, but the Block rest policy widens it
            // back onto one grapheme — a helix caret is always a 1-wide cover, so
            // `a` rests *on* 'b' with the old selection gone.
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::CollapseSelection(Direction::Forward));
            assert_eq!(editor.get_selection(), Some((4, 5)));
            assert_eq!(editor.insertion_point(), 4);
        }

        #[test]
        fn helix_collapse_selection_backward_lands_at_its_start() {
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::CollapseSelection(Direction::Backward));
            assert_eq!(editor.get_selection(), Some((0, 1)));
            assert_eq!(editor.insertion_point(), 0);
        }

        #[test]
        fn helix_undo_re_widens_the_restored_caret() {
            // `undo()` replaces the whole LineBuffer, cursor included, so the
            // restored cursor is whatever the undo stack recorded — here the pre-cut
            // selection (0, 4). Nothing in the Undo arm normalizes it: the
            // unconditional `commit_cursor()` at the end of `run_edit_command` does,
            // via the Block rest policy. That is the guarantee under test — a command
            // that never mentions the cursor still cannot leave a helix caret
            // rendering as a bar.
            //
            // Note *where* it lands. Undo's `EditType` is not
            // `MoveCursor { select: true }`, so `run_edit_command` calls
            // `clear_selection`, which collapses to the caret — for a forward Block
            // selection that is the last covered grapheme's start (3, the space), not
            // the selection start. So `d` then `u` restores the text but parks the
            // caret at the end of it. Real helix re-highlights the restored selection
            // instead; pinned here so that deviation is visible rather than
            // rediscovered.
            let mut editor = helix_editor("foo bar baz");
            editor.move_to_position(0, false);
            editor.run_edit_command(&EditCommand::Select(word_start_fwd()));
            editor.run_edit_command(&EditCommand::CutSelection {
                granularity: Granularity::CharWise,
            });
            assert_eq!(editor.get_buffer(), "bar baz");

            editor.run_edit_command(&EditCommand::Undo);
            assert_eq!(editor.get_buffer(), "foo bar baz");
            // a 1-wide cover, not a bare point
            assert_eq!(editor.get_selection(), Some((3, 4)));
            assert_eq!(editor.insertion_point(), 3);
        }

        /// Helix `p`: insert at the selection's far edge, leaving the pasted text
        /// selected. The resting block covers the final `o`, so the far edge is 3.
        #[test]
        fn helix_paste_after_lands_past_the_selection_and_selects_it() {
            let mut editor = helix_editor("foo");
            editor.move_to_position(2, false);
            editor.commit_cursor();
            editor.cut_buffer.set("bar", Granularity::CharWise);

            editor.run_edit_command(&EditCommand::PasteAtSelectionEdge {
                direction: Direction::Forward,
                count: 1,
            });

            assert_eq!(editor.get_buffer(), "foobar");
            assert_eq!(editor.get_selection(), Some((3, 6)));
        }

        /// Helix `P`: the near edge instead, so the pasted text pushes the covered
        /// grapheme right rather than following it.
        #[test]
        fn helix_paste_before_lands_at_the_selection_start_and_selects_it() {
            let mut editor = helix_editor("foo");
            editor.move_to_position(2, false);
            editor.commit_cursor();
            editor.cut_buffer.set("bar", Granularity::CharWise);

            editor.run_edit_command(&EditCommand::PasteAtSelectionEdge {
                direction: Direction::Backward,
                count: 1,
            });

            assert_eq!(editor.get_buffer(), "fobaro");
            assert_eq!(editor.get_selection(), Some((2, 5)));
        }

        /// `3p` pastes three copies and selects *all* of them. This is the reason
        /// the count rides inside the command rather than repeating the event.
        #[test]
        fn helix_paste_count_selects_every_copy() {
            let mut editor = helix_editor("foo");
            editor.move_to_position(2, false);
            editor.commit_cursor();
            editor.cut_buffer.set("ab", Granularity::CharWise);

            editor.run_edit_command(&EditCommand::PasteAtSelectionEdge {
                direction: Direction::Forward,
                count: 3,
            });

            assert_eq!(editor.get_buffer(), "fooababab");
            assert_eq!(editor.get_selection(), Some((3, 9)));
        }

        /// Pasting nothing must not move the caret: the guard has to run *before*
        /// the cursor is written, or the block would shift one grapheme right for a
        /// paste that inserted no text.
        #[test]
        fn helix_paste_of_an_empty_cut_buffer_leaves_the_caret_put() {
            let mut editor = helix_editor("foobar");
            editor.move_to_position(1, false);
            editor.commit_cursor();
            let before = editor.get_selection();
            editor.cut_buffer.set("", Granularity::CharWise);

            editor.run_edit_command(&EditCommand::PasteAtSelectionEdge {
                direction: Direction::Forward,
                count: 1,
            });

            assert_eq!(editor.get_buffer(), "foobar");
            assert_eq!(editor.get_selection(), before);
        }

        /// `café` has `é` at [3,5), so a caret resting on it has a far edge of 5.
        /// Byte arithmetic that assumed one byte per grapheme would land mid-`é`.
        ///
        /// The pasted text is deliberately multibyte *and* more than one grapheme
        /// wide: with a single-grapheme payload, "selection covers what was pasted"
        /// and "1-wide block at the far end" are the same answer, so the assertion
        /// would hold even if the selection were being collapsed.
        #[test]
        fn helix_paste_does_not_split_a_multibyte_grapheme() {
            let mut editor = helix_editor("café");
            editor.move_to_position(3, false);
            editor.commit_cursor();
            editor.cut_buffer.set("éx", Granularity::CharWise);

            editor.run_edit_command(&EditCommand::PasteAtSelectionEdge {
                direction: Direction::Forward,
                count: 1,
            });

            assert_eq!(editor.get_buffer(), "cafééx");
            // 3 bytes of payload from the far edge of the first `é`
            assert_eq!(editor.get_selection(), Some((5, 8)));
        }
    }

    #[test]
    fn cut_word_forward_removes_range_and_yanks() {
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: word_start_fwd(),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "bar baz");
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, "foo ");
    }

    #[test]
    fn cut_word_backward_removes_preceding_word() {
        let mut editor = editor_with("foo bar");
        editor.move_to_position(7, false); // end of buffer
        editor.run_edit_command(&EditCommand::Cut {
            target: MotionTarget::Word {
                kind: WordKind::Word,
                edge: WordEdge::Start,
                direction: Direction::Backward,
            },
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "foo ");
        assert_eq!(editor.insertion_point(), 4); // cursor lands at the range start
        assert_eq!(editor.cut_buffer.get().0, "bar");
    }

    #[test]
    fn copy_word_forward_yanks_without_editing() {
        let mut editor = editor_with("foo bar");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Copy {
            target: word_start_fwd(),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), "foo bar"); // buffer untouched
        assert_eq!(editor.insertion_point(), 0); // cursor untouched
        assert_eq!(editor.cut_buffer.get().0, "foo ");
    }

    #[test]
    fn erase_word_forward_deletes_without_touching_register() {
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Erase(word_start_fwd()));
        assert_eq!(editor.get_buffer(), "bar baz");
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, ""); // register left untouched
    }

    #[test]
    fn erase_find_forward_is_inclusive() {
        // op_end (inclusive forward find) must reach Erase through `operate`:
        // `dt`-style would stop short, but `Find { On }` eats through the 'b'.
        let mut editor = editor_with("foo bar baz");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Erase(find(
            'b',
            Direction::Forward,
            FindStop::On,
        )));
        assert_eq!(editor.get_buffer(), "ar baz"); // removed "foo b"
        assert_eq!(editor.insertion_point(), 0);
        assert_eq!(editor.cut_buffer.get().0, ""); // register left untouched
    }

    #[test]
    fn erase_grapheme_backward_over_multibyte() {
        // backward span (origin > op_end) across a 2-byte grapheme.
        let mut editor = editor_with("café"); // 'é' is [3,5)
        editor.move_to_position(5, false);
        editor.run_edit_command(&EditCommand::Erase(MotionTarget::Grapheme(
            Direction::Backward,
        )));
        assert_eq!(editor.get_buffer(), "caf");
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.cut_buffer.get().0, ""); // register left untouched
    }

    /// `e` as a target: small-word end, forward.
    fn word_end_fwd() -> MotionTarget {
        MotionTarget::Word {
            kind: WordKind::Word,
            edge: WordEdge::End,
            direction: Direction::Forward,
        }
    }

    #[test]
    fn move_word_end_landing_follows_caret_geometry() {
        // The same `Word{End}` target lands differently by caret geometry: a
        // block caret (vi normal) rests *on* the last grapheme; a bar caret
        // (emacs / default) rests on the word's trailing boundary one past it.
        let mut block = vi_editor("foo bar", PromptViMode::Normal);
        block.move_to_position(0, false);
        block.run_edit_command(&EditCommand::Move(word_end_fwd()));
        assert_eq!(block.insertion_point(), 2); // on the second 'o'

        let mut bar = editor_with("foo bar"); // default = emacs, Between
        bar.move_to_position(0, false);
        bar.run_edit_command(&EditCommand::Move(word_end_fwd()));
        assert_eq!(bar.insertion_point(), 3); // trailing boundary, past the 'o'
    }

    #[test]
    fn cut_word_end_is_inclusive_of_last_char() {
        // vi `de`: same target as `e`, but the operator *consumes* the char the
        // motion lands on — so `de` from the start of "foo" deletes all of "foo".
        let mut editor = editor_with("foo bar");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::Cut {
            target: word_end_fwd(),
            granularity: Granularity::CharWise,
        });
        assert_eq!(editor.get_buffer(), " bar");
        assert_eq!(editor.cut_buffer.get().0, "foo");
    }

    // --- emacs word-command lowering (legacy `*Word*` sugar) -------------
    //
    // The `MoveWord*`/`CutWord*`/`CopyWord*` commands now lower onto the
    // `MotionTarget` verb path with `WordKind::Unicode`. These pin the two
    // properties that lowering could silently break: the emacs `M-f` "rest
    // after the word" fork, and that emacs words stay UAX-29 (contractions /
    // punctuation kept whole) rather than drifting to the vi class-word rule.

    /// Differential guard: every word command must, at *every* cursor position,
    /// produce exactly what the legacy `LineBuffer::*_index` spec produces. The
    /// `*_index` methods are still present (deleted only in a later step), so
    /// this compares the live command against the pre-migration definition
    /// across a sweep of positions — catching the position-dependent skips that
    /// the hand-picked single-position tests above missed.
    #[test]
    fn word_commands_match_legacy_spec_at_every_position() {
        let buffers = [
            "foo bar baz",
            "foo.bar baz",    // punctuation kept whole by UAX-29
            "can't stop now", // contraction
            "café résumé",    // multibyte graphemes
            "  lead trail  ", // leading / trailing whitespace
            "a",
            "",
        ];
        for buf in buffers {
            for pos in (0..=buf.len()).filter(|p| buf.is_char_boundary(*p)) {
                // Each entry: the command under test, and the resolver
                // expression it must agree with in this (default = emacs, bar)
                // editor. Move commands compare the landing position; cut/copy
                // compare the resulting (buffer, register). `wb` resolves a word
                // boundary the same way the commands' plumbing does, so this pins
                // that the dispatch/operate wiring stays faithful to `locate_word`.
                fn wb(
                    lb: &LineBuffer,
                    kind: WordKind,
                    edge: WordEdge,
                    fwd: bool,
                    block: bool,
                ) -> usize {
                    let buf = lb.get_buffer();
                    let origin = lb.insertion_point();
                    // Mirror resolve_motion's block word-end identity independently
                    // (the bar boundary one cell over, rendered one cell back).
                    use crate::core_editor::graphemes::{
                        next_grapheme_boundary, prev_grapheme_boundary,
                    };
                    use crate::core_editor::word;
                    let dir = if fwd {
                        Direction::Forward
                    } else {
                        Direction::Backward
                    };
                    if block && fwd && edge == WordEdge::End {
                        let probe = next_grapheme_boundary(buf, origin);
                        prev_grapheme_boundary(buf, word::locate_word(buf, probe, kind, edge, dir))
                    } else {
                        word::locate_word(buf, origin, kind, edge, dir)
                    }
                }
                #[allow(clippy::type_complexity)]
                let moves: &[(EditCommand, fn(&LineBuffer) -> usize)] = &[
                    (EditCommand::MoveWordLeft { select: false }, |lb| {
                        wb(lb, WordKind::Unicode, WordEdge::Start, false, false)
                    }),
                    (EditCommand::MoveBigWordLeft { select: false }, |lb| {
                        wb(lb, WordKind::LongWord, WordEdge::Start, false, false)
                    }),
                    // bar caret: forward word-end rests on the trailing boundary
                    (EditCommand::MoveWordRight { select: false }, |lb| {
                        wb(lb, WordKind::Unicode, WordEdge::End, true, false)
                    }),
                    (EditCommand::MoveWordRightStart { select: false }, |lb| {
                        wb(lb, WordKind::Unicode, WordEdge::Start, true, false)
                    }),
                    (EditCommand::MoveBigWordRightStart { select: false }, |lb| {
                        wb(lb, WordKind::LongWord, WordEdge::Start, true, false)
                    }),
                    // vi-`e` on-char reading, forced block geometry
                    (EditCommand::MoveWordRightEnd { select: false }, |lb| {
                        wb(lb, WordKind::Unicode, WordEdge::End, true, true)
                    }),
                    (EditCommand::MoveBigWordRightEnd { select: false }, |lb| {
                        wb(lb, WordKind::LongWord, WordEdge::End, true, true)
                    }),
                ];
                for (cmd, legacy) in moves {
                    let mut got = editor_with(buf);
                    got.move_to_position(pos, false);
                    got.run_edit_command(cmd);
                    let mut spec = editor_with(buf);
                    spec.move_to_position(pos, false);
                    let target = legacy(&spec.line_buffer);
                    assert_eq!(
                        got.insertion_point(),
                        target,
                        "{cmd:?} at pos {pos} of {buf:?}"
                    );
                }

                // Cut/Copy commands: legacy consumed `lo..hi`. Cut compares the
                // resulting (buffer, register); Copy leaves the buffer and only
                // fills the register.
                #[allow(clippy::type_complexity)]
                let ops: &[(
                    EditCommand,
                    EditCommand,
                    fn(&LineBuffer, usize) -> (usize, usize),
                )] = &[
                    (
                        EditCommand::CutWordLeft,
                        EditCommand::CopyWordLeft,
                        |lb, ip| (wb(lb, WordKind::Unicode, WordEdge::Start, false, false), ip),
                    ),
                    (
                        EditCommand::CutBigWordLeft,
                        EditCommand::CopyBigWordLeft,
                        |lb, ip| {
                            (
                                wb(lb, WordKind::LongWord, WordEdge::Start, false, false),
                                ip,
                            )
                        },
                    ),
                    (
                        EditCommand::CutWordRight,
                        EditCommand::CopyWordRight,
                        |lb, ip| (ip, wb(lb, WordKind::Unicode, WordEdge::End, true, false)),
                    ),
                    (
                        EditCommand::CutBigWordRight,
                        EditCommand::CopyBigWordRight,
                        |lb, ip| (ip, wb(lb, WordKind::LongWord, WordEdge::End, true, false)),
                    ),
                    (
                        EditCommand::CutWordRightToNext,
                        EditCommand::CopyWordRightToNext,
                        |lb, ip| (ip, wb(lb, WordKind::Unicode, WordEdge::Start, true, false)),
                    ),
                    (
                        EditCommand::CutBigWordRightToNext,
                        EditCommand::CopyBigWordRightToNext,
                        |lb, ip| (ip, wb(lb, WordKind::LongWord, WordEdge::Start, true, false)),
                    ),
                ];
                for (cut_cmd, copy_cmd, legacy) in ops {
                    // Cut
                    let mut got = editor_with(buf);
                    got.move_to_position(pos, false);
                    got.run_edit_command(cut_cmd);
                    let mut spec = editor_with(buf);
                    spec.move_to_position(pos, false);
                    let (lo, hi) = legacy(&spec.line_buffer, pos);
                    spec.cut_range(lo..hi);
                    let got_pair = (got.get_buffer().to_string(), got.cut_buffer.get().0);
                    let spec_pair = (spec.get_buffer().to_string(), spec.cut_buffer.get().0);
                    assert_eq!(got_pair, spec_pair, "{cut_cmd:?} at pos {pos} of {buf:?}");

                    // Copy: buffer untouched, register == legacy slice.
                    let mut got = editor_with(buf);
                    got.move_to_position(pos, false);
                    got.run_edit_command(copy_cmd);
                    assert_eq!(got.get_buffer(), buf, "{copy_cmd:?} touched buffer");
                    let expect = buf.get(lo..hi).unwrap_or("");
                    assert_eq!(
                        got.cut_buffer.get().0,
                        expect,
                        "{copy_cmd:?} at pos {pos} of {buf:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn move_word_right_rests_after_word_like_emacs_meta_f() {
        // emacs `M-f`: the bar lands *after* "foo" (byte 3), not *on* its last
        // char (byte 2, where a bare `e`/`Move(End)` would stop).
        let mut editor = editor_with("foo bar");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::MoveWordRight { select: false });
        assert_eq!(editor.insertion_point(), 3);
    }

    #[test]
    fn move_word_right_keeps_contraction_whole() {
        // UAX-29 keeps "can't" one word, so `M-f` skips past the apostrophe to
        // byte 5; a vi class-word would have stopped on the `'` at byte 3.
        let mut editor = editor_with("can't stop");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::MoveWordRight { select: false });
        assert_eq!(editor.insertion_point(), 5);
    }

    #[test]
    fn cut_word_right_consumes_whole_contraction() {
        // emacs `M-d` over "can't" removes the whole contraction (bytes 0..5),
        // leaving " stop" — a class-word `dw` would cut only "can" (0..3).
        let mut editor = editor_with("can't stop");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::CutWordRight);
        assert_eq!(editor.get_buffer(), " stop");
        assert_eq!(editor.cut_buffer.get().0, "can't");
    }

    #[test]
    fn move_word_right_with_select_extends_anchor() {
        // The `select` flag maps to Extend: anchor stays at the origin while the
        // head travels to the after-word rest position.
        let mut editor = editor_with("foo bar");
        editor.move_to_position(0, false);
        editor.run_edit_command(&EditCommand::MoveWordRight { select: true });
        assert_eq!(editor.insertion_point(), 3);
        assert_eq!(editor.get_selection(), Some((0, 3)));
    }

    // emacs forward-word completes the *current* word with no skip — the case a
    // vi-`e` verb path gets wrong. Pin the cursor-inside-word positions that the
    // word-start-only tests above can't see.

    #[test]
    fn move_word_right_from_midword_completes_current_word() {
        // `M-f` from byte 2 (between the o's of "foo") rests at 3 (after "foo"),
        // NOT 7 (which is where the vi-`e` skip would land).
        let mut editor = editor_with("foo bar");
        editor.move_to_position(2, false);
        editor.run_edit_command(&EditCommand::MoveWordRight { select: false });
        assert_eq!(editor.insertion_point(), 3);
    }

    #[test]
    fn cut_word_right_from_midword_consumes_rest_of_word() {
        // `M-d` from byte 2 in "foo bar" kills only "o" (the rest of "foo"),
        // leaving "fo bar" — a vi-`e` skip would have eaten "o bar".
        let mut editor = editor_with("foo bar");
        editor.move_to_position(2, false);
        editor.run_edit_command(&EditCommand::CutWordRight);
        assert_eq!(editor.get_buffer(), "fo bar");
        assert_eq!(editor.cut_buffer.get().0, "o");
    }

    #[test]
    fn cut_big_word_right_from_midword_consumes_rest_of_word() {
        let mut editor = editor_with("foo.bar baz");
        editor.move_to_position(2, false); // inside "foo.bar" (one big WORD)
        editor.run_edit_command(&EditCommand::CutBigWordRight);
        assert_eq!(editor.get_buffer(), "fo baz");
        assert_eq!(editor.cut_buffer.get().0, "o.bar");
    }

    // --- migration characterization -------------------------------------
    //
    // The new `MotionTarget` verbs must have the *same buffer effect* as the
    // dedicated commands they replace — the old command is the spec. These
    // assert `new == old` so they need no hand-computed vim semantics. They
    // pass on the pre-migration code, so they retroactively prove C1's `0`/`$`
    // re-lowering was behavior-preserving and *gate* C2's `f`/`t` re-lowering:
    // they must stay green after the motions emit `Cut/Move(Find)`.

    /// Run `cmd` on `buffer` from `cursor`; return (buffer, cursor, cut text).
    fn outcome(
        buffer: &str,
        cursor: usize,
        cmd: &EditCommand,
    ) -> (String, usize, Option<(usize, usize)>, String) {
        let mut editor = editor_with(buffer);
        editor.move_to_position(cursor, false);
        editor.run_edit_command(cmd);
        (
            editor.get_buffer().to_string(),
            editor.insertion_point(),
            editor.get_selection(),
            editor.cut_buffer.get().0,
        )
    }

    /// Assert two commands have identical effect from the same starting point.
    fn equivalent(buffer: &str, cursor: usize, new: &EditCommand, old: &EditCommand) {
        assert_eq!(outcome(buffer, cursor, new), outcome(buffer, cursor, old));
    }

    fn find(ch: char, direction: Direction, stop: FindStop) -> MotionTarget {
        MotionTarget::Find {
            ch,
            direction,
            stop,
        }
    }

    // C1 backfill: `0`/`$` line edges vs the dedicated line cut/copy commands.

    #[test]
    fn cut_line_edge_matches_dedicated_line_cuts() {
        // `d$` and `d0` on a single line.
        equivalent(
            "foo bar",
            2,
            &EditCommand::Cut {
                target: MotionTarget::LineEdge(Direction::Forward),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutToLineEnd,
        );
        equivalent(
            "foo bar",
            4,
            &EditCommand::Cut {
                target: MotionTarget::LineEdge(Direction::Backward),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutFromLineStart,
        );
    }

    #[test]
    fn copy_line_edge_matches_dedicated_line_copies() {
        equivalent(
            "foo bar",
            2,
            &EditCommand::Copy {
                target: MotionTarget::LineEdge(Direction::Forward),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CopyToLineEnd,
        );
        equivalent(
            "foo bar",
            4,
            &EditCommand::Copy {
                target: MotionTarget::LineEdge(Direction::Backward),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CopyFromLineStart,
        );
    }

    #[test]
    fn cut_line_edge_forward_stops_at_newline() {
        // The riskiest C1 claim: on a multiline buffer `d$` must cut only to the
        // `\n`, matching `CutToLineEnd` — not run to the buffer end.
        equivalent(
            "ab\ncd",
            0,
            &EditCommand::Cut {
                target: MotionTarget::LineEdge(Direction::Forward),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutToLineEnd,
        );
        let (buffer, cursor, _selection, cut) = outcome(
            "ab\ncd",
            0,
            &EditCommand::Cut {
                target: MotionTarget::LineEdge(Direction::Forward),
                granularity: Granularity::CharWise,
            },
        );
        assert_eq!(buffer, "\ncd");
        assert_eq!(cursor, 0);
        assert_eq!(cut, "ab");
    }

    // C3 gate: `gg`/`G` (BufferEdge) vs the dedicated MoveToStart/MoveToEnd.
    // BufferEdge ignores line breaks — it goes to the buffer edge, not a line
    // edge — so these also confirm the multiline behavior.

    #[test]
    fn move_buffer_edge_matches_move_to_start_end() {
        equivalent(
            "foo bar",
            3,
            &EditCommand::Move(MotionTarget::BufferEdge(Direction::Backward)),
            &EditCommand::MoveToStart { select: false },
        );
        equivalent(
            "foo bar",
            3,
            &EditCommand::Move(MotionTarget::BufferEdge(Direction::Forward)),
            &EditCommand::MoveToEnd { select: false },
        );
    }

    #[test]
    fn extend_buffer_edge_matches_move_to_start_end_selecting() {
        // visual `gg`/`G` — the selection must match too (now compared by `outcome`)
        equivalent(
            "foo bar",
            3,
            &EditCommand::Extend(MotionTarget::BufferEdge(Direction::Backward)),
            &EditCommand::MoveToStart { select: true },
        );
        equivalent(
            "foo bar",
            3,
            &EditCommand::Extend(MotionTarget::BufferEdge(Direction::Forward)),
            &EditCommand::MoveToEnd { select: true },
        );
    }

    #[test]
    fn buffer_edge_spans_lines() {
        // from the second line, `gg` lands at buffer start (not the line start)
        // and `G` at buffer end.
        equivalent(
            "ab\ncd",
            4,
            &EditCommand::Move(MotionTarget::BufferEdge(Direction::Backward)),
            &EditCommand::MoveToStart { select: false },
        );
        equivalent(
            "ab\ncd",
            0,
            &EditCommand::Move(MotionTarget::BufferEdge(Direction::Forward)),
            &EditCommand::MoveToEnd { select: false },
        );
    }

    // C2 gate: `f`/`t`/`F`/`T` (Find) vs the dedicated char-search commands.

    #[test]
    fn cut_find_forward_on_matches_cut_right_until() {
        // df b
        equivalent(
            "foo bar baz",
            0,
            &EditCommand::Cut {
                target: find('b', Direction::Forward, FindStop::On),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutRightUntil('b'),
        );
    }

    #[test]
    fn cut_find_forward_before_matches_cut_right_before() {
        // dt b
        equivalent(
            "foo bar baz",
            0,
            &EditCommand::Cut {
                target: find('b', Direction::Forward, FindStop::Before),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutRightBefore('b'),
        );
    }

    #[test]
    fn cut_find_backward_on_matches_cut_left_until() {
        // dF o (cursor at end of buffer)
        equivalent(
            "foo bar baz",
            11,
            &EditCommand::Cut {
                target: find('o', Direction::Backward, FindStop::On),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutLeftUntil('o'),
        );
    }

    #[test]
    fn cut_find_backward_before_matches_cut_left_before() {
        // dT o
        equivalent(
            "foo bar baz",
            11,
            &EditCommand::Cut {
                target: find('o', Direction::Backward, FindStop::Before),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutLeftBefore('o'),
        );
    }

    #[test]
    fn cut_find_absent_char_is_noop() {
        equivalent(
            "foo bar",
            0,
            &EditCommand::Cut {
                target: find('z', Direction::Forward, FindStop::On),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CutRightUntil('z'),
        );
    }

    #[test]
    fn copy_find_forward_matches_copy_right_until() {
        equivalent(
            "foo bar baz",
            0,
            &EditCommand::Copy {
                target: find('b', Direction::Forward, FindStop::On),
                granularity: Granularity::CharWise,
            },
            &EditCommand::CopyRightUntil('b'),
        );
    }

    #[test]
    fn move_find_forward_matches_move_right_until() {
        // Guards the `f`-vs-`;` two-path divergence: bare `f` (which will emit
        // `Move(Find)`) must land where the replay path `;` lands — and `;`
        // keeps using `MoveRightUntil`.
        equivalent(
            "foo bar baz",
            0,
            &EditCommand::Move(find('b', Direction::Forward, FindStop::On)),
            &EditCommand::MoveRightUntil {
                c: 'b',
                select: false,
            },
        );
    }

    // The remaining three `Move` corners gate C2(b): `;`/`,` replay re-emits
    // `Move(stored Find)`, and `,` reverses the stored direction. Proving each
    // `Move(Find{..})` matches the dedicated `Move*Until`/`Move*Before` it
    // replaces means the replay migration preserves where the cursor lands —
    // including the reversed (`,`) direction.

    #[test]
    fn move_find_forward_before_matches_move_right_before() {
        // bare `;` after `t`
        equivalent(
            "foo bar baz",
            0,
            &EditCommand::Move(find('b', Direction::Forward, FindStop::Before)),
            &EditCommand::MoveRightBefore {
                c: 'b',
                select: false,
            },
        );
    }

    #[test]
    fn move_find_backward_on_matches_move_left_until() {
        // bare `;` after `F`, and the `,`-reverse of `f`
        equivalent(
            "foo bar baz",
            11,
            &EditCommand::Move(find('o', Direction::Backward, FindStop::On)),
            &EditCommand::MoveLeftUntil {
                c: 'o',
                select: false,
            },
        );
    }

    #[test]
    fn move_find_backward_before_matches_move_left_before() {
        // bare `;` after `T`, and the `,`-reverse of `t`
        equivalent(
            "foo bar baz",
            11,
            &EditCommand::Move(find('o', Direction::Backward, FindStop::Before)),
            &EditCommand::MoveLeftBefore {
                c: 'o',
                select: false,
            },
        );
    }
}