zshrs 0.11.0

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
//! ZLE main routines - Direct port from zsh/Src/Zle/zle_main.c
//!
//! Core event loop, initialization, and main entry points for the line editor.
//!
//! Implements:
//! - zleread() - main entry point for line reading
//! - zlecore() - core editing loop
//! - zsetterm() - terminal setup
//! - getbyte(), getfullchar() - input reading with UTF-8 support
//! - ungetbyte(), ungetbytes() - input pushback
//! - calc_timeout() - key timeout handling
//! - trashzle(), resetprompt() - display management
//! - recursive_edit() - nested editing
//! - bin_vared() - vared builtin
//! - zle_main_entry() - module entry point

use std::collections::VecDeque;
use crate::ported::utils::zwarnnam;
use std::io::{self, Read, Write};
use std::os::unix::io::{AsRawFd, RawFd};
use std::time::{Duration, Instant};

use super::zle_keymap::Keymap;
use super::zle_thingy::Thingy;
use super::zle_h::widget as Widget;
use super::zle_h::{ZLE_LASTCOL, ZLE_NOTCOMMAND};

/// ZLE character type - always char in Rust (Unicode native)

// --- AUTO: cross-zle hoisted-fn use glob ---
#[allow(unused_imports)]
use crate::ported::zle::zle_h::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_misc::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_hist::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_move::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_word::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_params::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_vi::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_utils::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_refresh::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_tricky::*;
#[allow(unused_imports)]
use crate::ported::zle::textobjects::*;
#[allow(unused_imports)]
use crate::ported::zle::deltochar::*;
use crate::zle::zle_h::{change, modifier, widget};

pub type ZleChar = char;

/// ZLE string type
pub type ZleString = Vec<ZleChar>;

/// ZLE integer type for character values
pub type ZleInt = i32;

/// EOF marker
pub const ZLEEOF: ZleInt = -1;

// `ZleReadFlags` deleted — Rust-only struct wrapping what C carries
// as bare `int flags` with `ZLRF_HISTORY` / `ZLRF_NOSETTY` /
// `ZLRF_IGNOREEOF` bits (Src/zsh.h:3203-3205). The fake fields
// (no_history / completion / vared) had no C counterpart; C uses
// `!(zlereadflags & ZLRF_HISTORY)` inline for the no-history test
// and a separate `zlecontext == ZLCON_VARED` check for vared mode.
// `zlereadflags` is now `i32` matching C's `int zlereadflags`
// (Src/Zle/zle_main.c:90).

// `ZleContext` deleted — Rust-named enum duplicating the legit C
// enum at Src/zsh.h:3211-3216 (`ZLCON_LINE_START` / `ZLCON_LINE_CONT`
// / `ZLCON_SELECT` / `ZLCON_VARED`), already ported in zsh_h.rs:3162-3165.
// `ZLECONTEXT` is now an `AtomicI32` static matching C's `int
// zlecontext` (zle_main.c:163).


impl Default for modifier {
    fn default() -> Self {
        // c:1604 initmodifier — mult=1, tmult=1, base=10.
        modifier {
            flags: 0,
            mult: 1,
            tmult: 1,
            vibuf: 0,
            base: 10,
        }
    }
}

use super::zle_h::{MOD_MULT, MOD_TMULT, MOD_VIBUF, MOD_VIAPP, MOD_NEG, MOD_NULL, MOD_CHAR, MOD_LINE, MOD_PRI, MOD_CLIP, MOD_OSSEL};

// `ModifierFlags` bitflags wrapper deleted — C uses bare `int flags`
// in `struct modifier` (zle.h:246) with the `MOD_*` bit constants
// at zle.h:253-263, already legit-ported in zle_h.rs:371-381.
// modifier.flags is now `i32` matching C verbatim.

/// Direct port of `struct change` from `Src/Zle/zle.h:284-294`.
/// Undo change record. `ChangeFlags` bitflags wrapper deleted —
/// C uses bare `int flags` with `CH_NEXT` (1<<0) and `CH_PREV`
/// (1<<1) bits (zle.h:297-298, ported in zle_h.rs as i32).
// `WatchFd` deleted — duplicate of `struct watch_fd` already legit-
// ported at zle_h.rs:781 (Src/Zle/zle.h:572). The Rust port uses
// `super::zle_h::watch_fd` directly.

// `TimeoutType` / `Timeout` deleted — Rust-named duplicates of the
// legit `ztmouttp` enum + `ztmout` struct already ported at
// `zle_main.c:398/432`. See definitions below.

/// Maximum timeout value (about 24 days in 100ths of a second)
/// Port of `ZMAXTIMEOUT` macro from `Src/Zle/zle_main.c:429`.
/// `#define ZMAXTIMEOUT ((time_t)1 << (sizeof(int)*8-11))`.
/// Maximum keytimeout value clamped before passing to select(2),
/// keeps the (microseconds * 100) product within `time_t` range.
/// On a 32-bit `int` platform: `1 << 21` (~2.1M centiseconds = 21k sec).
pub const ZMAXTIMEOUT: u64 = 1 << 21;                                        // c:429

/// Port of `MAXFOUND` from `Src/Zle/zle_main.c:1925`.
/// Hash-search saturation cap: stop walking after this many matches
/// in the brief-key-description scan — keeps the prompt-line summary
/// short enough to fit on screen.
pub const MAXFOUND: usize = 4;                                               // c:1925

/// Port of `enum ztmouttp` from `Src/Zle/zle_main.c:398`. Discriminator
/// for the active read-timeout source: none, key (do_keytmout), function
/// (timedfns), or maxed-out (re-arm needed).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum ztmouttp {                                                          // c:398
    ZTM_NONE = 0,                                                            // c:401
    ZTM_KEY  = 1,                                                            // c:406
    ZTM_FUNC = 2,                                                            // c:412
    ZTM_MAX  = 3,                                                            // c:428
}

/// Port of `struct ztmout` from `Src/Zle/zle_main.c:432`. Carries the
/// active timeout type plus expiration in 100ths of a second.
#[derive(Debug, Clone, Copy)]
#[allow(non_camel_case_types)]
pub struct ztmout {                                                          // c:432
    pub tp: ztmouttp,                                                        // c:434 enum ztmouttp tp
    pub exp100ths: i64,                                                      // c:438 time_t exp100ths
}

/// Port of `struct findfunc` from `Src/Zle/zle_main.c:1927`. Closure
/// state for the `describe-key-briefly` widget — accumulates the
/// found-binding hits up to `MAXFOUND` and a status message.
#[derive(Debug, Default)]
#[allow(non_camel_case_types)]
pub struct findfunc {                                                        // c:1927
    /// Target Thingy we're searching for; matched against scan key.
    /// Cell holds `None` until set; `usize` indexes into THINGYTAB.
    pub func: Option<usize>,                                                 // c:1928
    /// Hit counter; capped at MAXFOUND.
    pub found: usize,                                                        // c:1929
    /// Accumulated message: " is on KEY1 KEY2 ..." or similar.
    pub msg: String,                                                         // c:1930
}

// `pub struct Zle;` deleted along with `impl Default for Zle` and
// `impl Zle { fn new() }`. The unit marker had no C counterpart and
// served only as a per-instance dispatch tag for the now-deleted
// methods. State init lives in `zle_reset()` below, the C-equivalent
// of `zleread()`'s reset block at `Src/Zle/zle_main.c:1216`.

// `CompletionRequest` enum deleted along with the matching field.
// C dispatches completion-widget variants via separate function
// pointers in `Src/Zle/zle_tricky.c` — no enum type.

/// Process-wide lock that serialises ZLE-touching tests. The ZLE
/// session state lives in file-scope statics (ZLELINE/ZLECS/etc.);
/// `cargo test` runs tests in parallel by default which races on
/// those shared statics. Tests acquire this lock at the top
/// (typically via `zle_test_setup()` below) so the parallel runner
/// effectively serialises just the ZLE-touching subset. No C
/// counterpart — C is single-threaded so the question doesn't arise.
#[doc(hidden)]
pub static ZLE_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// Test-only setup helper: acquires `ZLE_TEST_LOCK` and resets state.
/// Returns the lock guard which holds for the test body's lifetime.
/// Pattern: `let _g = crate::ported::zle::zle_main::zle_test_setup();`
/// at the top of every `#[test]` that mutates ZLE statics.
#[doc(hidden)]
pub fn zle_test_setup() -> std::sync::MutexGuard<'static, ()> {
    // Poison-tolerant: if a prior test panicked while holding the
    // lock, recover the guard rather than poisoning every subsequent
    // test. Tests reset state via zle_reset() anyway.
    let guard = ZLE_TEST_LOCK
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    zle_reset();
    guard
}

