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