ztmux 0.1.0

A Rust port of tmux — the full terminal multiplexer, server and client
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
// Copyright (c) 2020 Anindya Mukherjee <anindya49@hotmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::*;

/// C `vendor/tmux/grid-reader.c:24`: `void grid_reader_start(struct grid_reader *gr, struct grid *gd, u_int cx, u_int cy)`
pub unsafe fn grid_reader_start(gr: *mut grid_reader, gd: *mut grid, cx: u32, cy: u32) {
    unsafe {
        (*gr).gd = gd;
        (*gr).cx = cx;
        (*gr).cy = cy;
    }
}

/// C `vendor/tmux/grid-reader.c:33`: `void grid_reader_get_cursor(struct grid_reader *gr, u_int *cx, u_int *cy)`
pub unsafe fn grid_reader_get_cursor(gr: *mut grid_reader, cx: *mut u32, cy: *mut u32) {
    unsafe {
        *cx = (*gr).cx;
        *cy = (*gr).cy;
    }
}

/// C `vendor/tmux/grid-reader.c:41`: `u_int grid_reader_line_length(struct grid_reader *gr)`
pub unsafe fn grid_reader_line_length(gr: *mut grid_reader) -> u32 {
    unsafe { grid_line_length((*gr).gd, (*gr).cy) }
}

/// C `vendor/tmux/grid-reader.c:48`: `void grid_reader_cursor_right(struct grid_reader *gr, int wrap, int all, int onemore)`
pub unsafe fn grid_reader_cursor_right(gr: *mut grid_reader, wrap: u32, all: i32) {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();

        let px = if all != 0 {
            (*(*gr).gd).sx
        } else {
            grid_reader_line_length(gr)
        };

        if wrap != 0 && (*gr).cx >= px && (*gr).cy < (*(*gr).gd).hsize + (*(*gr).gd).sy - 1 {
            grid_reader_cursor_start_of_line(gr, 0);
            grid_reader_cursor_down(gr);
        } else if (*gr).cx < px {
            (*gr).cx += 1;
            while (*gr).cx < px {
                grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, gc.as_mut_ptr());
                if !(*gc.as_ptr()).flags.intersects(grid_flag::PADDING) {
                    break;
                }
                (*gr).cx += 1;
            }
        }
    }
}

/// C `vendor/tmux/grid-reader.c:79`: `void grid_reader_cursor_left(struct grid_reader *gr, int wrap)`
pub unsafe fn grid_reader_cursor_left(gr: *mut grid_reader, wrap: i32) {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();

        while (*gr).cx > 0 {
            grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, gc.as_mut_ptr());
            if !(*gc.as_ptr()).flags.intersects(grid_flag::PADDING) {
                break;
            }
            (*gr).cx -= 1;
        }
        if (*gr).cx == 0
            && (*gr).cy > 0
            && (wrap != 0
                || (*grid_get_line((*gr).gd, (*gr).cy - 1))
                    .flags
                    .intersects(grid_line_flag::WRAPPED))
        {
            grid_reader_cursor_up(gr);
            grid_reader_cursor_end_of_line(gr, 0, 0);
        } else if (*gr).cx > 0 {
            (*gr).cx -= 1;
        }
    }
}

/// C `vendor/tmux/grid-reader.c:100`: `void grid_reader_cursor_down(struct grid_reader *gr)`
pub unsafe fn grid_reader_cursor_down(gr: *mut grid_reader) {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();

        if (*gr).cy < (*(*gr).gd).hsize + (*(*gr).gd).sy - 1 {
            (*gr).cy += 1;
        }
        while (*gr).cx > 0 {
            grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, gc);
            if !(*gc).flags.intersects(grid_flag::PADDING) {
                break;
            }
            (*gr).cx -= 1;
        }
    }
}

/// C `vendor/tmux/grid-reader.c:116`: `void grid_reader_cursor_up(struct grid_reader *gr)`
pub unsafe fn grid_reader_cursor_up(gr: *mut grid_reader) {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();

        if (*gr).cy > 0 {
            (*gr).cy -= 1;
        }
        while (*gr).cx > 0 {
            grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, gc);
            if !(*gc).flags.intersects(grid_flag::PADDING) {
                break;
            }
            (*gr).cx -= 1;
        }
    }
}