/// Reset every ZLE-session file-scope static to its zero-state.
/// Equivalent to the global state initialisation that `zleread()`
/// performs at the start of each line edit in Src/Zle/zle_main.c:1216
/// — `zleline = NULL; zlecs = zlell = 0; done = 0; eofsent = 0; ...`.
/// Called by tests and by the host before entering a new edit
/// session. No C counterpart name; the equivalent C reset is the
/// inline assignment block at the head of `zleread`.
pub fn zle_reset() {
    // Seed the global keymap table on first reset call. The C source
    // fires `createkeymapnamtab()` + `default_bindings()` once at
    // module init (zle.c:init_zle).
    crate::ported::zle::zle_keymap::createkeymapnamtab();
    crate::ported::zle::zle_keymap::default_bindings();
    use std::sync::atomic::Ordering;
    ZLELINE.lock().unwrap().clear();
    ZLECS.store(0, Ordering::SeqCst);
    ZLELL.store(0, Ordering::SeqCst);
    MARK.store(0, Ordering::SeqCst);
    *LBINDK.lock().unwrap() = None;
    *BINDK.lock().unwrap() = None;
    *ZMOD.lock().unwrap() = modifier { flags: 0, mult: 1, tmult: 1, vibuf: 0, base: 10 };
    *STATUSLINE.lock().unwrap() = None;
    STACKHIST.store(0, Ordering::SeqCst);
    STACKCS.store(0, Ordering::SeqCst);
    VISTARTCHANGE.store(0, Ordering::SeqCst);
    UNDO_STACK.lock().unwrap().clear();
    CHANGENO.store(0, Ordering::SeqCst);
    KUNGETBUF.lock().unwrap().clear();
    BAUD.store(38400, Ordering::SeqCst);
    WATCH_FDS.lock().unwrap().clear();
    *COMPWIDGET.lock().unwrap() = None;
    HASCOMPMOD.store(false, Ordering::SeqCst);
    TTYFD.store(0, Ordering::SeqCst);
    LPROMPT.lock().unwrap().clear();
    RPROMPT.lock().unwrap().clear();
    PRE_ZLE_STATUS.store(0, Ordering::SeqCst);
    for slot in vibuf().lock().unwrap().iter_mut() { slot.clear(); }
    KILLRING.lock().unwrap().clear();
    KILLRINGMAX.store(8, Ordering::SeqCst);
    YANKLAST.store(false, Ordering::SeqCst);
    NEG_ARG.store(false, Ordering::SeqCst);
    MULT.store(1, Ordering::SeqCst);
    *history().lock().unwrap() = super::zle_hist::History::new(2000);
    LASTCOL.store(-1, Ordering::SeqCst);
    BUFSTACK.lock().unwrap().clear();
    VICHGBUF.lock().unwrap().clear();
    *SRCH_STR.lock().unwrap() = None;
    LASTLINE.lock().unwrap().clear();
    LASTLL.store(0, Ordering::SeqCst);
    LASTCS.store(0, Ordering::SeqCst);
    CURCHANGE.store(0, Ordering::SeqCst);
    UNDO_CHANGENO.store(0, Ordering::SeqCst);
    UNDO_LIMITNO.store(0, Ordering::SeqCst);
    VIINSBEGIN.store(0, Ordering::SeqCst);
    YANKB.store(0, Ordering::SeqCst);
    YANKE.store(0, Ordering::SeqCst);
    YANKCS.store(0, Ordering::SeqCst);
    *KCT.lock().unwrap() = None;
    *vimarks().lock().unwrap() = [None; 27];
    REGION_ACTIVE.store(0, Ordering::SeqCst);
    PENDING_HOOKS.lock().unwrap().clear();
    RAW_LP.lock().unwrap().clear();
    RAW_RP.lock().unwrap().clear();
    *highlight().lock().unwrap() = super::zle_refresh::HighlightManager::new();
}

    /// Configure the terminal for ZLE input.
    /// Port of `zsetterm()` from Src/Zle/zle_main.c:210. The C source
    /// disables ICANON + ECHO, sets VMIN=1 / VTIME=0 (one-byte
    /// blocking reads), captures VEOF as `eofchar` for the empty-line
    /// EOF detection in zlecore (zle_main.c:1139), and disables TAB3
    /// output mapping plus VQUIT/VSUSP/VDSUSP so the keymap can rebind
    /// those control chars. Our Rust port covers the daily-driver
    /// subset: ICANON+ECHO off, VMIN/VTIME, and eofchar capture from
    // set up terminal                                                       // c:210
    /// VEOF. The flow-control + TAB3 + IXON disables and the
    /// fetchttyinfo/attachtty save state remain on the host side.
    pub fn zsetterm() -> io::Result<()> {                           // c:210
        // termios::FromRawFd is not used directly here — the path goes
        // through termios::Termios::from_fd which already opens the fd.
        let mut termios = termios::Termios::from_fd(crate::ported::zle::zle_main::TTYFD.load(std::sync::atomic::Ordering::SeqCst))?;

        // Capture VEOF before we mask it — zlecore checks lastchar
        // against eofchar for the empty-line EOF branch (zle_main.c:1139).
        let veof = termios.c_cc[termios::VEOF];
        if veof != 0 {
            EOFCHAR.store((veof) as i32, std::sync::atomic::Ordering::SeqCst);
        }

        // Disable canonical line input + echo so we receive raw keys.
        termios.c_lflag &= !(termios::ICANON | termios::ECHO);
        termios.c_cc[termios::VMIN] = 1;
        termios.c_cc[termios::VTIME] = 0;

        termios::tcsetattr(crate::ported::zle::zle_main::TTYFD.load(std::sync::atomic::Ordering::SeqCst), termios::TCSANOW, &termios)?;
        Ok(())
    }

    /// Push one byte back to the head of the input queue.
    /// Port of `ungetbyte(int ch)` from Src/Zle/zle_main.c:348. Used by
    /// keymap-trie resolution and `quoted-insert` to put back a byte
    /// the loop already read but isn't ready to consume.
    pub fn ungetbyte(ch: u8) {                                    // c:348
        crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().push_front(ch);
    }

    /// Push a byte slice back onto the input queue, preserving order.
    /// Port of `ungetbytes(char *s, int len)` from Src/Zle/zle_main.c:357. Iterates
    /// the slice in reverse so that a subsequent forward read returns
    /// `s[0]` first — matches the C source's `while(len--) ungetbyte(s[len])`
    /// pattern.
    /// WARNING: param names don't match C — Rust=(s) vs C=(s, len)
    pub fn ungetbytes(s: &[u8]) {
        for &b in s.iter().rev() {
            crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().push_front(b);
        }
    }

    /// Direct port of `static void calc_timeout(struct ztmout *tmoutp,
    /// long do_keytmout, int full)` from `Src/Zle/zle_main.c:454`.
    /// Picks the next read timeout based on do_keytmout + the
    /// timedfns list. Truncated to the keymap timeout subset until
    /// timedfns wiring lands.
    fn calc_timeout(do_keytmout: bool) -> ztmout {                    // c:454
        let kt = KEYTIMEOUT.load(std::sync::atomic::Ordering::SeqCst);
        if do_keytmout && kt > 0 {
            let exp = if kt > ZMAXTIMEOUT * 100 { ZMAXTIMEOUT * 100 } else { kt };
            ztmout {
                tp: ztmouttp::ZTM_KEY,
                exp100ths: exp as i64,
            }
        } else {
            ztmout {
                tp: ztmouttp::ZTM_NONE,
                exp100ths: 0,
            }
        }
    }

    /// Read one byte from the input queue (or stdin) with optional
    /// keymap-timeout semantics.
    /// Port of `raw_getbyte(long do_keytmout, char *cptr, int full)` from Src/Zle/zle_main.c:506. The C
    /// source consults `kungetct`/`kungetbuf` (our `unget_buf`) first,
    /// then drops to a poll/select wait against SHTTY honouring
    /// `do_keytmout * KEYTIMEOUT`. Returns None on timeout/EOF — the
    /// C source uses EOF as the same sentinel.
    /// WARNING: param names don't match C — Rust=(do_keytmout) vs C=(do_keytmout, cptr, full)
    pub fn raw_getbyte(do_keytmout: bool) -> Option<u8> {
        // Check unget buffer first
        if let Some(b) = crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front() {
            return Some(b);
        }

        let timeout = calc_timeout(do_keytmout);

        let timeout_duration = if timeout.tp != ztmouttp::ZTM_NONE {
            Some(Duration::from_millis((timeout.exp100ths * 10) as u64))
        } else {
            None
        };

        // Use poll/select to wait for input with timeout
        let mut buf = [0u8; 1];

        if let Some(dur) = timeout_duration {
            // Set up poll
            let start = Instant::now();
            loop {
                if start.elapsed() >= dur {
                    return None; // Timeout
                }

                // Try non-blocking read
                match try_read_byte(&mut buf) {
                    Ok(true) => return Some(buf[0]),
                    Ok(false) => {
                        // No data, sleep a bit and retry
                        std::thread::sleep(Duration::from_millis(10));
                    }
                    Err(_) => return None,
                }
            }
        } else {
            // No timeout requested. C zsh's `raw_getbyte()` here calls
            // `read(SHTTY, cptr, 1)` (zle_main.c:560) where SHTTY has
            // been put into raw mode (VMIN=1, VTIME=0, ICANON cleared)
            // by `zsetterm()` in zle_main.c:210. In that mode the read
            // returns one byte per keystroke. Outside ZLE, when stdin
            // is a TTY in canonical mode (e.g. unit tests, or zshrs not
            // yet inside a ZLE session), a bare `read` would block
            // until a full line is typed — which deadlocks tests like
            // `widget_universal_argument(empty unget_buf)` that expect
            // None when no input is pending. Detect that case via
            // `isatty + tcgetattr(ICANON)` and return None instead of
            // blocking; only honour the C-faithful blocking read when
            // we know the descriptor is in raw mode.
            use std::os::unix::io::AsRawFd;
            let fd = io::stdin().as_raw_fd();
            let is_tty = unsafe { libc::isatty(fd) } == 1;
            let in_raw_mode = if is_tty {
                let mut t: libc::termios = unsafe { std::mem::zeroed() };
                if unsafe { libc::tcgetattr(fd, &mut t) } == 0 {
                    (t.c_lflag & libc::ICANON) == 0
                } else {
                    false
                }
            } else {
                // Pipe / file / closed — `read` returns Ok(0) on EOF
                // immediately, so blocking is fine here too.
                true
            };
            if !in_raw_mode {
                return None;
            }
            match io::stdin().read(&mut buf) {
                Ok(1) => Some(buf[0]),
                _ => None,
            }
        }
    }

    /// Try to read a byte non-blocking
    fn try_read_byte(buf: &mut [u8]) -> io::Result<bool> {
        use std::os::unix::io::AsRawFd;

        let mut fds = [libc::pollfd {
            fd: io::stdin().as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        }];

        let ret = unsafe { libc::poll(fds.as_mut_ptr(), 1, 0) };

        if ret > 0 && (fds[0].revents & libc::POLLIN) != 0 {
            match io::stdin().read(buf) {
                Ok(1) => Ok(true),
                Ok(_) => Ok(false),
                Err(e) => Err(e),
            }
        } else {
            Ok(false)
        }
    }

    /// Read one byte from input with the kernel's CR/LF swap reversed.
    /// Port of `getbyte(long do_keytmout, int *timeout, int full)` from Src/Zle/zle_main.c:861. The C source's
    /// `\n` ↔ `\r` swap is the inverse of the IO mapping that
    /// zsetterm() installs (`tio.c_iflag |= INLCR | ICRNL`) so the
    /// keymap dispatcher always sees a consistent newline byte. The
    /// final byte is also stashed in `lastchar` for widgets that
    /// inspect what triggered them (digit-argument, vi-find-char).
    /// WARNING: param names don't match C — Rust=(do_keytmout) vs C=(do_keytmout, timeout, full)
    pub fn getbyte(do_keytmout: bool) -> Option<u8> {
        let b = raw_getbyte(do_keytmout)?;

        // Handle newline/carriage return translation
        // (The C code swaps \n and \r for typeahead handling)
        let b = if b == b'\n' {
            b'\r'
        } else if b == b'\r' {
            b'\n'
        } else {
            b
        };

        crate::ported::zle::compcore::LASTCHAR.store((b as ZleInt) as i32, std::sync::atomic::Ordering::SeqCst);
        Some(b)
    }

    /// Read one complete (possibly multi-byte) character from input.
    /// Port of `getfullchar(int do_keytmout)` from Src/Zle/zle_main.c:967. The C
    /// source delegates to `getrestchar()` (zle_main.c:990) for the
    /// wide-char assembly when the lead byte signals a UTF-8 sequence.
    /// Our Rust port reads continuation bytes directly until the UTF-8
    /// envelope is complete, then `str::from_utf8` produces the char.
    /// Updates `lastchar_wide` so widgets can inspect the triggering
    /// codepoint regardless of byte width.
    pub fn getfullchar(do_keytmout: bool) -> Option<char> {
        let b = getbyte(do_keytmout)?;

        // UTF-8 decoding
        if b < 0x80 {
            let c = b as char;
            crate::ported::zle::zle_main::LASTCHAR_WIDE.store((c as ZleInt) as i32, std::sync::atomic::Ordering::SeqCst);
            crate::ported::zle::zle_main::LASTCHAR_WIDE_VALID.store(1, std::sync::atomic::Ordering::SeqCst);
            return Some(c);
        }

        // Multi-byte UTF-8
        let mut bytes = vec![b];
        let expected_len = if b < 0xE0 {
            2
        } else if b < 0xF0 {
            3
        } else {
            4
        };

        while bytes.len() < expected_len {
            if let Some(next) = getbyte(true) {
                if (next & 0xC0) != 0x80 {
                    // Invalid continuation byte, unget and return error
                    ungetbyte(next);
                    break;
                }
                bytes.push(next);
            } else {
                break;
            }
        }

        if let Ok(s) = std::str::from_utf8(&bytes) {
            if let Some(c) = s.chars().next() {
                crate::ported::zle::zle_main::LASTCHAR_WIDE.store((c as ZleInt) as i32, std::sync::atomic::Ordering::SeqCst);
                crate::ported::zle::zle_main::LASTCHAR_WIDE_VALID.store(1, std::sync::atomic::Ordering::SeqCst);
                return Some(c);
            }
        }

        crate::ported::zle::zle_main::LASTCHAR_WIDE_VALID.store(0, std::sync::atomic::Ordering::SeqCst);
        None
    }

    /// Run the registered redraw hook (`zle-line-pre-redraw` in zsh).
    /// Port of `redrawhook()` from Src/Zle/zle_main.c — the C version looks
    /// up `Th(z_redrawhook)` and executes via `execzlefunc`. This Rust port
    /// queues the hook name on `pending_hooks` for the host to dispatch
    /// after the ZLE call returns; the comment at zle_utils.c:1764
    /// ("If anything here needs changing, see also redrawhook()") is the
    /// reason this matches `zle_call_hook`'s queueing approach exactly.
    pub fn redrawhook() {
        crate::ported::zle::zle_main::PENDING_HOOKS.lock().unwrap()
            .push(("zle-line-pre-redraw".to_string(), None));
    }

    /// Core ZLE loop.
    /// Port of `zlecore()` from Src/Zle/zle_main.c:1110. The C source
    /// loops until `done || errflag || exit_pending`, calling
    /// `getkeycmd()` to resolve a multi-byte key sequence into a Thingy,
    /// dispatching via `execzlefunc()`, then running `handleprefixes()`,
    /// vi-cursor cleanup, `handleundo()`, and `redrawhook()` between
    /// iterations. This Rust port mirrors that flow with our single-char
    /// keymap lookup as the resolver — multi-byte sequences flow through
    /// `getfullchar` + UTF-8 decode, while bound key sequences (e.g.
    /// `^X^E`) currently rely on the binding's first byte; the
    /// keymap-trie walk is a follow-up port.
    pub fn zlecore() {                                              // c:1110
        crate::ported::zle::zle_misc::DONE.store(0, std::sync::atomic::Ordering::SeqCst);

        while crate::ported::zle::zle_misc::DONE.load(std::sync::atomic::Ordering::SeqCst) == 0 {
            // EOF handling: empty line + Ctrl-D (eofchar) => terminate.
            // Mirrors zle_main.c:1139-1150 (lastchar == eofchar guard).
            // We can only check this *after* reading a char, so the
            // detection lives below.

            // Resolve the next bound widget via multi-byte keymap lookup.
            // Mirrors zle_main.c:1136 `bindk = getkeycmd();` — our
            // get_key_cmd walks the keymap trie reading bytes until it
            // hits a leaf or a non-prefix.
            let thingy = match get_key_cmd() {
                Some(t) => t,
                None => {
                    EOFSENT.store(1, std::sync::atomic::Ordering::SeqCst);
                    crate::ported::zle::zle_misc::DONE.store(1, std::sync::atomic::Ordering::SeqCst);
                    continue;
                }
            };

            // EOF on empty line: matches C's eofchar branch
            // (zle_main.c:1139-1150 — guarded by ZLRF_IGNOREEOF too).
            if crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) == 0
                && crate::ported::zle::compcore::LASTCHAR.load(std::sync::atomic::Ordering::SeqCst) == EOFCHAR.load(std::sync::atomic::Ordering::SeqCst)
                && (crate::ported::zle::zle_main::ZLEREADFLAGS.load(std::sync::atomic::Ordering::SeqCst) & crate::ported::zsh_h::ZLRF_HISTORY) != 0
            {
                EOFSENT.store(1, std::sync::atomic::Ordering::SeqCst);
                crate::ported::zle::zle_misc::DONE.store(1, std::sync::atomic::Ordering::SeqCst);
                continue;
            }

            *crate::ported::zle::zle_main::LBINDK.lock().unwrap() = crate::ported::zle::zle_main::BINDK.lock().unwrap().take();
            *crate::ported::zle::zle_main::BINDK.lock().unwrap() = Some(thingy.clone());

            if let Some(widget) = &thingy.widget {
                execute_widget(widget);
            } else {
                // The Thingy resolved but has no widget — matches the C
                // `handlefeep` call at zle_main.c:1152 when execzlefunc
                // returns failure.
                handle_feep();
            }

            // Post-widget processing matches zle_main.c:1156-1167:
            //   handleprefixes()  → promote TMULT, otherwise reset
            //   vi cursor adjust  → don't sit on '\n' in vi cmd mode
            //   handleundo()      → done in execute_widget
            //   redrawhook()      → queue zle-line-pre-redraw
            handleprefixes();
            if in_vi_cmd_mode()
                && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > findbol()
                && (crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) == crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                    || crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied() == Some('\n'))
                && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0
            {
                crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
            }
            redrawhook();

            // Refresh display if any widget asked for it.
            if crate::ported::zle::zle_main::ZLE_RESET_NEEDED.load(std::sync::atomic::Ordering::SeqCst) != 0 {
                zrefresh();
                crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(0, std::sync::atomic::Ordering::SeqCst);
            }
        }
    }

    /// Are we currently in the vi command keymap?
    /// Port of `invicmdmode()` from Src/Zle/zle_main.c (the C macro just
    /// compares the active keymap pointer against `vicmd`).
    pub fn in_vi_cmd_mode() -> bool {
        *crate::ported::zle::zle_keymap::curkeymapname() == "vicmd"
    }

    /// Read a multi-byte key sequence from input and resolve it against
    /// the current keymap. Returns the bound `Thingy` or `None` on EOF.
    ///
    /// Port of `getkeymapcmd(Keymap km, Thingy *funcp, char **strp)` from Src/Zle/zle_keymap.c:1581 + the
    /// thin `getkeycmd()` wrapper at zle_keymap.c:1768. The C source
    /// reads bytes into a `keybuf`, looks up the partial sequence after
    /// each byte, tracks the longest prefix that hit a binding, and
    /// stops when either (a) the current sequence is no longer a prefix
    /// of any binding, or (b) the input read times out while waiting
    /// for the next byte. Excess bytes past the matched prefix are
    /// unget back into the input buffer.
    ///
    /// Simplified compared to the C source: skips the CSI-sequence
    /// special handling at zle_keymap.c:1645 and the
    /// `t_executenamedcmd` redirection at zle_keymap.c:1787 — both are
    /// host-driven concerns that the bin can layer on top.
    pub fn get_key_cmd() -> Option<super::zle_thingy::Thingy> {
        let km = {
            let local = crate::ported::zle::zle_keymap::LOCALKEYMAP.lock().unwrap().clone();
            let cur = crate::ported::zle::zle_keymap::curkeymap.lock().unwrap().clone();
            local.or(cur)?
        };
        let mut buf: Vec<u8> = Vec::with_capacity(8);
        let mut last_match: Option<super::zle_thingy::Thingy> = None;
        let mut last_match_len = 0usize;

        loop {
            // Read one byte. Use timed read once we have a partial match
            // (a prefix that already hit a binding); otherwise block.
            let do_keytmout = last_match.is_some();
            let b = getbyte(do_keytmout)?;
            buf.push(b);

            // Look up the current buffer.
            let (current_match, is_prefix) = if buf.len() == 1 {
                let m = km.first[b as usize].clone();
                let pfx = km
                    .multi
                    .keys()
                    .any(|k| k.len() > 1 && k[0] == b);
                (m, pfx)
            } else {
                let entry = km.multi.get(&buf[..]);
                let m = entry.and_then(|e| e.bind.clone());
                let pfx = entry.map(|e| e.prefixct > 0).unwrap_or(false);
                (m, pfx)
            };

            if let Some(t) = current_match {
                last_match = Some(t);
                last_match_len = buf.len();
            }

            // If this sequence is no longer a prefix of any binding,
            // stop. C's getkeymapcmd:1614 makes the same call —
            // keep reading only while ispfx is true.
            if !is_prefix {
                break;
            }
        }

        // Unget any bytes past the matched prefix so the next read sees
        // them. Mirrors the lastlen / keybuflen accounting in
        // zle_keymap.c:1619.
        if last_match.is_some() && buf.len() > last_match_len {
            let extra = buf[last_match_len..].to_vec();
            ungetbytes(&extra);
        }

        last_match
    }

    /// Execute a widget. Port of `execzlefunc(Thingy func, char **args, int set_bindk, int set_lbindk)` from Src/Zle/zle_main.c:1420.
    ///
    /// The C source manages a few per-widget side effects we replicate
    /// here:
    ///   * `lastcol = -1` reset for any widget that isn't flagged
    ///     `LASTCOL` (zle_main.c:1476). The vertical-motion widgets use
    ///     this to maintain a sticky column across `up-line` / `down-line`.
    ///   * `lastcmd = widget.flags` unless the widget is `NOTCOMMAND`
    ///     (zle_main.c:1497). The yank-pop widget consults this to know
    ///     whether the previous widget was a yank.
    ///   * `handleundo()` snapshot pre-call + `mkundoent()` capture
    ///     post-call (zle_main.c calls `handleundo()` from the zlecore
    ///     loop after each widget).
    fn execute_widget(widget: &widget) {
        // Reset sticky column unless the widget keeps it.
        if (widget.flags & ZLE_LASTCOL) == 0 {
            crate::ported::zle::zle_main::LASTCOL.store(-1, std::sync::atomic::Ordering::SeqCst);
        }

        // Snapshot the line so mkundoent can diff it post-widget.
        // Port of setlastline()/handleundo() framing in zle_main.c:1161.
        handleundo();

        match &widget.u {
            super::zle_h::WidgetImpl::Internal(f) => {
                let _ = f(&[]);
            }
            super::zle_h::WidgetImpl::UserFunc(name) => {
                // User-defined widget (`zle -N name shell-fn`): the C
                // source dispatches via execzlefunc() at zle_main.c:1502
                // through executenamedfunc which calls the bound shell
                // function. Direct dispatch through the canonical
                // execzlefunc path now invokes the function via
                // fusevm_bridge inside this same key-loop call frame,
                // so widget side-effects (BUFFER/CURSOR/etc.) land on
                // the live ZLE state synchronously rather than waiting
                // for a host drain pass.
                let _ = crate::ported::zle::zle_main::execzlefunc(name, &[]);
            }
            _ => {}
        }

        // Update lastcmd for yank-pop / next-widget chains, unless the
        // widget is NOTCOMMAND (digit-arg, prefix, etc.) — zle_main.c:1497.
        if (widget.flags & ZLE_NOTCOMMAND) == 0 {
            LASTCMD.store(widget.flags as u32, std::sync::atomic::Ordering::SeqCst);
        }

        // Capture the change (if any) into the undo stack. undo/redo widgets
        // call mkundoent themselves, so a no-op diff here is harmless.
        mkundoent();
    }

    /// Self-insert character (internal, used by zlecore)
    fn do_self_insert(c: char) {
        if (crate::ported::zle::zle_main::INSMODE.load(std::sync::atomic::Ordering::SeqCst) != 0) {
            // Insert mode
            crate::ported::zle::zle_main::ZLELINE.lock().unwrap().insert(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst), c);
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            crate::ported::zle::zle_main::ZLELL.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        } else {
            // Overwrite mode
            if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)] = c;
            } else {
                crate::ported::zle::zle_main::ZLELINE.lock().unwrap().push(c);
                crate::ported::zle::zle_main::ZLELL.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            }
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Run a line edit and return the user's accepted line.
    /// Port of `zleread(char **lp, char **rp, int flags, int context, char *init, char *finish)` from Src/Zle/zle_main.c:1216 — the
    /// canonical entry point for "read one line interactively". The C
    /// source's full chain is: setup tty + signals → run zle-line-init
    /// hook → zlecore loop until done → run zle-line-finish hook →
    /// restore tty + return the line. Our Rust port stashes the
    // - finish: "zle-line-finish"                                          // c:1216
    /// prompt templates, expands them, sets the read flags + context,
    /// then enters zlecore; the host (bin) handles the line-init /
    /// line-finish hooks via pending_hooks.
    /// WARNING: param names don't match C — Rust=(lprompt, rprompt, flags, context) vs C=(lp, rp, flags, context, init, finish)
    pub fn zleread(                                                          // c:1216
        lprompt: &str,
        rprompt: &str,
        flags: i32,
        context: i32,
    ) -> io::Result<String> {
        // Stash the unexpanded templates so reexpandprompt() can re-run
        // expansion later. C zsh saves these in the global raw_lp/raw_rp
        // slots — the Rust port keeps the same shape as file-scope
        // `RAW_LP`/`RAW_RP` statics (zle_main.rs).
        *crate::ported::zle::zle_main::RAW_LP.lock().unwrap() = lprompt.to_string();
        *crate::ported::zle::zle_main::RAW_RP.lock().unwrap() = rprompt.to_string();
        *crate::ported::zle::zle_main::LPROMPT.lock().unwrap() = crate::prompt::expand_prompt(lprompt);
        *crate::ported::zle::zle_main::RPROMPT.lock().unwrap() = crate::prompt::expand_prompt(rprompt);
        crate::ported::zle::zle_main::ZLEREADFLAGS.store(flags, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::ZLECONTEXT.store(context, std::sync::atomic::Ordering::SeqCst);

        // Initialize line
        crate::ported::zle::zle_main::ZLELINE.lock().unwrap().clear();
        crate::ported::zle::zle_main::ZLECS.store(0, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::ZLELL.store(0, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::MARK.store(0, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_misc::DONE.store(0, std::sync::atomic::Ordering::SeqCst);

        // Set up terminal
        zsetterm()?;

        // Display prompt — port of `write_loop(SHTTY, lprompt, lpromptlen)`
        // at `Src/Zle/zle_main.c:1321`. C writes the expanded prompt
        // directly to the shell-output fd; we mirror that, with stdout
        // fallback for non-interactive paths.
        {
            use std::sync::atomic::Ordering;
            let fd = crate::ported::init::SHTTY.load(Ordering::Relaxed);
            let out = if fd >= 0 { fd } else { 1 };
            let _ = crate::ported::utils::write_loop(out, lprompt.as_bytes());
        }

        // Enter core loop
        zlecore();

        // Return the line
        Ok(crate::ported::zle::zle_main::ZLELINE.lock().unwrap().iter().collect())
    }

    /// Initialize ZLE modifiers
    /// Reset zmod to its starting state (port of `initmodifier()` from
    /// Src/Zle/zle_main.c:1604). The C source sets mult=1, tmult=1,
    /// vibuf=0, base=10 — `tmult=1` is what makes successive C-u
    /// invocations multiply (1→4→16→64) instead of staying at 0.
    pub fn initmodifier() {
        *crate::ported::zle::zle_main::ZMOD.lock().unwrap() = modifier {
            flags: 0,
            mult: 1,
            tmult: 1,
            vibuf: 0,
            base: 10,
        };
    }

    /// Handle the prefix-command flag after each widget invocation.
    /// Port of `handleprefixes()` from Src/Zle/zle_main.c:1618. If
    /// `prefixflag` is set the previous widget was a prefix (e.g.
    /// digit-argument, universal-argument); promote the temp multiplier
    /// (TMULT) into the live multiplier (MULT) and clear the flag. If
    /// `prefixflag` is *not* set we entered this loop iteration after a
    /// non-prefix widget, so reset the modifier to its default state via
    /// `initmodifier`.
    pub fn handleprefixes() {
        if (crate::ported::zle::zle_main::PREFIXFLAG.load(std::sync::atomic::Ordering::SeqCst) != 0) {
            crate::ported::zle::zle_main::PREFIXFLAG.store(0, std::sync::atomic::Ordering::SeqCst);
            if crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_TMULT != 0 {
                crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags &= !MOD_TMULT;
                crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_MULT;
                let mut __g_zmod = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
                __g_zmod.mult = __g_zmod.tmult;
            }
        } else {
            initmodifier();
        }
    }

    /// Move past the ZLE display so non-ZLE output (a child command's
    /// output, an error message, etc.) doesn't overwrite the prompt.
    /// Port of `trashzle()` from Src/Zle/zle_main.c:2068. The C source
    /// runs a final zrefresh, applies the prompt's text attributes,
    /// moves to the bottom of the displayed lines (`moveto(nlnct, 0)`),
    /// optionally clears to end-of-display via the TCCLEAREOD termcap,
    /// emits postedit if set, then flags `resetneeded` and restores tty
    /// state. Our simplified version does the equivalent for a
    /// single-line display: emit \\r + clear-to-EOL, flush stdout, then
    /// arm `resetneeded` so the next zlecore iteration redraws.
    pub fn trashzle() {                                             // c:2068
        // c:2089 — emit `\r` then `cleareol` (CSI K) to wipe the
        //          current line. C drives this via tcout(TCCR) +
        //          tcout(TCCLEAREOL); our simplified Rust uses the
        //          raw CSI directly.
        // c:2091 — applytextattributes(0) → CSI 0m resets all SGR.
        // Write both blobs to SHTTY (stdout fallback) so the prompt
        // teardown reaches the same destination as the prompt write.
        use std::sync::atomic::Ordering;
        let fd = crate::ported::init::SHTTY.load(Ordering::Relaxed);
        let out = if fd >= 0 { fd } else { 1 };
        let _ = crate::ported::utils::write_loop(out, b"\r\x1b[K\x1b[0m");
        crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Mark the prompt as needing a re-expand on next refresh.
    /// Port of `resetprompt(UNUSED(char **args))` from Src/Zle/zle_main.c:2048. The C
    /// source calls `zle_resetprompt()` which sets `resetneeded` and
    /// `clearflag`; our simplified version just flips `resetneeded`
    /// (clearflag's TCCLEAREOD path isn't wired through this crate).
    /// WARNING: param names don't match C — Rust=() vs C=(args)
    pub fn resetprompt() {
        crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Re-run prompt expansion against the saved templates.
    /// Port of `reexpandprompt()` from Src/Zle/zle_main.c — used after
    /// events that change values referenced by prompt escapes (PWD,
    /// command status, jobs count, sigwinch). Re-expands `lprompt_raw`
    /// and `rprompt_raw` via `prompt::expand_prompt` with a fresh
    /// `PromptContext` so escapes pick up the latest env / state.
    pub fn reexpandprompt() {
        // PromptContext was removed from prompt.rs's public surface;
        // expand_prompt() takes only the format string now and reads
        // env/state internally.
        let raw_lp = crate::ported::zle::zle_main::RAW_LP.lock().unwrap().clone();
        let raw_rp = crate::ported::zle::zle_main::RAW_RP.lock().unwrap().clone();
        *crate::ported::zle::zle_main::LPROMPT.lock().unwrap() = crate::prompt::expand_prompt(&raw_lp);
        *crate::ported::zle::zle_main::RPROMPT.lock().unwrap() = crate::prompt::expand_prompt(&raw_rp);
        crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Run a nested edit session — used by user widgets to invoke the
    /// editor recursively (e.g. read a sub-line for completion search).
    ///
    /// Port of `recursiveedit(UNUSED(char **args))` from Src/Zle/zle_main.c:1974. The C
    /// source increments `zle_recursive`, calls `redrawhook()` +
    /// `zrefresh()` to ensure the screen reflects current state,
    /// re-enters `zlecore()`, then resets `errflag`/`done`/`eofsent`
    /// so the parent edit session continues after the recursive call
    /// returns. Returns 1 if the inner edit aborted with errflag set,
    /// matching the C `locerror` path at zle_main.c:1992.
    pub fn recursive_edit() -> i32 {
        crate::ported::zle::zle_main::ZLE_RECURSIVE.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let old_done = crate::ported::zle::zle_misc::DONE.load(std::sync::atomic::Ordering::SeqCst) != 0;
        let old_eofsent = EOFSENT.load(std::sync::atomic::Ordering::SeqCst);

        // Mirror zle_main.c:1984-1986 — refresh before entering the
        // sub-loop so the user sees current state on enter.
        redrawhook();
        zrefresh();

        crate::ported::zle::zle_misc::DONE.store(0, std::sync::atomic::Ordering::SeqCst);
        EOFSENT.store(0, std::sync::atomic::Ordering::SeqCst);
        zlecore();

        // C source resets errflag/done/eofsent on exit (zle_main.c:1993)
        // so the outer loop continues. We don't have an errflag global,
        // so the local-error signal collapses to "did the inner exit
        // via abort_line?" — approximated by checking eofsent.
        let locerror = EOFSENT.load(std::sync::atomic::Ordering::SeqCst);

        crate::ported::zle::zle_misc::DONE.store(if old_done { 1 } else { 0 }, std::sync::atomic::Ordering::SeqCst);
        EOFSENT.store(old_eofsent, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::ZLE_RECURSIVE.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);

        locerror
    }

    /// Mark the line as accepted; zlecore will exit on the next iteration.
    /// Port of `acceptline(UNUSED(char **args))` from Src/Zle/zle_misc.c:401 — the C source
    /// just sets the global `done` flag.
    pub fn finish_line() {
        crate::ported::zle::zle_misc::DONE.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Abort the current line edit and exit zlecore with an empty buffer.
    /// Port of the Ctrl-C / send-break exit path from Src/Zle/zle_misc.c:1144
    /// (`sendbreak`) combined with the abort cleanup at zle_main.c:1162
    /// (the `errflag |= ERRFLAG_ERROR; break;` arm). The C source uses
    /// errflag globals to communicate the abort; we model it with a bool.
    pub fn abort_line() {
        crate::ported::zle::zle_main::ZLELINE.lock().unwrap().clear();
        crate::ported::zle::zle_main::ZLECS.store(0, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::ZLELL.store(0, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_misc::DONE.store(1, std::sync::atomic::Ordering::SeqCst);
    }


    // `save_keymap` / `restore_keymap` + `SavedKeymap` struct deleted —
    // none of those names exist in Src/Zle/zle_main.c. C handles
    // keymap save/restore by reading/writing the `curkeymap` and
    // `localkeymap` globals directly inside the bindkey paths.

    /// Describe key briefly
    /// Port of describekeybriefly(UNUSED(char **args)) from zle_main.c
    pub fn describe_key_briefly() {
        if let Some(c) = getfullchar(false) {
            let thingy = if c as u32 > 255 {
                None
            } else {
                let km = crate::ported::zle::zle_keymap::LOCALKEYMAP
                    .lock()
                    .unwrap()
                    .clone()
                    .or_else(|| {
                        crate::ported::zle::zle_keymap::curkeymap.lock().unwrap().clone()
                    });
                km.and_then(|k| k.first[c as usize].clone())
            };
            if let Some(thingy) = thingy {
                display_msg(&format!("{} is bound to {}", c, thingy.nam));
            } else {
                display_msg(&format!("{} is not bound", c));
            }
        }
    }

    /// Where is command
    /// Port of whereis(UNUSED(char **args)) from zle_main.c
    pub fn whereis(widget_name: &str) -> Vec<String> {
        let mut bindings = Vec::new();
        let tab = crate::ported::zle::zle_keymap::keymapnamtab().lock().unwrap();
        for (name, node) in tab.iter() {
            let km = &node.keymap;
            // Check single char bindings
            for (i, opt) in km.first.iter().enumerate() {
                if let Some(t) = opt {
                    if t.nam == widget_name {
                        bindings.push(format!("{}:{}", name, super::zle_utils::printbind(&[i as u8])));
                    }
                }
            }

            // Check multi-char bindings
            for (seq, kb) in &km.multi {
                if let Some(ref t) = kb.bind {
                    if t.nam == widget_name {
                        bindings.push(format!("{}:{}", name, super::zle_utils::printbind(seq)));
                    }
                }
            }
        }

        bindings
    }

    /// Execute an immortal (built-in) function
    /// Port of `execimmortal(Thingy func, char **args)` from `Src/Zle/zle_main.c`.
    /// WARNING: param names don't match C — Rust=(name) vs C=(func, args)
    #[allow(unused_variables)]


    /// Execute a ZLE function by name
    /// Port of `execzlefunc(Thingy func, char **args, int set_bindk, int set_lbindk)` from `Src/Zle/zle_main.c`.
    /// WARNING: param names don't match C — Rust=(name, _args) vs C=(func, args, set_bindk, set_lbindk)
    #[allow(unused_variables)]


    /// Break read (for signals)
    /// Port of `breakread(int fd, char *buf, int n)` from `Src/Zle/zle_main.c`.
    /// WARNING: param names don't match C — Rust=() vs C=(fd, buf, n)


    /// Handle before trap
    /// Port of `zlebeforetrap(UNUSED(Hookdef dummy), UNUSED(void *dat))` from `Src/Zle/zle_main.c`.
    /// WARNING: param names don't match C — Rust=() vs C=(dummy, dat)


    /// Handle after trap
    /// Port of `zleaftertrap(UNUSED(Hookdef dummy), UNUSED(void *dat))` from `Src/Zle/zle_main.c`.
    /// WARNING: param names don't match C — Rust=() vs C=(dummy, dat)


    /// ZLE reset prompt
    /// Port of zle_resetprompt() from zle_main.c  


    /// Display message to user (internal)
    fn display_msg(msg: &str) {
        eprintln!("{}", msg);
    }

    /// The expanded left prompt string (post-`reexpandprompt`).
    pub fn prompt() -> String {
        crate::ported::zle::zle_main::LPROMPT.lock().unwrap().clone()
    }

    /// The expanded right prompt string (RPS1-equivalent).
    pub fn rprompt() -> String {
        crate::ported::zle::zle_main::RPROMPT.lock().unwrap().clone()
    }

    /// Set prompt
    pub fn set_prompt(prompt: &str) {
        *crate::ported::zle::zle_main::LPROMPT.lock().unwrap() = prompt.to_string();
        crate::ported::zle::zle_main::ZLE_RESET_NEEDED.store(1, std::sync::atomic::Ordering::SeqCst);
    }

    /// Get repeat count
    pub fn get_mult() -> i32 {
        if crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0 {
            crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult
        } else {
            1
        }
    }

    /// Toggle negative argument flag
    pub fn toggle_neg_arg() {
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags ^= MOD_NEG;
    }

    /// Check if negative argument
    pub fn is_neg() -> bool {
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_NEG != 0
    }

    /// Vi command mode flag
    pub fn is_vicmd() -> bool {
        *crate::ported::zle::zle_keymap::curkeymapname() == "vicmd"
    }

    /// Vi insert mode flag
    pub fn is_viins() -> bool {
        *crate::ported::zle::zle_keymap::curkeymapname() == "viins"
    }

    /// Emacs mode flag
    pub fn is_emacs() -> bool {
        let n = crate::ported::zle::zle_keymap::curkeymapname();
        *n == "emacs" || *n == "main"
    }

    /// Check if last command was yank
    pub fn was_yank() -> bool {
        (LASTCMD.load(std::sync::atomic::Ordering::SeqCst) as i32 & ZLE_YANK) != 0
    }


// `SavedKeymap` deleted — Rust-invented helper for `save_keymap` /
// `restore_keymap` (also deleted above). No C counterpart.

// `acceptline(&str) -> Option<Widget>` deleted — Rust-only helper
// that just wrapped `Widget::builtin(name)` in `Some(...)`. Callers
// (execimmortal, execzlefunc) inlined to use `Widget::builtin`
// directly. The real C `acceptline()` (zle_misc.c:401) takes
// `char **args` and returns int; its Rust port lives at
// `zle_misc.rs:708` (the legit free fn).

// `vared_zle_run` deleted — Rust-only helper with no C counterpart
// (the C `bin_vared` inlines its zleread call at c:1839-1860). The
// fake helper had no callers and bundled a `VaredOpts` struct
// (also deleted) that doesn't exist in C.

/// Direct port of `bin_vared(char *name, char **args, Options ops, UNUSED(int func))` from `Src/Zle/zle_main.c:1678`.
/// C signature: `static int bin_vared(char *name, char **args,
/// Options ops, UNUSED(int func))`.
/// BUILTIN spec at zle_main.c:2186 takes `"AaceghM:m:p:r:i:f:"`.
/// WARNING: param names don't match C — Rust=(name, args, _func) vs C=(name, args, ops, func)
pub fn bin_vared(name: &str, args: &[String],                                // c:1678
                 ops: &crate::ported::zsh_h::options, _func: i32) -> i32 {
    use crate::ported::zsh_h::{OPT_ISSET, OPT_ARG_SAFE, PM_SCALAR, PM_ARRAY, PM_HASHED};
    use crate::ported::utils::zwarnnam;
    let mut type_: u32 = PM_SCALAR;                                          // c:1685
    // c:1691 — `if ((interact && unset(USEZLE)) || !strcmp(term, "emacs"))`.
    let term = std::env::var("TERM").unwrap_or_default();
    if term == "emacs" {                                                     // c:1691
        zwarnnam(name, "ZLE not enabled");                                   // c:1692
        return 1;                                                            // c:1693
    }
    // c:1695 — refuse recursive ZLE.
    if crate::ported::builtins::sched::zleactive.load(                       // c:1695
        std::sync::atomic::Ordering::Relaxed) != 0 {
        zwarnnam(name, "ZLE cannot be used recursively (yet)");              // c:1696
        return 1;                                                            // c:1697
    }
    // c:1700 — `warn_flags = OPT_ISSET(ops, 'g') ? 0 : ASSPM_WARN;` —
    // affects setsparam path; tracked but not yet wired through.
    let _warn_flags = if OPT_ISSET(ops, b'g') { 0 } else { 1 };              // c:1700 ASSPM_WARN
    if OPT_ISSET(ops, b'A') {                                                // c:1701
        if OPT_ISSET(ops, b'a') {                                            // c:1703
            zwarnnam(name, "specify only one of -a and -A");                 // c:1705
            return 1;                                                        // c:1706
        }
        type_ = PM_HASHED;                                                   // c:1708
    } else if OPT_ISSET(ops, b'a') {                                         // c:1710
        type_ = PM_ARRAY;                                                    // c:1711
    }
    let p1 = OPT_ARG_SAFE(ops, b'p').unwrap_or("");                          // c:1712
    let p2 = OPT_ARG_SAFE(ops, b'r').unwrap_or("");                          // c:1713
    let main_keymapname  = OPT_ARG_SAFE(ops, b'M').unwrap_or("");            // c:1714
    let vicmd_keymapname = OPT_ARG_SAFE(ops, b'm').unwrap_or("");            // c:1715
    let init             = OPT_ARG_SAFE(ops, b'i').unwrap_or("");            // c:1716
    let finish           = OPT_ARG_SAFE(ops, b'f').unwrap_or("");            // c:1717
    let _ = (main_keymapname, vicmd_keymapname, init, finish);
    if type_ != PM_SCALAR && !OPT_ISSET(ops, b'c') {                         // c:1719
        zwarnnam(name,                                                       // c:1720
            &format!("-{} ignored", if type_ == PM_ARRAY { "a" } else { "A" }));
    }
    // c:1724 — `s = args[0];`
    if args.is_empty() {
        zwarnnam(name, "not enough arguments");
        return 1;
    }
    let varname = &args[0];                                                  // c:1724
    // c:1725 queue_signals.
    crate::ported::mem::queue_signals();
    // c:1726 — `fetchvalue(&vbuf, &s, ...)`. C looks the param up in
    //          paramtab; for -c (create), allow missing variable;
    //          otherwise error. Was reading the OS env via
    //          `std::env::var` plus an invented `__zshrs_array`
    //          fallback that never matches anything real. Read
    //          paramtab directly so scalar + array + hashed params
    //          all count as "exists".
    let exists = {
        let tab = crate::ported::params::paramtab().read().unwrap();
        tab.contains_key(varname)
    };
    if !exists && !OPT_ISSET(ops, b'c') {                                    // c:1728
        crate::ported::mem::unqueue_signals();                               // c:1729
        zwarnnam(name, &format!("no such variable: {}", varname));           // c:1730
        return 1;                                                            // c:1731
    }
    crate::ported::mem::unqueue_signals();
    // c:1841-1860 — zleread(ZLCON_VARED) drives the actual edit. Static-
    // link path: the live ZLE editor isn't reachable from this lib-side
    // entrypoint. Delegate to vared_zle_run when the ZLE entrypoint is
    // wired into the executor; until then, fall back to a stdin read so the
    // builtin is functional in non-interactive scripts that pipe input.
    let prompt = if !p1.is_empty() { p1.to_string() } else { String::new() };
    let rprompt = if !p2.is_empty() { p2.to_string() } else { String::new() };
    // c:1841-1846 — `zleread` writes lprompt + current-value + rprompt
    //                to shout (the controlling tty), then takes input.
    //                Was a fake: prompt→stderr / current→stdout via
    //                `eprint!`/`print!`, AND `current` came from
    //                `std::env::var` instead of `getsparam`. Both
    //                routes now match C: SHTTY (stdout fallback) and
    //                paramtab.
    let current = crate::ported::params::getsparam(varname).unwrap_or_default();
    {
        use std::sync::atomic::Ordering;
        let fd = crate::ported::init::SHTTY.load(Ordering::Relaxed);
        let out = if fd >= 0 { fd } else { 1 };
        if !prompt.is_empty() {
            let _ = crate::ported::utils::write_loop(out, prompt.as_bytes());
        }
        let _ = crate::ported::utils::write_loop(out, current.as_bytes());
        if !rprompt.is_empty() {
            let _ = crate::ported::utils::write_loop(out, rprompt.as_bytes());
        }
    }
    let mut input = String::new();
    if std::io::stdin().read_line(&mut input).is_ok() {                      // c:1841 zleread fallback
        let value = input.trim_end_matches('\n').to_string();
        crate::ported::params::setsparam(varname, &value);           // c:1893 setsparam
        return 0;                                                            // c:1903
    }
    1
}

// `VaredOpts` deleted — Rust-invented options struct that bundled
// the per-flag args bin_vared parses (-p/-r/-e/-h). C reads them
// from `Options ops` via OPT_ARG/OPT_ISSET inline, no separate
// struct. The fake had no users after `vared_zle_run` was deleted.

// `zle_main_entry` / `ZleOperation` / `ZleData` deleted — none of
// these names exist in Src/Zle/zle_main.c. The C module entry
// points are `setup_` (c:2123), `boot_`, `cleanup_`, `finish_`
// (the standard module-loader callbacks).

/// Module for termios operations
mod termios {
    pub use libc::{ECHO, ICANON, TCSANOW, VEOF, VMIN, VTIME};
    use std::io;
    use std::os::unix::io::RawFd;

    #[derive(Clone)]
    pub struct Termios {
        inner: libc::termios,
    }

    impl Termios {
        pub fn from_fd(fd: RawFd) -> io::Result<Self> {
            let mut termios = std::mem::MaybeUninit::uninit();
            let ret = unsafe { libc::tcgetattr(fd, termios.as_mut_ptr()) };
            if ret != 0 {
                return Err(io::Error::last_os_error());
            }
            Ok(Termios {
                inner: unsafe { termios.assume_init() },
            })
        }
    }

    impl std::ops::Deref for Termios {
        type Target = libc::termios;
        fn deref(&self) -> &Self::Target {
            &self.inner
        }
    }

    impl std::ops::DerefMut for Termios {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.inner
        }
    }

    /// Apply the given termios settings to the fd.
    /// Thin libc wrapper. Equivalent to the `settyinfo()` helper at
    /// Src/utils.c which fronts the same `tcsetattr(3)` call zsh
    /// uses to install / restore tty modes around `zsetterm` and
    /// `trashzle`.
    pub fn tcsetattr(fd: RawFd, action: i32, termios: &Termios) -> io::Result<()> {
        let ret = unsafe { libc::tcsetattr(fd, action, &termios.inner) };
        if ret != 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    }
}

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

    #[test]
    fn ztmouttp_discriminant_values() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:401-428 — sequential 0..=3.
        assert_eq!(ztmouttp::ZTM_NONE as i32, 0);
        assert_eq!(ztmouttp::ZTM_KEY  as i32, 1);
        assert_eq!(ztmouttp::ZTM_FUNC as i32, 2);
        assert_eq!(ztmouttp::ZTM_MAX  as i32, 3);
    }

    #[test]
    fn ztmout_default_carries_none_type() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let t = ztmout { tp: ztmouttp::ZTM_NONE, exp100ths: 0 };
        assert_eq!(t.tp, ztmouttp::ZTM_NONE);
    }

    #[test]
    fn findfunc_default_is_empty() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:1927 — fresh state: no func, zero hits, no msg.
        let f = findfunc::default();
        assert_eq!(f.func, None);
        assert_eq!(f.found, 0);
        assert!(f.msg.is_empty());
    }

    #[test]
    fn findfunc_can_accumulate_message() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let mut f = findfunc { func: Some(42), found: 0, msg: String::new() };
        f.found += 1;
        f.msg.push_str(" is on KEY1");
        assert_eq!(f.found, 1);
        assert!(f.msg.contains("is on"));
    }
}

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

    #[test]
    fn handleprefixes_promotes_tmult_to_mult_when_prefixflag_set() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_TMULT;
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().tmult = 7;
        crate::ported::zle::zle_main::PREFIXFLAG.store(1, std::sync::atomic::Ordering::SeqCst);
        handleprefixes();
        assert!(crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0);
        assert!(!crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_TMULT != 0);
        assert_eq!(crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult, 7);
        assert!(crate::ported::zle::zle_main::PREFIXFLAG.load(std::sync::atomic::Ordering::SeqCst) == 0);
    }

    #[test]
    fn handleprefixes_resets_modifier_when_prefixflag_cleared() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_MULT;
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult = 9;
        crate::ported::zle::zle_main::PREFIXFLAG.store(0, std::sync::atomic::Ordering::SeqCst);
        handleprefixes();
        // initmodifier resets to defaults: mult=1, no flags.
        assert_eq!(crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult, 1);
        assert!(!crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0);
    }

    #[test]
    fn get_key_cmd_resolves_single_byte_binding() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_keymap::selectkeymap("emacs", 1);
        ungetbytes(b"\x05"); // Ctrl-E — emacs default = end-of-line
        let t = get_key_cmd().expect("should resolve Ctrl-E");
        assert_eq!(t.nam, "end-of-line");
    }

    #[test]
    fn get_key_cmd_resolves_multi_byte_sequence() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_keymap::selectkeymap("emacs", 1);
        // ESC-d is bind to kill-word in zle_bindings.c emacs table.
        // Push the bytes and resolve — multi-byte traversal kicks in.
        ungetbytes(b"\x1bd");
        let t = get_key_cmd().expect("should resolve ESC-d");
        // Either kill-word or whatever the emacs default binds; assert
        // we got *some* widget (the trie walk worked beyond the single
        // byte) by checking the keybuf actually traversed past 1 byte.
        // Concretely: the widget shouldn't be a literal self-insert for
        // ESC, since that would mean trie walk failed.
        assert_ne!(t.nam, "self-insert");
    }

    #[test]
    fn get_key_cmd_returns_none_on_eof() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_keymap::selectkeymap("emacs", 1);
        // No bytes fed, no terminal attached — getbyte should return None.
        let result = get_key_cmd();
        // In test context with no real tty, getbyte may block; but our
        // unget buffer is empty AND raw_getbyte's poll path returns None
        // on no-input timeout. With a non-prefix initial byte not in the
        // unget buf, get_key_cmd's first getbyte returns None → we
        // return None. This is the path the test exercises.
        // (If the test runner's stdin is a real terminal, this will
        // block — fine in CI where stdin is a pipe.)
        let _ = result;
    }

    #[test]
    fn handle_undo_snapshots_line_for_subsequent_diff() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        *crate::ported::zle::zle_main::ZLELINE.lock().unwrap() = "abc".chars().collect();
        crate::ported::zle::zle_main::ZLELL.store(3, std::sync::atomic::Ordering::SeqCst);
        crate::ported::zle::zle_main::ZLECS.store(3, std::sync::atomic::Ordering::SeqCst);
        handleundo();
        assert_eq!(crate::ported::zle::zle_main::LASTLINE.lock().unwrap().iter().collect::<String>(), "abc");
        assert_eq!(crate::ported::zle::zle_main::LASTLL.load(std::sync::atomic::Ordering::SeqCst), 3);
        assert_eq!(crate::ported::zle::zle_main::LASTCS.load(std::sync::atomic::Ordering::SeqCst), 3);
    }

    #[test]
    fn in_vi_cmd_mode_reflects_active_keymap_name() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        *crate::ported::zle::zle_keymap::curkeymapname() = "emacs".to_string();
        assert!(!in_vi_cmd_mode());
        *crate::ported::zle::zle_keymap::curkeymapname() = "vicmd".to_string();
        assert!(in_vi_cmd_mode());
    }

    // ---------- ungetbytes_unmeta real-port tests ----------

    #[test]
    fn ungetbytes_unmeta_plain_bytes() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:375 — non-Meta bytes pushed back in reverse.
        crate::ported::zle::zle_main::zle_reset();
        // Pre-clear unget_buf in case { crate::ported::zle::zle_main::zle_reset() } leaves anything.
        crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().clear();
        ungetbytes_unmeta(b"abc");
        // After backward walk: ungetbyte('c'), then 'b', then 'a'
        // → unget_buf front = ['a', 'b', 'c'] in read order.
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'a'));
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'b'));
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'c'));
    }

    #[test]
    fn ungetbytes_unmeta_decodes_meta_pair() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:370-373 — `\x83 X` decodes to (X XOR 0x20). Meta = 0x83.
        // Encode 'a' meta-quoted: 0x83 followed by 'a' XOR 0x20 = 0x41.
        // So [0x83, 0x41] → emit 0x41 ^ 0x20 = 0x61 = 'a'.
        crate::ported::zle::zle_main::zle_reset();
        crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().clear();
        ungetbytes_unmeta(&[0x83, 0x41]);
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'a'));
        assert!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().is_empty());
    }

    #[test]
    fn ungetbytes_unmeta_mixed_meta_and_plain() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // 'X' + Meta + 'a'XOR0x20 + 'Z' → 3 chars: 'X', 'a', 'Z'.
        // Encoded: [0x58, 0x83, 0x41, 0x5a].
        crate::ported::zle::zle_main::zle_reset();
        crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().clear();
        ungetbytes_unmeta(&[0x58, 0x83, 0x41, 0x5a]);
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'X'));
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'a'));
        assert_eq!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().pop_front(), Some(b'Z'));
        assert!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().is_empty());
    }

    #[test]
    fn ungetbytes_unmeta_empty_input() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().clear();
        ungetbytes_unmeta(b"");
        assert!(crate::ported::zle::zle_main::KUNGETBUF.lock().unwrap().is_empty());
    }
}

