1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
10 * move.c: Functions for moving the cursor and scrolling text.
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
15 * 2. Scroll the text, the cursor is moved into the text visible in the
17 * The 'scrolloff' option makes this a bit complicated.
22 static void comp_botline
__ARGS((win_T
*wp
));
23 static int scrolljump_value
__ARGS((void));
24 static int check_top_offset
__ARGS((void));
25 static void curs_rows
__ARGS((win_T
*wp
, int do_botline
));
26 static void validate_botline_win
__ARGS((win_T
*wp
));
27 static void validate_cheight
__ARGS((void));
31 linenr_T lnum
; /* line number */
33 int fill
; /* filler lines */
35 int height
; /* height of added line */
38 static void topline_back
__ARGS((lineoff_T
*lp
));
39 static void botline_forw
__ARGS((lineoff_T
*lp
));
41 static void botline_topline
__ARGS((lineoff_T
*lp
));
42 static void topline_botline
__ARGS((lineoff_T
*lp
));
43 static void max_topfill
__ARGS((void));
47 * Compute wp->w_botline for the current wp->w_topline. Can be called after
48 * wp->w_topline changed.
63 * If w_cline_row is valid, start there.
64 * Otherwise have to start at w_topline.
66 check_cursor_moved(wp
);
67 if (wp
->w_valid
& VALID_CROW
)
69 lnum
= wp
->w_cursor
.lnum
;
70 done
= wp
->w_cline_row
;
78 for ( ; lnum
<= wp
->w_buffer
->b_ml
.ml_line_count
; ++lnum
)
83 if (hasFoldingWin(wp
, lnum
, NULL
, &last
, TRUE
, NULL
))
91 if (lnum
== wp
->w_topline
)
92 n
= plines_win_nofill(wp
, lnum
, TRUE
) + wp
->w_topfill
;
95 n
= plines_win(wp
, lnum
, TRUE
);
98 lnum
<= wp
->w_cursor
.lnum
&& last
>= wp
->w_cursor
.lnum
100 lnum
== wp
->w_cursor
.lnum
104 wp
->w_cline_row
= done
;
105 wp
->w_cline_height
= n
;
107 wp
->w_cline_folded
= folded
;
109 wp
->w_valid
|= (VALID_CROW
|VALID_CHEIGHT
);
111 if (done
+ n
> wp
->w_height
)
119 /* wp->w_botline is the line that is just below the window */
120 wp
->w_botline
= lnum
;
121 wp
->w_valid
|= VALID_BOTLINE
|VALID_BOTLINE_AP
;
123 set_empty_rows(wp
, done
);
127 * Update curwin->w_topline and redraw if necessary.
128 * Used to update the screen before printing a message.
131 update_topline_redraw()
139 * Update curwin->w_topline to move the cursor onto the screen.
147 linenr_T old_topline
;
154 int check_topline
= FALSE
;
155 int check_botline
= FALSE
;
160 if (!screen_valid(TRUE
))
163 check_cursor_moved(curwin
);
164 if (curwin
->w_valid
& VALID_TOPLINE
)
168 /* When dragging with the mouse, don't scroll that quickly */
169 if (mouse_dragging
> 0)
170 p_so
= mouse_dragging
- 1;
173 old_topline
= curwin
->w_topline
;
175 old_topfill
= curwin
->w_topfill
;
179 * If the buffer is empty, always set topline to 1.
181 if (bufempty()) /* special case - file is empty */
183 if (curwin
->w_topline
!= 1)
184 redraw_later(NOT_VALID
);
185 curwin
->w_topline
= 1;
186 curwin
->w_botline
= 2;
187 curwin
->w_valid
|= VALID_BOTLINE
|VALID_BOTLINE_AP
;
188 #ifdef FEAT_SCROLLBIND
189 curwin
->w_scbind_pos
= 1;
194 * If the cursor is above or near the top of the window, scroll the window
195 * to show the line the cursor is in, with 'scrolloff' context.
199 if (curwin
->w_topline
> 1)
201 /* If the cursor is above topline, scrolling is always needed.
202 * If the cursor is far below topline and there is no folding,
203 * scrolling down is never needed. */
204 if (curwin
->w_cursor
.lnum
< curwin
->w_topline
)
205 check_topline
= TRUE
;
206 else if (check_top_offset())
207 check_topline
= TRUE
;
210 /* Check if there are more filler lines than allowed. */
211 if (!check_topline
&& curwin
->w_topfill
> diff_check_fill(curwin
,
213 check_topline
= TRUE
;
218 halfheight
= curwin
->w_height
/ 2 - 1;
223 if (hasAnyFolding(curwin
))
225 /* Count the number of logical lines between the cursor and
226 * topline + p_so (approximation of how much will be
229 for (lnum
= curwin
->w_cursor
.lnum
;
230 lnum
< curwin
->w_topline
+ p_so
; ++lnum
)
233 /* stop at end of file or when we know we are far off */
234 if (lnum
>= curbuf
->b_ml
.ml_line_count
|| n
>= halfheight
)
236 (void)hasFolding(lnum
, NULL
, &lnum
);
241 n
= curwin
->w_topline
+ p_so
- curwin
->w_cursor
.lnum
;
243 /* If we weren't very close to begin with, we scroll to put the
244 * cursor in the middle of the window. Otherwise put the cursor
245 * near the top of the window. */
247 scroll_cursor_halfway(FALSE
);
250 scroll_cursor_top(scrolljump_value(), FALSE
);
251 check_botline
= TRUE
;
258 /* Make sure topline is the first line of a fold. */
259 (void)hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
261 check_botline
= TRUE
;
266 * If the cursor is below the bottom of the window, scroll the window
267 * to put the cursor on the window.
268 * When w_botline is invalid, recompute it first, to avoid a redraw later.
269 * If w_botline was approximated, we might need a redraw later in a few
270 * cases, but we don't want to spend (a lot of) time recomputing w_botline
271 * for every small change.
275 if (!(curwin
->w_valid
& VALID_BOTLINE_AP
))
278 if (curwin
->w_botline
<= curbuf
->b_ml
.ml_line_count
)
280 if (curwin
->w_cursor
.lnum
< curwin
->w_botline
)
282 if (((long)curwin
->w_cursor
.lnum
283 >= (long)curwin
->w_botline
- p_so
285 || hasAnyFolding(curwin
)
291 /* Cursor is (a few lines) above botline, check if there are
292 * 'scrolloff' window lines below the cursor. If not, need to
294 n
= curwin
->w_empty_rows
;
295 loff
.lnum
= curwin
->w_cursor
.lnum
;
297 /* In a fold go to its last line. */
298 (void)hasFolding(loff
.lnum
, NULL
, &loff
.lnum
);
302 n
+= curwin
->w_filler_rows
;
305 while (loff
.lnum
< curwin
->w_botline
307 && (loff
.lnum
+ 1 < curwin
->w_botline
|| loff
.fill
== 0)
317 /* sufficient context, no need to scroll */
318 check_botline
= FALSE
;
321 /* sufficient context, no need to scroll */
322 check_botline
= FALSE
;
327 if (hasAnyFolding(curwin
))
329 /* Count the number of logical lines between the cursor and
330 * botline - p_so (approximation of how much will be
333 for (lnum
= curwin
->w_cursor
.lnum
;
334 lnum
>= curwin
->w_botline
- p_so
; --lnum
)
337 /* stop at end of file or when we know we are far off */
338 if (lnum
<= 0 || line_count
> curwin
->w_height
+ 1)
340 (void)hasFolding(lnum
, &lnum
, NULL
);
345 line_count
= curwin
->w_cursor
.lnum
- curwin
->w_botline
347 if (line_count
<= curwin
->w_height
+ 1)
348 scroll_cursor_bot(scrolljump_value(), FALSE
);
350 scroll_cursor_halfway(FALSE
);
354 curwin
->w_valid
|= VALID_TOPLINE
;
357 * Need to redraw when topline changed.
359 if (curwin
->w_topline
!= old_topline
361 || curwin
->w_topfill
!= old_topfill
366 if (curwin
->w_skipcol
!= 0)
368 curwin
->w_skipcol
= 0;
369 redraw_later(NOT_VALID
);
373 /* May need to set w_skipcol when cursor in w_topline. */
374 if (curwin
->w_cursor
.lnum
== curwin
->w_topline
)
384 * Return the scrolljump value to use for the current window.
385 * When 'scrolljump' is positive use it as-is.
386 * When 'scrolljump' is negative use it as a percentage of the window height.
393 return (curwin
->w_height
* -p_sj
) / 100;
397 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
406 if (curwin
->w_cursor
.lnum
< curwin
->w_topline
+ p_so
408 || hasAnyFolding(curwin
)
412 loff
.lnum
= curwin
->w_cursor
.lnum
;
415 n
= curwin
->w_topfill
; /* always have this context */
419 /* Count the visible screen lines above the cursor line. */
423 /* Stop when included a line above the window. */
424 if (loff
.lnum
< curwin
->w_topline
426 || (loff
.lnum
== curwin
->w_topline
&& loff
.fill
> 0)
441 if (curwin
->w_set_curswant
)
444 curwin
->w_curswant
= curwin
->w_virtcol
;
445 curwin
->w_set_curswant
= FALSE
;
450 * Check if the cursor has moved. Set the w_valid flag accordingly.
453 check_cursor_moved(wp
)
456 if (wp
->w_cursor
.lnum
!= wp
->w_valid_cursor
.lnum
)
458 wp
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
459 |VALID_CHEIGHT
|VALID_CROW
|VALID_TOPLINE
);
460 wp
->w_valid_cursor
= wp
->w_cursor
;
461 wp
->w_valid_leftcol
= wp
->w_leftcol
;
463 else if (wp
->w_cursor
.col
!= wp
->w_valid_cursor
.col
464 || wp
->w_leftcol
!= wp
->w_valid_leftcol
465 #ifdef FEAT_VIRTUALEDIT
466 || wp
->w_cursor
.coladd
!= wp
->w_valid_cursor
.coladd
470 wp
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
);
471 wp
->w_valid_cursor
.col
= wp
->w_cursor
.col
;
472 wp
->w_valid_leftcol
= wp
->w_leftcol
;
473 #ifdef FEAT_VIRTUALEDIT
474 wp
->w_valid_cursor
.coladd
= wp
->w_cursor
.coladd
;
480 * Call this function when some window settings have changed, which require
481 * the cursor position, botline and topline to be recomputed and the window to
482 * be redrawn. E.g, when changing the 'wrap' option or folding.
485 changed_window_setting()
487 changed_window_setting_win(curwin
);
491 changed_window_setting_win(wp
)
494 wp
->w_lines_valid
= 0;
495 changed_line_abv_curs_win(wp
);
496 wp
->w_valid
&= ~(VALID_BOTLINE
|VALID_BOTLINE_AP
|VALID_TOPLINE
);
497 redraw_win_later(wp
, NOT_VALID
);
501 * Set wp->w_topline to a certain number.
504 set_topline(wp
, lnum
)
509 /* go to first of folded lines */
510 (void)hasFoldingWin(wp
, lnum
, &lnum
, NULL
, TRUE
, NULL
);
512 /* Approximate the value of w_botline */
513 wp
->w_botline
+= lnum
- wp
->w_topline
;
514 wp
->w_topline
= lnum
;
516 wp
->w_topline_was_set
= TRUE
;
521 wp
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
|VALID_TOPLINE
);
522 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
527 * Call this function when the length of the cursor line (in screen
528 * characters) has changed, and the change is before the cursor.
529 * Need to take care of w_botline separately!
532 changed_cline_bef_curs()
534 curwin
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
535 |VALID_CHEIGHT
|VALID_TOPLINE
);
539 changed_cline_bef_curs_win(wp
)
542 wp
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
543 |VALID_CHEIGHT
|VALID_TOPLINE
);
548 * Call this function when the length of the cursor line (in screen
549 * characters) has changed, and the position of the cursor doesn't change.
550 * Need to take care of w_botline separately!
553 changed_cline_aft_curs()
555 curwin
->w_valid
&= ~VALID_CHEIGHT
;
560 * Call this function when the length of a line (in screen characters) above
561 * the cursor have changed.
562 * Need to take care of w_botline separately!
565 changed_line_abv_curs()
567 curwin
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
|VALID_CROW
568 |VALID_CHEIGHT
|VALID_TOPLINE
);
572 changed_line_abv_curs_win(wp
)
575 wp
->w_valid
&= ~(VALID_WROW
|VALID_WCOL
|VALID_VIRTCOL
|VALID_CROW
576 |VALID_CHEIGHT
|VALID_TOPLINE
);
580 * Make sure the value of curwin->w_botline is valid.
585 if (!(curwin
->w_valid
& VALID_BOTLINE
))
586 comp_botline(curwin
);
590 * Make sure the value of wp->w_botline is valid.
593 validate_botline_win(wp
)
596 if (!(wp
->w_valid
& VALID_BOTLINE
))
601 * Mark curwin->w_botline as invalid (because of some change in the buffer).
606 curwin
->w_valid
&= ~(VALID_BOTLINE
|VALID_BOTLINE_AP
);
610 invalidate_botline_win(wp
)
613 wp
->w_valid
&= ~(VALID_BOTLINE
|VALID_BOTLINE_AP
);
616 #if 0 /* never used */
618 * Mark curwin->w_botline as approximated (because of some small change in the
622 approximate_botline()
624 curwin
->w_valid
&= ~VALID_BOTLINE
;
629 approximate_botline_win(wp
)
632 wp
->w_valid
&= ~VALID_BOTLINE
;
637 * Return TRUE if curwin->w_botline is valid.
642 return (curwin
->w_valid
& VALID_BOTLINE
);
648 * Return TRUE if curwin->w_botline is valid or approximated.
651 botline_approximated()
653 return (curwin
->w_valid
& VALID_BOTLINE_AP
);
658 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
663 check_cursor_moved(curwin
);
664 return ((curwin
->w_valid
& (VALID_WROW
|VALID_WCOL
)) ==
665 (VALID_WROW
|VALID_WCOL
));
669 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
670 * w_topline must be valid, you may need to call update_topline() first!
675 check_cursor_moved(curwin
);
676 if ((curwin
->w_valid
& (VALID_WCOL
|VALID_WROW
)) != (VALID_WCOL
|VALID_WROW
))
680 #if defined(FEAT_GUI) || defined(PROTO)
682 * validate w_cline_row.
688 * First make sure that w_topline is valid (after moving the cursor).
691 check_cursor_moved(curwin
);
692 if (!(curwin
->w_valid
& VALID_CROW
))
693 curs_rows(curwin
, FALSE
);
698 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
701 * Returns OK when cursor is in the window, FAIL when it isn't.
704 curs_rows(wp
, do_botline
)
706 int do_botline
; /* also compute w_botline */
716 /* Check if wp->w_lines[].wl_size is invalid */
717 all_invalid
= (!redrawing()
718 || wp
->w_lines_valid
== 0
719 || wp
->w_lines
[0].wl_lnum
> wp
->w_topline
);
722 for (lnum
= wp
->w_topline
; lnum
< wp
->w_cursor
.lnum
; ++i
)
725 if (!all_invalid
&& i
< wp
->w_lines_valid
)
727 if (wp
->w_lines
[i
].wl_lnum
< lnum
|| !wp
->w_lines
[i
].wl_valid
)
728 continue; /* skip changed or deleted lines */
729 if (wp
->w_lines
[i
].wl_lnum
== lnum
)
732 /* Check for newly inserted lines below this row, in which
733 * case we need to check for folded lines. */
734 if (!wp
->w_buffer
->b_mod_set
735 || wp
->w_lines
[i
].wl_lastlnum
< wp
->w_cursor
.lnum
736 || wp
->w_buffer
->b_mod_top
737 > wp
->w_lines
[i
].wl_lastlnum
+ 1)
741 else if (wp
->w_lines
[i
].wl_lnum
> lnum
)
742 --i
; /* hold at inserted lines */
746 && (lnum
!= wp
->w_topline
|| !wp
->w_p_diff
)
751 lnum
= wp
->w_lines
[i
].wl_lastlnum
+ 1;
752 /* Cursor inside folded lines, don't count this row */
753 if (lnum
> wp
->w_cursor
.lnum
)
758 wp
->w_cline_row
+= wp
->w_lines
[i
].wl_size
;
763 fold_count
= foldedCount(wp
, lnum
, NULL
);
767 if (lnum
> wp
->w_cursor
.lnum
)
774 if (lnum
== wp
->w_topline
)
775 wp
->w_cline_row
+= plines_win_nofill(wp
, lnum
++, TRUE
)
779 wp
->w_cline_row
+= plines_win(wp
, lnum
++, TRUE
);
783 check_cursor_moved(wp
);
784 if (!(wp
->w_valid
& VALID_CHEIGHT
))
787 || i
== wp
->w_lines_valid
788 || (i
< wp
->w_lines_valid
789 && (!wp
->w_lines
[i
].wl_valid
790 || wp
->w_lines
[i
].wl_lnum
!= wp
->w_cursor
.lnum
)))
793 if (wp
->w_cursor
.lnum
== wp
->w_topline
)
794 wp
->w_cline_height
= plines_win_nofill(wp
, wp
->w_cursor
.lnum
,
795 TRUE
) + wp
->w_topfill
;
798 wp
->w_cline_height
= plines_win(wp
, wp
->w_cursor
.lnum
, TRUE
);
800 wp
->w_cline_folded
= hasFoldingWin(wp
, wp
->w_cursor
.lnum
,
801 NULL
, NULL
, TRUE
, NULL
);
804 else if (i
> wp
->w_lines_valid
)
806 /* a line that is too long to fit on the last screen line */
807 wp
->w_cline_height
= 0;
809 wp
->w_cline_folded
= hasFoldingWin(wp
, wp
->w_cursor
.lnum
,
810 NULL
, NULL
, TRUE
, NULL
);
815 wp
->w_cline_height
= wp
->w_lines
[i
].wl_size
;
817 wp
->w_cline_folded
= wp
->w_lines
[i
].wl_folded
;
822 wp
->w_valid
|= VALID_CROW
|VALID_CHEIGHT
;
824 /* validate botline too, if update_screen doesn't do it */
825 if (do_botline
&& all_invalid
)
826 validate_botline_win(wp
);
830 * Validate curwin->w_virtcol only.
835 validate_virtcol_win(curwin
);
839 * Validate wp->w_virtcol only.
842 validate_virtcol_win(wp
)
845 check_cursor_moved(wp
);
846 if (!(wp
->w_valid
& VALID_VIRTCOL
))
848 getvvcol(wp
, &wp
->w_cursor
, NULL
, &(wp
->w_virtcol
), NULL
);
849 wp
->w_valid
|= VALID_VIRTCOL
;
852 # ifdef FEAT_INS_EXPAND
856 redraw_win_later(wp
, SOME_VALID
);
862 * Validate curwin->w_cline_height only.
867 check_cursor_moved(curwin
);
868 if (!(curwin
->w_valid
& VALID_CHEIGHT
))
871 if (curwin
->w_cursor
.lnum
== curwin
->w_topline
)
872 curwin
->w_cline_height
= plines_nofill(curwin
->w_cursor
.lnum
)
876 curwin
->w_cline_height
= plines(curwin
->w_cursor
.lnum
);
878 curwin
->w_cline_folded
= hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
);
880 curwin
->w_valid
|= VALID_CHEIGHT
;
885 * Validate w_wcol and w_virtcol only.
888 validate_cursor_col()
895 if (!(curwin
->w_valid
& VALID_WCOL
))
897 col
= curwin
->w_virtcol
;
898 off
= curwin_col_off();
900 width
= W_WIDTH(curwin
) - off
+ curwin_col_off2();
902 /* long line wrapping, adjust curwin->w_wrow */
904 && col
>= (colnr_T
)W_WIDTH(curwin
)
906 /* use same formula as what is used in curs_columns() */
907 col
-= ((col
- W_WIDTH(curwin
)) / width
+ 1) * width
;
908 if (col
> (int)curwin
->w_leftcol
)
909 col
-= curwin
->w_leftcol
;
912 curwin
->w_wcol
= col
;
914 curwin
->w_valid
|= VALID_WCOL
;
919 * Compute offset of a window, occupied by line number, fold column and sign
920 * column (these don't move when scrolling horizontally).
926 return ((wp
->w_p_nu
? number_width(wp
) + 1 : 0)
928 + (cmdwin_type
== 0 || wp
!= curwin
? 0 : 1)
935 # ifdef FEAT_NETBEANS_INTG
936 /* always show glyph gutter in netbeans */
939 wp
->w_buffer
->b_signlist
!= NULL
? 2 : 0)
947 return win_col_off(curwin
);
951 * Return the difference in column offset for the second screen line of a
952 * wrapped line. It's 8 if 'number' is on and 'n' is in 'cpoptions'.
958 if (wp
->w_p_nu
&& vim_strchr(p_cpo
, CPO_NUMCOL
) != NULL
)
959 return number_width(wp
) + 1;
966 return win_col_off2(curwin
);
970 * compute curwin->w_wcol and curwin->w_virtcol.
971 * Also updates curwin->w_wrow and curwin->w_cline_row.
972 * Also updates curwin->w_leftcol.
976 int scroll
; /* when TRUE, may scroll horizontally */
979 int extra
; /* offset for first screen line */
980 int off_left
, off_right
;
988 colnr_T prev_skipcol
;
991 * First make sure that w_topline is valid (after moving the cursor).
996 * Next make sure that w_cline_row is valid.
998 if (!(curwin
->w_valid
& VALID_CROW
))
999 curs_rows(curwin
, FALSE
);
1002 * Compute the number of virtual columns.
1005 if (curwin
->w_cline_folded
)
1006 /* In a folded line the cursor is always in the first column */
1007 startcol
= curwin
->w_virtcol
= endcol
= curwin
->w_leftcol
;
1010 getvvcol(curwin
, &curwin
->w_cursor
,
1011 &startcol
, &(curwin
->w_virtcol
), &endcol
);
1013 /* remove '$' from change command when cursor moves onto it */
1014 if (startcol
> dollar_vcol
)
1017 extra
= curwin_col_off();
1018 curwin
->w_wcol
= curwin
->w_virtcol
+ extra
;
1022 * Now compute w_wrow, counting screen lines from w_cline_row.
1024 curwin
->w_wrow
= curwin
->w_cline_row
;
1026 textwidth
= W_WIDTH(curwin
) - extra
;
1029 /* No room for text, put cursor in last char of window. */
1030 curwin
->w_wcol
= W_WIDTH(curwin
) - 1;
1031 curwin
->w_wrow
= curwin
->w_height
- 1;
1033 else if (curwin
->w_p_wrap
1034 #ifdef FEAT_VERTSPLIT
1035 && curwin
->w_width
!= 0
1039 width
= textwidth
+ curwin_col_off2();
1041 /* long line wrapping, adjust curwin->w_wrow */
1042 if (curwin
->w_wcol
>= W_WIDTH(curwin
))
1044 /* this same formula is used in validate_cursor_col() */
1045 n
= (curwin
->w_wcol
- W_WIDTH(curwin
)) / width
+ 1;
1046 curwin
->w_wcol
-= n
* width
;
1047 curwin
->w_wrow
+= n
;
1049 #ifdef FEAT_LINEBREAK
1050 /* When cursor wraps to first char of next line in Insert
1051 * mode, the 'showbreak' string isn't shown, backup to first
1053 if (*p_sbr
&& *ml_get_cursor() == NUL
1054 && curwin
->w_wcol
== (int)vim_strsize(p_sbr
))
1060 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1062 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
1065 && !curwin
->w_cline_folded
1070 * If Cursor is left of the screen, scroll rightwards.
1071 * If Cursor is right of the screen, scroll leftwards
1072 * If we get closer to the edge than 'sidescrolloff', scroll a little
1075 off_left
= (int)startcol
- (int)curwin
->w_leftcol
- p_siso
;
1076 off_right
= (int)endcol
- (int)(curwin
->w_leftcol
+ W_WIDTH(curwin
)
1078 if (off_left
< 0 || off_right
> 0)
1085 /* When far off or not enough room on either side, put cursor in
1086 * middle of window. */
1087 if (p_ss
== 0 || diff
>= textwidth
/ 2 || off_right
>= off_left
)
1088 new_leftcol
= curwin
->w_wcol
- extra
- textwidth
/ 2;
1094 new_leftcol
= curwin
->w_leftcol
- diff
;
1096 new_leftcol
= curwin
->w_leftcol
+ diff
;
1098 if (new_leftcol
< 0)
1100 if (new_leftcol
!= (int)curwin
->w_leftcol
)
1102 curwin
->w_leftcol
= new_leftcol
;
1103 /* screen has to be redrawn with new curwin->w_leftcol */
1104 redraw_later(NOT_VALID
);
1107 curwin
->w_wcol
-= curwin
->w_leftcol
;
1109 else if (curwin
->w_wcol
> (int)curwin
->w_leftcol
)
1110 curwin
->w_wcol
-= curwin
->w_leftcol
;
1115 /* Skip over filler lines. At the top use w_topfill, there
1116 * may be some filler lines above the window. */
1117 if (curwin
->w_cursor
.lnum
== curwin
->w_topline
)
1118 curwin
->w_wrow
+= curwin
->w_topfill
;
1120 curwin
->w_wrow
+= diff_check_fill(curwin
, curwin
->w_cursor
.lnum
);
1123 prev_skipcol
= curwin
->w_skipcol
;
1126 if ((curwin
->w_wrow
>= curwin
->w_height
1127 || ((prev_skipcol
> 0
1128 || curwin
->w_wrow
+ p_so
>= curwin
->w_height
)
1135 (curwin
, curwin
->w_cursor
.lnum
, FALSE
))
1136 - 1 >= curwin
->w_height
))
1137 && curwin
->w_height
!= 0
1138 && curwin
->w_cursor
.lnum
== curwin
->w_topline
1140 #ifdef FEAT_VERTSPLIT
1141 && curwin
->w_width
!= 0
1145 /* Cursor past end of screen. Happens with a single line that does
1146 * not fit on screen. Find a skipcol to show the text around the
1147 * cursor. Avoid scrolling all the time. compute value of "extra":
1148 * 1: Less than "p_so" lines above
1149 * 2: Less than "p_so" lines below
1150 * 3: both of them */
1152 if (curwin
->w_skipcol
+ p_so
* width
> curwin
->w_virtcol
)
1154 /* Compute last display line of the buffer line that we want at the
1155 * bottom of the window. */
1157 p_lines
= plines_win(curwin
, curwin
->w_cursor
.lnum
, FALSE
);
1159 if (p_lines
> curwin
->w_wrow
+ p_so
)
1160 n
= curwin
->w_wrow
+ p_so
;
1163 if ((colnr_T
)n
>= curwin
->w_height
+ curwin
->w_skipcol
/ width
)
1166 if (extra
== 3 || p_lines
< p_so
* 2)
1168 /* not enough room for 'scrolloff', put cursor in the middle */
1169 n
= curwin
->w_virtcol
/ width
;
1170 if (n
> curwin
->w_height
/ 2)
1171 n
-= curwin
->w_height
/ 2;
1174 /* don't skip more than necessary */
1175 if (n
> p_lines
- curwin
->w_height
+ 1)
1176 n
= p_lines
- curwin
->w_height
+ 1;
1177 curwin
->w_skipcol
= n
* width
;
1179 else if (extra
== 1)
1181 /* less then 'scrolloff' lines above, decrease skipcol */
1182 extra
= (curwin
->w_skipcol
+ p_so
* width
- curwin
->w_virtcol
1183 + width
- 1) / width
;
1186 if ((colnr_T
)(extra
* width
) > curwin
->w_skipcol
)
1187 extra
= curwin
->w_skipcol
/ width
;
1188 curwin
->w_skipcol
-= extra
* width
;
1191 else if (extra
== 2)
1193 /* less then 'scrolloff' lines below, increase skipcol */
1194 endcol
= (n
- curwin
->w_height
+ 1) * width
;
1195 while (endcol
> curwin
->w_virtcol
)
1197 if (endcol
> curwin
->w_skipcol
)
1198 curwin
->w_skipcol
= endcol
;
1201 curwin
->w_wrow
-= curwin
->w_skipcol
/ width
;
1202 if (curwin
->w_wrow
>= curwin
->w_height
)
1204 /* small window, make sure cursor is in it */
1205 extra
= curwin
->w_wrow
- curwin
->w_height
+ 1;
1206 curwin
->w_skipcol
+= extra
* width
;
1207 curwin
->w_wrow
-= extra
;
1210 extra
= ((int)prev_skipcol
- (int)curwin
->w_skipcol
) / width
;
1212 win_ins_lines(curwin
, 0, extra
, FALSE
, FALSE
);
1214 win_del_lines(curwin
, 0, -extra
, FALSE
, FALSE
);
1217 curwin
->w_skipcol
= 0;
1218 if (prev_skipcol
!= curwin
->w_skipcol
)
1219 redraw_later(NOT_VALID
);
1222 /* Redraw when w_virtcol changes and 'cursorcolumn' is set, or when w_row
1223 * changes and 'cursorline' is set. */
1224 if (((curwin
->w_p_cuc
&& (curwin
->w_valid
& VALID_VIRTCOL
) == 0)
1225 || (curwin
->w_p_cul
&& (curwin
->w_valid
& VALID_WROW
) == 0))
1226 # ifdef FEAT_INS_EXPAND
1230 redraw_later(SOME_VALID
);
1233 curwin
->w_valid
|= VALID_WCOL
|VALID_WROW
|VALID_VIRTCOL
;
1237 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1240 scrolldown(line_count
, byfold
)
1242 int byfold UNUSED
; /* TRUE: count a closed fold as one line */
1244 long done
= 0; /* total # of physical lines done */
1251 /* Make sure w_topline is at the first of a sequence of folded lines. */
1252 (void)hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
1254 validate_cursor(); /* w_wrow needs to be valid */
1255 while (line_count
-- > 0)
1258 if (curwin
->w_topfill
< diff_check(curwin
, curwin
->w_topline
)
1259 && curwin
->w_topfill
< curwin
->w_height
- 1)
1261 ++curwin
->w_topfill
;
1267 if (curwin
->w_topline
== 1)
1269 --curwin
->w_topline
;
1271 curwin
->w_topfill
= 0;
1274 /* A sequence of folded lines only counts for one logical line */
1275 if (hasFolding(curwin
->w_topline
, &first
, NULL
))
1279 line_count
-= curwin
->w_topline
- first
- 1;
1280 curwin
->w_botline
-= curwin
->w_topline
- first
;
1281 curwin
->w_topline
= first
;
1286 done
+= plines_nofill(curwin
->w_topline
);
1288 done
+= plines(curwin
->w_topline
);
1291 --curwin
->w_botline
; /* approximate w_botline */
1292 invalidate_botline();
1294 curwin
->w_wrow
+= done
; /* keep w_wrow updated */
1295 curwin
->w_cline_row
+= done
; /* keep w_cline_row updated */
1298 if (curwin
->w_cursor
.lnum
== curwin
->w_topline
)
1299 curwin
->w_cline_row
= 0;
1300 check_topfill(curwin
, TRUE
);
1304 * Compute the row number of the last row of the cursor line
1305 * and move the cursor onto the displayed part of the window.
1307 wrow
= curwin
->w_wrow
;
1308 if (curwin
->w_p_wrap
1309 #ifdef FEAT_VERTSPLIT
1310 && curwin
->w_width
!= 0
1316 wrow
+= curwin
->w_cline_height
- 1 -
1317 curwin
->w_virtcol
/ W_WIDTH(curwin
);
1319 while (wrow
>= curwin
->w_height
&& curwin
->w_cursor
.lnum
> 1)
1322 if (hasFolding(curwin
->w_cursor
.lnum
, &first
, NULL
))
1326 curwin
->w_cursor
.lnum
= 1;
1328 curwin
->w_cursor
.lnum
= first
- 1;
1332 wrow
-= plines(curwin
->w_cursor
.lnum
--);
1334 ~(VALID_WROW
|VALID_WCOL
|VALID_CHEIGHT
|VALID_CROW
|VALID_VIRTCOL
);
1340 /* Move cursor to first line of closed fold. */
1343 coladvance(curwin
->w_curswant
);
1348 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1351 scrollup(line_count
, byfold
)
1353 int byfold UNUSED
; /* TRUE: count a closed fold as one line */
1355 #if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1359 # ifdef FEAT_FOLDING
1360 (byfold
&& hasAnyFolding(curwin
))
1370 /* count each sequence of folded lines as one logical line */
1371 lnum
= curwin
->w_topline
;
1372 while (line_count
--)
1375 if (curwin
->w_topfill
> 0)
1376 --curwin
->w_topfill
;
1380 # ifdef FEAT_FOLDING
1382 (void)hasFolding(lnum
, NULL
, &lnum
);
1384 if (lnum
>= curbuf
->b_ml
.ml_line_count
)
1388 curwin
->w_topfill
= diff_check_fill(curwin
, lnum
);
1392 /* approximate w_botline */
1393 curwin
->w_botline
+= lnum
- curwin
->w_topline
;
1394 curwin
->w_topline
= lnum
;
1399 curwin
->w_topline
+= line_count
;
1400 curwin
->w_botline
+= line_count
; /* approximate w_botline */
1403 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
1404 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
1405 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
+ 1)
1406 curwin
->w_botline
= curbuf
->b_ml
.ml_line_count
+ 1;
1409 check_topfill(curwin
, FALSE
);
1413 if (hasAnyFolding(curwin
))
1414 /* Make sure w_topline is at the first of a sequence of folded lines. */
1415 (void)hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
1418 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
);
1419 if (curwin
->w_cursor
.lnum
< curwin
->w_topline
)
1421 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
1423 ~(VALID_WROW
|VALID_WCOL
|VALID_CHEIGHT
|VALID_CROW
|VALID_VIRTCOL
);
1424 coladvance(curwin
->w_curswant
);
1430 * Don't end up with too many filler lines in the window.
1433 check_topfill(wp
, down
)
1435 int down
; /* when TRUE scroll down when not enough space */
1439 if (wp
->w_topfill
> 0)
1441 n
= plines_win_nofill(wp
, wp
->w_topline
, TRUE
);
1442 if (wp
->w_topfill
+ n
> wp
->w_height
)
1444 if (down
&& wp
->w_topline
> 1)
1451 wp
->w_topfill
= wp
->w_height
- n
;
1452 if (wp
->w_topfill
< 0)
1460 * Use as many filler lines as possible for w_topline. Make sure w_topline
1468 n
= plines_nofill(curwin
->w_topline
);
1469 if (n
>= curwin
->w_height
)
1470 curwin
->w_topfill
= 0;
1473 curwin
->w_topfill
= diff_check_fill(curwin
, curwin
->w_topline
);
1474 if (curwin
->w_topfill
+ n
> curwin
->w_height
)
1475 curwin
->w_topfill
= curwin
->w_height
- n
;
1480 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1482 * Scroll the screen one line down, but don't do it if it would move the
1483 * cursor off the screen.
1490 int can_fill
= (curwin
->w_topfill
1491 < diff_check_fill(curwin
, curwin
->w_topline
));
1494 if (curwin
->w_topline
<= 1
1501 validate_cursor(); /* w_wrow needs to be valid */
1504 * Compute the row number of the last row of the cursor line
1505 * and make sure it doesn't go off the screen. Make sure the cursor
1506 * doesn't go past 'scrolloff' lines from the screen end.
1508 end_row
= curwin
->w_wrow
;
1513 end_row
+= plines_nofill(curwin
->w_topline
- 1);
1515 end_row
+= plines(curwin
->w_topline
- 1);
1517 if (curwin
->w_p_wrap
1518 #ifdef FEAT_VERTSPLIT
1519 && curwin
->w_width
!= 0
1525 end_row
+= curwin
->w_cline_height
- 1 -
1526 curwin
->w_virtcol
/ W_WIDTH(curwin
);
1528 if (end_row
< curwin
->w_height
- p_so
)
1533 ++curwin
->w_topfill
;
1534 check_topfill(curwin
, TRUE
);
1538 --curwin
->w_topline
;
1539 curwin
->w_topfill
= 0;
1542 --curwin
->w_topline
;
1545 hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
1547 --curwin
->w_botline
; /* approximate w_botline */
1548 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
);
1553 * Scroll the screen one line up, but don't do it if it would move the cursor
1561 if (curwin
->w_topline
== curbuf
->b_ml
.ml_line_count
1563 && curwin
->w_topfill
== 0
1568 validate_cursor(); /* w_wrow needs to be valid */
1571 * Compute the row number of the first row of the cursor line
1572 * and make sure it doesn't go off the screen. Make sure the cursor
1573 * doesn't go before 'scrolloff' lines from the screen start.
1576 start_row
= curwin
->w_wrow
- plines_nofill(curwin
->w_topline
)
1577 - curwin
->w_topfill
;
1579 start_row
= curwin
->w_wrow
- plines(curwin
->w_topline
);
1581 if (curwin
->w_p_wrap
1582 #ifdef FEAT_VERTSPLIT
1583 && curwin
->w_width
!= 0
1588 start_row
-= curwin
->w_virtcol
/ W_WIDTH(curwin
);
1590 if (start_row
>= p_so
)
1593 if (curwin
->w_topfill
> 0)
1594 --curwin
->w_topfill
;
1599 (void)hasFolding(curwin
->w_topline
, NULL
, &curwin
->w_topline
);
1601 ++curwin
->w_topline
;
1603 ++curwin
->w_botline
; /* approximate w_botline */
1604 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
);
1607 #endif /* FEAT_INS_EXPAND */
1610 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1611 * a (wrapped) text line. Uses and sets "lp->fill".
1612 * Returns the height of the added line in "lp->height".
1613 * Lines above the first one are incredibly high.
1620 if (lp
->fill
< diff_check_fill(curwin
, lp
->lnum
))
1622 /* Add a filler line. */
1634 lp
->height
= MAXCOL
;
1637 if (hasFolding(lp
->lnum
, &lp
->lnum
, NULL
))
1638 /* Add a closed fold */
1644 lp
->height
= plines_nofill(lp
->lnum
);
1646 lp
->height
= plines(lp
->lnum
);
1653 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1654 * a (wrapped) text line. Uses and sets "lp->fill".
1655 * Returns the height of the added line in "lp->height".
1656 * Lines below the last one are incredibly high.
1663 if (lp
->fill
< diff_check_fill(curwin
, lp
->lnum
+ 1))
1665 /* Add a filler line. */
1676 if (lp
->lnum
> curbuf
->b_ml
.ml_line_count
)
1677 lp
->height
= MAXCOL
;
1680 if (hasFolding(lp
->lnum
, NULL
, &lp
->lnum
))
1681 /* Add a closed fold */
1687 lp
->height
= plines_nofill(lp
->lnum
);
1689 lp
->height
= plines(lp
->lnum
);
1697 * Switch from including filler lines below lp->lnum to including filler
1698 * lines above loff.lnum + 1. This keeps pointing to the same line.
1699 * When there are no filler lines nothing changes.
1708 lp
->fill
= diff_check_fill(curwin
, lp
->lnum
) - lp
->fill
+ 1;
1713 * Switch from including filler lines above lp->lnum to including filler
1714 * lines below loff.lnum - 1. This keeps pointing to the same line.
1715 * When there are no filler lines nothing changes.
1723 lp
->fill
= diff_check_fill(curwin
, lp
->lnum
) - lp
->fill
+ 1;
1730 * Recompute topline to put the cursor at the top of the window.
1731 * Scroll at least "min_scroll" lines.
1732 * If "always" is TRUE, always set topline (for "zt").
1735 scroll_cursor_top(min_scroll
, always
)
1743 linenr_T top
; /* just above displayed lines */
1744 linenr_T bot
; /* just below displayed lines */
1745 linenr_T old_topline
= curwin
->w_topline
;
1747 linenr_T old_topfill
= curwin
->w_topfill
;
1749 linenr_T new_topline
;
1753 if (mouse_dragging
> 0)
1754 off
= mouse_dragging
- 1;
1758 * Decrease topline until:
1760 * - (part of) the cursor line is moved off the screen or
1761 * - moved at least 'scrolljump' lines and
1762 * - at least 'scrolloff' lines above and below the cursor
1765 used
= curwin
->w_cline_height
;
1766 if (curwin
->w_cursor
.lnum
< curwin
->w_topline
)
1770 if (hasFolding(curwin
->w_cursor
.lnum
, &top
, &bot
))
1778 top
= curwin
->w_cursor
.lnum
- 1;
1779 bot
= curwin
->w_cursor
.lnum
+ 1;
1781 new_topline
= top
+ 1;
1784 /* count filler lines of the cursor window as context */
1785 i
= diff_check_fill(curwin
, curwin
->w_cursor
.lnum
);
1791 * Check if the lines from "top" to "bot" fit in the window. If they do,
1792 * set new_topline and advance "top" and "bot" to include more lines.
1797 if (hasFolding(top
, &top
, NULL
))
1798 /* count one logical line for a sequence of folded lines */
1804 if (extra
+ i
<= off
&& bot
< curbuf
->b_ml
.ml_line_count
)
1807 if (hasFolding(bot
, NULL
, &bot
))
1808 /* count one logical line for a sequence of folded lines */
1812 used
+= plines(bot
);
1814 if (used
> curwin
->w_height
)
1816 if (top
< curwin
->w_topline
)
1820 * If scrolling is needed, scroll at least 'sj' lines.
1822 if ((new_topline
>= curwin
->w_topline
|| scrolled
> min_scroll
)
1833 * If we don't have enough space, put cursor in the middle.
1834 * This makes sure we get the same position when using "k" and "j"
1835 * in a small window.
1837 if (used
> curwin
->w_height
)
1838 scroll_cursor_halfway(FALSE
);
1842 * If "always" is FALSE, only adjust topline to a lower value, higher
1843 * value may happen with wrapping lines
1845 if (new_topline
< curwin
->w_topline
|| always
)
1846 curwin
->w_topline
= new_topline
;
1847 if (curwin
->w_topline
> curwin
->w_cursor
.lnum
)
1848 curwin
->w_topline
= curwin
->w_cursor
.lnum
;
1850 curwin
->w_topfill
= diff_check_fill(curwin
, curwin
->w_topline
);
1851 if (curwin
->w_topfill
> 0 && extra
> off
)
1853 curwin
->w_topfill
-= extra
- off
;
1854 if (curwin
->w_topfill
< 0)
1855 curwin
->w_topfill
= 0;
1857 check_topfill(curwin
, FALSE
);
1859 if (curwin
->w_topline
!= old_topline
1861 || curwin
->w_topfill
!= old_topfill
1865 ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
|VALID_BOTLINE_AP
);
1866 curwin
->w_valid
|= VALID_TOPLINE
;
1871 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1872 * screen lines for text lines.
1875 set_empty_rows(wp
, used
)
1880 wp
->w_filler_rows
= 0;
1883 wp
->w_empty_rows
= 0; /* single line that doesn't fit */
1886 wp
->w_empty_rows
= wp
->w_height
- used
;
1888 if (wp
->w_botline
<= wp
->w_buffer
->b_ml
.ml_line_count
)
1890 wp
->w_filler_rows
= diff_check_fill(wp
, wp
->w_botline
);
1891 if (wp
->w_empty_rows
> wp
->w_filler_rows
)
1892 wp
->w_empty_rows
-= wp
->w_filler_rows
;
1895 wp
->w_filler_rows
= wp
->w_empty_rows
;
1896 wp
->w_empty_rows
= 0;
1904 * Recompute topline to put the cursor at the bottom of the window.
1905 * Scroll at least "min_scroll" lines.
1906 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1907 * This is messy stuff!!!
1910 scroll_cursor_bot(min_scroll
, set_topbot
)
1918 linenr_T line_count
;
1919 linenr_T old_topline
= curwin
->w_topline
;
1923 int old_topfill
= curwin
->w_topfill
;
1924 int fill_below_window
;
1926 linenr_T old_botline
= curwin
->w_botline
;
1927 linenr_T old_valid
= curwin
->w_valid
;
1928 int old_empty_rows
= curwin
->w_empty_rows
;
1929 linenr_T cln
; /* Cursor Line Number */
1931 cln
= curwin
->w_cursor
.lnum
;
1935 curwin
->w_botline
= cln
+ 1;
1939 for (curwin
->w_topline
= curwin
->w_botline
;
1940 curwin
->w_topline
> 1;
1941 curwin
->w_topline
= loff
.lnum
)
1943 loff
.lnum
= curwin
->w_topline
;
1944 topline_back(&loff
);
1945 if (used
+ loff
.height
> curwin
->w_height
)
1947 used
+= loff
.height
;
1949 curwin
->w_topfill
= loff
.fill
;
1952 set_empty_rows(curwin
, used
);
1953 curwin
->w_valid
|= VALID_BOTLINE
|VALID_BOTLINE_AP
;
1954 if (curwin
->w_topline
!= old_topline
1956 || curwin
->w_topfill
!= old_topfill
1959 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
);
1964 /* The lines of the cursor line itself are always used. */
1966 used
= plines_nofill(cln
);
1969 used
= curwin
->w_cline_height
;
1972 /* If the cursor is below botline, we will at least scroll by the height
1973 * of the cursor line. Correct for empty lines, which are really part of
1975 if (cln
>= curwin
->w_botline
)
1978 if (cln
== curwin
->w_botline
)
1979 scrolled
-= curwin
->w_empty_rows
;
1983 * Stop counting lines to scroll when
1984 * - hitting start of the file
1985 * - scrolled nothing or at least 'sj' lines
1986 * - at least 'so' lines below the cursor
1987 * - lines between botline and cursor have been counted
1990 if (!hasFolding(curwin
->w_cursor
.lnum
, &loff
.lnum
, &boff
.lnum
))
1999 fill_below_window
= diff_check_fill(curwin
, curwin
->w_botline
)
2000 - curwin
->w_filler_rows
;
2003 while (loff
.lnum
> 1)
2005 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
2006 * context for 'scrolloff' and counted all lines below the window. */
2007 if ((((scrolled
<= 0 || scrolled
>= min_scroll
)
2010 mouse_dragging
> 0 ? mouse_dragging
- 1 :
2013 || boff
.lnum
+ 1 > curbuf
->b_ml
.ml_line_count
)
2014 && loff
.lnum
<= curwin
->w_botline
2016 && (loff
.lnum
< curwin
->w_botline
2017 || loff
.fill
>= fill_below_window
)
2022 /* Add one line above */
2023 topline_back(&loff
);
2024 used
+= loff
.height
;
2025 if (used
> curwin
->w_height
)
2027 if (loff
.lnum
>= curwin
->w_botline
2029 && (loff
.lnum
> curwin
->w_botline
2030 || loff
.fill
<= fill_below_window
)
2034 /* Count screen lines that are below the window. */
2035 scrolled
+= loff
.height
;
2036 if (loff
.lnum
== curwin
->w_botline
2041 scrolled
-= curwin
->w_empty_rows
;
2044 if (boff
.lnum
< curbuf
->b_ml
.ml_line_count
)
2046 /* Add one line below */
2047 botline_forw(&boff
);
2048 used
+= boff
.height
;
2049 if (used
> curwin
->w_height
)
2053 mouse_dragging
> 0 ? mouse_dragging
- 1 :
2055 p_so
) || scrolled
< min_scroll
)
2057 extra
+= boff
.height
;
2058 if (boff
.lnum
>= curwin
->w_botline
2060 || (boff
.lnum
+ 1 == curwin
->w_botline
2061 && boff
.fill
> curwin
->w_filler_rows
)
2065 /* Count screen lines that are below the window. */
2066 scrolled
+= boff
.height
;
2067 if (boff
.lnum
== curwin
->w_botline
2072 scrolled
-= curwin
->w_empty_rows
;
2078 /* curwin->w_empty_rows is larger, no need to scroll */
2081 /* more than a screenfull, don't scroll but redraw */
2082 else if (used
> curwin
->w_height
)
2084 /* scroll minimal number of lines */
2089 boff
.fill
= curwin
->w_topfill
;
2091 boff
.lnum
= curwin
->w_topline
- 1;
2092 for (i
= 0; i
< scrolled
&& boff
.lnum
< curwin
->w_botline
; )
2094 botline_forw(&boff
);
2098 if (i
< scrolled
) /* below curwin->w_botline, don't scroll */
2103 * Scroll up if the cursor is off the bottom of the screen a bit.
2104 * Otherwise put it at 1/2 of the screen.
2106 if (line_count
>= curwin
->w_height
&& line_count
> min_scroll
)
2107 scroll_cursor_halfway(FALSE
);
2109 scrollup(line_count
, TRUE
);
2112 * If topline didn't change we need to restore w_botline and w_empty_rows
2113 * (we changed them).
2114 * If topline did change, update_screen() will set botline.
2116 if (curwin
->w_topline
== old_topline
&& set_topbot
)
2118 curwin
->w_botline
= old_botline
;
2119 curwin
->w_empty_rows
= old_empty_rows
;
2120 curwin
->w_valid
= old_valid
;
2122 curwin
->w_valid
|= VALID_TOPLINE
;
2126 * Recompute topline to put the cursor halfway the window
2127 * If "atend" is TRUE, also put it halfway at the end of the file.
2130 scroll_cursor_halfway(atend
)
2143 loff
.lnum
= boff
.lnum
= curwin
->w_cursor
.lnum
;
2145 (void)hasFolding(loff
.lnum
, &loff
.lnum
, &boff
.lnum
);
2148 used
= plines_nofill(loff
.lnum
);
2152 used
= plines(loff
.lnum
);
2154 topline
= loff
.lnum
;
2157 if (below
<= above
) /* add a line below the cursor first */
2159 if (boff
.lnum
< curbuf
->b_ml
.ml_line_count
)
2161 botline_forw(&boff
);
2162 used
+= boff
.height
;
2163 if (used
> curwin
->w_height
)
2165 below
+= boff
.height
;
2169 ++below
; /* count a "~" line */
2175 if (below
> above
) /* add a line above the cursor */
2177 topline_back(&loff
);
2178 used
+= loff
.height
;
2179 if (used
> curwin
->w_height
)
2181 above
+= loff
.height
;
2182 topline
= loff
.lnum
;
2184 topfill
= loff
.fill
;
2189 if (!hasFolding(topline
, &curwin
->w_topline
, NULL
))
2191 curwin
->w_topline
= topline
;
2193 curwin
->w_topfill
= topfill
;
2194 check_topfill(curwin
, FALSE
);
2196 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
|VALID_BOTLINE_AP
);
2197 curwin
->w_valid
|= VALID_TOPLINE
;
2201 * Correct the cursor position so that it is in a part of the screen at least
2202 * 'so' lines from the top and bottom, if possible.
2203 * If not possible, put it at the same position as scroll_cursor_halfway().
2204 * When called topline must be valid!
2209 int above
= 0; /* screen lines above topline */
2211 int below
= 0; /* screen lines below botline */
2213 int above_wanted
, below_wanted
;
2214 linenr_T cln
; /* Cursor Line Number */
2218 * How many lines we would like to have above/below the cursor depends on
2219 * whether the first/last line of the file is on screen.
2221 above_wanted
= p_so
;
2222 below_wanted
= p_so
;
2224 if (mouse_dragging
> 0)
2226 above_wanted
= mouse_dragging
- 1;
2227 below_wanted
= mouse_dragging
- 1;
2230 if (curwin
->w_topline
== 1)
2233 max_off
= curwin
->w_height
/ 2;
2234 if (below_wanted
> max_off
)
2235 below_wanted
= max_off
;
2238 if (curwin
->w_botline
== curbuf
->b_ml
.ml_line_count
+ 1
2240 && mouse_dragging
== 0
2245 max_off
= (curwin
->w_height
- 1) / 2;
2246 if (above_wanted
> max_off
)
2247 above_wanted
= max_off
;
2251 * If there are sufficient file-lines above and below the cursor, we can
2254 cln
= curwin
->w_cursor
.lnum
;
2255 if (cln
>= curwin
->w_topline
+ above_wanted
2256 && cln
< curwin
->w_botline
- below_wanted
2258 && !hasAnyFolding(curwin
)
2264 * Narrow down the area where the cursor can be put by taking lines from
2265 * the top and the bottom until:
2266 * - the desired context lines are found
2267 * - the lines from the top is past the lines from the bottom
2269 topline
= curwin
->w_topline
;
2270 botline
= curwin
->w_botline
- 1;
2272 /* count filler lines as context */
2273 above
= curwin
->w_topfill
;
2274 below
= curwin
->w_filler_rows
;
2276 while ((above
< above_wanted
|| below
< below_wanted
) && topline
< botline
)
2278 if (below
< below_wanted
&& (below
<= above
|| above
>= above_wanted
))
2281 if (hasFolding(botline
, &botline
, NULL
))
2285 below
+= plines(botline
);
2288 if (above
< above_wanted
&& (above
< below
|| below
>= below_wanted
))
2291 if (hasFolding(topline
, NULL
, &topline
))
2296 above
+= plines(topline
);
2298 above
+= plines_nofill(topline
);
2300 /* Count filler lines below this line as context. */
2301 if (topline
< botline
)
2302 above
+= diff_check_fill(curwin
, topline
+ 1);
2307 if (topline
== botline
|| botline
== 0)
2308 curwin
->w_cursor
.lnum
= topline
;
2309 else if (topline
> botline
)
2310 curwin
->w_cursor
.lnum
= botline
;
2313 if (cln
< topline
&& curwin
->w_topline
> 1)
2315 curwin
->w_cursor
.lnum
= topline
;
2317 ~(VALID_WROW
|VALID_WCOL
|VALID_CHEIGHT
|VALID_CROW
);
2319 if (cln
> botline
&& curwin
->w_botline
<= curbuf
->b_ml
.ml_line_count
)
2321 curwin
->w_cursor
.lnum
= botline
;
2323 ~(VALID_WROW
|VALID_WCOL
|VALID_CHEIGHT
|VALID_CROW
);
2326 curwin
->w_valid
|= VALID_TOPLINE
;
2329 static void get_scroll_overlap
__ARGS((lineoff_T
*lp
, int dir
));
2332 * move screen 'count' pages up or down and update screen
2334 * return FAIL for failure, OK otherwise
2344 linenr_T old_topline
= curwin
->w_topline
;
2346 if (curbuf
->b_ml
.ml_line_count
== 1) /* nothing to do */
2352 for ( ; count
> 0; --count
)
2356 * It's an error to move a page up when the first line is already on
2357 * the screen. It's an error to move a page down when the last line
2358 * is on the screen and the topline is 'scrolloff' lines from the
2362 ? ((curwin
->w_topline
>= curbuf
->b_ml
.ml_line_count
- p_so
)
2363 && curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
2364 : (curwin
->w_topline
== 1
2366 && curwin
->w_topfill
==
2367 diff_check_fill(curwin
, curwin
->w_topline
)
2381 if (firstwin
== lastwin
&& p_window
> 0 && p_window
< Rows
- 1)
2383 /* Vi compatible scrolling */
2385 ++curwin
->w_topline
;
2387 curwin
->w_topline
+= p_window
- 2;
2388 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
2389 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
2390 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
2392 else if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
2394 /* at end of file */
2395 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
2397 curwin
->w_topfill
= 0;
2399 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
);
2403 /* For the overlap, start with the line just below the window
2404 * and go upwards. */
2405 loff
.lnum
= curwin
->w_botline
;
2407 loff
.fill
= diff_check_fill(curwin
, loff
.lnum
)
2408 - curwin
->w_filler_rows
;
2410 get_scroll_overlap(&loff
, -1);
2411 curwin
->w_topline
= loff
.lnum
;
2413 curwin
->w_topfill
= loff
.fill
;
2414 check_topfill(curwin
, FALSE
);
2416 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
2417 curwin
->w_valid
&= ~(VALID_WCOL
|VALID_CHEIGHT
|VALID_WROW
|
2418 VALID_CROW
|VALID_BOTLINE
|VALID_BOTLINE_AP
);
2421 else /* dir == BACKWARDS */
2424 if (curwin
->w_topline
== 1)
2426 /* Include max number of filler lines */
2431 if (firstwin
== lastwin
&& p_window
> 0 && p_window
< Rows
- 1)
2433 /* Vi compatible scrolling (sort of) */
2435 --curwin
->w_topline
;
2437 curwin
->w_topline
-= p_window
- 2;
2438 if (curwin
->w_topline
< 1)
2439 curwin
->w_topline
= 1;
2440 curwin
->w_cursor
.lnum
= curwin
->w_topline
+ p_window
- 1;
2441 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
2442 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
2446 /* Find the line at the top of the window that is going to be the
2447 * line at the bottom of the window. Make sure this results in
2448 * the same line as before doing CTRL-F. */
2449 loff
.lnum
= curwin
->w_topline
- 1;
2451 loff
.fill
= diff_check_fill(curwin
, loff
.lnum
+ 1)
2452 - curwin
->w_topfill
;
2454 get_scroll_overlap(&loff
, 1);
2456 if (loff
.lnum
>= curbuf
->b_ml
.ml_line_count
)
2458 loff
.lnum
= curbuf
->b_ml
.ml_line_count
;
2464 botline_topline(&loff
);
2467 curwin
->w_cursor
.lnum
= loff
.lnum
;
2469 /* Find the line just above the new topline to get the right line
2470 * at the bottom of the window. */
2472 while (n
<= curwin
->w_height
&& loff
.lnum
>= 1)
2474 topline_back(&loff
);
2477 if (n
<= curwin
->w_height
) /* at begin of file */
2479 curwin
->w_topline
= 1;
2483 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
);
2487 /* Go two lines forward again. */
2489 topline_botline(&loff
);
2491 botline_forw(&loff
);
2492 botline_forw(&loff
);
2494 botline_topline(&loff
);
2497 /* We're at the wrong end of a fold now. */
2498 (void)hasFolding(loff
.lnum
, &loff
.lnum
, NULL
);
2501 /* Always scroll at least one line. Avoid getting stuck on
2502 * very long lines. */
2503 if (loff
.lnum
>= curwin
->w_topline
2505 && (loff
.lnum
> curwin
->w_topline
2506 || loff
.fill
>= curwin
->w_topfill
)
2511 /* First try using the maximum number of filler lines. If
2512 * that's not enough, backup one line. */
2513 loff
.fill
= curwin
->w_topfill
;
2514 if (curwin
->w_topfill
< diff_check_fill(curwin
,
2517 if (curwin
->w_topfill
== loff
.fill
)
2520 --curwin
->w_topline
;
2522 curwin
->w_topfill
= 0;
2525 comp_botline(curwin
);
2526 curwin
->w_cursor
.lnum
= curwin
->w_botline
- 1;
2527 curwin
->w_valid
&= ~(VALID_WCOL
|VALID_CHEIGHT
|
2528 VALID_WROW
|VALID_CROW
);
2532 curwin
->w_topline
= loff
.lnum
;
2534 curwin
->w_topfill
= loff
.fill
;
2535 check_topfill(curwin
, FALSE
);
2537 curwin
->w_valid
&= ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
);
2547 beginline(BL_SOL
| BL_FIX
);
2548 curwin
->w_valid
&= ~(VALID_WCOL
|VALID_WROW
|VALID_VIRTCOL
);
2551 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2552 * But make sure we scroll at least one line (happens with mix of long
2553 * wrapping lines and non-wrapping line).
2555 if (retval
== OK
&& dir
== FORWARD
&& check_top_offset())
2557 scroll_cursor_top(1, FALSE
);
2558 if (curwin
->w_topline
<= old_topline
2559 && old_topline
< curbuf
->b_ml
.ml_line_count
)
2561 curwin
->w_topline
= old_topline
+ 1;
2563 (void)hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
2568 redraw_later(VALID
);
2573 * Decide how much overlap to use for page-up or page-down scrolling.
2574 * This is symmetric, so that doing both keeps the same lines displayed.
2575 * Three lines are examined:
2577 * before CTRL-F after CTRL-F / before CTRL-B
2579 * l1 last but one line ------------
2580 * l2 last text line l2 top text line
2581 * ------------- l3 second text line
2585 get_scroll_overlap(lp
, dir
)
2590 int min_height
= curwin
->w_height
- 2;
2591 lineoff_T loff0
, loff1
, loff2
;
2597 lp
->height
= plines_nofill(lp
->lnum
);
2599 lp
->height
= plines(lp
->lnum
);
2602 if (h1
> min_height
)
2603 return; /* no overlap */
2611 if (h2
+ h1
> min_height
)
2613 *lp
= loff0
; /* no overlap */
2623 if (h3
+ h2
> min_height
)
2625 *lp
= loff0
; /* no overlap */
2635 if (h4
+ h3
+ h2
> min_height
|| h3
+ h2
+ h1
> min_height
)
2636 *lp
= loff1
; /* 1 line overlap */
2638 *lp
= loff2
; /* 2 lines overlap */
2642 /* #define KEEP_SCREEN_LINE */
2644 * Scroll 'scroll' lines up or down.
2647 halfpage(flag
, Prenum
)
2657 curwin
->w_p_scr
= (Prenum
> curwin
->w_height
) ?
2658 curwin
->w_height
: Prenum
;
2659 n
= (curwin
->w_p_scr
<= curwin
->w_height
) ?
2660 curwin
->w_p_scr
: curwin
->w_height
;
2663 room
= curwin
->w_empty_rows
;
2665 room
+= curwin
->w_filler_rows
;
2670 * scroll the text up
2672 while (n
> 0 && curwin
->w_botline
<= curbuf
->b_ml
.ml_line_count
)
2675 if (curwin
->w_topfill
> 0)
2678 if (--n
< 0 && scrolled
> 0)
2680 --curwin
->w_topfill
;
2686 i
= plines_nofill(curwin
->w_topline
);
2688 i
= plines(curwin
->w_topline
);
2691 if (n
< 0 && scrolled
> 0)
2694 (void)hasFolding(curwin
->w_topline
, NULL
, &curwin
->w_topline
);
2696 ++curwin
->w_topline
;
2698 curwin
->w_topfill
= diff_check_fill(curwin
, curwin
->w_topline
);
2701 #ifndef KEEP_SCREEN_LINE
2702 if (curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
2704 ++curwin
->w_cursor
.lnum
;
2706 ~(VALID_VIRTCOL
|VALID_CHEIGHT
|VALID_WCOL
);
2710 curwin
->w_valid
&= ~(VALID_CROW
|VALID_WROW
);
2714 * Correct w_botline for changed w_topline.
2715 * Won't work when there are filler lines.
2718 if (curwin
->w_p_diff
)
2719 curwin
->w_valid
&= ~(VALID_BOTLINE
|VALID_BOTLINE_AP
);
2726 i
= plines(curwin
->w_botline
);
2730 (void)hasFolding(curwin
->w_botline
, NULL
,
2731 &curwin
->w_botline
);
2733 ++curwin
->w_botline
;
2735 } while (curwin
->w_botline
<= curbuf
->b_ml
.ml_line_count
);
2739 #ifndef KEEP_SCREEN_LINE
2741 * When hit bottom of the file: move cursor down.
2745 # ifdef FEAT_FOLDING
2746 if (hasAnyFolding(curwin
))
2749 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
2751 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
2752 &curwin
->w_cursor
.lnum
);
2753 ++curwin
->w_cursor
.lnum
;
2758 curwin
->w_cursor
.lnum
+= n
;
2759 check_cursor_lnum();
2762 /* try to put the cursor in the same screen line */
2763 while ((curwin
->w_cursor
.lnum
< curwin
->w_topline
|| scrolled
> 0)
2764 && curwin
->w_cursor
.lnum
< curwin
->w_botline
- 1)
2766 scrolled
-= plines(curwin
->w_cursor
.lnum
);
2767 if (scrolled
< 0 && curwin
->w_cursor
.lnum
>= curwin
->w_topline
)
2769 # ifdef FEAT_FOLDING
2770 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
2771 &curwin
->w_cursor
.lnum
);
2773 ++curwin
->w_cursor
.lnum
;
2780 * scroll the text down
2782 while (n
> 0 && curwin
->w_topline
> 1)
2785 if (curwin
->w_topfill
< diff_check_fill(curwin
, curwin
->w_topline
))
2788 if (--n
< 0 && scrolled
> 0)
2790 ++curwin
->w_topfill
;
2796 i
= plines_nofill(curwin
->w_topline
- 1);
2798 i
= plines(curwin
->w_topline
- 1);
2801 if (n
< 0 && scrolled
> 0)
2803 --curwin
->w_topline
;
2805 (void)hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
2808 curwin
->w_topfill
= 0;
2811 curwin
->w_valid
&= ~(VALID_CROW
|VALID_WROW
|
2812 VALID_BOTLINE
|VALID_BOTLINE_AP
);
2814 #ifndef KEEP_SCREEN_LINE
2815 if (curwin
->w_cursor
.lnum
> 1)
2817 --curwin
->w_cursor
.lnum
;
2818 curwin
->w_valid
&= ~(VALID_VIRTCOL
|VALID_CHEIGHT
|VALID_WCOL
);
2822 #ifndef KEEP_SCREEN_LINE
2824 * When hit top of the file: move cursor up.
2828 if (curwin
->w_cursor
.lnum
<= (linenr_T
)n
)
2829 curwin
->w_cursor
.lnum
= 1;
2831 # ifdef FEAT_FOLDING
2832 if (hasAnyFolding(curwin
))
2834 while (--n
>= 0 && curwin
->w_cursor
.lnum
> 1)
2836 --curwin
->w_cursor
.lnum
;
2837 (void)hasFolding(curwin
->w_cursor
.lnum
,
2838 &curwin
->w_cursor
.lnum
, NULL
);
2843 curwin
->w_cursor
.lnum
-= n
;
2846 /* try to put the cursor in the same screen line */
2847 scrolled
+= n
; /* move cursor when topline is 1 */
2848 while (curwin
->w_cursor
.lnum
> curwin
->w_topline
2849 && (scrolled
> 0 || curwin
->w_cursor
.lnum
>= curwin
->w_botline
))
2851 scrolled
-= plines(curwin
->w_cursor
.lnum
- 1);
2852 if (scrolled
< 0 && curwin
->w_cursor
.lnum
< curwin
->w_botline
)
2854 --curwin
->w_cursor
.lnum
;
2855 # ifdef FEAT_FOLDING
2861 # ifdef FEAT_FOLDING
2862 /* Move cursor to first line of closed fold. */
2866 check_topfill(curwin
, !flag
);
2869 beginline(BL_SOL
| BL_FIX
);
2870 redraw_later(VALID
);