zshrs 0.11.18

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
//! `zle_word` — port of `Src/Zle/zle_word.c`.
//!
//! Word-related editor widgets: forward/backward word motion in the
//! emacs and vi flavors (with both "word" and "blank-word" variants),
//! word-region kill/delete, case-conversion (upcase/downcase/capitalize),
//! and `transpose-words`.
//!
//! C source: 23 ported total — `forwardword`, `wordclass`, `viforwardword`,
//! `viforwardblankword`, `emacsforwardword`, `viforwardblankwordend`,
//! `viforwardwordend`, `backwardword`, `vibackwardword`,
//! `vibackwardblankword`, `vibackwardwordend`, `vibackwardblankwordend`,
//! `emacsbackwardword`, `backwarddeleteword`, `vibackwardkillword`,
//! `backwardkillword`, `upcaseword`, `downcaseword`, `capitalizeword`,
//! `deleteword`, `killword`, `transposewords`. Zero structs/enums in
//! zle_word.c (only the function definitions).
//!
//! Order in this file mirrors C source order verbatim.

use super::zle_h::MOD_MULT;

// ---------------------------------------------------------------------------
// Helpers shared by every widget below — character classification + cursor
// movement. All inlined where used; no Rust-only helper ported.
//
// `INCCS()` / `DECCS()` (zle.h) — increment/decrement `zlecs`. C
// versions handle multibyte glyph-cluster boundaries; zshrs treats
// the buffer as `Vec<char>` (already glyph-cluster-aligned), so each
// step is a plain `+= 1` / `-= 1`.
//
// `INCPOS(p)` / `DECPOS(p)` (zle.h) — same as INCCS/DECCS but for
// any local `int pos` variable.
//
// `ZC_iword(c)` — c is a "word" character: alphanumeric or `_`
// (matches the C `iword` definition at zsh.h).
// `ZC_ialnum`, `ZC_ialpha` — alphanumeric / alphabetic.
// `ZC_iblank`, `ZC_inblank` — blank (space/tab) / non-newline-blank.
// `ZC_ipunct` — punctuation.
// `ZC_toupper`, `ZC_tolower` — case conversion.
//
// All inlined per-call; not extracted to helpers since C uses macros
// (which the build script's drift gate forbids reproducing as ported).
// ---------------------------------------------------------------------------

// `zmult` (zsh.h global, set by digit-argument widgets) and
// `wordflag`/`virangeflag` (Src/Zle/zle_vi.c:36-41 file-statics) are
// inlined at every call site rather than wrapped as helper ported —
// the build script's drift gate rejects Rust-only helpers, even when
// they only collapse repeated reads. Pattern:
//
//   zmult    → `if ZMOD.lock().unwrap().flags & MOD_MULT != 0
//                 { ZMOD.lock().unwrap().mult } else { 1 }`
//   set zmult → `ZMOD.lock().unwrap().mult = v;
//                ZMOD.lock().unwrap().flags |= MOD_MULT;`
//   wordflag/virangeflag → `crate::ported::zle::zle_vi::WORDFLAG /
//   VIRANGEFLAG.load(Ordering::Relaxed)` — the vi-mode atomics live
//   in zle_vi.rs and are set/cleared by getvirange + startvichange.
//
// See textobjects.rs:32 for the same `zmod.mult` pattern.

// ZC_ char-class predicates — C macros expanded inline.

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

// Local aliases routing to the canonical `ZC_*` predicates in
// `zle_h.rs:246-271` (port of `Src/Zle/zle.h:60-73`). Re-defining
// these inline here is a divergence trap — the previous versions
// had narrow `space || tab` iblank and `space || tab || \n` inblank
// that did NOT match the C `wcsiblank`/`iswspace` semantics. Always
// route through the canonical port so a regression has one place
// to fix, not two.
#[inline]
fn zc_iword(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_iword(c)
}
#[inline]
fn zc_ialnum(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_ialnum(c)
}
#[inline]
fn zc_ialpha(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_ialpha(c)
}
#[inline]
fn zc_iblank(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_iblank(c)
}
#[inline]
fn zc_inblank(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_inblank(c)
}
#[inline]
fn zc_ipunct(c: char) -> bool {
    crate::ported::zle::zle_h::ZC_ipunct(c)
}

/// Port of `forwardword(char **args)` from `Src/Zle/zle_word.c:45`.
///
/// C signature: `int forwardword(char **args)`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn forwardword(args: &[String]) -> i32 {
    // c:45
    let n = if ZMOD.lock().unwrap().flags & MOD_MULT != 0 {
        ZMOD.lock().unwrap().mult
    } else {
        1
    }; // c:45
    if n < 0 {
        // c:49
        let saved = n;
        ZMOD.lock().unwrap().mult = -n;
        ZMOD.lock().unwrap().flags |= MOD_MULT; // c:51
        let ret = backwardword(args); // c:52
        ZMOD.lock().unwrap().mult = saved;
        ZMOD.lock().unwrap().flags |= MOD_MULT; // c:53
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:56
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:57
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:58 INCCS
        }
        // c:59 — `if (wordflag && !n) return 0;`. wordflag is the
        // vi-range/word-motion kludge flag set by getvirange at
        // zle_vi.c:186; ported as atomic WORDFLAG. Was hardcoded to
        // `false`, dropping the vi-word-motion early-exit.
        if WORDFLAG.load(std::sync::atomic::Ordering::Relaxed) != 0
            && n == 0
        {
            return 0; // c:60
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:74
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:74 INCCS
        }
    }
    0 // c:74
}

