1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
// 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, onemore: i32) {
unsafe {
let mut gc = MaybeUninit::<grid_cell>::uninit();
// In vi mode (onemore == 0) the cursor may not pass the last character,
// so the limit is line_length - 1; onemore/emacs allows one past.
let px = if all != 0 {
(*(*gr).gd).sx
} else if onemore != 0 {
grid_reader_line_length(gr)
} else {
let len = grid_reader_line_length(gr);
if len != 0 { len - 1 } else { 0 }
};
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 {
// grid_in_set returns the column span of the match (a tab reports its full
// width); the word-motion loops advance one column at a time, so a tab's
// padding cells all report as "in set" and the cursor steps past the tab.
unsafe { grid_in_set((*gr).gd, (*gr).cx, (*gr).cy, set) != 0 }
}
/// 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' ')
&& !(*gc).flags.contains(grid_flag::TAB)
&& !(*gc).flags.contains(grid_flag::PADDING)
{
(*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: with onemore!=0 px is the line length, with
// all!=0 px is the grid width. (onemore==0 stops at line length - 1.)
// 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, 1);
assert_eq!(gr.cx, 1);
grid_reader_cursor_right(&mut gr, 0, 0, 1);
assert_eq!(gr.cx, 2);
grid_reader_cursor_right(&mut gr, 0, 0, 1);
assert_eq!(gr.cx, 3); // reached px == line length
// At px with no wrap: no movement.
grid_reader_cursor_right(&mut gr, 0, 0, 1);
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, 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, 1);
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);
}
}
// grid_reader_cursor_jump scans forward from cx on the current line (and
// across wrapped continuations) for the first cell matching jc
// (grid-reader.c:357). Found -> cursor moves onto it and returns 1.
#[test]
fn test_cursor_jump_forward_found() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"foo.bar");
let jc = utf8_data::new([b'.'], 0, 1, 1);
let mut gr = reader(gd, 0, 0);
assert_eq!(grid_reader_cursor_jump(&mut gr, &jc), 1);
assert_eq!((gr.cx, gr.cy), (3, 0)); // landed on '.'
grid_destroy(gd);
}
}
// No match on the (unwrapped) line -> returns 0 and leaves the cursor put
// (grid-reader.c:377).
#[test]
fn test_cursor_jump_forward_not_found() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"foobar");
let jc = utf8_data::new([b'z'], 0, 1, 1);
let mut gr = reader(gd, 2, 0);
assert_eq!(grid_reader_cursor_jump(&mut gr, &jc), 0);
assert_eq!((gr.cx, gr.cy), (2, 0));
grid_destroy(gd);
}
}
// The starting cell itself is included in the scan: jumping to the char the
// cursor already sits on returns 1 without moving (px starts at gr->cx).
#[test]
fn test_cursor_jump_forward_on_current_cell() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"a.b");
let jc = utf8_data::new([b'.'], 0, 1, 1);
let mut gr = reader(gd, 1, 0); // already on '.'
assert_eq!(grid_reader_cursor_jump(&mut gr, &jc), 1);
assert_eq!((gr.cx, gr.cy), (1, 0));
grid_destroy(gd);
}
}
// A wrapped line continues the scan onto the next row (grid-reader.c:377:
// only stops when py==yy or the line is not WRAPPED).
#[test]
fn test_cursor_jump_forward_across_wrap() {
let gd = grid_create(3, 3, 0);
unsafe {
set_line(gd, 0, b"abc");
set_line(gd, 1, b"d.e");
(*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;
let jc = utf8_data::new([b'.'], 0, 1, 1);
let mut gr = reader(gd, 0, 0);
assert_eq!(grid_reader_cursor_jump(&mut gr, &jc), 1);
assert_eq!((gr.cx, gr.cy), (1, 1)); // '.' on the wrapped row
grid_destroy(gd);
}
}
// grid_reader_cursor_jump_back scans left from the cursor and lands on the
// first cell whose data equals jc, using the same non-padding match test as
// grid_reader_cell_equals_data (grid-reader.c:342): `!PADDING && size==size &&
// memcmp==0`. The earlier port folded the PADDING check inside a negated AND
// (`!(PADDING && ...)`), which was true for every ordinary cell and so
// "matched" the current cell immediately; fixed to mirror the forward
// grid_reader_cursor_jump. Starting on 'd' (cx=4) of "a.bcd", a back-scan for
// '.' skips d/c/b and stops on '.' at cx=1.
#[test]
fn test_cursor_jump_back_finds_char_to_left() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"a.bcd");
let mut jc = utf8_data::new([b'.'], 0, 1, 1);
let mut gr = reader(gd, 4, 0);
assert_eq!(grid_reader_cursor_jump_back(&mut gr, &mut jc), 1);
assert_eq!((gr.cx, gr.cy), (1, 0));
// No matching char to the left of the cursor: returns 0, no move.
let mut miss = utf8_data::new([b'z'], 0, 1, 1);
let mut gr2 = reader(gd, 4, 0);
assert_eq!(grid_reader_cursor_jump_back(&mut gr2, &mut miss), 0);
assert_eq!((gr2.cx, gr2.cy), (4, 0));
grid_destroy(gd);
}
}
// grid_reader_cursor_back_to_indentation moves to the first non-blank cell of
// the (logical, un-wrapped) line (grid-reader.c:414).
#[test]
fn test_back_to_indentation() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b" foo");
let mut gr = reader(gd, 5, 0);
grid_reader_cursor_back_to_indentation(&mut gr);
assert_eq!((gr.cx, gr.cy), (3, 0)); // first non-space
grid_destroy(gd);
}
}
// An all-blank line has no indentation target, so the cursor is restored to
// where it started (grid-reader.c:439 oldx/oldy).
#[test]
fn test_back_to_indentation_all_blank_restores() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b" "); // spaces trim to length 0
let mut gr = reader(gd, 4, 0);
grid_reader_cursor_back_to_indentation(&mut gr);
assert_eq!((gr.cx, gr.cy), (4, 0));
grid_destroy(gd);
}
}
// back_to_indentation follows a WRAPPED line back to its start before
// scanning (grid_reader_cursor_start_of_line(gr, 1)), so indentation is found
// on the first physical row of the logical line.
#[test]
fn test_back_to_indentation_wrapped() {
let gd = grid_create(3, 3, 0);
unsafe {
set_line(gd, 0, b" ab");
set_line(gd, 1, b"cde");
(*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;
// Start on the wrapped continuation row.
let mut gr = reader(gd, 2, 1);
grid_reader_cursor_back_to_indentation(&mut gr);
assert_eq!((gr.cx, gr.cy), (1, 0)); // 'a' at col 1 of physical row 0
grid_destroy(gd);
}
}
// With a separator set, grid_reader_cursor_next_word treats separator runs as
// their own word: from a word char it advances to the following separator run
// and then skips trailing whitespace (grid-reader.c:193).
#[test]
fn test_next_word_with_separators() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"foo.bar baz");
let sep = crate::c!(".");
// Starting on the separator '.' (index 3): it is its own word, so the
// cursor advances past it to the next word "bar" at index 4.
let mut gr = reader(gd, 3, 0);
grid_reader_cursor_next_word(&mut gr, sep);
assert_eq!((gr.cx, gr.cy), (4, 0));
grid_destroy(gd);
}
}
// grid_reader_cursor_next_word_end with a separator set stops at the end of a
// separator run when starting inside one (grid-reader.c:238).
#[test]
fn test_next_word_end_with_separators() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"a...b");
let sep = crate::c!(".");
// From the first '.' (index 1) advance across the "..." run; the loop
// stops at the first non-separator ('b', index 4).
let mut gr = reader(gd, 1, 0);
grid_reader_cursor_next_word_end(&mut gr, sep);
assert_eq!((gr.cx, gr.cy), (4, 0));
grid_destroy(gd);
}
}
// grid_reader_cursor_start_of_line(wrap=1) climbs to the first row of a
// wrapped logical line; end_of_line(wrap=1) descends to the last
// (grid-reader.c:132/145).
#[test]
fn test_start_end_of_line_wrap() {
let gd = grid_create(3, 4, 0);
unsafe {
set_line(gd, 0, b"abc");
set_line(gd, 1, b"def");
set_line(gd, 2, b"gh");
(*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;
(*grid_get_line(gd, 1)).flags |= grid_line_flag::WRAPPED;
// From the middle physical row, wrap-start climbs to row 0.
let mut gr = reader(gd, 1, 1);
grid_reader_cursor_start_of_line(&mut gr, 1);
assert_eq!((gr.cx, gr.cy), (0, 0));
// Wrap-end descends to the last physical row (row 2, length 2).
grid_reader_cursor_end_of_line(&mut gr, 1, 0);
assert_eq!((gr.cx, gr.cy), (2, 2));
grid_destroy(gd);
}
}
// grid_reader_cursor_right skips PADDING cells: a wide glyph occupies a real
// cell plus a trailing padding cell, so moving right lands past the padding
// (grid-reader.c:54).
#[test]
fn test_cursor_right_skips_padding() {
let gd = grid_create(20, 3, 0);
unsafe {
// Column 0 'a', column 1 real, column 2 padding, column 3 'b'.
set_line(gd, 0, b"a");
let mut wide = make_cell(b'W');
wide.data.width = 2;
grid_set_cell(gd, 1, 0, &wide);
grid_set_padding(gd, 2, 0);
let b = make_cell(b'b');
grid_set_cell(gd, 3, 0, &b);
let mut gr = reader(gd, 1, 0);
// From the wide cell, right should skip the padding at 2 and reach 3.
grid_reader_cursor_right(&mut gr, 0, 1, 1);
assert_eq!(gr.cx, 3);
grid_destroy(gd);
}
}
// grid_reader_cursor_previous_word with stop_at_eol=true stops at a line
// boundary rather than crossing into the previous line's trailing word
// (grid-reader.c:305).
#[test]
fn test_previous_word_stop_at_eol() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"foo");
set_line(gd, 1, b"bar");
// Rows are NOT wrapped: independent logical lines.
let sep = crate::c!("");
// From the start of "bar" on row 1, previous-word with stop_at_eol
// climbs to row 0 and stops at its end rather than entering "foo".
let mut gr = reader(gd, 0, 1);
grid_reader_cursor_previous_word(&mut gr, sep, 1, true);
assert_eq!(gr.cy, 0);
grid_destroy(gd);
}
}
// grid_reader_handle_wrap returns 0 when the cursor is past the line end
// (cx > *xx) but already on the last physical row (cy == *yy), signalling the
// caller there is nothing further to scan (grid-reader.c:171). The cursor is
// left untouched.
#[test]
fn test_handle_wrap_at_end_returns_zero() {
let gd = grid_create(3, 1, 0);
unsafe {
let mut gr = reader(gd, 2, 0);
let mut xx = 0u32; // cursor (cx=2) is already past xx
let mut yy = 0u32; // and on the last row
assert_eq!(grid_reader_handle_wrap(&mut gr, &mut xx, &mut yy), 0);
assert_eq!((gr.cx, gr.cy), (2, 0));
grid_destroy(gd);
}
}
// grid_reader_handle_wrap advances onto the next physical row when the cursor
// is past the line end and the current row is not the last (grid-reader.c:174,
// start_of_line + cursor_down). It stops once cx is within the new line's
// extent and returns 1.
#[test]
fn test_handle_wrap_advances_on_wrapped_line() {
let gd = grid_create(3, 2, 0);
unsafe {
set_line(gd, 0, b"abc");
set_line(gd, 1, b"de");
(*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;
let mut gr = reader(gd, 3, 0); // past end of row 0
let mut xx = 0u32;
let mut yy = 1u32; // last row index
assert_eq!(grid_reader_handle_wrap(&mut gr, &mut xx, &mut yy), 1);
assert_eq!((gr.cx, gr.cy), (0, 1)); // dropped to start of row 1
grid_destroy(gd);
}
}
// grid_reader_in_set matches the cell glyph against a set and always reports
// false for a PADDING cell (grid-reader.c:196-201), regardless of the set.
#[test]
fn test_in_set_matches_and_padding() {
let gd = grid_create(20, 2, 0);
unsafe {
set_line(gd, 0, b"a.");
grid_set_padding(gd, 5, 0);
let mut gr = reader(gd, 0, 0); // on 'a'
assert!(grid_reader_in_set(&mut gr, crate::c!("abc")));
assert!(!grid_reader_in_set(&mut gr, crate::c!(".")));
gr.cx = 1; // on '.'
assert!(grid_reader_in_set(&mut gr, crate::c!(".")));
gr.cx = 5; // on the padding cell
assert!(!grid_reader_in_set(&mut gr, crate::c!(".")));
assert!(!grid_reader_in_set(&mut gr, crate::c!(" ")));
grid_destroy(gd);
}
}
// grid_reader_cursor_jump_back continues the leftward scan up onto a wrapped
// predecessor row (grid-reader.c:444-451): from 'f' on the wrapped
// continuation "def", a back-scan for '.' walks up into "a.c" and stops on the
// '.' at (1,0).
#[test]
fn test_cursor_jump_back_across_wrap() {
let gd = grid_create(3, 3, 0);
unsafe {
set_line(gd, 0, b"a.c");
set_line(gd, 1, b"def");
(*grid_get_line(gd, 0)).flags |= grid_line_flag::WRAPPED;
let mut jc = utf8_data::new([b'.'], 0, 1, 1);
let mut gr = reader(gd, 2, 1); // on 'f'
assert_eq!(grid_reader_cursor_jump_back(&mut gr, &mut jc), 1);
assert_eq!((gr.cx, gr.cy), (1, 0));
grid_destroy(gd);
}
}
// grid_reader_cursor_down lands on the next row and then backs the cursor off
// any PADDING cell it falls on, so the cursor rests on the real wide-character
// cell to its left (grid-reader.c:101-107).
#[test]
fn test_cursor_down_skips_padding() {
let gd = grid_create(20, 3, 0);
unsafe {
// Row 1: wide 'W' at col 1 (real), padding at col 2.
let mut wide = make_cell(b'W');
wide.data.width = 2;
grid_set_cell(gd, 1, 1, &wide);
grid_set_padding(gd, 2, 1);
let mut gr = reader(gd, 2, 0); // column 2, row 0
grid_reader_cursor_down(&mut gr);
assert_eq!((gr.cx, gr.cy), (1, 1)); // backed off padding onto 'W'
grid_destroy(gd);
}
}
// grid_reader_cursor_up mirrors cursor_down: it moves up a row and backs the
// cursor off a PADDING cell onto the real cell (grid-reader.c:120-126).
#[test]
fn test_cursor_up_skips_padding() {
let gd = grid_create(20, 3, 0);
unsafe {
let mut wide = make_cell(b'W');
wide.data.width = 2;
grid_set_cell(gd, 1, 1, &wide);
grid_set_padding(gd, 2, 1);
let mut gr = reader(gd, 2, 2); // column 2, row 2
grid_reader_cursor_up(&mut gr);
assert_eq!((gr.cx, gr.cy), (1, 1));
grid_destroy(gd);
}
}
// grid_reader_cursor_right with all!=0 uses the grid width as the limit and,
// with wrap==0, clamps at that width rather than advancing off the row
// (grid-reader.c:48-61).
#[test]
fn test_cursor_right_all_clamps_at_grid_width() {
let gd = grid_create(5, 1, 0);
unsafe {
set_line(gd, 0, b"abc");
let mut gr = reader(gd, 3, 0);
grid_reader_cursor_right(&mut gr, 0, 1, 1);
assert_eq!(gr.cx, 4);
grid_reader_cursor_right(&mut gr, 0, 1, 1);
assert_eq!(gr.cx, 5); // reached grid width
grid_reader_cursor_right(&mut gr, 0, 1, 1);
assert_eq!(gr.cx, 5); // clamped: no wrap, no further motion
grid_destroy(gd);
}
}
// grid_reader_cursor_previous_word with a separator set treats the separator
// as a word boundary: starting inside "bar" of "foo.bar", the back-scan stops
// at the start of "bar" (index 4) rather than merging across the '.' into
// "foo" (grid-reader.c:337-366).
#[test]
fn test_previous_word_with_separator_midword() {
let gd = grid_create(20, 3, 0);
unsafe {
set_line(gd, 0, b"foo.bar");
let sep = crate::c!(".");
let mut gr = reader(gd, 6, 0); // on 'r', the last char of "bar"
grid_reader_cursor_previous_word(&mut gr, sep, 1, false);
assert_eq!((gr.cx, gr.cy), (4, 0)); // start of "bar"
grid_destroy(gd);
}
}
}