/// C `vendor/tmux/grid-reader.c:132`: `void grid_reader_cursor_start_of_line(struct grid_reader *gr, int wrap)`
pub unsafe fn grid_reader_cursor_start_of_line(gr: *mut grid_reader, wrap: i32) {
    unsafe {
        if wrap != 0 {
            while (*gr).cy > 0
                && (*grid_get_line((*gr).gd, (*gr).cy - 1))
                    .flags
                    .intersects(grid_line_flag::WRAPPED)
            {
                (*gr).cy -= 1;
            }
        }
        (*gr).cx = 0;
    }
}

/// C `vendor/tmux/grid-reader.c:145`: `void grid_reader_cursor_end_of_line(struct grid_reader *gr, int wrap, int all)`
pub unsafe fn grid_reader_cursor_end_of_line(gr: *mut grid_reader, wrap: i32, all: i32) {
    unsafe {
        if wrap != 0 {
            let yy = (*(*gr).gd).hsize + (*(*gr).gd).sy - 1;
            while (*gr).cy < yy
                && (*grid_get_line((*gr).gd, (*gr).cy))
                    .flags
                    .intersects(grid_line_flag::WRAPPED)
            {
                (*gr).cy += 1;
            }
        }
        if all != 0 {
            (*gr).cx = (*(*gr).gd).sx;
        } else {
            (*gr).cx = grid_reader_line_length(gr);
        }
    }
}

/// C `vendor/tmux/grid-reader.c:163`: `static int grid_reader_handle_wrap(struct grid_reader *gr, u_int *xx, u_int *yy)`
pub unsafe fn grid_reader_handle_wrap(gr: *mut grid_reader, xx: *mut u32, yy: *mut u32) -> i32 {
    unsafe {
        while (*gr).cx > *xx {
            if (*gr).cy == *yy {
                return 0;
            }
            grid_reader_cursor_start_of_line(gr, 0);
            grid_reader_cursor_down(gr);

            if (*grid_get_line((*gr).gd, (*gr).cy))
                .flags
                .intersects(grid_line_flag::WRAPPED)
            {
                *xx = (*(*gr).gd).sx - 1;
            } else {
                *xx = grid_reader_line_length(gr);
            }
        }
    }
    1
}

/// C `vendor/tmux/grid-reader.c:186`: `int grid_reader_in_set(struct grid_reader *gr, const char *set)`
pub unsafe fn grid_reader_in_set(gr: *mut grid_reader, set: *const u8) -> bool {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();

        grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, gc);
        if (*gc).flags.intersects(grid_flag::PADDING) {
            return false;
        }
        utf8_cstrhas(set, &raw mut (*gc).data)
    }
}

/// C `vendor/tmux/grid-reader.c:193`: `void grid_reader_cursor_next_word(struct grid_reader *gr, const char *separators)`
pub unsafe fn grid_reader_cursor_next_word(gr: *mut grid_reader, separators: *const u8) {
    unsafe {
        // Do not break up wrapped words.
        let mut xx = if (*grid_get_line((*gr).gd, (*gr).cy))
            .flags
            .intersects(grid_line_flag::WRAPPED)
        {
            (*(*gr).gd).sx - 1
        } else {
            grid_reader_line_length(gr)
        };
        let mut yy = (*(*gr).gd).hsize + (*(*gr).gd).sy - 1;

        if grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) == 0 {
            return;
        }
        if !grid_reader_in_set(gr, WHITESPACE) {
            if grid_reader_in_set(gr, separators) {
                loop {
                    (*gr).cx += 1;

                    if !(grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0
                        && grid_reader_in_set(gr, separators)
                        && !grid_reader_in_set(gr, WHITESPACE))
                    {
                        break;
                    }
                }
            } else {
                loop {
                    (*gr).cx += 1;
                    if !(grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0
                        && (!grid_reader_in_set(gr, separators)
                            || grid_reader_in_set(gr, WHITESPACE)))
                    {
                        break;
                    }
                }
            }
        }
        while grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0
            && grid_reader_in_set(gr, WHITESPACE)
        {
            (*gr).cx += 1;
        }
    }
}