/// Port of `wordclass(ZLE_CHAR_T x)` from `Src/Zle/zle_word.c:74`. Returns the
/// vi-mode word class for a character: 0=blank, 1=alnum or `_`,
/// 2=punctuation, 3=other.
///
/// C signature: `int wordclass(ZLE_CHAR_T x)`.
pub fn wordclass(x: char) -> i32 {
    // c:74
    // c:82 — `(ZC_iblank(x) ? 0 : ((ZC_ialnum(x) || ZWC('_') == x) ? 1 :
    //          ZC_ipunct(x) ? 2 : 3))`
    if zc_iblank(x) {
        0
    } else if zc_ialnum(x) || x == '_' {
        1
    } else if zc_ipunct(x) {
        2
    } else {
        3
    }
}

/// Port of `viforwardword(char **args)` from `Src/Zle/zle_word.c:82`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn viforwardword(args: &[String]) -> i32 {
    // c:82
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:86
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardword(args); // c:89
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:93
        n -= 1;
        let cc =
            wordclass(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)]); // c:95
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && wordclass(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
                == cc
        {
            // c:96
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:97 INCCS
        }
        // c:99 — `if (wordflag && !n) return 0;` (see forwardword note).
        if WORDFLAG.load(std::sync::atomic::Ordering::Relaxed) != 0
            && n == 0
        {
            return 0;
        } // c:99
        let mut nl = if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n'
        {
            1
        } else {
            0
        }; // c:101
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && nl < 2
            && zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        // c:112
        {
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:112 INCCS
            if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
                < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                && ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n'
            {
                nl += 1;
            } // c:112
        }
    }
    0 // c:112
}

/// Port of `viforwardblankword(char **args)` from `Src/Zle/zle_word.c:112`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn viforwardblankword(args: &[String]) -> i32 {
    // c:112
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardblankword(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:125
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        // c:127 — `if (wordflag && !n) return 0;` (see forwardword note).
        if WORDFLAG.load(std::sync::atomic::Ordering::Relaxed) != 0
            && n == 0
        {
            return 0;
        } // c:127
        let mut nl = if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n'
        {
            1
        } else {
            0
        };
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && nl < 2
            && zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
                < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                && ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n'
            {
                nl += 1;
            }
        }
    }
    0
}

/// Port of `emacsforwardword(char **args)` from `Src/Zle/zle_word.c:140`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn emacsforwardword(args: &[String]) -> i32 {
    // c:140
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:144
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = emacsbackwardword(args); // c:147
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:151
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:152
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        // c:164 — `if (wordflag && !n) return 0;` (see forwardword note).
        if WORDFLAG.load(std::sync::atomic::Ordering::Relaxed) != 0
            && n == 0
        {
            return 0;
        } // c:164
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:164
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
    }
    0
}

/// Port of `viforwardblankwordend(char **args)` from `Src/Zle/zle_word.c:164`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn viforwardblankwordend(args: &[String]) -> i32 {
    // c:164
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardblankwordend(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        // c:176-182 — skip inblank chars; advance pos one ahead via INCPOS
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        {
            // c:176
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1; // c:178 INCPOS
            if pos > ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                || !zc_inblank(
                    ZLELINE.lock().unwrap()[pos.min(
                        ZLELL
                            .load(std::sync::atomic::Ordering::SeqCst)
                            .saturating_sub(1),
                    )],
                )
            {
                break;
            }
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:181
        }
        // c:183-189 — advance over non-inblank chars.
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        {
            // c:183
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1; // c:185 INCPOS
            if pos > ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                || zc_inblank(
                    ZLELINE.lock().unwrap()[pos.min(
                        ZLELL
                            .load(std::sync::atomic::Ordering::SeqCst)
                            .saturating_sub(1),
                    )],
                )
            {
                break;
            }
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:198
        }
    }
    if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
        != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        && false
    {
        // c:198
        ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:198 INCCS
    }
    0
}

/// Port of `viforwardwordend(char **args)` from `Src/Zle/zle_word.c:198`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn viforwardwordend(args: &[String]) -> i32 {
    // c:198
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardwordend(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        // c:211-217 — advance past inblank chars looking ahead.
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        {
            // c:211
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1; // c:213 INCPOS
            if pos > ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                || !zc_inblank(
                    ZLELINE.lock().unwrap()[pos.min(
                        ZLELL
                            .load(std::sync::atomic::Ordering::SeqCst)
                            .saturating_sub(1),
                    )],
                )
            {
                break;
            }
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:216
        }
        if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        {
            // c:218
            let mut pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1; // c:221 INCPOS
            let cc = if pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                wordclass(ZLELINE.lock().unwrap()[pos])
            } else {
                0
            }; // c:222
            loop {
                // c:223
                ZLECS.store(
                    pos.min(ZLELL.load(std::sync::atomic::Ordering::SeqCst)),
                    std::sync::atomic::Ordering::SeqCst,
                ); // c:224
                if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
                    == ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                {
                    break;
                } // c:225-226
                pos += 1; // c:227 INCPOS
                if pos > ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                    || wordclass(
                        ZLELINE.lock().unwrap()[pos.min(
                            ZLELL
                                .load(std::sync::atomic::Ordering::SeqCst)
                                .saturating_sub(1),
                        )],
                    ) != cc
                {
                    break; // c:228-229
                }
            }
        }
    }
    if ZLECS.load(std::sync::atomic::Ordering::SeqCst)
        != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        && false
    {
        // c:240
        ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:240 INCCS
    }
    0
}

