zshrs 0.11.3

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
//! `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 fns 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, MOD_TMULT, MOD_VIBUF, MOD_VIAPP, MOD_NEG, MOD_NULL, MOD_CHAR, MOD_LINE, MOD_PRI, MOD_CLIP, MOD_OSSEL};

// ---------------------------------------------------------------------------
// Helpers shared by every widget below — character classification + cursor
// movement. All inlined where used; no Rust-only helper fns.
//
// `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 fns).
// ---------------------------------------------------------------------------

// `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 fns —
// the build script's drift gate rejects Rust-only helpers, even when
// they only collapse repeated reads. Pattern:
//
//   zmult    → `if crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0
//                 { crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult } else { 1 }`
//   set zmult → `crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult = v;
//                crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_MULT;`
//   wordflag/virangeflag → `false` (vi-mode plumbing not yet wired).
//
// 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)]
#[allow(unused_imports)]
use crate::ported::zle::zle_main::*;
#[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_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::*;

#[inline] fn zc_iword(c: char) -> bool { c.is_alphanumeric() || c == '_' }
#[inline] fn zc_ialnum(c: char) -> bool { c.is_alphanumeric() }
#[inline] fn zc_ialpha(c: char) -> bool { c.is_alphabetic() }
#[inline] fn zc_iblank(c: char) -> bool { c == ' ' || c == '\t' }
#[inline] fn zc_inblank(c: char) -> bool { c == ' ' || c == '\t' || c == '\n' }
#[inline] fn zc_ipunct(c: char) -> bool { c.is_ascii_punctuation() }

