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