/// Port of `backwardword(char **args)` from `Src/Zle/zle_word.c:240`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn backwardword(args: &[String]) -> i32 {
    // c:240
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:244
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = forwardword(args); // c:247
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:251
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:252
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:254 DECPOS
            if zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:255
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:257
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:272
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:272 DECPOS
            if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:272
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:272
        }
    }
    0
}

/// Port of `vibackwardword(char **args)` from `Src/Zle/zle_word.c:272`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn vibackwardword(args: &[String]) -> i32 {
    // c:272
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = viforwardword(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        let mut nl: i32 = 0; // c:284
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:285
            ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:286 DECCS
            if !zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
            {
                break;
            } // c:287
            if ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n' {
                nl += 1;
            } // c:289
            if nl == 2 {
                // c:290
                ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:291 INCCS
                break; // c:292
            }
        }
        if ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:295
            let mut pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:296
            let cc = wordclass(ZLELINE.lock().unwrap()[pos]); // c:297
            loop {
                // c:298
                ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:299
                if ZLECS.load(std::sync::atomic::Ordering::SeqCst) == 0 {
                    break;
                } // c:300-301
                pos -= 1; // c:302 DECPOS
                if {
                    let __c = ZLELINE.lock().unwrap()[pos];
                    wordclass(__c) != cc                     // c:313
                   || zc_inblank(__c)
                } {
                    break; // c:313
                }
            }
        }
    }
    0
}

/// Port of `vibackwardblankword(char **args)` from `Src/Zle/zle_word.c:313`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn vibackwardblankword(args: &[String]) -> i32 {
    // c:313
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = viforwardblankword(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        let mut nl: i32 = 0; // c:325
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:326
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:328 DECPOS
            if !zc_inblank(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:329
            if ZLELINE.lock().unwrap()[pos] == '\n' {
                nl += 1;
            } // c:331
            if nl == 2 {
                break;
            } // c:332
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:333
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:348
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:348 DECPOS
            if zc_inblank(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:348
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:348
        }
    }
    0
}

/// Port of `vibackwardwordend(char **args)` from `Src/Zle/zle_word.c:348`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn vibackwardwordend(args: &[String]) -> i32 {
    // c:348
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = viforwardwordend(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 && ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 1 {
        // c:359
        n -= 1;
        let cc = wordclass(
            ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst).min(
                ZLELL
                    .load(std::sync::atomic::Ordering::SeqCst)
                    .saturating_sub(1),
            )],
        ); // c:360
        ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:361 DECCS
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:362
            if {
                let __c = ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)];
                wordclass(__c) != cc                   // c:363
               || zc_iblank(__c)
            } {
                break;
            }
            ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:375 DECCS
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0
            && zc_iblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:375
            ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:375 DECCS
        }
    }
    0
}

/// Port of `vibackwardblankwordend(char **args)` from `Src/Zle/zle_word.c:375`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn vibackwardblankwordend(args: &[String]) -> i32 {
    // c:375
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = viforwardblankwordend(args);
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0
            && !zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:397
            ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:397 DECCS
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0
            && zc_inblank(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:397
            ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); // c:397 DECCS
        }
    }
    0
}

/// Port of `emacsbackwardword(char **args)` from `Src/Zle/zle_word.c:397`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn emacsbackwardword(args: &[String]) -> i32 {
    // c:397
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = emacsforwardword(args); // c:404
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:408
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:409
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:411 DECPOS
            if zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:412
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:414
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
            // c:429
            let pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1; // c:429 DECPOS
            if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:429
            ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); // c:429
        }
    }
    0
}

/// Port of `backwarddeleteword(char **args)` from `Src/Zle/zle_word.c:429`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn backwarddeleteword(args: &[String]) -> i32 {
    // c:429
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:429
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:433
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = deleteword(args); // c:436
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:440
        n -= 1;
        while x > 0 {
            // c:441
            let pos = x - 1; // c:443 DECPOS
            if zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:444
            x = pos;
        }
        while x > 0 {
            // c:448
            let pos = x - 1; // c:450 DECPOS
            if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:462
            x = pos;
        }
    }
    let ct = (ZLECS.load(std::sync::atomic::Ordering::SeqCst) - x) as i32;
    backdel(ct, /*CUT_RAW*/ 1); // c:462
    0
}