// ===========================================================
// Methods moved verbatim from src/ported/exec.rs because their
// C counterpart's source file maps 1:1 to this Rust module.
// Rust permits multiple inherent impl blocks for the same
// type within a crate, so call sites in exec.rs are unchanged.
// ===========================================================

// BEGIN moved-from-exec-rs
// (impl ShellExecutor block moved to src/exec_shims.rs — see file marker)

// END moved-from-exec-rs

/// Direct port of `static int boot_(Module m)` from
/// `Src/Zle/zle_main.c:2301`.
/// ```c
/// addhookfunc("before_trap", zlebeforetrap);
/// addhookfunc("after_trap",  zleaftertrap);
/// addhookdefs(m, zlehooks, sizeof(zlehooks)/sizeof(*zlehooks));
/// return 0;
/// ```
pub fn boot_(_m: *const crate::ported::zsh_h::module) -> i32 {               // c:2301
    // c:2301-2304 — `addhookfunc("before_trap", zlebeforetrap);
    //                addhookfunc("after_trap",  zleaftertrap);`
    crate::ported::module::addhookfunc("before_trap", "zlebeforetrap");      // c:2303
    crate::ported::module::addhookfunc("after_trap",  "zleaftertrap");       // c:2304

    // Register comphooks defs. C zsh's complete-module setup_() does
    // `addhookdefs(m, comphooks, ...)` (complete.c:1766) with the
    // 5-entry comphooks[] table (complete.c:1702). The zle and
    // complete modules are statically linked together in zshrs so the
    // registration happens here; this seeds HOOKTAB with empty handler
    // lists for each name so `add-zsh-hook NAME fn` (and the canonical
    // after_complete runhookdef walk in compcore.rs) see valid hook
    // entries. Insertion is idempotent — re-running boot_() is a
    // no-op for already-present entries.
    if let Ok(mut t) = crate::ported::module::HOOKTAB.lock() {
        for name in ["insert_match",                                         // c:1703
                     "menu_start",                                           // c:1704
                     "compctl_make",                                         // c:1705
                     "compctl_cleanup",                                      // c:1706
                     "comp_list_matches"]                                    // c:1707
        {
            t.entry(name.to_string()).or_default();
        }
    }
    0                                                                        // c:2309
}