/// C `vendor/tmux/grid-reader.c:238`: `void grid_reader_cursor_next_word_end(struct grid_reader *gr, const char *separators)`
pub unsafe fn grid_reader_cursor_next_word_end(gr: *mut grid_reader, separators: *const u8) {
    unsafe {
        // Do not break up wrapped words.
        let mut xx = if (*grid_get_line((*gr).gd, (*gr).cy))
            .flags
            .intersects(grid_line_flag::WRAPPED)
        {
            (*(*gr).gd).sx - 1
        } else {
            grid_reader_line_length(gr)
        };
        let mut yy = (*(*gr).gd).hsize + (*(*gr).gd).sy - 1;

        while grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0 {
            if grid_reader_in_set(gr, WHITESPACE) {
                (*gr).cx += 1;
            } else if grid_reader_in_set(gr, separators) {
                loop {
                    (*gr).cx += 1;

                    if !(grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0
                        && grid_reader_in_set(gr, separators)
                        && !grid_reader_in_set(gr, WHITESPACE))
                    {
                        break;
                    }
                }
                return;
            } else {
                loop {
                    (*gr).cx += 1;

                    if !(grid_reader_handle_wrap(gr, &raw mut xx, &raw mut yy) != 0
                        && !(grid_reader_in_set(gr, WHITESPACE)
                            || grid_reader_in_set(gr, separators)))
                    {
                        break;
                    }
                }
                return;
            }
        }
    }
}

/// C `vendor/tmux/grid-reader.c:283`: `void grid_reader_cursor_previous_word(struct grid_reader *gr, const char *separators, int already, int stop_at_eol)`
pub unsafe fn grid_reader_cursor_previous_word(
    gr: *mut grid_reader,
    separators: *const u8,
    already: i32,
    stop_at_eol: bool,
) {
    unsafe {
        let mut oldx: i32;
        let word_is_letters;

        if already != 0 || grid_reader_in_set(gr, WHITESPACE) {
            loop {
                if (*gr).cx > 0 {
                    (*gr).cx -= 1;
                    if !grid_reader_in_set(gr, WHITESPACE) {
                        word_is_letters = !grid_reader_in_set(gr, separators);
                        break;
                    }
                } else {
                    if (*gr).cy == 0 {
                        return;
                    }
                    grid_reader_cursor_up(gr);
                    grid_reader_cursor_end_of_line(gr, 0, 0);

                    if stop_at_eol && (*gr).cx > 0 {
                        oldx = (*gr).cx as i32;
                        (*gr).cx -= 1;
                        let at_eol = grid_reader_in_set(gr, WHITESPACE);
                        (*gr).cx = oldx as u32;
                        if at_eol {
                            word_is_letters = false;
                            break;
                        }
                    }
                }
            }
        } else {
            word_is_letters = !grid_reader_in_set(gr, separators);
        }

        let mut oldx;
        let mut oldy;
        loop {
            oldx = (*gr).cx;
            oldy = (*gr).cy;
            if (*gr).cx == 0 {
                if (*gr).cy == 0
                    || (!(*grid_get_line((*gr).gd, (*gr).cy - 1))
                        .flags
                        .intersects(grid_line_flag::WRAPPED))
                {
                    break;
                }
                grid_reader_cursor_up(gr);
                grid_reader_cursor_end_of_line(gr, 0, 1);
            }
            if (*gr).cx > 0 {
                (*gr).cx -= 1;
            }

            if grid_reader_in_set(gr, WHITESPACE)
                || word_is_letters == grid_reader_in_set(gr, separators)
            {
                break;
            }
        }
        (*gr).cx = oldx;
        (*gr).cy = oldy;
    }
}