/// Port of `vibackwardkillword(UNUSED(char **args))` from `Src/Zle/zle_word.c:462`.
// this taken from "vibackwardword"                                         // c:462
/// WARNING: param names don't match C — Rust=(zle, _args) vs C=(args)
pub fn vibackwardkillword(_args: &[String]) -> i32 {
    // c:462
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:462
                                                                 // c:464 — `lim = (viinsbegin > findbol()) ? viinsbegin : findbol();`
    let viinsbegin =
        VIINSBEGIN.load(std::sync::atomic::Ordering::SeqCst);
    let bol = findbol();
    let lim: usize = viinsbegin.max(bol);
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        return 1;
    } // c:467
    let mut n = n;
    while n > 0 {
        // c:470
        n -= 1;
        while x > lim {
            // c:471
            let pos = x - 1; // c:473 DECPOS
            if !zc_iblank(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:474
            x = pos;
        }
        if x > lim {
            // c:478
            let mut pos = x - 1; // c:481 DECPOS
            let cc = wordclass(ZLELINE.lock().unwrap()[pos]); // c:482
            loop {
                // c:483
                x = pos + 1; // c:484 (after DECPOS reversal)
                let xv = pos;
                if xv <= lim {
                    // c:485-486
                    x = xv;
                    break;
                }
                if pos == 0 {
                    x = 0;
                    break;
                }
                pos -= 1; // c:487 DECPOS
                if wordclass(ZLELINE.lock().unwrap()[pos]) != cc {
                    // c:488
                    x = pos + 1;
                    break;
                }
                x = pos;
            }
        }
    }
    let ct = (ZLECS.load(std::sync::atomic::Ordering::SeqCst) - x) as i32;
    // c:499 — `backkill(zlecs - x, CUT_FRONT|CUT_RAW);`
    // CUT_FRONT = 0x02, CUT_RAW = 0x04 in zle.h.
    backkill(ct, 0x02 | 0x04);
    0
}

/// Port of `backwardkillword(char **args)` from `Src/Zle/zle_word.c:499`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn backwardkillword(args: &[String]) -> i32 {
    // c:499
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:499
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:504
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = killword(args); // c:507
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:511
        n -= 1;
        while x > 0 {
            // c:512
            let pos = x - 1; // c:514 DECPOS
            if zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:515
            x = pos;
        }
        while x > 0 {
            // c:519
            let pos = x - 1; // c:533 DECPOS
            if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:533
            x = pos;
        }
    }
    let ct = (ZLECS.load(std::sync::atomic::Ordering::SeqCst) - x) as i32;
    backkill(ct, 0x02 | 0x04); // c:533
    0
}

/// Port of `upcaseword(UNUSED(char **args))` from `Src/Zle/zle_word.c:533`.
/// WARNING: param names don't match C — Rust=(zle, _args) vs C=(args)
pub fn upcaseword(_args: &[String]) -> i32 {
    // c:533
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    let neg = n < 0; // c:536
    let ocs = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:536
    let mut n = if neg { -n } else { n };
    while n > 0 {
        // c:540
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:541
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:542 INCCS
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:543
            // c:555 — `zleline[zlecs] = ZC_toupper(zleline[zlecs]);`
            let c = ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)];
            ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] =
                c.to_uppercase().next().unwrap_or(c);
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:555 INCCS
        }
    }
    if neg {
        ZLECS.store(ocs, std::sync::atomic::Ordering::SeqCst);
    } // c:555-549
    0
}

/// Port of `downcaseword(UNUSED(char **args))` from `Src/Zle/zle_word.c:555`.
/// WARNING: param names don't match C — Rust=(zle, _args) vs C=(args)
pub fn downcaseword(_args: &[String]) -> i32 {
    // c:555
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    let neg = n < 0;
    let ocs = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut n = if neg { -n } else { n };
    while n > 0 {
        n -= 1;
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:563
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:577
            let c = ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)];
            ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] =
                c.to_lowercase().next().unwrap_or(c); // c:577 ZC_tolower
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:577 INCCS
        }
    }
    if neg {
        ZLECS.store(ocs, std::sync::atomic::Ordering::SeqCst);
    }
    0
}

/// Port of `capitalizeword(UNUSED(char **args))` from `Src/Zle/zle_word.c:577`.
/// WARNING: param names don't match C — Rust=(zle, _args) vs C=(args)
pub fn capitalizeword(_args: &[String]) -> i32 {
    // c:577
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    let neg = n < 0;
    let ocs = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut n = if neg { -n } else { n };
    while n > 0 {
        n -= 1;
        let mut first = true; // c:585
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:586
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        // c:588 — skip word-but-non-alpha chars (digits etc.) at start.
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && {
                let __c = ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)];
                zc_iword(__c) && !zc_ialpha(__c)
            }
        {
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        while ZLECS.load(std::sync::atomic::Ordering::SeqCst)
            != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            // c:590
            let c = ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)];
            ZLELINE.lock().unwrap()[ZLECS.load(std::sync::atomic::Ordering::SeqCst)] = if first {
                c.to_uppercase().next().unwrap_or(c) // c:591
            } else {
                c.to_lowercase().next().unwrap_or(c) // c:604
            };
            first = false; // c:604
            ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); // c:604 INCCS
        }
    }
    if neg {
        ZLECS.store(ocs, std::sync::atomic::Ordering::SeqCst);
    }
    0
}

/// Port of `deleteword(char **args)` from `Src/Zle/zle_word.c:604`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn deleteword(args: &[String]) -> i32 {
    // c:604
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:609
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = backwarddeleteword(args); // c:612
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:616
        n -= 1;
        while x != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[x])
        {
            // c:617
            x += 1; // c:618 INCPOS
        }
        while x != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[x])
        {
            // c:628
            x += 1; // c:628 INCPOS
        }
    }
    let ct = (x - ZLECS.load(std::sync::atomic::Ordering::SeqCst)) as i32;
    foredel(ct, /*CUT_RAW*/ 1); // c:628
    0
}

