blob: dff3bb09d7fb8bcb5da9f11a3c5b63a0cb3a6860 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void redraw_for_cursorline(win_T *wp);
23static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
29 linenr_T lnum; /* line number */
30#ifdef FEAT_DIFF
31 int fill; /* filler lines */
32#endif
33 int height; /* height of added line */
34} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
40 * Compute wp->w_botline for the current wp->w_topline. Can be called after
41 * wp->w_topline changed.
42 */
43 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010044comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000045{
46 int n;
47 linenr_T lnum;
48 int done;
49#ifdef FEAT_FOLDING
50 linenr_T last;
51 int folded;
52#endif
53
54 /*
55 * If w_cline_row is valid, start there.
56 * Otherwise have to start at w_topline.
57 */
58 check_cursor_moved(wp);
59 if (wp->w_valid & VALID_CROW)
60 {
61 lnum = wp->w_cursor.lnum;
62 done = wp->w_cline_row;
63 }
64 else
65 {
66 lnum = wp->w_topline;
67 done = 0;
68 }
69
70 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
71 {
72#ifdef FEAT_FOLDING
73 last = lnum;
74 folded = FALSE;
75 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
76 {
77 n = 1;
78 folded = TRUE;
79 }
80 else
81#endif
82#ifdef FEAT_DIFF
83 if (lnum == wp->w_topline)
84 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
85 else
86#endif
87 n = plines_win(wp, lnum, TRUE);
88 if (
89#ifdef FEAT_FOLDING
90 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
91#else
92 lnum == wp->w_cursor.lnum
93#endif
94 )
95 {
96 wp->w_cline_row = done;
97 wp->w_cline_height = n;
98#ifdef FEAT_FOLDING
99 wp->w_cline_folded = folded;
100#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100101 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
103 }
104 if (done + n > wp->w_height)
105 break;
106 done += n;
107#ifdef FEAT_FOLDING
108 lnum = last;
109#endif
110 }
111
112 /* wp->w_botline is the line that is just below the window */
113 wp->w_botline = lnum;
114 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
115
116 set_empty_rows(wp, done);
117}
118
Bram Moolenaar90a99792018-09-12 21:52:18 +0200119#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200120 void
121reset_cursorline(void)
122{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200123 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200124}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200125#endif
126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/*
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
Bram Moolenaar9b578142016-01-30 19:39:49 +0100132redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100133{
134 if ((wp->w_p_rnu
135#ifdef FEAT_SYN_HL
136 || wp->w_p_cul
137#endif
138 )
139 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100140#ifdef FEAT_INS_EXPAND
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100141 && !pum_visible()
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100142#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100143 )
Bram Moolenaar90a99792018-09-12 21:52:18 +0200144 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_rnu)
146 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200147 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200148#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200149 if (wp->w_p_cul)
150 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200151 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200152 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200153 // "w_last_cursorline" may be outdated, worst case we redraw
154 // too much. This is optimized for moving the cursor around in
155 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100156 redrawWinline(wp, wp->w_last_cursorline);
157 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200158 }
159 else
160 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200161 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200162 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200163#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200164 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100165}
166
167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 * Update curwin->w_topline and redraw if necessary.
169 * Used to update the screen before printing a message.
170 */
171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100172update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173{
174 update_topline();
175 if (must_redraw)
176 update_screen(0);
177}
178
179/*
180 * Update curwin->w_topline to move the cursor onto the screen.
181 */
182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100183update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
185 long line_count;
186 int halfheight;
187 int n;
188 linenr_T old_topline;
189#ifdef FEAT_DIFF
190 int old_topfill;
191#endif
192#ifdef FEAT_FOLDING
193 linenr_T lnum;
194#endif
195 int check_topline = FALSE;
196 int check_botline = FALSE;
197#ifdef FEAT_MOUSE
198 int save_so = p_so;
199#endif
200
Bram Moolenaard5d37532017-03-27 23:02:07 +0200201 /* If there is no valid screen and when the window height is zero just use
202 * the cursor line. */
203 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200204 {
205 curwin->w_topline = curwin->w_cursor.lnum;
206 curwin->w_botline = curwin->w_topline;
207 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200208 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200209 return;
210 }
211
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 check_cursor_moved(curwin);
213 if (curwin->w_valid & VALID_TOPLINE)
214 return;
215
216#ifdef FEAT_MOUSE
217 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000218 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 p_so = mouse_dragging - 1;
220#endif
221
222 old_topline = curwin->w_topline;
223#ifdef FEAT_DIFF
224 old_topfill = curwin->w_topfill;
225#endif
226
227 /*
228 * If the buffer is empty, always set topline to 1.
229 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100230 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {
232 if (curwin->w_topline != 1)
233 redraw_later(NOT_VALID);
234 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 curwin->w_botline = 2;
236 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 }
239
240 /*
241 * If the cursor is above or near the top of the window, scroll the window
242 * to show the line the cursor is in, with 'scrolloff' context.
243 */
244 else
245 {
246 if (curwin->w_topline > 1)
247 {
248 /* If the cursor is above topline, scrolling is always needed.
249 * If the cursor is far below topline and there is no folding,
250 * scrolling down is never needed. */
251 if (curwin->w_cursor.lnum < curwin->w_topline)
252 check_topline = TRUE;
253 else if (check_top_offset())
254 check_topline = TRUE;
255 }
256#ifdef FEAT_DIFF
257 /* Check if there are more filler lines than allowed. */
258 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
259 curwin->w_topline))
260 check_topline = TRUE;
261#endif
262
263 if (check_topline)
264 {
265 halfheight = curwin->w_height / 2 - 1;
266 if (halfheight < 2)
267 halfheight = 2;
268
269#ifdef FEAT_FOLDING
270 if (hasAnyFolding(curwin))
271 {
272 /* Count the number of logical lines between the cursor and
273 * topline + p_so (approximation of how much will be
274 * scrolled). */
275 n = 0;
276 for (lnum = curwin->w_cursor.lnum;
277 lnum < curwin->w_topline + p_so; ++lnum)
278 {
279 ++n;
280 /* stop at end of file or when we know we are far off */
281 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
282 break;
283 (void)hasFolding(lnum, NULL, &lnum);
284 }
285 }
286 else
287#endif
288 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
289
290 /* If we weren't very close to begin with, we scroll to put the
291 * cursor in the middle of the window. Otherwise put the cursor
292 * near the top of the window. */
293 if (n >= halfheight)
294 scroll_cursor_halfway(FALSE);
295 else
296 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000297 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 check_botline = TRUE;
299 }
300 }
301
302 else
303 {
304#ifdef FEAT_FOLDING
305 /* Make sure topline is the first line of a fold. */
306 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
307#endif
308 check_botline = TRUE;
309 }
310 }
311
312 /*
313 * If the cursor is below the bottom of the window, scroll the window
314 * to put the cursor on the window.
315 * When w_botline is invalid, recompute it first, to avoid a redraw later.
316 * If w_botline was approximated, we might need a redraw later in a few
317 * cases, but we don't want to spend (a lot of) time recomputing w_botline
318 * for every small change.
319 */
320 if (check_botline)
321 {
322 if (!(curwin->w_valid & VALID_BOTLINE_AP))
323 validate_botline();
324
325 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
326 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000327 if (curwin->w_cursor.lnum < curwin->w_botline)
328 {
329 if (((long)curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 >= (long)curwin->w_botline - p_so
331#ifdef FEAT_FOLDING
332 || hasAnyFolding(curwin)
333#endif
334 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 lineoff_T loff;
337
Bram Moolenaard4153d42008-11-15 15:06:17 +0000338 /* Cursor is (a few lines) above botline, check if there are
339 * 'scrolloff' window lines below the cursor. If not, need to
340 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 n = curwin->w_empty_rows;
342 loff.lnum = curwin->w_cursor.lnum;
343#ifdef FEAT_FOLDING
344 /* In a fold go to its last line. */
345 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
346#endif
347#ifdef FEAT_DIFF
348 loff.fill = 0;
349 n += curwin->w_filler_rows;
350#endif
351 loff.height = 0;
352 while (loff.lnum < curwin->w_botline
353#ifdef FEAT_DIFF
354 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
355#endif
356 )
357 {
358 n += loff.height;
359 if (n >= p_so)
360 break;
361 botline_forw(&loff);
362 }
363 if (n >= p_so)
364 /* sufficient context, no need to scroll */
365 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000366 }
367 else
368 /* sufficient context, no need to scroll */
369 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 if (check_botline)
372 {
373#ifdef FEAT_FOLDING
374 if (hasAnyFolding(curwin))
375 {
376 /* Count the number of logical lines between the cursor and
377 * botline - p_so (approximation of how much will be
378 * scrolled). */
379 line_count = 0;
380 for (lnum = curwin->w_cursor.lnum;
381 lnum >= curwin->w_botline - p_so; --lnum)
382 {
383 ++line_count;
384 /* stop at end of file or when we know we are far off */
385 if (lnum <= 0 || line_count > curwin->w_height + 1)
386 break;
387 (void)hasFolding(lnum, &lnum, NULL);
388 }
389 }
390 else
391#endif
392 line_count = curwin->w_cursor.lnum - curwin->w_botline
393 + 1 + p_so;
394 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000395 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 else
397 scroll_cursor_halfway(FALSE);
398 }
399 }
400 }
401 curwin->w_valid |= VALID_TOPLINE;
402
403 /*
404 * Need to redraw when topline changed.
405 */
406 if (curwin->w_topline != old_topline
407#ifdef FEAT_DIFF
408 || curwin->w_topfill != old_topfill
409#endif
410 )
411 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100412 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000413 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {
415 curwin->w_skipcol = 0;
416 redraw_later(NOT_VALID);
417 }
418 else
419 redraw_later(VALID);
420 /* May need to set w_skipcol when cursor in w_topline. */
421 if (curwin->w_cursor.lnum == curwin->w_topline)
422 validate_cursor();
423 }
424
425#ifdef FEAT_MOUSE
426 p_so = save_so;
427#endif
428}
429
430/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000431 * Return the scrolljump value to use for the current window.
432 * When 'scrolljump' is positive use it as-is.
433 * When 'scrolljump' is negative use it as a percentage of the window height.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000437{
438 if (p_sj >= 0)
439 return (int)p_sj;
440 return (curwin->w_height * -p_sj) / 100;
441}
442
443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
445 * current window.
446 */
447 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100448check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449{
450 lineoff_T loff;
451 int n;
452
453 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
454#ifdef FEAT_FOLDING
455 || hasAnyFolding(curwin)
456#endif
457 )
458 {
459 loff.lnum = curwin->w_cursor.lnum;
460#ifdef FEAT_DIFF
461 loff.fill = 0;
462 n = curwin->w_topfill; /* always have this context */
463#else
464 n = 0;
465#endif
466 /* Count the visible screen lines above the cursor line. */
467 while (n < p_so)
468 {
469 topline_back(&loff);
470 /* Stop when included a line above the window. */
471 if (loff.lnum < curwin->w_topline
472#ifdef FEAT_DIFF
473 || (loff.lnum == curwin->w_topline && loff.fill > 0)
474#endif
475 )
476 break;
477 n += loff.height;
478 }
479 if (n < p_so)
480 return TRUE;
481 }
482 return FALSE;
483}
484
485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100486update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 if (curwin->w_set_curswant)
489 {
490 validate_virtcol();
491 curwin->w_curswant = curwin->w_virtcol;
492 curwin->w_set_curswant = FALSE;
493 }
494}
495
496/*
497 * Check if the cursor has moved. Set the w_valid flag accordingly.
498 */
499 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100500check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
503 {
504 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
505 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
506 wp->w_valid_cursor = wp->w_cursor;
507 wp->w_valid_leftcol = wp->w_leftcol;
508 }
509 else if (wp->w_cursor.col != wp->w_valid_cursor.col
510 || wp->w_leftcol != wp->w_valid_leftcol
511#ifdef FEAT_VIRTUALEDIT
512 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
513#endif
514 )
515 {
516 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
517 wp->w_valid_cursor.col = wp->w_cursor.col;
518 wp->w_valid_leftcol = wp->w_leftcol;
519#ifdef FEAT_VIRTUALEDIT
520 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
521#endif
522 }
523}
524
525/*
526 * Call this function when some window settings have changed, which require
527 * the cursor position, botline and topline to be recomputed and the window to
528 * be redrawn. E.g, when changing the 'wrap' option or folding.
529 */
530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100531changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 changed_window_setting_win(curwin);
534}
535
536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100537changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 wp->w_lines_valid = 0;
540 changed_line_abv_curs_win(wp);
541 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
542 redraw_win_later(wp, NOT_VALID);
543}
544
545/*
546 * Set wp->w_topline to a certain number.
547 */
548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100549set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551#ifdef FEAT_FOLDING
552 /* go to first of folded lines */
553 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
554#endif
555 /* Approximate the value of w_botline */
556 wp->w_botline += lnum - wp->w_topline;
557 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000558 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_DIFF
560 wp->w_topfill = 0;
561#endif
562 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
563 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
564 redraw_later(VALID);
565}
566
567/*
568 * Call this function when the length of the cursor line (in screen
569 * characters) has changed, and the change is before the cursor.
570 * Need to take care of w_botline separately!
571 */
572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100573changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
576 |VALID_CHEIGHT|VALID_TOPLINE);
577}
578
579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100580changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
583 |VALID_CHEIGHT|VALID_TOPLINE);
584}
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586/*
587 * Call this function when the length of a line (in screen characters) above
588 * the cursor have changed.
589 * Need to take care of w_botline separately!
590 */
591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100592changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
595 |VALID_CHEIGHT|VALID_TOPLINE);
596}
597
598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
602 |VALID_CHEIGHT|VALID_TOPLINE);
603}
604
605/*
606 * Make sure the value of curwin->w_botline is valid.
607 */
608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (!(curwin->w_valid & VALID_BOTLINE))
612 comp_botline(curwin);
613}
614
615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * Mark curwin->w_botline as invalid (because of some change in the buffer).
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
622}
623
624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100625invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
628}
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631approximate_botline_win(
632 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 wp->w_valid &= ~VALID_BOTLINE;
635}
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637/*
638 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
639 */
640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100641cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 check_cursor_moved(curwin);
644 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
645 (VALID_WROW|VALID_WCOL));
646}
647
648/*
649 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
650 * w_topline must be valid, you may need to call update_topline() first!
651 */
652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100653validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 check_cursor_moved(curwin);
656 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
657 curs_columns(TRUE);
658}
659
660#if defined(FEAT_GUI) || defined(PROTO)
661/*
662 * validate w_cline_row.
663 */
664 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 /*
668 * First make sure that w_topline is valid (after moving the cursor).
669 */
670 update_topline();
671 check_cursor_moved(curwin);
672 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100673 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674}
675#endif
676
677/*
678 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200679 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 linenr_T lnum;
685 int i;
686 int all_invalid;
687 int valid;
688#ifdef FEAT_FOLDING
689 long fold_count;
690#endif
691
692 /* Check if wp->w_lines[].wl_size is invalid */
693 all_invalid = (!redrawing()
694 || wp->w_lines_valid == 0
695 || wp->w_lines[0].wl_lnum > wp->w_topline);
696 i = 0;
697 wp->w_cline_row = 0;
698 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
699 {
700 valid = FALSE;
701 if (!all_invalid && i < wp->w_lines_valid)
702 {
703 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
704 continue; /* skip changed or deleted lines */
705 if (wp->w_lines[i].wl_lnum == lnum)
706 {
707#ifdef FEAT_FOLDING
708 /* Check for newly inserted lines below this row, in which
709 * case we need to check for folded lines. */
710 if (!wp->w_buffer->b_mod_set
711 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
712 || wp->w_buffer->b_mod_top
713 > wp->w_lines[i].wl_lastlnum + 1)
714#endif
715 valid = TRUE;
716 }
717 else if (wp->w_lines[i].wl_lnum > lnum)
718 --i; /* hold at inserted lines */
719 }
720 if (valid
721#ifdef FEAT_DIFF
722 && (lnum != wp->w_topline || !wp->w_p_diff)
723#endif
724 )
725 {
726#ifdef FEAT_FOLDING
727 lnum = wp->w_lines[i].wl_lastlnum + 1;
728 /* Cursor inside folded lines, don't count this row */
729 if (lnum > wp->w_cursor.lnum)
730 break;
731#else
732 ++lnum;
733#endif
734 wp->w_cline_row += wp->w_lines[i].wl_size;
735 }
736 else
737 {
738#ifdef FEAT_FOLDING
739 fold_count = foldedCount(wp, lnum, NULL);
740 if (fold_count)
741 {
742 lnum += fold_count;
743 if (lnum > wp->w_cursor.lnum)
744 break;
745 ++wp->w_cline_row;
746 }
747 else
748#endif
749#ifdef FEAT_DIFF
750 if (lnum == wp->w_topline)
751 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
752 + wp->w_topfill;
753 else
754#endif
755 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
756 }
757 }
758
759 check_cursor_moved(wp);
760 if (!(wp->w_valid & VALID_CHEIGHT))
761 {
762 if (all_invalid
763 || i == wp->w_lines_valid
764 || (i < wp->w_lines_valid
765 && (!wp->w_lines[i].wl_valid
766 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
767 {
768#ifdef FEAT_DIFF
769 if (wp->w_cursor.lnum == wp->w_topline)
770 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
771 TRUE) + wp->w_topfill;
772 else
773#endif
774 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
775#ifdef FEAT_FOLDING
776 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
777 NULL, NULL, TRUE, NULL);
778#endif
779 }
780 else if (i > wp->w_lines_valid)
781 {
782 /* a line that is too long to fit on the last screen line */
783 wp->w_cline_height = 0;
784#ifdef FEAT_FOLDING
785 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
786 NULL, NULL, TRUE, NULL);
787#endif
788 }
789 else
790 {
791 wp->w_cline_height = wp->w_lines[i].wl_size;
792#ifdef FEAT_FOLDING
793 wp->w_cline_folded = wp->w_lines[i].wl_folded;
794#endif
795 }
796 }
797
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100798 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Validate curwin->w_virtcol only.
805 */
806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100807validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
809 validate_virtcol_win(curwin);
810}
811
812/*
813 * Validate wp->w_virtcol only.
814 */
815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 check_cursor_moved(wp);
819 if (!(wp->w_valid & VALID_VIRTCOL))
820 {
821 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
822 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000823#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000824 if (wp->w_p_cuc
825# ifdef FEAT_INS_EXPAND
826 && !pum_visible()
827# endif
828 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000829 redraw_win_later(wp, SOME_VALID);
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832}
833
834/*
835 * Validate curwin->w_cline_height only.
836 */
837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 check_cursor_moved(curwin);
841 if (!(curwin->w_valid & VALID_CHEIGHT))
842 {
843#ifdef FEAT_DIFF
844 if (curwin->w_cursor.lnum == curwin->w_topline)
845 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
846 + curwin->w_topfill;
847 else
848#endif
849 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
850#ifdef FEAT_FOLDING
851 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
852#endif
853 curwin->w_valid |= VALID_CHEIGHT;
854 }
855}
856
857/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000858 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100861validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 colnr_T off;
864 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100865 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 validate_virtcol();
868 if (!(curwin->w_valid & VALID_WCOL))
869 {
870 col = curwin->w_virtcol;
871 off = curwin_col_off();
872 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200873 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000876 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200877 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100878 && width > 0)
879 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200880 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000881 if (col > (int)curwin->w_leftcol)
882 col -= curwin->w_leftcol;
883 else
884 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 curwin->w_valid |= VALID_WCOL;
888 }
889}
890
891/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200892 * Compute offset of a window, occupied by absolute or relative line number,
893 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 */
895 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100896win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
Bram Moolenaar64486672010-05-16 15:46:46 +0200898 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#ifdef FEAT_CMDWIN
900 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
901#endif
902#ifdef FEAT_FOLDING
903 + wp->w_p_fdc
904#endif
905#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200906 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 );
909}
910
911 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100912curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913{
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
Bram Moolenaar9b578142016-01-30 19:39:49 +0100923win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
Bram Moolenaar64486672010-05-16 15:46:46 +0200925 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000926 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 return 0;
928}
929
930 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100931curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
933 return win_col_off2(curwin);
934}
935
936/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100937 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 * Also updates curwin->w_wrow and curwin->w_cline_row.
939 * Also updates curwin->w_leftcol.
940 */
941 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100942curs_columns(
943 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945 int diff;
946 int extra; /* offset for first screen line */
947 int off_left, off_right;
948 int n;
949 int p_lines;
950 int width = 0;
951 int textwidth;
952 int new_leftcol;
953 colnr_T startcol;
954 colnr_T endcol;
955 colnr_T prev_skipcol;
956
957 /*
958 * First make sure that w_topline is valid (after moving the cursor).
959 */
960 update_topline();
961
962 /*
963 * Next make sure that w_cline_row is valid.
964 */
965 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100966 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 /*
969 * Compute the number of virtual columns.
970 */
971#ifdef FEAT_FOLDING
972 if (curwin->w_cline_folded)
973 /* In a folded line the cursor is always in the first column */
974 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
975 else
976#endif
977 getvvcol(curwin, &curwin->w_cursor,
978 &startcol, &(curwin->w_virtcol), &endcol);
979
980 /* remove '$' from change command when cursor moves onto it */
981 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100982 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 extra = curwin_col_off();
985 curwin->w_wcol = curwin->w_virtcol + extra;
986 endcol += extra;
987
988 /*
989 * Now compute w_wrow, counting screen lines from w_cline_row.
990 */
991 curwin->w_wrow = curwin->w_cline_row;
992
Bram Moolenaar02631462017-09-22 15:20:32 +0200993 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (textwidth <= 0)
995 {
996 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 curwin->w_wrow = curwin->w_height - 1;
999 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001000 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {
1002 width = textwidth + curwin_col_off2();
1003
1004 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +02001005 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001007 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +02001008 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 curwin->w_wcol -= n * width;
1010 curwin->w_wrow += n;
1011
1012#ifdef FEAT_LINEBREAK
1013 /* When cursor wraps to first char of next line in Insert
1014 * mode, the 'showbreak' string isn't shown, backup to first
1015 * column */
1016 if (*p_sbr && *ml_get_cursor() == NUL
1017 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1018 curwin->w_wcol = 0;
1019#endif
1020 }
1021 }
1022
1023 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1024 * is not folded.
1025 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001026 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_FOLDING
1028 && !curwin->w_cline_folded
1029#endif
1030 )
1031 {
1032 /*
1033 * If Cursor is left of the screen, scroll rightwards.
1034 * If Cursor is right of the screen, scroll leftwards
1035 * If we get closer to the edge than 'sidescrolloff', scroll a little
1036 * extra
1037 */
1038 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001039 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 - p_siso) + 1;
1041 if (off_left < 0 || off_right > 0)
1042 {
1043 if (off_left < 0)
1044 diff = -off_left;
1045 else
1046 diff = off_right;
1047
1048 /* When far off or not enough room on either side, put cursor in
1049 * middle of window. */
1050 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1051 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1052 else
1053 {
1054 if (diff < p_ss)
1055 diff = p_ss;
1056 if (off_left < 0)
1057 new_leftcol = curwin->w_leftcol - diff;
1058 else
1059 new_leftcol = curwin->w_leftcol + diff;
1060 }
1061 if (new_leftcol < 0)
1062 new_leftcol = 0;
1063 if (new_leftcol != (int)curwin->w_leftcol)
1064 {
1065 curwin->w_leftcol = new_leftcol;
1066 /* screen has to be redrawn with new curwin->w_leftcol */
1067 redraw_later(NOT_VALID);
1068 }
1069 }
1070 curwin->w_wcol -= curwin->w_leftcol;
1071 }
1072 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1073 curwin->w_wcol -= curwin->w_leftcol;
1074 else
1075 curwin->w_wcol = 0;
1076
1077#ifdef FEAT_DIFF
1078 /* Skip over filler lines. At the top use w_topfill, there
1079 * may be some filler lines above the window. */
1080 if (curwin->w_cursor.lnum == curwin->w_topline)
1081 curwin->w_wrow += curwin->w_topfill;
1082 else
1083 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1084#endif
1085
1086 prev_skipcol = curwin->w_skipcol;
1087
1088 p_lines = 0;
1089 if ((curwin->w_wrow >= curwin->w_height
1090 || ((prev_skipcol > 0
1091 || curwin->w_wrow + p_so >= curwin->w_height)
1092 && (p_lines =
1093#ifdef FEAT_DIFF
1094 plines_win_nofill
1095#else
1096 plines_win
1097#endif
1098 (curwin, curwin->w_cursor.lnum, FALSE))
1099 - 1 >= curwin->w_height))
1100 && curwin->w_height != 0
1101 && curwin->w_cursor.lnum == curwin->w_topline
1102 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001103 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
1105 /* Cursor past end of screen. Happens with a single line that does
1106 * not fit on screen. Find a skipcol to show the text around the
1107 * cursor. Avoid scrolling all the time. compute value of "extra":
1108 * 1: Less than "p_so" lines above
1109 * 2: Less than "p_so" lines below
1110 * 3: both of them */
1111 extra = 0;
1112 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1113 extra = 1;
1114 /* Compute last display line of the buffer line that we want at the
1115 * bottom of the window. */
1116 if (p_lines == 0)
1117 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1118 --p_lines;
1119 if (p_lines > curwin->w_wrow + p_so)
1120 n = curwin->w_wrow + p_so;
1121 else
1122 n = p_lines;
1123 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1124 extra += 2;
1125
1126 if (extra == 3 || p_lines < p_so * 2)
1127 {
1128 /* not enough room for 'scrolloff', put cursor in the middle */
1129 n = curwin->w_virtcol / width;
1130 if (n > curwin->w_height / 2)
1131 n -= curwin->w_height / 2;
1132 else
1133 n = 0;
1134 /* don't skip more than necessary */
1135 if (n > p_lines - curwin->w_height + 1)
1136 n = p_lines - curwin->w_height + 1;
1137 curwin->w_skipcol = n * width;
1138 }
1139 else if (extra == 1)
1140 {
1141 /* less then 'scrolloff' lines above, decrease skipcol */
1142 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1143 + width - 1) / width;
1144 if (extra > 0)
1145 {
1146 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1147 extra = curwin->w_skipcol / width;
1148 curwin->w_skipcol -= extra * width;
1149 }
1150 }
1151 else if (extra == 2)
1152 {
1153 /* less then 'scrolloff' lines below, increase skipcol */
1154 endcol = (n - curwin->w_height + 1) * width;
1155 while (endcol > curwin->w_virtcol)
1156 endcol -= width;
1157 if (endcol > curwin->w_skipcol)
1158 curwin->w_skipcol = endcol;
1159 }
1160
1161 curwin->w_wrow -= curwin->w_skipcol / width;
1162 if (curwin->w_wrow >= curwin->w_height)
1163 {
1164 /* small window, make sure cursor is in it */
1165 extra = curwin->w_wrow - curwin->w_height + 1;
1166 curwin->w_skipcol += extra * width;
1167 curwin->w_wrow -= extra;
1168 }
1169
1170 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1171 if (extra > 0)
1172 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1173 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001174 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 else
1177 curwin->w_skipcol = 0;
1178 if (prev_skipcol != curwin->w_skipcol)
1179 redraw_later(NOT_VALID);
1180
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001181#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001182 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1183 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001184# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001185 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001186# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001187 )
1188 redraw_later(SOME_VALID);
1189#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1192}
1193
1194/*
1195 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001198scrolldown(
1199 long line_count,
1200 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 long done = 0; /* total # of physical lines done */
1203 int wrow;
1204 int moved = FALSE;
1205
1206#ifdef FEAT_FOLDING
1207 linenr_T first;
1208
1209 /* Make sure w_topline is at the first of a sequence of folded lines. */
1210 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1211#endif
1212 validate_cursor(); /* w_wrow needs to be valid */
1213 while (line_count-- > 0)
1214 {
1215#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001216 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1217 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 ++curwin->w_topfill;
1220 ++done;
1221 }
1222 else
1223#endif
1224 {
1225 if (curwin->w_topline == 1)
1226 break;
1227 --curwin->w_topline;
1228#ifdef FEAT_DIFF
1229 curwin->w_topfill = 0;
1230#endif
1231#ifdef FEAT_FOLDING
1232 /* A sequence of folded lines only counts for one logical line */
1233 if (hasFolding(curwin->w_topline, &first, NULL))
1234 {
1235 ++done;
1236 if (!byfold)
1237 line_count -= curwin->w_topline - first - 1;
1238 curwin->w_botline -= curwin->w_topline - first;
1239 curwin->w_topline = first;
1240 }
1241 else
1242#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001243 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 --curwin->w_botline; /* approximate w_botline */
1246 invalidate_botline();
1247 }
1248 curwin->w_wrow += done; /* keep w_wrow updated */
1249 curwin->w_cline_row += done; /* keep w_cline_row updated */
1250
1251#ifdef FEAT_DIFF
1252 if (curwin->w_cursor.lnum == curwin->w_topline)
1253 curwin->w_cline_row = 0;
1254 check_topfill(curwin, TRUE);
1255#endif
1256
1257 /*
1258 * Compute the row number of the last row of the cursor line
1259 * and move the cursor onto the displayed part of the window.
1260 */
1261 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001262 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 validate_virtcol();
1265 validate_cheight();
1266 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001267 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1270 {
1271#ifdef FEAT_FOLDING
1272 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1273 {
1274 --wrow;
1275 if (first == 1)
1276 curwin->w_cursor.lnum = 1;
1277 else
1278 curwin->w_cursor.lnum = first - 1;
1279 }
1280 else
1281#endif
1282 wrow -= plines(curwin->w_cursor.lnum--);
1283 curwin->w_valid &=
1284 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1285 moved = TRUE;
1286 }
1287 if (moved)
1288 {
1289#ifdef FEAT_FOLDING
1290 /* Move cursor to first line of closed fold. */
1291 foldAdjustCursor();
1292#endif
1293 coladvance(curwin->w_curswant);
1294 }
1295}
1296
1297/*
1298 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301scrollup(
1302 long line_count,
1303 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1306 linenr_T lnum;
1307
1308 if (
1309# ifdef FEAT_FOLDING
1310 (byfold && hasAnyFolding(curwin))
1311# ifdef FEAT_DIFF
1312 ||
1313# endif
1314# endif
1315# ifdef FEAT_DIFF
1316 curwin->w_p_diff
1317# endif
1318 )
1319 {
1320 /* count each sequence of folded lines as one logical line */
1321 lnum = curwin->w_topline;
1322 while (line_count--)
1323 {
1324# ifdef FEAT_DIFF
1325 if (curwin->w_topfill > 0)
1326 --curwin->w_topfill;
1327 else
1328# endif
1329 {
1330# ifdef FEAT_FOLDING
1331 if (byfold)
1332 (void)hasFolding(lnum, NULL, &lnum);
1333# endif
1334 if (lnum >= curbuf->b_ml.ml_line_count)
1335 break;
1336 ++lnum;
1337# ifdef FEAT_DIFF
1338 curwin->w_topfill = diff_check_fill(curwin, lnum);
1339# endif
1340 }
1341 }
1342 /* approximate w_botline */
1343 curwin->w_botline += lnum - curwin->w_topline;
1344 curwin->w_topline = lnum;
1345 }
1346 else
1347#endif
1348 {
1349 curwin->w_topline += line_count;
1350 curwin->w_botline += line_count; /* approximate w_botline */
1351 }
1352
1353 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1354 curwin->w_topline = curbuf->b_ml.ml_line_count;
1355 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1356 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1357
1358#ifdef FEAT_DIFF
1359 check_topfill(curwin, FALSE);
1360#endif
1361
1362#ifdef FEAT_FOLDING
1363 if (hasAnyFolding(curwin))
1364 /* Make sure w_topline is at the first of a sequence of folded lines. */
1365 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1366#endif
1367
1368 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1369 if (curwin->w_cursor.lnum < curwin->w_topline)
1370 {
1371 curwin->w_cursor.lnum = curwin->w_topline;
1372 curwin->w_valid &=
1373 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1374 coladvance(curwin->w_curswant);
1375 }
1376}
1377
1378#ifdef FEAT_DIFF
1379/*
1380 * Don't end up with too many filler lines in the window.
1381 */
1382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001383check_topfill(
1384 win_T *wp,
1385 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 int n;
1388
1389 if (wp->w_topfill > 0)
1390 {
1391 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1392 if (wp->w_topfill + n > wp->w_height)
1393 {
1394 if (down && wp->w_topline > 1)
1395 {
1396 --wp->w_topline;
1397 wp->w_topfill = 0;
1398 }
1399 else
1400 {
1401 wp->w_topfill = wp->w_height - n;
1402 if (wp->w_topfill < 0)
1403 wp->w_topfill = 0;
1404 }
1405 }
1406 }
1407}
1408
1409/*
1410 * Use as many filler lines as possible for w_topline. Make sure w_topline
1411 * is still visible.
1412 */
1413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001414max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 int n;
1417
1418 n = plines_nofill(curwin->w_topline);
1419 if (n >= curwin->w_height)
1420 curwin->w_topfill = 0;
1421 else
1422 {
1423 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1424 if (curwin->w_topfill + n > curwin->w_height)
1425 curwin->w_topfill = curwin->w_height - n;
1426 }
1427}
1428#endif
1429
1430#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1431/*
1432 * Scroll the screen one line down, but don't do it if it would move the
1433 * cursor off the screen.
1434 */
1435 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001436scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 int end_row;
1439#ifdef FEAT_DIFF
1440 int can_fill = (curwin->w_topfill
1441 < diff_check_fill(curwin, curwin->w_topline));
1442#endif
1443
1444 if (curwin->w_topline <= 1
1445#ifdef FEAT_DIFF
1446 && !can_fill
1447#endif
1448 )
1449 return;
1450
1451 validate_cursor(); /* w_wrow needs to be valid */
1452
1453 /*
1454 * Compute the row number of the last row of the cursor line
1455 * and make sure it doesn't go off the screen. Make sure the cursor
1456 * doesn't go past 'scrolloff' lines from the screen end.
1457 */
1458 end_row = curwin->w_wrow;
1459#ifdef FEAT_DIFF
1460 if (can_fill)
1461 ++end_row;
1462 else
1463 end_row += plines_nofill(curwin->w_topline - 1);
1464#else
1465 end_row += plines(curwin->w_topline - 1);
1466#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001467 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 validate_cheight();
1470 validate_virtcol();
1471 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001472 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 if (end_row < curwin->w_height - p_so)
1475 {
1476#ifdef FEAT_DIFF
1477 if (can_fill)
1478 {
1479 ++curwin->w_topfill;
1480 check_topfill(curwin, TRUE);
1481 }
1482 else
1483 {
1484 --curwin->w_topline;
1485 curwin->w_topfill = 0;
1486 }
1487#else
1488 --curwin->w_topline;
1489#endif
1490#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001491 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492#endif
1493 --curwin->w_botline; /* approximate w_botline */
1494 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1495 }
1496}
1497
1498/*
1499 * Scroll the screen one line up, but don't do it if it would move the cursor
1500 * off the screen.
1501 */
1502 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 int start_row;
1506
1507 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1508#ifdef FEAT_DIFF
1509 && curwin->w_topfill == 0
1510#endif
1511 )
1512 return;
1513
1514 validate_cursor(); /* w_wrow needs to be valid */
1515
1516 /*
1517 * Compute the row number of the first row of the cursor line
1518 * and make sure it doesn't go off the screen. Make sure the cursor
1519 * doesn't go before 'scrolloff' lines from the screen start.
1520 */
1521#ifdef FEAT_DIFF
1522 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1523 - curwin->w_topfill;
1524#else
1525 start_row = curwin->w_wrow - plines(curwin->w_topline);
1526#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001527 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001530 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 if (start_row >= p_so)
1533 {
1534#ifdef FEAT_DIFF
1535 if (curwin->w_topfill > 0)
1536 --curwin->w_topfill;
1537 else
1538#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001539 {
1540#ifdef FEAT_FOLDING
1541 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 ++curwin->w_botline; /* approximate w_botline */
1546 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1547 }
1548}
1549#endif /* FEAT_INS_EXPAND */
1550
1551/*
1552 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1553 * a (wrapped) text line. Uses and sets "lp->fill".
1554 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001555 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
1557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001558topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560#ifdef FEAT_DIFF
1561 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1562 {
1563 /* Add a filler line. */
1564 ++lp->fill;
1565 lp->height = 1;
1566 }
1567 else
1568#endif
1569 {
1570 --lp->lnum;
1571#ifdef FEAT_DIFF
1572 lp->fill = 0;
1573#endif
1574 if (lp->lnum < 1)
1575 lp->height = MAXCOL;
1576 else
1577#ifdef FEAT_FOLDING
1578 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1579 /* Add a closed fold */
1580 lp->height = 1;
1581 else
1582#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001583 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585}
1586
1587/*
1588 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1589 * a (wrapped) text line. Uses and sets "lp->fill".
1590 * Returns the height of the added line in "lp->height".
1591 * Lines below the last one are incredibly high.
1592 */
1593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001594botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596#ifdef FEAT_DIFF
1597 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1598 {
1599 /* Add a filler line. */
1600 ++lp->fill;
1601 lp->height = 1;
1602 }
1603 else
1604#endif
1605 {
1606 ++lp->lnum;
1607#ifdef FEAT_DIFF
1608 lp->fill = 0;
1609#endif
1610 if (lp->lnum > curbuf->b_ml.ml_line_count)
1611 lp->height = MAXCOL;
1612 else
1613#ifdef FEAT_FOLDING
1614 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1615 /* Add a closed fold */
1616 lp->height = 1;
1617 else
1618#endif
1619 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001620 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
1622 }
1623}
1624
1625#ifdef FEAT_DIFF
1626/*
1627 * Switch from including filler lines below lp->lnum to including filler
1628 * lines above loff.lnum + 1. This keeps pointing to the same line.
1629 * When there are no filler lines nothing changes.
1630 */
1631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001632botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 if (lp->fill > 0)
1635 {
1636 ++lp->lnum;
1637 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1638 }
1639}
1640
1641/*
1642 * Switch from including filler lines above lp->lnum to including filler
1643 * lines below loff.lnum - 1. This keeps pointing to the same line.
1644 * When there are no filler lines nothing changes.
1645 */
1646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001647topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 if (lp->fill > 0)
1650 {
1651 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1652 --lp->lnum;
1653 }
1654}
1655#endif
1656
1657/*
1658 * Recompute topline to put the cursor at the top of the window.
1659 * Scroll at least "min_scroll" lines.
1660 * If "always" is TRUE, always set topline (for "zt").
1661 */
1662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001663scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 int scrolled = 0;
1666 int extra = 0;
1667 int used;
1668 int i;
1669 linenr_T top; /* just above displayed lines */
1670 linenr_T bot; /* just below displayed lines */
1671 linenr_T old_topline = curwin->w_topline;
1672#ifdef FEAT_DIFF
1673 linenr_T old_topfill = curwin->w_topfill;
1674#endif
1675 linenr_T new_topline;
1676 int off = p_so;
1677
1678#ifdef FEAT_MOUSE
1679 if (mouse_dragging > 0)
1680 off = mouse_dragging - 1;
1681#endif
1682
1683 /*
1684 * Decrease topline until:
1685 * - it has become 1
1686 * - (part of) the cursor line is moved off the screen or
1687 * - moved at least 'scrolljump' lines and
1688 * - at least 'scrolloff' lines above and below the cursor
1689 */
1690 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001691 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (curwin->w_cursor.lnum < curwin->w_topline)
1693 scrolled = used;
1694
1695#ifdef FEAT_FOLDING
1696 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1697 {
1698 --top;
1699 ++bot;
1700 }
1701 else
1702#endif
1703 {
1704 top = curwin->w_cursor.lnum - 1;
1705 bot = curwin->w_cursor.lnum + 1;
1706 }
1707 new_topline = top + 1;
1708
1709#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001710 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001711 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001712 * Hide filler lines above cursor line by adding them to "extra". */
1713 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#endif
1715
1716 /*
1717 * Check if the lines from "top" to "bot" fit in the window. If they do,
1718 * set new_topline and advance "top" and "bot" to include more lines.
1719 */
1720 while (top > 0)
1721 {
1722#ifdef FEAT_FOLDING
1723 if (hasFolding(top, &top, NULL))
1724 /* count one logical line for a sequence of folded lines */
1725 i = 1;
1726 else
1727#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001728 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 used += i;
1730 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1731 {
1732#ifdef FEAT_FOLDING
1733 if (hasFolding(bot, NULL, &bot))
1734 /* count one logical line for a sequence of folded lines */
1735 ++used;
1736 else
1737#endif
1738 used += plines(bot);
1739 }
1740 if (used > curwin->w_height)
1741 break;
1742 if (top < curwin->w_topline)
1743 scrolled += i;
1744
1745 /*
1746 * If scrolling is needed, scroll at least 'sj' lines.
1747 */
1748 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1749 && extra >= off)
1750 break;
1751
1752 extra += i;
1753 new_topline = top;
1754 --top;
1755 ++bot;
1756 }
1757
1758 /*
1759 * If we don't have enough space, put cursor in the middle.
1760 * This makes sure we get the same position when using "k" and "j"
1761 * in a small window.
1762 */
1763 if (used > curwin->w_height)
1764 scroll_cursor_halfway(FALSE);
1765 else
1766 {
1767 /*
1768 * If "always" is FALSE, only adjust topline to a lower value, higher
1769 * value may happen with wrapping lines
1770 */
1771 if (new_topline < curwin->w_topline || always)
1772 curwin->w_topline = new_topline;
1773 if (curwin->w_topline > curwin->w_cursor.lnum)
1774 curwin->w_topline = curwin->w_cursor.lnum;
1775#ifdef FEAT_DIFF
1776 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1777 if (curwin->w_topfill > 0 && extra > off)
1778 {
1779 curwin->w_topfill -= extra - off;
1780 if (curwin->w_topfill < 0)
1781 curwin->w_topfill = 0;
1782 }
1783 check_topfill(curwin, FALSE);
1784#endif
1785 if (curwin->w_topline != old_topline
1786#ifdef FEAT_DIFF
1787 || curwin->w_topfill != old_topfill
1788#endif
1789 )
1790 curwin->w_valid &=
1791 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1792 curwin->w_valid |= VALID_TOPLINE;
1793 }
1794}
1795
1796/*
1797 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1798 * screen lines for text lines.
1799 */
1800 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001801set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803#ifdef FEAT_DIFF
1804 wp->w_filler_rows = 0;
1805#endif
1806 if (used == 0)
1807 wp->w_empty_rows = 0; /* single line that doesn't fit */
1808 else
1809 {
1810 wp->w_empty_rows = wp->w_height - used;
1811#ifdef FEAT_DIFF
1812 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1813 {
1814 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1815 if (wp->w_empty_rows > wp->w_filler_rows)
1816 wp->w_empty_rows -= wp->w_filler_rows;
1817 else
1818 {
1819 wp->w_filler_rows = wp->w_empty_rows;
1820 wp->w_empty_rows = 0;
1821 }
1822 }
1823#endif
1824 }
1825}
1826
1827/*
1828 * Recompute topline to put the cursor at the bottom of the window.
1829 * Scroll at least "min_scroll" lines.
1830 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1831 * This is messy stuff!!!
1832 */
1833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001834scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 int used;
1837 int scrolled = 0;
1838 int extra = 0;
1839 int i;
1840 linenr_T line_count;
1841 linenr_T old_topline = curwin->w_topline;
1842 lineoff_T loff;
1843 lineoff_T boff;
1844#ifdef FEAT_DIFF
1845 int old_topfill = curwin->w_topfill;
1846 int fill_below_window;
1847#endif
1848 linenr_T old_botline = curwin->w_botline;
1849 linenr_T old_valid = curwin->w_valid;
1850 int old_empty_rows = curwin->w_empty_rows;
1851 linenr_T cln; /* Cursor Line Number */
1852
1853 cln = curwin->w_cursor.lnum;
1854 if (set_topbot)
1855 {
1856 used = 0;
1857 curwin->w_botline = cln + 1;
1858#ifdef FEAT_DIFF
1859 loff.fill = 0;
1860#endif
1861 for (curwin->w_topline = curwin->w_botline;
1862 curwin->w_topline > 1;
1863 curwin->w_topline = loff.lnum)
1864 {
1865 loff.lnum = curwin->w_topline;
1866 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001867 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 break;
1869 used += loff.height;
1870#ifdef FEAT_DIFF
1871 curwin->w_topfill = loff.fill;
1872#endif
1873 }
1874 set_empty_rows(curwin, used);
1875 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1876 if (curwin->w_topline != old_topline
1877#ifdef FEAT_DIFF
1878 || curwin->w_topfill != old_topfill
1879#endif
1880 )
1881 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1882 }
1883 else
1884 validate_botline();
1885
1886 /* The lines of the cursor line itself are always used. */
1887#ifdef FEAT_DIFF
1888 used = plines_nofill(cln);
1889#else
1890 validate_cheight();
1891 used = curwin->w_cline_height;
1892#endif
1893
1894 /* If the cursor is below botline, we will at least scroll by the height
1895 * of the cursor line. Correct for empty lines, which are really part of
1896 * botline. */
1897 if (cln >= curwin->w_botline)
1898 {
1899 scrolled = used;
1900 if (cln == curwin->w_botline)
1901 scrolled -= curwin->w_empty_rows;
1902 }
1903
1904 /*
1905 * Stop counting lines to scroll when
1906 * - hitting start of the file
1907 * - scrolled nothing or at least 'sj' lines
1908 * - at least 'so' lines below the cursor
1909 * - lines between botline and cursor have been counted
1910 */
1911#ifdef FEAT_FOLDING
1912 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1913#endif
1914 {
1915 loff.lnum = cln;
1916 boff.lnum = cln;
1917 }
1918#ifdef FEAT_DIFF
1919 loff.fill = 0;
1920 boff.fill = 0;
1921 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1922 - curwin->w_filler_rows;
1923#endif
1924
1925 while (loff.lnum > 1)
1926 {
1927 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1928 * context for 'scrolloff' and counted all lines below the window. */
1929 if ((((scrolled <= 0 || scrolled >= min_scroll)
1930 && extra >= (
1931#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001932 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933#endif
1934 p_so))
1935 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1936 && loff.lnum <= curwin->w_botline
1937#ifdef FEAT_DIFF
1938 && (loff.lnum < curwin->w_botline
1939 || loff.fill >= fill_below_window)
1940#endif
1941 )
1942 break;
1943
1944 /* Add one line above */
1945 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001946 if (loff.height == MAXCOL)
1947 used = MAXCOL;
1948 else
1949 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (used > curwin->w_height)
1951 break;
1952 if (loff.lnum >= curwin->w_botline
1953#ifdef FEAT_DIFF
1954 && (loff.lnum > curwin->w_botline
1955 || loff.fill <= fill_below_window)
1956#endif
1957 )
1958 {
1959 /* Count screen lines that are below the window. */
1960 scrolled += loff.height;
1961 if (loff.lnum == curwin->w_botline
1962#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001963 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964#endif
1965 )
1966 scrolled -= curwin->w_empty_rows;
1967 }
1968
1969 if (boff.lnum < curbuf->b_ml.ml_line_count)
1970 {
1971 /* Add one line below */
1972 botline_forw(&boff);
1973 used += boff.height;
1974 if (used > curwin->w_height)
1975 break;
1976 if (extra < (
1977#ifdef FEAT_MOUSE
1978 mouse_dragging > 0 ? mouse_dragging - 1 :
1979#endif
1980 p_so) || scrolled < min_scroll)
1981 {
1982 extra += boff.height;
1983 if (boff.lnum >= curwin->w_botline
1984#ifdef FEAT_DIFF
1985 || (boff.lnum + 1 == curwin->w_botline
1986 && boff.fill > curwin->w_filler_rows)
1987#endif
1988 )
1989 {
1990 /* Count screen lines that are below the window. */
1991 scrolled += boff.height;
1992 if (boff.lnum == curwin->w_botline
1993#ifdef FEAT_DIFF
1994 && boff.fill == 0
1995#endif
1996 )
1997 scrolled -= curwin->w_empty_rows;
1998 }
1999 }
2000 }
2001 }
2002
2003 /* curwin->w_empty_rows is larger, no need to scroll */
2004 if (scrolled <= 0)
2005 line_count = 0;
2006 /* more than a screenfull, don't scroll but redraw */
2007 else if (used > curwin->w_height)
2008 line_count = used;
2009 /* scroll minimal number of lines */
2010 else
2011 {
2012 line_count = 0;
2013#ifdef FEAT_DIFF
2014 boff.fill = curwin->w_topfill;
2015#endif
2016 boff.lnum = curwin->w_topline - 1;
2017 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2018 {
2019 botline_forw(&boff);
2020 i += boff.height;
2021 ++line_count;
2022 }
2023 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2024 line_count = 9999;
2025 }
2026
2027 /*
2028 * Scroll up if the cursor is off the bottom of the screen a bit.
2029 * Otherwise put it at 1/2 of the screen.
2030 */
2031 if (line_count >= curwin->w_height && line_count > min_scroll)
2032 scroll_cursor_halfway(FALSE);
2033 else
2034 scrollup(line_count, TRUE);
2035
2036 /*
2037 * If topline didn't change we need to restore w_botline and w_empty_rows
2038 * (we changed them).
2039 * If topline did change, update_screen() will set botline.
2040 */
2041 if (curwin->w_topline == old_topline && set_topbot)
2042 {
2043 curwin->w_botline = old_botline;
2044 curwin->w_empty_rows = old_empty_rows;
2045 curwin->w_valid = old_valid;
2046 }
2047 curwin->w_valid |= VALID_TOPLINE;
2048}
2049
2050/*
2051 * Recompute topline to put the cursor halfway the window
2052 * If "atend" is TRUE, also put it halfway at the end of the file.
2053 */
2054 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002055scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int above = 0;
2058 linenr_T topline;
2059#ifdef FEAT_DIFF
2060 int topfill = 0;
2061#endif
2062 int below = 0;
2063 int used;
2064 lineoff_T loff;
2065 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002066#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002067 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2071#ifdef FEAT_FOLDING
2072 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2073#endif
2074#ifdef FEAT_DIFF
2075 used = plines_nofill(loff.lnum);
2076 loff.fill = 0;
2077 boff.fill = 0;
2078#else
2079 used = plines(loff.lnum);
2080#endif
2081 topline = loff.lnum;
2082 while (topline > 1)
2083 {
2084 if (below <= above) /* add a line below the cursor first */
2085 {
2086 if (boff.lnum < curbuf->b_ml.ml_line_count)
2087 {
2088 botline_forw(&boff);
2089 used += boff.height;
2090 if (used > curwin->w_height)
2091 break;
2092 below += boff.height;
2093 }
2094 else
2095 {
2096 ++below; /* count a "~" line */
2097 if (atend)
2098 ++used;
2099 }
2100 }
2101
2102 if (below > above) /* add a line above the cursor */
2103 {
2104 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002105 if (loff.height == MAXCOL)
2106 used = MAXCOL;
2107 else
2108 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (used > curwin->w_height)
2110 break;
2111 above += loff.height;
2112 topline = loff.lnum;
2113#ifdef FEAT_DIFF
2114 topfill = loff.fill;
2115#endif
2116 }
2117 }
2118#ifdef FEAT_FOLDING
2119 if (!hasFolding(topline, &curwin->w_topline, NULL))
2120#endif
2121 curwin->w_topline = topline;
2122#ifdef FEAT_DIFF
2123 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002124 if (old_topline > curwin->w_topline + curwin->w_height)
2125 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 check_topfill(curwin, FALSE);
2127#endif
2128 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2129 curwin->w_valid |= VALID_TOPLINE;
2130}
2131
2132/*
2133 * Correct the cursor position so that it is in a part of the screen at least
2134 * 'so' lines from the top and bottom, if possible.
2135 * If not possible, put it at the same position as scroll_cursor_halfway().
2136 * When called topline must be valid!
2137 */
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 int above = 0; /* screen lines above topline */
2142 linenr_T topline;
2143 int below = 0; /* screen lines below botline */
2144 linenr_T botline;
2145 int above_wanted, below_wanted;
2146 linenr_T cln; /* Cursor Line Number */
2147 int max_off;
2148
2149 /*
2150 * How many lines we would like to have above/below the cursor depends on
2151 * whether the first/last line of the file is on screen.
2152 */
2153 above_wanted = p_so;
2154 below_wanted = p_so;
2155#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002156 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 above_wanted = mouse_dragging - 1;
2159 below_wanted = mouse_dragging - 1;
2160 }
2161#endif
2162 if (curwin->w_topline == 1)
2163 {
2164 above_wanted = 0;
2165 max_off = curwin->w_height / 2;
2166 if (below_wanted > max_off)
2167 below_wanted = max_off;
2168 }
2169 validate_botline();
2170 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2171#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002172 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
2174 )
2175 {
2176 below_wanted = 0;
2177 max_off = (curwin->w_height - 1) / 2;
2178 if (above_wanted > max_off)
2179 above_wanted = max_off;
2180 }
2181
2182 /*
2183 * If there are sufficient file-lines above and below the cursor, we can
2184 * return now.
2185 */
2186 cln = curwin->w_cursor.lnum;
2187 if (cln >= curwin->w_topline + above_wanted
2188 && cln < curwin->w_botline - below_wanted
2189#ifdef FEAT_FOLDING
2190 && !hasAnyFolding(curwin)
2191#endif
2192 )
2193 return;
2194
2195 /*
2196 * Narrow down the area where the cursor can be put by taking lines from
2197 * the top and the bottom until:
2198 * - the desired context lines are found
2199 * - the lines from the top is past the lines from the bottom
2200 */
2201 topline = curwin->w_topline;
2202 botline = curwin->w_botline - 1;
2203#ifdef FEAT_DIFF
2204 /* count filler lines as context */
2205 above = curwin->w_topfill;
2206 below = curwin->w_filler_rows;
2207#endif
2208 while ((above < above_wanted || below < below_wanted) && topline < botline)
2209 {
2210 if (below < below_wanted && (below <= above || above >= above_wanted))
2211 {
2212#ifdef FEAT_FOLDING
2213 if (hasFolding(botline, &botline, NULL))
2214 ++below;
2215 else
2216#endif
2217 below += plines(botline);
2218 --botline;
2219 }
2220 if (above < above_wanted && (above < below || below >= below_wanted))
2221 {
2222#ifdef FEAT_FOLDING
2223 if (hasFolding(topline, NULL, &topline))
2224 ++above;
2225 else
2226#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002227 above += PLINES_NOFILL(topline);
2228#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 /* Count filler lines below this line as context. */
2230 if (topline < botline)
2231 above += diff_check_fill(curwin, topline + 1);
2232#endif
2233 ++topline;
2234 }
2235 }
2236 if (topline == botline || botline == 0)
2237 curwin->w_cursor.lnum = topline;
2238 else if (topline > botline)
2239 curwin->w_cursor.lnum = botline;
2240 else
2241 {
2242 if (cln < topline && curwin->w_topline > 1)
2243 {
2244 curwin->w_cursor.lnum = topline;
2245 curwin->w_valid &=
2246 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2247 }
2248 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2249 {
2250 curwin->w_cursor.lnum = botline;
2251 curwin->w_valid &=
2252 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2253 }
2254 }
2255 curwin->w_valid |= VALID_TOPLINE;
2256}
2257
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002258static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260/*
2261 * move screen 'count' pages up or down and update screen
2262 *
2263 * return FAIL for failure, OK otherwise
2264 */
2265 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 long n;
2269 int retval = OK;
2270 lineoff_T loff;
2271 linenr_T old_topline = curwin->w_topline;
2272
2273 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2274 {
2275 beep_flush();
2276 return FAIL;
2277 }
2278
2279 for ( ; count > 0; --count)
2280 {
2281 validate_botline();
2282 /*
2283 * It's an error to move a page up when the first line is already on
2284 * the screen. It's an error to move a page down when the last line
2285 * is on the screen and the topline is 'scrolloff' lines from the
2286 * last line.
2287 */
2288 if (dir == FORWARD
2289 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2290 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2291 : (curwin->w_topline == 1
2292#ifdef FEAT_DIFF
2293 && curwin->w_topfill ==
2294 diff_check_fill(curwin, curwin->w_topline)
2295#endif
2296 ))
2297 {
2298 beep_flush();
2299 retval = FAIL;
2300 break;
2301 }
2302
2303#ifdef FEAT_DIFF
2304 loff.fill = 0;
2305#endif
2306 if (dir == FORWARD)
2307 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002308 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002310 /* Vi compatible scrolling */
2311 if (p_window <= 2)
2312 ++curwin->w_topline;
2313 else
2314 curwin->w_topline += p_window - 2;
2315 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2316 curwin->w_topline = curbuf->b_ml.ml_line_count;
2317 curwin->w_cursor.lnum = curwin->w_topline;
2318 }
2319 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2320 {
2321 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 curwin->w_topline = curbuf->b_ml.ml_line_count;
2323#ifdef FEAT_DIFF
2324 curwin->w_topfill = 0;
2325#endif
2326 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2327 }
2328 else
2329 {
2330 /* For the overlap, start with the line just below the window
2331 * and go upwards. */
2332 loff.lnum = curwin->w_botline;
2333#ifdef FEAT_DIFF
2334 loff.fill = diff_check_fill(curwin, loff.lnum)
2335 - curwin->w_filler_rows;
2336#endif
2337 get_scroll_overlap(&loff, -1);
2338 curwin->w_topline = loff.lnum;
2339#ifdef FEAT_DIFF
2340 curwin->w_topfill = loff.fill;
2341 check_topfill(curwin, FALSE);
2342#endif
2343 curwin->w_cursor.lnum = curwin->w_topline;
2344 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2345 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2346 }
2347 }
2348 else /* dir == BACKWARDS */
2349 {
2350#ifdef FEAT_DIFF
2351 if (curwin->w_topline == 1)
2352 {
2353 /* Include max number of filler lines */
2354 max_topfill();
2355 continue;
2356 }
2357#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002358 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002359 {
2360 /* Vi compatible scrolling (sort of) */
2361 if (p_window <= 2)
2362 --curwin->w_topline;
2363 else
2364 curwin->w_topline -= p_window - 2;
2365 if (curwin->w_topline < 1)
2366 curwin->w_topline = 1;
2367 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2368 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2369 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2370 continue;
2371 }
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 /* Find the line at the top of the window that is going to be the
2374 * line at the bottom of the window. Make sure this results in
2375 * the same line as before doing CTRL-F. */
2376 loff.lnum = curwin->w_topline - 1;
2377#ifdef FEAT_DIFF
2378 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2379 - curwin->w_topfill;
2380#endif
2381 get_scroll_overlap(&loff, 1);
2382
2383 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2384 {
2385 loff.lnum = curbuf->b_ml.ml_line_count;
2386#ifdef FEAT_DIFF
2387 loff.fill = 0;
2388 }
2389 else
2390 {
2391 botline_topline(&loff);
2392#endif
2393 }
2394 curwin->w_cursor.lnum = loff.lnum;
2395
2396 /* Find the line just above the new topline to get the right line
2397 * at the bottom of the window. */
2398 n = 0;
2399 while (n <= curwin->w_height && loff.lnum >= 1)
2400 {
2401 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002402 if (loff.height == MAXCOL)
2403 n = MAXCOL;
2404 else
2405 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002407 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 curwin->w_topline = 1;
2410#ifdef FEAT_DIFF
2411 max_topfill();
2412#endif
2413 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2414 }
2415 else
2416 {
2417 /* Go two lines forward again. */
2418#ifdef FEAT_DIFF
2419 topline_botline(&loff);
2420#endif
2421 botline_forw(&loff);
2422 botline_forw(&loff);
2423#ifdef FEAT_DIFF
2424 botline_topline(&loff);
2425#endif
2426#ifdef FEAT_FOLDING
2427 /* We're at the wrong end of a fold now. */
2428 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2429#endif
2430
2431 /* Always scroll at least one line. Avoid getting stuck on
2432 * very long lines. */
2433 if (loff.lnum >= curwin->w_topline
2434#ifdef FEAT_DIFF
2435 && (loff.lnum > curwin->w_topline
2436 || loff.fill >= curwin->w_topfill)
2437#endif
2438 )
2439 {
2440#ifdef FEAT_DIFF
2441 /* First try using the maximum number of filler lines. If
2442 * that's not enough, backup one line. */
2443 loff.fill = curwin->w_topfill;
2444 if (curwin->w_topfill < diff_check_fill(curwin,
2445 curwin->w_topline))
2446 max_topfill();
2447 if (curwin->w_topfill == loff.fill)
2448#endif
2449 {
2450 --curwin->w_topline;
2451#ifdef FEAT_DIFF
2452 curwin->w_topfill = 0;
2453#endif
2454 }
2455 comp_botline(curwin);
2456 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002457 curwin->w_valid &=
2458 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460 else
2461 {
2462 curwin->w_topline = loff.lnum;
2463#ifdef FEAT_DIFF
2464 curwin->w_topfill = loff.fill;
2465 check_topfill(curwin, FALSE);
2466#endif
2467 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2468 }
2469 }
2470 }
2471 }
2472#ifdef FEAT_FOLDING
2473 foldAdjustCursor();
2474#endif
2475 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002476 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002477 if (retval == OK)
2478 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2480
Bram Moolenaar907dad72018-07-10 15:07:15 +02002481 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002483 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2484 // But make sure we scroll at least one line (happens with mix of long
2485 // wrapping lines and non-wrapping line).
2486 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002488 scroll_cursor_top(1, FALSE);
2489 if (curwin->w_topline <= old_topline
2490 && old_topline < curbuf->b_ml.ml_line_count)
2491 {
2492 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002494 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2495#endif
2496 }
2497 }
2498#ifdef FEAT_FOLDING
2499 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503
2504 redraw_later(VALID);
2505 return retval;
2506}
2507
2508/*
2509 * Decide how much overlap to use for page-up or page-down scrolling.
2510 * This is symmetric, so that doing both keeps the same lines displayed.
2511 * Three lines are examined:
2512 *
2513 * before CTRL-F after CTRL-F / before CTRL-B
2514 * etc. l1
2515 * l1 last but one line ------------
2516 * l2 last text line l2 top text line
2517 * ------------- l3 second text line
2518 * l3 etc.
2519 */
2520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 int h1, h2, h3, h4;
2524 int min_height = curwin->w_height - 2;
2525 lineoff_T loff0, loff1, loff2;
2526
2527#ifdef FEAT_DIFF
2528 if (lp->fill > 0)
2529 lp->height = 1;
2530 else
2531 lp->height = plines_nofill(lp->lnum);
2532#else
2533 lp->height = plines(lp->lnum);
2534#endif
2535 h1 = lp->height;
2536 if (h1 > min_height)
2537 return; /* no overlap */
2538
2539 loff0 = *lp;
2540 if (dir > 0)
2541 botline_forw(lp);
2542 else
2543 topline_back(lp);
2544 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002545 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 *lp = loff0; /* no overlap */
2548 return;
2549 }
2550
2551 loff1 = *lp;
2552 if (dir > 0)
2553 botline_forw(lp);
2554 else
2555 topline_back(lp);
2556 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002557 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 *lp = loff0; /* no overlap */
2560 return;
2561 }
2562
2563 loff2 = *lp;
2564 if (dir > 0)
2565 botline_forw(lp);
2566 else
2567 topline_back(lp);
2568 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002569 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 *lp = loff1; /* 1 line overlap */
2571 else
2572 *lp = loff2; /* 2 lines overlap */
2573 return;
2574}
2575
2576/* #define KEEP_SCREEN_LINE */
2577/*
2578 * Scroll 'scroll' lines up or down.
2579 */
2580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002581halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 long scrolled = 0;
2584 int i;
2585 int n;
2586 int room;
2587
2588 if (Prenum)
2589 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2590 curwin->w_height : Prenum;
2591 n = (curwin->w_p_scr <= curwin->w_height) ?
2592 curwin->w_p_scr : curwin->w_height;
2593
Bram Moolenaard5d37532017-03-27 23:02:07 +02002594 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 validate_botline();
2596 room = curwin->w_empty_rows;
2597#ifdef FEAT_DIFF
2598 room += curwin->w_filler_rows;
2599#endif
2600 if (flag)
2601 {
2602 /*
2603 * scroll the text up
2604 */
2605 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2606 {
2607#ifdef FEAT_DIFF
2608 if (curwin->w_topfill > 0)
2609 {
2610 i = 1;
2611 if (--n < 0 && scrolled > 0)
2612 break;
2613 --curwin->w_topfill;
2614 }
2615 else
2616#endif
2617 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002618 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 n -= i;
2620 if (n < 0 && scrolled > 0)
2621 break;
2622#ifdef FEAT_FOLDING
2623 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2624#endif
2625 ++curwin->w_topline;
2626#ifdef FEAT_DIFF
2627 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2628#endif
2629
2630#ifndef KEEP_SCREEN_LINE
2631 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2632 {
2633 ++curwin->w_cursor.lnum;
2634 curwin->w_valid &=
2635 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2636 }
2637#endif
2638 }
2639 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2640 scrolled += i;
2641
2642 /*
2643 * Correct w_botline for changed w_topline.
2644 * Won't work when there are filler lines.
2645 */
2646#ifdef FEAT_DIFF
2647 if (curwin->w_p_diff)
2648 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2649 else
2650#endif
2651 {
2652 room += i;
2653 do
2654 {
2655 i = plines(curwin->w_botline);
2656 if (i > room)
2657 break;
2658#ifdef FEAT_FOLDING
2659 (void)hasFolding(curwin->w_botline, NULL,
2660 &curwin->w_botline);
2661#endif
2662 ++curwin->w_botline;
2663 room -= i;
2664 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2665 }
2666 }
2667
2668#ifndef KEEP_SCREEN_LINE
2669 /*
2670 * When hit bottom of the file: move cursor down.
2671 */
2672 if (n > 0)
2673 {
2674# ifdef FEAT_FOLDING
2675 if (hasAnyFolding(curwin))
2676 {
2677 while (--n >= 0
2678 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2679 {
2680 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2681 &curwin->w_cursor.lnum);
2682 ++curwin->w_cursor.lnum;
2683 }
2684 }
2685 else
2686# endif
2687 curwin->w_cursor.lnum += n;
2688 check_cursor_lnum();
2689 }
2690#else
2691 /* try to put the cursor in the same screen line */
2692 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2693 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2694 {
2695 scrolled -= plines(curwin->w_cursor.lnum);
2696 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2697 break;
2698# ifdef FEAT_FOLDING
2699 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2700 &curwin->w_cursor.lnum);
2701# endif
2702 ++curwin->w_cursor.lnum;
2703 }
2704#endif
2705 }
2706 else
2707 {
2708 /*
2709 * scroll the text down
2710 */
2711 while (n > 0 && curwin->w_topline > 1)
2712 {
2713#ifdef FEAT_DIFF
2714 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2715 {
2716 i = 1;
2717 if (--n < 0 && scrolled > 0)
2718 break;
2719 ++curwin->w_topfill;
2720 }
2721 else
2722#endif
2723 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002724 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 n -= i;
2726 if (n < 0 && scrolled > 0)
2727 break;
2728 --curwin->w_topline;
2729#ifdef FEAT_FOLDING
2730 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2731#endif
2732#ifdef FEAT_DIFF
2733 curwin->w_topfill = 0;
2734#endif
2735 }
2736 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2737 VALID_BOTLINE|VALID_BOTLINE_AP);
2738 scrolled += i;
2739#ifndef KEEP_SCREEN_LINE
2740 if (curwin->w_cursor.lnum > 1)
2741 {
2742 --curwin->w_cursor.lnum;
2743 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2744 }
2745#endif
2746 }
2747#ifndef KEEP_SCREEN_LINE
2748 /*
2749 * When hit top of the file: move cursor up.
2750 */
2751 if (n > 0)
2752 {
2753 if (curwin->w_cursor.lnum <= (linenr_T)n)
2754 curwin->w_cursor.lnum = 1;
2755 else
2756# ifdef FEAT_FOLDING
2757 if (hasAnyFolding(curwin))
2758 {
2759 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2760 {
2761 --curwin->w_cursor.lnum;
2762 (void)hasFolding(curwin->w_cursor.lnum,
2763 &curwin->w_cursor.lnum, NULL);
2764 }
2765 }
2766 else
2767# endif
2768 curwin->w_cursor.lnum -= n;
2769 }
2770#else
2771 /* try to put the cursor in the same screen line */
2772 scrolled += n; /* move cursor when topline is 1 */
2773 while (curwin->w_cursor.lnum > curwin->w_topline
2774 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2775 {
2776 scrolled -= plines(curwin->w_cursor.lnum - 1);
2777 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2778 break;
2779 --curwin->w_cursor.lnum;
2780# ifdef FEAT_FOLDING
2781 foldAdjustCursor();
2782# endif
2783 }
2784#endif
2785 }
2786# ifdef FEAT_FOLDING
2787 /* Move cursor to first line of closed fold. */
2788 foldAdjustCursor();
2789# endif
2790#ifdef FEAT_DIFF
2791 check_topfill(curwin, !flag);
2792#endif
2793 cursor_correct();
2794 beginline(BL_SOL | BL_FIX);
2795 redraw_later(VALID);
2796}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002797
Bram Moolenaar860cae12010-06-05 23:22:07 +02002798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002799do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002800{
2801 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002802 colnr_T col = curwin->w_cursor.col;
2803# ifdef FEAT_VIRTUALEDIT
2804 colnr_T coladd = curwin->w_cursor.coladd;
2805# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002806 colnr_T curswant = curwin->w_curswant;
2807 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002808 win_T *old_curwin = curwin;
2809 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002810 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002811 int old_VIsual_select = VIsual_select;
2812 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002813
2814 /*
2815 * loop through the cursorbound windows
2816 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002818 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819 {
2820 curbuf = curwin->w_buffer;
2821 /* skip original window and windows with 'noscrollbind' */
2822 if (curwin != old_curwin && curwin->w_p_crb)
2823 {
2824# ifdef FEAT_DIFF
2825 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002826 curwin->w_cursor.lnum =
2827 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 else
2829# endif
2830 curwin->w_cursor.lnum = line;
2831 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002832# ifdef FEAT_VIRTUALEDIT
2833 curwin->w_cursor.coladd = coladd;
2834# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002835 curwin->w_curswant = curswant;
2836 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837
Bram Moolenaar61452852011-02-01 18:01:11 +01002838 /* Make sure the cursor is in a valid position. Temporarily set
2839 * "restart_edit" to allow the cursor to be beyond the EOL. */
2840 restart_edit_save = restart_edit;
2841 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002842 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002843# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002844 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002845 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002846# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002847 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002848 /* Correct cursor for multi-byte character. */
2849 if (has_mbyte)
2850 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002851 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002852
2853 /* Only scroll when 'scrollbind' hasn't done this. */
2854 if (!curwin->w_p_scb)
2855 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002857 }
2858 }
2859
2860 /*
2861 * reset current-window
2862 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002863 VIsual_select = old_VIsual_select;
2864 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002865 curwin = old_curwin;
2866 curbuf = old_curbuf;
2867}