/// Port of `breakread(int fd, char *buf, int n)` from Src/Zle/zle_main.c:381.
pub fn breakread(fd: i32, buf: &mut [u8], n: usize) -> isize {               // c:381
    // C body (c:381-389): `#if defined(pyr) && defined(HAVE_SELECT)`
    // wrapper around select+read for the Pyramid (legacy) build.
    // zshrs targets only modern Unices where read(2) is restartable —
    // direct passthrough via libc::read (no File-from-fd ownership game).
    if n == 0 || buf.is_empty() {
        return 0;
    }
    let count = n.min(buf.len());
    let r = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, count) };
    r as isize
}

/// Direct port of `static int cleanup_(Module m)` from
/// `Src/Zle/zle_main.c:2312`.
/// ```c
/// if (zleactive) { zerrnam(...); return 1; }
/// deletehookfunc("before_trap", zlebeforetrap);
/// deletehookfunc("after_trap", zleaftertrap);
/// // delete keymaps + restore old entry points
/// return 0;
/// ```
pub fn cleanup_(_m: *const crate::ported::zsh_h::module) -> i32 {            // c:2312
    use std::sync::atomic::Ordering;
    // c:2314 — refuse to unload while ZLE is active.
    if crate::ported::builtins::sched::zleactive.load(Ordering::Relaxed) != 0 {
        return 1;
    }
    // c:2318-2319 — `deletehookfunc("before_trap", zlebeforetrap);
    //                deletehookfunc("after_trap",  zleaftertrap);`
    crate::ported::module::deletehookfunc("before_trap", "zlebeforetrap");   // c:2318
    crate::ported::module::deletehookfunc("after_trap",  "zleaftertrap");    // c:2319
    // c:2321-2324 — `deletekeymap(...)`. Drop is automatic on Arc<Keymap>;
    // explicit-name unlink from keymapnamtab so the next module load starts
    // fresh.
    if let Ok(mut tab) = crate::ported::zle::zle_keymap::keymapnamtab().lock() {
        tab.clear();
    }
    0                                                                        // c:2325
}

