blob: 42f778438fc4021ce4d7a26c6a00920f2eaf5c96 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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.
11 *
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.
18 */
19
20#include "vim.h"
21
22static void comp_botline __ARGS((win_T *wp));
Bram Moolenaar3d6db142014-03-28 21:49:32 +010023static void redraw_for_cursorline __ARGS((win_T *wp));
Bram Moolenaar1e015462005-09-25 22:16:38 +000024static int scrolljump_value __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025static int check_top_offset __ARGS((void));
26static void curs_rows __ARGS((win_T *wp, int do_botline));
27static void validate_botline_win __ARGS((win_T *wp));
28static void validate_cheight __ARGS((void));
29
30typedef struct
31{
32 linenr_T lnum; /* line number */
33#ifdef FEAT_DIFF
34 int fill; /* filler lines */
35#endif
36 int height; /* height of added line */
37} lineoff_T;
38
39static void topline_back __ARGS((lineoff_T *lp));
40static void botline_forw __ARGS((lineoff_T *lp));
41#ifdef FEAT_DIFF
42static void botline_topline __ARGS((lineoff_T *lp));
43static void topline_botline __ARGS((lineoff_T *lp));
44static void max_topfill __ARGS((void));
45#endif
46
47/*
48 * Compute wp->w_botline for the current wp->w_topline. Can be called after
49 * wp->w_topline changed.
50 */
51 static void
52comp_botline(wp)
53 win_T *wp;
54{
55 int n;
56 linenr_T lnum;
57 int done;
58#ifdef FEAT_FOLDING
59 linenr_T last;
60 int folded;
61#endif
62
63 /*
64 * If w_cline_row is valid, start there.
65 * Otherwise have to start at w_topline.
66 */
67 check_cursor_moved(wp);
68 if (wp->w_valid & VALID_CROW)
69 {
70 lnum = wp->w_cursor.lnum;
71 done = wp->w_cline_row;
72 }
73 else
74 {
75 lnum = wp->w_topline;
76 done = 0;
77 }
78
79 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
80 {
81#ifdef FEAT_FOLDING
82 last = lnum;
83 folded = FALSE;
84 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
85 {
86 n = 1;
87 folded = TRUE;
88 }
89 else
90#endif
91#ifdef FEAT_DIFF
92 if (lnum == wp->w_topline)
93 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
94 else
95#endif
96 n = plines_win(wp, lnum, TRUE);
97 if (
98#ifdef FEAT_FOLDING
99 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
100#else
101 lnum == wp->w_cursor.lnum
102#endif
103 )
104 {
105 wp->w_cline_row = done;
106 wp->w_cline_height = n;
107#ifdef FEAT_FOLDING
108 wp->w_cline_folded = folded;
109#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100110 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
112 }
113 if (done + n > wp->w_height)
114 break;
115 done += n;
116#ifdef FEAT_FOLDING
117 lnum = last;
118#endif
119 }
120
121 /* wp->w_botline is the line that is just below the window */
122 wp->w_botline = lnum;
123 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
124
125 set_empty_rows(wp, done);
126}
127
128/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100129 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
130 * set.
131 */
132 static void
133redraw_for_cursorline(wp)
134 win_T *wp;
135{
136 if ((wp->w_p_rnu
137#ifdef FEAT_SYN_HL
138 || wp->w_p_cul
139#endif
140 )
141 && (wp->w_valid & VALID_CROW) == 0
142# ifdef FEAT_INS_EXPAND
143 && !pum_visible()
144# endif
145 )
146 redraw_win_later(wp, SOME_VALID);
147}
148
149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 * Update curwin->w_topline and redraw if necessary.
151 * Used to update the screen before printing a message.
152 */
153 void
154update_topline_redraw()
155{
156 update_topline();
157 if (must_redraw)
158 update_screen(0);
159}
160
161/*
162 * Update curwin->w_topline to move the cursor onto the screen.
163 */
164 void
165update_topline()
166{
167 long line_count;
168 int halfheight;
169 int n;
170 linenr_T old_topline;
171#ifdef FEAT_DIFF
172 int old_topfill;
173#endif
174#ifdef FEAT_FOLDING
175 linenr_T lnum;
176#endif
177 int check_topline = FALSE;
178 int check_botline = FALSE;
179#ifdef FEAT_MOUSE
180 int save_so = p_so;
181#endif
182
183 if (!screen_valid(TRUE))
184 return;
185
186 check_cursor_moved(curwin);
187 if (curwin->w_valid & VALID_TOPLINE)
188 return;
189
190#ifdef FEAT_MOUSE
191 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000192 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 p_so = mouse_dragging - 1;
194#endif
195
196 old_topline = curwin->w_topline;
197#ifdef FEAT_DIFF
198 old_topfill = curwin->w_topfill;
199#endif
200
201 /*
202 * If the buffer is empty, always set topline to 1.
203 */
204 if (bufempty()) /* special case - file is empty */
205 {
206 if (curwin->w_topline != 1)
207 redraw_later(NOT_VALID);
208 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 curwin->w_botline = 2;
210 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
211#ifdef FEAT_SCROLLBIND
212 curwin->w_scbind_pos = 1;
213#endif
214 }
215
216 /*
217 * If the cursor is above or near the top of the window, scroll the window
218 * to show the line the cursor is in, with 'scrolloff' context.
219 */
220 else
221 {
222 if (curwin->w_topline > 1)
223 {
224 /* If the cursor is above topline, scrolling is always needed.
225 * If the cursor is far below topline and there is no folding,
226 * scrolling down is never needed. */
227 if (curwin->w_cursor.lnum < curwin->w_topline)
228 check_topline = TRUE;
229 else if (check_top_offset())
230 check_topline = TRUE;
231 }
232#ifdef FEAT_DIFF
233 /* Check if there are more filler lines than allowed. */
234 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
235 curwin->w_topline))
236 check_topline = TRUE;
237#endif
238
239 if (check_topline)
240 {
241 halfheight = curwin->w_height / 2 - 1;
242 if (halfheight < 2)
243 halfheight = 2;
244
245#ifdef FEAT_FOLDING
246 if (hasAnyFolding(curwin))
247 {
248 /* Count the number of logical lines between the cursor and
249 * topline + p_so (approximation of how much will be
250 * scrolled). */
251 n = 0;
252 for (lnum = curwin->w_cursor.lnum;
253 lnum < curwin->w_topline + p_so; ++lnum)
254 {
255 ++n;
256 /* stop at end of file or when we know we are far off */
257 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
258 break;
259 (void)hasFolding(lnum, NULL, &lnum);
260 }
261 }
262 else
263#endif
264 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
265
266 /* If we weren't very close to begin with, we scroll to put the
267 * cursor in the middle of the window. Otherwise put the cursor
268 * near the top of the window. */
269 if (n >= halfheight)
270 scroll_cursor_halfway(FALSE);
271 else
272 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000273 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 check_botline = TRUE;
275 }
276 }
277
278 else
279 {
280#ifdef FEAT_FOLDING
281 /* Make sure topline is the first line of a fold. */
282 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
283#endif
284 check_botline = TRUE;
285 }
286 }
287
288 /*
289 * If the cursor is below the bottom of the window, scroll the window
290 * to put the cursor on the window.
291 * When w_botline is invalid, recompute it first, to avoid a redraw later.
292 * If w_botline was approximated, we might need a redraw later in a few
293 * cases, but we don't want to spend (a lot of) time recomputing w_botline
294 * for every small change.
295 */
296 if (check_botline)
297 {
298 if (!(curwin->w_valid & VALID_BOTLINE_AP))
299 validate_botline();
300
301 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
302 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000303 if (curwin->w_cursor.lnum < curwin->w_botline)
304 {
305 if (((long)curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 >= (long)curwin->w_botline - p_so
307#ifdef FEAT_FOLDING
308 || hasAnyFolding(curwin)
309#endif
310 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 lineoff_T loff;
313
Bram Moolenaard4153d42008-11-15 15:06:17 +0000314 /* Cursor is (a few lines) above botline, check if there are
315 * 'scrolloff' window lines below the cursor. If not, need to
316 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 n = curwin->w_empty_rows;
318 loff.lnum = curwin->w_cursor.lnum;
319#ifdef FEAT_FOLDING
320 /* In a fold go to its last line. */
321 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
322#endif
323#ifdef FEAT_DIFF
324 loff.fill = 0;
325 n += curwin->w_filler_rows;
326#endif
327 loff.height = 0;
328 while (loff.lnum < curwin->w_botline
329#ifdef FEAT_DIFF
330 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
331#endif
332 )
333 {
334 n += loff.height;
335 if (n >= p_so)
336 break;
337 botline_forw(&loff);
338 }
339 if (n >= p_so)
340 /* sufficient context, no need to scroll */
341 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000342 }
343 else
344 /* sufficient context, no need to scroll */
345 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 }
347 if (check_botline)
348 {
349#ifdef FEAT_FOLDING
350 if (hasAnyFolding(curwin))
351 {
352 /* Count the number of logical lines between the cursor and
353 * botline - p_so (approximation of how much will be
354 * scrolled). */
355 line_count = 0;
356 for (lnum = curwin->w_cursor.lnum;
357 lnum >= curwin->w_botline - p_so; --lnum)
358 {
359 ++line_count;
360 /* stop at end of file or when we know we are far off */
361 if (lnum <= 0 || line_count > curwin->w_height + 1)
362 break;
363 (void)hasFolding(lnum, &lnum, NULL);
364 }
365 }
366 else
367#endif
368 line_count = curwin->w_cursor.lnum - curwin->w_botline
369 + 1 + p_so;
370 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000371 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 else
373 scroll_cursor_halfway(FALSE);
374 }
375 }
376 }
377 curwin->w_valid |= VALID_TOPLINE;
378
379 /*
380 * Need to redraw when topline changed.
381 */
382 if (curwin->w_topline != old_topline
383#ifdef FEAT_DIFF
384 || curwin->w_topfill != old_topfill
385#endif
386 )
387 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100388 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000389 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
391 curwin->w_skipcol = 0;
392 redraw_later(NOT_VALID);
393 }
394 else
395 redraw_later(VALID);
396 /* May need to set w_skipcol when cursor in w_topline. */
397 if (curwin->w_cursor.lnum == curwin->w_topline)
398 validate_cursor();
399 }
400
401#ifdef FEAT_MOUSE
402 p_so = save_so;
403#endif
404}
405
406/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000407 * Return the scrolljump value to use for the current window.
408 * When 'scrolljump' is positive use it as-is.
409 * When 'scrolljump' is negative use it as a percentage of the window height.
410 */
411 static int
412scrolljump_value()
413{
414 if (p_sj >= 0)
415 return (int)p_sj;
416 return (curwin->w_height * -p_sj) / 100;
417}
418
419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
421 * current window.
422 */
423 static int
424check_top_offset()
425{
426 lineoff_T loff;
427 int n;
428
429 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
430#ifdef FEAT_FOLDING
431 || hasAnyFolding(curwin)
432#endif
433 )
434 {
435 loff.lnum = curwin->w_cursor.lnum;
436#ifdef FEAT_DIFF
437 loff.fill = 0;
438 n = curwin->w_topfill; /* always have this context */
439#else
440 n = 0;
441#endif
442 /* Count the visible screen lines above the cursor line. */
443 while (n < p_so)
444 {
445 topline_back(&loff);
446 /* Stop when included a line above the window. */
447 if (loff.lnum < curwin->w_topline
448#ifdef FEAT_DIFF
449 || (loff.lnum == curwin->w_topline && loff.fill > 0)
450#endif
451 )
452 break;
453 n += loff.height;
454 }
455 if (n < p_so)
456 return TRUE;
457 }
458 return FALSE;
459}
460
461 void
462update_curswant()
463{
464 if (curwin->w_set_curswant)
465 {
466 validate_virtcol();
467 curwin->w_curswant = curwin->w_virtcol;
468 curwin->w_set_curswant = FALSE;
469 }
470}
471
472/*
473 * Check if the cursor has moved. Set the w_valid flag accordingly.
474 */
475 void
476check_cursor_moved(wp)
477 win_T *wp;
478{
479 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
480 {
481 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
482 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
483 wp->w_valid_cursor = wp->w_cursor;
484 wp->w_valid_leftcol = wp->w_leftcol;
485 }
486 else if (wp->w_cursor.col != wp->w_valid_cursor.col
487 || wp->w_leftcol != wp->w_valid_leftcol
488#ifdef FEAT_VIRTUALEDIT
489 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
490#endif
491 )
492 {
493 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
494 wp->w_valid_cursor.col = wp->w_cursor.col;
495 wp->w_valid_leftcol = wp->w_leftcol;
496#ifdef FEAT_VIRTUALEDIT
497 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
498#endif
499 }
500}
501
502/*
503 * Call this function when some window settings have changed, which require
504 * the cursor position, botline and topline to be recomputed and the window to
505 * be redrawn. E.g, when changing the 'wrap' option or folding.
506 */
507 void
508changed_window_setting()
509{
510 changed_window_setting_win(curwin);
511}
512
513 void
514changed_window_setting_win(wp)
515 win_T *wp;
516{
517 wp->w_lines_valid = 0;
518 changed_line_abv_curs_win(wp);
519 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
520 redraw_win_later(wp, NOT_VALID);
521}
522
523/*
524 * Set wp->w_topline to a certain number.
525 */
526 void
527set_topline(wp, lnum)
528 win_T *wp;
529 linenr_T lnum;
530{
531#ifdef FEAT_FOLDING
532 /* go to first of folded lines */
533 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
534#endif
535 /* Approximate the value of w_botline */
536 wp->w_botline += lnum - wp->w_topline;
537 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000538#ifdef FEAT_AUTOCMD
539 wp->w_topline_was_set = TRUE;
540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef FEAT_DIFF
542 wp->w_topfill = 0;
543#endif
544 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
545 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
546 redraw_later(VALID);
547}
548
549/*
550 * Call this function when the length of the cursor line (in screen
551 * characters) has changed, and the change is before the cursor.
552 * Need to take care of w_botline separately!
553 */
554 void
555changed_cline_bef_curs()
556{
557 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
558 |VALID_CHEIGHT|VALID_TOPLINE);
559}
560
561 void
562changed_cline_bef_curs_win(wp)
563 win_T *wp;
564{
565 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
566 |VALID_CHEIGHT|VALID_TOPLINE);
567}
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569/*
570 * Call this function when the length of a line (in screen characters) above
571 * the cursor have changed.
572 * Need to take care of w_botline separately!
573 */
574 void
575changed_line_abv_curs()
576{
577 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
578 |VALID_CHEIGHT|VALID_TOPLINE);
579}
580
581 void
582changed_line_abv_curs_win(wp)
583 win_T *wp;
584{
585 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
586 |VALID_CHEIGHT|VALID_TOPLINE);
587}
588
589/*
590 * Make sure the value of curwin->w_botline is valid.
591 */
592 void
593validate_botline()
594{
595 if (!(curwin->w_valid & VALID_BOTLINE))
596 comp_botline(curwin);
597}
598
599/*
600 * Make sure the value of wp->w_botline is valid.
601 */
602 static void
603validate_botline_win(wp)
604 win_T *wp;
605{
606 if (!(wp->w_valid & VALID_BOTLINE))
607 comp_botline(wp);
608}
609
610/*
611 * Mark curwin->w_botline as invalid (because of some change in the buffer).
612 */
613 void
614invalidate_botline()
615{
616 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
617}
618
619 void
620invalidate_botline_win(wp)
621 win_T *wp;
622{
623 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 void
627approximate_botline_win(wp)
628 win_T *wp;
629{
630 wp->w_valid &= ~VALID_BOTLINE;
631}
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633/*
634 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
635 */
636 int
637cursor_valid()
638{
639 check_cursor_moved(curwin);
640 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
641 (VALID_WROW|VALID_WCOL));
642}
643
644/*
645 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
646 * w_topline must be valid, you may need to call update_topline() first!
647 */
648 void
649validate_cursor()
650{
651 check_cursor_moved(curwin);
652 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
653 curs_columns(TRUE);
654}
655
656#if defined(FEAT_GUI) || defined(PROTO)
657/*
658 * validate w_cline_row.
659 */
660 void
661validate_cline_row()
662{
663 /*
664 * First make sure that w_topline is valid (after moving the cursor).
665 */
666 update_topline();
667 check_cursor_moved(curwin);
668 if (!(curwin->w_valid & VALID_CROW))
669 curs_rows(curwin, FALSE);
670}
671#endif
672
673/*
674 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200675 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 *
677 * Returns OK when cursor is in the window, FAIL when it isn't.
678 */
679 static void
680curs_rows(wp, do_botline)
681 win_T *wp;
682 int do_botline; /* also compute w_botline */
683{
684 linenr_T lnum;
685 int i;
686 int all_invalid;
687 int valid;
688#ifdef FEAT_FOLDING
689 long fold_count;
690#endif
691
692 /* Check if wp->w_lines[].wl_size is invalid */
693 all_invalid = (!redrawing()
694 || wp->w_lines_valid == 0
695 || wp->w_lines[0].wl_lnum > wp->w_topline);
696 i = 0;
697 wp->w_cline_row = 0;
698 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
699 {
700 valid = FALSE;
701 if (!all_invalid && i < wp->w_lines_valid)
702 {
703 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
704 continue; /* skip changed or deleted lines */
705 if (wp->w_lines[i].wl_lnum == lnum)
706 {
707#ifdef FEAT_FOLDING
708 /* Check for newly inserted lines below this row, in which
709 * case we need to check for folded lines. */
710 if (!wp->w_buffer->b_mod_set
711 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
712 || wp->w_buffer->b_mod_top
713 > wp->w_lines[i].wl_lastlnum + 1)
714#endif
715 valid = TRUE;
716 }
717 else if (wp->w_lines[i].wl_lnum > lnum)
718 --i; /* hold at inserted lines */
719 }
720 if (valid
721#ifdef FEAT_DIFF
722 && (lnum != wp->w_topline || !wp->w_p_diff)
723#endif
724 )
725 {
726#ifdef FEAT_FOLDING
727 lnum = wp->w_lines[i].wl_lastlnum + 1;
728 /* Cursor inside folded lines, don't count this row */
729 if (lnum > wp->w_cursor.lnum)
730 break;
731#else
732 ++lnum;
733#endif
734 wp->w_cline_row += wp->w_lines[i].wl_size;
735 }
736 else
737 {
738#ifdef FEAT_FOLDING
739 fold_count = foldedCount(wp, lnum, NULL);
740 if (fold_count)
741 {
742 lnum += fold_count;
743 if (lnum > wp->w_cursor.lnum)
744 break;
745 ++wp->w_cline_row;
746 }
747 else
748#endif
749#ifdef FEAT_DIFF
750 if (lnum == wp->w_topline)
751 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
752 + wp->w_topfill;
753 else
754#endif
755 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
756 }
757 }
758
759 check_cursor_moved(wp);
760 if (!(wp->w_valid & VALID_CHEIGHT))
761 {
762 if (all_invalid
763 || i == wp->w_lines_valid
764 || (i < wp->w_lines_valid
765 && (!wp->w_lines[i].wl_valid
766 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
767 {
768#ifdef FEAT_DIFF
769 if (wp->w_cursor.lnum == wp->w_topline)
770 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
771 TRUE) + wp->w_topfill;
772 else
773#endif
774 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
775#ifdef FEAT_FOLDING
776 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
777 NULL, NULL, TRUE, NULL);
778#endif
779 }
780 else if (i > wp->w_lines_valid)
781 {
782 /* a line that is too long to fit on the last screen line */
783 wp->w_cline_height = 0;
784#ifdef FEAT_FOLDING
785 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
786 NULL, NULL, TRUE, NULL);
787#endif
788 }
789 else
790 {
791 wp->w_cline_height = wp->w_lines[i].wl_size;
792#ifdef FEAT_FOLDING
793 wp->w_cline_folded = wp->w_lines[i].wl_folded;
794#endif
795 }
796 }
797
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100798 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
800
801 /* validate botline too, if update_screen doesn't do it */
802 if (do_botline && all_invalid)
803 validate_botline_win(wp);
804}
805
806/*
807 * Validate curwin->w_virtcol only.
808 */
809 void
810validate_virtcol()
811{
812 validate_virtcol_win(curwin);
813}
814
815/*
816 * Validate wp->w_virtcol only.
817 */
818 void
819validate_virtcol_win(wp)
820 win_T *wp;
821{
822 check_cursor_moved(wp);
823 if (!(wp->w_valid & VALID_VIRTCOL))
824 {
825 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
826 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000827#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000828 if (wp->w_p_cuc
829# ifdef FEAT_INS_EXPAND
830 && !pum_visible()
831# endif
832 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000833 redraw_win_later(wp, SOME_VALID);
834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836}
837
838/*
839 * Validate curwin->w_cline_height only.
840 */
841 static void
842validate_cheight()
843{
844 check_cursor_moved(curwin);
845 if (!(curwin->w_valid & VALID_CHEIGHT))
846 {
847#ifdef FEAT_DIFF
848 if (curwin->w_cursor.lnum == curwin->w_topline)
849 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
850 + curwin->w_topfill;
851 else
852#endif
853 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
854#ifdef FEAT_FOLDING
855 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
856#endif
857 curwin->w_valid |= VALID_CHEIGHT;
858 }
859}
860
861/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000862 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 */
864 void
865validate_cursor_col()
866{
867 colnr_T off;
868 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100869 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
871 validate_virtcol();
872 if (!(curwin->w_valid & VALID_WCOL))
873 {
874 col = curwin->w_virtcol;
875 off = curwin_col_off();
876 col += off;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100877 width = W_WIDTH(curwin) - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000880 if (curwin->w_p_wrap
881 && col >= (colnr_T)W_WIDTH(curwin)
Bram Moolenaar6427c602010-02-03 17:43:07 +0100882 && width > 0)
883 /* use same formula as what is used in curs_columns() */
884 col -= ((col - W_WIDTH(curwin)) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000885 if (col > (int)curwin->w_leftcol)
886 col -= curwin->w_leftcol;
887 else
888 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000890
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 curwin->w_valid |= VALID_WCOL;
892 }
893}
894
895/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200896 * Compute offset of a window, occupied by absolute or relative line number,
897 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 */
899 int
900win_col_off(wp)
901 win_T *wp;
902{
Bram Moolenaar64486672010-05-16 15:46:46 +0200903 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#ifdef FEAT_CMDWIN
905 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
906#endif
907#ifdef FEAT_FOLDING
908 + wp->w_p_fdc
909#endif
910#ifdef FEAT_SIGNS
911 + (
912# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200913 /* show glyph gutter in netbeans */
914 netbeans_active() ||
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915# endif
916 wp->w_buffer->b_signlist != NULL ? 2 : 0)
917#endif
918 );
919}
920
921 int
922curwin_col_off()
923{
924 return win_col_off(curwin);
925}
926
927/*
928 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200929 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
930 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 */
932 int
933win_col_off2(wp)
934 win_T *wp;
935{
Bram Moolenaar64486672010-05-16 15:46:46 +0200936 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000937 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 return 0;
939}
940
941 int
942curwin_col_off2()
943{
944 return win_col_off2(curwin);
945}
946
947/*
948 * compute curwin->w_wcol and curwin->w_virtcol.
949 * Also updates curwin->w_wrow and curwin->w_cline_row.
950 * Also updates curwin->w_leftcol.
951 */
952 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100953curs_columns(may_scroll)
954 int may_scroll; /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 int diff;
957 int extra; /* offset for first screen line */
958 int off_left, off_right;
959 int n;
960 int p_lines;
961 int width = 0;
962 int textwidth;
963 int new_leftcol;
964 colnr_T startcol;
965 colnr_T endcol;
966 colnr_T prev_skipcol;
967
968 /*
969 * First make sure that w_topline is valid (after moving the cursor).
970 */
971 update_topline();
972
973 /*
974 * Next make sure that w_cline_row is valid.
975 */
976 if (!(curwin->w_valid & VALID_CROW))
977 curs_rows(curwin, FALSE);
978
979 /*
980 * Compute the number of virtual columns.
981 */
982#ifdef FEAT_FOLDING
983 if (curwin->w_cline_folded)
984 /* In a folded line the cursor is always in the first column */
985 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
986 else
987#endif
988 getvvcol(curwin, &curwin->w_cursor,
989 &startcol, &(curwin->w_virtcol), &endcol);
990
991 /* remove '$' from change command when cursor moves onto it */
992 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100993 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 extra = curwin_col_off();
996 curwin->w_wcol = curwin->w_virtcol + extra;
997 endcol += extra;
998
999 /*
1000 * Now compute w_wrow, counting screen lines from w_cline_row.
1001 */
1002 curwin->w_wrow = curwin->w_cline_row;
1003
1004 textwidth = W_WIDTH(curwin) - extra;
1005 if (textwidth <= 0)
1006 {
1007 /* No room for text, put cursor in last char of window. */
1008 curwin->w_wcol = W_WIDTH(curwin) - 1;
1009 curwin->w_wrow = curwin->w_height - 1;
1010 }
1011 else if (curwin->w_p_wrap
1012#ifdef FEAT_VERTSPLIT
1013 && curwin->w_width != 0
1014#endif
1015 )
1016 {
1017 width = textwidth + curwin_col_off2();
1018
1019 /* long line wrapping, adjust curwin->w_wrow */
1020 if (curwin->w_wcol >= W_WIDTH(curwin))
1021 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001022 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 n = (curwin->w_wcol - W_WIDTH(curwin)) / width + 1;
1024 curwin->w_wcol -= n * width;
1025 curwin->w_wrow += n;
1026
1027#ifdef FEAT_LINEBREAK
1028 /* When cursor wraps to first char of next line in Insert
1029 * mode, the 'showbreak' string isn't shown, backup to first
1030 * column */
1031 if (*p_sbr && *ml_get_cursor() == NUL
1032 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1033 curwin->w_wcol = 0;
1034#endif
1035 }
1036 }
1037
1038 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1039 * is not folded.
1040 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001041 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#ifdef FEAT_FOLDING
1043 && !curwin->w_cline_folded
1044#endif
1045 )
1046 {
1047 /*
1048 * If Cursor is left of the screen, scroll rightwards.
1049 * If Cursor is right of the screen, scroll leftwards
1050 * If we get closer to the edge than 'sidescrolloff', scroll a little
1051 * extra
1052 */
1053 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
1054 off_right = (int)endcol - (int)(curwin->w_leftcol + W_WIDTH(curwin)
1055 - p_siso) + 1;
1056 if (off_left < 0 || off_right > 0)
1057 {
1058 if (off_left < 0)
1059 diff = -off_left;
1060 else
1061 diff = off_right;
1062
1063 /* When far off or not enough room on either side, put cursor in
1064 * middle of window. */
1065 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1066 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1067 else
1068 {
1069 if (diff < p_ss)
1070 diff = p_ss;
1071 if (off_left < 0)
1072 new_leftcol = curwin->w_leftcol - diff;
1073 else
1074 new_leftcol = curwin->w_leftcol + diff;
1075 }
1076 if (new_leftcol < 0)
1077 new_leftcol = 0;
1078 if (new_leftcol != (int)curwin->w_leftcol)
1079 {
1080 curwin->w_leftcol = new_leftcol;
1081 /* screen has to be redrawn with new curwin->w_leftcol */
1082 redraw_later(NOT_VALID);
1083 }
1084 }
1085 curwin->w_wcol -= curwin->w_leftcol;
1086 }
1087 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1088 curwin->w_wcol -= curwin->w_leftcol;
1089 else
1090 curwin->w_wcol = 0;
1091
1092#ifdef FEAT_DIFF
1093 /* Skip over filler lines. At the top use w_topfill, there
1094 * may be some filler lines above the window. */
1095 if (curwin->w_cursor.lnum == curwin->w_topline)
1096 curwin->w_wrow += curwin->w_topfill;
1097 else
1098 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1099#endif
1100
1101 prev_skipcol = curwin->w_skipcol;
1102
1103 p_lines = 0;
1104 if ((curwin->w_wrow >= curwin->w_height
1105 || ((prev_skipcol > 0
1106 || curwin->w_wrow + p_so >= curwin->w_height)
1107 && (p_lines =
1108#ifdef FEAT_DIFF
1109 plines_win_nofill
1110#else
1111 plines_win
1112#endif
1113 (curwin, curwin->w_cursor.lnum, FALSE))
1114 - 1 >= curwin->w_height))
1115 && curwin->w_height != 0
1116 && curwin->w_cursor.lnum == curwin->w_topline
1117 && width > 0
1118#ifdef FEAT_VERTSPLIT
1119 && curwin->w_width != 0
1120#endif
1121 )
1122 {
1123 /* Cursor past end of screen. Happens with a single line that does
1124 * not fit on screen. Find a skipcol to show the text around the
1125 * cursor. Avoid scrolling all the time. compute value of "extra":
1126 * 1: Less than "p_so" lines above
1127 * 2: Less than "p_so" lines below
1128 * 3: both of them */
1129 extra = 0;
1130 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1131 extra = 1;
1132 /* Compute last display line of the buffer line that we want at the
1133 * bottom of the window. */
1134 if (p_lines == 0)
1135 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1136 --p_lines;
1137 if (p_lines > curwin->w_wrow + p_so)
1138 n = curwin->w_wrow + p_so;
1139 else
1140 n = p_lines;
1141 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1142 extra += 2;
1143
1144 if (extra == 3 || p_lines < p_so * 2)
1145 {
1146 /* not enough room for 'scrolloff', put cursor in the middle */
1147 n = curwin->w_virtcol / width;
1148 if (n > curwin->w_height / 2)
1149 n -= curwin->w_height / 2;
1150 else
1151 n = 0;
1152 /* don't skip more than necessary */
1153 if (n > p_lines - curwin->w_height + 1)
1154 n = p_lines - curwin->w_height + 1;
1155 curwin->w_skipcol = n * width;
1156 }
1157 else if (extra == 1)
1158 {
1159 /* less then 'scrolloff' lines above, decrease skipcol */
1160 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1161 + width - 1) / width;
1162 if (extra > 0)
1163 {
1164 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1165 extra = curwin->w_skipcol / width;
1166 curwin->w_skipcol -= extra * width;
1167 }
1168 }
1169 else if (extra == 2)
1170 {
1171 /* less then 'scrolloff' lines below, increase skipcol */
1172 endcol = (n - curwin->w_height + 1) * width;
1173 while (endcol > curwin->w_virtcol)
1174 endcol -= width;
1175 if (endcol > curwin->w_skipcol)
1176 curwin->w_skipcol = endcol;
1177 }
1178
1179 curwin->w_wrow -= curwin->w_skipcol / width;
1180 if (curwin->w_wrow >= curwin->w_height)
1181 {
1182 /* small window, make sure cursor is in it */
1183 extra = curwin->w_wrow - curwin->w_height + 1;
1184 curwin->w_skipcol += extra * width;
1185 curwin->w_wrow -= extra;
1186 }
1187
1188 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1189 if (extra > 0)
1190 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1191 else if (extra < 0)
1192 win_del_lines(curwin, 0, -extra, FALSE, FALSE);
1193 }
1194 else
1195 curwin->w_skipcol = 0;
1196 if (prev_skipcol != curwin->w_skipcol)
1197 redraw_later(NOT_VALID);
1198
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001199#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001200 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1201 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001202# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001203 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001204# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001205 )
1206 redraw_later(SOME_VALID);
1207#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1210}
1211
1212/*
1213 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 void
1216scrolldown(line_count, byfold)
1217 long line_count;
Bram Moolenaar78a15312009-05-15 19:33:18 +00001218 int byfold UNUSED; /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220 long done = 0; /* total # of physical lines done */
1221 int wrow;
1222 int moved = FALSE;
1223
1224#ifdef FEAT_FOLDING
1225 linenr_T first;
1226
1227 /* Make sure w_topline is at the first of a sequence of folded lines. */
1228 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1229#endif
1230 validate_cursor(); /* w_wrow needs to be valid */
1231 while (line_count-- > 0)
1232 {
1233#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001234 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1235 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 ++curwin->w_topfill;
1238 ++done;
1239 }
1240 else
1241#endif
1242 {
1243 if (curwin->w_topline == 1)
1244 break;
1245 --curwin->w_topline;
1246#ifdef FEAT_DIFF
1247 curwin->w_topfill = 0;
1248#endif
1249#ifdef FEAT_FOLDING
1250 /* A sequence of folded lines only counts for one logical line */
1251 if (hasFolding(curwin->w_topline, &first, NULL))
1252 {
1253 ++done;
1254 if (!byfold)
1255 line_count -= curwin->w_topline - first - 1;
1256 curwin->w_botline -= curwin->w_topline - first;
1257 curwin->w_topline = first;
1258 }
1259 else
1260#endif
1261#ifdef FEAT_DIFF
1262 done += plines_nofill(curwin->w_topline);
1263#else
1264 done += plines(curwin->w_topline);
1265#endif
1266 }
1267 --curwin->w_botline; /* approximate w_botline */
1268 invalidate_botline();
1269 }
1270 curwin->w_wrow += done; /* keep w_wrow updated */
1271 curwin->w_cline_row += done; /* keep w_cline_row updated */
1272
1273#ifdef FEAT_DIFF
1274 if (curwin->w_cursor.lnum == curwin->w_topline)
1275 curwin->w_cline_row = 0;
1276 check_topfill(curwin, TRUE);
1277#endif
1278
1279 /*
1280 * Compute the row number of the last row of the cursor line
1281 * and move the cursor onto the displayed part of the window.
1282 */
1283 wrow = curwin->w_wrow;
1284 if (curwin->w_p_wrap
1285#ifdef FEAT_VERTSPLIT
1286 && curwin->w_width != 0
1287#endif
1288 )
1289 {
1290 validate_virtcol();
1291 validate_cheight();
1292 wrow += curwin->w_cline_height - 1 -
1293 curwin->w_virtcol / W_WIDTH(curwin);
1294 }
1295 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1296 {
1297#ifdef FEAT_FOLDING
1298 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1299 {
1300 --wrow;
1301 if (first == 1)
1302 curwin->w_cursor.lnum = 1;
1303 else
1304 curwin->w_cursor.lnum = first - 1;
1305 }
1306 else
1307#endif
1308 wrow -= plines(curwin->w_cursor.lnum--);
1309 curwin->w_valid &=
1310 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1311 moved = TRUE;
1312 }
1313 if (moved)
1314 {
1315#ifdef FEAT_FOLDING
1316 /* Move cursor to first line of closed fold. */
1317 foldAdjustCursor();
1318#endif
1319 coladvance(curwin->w_curswant);
1320 }
1321}
1322
1323/*
1324 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 void
1327scrollup(line_count, byfold)
1328 long line_count;
Bram Moolenaar78a15312009-05-15 19:33:18 +00001329 int byfold UNUSED; /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1332 linenr_T lnum;
1333
1334 if (
1335# ifdef FEAT_FOLDING
1336 (byfold && hasAnyFolding(curwin))
1337# ifdef FEAT_DIFF
1338 ||
1339# endif
1340# endif
1341# ifdef FEAT_DIFF
1342 curwin->w_p_diff
1343# endif
1344 )
1345 {
1346 /* count each sequence of folded lines as one logical line */
1347 lnum = curwin->w_topline;
1348 while (line_count--)
1349 {
1350# ifdef FEAT_DIFF
1351 if (curwin->w_topfill > 0)
1352 --curwin->w_topfill;
1353 else
1354# endif
1355 {
1356# ifdef FEAT_FOLDING
1357 if (byfold)
1358 (void)hasFolding(lnum, NULL, &lnum);
1359# endif
1360 if (lnum >= curbuf->b_ml.ml_line_count)
1361 break;
1362 ++lnum;
1363# ifdef FEAT_DIFF
1364 curwin->w_topfill = diff_check_fill(curwin, lnum);
1365# endif
1366 }
1367 }
1368 /* approximate w_botline */
1369 curwin->w_botline += lnum - curwin->w_topline;
1370 curwin->w_topline = lnum;
1371 }
1372 else
1373#endif
1374 {
1375 curwin->w_topline += line_count;
1376 curwin->w_botline += line_count; /* approximate w_botline */
1377 }
1378
1379 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1380 curwin->w_topline = curbuf->b_ml.ml_line_count;
1381 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1382 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1383
1384#ifdef FEAT_DIFF
1385 check_topfill(curwin, FALSE);
1386#endif
1387
1388#ifdef FEAT_FOLDING
1389 if (hasAnyFolding(curwin))
1390 /* Make sure w_topline is at the first of a sequence of folded lines. */
1391 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1392#endif
1393
1394 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1395 if (curwin->w_cursor.lnum < curwin->w_topline)
1396 {
1397 curwin->w_cursor.lnum = curwin->w_topline;
1398 curwin->w_valid &=
1399 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1400 coladvance(curwin->w_curswant);
1401 }
1402}
1403
1404#ifdef FEAT_DIFF
1405/*
1406 * Don't end up with too many filler lines in the window.
1407 */
1408 void
1409check_topfill(wp, down)
1410 win_T *wp;
1411 int down; /* when TRUE scroll down when not enough space */
1412{
1413 int n;
1414
1415 if (wp->w_topfill > 0)
1416 {
1417 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1418 if (wp->w_topfill + n > wp->w_height)
1419 {
1420 if (down && wp->w_topline > 1)
1421 {
1422 --wp->w_topline;
1423 wp->w_topfill = 0;
1424 }
1425 else
1426 {
1427 wp->w_topfill = wp->w_height - n;
1428 if (wp->w_topfill < 0)
1429 wp->w_topfill = 0;
1430 }
1431 }
1432 }
1433}
1434
1435/*
1436 * Use as many filler lines as possible for w_topline. Make sure w_topline
1437 * is still visible.
1438 */
1439 static void
1440max_topfill()
1441{
1442 int n;
1443
1444 n = plines_nofill(curwin->w_topline);
1445 if (n >= curwin->w_height)
1446 curwin->w_topfill = 0;
1447 else
1448 {
1449 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1450 if (curwin->w_topfill + n > curwin->w_height)
1451 curwin->w_topfill = curwin->w_height - n;
1452 }
1453}
1454#endif
1455
1456#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1457/*
1458 * Scroll the screen one line down, but don't do it if it would move the
1459 * cursor off the screen.
1460 */
1461 void
1462scrolldown_clamp()
1463{
1464 int end_row;
1465#ifdef FEAT_DIFF
1466 int can_fill = (curwin->w_topfill
1467 < diff_check_fill(curwin, curwin->w_topline));
1468#endif
1469
1470 if (curwin->w_topline <= 1
1471#ifdef FEAT_DIFF
1472 && !can_fill
1473#endif
1474 )
1475 return;
1476
1477 validate_cursor(); /* w_wrow needs to be valid */
1478
1479 /*
1480 * Compute the row number of the last row of the cursor line
1481 * and make sure it doesn't go off the screen. Make sure the cursor
1482 * doesn't go past 'scrolloff' lines from the screen end.
1483 */
1484 end_row = curwin->w_wrow;
1485#ifdef FEAT_DIFF
1486 if (can_fill)
1487 ++end_row;
1488 else
1489 end_row += plines_nofill(curwin->w_topline - 1);
1490#else
1491 end_row += plines(curwin->w_topline - 1);
1492#endif
1493 if (curwin->w_p_wrap
1494#ifdef FEAT_VERTSPLIT
1495 && curwin->w_width != 0
1496#endif
1497 )
1498 {
1499 validate_cheight();
1500 validate_virtcol();
1501 end_row += curwin->w_cline_height - 1 -
1502 curwin->w_virtcol / W_WIDTH(curwin);
1503 }
1504 if (end_row < curwin->w_height - p_so)
1505 {
1506#ifdef FEAT_DIFF
1507 if (can_fill)
1508 {
1509 ++curwin->w_topfill;
1510 check_topfill(curwin, TRUE);
1511 }
1512 else
1513 {
1514 --curwin->w_topline;
1515 curwin->w_topfill = 0;
1516 }
1517#else
1518 --curwin->w_topline;
1519#endif
1520#ifdef FEAT_FOLDING
1521 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1522#endif
1523 --curwin->w_botline; /* approximate w_botline */
1524 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1525 }
1526}
1527
1528/*
1529 * Scroll the screen one line up, but don't do it if it would move the cursor
1530 * off the screen.
1531 */
1532 void
1533scrollup_clamp()
1534{
1535 int start_row;
1536
1537 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1538#ifdef FEAT_DIFF
1539 && curwin->w_topfill == 0
1540#endif
1541 )
1542 return;
1543
1544 validate_cursor(); /* w_wrow needs to be valid */
1545
1546 /*
1547 * Compute the row number of the first row of the cursor line
1548 * and make sure it doesn't go off the screen. Make sure the cursor
1549 * doesn't go before 'scrolloff' lines from the screen start.
1550 */
1551#ifdef FEAT_DIFF
1552 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1553 - curwin->w_topfill;
1554#else
1555 start_row = curwin->w_wrow - plines(curwin->w_topline);
1556#endif
1557 if (curwin->w_p_wrap
1558#ifdef FEAT_VERTSPLIT
1559 && curwin->w_width != 0
1560#endif
1561 )
1562 {
1563 validate_virtcol();
1564 start_row -= curwin->w_virtcol / W_WIDTH(curwin);
1565 }
1566 if (start_row >= p_so)
1567 {
1568#ifdef FEAT_DIFF
1569 if (curwin->w_topfill > 0)
1570 --curwin->w_topfill;
1571 else
1572#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001573 {
1574#ifdef FEAT_FOLDING
1575 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 ++curwin->w_botline; /* approximate w_botline */
1580 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1581 }
1582}
1583#endif /* FEAT_INS_EXPAND */
1584
1585/*
1586 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1587 * a (wrapped) text line. Uses and sets "lp->fill".
1588 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001589 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
1592topline_back(lp)
1593 lineoff_T *lp;
1594{
1595#ifdef FEAT_DIFF
1596 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1597 {
1598 /* Add a filler line. */
1599 ++lp->fill;
1600 lp->height = 1;
1601 }
1602 else
1603#endif
1604 {
1605 --lp->lnum;
1606#ifdef FEAT_DIFF
1607 lp->fill = 0;
1608#endif
1609 if (lp->lnum < 1)
1610 lp->height = MAXCOL;
1611 else
1612#ifdef FEAT_FOLDING
1613 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1614 /* Add a closed fold */
1615 lp->height = 1;
1616 else
1617#endif
1618 {
1619#ifdef FEAT_DIFF
1620 lp->height = plines_nofill(lp->lnum);
1621#else
1622 lp->height = plines(lp->lnum);
1623#endif
1624 }
1625 }
1626}
1627
1628/*
1629 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1630 * a (wrapped) text line. Uses and sets "lp->fill".
1631 * Returns the height of the added line in "lp->height".
1632 * Lines below the last one are incredibly high.
1633 */
1634 static void
1635botline_forw(lp)
1636 lineoff_T *lp;
1637{
1638#ifdef FEAT_DIFF
1639 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1640 {
1641 /* Add a filler line. */
1642 ++lp->fill;
1643 lp->height = 1;
1644 }
1645 else
1646#endif
1647 {
1648 ++lp->lnum;
1649#ifdef FEAT_DIFF
1650 lp->fill = 0;
1651#endif
1652 if (lp->lnum > curbuf->b_ml.ml_line_count)
1653 lp->height = MAXCOL;
1654 else
1655#ifdef FEAT_FOLDING
1656 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1657 /* Add a closed fold */
1658 lp->height = 1;
1659 else
1660#endif
1661 {
1662#ifdef FEAT_DIFF
1663 lp->height = plines_nofill(lp->lnum);
1664#else
1665 lp->height = plines(lp->lnum);
1666#endif
1667 }
1668 }
1669}
1670
1671#ifdef FEAT_DIFF
1672/*
1673 * Switch from including filler lines below lp->lnum to including filler
1674 * lines above loff.lnum + 1. This keeps pointing to the same line.
1675 * When there are no filler lines nothing changes.
1676 */
1677 static void
1678botline_topline(lp)
1679 lineoff_T *lp;
1680{
1681 if (lp->fill > 0)
1682 {
1683 ++lp->lnum;
1684 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1685 }
1686}
1687
1688/*
1689 * Switch from including filler lines above lp->lnum to including filler
1690 * lines below loff.lnum - 1. This keeps pointing to the same line.
1691 * When there are no filler lines nothing changes.
1692 */
1693 static void
1694topline_botline(lp)
1695 lineoff_T *lp;
1696{
1697 if (lp->fill > 0)
1698 {
1699 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1700 --lp->lnum;
1701 }
1702}
1703#endif
1704
1705/*
1706 * Recompute topline to put the cursor at the top of the window.
1707 * Scroll at least "min_scroll" lines.
1708 * If "always" is TRUE, always set topline (for "zt").
1709 */
1710 void
1711scroll_cursor_top(min_scroll, always)
1712 int min_scroll;
1713 int always;
1714{
1715 int scrolled = 0;
1716 int extra = 0;
1717 int used;
1718 int i;
1719 linenr_T top; /* just above displayed lines */
1720 linenr_T bot; /* just below displayed lines */
1721 linenr_T old_topline = curwin->w_topline;
1722#ifdef FEAT_DIFF
1723 linenr_T old_topfill = curwin->w_topfill;
1724#endif
1725 linenr_T new_topline;
1726 int off = p_so;
1727
1728#ifdef FEAT_MOUSE
1729 if (mouse_dragging > 0)
1730 off = mouse_dragging - 1;
1731#endif
1732
1733 /*
1734 * Decrease topline until:
1735 * - it has become 1
1736 * - (part of) the cursor line is moved off the screen or
1737 * - moved at least 'scrolljump' lines and
1738 * - at least 'scrolloff' lines above and below the cursor
1739 */
1740 validate_cheight();
1741 used = curwin->w_cline_height;
1742 if (curwin->w_cursor.lnum < curwin->w_topline)
1743 scrolled = used;
1744
1745#ifdef FEAT_FOLDING
1746 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1747 {
1748 --top;
1749 ++bot;
1750 }
1751 else
1752#endif
1753 {
1754 top = curwin->w_cursor.lnum - 1;
1755 bot = curwin->w_cursor.lnum + 1;
1756 }
1757 new_topline = top + 1;
1758
1759#ifdef FEAT_DIFF
1760 /* count filler lines of the cursor window as context */
1761 i = diff_check_fill(curwin, curwin->w_cursor.lnum);
1762 used += i;
1763 extra += i;
1764#endif
1765
1766 /*
1767 * Check if the lines from "top" to "bot" fit in the window. If they do,
1768 * set new_topline and advance "top" and "bot" to include more lines.
1769 */
1770 while (top > 0)
1771 {
1772#ifdef FEAT_FOLDING
1773 if (hasFolding(top, &top, NULL))
1774 /* count one logical line for a sequence of folded lines */
1775 i = 1;
1776 else
1777#endif
1778 i = plines(top);
1779 used += i;
1780 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1781 {
1782#ifdef FEAT_FOLDING
1783 if (hasFolding(bot, NULL, &bot))
1784 /* count one logical line for a sequence of folded lines */
1785 ++used;
1786 else
1787#endif
1788 used += plines(bot);
1789 }
1790 if (used > curwin->w_height)
1791 break;
1792 if (top < curwin->w_topline)
1793 scrolled += i;
1794
1795 /*
1796 * If scrolling is needed, scroll at least 'sj' lines.
1797 */
1798 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1799 && extra >= off)
1800 break;
1801
1802 extra += i;
1803 new_topline = top;
1804 --top;
1805 ++bot;
1806 }
1807
1808 /*
1809 * If we don't have enough space, put cursor in the middle.
1810 * This makes sure we get the same position when using "k" and "j"
1811 * in a small window.
1812 */
1813 if (used > curwin->w_height)
1814 scroll_cursor_halfway(FALSE);
1815 else
1816 {
1817 /*
1818 * If "always" is FALSE, only adjust topline to a lower value, higher
1819 * value may happen with wrapping lines
1820 */
1821 if (new_topline < curwin->w_topline || always)
1822 curwin->w_topline = new_topline;
1823 if (curwin->w_topline > curwin->w_cursor.lnum)
1824 curwin->w_topline = curwin->w_cursor.lnum;
1825#ifdef FEAT_DIFF
1826 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1827 if (curwin->w_topfill > 0 && extra > off)
1828 {
1829 curwin->w_topfill -= extra - off;
1830 if (curwin->w_topfill < 0)
1831 curwin->w_topfill = 0;
1832 }
1833 check_topfill(curwin, FALSE);
1834#endif
1835 if (curwin->w_topline != old_topline
1836#ifdef FEAT_DIFF
1837 || curwin->w_topfill != old_topfill
1838#endif
1839 )
1840 curwin->w_valid &=
1841 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1842 curwin->w_valid |= VALID_TOPLINE;
1843 }
1844}
1845
1846/*
1847 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1848 * screen lines for text lines.
1849 */
1850 void
1851set_empty_rows(wp, used)
1852 win_T *wp;
1853 int used;
1854{
1855#ifdef FEAT_DIFF
1856 wp->w_filler_rows = 0;
1857#endif
1858 if (used == 0)
1859 wp->w_empty_rows = 0; /* single line that doesn't fit */
1860 else
1861 {
1862 wp->w_empty_rows = wp->w_height - used;
1863#ifdef FEAT_DIFF
1864 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1865 {
1866 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1867 if (wp->w_empty_rows > wp->w_filler_rows)
1868 wp->w_empty_rows -= wp->w_filler_rows;
1869 else
1870 {
1871 wp->w_filler_rows = wp->w_empty_rows;
1872 wp->w_empty_rows = 0;
1873 }
1874 }
1875#endif
1876 }
1877}
1878
1879/*
1880 * Recompute topline to put the cursor at the bottom of the window.
1881 * Scroll at least "min_scroll" lines.
1882 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1883 * This is messy stuff!!!
1884 */
1885 void
1886scroll_cursor_bot(min_scroll, set_topbot)
1887 int min_scroll;
1888 int set_topbot;
1889{
1890 int used;
1891 int scrolled = 0;
1892 int extra = 0;
1893 int i;
1894 linenr_T line_count;
1895 linenr_T old_topline = curwin->w_topline;
1896 lineoff_T loff;
1897 lineoff_T boff;
1898#ifdef FEAT_DIFF
1899 int old_topfill = curwin->w_topfill;
1900 int fill_below_window;
1901#endif
1902 linenr_T old_botline = curwin->w_botline;
1903 linenr_T old_valid = curwin->w_valid;
1904 int old_empty_rows = curwin->w_empty_rows;
1905 linenr_T cln; /* Cursor Line Number */
1906
1907 cln = curwin->w_cursor.lnum;
1908 if (set_topbot)
1909 {
1910 used = 0;
1911 curwin->w_botline = cln + 1;
1912#ifdef FEAT_DIFF
1913 loff.fill = 0;
1914#endif
1915 for (curwin->w_topline = curwin->w_botline;
1916 curwin->w_topline > 1;
1917 curwin->w_topline = loff.lnum)
1918 {
1919 loff.lnum = curwin->w_topline;
1920 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001921 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 break;
1923 used += loff.height;
1924#ifdef FEAT_DIFF
1925 curwin->w_topfill = loff.fill;
1926#endif
1927 }
1928 set_empty_rows(curwin, used);
1929 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1930 if (curwin->w_topline != old_topline
1931#ifdef FEAT_DIFF
1932 || curwin->w_topfill != old_topfill
1933#endif
1934 )
1935 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1936 }
1937 else
1938 validate_botline();
1939
1940 /* The lines of the cursor line itself are always used. */
1941#ifdef FEAT_DIFF
1942 used = plines_nofill(cln);
1943#else
1944 validate_cheight();
1945 used = curwin->w_cline_height;
1946#endif
1947
1948 /* If the cursor is below botline, we will at least scroll by the height
1949 * of the cursor line. Correct for empty lines, which are really part of
1950 * botline. */
1951 if (cln >= curwin->w_botline)
1952 {
1953 scrolled = used;
1954 if (cln == curwin->w_botline)
1955 scrolled -= curwin->w_empty_rows;
1956 }
1957
1958 /*
1959 * Stop counting lines to scroll when
1960 * - hitting start of the file
1961 * - scrolled nothing or at least 'sj' lines
1962 * - at least 'so' lines below the cursor
1963 * - lines between botline and cursor have been counted
1964 */
1965#ifdef FEAT_FOLDING
1966 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1967#endif
1968 {
1969 loff.lnum = cln;
1970 boff.lnum = cln;
1971 }
1972#ifdef FEAT_DIFF
1973 loff.fill = 0;
1974 boff.fill = 0;
1975 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1976 - curwin->w_filler_rows;
1977#endif
1978
1979 while (loff.lnum > 1)
1980 {
1981 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1982 * context for 'scrolloff' and counted all lines below the window. */
1983 if ((((scrolled <= 0 || scrolled >= min_scroll)
1984 && extra >= (
1985#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001986 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#endif
1988 p_so))
1989 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1990 && loff.lnum <= curwin->w_botline
1991#ifdef FEAT_DIFF
1992 && (loff.lnum < curwin->w_botline
1993 || loff.fill >= fill_below_window)
1994#endif
1995 )
1996 break;
1997
1998 /* Add one line above */
1999 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002000 if (loff.height == MAXCOL)
2001 used = MAXCOL;
2002 else
2003 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (used > curwin->w_height)
2005 break;
2006 if (loff.lnum >= curwin->w_botline
2007#ifdef FEAT_DIFF
2008 && (loff.lnum > curwin->w_botline
2009 || loff.fill <= fill_below_window)
2010#endif
2011 )
2012 {
2013 /* Count screen lines that are below the window. */
2014 scrolled += loff.height;
2015 if (loff.lnum == curwin->w_botline
2016#ifdef FEAT_DIFF
2017 && boff.fill == 0
2018#endif
2019 )
2020 scrolled -= curwin->w_empty_rows;
2021 }
2022
2023 if (boff.lnum < curbuf->b_ml.ml_line_count)
2024 {
2025 /* Add one line below */
2026 botline_forw(&boff);
2027 used += boff.height;
2028 if (used > curwin->w_height)
2029 break;
2030 if (extra < (
2031#ifdef FEAT_MOUSE
2032 mouse_dragging > 0 ? mouse_dragging - 1 :
2033#endif
2034 p_so) || scrolled < min_scroll)
2035 {
2036 extra += boff.height;
2037 if (boff.lnum >= curwin->w_botline
2038#ifdef FEAT_DIFF
2039 || (boff.lnum + 1 == curwin->w_botline
2040 && boff.fill > curwin->w_filler_rows)
2041#endif
2042 )
2043 {
2044 /* Count screen lines that are below the window. */
2045 scrolled += boff.height;
2046 if (boff.lnum == curwin->w_botline
2047#ifdef FEAT_DIFF
2048 && boff.fill == 0
2049#endif
2050 )
2051 scrolled -= curwin->w_empty_rows;
2052 }
2053 }
2054 }
2055 }
2056
2057 /* curwin->w_empty_rows is larger, no need to scroll */
2058 if (scrolled <= 0)
2059 line_count = 0;
2060 /* more than a screenfull, don't scroll but redraw */
2061 else if (used > curwin->w_height)
2062 line_count = used;
2063 /* scroll minimal number of lines */
2064 else
2065 {
2066 line_count = 0;
2067#ifdef FEAT_DIFF
2068 boff.fill = curwin->w_topfill;
2069#endif
2070 boff.lnum = curwin->w_topline - 1;
2071 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2072 {
2073 botline_forw(&boff);
2074 i += boff.height;
2075 ++line_count;
2076 }
2077 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2078 line_count = 9999;
2079 }
2080
2081 /*
2082 * Scroll up if the cursor is off the bottom of the screen a bit.
2083 * Otherwise put it at 1/2 of the screen.
2084 */
2085 if (line_count >= curwin->w_height && line_count > min_scroll)
2086 scroll_cursor_halfway(FALSE);
2087 else
2088 scrollup(line_count, TRUE);
2089
2090 /*
2091 * If topline didn't change we need to restore w_botline and w_empty_rows
2092 * (we changed them).
2093 * If topline did change, update_screen() will set botline.
2094 */
2095 if (curwin->w_topline == old_topline && set_topbot)
2096 {
2097 curwin->w_botline = old_botline;
2098 curwin->w_empty_rows = old_empty_rows;
2099 curwin->w_valid = old_valid;
2100 }
2101 curwin->w_valid |= VALID_TOPLINE;
2102}
2103
2104/*
2105 * Recompute topline to put the cursor halfway the window
2106 * If "atend" is TRUE, also put it halfway at the end of the file.
2107 */
2108 void
2109scroll_cursor_halfway(atend)
2110 int atend;
2111{
2112 int above = 0;
2113 linenr_T topline;
2114#ifdef FEAT_DIFF
2115 int topfill = 0;
2116#endif
2117 int below = 0;
2118 int used;
2119 lineoff_T loff;
2120 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002121#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002122 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2126#ifdef FEAT_FOLDING
2127 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2128#endif
2129#ifdef FEAT_DIFF
2130 used = plines_nofill(loff.lnum);
2131 loff.fill = 0;
2132 boff.fill = 0;
2133#else
2134 used = plines(loff.lnum);
2135#endif
2136 topline = loff.lnum;
2137 while (topline > 1)
2138 {
2139 if (below <= above) /* add a line below the cursor first */
2140 {
2141 if (boff.lnum < curbuf->b_ml.ml_line_count)
2142 {
2143 botline_forw(&boff);
2144 used += boff.height;
2145 if (used > curwin->w_height)
2146 break;
2147 below += boff.height;
2148 }
2149 else
2150 {
2151 ++below; /* count a "~" line */
2152 if (atend)
2153 ++used;
2154 }
2155 }
2156
2157 if (below > above) /* add a line above the cursor */
2158 {
2159 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002160 if (loff.height == MAXCOL)
2161 used = MAXCOL;
2162 else
2163 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (used > curwin->w_height)
2165 break;
2166 above += loff.height;
2167 topline = loff.lnum;
2168#ifdef FEAT_DIFF
2169 topfill = loff.fill;
2170#endif
2171 }
2172 }
2173#ifdef FEAT_FOLDING
2174 if (!hasFolding(topline, &curwin->w_topline, NULL))
2175#endif
2176 curwin->w_topline = topline;
2177#ifdef FEAT_DIFF
2178 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002179 if (old_topline > curwin->w_topline + curwin->w_height)
2180 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 check_topfill(curwin, FALSE);
2182#endif
2183 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2184 curwin->w_valid |= VALID_TOPLINE;
2185}
2186
2187/*
2188 * Correct the cursor position so that it is in a part of the screen at least
2189 * 'so' lines from the top and bottom, if possible.
2190 * If not possible, put it at the same position as scroll_cursor_halfway().
2191 * When called topline must be valid!
2192 */
2193 void
2194cursor_correct()
2195{
2196 int above = 0; /* screen lines above topline */
2197 linenr_T topline;
2198 int below = 0; /* screen lines below botline */
2199 linenr_T botline;
2200 int above_wanted, below_wanted;
2201 linenr_T cln; /* Cursor Line Number */
2202 int max_off;
2203
2204 /*
2205 * How many lines we would like to have above/below the cursor depends on
2206 * whether the first/last line of the file is on screen.
2207 */
2208 above_wanted = p_so;
2209 below_wanted = p_so;
2210#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002211 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 above_wanted = mouse_dragging - 1;
2214 below_wanted = mouse_dragging - 1;
2215 }
2216#endif
2217 if (curwin->w_topline == 1)
2218 {
2219 above_wanted = 0;
2220 max_off = curwin->w_height / 2;
2221 if (below_wanted > max_off)
2222 below_wanted = max_off;
2223 }
2224 validate_botline();
2225 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2226#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002227 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228#endif
2229 )
2230 {
2231 below_wanted = 0;
2232 max_off = (curwin->w_height - 1) / 2;
2233 if (above_wanted > max_off)
2234 above_wanted = max_off;
2235 }
2236
2237 /*
2238 * If there are sufficient file-lines above and below the cursor, we can
2239 * return now.
2240 */
2241 cln = curwin->w_cursor.lnum;
2242 if (cln >= curwin->w_topline + above_wanted
2243 && cln < curwin->w_botline - below_wanted
2244#ifdef FEAT_FOLDING
2245 && !hasAnyFolding(curwin)
2246#endif
2247 )
2248 return;
2249
2250 /*
2251 * Narrow down the area where the cursor can be put by taking lines from
2252 * the top and the bottom until:
2253 * - the desired context lines are found
2254 * - the lines from the top is past the lines from the bottom
2255 */
2256 topline = curwin->w_topline;
2257 botline = curwin->w_botline - 1;
2258#ifdef FEAT_DIFF
2259 /* count filler lines as context */
2260 above = curwin->w_topfill;
2261 below = curwin->w_filler_rows;
2262#endif
2263 while ((above < above_wanted || below < below_wanted) && topline < botline)
2264 {
2265 if (below < below_wanted && (below <= above || above >= above_wanted))
2266 {
2267#ifdef FEAT_FOLDING
2268 if (hasFolding(botline, &botline, NULL))
2269 ++below;
2270 else
2271#endif
2272 below += plines(botline);
2273 --botline;
2274 }
2275 if (above < above_wanted && (above < below || below >= below_wanted))
2276 {
2277#ifdef FEAT_FOLDING
2278 if (hasFolding(topline, NULL, &topline))
2279 ++above;
2280 else
2281#endif
2282#ifndef FEAT_DIFF
2283 above += plines(topline);
2284#else
2285 above += plines_nofill(topline);
2286
2287 /* Count filler lines below this line as context. */
2288 if (topline < botline)
2289 above += diff_check_fill(curwin, topline + 1);
2290#endif
2291 ++topline;
2292 }
2293 }
2294 if (topline == botline || botline == 0)
2295 curwin->w_cursor.lnum = topline;
2296 else if (topline > botline)
2297 curwin->w_cursor.lnum = botline;
2298 else
2299 {
2300 if (cln < topline && curwin->w_topline > 1)
2301 {
2302 curwin->w_cursor.lnum = topline;
2303 curwin->w_valid &=
2304 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2305 }
2306 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2307 {
2308 curwin->w_cursor.lnum = botline;
2309 curwin->w_valid &=
2310 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2311 }
2312 }
2313 curwin->w_valid |= VALID_TOPLINE;
2314}
2315
2316static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir));
2317
2318/*
2319 * move screen 'count' pages up or down and update screen
2320 *
2321 * return FAIL for failure, OK otherwise
2322 */
2323 int
2324onepage(dir, count)
2325 int dir;
2326 long count;
2327{
2328 long n;
2329 int retval = OK;
2330 lineoff_T loff;
2331 linenr_T old_topline = curwin->w_topline;
2332
2333 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2334 {
2335 beep_flush();
2336 return FAIL;
2337 }
2338
2339 for ( ; count > 0; --count)
2340 {
2341 validate_botline();
2342 /*
2343 * It's an error to move a page up when the first line is already on
2344 * the screen. It's an error to move a page down when the last line
2345 * is on the screen and the topline is 'scrolloff' lines from the
2346 * last line.
2347 */
2348 if (dir == FORWARD
2349 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2350 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2351 : (curwin->w_topline == 1
2352#ifdef FEAT_DIFF
2353 && curwin->w_topfill ==
2354 diff_check_fill(curwin, curwin->w_topline)
2355#endif
2356 ))
2357 {
2358 beep_flush();
2359 retval = FAIL;
2360 break;
2361 }
2362
2363#ifdef FEAT_DIFF
2364 loff.fill = 0;
2365#endif
2366 if (dir == FORWARD)
2367 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002368 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002370 /* Vi compatible scrolling */
2371 if (p_window <= 2)
2372 ++curwin->w_topline;
2373 else
2374 curwin->w_topline += p_window - 2;
2375 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2376 curwin->w_topline = curbuf->b_ml.ml_line_count;
2377 curwin->w_cursor.lnum = curwin->w_topline;
2378 }
2379 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2380 {
2381 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 curwin->w_topline = curbuf->b_ml.ml_line_count;
2383#ifdef FEAT_DIFF
2384 curwin->w_topfill = 0;
2385#endif
2386 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2387 }
2388 else
2389 {
2390 /* For the overlap, start with the line just below the window
2391 * and go upwards. */
2392 loff.lnum = curwin->w_botline;
2393#ifdef FEAT_DIFF
2394 loff.fill = diff_check_fill(curwin, loff.lnum)
2395 - curwin->w_filler_rows;
2396#endif
2397 get_scroll_overlap(&loff, -1);
2398 curwin->w_topline = loff.lnum;
2399#ifdef FEAT_DIFF
2400 curwin->w_topfill = loff.fill;
2401 check_topfill(curwin, FALSE);
2402#endif
2403 curwin->w_cursor.lnum = curwin->w_topline;
2404 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2405 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2406 }
2407 }
2408 else /* dir == BACKWARDS */
2409 {
2410#ifdef FEAT_DIFF
2411 if (curwin->w_topline == 1)
2412 {
2413 /* Include max number of filler lines */
2414 max_topfill();
2415 continue;
2416 }
2417#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002418 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
2419 {
2420 /* Vi compatible scrolling (sort of) */
2421 if (p_window <= 2)
2422 --curwin->w_topline;
2423 else
2424 curwin->w_topline -= p_window - 2;
2425 if (curwin->w_topline < 1)
2426 curwin->w_topline = 1;
2427 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2428 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2429 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2430 continue;
2431 }
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /* Find the line at the top of the window that is going to be the
2434 * line at the bottom of the window. Make sure this results in
2435 * the same line as before doing CTRL-F. */
2436 loff.lnum = curwin->w_topline - 1;
2437#ifdef FEAT_DIFF
2438 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2439 - curwin->w_topfill;
2440#endif
2441 get_scroll_overlap(&loff, 1);
2442
2443 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2444 {
2445 loff.lnum = curbuf->b_ml.ml_line_count;
2446#ifdef FEAT_DIFF
2447 loff.fill = 0;
2448 }
2449 else
2450 {
2451 botline_topline(&loff);
2452#endif
2453 }
2454 curwin->w_cursor.lnum = loff.lnum;
2455
2456 /* Find the line just above the new topline to get the right line
2457 * at the bottom of the window. */
2458 n = 0;
2459 while (n <= curwin->w_height && loff.lnum >= 1)
2460 {
2461 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002462 if (loff.height == MAXCOL)
2463 n = MAXCOL;
2464 else
2465 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002467 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 curwin->w_topline = 1;
2470#ifdef FEAT_DIFF
2471 max_topfill();
2472#endif
2473 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2474 }
2475 else
2476 {
2477 /* Go two lines forward again. */
2478#ifdef FEAT_DIFF
2479 topline_botline(&loff);
2480#endif
2481 botline_forw(&loff);
2482 botline_forw(&loff);
2483#ifdef FEAT_DIFF
2484 botline_topline(&loff);
2485#endif
2486#ifdef FEAT_FOLDING
2487 /* We're at the wrong end of a fold now. */
2488 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2489#endif
2490
2491 /* Always scroll at least one line. Avoid getting stuck on
2492 * very long lines. */
2493 if (loff.lnum >= curwin->w_topline
2494#ifdef FEAT_DIFF
2495 && (loff.lnum > curwin->w_topline
2496 || loff.fill >= curwin->w_topfill)
2497#endif
2498 )
2499 {
2500#ifdef FEAT_DIFF
2501 /* First try using the maximum number of filler lines. If
2502 * that's not enough, backup one line. */
2503 loff.fill = curwin->w_topfill;
2504 if (curwin->w_topfill < diff_check_fill(curwin,
2505 curwin->w_topline))
2506 max_topfill();
2507 if (curwin->w_topfill == loff.fill)
2508#endif
2509 {
2510 --curwin->w_topline;
2511#ifdef FEAT_DIFF
2512 curwin->w_topfill = 0;
2513#endif
2514 }
2515 comp_botline(curwin);
2516 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002517 curwin->w_valid &=
2518 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 else
2521 {
2522 curwin->w_topline = loff.lnum;
2523#ifdef FEAT_DIFF
2524 curwin->w_topfill = loff.fill;
2525 check_topfill(curwin, FALSE);
2526#endif
2527 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2528 }
2529 }
2530 }
2531 }
2532#ifdef FEAT_FOLDING
2533 foldAdjustCursor();
2534#endif
2535 cursor_correct();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002536 if (retval == OK)
2537 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2539
2540 /*
2541 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2542 * But make sure we scroll at least one line (happens with mix of long
2543 * wrapping lines and non-wrapping line).
2544 */
2545 if (retval == OK && dir == FORWARD && check_top_offset())
2546 {
2547 scroll_cursor_top(1, FALSE);
2548 if (curwin->w_topline <= old_topline
2549 && old_topline < curbuf->b_ml.ml_line_count)
2550 {
2551 curwin->w_topline = old_topline + 1;
2552#ifdef FEAT_FOLDING
2553 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2554#endif
2555 }
2556 }
2557
2558 redraw_later(VALID);
2559 return retval;
2560}
2561
2562/*
2563 * Decide how much overlap to use for page-up or page-down scrolling.
2564 * This is symmetric, so that doing both keeps the same lines displayed.
2565 * Three lines are examined:
2566 *
2567 * before CTRL-F after CTRL-F / before CTRL-B
2568 * etc. l1
2569 * l1 last but one line ------------
2570 * l2 last text line l2 top text line
2571 * ------------- l3 second text line
2572 * l3 etc.
2573 */
2574 static void
2575get_scroll_overlap(lp, dir)
2576 lineoff_T *lp;
2577 int dir;
2578{
2579 int h1, h2, h3, h4;
2580 int min_height = curwin->w_height - 2;
2581 lineoff_T loff0, loff1, loff2;
2582
2583#ifdef FEAT_DIFF
2584 if (lp->fill > 0)
2585 lp->height = 1;
2586 else
2587 lp->height = plines_nofill(lp->lnum);
2588#else
2589 lp->height = plines(lp->lnum);
2590#endif
2591 h1 = lp->height;
2592 if (h1 > min_height)
2593 return; /* no overlap */
2594
2595 loff0 = *lp;
2596 if (dir > 0)
2597 botline_forw(lp);
2598 else
2599 topline_back(lp);
2600 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002601 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 *lp = loff0; /* no overlap */
2604 return;
2605 }
2606
2607 loff1 = *lp;
2608 if (dir > 0)
2609 botline_forw(lp);
2610 else
2611 topline_back(lp);
2612 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002613 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 *lp = loff0; /* no overlap */
2616 return;
2617 }
2618
2619 loff2 = *lp;
2620 if (dir > 0)
2621 botline_forw(lp);
2622 else
2623 topline_back(lp);
2624 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002625 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 *lp = loff1; /* 1 line overlap */
2627 else
2628 *lp = loff2; /* 2 lines overlap */
2629 return;
2630}
2631
2632/* #define KEEP_SCREEN_LINE */
2633/*
2634 * Scroll 'scroll' lines up or down.
2635 */
2636 void
2637halfpage(flag, Prenum)
2638 int flag;
2639 linenr_T Prenum;
2640{
2641 long scrolled = 0;
2642 int i;
2643 int n;
2644 int room;
2645
2646 if (Prenum)
2647 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2648 curwin->w_height : Prenum;
2649 n = (curwin->w_p_scr <= curwin->w_height) ?
2650 curwin->w_p_scr : curwin->w_height;
2651
2652 validate_botline();
2653 room = curwin->w_empty_rows;
2654#ifdef FEAT_DIFF
2655 room += curwin->w_filler_rows;
2656#endif
2657 if (flag)
2658 {
2659 /*
2660 * scroll the text up
2661 */
2662 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2663 {
2664#ifdef FEAT_DIFF
2665 if (curwin->w_topfill > 0)
2666 {
2667 i = 1;
2668 if (--n < 0 && scrolled > 0)
2669 break;
2670 --curwin->w_topfill;
2671 }
2672 else
2673#endif
2674 {
2675#ifdef FEAT_DIFF
2676 i = plines_nofill(curwin->w_topline);
2677#else
2678 i = plines(curwin->w_topline);
2679#endif
2680 n -= i;
2681 if (n < 0 && scrolled > 0)
2682 break;
2683#ifdef FEAT_FOLDING
2684 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2685#endif
2686 ++curwin->w_topline;
2687#ifdef FEAT_DIFF
2688 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2689#endif
2690
2691#ifndef KEEP_SCREEN_LINE
2692 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2693 {
2694 ++curwin->w_cursor.lnum;
2695 curwin->w_valid &=
2696 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2697 }
2698#endif
2699 }
2700 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2701 scrolled += i;
2702
2703 /*
2704 * Correct w_botline for changed w_topline.
2705 * Won't work when there are filler lines.
2706 */
2707#ifdef FEAT_DIFF
2708 if (curwin->w_p_diff)
2709 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2710 else
2711#endif
2712 {
2713 room += i;
2714 do
2715 {
2716 i = plines(curwin->w_botline);
2717 if (i > room)
2718 break;
2719#ifdef FEAT_FOLDING
2720 (void)hasFolding(curwin->w_botline, NULL,
2721 &curwin->w_botline);
2722#endif
2723 ++curwin->w_botline;
2724 room -= i;
2725 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2726 }
2727 }
2728
2729#ifndef KEEP_SCREEN_LINE
2730 /*
2731 * When hit bottom of the file: move cursor down.
2732 */
2733 if (n > 0)
2734 {
2735# ifdef FEAT_FOLDING
2736 if (hasAnyFolding(curwin))
2737 {
2738 while (--n >= 0
2739 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2740 {
2741 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2742 &curwin->w_cursor.lnum);
2743 ++curwin->w_cursor.lnum;
2744 }
2745 }
2746 else
2747# endif
2748 curwin->w_cursor.lnum += n;
2749 check_cursor_lnum();
2750 }
2751#else
2752 /* try to put the cursor in the same screen line */
2753 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2754 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2755 {
2756 scrolled -= plines(curwin->w_cursor.lnum);
2757 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2758 break;
2759# ifdef FEAT_FOLDING
2760 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2761 &curwin->w_cursor.lnum);
2762# endif
2763 ++curwin->w_cursor.lnum;
2764 }
2765#endif
2766 }
2767 else
2768 {
2769 /*
2770 * scroll the text down
2771 */
2772 while (n > 0 && curwin->w_topline > 1)
2773 {
2774#ifdef FEAT_DIFF
2775 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2776 {
2777 i = 1;
2778 if (--n < 0 && scrolled > 0)
2779 break;
2780 ++curwin->w_topfill;
2781 }
2782 else
2783#endif
2784 {
2785#ifdef FEAT_DIFF
2786 i = plines_nofill(curwin->w_topline - 1);
2787#else
2788 i = plines(curwin->w_topline - 1);
2789#endif
2790 n -= i;
2791 if (n < 0 && scrolled > 0)
2792 break;
2793 --curwin->w_topline;
2794#ifdef FEAT_FOLDING
2795 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2796#endif
2797#ifdef FEAT_DIFF
2798 curwin->w_topfill = 0;
2799#endif
2800 }
2801 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2802 VALID_BOTLINE|VALID_BOTLINE_AP);
2803 scrolled += i;
2804#ifndef KEEP_SCREEN_LINE
2805 if (curwin->w_cursor.lnum > 1)
2806 {
2807 --curwin->w_cursor.lnum;
2808 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2809 }
2810#endif
2811 }
2812#ifndef KEEP_SCREEN_LINE
2813 /*
2814 * When hit top of the file: move cursor up.
2815 */
2816 if (n > 0)
2817 {
2818 if (curwin->w_cursor.lnum <= (linenr_T)n)
2819 curwin->w_cursor.lnum = 1;
2820 else
2821# ifdef FEAT_FOLDING
2822 if (hasAnyFolding(curwin))
2823 {
2824 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2825 {
2826 --curwin->w_cursor.lnum;
2827 (void)hasFolding(curwin->w_cursor.lnum,
2828 &curwin->w_cursor.lnum, NULL);
2829 }
2830 }
2831 else
2832# endif
2833 curwin->w_cursor.lnum -= n;
2834 }
2835#else
2836 /* try to put the cursor in the same screen line */
2837 scrolled += n; /* move cursor when topline is 1 */
2838 while (curwin->w_cursor.lnum > curwin->w_topline
2839 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2840 {
2841 scrolled -= plines(curwin->w_cursor.lnum - 1);
2842 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2843 break;
2844 --curwin->w_cursor.lnum;
2845# ifdef FEAT_FOLDING
2846 foldAdjustCursor();
2847# endif
2848 }
2849#endif
2850 }
2851# ifdef FEAT_FOLDING
2852 /* Move cursor to first line of closed fold. */
2853 foldAdjustCursor();
2854# endif
2855#ifdef FEAT_DIFF
2856 check_topfill(curwin, !flag);
2857#endif
2858 cursor_correct();
2859 beginline(BL_SOL | BL_FIX);
2860 redraw_later(VALID);
2861}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862
2863#if defined(FEAT_CURSORBIND) || defined(PROTO)
2864 void
2865do_check_cursorbind()
2866{
2867 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002868 colnr_T col = curwin->w_cursor.col;
2869# ifdef FEAT_VIRTUALEDIT
2870 colnr_T coladd = curwin->w_cursor.coladd;
2871# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002872 colnr_T curswant = curwin->w_curswant;
2873 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002874 win_T *old_curwin = curwin;
2875 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002876 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002877 int old_VIsual_select = VIsual_select;
2878 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879
2880 /*
2881 * loop through the cursorbound windows
2882 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883 VIsual_select = VIsual_active = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002884 for (curwin = firstwin; curwin; curwin = curwin->w_next)
2885 {
2886 curbuf = curwin->w_buffer;
2887 /* skip original window and windows with 'noscrollbind' */
2888 if (curwin != old_curwin && curwin->w_p_crb)
2889 {
2890# ifdef FEAT_DIFF
2891 if (curwin->w_p_diff)
2892 curwin->w_cursor.lnum
2893 = diff_get_corresponding_line(old_curbuf,
2894 line,
2895 curbuf,
2896 curwin->w_cursor.lnum);
2897 else
2898# endif
2899 curwin->w_cursor.lnum = line;
2900 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002901# ifdef FEAT_VIRTUALEDIT
2902 curwin->w_cursor.coladd = coladd;
2903# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002904 curwin->w_curswant = curswant;
2905 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002906
Bram Moolenaar61452852011-02-01 18:01:11 +01002907 /* Make sure the cursor is in a valid position. Temporarily set
2908 * "restart_edit" to allow the cursor to be beyond the EOL. */
2909 restart_edit_save = restart_edit;
2910 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002911 check_cursor();
Bram Moolenaar61452852011-02-01 18:01:11 +01002912 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002913# ifdef FEAT_MBYTE
2914 /* Correct cursor for multi-byte character. */
2915 if (has_mbyte)
2916 mb_adjust_cursor();
2917# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002918 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002919
2920 /* Only scroll when 'scrollbind' hasn't done this. */
2921 if (!curwin->w_p_scb)
2922 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002923# ifdef FEAT_WINDOWS
2924 curwin->w_redr_status = TRUE;
2925# endif
2926 }
2927 }
2928
2929 /*
2930 * reset current-window
2931 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002932 VIsual_select = old_VIsual_select;
2933 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002934 curwin = old_curwin;
2935 curbuf = old_curbuf;
2936}
2937#endif /* FEAT_CURSORBIND */