blob: 14b7a6e7dc5c66bf3ea0bb0f070cce6ef6a69ea6 [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 int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
28 linenr_T lnum; /* line number */
29#ifdef FEAT_DIFF
30 int fill; /* filler lines */
31#endif
32 int height; /* height of added line */
33} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
111 /* wp->w_botline is the line that is just below the window */
112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100139#ifdef FEAT_INS_EXPAND
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100140 && !pum_visible()
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100141#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100142 )
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200144 if (wp->w_p_rnu)
145 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200146 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200147#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 if (wp->w_p_cul)
149 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200150 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200151 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200152 // "w_last_cursorline" may be outdated, worst case we redraw
153 // too much. This is optimized for moving the cursor around in
154 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100155 redrawWinline(wp, wp->w_last_cursorline);
156 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
158 else
159 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200160 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200161#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200162 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100163}
164
165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 * Update curwin->w_topline and redraw if necessary.
167 * Used to update the screen before printing a message.
168 */
169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100170update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 update_topline();
173 if (must_redraw)
174 update_screen(0);
175}
176
177/*
178 * Update curwin->w_topline to move the cursor onto the screen.
179 */
180 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100181update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 long line_count;
184 int halfheight;
185 int n;
186 linenr_T old_topline;
187#ifdef FEAT_DIFF
188 int old_topfill;
189#endif
190#ifdef FEAT_FOLDING
191 linenr_T lnum;
192#endif
193 int check_topline = FALSE;
194 int check_botline = FALSE;
195#ifdef FEAT_MOUSE
196 int save_so = p_so;
197#endif
198
Bram Moolenaard5d37532017-03-27 23:02:07 +0200199 /* If there is no valid screen and when the window height is zero just use
200 * the cursor line. */
201 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200202 {
203 curwin->w_topline = curwin->w_cursor.lnum;
204 curwin->w_botline = curwin->w_topline;
205 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200206 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200207 return;
208 }
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 check_cursor_moved(curwin);
211 if (curwin->w_valid & VALID_TOPLINE)
212 return;
213
214#ifdef FEAT_MOUSE
215 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000216 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 p_so = mouse_dragging - 1;
218#endif
219
220 old_topline = curwin->w_topline;
221#ifdef FEAT_DIFF
222 old_topfill = curwin->w_topfill;
223#endif
224
225 /*
226 * If the buffer is empty, always set topline to 1.
227 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100228 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {
230 if (curwin->w_topline != 1)
231 redraw_later(NOT_VALID);
232 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 curwin->w_botline = 2;
234 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237
238 /*
239 * If the cursor is above or near the top of the window, scroll the window
240 * to show the line the cursor is in, with 'scrolloff' context.
241 */
242 else
243 {
244 if (curwin->w_topline > 1)
245 {
246 /* If the cursor is above topline, scrolling is always needed.
247 * If the cursor is far below topline and there is no folding,
248 * scrolling down is never needed. */
249 if (curwin->w_cursor.lnum < curwin->w_topline)
250 check_topline = TRUE;
251 else if (check_top_offset())
252 check_topline = TRUE;
253 }
254#ifdef FEAT_DIFF
255 /* Check if there are more filler lines than allowed. */
256 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
257 curwin->w_topline))
258 check_topline = TRUE;
259#endif
260
261 if (check_topline)
262 {
263 halfheight = curwin->w_height / 2 - 1;
264 if (halfheight < 2)
265 halfheight = 2;
266
267#ifdef FEAT_FOLDING
268 if (hasAnyFolding(curwin))
269 {
270 /* Count the number of logical lines between the cursor and
271 * topline + p_so (approximation of how much will be
272 * scrolled). */
273 n = 0;
274 for (lnum = curwin->w_cursor.lnum;
275 lnum < curwin->w_topline + p_so; ++lnum)
276 {
277 ++n;
278 /* stop at end of file or when we know we are far off */
279 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
280 break;
281 (void)hasFolding(lnum, NULL, &lnum);
282 }
283 }
284 else
285#endif
286 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
287
288 /* If we weren't very close to begin with, we scroll to put the
289 * cursor in the middle of the window. Otherwise put the cursor
290 * near the top of the window. */
291 if (n >= halfheight)
292 scroll_cursor_halfway(FALSE);
293 else
294 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000295 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 check_botline = TRUE;
297 }
298 }
299
300 else
301 {
302#ifdef FEAT_FOLDING
303 /* Make sure topline is the first line of a fold. */
304 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
305#endif
306 check_botline = TRUE;
307 }
308 }
309
310 /*
311 * If the cursor is below the bottom of the window, scroll the window
312 * to put the cursor on the window.
313 * When w_botline is invalid, recompute it first, to avoid a redraw later.
314 * If w_botline was approximated, we might need a redraw later in a few
315 * cases, but we don't want to spend (a lot of) time recomputing w_botline
316 * for every small change.
317 */
318 if (check_botline)
319 {
320 if (!(curwin->w_valid & VALID_BOTLINE_AP))
321 validate_botline();
322
323 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
324 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000325 if (curwin->w_cursor.lnum < curwin->w_botline)
326 {
327 if (((long)curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 >= (long)curwin->w_botline - p_so
329#ifdef FEAT_FOLDING
330 || hasAnyFolding(curwin)
331#endif
332 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 lineoff_T loff;
335
Bram Moolenaard4153d42008-11-15 15:06:17 +0000336 /* Cursor is (a few lines) above botline, check if there are
337 * 'scrolloff' window lines below the cursor. If not, need to
338 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 n = curwin->w_empty_rows;
340 loff.lnum = curwin->w_cursor.lnum;
341#ifdef FEAT_FOLDING
342 /* In a fold go to its last line. */
343 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
344#endif
345#ifdef FEAT_DIFF
346 loff.fill = 0;
347 n += curwin->w_filler_rows;
348#endif
349 loff.height = 0;
350 while (loff.lnum < curwin->w_botline
351#ifdef FEAT_DIFF
352 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
353#endif
354 )
355 {
356 n += loff.height;
357 if (n >= p_so)
358 break;
359 botline_forw(&loff);
360 }
361 if (n >= p_so)
362 /* sufficient context, no need to scroll */
363 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000364 }
365 else
366 /* sufficient context, no need to scroll */
367 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
369 if (check_botline)
370 {
371#ifdef FEAT_FOLDING
372 if (hasAnyFolding(curwin))
373 {
374 /* Count the number of logical lines between the cursor and
375 * botline - p_so (approximation of how much will be
376 * scrolled). */
377 line_count = 0;
378 for (lnum = curwin->w_cursor.lnum;
379 lnum >= curwin->w_botline - p_so; --lnum)
380 {
381 ++line_count;
382 /* stop at end of file or when we know we are far off */
383 if (lnum <= 0 || line_count > curwin->w_height + 1)
384 break;
385 (void)hasFolding(lnum, &lnum, NULL);
386 }
387 }
388 else
389#endif
390 line_count = curwin->w_cursor.lnum - curwin->w_botline
391 + 1 + p_so;
392 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 else
395 scroll_cursor_halfway(FALSE);
396 }
397 }
398 }
399 curwin->w_valid |= VALID_TOPLINE;
400
401 /*
402 * Need to redraw when topline changed.
403 */
404 if (curwin->w_topline != old_topline
405#ifdef FEAT_DIFF
406 || curwin->w_topfill != old_topfill
407#endif
408 )
409 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100410 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000411 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 curwin->w_skipcol = 0;
414 redraw_later(NOT_VALID);
415 }
416 else
417 redraw_later(VALID);
418 /* May need to set w_skipcol when cursor in w_topline. */
419 if (curwin->w_cursor.lnum == curwin->w_topline)
420 validate_cursor();
421 }
422
423#ifdef FEAT_MOUSE
424 p_so = save_so;
425#endif
426}
427
428/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000429 * Return the scrolljump value to use for the current window.
430 * When 'scrolljump' is positive use it as-is.
431 * When 'scrolljump' is negative use it as a percentage of the window height.
432 */
433 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100434scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000435{
436 if (p_sj >= 0)
437 return (int)p_sj;
438 return (curwin->w_height * -p_sj) / 100;
439}
440
441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
443 * current window.
444 */
445 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100446check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
448 lineoff_T loff;
449 int n;
450
451 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
452#ifdef FEAT_FOLDING
453 || hasAnyFolding(curwin)
454#endif
455 )
456 {
457 loff.lnum = curwin->w_cursor.lnum;
458#ifdef FEAT_DIFF
459 loff.fill = 0;
460 n = curwin->w_topfill; /* always have this context */
461#else
462 n = 0;
463#endif
464 /* Count the visible screen lines above the cursor line. */
465 while (n < p_so)
466 {
467 topline_back(&loff);
468 /* Stop when included a line above the window. */
469 if (loff.lnum < curwin->w_topline
470#ifdef FEAT_DIFF
471 || (loff.lnum == curwin->w_topline && loff.fill > 0)
472#endif
473 )
474 break;
475 n += loff.height;
476 }
477 if (n < p_so)
478 return TRUE;
479 }
480 return FALSE;
481}
482
483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
486 if (curwin->w_set_curswant)
487 {
488 validate_virtcol();
489 curwin->w_curswant = curwin->w_virtcol;
490 curwin->w_set_curswant = FALSE;
491 }
492}
493
494/*
495 * Check if the cursor has moved. Set the w_valid flag accordingly.
496 */
497 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100498check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
501 {
502 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
503 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
504 wp->w_valid_cursor = wp->w_cursor;
505 wp->w_valid_leftcol = wp->w_leftcol;
506 }
507 else if (wp->w_cursor.col != wp->w_valid_cursor.col
508 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100509 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 {
511 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
512 wp->w_valid_cursor.col = wp->w_cursor.col;
513 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 }
516}
517
518/*
519 * Call this function when some window settings have changed, which require
520 * the cursor position, botline and topline to be recomputed and the window to
521 * be redrawn. E.g, when changing the 'wrap' option or folding.
522 */
523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100524changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 changed_window_setting_win(curwin);
527}
528
529 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100530changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 wp->w_lines_valid = 0;
533 changed_line_abv_curs_win(wp);
534 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
535 redraw_win_later(wp, NOT_VALID);
536}
537
538/*
539 * Set wp->w_topline to a certain number.
540 */
541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544#ifdef FEAT_FOLDING
545 /* go to first of folded lines */
546 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
547#endif
548 /* Approximate the value of w_botline */
549 wp->w_botline += lnum - wp->w_topline;
550 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000551 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552#ifdef FEAT_DIFF
553 wp->w_topfill = 0;
554#endif
555 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
556 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
557 redraw_later(VALID);
558}
559
560/*
561 * Call this function when the length of the cursor line (in screen
562 * characters) has changed, and the change is before the cursor.
563 * Need to take care of w_botline separately!
564 */
565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
569 |VALID_CHEIGHT|VALID_TOPLINE);
570}
571
572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100573changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
576 |VALID_CHEIGHT|VALID_TOPLINE);
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579/*
580 * Call this function when the length of a line (in screen characters) above
581 * the cursor have changed.
582 * Need to take care of w_botline separately!
583 */
584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100585changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
588 |VALID_CHEIGHT|VALID_TOPLINE);
589}
590
591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100592changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
595 |VALID_CHEIGHT|VALID_TOPLINE);
596}
597
598/*
599 * Make sure the value of curwin->w_botline is valid.
600 */
601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100602validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604 if (!(curwin->w_valid & VALID_BOTLINE))
605 comp_botline(curwin);
606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * Mark curwin->w_botline as invalid (because of some change in the buffer).
610 */
611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100612invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
614 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
615}
616
617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100624approximate_botline_win(
625 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 wp->w_valid &= ~VALID_BOTLINE;
628}
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630/*
631 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
632 */
633 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100634cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 check_cursor_moved(curwin);
637 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
638 (VALID_WROW|VALID_WCOL));
639}
640
641/*
642 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
643 * w_topline must be valid, you may need to call update_topline() first!
644 */
645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 check_cursor_moved(curwin);
649 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
650 curs_columns(TRUE);
651}
652
653#if defined(FEAT_GUI) || defined(PROTO)
654/*
655 * validate w_cline_row.
656 */
657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100658validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 /*
661 * First make sure that w_topline is valid (after moving the cursor).
662 */
663 update_topline();
664 check_cursor_moved(curwin);
665 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100666 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667}
668#endif
669
670/*
671 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200672 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 */
674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100675curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 linenr_T lnum;
678 int i;
679 int all_invalid;
680 int valid;
681#ifdef FEAT_FOLDING
682 long fold_count;
683#endif
684
685 /* Check if wp->w_lines[].wl_size is invalid */
686 all_invalid = (!redrawing()
687 || wp->w_lines_valid == 0
688 || wp->w_lines[0].wl_lnum > wp->w_topline);
689 i = 0;
690 wp->w_cline_row = 0;
691 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
692 {
693 valid = FALSE;
694 if (!all_invalid && i < wp->w_lines_valid)
695 {
696 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
697 continue; /* skip changed or deleted lines */
698 if (wp->w_lines[i].wl_lnum == lnum)
699 {
700#ifdef FEAT_FOLDING
701 /* Check for newly inserted lines below this row, in which
702 * case we need to check for folded lines. */
703 if (!wp->w_buffer->b_mod_set
704 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
705 || wp->w_buffer->b_mod_top
706 > wp->w_lines[i].wl_lastlnum + 1)
707#endif
708 valid = TRUE;
709 }
710 else if (wp->w_lines[i].wl_lnum > lnum)
711 --i; /* hold at inserted lines */
712 }
713 if (valid
714#ifdef FEAT_DIFF
715 && (lnum != wp->w_topline || !wp->w_p_diff)
716#endif
717 )
718 {
719#ifdef FEAT_FOLDING
720 lnum = wp->w_lines[i].wl_lastlnum + 1;
721 /* Cursor inside folded lines, don't count this row */
722 if (lnum > wp->w_cursor.lnum)
723 break;
724#else
725 ++lnum;
726#endif
727 wp->w_cline_row += wp->w_lines[i].wl_size;
728 }
729 else
730 {
731#ifdef FEAT_FOLDING
732 fold_count = foldedCount(wp, lnum, NULL);
733 if (fold_count)
734 {
735 lnum += fold_count;
736 if (lnum > wp->w_cursor.lnum)
737 break;
738 ++wp->w_cline_row;
739 }
740 else
741#endif
742#ifdef FEAT_DIFF
743 if (lnum == wp->w_topline)
744 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
745 + wp->w_topfill;
746 else
747#endif
748 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
749 }
750 }
751
752 check_cursor_moved(wp);
753 if (!(wp->w_valid & VALID_CHEIGHT))
754 {
755 if (all_invalid
756 || i == wp->w_lines_valid
757 || (i < wp->w_lines_valid
758 && (!wp->w_lines[i].wl_valid
759 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
760 {
761#ifdef FEAT_DIFF
762 if (wp->w_cursor.lnum == wp->w_topline)
763 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
764 TRUE) + wp->w_topfill;
765 else
766#endif
767 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
768#ifdef FEAT_FOLDING
769 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
770 NULL, NULL, TRUE, NULL);
771#endif
772 }
773 else if (i > wp->w_lines_valid)
774 {
775 /* a line that is too long to fit on the last screen line */
776 wp->w_cline_height = 0;
777#ifdef FEAT_FOLDING
778 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
779 NULL, NULL, TRUE, NULL);
780#endif
781 }
782 else
783 {
784 wp->w_cline_height = wp->w_lines[i].wl_size;
785#ifdef FEAT_FOLDING
786 wp->w_cline_folded = wp->w_lines[i].wl_folded;
787#endif
788 }
789 }
790
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100791 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
796/*
797 * Validate curwin->w_virtcol only.
798 */
799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100800validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 validate_virtcol_win(curwin);
803}
804
805/*
806 * Validate wp->w_virtcol only.
807 */
808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 check_cursor_moved(wp);
812 if (!(wp->w_valid & VALID_VIRTCOL))
813 {
814 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
815 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000816#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000817 if (wp->w_p_cuc
818# ifdef FEAT_INS_EXPAND
819 && !pum_visible()
820# endif
821 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000822 redraw_win_later(wp, SOME_VALID);
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825}
826
827/*
828 * Validate curwin->w_cline_height only.
829 */
830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100831validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
833 check_cursor_moved(curwin);
834 if (!(curwin->w_valid & VALID_CHEIGHT))
835 {
836#ifdef FEAT_DIFF
837 if (curwin->w_cursor.lnum == curwin->w_topline)
838 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
839 + curwin->w_topfill;
840 else
841#endif
842 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
843#ifdef FEAT_FOLDING
844 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
845#endif
846 curwin->w_valid |= VALID_CHEIGHT;
847 }
848}
849
850/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000851 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 colnr_T off;
857 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100858 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 validate_virtcol();
861 if (!(curwin->w_valid & VALID_WCOL))
862 {
863 col = curwin->w_virtcol;
864 off = curwin_col_off();
865 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200866 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000869 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200870 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100871 && width > 0)
872 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200873 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000874 if (col > (int)curwin->w_leftcol)
875 col -= curwin->w_leftcol;
876 else
877 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 curwin->w_valid |= VALID_WCOL;
881 }
882}
883
884/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200885 * Compute offset of a window, occupied by absolute or relative line number,
886 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 */
888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100889win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
Bram Moolenaar64486672010-05-16 15:46:46 +0200891 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#ifdef FEAT_CMDWIN
893 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
894#endif
895#ifdef FEAT_FOLDING
896 + wp->w_p_fdc
897#endif
898#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200899 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901 );
902}
903
904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100905curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 return win_col_off(curwin);
908}
909
910/*
911 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200912 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
913 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 */
915 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100916win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
Bram Moolenaar64486672010-05-16 15:46:46 +0200918 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000919 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 return 0;
921}
922
923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100924curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 return win_col_off2(curwin);
927}
928
929/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100930 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * Also updates curwin->w_wrow and curwin->w_cline_row.
932 * Also updates curwin->w_leftcol.
933 */
934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100935curs_columns(
936 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 int diff;
939 int extra; /* offset for first screen line */
940 int off_left, off_right;
941 int n;
942 int p_lines;
943 int width = 0;
944 int textwidth;
945 int new_leftcol;
946 colnr_T startcol;
947 colnr_T endcol;
948 colnr_T prev_skipcol;
949
950 /*
951 * First make sure that w_topline is valid (after moving the cursor).
952 */
953 update_topline();
954
955 /*
956 * Next make sure that w_cline_row is valid.
957 */
958 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100959 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * Compute the number of virtual columns.
963 */
964#ifdef FEAT_FOLDING
965 if (curwin->w_cline_folded)
966 /* In a folded line the cursor is always in the first column */
967 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
968 else
969#endif
970 getvvcol(curwin, &curwin->w_cursor,
971 &startcol, &(curwin->w_virtcol), &endcol);
972
973 /* remove '$' from change command when cursor moves onto it */
974 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100975 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 extra = curwin_col_off();
978 curwin->w_wcol = curwin->w_virtcol + extra;
979 endcol += extra;
980
981 /*
982 * Now compute w_wrow, counting screen lines from w_cline_row.
983 */
984 curwin->w_wrow = curwin->w_cline_row;
985
Bram Moolenaar02631462017-09-22 15:20:32 +0200986 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (textwidth <= 0)
988 {
989 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200990 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 curwin->w_wrow = curwin->w_height - 1;
992 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200993 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
995 width = textwidth + curwin_col_off2();
996
997 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +0200998 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001000 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +02001001 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 curwin->w_wcol -= n * width;
1003 curwin->w_wrow += n;
1004
1005#ifdef FEAT_LINEBREAK
1006 /* When cursor wraps to first char of next line in Insert
1007 * mode, the 'showbreak' string isn't shown, backup to first
1008 * column */
1009 if (*p_sbr && *ml_get_cursor() == NUL
1010 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1011 curwin->w_wcol = 0;
1012#endif
1013 }
1014 }
1015
1016 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1017 * is not folded.
1018 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001019 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_FOLDING
1021 && !curwin->w_cline_folded
1022#endif
1023 )
1024 {
1025 /*
1026 * If Cursor is left of the screen, scroll rightwards.
1027 * If Cursor is right of the screen, scroll leftwards
1028 * If we get closer to the edge than 'sidescrolloff', scroll a little
1029 * extra
1030 */
1031 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001032 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 - p_siso) + 1;
1034 if (off_left < 0 || off_right > 0)
1035 {
1036 if (off_left < 0)
1037 diff = -off_left;
1038 else
1039 diff = off_right;
1040
1041 /* When far off or not enough room on either side, put cursor in
1042 * middle of window. */
1043 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1044 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1045 else
1046 {
1047 if (diff < p_ss)
1048 diff = p_ss;
1049 if (off_left < 0)
1050 new_leftcol = curwin->w_leftcol - diff;
1051 else
1052 new_leftcol = curwin->w_leftcol + diff;
1053 }
1054 if (new_leftcol < 0)
1055 new_leftcol = 0;
1056 if (new_leftcol != (int)curwin->w_leftcol)
1057 {
1058 curwin->w_leftcol = new_leftcol;
1059 /* screen has to be redrawn with new curwin->w_leftcol */
1060 redraw_later(NOT_VALID);
1061 }
1062 }
1063 curwin->w_wcol -= curwin->w_leftcol;
1064 }
1065 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1066 curwin->w_wcol -= curwin->w_leftcol;
1067 else
1068 curwin->w_wcol = 0;
1069
1070#ifdef FEAT_DIFF
1071 /* Skip over filler lines. At the top use w_topfill, there
1072 * may be some filler lines above the window. */
1073 if (curwin->w_cursor.lnum == curwin->w_topline)
1074 curwin->w_wrow += curwin->w_topfill;
1075 else
1076 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1077#endif
1078
1079 prev_skipcol = curwin->w_skipcol;
1080
1081 p_lines = 0;
1082 if ((curwin->w_wrow >= curwin->w_height
1083 || ((prev_skipcol > 0
1084 || curwin->w_wrow + p_so >= curwin->w_height)
1085 && (p_lines =
1086#ifdef FEAT_DIFF
1087 plines_win_nofill
1088#else
1089 plines_win
1090#endif
1091 (curwin, curwin->w_cursor.lnum, FALSE))
1092 - 1 >= curwin->w_height))
1093 && curwin->w_height != 0
1094 && curwin->w_cursor.lnum == curwin->w_topline
1095 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001096 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
1098 /* Cursor past end of screen. Happens with a single line that does
1099 * not fit on screen. Find a skipcol to show the text around the
1100 * cursor. Avoid scrolling all the time. compute value of "extra":
1101 * 1: Less than "p_so" lines above
1102 * 2: Less than "p_so" lines below
1103 * 3: both of them */
1104 extra = 0;
1105 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1106 extra = 1;
1107 /* Compute last display line of the buffer line that we want at the
1108 * bottom of the window. */
1109 if (p_lines == 0)
1110 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1111 --p_lines;
1112 if (p_lines > curwin->w_wrow + p_so)
1113 n = curwin->w_wrow + p_so;
1114 else
1115 n = p_lines;
1116 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1117 extra += 2;
1118
1119 if (extra == 3 || p_lines < p_so * 2)
1120 {
1121 /* not enough room for 'scrolloff', put cursor in the middle */
1122 n = curwin->w_virtcol / width;
1123 if (n > curwin->w_height / 2)
1124 n -= curwin->w_height / 2;
1125 else
1126 n = 0;
1127 /* don't skip more than necessary */
1128 if (n > p_lines - curwin->w_height + 1)
1129 n = p_lines - curwin->w_height + 1;
1130 curwin->w_skipcol = n * width;
1131 }
1132 else if (extra == 1)
1133 {
1134 /* less then 'scrolloff' lines above, decrease skipcol */
1135 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1136 + width - 1) / width;
1137 if (extra > 0)
1138 {
1139 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1140 extra = curwin->w_skipcol / width;
1141 curwin->w_skipcol -= extra * width;
1142 }
1143 }
1144 else if (extra == 2)
1145 {
1146 /* less then 'scrolloff' lines below, increase skipcol */
1147 endcol = (n - curwin->w_height + 1) * width;
1148 while (endcol > curwin->w_virtcol)
1149 endcol -= width;
1150 if (endcol > curwin->w_skipcol)
1151 curwin->w_skipcol = endcol;
1152 }
1153
1154 curwin->w_wrow -= curwin->w_skipcol / width;
1155 if (curwin->w_wrow >= curwin->w_height)
1156 {
1157 /* small window, make sure cursor is in it */
1158 extra = curwin->w_wrow - curwin->w_height + 1;
1159 curwin->w_skipcol += extra * width;
1160 curwin->w_wrow -= extra;
1161 }
1162
1163 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1164 if (extra > 0)
1165 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1166 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001167 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 else
1170 curwin->w_skipcol = 0;
1171 if (prev_skipcol != curwin->w_skipcol)
1172 redraw_later(NOT_VALID);
1173
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001174#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001175 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1176 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001177# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001178 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001179# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001180 )
1181 redraw_later(SOME_VALID);
1182#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1185}
1186
1187/*
1188 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001191scrolldown(
1192 long line_count,
1193 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 long done = 0; /* total # of physical lines done */
1196 int wrow;
1197 int moved = FALSE;
1198
1199#ifdef FEAT_FOLDING
1200 linenr_T first;
1201
1202 /* Make sure w_topline is at the first of a sequence of folded lines. */
1203 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1204#endif
1205 validate_cursor(); /* w_wrow needs to be valid */
1206 while (line_count-- > 0)
1207 {
1208#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001209 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1210 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 ++curwin->w_topfill;
1213 ++done;
1214 }
1215 else
1216#endif
1217 {
1218 if (curwin->w_topline == 1)
1219 break;
1220 --curwin->w_topline;
1221#ifdef FEAT_DIFF
1222 curwin->w_topfill = 0;
1223#endif
1224#ifdef FEAT_FOLDING
1225 /* A sequence of folded lines only counts for one logical line */
1226 if (hasFolding(curwin->w_topline, &first, NULL))
1227 {
1228 ++done;
1229 if (!byfold)
1230 line_count -= curwin->w_topline - first - 1;
1231 curwin->w_botline -= curwin->w_topline - first;
1232 curwin->w_topline = first;
1233 }
1234 else
1235#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001236 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238 --curwin->w_botline; /* approximate w_botline */
1239 invalidate_botline();
1240 }
1241 curwin->w_wrow += done; /* keep w_wrow updated */
1242 curwin->w_cline_row += done; /* keep w_cline_row updated */
1243
1244#ifdef FEAT_DIFF
1245 if (curwin->w_cursor.lnum == curwin->w_topline)
1246 curwin->w_cline_row = 0;
1247 check_topfill(curwin, TRUE);
1248#endif
1249
1250 /*
1251 * Compute the row number of the last row of the cursor line
1252 * and move the cursor onto the displayed part of the window.
1253 */
1254 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001255 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 validate_virtcol();
1258 validate_cheight();
1259 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001260 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1263 {
1264#ifdef FEAT_FOLDING
1265 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1266 {
1267 --wrow;
1268 if (first == 1)
1269 curwin->w_cursor.lnum = 1;
1270 else
1271 curwin->w_cursor.lnum = first - 1;
1272 }
1273 else
1274#endif
1275 wrow -= plines(curwin->w_cursor.lnum--);
1276 curwin->w_valid &=
1277 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1278 moved = TRUE;
1279 }
1280 if (moved)
1281 {
1282#ifdef FEAT_FOLDING
1283 /* Move cursor to first line of closed fold. */
1284 foldAdjustCursor();
1285#endif
1286 coladvance(curwin->w_curswant);
1287 }
1288}
1289
1290/*
1291 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001294scrollup(
1295 long line_count,
1296 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1299 linenr_T lnum;
1300
1301 if (
1302# ifdef FEAT_FOLDING
1303 (byfold && hasAnyFolding(curwin))
1304# ifdef FEAT_DIFF
1305 ||
1306# endif
1307# endif
1308# ifdef FEAT_DIFF
1309 curwin->w_p_diff
1310# endif
1311 )
1312 {
1313 /* count each sequence of folded lines as one logical line */
1314 lnum = curwin->w_topline;
1315 while (line_count--)
1316 {
1317# ifdef FEAT_DIFF
1318 if (curwin->w_topfill > 0)
1319 --curwin->w_topfill;
1320 else
1321# endif
1322 {
1323# ifdef FEAT_FOLDING
1324 if (byfold)
1325 (void)hasFolding(lnum, NULL, &lnum);
1326# endif
1327 if (lnum >= curbuf->b_ml.ml_line_count)
1328 break;
1329 ++lnum;
1330# ifdef FEAT_DIFF
1331 curwin->w_topfill = diff_check_fill(curwin, lnum);
1332# endif
1333 }
1334 }
1335 /* approximate w_botline */
1336 curwin->w_botline += lnum - curwin->w_topline;
1337 curwin->w_topline = lnum;
1338 }
1339 else
1340#endif
1341 {
1342 curwin->w_topline += line_count;
1343 curwin->w_botline += line_count; /* approximate w_botline */
1344 }
1345
1346 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1347 curwin->w_topline = curbuf->b_ml.ml_line_count;
1348 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1349 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1350
1351#ifdef FEAT_DIFF
1352 check_topfill(curwin, FALSE);
1353#endif
1354
1355#ifdef FEAT_FOLDING
1356 if (hasAnyFolding(curwin))
1357 /* Make sure w_topline is at the first of a sequence of folded lines. */
1358 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1359#endif
1360
1361 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1362 if (curwin->w_cursor.lnum < curwin->w_topline)
1363 {
1364 curwin->w_cursor.lnum = curwin->w_topline;
1365 curwin->w_valid &=
1366 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1367 coladvance(curwin->w_curswant);
1368 }
1369}
1370
1371#ifdef FEAT_DIFF
1372/*
1373 * Don't end up with too many filler lines in the window.
1374 */
1375 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001376check_topfill(
1377 win_T *wp,
1378 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 int n;
1381
1382 if (wp->w_topfill > 0)
1383 {
1384 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1385 if (wp->w_topfill + n > wp->w_height)
1386 {
1387 if (down && wp->w_topline > 1)
1388 {
1389 --wp->w_topline;
1390 wp->w_topfill = 0;
1391 }
1392 else
1393 {
1394 wp->w_topfill = wp->w_height - n;
1395 if (wp->w_topfill < 0)
1396 wp->w_topfill = 0;
1397 }
1398 }
1399 }
1400}
1401
1402/*
1403 * Use as many filler lines as possible for w_topline. Make sure w_topline
1404 * is still visible.
1405 */
1406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001407max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 int n;
1410
1411 n = plines_nofill(curwin->w_topline);
1412 if (n >= curwin->w_height)
1413 curwin->w_topfill = 0;
1414 else
1415 {
1416 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1417 if (curwin->w_topfill + n > curwin->w_height)
1418 curwin->w_topfill = curwin->w_height - n;
1419 }
1420}
1421#endif
1422
1423#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1424/*
1425 * Scroll the screen one line down, but don't do it if it would move the
1426 * cursor off the screen.
1427 */
1428 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001429scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 int end_row;
1432#ifdef FEAT_DIFF
1433 int can_fill = (curwin->w_topfill
1434 < diff_check_fill(curwin, curwin->w_topline));
1435#endif
1436
1437 if (curwin->w_topline <= 1
1438#ifdef FEAT_DIFF
1439 && !can_fill
1440#endif
1441 )
1442 return;
1443
1444 validate_cursor(); /* w_wrow needs to be valid */
1445
1446 /*
1447 * Compute the row number of the last row of the cursor line
1448 * and make sure it doesn't go off the screen. Make sure the cursor
1449 * doesn't go past 'scrolloff' lines from the screen end.
1450 */
1451 end_row = curwin->w_wrow;
1452#ifdef FEAT_DIFF
1453 if (can_fill)
1454 ++end_row;
1455 else
1456 end_row += plines_nofill(curwin->w_topline - 1);
1457#else
1458 end_row += plines(curwin->w_topline - 1);
1459#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001460 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
1462 validate_cheight();
1463 validate_virtcol();
1464 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001465 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 }
1467 if (end_row < curwin->w_height - p_so)
1468 {
1469#ifdef FEAT_DIFF
1470 if (can_fill)
1471 {
1472 ++curwin->w_topfill;
1473 check_topfill(curwin, TRUE);
1474 }
1475 else
1476 {
1477 --curwin->w_topline;
1478 curwin->w_topfill = 0;
1479 }
1480#else
1481 --curwin->w_topline;
1482#endif
1483#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001484 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486 --curwin->w_botline; /* approximate w_botline */
1487 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1488 }
1489}
1490
1491/*
1492 * Scroll the screen one line up, but don't do it if it would move the cursor
1493 * off the screen.
1494 */
1495 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001496scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 int start_row;
1499
1500 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1501#ifdef FEAT_DIFF
1502 && curwin->w_topfill == 0
1503#endif
1504 )
1505 return;
1506
1507 validate_cursor(); /* w_wrow needs to be valid */
1508
1509 /*
1510 * Compute the row number of the first row of the cursor line
1511 * and make sure it doesn't go off the screen. Make sure the cursor
1512 * doesn't go before 'scrolloff' lines from the screen start.
1513 */
1514#ifdef FEAT_DIFF
1515 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1516 - curwin->w_topfill;
1517#else
1518 start_row = curwin->w_wrow - plines(curwin->w_topline);
1519#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001520 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001523 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525 if (start_row >= p_so)
1526 {
1527#ifdef FEAT_DIFF
1528 if (curwin->w_topfill > 0)
1529 --curwin->w_topfill;
1530 else
1531#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001532 {
1533#ifdef FEAT_FOLDING
1534 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 ++curwin->w_botline; /* approximate w_botline */
1539 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1540 }
1541}
1542#endif /* FEAT_INS_EXPAND */
1543
1544/*
1545 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1546 * a (wrapped) text line. Uses and sets "lp->fill".
1547 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001548 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
1550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001551topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553#ifdef FEAT_DIFF
1554 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1555 {
1556 /* Add a filler line. */
1557 ++lp->fill;
1558 lp->height = 1;
1559 }
1560 else
1561#endif
1562 {
1563 --lp->lnum;
1564#ifdef FEAT_DIFF
1565 lp->fill = 0;
1566#endif
1567 if (lp->lnum < 1)
1568 lp->height = MAXCOL;
1569 else
1570#ifdef FEAT_FOLDING
1571 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1572 /* Add a closed fold */
1573 lp->height = 1;
1574 else
1575#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001576 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578}
1579
1580/*
1581 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1582 * a (wrapped) text line. Uses and sets "lp->fill".
1583 * Returns the height of the added line in "lp->height".
1584 * Lines below the last one are incredibly high.
1585 */
1586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001587botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589#ifdef FEAT_DIFF
1590 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1591 {
1592 /* Add a filler line. */
1593 ++lp->fill;
1594 lp->height = 1;
1595 }
1596 else
1597#endif
1598 {
1599 ++lp->lnum;
1600#ifdef FEAT_DIFF
1601 lp->fill = 0;
1602#endif
1603 if (lp->lnum > curbuf->b_ml.ml_line_count)
1604 lp->height = MAXCOL;
1605 else
1606#ifdef FEAT_FOLDING
1607 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1608 /* Add a closed fold */
1609 lp->height = 1;
1610 else
1611#endif
1612 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001613 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615 }
1616}
1617
1618#ifdef FEAT_DIFF
1619/*
1620 * Switch from including filler lines below lp->lnum to including filler
1621 * lines above loff.lnum + 1. This keeps pointing to the same line.
1622 * When there are no filler lines nothing changes.
1623 */
1624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001625botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 if (lp->fill > 0)
1628 {
1629 ++lp->lnum;
1630 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1631 }
1632}
1633
1634/*
1635 * Switch from including filler lines above lp->lnum to including filler
1636 * lines below loff.lnum - 1. This keeps pointing to the same line.
1637 * When there are no filler lines nothing changes.
1638 */
1639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001640topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 if (lp->fill > 0)
1643 {
1644 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1645 --lp->lnum;
1646 }
1647}
1648#endif
1649
1650/*
1651 * Recompute topline to put the cursor at the top of the window.
1652 * Scroll at least "min_scroll" lines.
1653 * If "always" is TRUE, always set topline (for "zt").
1654 */
1655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001656scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658 int scrolled = 0;
1659 int extra = 0;
1660 int used;
1661 int i;
1662 linenr_T top; /* just above displayed lines */
1663 linenr_T bot; /* just below displayed lines */
1664 linenr_T old_topline = curwin->w_topline;
1665#ifdef FEAT_DIFF
1666 linenr_T old_topfill = curwin->w_topfill;
1667#endif
1668 linenr_T new_topline;
1669 int off = p_so;
1670
1671#ifdef FEAT_MOUSE
1672 if (mouse_dragging > 0)
1673 off = mouse_dragging - 1;
1674#endif
1675
1676 /*
1677 * Decrease topline until:
1678 * - it has become 1
1679 * - (part of) the cursor line is moved off the screen or
1680 * - moved at least 'scrolljump' lines and
1681 * - at least 'scrolloff' lines above and below the cursor
1682 */
1683 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001684 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (curwin->w_cursor.lnum < curwin->w_topline)
1686 scrolled = used;
1687
1688#ifdef FEAT_FOLDING
1689 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1690 {
1691 --top;
1692 ++bot;
1693 }
1694 else
1695#endif
1696 {
1697 top = curwin->w_cursor.lnum - 1;
1698 bot = curwin->w_cursor.lnum + 1;
1699 }
1700 new_topline = top + 1;
1701
1702#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001703 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001704 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001705 * Hide filler lines above cursor line by adding them to "extra". */
1706 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
1708
1709 /*
1710 * Check if the lines from "top" to "bot" fit in the window. If they do,
1711 * set new_topline and advance "top" and "bot" to include more lines.
1712 */
1713 while (top > 0)
1714 {
1715#ifdef FEAT_FOLDING
1716 if (hasFolding(top, &top, NULL))
1717 /* count one logical line for a sequence of folded lines */
1718 i = 1;
1719 else
1720#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001721 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 used += i;
1723 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1724 {
1725#ifdef FEAT_FOLDING
1726 if (hasFolding(bot, NULL, &bot))
1727 /* count one logical line for a sequence of folded lines */
1728 ++used;
1729 else
1730#endif
1731 used += plines(bot);
1732 }
1733 if (used > curwin->w_height)
1734 break;
1735 if (top < curwin->w_topline)
1736 scrolled += i;
1737
1738 /*
1739 * If scrolling is needed, scroll at least 'sj' lines.
1740 */
1741 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1742 && extra >= off)
1743 break;
1744
1745 extra += i;
1746 new_topline = top;
1747 --top;
1748 ++bot;
1749 }
1750
1751 /*
1752 * If we don't have enough space, put cursor in the middle.
1753 * This makes sure we get the same position when using "k" and "j"
1754 * in a small window.
1755 */
1756 if (used > curwin->w_height)
1757 scroll_cursor_halfway(FALSE);
1758 else
1759 {
1760 /*
1761 * If "always" is FALSE, only adjust topline to a lower value, higher
1762 * value may happen with wrapping lines
1763 */
1764 if (new_topline < curwin->w_topline || always)
1765 curwin->w_topline = new_topline;
1766 if (curwin->w_topline > curwin->w_cursor.lnum)
1767 curwin->w_topline = curwin->w_cursor.lnum;
1768#ifdef FEAT_DIFF
1769 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1770 if (curwin->w_topfill > 0 && extra > off)
1771 {
1772 curwin->w_topfill -= extra - off;
1773 if (curwin->w_topfill < 0)
1774 curwin->w_topfill = 0;
1775 }
1776 check_topfill(curwin, FALSE);
1777#endif
1778 if (curwin->w_topline != old_topline
1779#ifdef FEAT_DIFF
1780 || curwin->w_topfill != old_topfill
1781#endif
1782 )
1783 curwin->w_valid &=
1784 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1785 curwin->w_valid |= VALID_TOPLINE;
1786 }
1787}
1788
1789/*
1790 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1791 * screen lines for text lines.
1792 */
1793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001794set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796#ifdef FEAT_DIFF
1797 wp->w_filler_rows = 0;
1798#endif
1799 if (used == 0)
1800 wp->w_empty_rows = 0; /* single line that doesn't fit */
1801 else
1802 {
1803 wp->w_empty_rows = wp->w_height - used;
1804#ifdef FEAT_DIFF
1805 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1806 {
1807 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1808 if (wp->w_empty_rows > wp->w_filler_rows)
1809 wp->w_empty_rows -= wp->w_filler_rows;
1810 else
1811 {
1812 wp->w_filler_rows = wp->w_empty_rows;
1813 wp->w_empty_rows = 0;
1814 }
1815 }
1816#endif
1817 }
1818}
1819
1820/*
1821 * Recompute topline to put the cursor at the bottom of the window.
1822 * Scroll at least "min_scroll" lines.
1823 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1824 * This is messy stuff!!!
1825 */
1826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 int used;
1830 int scrolled = 0;
1831 int extra = 0;
1832 int i;
1833 linenr_T line_count;
1834 linenr_T old_topline = curwin->w_topline;
1835 lineoff_T loff;
1836 lineoff_T boff;
1837#ifdef FEAT_DIFF
1838 int old_topfill = curwin->w_topfill;
1839 int fill_below_window;
1840#endif
1841 linenr_T old_botline = curwin->w_botline;
1842 linenr_T old_valid = curwin->w_valid;
1843 int old_empty_rows = curwin->w_empty_rows;
1844 linenr_T cln; /* Cursor Line Number */
1845
1846 cln = curwin->w_cursor.lnum;
1847 if (set_topbot)
1848 {
1849 used = 0;
1850 curwin->w_botline = cln + 1;
1851#ifdef FEAT_DIFF
1852 loff.fill = 0;
1853#endif
1854 for (curwin->w_topline = curwin->w_botline;
1855 curwin->w_topline > 1;
1856 curwin->w_topline = loff.lnum)
1857 {
1858 loff.lnum = curwin->w_topline;
1859 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001860 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 break;
1862 used += loff.height;
1863#ifdef FEAT_DIFF
1864 curwin->w_topfill = loff.fill;
1865#endif
1866 }
1867 set_empty_rows(curwin, used);
1868 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1869 if (curwin->w_topline != old_topline
1870#ifdef FEAT_DIFF
1871 || curwin->w_topfill != old_topfill
1872#endif
1873 )
1874 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1875 }
1876 else
1877 validate_botline();
1878
1879 /* The lines of the cursor line itself are always used. */
1880#ifdef FEAT_DIFF
1881 used = plines_nofill(cln);
1882#else
1883 validate_cheight();
1884 used = curwin->w_cline_height;
1885#endif
1886
1887 /* If the cursor is below botline, we will at least scroll by the height
1888 * of the cursor line. Correct for empty lines, which are really part of
1889 * botline. */
1890 if (cln >= curwin->w_botline)
1891 {
1892 scrolled = used;
1893 if (cln == curwin->w_botline)
1894 scrolled -= curwin->w_empty_rows;
1895 }
1896
1897 /*
1898 * Stop counting lines to scroll when
1899 * - hitting start of the file
1900 * - scrolled nothing or at least 'sj' lines
1901 * - at least 'so' lines below the cursor
1902 * - lines between botline and cursor have been counted
1903 */
1904#ifdef FEAT_FOLDING
1905 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1906#endif
1907 {
1908 loff.lnum = cln;
1909 boff.lnum = cln;
1910 }
1911#ifdef FEAT_DIFF
1912 loff.fill = 0;
1913 boff.fill = 0;
1914 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1915 - curwin->w_filler_rows;
1916#endif
1917
1918 while (loff.lnum > 1)
1919 {
1920 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1921 * context for 'scrolloff' and counted all lines below the window. */
1922 if ((((scrolled <= 0 || scrolled >= min_scroll)
1923 && extra >= (
1924#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001925 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#endif
1927 p_so))
1928 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1929 && loff.lnum <= curwin->w_botline
1930#ifdef FEAT_DIFF
1931 && (loff.lnum < curwin->w_botline
1932 || loff.fill >= fill_below_window)
1933#endif
1934 )
1935 break;
1936
1937 /* Add one line above */
1938 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001939 if (loff.height == MAXCOL)
1940 used = MAXCOL;
1941 else
1942 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (used > curwin->w_height)
1944 break;
1945 if (loff.lnum >= curwin->w_botline
1946#ifdef FEAT_DIFF
1947 && (loff.lnum > curwin->w_botline
1948 || loff.fill <= fill_below_window)
1949#endif
1950 )
1951 {
1952 /* Count screen lines that are below the window. */
1953 scrolled += loff.height;
1954 if (loff.lnum == curwin->w_botline
1955#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001956 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#endif
1958 )
1959 scrolled -= curwin->w_empty_rows;
1960 }
1961
1962 if (boff.lnum < curbuf->b_ml.ml_line_count)
1963 {
1964 /* Add one line below */
1965 botline_forw(&boff);
1966 used += boff.height;
1967 if (used > curwin->w_height)
1968 break;
1969 if (extra < (
1970#ifdef FEAT_MOUSE
1971 mouse_dragging > 0 ? mouse_dragging - 1 :
1972#endif
1973 p_so) || scrolled < min_scroll)
1974 {
1975 extra += boff.height;
1976 if (boff.lnum >= curwin->w_botline
1977#ifdef FEAT_DIFF
1978 || (boff.lnum + 1 == curwin->w_botline
1979 && boff.fill > curwin->w_filler_rows)
1980#endif
1981 )
1982 {
1983 /* Count screen lines that are below the window. */
1984 scrolled += boff.height;
1985 if (boff.lnum == curwin->w_botline
1986#ifdef FEAT_DIFF
1987 && boff.fill == 0
1988#endif
1989 )
1990 scrolled -= curwin->w_empty_rows;
1991 }
1992 }
1993 }
1994 }
1995
1996 /* curwin->w_empty_rows is larger, no need to scroll */
1997 if (scrolled <= 0)
1998 line_count = 0;
1999 /* more than a screenfull, don't scroll but redraw */
2000 else if (used > curwin->w_height)
2001 line_count = used;
2002 /* scroll minimal number of lines */
2003 else
2004 {
2005 line_count = 0;
2006#ifdef FEAT_DIFF
2007 boff.fill = curwin->w_topfill;
2008#endif
2009 boff.lnum = curwin->w_topline - 1;
2010 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2011 {
2012 botline_forw(&boff);
2013 i += boff.height;
2014 ++line_count;
2015 }
2016 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2017 line_count = 9999;
2018 }
2019
2020 /*
2021 * Scroll up if the cursor is off the bottom of the screen a bit.
2022 * Otherwise put it at 1/2 of the screen.
2023 */
2024 if (line_count >= curwin->w_height && line_count > min_scroll)
2025 scroll_cursor_halfway(FALSE);
2026 else
2027 scrollup(line_count, TRUE);
2028
2029 /*
2030 * If topline didn't change we need to restore w_botline and w_empty_rows
2031 * (we changed them).
2032 * If topline did change, update_screen() will set botline.
2033 */
2034 if (curwin->w_topline == old_topline && set_topbot)
2035 {
2036 curwin->w_botline = old_botline;
2037 curwin->w_empty_rows = old_empty_rows;
2038 curwin->w_valid = old_valid;
2039 }
2040 curwin->w_valid |= VALID_TOPLINE;
2041}
2042
2043/*
2044 * Recompute topline to put the cursor halfway the window
2045 * If "atend" is TRUE, also put it halfway at the end of the file.
2046 */
2047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int above = 0;
2051 linenr_T topline;
2052#ifdef FEAT_DIFF
2053 int topfill = 0;
2054#endif
2055 int below = 0;
2056 int used;
2057 lineoff_T loff;
2058 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002059#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002060 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2064#ifdef FEAT_FOLDING
2065 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2066#endif
2067#ifdef FEAT_DIFF
2068 used = plines_nofill(loff.lnum);
2069 loff.fill = 0;
2070 boff.fill = 0;
2071#else
2072 used = plines(loff.lnum);
2073#endif
2074 topline = loff.lnum;
2075 while (topline > 1)
2076 {
2077 if (below <= above) /* add a line below the cursor first */
2078 {
2079 if (boff.lnum < curbuf->b_ml.ml_line_count)
2080 {
2081 botline_forw(&boff);
2082 used += boff.height;
2083 if (used > curwin->w_height)
2084 break;
2085 below += boff.height;
2086 }
2087 else
2088 {
2089 ++below; /* count a "~" line */
2090 if (atend)
2091 ++used;
2092 }
2093 }
2094
2095 if (below > above) /* add a line above the cursor */
2096 {
2097 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002098 if (loff.height == MAXCOL)
2099 used = MAXCOL;
2100 else
2101 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (used > curwin->w_height)
2103 break;
2104 above += loff.height;
2105 topline = loff.lnum;
2106#ifdef FEAT_DIFF
2107 topfill = loff.fill;
2108#endif
2109 }
2110 }
2111#ifdef FEAT_FOLDING
2112 if (!hasFolding(topline, &curwin->w_topline, NULL))
2113#endif
2114 curwin->w_topline = topline;
2115#ifdef FEAT_DIFF
2116 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002117 if (old_topline > curwin->w_topline + curwin->w_height)
2118 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 check_topfill(curwin, FALSE);
2120#endif
2121 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2122 curwin->w_valid |= VALID_TOPLINE;
2123}
2124
2125/*
2126 * Correct the cursor position so that it is in a part of the screen at least
2127 * 'so' lines from the top and bottom, if possible.
2128 * If not possible, put it at the same position as scroll_cursor_halfway().
2129 * When called topline must be valid!
2130 */
2131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int above = 0; /* screen lines above topline */
2135 linenr_T topline;
2136 int below = 0; /* screen lines below botline */
2137 linenr_T botline;
2138 int above_wanted, below_wanted;
2139 linenr_T cln; /* Cursor Line Number */
2140 int max_off;
2141
2142 /*
2143 * How many lines we would like to have above/below the cursor depends on
2144 * whether the first/last line of the file is on screen.
2145 */
2146 above_wanted = p_so;
2147 below_wanted = p_so;
2148#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002149 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 above_wanted = mouse_dragging - 1;
2152 below_wanted = mouse_dragging - 1;
2153 }
2154#endif
2155 if (curwin->w_topline == 1)
2156 {
2157 above_wanted = 0;
2158 max_off = curwin->w_height / 2;
2159 if (below_wanted > max_off)
2160 below_wanted = max_off;
2161 }
2162 validate_botline();
2163 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2164#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002165 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#endif
2167 )
2168 {
2169 below_wanted = 0;
2170 max_off = (curwin->w_height - 1) / 2;
2171 if (above_wanted > max_off)
2172 above_wanted = max_off;
2173 }
2174
2175 /*
2176 * If there are sufficient file-lines above and below the cursor, we can
2177 * return now.
2178 */
2179 cln = curwin->w_cursor.lnum;
2180 if (cln >= curwin->w_topline + above_wanted
2181 && cln < curwin->w_botline - below_wanted
2182#ifdef FEAT_FOLDING
2183 && !hasAnyFolding(curwin)
2184#endif
2185 )
2186 return;
2187
2188 /*
2189 * Narrow down the area where the cursor can be put by taking lines from
2190 * the top and the bottom until:
2191 * - the desired context lines are found
2192 * - the lines from the top is past the lines from the bottom
2193 */
2194 topline = curwin->w_topline;
2195 botline = curwin->w_botline - 1;
2196#ifdef FEAT_DIFF
2197 /* count filler lines as context */
2198 above = curwin->w_topfill;
2199 below = curwin->w_filler_rows;
2200#endif
2201 while ((above < above_wanted || below < below_wanted) && topline < botline)
2202 {
2203 if (below < below_wanted && (below <= above || above >= above_wanted))
2204 {
2205#ifdef FEAT_FOLDING
2206 if (hasFolding(botline, &botline, NULL))
2207 ++below;
2208 else
2209#endif
2210 below += plines(botline);
2211 --botline;
2212 }
2213 if (above < above_wanted && (above < below || below >= below_wanted))
2214 {
2215#ifdef FEAT_FOLDING
2216 if (hasFolding(topline, NULL, &topline))
2217 ++above;
2218 else
2219#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002220 above += PLINES_NOFILL(topline);
2221#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /* Count filler lines below this line as context. */
2223 if (topline < botline)
2224 above += diff_check_fill(curwin, topline + 1);
2225#endif
2226 ++topline;
2227 }
2228 }
2229 if (topline == botline || botline == 0)
2230 curwin->w_cursor.lnum = topline;
2231 else if (topline > botline)
2232 curwin->w_cursor.lnum = botline;
2233 else
2234 {
2235 if (cln < topline && curwin->w_topline > 1)
2236 {
2237 curwin->w_cursor.lnum = topline;
2238 curwin->w_valid &=
2239 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2240 }
2241 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2242 {
2243 curwin->w_cursor.lnum = botline;
2244 curwin->w_valid &=
2245 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2246 }
2247 }
2248 curwin->w_valid |= VALID_TOPLINE;
2249}
2250
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002251static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253/*
2254 * move screen 'count' pages up or down and update screen
2255 *
2256 * return FAIL for failure, OK otherwise
2257 */
2258 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002259onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 long n;
2262 int retval = OK;
2263 lineoff_T loff;
2264 linenr_T old_topline = curwin->w_topline;
2265
2266 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2267 {
2268 beep_flush();
2269 return FAIL;
2270 }
2271
2272 for ( ; count > 0; --count)
2273 {
2274 validate_botline();
2275 /*
2276 * It's an error to move a page up when the first line is already on
2277 * the screen. It's an error to move a page down when the last line
2278 * is on the screen and the topline is 'scrolloff' lines from the
2279 * last line.
2280 */
2281 if (dir == FORWARD
2282 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2283 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2284 : (curwin->w_topline == 1
2285#ifdef FEAT_DIFF
2286 && curwin->w_topfill ==
2287 diff_check_fill(curwin, curwin->w_topline)
2288#endif
2289 ))
2290 {
2291 beep_flush();
2292 retval = FAIL;
2293 break;
2294 }
2295
2296#ifdef FEAT_DIFF
2297 loff.fill = 0;
2298#endif
2299 if (dir == FORWARD)
2300 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002301 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002303 /* Vi compatible scrolling */
2304 if (p_window <= 2)
2305 ++curwin->w_topline;
2306 else
2307 curwin->w_topline += p_window - 2;
2308 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2309 curwin->w_topline = curbuf->b_ml.ml_line_count;
2310 curwin->w_cursor.lnum = curwin->w_topline;
2311 }
2312 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2313 {
2314 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 curwin->w_topline = curbuf->b_ml.ml_line_count;
2316#ifdef FEAT_DIFF
2317 curwin->w_topfill = 0;
2318#endif
2319 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2320 }
2321 else
2322 {
2323 /* For the overlap, start with the line just below the window
2324 * and go upwards. */
2325 loff.lnum = curwin->w_botline;
2326#ifdef FEAT_DIFF
2327 loff.fill = diff_check_fill(curwin, loff.lnum)
2328 - curwin->w_filler_rows;
2329#endif
2330 get_scroll_overlap(&loff, -1);
2331 curwin->w_topline = loff.lnum;
2332#ifdef FEAT_DIFF
2333 curwin->w_topfill = loff.fill;
2334 check_topfill(curwin, FALSE);
2335#endif
2336 curwin->w_cursor.lnum = curwin->w_topline;
2337 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2338 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2339 }
2340 }
2341 else /* dir == BACKWARDS */
2342 {
2343#ifdef FEAT_DIFF
2344 if (curwin->w_topline == 1)
2345 {
2346 /* Include max number of filler lines */
2347 max_topfill();
2348 continue;
2349 }
2350#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002351 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002352 {
2353 /* Vi compatible scrolling (sort of) */
2354 if (p_window <= 2)
2355 --curwin->w_topline;
2356 else
2357 curwin->w_topline -= p_window - 2;
2358 if (curwin->w_topline < 1)
2359 curwin->w_topline = 1;
2360 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2361 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2362 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2363 continue;
2364 }
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 /* Find the line at the top of the window that is going to be the
2367 * line at the bottom of the window. Make sure this results in
2368 * the same line as before doing CTRL-F. */
2369 loff.lnum = curwin->w_topline - 1;
2370#ifdef FEAT_DIFF
2371 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2372 - curwin->w_topfill;
2373#endif
2374 get_scroll_overlap(&loff, 1);
2375
2376 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2377 {
2378 loff.lnum = curbuf->b_ml.ml_line_count;
2379#ifdef FEAT_DIFF
2380 loff.fill = 0;
2381 }
2382 else
2383 {
2384 botline_topline(&loff);
2385#endif
2386 }
2387 curwin->w_cursor.lnum = loff.lnum;
2388
2389 /* Find the line just above the new topline to get the right line
2390 * at the bottom of the window. */
2391 n = 0;
2392 while (n <= curwin->w_height && loff.lnum >= 1)
2393 {
2394 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002395 if (loff.height == MAXCOL)
2396 n = MAXCOL;
2397 else
2398 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002400 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 curwin->w_topline = 1;
2403#ifdef FEAT_DIFF
2404 max_topfill();
2405#endif
2406 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2407 }
2408 else
2409 {
2410 /* Go two lines forward again. */
2411#ifdef FEAT_DIFF
2412 topline_botline(&loff);
2413#endif
2414 botline_forw(&loff);
2415 botline_forw(&loff);
2416#ifdef FEAT_DIFF
2417 botline_topline(&loff);
2418#endif
2419#ifdef FEAT_FOLDING
2420 /* We're at the wrong end of a fold now. */
2421 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2422#endif
2423
2424 /* Always scroll at least one line. Avoid getting stuck on
2425 * very long lines. */
2426 if (loff.lnum >= curwin->w_topline
2427#ifdef FEAT_DIFF
2428 && (loff.lnum > curwin->w_topline
2429 || loff.fill >= curwin->w_topfill)
2430#endif
2431 )
2432 {
2433#ifdef FEAT_DIFF
2434 /* First try using the maximum number of filler lines. If
2435 * that's not enough, backup one line. */
2436 loff.fill = curwin->w_topfill;
2437 if (curwin->w_topfill < diff_check_fill(curwin,
2438 curwin->w_topline))
2439 max_topfill();
2440 if (curwin->w_topfill == loff.fill)
2441#endif
2442 {
2443 --curwin->w_topline;
2444#ifdef FEAT_DIFF
2445 curwin->w_topfill = 0;
2446#endif
2447 }
2448 comp_botline(curwin);
2449 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002450 curwin->w_valid &=
2451 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 else
2454 {
2455 curwin->w_topline = loff.lnum;
2456#ifdef FEAT_DIFF
2457 curwin->w_topfill = loff.fill;
2458 check_topfill(curwin, FALSE);
2459#endif
2460 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2461 }
2462 }
2463 }
2464 }
2465#ifdef FEAT_FOLDING
2466 foldAdjustCursor();
2467#endif
2468 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002469 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002470 if (retval == OK)
2471 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2473
Bram Moolenaar907dad72018-07-10 15:07:15 +02002474 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002476 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2477 // But make sure we scroll at least one line (happens with mix of long
2478 // wrapping lines and non-wrapping line).
2479 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002481 scroll_cursor_top(1, FALSE);
2482 if (curwin->w_topline <= old_topline
2483 && old_topline < curbuf->b_ml.ml_line_count)
2484 {
2485 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002487 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2488#endif
2489 }
2490 }
2491#ifdef FEAT_FOLDING
2492 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496
2497 redraw_later(VALID);
2498 return retval;
2499}
2500
2501/*
2502 * Decide how much overlap to use for page-up or page-down scrolling.
2503 * This is symmetric, so that doing both keeps the same lines displayed.
2504 * Three lines are examined:
2505 *
2506 * before CTRL-F after CTRL-F / before CTRL-B
2507 * etc. l1
2508 * l1 last but one line ------------
2509 * l2 last text line l2 top text line
2510 * ------------- l3 second text line
2511 * l3 etc.
2512 */
2513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002514get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 int h1, h2, h3, h4;
2517 int min_height = curwin->w_height - 2;
2518 lineoff_T loff0, loff1, loff2;
2519
2520#ifdef FEAT_DIFF
2521 if (lp->fill > 0)
2522 lp->height = 1;
2523 else
2524 lp->height = plines_nofill(lp->lnum);
2525#else
2526 lp->height = plines(lp->lnum);
2527#endif
2528 h1 = lp->height;
2529 if (h1 > min_height)
2530 return; /* no overlap */
2531
2532 loff0 = *lp;
2533 if (dir > 0)
2534 botline_forw(lp);
2535 else
2536 topline_back(lp);
2537 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002538 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 *lp = loff0; /* no overlap */
2541 return;
2542 }
2543
2544 loff1 = *lp;
2545 if (dir > 0)
2546 botline_forw(lp);
2547 else
2548 topline_back(lp);
2549 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002550 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 *lp = loff0; /* no overlap */
2553 return;
2554 }
2555
2556 loff2 = *lp;
2557 if (dir > 0)
2558 botline_forw(lp);
2559 else
2560 topline_back(lp);
2561 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002562 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 *lp = loff1; /* 1 line overlap */
2564 else
2565 *lp = loff2; /* 2 lines overlap */
2566 return;
2567}
2568
2569/* #define KEEP_SCREEN_LINE */
2570/*
2571 * Scroll 'scroll' lines up or down.
2572 */
2573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002574halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 long scrolled = 0;
2577 int i;
2578 int n;
2579 int room;
2580
2581 if (Prenum)
2582 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2583 curwin->w_height : Prenum;
2584 n = (curwin->w_p_scr <= curwin->w_height) ?
2585 curwin->w_p_scr : curwin->w_height;
2586
Bram Moolenaard5d37532017-03-27 23:02:07 +02002587 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 validate_botline();
2589 room = curwin->w_empty_rows;
2590#ifdef FEAT_DIFF
2591 room += curwin->w_filler_rows;
2592#endif
2593 if (flag)
2594 {
2595 /*
2596 * scroll the text up
2597 */
2598 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2599 {
2600#ifdef FEAT_DIFF
2601 if (curwin->w_topfill > 0)
2602 {
2603 i = 1;
2604 if (--n < 0 && scrolled > 0)
2605 break;
2606 --curwin->w_topfill;
2607 }
2608 else
2609#endif
2610 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002611 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 n -= i;
2613 if (n < 0 && scrolled > 0)
2614 break;
2615#ifdef FEAT_FOLDING
2616 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2617#endif
2618 ++curwin->w_topline;
2619#ifdef FEAT_DIFF
2620 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2621#endif
2622
2623#ifndef KEEP_SCREEN_LINE
2624 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2625 {
2626 ++curwin->w_cursor.lnum;
2627 curwin->w_valid &=
2628 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2629 }
2630#endif
2631 }
2632 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2633 scrolled += i;
2634
2635 /*
2636 * Correct w_botline for changed w_topline.
2637 * Won't work when there are filler lines.
2638 */
2639#ifdef FEAT_DIFF
2640 if (curwin->w_p_diff)
2641 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2642 else
2643#endif
2644 {
2645 room += i;
2646 do
2647 {
2648 i = plines(curwin->w_botline);
2649 if (i > room)
2650 break;
2651#ifdef FEAT_FOLDING
2652 (void)hasFolding(curwin->w_botline, NULL,
2653 &curwin->w_botline);
2654#endif
2655 ++curwin->w_botline;
2656 room -= i;
2657 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2658 }
2659 }
2660
2661#ifndef KEEP_SCREEN_LINE
2662 /*
2663 * When hit bottom of the file: move cursor down.
2664 */
2665 if (n > 0)
2666 {
2667# ifdef FEAT_FOLDING
2668 if (hasAnyFolding(curwin))
2669 {
2670 while (--n >= 0
2671 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2672 {
2673 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2674 &curwin->w_cursor.lnum);
2675 ++curwin->w_cursor.lnum;
2676 }
2677 }
2678 else
2679# endif
2680 curwin->w_cursor.lnum += n;
2681 check_cursor_lnum();
2682 }
2683#else
2684 /* try to put the cursor in the same screen line */
2685 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2686 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2687 {
2688 scrolled -= plines(curwin->w_cursor.lnum);
2689 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2690 break;
2691# ifdef FEAT_FOLDING
2692 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2693 &curwin->w_cursor.lnum);
2694# endif
2695 ++curwin->w_cursor.lnum;
2696 }
2697#endif
2698 }
2699 else
2700 {
2701 /*
2702 * scroll the text down
2703 */
2704 while (n > 0 && curwin->w_topline > 1)
2705 {
2706#ifdef FEAT_DIFF
2707 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2708 {
2709 i = 1;
2710 if (--n < 0 && scrolled > 0)
2711 break;
2712 ++curwin->w_topfill;
2713 }
2714 else
2715#endif
2716 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002717 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 n -= i;
2719 if (n < 0 && scrolled > 0)
2720 break;
2721 --curwin->w_topline;
2722#ifdef FEAT_FOLDING
2723 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2724#endif
2725#ifdef FEAT_DIFF
2726 curwin->w_topfill = 0;
2727#endif
2728 }
2729 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2730 VALID_BOTLINE|VALID_BOTLINE_AP);
2731 scrolled += i;
2732#ifndef KEEP_SCREEN_LINE
2733 if (curwin->w_cursor.lnum > 1)
2734 {
2735 --curwin->w_cursor.lnum;
2736 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2737 }
2738#endif
2739 }
2740#ifndef KEEP_SCREEN_LINE
2741 /*
2742 * When hit top of the file: move cursor up.
2743 */
2744 if (n > 0)
2745 {
2746 if (curwin->w_cursor.lnum <= (linenr_T)n)
2747 curwin->w_cursor.lnum = 1;
2748 else
2749# ifdef FEAT_FOLDING
2750 if (hasAnyFolding(curwin))
2751 {
2752 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2753 {
2754 --curwin->w_cursor.lnum;
2755 (void)hasFolding(curwin->w_cursor.lnum,
2756 &curwin->w_cursor.lnum, NULL);
2757 }
2758 }
2759 else
2760# endif
2761 curwin->w_cursor.lnum -= n;
2762 }
2763#else
2764 /* try to put the cursor in the same screen line */
2765 scrolled += n; /* move cursor when topline is 1 */
2766 while (curwin->w_cursor.lnum > curwin->w_topline
2767 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2768 {
2769 scrolled -= plines(curwin->w_cursor.lnum - 1);
2770 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2771 break;
2772 --curwin->w_cursor.lnum;
2773# ifdef FEAT_FOLDING
2774 foldAdjustCursor();
2775# endif
2776 }
2777#endif
2778 }
2779# ifdef FEAT_FOLDING
2780 /* Move cursor to first line of closed fold. */
2781 foldAdjustCursor();
2782# endif
2783#ifdef FEAT_DIFF
2784 check_topfill(curwin, !flag);
2785#endif
2786 cursor_correct();
2787 beginline(BL_SOL | BL_FIX);
2788 redraw_later(VALID);
2789}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002790
Bram Moolenaar860cae12010-06-05 23:22:07 +02002791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002792do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002793{
2794 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002795 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002796 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002797 colnr_T curswant = curwin->w_curswant;
2798 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002799 win_T *old_curwin = curwin;
2800 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002801 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002802 int old_VIsual_select = VIsual_select;
2803 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002804
2805 /*
2806 * loop through the cursorbound windows
2807 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002808 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002809 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002810 {
2811 curbuf = curwin->w_buffer;
2812 /* skip original window and windows with 'noscrollbind' */
2813 if (curwin != old_curwin && curwin->w_p_crb)
2814 {
2815# ifdef FEAT_DIFF
2816 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002817 curwin->w_cursor.lnum =
2818 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819 else
2820# endif
2821 curwin->w_cursor.lnum = line;
2822 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002823 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002824 curwin->w_curswant = curswant;
2825 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002826
Bram Moolenaar61452852011-02-01 18:01:11 +01002827 /* Make sure the cursor is in a valid position. Temporarily set
2828 * "restart_edit" to allow the cursor to be beyond the EOL. */
2829 restart_edit_save = restart_edit;
2830 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002831 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002832# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002833 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002834 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002835# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002836 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837 /* Correct cursor for multi-byte character. */
2838 if (has_mbyte)
2839 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002840 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002841
2842 /* Only scroll when 'scrollbind' hasn't done this. */
2843 if (!curwin->w_p_scb)
2844 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002845 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 }
2847 }
2848
2849 /*
2850 * reset current-window
2851 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002852 VIsual_select = old_VIsual_select;
2853 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 curwin = old_curwin;
2855 curbuf = old_curbuf;
2856}