/// Direct port of `int describekeybriefly(char **args)` from
/// `Src/Zle/zle_main.c:1892`. Prompts for a key sequence,
/// resolves it through the current keymap, and prints the bound
/// widget name via `showmsg`.
///
/// **Substrate trade-off:** the interactive prompt path needs the
/// `getkeymapcmd` input driver (live key buffer + `statusline`
/// "Describe key briefly: _" prompt + `zrefresh` redraw + main-
/// keymap walk). Compcore-call-context fns don't have access to
/// the live key reader. Returns 1 to signal "no resolution"; the
/// live widget tick has its own copy of this fn that touches the
/// active state directly.
pub fn describekeybriefly() -> i32 {                                         // c:1892
    1                                                                        // c:1929 no-resolution path
}

/// Port of `enables_(UNUSED(Module m), UNUSED(int **enables))` from Src/Zle/zle_main.c:2294.
#[allow(unused_variables)]
pub fn enables_(m: *const crate::ported::zsh_h::module, enables: &mut Option<Vec<i32>>) -> i32 {
    // c:zle_main.c enables_ — `return handlefeatures(m, &module_features, enables)`.
    // Module-features substrate is shared across all module loaders;
    // returns the feature-mask handler.
    0
}

/// Port of `execimmortal(Thingy func, char **args)` from Src/Zle/zle_main.c:1404.
pub fn execimmortal(func: &str, args: &[String]) -> i32 {                    // c:1404
    // C body (c:1404-1410): `Thingy immortal = rthingy_nocreate(dyncat(".", func));
    //                       if (immortal) return execzlefunc(immortal, args, 0, 0);
    //                       return 1`.
    // Look up `.NAME` and dispatch to execzlefunc; the dot-prefixed
    // func guarantees we hit the immortal/canonical thingy.
    let dotted = format!(".{}", func);
    if crate::ported::zle::zle_thingy::rthingy_nocreate(&dotted) {           // c:1406
        // c:1407 — `return execzlefunc(immortal, args, 0, 0)`.
        return execzlefunc(&dotted, args);
    }
    1                                                                        // c:1409
}

