blob: 02694123887ac89a11955e48323209e1b5a247bc [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
1515 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1516#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();
1735 used = curwin->w_cline_height;
1736 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
1754 /* count filler lines of the cursor window as context */
1755 i = diff_check_fill(curwin, curwin->w_cursor.lnum);
1756 used += i;
1757 extra += i;
1758#endif
1759
1760 /*
1761 * Check if the lines from "top" to "bot" fit in the window. If they do,
1762 * set new_topline and advance "top" and "bot" to include more lines.
1763 */
1764 while (top > 0)
1765 {
1766#ifdef FEAT_FOLDING
1767 if (hasFolding(top, &top, NULL))
1768 /* count one logical line for a sequence of folded lines */
1769 i = 1;
1770 else
1771#endif
1772 i = plines(top);
1773 used += i;
1774 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1775 {
1776#ifdef FEAT_FOLDING
1777 if (hasFolding(bot, NULL, &bot))
1778 /* count one logical line for a sequence of folded lines */
1779 ++used;
1780 else
1781#endif
1782 used += plines(bot);
1783 }
1784 if (used > curwin->w_height)
1785 break;
1786 if (top < curwin->w_topline)
1787 scrolled += i;
1788
1789 /*
1790 * If scrolling is needed, scroll at least 'sj' lines.
1791 */
1792 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1793 && extra >= off)
1794 break;
1795
1796 extra += i;
1797 new_topline = top;
1798 --top;
1799 ++bot;
1800 }
1801
1802 /*
1803 * If we don't have enough space, put cursor in the middle.
1804 * This makes sure we get the same position when using "k" and "j"
1805 * in a small window.
1806 */
1807 if (used > curwin->w_height)
1808 scroll_cursor_halfway(FALSE);
1809 else
1810 {
1811 /*
1812 * If "always" is FALSE, only adjust topline to a lower value, higher
1813 * value may happen with wrapping lines
1814 */
1815 if (new_topline < curwin->w_topline || always)
1816 curwin->w_topline = new_topline;
1817 if (curwin->w_topline > curwin->w_cursor.lnum)
1818 curwin->w_topline = curwin->w_cursor.lnum;
1819#ifdef FEAT_DIFF
1820 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1821 if (curwin->w_topfill > 0 && extra > off)
1822 {
1823 curwin->w_topfill -= extra - off;
1824 if (curwin->w_topfill < 0)
1825 curwin->w_topfill = 0;
1826 }
1827 check_topfill(curwin, FALSE);
1828#endif
1829 if (curwin->w_topline != old_topline
1830#ifdef FEAT_DIFF
1831 || curwin->w_topfill != old_topfill
1832#endif
1833 )
1834 curwin->w_valid &=
1835 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1836 curwin->w_valid |= VALID_TOPLINE;
1837 }
1838}
1839
1840/*
1841 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1842 * screen lines for text lines.
1843 */
1844 void
1845set_empty_rows(wp, used)
1846 win_T *wp;
1847 int used;
1848{
1849#ifdef FEAT_DIFF
1850 wp->w_filler_rows = 0;
1851#endif
1852 if (used == 0)
1853 wp->w_empty_rows = 0; /* single line that doesn't fit */
1854 else
1855 {
1856 wp->w_empty_rows = wp->w_height - used;
1857#ifdef FEAT_DIFF
1858 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1859 {
1860 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1861 if (wp->w_empty_rows > wp->w_filler_rows)
1862 wp->w_empty_rows -= wp->w_filler_rows;
1863 else
1864 {
1865 wp->w_filler_rows = wp->w_empty_rows;
1866 wp->w_empty_rows = 0;
1867 }
1868 }
1869#endif
1870 }
1871}
1872
1873/*
1874 * Recompute topline to put the cursor at the bottom of the window.
1875 * Scroll at least "min_scroll" lines.
1876 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1877 * This is messy stuff!!!
1878 */
1879 void
1880scroll_cursor_bot(min_scroll, set_topbot)
1881 int min_scroll;
1882 int set_topbot;
1883{
1884 int used;
1885 int scrolled = 0;
1886 int extra = 0;
1887 int i;
1888 linenr_T line_count;
1889 linenr_T old_topline = curwin->w_topline;
1890 lineoff_T loff;
1891 lineoff_T boff;
1892#ifdef FEAT_DIFF
1893 int old_topfill = curwin->w_topfill;
1894 int fill_below_window;
1895#endif
1896 linenr_T old_botline = curwin->w_botline;
1897 linenr_T old_valid = curwin->w_valid;
1898 int old_empty_rows = curwin->w_empty_rows;
1899 linenr_T cln; /* Cursor Line Number */
1900
1901 cln = curwin->w_cursor.lnum;
1902 if (set_topbot)
1903 {
1904 used = 0;
1905 curwin->w_botline = cln + 1;
1906#ifdef FEAT_DIFF
1907 loff.fill = 0;
1908#endif
1909 for (curwin->w_topline = curwin->w_botline;
1910 curwin->w_topline > 1;
1911 curwin->w_topline = loff.lnum)
1912 {
1913 loff.lnum = curwin->w_topline;
1914 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001915 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 break;
1917 used += loff.height;
1918#ifdef FEAT_DIFF
1919 curwin->w_topfill = loff.fill;
1920#endif
1921 }
1922 set_empty_rows(curwin, used);
1923 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1924 if (curwin->w_topline != old_topline
1925#ifdef FEAT_DIFF
1926 || curwin->w_topfill != old_topfill
1927#endif
1928 )
1929 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1930 }
1931 else
1932 validate_botline();
1933
1934 /* The lines of the cursor line itself are always used. */
1935#ifdef FEAT_DIFF
1936 used = plines_nofill(cln);
1937#else
1938 validate_cheight();
1939 used = curwin->w_cline_height;
1940#endif
1941
1942 /* If the cursor is below botline, we will at least scroll by the height
1943 * of the cursor line. Correct for empty lines, which are really part of
1944 * botline. */
1945 if (cln >= curwin->w_botline)
1946 {
1947 scrolled = used;
1948 if (cln == curwin->w_botline)
1949 scrolled -= curwin->w_empty_rows;
1950 }
1951
1952 /*
1953 * Stop counting lines to scroll when
1954 * - hitting start of the file
1955 * - scrolled nothing or at least 'sj' lines
1956 * - at least 'so' lines below the cursor
1957 * - lines between botline and cursor have been counted
1958 */
1959#ifdef FEAT_FOLDING
1960 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1961#endif
1962 {
1963 loff.lnum = cln;
1964 boff.lnum = cln;
1965 }
1966#ifdef FEAT_DIFF
1967 loff.fill = 0;
1968 boff.fill = 0;
1969 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1970 - curwin->w_filler_rows;
1971#endif
1972
1973 while (loff.lnum > 1)
1974 {
1975 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1976 * context for 'scrolloff' and counted all lines below the window. */
1977 if ((((scrolled <= 0 || scrolled >= min_scroll)
1978 && extra >= (
1979#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001980 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#endif
1982 p_so))
1983 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1984 && loff.lnum <= curwin->w_botline
1985#ifdef FEAT_DIFF
1986 && (loff.lnum < curwin->w_botline
1987 || loff.fill >= fill_below_window)
1988#endif
1989 )
1990 break;
1991
1992 /* Add one line above */
1993 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001994 if (loff.height == MAXCOL)
1995 used = MAXCOL;
1996 else
1997 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 if (used > curwin->w_height)
1999 break;
2000 if (loff.lnum >= curwin->w_botline
2001#ifdef FEAT_DIFF
2002 && (loff.lnum > curwin->w_botline
2003 || loff.fill <= fill_below_window)
2004#endif
2005 )
2006 {
2007 /* Count screen lines that are below the window. */
2008 scrolled += loff.height;
2009 if (loff.lnum == curwin->w_botline
2010#ifdef FEAT_DIFF
2011 && boff.fill == 0
2012#endif
2013 )
2014 scrolled -= curwin->w_empty_rows;
2015 }
2016
2017 if (boff.lnum < curbuf->b_ml.ml_line_count)
2018 {
2019 /* Add one line below */
2020 botline_forw(&boff);
2021 used += boff.height;
2022 if (used > curwin->w_height)
2023 break;
2024 if (extra < (
2025#ifdef FEAT_MOUSE
2026 mouse_dragging > 0 ? mouse_dragging - 1 :
2027#endif
2028 p_so) || scrolled < min_scroll)
2029 {
2030 extra += boff.height;
2031 if (boff.lnum >= curwin->w_botline
2032#ifdef FEAT_DIFF
2033 || (boff.lnum + 1 == curwin->w_botline
2034 && boff.fill > curwin->w_filler_rows)
2035#endif
2036 )
2037 {
2038 /* Count screen lines that are below the window. */
2039 scrolled += boff.height;
2040 if (boff.lnum == curwin->w_botline
2041#ifdef FEAT_DIFF
2042 && boff.fill == 0
2043#endif
2044 )
2045 scrolled -= curwin->w_empty_rows;
2046 }
2047 }
2048 }
2049 }
2050
2051 /* curwin->w_empty_rows is larger, no need to scroll */
2052 if (scrolled <= 0)
2053 line_count = 0;
2054 /* more than a screenfull, don't scroll but redraw */
2055 else if (used > curwin->w_height)
2056 line_count = used;
2057 /* scroll minimal number of lines */
2058 else
2059 {
2060 line_count = 0;
2061#ifdef FEAT_DIFF
2062 boff.fill = curwin->w_topfill;
2063#endif
2064 boff.lnum = curwin->w_topline - 1;
2065 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2066 {
2067 botline_forw(&boff);
2068 i += boff.height;
2069 ++line_count;
2070 }
2071 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2072 line_count = 9999;
2073 }
2074
2075 /*
2076 * Scroll up if the cursor is off the bottom of the screen a bit.
2077 * Otherwise put it at 1/2 of the screen.
2078 */
2079 if (line_count >= curwin->w_height && line_count > min_scroll)
2080 scroll_cursor_halfway(FALSE);
2081 else
2082 scrollup(line_count, TRUE);
2083
2084 /*
2085 * If topline didn't change we need to restore w_botline and w_empty_rows
2086 * (we changed them).
2087 * If topline did change, update_screen() will set botline.
2088 */
2089 if (curwin->w_topline == old_topline && set_topbot)
2090 {
2091 curwin->w_botline = old_botline;
2092 curwin->w_empty_rows = old_empty_rows;
2093 curwin->w_valid = old_valid;
2094 }
2095 curwin->w_valid |= VALID_TOPLINE;
2096}
2097
2098/*
2099 * Recompute topline to put the cursor halfway the window
2100 * If "atend" is TRUE, also put it halfway at the end of the file.
2101 */
2102 void
2103scroll_cursor_halfway(atend)
2104 int atend;
2105{
2106 int above = 0;
2107 linenr_T topline;
2108#ifdef FEAT_DIFF
2109 int topfill = 0;
2110#endif
2111 int below = 0;
2112 int used;
2113 lineoff_T loff;
2114 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002115#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002116 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2120#ifdef FEAT_FOLDING
2121 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2122#endif
2123#ifdef FEAT_DIFF
2124 used = plines_nofill(loff.lnum);
2125 loff.fill = 0;
2126 boff.fill = 0;
2127#else
2128 used = plines(loff.lnum);
2129#endif
2130 topline = loff.lnum;
2131 while (topline > 1)
2132 {
2133 if (below <= above) /* add a line below the cursor first */
2134 {
2135 if (boff.lnum < curbuf->b_ml.ml_line_count)
2136 {
2137 botline_forw(&boff);
2138 used += boff.height;
2139 if (used > curwin->w_height)
2140 break;
2141 below += boff.height;
2142 }
2143 else
2144 {
2145 ++below; /* count a "~" line */
2146 if (atend)
2147 ++used;
2148 }
2149 }
2150
2151 if (below > above) /* add a line above the cursor */
2152 {
2153 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002154 if (loff.height == MAXCOL)
2155 used = MAXCOL;
2156 else
2157 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (used > curwin->w_height)
2159 break;
2160 above += loff.height;
2161 topline = loff.lnum;
2162#ifdef FEAT_DIFF
2163 topfill = loff.fill;
2164#endif
2165 }
2166 }
2167#ifdef FEAT_FOLDING
2168 if (!hasFolding(topline, &curwin->w_topline, NULL))
2169#endif
2170 curwin->w_topline = topline;
2171#ifdef FEAT_DIFF
2172 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002173 if (old_topline > curwin->w_topline + curwin->w_height)
2174 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 check_topfill(curwin, FALSE);
2176#endif
2177 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2178 curwin->w_valid |= VALID_TOPLINE;
2179}
2180
2181/*
2182 * Correct the cursor position so that it is in a part of the screen at least
2183 * 'so' lines from the top and bottom, if possible.
2184 * If not possible, put it at the same position as scroll_cursor_halfway().
2185 * When called topline must be valid!
2186 */
2187 void
2188cursor_correct()
2189{
2190 int above = 0; /* screen lines above topline */
2191 linenr_T topline;
2192 int below = 0; /* screen lines below botline */
2193 linenr_T botline;
2194 int above_wanted, below_wanted;
2195 linenr_T cln; /* Cursor Line Number */
2196 int max_off;
2197
2198 /*
2199 * How many lines we would like to have above/below the cursor depends on
2200 * whether the first/last line of the file is on screen.
2201 */
2202 above_wanted = p_so;
2203 below_wanted = p_so;
2204#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002205 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 above_wanted = mouse_dragging - 1;
2208 below_wanted = mouse_dragging - 1;
2209 }
2210#endif
2211 if (curwin->w_topline == 1)
2212 {
2213 above_wanted = 0;
2214 max_off = curwin->w_height / 2;
2215 if (below_wanted > max_off)
2216 below_wanted = max_off;
2217 }
2218 validate_botline();
2219 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2220#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002221 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222#endif
2223 )
2224 {
2225 below_wanted = 0;
2226 max_off = (curwin->w_height - 1) / 2;
2227 if (above_wanted > max_off)
2228 above_wanted = max_off;
2229 }
2230
2231 /*
2232 * If there are sufficient file-lines above and below the cursor, we can
2233 * return now.
2234 */
2235 cln = curwin->w_cursor.lnum;
2236 if (cln >= curwin->w_topline + above_wanted
2237 && cln < curwin->w_botline - below_wanted
2238#ifdef FEAT_FOLDING
2239 && !hasAnyFolding(curwin)
2240#endif
2241 )
2242 return;
2243
2244 /*
2245 * Narrow down the area where the cursor can be put by taking lines from
2246 * the top and the bottom until:
2247 * - the desired context lines are found
2248 * - the lines from the top is past the lines from the bottom
2249 */
2250 topline = curwin->w_topline;
2251 botline = curwin->w_botline - 1;
2252#ifdef FEAT_DIFF
2253 /* count filler lines as context */
2254 above = curwin->w_topfill;
2255 below = curwin->w_filler_rows;
2256#endif
2257 while ((above < above_wanted || below < below_wanted) && topline < botline)
2258 {
2259 if (below < below_wanted && (below <= above || above >= above_wanted))
2260 {
2261#ifdef FEAT_FOLDING
2262 if (hasFolding(botline, &botline, NULL))
2263 ++below;
2264 else
2265#endif
2266 below += plines(botline);
2267 --botline;
2268 }
2269 if (above < above_wanted && (above < below || below >= below_wanted))
2270 {
2271#ifdef FEAT_FOLDING
2272 if (hasFolding(topline, NULL, &topline))
2273 ++above;
2274 else
2275#endif
2276#ifndef FEAT_DIFF
2277 above += plines(topline);
2278#else
2279 above += plines_nofill(topline);
2280
2281 /* Count filler lines below this line as context. */
2282 if (topline < botline)
2283 above += diff_check_fill(curwin, topline + 1);
2284#endif
2285 ++topline;
2286 }
2287 }
2288 if (topline == botline || botline == 0)
2289 curwin->w_cursor.lnum = topline;
2290 else if (topline > botline)
2291 curwin->w_cursor.lnum = botline;
2292 else
2293 {
2294 if (cln < topline && curwin->w_topline > 1)
2295 {
2296 curwin->w_cursor.lnum = topline;
2297 curwin->w_valid &=
2298 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2299 }
2300 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2301 {
2302 curwin->w_cursor.lnum = botline;
2303 curwin->w_valid &=
2304 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2305 }
2306 }
2307 curwin->w_valid |= VALID_TOPLINE;
2308}
2309
2310static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir));
2311
2312/*
2313 * move screen 'count' pages up or down and update screen
2314 *
2315 * return FAIL for failure, OK otherwise
2316 */
2317 int
2318onepage(dir, count)
2319 int dir;
2320 long count;
2321{
2322 long n;
2323 int retval = OK;
2324 lineoff_T loff;
2325 linenr_T old_topline = curwin->w_topline;
2326
2327 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2328 {
2329 beep_flush();
2330 return FAIL;
2331 }
2332
2333 for ( ; count > 0; --count)
2334 {
2335 validate_botline();
2336 /*
2337 * It's an error to move a page up when the first line is already on
2338 * the screen. It's an error to move a page down when the last line
2339 * is on the screen and the topline is 'scrolloff' lines from the
2340 * last line.
2341 */
2342 if (dir == FORWARD
2343 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2344 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2345 : (curwin->w_topline == 1
2346#ifdef FEAT_DIFF
2347 && curwin->w_topfill ==
2348 diff_check_fill(curwin, curwin->w_topline)
2349#endif
2350 ))
2351 {
2352 beep_flush();
2353 retval = FAIL;
2354 break;
2355 }
2356
2357#ifdef FEAT_DIFF
2358 loff.fill = 0;
2359#endif
2360 if (dir == FORWARD)
2361 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002362 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002364 /* Vi compatible scrolling */
2365 if (p_window <= 2)
2366 ++curwin->w_topline;
2367 else
2368 curwin->w_topline += p_window - 2;
2369 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2370 curwin->w_topline = curbuf->b_ml.ml_line_count;
2371 curwin->w_cursor.lnum = curwin->w_topline;
2372 }
2373 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2374 {
2375 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 curwin->w_topline = curbuf->b_ml.ml_line_count;
2377#ifdef FEAT_DIFF
2378 curwin->w_topfill = 0;
2379#endif
2380 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2381 }
2382 else
2383 {
2384 /* For the overlap, start with the line just below the window
2385 * and go upwards. */
2386 loff.lnum = curwin->w_botline;
2387#ifdef FEAT_DIFF
2388 loff.fill = diff_check_fill(curwin, loff.lnum)
2389 - curwin->w_filler_rows;
2390#endif
2391 get_scroll_overlap(&loff, -1);
2392 curwin->w_topline = loff.lnum;
2393#ifdef FEAT_DIFF
2394 curwin->w_topfill = loff.fill;
2395 check_topfill(curwin, FALSE);
2396#endif
2397 curwin->w_cursor.lnum = curwin->w_topline;
2398 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2399 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2400 }
2401 }
2402 else /* dir == BACKWARDS */
2403 {
2404#ifdef FEAT_DIFF
2405 if (curwin->w_topline == 1)
2406 {
2407 /* Include max number of filler lines */
2408 max_topfill();
2409 continue;
2410 }
2411#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002412 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
2413 {
2414 /* Vi compatible scrolling (sort of) */
2415 if (p_window <= 2)
2416 --curwin->w_topline;
2417 else
2418 curwin->w_topline -= p_window - 2;
2419 if (curwin->w_topline < 1)
2420 curwin->w_topline = 1;
2421 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2422 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2423 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2424 continue;
2425 }
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* Find the line at the top of the window that is going to be the
2428 * line at the bottom of the window. Make sure this results in
2429 * the same line as before doing CTRL-F. */
2430 loff.lnum = curwin->w_topline - 1;
2431#ifdef FEAT_DIFF
2432 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2433 - curwin->w_topfill;
2434#endif
2435 get_scroll_overlap(&loff, 1);
2436
2437 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2438 {
2439 loff.lnum = curbuf->b_ml.ml_line_count;
2440#ifdef FEAT_DIFF
2441 loff.fill = 0;
2442 }
2443 else
2444 {
2445 botline_topline(&loff);
2446#endif
2447 }
2448 curwin->w_cursor.lnum = loff.lnum;
2449
2450 /* Find the line just above the new topline to get the right line
2451 * at the bottom of the window. */
2452 n = 0;
2453 while (n <= curwin->w_height && loff.lnum >= 1)
2454 {
2455 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002456 if (loff.height == MAXCOL)
2457 n = MAXCOL;
2458 else
2459 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002461 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 curwin->w_topline = 1;
2464#ifdef FEAT_DIFF
2465 max_topfill();
2466#endif
2467 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2468 }
2469 else
2470 {
2471 /* Go two lines forward again. */
2472#ifdef FEAT_DIFF
2473 topline_botline(&loff);
2474#endif
2475 botline_forw(&loff);
2476 botline_forw(&loff);
2477#ifdef FEAT_DIFF
2478 botline_topline(&loff);
2479#endif
2480#ifdef FEAT_FOLDING
2481 /* We're at the wrong end of a fold now. */
2482 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2483#endif
2484
2485 /* Always scroll at least one line. Avoid getting stuck on
2486 * very long lines. */
2487 if (loff.lnum >= curwin->w_topline
2488#ifdef FEAT_DIFF
2489 && (loff.lnum > curwin->w_topline
2490 || loff.fill >= curwin->w_topfill)
2491#endif
2492 )
2493 {
2494#ifdef FEAT_DIFF
2495 /* First try using the maximum number of filler lines. If
2496 * that's not enough, backup one line. */
2497 loff.fill = curwin->w_topfill;
2498 if (curwin->w_topfill < diff_check_fill(curwin,
2499 curwin->w_topline))
2500 max_topfill();
2501 if (curwin->w_topfill == loff.fill)
2502#endif
2503 {
2504 --curwin->w_topline;
2505#ifdef FEAT_DIFF
2506 curwin->w_topfill = 0;
2507#endif
2508 }
2509 comp_botline(curwin);
2510 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002511 curwin->w_valid &=
2512 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 else
2515 {
2516 curwin->w_topline = loff.lnum;
2517#ifdef FEAT_DIFF
2518 curwin->w_topfill = loff.fill;
2519 check_topfill(curwin, FALSE);
2520#endif
2521 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2522 }
2523 }
2524 }
2525 }
2526#ifdef FEAT_FOLDING
2527 foldAdjustCursor();
2528#endif
2529 cursor_correct();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002530 if (retval == OK)
2531 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2533
2534 /*
2535 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2536 * But make sure we scroll at least one line (happens with mix of long
2537 * wrapping lines and non-wrapping line).
2538 */
2539 if (retval == OK && dir == FORWARD && check_top_offset())
2540 {
2541 scroll_cursor_top(1, FALSE);
2542 if (curwin->w_topline <= old_topline
2543 && old_topline < curbuf->b_ml.ml_line_count)
2544 {
2545 curwin->w_topline = old_topline + 1;
2546#ifdef FEAT_FOLDING
2547 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2548#endif
2549 }
2550 }
2551
2552 redraw_later(VALID);
2553 return retval;
2554}
2555
2556/*
2557 * Decide how much overlap to use for page-up or page-down scrolling.
2558 * This is symmetric, so that doing both keeps the same lines displayed.
2559 * Three lines are examined:
2560 *
2561 * before CTRL-F after CTRL-F / before CTRL-B
2562 * etc. l1
2563 * l1 last but one line ------------
2564 * l2 last text line l2 top text line
2565 * ------------- l3 second text line
2566 * l3 etc.
2567 */
2568 static void
2569get_scroll_overlap(lp, dir)
2570 lineoff_T *lp;
2571 int dir;
2572{
2573 int h1, h2, h3, h4;
2574 int min_height = curwin->w_height - 2;
2575 lineoff_T loff0, loff1, loff2;
2576
2577#ifdef FEAT_DIFF
2578 if (lp->fill > 0)
2579 lp->height = 1;
2580 else
2581 lp->height = plines_nofill(lp->lnum);
2582#else
2583 lp->height = plines(lp->lnum);
2584#endif
2585 h1 = lp->height;
2586 if (h1 > min_height)
2587 return; /* no overlap */
2588
2589 loff0 = *lp;
2590 if (dir > 0)
2591 botline_forw(lp);
2592 else
2593 topline_back(lp);
2594 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002595 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 *lp = loff0; /* no overlap */
2598 return;
2599 }
2600
2601 loff1 = *lp;
2602 if (dir > 0)
2603 botline_forw(lp);
2604 else
2605 topline_back(lp);
2606 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002607 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 *lp = loff0; /* no overlap */
2610 return;
2611 }
2612
2613 loff2 = *lp;
2614 if (dir > 0)
2615 botline_forw(lp);
2616 else
2617 topline_back(lp);
2618 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002619 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 *lp = loff1; /* 1 line overlap */
2621 else
2622 *lp = loff2; /* 2 lines overlap */
2623 return;
2624}
2625
2626/* #define KEEP_SCREEN_LINE */
2627/*
2628 * Scroll 'scroll' lines up or down.
2629 */
2630 void
2631halfpage(flag, Prenum)
2632 int flag;
2633 linenr_T Prenum;
2634{
2635 long scrolled = 0;
2636 int i;
2637 int n;
2638 int room;
2639
2640 if (Prenum)
2641 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2642 curwin->w_height : Prenum;
2643 n = (curwin->w_p_scr <= curwin->w_height) ?
2644 curwin->w_p_scr : curwin->w_height;
2645
2646 validate_botline();
2647 room = curwin->w_empty_rows;
2648#ifdef FEAT_DIFF
2649 room += curwin->w_filler_rows;
2650#endif
2651 if (flag)
2652 {
2653 /*
2654 * scroll the text up
2655 */
2656 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2657 {
2658#ifdef FEAT_DIFF
2659 if (curwin->w_topfill > 0)
2660 {
2661 i = 1;
2662 if (--n < 0 && scrolled > 0)
2663 break;
2664 --curwin->w_topfill;
2665 }
2666 else
2667#endif
2668 {
2669#ifdef FEAT_DIFF
2670 i = plines_nofill(curwin->w_topline);
2671#else
2672 i = plines(curwin->w_topline);
2673#endif
2674 n -= i;
2675 if (n < 0 && scrolled > 0)
2676 break;
2677#ifdef FEAT_FOLDING
2678 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2679#endif
2680 ++curwin->w_topline;
2681#ifdef FEAT_DIFF
2682 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2683#endif
2684
2685#ifndef KEEP_SCREEN_LINE
2686 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2687 {
2688 ++curwin->w_cursor.lnum;
2689 curwin->w_valid &=
2690 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2691 }
2692#endif
2693 }
2694 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2695 scrolled += i;
2696
2697 /*
2698 * Correct w_botline for changed w_topline.
2699 * Won't work when there are filler lines.
2700 */
2701#ifdef FEAT_DIFF
2702 if (curwin->w_p_diff)
2703 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2704 else
2705#endif
2706 {
2707 room += i;
2708 do
2709 {
2710 i = plines(curwin->w_botline);
2711 if (i > room)
2712 break;
2713#ifdef FEAT_FOLDING
2714 (void)hasFolding(curwin->w_botline, NULL,
2715 &curwin->w_botline);
2716#endif
2717 ++curwin->w_botline;
2718 room -= i;
2719 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2720 }
2721 }
2722
2723#ifndef KEEP_SCREEN_LINE
2724 /*
2725 * When hit bottom of the file: move cursor down.
2726 */
2727 if (n > 0)
2728 {
2729# ifdef FEAT_FOLDING
2730 if (hasAnyFolding(curwin))
2731 {
2732 while (--n >= 0
2733 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2734 {
2735 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2736 &curwin->w_cursor.lnum);
2737 ++curwin->w_cursor.lnum;
2738 }
2739 }
2740 else
2741# endif
2742 curwin->w_cursor.lnum += n;
2743 check_cursor_lnum();
2744 }
2745#else
2746 /* try to put the cursor in the same screen line */
2747 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2748 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2749 {
2750 scrolled -= plines(curwin->w_cursor.lnum);
2751 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2752 break;
2753# ifdef FEAT_FOLDING
2754 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2755 &curwin->w_cursor.lnum);
2756# endif
2757 ++curwin->w_cursor.lnum;
2758 }
2759#endif
2760 }
2761 else
2762 {
2763 /*
2764 * scroll the text down
2765 */
2766 while (n > 0 && curwin->w_topline > 1)
2767 {
2768#ifdef FEAT_DIFF
2769 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2770 {
2771 i = 1;
2772 if (--n < 0 && scrolled > 0)
2773 break;
2774 ++curwin->w_topfill;
2775 }
2776 else
2777#endif
2778 {
2779#ifdef FEAT_DIFF
2780 i = plines_nofill(curwin->w_topline - 1);
2781#else
2782 i = plines(curwin->w_topline - 1);
2783#endif
2784 n -= i;
2785 if (n < 0 && scrolled > 0)
2786 break;
2787 --curwin->w_topline;
2788#ifdef FEAT_FOLDING
2789 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2790#endif
2791#ifdef FEAT_DIFF
2792 curwin->w_topfill = 0;
2793#endif
2794 }
2795 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2796 VALID_BOTLINE|VALID_BOTLINE_AP);
2797 scrolled += i;
2798#ifndef KEEP_SCREEN_LINE
2799 if (curwin->w_cursor.lnum > 1)
2800 {
2801 --curwin->w_cursor.lnum;
2802 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2803 }
2804#endif
2805 }
2806#ifndef KEEP_SCREEN_LINE
2807 /*
2808 * When hit top of the file: move cursor up.
2809 */
2810 if (n > 0)
2811 {
2812 if (curwin->w_cursor.lnum <= (linenr_T)n)
2813 curwin->w_cursor.lnum = 1;
2814 else
2815# ifdef FEAT_FOLDING
2816 if (hasAnyFolding(curwin))
2817 {
2818 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2819 {
2820 --curwin->w_cursor.lnum;
2821 (void)hasFolding(curwin->w_cursor.lnum,
2822 &curwin->w_cursor.lnum, NULL);
2823 }
2824 }
2825 else
2826# endif
2827 curwin->w_cursor.lnum -= n;
2828 }
2829#else
2830 /* try to put the cursor in the same screen line */
2831 scrolled += n; /* move cursor when topline is 1 */
2832 while (curwin->w_cursor.lnum > curwin->w_topline
2833 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2834 {
2835 scrolled -= plines(curwin->w_cursor.lnum - 1);
2836 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2837 break;
2838 --curwin->w_cursor.lnum;
2839# ifdef FEAT_FOLDING
2840 foldAdjustCursor();
2841# endif
2842 }
2843#endif
2844 }
2845# ifdef FEAT_FOLDING
2846 /* Move cursor to first line of closed fold. */
2847 foldAdjustCursor();
2848# endif
2849#ifdef FEAT_DIFF
2850 check_topfill(curwin, !flag);
2851#endif
2852 cursor_correct();
2853 beginline(BL_SOL | BL_FIX);
2854 redraw_later(VALID);
2855}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856
2857#if defined(FEAT_CURSORBIND) || defined(PROTO)
2858 void
2859do_check_cursorbind()
2860{
2861 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002862 colnr_T col = curwin->w_cursor.col;
2863# ifdef FEAT_VIRTUALEDIT
2864 colnr_T coladd = curwin->w_cursor.coladd;
2865# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002866 colnr_T curswant = curwin->w_curswant;
2867 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002868 win_T *old_curwin = curwin;
2869 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002870 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 int old_VIsual_select = VIsual_select;
2872 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873
2874 /*
2875 * loop through the cursorbound windows
2876 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002877 VIsual_select = VIsual_active = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878 for (curwin = firstwin; curwin; curwin = curwin->w_next)
2879 {
2880 curbuf = curwin->w_buffer;
2881 /* skip original window and windows with 'noscrollbind' */
2882 if (curwin != old_curwin && curwin->w_p_crb)
2883 {
2884# ifdef FEAT_DIFF
2885 if (curwin->w_p_diff)
2886 curwin->w_cursor.lnum
2887 = diff_get_corresponding_line(old_curbuf,
2888 line,
2889 curbuf,
2890 curwin->w_cursor.lnum);
2891 else
2892# endif
2893 curwin->w_cursor.lnum = line;
2894 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002895# ifdef FEAT_VIRTUALEDIT
2896 curwin->w_cursor.coladd = coladd;
2897# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002898 curwin->w_curswant = curswant;
2899 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900
Bram Moolenaar61452852011-02-01 18:01:11 +01002901 /* Make sure the cursor is in a valid position. Temporarily set
2902 * "restart_edit" to allow the cursor to be beyond the EOL. */
2903 restart_edit_save = restart_edit;
2904 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905 check_cursor();
Bram Moolenaar61452852011-02-01 18:01:11 +01002906 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002907# ifdef FEAT_MBYTE
2908 /* Correct cursor for multi-byte character. */
2909 if (has_mbyte)
2910 mb_adjust_cursor();
2911# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002912 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002913
2914 /* Only scroll when 'scrollbind' hasn't done this. */
2915 if (!curwin->w_p_scb)
2916 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002917# ifdef FEAT_WINDOWS
2918 curwin->w_redr_status = TRUE;
2919# endif
2920 }
2921 }
2922
2923 /*
2924 * reset current-window
2925 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002926 VIsual_select = old_VIsual_select;
2927 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002928 curwin = old_curwin;
2929 curbuf = old_curbuf;
2930}
2931#endif /* FEAT_CURSORBIND */