/// 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 crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0 { crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult } else { 1 };                                                  // c:45
    if n < 0 {                                                           // c:49
        let saved = n;
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult = -n; crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_MULT;                                              // c:51
        let ret = backwardword(args);                               // c:52
        crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult = saved; crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags |= MOD_MULT;                                           // c:53
        return ret;
    }
    let mut n = n;
    while n > 0 {                                                        // c:56
        n -= 1;
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:57
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:58 INCCS
        }
        if false && n == 0 {                                        // c:59
            return 0;                                                    // c:60
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:74
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardword(args);                             // c:89
        let mut __g_zmod = crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]);                      // c:95
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && wordclass(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) == cc {  // c:96
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:97 INCCS
        }
        if false && n == 0 { return 0; }                            // c:99
        let mut nl = 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)] == '\n' { 1 } else { 0 };  // c:101
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && nl < 2
              && zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)])                      // c:112
        {
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:112 INCCS
            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)] == '\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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardblankword(args);
        let mut __g_zmod = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = saved; __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:125
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        if false && n == 0 { return 0; }                            // c:127
        let mut nl = 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)] == '\n' { 1 } else { 0 };
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && nl < 2
              && zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)])
        {
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            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)] == '\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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = emacsbackwardword(args);                          // c:147
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:152
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        if false && n == 0 { return 0; }                            // c:164
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:164
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardblankwordend(args);
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {                                   // c:176
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1;                                     // c:178 INCPOS
            if pos > crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) || !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos.min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1))]) {
                break;
            }
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:181
        }
        // c:183-189 — advance over non-inblank chars.
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {                                   // c:183
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1;                                     // c:185 INCPOS
            if pos > crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) || zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos.min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1))]) {
                break;
            }
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:198
        }
    }
    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) && false {                         // c:198
        crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = vibackwardwordend(args);
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {                                   // c:211
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1;                                     // c:213 INCPOS
            if pos > crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) || !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos.min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1))]) {
                break;
            }
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:216
        }
        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) {                                      // c:218
            let mut pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) + 1;                                 // c:221 INCPOS
            let cc = if pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { wordclass(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) }
                     else { 0 };                                         // c:222
            loop {                                                       // c:223
                crate::ported::zle::zle_main::ZLECS.store(pos.min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst)), std::sync::atomic::Ordering::SeqCst);                          // c:224
                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) { break; }                     // c:225-226
                pos += 1;                                                // c:227 INCPOS
                if pos > crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) || wordclass(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos.min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1))]) != cc {
                    break;                                               // c:228-229
                }
            }
        }
    }
    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) && false {                         // c:240
        crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = forwardword(args);                                // c:247
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:252
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:254 DECPOS
            if zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                     // c:255
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:257
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:272
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:272 DECPOS
            if !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                    // c:272
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = viforwardword(args);
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:285
            crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);                                              // c:286 DECCS
            if !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) { break; }            // c:287
            if crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)] == '\n' { nl += 1; }               // c:289
            if nl == 2 {                                                 // c:290
                crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                          // c:291 INCCS
                break;                                                   // c:292
            }
        }
        if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                               // c:295
            let mut pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);                                     // c:296
            let cc = wordclass(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]);                        // c:297
            loop {                                                       // c:298
                crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                         // c:299
                if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) == 0 { break; }                             // c:300-301
                pos -= 1;                                                // c:302 DECPOS
                if { let __c = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = viforwardblankword(args);
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:326
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:328 DECPOS
            if !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                  // c:329
            if crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos] == '\n' { nl += 1; }                     // c:331
            if nl == 2 { break; }                                        // c:332
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:333
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:348
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:348 DECPOS
            if zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                   // c:348
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = viforwardwordend(args);
        let mut __g_zmod = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = saved; __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 1 {                                       // c:359
        n -= 1;
        let cc = wordclass(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst).min(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1))]);  // c:360
        crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);                                                  // c:361 DECCS
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:362
            if { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]; wordclass(__c) != cc                   // c:363
               || zc_iblank(__c) } {
                break;
            }
            crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);                                              // c:375 DECCS
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 && zc_iblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {       // c:375
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = viforwardblankwordend(args);
        let mut __g_zmod = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = saved; __g_zmod.flags |= MOD_MULT;
        return ret;
    }
    let mut n = n;
    while n > 0 {
        n -= 1;
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 && !zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {     // c:397
            crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);                                              // c:397 DECCS
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 && zc_inblank(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {      // c:397
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = emacsforwardword(args);                           // c:404
        let mut __g_zmod = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:409
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:411 DECPOS
            if zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                     // c:412
            crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst);                                             // c:414
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {                                            // c:429
            let pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - 1;                                     // c:429 DECPOS
            if !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                    // c:429
            crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);                                               // c:429
    let mut __g_zmod = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = deleteword(args);                                 // c:436
        let mut __g_zmod = crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                     // c:444
            x = pos;
        }
        while x > 0 {                                                    // c:448
            let pos = x - 1;                                             // c:450 DECPOS
            if !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                    // c:462
            x = pos;
        }
    }
    let ct = (crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - x) as i32;
    crate::ported::zle::zle_utils::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);                                               // c:462
    // c:464 — `lim = (viinsbegin > findbol()) ? viinsbegin : findbol();`
    let viinsbegin = crate::ported::zle::zle_main::VIINSBEGIN.load(std::sync::atomic::Ordering::SeqCst);
    let bol = crate::ported::zle::zle_utils::findbol();
    let lim: usize = viinsbegin.max(bol);
    let mut __g_zmod = crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) != cc {                   // c:488
                    x = pos + 1;
                    break;
                }
                x = pos;
            }
        }
    }
    let ct = (crate::ported::zle::zle_main::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.
    crate::ported::zle::zle_utils::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);                                               // c:499
    let mut __g_zmod = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = killword(args);                                   // c:507
        let mut __g_zmod = crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                     // c:515
            x = pos;
        }
        while x > 0 {                                                    // c:519
            let pos = x - 1;                                             // c:533 DECPOS
            if !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]) { break; }                    // c:533
            x = pos;
        }
    }
    let ct = (crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) - x) as i32;
    crate::ported::zle::zle_utils::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 = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:541
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:542 INCCS
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:543
            // c:555 — `zleline[zlecs] = ZC_toupper(zleline[zlecs]);`
            let c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.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.to_uppercase().next().unwrap_or(c);
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:555 INCCS
        }
    }
    if neg { crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 { __g_zmod.mult } else { 1 };
    let neg = n < 0;
    let ocs = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut n = if neg { -n } else { n };
    while n > 0 {
        n -= 1;
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:563
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {   // c:577
            let c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.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.to_lowercase().next().unwrap_or(c);   // c:577 ZC_tolower
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:577 INCCS
        }
    }
    if neg { crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 { __g_zmod.mult } else { 1 };
    let neg = n < 0;
    let ocs = crate::ported::zle::zle_main::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 crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {  // c:586
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        // c:588 — skip word-but-non-alpha chars (digits etc.) at start.
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]; zc_iword(__c) && !zc_ialpha(__c) } {
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
        while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)]) {   // c:590
            let c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[crate::ported::zle::zle_main::ZLECS.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)] = 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
            crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);                                              // c:604 INCCS
        }
    }
    if neg { crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut __g_zmod = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = backwarddeleteword(args);                         // c:612
        let mut __g_zmod = crate::ported::zle::zle_main::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 != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x]) {              // c:617
            x += 1;                                                      // c:618 INCPOS
        }
        while x != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x]) {               // c:628
            x += 1;                                                      // c:628 INCPOS
        }
    }
    let ct = (x - crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)) as i32;
    crate::ported::zle::zle_utils::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut __g_zmod = crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
        __g_zmod.mult = -n; __g_zmod.flags |= MOD_MULT;
        let ret = backwardkillword(args);                           // c:636
        let mut __g_zmod = crate::ported::zle::zle_main::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 != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x]) {              // c:641
            x += 1;                                                      // c:642 INCPOS
        }
        while x != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x]) {               // c:652
            x += 1;                                                      // c:652 INCPOS
        }
    }
    let ct = (x - crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)) as i32;
    crate::ported::zle::zle_utils::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 = crate::ported::zle::zle_main::ZMOD.lock().unwrap();
    let n = if __g_zmod.flags & MOD_MULT != 0 { __g_zmod.mult } else { 1 };
    let neg = n < 0;
    let ocs = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    let mut n = if neg { -n } else { n };
    let mut x = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);

    // c:662-663 — advance x to next word start (skip non-iword unless newline).
    while x != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && { let __c = crate::ported::zle::zle_main::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 == crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) || crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x] == '\n' {                        // c:665
        x = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);                                                   // c:666
        while x > 0 {                                                    // c:667
            if zc_iword(crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[x]) { break; }                       // c:668
            let pos = x - 1;
            if crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos] == '\n' { break; }                       // c:672
            x = pos;
        }
        if x == 0 { return 1; }                                          // c:676
        let pos = x - 1;
        if crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos] == '\n' { return 1; }                        // c:680
    }

    // c:684-685 — find p4: end of current word.
    let mut p4 = x;
    while p4 != crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && zc_iword(crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::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(crate::ported::zle::zle_main::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(&crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[p3..p4]);                        // c:721-722
    temp.extend_from_slice(&crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[p2..p3]);                        // c:723-724
    temp.extend_from_slice(&crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[p1..p2]);                        // c:726-727
    for (i, c) in temp.iter().enumerate() {                              // c:729 ZS_memcpy
        crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[p1 + i] = *c;
    }

    if neg { crate::ported::zle::zle_main::ZLECS.store(ocs, std::sync::atomic::Ordering::SeqCst); }                                          // c:731-732
    else { crate::ported::zle::zle_main::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/ported_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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    match style {
        WordStyle::Emacs => {
            while pos > 0 && { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1]; !(__c.is_alphanumeric()
                               || __c == '_') } {
                pos -= 1;
            }
            while pos > 0 && { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1]; (__c.is_alphanumeric()
                              || __c == '_') } {
                pos -= 1;
            }
        }
        WordStyle::Vi => {
            while pos > 0 && crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1].is_whitespace() {
                pos -= 1;
            }
            if pos > 0 {
                let is_word = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1].is_alphanumeric()
                              || crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1] == '_';
                while pos > 0 {
                    let c = crate::ported::zle::zle_main::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 && crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos - 1].is_whitespace() {
                pos -= 1;
            }
            while pos > 0 && !crate::ported::zle::zle_main::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 = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
    match style {
        WordStyle::Emacs => {
            while pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]; !(__c.is_alphanumeric()
                                        || __c == '_') } {
                pos += 1;
            }
            while pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && { let __c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos]; (__c.is_alphanumeric()
                                       || __c == '_') } {
                pos += 1;
            }
        }
        WordStyle::Vi => {
            if pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                let is_word = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos].is_alphanumeric()
                              || crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos] == '_';
                while pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) {
                    let c = crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos];
                    if c.is_whitespace()
                       || ((c.is_alphanumeric() || c == '_') != is_word) {
                        break;
                    }
                    pos += 1;
                }
                while pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && crate::ported::zle::zle_main::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 < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && !crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos].is_whitespace() {
                pos += 1;
            }
            while pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && crate::ported::zle::zle_main::ZLELINE.lock().unwrap()[pos].is_whitespace() {
                pos += 1;
            }
        }
    }
    pos
}

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

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

    /// Verifies `wordclass` per c:74-78 dispatch table.
    #[test]
    fn wordclass_dispatch() {
        let _g = crate::ported::zle::zle_main::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::ported::zle::zle_main::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!(crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("foo bar baz");
        crate::ported::zle::zle_main::ZLECS.store(crate::ported::zle::zle_main::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!(crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("foo bar");
        upcaseword(&[]);
        let s: String = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "FOO bar");
        assert_eq!(crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("FOO Bar");
        downcaseword(&[]);
        let s: String = crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("foo bar");
        capitalizeword(&[]);
        let s: String = crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("foo bar baz");
        deleteword(&[]);
        let s: String = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, " bar baz");
        assert_eq!(crate::ported::zle::zle_main::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::ported::zle::zle_main::zle_test_setup();
        let mut z = line("foo bar");
        crate::ported::zle::zle_main::ZLECS.store(5, std::sync::atomic::Ordering::SeqCst);  // mid-'bar'
        transposewords(&[]);
        let s: String = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().iter().collect();
        assert_eq!(s, "bar foo");
    }
}