/// Direct port of `int execzlefunc(Thingy func, char **args, int set_bindk,
///                                  int may_cd)` from `Src/Zle/zle_main.c:1420-1601`.
/// Widget invocation pipeline. C body walks the `Widget` union and
/// dispatches to either an internal widget fn (`WIDGET_INT`) or a
/// shell function (`WIDGET_FUNCTION`), wrapping the call in
/// metafy/unmetafy of `zlemetaline` and tracking `bindk`/`lastcmd`.
///
/// Rust port covers:
///   - Internal widget lookup via thingytab (read-side already
///     ported).
///   - Shell-function dispatch via the canonical getshfunc + LASTVAL
///     read path (mirrors C's `doshfunc` invocation).
///   - lastcmd update from the widget's flag mask.
/// Bindk/metafy boundary management lives on the per-thread Zle
/// struct already.
/// Port of `execzlefunc(Thingy func, char **args, int set_bindk, int set_lbindk)` from `Src/Zle/zle_main.c:1420`.
/// WARNING: param names don't match C — Rust=(name, args) vs C=(func, args, set_bindk, set_lbindk)
pub fn execzlefunc(name: &str, args: &[String]) -> i32 {                     // c:1420
    // c:1420 — `if (!func) return 1`.
    if !crate::ported::zle::zle_thingy::rthingy_nocreate(name) {             // c:1422
        return 1;
    }

    // c:1437 — `if (func->widget->flags & WIDGET_INT)` — internal
    // widget dispatch. Without the Widget union in scope we route
    // through the shell-function path which exercises the user-
    // visible side effects.

    // c:1490 — `doshfunc(shf, args, …)` — invoke the user's shfunc.
    // Real dispatch via the canonical fusevm_bridge::with_executor +
    // dispatch_function_call path used by signal-trap shfunc dispatch
    // (signals.rs:1087). Routes through the wordcode VM directly so
    // the widget body's side-effects (BUFFER/CURSOR/KEYMAP writes,
    // zle -K, etc.) land on the live ZLE state in this thread.
    if crate::ported::utils::getshfunc(name).is_some() {                     // c:1490
        let rc = crate::fusevm_bridge::with_executor(|exec| {
            exec.dispatch_function_call(name, args).unwrap_or(0)
        });
        // c:1530 — capture LASTVAL after the call. dispatch_function_call
        // sets LASTVAL itself; mirror the return through.
        crate::ported::builtin::LASTVAL.store(rc, std::sync::atomic::Ordering::Relaxed);
        return rc;
    }

    // c:1597 — fall through: widget exists in thingytab but has no
    // shfunc binding. Return success with no side effect.
    0
}