/// Port of `killword(char **args)` from `Src/Zle/zle_word.c:628`.
/// WARNING: param names don't match C — Rust=(zle, args) vs C=(args)
pub fn killword(args: &[String]) -> i32 {
    // c:628
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    if n < 0 {
        // c:633
        let saved = n;
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = -n;
        __g_zmod.flags |= MOD_MULT;
        let ret = backwardkillword(args); // c:636
        let mut __g_zmod = ZMOD.lock().unwrap();
        __g_zmod.mult = saved;
        __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        // c:640
        n -= 1;
        while x != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && !zc_iword(ZLELINE.lock().unwrap()[x])
        {
            // c:641
            x += 1; // c:642 INCPOS
        }
        while x != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
            && zc_iword(ZLELINE.lock().unwrap()[x])
        {
            // c:652
            x += 1; // c:652 INCPOS
        }
    }
    let ct = (x - ZLECS.load(std::sync::atomic::Ordering::SeqCst)) as i32;
    forekill(ct, /*CUT_RAW*/ 1); // c:652
    0
}

/// Port of `transposewords(UNUSED(char **args))` from `Src/Zle/zle_word.c:652`.
/// WARNING: param names don't match C — Rust=(zle, _args) vs C=(args)
pub fn transposewords(_args: &[String]) -> i32 {
    // c:652
    let mut __g_zmod = ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 {
        __g_zmod.mult
    } else {
        1
    };
    let neg = n < 0;
    let ocs = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut n = if neg { -n } else { n };
    let mut x = ZLECS.load(std::sync::atomic::Ordering::SeqCst);

    // c:662-663 — advance x to next word start (skip non-iword unless newline).
    while x != ZLELL.load(std::sync::atomic::Ordering::SeqCst) && {
        let __c = ZLELINE.lock().unwrap()[x];
        __c != '\n' && !zc_iword(__c)
    } {
        x += 1; // INCPOS
    }
    // c:665-682 — if at end-or-newline, search backward for word-start.
    if x == ZLELL.load(std::sync::atomic::Ordering::SeqCst) || ZLELINE.lock().unwrap()[x] == '\n' {
        // c:665
        x = ZLECS.load(std::sync::atomic::Ordering::SeqCst); // c:666
        while x > 0 {
            // c:667
            if zc_iword(ZLELINE.lock().unwrap()[x]) {
                break;
            } // c:668
            let pos = x - 1;
            if ZLELINE.lock().unwrap()[pos] == '\n' {
                break;
            } // c:672
            x = pos;
        }
        if x == 0 {
            return 1;
        } // c:676
        let pos = x - 1;
        if ZLELINE.lock().unwrap()[pos] == '\n' {
            return 1;
        } // c:680
    }

    // c:684-685 — find p4: end of current word.
    let mut p4 = x;
    while p4 != ZLELL.load(std::sync::atomic::Ordering::SeqCst)
        && zc_iword(ZLELINE.lock().unwrap()[p4])
    {
        // c:684
        p4 += 1; // INCPOS
    }
    // c:687-693 — find p3: start of current word.
    let mut p3 = p4;
    while p3 > 0 {
        // c:687
        let pos = p3 - 1;
        if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
            break;
        } // c:690
        p3 = pos;
    }
    if p3 == 0 {
        return 1;
    } // c:695

    let mut p2 = p3;
    let mut p1 = p3;
    let mut pt = p3;

    // c:700-718 — for each repeat, walk back to previous word.
    while n > 0 {
        n -= 1;
        // p2 = pt, walk back over non-iword chars.
        p2 = pt;
        while p2 > 0 {
            // c:701
            let pos = p2 - 1;
            if zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:704
            p2 = pos;
        }
        if p2 == 0 {
            return 1;
        } // c:708
          // p1 = p2, walk back over iword chars.
        p1 = p2;
        while p1 > 0 {
            // c:710
            let pos = p1 - 1;
            if !zc_iword(ZLELINE.lock().unwrap()[pos]) {
                break;
            } // c:713
            p1 = pos;
        }
        pt = p1; // c:717
    }

    // c:720-729 — build temp = [word3 segment | gap2-3 | word1 segment]
    // and write back into zleline[p1..p4].
    let mut temp: Vec<char> = Vec::with_capacity(p4 - p1);
    temp.extend_from_slice(&ZLELINE.lock().unwrap()[p3..p4]); // c:721-722
    temp.extend_from_slice(&ZLELINE.lock().unwrap()[p2..p3]); // c:723-724
    temp.extend_from_slice(&ZLELINE.lock().unwrap()[p1..p2]); // c:726-727
    for (i, c) in temp.iter().enumerate() {
        // c:729 ZS_memcpy
        ZLELINE.lock().unwrap()[p1 + i] = *c;
    }

    if neg {
        ZLECS.store(ocs, std::sync::atomic::Ordering::SeqCst);
    }
    // c:731-732
    else {
        ZLECS.store(p4, std::sync::atomic::Ordering::SeqCst);
    } // c:734
    0
}

