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