Merge branch 'vim'
[MacVim.git] / src / move.c
blob17b16a1e89fd2b651e0e260df1c3596d5eb361c6
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.
8 */
9 /*
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
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
20 #include "vim.h"
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));
29 typedef struct
31 linenr_T lnum; /* line number */
32 #ifdef FEAT_DIFF
33 int fill; /* filler lines */
34 #endif
35 int height; /* height of added line */
36 } lineoff_T;
38 static void topline_back __ARGS((lineoff_T *lp));
39 static void botline_forw __ARGS((lineoff_T *lp));
40 #ifdef FEAT_DIFF
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));
44 #endif
47 * Compute wp->w_botline for the current wp->w_topline. Can be called after
48 * wp->w_topline changed.
50 static void
51 comp_botline(wp)
52 win_T *wp;
54 int n;
55 linenr_T lnum;
56 int done;
57 #ifdef FEAT_FOLDING
58 linenr_T last;
59 int folded;
60 #endif
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;
72 else
74 lnum = wp->w_topline;
75 done = 0;
78 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
80 #ifdef FEAT_FOLDING
81 last = lnum;
82 folded = FALSE;
83 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
85 n = 1;
86 folded = TRUE;
88 else
89 #endif
90 #ifdef FEAT_DIFF
91 if (lnum == wp->w_topline)
92 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
93 else
94 #endif
95 n = plines_win(wp, lnum, TRUE);
96 if (
97 #ifdef FEAT_FOLDING
98 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
99 #else
100 lnum == wp->w_cursor.lnum
101 #endif
104 wp->w_cline_row = done;
105 wp->w_cline_height = n;
106 #ifdef FEAT_FOLDING
107 wp->w_cline_folded = folded;
108 #endif
109 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
111 if (done + n > wp->w_height)
112 break;
113 done += n;
114 #ifdef FEAT_FOLDING
115 lnum = last;
116 #endif
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.
130 void
131 update_topline_redraw()
133 update_topline();
134 if (must_redraw)
135 update_screen(0);
139 * Update curwin->w_topline to move the cursor onto the screen.
141 void
142 update_topline()
144 long line_count;
145 int halfheight;
146 int n;
147 linenr_T old_topline;
148 #ifdef FEAT_DIFF
149 int old_topfill;
150 #endif
151 #ifdef FEAT_FOLDING
152 linenr_T lnum;
153 #endif
154 int check_topline = FALSE;
155 int check_botline = FALSE;
156 #ifdef FEAT_MOUSE
157 int save_so = p_so;
158 #endif
160 if (!screen_valid(TRUE))
161 return;
163 check_cursor_moved(curwin);
164 if (curwin->w_valid & VALID_TOPLINE)
165 return;
167 #ifdef FEAT_MOUSE
168 /* When dragging with the mouse, don't scroll that quickly */
169 if (mouse_dragging > 0)
170 p_so = mouse_dragging - 1;
171 #endif
173 old_topline = curwin->w_topline;
174 #ifdef FEAT_DIFF
175 old_topfill = curwin->w_topfill;
176 #endif
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 #ifdef FEAT_DIFF
187 curwin->w_topfill = 0;
188 #endif
189 curwin->w_botline = 2;
190 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
191 #ifdef FEAT_SCROLLBIND
192 curwin->w_scbind_pos = 1;
193 #endif
197 * If the cursor is above or near the top of the window, scroll the window
198 * to show the line the cursor is in, with 'scrolloff' context.
200 else
202 if (curwin->w_topline > 1)
204 /* If the cursor is above topline, scrolling is always needed.
205 * If the cursor is far below topline and there is no folding,
206 * scrolling down is never needed. */
207 if (curwin->w_cursor.lnum < curwin->w_topline)
208 check_topline = TRUE;
209 else if (check_top_offset())
210 check_topline = TRUE;
212 #ifdef FEAT_DIFF
213 /* Check if there are more filler lines than allowed. */
214 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
215 curwin->w_topline))
216 check_topline = TRUE;
217 #endif
219 if (check_topline)
221 halfheight = curwin->w_height / 2 - 1;
222 if (halfheight < 2)
223 halfheight = 2;
225 #ifdef FEAT_FOLDING
226 if (hasAnyFolding(curwin))
228 /* Count the number of logical lines between the cursor and
229 * topline + p_so (approximation of how much will be
230 * scrolled). */
231 n = 0;
232 for (lnum = curwin->w_cursor.lnum;
233 lnum < curwin->w_topline + p_so; ++lnum)
235 ++n;
236 /* stop at end of file or when we know we are far off */
237 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
238 break;
239 (void)hasFolding(lnum, NULL, &lnum);
242 else
243 #endif
244 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
246 /* If we weren't very close to begin with, we scroll to put the
247 * cursor in the middle of the window. Otherwise put the cursor
248 * near the top of the window. */
249 if (n >= halfheight)
250 scroll_cursor_halfway(FALSE);
251 else
253 scroll_cursor_top(scrolljump_value(), FALSE);
254 check_botline = TRUE;
258 else
260 #ifdef FEAT_FOLDING
261 /* Make sure topline is the first line of a fold. */
262 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
263 #endif
264 check_botline = TRUE;
269 * If the cursor is below the bottom of the window, scroll the window
270 * to put the cursor on the window.
271 * When w_botline is invalid, recompute it first, to avoid a redraw later.
272 * If w_botline was approximated, we might need a redraw later in a few
273 * cases, but we don't want to spend (a lot of) time recomputing w_botline
274 * for every small change.
276 if (check_botline)
278 if (!(curwin->w_valid & VALID_BOTLINE_AP))
279 validate_botline();
281 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
283 if (curwin->w_cursor.lnum < curwin->w_botline)
285 if (((long)curwin->w_cursor.lnum
286 >= (long)curwin->w_botline - p_so
287 #ifdef FEAT_FOLDING
288 || hasAnyFolding(curwin)
289 #endif
292 lineoff_T loff;
294 /* Cursor is (a few lines) above botline, check if there are
295 * 'scrolloff' window lines below the cursor. If not, need to
296 * scroll. */
297 n = curwin->w_empty_rows;
298 loff.lnum = curwin->w_cursor.lnum;
299 #ifdef FEAT_FOLDING
300 /* In a fold go to its last line. */
301 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
302 #endif
303 #ifdef FEAT_DIFF
304 loff.fill = 0;
305 n += curwin->w_filler_rows;
306 #endif
307 loff.height = 0;
308 while (loff.lnum < curwin->w_botline
309 #ifdef FEAT_DIFF
310 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
311 #endif
314 n += loff.height;
315 if (n >= p_so)
316 break;
317 botline_forw(&loff);
319 if (n >= p_so)
320 /* sufficient context, no need to scroll */
321 check_botline = FALSE;
323 else
324 /* sufficient context, no need to scroll */
325 check_botline = FALSE;
327 if (check_botline)
329 #ifdef FEAT_FOLDING
330 if (hasAnyFolding(curwin))
332 /* Count the number of logical lines between the cursor and
333 * botline - p_so (approximation of how much will be
334 * scrolled). */
335 line_count = 0;
336 for (lnum = curwin->w_cursor.lnum;
337 lnum >= curwin->w_botline - p_so; --lnum)
339 ++line_count;
340 /* stop at end of file or when we know we are far off */
341 if (lnum <= 0 || line_count > curwin->w_height + 1)
342 break;
343 (void)hasFolding(lnum, &lnum, NULL);
346 else
347 #endif
348 line_count = curwin->w_cursor.lnum - curwin->w_botline
349 + 1 + p_so;
350 if (line_count <= curwin->w_height + 1)
351 scroll_cursor_bot(scrolljump_value(), FALSE);
352 else
353 scroll_cursor_halfway(FALSE);
357 curwin->w_valid |= VALID_TOPLINE;
360 * Need to redraw when topline changed.
362 if (curwin->w_topline != old_topline
363 #ifdef FEAT_DIFF
364 || curwin->w_topfill != old_topfill
365 #endif
368 dollar_vcol = 0;
369 if (curwin->w_skipcol != 0)
371 curwin->w_skipcol = 0;
372 redraw_later(NOT_VALID);
374 else
375 redraw_later(VALID);
376 /* May need to set w_skipcol when cursor in w_topline. */
377 if (curwin->w_cursor.lnum == curwin->w_topline)
378 validate_cursor();
381 #ifdef FEAT_MOUSE
382 p_so = save_so;
383 #endif
387 * Return the scrolljump value to use for the current window.
388 * When 'scrolljump' is positive use it as-is.
389 * When 'scrolljump' is negative use it as a percentage of the window height.
391 static int
392 scrolljump_value()
394 if (p_sj >= 0)
395 return (int)p_sj;
396 return (curwin->w_height * -p_sj) / 100;
400 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
401 * current window.
403 static int
404 check_top_offset()
406 lineoff_T loff;
407 int n;
409 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
410 #ifdef FEAT_FOLDING
411 || hasAnyFolding(curwin)
412 #endif
415 loff.lnum = curwin->w_cursor.lnum;
416 #ifdef FEAT_DIFF
417 loff.fill = 0;
418 n = curwin->w_topfill; /* always have this context */
419 #else
420 n = 0;
421 #endif
422 /* Count the visible screen lines above the cursor line. */
423 while (n < p_so)
425 topline_back(&loff);
426 /* Stop when included a line above the window. */
427 if (loff.lnum < curwin->w_topline
428 #ifdef FEAT_DIFF
429 || (loff.lnum == curwin->w_topline && loff.fill > 0)
430 #endif
432 break;
433 n += loff.height;
435 if (n < p_so)
436 return TRUE;
438 return FALSE;
441 void
442 update_curswant()
444 if (curwin->w_set_curswant)
446 validate_virtcol();
447 curwin->w_curswant = curwin->w_virtcol;
448 curwin->w_set_curswant = FALSE;
453 * Check if the cursor has moved. Set the w_valid flag accordingly.
455 void
456 check_cursor_moved(wp)
457 win_T *wp;
459 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
461 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
462 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
463 wp->w_valid_cursor = wp->w_cursor;
464 wp->w_valid_leftcol = wp->w_leftcol;
466 else if (wp->w_cursor.col != wp->w_valid_cursor.col
467 || wp->w_leftcol != wp->w_valid_leftcol
468 #ifdef FEAT_VIRTUALEDIT
469 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
470 #endif
473 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
474 wp->w_valid_cursor.col = wp->w_cursor.col;
475 wp->w_valid_leftcol = wp->w_leftcol;
476 #ifdef FEAT_VIRTUALEDIT
477 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
478 #endif
483 * Call this function when some window settings have changed, which require
484 * the cursor position, botline and topline to be recomputed and the window to
485 * be redrawn. E.g, when changing the 'wrap' option or folding.
487 void
488 changed_window_setting()
490 changed_window_setting_win(curwin);
493 void
494 changed_window_setting_win(wp)
495 win_T *wp;
497 wp->w_lines_valid = 0;
498 changed_line_abv_curs_win(wp);
499 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
500 redraw_win_later(wp, NOT_VALID);
504 * Set wp->w_topline to a certain number.
506 void
507 set_topline(wp, lnum)
508 win_T *wp;
509 linenr_T lnum;
511 #ifdef FEAT_FOLDING
512 /* go to first of folded lines */
513 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
514 #endif
515 /* Approximate the value of w_botline */
516 wp->w_botline += lnum - wp->w_topline;
517 wp->w_topline = lnum;
518 #ifdef FEAT_AUTOCMD
519 wp->w_topline_was_set = TRUE;
520 #endif
521 #ifdef FEAT_DIFF
522 wp->w_topfill = 0;
523 #endif
524 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
525 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
526 redraw_later(VALID);
530 * Call this function when the length of the cursor line (in screen
531 * characters) has changed, and the change is before the cursor.
532 * Need to take care of w_botline separately!
534 void
535 changed_cline_bef_curs()
537 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
538 |VALID_CHEIGHT|VALID_TOPLINE);
541 void
542 changed_cline_bef_curs_win(wp)
543 win_T *wp;
545 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
546 |VALID_CHEIGHT|VALID_TOPLINE);
549 #if 0 /* not used */
551 * Call this function when the length of the cursor line (in screen
552 * characters) has changed, and the position of the cursor doesn't change.
553 * Need to take care of w_botline separately!
555 void
556 changed_cline_aft_curs()
558 curwin->w_valid &= ~VALID_CHEIGHT;
560 #endif
563 * Call this function when the length of a line (in screen characters) above
564 * the cursor have changed.
565 * Need to take care of w_botline separately!
567 void
568 changed_line_abv_curs()
570 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
571 |VALID_CHEIGHT|VALID_TOPLINE);
574 void
575 changed_line_abv_curs_win(wp)
576 win_T *wp;
578 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
579 |VALID_CHEIGHT|VALID_TOPLINE);
583 * Make sure the value of curwin->w_botline is valid.
585 void
586 validate_botline()
588 if (!(curwin->w_valid & VALID_BOTLINE))
589 comp_botline(curwin);
593 * Make sure the value of wp->w_botline is valid.
595 static void
596 validate_botline_win(wp)
597 win_T *wp;
599 if (!(wp->w_valid & VALID_BOTLINE))
600 comp_botline(wp);
604 * Mark curwin->w_botline as invalid (because of some change in the buffer).
606 void
607 invalidate_botline()
609 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
612 void
613 invalidate_botline_win(wp)
614 win_T *wp;
616 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
619 #if 0 /* never used */
621 * Mark curwin->w_botline as approximated (because of some small change in the
622 * buffer).
624 void
625 approximate_botline()
627 curwin->w_valid &= ~VALID_BOTLINE;
629 #endif
631 void
632 approximate_botline_win(wp)
633 win_T *wp;
635 wp->w_valid &= ~VALID_BOTLINE;
638 #if 0 /* not used */
640 * Return TRUE if curwin->w_botline is valid.
643 botline_valid()
645 return (curwin->w_valid & VALID_BOTLINE);
647 #endif
649 #if 0 /* not used */
651 * Return TRUE if curwin->w_botline is valid or approximated.
654 botline_approximated()
656 return (curwin->w_valid & VALID_BOTLINE_AP);
658 #endif
661 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
664 cursor_valid()
666 check_cursor_moved(curwin);
667 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
668 (VALID_WROW|VALID_WCOL));
672 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
673 * w_topline must be valid, you may need to call update_topline() first!
675 void
676 validate_cursor()
678 check_cursor_moved(curwin);
679 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
680 curs_columns(TRUE);
683 #if defined(FEAT_GUI) || defined(PROTO)
685 * validate w_cline_row.
687 void
688 validate_cline_row()
691 * First make sure that w_topline is valid (after moving the cursor).
693 update_topline();
694 check_cursor_moved(curwin);
695 if (!(curwin->w_valid & VALID_CROW))
696 curs_rows(curwin, FALSE);
698 #endif
701 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
702 * of wp->w_topine.
704 * Returns OK when cursor is in the window, FAIL when it isn't.
706 static void
707 curs_rows(wp, do_botline)
708 win_T *wp;
709 int do_botline; /* also compute w_botline */
711 linenr_T lnum;
712 int i;
713 int all_invalid;
714 int valid;
715 #ifdef FEAT_FOLDING
716 long fold_count;
717 #endif
719 /* Check if wp->w_lines[].wl_size is invalid */
720 all_invalid = (!redrawing()
721 || wp->w_lines_valid == 0
722 || wp->w_lines[0].wl_lnum > wp->w_topline);
723 i = 0;
724 wp->w_cline_row = 0;
725 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
727 valid = FALSE;
728 if (!all_invalid && i < wp->w_lines_valid)
730 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
731 continue; /* skip changed or deleted lines */
732 if (wp->w_lines[i].wl_lnum == lnum)
734 #ifdef FEAT_FOLDING
735 /* Check for newly inserted lines below this row, in which
736 * case we need to check for folded lines. */
737 if (!wp->w_buffer->b_mod_set
738 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
739 || wp->w_buffer->b_mod_top
740 > wp->w_lines[i].wl_lastlnum + 1)
741 #endif
742 valid = TRUE;
744 else if (wp->w_lines[i].wl_lnum > lnum)
745 --i; /* hold at inserted lines */
747 if (valid
748 #ifdef FEAT_DIFF
749 && (lnum != wp->w_topline || !wp->w_p_diff)
750 #endif
753 #ifdef FEAT_FOLDING
754 lnum = wp->w_lines[i].wl_lastlnum + 1;
755 /* Cursor inside folded lines, don't count this row */
756 if (lnum > wp->w_cursor.lnum)
757 break;
758 #else
759 ++lnum;
760 #endif
761 wp->w_cline_row += wp->w_lines[i].wl_size;
763 else
765 #ifdef FEAT_FOLDING
766 fold_count = foldedCount(wp, lnum, NULL);
767 if (fold_count)
769 lnum += fold_count;
770 if (lnum > wp->w_cursor.lnum)
771 break;
772 ++wp->w_cline_row;
774 else
775 #endif
776 #ifdef FEAT_DIFF
777 if (lnum == wp->w_topline)
778 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
779 + wp->w_topfill;
780 else
781 #endif
782 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
786 check_cursor_moved(wp);
787 if (!(wp->w_valid & VALID_CHEIGHT))
789 if (all_invalid
790 || i == wp->w_lines_valid
791 || (i < wp->w_lines_valid
792 && (!wp->w_lines[i].wl_valid
793 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
795 #ifdef FEAT_DIFF
796 if (wp->w_cursor.lnum == wp->w_topline)
797 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
798 TRUE) + wp->w_topfill;
799 else
800 #endif
801 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
802 #ifdef FEAT_FOLDING
803 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
804 NULL, NULL, TRUE, NULL);
805 #endif
807 else if (i > wp->w_lines_valid)
809 /* a line that is too long to fit on the last screen line */
810 wp->w_cline_height = 0;
811 #ifdef FEAT_FOLDING
812 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
813 NULL, NULL, TRUE, NULL);
814 #endif
816 else
818 wp->w_cline_height = wp->w_lines[i].wl_size;
819 #ifdef FEAT_FOLDING
820 wp->w_cline_folded = wp->w_lines[i].wl_folded;
821 #endif
825 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
827 /* validate botline too, if update_screen doesn't do it */
828 if (do_botline && all_invalid)
829 validate_botline_win(wp);
833 * Validate curwin->w_virtcol only.
835 void
836 validate_virtcol()
838 validate_virtcol_win(curwin);
842 * Validate wp->w_virtcol only.
844 void
845 validate_virtcol_win(wp)
846 win_T *wp;
848 check_cursor_moved(wp);
849 if (!(wp->w_valid & VALID_VIRTCOL))
851 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
852 wp->w_valid |= VALID_VIRTCOL;
853 #ifdef FEAT_SYN_HL
854 if (wp->w_p_cuc
855 # ifdef FEAT_INS_EXPAND
856 && !pum_visible()
857 # endif
859 redraw_win_later(wp, SOME_VALID);
860 #endif
865 * Validate curwin->w_cline_height only.
867 static void
868 validate_cheight()
870 check_cursor_moved(curwin);
871 if (!(curwin->w_valid & VALID_CHEIGHT))
873 #ifdef FEAT_DIFF
874 if (curwin->w_cursor.lnum == curwin->w_topline)
875 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
876 + curwin->w_topfill;
877 else
878 #endif
879 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
880 #ifdef FEAT_FOLDING
881 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
882 #endif
883 curwin->w_valid |= VALID_CHEIGHT;
888 * Validate w_wcol and w_virtcol only.
890 void
891 validate_cursor_col()
893 colnr_T off;
894 colnr_T col;
896 validate_virtcol();
897 if (!(curwin->w_valid & VALID_WCOL))
899 col = curwin->w_virtcol;
900 off = curwin_col_off();
901 col += off;
903 /* long line wrapping, adjust curwin->w_wrow */
904 if (curwin->w_p_wrap
905 && col >= (colnr_T)W_WIDTH(curwin)
906 && W_WIDTH(curwin) - off + curwin_col_off2() > 0)
908 col -= W_WIDTH(curwin);
909 col = col % (W_WIDTH(curwin) - off + curwin_col_off2());
911 if (col > (int)curwin->w_leftcol)
912 col -= curwin->w_leftcol;
913 else
914 col = 0;
915 curwin->w_wcol = col;
917 curwin->w_valid |= VALID_WCOL;
922 * Compute offset of a window, occupied by line number, fold column and sign
923 * column (these don't move when scrolling horizontally).
926 win_col_off(wp)
927 win_T *wp;
929 return ((wp->w_p_nu ? number_width(wp) + 1 : 0)
930 #ifdef FEAT_CMDWIN
931 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
932 #endif
933 #ifdef FEAT_FOLDING
934 + wp->w_p_fdc
935 #endif
936 #ifdef FEAT_SIGNS
938 # ifdef FEAT_NETBEANS_INTG
939 /* always show glyph gutter in netbeans */
940 usingNetbeans ||
941 # endif
942 wp->w_buffer->b_signlist != NULL ? 2 : 0)
943 #endif
948 curwin_col_off()
950 return win_col_off(curwin);
954 * Return the difference in column offset for the second screen line of a
955 * wrapped line. It's 8 if 'number' is on and 'n' is in 'cpoptions'.
958 win_col_off2(wp)
959 win_T *wp;
961 if (wp->w_p_nu && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
962 return number_width(wp) + 1;
963 return 0;
967 curwin_col_off2()
969 return win_col_off2(curwin);
973 * compute curwin->w_wcol and curwin->w_virtcol.
974 * Also updates curwin->w_wrow and curwin->w_cline_row.
975 * Also updates curwin->w_leftcol.
977 void
978 curs_columns(scroll)
979 int scroll; /* when TRUE, may scroll horizontally */
981 int diff;
982 int extra; /* offset for first screen line */
983 int off_left, off_right;
984 int n;
985 int p_lines;
986 int width = 0;
987 int textwidth;
988 int new_leftcol;
989 colnr_T startcol;
990 colnr_T endcol;
991 colnr_T prev_skipcol;
994 * First make sure that w_topline is valid (after moving the cursor).
996 update_topline();
999 * Next make sure that w_cline_row is valid.
1001 if (!(curwin->w_valid & VALID_CROW))
1002 curs_rows(curwin, FALSE);
1005 * Compute the number of virtual columns.
1007 #ifdef FEAT_FOLDING
1008 if (curwin->w_cline_folded)
1009 /* In a folded line the cursor is always in the first column */
1010 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1011 else
1012 #endif
1013 getvvcol(curwin, &curwin->w_cursor,
1014 &startcol, &(curwin->w_virtcol), &endcol);
1016 /* remove '$' from change command when cursor moves onto it */
1017 if (startcol > dollar_vcol)
1018 dollar_vcol = 0;
1020 extra = curwin_col_off();
1021 curwin->w_wcol = curwin->w_virtcol + extra;
1022 endcol += extra;
1025 * Now compute w_wrow, counting screen lines from w_cline_row.
1027 curwin->w_wrow = curwin->w_cline_row;
1029 textwidth = W_WIDTH(curwin) - extra;
1030 if (textwidth <= 0)
1032 /* No room for text, put cursor in last char of window. */
1033 curwin->w_wcol = W_WIDTH(curwin) - 1;
1034 curwin->w_wrow = curwin->w_height - 1;
1036 else if (curwin->w_p_wrap
1037 #ifdef FEAT_VERTSPLIT
1038 && curwin->w_width != 0
1039 #endif
1042 width = textwidth + curwin_col_off2();
1044 /* long line wrapping, adjust curwin->w_wrow */
1045 if (curwin->w_wcol >= W_WIDTH(curwin))
1047 n = (curwin->w_wcol - W_WIDTH(curwin)) / width + 1;
1048 curwin->w_wcol -= n * width;
1049 curwin->w_wrow += n;
1051 #ifdef FEAT_LINEBREAK
1052 /* When cursor wraps to first char of next line in Insert
1053 * mode, the 'showbreak' string isn't shown, backup to first
1054 * column */
1055 if (*p_sbr && *ml_get_cursor() == NUL
1056 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1057 curwin->w_wcol = 0;
1058 #endif
1062 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1063 * is not folded.
1064 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
1065 else if (scroll
1066 #ifdef FEAT_FOLDING
1067 && !curwin->w_cline_folded
1068 #endif
1072 * If Cursor is left of the screen, scroll rightwards.
1073 * If Cursor is right of the screen, scroll leftwards
1074 * If we get closer to the edge than 'sidescrolloff', scroll a little
1075 * extra
1077 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
1078 off_right = (int)endcol - (int)(curwin->w_leftcol + W_WIDTH(curwin)
1079 - p_siso) + 1;
1080 if (off_left < 0 || off_right > 0)
1082 if (off_left < 0)
1083 diff = -off_left;
1084 else
1085 diff = off_right;
1087 /* When far off or not enough room on either side, put cursor in
1088 * middle of window. */
1089 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1090 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1091 else
1093 if (diff < p_ss)
1094 diff = p_ss;
1095 if (off_left < 0)
1096 new_leftcol = curwin->w_leftcol - diff;
1097 else
1098 new_leftcol = curwin->w_leftcol + diff;
1100 if (new_leftcol < 0)
1101 new_leftcol = 0;
1102 if (new_leftcol != (int)curwin->w_leftcol)
1104 curwin->w_leftcol = new_leftcol;
1105 /* screen has to be redrawn with new curwin->w_leftcol */
1106 redraw_later(NOT_VALID);
1109 curwin->w_wcol -= curwin->w_leftcol;
1111 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1112 curwin->w_wcol -= curwin->w_leftcol;
1113 else
1114 curwin->w_wcol = 0;
1116 #ifdef FEAT_DIFF
1117 /* Skip over filler lines. At the top use w_topfill, there
1118 * may be some filler lines above the window. */
1119 if (curwin->w_cursor.lnum == curwin->w_topline)
1120 curwin->w_wrow += curwin->w_topfill;
1121 else
1122 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1123 #endif
1125 prev_skipcol = curwin->w_skipcol;
1127 p_lines = 0;
1128 if ((curwin->w_wrow >= curwin->w_height
1129 || ((prev_skipcol > 0
1130 || curwin->w_wrow + p_so >= curwin->w_height)
1131 && (p_lines =
1132 #ifdef FEAT_DIFF
1133 plines_win_nofill
1134 #else
1135 plines_win
1136 #endif
1137 (curwin, curwin->w_cursor.lnum, FALSE))
1138 - 1 >= curwin->w_height))
1139 && curwin->w_height != 0
1140 && curwin->w_cursor.lnum == curwin->w_topline
1141 && width > 0
1142 #ifdef FEAT_VERTSPLIT
1143 && curwin->w_width != 0
1144 #endif
1147 /* Cursor past end of screen. Happens with a single line that does
1148 * not fit on screen. Find a skipcol to show the text around the
1149 * cursor. Avoid scrolling all the time. compute value of "extra":
1150 * 1: Less than "p_so" lines above
1151 * 2: Less than "p_so" lines below
1152 * 3: both of them */
1153 extra = 0;
1154 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1155 extra = 1;
1156 /* Compute last display line of the buffer line that we want at the
1157 * bottom of the window. */
1158 if (p_lines == 0)
1159 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1160 --p_lines;
1161 if (p_lines > curwin->w_wrow + p_so)
1162 n = curwin->w_wrow + p_so;
1163 else
1164 n = p_lines;
1165 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1166 extra += 2;
1168 if (extra == 3 || p_lines < p_so * 2)
1170 /* not enough room for 'scrolloff', put cursor in the middle */
1171 n = curwin->w_virtcol / width;
1172 if (n > curwin->w_height / 2)
1173 n -= curwin->w_height / 2;
1174 else
1175 n = 0;
1176 /* don't skip more than necessary */
1177 if (n > p_lines - curwin->w_height + 1)
1178 n = p_lines - curwin->w_height + 1;
1179 curwin->w_skipcol = n * width;
1181 else if (extra == 1)
1183 /* less then 'scrolloff' lines above, decrease skipcol */
1184 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1185 + width - 1) / width;
1186 if (extra > 0)
1188 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1189 extra = curwin->w_skipcol / width;
1190 curwin->w_skipcol -= extra * width;
1193 else if (extra == 2)
1195 /* less then 'scrolloff' lines below, increase skipcol */
1196 endcol = (n - curwin->w_height + 1) * width;
1197 while (endcol > curwin->w_virtcol)
1198 endcol -= width;
1199 if (endcol > curwin->w_skipcol)
1200 curwin->w_skipcol = endcol;
1203 curwin->w_wrow -= curwin->w_skipcol / width;
1204 if (curwin->w_wrow >= curwin->w_height)
1206 /* small window, make sure cursor is in it */
1207 extra = curwin->w_wrow - curwin->w_height + 1;
1208 curwin->w_skipcol += extra * width;
1209 curwin->w_wrow -= extra;
1212 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1213 if (extra > 0)
1214 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1215 else if (extra < 0)
1216 win_del_lines(curwin, 0, -extra, FALSE, FALSE);
1218 else
1219 curwin->w_skipcol = 0;
1220 if (prev_skipcol != curwin->w_skipcol)
1221 redraw_later(NOT_VALID);
1223 #ifdef FEAT_SYN_HL
1224 /* Redraw when w_virtcol changes and 'cursorcolumn' is set, or when w_row
1225 * changes and 'cursorline' is set. */
1226 if (((curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0)
1227 || (curwin->w_p_cul && (curwin->w_valid & VALID_WROW) == 0))
1228 # ifdef FEAT_INS_EXPAND
1229 && !pum_visible()
1230 # endif
1232 redraw_later(SOME_VALID);
1233 #endif
1235 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1239 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1241 /*ARGSUSED*/
1242 void
1243 scrolldown(line_count, byfold)
1244 long line_count;
1245 int byfold; /* TRUE: count a closed fold as one line */
1247 long done = 0; /* total # of physical lines done */
1248 int wrow;
1249 int moved = FALSE;
1251 #ifdef FEAT_FOLDING
1252 linenr_T first;
1254 /* Make sure w_topline is at the first of a sequence of folded lines. */
1255 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1256 #endif
1257 validate_cursor(); /* w_wrow needs to be valid */
1258 while (line_count-- > 0)
1260 #ifdef FEAT_DIFF
1261 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1263 ++curwin->w_topfill;
1264 ++done;
1266 else
1267 #endif
1269 if (curwin->w_topline == 1)
1270 break;
1271 --curwin->w_topline;
1272 #ifdef FEAT_DIFF
1273 curwin->w_topfill = 0;
1274 #endif
1275 #ifdef FEAT_FOLDING
1276 /* A sequence of folded lines only counts for one logical line */
1277 if (hasFolding(curwin->w_topline, &first, NULL))
1279 ++done;
1280 if (!byfold)
1281 line_count -= curwin->w_topline - first - 1;
1282 curwin->w_botline -= curwin->w_topline - first;
1283 curwin->w_topline = first;
1285 else
1286 #endif
1287 #ifdef FEAT_DIFF
1288 done += plines_nofill(curwin->w_topline);
1289 #else
1290 done += plines(curwin->w_topline);
1291 #endif
1293 --curwin->w_botline; /* approximate w_botline */
1294 invalidate_botline();
1296 curwin->w_wrow += done; /* keep w_wrow updated */
1297 curwin->w_cline_row += done; /* keep w_cline_row updated */
1299 #ifdef FEAT_DIFF
1300 if (curwin->w_cursor.lnum == curwin->w_topline)
1301 curwin->w_cline_row = 0;
1302 check_topfill(curwin, TRUE);
1303 #endif
1306 * Compute the row number of the last row of the cursor line
1307 * and move the cursor onto the displayed part of the window.
1309 wrow = curwin->w_wrow;
1310 if (curwin->w_p_wrap
1311 #ifdef FEAT_VERTSPLIT
1312 && curwin->w_width != 0
1313 #endif
1316 validate_virtcol();
1317 validate_cheight();
1318 wrow += curwin->w_cline_height - 1 -
1319 curwin->w_virtcol / W_WIDTH(curwin);
1321 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1323 #ifdef FEAT_FOLDING
1324 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1326 --wrow;
1327 if (first == 1)
1328 curwin->w_cursor.lnum = 1;
1329 else
1330 curwin->w_cursor.lnum = first - 1;
1332 else
1333 #endif
1334 wrow -= plines(curwin->w_cursor.lnum--);
1335 curwin->w_valid &=
1336 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1337 moved = TRUE;
1339 if (moved)
1341 #ifdef FEAT_FOLDING
1342 /* Move cursor to first line of closed fold. */
1343 foldAdjustCursor();
1344 #endif
1345 coladvance(curwin->w_curswant);
1350 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1352 /*ARGSUSED*/
1353 void
1354 scrollup(line_count, byfold)
1355 long line_count;
1356 int byfold; /* TRUE: count a closed fold as one line */
1358 #if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1359 linenr_T lnum;
1361 if (
1362 # ifdef FEAT_FOLDING
1363 (byfold && hasAnyFolding(curwin))
1364 # ifdef FEAT_DIFF
1366 # endif
1367 # endif
1368 # ifdef FEAT_DIFF
1369 curwin->w_p_diff
1370 # endif
1373 /* count each sequence of folded lines as one logical line */
1374 lnum = curwin->w_topline;
1375 while (line_count--)
1377 # ifdef FEAT_DIFF
1378 if (curwin->w_topfill > 0)
1379 --curwin->w_topfill;
1380 else
1381 # endif
1383 # ifdef FEAT_FOLDING
1384 if (byfold)
1385 (void)hasFolding(lnum, NULL, &lnum);
1386 # endif
1387 if (lnum >= curbuf->b_ml.ml_line_count)
1388 break;
1389 ++lnum;
1390 # ifdef FEAT_DIFF
1391 curwin->w_topfill = diff_check_fill(curwin, lnum);
1392 # endif
1395 /* approximate w_botline */
1396 curwin->w_botline += lnum - curwin->w_topline;
1397 curwin->w_topline = lnum;
1399 else
1400 #endif
1402 curwin->w_topline += line_count;
1403 curwin->w_botline += line_count; /* approximate w_botline */
1406 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1407 curwin->w_topline = curbuf->b_ml.ml_line_count;
1408 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1409 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1411 #ifdef FEAT_DIFF
1412 check_topfill(curwin, FALSE);
1413 #endif
1415 #ifdef FEAT_FOLDING
1416 if (hasAnyFolding(curwin))
1417 /* Make sure w_topline is at the first of a sequence of folded lines. */
1418 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1419 #endif
1421 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1422 if (curwin->w_cursor.lnum < curwin->w_topline)
1424 curwin->w_cursor.lnum = curwin->w_topline;
1425 curwin->w_valid &=
1426 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1427 coladvance(curwin->w_curswant);
1431 #ifdef FEAT_DIFF
1433 * Don't end up with too many filler lines in the window.
1435 void
1436 check_topfill(wp, down)
1437 win_T *wp;
1438 int down; /* when TRUE scroll down when not enough space */
1440 int n;
1442 if (wp->w_topfill > 0)
1444 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1445 if (wp->w_topfill + n > wp->w_height)
1447 if (down && wp->w_topline > 1)
1449 --wp->w_topline;
1450 wp->w_topfill = 0;
1452 else
1454 wp->w_topfill = wp->w_height - n;
1455 if (wp->w_topfill < 0)
1456 wp->w_topfill = 0;
1463 * Use as many filler lines as possible for w_topline. Make sure w_topline
1464 * is still visible.
1466 static void
1467 max_topfill()
1469 int n;
1471 n = plines_nofill(curwin->w_topline);
1472 if (n >= curwin->w_height)
1473 curwin->w_topfill = 0;
1474 else
1476 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1477 if (curwin->w_topfill + n > curwin->w_height)
1478 curwin->w_topfill = curwin->w_height - n;
1481 #endif
1483 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1485 * Scroll the screen one line down, but don't do it if it would move the
1486 * cursor off the screen.
1488 void
1489 scrolldown_clamp()
1491 int end_row;
1492 #ifdef FEAT_DIFF
1493 int can_fill = (curwin->w_topfill
1494 < diff_check_fill(curwin, curwin->w_topline));
1495 #endif
1497 if (curwin->w_topline <= 1
1498 #ifdef FEAT_DIFF
1499 && !can_fill
1500 #endif
1502 return;
1504 validate_cursor(); /* w_wrow needs to be valid */
1507 * Compute the row number of the last row of the cursor line
1508 * and make sure it doesn't go off the screen. Make sure the cursor
1509 * doesn't go past 'scrolloff' lines from the screen end.
1511 end_row = curwin->w_wrow;
1512 #ifdef FEAT_DIFF
1513 if (can_fill)
1514 ++end_row;
1515 else
1516 end_row += plines_nofill(curwin->w_topline - 1);
1517 #else
1518 end_row += plines(curwin->w_topline - 1);
1519 #endif
1520 if (curwin->w_p_wrap
1521 #ifdef FEAT_VERTSPLIT
1522 && curwin->w_width != 0
1523 #endif
1526 validate_cheight();
1527 validate_virtcol();
1528 end_row += curwin->w_cline_height - 1 -
1529 curwin->w_virtcol / W_WIDTH(curwin);
1531 if (end_row < curwin->w_height - p_so)
1533 #ifdef FEAT_DIFF
1534 if (can_fill)
1536 ++curwin->w_topfill;
1537 check_topfill(curwin, TRUE);
1539 else
1541 --curwin->w_topline;
1542 curwin->w_topfill = 0;
1544 #else
1545 --curwin->w_topline;
1546 #endif
1547 #ifdef FEAT_FOLDING
1548 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1549 #endif
1550 --curwin->w_botline; /* approximate w_botline */
1551 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1556 * Scroll the screen one line up, but don't do it if it would move the cursor
1557 * off the screen.
1559 void
1560 scrollup_clamp()
1562 int start_row;
1564 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1565 #ifdef FEAT_DIFF
1566 && curwin->w_topfill == 0
1567 #endif
1569 return;
1571 validate_cursor(); /* w_wrow needs to be valid */
1574 * Compute the row number of the first row of the cursor line
1575 * and make sure it doesn't go off the screen. Make sure the cursor
1576 * doesn't go before 'scrolloff' lines from the screen start.
1578 #ifdef FEAT_DIFF
1579 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1580 - curwin->w_topfill;
1581 #else
1582 start_row = curwin->w_wrow - plines(curwin->w_topline);
1583 #endif
1584 if (curwin->w_p_wrap
1585 #ifdef FEAT_VERTSPLIT
1586 && curwin->w_width != 0
1587 #endif
1590 validate_virtcol();
1591 start_row -= curwin->w_virtcol / W_WIDTH(curwin);
1593 if (start_row >= p_so)
1595 #ifdef FEAT_DIFF
1596 if (curwin->w_topfill > 0)
1597 --curwin->w_topfill;
1598 else
1599 #endif
1601 #ifdef FEAT_FOLDING
1602 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1603 #endif
1604 ++curwin->w_topline;
1606 ++curwin->w_botline; /* approximate w_botline */
1607 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1610 #endif /* FEAT_INS_EXPAND */
1613 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1614 * a (wrapped) text line. Uses and sets "lp->fill".
1615 * Returns the height of the added line in "lp->height".
1616 * Lines above the first one are incredibly high.
1618 static void
1619 topline_back(lp)
1620 lineoff_T *lp;
1622 #ifdef FEAT_DIFF
1623 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1625 /* Add a filler line. */
1626 ++lp->fill;
1627 lp->height = 1;
1629 else
1630 #endif
1632 --lp->lnum;
1633 #ifdef FEAT_DIFF
1634 lp->fill = 0;
1635 #endif
1636 if (lp->lnum < 1)
1637 lp->height = MAXCOL;
1638 else
1639 #ifdef FEAT_FOLDING
1640 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1641 /* Add a closed fold */
1642 lp->height = 1;
1643 else
1644 #endif
1646 #ifdef FEAT_DIFF
1647 lp->height = plines_nofill(lp->lnum);
1648 #else
1649 lp->height = plines(lp->lnum);
1650 #endif
1656 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1657 * a (wrapped) text line. Uses and sets "lp->fill".
1658 * Returns the height of the added line in "lp->height".
1659 * Lines below the last one are incredibly high.
1661 static void
1662 botline_forw(lp)
1663 lineoff_T *lp;
1665 #ifdef FEAT_DIFF
1666 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1668 /* Add a filler line. */
1669 ++lp->fill;
1670 lp->height = 1;
1672 else
1673 #endif
1675 ++lp->lnum;
1676 #ifdef FEAT_DIFF
1677 lp->fill = 0;
1678 #endif
1679 if (lp->lnum > curbuf->b_ml.ml_line_count)
1680 lp->height = MAXCOL;
1681 else
1682 #ifdef FEAT_FOLDING
1683 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1684 /* Add a closed fold */
1685 lp->height = 1;
1686 else
1687 #endif
1689 #ifdef FEAT_DIFF
1690 lp->height = plines_nofill(lp->lnum);
1691 #else
1692 lp->height = plines(lp->lnum);
1693 #endif
1698 #ifdef FEAT_DIFF
1700 * Switch from including filler lines below lp->lnum to including filler
1701 * lines above loff.lnum + 1. This keeps pointing to the same line.
1702 * When there are no filler lines nothing changes.
1704 static void
1705 botline_topline(lp)
1706 lineoff_T *lp;
1708 if (lp->fill > 0)
1710 ++lp->lnum;
1711 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1716 * Switch from including filler lines above lp->lnum to including filler
1717 * lines below loff.lnum - 1. This keeps pointing to the same line.
1718 * When there are no filler lines nothing changes.
1720 static void
1721 topline_botline(lp)
1722 lineoff_T *lp;
1724 if (lp->fill > 0)
1726 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1727 --lp->lnum;
1730 #endif
1733 * Recompute topline to put the cursor at the top of the window.
1734 * Scroll at least "min_scroll" lines.
1735 * If "always" is TRUE, always set topline (for "zt").
1737 void
1738 scroll_cursor_top(min_scroll, always)
1739 int min_scroll;
1740 int always;
1742 int scrolled = 0;
1743 int extra = 0;
1744 int used;
1745 int i;
1746 linenr_T top; /* just above displayed lines */
1747 linenr_T bot; /* just below displayed lines */
1748 linenr_T old_topline = curwin->w_topline;
1749 #ifdef FEAT_DIFF
1750 linenr_T old_topfill = curwin->w_topfill;
1751 #endif
1752 linenr_T new_topline;
1753 int off = p_so;
1755 #ifdef FEAT_MOUSE
1756 if (mouse_dragging > 0)
1757 off = mouse_dragging - 1;
1758 #endif
1761 * Decrease topline until:
1762 * - it has become 1
1763 * - (part of) the cursor line is moved off the screen or
1764 * - moved at least 'scrolljump' lines and
1765 * - at least 'scrolloff' lines above and below the cursor
1767 validate_cheight();
1768 used = curwin->w_cline_height;
1769 if (curwin->w_cursor.lnum < curwin->w_topline)
1770 scrolled = used;
1772 #ifdef FEAT_FOLDING
1773 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1775 --top;
1776 ++bot;
1778 else
1779 #endif
1781 top = curwin->w_cursor.lnum - 1;
1782 bot = curwin->w_cursor.lnum + 1;
1784 new_topline = top + 1;
1786 #ifdef FEAT_DIFF
1787 /* count filler lines of the cursor window as context */
1788 i = diff_check_fill(curwin, curwin->w_cursor.lnum);
1789 used += i;
1790 extra += i;
1791 #endif
1794 * Check if the lines from "top" to "bot" fit in the window. If they do,
1795 * set new_topline and advance "top" and "bot" to include more lines.
1797 while (top > 0)
1799 #ifdef FEAT_FOLDING
1800 if (hasFolding(top, &top, NULL))
1801 /* count one logical line for a sequence of folded lines */
1802 i = 1;
1803 else
1804 #endif
1805 i = plines(top);
1806 used += i;
1807 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1809 #ifdef FEAT_FOLDING
1810 if (hasFolding(bot, NULL, &bot))
1811 /* count one logical line for a sequence of folded lines */
1812 ++used;
1813 else
1814 #endif
1815 used += plines(bot);
1817 if (used > curwin->w_height)
1818 break;
1819 if (top < curwin->w_topline)
1820 scrolled += i;
1823 * If scrolling is needed, scroll at least 'sj' lines.
1825 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1826 && extra >= off)
1827 break;
1829 extra += i;
1830 new_topline = top;
1831 --top;
1832 ++bot;
1836 * If we don't have enough space, put cursor in the middle.
1837 * This makes sure we get the same position when using "k" and "j"
1838 * in a small window.
1840 if (used > curwin->w_height)
1841 scroll_cursor_halfway(FALSE);
1842 else
1845 * If "always" is FALSE, only adjust topline to a lower value, higher
1846 * value may happen with wrapping lines
1848 if (new_topline < curwin->w_topline || always)
1849 curwin->w_topline = new_topline;
1850 if (curwin->w_topline > curwin->w_cursor.lnum)
1851 curwin->w_topline = curwin->w_cursor.lnum;
1852 #ifdef FEAT_DIFF
1853 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1854 if (curwin->w_topfill > 0 && extra > off)
1856 curwin->w_topfill -= extra - off;
1857 if (curwin->w_topfill < 0)
1858 curwin->w_topfill = 0;
1860 check_topfill(curwin, FALSE);
1861 #endif
1862 if (curwin->w_topline != old_topline
1863 #ifdef FEAT_DIFF
1864 || curwin->w_topfill != old_topfill
1865 #endif
1867 curwin->w_valid &=
1868 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1869 curwin->w_valid |= VALID_TOPLINE;
1874 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1875 * screen lines for text lines.
1877 void
1878 set_empty_rows(wp, used)
1879 win_T *wp;
1880 int used;
1882 #ifdef FEAT_DIFF
1883 wp->w_filler_rows = 0;
1884 #endif
1885 if (used == 0)
1886 wp->w_empty_rows = 0; /* single line that doesn't fit */
1887 else
1889 wp->w_empty_rows = wp->w_height - used;
1890 #ifdef FEAT_DIFF
1891 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1893 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1894 if (wp->w_empty_rows > wp->w_filler_rows)
1895 wp->w_empty_rows -= wp->w_filler_rows;
1896 else
1898 wp->w_filler_rows = wp->w_empty_rows;
1899 wp->w_empty_rows = 0;
1902 #endif
1907 * Recompute topline to put the cursor at the bottom of the window.
1908 * Scroll at least "min_scroll" lines.
1909 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1910 * This is messy stuff!!!
1912 void
1913 scroll_cursor_bot(min_scroll, set_topbot)
1914 int min_scroll;
1915 int set_topbot;
1917 int used;
1918 int scrolled = 0;
1919 int extra = 0;
1920 int i;
1921 linenr_T line_count;
1922 linenr_T old_topline = curwin->w_topline;
1923 lineoff_T loff;
1924 lineoff_T boff;
1925 #ifdef FEAT_DIFF
1926 int old_topfill = curwin->w_topfill;
1927 int fill_below_window;
1928 #endif
1929 linenr_T old_botline = curwin->w_botline;
1930 linenr_T old_valid = curwin->w_valid;
1931 int old_empty_rows = curwin->w_empty_rows;
1932 linenr_T cln; /* Cursor Line Number */
1934 cln = curwin->w_cursor.lnum;
1935 if (set_topbot)
1937 used = 0;
1938 curwin->w_botline = cln + 1;
1939 #ifdef FEAT_DIFF
1940 loff.fill = 0;
1941 #endif
1942 for (curwin->w_topline = curwin->w_botline;
1943 curwin->w_topline > 1;
1944 curwin->w_topline = loff.lnum)
1946 loff.lnum = curwin->w_topline;
1947 topline_back(&loff);
1948 if (used + loff.height > curwin->w_height)
1949 break;
1950 used += loff.height;
1951 #ifdef FEAT_DIFF
1952 curwin->w_topfill = loff.fill;
1953 #endif
1955 set_empty_rows(curwin, used);
1956 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1957 if (curwin->w_topline != old_topline
1958 #ifdef FEAT_DIFF
1959 || curwin->w_topfill != old_topfill
1960 #endif
1962 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1964 else
1965 validate_botline();
1967 /* The lines of the cursor line itself are always used. */
1968 #ifdef FEAT_DIFF
1969 used = plines_nofill(cln);
1970 #else
1971 validate_cheight();
1972 used = curwin->w_cline_height;
1973 #endif
1975 /* If the cursor is below botline, we will at least scroll by the height
1976 * of the cursor line. Correct for empty lines, which are really part of
1977 * botline. */
1978 if (cln >= curwin->w_botline)
1980 scrolled = used;
1981 if (cln == curwin->w_botline)
1982 scrolled -= curwin->w_empty_rows;
1986 * Stop counting lines to scroll when
1987 * - hitting start of the file
1988 * - scrolled nothing or at least 'sj' lines
1989 * - at least 'so' lines below the cursor
1990 * - lines between botline and cursor have been counted
1992 #ifdef FEAT_FOLDING
1993 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1994 #endif
1996 loff.lnum = cln;
1997 boff.lnum = cln;
1999 #ifdef FEAT_DIFF
2000 loff.fill = 0;
2001 boff.fill = 0;
2002 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2003 - curwin->w_filler_rows;
2004 #endif
2006 while (loff.lnum > 1)
2008 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
2009 * context for 'scrolloff' and counted all lines below the window. */
2010 if ((((scrolled <= 0 || scrolled >= min_scroll)
2011 && extra >= (
2012 #ifdef FEAT_MOUSE
2013 mouse_dragging > 0 ? mouse_dragging - 1 :
2014 #endif
2015 p_so))
2016 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2017 && loff.lnum <= curwin->w_botline
2018 #ifdef FEAT_DIFF
2019 && (loff.lnum < curwin->w_botline
2020 || loff.fill >= fill_below_window)
2021 #endif
2023 break;
2025 /* Add one line above */
2026 topline_back(&loff);
2027 used += loff.height;
2028 if (used > curwin->w_height)
2029 break;
2030 if (loff.lnum >= curwin->w_botline
2031 #ifdef FEAT_DIFF
2032 && (loff.lnum > curwin->w_botline
2033 || loff.fill <= fill_below_window)
2034 #endif
2037 /* Count screen lines that are below the window. */
2038 scrolled += loff.height;
2039 if (loff.lnum == curwin->w_botline
2040 #ifdef FEAT_DIFF
2041 && boff.fill == 0
2042 #endif
2044 scrolled -= curwin->w_empty_rows;
2047 if (boff.lnum < curbuf->b_ml.ml_line_count)
2049 /* Add one line below */
2050 botline_forw(&boff);
2051 used += boff.height;
2052 if (used > curwin->w_height)
2053 break;
2054 if (extra < (
2055 #ifdef FEAT_MOUSE
2056 mouse_dragging > 0 ? mouse_dragging - 1 :
2057 #endif
2058 p_so) || scrolled < min_scroll)
2060 extra += boff.height;
2061 if (boff.lnum >= curwin->w_botline
2062 #ifdef FEAT_DIFF
2063 || (boff.lnum + 1 == curwin->w_botline
2064 && boff.fill > curwin->w_filler_rows)
2065 #endif
2068 /* Count screen lines that are below the window. */
2069 scrolled += boff.height;
2070 if (boff.lnum == curwin->w_botline
2071 #ifdef FEAT_DIFF
2072 && boff.fill == 0
2073 #endif
2075 scrolled -= curwin->w_empty_rows;
2081 /* curwin->w_empty_rows is larger, no need to scroll */
2082 if (scrolled <= 0)
2083 line_count = 0;
2084 /* more than a screenfull, don't scroll but redraw */
2085 else if (used > curwin->w_height)
2086 line_count = used;
2087 /* scroll minimal number of lines */
2088 else
2090 line_count = 0;
2091 #ifdef FEAT_DIFF
2092 boff.fill = curwin->w_topfill;
2093 #endif
2094 boff.lnum = curwin->w_topline - 1;
2095 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2097 botline_forw(&boff);
2098 i += boff.height;
2099 ++line_count;
2101 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2102 line_count = 9999;
2106 * Scroll up if the cursor is off the bottom of the screen a bit.
2107 * Otherwise put it at 1/2 of the screen.
2109 if (line_count >= curwin->w_height && line_count > min_scroll)
2110 scroll_cursor_halfway(FALSE);
2111 else
2112 scrollup(line_count, TRUE);
2115 * If topline didn't change we need to restore w_botline and w_empty_rows
2116 * (we changed them).
2117 * If topline did change, update_screen() will set botline.
2119 if (curwin->w_topline == old_topline && set_topbot)
2121 curwin->w_botline = old_botline;
2122 curwin->w_empty_rows = old_empty_rows;
2123 curwin->w_valid = old_valid;
2125 curwin->w_valid |= VALID_TOPLINE;
2129 * Recompute topline to put the cursor halfway the window
2130 * If "atend" is TRUE, also put it halfway at the end of the file.
2132 void
2133 scroll_cursor_halfway(atend)
2134 int atend;
2136 int above = 0;
2137 linenr_T topline;
2138 #ifdef FEAT_DIFF
2139 int topfill = 0;
2140 #endif
2141 int below = 0;
2142 int used;
2143 lineoff_T loff;
2144 lineoff_T boff;
2146 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2147 #ifdef FEAT_FOLDING
2148 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2149 #endif
2150 #ifdef FEAT_DIFF
2151 used = plines_nofill(loff.lnum);
2152 loff.fill = 0;
2153 boff.fill = 0;
2154 #else
2155 used = plines(loff.lnum);
2156 #endif
2157 topline = loff.lnum;
2158 while (topline > 1)
2160 if (below <= above) /* add a line below the cursor first */
2162 if (boff.lnum < curbuf->b_ml.ml_line_count)
2164 botline_forw(&boff);
2165 used += boff.height;
2166 if (used > curwin->w_height)
2167 break;
2168 below += boff.height;
2170 else
2172 ++below; /* count a "~" line */
2173 if (atend)
2174 ++used;
2178 if (below > above) /* add a line above the cursor */
2180 topline_back(&loff);
2181 used += loff.height;
2182 if (used > curwin->w_height)
2183 break;
2184 above += loff.height;
2185 topline = loff.lnum;
2186 #ifdef FEAT_DIFF
2187 topfill = loff.fill;
2188 #endif
2191 #ifdef FEAT_FOLDING
2192 if (!hasFolding(topline, &curwin->w_topline, NULL))
2193 #endif
2194 curwin->w_topline = topline;
2195 #ifdef FEAT_DIFF
2196 curwin->w_topfill = topfill;
2197 check_topfill(curwin, FALSE);
2198 #endif
2199 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2200 curwin->w_valid |= VALID_TOPLINE;
2204 * Correct the cursor position so that it is in a part of the screen at least
2205 * 'so' lines from the top and bottom, if possible.
2206 * If not possible, put it at the same position as scroll_cursor_halfway().
2207 * When called topline must be valid!
2209 void
2210 cursor_correct()
2212 int above = 0; /* screen lines above topline */
2213 linenr_T topline;
2214 int below = 0; /* screen lines below botline */
2215 linenr_T botline;
2216 int above_wanted, below_wanted;
2217 linenr_T cln; /* Cursor Line Number */
2218 int max_off;
2221 * How many lines we would like to have above/below the cursor depends on
2222 * whether the first/last line of the file is on screen.
2224 above_wanted = p_so;
2225 below_wanted = p_so;
2226 #ifdef FEAT_MOUSE
2227 if (mouse_dragging > 0)
2229 above_wanted = mouse_dragging - 1;
2230 below_wanted = mouse_dragging - 1;
2232 #endif
2233 if (curwin->w_topline == 1)
2235 above_wanted = 0;
2236 max_off = curwin->w_height / 2;
2237 if (below_wanted > max_off)
2238 below_wanted = max_off;
2240 validate_botline();
2241 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2242 #ifdef FEAT_MOUSE
2243 && mouse_dragging == 0
2244 #endif
2247 below_wanted = 0;
2248 max_off = (curwin->w_height - 1) / 2;
2249 if (above_wanted > max_off)
2250 above_wanted = max_off;
2254 * If there are sufficient file-lines above and below the cursor, we can
2255 * return now.
2257 cln = curwin->w_cursor.lnum;
2258 if (cln >= curwin->w_topline + above_wanted
2259 && cln < curwin->w_botline - below_wanted
2260 #ifdef FEAT_FOLDING
2261 && !hasAnyFolding(curwin)
2262 #endif
2264 return;
2267 * Narrow down the area where the cursor can be put by taking lines from
2268 * the top and the bottom until:
2269 * - the desired context lines are found
2270 * - the lines from the top is past the lines from the bottom
2272 topline = curwin->w_topline;
2273 botline = curwin->w_botline - 1;
2274 #ifdef FEAT_DIFF
2275 /* count filler lines as context */
2276 above = curwin->w_topfill;
2277 below = curwin->w_filler_rows;
2278 #endif
2279 while ((above < above_wanted || below < below_wanted) && topline < botline)
2281 if (below < below_wanted && (below <= above || above >= above_wanted))
2283 #ifdef FEAT_FOLDING
2284 if (hasFolding(botline, &botline, NULL))
2285 ++below;
2286 else
2287 #endif
2288 below += plines(botline);
2289 --botline;
2291 if (above < above_wanted && (above < below || below >= below_wanted))
2293 #ifdef FEAT_FOLDING
2294 if (hasFolding(topline, NULL, &topline))
2295 ++above;
2296 else
2297 #endif
2298 #ifndef FEAT_DIFF
2299 above += plines(topline);
2300 #else
2301 above += plines_nofill(topline);
2303 /* Count filler lines below this line as context. */
2304 if (topline < botline)
2305 above += diff_check_fill(curwin, topline + 1);
2306 #endif
2307 ++topline;
2310 if (topline == botline || botline == 0)
2311 curwin->w_cursor.lnum = topline;
2312 else if (topline > botline)
2313 curwin->w_cursor.lnum = botline;
2314 else
2316 if (cln < topline && curwin->w_topline > 1)
2318 curwin->w_cursor.lnum = topline;
2319 curwin->w_valid &=
2320 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2322 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2324 curwin->w_cursor.lnum = botline;
2325 curwin->w_valid &=
2326 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2329 curwin->w_valid |= VALID_TOPLINE;
2332 static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir));
2335 * move screen 'count' pages up or down and update screen
2337 * return FAIL for failure, OK otherwise
2340 onepage(dir, count)
2341 int dir;
2342 long count;
2344 long n;
2345 int retval = OK;
2346 lineoff_T loff;
2347 linenr_T old_topline = curwin->w_topline;
2349 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2351 beep_flush();
2352 return FAIL;
2355 for ( ; count > 0; --count)
2357 validate_botline();
2359 * It's an error to move a page up when the first line is already on
2360 * the screen. It's an error to move a page down when the last line
2361 * is on the screen and the topline is 'scrolloff' lines from the
2362 * last line.
2364 if (dir == FORWARD
2365 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2366 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2367 : (curwin->w_topline == 1
2368 #ifdef FEAT_DIFF
2369 && curwin->w_topfill ==
2370 diff_check_fill(curwin, curwin->w_topline)
2371 #endif
2374 beep_flush();
2375 retval = FAIL;
2376 break;
2379 #ifdef FEAT_DIFF
2380 loff.fill = 0;
2381 #endif
2382 if (dir == FORWARD)
2384 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
2386 /* Vi compatible scrolling */
2387 if (p_window <= 2)
2388 ++curwin->w_topline;
2389 else
2390 curwin->w_topline += p_window - 2;
2391 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2392 curwin->w_topline = curbuf->b_ml.ml_line_count;
2393 curwin->w_cursor.lnum = curwin->w_topline;
2395 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2397 /* at end of file */
2398 curwin->w_topline = curbuf->b_ml.ml_line_count;
2399 #ifdef FEAT_DIFF
2400 curwin->w_topfill = 0;
2401 #endif
2402 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2404 else
2406 /* For the overlap, start with the line just below the window
2407 * and go upwards. */
2408 loff.lnum = curwin->w_botline;
2409 #ifdef FEAT_DIFF
2410 loff.fill = diff_check_fill(curwin, loff.lnum)
2411 - curwin->w_filler_rows;
2412 #endif
2413 get_scroll_overlap(&loff, -1);
2414 curwin->w_topline = loff.lnum;
2415 #ifdef FEAT_DIFF
2416 curwin->w_topfill = loff.fill;
2417 check_topfill(curwin, FALSE);
2418 #endif
2419 curwin->w_cursor.lnum = curwin->w_topline;
2420 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2421 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2424 else /* dir == BACKWARDS */
2426 #ifdef FEAT_DIFF
2427 if (curwin->w_topline == 1)
2429 /* Include max number of filler lines */
2430 max_topfill();
2431 continue;
2433 #endif
2434 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
2436 /* Vi compatible scrolling (sort of) */
2437 if (p_window <= 2)
2438 --curwin->w_topline;
2439 else
2440 curwin->w_topline -= p_window - 2;
2441 if (curwin->w_topline < 1)
2442 curwin->w_topline = 1;
2443 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2444 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2445 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2446 continue;
2449 /* Find the line at the top of the window that is going to be the
2450 * line at the bottom of the window. Make sure this results in
2451 * the same line as before doing CTRL-F. */
2452 loff.lnum = curwin->w_topline - 1;
2453 #ifdef FEAT_DIFF
2454 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2455 - curwin->w_topfill;
2456 #endif
2457 get_scroll_overlap(&loff, 1);
2459 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2461 loff.lnum = curbuf->b_ml.ml_line_count;
2462 #ifdef FEAT_DIFF
2463 loff.fill = 0;
2465 else
2467 botline_topline(&loff);
2468 #endif
2470 curwin->w_cursor.lnum = loff.lnum;
2472 /* Find the line just above the new topline to get the right line
2473 * at the bottom of the window. */
2474 n = 0;
2475 while (n <= curwin->w_height && loff.lnum >= 1)
2477 topline_back(&loff);
2478 n += loff.height;
2480 if (n <= curwin->w_height) /* at begin of file */
2482 curwin->w_topline = 1;
2483 #ifdef FEAT_DIFF
2484 max_topfill();
2485 #endif
2486 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2488 else
2490 /* Go two lines forward again. */
2491 #ifdef FEAT_DIFF
2492 topline_botline(&loff);
2493 #endif
2494 botline_forw(&loff);
2495 botline_forw(&loff);
2496 #ifdef FEAT_DIFF
2497 botline_topline(&loff);
2498 #endif
2499 #ifdef FEAT_FOLDING
2500 /* We're at the wrong end of a fold now. */
2501 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2502 #endif
2504 /* Always scroll at least one line. Avoid getting stuck on
2505 * very long lines. */
2506 if (loff.lnum >= curwin->w_topline
2507 #ifdef FEAT_DIFF
2508 && (loff.lnum > curwin->w_topline
2509 || loff.fill >= curwin->w_topfill)
2510 #endif
2513 #ifdef FEAT_DIFF
2514 /* First try using the maximum number of filler lines. If
2515 * that's not enough, backup one line. */
2516 loff.fill = curwin->w_topfill;
2517 if (curwin->w_topfill < diff_check_fill(curwin,
2518 curwin->w_topline))
2519 max_topfill();
2520 if (curwin->w_topfill == loff.fill)
2521 #endif
2523 --curwin->w_topline;
2524 #ifdef FEAT_DIFF
2525 curwin->w_topfill = 0;
2526 #endif
2528 comp_botline(curwin);
2529 curwin->w_cursor.lnum = curwin->w_botline - 1;
2530 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|
2531 VALID_WROW|VALID_CROW);
2533 else
2535 curwin->w_topline = loff.lnum;
2536 #ifdef FEAT_DIFF
2537 curwin->w_topfill = loff.fill;
2538 check_topfill(curwin, FALSE);
2539 #endif
2540 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2545 #ifdef FEAT_FOLDING
2546 foldAdjustCursor();
2547 #endif
2548 cursor_correct();
2549 if (retval == OK)
2550 beginline(BL_SOL | BL_FIX);
2551 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2554 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2555 * But make sure we scroll at least one line (happens with mix of long
2556 * wrapping lines and non-wrapping line).
2558 if (retval == OK && dir == FORWARD && check_top_offset())
2560 scroll_cursor_top(1, FALSE);
2561 if (curwin->w_topline <= old_topline
2562 && old_topline < curbuf->b_ml.ml_line_count)
2564 curwin->w_topline = old_topline + 1;
2565 #ifdef FEAT_FOLDING
2566 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2567 #endif
2571 redraw_later(VALID);
2572 return retval;
2576 * Decide how much overlap to use for page-up or page-down scrolling.
2577 * This is symmetric, so that doing both keeps the same lines displayed.
2578 * Three lines are examined:
2580 * before CTRL-F after CTRL-F / before CTRL-B
2581 * etc. l1
2582 * l1 last but one line ------------
2583 * l2 last text line l2 top text line
2584 * ------------- l3 second text line
2585 * l3 etc.
2587 static void
2588 get_scroll_overlap(lp, dir)
2589 lineoff_T *lp;
2590 int dir;
2592 int h1, h2, h3, h4;
2593 int min_height = curwin->w_height - 2;
2594 lineoff_T loff0, loff1, loff2;
2596 #ifdef FEAT_DIFF
2597 if (lp->fill > 0)
2598 lp->height = 1;
2599 else
2600 lp->height = plines_nofill(lp->lnum);
2601 #else
2602 lp->height = plines(lp->lnum);
2603 #endif
2604 h1 = lp->height;
2605 if (h1 > min_height)
2606 return; /* no overlap */
2608 loff0 = *lp;
2609 if (dir > 0)
2610 botline_forw(lp);
2611 else
2612 topline_back(lp);
2613 h2 = lp->height;
2614 if (h2 + h1 > min_height)
2616 *lp = loff0; /* no overlap */
2617 return;
2620 loff1 = *lp;
2621 if (dir > 0)
2622 botline_forw(lp);
2623 else
2624 topline_back(lp);
2625 h3 = lp->height;
2626 if (h3 + h2 > min_height)
2628 *lp = loff0; /* no overlap */
2629 return;
2632 loff2 = *lp;
2633 if (dir > 0)
2634 botline_forw(lp);
2635 else
2636 topline_back(lp);
2637 h4 = lp->height;
2638 if (h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
2639 *lp = loff1; /* 1 line overlap */
2640 else
2641 *lp = loff2; /* 2 lines overlap */
2642 return;
2645 /* #define KEEP_SCREEN_LINE */
2647 * Scroll 'scroll' lines up or down.
2649 void
2650 halfpage(flag, Prenum)
2651 int flag;
2652 linenr_T Prenum;
2654 long scrolled = 0;
2655 int i;
2656 int n;
2657 int room;
2659 if (Prenum)
2660 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2661 curwin->w_height : Prenum;
2662 n = (curwin->w_p_scr <= curwin->w_height) ?
2663 curwin->w_p_scr : curwin->w_height;
2665 validate_botline();
2666 room = curwin->w_empty_rows;
2667 #ifdef FEAT_DIFF
2668 room += curwin->w_filler_rows;
2669 #endif
2670 if (flag)
2673 * scroll the text up
2675 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2677 #ifdef FEAT_DIFF
2678 if (curwin->w_topfill > 0)
2680 i = 1;
2681 if (--n < 0 && scrolled > 0)
2682 break;
2683 --curwin->w_topfill;
2685 else
2686 #endif
2688 #ifdef FEAT_DIFF
2689 i = plines_nofill(curwin->w_topline);
2690 #else
2691 i = plines(curwin->w_topline);
2692 #endif
2693 n -= i;
2694 if (n < 0 && scrolled > 0)
2695 break;
2696 #ifdef FEAT_FOLDING
2697 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2698 #endif
2699 ++curwin->w_topline;
2700 #ifdef FEAT_DIFF
2701 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2702 #endif
2704 #ifndef KEEP_SCREEN_LINE
2705 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2707 ++curwin->w_cursor.lnum;
2708 curwin->w_valid &=
2709 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2711 #endif
2713 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2714 scrolled += i;
2717 * Correct w_botline for changed w_topline.
2718 * Won't work when there are filler lines.
2720 #ifdef FEAT_DIFF
2721 if (curwin->w_p_diff)
2722 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2723 else
2724 #endif
2726 room += i;
2729 i = plines(curwin->w_botline);
2730 if (i > room)
2731 break;
2732 #ifdef FEAT_FOLDING
2733 (void)hasFolding(curwin->w_botline, NULL,
2734 &curwin->w_botline);
2735 #endif
2736 ++curwin->w_botline;
2737 room -= i;
2738 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2742 #ifndef KEEP_SCREEN_LINE
2744 * When hit bottom of the file: move cursor down.
2746 if (n > 0)
2748 # ifdef FEAT_FOLDING
2749 if (hasAnyFolding(curwin))
2751 while (--n >= 0
2752 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2754 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2755 &curwin->w_cursor.lnum);
2756 ++curwin->w_cursor.lnum;
2759 else
2760 # endif
2761 curwin->w_cursor.lnum += n;
2762 check_cursor_lnum();
2764 #else
2765 /* try to put the cursor in the same screen line */
2766 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2767 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2769 scrolled -= plines(curwin->w_cursor.lnum);
2770 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2771 break;
2772 # ifdef FEAT_FOLDING
2773 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2774 &curwin->w_cursor.lnum);
2775 # endif
2776 ++curwin->w_cursor.lnum;
2778 #endif
2780 else
2783 * scroll the text down
2785 while (n > 0 && curwin->w_topline > 1)
2787 #ifdef FEAT_DIFF
2788 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2790 i = 1;
2791 if (--n < 0 && scrolled > 0)
2792 break;
2793 ++curwin->w_topfill;
2795 else
2796 #endif
2798 #ifdef FEAT_DIFF
2799 i = plines_nofill(curwin->w_topline - 1);
2800 #else
2801 i = plines(curwin->w_topline - 1);
2802 #endif
2803 n -= i;
2804 if (n < 0 && scrolled > 0)
2805 break;
2806 --curwin->w_topline;
2807 #ifdef FEAT_FOLDING
2808 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2809 #endif
2810 #ifdef FEAT_DIFF
2811 curwin->w_topfill = 0;
2812 #endif
2814 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2815 VALID_BOTLINE|VALID_BOTLINE_AP);
2816 scrolled += i;
2817 #ifndef KEEP_SCREEN_LINE
2818 if (curwin->w_cursor.lnum > 1)
2820 --curwin->w_cursor.lnum;
2821 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2823 #endif
2825 #ifndef KEEP_SCREEN_LINE
2827 * When hit top of the file: move cursor up.
2829 if (n > 0)
2831 if (curwin->w_cursor.lnum <= (linenr_T)n)
2832 curwin->w_cursor.lnum = 1;
2833 else
2834 # ifdef FEAT_FOLDING
2835 if (hasAnyFolding(curwin))
2837 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2839 --curwin->w_cursor.lnum;
2840 (void)hasFolding(curwin->w_cursor.lnum,
2841 &curwin->w_cursor.lnum, NULL);
2844 else
2845 # endif
2846 curwin->w_cursor.lnum -= n;
2848 #else
2849 /* try to put the cursor in the same screen line */
2850 scrolled += n; /* move cursor when topline is 1 */
2851 while (curwin->w_cursor.lnum > curwin->w_topline
2852 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2854 scrolled -= plines(curwin->w_cursor.lnum - 1);
2855 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2856 break;
2857 --curwin->w_cursor.lnum;
2858 # ifdef FEAT_FOLDING
2859 foldAdjustCursor();
2860 # endif
2862 #endif
2864 # ifdef FEAT_FOLDING
2865 /* Move cursor to first line of closed fold. */
2866 foldAdjustCursor();
2867 # endif
2868 #ifdef FEAT_DIFF
2869 check_topfill(curwin, !flag);
2870 #endif
2871 cursor_correct();
2872 beginline(BL_SOL | BL_FIX);
2873 redraw_later(VALID);