/// C `vendor/tmux/grid-reader.c:357`: `int grid_reader_cursor_jump(struct grid_reader *gr, const struct utf8_data *jc)`
pub unsafe fn grid_reader_cursor_jump(gr: *mut grid_reader, jc: *const utf8_data) -> i32 {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();

        let mut px = (*gr).cx;
        let yy = (*(*gr).gd).hsize + (*(*gr).gd).sy - 1;

        let mut py = (*gr).cy;
        while py <= yy {
            let xx = grid_line_length((*gr).gd, py);
            while px < xx {
                grid_get_cell((*gr).gd, px, py, gc);
                if !(*gc).flags.intersects(grid_flag::PADDING)
                    && (*gc).data.size == (*jc).size
                    && memcmp(
                        (*gc).data.data.as_ptr().cast(),
                        (*jc).data.as_ptr().cast(),
                        (*gc).data.size as usize,
                    ) == 0
                {
                    (*gr).cx = px;
                    (*gr).cy = py;
                    return 1;
                }
                px += 1;
            }

            if py == yy
                || !(*grid_get_line((*gr).gd, py))
                    .flags
                    .intersects(grid_line_flag::WRAPPED)
            {
                return 0;
            }
            px = 0;
            py += 1;
        }
    }
    0
}

/// C `vendor/tmux/grid-reader.c:387`: `int grid_reader_cursor_jump_back(struct grid_reader *gr, const struct utf8_data *jc)`
pub unsafe fn grid_reader_cursor_jump_back(gr: *mut grid_reader, jc: *mut utf8_data) -> i32 {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();

        let mut xx = (*gr).cx + 1;

        let mut py = (*gr).cy + 1;
        let mut px;
        while py > 0 {
            px = xx;
            while px > 0 {
                grid_get_cell((*gr).gd, px - 1, py - 1, gc);
                if !((*gc).flags.intersects(grid_flag::PADDING)
                    && (*gc).data.size == (*jc).size
                    && memcmp(
                        (*gc).data.data.as_ptr().cast(),
                        (*jc).data.as_ptr().cast(),
                        (*gc).data.size as usize,
                    ) == 0)
                {
                    (*gr).cx = px - 1;
                    (*gr).cy = py - 1;
                    return 1;
                }
                px -= 1;
            }

            if py == 1
                || !(*grid_get_line((*gr).gd, py - 2))
                    .flags
                    .intersects(grid_line_flag::WRAPPED)
            {
                return 0;
            }
            xx = grid_line_length((*gr).gd, py - 2);
            py -= 1;
        }
    }
    0
}