/// Direct port of `static int features_(Module m, char ***features)`
/// from `Src/Zle/zle_main.c:2286`. Returns the module's
/// feature-name array via `featuresarray(m, &module_features)`,
/// matching the C body line-for-line.
pub fn features_(_m: *const crate::ported::zsh_h::module,
                 features: &mut Vec<String>) -> i32 {                        // c:2286
    // c:2287-2288 — `*features = featuresarray(m, &module_features); return 0`.
    // zle_main.c registers builtins ("zle", "bindkey", "vared"), conddefs
    // (when binding-keymap conditions are loaded), and param defs. Each
    // contributes "b:<name>" / "c:<name>" / "p:<name>" entries — matching
    // the format C's featuresarray() emits.
    features.clear();
    features.extend([
        "b:bindkey".to_string(),
        "b:vared".to_string(),
        "b:zle".to_string(),
        "p:KEYMAP".to_string(),
        "p:CONTEXT".to_string(),
        "p:KEYS".to_string(),
        "p:NUMERIC".to_string(),
        "p:PREDISPLAY".to_string(),
        "p:POSTDISPLAY".to_string(),
        "p:BUFFER".to_string(),
        "p:CURSOR".to_string(),
        "p:CUTBUFFER".to_string(),
        "p:HISTNO".to_string(),
        "p:KILLRING".to_string(),
        "p:LASTSEARCH".to_string(),
        "p:LASTWIDGET".to_string(),
        "p:MARK".to_string(),
        "p:PREBUFFER".to_string(),
        "p:RBUFFER".to_string(),
        "p:LBUFFER".to_string(),
        "p:REGION_ACTIVE".to_string(),
        "p:UNDO_CHANGE_NO".to_string(),
        "p:UNDO_LIMIT_NO".to_string(),
        "p:WIDGET".to_string(),
        "p:WIDGETSTYLE".to_string(),
        "p:WIDGETFUNC".to_string(),
        "p:registers".to_string(),
        "p:ZLE_LINE_ABORTED".to_string(),
    ]);
    0                                                                        // c:2288
}

/// Port of `finish_(UNUSED(Module m))` from Src/Zle/zle_main.c:2327.
#[allow(unused_variables)]
pub fn finish_(m: *const crate::ported::zsh_h::module) -> i32 {             // c:zle_main.c finish_
    // C body: per-module dispose hook, runs after cleanup_; releases
    // per-module-instance state. zshrs has no per-module state; no-op.
    0
}

/// Port of `getrestchar(int inchar, char *outstr, int *outcount)` from Src/Zle/zle_main.c:990.
/// WARNING: param names don't match C — Rust=(zle, inchar) vs C=(inchar, outstr, outcount)
pub fn getrestchar(inchar: i32) -> i32 {                      // c:990
    // c:990 — `lastchar_wide_valid = 1`. Mark wide cache as valid.
    crate::ported::zle::zle_main::LASTCHAR_WIDE_VALID.store(1, std::sync::atomic::Ordering::SeqCst);
    // c:1006-1009 — `if (inchar == EOF) return WEOF (cached)`.
    if inchar < 0 {
        crate::ported::zle::zle_main::LASTCHAR_WIDE.store((-1) as i32, std::sync::atomic::Ordering::SeqCst);
        return -1;                                                           // c:1009 ZLEEOF
    }
    // c:1016+ — multibyte byte-stream → wide-char accumulator.
    // zshrs is UTF-8 native; for an ASCII char inchar fits in
    // lastchar_wide directly (mb_metacharlenconv state machine
    // collapses to identity for the BMP single-byte path).
    crate::ported::zle::zle_main::LASTCHAR_WIDE.store((inchar) as i32, std::sync::atomic::Ordering::SeqCst);
    inchar
}

/// Port of `recursiveedit(UNUSED(char **args))` from Src/Zle/zle_main.c:1974.
pub fn recursiveedit() -> i32 {                                 // c:1974
    // C body (c:1976-1995): `++zle_recursive; redrawhook(); zrefresh();
    //                       zlecore(); --zle_recursive;
    //                       locerror = errflag ? 1 : 0;
    //                       errflag = done = eofsent = 0; return locerror`.
    // zlecore needs the editor mainloop substrate; we faithfully
    // bump/decrement zle_recursive and reset errflag/done.
    use std::sync::atomic::Ordering;
    crate::ported::zle::zle_main::ZLE_RECURSIVE.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
    // c:1984-1986 — `redrawhook(); zrefresh(); zlecore()`. Deferred.
    crate::ported::zle::zle_main::ZLE_RECURSIVE.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
    let cur_errflag = crate::ported::utils::errflag.load(Ordering::Relaxed);
    let locerror = if cur_errflag != 0 { 1 } else { 0 };
    crate::ported::utils::errflag.store(0, Ordering::Relaxed);
    crate::ported::zle::zle_misc::DONE.store(0, Ordering::SeqCst);           // c:1993
    locerror                                                                 // c:1995
}

/// Port of `restorekeymap(char *cmdname, char *oldname, char *newname, Keymap savemap)` from Src/Zle/zle_main.c:1656.
/// WARNING: param names don't match C — Rust=(oldname, savemap) vs C=(cmdname, oldname, newname, savemap)
pub fn restorekeymap(oldname: &str, savemap: Option<std::sync::Arc<crate::ported::zle::zle_keymap::Keymap>>) {  // c:1656
    // C body (c:1657-1666): `if (savemap) { linkkeymap(savemap,
    //                       oldname, 0); unrefkeymap(savemap); }
    //                       else if (newname) zwarnnam(...)`.
    if let Some(km) = savemap {
        crate::ported::zle::zle_keymap::linkkeymap(km, oldname, 0);
    }
}

/// Port of `savekeymap(char *cmdname, char *oldname, char *newname, Keymap *savemapptr)` from Src/Zle/zle_main.c:1632.
/// WARNING: param names don't match C — Rust=(oldname, newname) vs C=(cmdname, oldname, newname, savemapptr)
pub fn savekeymap(oldname: &str, newname: &str) -> Option<std::sync::Arc<crate::ported::zle::zle_keymap::Keymap>> {  // c:1632
    // C body (c:1634-1651): `km = openkeymap(newname); if (km) {
    //                       *savemap = openkeymap(oldname);
    //                       if (*savemap != km) { refkeymap(*savemap);
    //                           linkkeymap(km, oldname, 0); } return 0; }
    //                       else return 1`.
    let km = crate::ported::zle::zle_keymap::openkeymap(newname)?;
    let saved = crate::ported::zle::zle_keymap::openkeymap(oldname);
    let same = saved.as_ref().map(|s| std::sync::Arc::ptr_eq(s, &km)).unwrap_or(false);
    if !same {
        crate::ported::zle::zle_keymap::linkkeymap(km, oldname, 0);
    }
    if same { None } else { saved }
}

/// Direct port of `static void scanfindfunc(char *seq, Thingy func,
///                                          char *str, void *magic)`
/// from `Src/Zle/zle_main.c:1935`. Per-keymap scan callback for
/// `describe-key-briefly`: when `func` matches the target in `ff`,
/// appends `" <seq>"` to `ff.msg`, capped at MAXFOUND hits.
pub fn scanfindfunc(seq: &str, func: &str, ff: &mut findfunc) {              // c:1935
    const MAXFOUND: usize = 3;                                               // c:1957
    // c:1939 — `if (func != ff->func) return`. Compare by widget name.
    let want = ff.func.map(|i| i.to_string()).unwrap_or_default();
    if !want.is_empty() && func != want { return; }
    // c:1942 — `if (!ff->found++) ff->msg = appstr(...," is on")`.
    if ff.found == 0 { ff.msg.push_str(" is on"); }
    ff.found += 1;
    if ff.found <= MAXFOUND {                                                // c:1944
        ff.msg.push(' ');                                                    // c:1946
        ff.msg.push_str(seq);                                                // c:1947 bindztrdup
    }
}

/// Port of `setup_(UNUSED(Module m))` from Src/Zle/zle_main.c:2243.
#[allow(unused_variables)]
pub fn setup_(m: *const crate::ported::zsh_h::module) -> i32 {              // c:zle_main.c setup_
    // C body: `bpaste = ... bracketed-paste arrays; set up editor
    //          entry points`. Module-init substrate; returns 0.
    0
}

/// Port of `ungetbytes_unmeta(char *s, int len)` from `Src/Zle/zle_main.c:365`.
/// ```c
/// void
/// ungetbytes_unmeta(char *s, int len)
/// {
///     s += len;
///     while (len--) {
///         if (len && s[-2] == Meta) {
///             ungetbyte(*--s ^ 32);
///             len--;
///             s--;
///         } else
///             ungetbyte(*--s);
///     }
/// }
/// ```
/// Push back a byte slice that may contain `Meta`-quoted (0x83 ch
/// XOR 0x20) sequences, decoding them as we go. C walks backward
/// through `s` because `ungetbyte` is a stack push — to surface
/// `s[0]` first on subsequent read, the last byte goes on first.
/// WARNING: param names don't match C — Rust=(zle, s) vs C=(s, len)
pub fn ungetbytes_unmeta(s: &[u8]) {                          // c:366
    let mut i = s.len();                                                     // c:366 s += len
    while i > 0 {                                                            // c:369 while (len--)
        // c:370 — `if (len && s[-2] == Meta)`. We check the byte
        // BEFORE the current s-1 position. After `*--s`, the index
        // becomes i-1. So `s[-2]` is `s[i-2]`.
        if i >= 2 && s[i - 2] == 0x83 {                                      // c:370 Meta = 0x83
            // c:371-373 — decode Meta-escape: emit (s[i-1] XOR 32),
            // skip the Meta byte.
            ungetbyte(s[i - 1] ^ 32);
            i -= 2;
        } else {
            // c:375 — emit raw byte.
            ungetbyte(s[i - 1]);
            i -= 1;
        }
    }
}

/// Direct port of `void zle_resetprompt(void)` from
/// `Src/Zle/zle_main.c:2058`.
/// ```c
/// reexpandprompt();
/// if (zleactive)
///     redisplay(NULL);
/// ```
/// Triggers a prompt re-expansion + redraw. C's globals are
/// `lpromptbuf`/`rpromptbuf` + the live terminal. Rust port sets
/// the `ZLE_RESET_NEEDED` flag so the next `zlecore` tick
/// reads it and triggers `reexpandprompt + redisplay`.
pub fn zle_resetprompt() {                                                   // c:2058
    use std::sync::atomic::Ordering;
    // c:2060 — `reexpandprompt()`. Flag drives the deferred re-expand
    // in zlecore (reads ZLE_RESET_NEEDED + clears it).
    ZLE_RESET_NEEDED.store(1, Ordering::SeqCst);
    if crate::ported::builtins::sched::zleactive.load(Ordering::Relaxed) != 0 {
        // c:2062 — `redisplay(NULL)`. The next zlecore tick observes
        // ZLE_RESET_NEEDED and invokes redisplay against the
        // file-scope ZLE statics.
        crate::ported::zle::zle_refresh::SHOWINGLIST.store(-2, Ordering::SeqCst);
    }
}

/// Cross-call flag for `zle_resetprompt`. Read + cleared by the
/// next `zlecore` iteration to drive a real prompt re-expand and
/// redraw. C uses an implicit `redisplay(NULL)` direct call; the
/// Rust port routes through this flag.
pub static ZLE_RESET_NEEDED: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `int insmode` from `Src/Zle/zle_main.c:124`. Non-zero
/// when ZLE is in insert mode (vs overwrite). Toggled by
/// `overwrite-mode` widget; consulted by `self-insert` /
/// `selfinsert()` to choose insert vs replace semantics.
pub static INSMODE: std::sync::atomic::AtomicI32 =                           // c:124
    std::sync::atomic::AtomicI32::new(1);

/// Port of `int lastchar_wide` from `Src/Zle/zle_main.c`. Wide
/// (multi-byte) version of the last input character — populated by
/// `getfullchar()` after assembling a multi-byte sequence, then
/// consumed by `selfinsert()`-class widgets in preference to the
/// byte-level `LASTCHAR` when the input was non-ASCII.
pub static LASTCHAR_WIDE: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `int lastchar_wide_valid` from `Src/Zle/zle_main.c`.
/// Set when `LASTCHAR_WIDE` holds a freshly-assembled wide-char
/// from `getfullchar()`; cleared by callers that consumed it.
pub static LASTCHAR_WIDE_VALID: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `int eofchar` from `Src/Zle/zle_main.c`. The termios
/// VEOF byte (typically Ctrl-D); compared against `LASTCHAR` to
/// detect end-of-file on an empty line.
pub static EOFCHAR: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(4);

/// Port of `int eofsent` from `Src/Zle/zle_main.c`. Set when the
/// user sent EOF on an empty line — drives the outer `zleread()`
/// loop to break and return an EOF sentinel.
pub static EOFSENT: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `int prefixflag` from `Src/Zle/zle_main.c`. Sticky flag
/// set by a prefix command (universal-argument, digit-argument,
/// vi-set-buffer, etc.) so the next widget consumes the argument
/// rather than starting a fresh count.
pub static PREFIXFLAG: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `int zlereadflags` from `Src/Zle/zle_main.c`. ZLRF_*
/// flags passed to `zleread()` controlling history/setty behaviour.
/// Default value matches input.c:418 (`ZLRF_HISTORY | ZLRF_NOSETTY`).
pub static ZLEREADFLAGS: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(
        crate::ported::zsh_h::ZLRF_HISTORY | crate::ported::zsh_h::ZLRF_NOSETTY,
    );

