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
//! [`VimEditorExt`] — vim-discipline accessor methods on the engine
//! [`Editor`], migrated out of `hjkl-engine` (#267 / #265 G3).
//!
//! These read the vim FSM state (`Editor::vim`) to answer render/selection
//! questions. They belong to the vim *discipline*, not the mode-agnostic
//! engine core, so they live here — a blanket trait impl on
//! `Editor<View, H>`. As `VimState` finishes relocating into this crate,
//! more of the engine's vim accessors move onto this trait; call sites pick
//! them up with `use hjkl_vim::VimEditorExt`.
use crate::vim::{
AbbrevTrigger, InsertDir, InsertReason, LastVisual, Motion, Operator, RangeKind, TextObject,
};
use hjkl_engine::input::Input;
use hjkl_engine::types::{Highlight, HighlightKind, Host, Pos};
use hjkl_engine::{Editor, FsmMode, MarkJump, MotionKind, VimMode};
/// Move a position back by one character, wrapping to the end of the previous
/// line when at column 0. Clamps at the buffer start `(0, 0)`. Used to render
/// exclusive (VSCode) char selections via the inclusive buffer-tui paint path.
///
/// Was `Editor::dec_pos_one_char` in the engine; it exists only to serve
/// [`VimEditorExt::buffer_selection`], so it moved here with it.
fn dec_pos_one_char<H: Host>(
ed: &Editor<hjkl_buffer::View, H>,
p: hjkl_buffer::Position,
) -> hjkl_buffer::Position {
use hjkl_buffer::Position;
if p.col > 0 {
return Position::new(p.row, p.col - 1);
}
if p.row > 0 {
let prev = p.row - 1;
let len = ed.line(prev).map_or(0, |l| l.chars().count());
return Position::new(prev, len);
}
Position::new(0, 0)
}
/// Common post-mutation sync for the `insert_*` primitives.
///
/// The vim FSM's `step` runs `ensure_cursor_in_scrolloff` at the end of every
/// normal/visual motion; insert-mode primitives bypass `step` and must
/// self-correct or the cursor scrolls off the viewport (held Enter, multi-line
/// backspace at BOL, arrow keys at edge, etc.).
///
/// Marks the content dirty, widens the insert row's autoindent tracking, and
/// re-checks scrolloff. Was `Editor::after_insert_mutation` (#267) — it exists
/// only to serve the insert primitives, so it moved here with them.
fn after_insert_mutation<H: Host>(ed: &mut Editor<hjkl_buffer::View, H>) {
ed.mark_content_dirty();
let (row, _) = ed.cursor();
crate::vim_state::vim_mut(ed).widen_insert_row(row);
ed.ensure_cursor_in_scrolloff();
}
/// Like [`after_insert_mutation`] but for cursor-only insert ops that do not
/// change content (arrows, Home/End, PageUp/Down). Skips the dirty mark.
fn after_insert_motion<H: Host>(ed: &mut Editor<hjkl_buffer::View, H>) {
let (row, _) = ed.cursor();
crate::vim_state::vim_mut(ed).widen_insert_row(row);
ed.ensure_cursor_in_scrolloff();
}
/// Vim-discipline read accessors layered onto every `Editor<View, H>`.
///
/// Blanket-implemented below; bring it into scope with
/// `use hjkl_vim::VimEditorExt` to call these on an `Editor`.
pub trait VimEditorExt {
/// VisualBlock selection bounds as `(top, bot, left, right)` — inclusive
/// rows and inclusive columns, derived from the block anchor and the
/// cursor's sticky column. Meaningful only while in VisualBlock mode;
/// callers that need the "are we in block mode?" guard use
/// [`VimEditorExt::block_highlight`] instead.
fn visual_block_bounds(&self) -> (usize, usize, usize, usize);
/// The VisualBlock highlight rectangle `(top, bot, left, right)`, or
/// `None` when the editor is not in VisualBlock mode.
fn block_highlight(&self) -> Option<(usize, usize, usize, usize)>;
/// Start/end `(row, col)` of the active char-wise Visual selection,
/// positionally ordered. `None` when not in Visual mode.
///
/// When [`hjkl_engine::editor::Settings::selection_exclusive`] is `false`
/// (default, vim behaviour): both endpoints are **inclusive** — the cells
/// at `start` and `end` are both selected.
///
/// When it is `true` (VSCode bar-cursor behaviour): the range is
/// **half-open** — `start` is included but `end` is the first cell that is
/// NOT selected (the caret sits before it). If the selection is empty
/// (`anchor == cursor`) `None` is returned so callers do not need to check
/// for zero-length ranges.
fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))>;
/// Return the half-open exclusive char-visual range `(start, end)` where
/// `end` is the first cell NOT selected (the caret position). `None`
/// when not in Visual mode or the selection is empty.
///
/// Convenience accessor for the VSCode dispatcher; avoids duplicating
/// the anchor/cursor ordering logic at the call site.
fn visual_char_range_exclusive(&self) -> Option<((usize, usize), (usize, usize))>;
/// Top/bottom rows of the active VisualLine selection (inclusive).
/// `None` when we're not in VisualLine mode.
fn line_highlight(&self) -> Option<(usize, usize)>;
/// Active selection in `hjkl_buffer::Selection` shape. `None` when not in
/// a Visual mode. The host hands this straight to `BufferView`.
fn buffer_selection(&self) -> Option<hjkl_buffer::Selection>;
/// Active visual selection as a SPEC [`Highlight`] with
/// [`HighlightKind::Selection`].
///
/// Returns `None` when the editor isn't in a Visual mode. Visual-line and
/// visual-block selections collapse to the bounding char range of the
/// selection — the SPEC `Selection` kind doesn't carry sub-line info
/// today; hosts that need full line / block geometry continue to read
/// [`VimEditorExt::buffer_selection`] (the legacy `hjkl_buffer::Selection`
/// shape).
fn selection_highlight(&self) -> Option<Highlight>;
// ─── Text-object resolution (hjkl#70) ──────────────────────────────────
//
// Pure functions — no cursor mutation, no mode change, no register write.
// Each delegates to the `crate::vim::text_object_*_bridge` resolvers,
// which remain in the engine until vim.rs itself relocates (#267).
//
// Return value: `Some((start, end))` where both positions are `(row, col)`
// char-column pairs and `end` is *exclusive* (one past the last char to act
// on), matching the convention used by `delete_range` / `yank_range` / etc.
//
// Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
// Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
// close-bracket variants are NOT accepted — the grammar layer normalises
// close→open before calling these.
/// Resolve the range of `iw` (inner word) at the cursor.
///
/// An inner word is the contiguous run of keyword characters (or
/// punctuation characters if the cursor is on punctuation) under the
/// cursor, without surrounding whitespace. Whitespace-only positions
/// return `None`.
fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `aw` (around word) at the cursor.
///
/// Like `iw` but extends the range to include trailing whitespace after
/// the word. If no trailing whitespace exists, leading whitespace before
/// the word is absorbed instead (vim `:help text-objects` behaviour).
fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `iW` (inner WORD) at the cursor.
///
/// A WORD is any contiguous run of non-whitespace characters — punctuation
/// is not a word boundary.
fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `aW` (around WORD) at the cursor.
fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `i<quote>` (inner quote) at the cursor.
///
/// Excludes the quote characters themselves. `None` when the cursor's line
/// contains fewer than two occurrences of `quote`, or no matching pair can
/// be found around or ahead of the cursor.
fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `a<quote>` (around quote) at the cursor.
///
/// Like `i<quote>` but includes the quote characters plus surrounding
/// whitespace on one side: trailing after the closing quote if any exists,
/// otherwise leading before the opening quote.
fn text_object_around_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
///
/// The cursor may be anywhere inside the pair or on a bracket character.
/// When not inside any pair the resolver falls back to a forward scan
/// (targets.vim-style: `ci(` works when the cursor is before `(`).
/// Multi-line pairs are supported.
fn text_object_inner_bracket(&self, open: char) -> Option<((usize, usize), (usize, usize))>;
/// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
///
/// Like `i<bracket>` but includes the bracket characters themselves.
fn text_object_around_bracket(&self, open: char) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `is` (inner sentence) at the cursor.
///
/// Excludes trailing whitespace. Sentence boundaries follow vim's `is`
/// semantics (period / `?` / `!` followed by whitespace or
/// end-of-paragraph).
fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `as` (around sentence) at the cursor.
///
/// Like `is` but includes trailing whitespace after the terminator.
fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `ip` (inner paragraph) at the cursor.
///
/// A paragraph is a block of non-blank lines bounded by blank lines or
/// buffer edges. `None` when the cursor is on a blank line.
fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `ap` (around paragraph) at the cursor.
///
/// Like `ip` but includes one trailing blank line when present.
fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `it` (inner tag) at the cursor.
///
/// Matches XML/HTML-style `<tag>...</tag>` pairs, returning the content
/// between the open and close tags (excluding the tags themselves).
fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))>;
/// Resolve `at` (around tag) at the cursor.
///
/// Like `it` but includes the open and close tag delimiters.
fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))>;
// ─── Range-mutation primitives (hjkl#70) ───────────────────────────────
//
// These do not consume input — the caller (the visual-mode operator path)
// has already resolved the range from the visual selection before calling
// in. Normal-mode op dispatch continues to use `apply_op_motion` /
// `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
/// Delete the region `[start, end)` and stash the removed text in
/// `register`. `'"'` selects the unnamed register (vim default);
/// `'a'`–`'z'` select named registers.
fn delete_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
);
/// Yank (copy) the region `[start, end)` into `register` without mutating
/// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
/// register; `'a'`–`'z'` select named registers.
fn yank_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
);
/// Delete the region `[start, end)` and transition to Insert mode (vim `c`
/// operator). The deleted text is stashed in `register`. On return the
/// editor is in Insert mode; the caller must not issue further normal-mode
/// ops until the insert session ends.
fn change_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
);
/// Indent (`count > 0`) or outdent (`count < 0`) the row span
/// `[start.0, end.0]`. Column components are ignored — indent is always
/// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
/// this call; pass `0` to use the current editor setting. `count == 0` is
/// a no-op.
fn indent_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
count: i32,
shiftwidth: u32,
);
/// Apply a case transformation (`Operator::Uppercase` /
/// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
/// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
/// Registers are left untouched — vim's case operators do not write to
/// registers.
fn case_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
op: Operator,
);
// ─── Block-shape range-mutation primitives (hjkl#70) ───────────────────
//
// Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
// line indices; `left_col`/`right_col` are inclusive char-column bounds.
// Ragged-edge handling (short lines not reaching `right_col`) matches the
// engine FSM's `apply_block_operator` path — short lines lose only the
// chars that exist. `register` is the target; `'"'` selects unnamed.
/// Delete a rectangular VisualBlock selection.
fn delete_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
);
/// Yank a rectangular VisualBlock selection into `register` without
/// mutating the buffer.
fn yank_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
);
/// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
/// operator). Mode is Insert on return.
fn change_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
);
/// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
/// Column bounds are ignored — vim's block indent is always linewise.
/// `count == 0` is a no-op.
fn indent_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
count: i32,
);
/// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
/// Column components are ignored — auto-indent is always linewise.
///
/// The algorithm is a naive bracket-depth counter: it scans the buffer
/// from row 0 to compute the correct depth at `start.0`, then for each
/// line in the target range strips existing leading whitespace and
/// prepends `depth × indent_unit`. Lines whose first non-whitespace
/// character is a close bracket get one fewer indent level. Empty /
/// whitespace-only lines are cleared. After the operation the cursor lands
/// on the first non-whitespace character of `start_row` (vim parity `==`).
///
/// **v1 limitation**: the bracket scan does not detect brackets inside
/// string literals or comments.
fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize));
// ─── Paste ─────────────────────────────────────────────────────────────
/// `p` — paste the unnamed register (or the register selected via `"r`)
/// after the cursor. Linewise content opens a new line below; charwise
/// content is inserted inline. Records `Paste { before: false }` for `.`.
fn paste_after(&mut self, count: usize);
/// `P` — paste the unnamed register (or the `"r` register) before the
/// cursor. Linewise content opens a new line above; charwise is inline.
/// Records `Paste { before: true }` for dot-repeat.
fn paste_before(&mut self, count: usize);
/// `gp` / `gP` — paste like `p`/`P` but leave the cursor just after the
/// pasted text. `before = true` for `gP`.
fn paste_cursor_after(&mut self, before: bool, count: usize);
/// `]p` / `[p` — linewise paste with the pasted block reindented to match
/// the current line. `before = true` for `[p`.
fn paste_reindent(&mut self, before: bool, count: usize);
/// Visual-mode `p` / `P` — replace the active selection with the register.
/// `before = true` for `P` (preserves the source register).
fn visual_paste(&mut self, before: bool);
// ─── Visual-mode operators ─────────────────────────────────────────────
/// Visual-mode `<C-a>`/`<C-x>` (uniform) and `g<C-a>`/`g<C-x>`
/// (`sequential`) — adjust the first number on each selected line.
fn adjust_number_visual(&mut self, delta: i64, sequential: bool);
/// Normal-mode `&` — repeat the last `:s` on the current line (no flags).
fn ampersand_repeat(&mut self);
/// Visual-mode `J` (`with_space = true`) / `gJ` (`false`) — join the
/// selected lines into one.
fn visual_join(&mut self, with_space: bool);
/// `[count]%` — jump to the line at `count` percent of the file.
fn goto_percent(&mut self, count: usize);
// ─── Jumplist motion ───────────────────────────────────────────────────
/// `<C-o>` — jump back `count` entries in the jumplist, saving the current
/// position on the forward stack so `<C-i>` can return.
fn jump_back(&mut self, count: usize);
/// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
/// saving the current position on the backward stack.
fn jump_forward(&mut self, count: usize);
// ─── Search ────────────────────────────────────────────────────────────
/// `n` — repeat the last `/` or `?` search `count` times in its original
/// direction. `forward = true` keeps the direction; `false` inverts (`N`).
fn search_repeat(&mut self, forward: bool, count: usize);
/// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
/// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
/// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
fn word_search(&mut self, forward: bool, whole_word: bool, count: usize);
// ─── Chord appliers ────────────────────────────────────────────────────
//
// Each applies a completed chord with a pre-captured count, so the
// pending-state reducers can dispatch without re-entering the engine FSM.
/// `r<x>` — replace the char under the cursor with `ch`, `count` times.
/// Cursor ends on the last replaced char; one undo snapshot at start.
fn replace_char_at(&mut self, ch: char, count: usize);
/// `f`/`F`/`t`/`T` — find `ch` on the current line. `forward` chooses
/// direction, `till` stops one char short. Records `last_find` for `;`/`,`.
fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize);
/// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
fn after_g(&mut self, ch: char, count: usize);
/// Apply the z-chord effect for `z<ch>` with a pre-captured `count` —
/// `zz`/`zt`/`zb` (scroll-cursor), the fold ops, and `zf`.
fn after_z(&mut self, ch: char, count: usize);
// ─── Operator dispatch ─────────────────────────────────────────────────
/// Apply an operator over a single-key motion (e.g. `dw`, `d$`, `dG`).
/// The engine resolves `motion_key` to a `Motion` via `parse_motion`.
/// `total_count` is the folded product of prefix and inner counts. No-op
/// when `motion_key` is not a known motion (vim cancels the operator).
fn apply_op_motion(&mut self, op: Operator, motion_key: char, total_count: usize);
/// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
fn apply_op_double(&mut self, op: Operator, total_count: usize);
/// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
/// `dT<x>`). Records `last_find` for `;` / `,` repeat and updates
/// `last_change` when `op` is Change (dot-repeat).
fn apply_op_find(&mut self, op: Operator, ch: char, forward: bool, till: bool, count: usize);
/// Apply an operator over a text-object range (`diw` / `daw` / `di"` …).
/// Unknown `ch` values are silently ignored, matching the FSM.
fn apply_op_text_obj(&mut self, op: Operator, ch: char, inner: bool, total_count: usize);
/// Apply an operator over a g-chord motion or case-op linewise form
/// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` …).
fn apply_op_g(&mut self, op: Operator, ch: char, total_count: usize);
// ─── Mode transitions ──────────────────────────────────────────────────
//
// Both the FSM and these wrappers write `current_mode`, so `vim_mode()`
// returns correct values regardless of which path performed the
// transition.
/// The current vim mode (Normal / Insert / Visual / VisualLine / VisualBlock).
fn vim_mode(&self) -> VimMode;
/// `v` from Normal — enter charwise Visual mode, anchoring the selection
/// at the current cursor position.
fn enter_visual_char(&mut self);
/// `V` from Normal — enter linewise Visual mode, anchoring on the current
/// line. Motions extend the selection by whole lines.
fn enter_visual_line(&mut self);
/// `<C-v>` from Normal — enter Visual-block mode. The selection is a
/// rectangle whose corners are the anchor and the live cursor.
fn enter_visual_block(&mut self);
/// Esc from any visual mode — set `<` / `>` marks, stash the selection for
/// `gv` re-entry, then return to Normal mode.
fn exit_visual_to_normal(&mut self);
/// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor so
/// the user can extend the other end of the selection. Does NOT mutate the
/// selection range; only the active endpoint changes.
fn visual_o_toggle(&mut self);
/// `gv` — restore the last visual selection (mode + anchor + cursor
/// position). No-op when no visual selection has been exited yet.
fn reenter_last_visual(&mut self);
/// Direct mode-transition entry point. Sets both the internal FSM mode and
/// the stable `current_mode` field read by `vim_mode()`.
///
/// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
/// …) which also set up required bookkeeping (anchors, sessions, …). Use
/// `set_mode` only when you need a raw mode flip without side-effects.
fn set_mode(&mut self, mode: VimMode);
// ─── Visual anchors ────────────────────────────────────────────────────
/// The charwise Visual-mode anchor `(row, col)`.
fn visual_anchor(&self) -> (usize, usize);
/// Set the charwise Visual-mode anchor.
fn set_visual_anchor(&mut self, anchor: (usize, usize));
/// The linewise Visual-mode anchor row.
fn visual_line_anchor(&self) -> usize;
/// Set the linewise Visual-mode anchor row.
fn set_visual_line_anchor(&mut self, row: usize);
/// The VisualBlock anchor `(row, col)`.
fn block_anchor(&self) -> (usize, usize);
/// Set the VisualBlock anchor.
fn set_block_anchor(&mut self, anchor: (usize, usize));
/// The VisualBlock sticky (virtual) column.
fn block_vcol(&self) -> usize;
/// Set the VisualBlock sticky (virtual) column.
fn set_block_vcol(&mut self, vcol: usize);
/// Whether the VisualBlock selection is "ragged" (`$` was pressed —
/// `:h v_b_$`): every row resolves its own right edge to its own EOL
/// instead of the block's fixed `right` column.
fn block_to_eol(&self) -> bool;
/// Set the VisualBlock ragged (`$`) flag.
fn set_block_to_eol(&mut self, to_eol: bool);
// ─── Yank / register staging ───────────────────────────────────────────
/// Set the pending `"r` register selector without consuming it.
fn set_pending_register_raw(&mut self, reg: Option<char>);
/// Take (and clear) the pending `"r` register selector.
fn take_pending_register_raw(&mut self) -> Option<char>;
// ─── Macro recording / replay ──────────────────────────────────────────
/// Register currently being recorded into via `q{reg}`, if any.
fn recording_macro(&self) -> Option<char>;
/// Set (or clear) the register being recorded into.
fn set_recording_macro(&mut self, reg: Option<char>);
/// Append an input to the in-flight macro recording.
fn push_recording_key(&mut self, input: Input);
/// Take (and clear) the recorded macro keys.
fn take_recording_keys(&mut self) -> Vec<Input>;
/// Replace the recorded macro keys wholesale.
fn set_recording_keys(&mut self, keys: Vec<Input>);
/// Number of keys recorded so far.
fn recording_keys_len(&self) -> usize;
/// Whether a macro is currently being replayed.
fn is_replaying_macro_raw(&self) -> bool;
/// Set the macro-replay flag.
fn set_replaying_macro_raw(&mut self, v: bool);
/// The last macro register played, for `@@`.
fn last_macro(&self) -> Option<char>;
/// Set the last macro register played.
fn set_last_macro(&mut self, reg: Option<char>);
// ─── Last insert / visual / viewport ───────────────────────────────────
/// Position where the last insert session ended (`gi`).
fn last_insert_pos(&self) -> Option<(usize, usize)>;
/// Set the last insert-session end position.
fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>);
/// Snapshot of the last visual selection, for `gv`.
fn last_visual(&self) -> Option<LastVisual>;
/// Set the last-visual snapshot.
fn set_last_visual(&mut self, snap: Option<LastVisual>);
/// Whether `Ctrl-R` is armed and awaiting a register name.
fn insert_pending_register(&self) -> bool;
/// Set the `Ctrl-R` pending-register flag.
fn set_insert_pending_register(&mut self, v: bool);
// ─── Change-mark start ─────────────────────────────────────────────────
/// The stashed `[` mark start for a Change operation, or `None`.
fn change_mark_start(&self) -> Option<(usize, usize)>;
/// Take (and clear) the stashed `[` mark start.
fn take_change_mark_start(&mut self) -> Option<(usize, usize)>;
/// Set the stashed `[` mark start.
fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>);
// ─── Visual / motion / search primitives ───────────────────────────────
//
// Vim *semantics* — motions, operators over selections, block-edge insert,
// search entry. These do not belong on a mode-agnostic rope editor; the
// engine keeps the raw buffer primitives (cursor, line reads, edits) and
// the vim discipline layers meaning on top (#265 / #267).
/// `true` when the editor is in any visual mode (Visual / VisualLine /
/// VisualBlock).
fn is_visual(&self) -> bool;
/// Apply `op` over `motion` with `count` repetitions, taking the full
/// vim-quirks path (operator context for `l`, clamping, etc.).
fn apply_op_with_motion_direct(&mut self, op: Operator, motion: &Motion, count: usize);
/// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
/// `delta = 1` increments, `-1` decrements; larger deltas multiply as in
/// vim's `5<C-a>`.
fn adjust_number(&mut self, delta: i64);
/// Open the `/` or `?` search prompt. `forward = true` for `/`.
fn enter_search(&mut self, forward: bool);
/// `d/pat` / `c/pat` / `y/pat` — open the search prompt in operator-pending
/// mode so the operator applies over the range to the match on commit.
fn enter_search_op(&mut self, forward: bool, op: Operator, count: usize);
/// Apply a pending operator-search over the exclusive charwise range from
/// `origin` to the current cursor (the just-found match position).
fn apply_op_search_range(&mut self, op: Operator, origin: (usize, usize));
/// VisualBlock `I` — enter Insert at the left edge of the block.
/// `count` repeats the typed text on every row (`[count]I`).
fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize, count: usize);
/// VisualBlock `A` — enter Insert at the right edge of the block.
/// `col` is the append/typed column (one past the block's right edge,
/// or the ragged `$` column); `left` is the block's own left edge,
/// where the cursor lands on Esc (verified against real nvim — `A`'s
/// post-Esc cursor is NOT `col` on a block wider than one column).
/// `count` repeats the typed text on every row (`[count]A`).
fn visual_block_append_at_right(
&mut self,
top: usize,
bot: usize,
col: usize,
left: usize,
count: usize,
);
/// Execute a motion, pushing to the jumplist for big jumps and updating the
/// sticky column.
fn execute_motion(&mut self, motion: Motion, count: usize);
/// Update the VisualBlock virtual column after a motion. Horizontal motions
/// sync `block_vcol` to the cursor column; vertical motions leave it alone
/// so the intended column survives clamping to shorter rows.
fn update_block_vcol(&mut self, motion: &Motion);
/// Apply `op` over the current visual selection (char-wise, linewise, or
/// block).
fn apply_visual_operator(&mut self, op: Operator, count: usize);
/// VisualBlock `r<ch>` — replace every character cell in the block with
/// `ch`.
fn replace_block_char(&mut self, ch: char);
/// Charwise (`v`) / linewise (`V`) Visual-mode `r<ch>` — replace every
/// character in the selection with `ch` (B2). Newlines are preserved;
/// registers are untouched.
fn visual_replace_char(&mut self, ch: char);
/// Visual-mode `i<ch>` / `a<ch>` — extend the selection to cover the text
/// object identified by `ch`.
fn visual_text_obj_extend(&mut self, ch: char, inner: bool);
// ─── Insert-mode primitives ────────────────────────────────────────────
//
// Each wraps a `crate::vim::insert_*_bridge` and, when the bridge
// reports a mutation, runs the post-mutation sync (dirty mark, insert-row
// widening, scrolloff correction). Callers must ensure the editor is in
// Insert (or Replace) mode first.
/// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
/// the cursor instead; at end-of-line, always appends. With `smartindent`,
/// closing brackets trigger a one-unit dedent on an otherwise-whitespace
/// line.
fn insert_char(&mut self, ch: char);
/// Insert a newline, applying autoindent / smartindent.
fn insert_newline(&mut self);
/// Insert a tab (or spaces to the next `softtabstop` boundary under
/// `expandtab`).
fn insert_tab(&mut self);
/// Backspace. Deletes a whole soft-tab run at an aligned boundary under
/// `softtabstop`; joins with the previous line at column 0.
fn insert_backspace(&mut self);
/// Delete the char under the cursor; joins with the next line at EOL.
fn insert_delete(&mut self);
/// Arrow-key motion in Insert, breaking the undo group per
/// `undo_break_on_motion`.
fn insert_arrow(&mut self, dir: InsertDir);
/// Home in Insert.
fn insert_home(&mut self);
/// End in Insert.
fn insert_end(&mut self);
/// PageUp in Insert.
fn insert_pageup(&mut self, viewport_h: u16);
/// PageDown in Insert.
fn insert_pagedown(&mut self, viewport_h: u16);
/// `Ctrl-W` — delete the word before the cursor.
fn insert_ctrl_w(&mut self);
/// `Ctrl-U` — delete to the start of the line.
fn insert_ctrl_u(&mut self);
/// `Ctrl-H` — backspace equivalent.
fn insert_ctrl_h(&mut self);
/// `Ctrl-O` — arm a one-shot Normal-mode command.
fn insert_ctrl_o_arm(&mut self);
/// `Ctrl-R` — arm register paste; the next char names the register.
fn insert_ctrl_r_arm(&mut self);
/// `Ctrl-T` — indent the current line one `shiftwidth`.
fn insert_ctrl_t(&mut self);
/// `Ctrl-D` — dedent the current line one `shiftwidth`.
fn insert_ctrl_d(&mut self);
/// `Ctrl-A` — insert the text typed during the most recent insert
/// session (vim's "." register).
fn insert_ctrl_a(&mut self);
/// `Ctrl-E` — insert the char in the same column of the line below.
fn insert_ctrl_e(&mut self);
/// `Ctrl-Y` — insert the char in the same column of the line above.
fn insert_ctrl_y(&mut self);
/// Paste register `reg` at the cursor (the `Ctrl-R` follow-up).
fn insert_paste_register(&mut self, reg: char);
/// `Ctrl-[` — expand any pending abbreviation (Esc-equivalent trigger).
fn insert_ctrl_bracket(&mut self);
/// Esc from Insert — end the insert session and return to Normal.
fn leave_insert_to_normal(&mut self);
// ─── Insert-mode entry ─────────────────────────────────────────────────
/// `i` — insert before the cursor, `count` times on commit.
fn enter_insert_i(&mut self, count: usize);
/// `I` — insert at the first non-blank of the line.
fn enter_insert_shift_i(&mut self, count: usize);
/// `a` — append after the cursor.
fn enter_insert_a(&mut self, count: usize);
/// `A` — append at end-of-line.
fn enter_insert_shift_a(&mut self, count: usize);
/// `o` — open a new line below and insert.
fn open_line_below(&mut self, count: usize);
/// `O` — open a new line above and insert.
fn open_line_above(&mut self, count: usize);
/// `R` — enter Replace mode.
fn enter_replace_mode(&mut self, count: usize);
// ─── Normal-mode edit primitives ───────────────────────────────────────
/// `x` — delete `count` chars forward.
fn delete_char_forward(&mut self, count: usize);
/// `X` — delete `count` chars backward.
fn delete_char_backward(&mut self, count: usize);
/// `s` — substitute `count` chars (delete then insert).
fn substitute_char(&mut self, count: usize);
/// `S` — substitute whole lines.
fn substitute_line(&mut self, count: usize);
/// `D` — delete to end-of-line (`[count]D` extends down count-1 lines).
fn delete_to_eol(&mut self, count: usize);
/// `C` — change to end-of-line (`[count]C` extends down count-1 lines).
fn change_to_eol(&mut self, count: usize);
/// `Y` — yank to end-of-line.
fn yank_to_eol(&mut self, count: usize);
/// `J` — join `count` lines.
fn join_line(&mut self, count: usize);
/// `~` — toggle case of `count` chars, advancing right.
fn toggle_case_at_cursor(&mut self, count: usize);
// ─── Vim mark commands ─────────────────────────────────────────────────
//
// Mark *storage* (`Editor::mark` / `set_mark` / `marks()` / `file_marks()`
// / the global-mark map) stays on the engine: a mark is a positional
// bookmark, which is an editor concern that other seams already consume
// (hjkl-ex backs `:marks` and `'a` line addressing with it, and LSP /
// quickfix / bookmark features could too).
//
// What lives here is the vim *command* layer on top of that storage — the
// `m` / `'` / `` ` `` keybindings, which decide linewise vs charwise jump
// and push the jumplist. That is vim semantics, not bookmark storage.
/// `.` — dot-repeat: replay the last buffered change at the cursor. A
/// non-zero `count` *replaces* the change's stored count (`:h .` — `3x`
/// then `2.` deletes 2, not 6); `count == 0` means no explicit count.
fn replay_last_change(&mut self, count: usize);
/// `m{ch}` — record a mark named `ch` at the current cursor position.
/// Invalid chars are silently ignored.
fn set_mark_at_cursor(&mut self, ch: char);
/// `'{ch}` — jump to mark `ch`, linewise (row, first non-blank). Pushes the
/// pre-jump position onto the jumplist if the cursor actually moved.
fn goto_mark_line(&mut self, ch: char);
/// `` `{ch} `` — jump to mark `ch`, charwise (exact row + col). Pushes the
/// pre-jump position onto the jumplist if the cursor actually moved.
fn goto_mark_char(&mut self, ch: char);
/// Like [`VimEditorExt::goto_mark_line`], but reports cross-buffer jumps:
/// uppercase marks (`'A'`–`'Z'`) living in another buffer return
/// [`MarkJump::CrossBuffer`] so the app can switch slots first.
fn try_goto_mark_line(&mut self, ch: char) -> MarkJump;
/// Charwise counterpart of [`VimEditorExt::try_goto_mark_line`].
fn try_goto_mark_char(&mut self, ch: char) -> MarkJump;
// ─── Vim FSM state accessors (pending chord, count, mode, macros) ──────
//
// The FSM in this crate reads and writes VimState through these. They are
// pure vim state, so they belong here rather than on the mode-agnostic
// engine core (#267).
/// Return a clone of the current pending chord state.
fn pending(&self) -> crate::vim::Pending;
/// Overwrite the pending chord state.
fn set_pending(&mut self, p: crate::vim::Pending);
/// Atomically take the pending chord, replacing it with `Pending::None`.
fn take_pending(&mut self) -> crate::vim::Pending;
/// Return the raw digit-prefix count (`0` = no prefix typed yet).
fn count(&self) -> usize;
/// Overwrite the digit-prefix count directly. Clamped at
/// [`crate::vim::MAX_COUNT`] (vim's documented count ceiling, `:h count`).
fn set_count(&mut self, c: usize);
/// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
fn accumulate_count_digit(&mut self, digit: usize);
/// Reset the count prefix to zero (no pending count).
fn reset_count(&mut self);
/// Consume the count and return it; resets to zero. Returns `1` when no
/// prefix was typed (mirrors `take_count` in vim.rs).
fn take_count(&mut self) -> usize;
/// Return the FSM-internal mode (Normal / Insert / Visual / …).
fn fsm_mode(&self) -> crate::vim::Mode;
/// Overwrite the FSM-internal mode without side-effects. Prefer the
/// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
fn set_fsm_mode(&mut self, m: crate::vim::Mode);
/// `true` while the `.` dot-repeat replay is running.
fn is_replaying(&self) -> bool;
/// Set or clear the dot-replay flag.
fn set_replaying(&mut self, v: bool);
/// `true` when we entered Normal from Insert via `Ctrl-o` and will return
/// to Insert after the next complete command.
fn is_one_shot_normal(&self) -> bool;
/// Set or clear the Ctrl-o one-shot-normal flag.
fn set_one_shot_normal(&mut self, v: bool);
/// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
/// `None` before any find command was executed.
fn last_find(&self) -> Option<(char, bool, bool)>;
/// Overwrite the stored last-find target.
fn set_last_find(&mut self, target: Option<(char, bool, bool)>);
/// Perform a vim-sneak style two-char digraph jump. Scans the buffer
/// from the current cursor for the `count`-th occurrence of `c1+c2`.
/// `forward=true` searches ahead; `forward=false` searches backward.
/// Respects `Settings::motion_sneak` — callers (hjkl-vim FSM) should
/// already gate on the setting; this method always executes the sneak.
fn sneak(&mut self, c1: char, c2: char, forward: bool, count: usize);
/// Apply an operator over a sneak digraph range. Charwise exclusive —
/// deletes from cursor up to (not including) the first char of the match.
fn apply_op_sneak(
&mut self,
op: crate::vim::Operator,
c1: char,
c2: char,
forward: bool,
total_count: usize,
);
/// Return the last sneak digraph and direction stored after a sneak motion.
/// `Some(((c1, c2), forward))` when a sneak has been performed this session;
/// `None` before any sneak. Used by `;`/`,` repeat and tests.
fn last_sneak(&self) -> Option<((char, char), bool)>;
/// Return a clone of the last recorded mutating change, or `None` before
/// any change has been made.
fn last_change(&self) -> Option<crate::vim::LastChange>;
/// Overwrite the stored last-change record.
fn set_last_change(&mut self, lc: Option<crate::vim::LastChange>);
/// Borrow the last-change record mutably (e.g. to fill in an `inserted`
/// field after the insert session completes).
fn last_change_mut(&mut self) -> Option<&mut crate::vim::LastChange>;
/// Borrow the active insert session, or `None` when not in Insert mode.
fn insert_session(&self) -> Option<&crate::vim::InsertSession>;
/// Borrow the active insert session mutably.
fn insert_session_mut(&mut self) -> Option<&mut crate::vim::InsertSession>;
/// Atomically take the insert session out, leaving `None`.
fn take_insert_session(&mut self) -> Option<crate::vim::InsertSession>;
/// Install a new insert session, replacing any existing one.
fn set_insert_session(&mut self, s: Option<crate::vim::InsertSession>);
// ─── Register selection / chord status / macro controller ──────────────
/// Return the user's pending register selection (set via `"<reg>` chord
/// before an operator). `None` if no register was selected — caller should
/// use the unnamed register `"`.
///
/// Read-only — does not consume / clear the pending selection. The
/// register is cleared by the engine after the next operator fires.
///
/// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
/// honor `"a` + visual op chord sequences.
fn pending_register(&self) -> Option<char>;
/// True when the user's pending register selector is `+` or `*`.
/// the host peeks this so it can refresh `sync_clipboard_register`
/// only when a clipboard read is actually about to happen.
fn pending_register_is_clipboard(&self) -> bool;
/// Register currently being recorded into via `q{reg}`. `None` when
/// no recording is active. Hosts use this to surface a "recording @r"
/// indicator in the status line.
fn recording_register(&self) -> Option<char>;
/// Pending repeat count the user has typed but not yet resolved
/// (e.g. pressing `5` before `d`). `None` when nothing is pending.
/// Hosts surface this in a "showcmd" area.
fn pending_count(&self) -> Option<u32>;
/// The operator character for any in-flight operator that is waiting
/// for a motion (e.g. `d` after the user types `d` but before a
/// motion). Returns `None` when no operator is pending.
fn pending_op(&self) -> Option<char>;
/// `true` when the engine is in any pending chord state — waiting for
/// the next key to complete a command (e.g. `r<char>` replace,
/// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
/// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
/// register selection `"<reg>`, macro recording target, etc).
///
/// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
/// and forward keys directly to the engine so in-flight commands can
/// complete without the host eating their continuation keys.
fn is_chord_pending(&self) -> bool;
/// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
/// is waiting for the next typed character to name the register to paste.
/// The dispatcher should call `insert_paste_register(c)` instead of
/// `insert_char(c)` for the next printable key, then the flag auto-clears.
///
/// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
/// without having to drive the full FSM.
fn is_insert_register_pending(&self) -> bool;
/// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
/// before `insert_paste_register(c)` in app-level dispatchers so that the
/// flag does not persist into the next key. Call before
/// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
///
/// Phase 6.5: used by `dispatch_insert_key` in the app crate.
fn clear_insert_register_pending(&mut self);
/// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
/// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
/// chars are silently ignored (no-op), matching the engine FSM's
/// `handle_select_register` behaviour.
///
/// Promoted to the public surface in 0.5.17 so the hjkl-vim
/// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
/// without re-entering the engine FSM. `handle_select_register` (engine FSM
/// path for macro-replay / defensive coverage) delegates here to avoid
/// logic duplication.
fn set_pending_register(&mut self, reg: char);
/// Begin recording keystrokes into register `reg`. The caller (app) is
/// responsible for stopping the recording via `stop_macro_record` when the
/// user presses bare `q`.
///
/// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
/// recording by pre-seeding `recording_keys` with the decoded text of the
/// matching lowercase register, matching vim's capital-register append
/// semantics.
/// - Lowercase `reg` clears `recording_keys` (fresh recording).
/// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
///
/// Promoted to the public surface in Phase 5b so the app's
/// `route_chord_key` can start a recording without re-entering the engine
/// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
/// defensive coverage) continues to use the same logic via delegation.
fn start_macro_record(&mut self, reg: char);
/// Finalize the active recording: encode `recording_keys` as text and write
/// to the matching (lowercase) named register. Clears both `recording_macro`
/// and `recording_keys`. No-ops if no recording is active.
///
/// Promoted to the public surface in Phase 5b so the app's `QChord` action
/// can stop a recording when the user presses bare `q` without re-entering
/// the engine FSM.
fn stop_macro_record(&mut self);
/// Returns `true` while a `q{reg}` recording is in progress.
/// Hosts use this to show a "recording @r" status indicator and to decide
/// whether bare `q` should stop the recording or open the `RecordMacroTarget`
/// chord.
fn is_recording_macro(&self) -> bool;
/// Returns `true` while a macro is being replayed. The app sets this flag
/// (via `play_macro`) and clears it (via `end_macro_replay`) around the
/// re-feed loop so the recorder hook can skip double-capture.
fn is_replaying_macro(&self) -> bool;
/// Decode the named register `reg` into a `Vec<hjkl_engine::input::Input>` and
/// prepare for replay, returning ONE iteration of the inputs the app
/// should re-feed through `route_chord_key`.
///
/// Count semantics live in the HOST: `3@a` replays the returned keys
/// three times by looping (or re-splicing a work queue), never by
/// materializing `keys × count` up front — an unclamped `999999999@a`
/// would otherwise allocate multi-GB before the first key plays
/// (audit R2).
///
/// Resolves `reg`:
/// - `'@'` → use `vim.last_macro`; returns empty vec if none.
/// - Any other char → lowercase it, read the register, decode.
///
/// Side-effects:
/// - Sets `vim.last_macro` to the resolved register.
/// - Sets `vim.replaying_macro = true` so the recorder hook skips during
/// replay. The app calls `end_macro_replay` after the loop finishes.
///
/// Returns an empty vec (and no side-effects for `'@'`) if the register is
/// unset or empty.
fn play_macro(&mut self, reg: char) -> Vec<hjkl_engine::input::Input>;
/// Clear the `replaying_macro` flag. Called by the app after the
/// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
fn end_macro_replay(&mut self);
/// Append `input` to the active recording (`recording_keys`) if and only
/// if a recording is in progress AND we are not currently replaying.
/// Called by the app's `route_chord_key` recorder hook so that user
/// keystrokes captured through the app-level chord path are recorded
/// (rather than relying solely on the engine FSM's in-step hook).
fn record_input(&mut self, input: hjkl_engine::input::Input);
// ─── Mode reset / mouse-driven selection / operator range probe ────────
/// Force back to Normal mode (used when dismissing completions etc.).
fn force_normal(&mut self);
/// Handle a left-button click at doc-space `(row, col)`. Exits Visual mode
/// if active, breaks the insert-mode undo group (vim parity for
/// `undo_break_on_motion`), then moves the cursor. The EOL clamp is
/// mode-aware (neovim parity): Normal/Visual cap at `len - 1`, Insert
/// allows the one-past-EOL position.
fn mouse_click_doc(&mut self, row: usize, col: usize);
/// Begin a mouse-drag selection: anchor at the cursor and enter
/// Visual-char mode. Idempotent if already in Visual-char.
fn mouse_begin_drag(&mut self);
/// Dry-run `motion_key` and return the `(min_row, max_row)` span between
/// the cursor row and the motion's target row, restoring the cursor
/// afterwards. `None` when `motion_key` is not a known motion.
fn range_for_op_motion(
&mut self,
motion_key: char,
total_count: usize,
) -> Option<(usize, usize)>;
// ─── Motion dispatch / operator range probes ───────────────────────────
/// Execute a named cursor motion `kind`, repeated `count` times. Maps the
/// keymap-layer `MotionKind` onto the vim motion primitives, bypassing the
/// FSM. Identical cursor semantics to the FSM path — sticky column, scroll
/// sync and big-jump tracking all apply.
fn apply_motion(&mut self, kind: MotionKind, count: usize);
/// Dry-run a `g`-prefixed motion and return `(min_row, max_row)` — for
/// `=gg` / `=gj` etc. `None` for unknown `ch`. The cursor is restored.
fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)>;
/// Dry-run a text object and return `(min_row, max_row)` — for `=iw` /
/// `=ap` etc. `None` for unknown `ch`.
fn range_for_op_text_obj(
&self,
ch: char,
inner: bool,
total_count: usize,
) -> Option<(usize, usize)>;
}
impl<H: Host> VimEditorExt for Editor<hjkl_buffer::View, H> {
fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
let (ar, ac) = crate::vim_state::vim(self).block_anchor;
let (cr, _) = self.cursor();
let cc = crate::vim_state::vim(self).block_vcol;
(ar.min(cr), ar.max(cr), ac.min(cc), ac.max(cc))
}
fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
if self.vim_mode() != VimMode::VisualBlock {
return None;
}
let (ar, ac) = crate::vim_state::vim(self).block_anchor;
let cr = self.cursor().0;
let cc = crate::vim_state::vim(self).block_vcol;
Some((ar.min(cr), ar.max(cr), ac.min(cc), ac.max(cc)))
}
fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
if self.vim_mode() != VimMode::Visual {
return None;
}
let anchor = crate::vim_state::vim(self).visual_anchor;
let cursor = self.cursor();
let (start, end) = if anchor <= cursor {
(anchor, cursor)
} else {
(cursor, anchor)
};
if self.settings().selection_exclusive {
// Half-open: start..end (end excluded). Empty when start == end.
if start == end {
return None;
}
Some((start, end))
} else {
// Inclusive (vim default): both endpoints are selected.
Some((start, end))
}
}
fn visual_char_range_exclusive(&self) -> Option<((usize, usize), (usize, usize))> {
if self.vim_mode() != VimMode::Visual {
return None;
}
let anchor = crate::vim_state::vim(self).visual_anchor;
let cursor = self.cursor();
if anchor == cursor {
return None;
}
let (start, end) = if anchor <= cursor {
(anchor, cursor)
} else {
(cursor, anchor)
};
Some((start, end))
}
fn line_highlight(&self) -> Option<(usize, usize)> {
if self.vim_mode() != VimMode::VisualLine {
return None;
}
let anchor = crate::vim_state::vim(self).visual_line_anchor;
let cursor = self.cursor().0;
Some((anchor.min(cursor), anchor.max(cursor)))
}
fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
use hjkl_buffer::{Position, Selection};
let (cr, cc) = self.cursor();
match self.vim_mode() {
VimMode::Visual => {
let (ar, ac) = crate::vim_state::vim(self).visual_anchor;
let head = Position::new(cr, cc);
if self.settings().selection_exclusive {
// Exclusive (VSCode bar-caret): render the half-open char set
// [start, end) so the cell under the caret is NOT highlighted.
// The buffer-tui renderer paints `row_span` inclusively, so
// drop one char off the max end. Empty selection → no
// highlight (caller is effectively in Insert).
let anchor_pos = Position::new(ar, ac);
if anchor_pos == head {
return None;
}
let (start, end) = if (ar, ac) <= (head.row, head.col) {
(anchor_pos, head)
} else {
(head, anchor_pos)
};
return Some(Selection::Char {
anchor: start,
head: dec_pos_one_char(self, end),
});
}
Some(Selection::Char {
anchor: Position::new(ar, ac),
head,
})
}
VimMode::VisualLine => Some(Selection::Line {
anchor_row: crate::vim_state::vim(self).visual_line_anchor,
head_row: cr,
}),
VimMode::VisualBlock => {
let (ar, ac) = crate::vim_state::vim(self).block_anchor;
let vcol = crate::vim_state::vim(self).block_vcol;
if crate::vim_state::vim(self).block_to_eol {
// Ragged (`$` — `:h v_b_$`): `Selection::Block::row_span`
// only ever resolves one fixed `(left, right)` pair for
// every row. Reuse the SAME `usize::MAX` "cap at the
// row's actual length" convention `Selection::Line`
// already uses (see `hjkl_buffer::selection::RowSpan`)
// by forcing the right corner's col to `usize::MAX` —
// the renderer then extends every row to its own EOL.
// Normalise which corner carries `MAX` so it's always
// the "right" one regardless of anchor/cursor order.
let left = ac.min(vcol);
return Some(Selection::Block {
anchor: Position::new(ar, left),
head: Position::new(cr, usize::MAX),
});
}
Some(Selection::Block {
anchor: Position::new(ar, ac),
head: Position::new(cr, vcol),
})
}
_ => None,
}
}
fn selection_highlight(&self) -> Option<Highlight> {
let sel = self.buffer_selection()?;
let (start, end) = match sel {
hjkl_buffer::Selection::Char { anchor, head } => {
let a = (anchor.row, anchor.col);
let h = (head.row, head.col);
if a <= h { (a, h) } else { (h, a) }
}
hjkl_buffer::Selection::Line {
anchor_row,
head_row,
} => {
let (top, bot) = if anchor_row <= head_row {
(anchor_row, head_row)
} else {
(head_row, anchor_row)
};
let last_col = self.line(bot).map_or(0, |l| l.len());
((top, 0), (bot, last_col))
}
hjkl_buffer::Selection::Block { anchor, head } => {
let (top, bot) = if anchor.row <= head.row {
(anchor.row, head.row)
} else {
(head.row, anchor.row)
};
let (left, right) = if anchor.col <= head.col {
(anchor.col, head.col)
} else {
(head.col, anchor.col)
};
((top, left), (bot, right))
}
};
Some(Highlight {
range: Pos {
line: start.0 as u32,
col: start.1 as u32,
}..Pos {
line: end.0 as u32,
col: end.1 as u32,
},
kind: HighlightKind::Selection,
})
}
// ─── Text-object resolution ────────────────────────────────────────────
fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_word_bridge(self)
}
fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_word_bridge(self)
}
fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_big_word_bridge(self)
}
fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_big_word_bridge(self)
}
fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_quote_bridge(self, quote)
}
fn text_object_around_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_quote_bridge(self, quote)
}
fn text_object_inner_bracket(&self, open: char) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_bracket_bridge(self, open)
}
fn text_object_around_bracket(&self, open: char) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_bracket_bridge(self, open)
}
fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_sentence_bridge(self)
}
fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_sentence_bridge(self)
}
fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_paragraph_bridge(self)
}
fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_paragraph_bridge(self)
}
fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_inner_tag_bridge(self)
}
fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
crate::vim::text_object_around_tag_bridge(self)
}
// ─── Range-mutation primitives ─────────────────────────────────────────
fn delete_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
) {
crate::vim::delete_range_bridge(self, start, end, kind, register);
}
fn yank_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
) {
crate::vim::yank_range_bridge(self, start, end, kind, register);
}
fn change_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
register: char,
) {
crate::vim::change_range_bridge(self, start, end, kind, register);
}
fn indent_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
count: i32,
shiftwidth: u32,
) {
crate::vim::indent_range_bridge(self, start, end, count, shiftwidth);
}
fn case_range(
&mut self,
start: (usize, usize),
end: (usize, usize),
kind: RangeKind,
op: Operator,
) {
crate::vim::case_range_bridge(self, start, end, kind, op);
}
// ─── Block-shape range-mutation primitives ─────────────────────────────
fn delete_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
) {
crate::vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
}
fn yank_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
) {
crate::vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
}
fn change_block(
&mut self,
top_row: usize,
bot_row: usize,
left_col: usize,
right_col: usize,
register: char,
) {
crate::vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
}
fn indent_block(
&mut self,
top_row: usize,
bot_row: usize,
_left_col: usize,
_right_col: usize,
count: i32,
) {
crate::vim::indent_block_bridge(self, top_row, bot_row, count);
}
fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
crate::vim::auto_indent_range_bridge(self, start, end);
}
// ─── Paste ─────────────────────────────────────────────────────────────
fn paste_after(&mut self, count: usize) {
crate::vim::paste_after_bridge(self, count);
}
fn paste_before(&mut self, count: usize) {
crate::vim::paste_before_bridge(self, count);
}
fn paste_cursor_after(&mut self, before: bool, count: usize) {
crate::vim::paste_bridge(self, before, count, true, false);
}
fn paste_reindent(&mut self, before: bool, count: usize) {
crate::vim::paste_bridge(self, before, count, false, true);
}
fn visual_paste(&mut self, before: bool) {
crate::vim::visual_paste(self, before);
}
// ─── Visual-mode operators ─────────────────────────────────────────────
fn adjust_number_visual(&mut self, delta: i64, sequential: bool) {
crate::vim::adjust_number_visual(self, delta, sequential);
}
fn ampersand_repeat(&mut self) {
crate::vim::ampersand_repeat(self);
}
fn visual_join(&mut self, with_space: bool) {
crate::vim::visual_join(self, with_space);
}
fn goto_percent(&mut self, count: usize) {
crate::vim::goto_percent(self, count);
}
// ─── Jumplist motion ───────────────────────────────────────────────────
fn jump_back(&mut self, count: usize) {
crate::vim::jump_back_bridge(self, count);
}
fn jump_forward(&mut self, count: usize) {
crate::vim::jump_forward_bridge(self, count);
}
// ─── Search ────────────────────────────────────────────────────────────
fn search_repeat(&mut self, forward: bool, count: usize) {
crate::vim::search_repeat_bridge(self, forward, count);
}
fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
crate::vim::word_search_bridge(self, forward, whole_word, count);
}
// ─── Chord appliers ────────────────────────────────────────────────────
fn replace_char_at(&mut self, ch: char, count: usize) {
crate::vim::replace_char(self, ch, count);
}
fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
crate::vim::apply_find_char(self, ch, forward, till, count.max(1));
}
fn after_g(&mut self, ch: char, count: usize) {
crate::vim::apply_after_g(self, ch, count);
}
fn after_z(&mut self, ch: char, count: usize) {
crate::vim::apply_after_z(self, ch, count);
}
// ─── Operator dispatch ─────────────────────────────────────────────────
fn apply_op_motion(&mut self, op: Operator, motion_key: char, total_count: usize) {
crate::vim::apply_op_motion_key(self, op, motion_key, total_count);
}
fn apply_op_double(&mut self, op: Operator, total_count: usize) {
crate::vim::apply_op_double(self, op, total_count);
}
fn apply_op_find(&mut self, op: Operator, ch: char, forward: bool, till: bool, count: usize) {
crate::vim::apply_op_find_motion(self, op, ch, forward, till, count);
}
fn apply_op_text_obj(&mut self, op: Operator, ch: char, inner: bool, total_count: usize) {
crate::vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
}
fn apply_op_g(&mut self, op: Operator, ch: char, total_count: usize) {
crate::vim::apply_op_g_inner(self, op, ch, total_count);
}
// ─── Mode transitions ──────────────────────────────────────────────────
fn vim_mode(&self) -> VimMode {
crate::vim_state::vim(self).current_mode
}
fn enter_visual_char(&mut self) {
crate::vim::enter_visual_char_bridge(self);
}
fn enter_visual_line(&mut self) {
crate::vim::enter_visual_line_bridge(self);
}
fn enter_visual_block(&mut self) {
crate::vim::enter_visual_block_bridge(self);
}
fn exit_visual_to_normal(&mut self) {
crate::vim::exit_visual_to_normal_bridge(self);
}
fn visual_o_toggle(&mut self) {
crate::vim::visual_o_toggle_bridge(self);
}
fn reenter_last_visual(&mut self) {
crate::vim::reenter_last_visual_bridge(self);
}
fn set_mode(&mut self, mode: VimMode) {
crate::vim::set_mode_bridge(self, mode);
}
// ─── Visual anchors ────────────────────────────────────────────────────
fn visual_anchor(&self) -> (usize, usize) {
crate::vim_state::vim(self).visual_anchor
}
fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
crate::vim_state::vim_mut(self).visual_anchor = anchor;
}
fn visual_line_anchor(&self) -> usize {
crate::vim_state::vim(self).visual_line_anchor
}
fn set_visual_line_anchor(&mut self, row: usize) {
crate::vim_state::vim_mut(self).visual_line_anchor = row;
}
fn block_anchor(&self) -> (usize, usize) {
crate::vim_state::vim(self).block_anchor
}
fn set_block_anchor(&mut self, anchor: (usize, usize)) {
crate::vim_state::vim_mut(self).block_anchor = anchor;
}
fn block_vcol(&self) -> usize {
crate::vim_state::vim(self).block_vcol
}
fn set_block_vcol(&mut self, vcol: usize) {
crate::vim_state::vim_mut(self).block_vcol = vcol;
}
fn block_to_eol(&self) -> bool {
crate::vim_state::vim(self).block_to_eol
}
fn set_block_to_eol(&mut self, to_eol: bool) {
crate::vim_state::vim_mut(self).block_to_eol = to_eol;
}
// ─── Yank / register staging ───────────────────────────────────────────
fn set_pending_register_raw(&mut self, reg: Option<char>) {
crate::vim_state::vim_mut(self).pending_register = reg;
}
fn take_pending_register_raw(&mut self) -> Option<char> {
crate::vim_state::vim_mut(self).pending_register.take()
}
// ─── Macro recording / replay ──────────────────────────────────────────
fn recording_macro(&self) -> Option<char> {
crate::vim_state::vim(self).recording_macro
}
fn set_recording_macro(&mut self, reg: Option<char>) {
crate::vim_state::vim_mut(self).recording_macro = reg;
}
fn push_recording_key(&mut self, input: Input) {
crate::vim_state::vim_mut(self).recording_keys.push(input);
}
fn take_recording_keys(&mut self) -> Vec<Input> {
std::mem::take(&mut crate::vim_state::vim_mut(self).recording_keys)
}
fn set_recording_keys(&mut self, keys: Vec<Input>) {
crate::vim_state::vim_mut(self).recording_keys = keys;
}
fn recording_keys_len(&self) -> usize {
crate::vim_state::vim(self).recording_keys.len()
}
fn is_replaying_macro_raw(&self) -> bool {
crate::vim_state::vim(self).replaying_macro
}
fn set_replaying_macro_raw(&mut self, v: bool) {
crate::vim_state::vim_mut(self).replaying_macro = v;
}
fn last_macro(&self) -> Option<char> {
crate::vim_state::vim(self).last_macro
}
fn set_last_macro(&mut self, reg: Option<char>) {
crate::vim_state::vim_mut(self).last_macro = reg;
}
// ─── Last insert / visual / viewport ───────────────────────────────────
fn last_insert_pos(&self) -> Option<(usize, usize)> {
crate::vim_state::vim(self).last_insert_pos
}
fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
crate::vim_state::vim_mut(self).last_insert_pos = pos;
}
fn last_visual(&self) -> Option<LastVisual> {
crate::vim_state::vim(self).last_visual
}
fn set_last_visual(&mut self, snap: Option<LastVisual>) {
crate::vim_state::vim_mut(self).last_visual = snap;
}
fn insert_pending_register(&self) -> bool {
crate::vim_state::vim(self).insert_pending_register
}
fn set_insert_pending_register(&mut self, v: bool) {
crate::vim_state::vim_mut(self).insert_pending_register = v;
}
// ─── Change-mark start ─────────────────────────────────────────────────
fn change_mark_start(&self) -> Option<(usize, usize)> {
crate::vim_state::vim(self).change_mark_start
}
fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
crate::vim_state::vim_mut(self).change_mark_start.take()
}
fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
crate::vim_state::vim_mut(self).change_mark_start = pos;
}
// ─── Visual / motion / search primitives ───────────────────────────────
fn is_visual(&self) -> bool {
matches!(
crate::vim_state::vim(self).mode,
FsmMode::Visual | FsmMode::VisualLine | FsmMode::VisualBlock
)
}
fn apply_op_with_motion_direct(&mut self, op: Operator, motion: &Motion, count: usize) {
crate::vim::apply_op_with_motion(self, op, motion, count);
}
fn adjust_number(&mut self, delta: i64) {
crate::vim::adjust_number(self, delta);
}
fn enter_search(&mut self, forward: bool) {
crate::vim::enter_search(self, forward);
}
fn enter_search_op(&mut self, forward: bool, op: Operator, count: usize) {
crate::vim::enter_search_op(self, forward, op, count);
}
fn apply_op_search_range(&mut self, op: Operator, origin: (usize, usize)) {
crate::vim::apply_op_search_range(self, op, origin);
}
fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize, count: usize) {
self.jump_cursor(top, col);
crate::vim_state::vim_mut(self).mode = FsmMode::Normal;
crate::vim::begin_insert(
self,
count,
InsertReason::BlockEdge {
top,
bot,
col,
pad: false,
// `I`'s insertion point already IS the block's left edge —
// but `leave_insert_to_normal_bridge` unconditionally steps
// the cursor back one column after Esc (vim's generic
// leave-insert adjustment), so store one PAST the target
// and let that step-back land exactly on `col`. Verified
// against real nvim at a non-zero left edge (the only
// existing case started at col 0, where the step-back's
// `col > 0` guard happened to no-op and masked this).
cursor_col: col + 1,
},
);
}
fn visual_block_append_at_right(
&mut self,
top: usize,
bot: usize,
col: usize,
left: usize,
count: usize,
) {
// vim `v_b_A`: pad the top row to `col` with spaces before the
// cursor lands there, same as `replicate_block_text` does for
// every other row on Esc. Without this, `jump_cursor` clamps
// `col` down to the row's current length and the typed text
// lands inside the block instead of past its right edge.
//
// The pad must land in the SAME undo group as the insert session
// (vim's block `A` is one `u` step — pad, typed text, and the
// replicated rows all revert together). So push the undo
// checkpoint ourselves before padding, then use
// `begin_insert_noundo` — mirrors the `Operator::Change` block
// path (push_undo, mutate, begin_insert_noundo) in `visual_ops`.
self.push_undo();
let line_len = hjkl_engine::buf_helpers::buf_line_chars(self.buffer(), top);
if col > line_len {
let pad: String = std::iter::repeat_n(' ', col - line_len).collect();
self.mutate_edit(hjkl_buffer::Edit::InsertStr {
at: hjkl_buffer::Position::new(top, line_len),
text: pad,
});
}
self.jump_cursor(top, col);
crate::vim_state::vim_mut(self).mode = FsmMode::Normal;
crate::vim::begin_insert_noundo(
self,
count,
InsertReason::BlockEdge {
top,
bot,
col,
pad: true,
// Same "one past the target, let the generic Esc step-back
// land exactly there" convention as `visual_block_insert_
// at_left` — see its comment.
cursor_col: left + 1,
},
);
}
fn execute_motion(&mut self, motion: Motion, count: usize) {
crate::vim::execute_motion(self, motion, count);
}
fn update_block_vcol(&mut self, motion: &Motion) {
crate::vim::update_block_vcol(self, motion);
}
fn apply_visual_operator(&mut self, op: Operator, count: usize) {
crate::vim::apply_visual_operator(self, op, count);
}
fn replace_block_char(&mut self, ch: char) {
crate::vim::block_replace(self, ch);
}
fn visual_replace_char(&mut self, ch: char) {
crate::vim::visual_replace_char(self, ch);
}
fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
let obj = match ch {
'w' => TextObject::Word { big: false },
'W' => TextObject::Word { big: true },
'"' | '\'' | '`' => TextObject::Quote(ch),
'(' | ')' | 'b' => TextObject::Bracket('('),
'[' | ']' => TextObject::Bracket('['),
'{' | '}' | 'B' => TextObject::Bracket('{'),
'<' | '>' => TextObject::Bracket('<'),
'p' => TextObject::Paragraph,
't' => TextObject::XmlTag,
's' => TextObject::Sentence,
_ => return,
};
let Some((start, end, kind)) = crate::vim::text_object_range(self, obj, inner, 1) else {
return;
};
// B6: `:h v_ip` — when the selection ALREADY exactly equals this
// text object's natural bounds (the user is re-applying `ip`/`ap`/
// etc. to a selection it already produced, e.g. `vipip`), the
// object GROWS instead of re-selecting the identical (so
// no-op-looking) range. `ip`/`ap` alternate paragraph and
// blank-run units this way; growth is implemented generically here
// by probing the SAME text object one row past the current end and
// unioning the two ranges, rather than hand-rolling paragraph-
// specific alternation logic.
// Compare the SELECTION'S END (which the cursor always tracks, by
// this function's own construction below) against the freshly
// computed object's end — NOT the anchor. After a grow, the anchor
// stays pinned at the FIRST application's start while the end keeps
// moving, so an anchor-based check would only ever match once
// (verified against real nvim: `vipipipd`, three applications,
// grows a second time too — the anchor-based check breaks that).
let already_matches = match kind {
RangeKind::Linewise => {
crate::vim_state::vim(self).mode == FsmMode::VisualLine && self.cursor().0 == end.0
}
_ => {
crate::vim_state::vim(self).mode == FsmMode::Visual
&& self.cursor() == crate::vim::retreat_one(self, end)
}
};
// When growing, keep the EXISTING anchor (the first application's
// start) — `start` above is the freshly computed single-object's
// start (e.g. the blank run's own start on a second `ip`), which is
// NOT where the accumulated selection began.
//
// The probe position differs by kind: Linewise units are probed one
// ROW past the current end (`ip`/`ap` grow onto the next line);
// charwise units (`iw`, quotes, brackets, …) are probed at `end`
// directly — `end` for an Exclusive object already points ONE PAST
// the last selected char, i.e. exactly where the next same-line unit
// begins (verified against real nvim: `viwiw` on "foo bar baz"
// grows "foo" to "foo " — the following WHITESPACE run on the SAME
// row, not a jump to the next line).
let (start, end) = if already_matches {
let existing_start = match kind {
RangeKind::Linewise => (crate::vim_state::vim(self).visual_line_anchor, 0),
_ => crate::vim_state::vim(self).visual_anchor,
};
let probe = match kind {
RangeKind::Linewise => (end.0 + 1, 0),
_ => end,
};
let saved_cursor = self.cursor();
self.jump_cursor(probe.0, probe.1);
let grown = crate::vim::text_object_range(self, obj, inner, 1)
.filter(|&(_, _, grown_kind)| grown_kind == kind)
.map(|(_, grown_end, _)| grown_end);
self.jump_cursor(saved_cursor.0, saved_cursor.1);
match grown {
Some(grown_end) if grown_end > end => (existing_start, grown_end),
_ => (existing_start, end),
}
} else {
(start, end)
};
// vim switches from visual-block to the text-object's native mode
// (charwise for iw/aw/iquote/ibracket, linewise for ip/ap). The
// block anchor is meaningless for the resulting charwise/linewise
// selection — replace it with the text object's computed start.
if crate::vim_state::vim(self).mode == FsmMode::VisualBlock {
match kind {
RangeKind::Linewise => {
crate::vim_state::vim_mut(self).visual_line_anchor = start.0;
crate::vim_state::vim_mut(self).mode = FsmMode::VisualLine;
crate::vim_state::vim_mut(self).current_mode = VimMode::VisualLine;
self.jump_cursor(end.0, 0);
}
_ => {
crate::vim_state::vim_mut(self).mode = FsmMode::Visual;
crate::vim_state::vim_mut(self).current_mode = VimMode::Visual;
crate::vim_state::vim_mut(self).visual_anchor = (start.0, start.1);
let (er, ec) = crate::vim::retreat_one(self, end);
self.jump_cursor(er, ec);
}
}
return;
}
match kind {
RangeKind::Linewise => {
crate::vim_state::vim_mut(self).visual_line_anchor = start.0;
crate::vim_state::vim_mut(self).mode = FsmMode::VisualLine;
crate::vim_state::vim_mut(self).current_mode = VimMode::VisualLine;
self.jump_cursor(end.0, 0);
}
_ => {
crate::vim_state::vim_mut(self).mode = FsmMode::Visual;
crate::vim_state::vim_mut(self).current_mode = VimMode::Visual;
crate::vim_state::vim_mut(self).visual_anchor = (start.0, start.1);
let (er, ec) = crate::vim::retreat_one(self, end);
self.jump_cursor(er, ec);
}
}
}
// ─── Insert-mode primitives ────────────────────────────────────────────
fn insert_char(&mut self, ch: char) {
if crate::vim::insert_char_bridge(self, ch) {
after_insert_mutation(self);
}
}
fn insert_newline(&mut self) {
if crate::vim::insert_newline_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_tab(&mut self) {
if crate::vim::insert_tab_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_backspace(&mut self) {
if crate::vim::insert_backspace_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_delete(&mut self) {
if crate::vim::insert_delete_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_arrow(&mut self, dir: InsertDir) {
crate::vim::insert_arrow_bridge(self, dir);
after_insert_motion(self);
}
fn insert_home(&mut self) {
crate::vim::insert_home_bridge(self);
after_insert_motion(self);
}
fn insert_end(&mut self) {
crate::vim::insert_end_bridge(self);
after_insert_motion(self);
}
fn insert_pageup(&mut self, viewport_h: u16) {
crate::vim::insert_pageup_bridge(self, viewport_h);
after_insert_motion(self);
}
fn insert_pagedown(&mut self, viewport_h: u16) {
crate::vim::insert_pagedown_bridge(self, viewport_h);
after_insert_motion(self);
}
fn insert_ctrl_w(&mut self) {
if crate::vim::insert_ctrl_w_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_ctrl_u(&mut self) {
if crate::vim::insert_ctrl_u_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_ctrl_h(&mut self) {
if crate::vim::insert_ctrl_h_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_ctrl_o_arm(&mut self) {
crate::vim::insert_ctrl_o_bridge(self);
}
fn insert_ctrl_r_arm(&mut self) {
crate::vim::insert_ctrl_r_bridge(self);
}
fn insert_ctrl_t(&mut self) {
// Indent-only: no scrolloff re-check (the cursor row does not move).
let mutated = crate::vim::insert_ctrl_t_bridge(self);
if mutated {
self.mark_content_dirty();
let (row, _) = self.cursor();
crate::vim_state::vim_mut(self).widen_insert_row(row);
}
}
fn insert_ctrl_d(&mut self) {
let mutated = crate::vim::insert_ctrl_d_bridge(self);
if mutated {
self.mark_content_dirty();
let (row, _) = self.cursor();
crate::vim_state::vim_mut(self).widen_insert_row(row);
}
}
fn insert_ctrl_a(&mut self) {
if crate::vim::insert_ctrl_a_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_ctrl_e(&mut self) {
if crate::vim::insert_ctrl_e_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_ctrl_y(&mut self) {
if crate::vim::insert_ctrl_y_bridge(self) {
after_insert_mutation(self);
}
}
fn insert_paste_register(&mut self, reg: char) {
crate::vim::insert_paste_register_bridge(self, reg);
let (row, _) = self.cursor();
crate::vim_state::vim_mut(self).widen_insert_row(row);
}
fn insert_ctrl_bracket(&mut self) {
if crate::vim::check_and_apply_abbrev(self, AbbrevTrigger::CtrlBracket) {
after_insert_mutation(self);
}
}
fn leave_insert_to_normal(&mut self) {
crate::vim::leave_insert_to_normal_bridge(self);
}
// ─── Insert-mode entry ─────────────────────────────────────────────────
fn enter_insert_i(&mut self, count: usize) {
crate::vim::enter_insert_i_bridge(self, count);
}
fn enter_insert_shift_i(&mut self, count: usize) {
crate::vim::enter_insert_shift_i_bridge(self, count);
}
fn enter_insert_a(&mut self, count: usize) {
crate::vim::enter_insert_a_bridge(self, count);
}
fn enter_insert_shift_a(&mut self, count: usize) {
crate::vim::enter_insert_shift_a_bridge(self, count);
}
fn open_line_below(&mut self, count: usize) {
crate::vim::open_line_below_bridge(self, count);
}
fn open_line_above(&mut self, count: usize) {
crate::vim::open_line_above_bridge(self, count);
}
fn enter_replace_mode(&mut self, count: usize) {
crate::vim::enter_replace_mode_bridge(self, count);
}
// ─── Normal-mode edit primitives ───────────────────────────────────────
fn delete_char_forward(&mut self, count: usize) {
crate::vim::delete_char_forward_bridge(self, count);
}
fn delete_char_backward(&mut self, count: usize) {
crate::vim::delete_char_backward_bridge(self, count);
}
fn substitute_char(&mut self, count: usize) {
crate::vim::substitute_char_bridge(self, count);
}
fn substitute_line(&mut self, count: usize) {
crate::vim::substitute_line_bridge(self, count);
}
fn delete_to_eol(&mut self, count: usize) {
crate::vim::delete_to_eol_bridge(self, count);
}
fn change_to_eol(&mut self, count: usize) {
crate::vim::change_to_eol_bridge(self, count);
}
fn yank_to_eol(&mut self, count: usize) {
crate::vim::yank_to_eol_bridge(self, count);
}
fn join_line(&mut self, count: usize) {
crate::vim::join_line_bridge(self, count);
}
fn toggle_case_at_cursor(&mut self, count: usize) {
crate::vim::toggle_case_at_cursor_bridge(self, count);
}
// ─── Vim mark commands ─────────────────────────────────────────────────
fn replay_last_change(&mut self, count: usize) {
crate::vim::replay_last_change(self, count);
}
fn set_mark_at_cursor(&mut self, ch: char) {
crate::vim::set_mark_at_cursor(self, ch);
}
fn goto_mark_line(&mut self, ch: char) {
crate::vim::goto_mark(self, ch, true);
}
fn goto_mark_char(&mut self, ch: char) {
crate::vim::goto_mark(self, ch, false);
}
fn try_goto_mark_line(&mut self, ch: char) -> MarkJump {
crate::vim::try_goto_mark(self, ch, true)
}
fn try_goto_mark_char(&mut self, ch: char) -> MarkJump {
crate::vim::try_goto_mark(self, ch, false)
}
// ─── Vim FSM state accessors ───────────────────────────────────────────
fn pending(&self) -> crate::vim::Pending {
crate::vim_state::vim(self).pending.clone()
}
fn set_pending(&mut self, p: crate::vim::Pending) {
crate::vim_state::vim_mut(self).pending = p;
}
fn take_pending(&mut self) -> crate::vim::Pending {
std::mem::take(&mut crate::vim_state::vim_mut(self).pending)
}
fn count(&self) -> usize {
crate::vim_state::vim(self).count
}
fn set_count(&mut self, c: usize) {
crate::vim_state::vim_mut(self).count = c.min(crate::vim::MAX_COUNT);
}
fn accumulate_count_digit(&mut self, digit: usize) {
// Saturate the add too: once the multiply has saturated at
// `usize::MAX`, a plain `+ digit` overflows (panic in debug builds)
// after ~20 typed digits. Then clamp at vim's documented count
// ceiling (`:h count`) so no apply loop can iterate more than
// 999,999,999 times regardless of how many digits were typed.
let v = crate::vim_state::vim_mut(self);
v.count = v
.count
.saturating_mul(10)
.saturating_add(digit)
.min(crate::vim::MAX_COUNT);
}
fn reset_count(&mut self) {
crate::vim_state::vim_mut(self).count = 0;
}
fn take_count(&mut self) -> usize {
if crate::vim_state::vim(self).count > 0 {
let n = crate::vim_state::vim(self).count;
crate::vim_state::vim_mut(self).count = 0;
n
} else {
1
}
}
fn fsm_mode(&self) -> crate::vim::Mode {
crate::vim_state::vim(self).mode
}
fn set_fsm_mode(&mut self, m: crate::vim::Mode) {
crate::vim_state::vim_mut(self).mode = m;
crate::vim_state::vim_mut(self).current_mode =
crate::vim_state::vim_mut(self).public_mode();
}
fn is_replaying(&self) -> bool {
crate::vim_state::vim(self).replaying
}
fn set_replaying(&mut self, v: bool) {
crate::vim_state::vim_mut(self).replaying = v;
}
fn is_one_shot_normal(&self) -> bool {
crate::vim_state::vim(self).one_shot_normal
}
fn set_one_shot_normal(&mut self, v: bool) {
crate::vim_state::vim_mut(self).one_shot_normal = v;
}
fn last_find(&self) -> Option<(char, bool, bool)> {
crate::vim_state::vim(self).last_find
}
fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
crate::vim_state::vim_mut(self).last_find = target;
}
fn sneak(&mut self, c1: char, c2: char, forward: bool, count: usize) {
crate::vim::apply_sneak(self, c1, c2, forward, count.max(1));
}
fn apply_op_sneak(
&mut self,
op: crate::vim::Operator,
c1: char,
c2: char,
forward: bool,
total_count: usize,
) {
crate::vim::apply_op_sneak(self, op, c1, c2, forward, total_count);
}
fn last_sneak(&self) -> Option<((char, char), bool)> {
crate::vim_state::vim(self).last_sneak
}
fn last_change(&self) -> Option<crate::vim::LastChange> {
crate::vim_state::vim(self).last_change.clone()
}
fn set_last_change(&mut self, lc: Option<crate::vim::LastChange>) {
crate::vim_state::vim_mut(self).last_change = lc;
}
fn last_change_mut(&mut self) -> Option<&mut crate::vim::LastChange> {
crate::vim_state::vim_mut(self).last_change.as_mut()
}
fn insert_session(&self) -> Option<&crate::vim::InsertSession> {
crate::vim_state::vim(self).insert_session.as_ref()
}
fn insert_session_mut(&mut self) -> Option<&mut crate::vim::InsertSession> {
crate::vim_state::vim_mut(self).insert_session.as_mut()
}
fn take_insert_session(&mut self) -> Option<crate::vim::InsertSession> {
crate::vim_state::vim_mut(self).insert_session.take()
}
fn set_insert_session(&mut self, s: Option<crate::vim::InsertSession>) {
crate::vim_state::vim_mut(self).insert_session = s;
}
// ─── Register selection / chord status / macro controller ──────────────
fn pending_register(&self) -> Option<char> {
crate::vim_state::vim(self).pending_register
}
fn pending_register_is_clipboard(&self) -> bool {
matches!(
crate::vim_state::vim(self).pending_register,
Some('+') | Some('*')
)
}
fn recording_register(&self) -> Option<char> {
crate::vim_state::vim(self).recording_macro
}
fn pending_count(&self) -> Option<u32> {
crate::vim_state::vim(self).pending_count_val()
}
fn pending_op(&self) -> Option<char> {
crate::vim_state::vim(self).pending_op_char()
}
fn is_chord_pending(&self) -> bool {
crate::vim_state::vim(self).is_chord_pending()
}
fn is_insert_register_pending(&self) -> bool {
crate::vim_state::vim(self).insert_pending_register
}
fn clear_insert_register_pending(&mut self) {
crate::vim_state::vim_mut(self).insert_pending_register = false;
}
fn set_pending_register(&mut self, reg: char) {
// `-` is the small-delete register (readable/pasteable, e.g. `"-p`).
if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_' | '-') {
crate::vim_state::vim_mut(self).pending_register = Some(reg);
}
// Invalid chars silently no-op (matches engine FSM behavior).
}
fn start_macro_record(&mut self, reg: char) {
if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
return;
}
crate::vim_state::vim_mut(self).recording_macro = Some(reg);
if reg.is_ascii_uppercase() {
// Seed recording_keys with the existing lowercase register's text
// decoded back to inputs so capital-register append continues from
// where the previous recording left off.
let lower = reg.to_ascii_lowercase();
let text = self
.with_registers(|r| r.read(lower).map(|s| s.text.clone()))
.unwrap_or_default();
crate::vim_state::vim_mut(self).recording_keys =
hjkl_engine::input::decode_macro(&text);
} else {
crate::vim_state::vim_mut(self).recording_keys.clear();
}
}
fn stop_macro_record(&mut self) {
let Some(reg) = crate::vim_state::vim_mut(self).recording_macro.take() else {
return;
};
let keys = std::mem::take(&mut crate::vim_state::vim_mut(self).recording_keys);
let text = hjkl_engine::input::encode_macro(&keys);
self.set_named_register_text(reg.to_ascii_lowercase(), text);
}
fn is_recording_macro(&self) -> bool {
crate::vim_state::vim(self).recording_macro.is_some()
}
fn is_replaying_macro(&self) -> bool {
crate::vim_state::vim(self).replaying_macro
}
fn play_macro(&mut self, reg: char) -> Vec<hjkl_engine::input::Input> {
let resolved = if reg == '@' {
match crate::vim_state::vim(self).last_macro {
Some(r) => r,
None => return vec![],
}
} else {
reg.to_ascii_lowercase()
};
let text = match self.with_registers(|regs| regs.read(resolved).cloned()) {
Some(slot) if !slot.text.is_empty() => slot.text,
_ => return vec![],
};
let keys = hjkl_engine::input::decode_macro(&text);
crate::vim_state::vim_mut(self).last_macro = Some(resolved);
crate::vim_state::vim_mut(self).replaying_macro = true;
// ONE iteration only — the host loops the count (audit R2). The old
// `keys.repeat(count)` materialized count × keys.len() Inputs up
// front, so `999999999@a` allocated multi-GB before playing a key.
keys
}
fn end_macro_replay(&mut self) {
crate::vim_state::vim_mut(self).replaying_macro = false;
}
fn record_input(&mut self, input: hjkl_engine::input::Input) {
if crate::vim_state::vim(self).recording_macro.is_some()
&& !crate::vim_state::vim(self).replaying_macro
{
crate::vim_state::vim_mut(self).recording_keys.push(input);
}
}
// ─── Mode reset / mouse-driven selection / operator range probe ────────
fn force_normal(&mut self) {
crate::vim::force_normal_bridge(self);
}
fn mouse_click_doc(&mut self, row: usize, col: usize) {
crate::vim::mouse_click_doc_bridge(self, row, col);
}
fn mouse_begin_drag(&mut self) {
crate::vim::mouse_begin_drag_bridge(self);
}
fn range_for_op_motion(
&mut self,
motion_key: char,
total_count: usize,
) -> Option<(usize, usize)> {
crate::vim::range_for_op_motion_bridge(self, motion_key, total_count)
}
// ─── Motion dispatch / operator range probes ───────────────────────────
fn apply_motion(&mut self, kind: MotionKind, count: usize) {
crate::vim::apply_motion_kind(self, kind, count);
}
fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
crate::vim::range_for_op_g_bridge(self, ch, total_count)
}
fn range_for_op_text_obj(
&self,
ch: char,
inner: bool,
total_count: usize,
) -> Option<(usize, usize)> {
crate::vim::range_for_op_text_obj_bridge(self, ch, inner, total_count)
}
}