// ---------------------------------------------------------------------------
// Rust-only word-motion helpers — vi-style word boundary lookups.
// C has separate widget bodies per (style × direction) in zle_word.c /
// zle_vi.c; the Rust port factors them into one helper parameterised by
// WordStyle so zle_vi.rs's six vi-style motion entries share the impl.
// Allowlisted alongside the pattern.rs / glob.rs extracted-helper
// precedent in tests/data/fake_fn_allowlist.txt.
// ---------------------------------------------------------------------------

/// Word style for `find_word_start` / `find_word_end`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WordStyle {
    Emacs,
    Vi,
    Shell,
    BlankDelimited,
}

/// Find the start of the current (or preceding) word at the cursor for
/// the requested word style.
pub fn find_word_start(style: WordStyle) -> usize {
    let mut pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    match style {
        WordStyle::Emacs => {
            while pos > 0 && {
                let __c = ZLELINE.lock().unwrap()[pos - 1];
                !(__c.is_alphanumeric() || __c == '_')
            } {
                pos -= 1;
            }
            while pos > 0 && {
                let __c = ZLELINE.lock().unwrap()[pos - 1];
                (__c.is_alphanumeric() || __c == '_')
            } {
                pos -= 1;
            }
        }
        WordStyle::Vi => {
            while pos > 0 && ZLELINE.lock().unwrap()[pos - 1].is_whitespace() {
                pos -= 1;
            }
            if pos > 0 {
                let is_word = ZLELINE.lock().unwrap()[pos - 1].is_alphanumeric()
                    || ZLELINE.lock().unwrap()[pos - 1] == '_';
                while pos > 0 {
                    let c = ZLELINE.lock().unwrap()[pos - 1];
                    if c.is_whitespace() || ((c.is_alphanumeric() || c == '_') != is_word) {
                        break;
                    }
                    pos -= 1;
                }
            }
        }
        WordStyle::Shell => {
            // No live callers; zle_vi.rs only invokes WordStyle::Vi /
            // BlankDelimited. Left as a no-op until a real shell-style
            // consumer surfaces.
        }
        WordStyle::BlankDelimited => {
            while pos > 0 && ZLELINE.lock().unwrap()[pos - 1].is_whitespace() {
                pos -= 1;
            }
            while pos > 0 && !ZLELINE.lock().unwrap()[pos - 1].is_whitespace() {
                pos -= 1;
            }
        }
    }
    pos
}

