1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
//! ZLE widgets - line editor commands
//!
//! Direct port from zsh/Src/Zle/zle.h widget structures
//!
//! A widget is a ZLE command that can be bound to keys or executed by name.
//! Widgets can be internal (implemented in Rust) or user-defined (shell functions).
use super::main::Zle;
/// Widget function type
pub type ZleIntFunc = fn(&mut Zle) -> i32;
/// Widget function variants
#[derive(Clone)]
pub enum WidgetFunc {
/// Internally implemented widget
Internal(fn(&mut Zle)),
/// User-defined widget (name of shell function)
User(String),
}
impl std::fmt::Debug for WidgetFunc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WidgetFunc::Internal(_) => write!(f, "Internal(<fn>)"),
WidgetFunc::User(name) => write!(f, "User({})", name),
}
}
}
bitflags::bitflags! {
/// Widget flags
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct WidgetFlags: u32 {
/// Widget is internally implemented
const INT = 1 << 0;
/// New style completion widget
const NCOMP = 1 << 1;
/// DON'T invalidate completion list
const MENUCMP = 1 << 2;
/// Yank after cursor
const YANKAFTER = 1 << 3;
/// Yank before cursor
const YANKBEFORE = 1 << 4;
/// Yank (either direction)
const YANK = Self::YANKAFTER.bits() | Self::YANKBEFORE.bits();
/// Command is a line-oriented movement
const LINEMOVE = 1 << 5;
/// Widget reads further keys so wait if prefix
const VIOPER = 1 << 6;
/// Command maintains lastcol correctly
const LASTCOL = 1 << 7;
/// Kill command
const KILL = 1 << 8;
/// DON'T remove added suffix
const KEEPSUFFIX = 1 << 9;
/// Widget should not alter lastcmd
const NOTCOMMAND = 1 << 10;
/// Usable for new style completion
const ISCOMP = 1 << 11;
/// Widget is in use
const INUSE = 1 << 12;
/// Request to free when no longer in use
const FREE = 1 << 13;
/// Widget should not alter lbindk
const NOLAST = 1 << 14;
}
}
/// A widget (ZLE command)
#[derive(Debug, Clone)]
pub struct Widget {
/// Flags
pub flags: WidgetFlags,
/// Widget function
pub func: WidgetFunc,
}
impl Widget {
/// Build a widget that points at a Rust function pointer with the
/// supplied ZLE flags.
/// Equivalent to the WIDGET_INT branch of `zalloc(sizeof(*w))` +
/// `w->u.fn = ...` in `addzlefunction()` at
/// Src/Zle/zle_thingy.c:281. The C source uses this for every
/// `iwidgets.list` entry (the static built-in table); we collapse
/// "internal" + "builtin" into the same WidgetFunc::Internal
/// variant since both end up dispatching a Rust fn ptr.
pub fn internal(name: &str, func: fn(&mut Zle), flags: WidgetFlags) -> Self {
let _ = name; // Mirrors addzlefunction's `w->name` field — unused
// here because dispatch is by table lookup, not name.
Widget {
flags: flags | WidgetFlags::INT,
func: WidgetFunc::Internal(func),
}
}
/// Resolve a built-in widget name to a Widget.
/// Equivalent to the lookup-by-name path in
/// `bin_zle_call`/`getkeycmd` (Src/Zle/zle_thingy.c) when the
/// resolved Thingy points at a built-in. Routes through
/// `get_builtin_widget` which is the static table corresponding
/// to zsh's `intwidget[]` — see `Src/Zle/iwidgets.list` for the
/// canonical name → fn mapping.
pub fn builtin(name: &str) -> Self {
let (func, flags) = get_builtin_widget(name);
Widget {
flags: flags | WidgetFlags::INT,
func: WidgetFunc::Internal(func),
}
}
/// Build a widget that wraps a user-defined shell function.
/// Equivalent to `bin_zle_new()` from Src/Zle/zle_thingy.c:584
/// (the `zle -N name [shell-fn]` builtin). The C source allocates
/// a fresh Widget without WIDGET_INT, sets `w->u.fnnam` to the
/// shell function name, and binds it to a Thingy.
pub fn user_defined(name: &str, func_name: &str) -> Self {
let _ = name;
Widget {
flags: WidgetFlags::empty(),
func: WidgetFunc::User(func_name.to_string()),
}
}
}
/// Get the builtin widget function for a name
fn get_builtin_widget(name: &str) -> (fn(&mut Zle), WidgetFlags) {
match name {
// Accept/execute
"accept-line" => (widget_accept_line, WidgetFlags::empty()),
"accept-and-hold" => (widget_accept_and_hold, WidgetFlags::empty()),
"accept-line-and-down-history" => {
(widget_accept_line_and_down_history, WidgetFlags::empty())
}
// Self-insert
"self-insert" => (widget_self_insert, WidgetFlags::empty()),
"self-insert-unmeta" => (widget_self_insert_unmeta, WidgetFlags::empty()),
// Movement - character
"forward-char" => (widget_forward_char, WidgetFlags::empty()),
"backward-char" => (widget_backward_char, WidgetFlags::empty()),
// Movement - word
"forward-word" => (widget_forward_word, WidgetFlags::empty()),
"backward-word" => (widget_backward_word, WidgetFlags::empty()),
// Movement - line
"beginning-of-line" => (widget_beginning_of_line, WidgetFlags::empty()),
"end-of-line" => (widget_end_of_line, WidgetFlags::empty()),
// Delete
"delete-char" => (widget_delete_char, WidgetFlags::empty()),
"backward-delete-char" => (widget_backward_delete_char, WidgetFlags::empty()),
"delete-char-or-list" => (widget_delete_char_or_list, WidgetFlags::empty()),
// Kill
"kill-line" => (widget_kill_line, WidgetFlags::KILL),
"backward-kill-line" => (widget_backward_kill_line, WidgetFlags::KILL),
"kill-whole-line" => (widget_kill_whole_line, WidgetFlags::KILL),
"kill-word" => (widget_kill_word, WidgetFlags::KILL),
"backward-kill-word" => (widget_backward_kill_word, WidgetFlags::KILL),
// Yank
"yank" => (widget_yank, WidgetFlags::YANK),
"yank-pop" => (widget_yank_pop, WidgetFlags::YANK),
// Undo
"undo" => (widget_undo, WidgetFlags::empty()),
"redo" => (widget_redo, WidgetFlags::empty()),
// History
"up-line-or-history" => (widget_up_line_or_history, WidgetFlags::LINEMOVE),
"down-line-or-history" => (widget_down_line_or_history, WidgetFlags::LINEMOVE),
"up-history" => (widget_up_history, WidgetFlags::LINEMOVE),
"down-history" => (widget_down_history, WidgetFlags::LINEMOVE),
"history-incremental-search-backward" => {
(widget_history_isearch_backward, WidgetFlags::empty())
}
"history-incremental-search-forward" => {
(widget_history_isearch_forward, WidgetFlags::empty())
}
"beginning-of-buffer-or-history" => {
(widget_beginning_of_buffer_or_history, WidgetFlags::LINEMOVE)
}
"end-of-buffer-or-history" => (widget_end_of_buffer_or_history, WidgetFlags::LINEMOVE),
// Misc
"transpose-chars" => (widget_transpose_chars, WidgetFlags::empty()),
"clear-screen" => (widget_clear_screen, WidgetFlags::empty()),
"redisplay" => (widget_redisplay, WidgetFlags::empty()),
"send-break" => (widget_send_break, WidgetFlags::empty()),
"overwrite-mode" => (widget_overwrite_mode, WidgetFlags::empty()),
"quoted-insert" => (widget_quoted_insert, WidgetFlags::empty()),
// Completion
"expand-or-complete" => (widget_expand_or_complete, WidgetFlags::MENUCMP),
"complete-word" => (widget_complete_word, WidgetFlags::MENUCMP),
"expand-word" => (widget_expand_word, WidgetFlags::empty()),
"list-choices" => (widget_list_choices, WidgetFlags::MENUCMP),
"menu-complete" => (widget_menu_complete, WidgetFlags::MENUCMP),
// Vi mode
"vi-cmd-mode" => (widget_vi_cmd_mode, WidgetFlags::empty()),
"vi-insert" => (widget_vi_insert, WidgetFlags::empty()),
"vi-insert-bol" => (widget_vi_insert_bol, WidgetFlags::empty()),
"vi-add-next" => (widget_vi_add_next, WidgetFlags::empty()),
"vi-add-eol" => (widget_vi_add_eol, WidgetFlags::empty()),
"vi-forward-char" => (widget_vi_forward_char, WidgetFlags::empty()),
"vi-backward-char" => (widget_vi_backward_char, WidgetFlags::empty()),
"vi-forward-word" => (widget_vi_forward_word, WidgetFlags::empty()),
"vi-forward-word-end" => (widget_vi_forward_word_end, WidgetFlags::empty()),
"vi-forward-blank-word" => (widget_vi_forward_blank_word, WidgetFlags::empty()),
"vi-forward-blank-word-end" => (widget_vi_forward_blank_word_end, WidgetFlags::empty()),
"vi-backward-word" => (widget_vi_backward_word, WidgetFlags::empty()),
"vi-backward-blank-word" => (widget_vi_backward_blank_word, WidgetFlags::empty()),
"vi-delete" => (widget_vi_delete, WidgetFlags::VIOPER | WidgetFlags::KILL),
"vi-delete-char" => (widget_vi_delete_char, WidgetFlags::empty()),
"vi-backward-delete-char" => (widget_vi_backward_delete_char, WidgetFlags::empty()),
"vi-change" => (widget_vi_change, WidgetFlags::VIOPER | WidgetFlags::KILL),
"vi-change-eol" => (widget_vi_change_eol, WidgetFlags::KILL),
"vi-kill-eol" => (widget_vi_kill_eol, WidgetFlags::KILL),
"vi-yank" => (widget_vi_yank, WidgetFlags::VIOPER),
"vi-yank-whole-line" => (widget_vi_yank_whole_line, WidgetFlags::empty()),
"vi-put-after" => (widget_vi_put_after, WidgetFlags::YANK),
"vi-put-before" => (widget_vi_put_before, WidgetFlags::YANK),
"vi-replace" => (widget_vi_replace, WidgetFlags::empty()),
"vi-replace-chars" => (widget_vi_replace_chars, WidgetFlags::empty()),
"vi-substitute" => (widget_vi_substitute, WidgetFlags::empty()),
"vi-change-whole-line" => (widget_vi_change_whole_line, WidgetFlags::KILL),
"vi-first-non-blank" => (widget_vi_first_non_blank, WidgetFlags::empty()),
"vi-end-of-line" => (widget_vi_end_of_line, WidgetFlags::empty()),
"vi-digit-or-beginning-of-line" => {
(widget_vi_digit_or_beginning_of_line, WidgetFlags::empty())
}
"vi-open-line-below" => (widget_vi_open_line_below, WidgetFlags::empty()),
"vi-open-line-above" => (widget_vi_open_line_above, WidgetFlags::empty()),
"vi-join" => (widget_vi_join, WidgetFlags::empty()),
"vi-repeat-change" => (widget_vi_repeat_change, WidgetFlags::empty()),
"vi-find-next-char" => (widget_vi_find_next_char, WidgetFlags::empty()),
"vi-find-prev-char" => (widget_vi_find_prev_char, WidgetFlags::empty()),
"vi-find-next-char-skip" => (widget_vi_find_next_char_skip, WidgetFlags::empty()),
"vi-find-prev-char-skip" => (widget_vi_find_prev_char_skip, WidgetFlags::empty()),
"vi-repeat-find" => (widget_vi_repeat_find, WidgetFlags::empty()),
"vi-rev-repeat-find" => (widget_vi_rev_repeat_find, WidgetFlags::empty()),
"vi-history-search-forward" => (widget_vi_history_search_forward, WidgetFlags::empty()),
"vi-history-search-backward" => (widget_vi_history_search_backward, WidgetFlags::empty()),
"vi-repeat-search" => (widget_vi_repeat_search, WidgetFlags::empty()),
"vi-rev-repeat-search" => (widget_vi_rev_repeat_search, WidgetFlags::empty()),
"vi-fetch-history" => (widget_vi_fetch_history, WidgetFlags::LINEMOVE),
"vi-goto-column" => (widget_vi_goto_column, WidgetFlags::empty()),
"vi-backward-kill-word" => (widget_vi_backward_kill_word, WidgetFlags::KILL),
// Digit argument
"digit-argument" => (widget_digit_argument, WidgetFlags::NOTCOMMAND),
// Region / mark — ports cited in each widget body docstring
// against Src/Zle/zle_move.c (set-mark / exchange-point /
// visual / deactivate) and zle_misc.c (quote-line / quote-region
// / copy-region-as-kill / pound-insert / copy-prev-word).
"set-mark-command" => (
widget_set_mark_command,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
"exchange-point-and-mark" => (widget_exchange_point_and_mark, WidgetFlags::empty()),
"deactivate-region" => (widget_deactivate_region, WidgetFlags::empty()),
"visual-mode" => (widget_visual_mode, WidgetFlags::empty()),
"visual-line-mode" => (widget_visual_line_mode, WidgetFlags::empty()),
"copy-region-as-kill" => (widget_copy_region_as_kill, WidgetFlags::KEEPSUFFIX),
"copy-prev-word" => (widget_copy_prev_word, WidgetFlags::KEEPSUFFIX),
"quote-line" => (widget_quote_line, WidgetFlags::empty()),
"quote-region" => (widget_quote_region, WidgetFlags::empty()),
"pound-insert" => (widget_pound_insert, WidgetFlags::empty()),
"vi-pound-insert" => (widget_pound_insert, WidgetFlags::empty()),
// Case changes — bodies in this file delegate to the existing
// capitalize/down/upcase methods on Zle (Src/Zle/zle_misc.c).
"capitalize-word" => (widget_capitalize_word, WidgetFlags::empty()),
"down-case-word" => (widget_down_case_word, WidgetFlags::empty()),
"up-case-word" => (widget_up_case_word, WidgetFlags::empty()),
"vi-down-case" => (
widget_down_case_word,
WidgetFlags::LASTCOL | WidgetFlags::VIOPER,
),
"vi-up-case" => (
widget_up_case_word,
WidgetFlags::LASTCOL | WidgetFlags::VIOPER,
),
// History (additional registrations) — bodies cite zle_hist.c.
"beginning-of-history" => (widget_beginning_of_history, WidgetFlags::empty()),
"end-of-history" => (widget_end_of_history, WidgetFlags::empty()),
"history-beginning-search-backward" => {
(widget_history_beginning_search_backward, WidgetFlags::empty())
}
"history-beginning-search-forward" => {
(widget_history_beginning_search_forward, WidgetFlags::empty())
}
"push-line" => (widget_push_line, WidgetFlags::empty()),
"push-line-or-edit" => (widget_push_line, WidgetFlags::empty()),
"transpose-words" => (widget_transpose_words, WidgetFlags::empty()),
"beep" => (widget_beep, WidgetFlags::empty()),
"describe-key-briefly" => (
widget_describe_key_briefly,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
// Word ops (extra) — Src/Zle/zle_word.c
"delete-word" => (widget_delete_word, WidgetFlags::empty()),
"backward-delete-word" => (widget_backward_delete_word, WidgetFlags::KEEPSUFFIX),
"emacs-forward-word" => (widget_emacs_forward_word, WidgetFlags::empty()),
"emacs-backward-word" => (widget_emacs_backward_word, WidgetFlags::empty()),
// Region kill/buffer — Src/Zle/zle_misc.c
"kill-region" => (widget_kill_region, WidgetFlags::KILL | WidgetFlags::KEEPSUFFIX),
"kill-buffer" => (widget_kill_buffer, WidgetFlags::KILL | WidgetFlags::KEEPSUFFIX),
// Vi mark widgets — bodies in this file delegate to the existing
// Zle::vi_set_mark / Zle::vi_goto_mark methods (Src/Zle/zle_move.c).
"vi-set-mark" => (widget_vi_set_mark_widget, WidgetFlags::empty()),
"vi-goto-mark" => (widget_vi_goto_mark_widget, WidgetFlags::empty()),
"vi-goto-mark-line" => (widget_vi_goto_mark_line_widget, WidgetFlags::empty()),
"vi-match-bracket" => (widget_vi_match_bracket, WidgetFlags::empty()),
"vi-caps-lock-panic" => (widget_vi_caps_lock_panic, WidgetFlags::empty()),
// Vi line/yank — Src/Zle/zle_vi.c
"vi-kill-line" => (widget_vi_kill_line, WidgetFlags::KILL),
"vi-yank-eol" => (widget_vi_yank_eol, WidgetFlags::empty()),
"vi-beginning-of-line" => (widget_vi_beginning_of_line, WidgetFlags::empty()),
"vi-swap-case" => (widget_vi_swap_case, WidgetFlags::empty()),
"vi-oper-swap-case" => (widget_vi_oper_swap_case, WidgetFlags::VIOPER),
"vi-undo-change" => (widget_vi_undo_change, WidgetFlags::empty()),
// Argument prefixes — Src/Zle/zle_misc.c
"universal-argument" => (widget_universal_argument, WidgetFlags::NOTCOMMAND),
"neg-argument" => (widget_neg_argument, WidgetFlags::NOTCOMMAND),
// Misc — Src/Zle/zle_main.c, zle_misc.c
"recursive-edit" => (widget_recursive_edit, WidgetFlags::empty()),
"what-cursor-position" => (
widget_what_cursor_position,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
"set-local-history" => (widget_set_local_history_widget, WidgetFlags::empty()),
"undefined-key" => (widget_undefined_key, WidgetFlags::empty()),
// History search variants — Src/Zle/zle_hist.c
"history-search-backward" => (widget_history_search_backward, WidgetFlags::empty()),
"history-search-forward" => (widget_history_search_forward, WidgetFlags::empty()),
"insert-last-word" => (widget_insert_last_word_widget, WidgetFlags::empty()),
// Cursor motion (extra) — Src/Zle/zle_hist.c
"up-line" => (widget_up_line, WidgetFlags::LINEMOVE),
"down-line" => (widget_down_line, WidgetFlags::LINEMOVE),
"up-line-or-search" => (widget_up_line_or_search, WidgetFlags::LINEMOVE),
"down-line-or-search" => (widget_down_line_or_search, WidgetFlags::LINEMOVE),
"vi-up-line-or-history" => (widget_vi_up_line_or_history, WidgetFlags::LINEMOVE),
"vi-down-line-or-history" => (widget_vi_down_line_or_history, WidgetFlags::LINEMOVE),
"beginning-of-line-hist" => (widget_beginning_of_line_hist, WidgetFlags::empty()),
"end-of-line-hist" => (widget_end_of_line_hist, WidgetFlags::empty()),
// Misc Src/Zle/zle_misc.c additions
"copy-prev-shell-word" => (widget_copy_prev_shell_word, WidgetFlags::KEEPSUFFIX),
"gosmacs-transpose-chars" => (widget_gosmacs_transpose_chars, WidgetFlags::empty()),
"reset-prompt" => (widget_reset_prompt, WidgetFlags::empty()),
"split-undo" => (widget_split_undo, WidgetFlags::empty()),
"argument-base" => (
widget_argument_base,
WidgetFlags::MENUCMP
| WidgetFlags::KEEPSUFFIX
| WidgetFlags::LASTCOL
| WidgetFlags::NOTCOMMAND,
),
// History extras — Src/Zle/zle_hist.c
"infer-next-history" => (widget_infer_next_history, WidgetFlags::empty()),
"accept-and-infer-next-history" => {
(widget_accept_and_infer_next_history, WidgetFlags::empty())
}
"get-line" => (widget_get_line, WidgetFlags::empty()),
"push-input" => (widget_push_input, WidgetFlags::empty()),
// Vi extras — Src/Zle/zle_vi.c
"vi-quoted-insert" => (widget_vi_quoted_insert, WidgetFlags::empty()),
"vi-set-buffer" => (widget_vi_set_buffer, WidgetFlags::NOTCOMMAND),
"vi-indent" => (widget_vi_indent, WidgetFlags::VIOPER),
"vi-unindent" => (widget_vi_unindent, WidgetFlags::VIOPER),
// Misc host-dispatch hooks — Src/Zle/zle_misc.c, zle_tricky.c
"run-help" => (
widget_run_help,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
"which-command" => (
widget_run_help,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
"expand-history" => (widget_expand_history, WidgetFlags::empty()),
"magic-space" => (widget_magic_space, WidgetFlags::KEEPSUFFIX | WidgetFlags::MENUCMP),
"spell-word" => (widget_spell_word, WidgetFlags::empty()),
"bracketed-paste" => (widget_bracketed_paste, WidgetFlags::empty()),
// Vi backward word-end (extra) — Src/Zle/zle_word.c
"vi-backward-word-end" => (widget_vi_backward_word_end, WidgetFlags::empty()),
"vi-backward-blank-word-end" => {
(widget_vi_backward_blank_word_end, WidgetFlags::empty())
}
// Text objects (vi `iw`/`aw` etc.) — Src/Zle/textobjects.c
"select-in-word" => (widget_select_in_word, WidgetFlags::empty()),
"select-a-word" => (widget_select_a_word, WidgetFlags::empty()),
"select-in-blank-word" => (widget_select_in_blank_word, WidgetFlags::empty()),
"select-a-blank-word" => (widget_select_a_blank_word, WidgetFlags::empty()),
"select-in-shell-word" => (widget_select_in_shell_word, WidgetFlags::empty()),
"select-a-shell-word" => (widget_select_a_shell_word, WidgetFlags::empty()),
// Completion menu navigation — Src/Zle/zle_tricky.c (host hooks)
"menu-expand-or-complete" => (widget_menu_expand_or_complete, WidgetFlags::MENUCMP),
"reverse-menu-complete" => (widget_reverse_menu_complete, WidgetFlags::MENUCMP),
"accept-and-menu-complete" => (
widget_accept_and_menu_complete,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX,
),
"list-expand" => (
widget_list_expand,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX,
),
"expand-cmd-path" => (widget_expand_cmd_path, WidgetFlags::empty()),
"expand-or-complete-prefix" => (widget_expand_or_complete_prefix, WidgetFlags::MENUCMP),
"end-of-list" => (
widget_end_of_list,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
// Suffix handling — Src/Zle/zle_misc.c
"auto-suffix-remove" => (widget_auto_suffix_remove, WidgetFlags::NOTCOMMAND),
"auto-suffix-retain" => (
widget_auto_suffix_retain,
WidgetFlags::KEEPSUFFIX | WidgetFlags::NOTCOMMAND,
),
// Region paste / put — Src/Zle/zle_misc.c
"put-replace-selection" => (widget_put_replace_selection, WidgetFlags::YANK),
// Named-command execution — Src/Zle/zle_thingy.c (host hooks)
"execute-named-cmd" => (widget_execute_named_cmd, WidgetFlags::empty()),
"execute-last-named-cmd" => (widget_execute_last_named_cmd, WidgetFlags::empty()),
"read-command" => (widget_read_command, WidgetFlags::empty()),
"where-is" => (
widget_where_is,
WidgetFlags::MENUCMP | WidgetFlags::KEEPSUFFIX | WidgetFlags::LASTCOL,
),
// Pattern isearch — Src/Zle/zle_hist.c:936/943
"history-incremental-pattern-search-backward" => (
widget_history_incremental_pattern_search_backward,
WidgetFlags::empty(),
),
"history-incremental-pattern-search-forward" => (
widget_history_incremental_pattern_search_forward,
WidgetFlags::empty(),
),
// Search acceptance — Src/Zle/zle_hist.c
"accept-search" => (widget_accept_search, WidgetFlags::empty()),
// Default: undefined widget
_ => (widget_undefined, WidgetFlags::empty()),
}
}
// Widget implementations
fn widget_accept_line(zle: &mut Zle) {
// Port of acceptline() from Src/Zle/zle_misc.c:401. The C source is
// a one-liner: `done = 1`; everything else (return current line,
// history append, hooks) happens in zleread() after zlecore returns.
zle.done = true;
}
fn widget_accept_and_hold(zle: &mut Zle) {
// Port of acceptandhold() from Src/Zle/zle_misc.c:409.
// Push current line onto bufstack so the next zleread() re-feeds it
// as the next entry, then exit the editor.
let line: String = zle.zleline.iter().collect();
zle.bufstack.push(line);
zle.stackcs = zle.zlecs;
zle.done = true;
zle.accept_line();
}
fn widget_accept_line_and_down_history(zle: &mut Zle) {
// Port of acceptlineanddownhistory() from Src/Zle/zle_hist.c:420.
// Move forward one history entry and queue it on bufstack so the
// next zleread() loads it; then exit the editor to run the current line.
let len = zle.history.entries.len();
let next_idx = zle.history.cursor + 1;
if next_idx < len {
if let Some(entry) = zle.history.entries.get(next_idx) {
zle.bufstack.push(entry.line.clone());
zle.stackhist = (entry.num as i32).max(0);
}
}
zle.done = true;
zle.accept_line();
}
fn widget_self_insert(zle: &mut Zle) {
// Port of selfinsert() from Src/Zle/zle_misc.c:113. Insert the
// last-read char at the cursor (`zmult` times — count-aware).
let n = zle.mult.max(1);
#[cfg(feature = "multibyte")]
let c_opt = char::from_u32(zle.lastchar as u32);
#[cfg(not(feature = "multibyte"))]
let c_opt = if (0..=127).contains(&zle.lastchar) {
Some(zle.lastchar as u8 as char)
} else {
None
};
if let Some(c) = c_opt {
for _ in 0..n {
zle.self_insert(c);
}
}
}
fn widget_self_insert_unmeta(zle: &mut Zle) {
// Port of selfinsertunmeta() from Src/Zle/zle_misc.c:149. Strip the
// 0x80 meta bit from lastchar before inserting — used when the user
// bound an Esc-prefixed key (e.g. `\\eA`) to literally insert 'A'.
let n = zle.mult.max(1);
let c = (zle.lastchar & 0x7f) as u8 as char;
for _ in 0..n {
zle.self_insert(c);
}
}
fn widget_forward_char(zle: &mut Zle) {
// Port of forwardchar() from Src/Zle/zle_move.c:441. Count-aware;
// negative count delegates to backward-char (mirrors the C source's
// recursive flip at zle_move.c:445).
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_backward_char(zle);
zle.mult = n;
return;
}
while zle.zlecs < zle.zlell && n > 0 {
zle.zlecs += 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_backward_char(zle: &mut Zle) {
// Port of backwardchar() from Src/Zle/zle_move.c:464.
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_forward_char(zle);
zle.mult = n;
return;
}
while zle.zlecs > 0 && n > 0 {
zle.zlecs -= 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_forward_word(zle: &mut Zle) {
// Port of forwardword() from Src/Zle/zle_word.c:45. Count-aware;
// skips the current word (iword chars) then trailing non-iword
// chars, repeated `mult` times.
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs >= zle.zlell {
break;
}
while zle.zlecs < zle.zlell && is_word_char(zle.zleline[zle.zlecs]) {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell && !is_word_char(zle.zleline[zle.zlecs]) {
zle.zlecs += 1;
}
}
zle.resetneeded = true;
}
fn widget_backward_word(zle: &mut Zle) {
// Port of backwardword() from Src/Zle/zle_word.c:240. Count-aware
// mirror of forward-word.
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs == 0 {
break;
}
while zle.zlecs > 0 && !is_word_char(zle.zleline[zle.zlecs - 1]) {
zle.zlecs -= 1;
}
while zle.zlecs > 0 && is_word_char(zle.zleline[zle.zlecs - 1]) {
zle.zlecs -= 1;
}
}
zle.resetneeded = true;
}
fn widget_beginning_of_line(zle: &mut Zle) {
// Port of beginningofline() from Src/Zle/zle_move.c. Cursor moves
// to the start of the current logical line — find_bol respects
// embedded newlines.
zle.zlecs = zle.find_bol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_end_of_line(zle: &mut Zle) {
// Port of endofline() from Src/Zle/zle_move.c.
zle.zlecs = zle.find_eol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_delete_char(zle: &mut Zle) {
// Port of deletechar() from Src/Zle/zle_misc.c:157. Count-aware;
// negative count delegates to backward-delete-char. The C source
// returns 1 (failure) if it can't delete `mult` chars (cursor hit
// EoB before completing); we beep instead since we don't propagate
// widget return codes through the zlecore.
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_backward_delete_char(zle);
zle.mult = n;
return;
}
while n > 0 {
if zle.zlecs >= zle.zlell {
zle.handle_feep();
return;
}
zle.zleline.remove(zle.zlecs);
zle.zlell -= 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_backward_delete_char(zle: &mut Zle) {
// Port of backwarddeletechar() from Src/Zle/zle_misc.c:180.
// Count-aware; negative count delegates to delete-char. C clamps
// count to zlecs (zle_misc.c:189) so deleting past BoB stops at 0
// rather than erroring.
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_delete_char(zle);
zle.mult = n;
return;
}
if (n as usize) > zle.zlecs {
n = zle.zlecs as i32;
}
while n > 0 && zle.zlecs > 0 {
zle.zlecs -= 1;
zle.zleline.remove(zle.zlecs);
zle.zlell -= 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_delete_char_or_list(zle: &mut Zle) {
// Port of deletecharorlist() from Src/Zle/zle_misc.c. With an empty
// buffer this is EOF; with non-end cursor it deletes one char; at
// end-of-line it falls through to list-choices completion.
if zle.zlell == 0 {
zle.done = true;
} else if zle.zlecs < zle.zlell {
widget_delete_char(zle);
} else {
zle.completion_request = Some(super::main::CompletionRequest::ListChoices);
}
}
fn widget_kill_line(zle: &mut Zle) {
// Port of killline() from Src/Zle/zle_misc.c:419. Count-aware:
// killing N lines from the cursor — for each iteration, if the
// cursor sits on a newline consume just that newline, otherwise
// kill from cursor to the next newline (or EoB). Final kill goes
// on the kill ring as one entry. Negative count delegates to
// backward-kill-line (zle_misc.c:423).
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_backward_kill_line(zle);
zle.mult = n;
return;
}
let start = zle.zlecs;
while n > 0 {
if zle.zlecs >= zle.zlell {
break;
}
if zle.zleline[zle.zlecs] == '\n' {
zle.zlecs += 1;
} else {
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs] != '\n' {
zle.zlecs += 1;
}
}
n -= 1;
}
if zle.zlecs > start {
let killed: Vec<char> = zle.zleline.drain(start..zle.zlecs).collect();
zle.zlell -= zle.zlecs - start;
zle.zlecs = start;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
}
fn widget_backward_kill_line(zle: &mut Zle) {
// Port of backwardkillline() from Src/Zle/zle_misc.c:225. Mirror of
// kill-line: per iteration, consume a leading \\n if present;
// otherwise back up to the previous \\n (or BoB). Negative count
// delegates to kill-line (zle_misc.c:229).
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_kill_line(zle);
zle.mult = n;
return;
}
let end = zle.zlecs;
while n > 0 {
if zle.zlecs == 0 {
break;
}
if zle.zleline[zle.zlecs - 1] == '\n' {
zle.zlecs -= 1;
} else {
while zle.zlecs > 0 && zle.zleline[zle.zlecs - 1] != '\n' {
zle.zlecs -= 1;
}
}
n -= 1;
}
if end > zle.zlecs {
let killed: Vec<char> = zle.zleline.drain(zle.zlecs..end).collect();
zle.zlell -= end - zle.zlecs;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
}
fn widget_kill_whole_line(zle: &mut Zle) {
// Port of killwholeline() from Src/Zle/zle_misc.c:195. The C source
// is count-aware: kills `mult` lines centered on the current line
// (or the whole buffer if -1). Our simplified version kills the
// entire buffer once — sufficient for the common single-line use,
// multi-line variants left as a follow-up.
if zle.zlell > 0 {
let killed = std::mem::take(&mut zle.zleline);
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.zlecs = 0;
zle.zlell = 0;
zle.resetneeded = true;
}
}
fn widget_kill_word(zle: &mut Zle) {
// Port of killword() from Src/Zle/zle_word.c. The C source skips
// non-word chars then the word, captures the killed region in the
// kill ring, and leaves the cursor at the start. Honours the count
// multiplier (mult) — `3M-d` kills three words.
let n = zle.mult.max(1);
let start = zle.zlecs;
for _ in 0..n {
if zle.zlecs >= zle.zlell {
break;
}
while zle.zlecs < zle.zlell && !is_word_char(zle.zleline[zle.zlecs]) {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell && is_word_char(zle.zleline[zle.zlecs]) {
zle.zlecs += 1;
}
}
let end = zle.zlecs;
zle.zlecs = start;
if end > start {
let killed: Vec<char> = zle.zleline.drain(start..end).collect();
zle.zlell -= end - start;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
}
fn widget_backward_kill_word(zle: &mut Zle) {
// Port of backwardkillword() from Src/Zle/zle_word.c. Mirrors
// kill-word but in the opposite direction; cursor lands at the
// start of the killed range. Count-aware.
let n = zle.mult.max(1);
let end = zle.zlecs;
for _ in 0..n {
if zle.zlecs == 0 {
break;
}
while zle.zlecs > 0 && !is_word_char(zle.zleline[zle.zlecs - 1]) {
zle.zlecs -= 1;
}
while zle.zlecs > 0 && is_word_char(zle.zleline[zle.zlecs - 1]) {
zle.zlecs -= 1;
}
}
let start = zle.zlecs;
if end > start {
let killed: Vec<char> = zle.zleline.drain(start..end).collect();
zle.zlell -= end - start;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
}
fn widget_yank(zle: &mut Zle) {
// Port of yank() from Src/Zle/zle_misc.c. Inserts the most-recent kill-ring
// entry at the cursor and remembers the inserted region so that an
// immediately-following yank-pop can rotate to the previous entry.
if let Some(text) = zle.killring.front().cloned() {
let start = zle.zlecs;
for c in &text {
zle.zleline.insert(zle.zlecs, *c);
zle.zlecs += 1;
zle.zlell += 1;
}
zle.yank_start = start;
zle.yank_end = start + text.len();
zle.yank_cs = zle.zlecs;
zle.yank_ring_idx = Some(0);
zle.yanklast = true;
zle.resetneeded = true;
}
}
fn widget_yank_pop(zle: &mut Zle) {
// Port of yankpop() from Src/Zle/zle_misc.c:728.
// Only meaningful immediately after a yank; replaces the just-yanked
// region with the previous kill-ring entry, cycling around the ring.
if !zle.yanklast {
return;
}
let ring_len = zle.killring.len();
if ring_len == 0 {
return;
}
// Advance to the next ring entry; skip empty buffers; bail out if we
// wrap all the way around without finding anything (matches kctstart guard
// in C zle_misc.c:730).
let start_idx = zle.yank_ring_idx.unwrap_or(0);
let mut idx = start_idx;
let mut found_idx: Option<usize> = None;
for _ in 0..ring_len {
idx = (idx + 1) % ring_len;
if idx == start_idx {
break;
}
if !zle.killring[idx].is_empty() {
found_idx = Some(idx);
break;
}
}
let new_idx = match found_idx {
Some(i) => i,
None => return,
};
let new_text: Vec<char> = zle.killring[new_idx].clone();
// Delete the previously-yanked region.
let yb = zle.yank_start.min(zle.zlell);
let ye = zle.yank_end.min(zle.zlell);
if ye > yb {
zle.zleline.drain(yb..ye);
zle.zlell -= ye - yb;
}
zle.zlecs = yb;
// Paste the new entry.
let start = zle.zlecs;
for c in &new_text {
zle.zleline.insert(zle.zlecs, *c);
zle.zlecs += 1;
zle.zlell += 1;
}
zle.yank_start = start;
zle.yank_end = start + new_text.len();
zle.yank_cs = zle.zlecs;
zle.yank_ring_idx = Some(new_idx);
zle.yanklast = true;
zle.resetneeded = true;
}
fn widget_undo(zle: &mut Zle) {
// Port of undo() from Src/Zle/zle_utils.c:1601.
let _ = zle.undo_widget();
}
fn widget_redo(zle: &mut Zle) {
// Port of redo() from Src/Zle/zle_utils.c:1661.
let _ = zle.redo_widget();
}
fn widget_up_line_or_history(zle: &mut Zle) {
// Port of uplineorhistory() from Src/Zle/zle_hist.c:282.
let _ = zle.up_line_or_history_widget();
}
fn widget_down_line_or_history(zle: &mut Zle) {
// Port of downlineorhistory() from Src/Zle/zle_hist.c:370.
let _ = zle.down_line_or_history_widget();
}
fn widget_up_history(zle: &mut Zle) {
// Port of uphistory() from Src/Zle/zle_hist.c:233.
// C calls zle_goto_hist(histline, -zmult, isset(HISTIGNOREDUPS)).
// skipdups=false until ZLE has access to ShellOptions; behavior matches HISTIGNOREDUPS unset.
let m = zle.mult;
zle.zle_goto_hist(-m, false);
}
fn widget_down_history(zle: &mut Zle) {
// Port of downhistory() from Src/Zle/zle_hist.c:434.
let m = zle.mult;
zle.zle_goto_hist(m, false);
}
fn widget_history_isearch_backward(zle: &mut Zle) {
// Port of historyincrementalsearchbackward() from Src/Zle/zle_hist.c:922
// (which is doisearch(-1, 0)).
do_isearch(zle, -1);
}
fn widget_history_isearch_forward(zle: &mut Zle) {
// Port of historyincrementalsearchforward() from Src/Zle/zle_hist.c:929
// (doisearch(1, 0)).
do_isearch(zle, 1);
}
/// Minimal port of doisearch() from zle_hist.c.
/// Reads characters into a pattern and re-searches history on each keystroke.
/// Recognised control chars: Ctrl-R repeats backward, Ctrl-S repeats forward,
/// Ctrl-G/Esc cancels (restores starting line), backspace shortens the
/// pattern, Enter accepts. Anything else exits the loop with the current
/// match in place.
fn do_isearch(zle: &mut Zle, mut dir: i32) {
// Save start state for cancel.
let start_line = zle.zleline.clone();
let start_cs = zle.zlecs;
let start_cursor = zle.history.cursor;
let mut pattern = String::new();
let mut current_idx: i32 = zle.history.cursor as i32;
while let Some(c) = zle.getfullchar(true) {
match c {
// Enter / Newline → accept current match.
'\r' | '\n' => break,
// Ctrl-G / Esc → cancel.
'\x07' | '\x1b' => {
zle.zleline = start_line;
zle.zlell = zle.zleline.len();
zle.zlecs = start_cs;
zle.history.cursor = start_cursor;
zle.resetneeded = true;
return;
}
// Ctrl-R → repeat backward.
'\x12' => {
dir = -1;
current_idx -= 1;
}
// Ctrl-S → repeat forward.
'\x13' => {
dir = 1;
current_idx += 1;
}
// Backspace / Delete → shrink pattern.
'\x08' | '\x7f' => {
pattern.pop();
current_idx = zle.history.cursor as i32;
}
// Other printable input → extend pattern.
ch if !ch.is_control() => {
pattern.push(ch);
}
_ => break,
}
if pattern.is_empty() {
continue;
}
zle.srch_str = Some(pattern.clone());
let len = zle.history.entries.len() as i32;
let matched: Option<usize> = if dir < 0 {
// Search backward starting at current_idx.
let mut i = current_idx.min(len - 1);
let mut found = None;
while i >= 0 {
if zle.history.entries[i as usize].line.contains(&pattern) {
found = Some(i as usize);
break;
}
i -= 1;
}
found
} else {
let mut i = current_idx.max(0);
let mut found = None;
while i < len {
if zle.history.entries[i as usize].line.contains(&pattern) {
found = Some(i as usize);
break;
}
i += 1;
}
found
};
if let Some(idx) = matched {
current_idx = idx as i32;
zle.history.cursor = idx;
zle.zleline = zle.history.entries[idx].line.chars().collect();
zle.zlell = zle.zleline.len();
// Place cursor at the start of the match for visual feedback.
zle.zlecs = zle.history.entries[idx]
.line
.find(&pattern)
.unwrap_or(0)
.min(zle.zlell);
zle.resetneeded = true;
} else {
// No match — beep but keep the prior position.
zle.handle_feep();
}
}
}
fn widget_beginning_of_buffer_or_history(zle: &mut Zle) {
// Port of beginningofbufferorhistory() from Src/Zle/zle_hist.c:573.
// If the cursor is past the start of its current logical line
// (findbol > 0), jump to absolute position 0 inside the buffer;
// otherwise we're already at BoB → fall through to
// beginning-of-history (load oldest entry).
if zle.find_bol(zle.zlecs) > 0 {
zle.zlecs = 0;
zle.resetneeded = true;
} else {
widget_beginning_of_history(zle);
}
}
fn widget_end_of_buffer_or_history(zle: &mut Zle) {
// Port of endofbufferorhistory() from Src/Zle/zle_hist.c:593.
if zle.find_eol(zle.zlecs) != zle.zlell {
zle.zlecs = zle.zlell;
zle.resetneeded = true;
} else {
widget_end_of_history(zle);
}
}
fn widget_transpose_chars(zle: &mut Zle) {
// Port of transposechars() from Src/Zle/zle_misc.c:313. The C source
// is count-aware (negative count transposes backwards, positive
// forwards, default 1) and respects newline boundaries — at BoL or
// EoL/EOB, the swap involves the cursor's neighbor on the same
// logical line. Cursor lands one past the swapped pair on positive
// count, mirroring emacs's `^T` advance.
let mut n = zle.mult;
let neg = n < 0;
if neg {
n = -n;
}
for _ in 0..n {
let mut ct = zle.zlecs;
// BoL (or right after \n) special-case: shift forward so we
// can swap (line[ct], line[ct+1]) — only valid if there's at
// least one more char before the next newline.
if ct == 0 || zle.zleline.get(ct - 1).copied() == Some('\n') {
if ct == zle.zlell || zle.zleline.get(ct).copied() == Some('\n') {
return;
}
if !neg {
zle.zlecs += 1;
}
ct += 1;
}
if neg {
if zle.zlecs > 0 && zle.zleline.get(zle.zlecs - 1).copied() != Some('\n') {
zle.zlecs -= 1;
if ct > 1 && zle.zleline.get(ct - 2).copied() != Some('\n') {
ct -= 1;
}
}
} else if zle.zlecs != zle.zlell
&& zle.zleline.get(zle.zlecs).copied() != Some('\n')
{
zle.zlecs += 1;
}
if ct == zle.zlell || zle.zleline.get(ct).copied() == Some('\n') {
ct -= 1;
}
if ct < 1 || zle.zleline.get(ct - 1).copied() == Some('\n') {
return;
}
zle.zleline.swap(ct - 1, ct);
}
zle.resetneeded = true;
}
fn widget_clear_screen(zle: &mut Zle) {
// Port of clearscreen() from Src/Zle/zle_refresh.c. Routes through
// the existing Zle::clearscreen helper which emits the CSI clear +
// home, then forces a refresh on the next zlecore iteration.
zle.clearscreen();
}
fn widget_redisplay(zle: &mut Zle) {
// Port of redisplay() from Src/Zle/zle_refresh.c. Just sets the
// reset flag — the next zlecore iteration calls zrefresh.
zle.resetneeded = true;
}
fn widget_send_break(zle: &mut Zle) {
// Port of sendbreak() from Src/Zle/zle_misc.c:1144. The C source
// sets errflag |= ERRFLAG_ERROR | ERRFLAG_INT and returns 1, which
// causes the zlecore loop at zle_main.c:1128 to exit. Our
// abort_line() clears the buffer and sets done=true — same outward
// effect, no errflag needed because we don't carry it through to
// the caller. (send_break() exists separately for non-widget
// callers that want just the buffer clear.)
zle.abort_line();
}
fn widget_overwrite_mode(zle: &mut Zle) {
// Port of overwritemode() from Src/Zle/zle_misc.c. Toggles between
// insert (default) and overwrite. Insert mode appends at the cursor;
// overwrite replaces the char under the cursor.
zle.insmode = !zle.insmode;
}
fn widget_quoted_insert(zle: &mut Zle) {
// Port of quotedinsert() from Src/Zle/zle_misc.c. Reads the next
// input char and inserts it literally (bypassing keymap dispatch),
// letting the user enter control chars verbatim — the canonical
// Ctrl-V binding.
if let Some(c) = zle.getfullchar(true) {
zle.self_insert(c);
}
}
fn widget_expand_or_complete(zle: &mut Zle) {
// Port of expandorcomplete() from Src/Zle/zle_tricky.c — tries
// expansion first, falls back to completion. Compsys lives in a
// separate crate; surface the request and let the host run it.
zle.completion_request = Some(super::main::CompletionRequest::ExpandOrComplete);
}
fn widget_complete_word(zle: &mut Zle) {
// Port of completeword() from Src/Zle/zle_tricky.c.
zle.completion_request = Some(super::main::CompletionRequest::CompleteWord);
}
fn widget_expand_word(zle: &mut Zle) {
// Port of expandword() from Src/Zle/zle_tricky.c — runs only the
// expansion phase (history, glob, parameter, brace) without falling
// through to completion.
zle.completion_request = Some(super::main::CompletionRequest::ExpandWord);
}
fn widget_list_choices(zle: &mut Zle) {
// Port of listchoices() from Src/Zle/zle_tricky.c — shows matches
// without inserting.
zle.completion_request = Some(super::main::CompletionRequest::ListChoices);
}
fn widget_menu_complete(zle: &mut Zle) {
// Port of menucomplete() from Src/Zle/zle_tricky.c — enters/steps
// the menu-selection state.
zle.completion_request = Some(super::main::CompletionRequest::MenuComplete);
}
// Vi mode widgets
fn widget_vi_cmd_mode(zle: &mut Zle) {
// Port of vicmdmode() from Src/Zle/zle_vi.c. ESC out of insert →
// command mode; cursor steps back one (vim convention) since vi
// command mode treats the cursor as ON a char rather than between.
zle.keymaps.select("vicmd");
if zle.zlecs > 0 {
zle.zlecs -= 1;
}
zle.resetneeded = true;
}
fn widget_vi_insert(zle: &mut Zle) {
// Port of viinsert() from Src/Zle/zle_vi.c:355.
zle.keymaps.select("viins");
zle.insmode = true;
}
fn widget_vi_insert_bol(zle: &mut Zle) {
// Port of viinsertbol() from Src/Zle/zle_vi.c:374. Vim's `I` —
// first-non-blank of current line, then enter insert mode.
zle.keymaps.select("viins");
zle.insmode = true;
let bol = zle.find_bol(zle.zlecs);
let mut p = bol;
while p < zle.zlell && zle.zleline[p].is_whitespace() && zle.zleline[p] != '\n' {
p += 1;
}
zle.zlecs = p;
zle.resetneeded = true;
}
fn widget_vi_add_next(zle: &mut Zle) {
// Port of viaddnext() from Src/Zle/zle_vi.c:336. Vim's `a` —
// step right one then enter insert mode (so insert lands AFTER
// the cursor's current char).
zle.keymaps.select("viins");
zle.insmode = true;
if zle.zlecs < zle.find_eol(zle.zlecs) {
zle.zlecs += 1;
}
zle.resetneeded = true;
}
fn widget_vi_add_eol(zle: &mut Zle) {
// Port of viaddeol() from Src/Zle/zle_vi.c:346. Vim's `A` —
// jump to end-of-line then enter insert mode.
zle.keymaps.select("viins");
zle.insmode = true;
zle.zlecs = zle.find_eol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_vi_forward_char(zle: &mut Zle) {
// Port of viforwardchar() from Src/Zle/zle_move.c:653. Vim's `l`
// — count-aware, can't cross EoL (cursor lands on the last char
// of the current logical line at most).
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_vi_backward_char(zle);
zle.mult = n;
return;
}
let eol = zle.find_eol(zle.zlecs);
let limit = eol.saturating_sub(1);
while zle.zlecs < limit && n > 0 {
zle.zlecs += 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_vi_backward_char(zle: &mut Zle) {
// Port of vibackwardchar() from Src/Zle/zle_move.c:683. Vim's `h`
// — count-aware, can't cross BoL.
let mut n = zle.mult;
if n < 0 {
zle.mult = -n;
widget_vi_forward_char(zle);
zle.mult = n;
return;
}
let bol = zle.find_bol(zle.zlecs);
while zle.zlecs > bol && n > 0 {
zle.zlecs -= 1;
n -= 1;
}
zle.resetneeded = true;
}
fn widget_vi_forward_word(zle: &mut Zle) {
// Port of viforwardword() from Src/Zle/zle_word.c. Vim's `w` —
// routes to forward-word with the iword class definition.
widget_forward_word(zle);
}
fn widget_vi_forward_word_end(zle: &mut Zle) {
// Port of viforwardwordend() from Src/Zle/zle_word.c. Vim's `e` —
// step right one, skip non-word, then walk word chars but land on
// the LAST word char (peek-ahead pattern). Count-aware.
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs < zle.zlell {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell && !is_word_char(zle.zleline[zle.zlecs]) {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell.saturating_sub(1)
&& is_word_char(zle.zleline[zle.zlecs + 1])
{
zle.zlecs += 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_forward_blank_word(zle: &mut Zle) {
// Port of viforwardblankword() from Src/Zle/zle_word.c. Vim's `W` —
// whitespace-only word boundary (no iword class distinction).
let n = zle.mult.max(1);
for _ in 0..n {
while zle.zlecs < zle.zlell && !zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_forward_blank_word_end(zle: &mut Zle) {
// Port of viforwardblankwordend() from Src/Zle/zle_word.c. Vim's
// `E` — whitespace-only end-of-word.
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs < zle.zlell {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
while zle.zlecs < zle.zlell.saturating_sub(1)
&& !zle.zleline[zle.zlecs + 1].is_whitespace()
{
zle.zlecs += 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_backward_word(zle: &mut Zle) {
// Port of vibackwardword() from Src/Zle/zle_word.c. Vim's `b`.
widget_backward_word(zle);
}
fn widget_vi_backward_blank_word(zle: &mut Zle) {
// Port of vibackwardblankword() from Src/Zle/zle_word.c. Vim's `B`.
let n = zle.mult.max(1);
for _ in 0..n {
while zle.zlecs > 0 && zle.zleline[zle.zlecs - 1].is_whitespace() {
zle.zlecs -= 1;
}
while zle.zlecs > 0 && !zle.zleline[zle.zlecs - 1].is_whitespace() {
zle.zlecs -= 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_delete(zle: &mut Zle) {
// Port of videlete() from Src/Zle/zle_vi.c:384.
let _ = zle.vi_delete_op();
}
fn widget_vi_delete_char(zle: &mut Zle) {
// Port of videletechar() from Src/Zle/zle_vi.c:405. Vim's `x`
// command — same as delete-char but C source clamps the count to
// findeol-zlecs to avoid spilling past the current line. We let
// delete-char run with EoB clamp; for vi-aware line-bounded
// semantics, callers should use vi_delete_op('l').
widget_delete_char(zle);
}
fn widget_vi_backward_delete_char(zle: &mut Zle) {
// Port of vibackwarddeletechar() from Src/Zle/zle_vi.c. Vim's `X`.
widget_backward_delete_char(zle);
}
fn widget_vi_change(zle: &mut Zle) {
// Port of vichange() from Src/Zle/zle_vi.c:438.
let _ = zle.vi_change_op();
}
fn widget_vi_change_eol(zle: &mut Zle) {
// Port of vichangeeol() from Src/Zle/zle_vi.c:482. Vim's `C` —
// kill from cursor to EoL, enter insert mode.
widget_kill_line(zle);
widget_vi_insert(zle);
}
fn widget_vi_kill_eol(zle: &mut Zle) {
// Port of vikilleol() from Src/Zle/zle_vi.c. Vim's `D` — kill
// from cursor to EoL without entering insert.
widget_kill_line(zle);
}
fn widget_vi_yank(zle: &mut Zle) {
// Port of viyank() from Src/Zle/zle_vi.c:507.
let _ = zle.vi_yank_op();
}
fn widget_vi_yank_whole_line(zle: &mut Zle) {
// Port of viyankwholeline() from Src/Zle/zle_vi.c:550. Vim's `Y` —
// yank `mult` whole lines into the kill ring (with the trailing
// newline included so vi-put-after / vi-put-before recognise this
// as a line-wise yank). C source: zle_vi.c:559 walks zlecs through
// findeol+1 to capture each line.
let n = zle.mult.max(1);
let bol = zle.find_bol(zle.zlecs);
let mut end = bol;
for _ in 0..n {
end = zle.find_eol(end);
if end < zle.zlell {
end += 1; // include trailing '\n'
} else {
break;
}
}
let region: Vec<char> = zle.zleline[bol..end].to_vec();
if !region.is_empty() {
zle.killring.push_front(region);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
}
}
fn widget_vi_put_after(zle: &mut Zle) {
// Port of viputafter() from Src/Zle/zle_misc.c. Vim's `p` command —
// for character-wise paste, insert AFTER the cursor; for line-wise
// paste (when the kill-ring entry contains a trailing '\n'), insert
// on a new line below. Cursor lands on the LAST char of the pasted
// text. The C source distinguishes line vs char ranges via the
// CUTBUFFER_LINE flag on the cutbuf; we approximate by checking
// whether the most-recent kill-ring entry ends in a newline.
let is_line_paste = zle
.killring
.front()
.and_then(|v| v.last().copied())
== Some('\n');
if is_line_paste {
// Move to end of current line, then paste (which inserts the
// newline-prefixed content immediately).
zle.zlecs = zle.find_eol(zle.zlecs);
if zle.zlecs < zle.zlell {
zle.zlecs += 1;
}
widget_yank(zle);
} else {
if zle.zlecs < zle.zlell {
zle.zlecs += 1;
}
widget_yank(zle);
// Vim convention: cursor on last pasted char, not after.
if zle.zlecs > 0 {
zle.zlecs -= 1;
}
}
}
fn widget_vi_put_before(zle: &mut Zle) {
// Port of viputbefore() from Src/Zle/zle_misc.c. Vim's `P` command —
// char-wise paste BEFORE the cursor; line-wise paste opens a new
// line ABOVE. Cursor lands on the last char of the pasted region.
let is_line_paste = zle
.killring
.front()
.and_then(|v| v.last().copied())
== Some('\n');
if is_line_paste {
// Move to start of current line, paste (the newline at end of
// the kill-ring entry pushes the existing line down).
zle.zlecs = zle.find_bol(zle.zlecs);
widget_yank(zle);
} else {
widget_yank(zle);
if zle.zlecs > 0 {
zle.zlecs -= 1;
}
}
}
fn widget_vi_replace(zle: &mut Zle) {
// Port of vireplace() from Src/Zle/zle_vi.c (the `R` command).
// Switch to insert keymap with overwrite mode so subsequent self-
// inserts replace existing chars instead of pushing them right.
zle.keymaps.select("viins");
zle.insmode = false;
}
fn widget_vi_replace_chars(zle: &mut Zle) {
// Port of vireplacechars() from Src/Zle/zle_vi.c (the `r` command).
// Read one char and overwrite the char under the cursor with it.
// The C source supports a numeric prefix (`3rX` replaces the next
// 3 chars all with X); replicated below.
if let Some(c) = zle.getfullchar(true) {
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs >= zle.zlell {
break;
}
zle.zleline[zle.zlecs] = c;
zle.zlecs += 1;
}
// Vim convention: cursor lands on the last replaced char.
if zle.zlecs > 0 {
zle.zlecs -= 1;
}
zle.resetneeded = true;
}
}
fn widget_vi_substitute(zle: &mut Zle) {
// Port of visubstitute() from Src/Zle/zle_vi.c:455. Delete `mult`
// chars then enter insert mode — vim's `s` command.
let n = zle.mult.max(1);
for _ in 0..n {
widget_delete_char(zle);
}
widget_vi_insert(zle);
}
fn widget_vi_change_whole_line(zle: &mut Zle) {
// Port of vichangewholeline() from Src/Zle/zle_vi.c:499 (the `S`
// command). Kill the whole line, then enter insert mode.
widget_kill_whole_line(zle);
widget_vi_insert(zle);
}
fn widget_vi_first_non_blank(zle: &mut Zle) {
// Port of vifirstnonblank() from Src/Zle/zle_move.c:862. Move
// cursor to the first non-blank character on the current line.
let bol = zle.find_bol(zle.zlecs);
let mut p = bol;
while p < zle.zlell && zle.zleline[p].is_whitespace() && zle.zleline[p] != '\n' {
p += 1;
}
zle.zlecs = p;
zle.resetneeded = true;
}
fn widget_vi_end_of_line(zle: &mut Zle) {
// Port of viendofline() from Src/Zle/zle_move.c:708. Vim's `$`
// semantics — cursor lands on the last char of the line, not past it.
let eol = zle.find_eol(zle.zlecs);
if eol > 0 && (eol == zle.zlell || zle.zleline[eol] == '\n') {
zle.zlecs = eol.saturating_sub(1);
} else {
zle.zlecs = eol;
}
zle.resetneeded = true;
}
fn widget_vi_digit_or_beginning_of_line(zle: &mut Zle) {
// Port of vidigitorbeginningofline() from Src/Zle/zle_vi.c. With
// an active numeric prefix the `0` key acts as a digit; otherwise
// it's beginning-of-line.
if zle.zmod.flags.contains(super::main::ModifierFlags::MULT) {
widget_digit_argument(zle);
} else {
widget_beginning_of_line(zle);
}
}
fn widget_vi_open_line_below(zle: &mut Zle) {
// Port of viopenlinebelow() from Src/Zle/zle_vi.c (the `o` command).
// Move to end of line, insert newline, enter insert mode.
zle.zlecs = zle.find_eol(zle.zlecs);
zle.self_insert('\n');
widget_vi_insert(zle);
}
fn widget_vi_open_line_above(zle: &mut Zle) {
// Port of viopenlineabove() from Src/Zle/zle_vi.c (the `O` command).
// Move to start of line, insert newline, step back, enter insert.
zle.zlecs = zle.find_bol(zle.zlecs);
zle.self_insert('\n');
if zle.zlecs > 0 {
zle.zlecs -= 1;
}
widget_vi_insert(zle);
}
fn widget_vi_join(zle: &mut Zle) {
// Port of vijoin() from Src/Zle/zle_misc.c (the `J` command).
// Find the newline at or after the cursor, remove it, and insert
// a separator space (unless the newline was at end-of-buffer or
// the next char was already whitespace). The C source also
// collapses any leading whitespace on the joined line — replicated
// here by skipping spaces after the removed newline.
let n = zle.mult.max(1);
for _ in 0..n {
let mut pos = zle.zlecs;
while pos < zle.zlell && zle.zleline[pos] != '\n' {
pos += 1;
}
if pos >= zle.zlell {
break;
}
// Remove the newline.
zle.zleline.remove(pos);
zle.zlell = zle.zleline.len();
// Eat leading whitespace on the joined line (vim convention).
while pos < zle.zlell && zle.zleline[pos] == ' ' {
zle.zleline.remove(pos);
zle.zlell = zle.zleline.len();
}
// Insert a single space separator if the join didn't already
// bridge two non-space chars at a sentence boundary.
if pos > 0
&& pos <= zle.zlell
&& zle.zleline.get(pos - 1).copied() != Some(' ')
&& pos < zle.zlell
{
zle.zleline.insert(pos, ' ');
zle.zlell += 1;
}
zle.zlecs = pos;
}
zle.resetneeded = true;
}
fn widget_vi_repeat_change(zle: &mut Zle) {
// Port of virepeatchange() from Src/Zle/zle_vi.c. Replays the keys in
// vi_chg_buf — but the recording side (which captures keystrokes during
// d/c/y operators into vi_chg_buf) is still pending, so without a
// recorded change the buffer is empty and the widget is a no-op.
// This matches zsh's behavior when no change has been made yet.
if zle.vi_chg_buf.is_empty() {
return;
}
// Re-feed the recorded keys via ungetbytes so the next iteration of
// zlecore re-runs them. ungetbytes prepends to the input buffer so the
// bytes will be consumed before any new keystrokes.
let bytes = zle.vi_chg_buf.clone();
zle.ungetbytes(&bytes);
}
fn widget_vi_find_next_char(zle: &mut Zle) {
// Port of vifindnextchar() from Src/Zle/zle_move.c:739.
zle.vi_find_char(true, false);
}
fn widget_vi_find_prev_char(zle: &mut Zle) {
// Port of vifindprevchar() from Src/Zle/zle_move.c:751.
zle.vi_find_char(false, false);
}
fn widget_vi_find_next_char_skip(zle: &mut Zle) {
// Port of vifindnextcharskip() from Src/Zle/zle_move.c:763.
zle.vi_find_char(true, true);
}
fn widget_vi_find_prev_char_skip(zle: &mut Zle) {
// Port of vifindprevcharskip() from Src/Zle/zle_move.c:775.
zle.vi_find_char(false, true);
}
fn widget_vi_repeat_find(zle: &mut Zle) {
// Port of virepeatfind() from Src/Zle/zle_move.c:835.
let _ = zle.vi_repeat_find();
}
fn widget_vi_rev_repeat_find(zle: &mut Zle) {
// Port of virevrepeatfind() from Src/Zle/zle_move.c:842.
let _ = zle.vi_rev_repeat_find();
}
fn widget_vi_history_search_forward(zle: &mut Zle) {
// Port of vihistorysearchforward() from Src/Zle/zle_hist.c.
// Read the search pattern starting from `?` then run a forward history search.
// For now: re-run the last srch_str if any.
let pat = match zle.srch_str.clone() {
Some(s) if !s.is_empty() => s,
_ => return,
};
let len = zle.history.entries.len();
let start = zle.history.cursor + 1;
for i in start..len {
if zle.history.entries[i].line.contains(&pat) {
if zle.history.saved_line.is_none() {
zle.history.saved_line = Some(zle.zleline.clone());
zle.history.saved_cs = zle.zlecs;
}
zle.history.cursor = i;
zle.zleline = zle.history.entries[i].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = 0;
zle.resetneeded = true;
return;
}
}
}
fn widget_vi_history_search_backward(zle: &mut Zle) {
// Port of vihistorysearchbackward() from Src/Zle/zle_hist.c.
let pat = match zle.srch_str.clone() {
Some(s) if !s.is_empty() => s,
_ => return,
};
if zle.history.cursor == 0 {
return;
}
let mut i = zle.history.cursor.min(zle.history.entries.len()).saturating_sub(1);
loop {
if zle.history.entries[i].line.contains(&pat) {
if zle.history.saved_line.is_none() {
zle.history.saved_line = Some(zle.zleline.clone());
zle.history.saved_cs = zle.zlecs;
}
zle.history.cursor = i;
zle.zleline = zle.history.entries[i].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = 0;
zle.resetneeded = true;
return;
}
if i == 0 {
break;
}
i -= 1;
}
}
fn widget_vi_repeat_search(zle: &mut Zle) {
// Port of virepeatsearch() from Src/Zle/zle_hist.c.
// Replays the last vi search in the same direction.
let mut hist = std::mem::take(&mut zle.history);
zle.vi_repeat_search(&mut hist);
zle.history = hist;
}
fn widget_vi_rev_repeat_search(zle: &mut Zle) {
// Port of virevrepeatsearch() from Src/Zle/zle_hist.c.
let mut hist = std::mem::take(&mut zle.history);
zle.vi_rev_repeat_search(&mut hist);
zle.history = hist;
}
fn widget_vi_fetch_history(zle: &mut Zle) {
// Port of vifetchhistory() from Src/Zle/zle_hist.c:1787.
// With no count: jump to the live (newest) entry. With a count: load
// that history event by 1-based index. Negative count is rejected.
if zle.mult < 0 {
return;
}
let has_mult = zle.zmod.flags.contains(super::main::ModifierFlags::MULT);
let on_live = zle.history.cursor >= zle.history.entries.len();
if on_live || zle.zlereadflags.no_history {
if !has_mult {
zle.zlecs = zle.zlell;
zle.zlecs = zle.find_bol(zle.zlecs);
zle.resetneeded = true;
return;
}
if zle.zlereadflags.no_history {
return;
}
}
let target_idx_1: i32 = if has_mult {
zle.zmod.mult
} else {
zle.history.entries.len() as i32
};
if target_idx_1 < 1 {
return;
}
let target_idx = (target_idx_1 - 1) as usize;
if target_idx >= zle.history.entries.len() {
return;
}
if zle.history.saved_line.is_none() && on_live {
zle.history.saved_line = Some(zle.zleline.clone());
zle.history.saved_cs = zle.zlecs;
}
zle.history.cursor = target_idx;
zle.zleline = zle.history.entries[target_idx].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = 0;
zle.resetneeded = true;
}
fn widget_vi_goto_column(zle: &mut Zle) {
// Port of vigotocolumn() from Src/Zle/zle_move.c. Vim's `|` —
// jump to column N (1-based) on the current logical line. The
// count is in zmod.mult; cursor lands at bol + (mult - 1),
// clamped to the line's EoL.
let col = zle.zmod.mult.saturating_sub(1) as usize;
let bol = zle.find_bol(zle.zlecs);
let eol = zle.find_eol(zle.zlecs);
zle.zlecs = (bol + col).min(eol);
zle.resetneeded = true;
}
fn widget_vi_backward_kill_word(zle: &mut Zle) {
// Port of vibackwardkillword() from Src/Zle/zle_word.c. Vim's
// ^W in insert mode — same as backward-kill-word but specifically
// bound for the vi insert keymap.
widget_backward_kill_word(zle);
}
fn widget_digit_argument(zle: &mut Zle) {
// Port of digitargument() from Src/Zle/zle_misc.c:950 plus the
// parsedigit() helper at zle_misc.c:919. Accepts a digit in the
// current zmod.base (10 by default; up to 36 honours a-z / A-Z),
// and accumulates it into zmod.tmult with sign tracking. After
// neg-argument has fired (MOD_NEG set), the first digit replaces
// the placeholder -1 so `M-- 5` ends up as -5, matching C zsh.
let base = zle.zmod.base;
let new_digit = parse_digit_in_base(zle.lastchar as u8, base);
if new_digit < 0 {
zle.handle_feep();
return;
}
let sign = if zle.zmod.mult < 0 { -1 } else { 1 };
if !zle
.zmod
.flags
.contains(crate::zle::main::ModifierFlags::TMULT)
{
zle.zmod.tmult = 0;
}
if zle.zmod.flags.contains(crate::zle::main::ModifierFlags::NEG) {
zle.zmod.tmult = sign * new_digit;
zle.zmod.flags.remove(crate::zle::main::ModifierFlags::NEG);
} else {
zle.zmod.tmult = zle.zmod.tmult * base + sign * new_digit;
}
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::TMULT);
zle.prefixflag = true;
}
fn widget_undefined(zle: &mut Zle) {
// Port of undefinedkey() from Src/Zle/zle_main.c. zsh dispatches
// here when a key has no binding in the current keymap; the C
// source just beeps. We do the same by ignoring `zle` (handle_feep
// would also work, but the zlecore's no-binding fallback already
// calls handle_feep before reaching this — calling it again would
// double-beep).
let _ = zle;
}
/// Parse a single byte as a digit in `base`. Returns `-1` if the byte
/// isn't a valid digit in that base.
/// Port of `parsedigit()` from Src/Zle/zle_misc.c:919. Used by
/// digit-argument and universal-argument to honour zmod.base ∈ [2, 36].
fn parse_digit_in_base(b: u8, base: i32) -> i32 {
let inkey = b as i32 & 0x7f;
if base > 10 {
if (b'a' as i32..b'a' as i32 + base - 10).contains(&inkey) {
return inkey - b'a' as i32 + 10;
}
if (b'A' as i32..b'A' as i32 + base - 10).contains(&inkey) {
return inkey - b'A' as i32 + 10;
}
if (b'0' as i32..=b'9' as i32).contains(&inkey) {
return inkey - b'0' as i32;
}
return -1;
}
if (b'0' as i32..b'0' as i32 + base).contains(&inkey) {
inkey - b'0' as i32
} else {
-1
}
}
// =============================================================================
// Section: misc widget ports added after the initial table — every widget
// body in this section cites the C source it ports. Bodies may delegate to
// existing Zle methods or inline the small ones; either way the docstring
// pins the Src/Zle/*.c origin.
// =============================================================================
fn widget_beep(zle: &mut Zle) {
// Port of beep / handlefeep() from Src/Zle/zle_utils.c. Just emits the
// bell — `handle_feep` already does the right thing.
zle.handle_feep();
}
fn widget_set_mark_command(zle: &mut Zle) {
// Port of setmarkcommand() from Src/Zle/zle_move.c:483. Negative count
// disables the region; otherwise marks the current cursor and turns
// on the visual region (charwise).
if zle.mult < 0 {
zle.region_active = 0;
return;
}
zle.mark = zle.zlecs;
zle.region_active = 1;
}
fn widget_exchange_point_and_mark(zle: &mut Zle) {
// Port of exchangepointandmark() from Src/Zle/zle_move.c:496. With
// mult==0 the C source just turns the region on without swapping;
// with mult>0 swaps cursor↔mark and clamps cursor.
if zle.mult == 0 {
zle.region_active = 1;
return;
}
let new_cs = zle.mark;
zle.mark = zle.zlecs;
zle.zlecs = new_cs.min(zle.zlell);
if zle.mult > 0 {
zle.region_active = 1;
}
zle.resetneeded = true;
}
fn widget_deactivate_region(zle: &mut Zle) {
// Port of deactivateregion() from Src/Zle/zle_move.c:564.
zle.vi_deactivate_region();
}
fn widget_visual_mode(zle: &mut Zle) {
// Port of visualmode() from Src/Zle/zle_move.c:516.
zle.vi_visual_mode();
}
fn widget_visual_line_mode(zle: &mut Zle) {
// Port of visuallinemode() from Src/Zle/zle_move.c:540.
zle.vi_visual_line_mode();
}
fn widget_capitalize_word(zle: &mut Zle) {
// Port of capitalizeword() from Src/Zle/zle_misc.c. Method already
// exists on Zle; this is the dispatch entry.
zle.capitalize_word();
}
fn widget_down_case_word(zle: &mut Zle) {
// Port of downcaseword() from Src/Zle/zle_misc.c.
zle.downcase_word();
}
fn widget_up_case_word(zle: &mut Zle) {
// Port of upcaseword() from Src/Zle/zle_misc.c.
zle.upcase_word();
}
fn widget_pound_insert(zle: &mut Zle) {
// Port of poundinsert() from Src/Zle/zle_misc.c:369. Toggle a leading
// `#` on every logical line so the entire input is commented out
// (or uncommented). Common keybinding: M-#.
zle.zlecs = 0;
let toggle_off = zle.zleline.first().copied() == Some('#');
if toggle_off {
// Walk every logical line, removing one leading '#' if present
// (C source: zle_misc.c:384-394).
let mut p = 0;
loop {
let bol = zle.find_bol(p);
if zle.zleline.get(bol).copied() == Some('#') {
zle.zleline.remove(bol);
if zle.zlell > 0 {
zle.zlell -= 1;
}
}
let eol = zle.find_eol(bol);
if eol >= zle.zlell {
break;
}
p = eol + 1;
}
} else {
// Insert '#' at start of every logical line (zle_misc.c:373-383).
let mut p = 0;
loop {
let bol = zle.find_bol(p);
zle.zleline.insert(bol, '#');
zle.zlell += 1;
let eol = zle.find_eol(bol);
if eol >= zle.zlell {
break;
}
p = eol + 1;
}
}
zle.zlecs = 0;
zle.done = true; // C zsh accepts the line after a pound-insert.
}
fn widget_quote_line(zle: &mut Zle) {
// Port of quoteline() from Src/Zle/zle_misc.c:1187. Wrap the entire
// buffer in single quotes, escaping any embedded single quote as
// `'\''` (the C source's makequote routine).
let inner: String = zle.zleline.iter().collect();
let escaped = inner.replace('\'', r"'\''");
let new_line = format!("'{}'", escaped);
zle.zleline = new_line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = zle.zlell;
zle.resetneeded = true;
}
fn widget_quote_region(zle: &mut Zle) {
// Port of quoteregion() from Src/Zle/zle_misc.c:1152. Wrap the
// currently-selected region (mark..zlecs, normalised) in single
// quotes with embedded-quote escaping.
let (lo, hi) = if zle.mark <= zle.zlecs {
(zle.mark, zle.zlecs)
} else {
(zle.zlecs, zle.mark)
};
let lo = lo.min(zle.zlell);
let hi = hi.min(zle.zlell);
if hi <= lo {
return;
}
let inner: String = zle.zleline[lo..hi].iter().collect();
let escaped = inner.replace('\'', r"'\''");
let wrapped = format!("'{}'", escaped);
let wrapped_chars: Vec<char> = wrapped.chars().collect();
zle.zleline.splice(lo..hi, wrapped_chars.iter().copied());
zle.zlell = zle.zleline.len();
zle.zlecs = lo + wrapped_chars.len();
zle.resetneeded = true;
}
fn widget_copy_region_as_kill(zle: &mut Zle) {
// Port of copyregionaskill() from Src/Zle/zle_misc.c:494. Copies
// mark..zlecs (normalised) onto the kill ring without removing it.
let (lo, hi) = if zle.mark <= zle.zlecs {
(zle.mark, zle.zlecs)
} else {
(zle.zlecs, zle.mark)
};
let lo = lo.min(zle.zlell);
let hi = hi.min(zle.zlell);
if hi <= lo {
return;
}
let region: Vec<char> = zle.zleline[lo..hi].to_vec();
zle.killring.push_front(region);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
}
fn widget_copy_prev_word(zle: &mut Zle) {
// Port of copyprevword() from Src/Zle/zle_misc.c:1066. Inserts the
// previous word (per ZC_iword) at the cursor. The full C version
// walks `zmult` words back; we replicate that by scanning backward
// through `mult` word-boundaries.
let n = zle.mult.max(1) as usize;
let mut end = zle.zlecs;
let mut start;
let mut word: Option<(usize, usize)> = None;
for _ in 0..n {
// Skip whitespace going backward.
while end > 0 && zle.zleline[end - 1].is_whitespace() {
end -= 1;
}
if end == 0 {
break;
}
start = end;
while start > 0 && !zle.zleline[start - 1].is_whitespace() {
start -= 1;
}
word = Some((start, end));
end = start;
}
if let Some((s, e)) = word {
let copied: Vec<char> = zle.zleline[s..e].to_vec();
for (i, c) in copied.iter().enumerate() {
zle.zleline.insert(zle.zlecs + i, *c);
}
zle.zlecs += copied.len();
zle.zlell = zle.zleline.len();
zle.resetneeded = true;
}
}
fn widget_transpose_words(zle: &mut Zle) {
// Port of transposewords() from Src/Zle/zle_word.c:652. The C source
// is a multi-step pointer dance; this Rust port recreates the
// common-case behavior: swap the two whitespace-separated words
// around (or before) the cursor. Multi-line + edge-case handling
// matches the C pattern of "fall back to nearest two prior words"
// when the cursor is past the last word on the line.
let n = zle.zlell;
if n == 0 {
return;
}
// Find the word containing or following the cursor (`p4` in C).
let mut p4 = zle.zlecs.min(n);
while p4 < n && !zle.zleline[p4].is_alphanumeric() && zle.zleline[p4] != '_' {
p4 += 1;
}
// If we landed past EOL, slide back to find the prior word.
if p4 == n {
let mut x = zle.zlecs;
while x > 0 && (!zle.zleline[x - 1].is_alphanumeric() && zle.zleline[x - 1] != '_') {
x -= 1;
}
if x == 0 {
return;
}
p4 = x;
}
let p3 = {
let mut x = p4;
while x < n && (zle.zleline[x].is_alphanumeric() || zle.zleline[x] == '_') {
x += 1;
}
x
};
let p4 = {
let mut x = p4;
while x > 0 && (zle.zleline[x - 1].is_alphanumeric() || zle.zleline[x - 1] == '_') {
x -= 1;
}
x
};
let p2 = {
let mut x = p4;
while x > 0 && !zle.zleline[x - 1].is_alphanumeric() && zle.zleline[x - 1] != '_' {
x -= 1;
}
x
};
let p1 = {
let mut x = p2;
while x > 0 && (zle.zleline[x - 1].is_alphanumeric() || zle.zleline[x - 1] == '_') {
x -= 1;
}
x
};
if p1 == p2 || p4 == p3 {
return;
}
let word1: Vec<char> = zle.zleline[p1..p2].to_vec();
let word2: Vec<char> = zle.zleline[p4..p3].to_vec();
let mut new_buf: Vec<char> = Vec::with_capacity(zle.zlell);
new_buf.extend_from_slice(&zle.zleline[..p1]);
new_buf.extend_from_slice(&word2);
new_buf.extend_from_slice(&zle.zleline[p2..p4]);
new_buf.extend_from_slice(&word1);
new_buf.extend_from_slice(&zle.zleline[p3..]);
zle.zleline = new_buf;
zle.zlell = zle.zleline.len();
zle.zlecs = p1 + word2.len() + (p4 - p2) + word1.len();
zle.resetneeded = true;
}
fn widget_history_beginning_search_backward(zle: &mut Zle) {
// Port of historybeginningsearchbackward() from Src/Zle/zle_hist.c:2039.
// Searches history for entries that start with the text *before* the
// cursor (the prefix), keeping the cursor where it is on a match.
let prefix: String = zle.zleline[..zle.zlecs.min(zle.zleline.len())]
.iter()
.collect();
if zle.history.cursor == 0 {
return;
}
let saved_cs = zle.zlecs;
let mut i = zle.history.cursor.min(zle.history.entries.len()).saturating_sub(1);
loop {
if zle.history.entries[i].line.starts_with(&prefix) {
if zle.history.saved_line.is_none() {
zle.history.saved_line = Some(zle.zleline.clone());
zle.history.saved_cs = saved_cs;
}
zle.history.cursor = i;
zle.zleline = zle.history.entries[i].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = saved_cs.min(zle.zlell);
zle.resetneeded = true;
return;
}
if i == 0 {
break;
}
i -= 1;
}
}
fn widget_history_beginning_search_forward(zle: &mut Zle) {
// Port of historybeginningsearchforward() — same shape as the
// backward variant (zle_hist.c:2039 area) but stepping forward.
let prefix: String = zle.zleline[..zle.zlecs.min(zle.zleline.len())]
.iter()
.collect();
let saved_cs = zle.zlecs;
let len = zle.history.entries.len();
for i in (zle.history.cursor + 1)..len {
if zle.history.entries[i].line.starts_with(&prefix) {
zle.history.cursor = i;
zle.zleline = zle.history.entries[i].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = saved_cs.min(zle.zlell);
zle.resetneeded = true;
return;
}
}
}
fn widget_beginning_of_history(zle: &mut Zle) {
// Port of beginningofhistory() from Src/Zle/zle_hist.c:464.
let mut hist = std::mem::take(&mut zle.history);
zle.beginning_of_history(&mut hist);
zle.history = hist;
}
fn widget_end_of_history(zle: &mut Zle) {
// Port of endofhistory() from Src/Zle/zle_hist.c:478.
let mut hist = std::mem::take(&mut zle.history);
zle.end_of_history(&mut hist);
zle.history = hist;
}
fn widget_push_line(zle: &mut Zle) {
// Port of pushline() from Src/Zle/zle_hist.c:832.
zle.push_line();
zle.done = true;
}
fn widget_describe_key_briefly(zle: &mut Zle) {
// Port of describekeybriefly() from Src/Zle/zle_thingy.c. Existing
// method on Zle handles the input read + lookup loop.
zle.describe_key_briefly();
}
fn widget_delete_word(zle: &mut Zle) {
// Port of deleteword() from Src/Zle/zle_word.c. Like kill-word but
// doesn't put the deleted text on the kill ring.
let saved_cs = zle.zlecs;
let end = zle.find_word_end(super::word::WordStyle::Emacs);
if end > saved_cs {
zle.zleline.drain(saved_cs..end);
zle.zlell = zle.zleline.len();
}
zle.resetneeded = true;
}
fn widget_backward_delete_word(zle: &mut Zle) {
// Port of backwarddeleteword() from Src/Zle/zle_word.c. Like
// backward-kill-word but no kill-ring update.
let end = zle.zlecs;
let start = zle.find_word_start(super::word::WordStyle::Emacs);
if end > start {
zle.zleline.drain(start..end);
zle.zlell = zle.zleline.len();
zle.zlecs = start;
}
zle.resetneeded = true;
}
fn widget_emacs_forward_word(zle: &mut Zle) {
// Port of emacsforwardword() from Src/Zle/zle_word.c — same as
// forward-word in emacs style; explicit name binding for users who
// want it independent of the global word style.
zle.zlecs = zle.find_word_end(super::word::WordStyle::Emacs);
zle.resetneeded = true;
}
fn widget_emacs_backward_word(zle: &mut Zle) {
// Port of emacsbackwardword() from Src/Zle/zle_word.c.
zle.zlecs = zle.find_word_start(super::word::WordStyle::Emacs);
zle.resetneeded = true;
}
fn widget_kill_region(zle: &mut Zle) {
// Port of killregion() from Src/Zle/zle_misc.c. Drains the region
// (mark..zlecs, normalised) into the kill ring and removes it.
let (lo, hi) = if zle.mark <= zle.zlecs {
(zle.mark, zle.zlecs)
} else {
(zle.zlecs, zle.mark)
};
let lo = lo.min(zle.zlell);
let hi = hi.min(zle.zlell);
if hi <= lo {
return;
}
let removed: Vec<char> = zle.zleline[lo..hi].to_vec();
zle.zleline.drain(lo..hi);
zle.zlell = zle.zleline.len();
zle.zlecs = lo;
zle.killring.push_front(removed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
fn widget_kill_buffer(zle: &mut Zle) {
// Port of killbuffer() from Src/Zle/zle_misc.c. Cuts the entire line
// to the kill ring.
if zle.zlell == 0 {
return;
}
let killed: Vec<char> = zle.zleline.clone();
zle.zleline.clear();
zle.zlell = 0;
zle.zlecs = 0;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
fn widget_vi_set_mark_widget(zle: &mut Zle) {
// Port of visetmark() from Src/Zle/zle_move.c:872. Reads the next
// char as the mark name and stores it in vi_marks via the existing
// Zle::vi_set_mark method.
if let Some(c) = zle.getfullchar(false) {
zle.vi_set_mark(c);
}
}
fn widget_vi_goto_mark_widget(zle: &mut Zle) {
// Port of vigotomark() from Src/Zle/zle_move.c:887.
if let Some(c) = zle.getfullchar(false) {
zle.vi_goto_mark(c);
}
}
fn widget_vi_goto_mark_line_widget(zle: &mut Zle) {
// Port of vigotomarkline() from Src/Zle/zle_move.c. Same as
// vi-goto-mark but lands at first non-blank of the line containing
// the mark.
if let Some(c) = zle.getfullchar(false) {
zle.vi_goto_mark(c);
// Move to first non-blank of the line we landed on.
let bol = zle.find_bol(zle.zlecs);
let mut p = bol;
while p < zle.zlell && zle.zleline[p].is_whitespace() && zle.zleline[p] != '\n' {
p += 1;
}
zle.zlecs = p;
zle.resetneeded = true;
}
}
fn widget_vi_match_bracket(zle: &mut Zle) {
// Port of vimatchbracket() from Src/Zle/zle_vi.c. Method already
// exists on Zle via vi_match_bracket.
zle.vi_match_bracket();
}
fn widget_vi_caps_lock_panic(zle: &mut Zle) {
// Port of vicapslockpanic() from Src/Zle/zle_vi.c. zsh's joke
// widget: blocks until you press a non-Caps-Lock key. Practical
// port simply beeps once.
zle.handle_feep();
}
fn widget_vi_kill_line(zle: &mut Zle) {
// Port of vikillline() from Src/Zle/zle_vi.c. Kills from cursor
// back to start of line — different from Emacs kill-line which
// kills forward.
let bol = zle.find_bol(zle.zlecs);
if zle.zlecs > bol {
let killed: Vec<char> = zle.zleline.drain(bol..zle.zlecs).collect();
zle.zlell = zle.zleline.len();
zle.zlecs = bol;
zle.killring.push_front(killed);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
zle.resetneeded = true;
}
}
fn widget_vi_yank_eol(zle: &mut Zle) {
// Port of viyankeol() from Src/Zle/zle_vi.c:537. Copies from cursor
// to end of line into the kill ring without removing.
let eol = zle.find_eol(zle.zlecs);
if eol > zle.zlecs {
let region: Vec<char> = zle.zleline[zle.zlecs..eol].to_vec();
zle.killring.push_front(region);
if zle.killring.len() > zle.killringmax {
zle.killring.pop_back();
}
}
}
fn widget_vi_beginning_of_line(zle: &mut Zle) {
// Port of vibeginningofline() from Src/Zle/zle_move.c:728.
zle.zlecs = zle.find_bol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_vi_swap_case(zle: &mut Zle) {
// Port of viswapcase() from Src/Zle/zle_vi.c. Swap the case of
// the char under the cursor and advance one position; repeat
// `mult` times.
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs >= zle.zlell {
break;
}
let c = zle.zleline[zle.zlecs];
let swapped = if c.is_uppercase() {
c.to_lowercase().next().unwrap_or(c)
} else if c.is_lowercase() {
c.to_uppercase().next().unwrap_or(c)
} else {
c
};
zle.zleline[zle.zlecs] = swapped;
if zle.zlecs < zle.zlell {
zle.zlecs += 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_oper_swap_case(zle: &mut Zle) {
// Port of vioperswapcase() from Src/Zle/zle_vi.c. As an operator,
// swaps the case of every char in a vi range. The range read is
// delegated to `vi_get_range('~')` (the C source uses the same
// operator-pending machinery as d/c/y).
if let Some((start, end, _)) = zle.vi_get_range('~') {
for i in start..end.min(zle.zlell) {
let c = zle.zleline[i];
let swapped = if c.is_uppercase() {
c.to_lowercase().next().unwrap_or(c)
} else if c.is_lowercase() {
c.to_uppercase().next().unwrap_or(c)
} else {
c
};
zle.zleline[i] = swapped;
}
zle.zlecs = start;
zle.resetneeded = true;
}
}
fn widget_vi_undo_change(zle: &mut Zle) {
// Port of viundochange() from Src/Zle/zle_vi.c. zsh's vi-undo-change
// walks back to the change boundary recorded at insert-mode entry
// (vistartchange) — undo until cur_change drops below that. Our
// simpler model: just call undo_widget once, matching the common
// behavior of `u` in vi command mode.
let _ = zle.undo_widget();
}
fn widget_universal_argument(zle: &mut Zle) {
// Port of universalargument() from Src/Zle/zle_misc.c:986. The C
// source greedily reads digits (and an optional leading '-') from
// the input stream right after C-u, then applies the result as
// tmult; if no digits follow, multiplies the existing tmult by 4
// (the classic emacs C-u-C-u → 16 chord). The leading '-' branch is
// distinct from neg-argument: it's a single token belonging to this
// widget's read loop. Any non-digit byte gets ungot back.
let mut digcnt = 0;
let mut pref: i32 = 0;
let mut minus: i32 = 1;
let base = zle.zmod.base;
while let Some(b) = zle.getbyte(false) {
if b == b'-' && digcnt == 0 {
minus = -1;
digcnt += 1;
continue;
}
let new_digit = parse_digit_in_base(b, base);
if new_digit >= 0 {
pref = pref * base + new_digit;
digcnt += 1;
} else {
zle.ungetbyte(b);
break;
}
}
if digcnt > 0 {
zle.zmod.tmult = minus * if pref != 0 { pref } else { 1 };
} else {
zle.zmod.tmult = zle.zmod.tmult.saturating_mul(4);
}
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::TMULT);
zle.prefixflag = true;
}
fn widget_neg_argument(zle: &mut Zle) {
// Port of negargument() from Src/Zle/zle_misc.c:974. The C source
// bails (returns 1) if MOD_TMULT is already set — neg-argument is
// only valid as the *first* prefix, not after a digit. Otherwise
// sets tmult = -1 and the MOD_TMULT|MOD_NEG flags so the next
// digit-argument knows to use sign on its first digit.
if zle.zmod.flags.contains(crate::zle::main::ModifierFlags::TMULT) {
zle.handle_feep();
return;
}
zle.zmod.tmult = -1;
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::TMULT);
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::NEG);
zle.prefixflag = true;
}
fn widget_recursive_edit(zle: &mut Zle) {
// Port of recursiveedit() from Src/Zle/zle_main.c. Method already
// exists on Zle via recursive_edit.
let _ = zle.recursive_edit();
}
fn widget_what_cursor_position(zle: &mut Zle) {
// Port of whatcursorposition() from Src/Zle/zle_misc.c. Emits a
// status-line message describing the cursor position. The C source
// formats "Char: X (NNN, 0xHH, 0bBB) Point N of N (PP%) Column N".
// Routed to our `show_msg` so the message lands wherever the host
// surfaces ZLE diagnostics.
let pos = zle.zlecs;
let len = zle.zlell;
let msg = if pos < len {
let c = zle.zleline[pos];
let pct = (pos * 100).checked_div(len).unwrap_or(0);
format!(
"Char: {} ({}, 0x{:X}) Point {} of {} ({}%)",
c, c as u32, c as u32, pos, len, pct
)
} else {
format!("Point {} of {} (end of buffer)", pos, len)
};
zle.show_msg(&msg);
}
fn widget_set_local_history_widget(zle: &mut Zle) {
// Port of setlocalhistory() from Src/Zle/zle_hist.c:794.
let has_mult = zle.zmod.flags.contains(super::main::ModifierFlags::MULT);
let mult = zle.zmod.mult;
let mut hist = std::mem::take(&mut zle.history);
zle.set_local_history(&mut hist, has_mult, mult);
zle.history = hist;
}
fn widget_undefined_key(zle: &mut Zle) {
// Port of undefinedkey() from Src/Zle/zle_main.c. The C source just
// beeps; we route to handle_feep.
zle.handle_feep();
}
fn widget_history_search_backward(zle: &mut Zle) {
// Port of historysearchbackward() from Src/Zle/zle_hist.c. Method
// exists; this is the dispatch entry.
let mut hist = std::mem::take(&mut zle.history);
zle.history_search_backward(&mut hist);
zle.history = hist;
}
fn widget_history_search_forward(zle: &mut Zle) {
// Port of historysearchforward() from Src/Zle/zle_hist.c.
let mut hist = std::mem::take(&mut zle.history);
zle.history_search_forward(&mut hist);
zle.history = hist;
}
fn widget_insert_last_word_widget(zle: &mut Zle) {
// Port of insertlastword() from Src/Zle/zle_hist.c. Method exists;
// this is the dispatch entry.
let hist = std::mem::take(&mut zle.history);
zle.insert_last_word(&hist);
zle.history = hist;
}
fn widget_up_line(zle: &mut Zle) {
// Port of upline() from Src/Zle/zle_hist.c:243. Just the
// multi-line cursor motion — no history fallback.
let _ = zle.upline();
zle.resetneeded = true;
}
fn widget_down_line(zle: &mut Zle) {
// Port of downline() from Src/Zle/zle_hist.c:332.
let _ = zle.downline();
zle.resetneeded = true;
}
fn widget_vi_up_line_or_history(zle: &mut Zle) {
// Port of viuplineorhistory() from Src/Zle/zle_hist.c:302. Same as
// up-line-or-history but lands at the first non-blank.
let _ = zle.up_line_or_history_widget();
let bol = zle.find_bol(zle.zlecs);
let mut p = bol;
while p < zle.zlell && zle.zleline[p].is_whitespace() && zle.zleline[p] != '\n' {
p += 1;
}
zle.zlecs = p;
zle.resetneeded = true;
}
fn widget_vi_down_line_or_history(zle: &mut Zle) {
// Port of vidownlineorhistory() from Src/Zle/zle_hist.c:390.
let _ = zle.down_line_or_history_widget();
let bol = zle.find_bol(zle.zlecs);
let mut p = bol;
while p < zle.zlell && zle.zleline[p].is_whitespace() && zle.zleline[p] != '\n' {
p += 1;
}
zle.zlecs = p;
zle.resetneeded = true;
}
fn widget_up_line_or_search(zle: &mut Zle) {
// Port of uplineorsearch() from Src/Zle/zle_hist.c:312. Try cursor
// motion first; if at top, fall through to history-search-backward.
let ocs = zle.zlecs;
let n = zle.upline();
if n != 0 {
zle.zlecs = ocs;
widget_history_search_backward(zle);
}
}
fn widget_down_line_or_search(zle: &mut Zle) {
// Port of downlineorsearch() from Src/Zle/zle_hist.c:400.
let ocs = zle.zlecs;
let n = zle.downline();
if n != 0 {
zle.zlecs = ocs;
widget_history_search_forward(zle);
}
}
fn widget_beginning_of_line_hist(zle: &mut Zle) {
// Port of beginningoflinehist() from Src/Zle/zle_move.c. Same as
// beginning-of-line at the start of the buffer; otherwise jumps to
// the start of the current logical line.
if zle.zlecs == 0 {
// already at top — could pull older history; for now no-op like
// beginning-of-line at top.
return;
}
zle.zlecs = zle.find_bol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_end_of_line_hist(zle: &mut Zle) {
// Port of endoflinehist() from Src/Zle/zle_move.c.
zle.zlecs = zle.find_eol(zle.zlecs);
zle.resetneeded = true;
}
fn widget_copy_prev_shell_word(zle: &mut Zle) {
// Port of copyprevshellword() from Src/Zle/zle_misc.c:1108. Copies
// the previous shell-word (quoted spans intact) at the cursor —
// uses our shell-word boundary helper from src/zle/word.rs.
let n = zle.mult.max(1) as usize;
let words = super::word::shell_words_for_test(&zle.zleline[..zle.zlell]);
if words.is_empty() {
return;
}
// Find the last word ending at-or-before the cursor.
let mut idx = words.len();
for (i, (s, _e)) in words.iter().enumerate() {
if *s >= zle.zlecs {
idx = i;
break;
}
}
if idx == 0 {
return;
}
// Pick the n-th previous (1-based).
if idx < n {
return;
}
let (s, e) = words[idx - n];
let word: Vec<char> = zle.zleline[s..e].to_vec();
for (i, c) in word.iter().enumerate() {
zle.zleline.insert(zle.zlecs + i, *c);
}
zle.zlecs += word.len();
zle.zlell = zle.zleline.len();
zle.resetneeded = true;
}
fn widget_gosmacs_transpose_chars(zle: &mut Zle) {
// Port of gosmacstransposechars() from Src/Zle/zle_misc.c. Like
// transpose-chars but doesn't advance the cursor afterwards (the
// C source: swaps the two chars before the cursor).
if zle.zlecs < 2 {
return;
}
zle.zleline.swap(zle.zlecs - 1, zle.zlecs - 2);
zle.resetneeded = true;
}
fn widget_reset_prompt(zle: &mut Zle) {
// Port of resetprompt() from Src/Zle/zle_main.c. Already a method on
// Zle (sets resetneeded); call through.
zle.resetprompt();
}
fn widget_split_undo(zle: &mut Zle) {
// Port of splitundo() from Src/Zle/zle_utils.c. Closes any pending
// change record so the next mkundoent starts a fresh entry. Routes
// to setlastline() which snapshots the current line state — the
// C source achieves the same effect by flushing nextchanges.
zle.setlastline();
}
fn widget_argument_base(zle: &mut Zle) {
// Port of argumentbase() from Src/Zle/zle_misc.c:1038. The C source
// takes the requested base from the previous mult (no explicit
// arg), validates it's in [2, 36], stashes it in zmod.base, and
// resets the rest of the modifier so the next digit-argument starts
// fresh in the new base. Useful for `M-2 M-x argument-base M-f f`
// = forward-word x15 in base 16.
let multbase = zle.zmod.mult;
if !(2..=36).contains(&multbase) {
zle.handle_feep();
return;
}
zle.zmod.base = multbase;
zle.zmod.flags = super::main::ModifierFlags::empty();
zle.zmod.mult = 1;
zle.zmod.tmult = 1;
zle.zmod.vibuf = 0;
zle.prefixflag = true;
}
fn widget_infer_next_history(zle: &mut Zle) {
// Port of infernexthistory() from Src/Zle/zle_hist.c. Looks for
// the entry following the most recent match of the current line
// and loads it. Useful when stepping through related commands.
let line: String = zle.zleline.iter().collect();
let len = zle.history.entries.len();
// Search backward for the matching entry.
for i in (0..len).rev() {
if zle.history.entries[i].line == line {
// Found — load the next one.
if i + 1 < len {
zle.history.cursor = i + 1;
zle.zleline = zle.history.entries[i + 1].line.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = zle.zlell;
zle.resetneeded = true;
}
return;
}
}
}
fn widget_accept_and_infer_next_history(zle: &mut Zle) {
// Port of acceptandinfernexthistory() from Src/Zle/zle_hist.c.
// Like accept-line but pre-loads the entry following the most
// recent match for the next prompt.
widget_infer_next_history(zle);
zle.done = true;
}
fn widget_vi_quoted_insert(zle: &mut Zle) {
// Port of viquotedinsert() from Src/Zle/zle_vi.c. Same as
// quoted-insert in our model — read the next char and self-insert
// it literally (existing widget_quoted_insert does this).
widget_quoted_insert(zle);
}
fn widget_run_help(zle: &mut Zle) {
// Port of processcmd() (run-help binding) from Src/Zle/zle_misc.c.
// The C source spawns the run-help function on the current command
// word; we record a hook so the host can dispatch it.
zle.call_hook("run-help", None);
}
fn widget_expand_history(zle: &mut Zle) {
// Port of expandhistory() from Src/Zle/zle_tricky.c:2921. zsh
// walks the line through the history-expansion machinery (`!!`,
// `!$`, `!:0` etc.). Without that engine wired in here, surface
// a hook for the host to satisfy.
zle.call_hook("expand-history", None);
}
fn widget_magic_space(zle: &mut Zle) {
// Port of magicspace() from Src/Zle/zle_tricky.c:2882. The C source
// expands history (via expandhistory above) then self-inserts a
// literal space.
widget_expand_history(zle);
zle.zleline.insert(zle.zlecs, ' ');
zle.zlecs += 1;
zle.zlell += 1;
zle.resetneeded = true;
}
fn widget_spell_word(zle: &mut Zle) {
// Port of spellword() from Src/Zle/zle_tricky.c. Surface as a hook
// — the C source spawns an external speller; the host binds.
zle.call_hook("spell-word", None);
}
fn widget_get_line(zle: &mut Zle) {
// Port of getline() from Src/Zle/zle_hist.c. Pops the most-recent
// bufstack entry into the current line.
if let Some(line) = zle.bufstack.pop() {
let chars: Vec<char> = line.chars().collect();
let new_cs = zle.zlecs.min(chars.len());
// Insert at cursor.
for (i, c) in chars.iter().enumerate() {
zle.zleline.insert(zle.zlecs + i, *c);
}
zle.zlecs = new_cs + chars.len();
zle.zlell = zle.zleline.len();
zle.resetneeded = true;
}
}
fn widget_push_input(zle: &mut Zle) {
// Port of pushinput() from Src/Zle/zle_hist.c. Pushes the entire
// input including any in-progress continuation onto bufstack and
// clears the editor — a superset of push-line that also flushes
// pending PS2 lines. With our single-line model it behaves like
// push-line.
zle.push_line();
}
fn widget_vi_set_buffer(zle: &mut Zle) {
// Port of visetbuffer() from Src/Zle/zle_vi.c. The C source reads
// a vi-buffer name (`"a..z`) and stores it for the next y/d/p.
// Without the full vibuf register dispatch wired here, consume the
// next char and stash it on zmod for later inspection.
if let Some(c) = zle.getfullchar(false) {
if c.is_ascii_lowercase() {
zle.zmod.vibuf = (c as i32) - ('a' as i32);
} else if c.is_ascii_uppercase() {
zle.zmod.vibuf = (c as i32) - ('A' as i32) + 26;
}
zle.prefixflag = true;
}
}
fn widget_vi_indent(zle: &mut Zle) {
// Port of viindent() from Src/Zle/zle_vi.c. Inserts SHWIDTH spaces
// at the start of every logical line in the range read via
// vi_get_range. Defaults to 4 spaces (tab width); zsh's actual
// shiftwidth comes from the SH_WORD_SPLIT family — left as a fixed
// 4 here until the wider option store is wired.
if let Some((start, end, _)) = zle.vi_get_range('>') {
let bol_start = zle.find_bol(start);
let mut p = bol_start;
while p < end && p <= zle.zlell {
for i in 0..4 {
zle.zleline.insert(p + i, ' ');
}
zle.zlell += 4;
p = zle.find_eol(p) + 1;
}
zle.zlecs = bol_start;
zle.resetneeded = true;
}
}
fn widget_vi_unindent(zle: &mut Zle) {
// Port of viunindent() from Src/Zle/zle_vi.c. Removes up to 4
// leading spaces from every logical line in the range.
if let Some((start, end, _)) = zle.vi_get_range('<') {
let bol_start = zle.find_bol(start);
let mut p = bol_start;
while p < end && p <= zle.zlell {
for _ in 0..4 {
if zle.zleline.get(p).copied() == Some(' ') {
zle.zleline.remove(p);
if zle.zlell > 0 {
zle.zlell -= 1;
}
} else {
break;
}
}
p = zle.find_eol(p) + 1;
}
zle.zlecs = bol_start;
zle.resetneeded = true;
}
}
fn widget_bracketed_paste(zle: &mut Zle) {
// Port of bracketedpaste() from Src/Zle/zle_misc.c. The C source
// reads bytes between the bracketed-paste open + close escapes.
// Surface as a hook so the host (which owns the input loop) drains
// and inserts the text — host-driven because the paste sentinel
// detection happens at the byte stream level.
zle.call_hook("bracketed-paste", None);
}
fn widget_vi_backward_word_end(zle: &mut Zle) {
// Port of vibackwardwordend() from Src/Zle/zle_word.c:348. Step
// backward to the end (last char) of the previous word. Faithful to
// the C loop: read class at current position, step back once, walk
// back through same-class non-blank chars, then through blanks.
let n = zle.mult.max(1);
let class_at = |c: char| -> i32 {
if c.is_whitespace() {
0
} else if c.is_alphanumeric() || c == '_' {
1
} else {
2
}
};
for _ in 0..n {
if zle.zlecs == 0 {
break;
}
let here = zle.zleline.get(zle.zlecs).copied().unwrap_or(' ');
let cc = class_at(here);
zle.zlecs -= 1;
while zle.zlecs > 0 {
let c = zle.zleline[zle.zlecs];
if class_at(c) != cc || c.is_whitespace() {
break;
}
zle.zlecs -= 1;
}
while zle.zlecs > 0 && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs -= 1;
}
}
zle.resetneeded = true;
}
fn widget_vi_backward_blank_word_end(zle: &mut Zle) {
// Port of vibackwardblankwordend() from Src/Zle/zle_word.c:375.
// Same shape as vibackwardwordend but whitespace is the only
// separator (no class distinction between alnum and punctuation).
let n = zle.mult.max(1);
for _ in 0..n {
if zle.zlecs == 0 {
break;
}
zle.zlecs -= 1;
while zle.zlecs > 0 && !zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs -= 1;
}
while zle.zlecs > 0 && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs -= 1;
}
}
zle.resetneeded = true;
}
fn widget_select_in_word(zle: &mut Zle) {
// Port of selectinword() from Src/Zle/textobjects.c. Sets a region
// containing the inner word at the cursor — different from
// Zle::find_word_start (which is a backward-motion helper); here we
// expand around the cursor while characters share the iword class.
let n = zle.zlell;
let pos = zle.zlecs.min(n);
if n == 0 {
return;
}
// Pick the class to span. If on a word char, use word-class; else
// sit on whitespace and use that class.
let cur_char = zle.zleline.get(pos).copied().unwrap_or(' ');
let is_word = cur_char.is_alphanumeric() || cur_char == '_';
let class = |c: char| -> bool {
if c.is_whitespace() {
!is_word && c.is_whitespace()
} else if is_word {
c.is_alphanumeric() || c == '_'
} else {
!c.is_alphanumeric() && c != '_' && !c.is_whitespace()
}
};
let mut start = pos;
while start > 0 && class(zle.zleline[start - 1]) == class(cur_char) {
start -= 1;
}
let mut end = pos;
while end < n && class(zle.zleline[end]) == class(cur_char) {
end += 1;
}
zle.mark = start;
zle.zlecs = end;
zle.region_active = 1;
zle.resetneeded = true;
}
fn widget_select_a_word(zle: &mut Zle) {
// Port of selectaword() from Src/Zle/textobjects.c. "around" form —
// includes a trailing whitespace separator if any.
widget_select_in_word(zle);
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
zle.resetneeded = true;
}
fn widget_select_in_blank_word(zle: &mut Zle) {
// Port of selectinblankword() from Src/Zle/textobjects.c. Spans a
// run of non-whitespace characters around the cursor.
let n = zle.zlell;
let pos = zle.zlecs.min(n);
if n == 0 || zle.zleline.get(pos).copied().unwrap_or(' ').is_whitespace() {
return;
}
let mut start = pos;
while start > 0 && !zle.zleline[start - 1].is_whitespace() {
start -= 1;
}
let mut end = pos;
while end < n && !zle.zleline[end].is_whitespace() {
end += 1;
}
zle.mark = start;
zle.zlecs = end;
zle.region_active = 1;
zle.resetneeded = true;
}
fn widget_select_a_blank_word(zle: &mut Zle) {
// Port of selectablankword() from Src/Zle/textobjects.c.
widget_select_in_blank_word(zle);
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
zle.resetneeded = true;
}
fn widget_select_in_shell_word(zle: &mut Zle) {
// Port of selectinshellword() from Src/Zle/textobjects.c. Uses the
// shell-word splitter that respects single/double quotes + escapes.
let saved = zle.zlecs;
let start = super::word::shell_word_start_before(&zle.zleline[..zle.zlell], saved);
let end = super::word::shell_word_end_after(&zle.zleline[..zle.zlell], saved);
zle.mark = start;
zle.zlecs = end;
zle.region_active = 1;
zle.resetneeded = true;
}
fn widget_select_a_shell_word(zle: &mut Zle) {
// Port of selectashellword() from Src/Zle/textobjects.c.
widget_select_in_shell_word(zle);
while zle.zlecs < zle.zlell && zle.zleline[zle.zlecs].is_whitespace() {
zle.zlecs += 1;
}
zle.resetneeded = true;
}
fn widget_accept_search(zle: &mut Zle) {
// Port of acceptsearch() from Src/Zle/zle_hist.c. Acceptance handler
// inside the isearch sub-loop — outside of isearch it's a no-op,
// which matches our design where do_isearch() handles its own loop.
let _ = zle;
}
fn widget_auto_suffix_remove(zle: &mut Zle) {
// Port of handlesuffix() (auto-suffix-remove flag) from
// Src/Zle/zle_misc.c. The C source clears the auto-removable
// suffix from the kill ring's tail. Surface as a hook so the host
// updates compsys's pending-suffix state.
zle.call_hook("auto-suffix-remove", None);
}
fn widget_auto_suffix_retain(zle: &mut Zle) {
// Port of handlesuffix() (KEEPSUFFIX) from Src/Zle/zle_misc.c.
zle.call_hook("auto-suffix-retain", None);
}
fn widget_put_replace_selection(zle: &mut Zle) {
// Port of putreplaceselection() from Src/Zle/zle_misc.c:680. With
// an active region, replaces it with the most-recent kill-ring
// entry; otherwise pastes at the cursor (same as yank).
if zle.region_active == 0 || zle.killring.is_empty() {
widget_yank(zle);
return;
}
let (lo, hi) = if zle.mark <= zle.zlecs {
(zle.mark, zle.zlecs)
} else {
(zle.zlecs, zle.mark)
};
let lo = lo.min(zle.zlell);
let hi = hi.min(zle.zlell);
zle.zleline.drain(lo..hi);
zle.zlell = zle.zleline.len();
zle.zlecs = lo;
let text: Vec<char> = zle
.killring
.front()
.cloned()
.unwrap_or_default();
for (i, c) in text.iter().enumerate() {
zle.zleline.insert(zle.zlecs + i, *c);
}
zle.zlecs += text.len();
zle.zlell = zle.zleline.len();
zle.region_active = 0;
zle.resetneeded = true;
}
fn widget_where_is(zle: &mut Zle) {
// Port of whereis() from Src/Zle/zle_thingy.c. The C source prompts
// for a widget name and shows what keys it's bound to. Surface as
// a hook so the host can prompt + look up in the current keymap.
zle.call_hook("where-is", None);
}
fn widget_execute_named_cmd(zle: &mut Zle) {
// Port of executenamedcmd() from Src/Zle/zle_thingy.c. Reads a
// widget name and executes it. Host-driven because completion of
// the prompt + dispatch live in host land.
zle.call_hook("execute-named-cmd", None);
}
fn widget_execute_last_named_cmd(zle: &mut Zle) {
// Port of executelastnamedcmd() from Src/Zle/zle_thingy.c. Same
// shape as execute-named-cmd; replays the last one.
zle.call_hook("execute-last-named-cmd", None);
}
fn widget_read_command(zle: &mut Zle) {
// Port of readcommand() from Src/Zle/zle_thingy.c. Reads a widget
// name from input and stores it for the host's executor.
zle.call_hook("read-command", None);
}
fn widget_menu_expand_or_complete(zle: &mut Zle) {
// Port of menuexpandorcomplete() from Src/Zle/zle_tricky.c. Menu
// completion variant of expand-or-complete.
zle.completion_request = Some(super::main::CompletionRequest::MenuComplete);
}
fn widget_reverse_menu_complete(zle: &mut Zle) {
// Port of reversemenucomplete() from Src/Zle/zle_tricky.c. Steps
// the menu backwards. Surfaced via a separate hook so the host's
// menu state knows which direction to step.
zle.call_hook("reverse-menu-complete", None);
}
fn widget_accept_and_menu_complete(zle: &mut Zle) {
// Port of acceptandmenucomplete() from Src/Zle/zle_tricky.c.
zle.call_hook("accept-and-menu-complete", None);
}
fn widget_list_expand(zle: &mut Zle) {
// Port of listexpand() from Src/Zle/zle_tricky.c. Expands current
// word and lists the candidates.
zle.completion_request = Some(super::main::CompletionRequest::ListChoices);
}
fn widget_expand_cmd_path(zle: &mut Zle) {
// Port of expandcmdpath() from Src/Zle/zle_tricky.c. Expands the
// first word into its full path via PATH lookup.
zle.call_hook("expand-cmd-path", None);
}
fn widget_expand_or_complete_prefix(zle: &mut Zle) {
// Port of expandorcompleteprefix() from Src/Zle/zle_tricky.c.
// Same as expand-or-complete but only considers the prefix before
// the cursor.
zle.completion_request = Some(super::main::CompletionRequest::ExpandOrComplete);
}
fn widget_end_of_list(zle: &mut Zle) {
// Port of endoflist() from Src/Zle/zle_tricky.c. Used inside the
// completion menu to dismiss the listing — host-driven.
zle.call_hook("end-of-list", None);
}
fn widget_history_incremental_pattern_search_backward(zle: &mut Zle) {
// Port of historyincrementalpatternsearchbackward() from
// Src/Zle/zle_hist.c:936. Pattern-mode variant of isearch — uses
// glob-pattern matching instead of plain-substring. Until the
// pattern engine is wired in to do_isearch, fall through to the
// plain backward isearch.
do_isearch(zle, -1);
}
fn widget_history_incremental_pattern_search_forward(zle: &mut Zle) {
// Port of historyincrementalpatternsearchforward() from
// Src/Zle/zle_hist.c:943.
do_isearch(zle, 1);
}
/// Check if a character is a word character
fn is_word_char(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
#[cfg(test)]
mod tests {
use super::*;
fn populated() -> Zle {
let mut zle = Zle::new();
zle.zleline = Vec::new();
zle.zlell = 0;
zle.zlecs = 0;
zle.killring.push_back("first".chars().collect());
zle.killring.push_back("second".chars().collect());
// VecDeque::push_back appends; killring[0] is what pop_front would
// return. widget_yank uses front() so the "most recent" entry is at
// index 0. Reset in newest-first order:
zle.killring.clear();
zle.killring.push_front("oldest".chars().collect());
zle.killring.push_front("middle".chars().collect());
zle.killring.push_front("newest".chars().collect());
zle
}
#[test]
fn complete_word_widget_surfaces_request() {
let mut zle = Zle::new();
widget_complete_word(&mut zle);
assert_eq!(
zle.completion_request,
Some(super::super::main::CompletionRequest::CompleteWord)
);
}
#[test]
fn expand_or_complete_widget_surfaces_request() {
let mut zle = Zle::new();
widget_expand_or_complete(&mut zle);
assert_eq!(
zle.completion_request,
Some(super::super::main::CompletionRequest::ExpandOrComplete)
);
}
#[test]
fn list_choices_widget_surfaces_request() {
let mut zle = Zle::new();
widget_list_choices(&mut zle);
assert_eq!(
zle.completion_request,
Some(super::super::main::CompletionRequest::ListChoices)
);
}
#[test]
fn menu_complete_widget_surfaces_request() {
let mut zle = Zle::new();
widget_menu_complete(&mut zle);
assert_eq!(
zle.completion_request,
Some(super::super::main::CompletionRequest::MenuComplete)
);
}
#[test]
fn delete_char_or_list_at_eol_surfaces_list_choices() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 3; // at end-of-line
widget_delete_char_or_list(&mut zle);
assert_eq!(
zle.completion_request,
Some(super::super::main::CompletionRequest::ListChoices)
);
}
#[test]
fn delete_char_or_list_mid_line_deletes_instead() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 1;
widget_delete_char_or_list(&mut zle);
// No completion request: it should have done a delete-char.
assert_eq!(zle.completion_request, None);
assert_eq!(zle.zleline.iter().collect::<String>(), "ac");
}
#[test]
fn set_mark_command_sets_mark_and_activates_region() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.zlecs = 3;
widget_set_mark_command(&mut zle);
assert_eq!(zle.mark, 3);
assert_eq!(zle.region_active, 1);
}
#[test]
fn set_mark_command_negative_count_deactivates() {
let mut zle = Zle::new();
zle.region_active = 1;
zle.mult = -1;
widget_set_mark_command(&mut zle);
assert_eq!(zle.region_active, 0);
}
#[test]
fn exchange_point_and_mark_swaps() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.zlecs = 4;
zle.mark = 1;
zle.mult = 1;
widget_exchange_point_and_mark(&mut zle);
assert_eq!(zle.zlecs, 1);
assert_eq!(zle.mark, 4);
}
#[test]
fn copy_region_as_kill_pushes_region_without_removing() {
let mut zle = Zle::new();
zle.zleline = "hello world".chars().collect();
zle.zlell = 11;
zle.zlecs = 5;
zle.mark = 0;
widget_copy_region_as_kill(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "hello world");
assert_eq!(
zle.killring.front().map(|v| v.iter().collect::<String>()),
Some("hello".to_string())
);
}
#[test]
fn copy_prev_word_inserts_previous_word_at_cursor() {
let mut zle = Zle::new();
zle.zleline = "echo hello ".chars().collect();
zle.zlell = 11;
zle.zlecs = 11;
zle.mult = 1;
widget_copy_prev_word(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "echo hello hello");
}
#[test]
fn quote_line_wraps_buffer_in_single_quotes() {
let mut zle = Zle::new();
zle.zleline = "echo hi".chars().collect();
zle.zlell = 7;
widget_quote_line(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "'echo hi'");
}
#[test]
fn quote_line_escapes_embedded_single_quote() {
let mut zle = Zle::new();
zle.zleline = "it's".chars().collect();
zle.zlell = 4;
widget_quote_line(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), r"'it'\''s'");
}
#[test]
fn quote_region_wraps_only_marked_span() {
let mut zle = Zle::new();
zle.zleline = "echo hi there".chars().collect();
zle.zlell = 13;
zle.mark = 5;
zle.zlecs = 7; // "hi"
widget_quote_region(&mut zle);
assert_eq!(
zle.zleline.iter().collect::<String>(),
"echo 'hi' there"
);
}
#[test]
fn pound_insert_toggles_leading_hash() {
let mut zle = Zle::new();
zle.zleline = "echo hi".chars().collect();
zle.zlell = 7;
widget_pound_insert(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "#echo hi");
// Toggle off.
zle.done = false;
widget_pound_insert(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "echo hi");
}
#[test]
fn transpose_words_swaps_two_words() {
let mut zle = Zle::new();
zle.zleline = "foo bar".chars().collect();
zle.zlell = 7;
zle.zlecs = 7; // at end of line
widget_transpose_words(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "bar foo");
}
#[test]
fn capitalize_word_widget_capitalizes_at_cursor() {
let mut zle = Zle::new();
zle.zleline = "hello world".chars().collect();
zle.zlell = 11;
zle.zlecs = 0;
widget_capitalize_word(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "Hello world");
}
#[test]
fn vi_put_after_pastes_after_cursor_on_charwise_yank() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 0; // on 'a'
zle.killring.push_front("XY".chars().collect());
widget_vi_put_after(&mut zle);
// `p` after 'a' inserts XY at index 1 → "aXYbc". Cursor lands
// on 'Y' (last char of pasted region) at index 2.
assert_eq!(zle.zleline.iter().collect::<String>(), "aXYbc");
assert_eq!(zle.zlecs, 2);
}
#[test]
fn vi_put_before_pastes_before_cursor_on_charwise_yank() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 1; // on 'b'
zle.killring.push_front("XY".chars().collect());
widget_vi_put_before(&mut zle);
// `P` at cursor 1 inserts XY → "aXYbc". Cursor lands on 'Y'.
assert_eq!(zle.zleline.iter().collect::<String>(), "aXYbc");
assert_eq!(zle.zlecs, 2);
}
#[test]
fn vi_put_after_linewise_pastes_on_new_line_below() {
let mut zle = Zle::new();
zle.zleline = "first\nthird".chars().collect();
zle.zlell = 11;
zle.zlecs = 2; // mid-"first"
// Linewise yank entry ends in newline.
zle.killring.push_front("second\n".chars().collect());
widget_vi_put_after(&mut zle);
let s: String = zle.zleline.iter().collect();
// Should now be "first\nsecond\nthird".
assert!(s.contains("first\nsecond\nthird"), "got: {:?}", s);
}
#[test]
fn vi_replace_chars_overwrites_one_char() {
let mut zle = Zle::new();
zle.zleline = "hello".chars().collect();
zle.zlell = 5;
zle.zlecs = 0;
zle.ungetbytes(b"X");
widget_vi_replace_chars(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "Xello");
}
#[test]
fn vi_replace_chars_with_count_overwrites_n_chars() {
let mut zle = Zle::new();
zle.zleline = "hello".chars().collect();
zle.zlell = 5;
zle.zlecs = 0;
zle.mult = 3;
zle.ungetbytes(b"X");
widget_vi_replace_chars(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "XXXlo");
// Cursor lands on the LAST replaced char.
assert_eq!(zle.zlecs, 2);
}
#[test]
fn vi_join_removes_newline_and_inserts_space() {
let mut zle = Zle::new();
zle.zleline = "foo\nbar".chars().collect();
zle.zlell = 7;
zle.zlecs = 0;
zle.mult = 1;
widget_vi_join(&mut zle);
// Newline replaced with single space.
assert_eq!(zle.zleline.iter().collect::<String>(), "foo bar");
}
#[test]
fn vi_open_line_above_starts_new_line_at_bol() {
let mut zle = Zle::new();
zle.zleline = "hello".chars().collect();
zle.zlell = 5;
zle.zlecs = 2;
widget_vi_open_line_above(&mut zle);
// Newline inserted at start; cursor sits before it on first line.
let s: String = zle.zleline.iter().collect();
assert!(s.starts_with('\n') || s.contains('\n'));
}
#[test]
fn vi_first_non_blank_skips_leading_whitespace() {
let mut zle = Zle::new();
zle.zleline = " hello".chars().collect();
zle.zlell = 9;
zle.zlecs = 8;
widget_vi_first_non_blank(&mut zle);
assert_eq!(zle.zlecs, 4);
}
#[test]
fn accept_line_widget_marks_done() {
let mut zle = Zle::new();
zle.zleline = "echo hi".chars().collect();
zle.zlell = 7;
widget_accept_line(&mut zle);
assert!(zle.done);
// accept-line keeps the buffer intact for the caller to read.
assert_eq!(zle.zleline.iter().collect::<String>(), "echo hi");
}
#[test]
fn send_break_widget_clears_buffer_and_marks_done() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 2;
widget_send_break(&mut zle);
assert!(zle.done);
assert!(zle.zleline.is_empty());
assert_eq!(zle.zlell, 0);
assert_eq!(zle.zlecs, 0);
}
#[test]
fn overwrite_mode_widget_toggles_insmode() {
let mut zle = Zle::new();
let initial = zle.insmode;
widget_overwrite_mode(&mut zle);
assert_eq!(zle.insmode, !initial);
widget_overwrite_mode(&mut zle);
assert_eq!(zle.insmode, initial);
}
#[test]
fn select_in_word_sets_region_around_current_word() {
let mut zle = Zle::new();
zle.zleline = "foo bar baz".chars().collect();
zle.zlell = 11;
zle.zlecs = 4; // inside "bar"
widget_select_in_word(&mut zle);
assert_eq!(zle.region_active, 1);
// mark/cursor should bracket "bar".
let lo = zle.mark.min(zle.zlecs);
let hi = zle.mark.max(zle.zlecs);
assert_eq!(&zle.zleline[lo..hi].iter().collect::<String>(), "bar");
}
#[test]
fn select_in_shell_word_treats_double_quoted_string_as_one_word() {
let mut zle = Zle::new();
zle.zleline = r#"echo "hello world""#.chars().collect();
zle.zlell = zle.zleline.len();
zle.zlecs = 8; // inside the quoted string
widget_select_in_shell_word(&mut zle);
let lo = zle.mark.min(zle.zlecs);
let hi = zle.mark.max(zle.zlecs);
let span: String = zle.zleline[lo..hi].iter().collect();
assert_eq!(span, r#""hello world""#);
}
#[test]
fn put_replace_selection_overwrites_active_region() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.killring.push_front("XYZ".chars().collect());
zle.mark = 1;
zle.zlecs = 4; // selecting "bcd"
zle.region_active = 1;
widget_put_replace_selection(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "aXYZef");
assert_eq!(zle.region_active, 0);
}
#[test]
fn vi_backward_word_end_lands_at_prior_word_end() {
let mut zle = Zle::new();
zle.zleline = "foo bar baz".chars().collect();
zle.zlell = 11;
zle.zlecs = 11; // at EOB (past 'z')
widget_vi_backward_word_end(&mut zle);
// vim's `ge` from past-EOL lands at the end of the LAST word —
// position of 'z' in "baz" = index 10 (matches the C source's
// vibackwardwordend in Src/Zle/zle_word.c:348).
assert_eq!(zle.zlecs, 10);
}
#[test]
fn kill_word_with_count_kills_n_words() {
let mut zle = Zle::new();
zle.zleline = "foo bar baz qux".chars().collect();
zle.zlell = 15;
zle.zlecs = 0;
zle.mult = 2;
widget_kill_word(&mut zle);
// 2*kill-word from start removes "foo bar" plus its trailing
// separator into the kill ring; remaining buffer starts at " baz".
let s: String = zle.zleline.iter().collect();
assert_eq!(s, " baz qux");
let killed = zle.killring.front().unwrap().iter().collect::<String>();
assert_eq!(killed, "foo bar");
}
#[test]
fn backward_kill_word_with_count_kills_n_words_back() {
let mut zle = Zle::new();
zle.zleline = "alpha beta gamma".chars().collect();
zle.zlell = 16;
zle.zlecs = 16;
zle.mult = 2;
widget_backward_kill_word(&mut zle);
let s: String = zle.zleline.iter().collect();
assert_eq!(s, "alpha ");
let killed = zle.killring.front().unwrap().iter().collect::<String>();
assert_eq!(killed, "beta gamma");
}
#[test]
fn delete_word_removes_word_without_kill_ring() {
let mut zle = Zle::new();
zle.zleline = "hello world".chars().collect();
zle.zlell = 11;
zle.zlecs = 0;
let kr_before = zle.killring.len();
widget_delete_word(&mut zle);
// Emacs delete-word removes the word but not the trailing separator
// (zle_word.c convention) — leaves a leading space.
assert_eq!(zle.zleline.iter().collect::<String>(), " world");
assert_eq!(zle.killring.len(), kr_before);
}
#[test]
fn kill_region_drains_into_kill_ring() {
let mut zle = Zle::new();
zle.zleline = "abcdefgh".chars().collect();
zle.zlell = 8;
zle.mark = 2;
zle.zlecs = 6;
widget_kill_region(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "abgh");
assert_eq!(
zle.killring.front().map(|v| v.iter().collect::<String>()),
Some("cdef".to_string())
);
}
#[test]
fn kill_buffer_clears_line_and_pushes_to_kill_ring() {
let mut zle = Zle::new();
zle.zleline = "echo hi".chars().collect();
zle.zlell = 7;
widget_kill_buffer(&mut zle);
assert!(zle.zleline.is_empty());
assert_eq!(zle.zlell, 0);
assert_eq!(zle.zlecs, 0);
assert_eq!(
zle.killring.front().map(|v| v.iter().collect::<String>()),
Some("echo hi".to_string())
);
}
#[test]
fn vi_kill_line_kills_back_to_bol() {
let mut zle = Zle::new();
zle.zleline = "abc def".chars().collect();
zle.zlell = 7;
zle.zlecs = 7;
widget_vi_kill_line(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "");
assert_eq!(
zle.killring.front().map(|v| v.iter().collect::<String>()),
Some("abc def".to_string())
);
}
#[test]
fn vi_swap_case_flips_letter_case_under_cursor() {
let mut zle = Zle::new();
zle.zleline = "Hello".chars().collect();
zle.zlell = 5;
zle.zlecs = 0;
widget_vi_swap_case(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "hello");
assert_eq!(zle.zlecs, 1);
}
#[test]
fn vi_swap_case_with_count_flips_n_chars() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.zlecs = 0;
zle.mult = 3;
widget_vi_swap_case(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "ABCdef");
assert_eq!(zle.zlecs, 3);
}
#[test]
fn universal_argument_bumps_tmult_by_4_each_call() {
// C zsh's universalargument() (zle_misc.c:986) updates tmult,
// not mult — handleprefixes promotes TMULT→MULT on the next
// widget call. Matches initmodifier() which seeds tmult=1.
let mut zle = Zle::new();
zle.initmodifier();
widget_universal_argument(&mut zle);
// No bytes available in test → digcnt=0 path → tmult *= 4.
assert_eq!(zle.zmod.tmult, 4);
assert!(zle.zmod.flags.contains(crate::zle::main::ModifierFlags::TMULT));
widget_universal_argument(&mut zle);
assert_eq!(zle.zmod.tmult, 16);
}
#[test]
fn universal_argument_with_digits_reads_pref() {
let mut zle = Zle::new();
zle.initmodifier();
// Pre-feed "42x" — universal-argument should pull the "42" and
// unget the trailing 'x' for the next read.
zle.ungetbytes(b"42x");
widget_universal_argument(&mut zle);
assert_eq!(zle.zmod.tmult, 42);
assert!(zle.zmod.flags.contains(crate::zle::main::ModifierFlags::TMULT));
// 'x' should still be in the unget buffer.
let next = zle.getbyte(false);
assert_eq!(next, Some(b'x'));
}
#[test]
fn universal_argument_with_minus_reads_negative() {
let mut zle = Zle::new();
zle.initmodifier();
zle.ungetbytes(b"-7\n");
widget_universal_argument(&mut zle);
assert_eq!(zle.zmod.tmult, -7);
}
#[test]
fn neg_argument_first_invocation_sets_tmult_minus_one() {
let mut zle = Zle::new();
zle.initmodifier();
widget_neg_argument(&mut zle);
assert_eq!(zle.zmod.tmult, -1);
assert!(zle.zmod.flags.contains(crate::zle::main::ModifierFlags::TMULT));
assert!(zle.zmod.flags.contains(crate::zle::main::ModifierFlags::NEG));
}
#[test]
fn neg_argument_after_tmult_already_set_is_rejected() {
// C: returns 1 (error/beep) if MOD_TMULT was already set.
let mut zle = Zle::new();
zle.initmodifier();
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::TMULT);
zle.zmod.tmult = 5;
widget_neg_argument(&mut zle);
// tmult unchanged, NEG NOT set.
assert_eq!(zle.zmod.tmult, 5);
assert!(!zle.zmod.flags.contains(crate::zle::main::ModifierFlags::NEG));
}
#[test]
fn digit_argument_after_neg_argument_inherits_sign() {
// After neg-argument, handleprefixes promotes tmult=-1 into
// mult=-1; the next digit-argument sees mult<0 and applies the
// negative sign to the first digit (C: zle_misc.c:961-964 +
// zle_main.c:1620 promote chain).
let mut zle = Zle::new();
zle.initmodifier();
widget_neg_argument(&mut zle);
// Simulate the zlecore→handleprefixes step the live loop runs
// between widgets.
zle.handleprefixes();
zle.lastchar = b'5' as i32;
widget_digit_argument(&mut zle);
assert_eq!(zle.zmod.tmult, -5);
assert!(!zle.zmod.flags.contains(crate::zle::main::ModifierFlags::NEG));
}
#[test]
fn vi_yank_whole_line_includes_trailing_newline() {
// Linewise yank must include the \n so vi-put-after's
// is_line_paste detection fires (zle_vi.c:559 path).
let mut zle = Zle::new();
zle.zleline = "first\nsecond".chars().collect();
zle.zlell = 12;
zle.zlecs = 2; // mid-"first"
zle.mult = 1;
widget_vi_yank_whole_line(&mut zle);
let killed = zle.killring.front().unwrap().iter().collect::<String>();
assert_eq!(killed, "first\n");
}
#[test]
fn vi_yank_whole_line_with_count_yanks_n_lines() {
let mut zle = Zle::new();
zle.zleline = "a\nb\nc\nd".chars().collect();
zle.zlell = 7;
zle.zlecs = 0;
zle.mult = 3;
widget_vi_yank_whole_line(&mut zle);
let killed = zle.killring.front().unwrap().iter().collect::<String>();
assert_eq!(killed, "a\nb\nc\n");
}
#[test]
fn vi_goto_column_lands_on_column_within_logical_line() {
let mut zle = Zle::new();
zle.zleline = "hello\nworld".chars().collect();
zle.zlell = 11;
zle.zlecs = 9; // mid-"world"
zle.zmod.mult = 3; // 1-based → column 3 = 'r'
widget_vi_goto_column(&mut zle);
// bol of "world" is 6 → 6 + (3-1) = 8.
assert_eq!(zle.zlecs, 8);
}
#[test]
fn vi_goto_column_clamps_to_end_of_line() {
let mut zle = Zle::new();
zle.zleline = "ab\ncd".chars().collect();
zle.zlell = 5;
zle.zlecs = 3; // on "cd" line
zle.zmod.mult = 99;
widget_vi_goto_column(&mut zle);
// EoL of "cd" is index 5; clamp lands there.
assert_eq!(zle.zlecs, 5);
}
#[test]
fn vi_add_eol_jumps_to_eol_of_current_line() {
let mut zle = Zle::new();
zle.zleline = "hello".chars().collect();
zle.zlell = 5;
zle.zlecs = 1;
widget_vi_add_eol(&mut zle);
assert_eq!(zle.zlecs, 5);
}
#[test]
fn vi_forward_char_clamps_at_eol() {
// Vim 'l' can't cross EoL — cursor lands on last char of
// current logical line at most.
let mut zle = Zle::new();
zle.zleline = "abc\ndef".chars().collect();
zle.zlell = 7;
zle.zlecs = 1; // on 'b' of "abc"
zle.mult = 5;
widget_vi_forward_char(&mut zle);
// EoL of "abc" is 3 (the \n); limit is 2 (eol-1 = on 'c').
assert_eq!(zle.zlecs, 2);
}
#[test]
fn forward_char_with_count_advances_n() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.zlecs = 0;
zle.mult = 3;
widget_forward_char(&mut zle);
assert_eq!(zle.zlecs, 3);
}
#[test]
fn forward_char_with_negative_count_delegates_backwards() {
let mut zle = Zle::new();
zle.zleline = "abcdef".chars().collect();
zle.zlell = 6;
zle.zlecs = 5;
zle.mult = -2;
widget_forward_char(&mut zle);
assert_eq!(zle.zlecs, 3);
}
#[test]
fn backward_delete_char_count_clamped_to_cursor() {
// C source: zle_misc.c:189 clamps mult to zlecs.
let mut zle = Zle::new();
zle.zleline = "ab".chars().collect();
zle.zlell = 2;
zle.zlecs = 1;
zle.mult = 5; // ask to delete 5 but only 1 char available before cursor
widget_backward_delete_char(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "b");
assert_eq!(zle.zlecs, 0);
}
#[test]
fn forward_word_with_count_skips_n_words() {
let mut zle = Zle::new();
zle.zleline = "a b c d".chars().collect();
zle.zlell = 7;
zle.zlecs = 0;
zle.mult = 2;
widget_forward_word(&mut zle);
// After 2 word skips: a→b, b→c, lands at c's start (index 4).
assert_eq!(zle.zlecs, 4);
}
#[test]
fn kill_line_with_count_kills_n_lines() {
let mut zle = Zle::new();
zle.zleline = "first\nsecond\nthird".chars().collect();
zle.zlell = 18;
zle.zlecs = 0;
zle.mult = 2;
widget_kill_line(&mut zle);
// 2 iterations: kill "first", then \n. Buffer: "second\nthird".
assert_eq!(zle.zleline.iter().collect::<String>(), "second\nthird");
}
#[test]
fn beginning_of_line_uses_find_bol_for_multiline() {
let mut zle = Zle::new();
zle.zleline = "first\nsecond".chars().collect();
zle.zlell = 12;
zle.zlecs = 9; // mid-"second"
widget_beginning_of_line(&mut zle);
// Should land at start of the SECOND logical line, not 0.
assert_eq!(zle.zlecs, 6);
}
#[test]
fn transpose_chars_swaps_at_cursor() {
let mut zle = Zle::new();
zle.zleline = "abcd".chars().collect();
zle.zlell = 4;
zle.zlecs = 2; // between 'b' and 'c'
widget_transpose_chars(&mut zle);
// Default count=1 swaps line[ct-1]<->line[ct] (= 'b'<->'c').
assert_eq!(zle.zleline.iter().collect::<String>(), "acbd");
// Cursor advances past the swap.
assert_eq!(zle.zlecs, 3);
}
#[test]
fn transpose_chars_at_eob_swaps_last_two() {
let mut zle = Zle::new();
zle.zleline = "ab".chars().collect();
zle.zlell = 2;
zle.zlecs = 2; // at end-of-buffer
widget_transpose_chars(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "ba");
}
#[test]
fn transpose_chars_at_bol_returns_without_swap() {
let mut zle = Zle::new();
zle.zleline = "ab".chars().collect();
zle.zlell = 2;
zle.zlecs = 0;
widget_transpose_chars(&mut zle);
// At BoL (ct=0) with content available, advance and swap.
assert_eq!(zle.zleline.iter().collect::<String>(), "ba");
}
#[test]
fn transpose_chars_with_negative_count_swaps_backwards() {
let mut zle = Zle::new();
zle.zleline = "abcd".chars().collect();
zle.zlell = 4;
zle.zlecs = 3;
zle.mult = -1;
widget_transpose_chars(&mut zle);
// Negative count moves cursor back, swaps prior pair.
assert_eq!(zle.zleline.iter().collect::<String>(), "acbd");
}
#[test]
fn parse_digit_in_base_handles_decimal_and_hex_and_invalid() {
assert_eq!(parse_digit_in_base(b'7', 10), 7);
assert_eq!(parse_digit_in_base(b'a', 16), 10);
assert_eq!(parse_digit_in_base(b'F', 16), 15);
assert_eq!(parse_digit_in_base(b'g', 16), -1); // out of range
assert_eq!(parse_digit_in_base(b'5', 8), 5);
assert_eq!(parse_digit_in_base(b'9', 8), -1); // 9 is not a base-8 digit
assert_eq!(parse_digit_in_base(b'z', 36), 35);
assert_eq!(parse_digit_in_base(b'!', 10), -1);
}
#[test]
fn argument_base_clamps_invalid_base_via_feep() {
let mut zle = Zle::new();
zle.initmodifier();
zle.zmod.mult = 1; // base 1 is invalid (< 2)
widget_argument_base(&mut zle);
// base unchanged at 10; no prefixflag side effect.
assert_eq!(zle.zmod.base, 10);
}
#[test]
fn vi_beginning_of_line_jumps_to_bol() {
let mut zle = Zle::new();
zle.zleline = " foo".chars().collect();
zle.zlell = 7;
zle.zlecs = 5;
widget_vi_beginning_of_line(&mut zle);
assert_eq!(zle.zlecs, 0);
}
#[test]
fn emacs_forward_word_moves_to_word_end() {
let mut zle = Zle::new();
zle.zleline = "hello world".chars().collect();
zle.zlell = 11;
zle.zlecs = 0;
widget_emacs_forward_word(&mut zle);
// find_word_end (Emacs style) skips non-word + word; "hello" ends
// at byte 5.
assert!(zle.zlecs >= 5);
}
#[test]
fn vi_yank_eol_copies_to_eol() {
let mut zle = Zle::new();
zle.zleline = "hello world".chars().collect();
zle.zlell = 11;
zle.zlecs = 6;
widget_vi_yank_eol(&mut zle);
// Cursor stays put; killring gets "world".
assert_eq!(zle.zleline.iter().collect::<String>(), "hello world");
assert_eq!(
zle.killring.front().map(|v| v.iter().collect::<String>()),
Some("world".to_string())
);
}
#[test]
fn what_cursor_position_does_not_panic_on_end_of_buffer() {
let mut zle = Zle::new();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
zle.zlecs = 3; // past last char
widget_what_cursor_position(&mut zle);
// No assertion on stderr — just verifying the EOB branch doesn't
// index out of bounds.
}
#[test]
fn history_beginning_search_backward_walks_to_matching_prefix() {
let mut zle = Zle::new();
zle.history.add("git commit".to_string());
zle.history.add("ls -la".to_string());
zle.history.add("git push".to_string());
zle.history.cursor = 3; // sentinel
zle.zleline = "git ".chars().collect();
zle.zlell = 4;
zle.zlecs = 4;
widget_history_beginning_search_backward(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "git push");
// Cursor stays where it was on the prefix.
assert_eq!(zle.zlecs, 4);
widget_history_beginning_search_backward(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "git commit");
}
#[test]
fn yank_records_region_for_yank_pop() {
let mut zle = populated();
widget_yank(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "newest");
assert_eq!(zle.yank_start, 0);
assert_eq!(zle.yank_end, 6);
assert_eq!(zle.yank_ring_idx, Some(0));
assert!(zle.yanklast);
}
#[test]
fn yank_pop_replaces_with_previous_kill_ring_entry() {
let mut zle = populated();
widget_yank(&mut zle);
widget_yank_pop(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "middle");
widget_yank_pop(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "oldest");
}
#[test]
fn yank_pop_no_op_without_prior_yank() {
let mut zle = populated();
zle.zleline = "abc".chars().collect();
zle.zlell = 3;
widget_yank_pop(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "abc");
}
#[test]
fn yank_pop_skips_empty_buffers() {
let mut zle = Zle::new();
zle.killring.push_front(Vec::new()); // empty buffer
zle.killring.push_front("real".chars().collect());
zle.killring.push_front("first".chars().collect());
// widget_yank picks killring[0] = "first"
widget_yank(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "first");
widget_yank_pop(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "real");
// Next pop wraps past the empty entry.
widget_yank_pop(&mut zle);
// After wrapping the empty entry, ring length 3, start_idx 1, advances
// to idx=2 (empty, skipped within loop), then idx=0 — but 0 == start_idx
// would only be true if start_idx were 0. We started from idx 1 and
// the empty slot is at 2, so advance: 2 (empty, continue), 0 ("first"),
// hit. Land on "first".
assert_eq!(zle.zleline.iter().collect::<String>(), "first");
}
#[test]
fn vi_fetch_history_no_count_on_live_jumps_to_bol() {
// When sitting on the live buffer with no explicit count, vi-fetch-history
// moves the cursor to the beginning of the current logical line.
// Port of C's `zlecs = zlell; zlecs = findbol()` no-mult branch
// (zle_hist.c:1793).
let mut zle = Zle::new();
zle.history.add("a".to_string());
zle.zleline = "abc def".chars().collect();
zle.zlell = 7;
zle.zlecs = 4;
zle.history.cursor = 1; // live buffer
widget_vi_fetch_history(&mut zle);
assert_eq!(zle.zlecs, 0);
}
#[test]
fn vi_fetch_history_with_count_jumps_to_event() {
let mut zle = Zle::new();
zle.history.add("a".to_string());
zle.history.add("b".to_string());
zle.history.add("c".to_string());
zle.history.cursor = 3; // live buffer
zle.zmod.flags.insert(crate::zle::main::ModifierFlags::MULT);
zle.zmod.mult = 2; // 1-based: event #2 = entry index 1
zle.mult = 2;
widget_vi_fetch_history(&mut zle);
assert_eq!(zle.zleline.iter().collect::<String>(), "b");
assert_eq!(zle.history.cursor, 1);
}
}