/
move.c
3633 lines (3380 loc) · 87 KB
/
move.c
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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* move.c: Functions for moving the cursor and scrolling text.
*
* There are two ways to move the cursor:
* 1. Move the cursor directly, the text is scrolled to keep the cursor in the
* window.
* 2. Scroll the text, the cursor is moved into the text visible in the
* window.
* The 'scrolloff' option makes this a bit complicated.
*/
#include "vim.h"
static int scrolljump_value(void);
static int check_top_offset(void);
static void curs_rows(win_T *wp);
typedef struct
{
linenr_T lnum; // line number
#ifdef FEAT_DIFF
int fill; // filler lines
#endif
int height; // height of added line
} lineoff_T;
static void topline_back(lineoff_T *lp);
static void botline_forw(lineoff_T *lp);
/*
* Reduce "n" for the screen lines skipped with "wp->w_skipcol".
*/
static int
adjust_plines_for_skipcol(win_T *wp, int n)
{
if (wp->w_skipcol == 0)
return n;
int off = 0;
int width = wp->w_width - win_col_off(wp);
if (wp->w_skipcol >= width)
{
++off;
int skip = wp->w_skipcol - width;
width += win_col_off2(wp);
while (skip >= width)
{
++off;
skip -= width;
}
}
wp->w_valid &= ~VALID_WROW;
return n - off;
}
/*
* Return how many lines "lnum" will take on the screen, taking into account
* whether it is the first line, whether w_skipcol is non-zero and limiting to
* the window height.
*/
static int
plines_correct_topline(win_T *wp, linenr_T lnum)
{
int n;
#ifdef FEAT_DIFF
if (lnum == wp->w_topline)
n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
else
#endif
n = plines_win(wp, lnum, FALSE);
if (lnum == wp->w_topline)
n = adjust_plines_for_skipcol(wp, n);
if (n > wp->w_height)
n = wp->w_height;
return n;
}
/*
* Compute wp->w_botline for the current wp->w_topline. Can be called after
* wp->w_topline changed.
*/
static void
comp_botline(win_T *wp)
{
int n;
linenr_T lnum;
int done;
#ifdef FEAT_FOLDING
linenr_T last;
int folded;
#endif
/*
* If w_cline_row is valid, start there.
* Otherwise have to start at w_topline.
*/
check_cursor_moved(wp);
if (wp->w_valid & VALID_CROW)
{
lnum = wp->w_cursor.lnum;
done = wp->w_cline_row;
}
else
{
lnum = wp->w_topline;
done = 0;
}
for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
{
#ifdef FEAT_FOLDING
last = lnum;
folded = FALSE;
if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
{
n = 1;
folded = TRUE;
}
else
#endif
{
n = plines_correct_topline(wp, lnum);
}
if (
#ifdef FEAT_FOLDING
lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
#else
lnum == wp->w_cursor.lnum
#endif
)
{
wp->w_cline_row = done;
wp->w_cline_height = n;
#ifdef FEAT_FOLDING
wp->w_cline_folded = folded;
#endif
redraw_for_cursorline(wp);
wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
}
if (done + n > wp->w_height)
break;
done += n;
#ifdef FEAT_FOLDING
lnum = last;
#endif
}
// wp->w_botline is the line that is just below the window
wp->w_botline = lnum;
wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
set_empty_rows(wp, done);
}
/*
* Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
* set.
*/
void
redraw_for_cursorline(win_T *wp)
{
if ((wp->w_p_rnu
#ifdef FEAT_SYN_HL
|| wp->w_p_cul
#endif
)
&& (wp->w_valid & VALID_CROW) == 0
&& !pum_visible())
{
// win_line() will redraw the number column and cursorline only.
redraw_win_later(wp, UPD_VALID);
}
}
#ifdef FEAT_SYN_HL
/*
* Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
* contains "screenline".
*/
static void
redraw_for_cursorcolumn(win_T *wp)
{
if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
{
// When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
if (wp->w_p_cuc)
redraw_win_later(wp, UPD_SOME_VALID);
// When 'cursorlineopt' contains "screenline" need to redraw with
// UPD_VALID.
else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
redraw_win_later(wp, UPD_VALID);
}
}
#endif
/*
* Calculates how much overlap the smoothscroll marker "<<<" overlaps with
* buffer text for curwin.
* Parameter "extra2" should be the padding on the 2nd line, not the first
* line.
* Returns the number of columns of overlap with buffer text, excluding the
* extra padding on the ledge.
*/
static int
smoothscroll_marker_overlap(int extra2)
{
#if defined(FEAT_LINEBREAK)
// We don't draw the <<< marker when in showbreak mode, thus no need to
// account for it. See wlv_screen_line().
if (*get_showbreak_value(curwin) != NUL)
return 0;
#endif
return extra2 > 3 ? 0 : 3 - extra2;
}
/*
* Calculates the skipcol offset for window "wp" given how many
* physical lines we want to scroll down.
*/
static int
skipcol_from_plines(win_T *wp, int plines_off)
{
int width1 = wp->w_width - win_col_off(wp);
int skipcol = 0;
if (plines_off > 0)
skipcol += width1;
if (plines_off > 1)
skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
return skipcol;
}
/*
* Set curwin->s_skipcol to zero and redraw later if needed.
*/
static void
reset_skipcol(void)
{
if (curwin->w_skipcol == 0)
return;
curwin->w_skipcol = 0;
// Should use the least expensive way that displays all that changed.
// UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
// enough when the top line gets another screen line.
redraw_later(UPD_SOME_VALID);
}
/*
* Update curwin->w_topline and redraw if necessary.
* Used to update the screen before printing a message.
*/
void
update_topline_redraw(void)
{
update_topline();
if (must_redraw)
update_screen(0);
}
/*
* Update curwin->w_topline to move the cursor onto the screen.
*/
void
update_topline(void)
{
long line_count;
int halfheight;
int n;
linenr_T old_topline;
#ifdef FEAT_DIFF
int old_topfill;
#endif
#ifdef FEAT_FOLDING
linenr_T lnum;
#endif
int check_topline = FALSE;
int check_botline = FALSE;
long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
int save_so = *so_ptr;
// Cursor is updated instead when this is TRUE for 'splitkeep'.
if (skip_update_topline)
return;
// If there is no valid screen and when the window height is zero just use
// the cursor line.
if (!screen_valid(TRUE) || curwin->w_height == 0)
{
check_cursor_lnum();
curwin->w_topline = curwin->w_cursor.lnum;
curwin->w_botline = curwin->w_topline;
curwin->w_scbind_pos = 1;
return;
}
check_cursor_moved(curwin);
if (curwin->w_valid & VALID_TOPLINE)
return;
// When dragging with the mouse, don't scroll that quickly
if (mouse_dragging > 0)
*so_ptr = mouse_dragging - 1;
old_topline = curwin->w_topline;
#ifdef FEAT_DIFF
old_topfill = curwin->w_topfill;
#endif
/*
* If the buffer is empty, always set topline to 1.
*/
if (BUFEMPTY()) // special case - file is empty
{
if (curwin->w_topline != 1)
redraw_later(UPD_NOT_VALID);
curwin->w_topline = 1;
curwin->w_botline = 2;
curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
curwin->w_scbind_pos = 1;
}
/*
* If the cursor is above or near the top of the window, scroll the window
* to show the line the cursor is in, with 'scrolloff' context.
*/
else
{
if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
{
// If the cursor is above topline, scrolling is always needed.
// If the cursor is far below topline and there is no folding,
// scrolling down is never needed.
if (curwin->w_cursor.lnum < curwin->w_topline)
check_topline = TRUE;
else if (check_top_offset())
check_topline = TRUE;
else if (curwin->w_cursor.lnum == curwin->w_topline)
{
colnr_T vcol;
// Check that the cursor position is visible. Add columns for
// the smoothscroll marker "<<<" displayed in the top-left if
// needed.
getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
int smoothscroll_overlap = smoothscroll_marker_overlap(
curwin_col_off() - curwin_col_off2());
if (curwin->w_skipcol + smoothscroll_overlap > vcol)
check_topline = TRUE;
}
}
#ifdef FEAT_DIFF
// Check if there are more filler lines than allowed.
if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
curwin->w_topline))
check_topline = TRUE;
#endif
if (check_topline)
{
halfheight = curwin->w_height / 2 - 1;
if (halfheight < 2)
halfheight = 2;
#ifdef FEAT_FOLDING
if (hasAnyFolding(curwin))
{
// Count the number of logical lines between the cursor and
// topline + scrolloff (approximation of how much will be
// scrolled).
n = 0;
for (lnum = curwin->w_cursor.lnum;
lnum < curwin->w_topline + *so_ptr; ++lnum)
{
++n;
// stop at end of file or when we know we are far off
if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
break;
(void)hasFolding(lnum, NULL, &lnum);
}
}
else
#endif
n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
// If we weren't very close to begin with, we scroll to put the
// cursor in the middle of the window. Otherwise put the cursor
// near the top of the window.
if (n >= halfheight)
scroll_cursor_halfway(FALSE, FALSE);
else
{
scroll_cursor_top(scrolljump_value(), FALSE);
check_botline = TRUE;
}
}
else
{
#ifdef FEAT_FOLDING
// Make sure topline is the first line of a fold.
(void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
#endif
check_botline = TRUE;
}
}
/*
* If the cursor is below the bottom of the window, scroll the window
* to put the cursor on the window.
* When w_botline is invalid, recompute it first, to avoid a redraw later.
* If w_botline was approximated, we might need a redraw later in a few
* cases, but we don't want to spend (a lot of) time recomputing w_botline
* for every small change.
*/
if (check_botline)
{
if (!(curwin->w_valid & VALID_BOTLINE_AP))
validate_botline();
if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
{
if (curwin->w_cursor.lnum < curwin->w_botline)
{
if (((long)curwin->w_cursor.lnum
>= (long)curwin->w_botline - *so_ptr
#ifdef FEAT_FOLDING
|| hasAnyFolding(curwin)
#endif
))
{
lineoff_T loff;
// Cursor is (a few lines) above botline, check if there are
// 'scrolloff' window lines below the cursor. If not, need to
// scroll.
n = curwin->w_empty_rows;
loff.lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
// In a fold go to its last line.
(void)hasFolding(loff.lnum, NULL, &loff.lnum);
#endif
#ifdef FEAT_DIFF
loff.fill = 0;
n += curwin->w_filler_rows;
#endif
loff.height = 0;
while (loff.lnum < curwin->w_botline
#ifdef FEAT_DIFF
&& (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
#endif
)
{
n += loff.height;
if (n >= *so_ptr)
break;
botline_forw(&loff);
}
if (n >= *so_ptr)
// sufficient context, no need to scroll
check_botline = FALSE;
}
else
// sufficient context, no need to scroll
check_botline = FALSE;
}
if (check_botline)
{
#ifdef FEAT_FOLDING
if (hasAnyFolding(curwin))
{
// Count the number of logical lines between the cursor and
// botline - scrolloff (approximation of how much will be
// scrolled).
line_count = 0;
for (lnum = curwin->w_cursor.lnum;
lnum >= curwin->w_botline - *so_ptr; --lnum)
{
++line_count;
// stop at end of file or when we know we are far off
if (lnum <= 0 || line_count > curwin->w_height + 1)
break;
(void)hasFolding(lnum, &lnum, NULL);
}
}
else
#endif
line_count = curwin->w_cursor.lnum - curwin->w_botline
+ 1 + *so_ptr;
if (line_count <= curwin->w_height + 1)
scroll_cursor_bot(scrolljump_value(), FALSE);
else
scroll_cursor_halfway(FALSE, FALSE);
}
}
}
curwin->w_valid |= VALID_TOPLINE;
/*
* Need to redraw when topline changed.
*/
if (curwin->w_topline != old_topline
#ifdef FEAT_DIFF
|| curwin->w_topfill != old_topfill
#endif
)
{
dollar_vcol = -1;
redraw_later(UPD_VALID);
reset_skipcol();
// May need to set w_skipcol when cursor in w_topline.
if (curwin->w_cursor.lnum == curwin->w_topline)
validate_cursor();
}
*so_ptr = save_so;
}
/*
* Return the scrolljump value to use for the current window.
* When 'scrolljump' is positive use it as-is.
* When 'scrolljump' is negative use it as a percentage of the window height.
*/
static int
scrolljump_value(void)
{
if (p_sj >= 0)
return (int)p_sj;
return (curwin->w_height * -p_sj) / 100;
}
/*
* Return TRUE when there are not 'scrolloff' lines above the cursor for the
* current window.
*/
static int
check_top_offset(void)
{
lineoff_T loff;
int n;
long so = get_scrolloff_value();
if (curwin->w_cursor.lnum < curwin->w_topline + so
#ifdef FEAT_FOLDING
|| hasAnyFolding(curwin)
#endif
)
{
loff.lnum = curwin->w_cursor.lnum;
#ifdef FEAT_DIFF
loff.fill = 0;
n = curwin->w_topfill; // always have this context
#else
n = 0;
#endif
// Count the visible screen lines above the cursor line.
while (n < so)
{
topline_back(&loff);
// Stop when included a line above the window.
if (loff.lnum < curwin->w_topline
#ifdef FEAT_DIFF
|| (loff.lnum == curwin->w_topline && loff.fill > 0)
#endif
)
break;
n += loff.height;
}
if (n < so)
return TRUE;
}
return FALSE;
}
/*
* Update w_curswant.
*/
void
update_curswant_force(void)
{
validate_virtcol();
curwin->w_curswant = curwin->w_virtcol
#ifdef FEAT_PROP_POPUP
- curwin->w_virtcol_first_char
#endif
;
curwin->w_set_curswant = FALSE;
}
/*
* Update w_curswant if w_set_curswant is set.
*/
void
update_curswant(void)
{
if (curwin->w_set_curswant)
update_curswant_force();
}
/*
* Check if the cursor has moved. Set the w_valid flag accordingly.
*/
void
check_cursor_moved(win_T *wp)
{
if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
{
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
|VALID_BOTLINE|VALID_BOTLINE_AP);
wp->w_valid_cursor = wp->w_cursor;
wp->w_valid_leftcol = wp->w_leftcol;
wp->w_valid_skipcol = wp->w_skipcol;
}
else if (wp->w_skipcol != wp->w_valid_skipcol)
{
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|VALID_CHEIGHT|VALID_CROW
|VALID_BOTLINE|VALID_BOTLINE_AP);
wp->w_valid_cursor = wp->w_cursor;
wp->w_valid_leftcol = wp->w_leftcol;
wp->w_valid_skipcol = wp->w_skipcol;
}
else if (wp->w_cursor.col != wp->w_valid_cursor.col
|| wp->w_leftcol != wp->w_valid_leftcol
|| wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
{
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
wp->w_valid_cursor.col = wp->w_cursor.col;
wp->w_valid_leftcol = wp->w_leftcol;
wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
}
}
/*
* Call this function when some window settings have changed, which require
* the cursor position, botline and topline to be recomputed and the window to
* be redrawn. E.g, when changing the 'wrap' option or folding.
*/
void
changed_window_setting(void)
{
changed_window_setting_win(curwin);
}
void
changed_window_setting_win(win_T *wp)
{
wp->w_lines_valid = 0;
changed_line_abv_curs_win(wp);
wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
redraw_win_later(wp, UPD_NOT_VALID);
}
/*
* Call changed_window_setting_win() for every window containing "buf".
*/
void
changed_window_setting_buf(buf_T *buf)
{
tabpage_T *tp;
win_T *wp;
FOR_ALL_TAB_WINDOWS(tp, wp)
if (wp->w_buffer == buf)
changed_window_setting_win(wp);
}
/*
* Set wp->w_topline to a certain number.
*/
void
set_topline(win_T *wp, linenr_T lnum)
{
#ifdef FEAT_DIFF
linenr_T prev_topline = wp->w_topline;
#endif
#ifdef FEAT_FOLDING
// go to first of folded lines
(void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
#endif
// Approximate the value of w_botline
wp->w_botline += lnum - wp->w_topline;
if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
wp->w_topline = lnum;
wp->w_topline_was_set = TRUE;
#ifdef FEAT_DIFF
if (lnum != prev_topline)
// Keep the filler lines when the topline didn't change.
wp->w_topfill = 0;
#endif
wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
// Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
redraw_later(UPD_VALID);
}
/*
* Call this function when the length of the cursor line (in screen
* characters) has changed, and the change is before the cursor.
* Need to take care of w_botline separately!
*/
void
changed_cline_bef_curs(void)
{
curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|VALID_CHEIGHT|VALID_TOPLINE);
}
void
changed_cline_bef_curs_win(win_T *wp)
{
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|VALID_CHEIGHT|VALID_TOPLINE);
}
/*
* Call this function when the length of a line (in screen characters) above
* the cursor have changed.
* Need to take care of w_botline separately!
*/
void
changed_line_abv_curs(void)
{
curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
|VALID_CHEIGHT|VALID_TOPLINE);
}
void
changed_line_abv_curs_win(win_T *wp)
{
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
|VALID_CHEIGHT|VALID_TOPLINE);
}
/*
* Display of line has changed for "buf", invalidate cursor position and
* w_botline.
*/
void
changed_line_display_buf(buf_T *buf)
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == buf)
wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|VALID_CROW|VALID_CHEIGHT
|VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
}
/*
* Make sure the value of curwin->w_botline is valid.
*/
void
validate_botline(void)
{
validate_botline_win(curwin);
}
/*
* Make sure the value of wp->w_botline is valid.
*/
void
validate_botline_win(win_T *wp)
{
if (!(wp->w_valid & VALID_BOTLINE))
comp_botline(wp);
}
/*
* Mark curwin->w_botline as invalid (because of some change in the buffer).
*/
void
invalidate_botline(void)
{
curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
}
void
invalidate_botline_win(win_T *wp)
{
wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
}
void
approximate_botline_win(
win_T *wp)
{
wp->w_valid &= ~VALID_BOTLINE;
}
/*
* Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
*/
int
cursor_valid(void)
{
check_cursor_moved(curwin);
return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
(VALID_WROW|VALID_WCOL));
}
/*
* Validate cursor position. Makes sure w_wrow and w_wcol are valid.
* w_topline must be valid, you may need to call update_topline() first!
*/
void
validate_cursor(void)
{
check_cursor_lnum();
check_cursor_moved(curwin);
if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
curs_columns(TRUE);
}
#if defined(FEAT_GUI) || defined(PROTO)
/*
* validate w_cline_row.
*/
void
validate_cline_row(void)
{
/*
* First make sure that w_topline is valid (after moving the cursor).
*/
update_topline();
check_cursor_moved(curwin);
if (!(curwin->w_valid & VALID_CROW))
curs_rows(curwin);
}
#endif
/*
* Compute wp->w_cline_row and wp->w_cline_height, based on the current value
* of wp->w_topline.
*/
static void
curs_rows(win_T *wp)
{
linenr_T lnum;
int i;
int all_invalid;
int valid;
#ifdef FEAT_FOLDING
long fold_count;
#endif
// Check if wp->w_lines[].wl_size is invalid
all_invalid = (!redrawing()
|| wp->w_lines_valid == 0
|| wp->w_lines[0].wl_lnum > wp->w_topline);
i = 0;
wp->w_cline_row = 0;
for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
{
valid = FALSE;
if (!all_invalid && i < wp->w_lines_valid)
{
if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
continue; // skip changed or deleted lines
if (wp->w_lines[i].wl_lnum == lnum)
{
#ifdef FEAT_FOLDING
// Check for newly inserted lines below this row, in which
// case we need to check for folded lines.
if (!wp->w_buffer->b_mod_set
|| wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
|| wp->w_buffer->b_mod_top
> wp->w_lines[i].wl_lastlnum + 1)
#endif
valid = TRUE;
}
else if (wp->w_lines[i].wl_lnum > lnum)
--i; // hold at inserted lines
}
if (valid
#ifdef FEAT_DIFF
&& (lnum != wp->w_topline || !wp->w_p_diff)
#endif
)
{
#ifdef FEAT_FOLDING
lnum = wp->w_lines[i].wl_lastlnum + 1;
// Cursor inside folded lines, don't count this row
if (lnum > wp->w_cursor.lnum)
break;
#else
++lnum;
#endif
wp->w_cline_row += wp->w_lines[i].wl_size;
}
else
{
#ifdef FEAT_FOLDING
fold_count = foldedCount(wp, lnum, NULL);
if (fold_count)
{
lnum += fold_count;
if (lnum > wp->w_cursor.lnum)
break;
++wp->w_cline_row;
}
else
#endif
{
wp->w_cline_row += plines_correct_topline(wp, lnum);
++lnum;
}
}
}
check_cursor_moved(wp);
if (!(wp->w_valid & VALID_CHEIGHT))
{
if (all_invalid
|| i == wp->w_lines_valid
|| (i < wp->w_lines_valid
&& (!wp->w_lines[i].wl_valid
|| wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
{
#ifdef FEAT_DIFF
if (wp->w_cursor.lnum == wp->w_topline)
wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
TRUE) + wp->w_topfill;
else
#endif
wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
#ifdef FEAT_FOLDING
wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
NULL, NULL, TRUE, NULL);
#endif
}
else if (i > wp->w_lines_valid)
{
// a line that is too long to fit on the last screen line
wp->w_cline_height = 0;
#ifdef FEAT_FOLDING
wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
NULL, NULL, TRUE, NULL);
#endif
}
else
{
wp->w_cline_height = wp->w_lines[i].wl_size;
#ifdef FEAT_FOLDING
wp->w_cline_folded = wp->w_lines[i].wl_folded;
#endif
}
}
redraw_for_cursorline(curwin);
wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
}
/*
* Validate curwin->w_virtcol only.
*/
void
validate_virtcol(void)
{
validate_virtcol_win(curwin);
}
/*
* Validate wp->w_virtcol only.
*/
void
validate_virtcol_win(win_T *wp)
{
check_cursor_moved(wp);
if (wp->w_valid & VALID_VIRTCOL)
return;
#ifdef FEAT_PROP_POPUP
wp->w_virtcol_first_char = 0;
#endif
getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
#ifdef FEAT_SYN_HL
redraw_for_cursorcolumn(wp);
#endif
wp->w_valid |= VALID_VIRTCOL;
}
/*
* Validate curwin->w_cline_height only.
*/
void