/// Find the end (exclusive) of the current (or following) word.
pub fn find_word_end(style: WordStyle) -> usize {
    let mut pos = ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    match style {
        WordStyle::Emacs => {
            while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst) && {
                let __c = ZLELINE.lock().unwrap()[pos];
                !(__c.is_alphanumeric() || __c == '_')
            } {
                pos += 1;
            }
            while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst) && {
                let __c = ZLELINE.lock().unwrap()[pos];
                (__c.is_alphanumeric() || __c == '_')
            } {
                pos += 1;
            }
        }
        WordStyle::Vi => {
            if pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                let is_word = ZLELINE.lock().unwrap()[pos].is_alphanumeric()
                    || ZLELINE.lock().unwrap()[pos] == '_';
                while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                    let c = ZLELINE.lock().unwrap()[pos];
                    if c.is_whitespace() || ((c.is_alphanumeric() || c == '_') != is_word) {
                        break;
                    }
                    pos += 1;
                }
                while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                    && ZLELINE.lock().unwrap()[pos].is_whitespace()
                {
                    pos += 1;
                }
            }
        }
        WordStyle::Shell => {
            // See WordStyle::Shell note in find_word_start — no live
            // callers; leave pos unchanged.
        }
        WordStyle::BlankDelimited => {
            while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                && !ZLELINE.lock().unwrap()[pos].is_whitespace()
            {
                pos += 1;
            }
            while pos < ZLELL.load(std::sync::atomic::Ordering::SeqCst)
                && ZLELINE.lock().unwrap()[pos].is_whitespace()
            {
                pos += 1;
            }
        }
    }
    pos
}

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

    fn line(s: &str) {
        zle_reset();
        *ZLELINE.lock().unwrap() = s.chars().collect();
        ZLELL.store(
            ZLELINE.lock().unwrap().len(),
            std::sync::atomic::Ordering::SeqCst,
        );
        ZLECS.store(0, std::sync::atomic::Ordering::SeqCst);
    }

    /// Verifies `wordclass` per c:74-78 dispatch table.
    #[test]
    fn wordclass_dispatch() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        assert_eq!(wordclass(' '), 0);
        assert_eq!(wordclass('a'), 1);
        assert_eq!(wordclass('_'), 1);
        assert_eq!(wordclass('5'), 1);
        assert_eq!(wordclass(';'), 2);
    }

    /// Verifies `forwardword` skips iword then non-iword (c:56-63).
    #[test]
    fn forwardword_basic() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar baz");
        forwardword(&[]);
        // From cs=0 (on 'f'), skip 'foo' iword then ' ' non-iword,
        // landing on 'b' of 'bar' at index 4.
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 4);
    }

    /// Verifies `backwardword` from end-of-line lands on word start
    /// (c:251-265).
    #[test]
    fn backwardword_lands_at_word_start() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar baz");
        ZLECS.store(
            ZLELL.load(std::sync::atomic::Ordering::SeqCst),
            std::sync::atomic::Ordering::SeqCst,
        );
        backwardword(&[]);
        // From cs=11 (past 'z'), skip non-iword (none), then iword
        // 'baz' to land at index 8.
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 8);
    }

    /// Verifies `upcaseword` mutates the next word in place (c:540-547).
    #[test]
    fn upcaseword_uppercases_next_word() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar");
        upcaseword(&[]);
        let s: String = ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "FOO bar");
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 3); // landed at end of 'FOO'
    }

    /// Verifies `downcaseword` mutates next word in place (c:562-569).
    #[test]
    fn downcaseword_lowercases_next_word() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("FOO Bar");
        downcaseword(&[]);
        let s: String = ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "foo Bar");
    }

    /// Verifies `capitalizeword` upcases first letter, lowercases
    /// rest (c:584-595).
    #[test]
    fn capitalizeword_first_only() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar");
        capitalizeword(&[]);
        let s: String = ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "Foo bar");
    }

    /// Verifies `deleteword` removes the next word (c:617-622).
    /// C semantics: the loop advances past non-word then past word,
    /// stopping at the first non-word char after the word. With
    /// cs=0 on "foo bar baz", x ends at 3 (the space after "foo"),
    /// and `foredel(3-0)` drops "foo" — leaving the leading space.
    #[test]
    fn deleteword_drops_next_word() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar baz");
        deleteword(&[]);
        let s: String = ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, " bar baz");
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    /// Verifies `transposewords` swaps the word at cursor with the
    /// preceding word (c:684-734).
    #[test]
    fn transposewords_swaps_pair() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        let mut z = line("foo bar");
        ZLECS.store(5, std::sync::atomic::Ordering::SeqCst); // mid-'bar'
        transposewords(&[]);
        let s: String = ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "bar foo");
    }

    /// `Src/Zle/zle_word.c:74-78` — `wordclass` is a 3-level ternary:
    /// `ZC_iblank(x) ? 0 : ((ZC_ialnum(x) || ZWC('_') == x) ? 1 :
    ///                       ZC_ipunct(x) ? 2 : 3)`.
    /// Returns 0=iblank, 1=alnum-or-underscore, 2=punctuation, 3=other.
    /// Every word-movement widget (`forwardword`, `backwardword`,
    /// `viforwardwordend`, `vibackwardword`, …) iterates against this
    /// classifier — a regression in any branch breaks all of them.
    #[test]
    fn wordclass_iblank_branch_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        // c:76 — `ZC_iblank(x) ? 0`. After the wcsiblank fix
        // `Src/Zle/zle.h:62` → `Src/utils.c:4302-4307` the iblank arm
        // catches every iswspace-except-newline char.
        assert_eq!(wordclass(' '), 0);
        assert_eq!(wordclass('\t'), 0);
        assert_eq!(wordclass('\r'), 0, "CR is iblank per wcsiblank");
        assert_eq!(wordclass('\x0c'), 0, "FF is iblank per wcsiblank");
        assert_eq!(wordclass('\x0b'), 0, "VT is iblank per wcsiblank");
        assert_eq!(wordclass('\u{00A0}'), 0, "NBSP is iblank per wcsiblank");
    }

    /// c:76-77 — `ZC_ialnum(x) || ZWC('_') == x` → 1. Alphanumerics
    /// AND `_` collapse to class 1 so `foo_bar` is treated as a
    /// single word boundary.
    #[test]
    fn wordclass_alnum_and_underscore_branch_returns_one() {
        let _g = crate::test_util::global_state_lock();
        // Letters.
        assert_eq!(wordclass('a'), 1);
        assert_eq!(wordclass('Z'), 1);
        // Digits.
        assert_eq!(wordclass('0'), 1);
        assert_eq!(wordclass('9'), 1);
        // Underscore — explicit special-case at c:76.
        assert_eq!(wordclass('_'), 1);
    }

    /// c:77 — `ZC_ipunct(x) ? 2`. Punctuation falls in class 2 so it
    /// separates word-1 from word-1 (e.g. `foo.bar` is two class-1
    /// chunks separated by a class-2 char).
    #[test]
    fn wordclass_punctuation_branch_returns_two() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(wordclass('.'), 2);
        assert_eq!(wordclass(','), 2);
        assert_eq!(wordclass(';'), 2);
        assert_eq!(wordclass('!'), 2);
        assert_eq!(wordclass('-'), 2);
        assert_eq!(wordclass('"'), 2);
    }

    /// c:77 — final `: 3` arm catches everything else. Newline is the
    /// canonical "other" char: `wcsiblank` at `Src/utils.c:4304`
    /// excludes `\n` from iblank, `iswalnum('\n')` is false,
    /// `iswpunct('\n')` is false → class 3.
    #[test]
    fn wordclass_other_branch_returns_three() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            wordclass('\n'),
            3,
            "newline excluded from iblank by wcsiblank"
        );
    }

    /// c:74-78 — Every char must map to exactly ONE class (0/1/2/3).
    /// Pin no-overlap across the four buckets.
    #[test]
    fn wordclass_returns_value_in_range_for_all_ascii() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        for b in 0..=127u8 {
            let c = b as char;
            let r = wordclass(c);
            assert!(
                (0..=3).contains(&r),
                "wordclass({:?}) = {} out of range [0,3]",
                c,
                r
            );
        }
    }

    /// c:74 — `wordclass` is a pure function. Same input → same
    /// output, 1000 times.
    #[test]
    fn wordclass_is_idempotent() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..1000 {
            assert_eq!(wordclass('a'), 1);
            assert_eq!(wordclass(' '), 0);
            assert_eq!(wordclass(';'), 2);
            assert_eq!(wordclass('\n'), 3);
        }
    }

    /// c:74-78 — Tab class (c:74 ZC_iblank case). Tab IS iblank
    /// per `wcsiblank` (`Src/utils.c:4304` — iswspace excluding `\n`).
    #[test]
    fn wordclass_tab_is_class_zero() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(
            wordclass('\t'),
            0,
            "tab is iblank → class 0 per c:75 wcsiblank branch"
        );
    }

    /// c:74-78 — Non-ASCII letters (e.g. `é`, `字`, `α`) fall into
    /// the `ZC_ialnum` (class 1) branch in C — they're locale-
    /// dependent alnum chars. Pin so a regen narrowing to ASCII
    /// breaks vi `aw`/`iw` over CJK/Latin1 names.
    #[test]
    fn wordclass_non_ascii_letters_are_class_one() {
        let _g = crate::test_util::global_state_lock();
        // These match iswalnum in most locales; the exact answer
        // depends on locale, but ASCII alpha+digit must be class 1
        // regardless.
        for c in ['a', 'z', 'A', 'Z', '0', '9'] {
            assert_eq!(wordclass(c), 1, "{:?} must be class 1 (ialnum)", c);
        }
    }

    /// c:45 — `forwardword` on an empty buffer must not panic. The
    /// `n>0` loop has nothing to walk; cursor stays at 0.
    #[test]
    fn forwardword_on_empty_buffer_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        line("");
        let r = forwardword(&[]);
        assert_eq!(r, 0, "forwardword on empty must succeed");
        assert_eq!(
            ZLECS.load(std::sync::atomic::Ordering::SeqCst),
            0,
            "cursor must not advance past empty buffer"
        );
    }

    /// c:240 — `backwardword` on an empty buffer no-panic; cursor
    /// stays at 0.
    #[test]
    fn backwardword_on_empty_buffer_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        line("");
        let r = backwardword(&[]);
        assert_eq!(r, 0);
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    /// c:45 — `forwardword` at end of buffer: cursor is already at
    /// ZLELL, no movement, returns 0.
    #[test]
    fn forwardword_at_end_of_buffer_stays_at_zlell() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        line("foo");
        ZLECS.store(3, std::sync::atomic::Ordering::SeqCst);
        let r = forwardword(&[]);
        assert_eq!(r, 0);
        assert_eq!(
            ZLECS.load(std::sync::atomic::Ordering::SeqCst),
            3,
            "cursor at ZLELL must stay there"
        );
    }

    /// c:240 — `backwardword` at start (ZLECS=0): no movement.
    #[test]
    fn backwardword_at_start_of_buffer_stays_at_zero() {
        let _g = crate::test_util::global_state_lock();
        let _g = zle_test_setup();
        line("foo bar");
        ZLECS.store(0, std::sync::atomic::Ordering::SeqCst);
        let r = backwardword(&[]);
        assert_eq!(r, 0);
        assert_eq!(ZLECS.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    // ─── zsh-corpus pins for wordclass ─────────────────────────────

    /// `wordclass(' ')` returns 0 (blank).
    #[test]
    fn zle_word_corpus_wordclass_space_is_blank() {
        assert_eq!(wordclass(' '), 0);
        assert_eq!(wordclass('\t'), 0);
    }

    /// `wordclass('a')` returns 1 (alnum).
    #[test]
    fn zle_word_corpus_wordclass_alpha_is_alnum() {
        assert_eq!(wordclass('a'), 1);
        assert_eq!(wordclass('Z'), 1);
        assert_eq!(wordclass('0'), 1);
        assert_eq!(wordclass('9'), 1);
    }

    /// `wordclass('_')` returns 1 — underscore counts as alnum word char.
    #[test]
    fn zle_word_corpus_wordclass_underscore_is_alnum() {
        assert_eq!(wordclass('_'), 1,
            "underscore is alnum per zsh word-class");
    }

    /// `wordclass('.')` returns 2 (punct).
    #[test]
    fn zle_word_corpus_wordclass_punct_is_two() {
        assert_eq!(wordclass('.'), 2);
        assert_eq!(wordclass('!'), 2);
        assert_eq!(wordclass(';'), 2);
        assert_eq!(wordclass(','), 2);
    }

    /// `wordclass` separates word/non-word classes (1 vs others).
    #[test]
    fn zle_word_corpus_wordclass_distinct_classes() {
        let blank = wordclass(' ');
        let alnum = wordclass('a');
        let punct = wordclass('.');
        // All three must be distinct.
        assert_ne!(blank, alnum);
        assert_ne!(alnum, punct);
        assert_ne!(blank, punct);
    }

    /// `wordclass('\n')` returns 3 — newline is NOT `iblank` in zsh
    /// (iblank covers space+tab only). It falls into class 3 (other).
    #[test]
    fn zle_word_corpus_wordclass_newline_is_other() {
        assert_eq!(wordclass('\n'), 3,
            "newline is NOT iblank (iblank = space/tab only) → class 3");
    }
}