/// Port of `int zlecontext` from `Src/Zle/zle_main.c`. ZLCON_*
/// context tag passed to `zleread()` — distinguishes line-start
/// vs continuation-line vs vared etc.
pub static ZLECONTEXT: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(crate::ported::zsh_h::ZLCON_LINE_START);

/// Port of `int zle_recursive` from `Src/Zle/zle_main.c`. Depth of
/// nested `recursive-edit` invocations; non-zero inhibits outer
/// loop exit.
pub static ZLE_RECURSIVE: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// Port of `time_t keytimeout` from `Src/Zle/zle_main.c`. Multi-byte
/// key-sequence timeout in 100ths of a second. 0 = no timeout. The
/// default 40 (0.4s) matches zsh's `$KEYTIMEOUT` startup default.
pub static KEYTIMEOUT: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(40);

/// Port of `int lastcmd` from `Src/Zle/zle_main.c:145`. Flags of
/// the most-recently-executed widget — drives `yank`/`yank-pop`
/// chaining, `ZLE_YANK`/`YANKAFTER` membership tests, etc.
/// Stored as the raw bits of `WidgetFlags` (u32) in an atomic.
pub static LASTCMD: std::sync::atomic::AtomicU32 =                           // c:145
    std::sync::atomic::AtomicU32::new(0);

/// Port of `Watch_fd watch_fds;` from `Src/Zle/zle_main.c:204`.
/// Global linked list (here: `Vec<watch_fd>`) of fd watchers
/// registered via `zle -F fd handler` for select/poll dispatch
/// inside `getkey()`. `bin_zle_fd` mutates this; the poll loop
/// reads it to know which fds to watch.
pub static WATCH_FDS: std::sync::Mutex<Vec<super::zle_h::watch_fd>> =        // c:204
    std::sync::Mutex::new(Vec::new());

// =====================================================================
// Former `Zle` struct fields, migrated to file-scope statics matching
// the C source's file-`static`s. Bucket 1 (per-evaluator) per
// docs/PORT_PLAN.md.
// =====================================================================

/// Port of `ZLE_STRING_T zleline` from `Src/Zle/zle_main.c:40`.
pub static ZLELINE: std::sync::Mutex<Vec<ZleChar>> = std::sync::Mutex::new(Vec::new());
/// Port of `int zlecs` from `Src/Zle/zle_main.c:45`.
pub static ZLECS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int zlell` from `Src/Zle/zle_main.c:45`.
pub static ZLELL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int mark` from `Src/Zle/zle_main.c:81`.
pub static MARK: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `Thingy lbindk` from `Src/Zle/zle_main.c`.
pub static LBINDK: std::sync::Mutex<Option<Thingy>> = std::sync::Mutex::new(None);
/// Port of `Thingy bindk` from `Src/Zle/zle_main.c`.
pub static BINDK: std::sync::Mutex<Option<Thingy>> = std::sync::Mutex::new(None);
/// Port of `struct modifier zmod` from `Src/Zle/zle_main.c:169`.
pub static ZMOD: std::sync::Mutex<modifier> = std::sync::Mutex::new(modifier { flags: 0, mult: 1, tmult: 1, vibuf: 0, base: 10 });
/// Port of `char *statusline` from `Src/Zle/zle_main.c`.
pub static STATUSLINE: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
/// Port of `zlong stackhist` from `Src/Zle/zle_hist.c`.
pub static STACKHIST: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
/// Port of `int stackcs` from `Src/Zle/zle_hist.c`.
pub static STACKCS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `zlong vistartchange` from `Src/Zle/zle_vi.c`.
pub static VISTARTCHANGE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Port of `struct change *changes` from `Src/Zle/zle_utils.c`.
pub static UNDO_STACK: std::sync::Mutex<Vec<change>> = std::sync::Mutex::new(Vec::new());
/// Port of `zlong changeno` from `Src/Zle/zle_utils.c`.
pub static CHANGENO: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Port of `char *kungetbuf` from `Src/Zle/zle_main.c:185`.
pub static KUNGETBUF: std::sync::Mutex<VecDeque<u8>> = std::sync::Mutex::new(VecDeque::new());
/// Port of `int baud` from `Src/Zle/zle_main.c`.
pub static BAUD: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(38400);
/// Port of `Widget compwidget` from `Src/Zle/zle_tricky.c`.
pub static COMPWIDGET: std::sync::Mutex<Option<Widget>> = std::sync::Mutex::new(None);
/// Port of `int hascompmod` from `Src/Zle/zle_tricky.c`.
pub static HASCOMPMOD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Port of `int SHTTY` from `Src/Zle/zle_main.c`.
pub static TTYFD: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
/// Port of `char *lprompt` (expanded) from `Src/Zle/zle_main.c`.
pub static LPROMPT: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
/// Port of `char *rprompt` (expanded) from `Src/Zle/zle_main.c`.
pub static RPROMPT: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
/// Port of `int pre_zle_status` from `Src/Zle/zle_main.c`.
pub static PRE_ZLE_STATUS: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
/// Port of `ZLE_STRING_T vibuf[36]` from `Src/Zle/zle_misc.c`.
pub static VIBUF: std::sync::OnceLock<std::sync::Mutex<[Vec<ZleChar>; 36]>> = std::sync::OnceLock::new();
pub fn vibuf() -> &'static std::sync::Mutex<[Vec<ZleChar>; 36]> {
    VIBUF.get_or_init(|| std::sync::Mutex::new(std::array::from_fn(|_| Vec::new())))
}
/// Port of `LinkList kring` from `Src/Zle/zle_misc.c`.
pub static KILLRING: std::sync::Mutex<VecDeque<Vec<ZleChar>>> = std::sync::Mutex::new(VecDeque::new());
/// Port of `int kringsize` from `Src/Zle/zle_misc.c`.
pub static KILLRINGMAX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(8);
/// Port of `int yanklast` from `Src/Zle/zle_misc.c`.
pub static YANKLAST: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Port of `int neg_arg` from `Src/Zle/zle_main.c`.
pub static NEG_ARG: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Port of `int mult` from `Src/Zle/zle_main.c`.
pub static MULT: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(1);
/// Port of `Histent histlist` ZLE-session view from `Src/Zle/zle_hist.c`.
pub static HISTORY: std::sync::OnceLock<std::sync::Mutex<super::zle_hist::History>> = std::sync::OnceLock::new();
pub fn history() -> &'static std::sync::Mutex<super::zle_hist::History> {
    HISTORY.get_or_init(|| std::sync::Mutex::new(super::zle_hist::History::new(2000)))
}
/// Port of `int lastcol` from `Src/Zle/zle_hist.c`.
pub static LASTCOL: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);
/// Port of `LinkList bufstack` from `Src/Zle/zle_hist.c`.
pub static BUFSTACK: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
/// Port of `char *vichgbuf` from `Src/Zle/zle_vi.c`.
pub static VICHGBUF: std::sync::Mutex<Vec<u8>> = std::sync::Mutex::new(Vec::new());
/// Port of `char *srch_str` from `Src/Zle/zle_hist.c`.
pub static SRCH_STR: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
/// Port of `ZLE_STRING_T lastline` from `Src/Zle/zle_utils.c`.
pub static LASTLINE: std::sync::Mutex<Vec<ZleChar>> = std::sync::Mutex::new(Vec::new());
/// Port of `int lastll` from `Src/Zle/zle_utils.c`.
pub static LASTLL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int lastcs` from `Src/Zle/zle_utils.c`.
pub static LASTCS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `struct change *curchange` from `Src/Zle/zle_utils.c` (as index).
pub static CURCHANGE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `zlong undo_changeno` from `Src/Zle/zle_utils.c`.
pub static UNDO_CHANGENO: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Port of `zlong undo_limitno` from `Src/Zle/zle_utils.c`.
pub static UNDO_LIMITNO: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Port of `int viinsbegin` from `Src/Zle/zle_vi.c:78`.
pub static VIINSBEGIN: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int yankb` from `Src/Zle/zle_misc.c`.
pub static YANKB: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int yanke` from `Src/Zle/zle_misc.c`.
pub static YANKE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int yankcs` from `Src/Zle/zle_misc.c`.
pub static YANKCS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// Port of `int kct` from `Src/Zle/zle_misc.c`.
pub static KCT: std::sync::Mutex<Option<usize>> = std::sync::Mutex::new(None);
/// Port of `int vimarkcs[27]` + `zlong vimarkline[27]` from `Src/Zle/zle_move.c`.
pub static VIMARKS: std::sync::OnceLock<std::sync::Mutex<[Option<(usize, i32)>; 27]>> = std::sync::OnceLock::new();
pub fn vimarks() -> &'static std::sync::Mutex<[Option<(usize, i32)>; 27]> {
    VIMARKS.get_or_init(|| std::sync::Mutex::new([None; 27]))
}
/// Port of `int region_active` from `Src/Zle/zle_main.c`.
pub static REGION_ACTIVE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
/// Rust-only queue for hooks. C dispatches inline via `zle_call_hook` (Src/Zle/zle_utils.c:1755).
pub static PENDING_HOOKS: std::sync::Mutex<Vec<(String, Option<String>)>> = std::sync::Mutex::new(Vec::new());
/// Port of `char *raw_lp` from `Src/Zle/zle_main.c`.
pub static RAW_LP: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
/// Port of `char *raw_rp` from `Src/Zle/zle_main.c`.
pub static RAW_RP: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
/// Port of `Region_highlight region_highlights` from `Src/Zle/zle_refresh.c`.
pub static HIGHLIGHT: std::sync::OnceLock<std::sync::Mutex<super::zle_refresh::HighlightManager>> = std::sync::OnceLock::new();
pub fn highlight() -> &'static std::sync::Mutex<super::zle_refresh::HighlightManager> {
    HIGHLIGHT.get_or_init(|| std::sync::Mutex::new(super::zle_refresh::HighlightManager::new()))
}

/// Port of `zleaftertrap(UNUSED(Hookdef dummy), UNUSED(void *dat))` from `Src/Zle/zle_main.c:2114`.
/// ```c
/// static int
/// zleaftertrap(UNUSED(Hookdef dummy), UNUSED(void *dat))
/// {
///     if (zleactive)
///         endparamscope();
///     return 0;
/// }
/// ```
/// Hook callback fired AFTER a trap handler runs — pops the
/// param scope that `zlebeforetrap` pushed (if zle is active).
/// WARNING: param names don't match C — Rust=() vs C=(dummy, dat)
pub fn zleaftertrap() -> i32 {                                               // c:2114
    use std::sync::atomic::Ordering;
    if crate::ported::builtins::sched::zleactive.load(Ordering::Relaxed) != 0 {  // c:2116
        crate::ported::params::endparamscope();                              // c:2117
    }
    0                                                                        // c:2119 return 0
}

/// Port of `zlebeforetrap(UNUSED(Hookdef dummy), UNUSED(void *dat))` from `Src/Zle/zle_main.c:2103`.
/// ```c
/// static int
/// zlebeforetrap(UNUSED(Hookdef dummy), UNUSED(void *dat))
/// {
///     if (zleactive) {
///         startparamscope();
///         makezleparams(1);
///     }
///     return 0;
/// }
/// ```
/// Hook callback fired BEFORE a trap handler runs — pushes a
/// param scope and exposes ZLE state to the trap function (when
/// zle is active).
/// WARNING: param names don't match C — Rust=() vs C=(dummy, dat)
pub fn zlebeforetrap() -> i32 {                                              // c:2104
    use std::sync::atomic::Ordering;
    if crate::ported::builtins::sched::zleactive.load(Ordering::Relaxed) != 0 {  // c:2106
        // c:2107 — `startparamscope()`. Push a param scope so trap
        // function locals don't leak into the outer shell state.
        // C uses the global `params` HashTable directly; the Rust
        // paramtab API takes a HashTable arg so we pass a fresh
        // empty one matching the C scope-push semantics.
        let mut local_scope: crate::ported::zsh_h::HashTable = Box::new(
            crate::ported::zsh_h::hashtable {
                hsize: 0, ct: 0, nodes: Vec::new(), tmpdata: 0,
                hash: None, emptytable: None, filltable: None,
                cmpnodes: None, addnode: None, getnode: None,
                getnode2: None, removenode: None, disablenode: None,
                enablenode: None, freenode: None, printnode: None,
                scantab: None,
            },
        );
        crate::ported::params::startparamscope(&mut local_scope);
        // c:2108 — `makezleparams(1)`. Snapshot the ZLE state ($BUFFER
        // etc.) into the paramtab as readonly so trap fns observe
        // the live editor state.
        crate::ported::zle::zle_params::makezleparams(1);
    }
    0                                                                        // c:2110 return 0
}