/// C `vendor/tmux/grid-reader.c:414`: `void grid_reader_cursor_back_to_indentation(struct grid_reader *gr)`
pub unsafe fn grid_reader_cursor_back_to_indentation(gr: *mut grid_reader) {
    unsafe {
        let mut gc = MaybeUninit::<grid_cell>::uninit();
        let gc = gc.as_mut_ptr();
        // u_int px, py, xx, yy, oldx, oldy;

        let yy = (*(*gr).gd).hsize + (*(*gr).gd).sy - 1;
        let oldx = (*gr).cx;
        let oldy = (*gr).cy;
        grid_reader_cursor_start_of_line(gr, 1);

        for py in (*gr).cy..=yy {
            let xx = grid_line_length((*gr).gd, py);
            for px in 0..xx {
                grid_get_cell((*gr).gd, px, py, gc);
                if (*gc).data.size != 1 || (*gc).data.data[0] != b' ' {
                    (*gr).cx = px;
                    (*gr).cy = py;
                    return;
                }
            }
            if !(*grid_get_line((*gr).gd, py))
                .flags
                .intersects(grid_line_flag::WRAPPED)
            {
                break;
            }
        }
        (*gr).cx = oldx;
        (*gr).cy = oldy;
    }
}

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

    /// Build a simple, non-extended one-column ASCII grid_cell with default
    /// attributes and flags (in particular no PADDING flag), mirroring the
    /// `make_cell` helper used in the grid.rs tests.
    fn make_cell(ch: u8) -> grid_cell {
        grid_cell::new(
            utf8_data::new([ch], 0, 1, 1),
            grid_attr::empty(),
            grid_flag::empty(),
            8,
            8,
            8,
            0,
        )
    }

    /// Write an ASCII string into row `py` starting at column 0.
    unsafe fn set_line(gd: *mut grid, py: u32, s: &[u8]) {
        unsafe {
            for (i, &ch) in s.iter().enumerate() {
                let gc = make_cell(ch);
                grid_set_cell(gd, i as u32, py, &gc);
            }
        }
    }

    fn reader(gd: *mut grid, cx: u32, cy: u32) -> grid_reader {
        grid_reader { gd, cx, cy }
    }

    // grid_reader_start just stores gd/cx/cy; grid_reader_get_cursor reads them
    // back (grid-reader.c:24 and grid-reader.c:33).
    #[test]
    fn test_start_and_get_cursor() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            let mut gr = reader(null_mut(), 0, 0);
            grid_reader_start(&mut gr, gd, 5, 2);
            assert_eq!(gr.cx, 5);
            assert_eq!(gr.cy, 2);
            assert!(std::ptr::eq(gr.gd, gd));

            let mut cx = 0u32;
            let mut cy = 0u32;
            grid_reader_get_cursor(&mut gr, &mut cx, &mut cy);
            assert_eq!(cx, 5);
            assert_eq!(cy, 2);

            grid_destroy(gd);
        }
    }

    // grid_reader_line_length delegates to grid_line_length, which trims
    // trailing spaces (grid-reader.c:41).
    #[test]
    fn test_line_length() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"hello world");
            set_line(gd, 1, b"  hi"); // leading spaces counted, no trailing
            // row 2 left empty

            let mut gr = reader(gd, 0, 0);
            assert_eq!(grid_reader_line_length(&mut gr), 11);
            gr.cy = 1;
            assert_eq!(grid_reader_line_length(&mut gr), 4);
            gr.cy = 2;
            assert_eq!(grid_reader_line_length(&mut gr), 0);

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_right: px is line length when all==0 (the Rust port
    // folds the C `onemore` case in), or the grid width when all!=0. Cursor
    // advances while below px (grid-reader.c:48).
    #[test]
    fn test_cursor_right_basic() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"abc"); // length 3
            let mut gr = reader(gd, 0, 0);

            grid_reader_cursor_right(&mut gr, 0, 0);
            assert_eq!(gr.cx, 1);
            grid_reader_cursor_right(&mut gr, 0, 0);
            assert_eq!(gr.cx, 2);
            grid_reader_cursor_right(&mut gr, 0, 0);
            assert_eq!(gr.cx, 3); // reached px == line length
            // At px with no wrap: no movement.
            grid_reader_cursor_right(&mut gr, 0, 0);
            assert_eq!(gr.cx, 3);

            // With all!=0, px becomes the grid width (20), so it advances again.
            grid_reader_cursor_right(&mut gr, 0, 1);
            assert_eq!(gr.cx, 4);

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_right with wrap: at/after px and not on the last row,
    // move to start of the next line (grid-reader.c:63).
    #[test]
    fn test_cursor_right_wrap() {
        let gd = grid_create(3, 2, 0);
        unsafe {
            set_line(gd, 0, b"abc"); // length 3 == px
            let mut gr = reader(gd, 3, 0);

            grid_reader_cursor_right(&mut gr, 1, 0);
            assert_eq!(gr.cx, 0);
            assert_eq!(gr.cy, 1);

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_left: decrement, and at column 0 of a wrapped/forced
    // line move up to the end of the previous line (grid-reader.c:79).
    #[test]
    fn test_cursor_left_basic() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"abc");
            let mut gr = reader(gd, 3, 0);

            grid_reader_cursor_left(&mut gr, 0);
            assert_eq!(gr.cx, 2);
            grid_reader_cursor_left(&mut gr, 0);
            assert_eq!(gr.cx, 1);
            grid_reader_cursor_left(&mut gr, 0);
            assert_eq!(gr.cx, 0);
            // At origin (cx==0, cy==0): no movement.
            grid_reader_cursor_left(&mut gr, 0);
            assert_eq!(gr.cx, 0);
            assert_eq!(gr.cy, 0);

            grid_destroy(gd);
        }
    }

    #[test]
    fn test_cursor_left_wrap_up() {
        let gd = grid_create(3, 2, 0);
        unsafe {
            set_line(gd, 0, b"abc");
            set_line(gd, 1, b"de");
            // Mark row 0 as wrapped so left from (0,1) climbs to end of row 0.
            (*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;

            let mut gr = reader(gd, 0, 1);
            grid_reader_cursor_left(&mut gr, 0);
            assert_eq!(gr.cy, 0);
            assert_eq!(gr.cx, 3); // end of line == length of "abc"

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_down / _up move cy within [0, hsize+sy-1]
    // (grid-reader.c:100 and grid-reader.c:116).
    #[test]
    fn test_cursor_up_down() {
        let gd = grid_create(20, 3, 0); // max cy == 2
        unsafe {
            let mut gr = reader(gd, 5, 0);

            grid_reader_cursor_down(&mut gr);
            assert_eq!((gr.cx, gr.cy), (5, 1));
            grid_reader_cursor_down(&mut gr);
            assert_eq!((gr.cx, gr.cy), (5, 2));
            grid_reader_cursor_down(&mut gr); // clamped at bottom
            assert_eq!((gr.cx, gr.cy), (5, 2));

            grid_reader_cursor_up(&mut gr);
            assert_eq!(gr.cy, 1);
            grid_reader_cursor_up(&mut gr);
            assert_eq!(gr.cy, 0);
            grid_reader_cursor_up(&mut gr); // clamped at top
            assert_eq!(gr.cy, 0);

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_start_of_line sets cx=0; cursor_end_of_line sets cx to
    // line length (all==0) or grid width (all==1) (grid-reader.c:132/145).
    #[test]
    fn test_start_and_end_of_line() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"hello world"); // length 11
            let mut gr = reader(gd, 5, 0);

            grid_reader_cursor_end_of_line(&mut gr, 0, 0);
            assert_eq!(gr.cx, 11);

            grid_reader_cursor_start_of_line(&mut gr, 0);
            assert_eq!(gr.cx, 0);

            grid_reader_cursor_end_of_line(&mut gr, 0, 1);
            assert_eq!(gr.cx, 20); // grid width

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_next_word: starting on whitespace exercises the final
    // whitespace-skip loop, which advances to the start of the next word
    // (grid-reader.c:231). (Starting mid-word exercises a separate branch that
    // diverges from the C source in this port; see the test module notes.)
    #[test]
    fn test_next_word_from_whitespace() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"foo bar baz");
            let sep = crate::c!("");

            // Cursor sits on the space (index 3) between "foo" and "bar".
            let mut gr = reader(gd, 3, 0);
            grid_reader_cursor_next_word(&mut gr, sep);
            assert_eq!((gr.cx, gr.cy), (4, 0)); // start of "bar"

            // And again from the space at index 7 before "baz".
            let mut gr = reader(gd, 7, 0);
            grid_reader_cursor_next_word(&mut gr, sep);
            assert_eq!((gr.cx, gr.cy), (8, 0)); // start of "baz"

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_next_word_end: from a word char, advance to the first
    // following whitespace/separator; from whitespace, skip it then the word
    // (grid-reader.c:238).
    #[test]
    fn test_next_word_end() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"foo bar baz");
            let sep = crate::c!("");

            // From start of "foo": stop at the space after it (index 3).
            let mut gr = reader(gd, 0, 0);
            grid_reader_cursor_next_word_end(&mut gr, sep);
            assert_eq!((gr.cx, gr.cy), (3, 0));

            // From the space at index 3: skip it, cross "bar", stop at space 7.
            let mut gr = reader(gd, 3, 0);
            grid_reader_cursor_next_word_end(&mut gr, sep);
            assert_eq!((gr.cx, gr.cy), (7, 0));

            grid_destroy(gd);
        }
    }

    // grid_reader_cursor_previous_word: move to the beginning of the current or
    // previous word (grid-reader.c:283).
    #[test]
    fn test_previous_word() {
        let gd = grid_create(20, 3, 0);
        unsafe {
            set_line(gd, 0, b"foo bar baz");
            let sep = crate::c!("");

            let mut gr = reader(gd, 8, 0); // start of "baz"
            grid_reader_cursor_previous_word(&mut gr, sep, 1, false);
            assert_eq!((gr.cx, gr.cy), (4, 0)); // start of "bar"
            grid_reader_cursor_previous_word(&mut gr, sep, 1, false);
            assert_eq!((gr.cx, gr.cy), (0, 0)); // start of "foo"
            // Already at the very first word: no movement.
            grid_reader_cursor_previous_word(&mut gr, sep, 1, false);
            assert_eq!((gr.cx, gr.cy), (0, 0));

            grid_destroy(gd);
        }
    }
}