blob: 68f8ae4a42c496a01f8ed6f0275a6618e24fb288 [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 comp_botline(win_T *wp);
23static void redraw_for_cursorline(win_T *wp);
24static int scrolljump_value(void);
25static int check_top_offset(void);
26static void curs_rows(win_T *wp);
27static void validate_cheight(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29typedef struct
30{
31 linenr_T lnum; /* line number */
32#ifdef FEAT_DIFF
33 int fill; /* filler lines */
34#endif
35 int height; /* height of added line */
36} lineoff_T;
37
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void topline_back(lineoff_T *lp);
39static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef FEAT_DIFF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010041static void botline_topline(lineoff_T *lp);
42static void topline_botline(lineoff_T *lp);
43static void max_topfill(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
45
46/*
47 * Compute wp->w_botline for the current wp->w_topline. Can be called after
48 * wp->w_topline changed.
49 */
50 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010051comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052{
53 int n;
54 linenr_T lnum;
55 int done;
56#ifdef FEAT_FOLDING
57 linenr_T last;
58 int folded;
59#endif
60
61 /*
62 * If w_cline_row is valid, start there.
63 * Otherwise have to start at w_topline.
64 */
65 check_cursor_moved(wp);
66 if (wp->w_valid & VALID_CROW)
67 {
68 lnum = wp->w_cursor.lnum;
69 done = wp->w_cline_row;
70 }
71 else
72 {
73 lnum = wp->w_topline;
74 done = 0;
75 }
76
77 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
78 {
79#ifdef FEAT_FOLDING
80 last = lnum;
81 folded = FALSE;
82 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
83 {
84 n = 1;
85 folded = TRUE;
86 }
87 else
88#endif
89#ifdef FEAT_DIFF
90 if (lnum == wp->w_topline)
91 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
92 else
93#endif
94 n = plines_win(wp, lnum, TRUE);
95 if (
96#ifdef FEAT_FOLDING
97 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
98#else
99 lnum == wp->w_cursor.lnum
100#endif
101 )
102 {
103 wp->w_cline_row = done;
104 wp->w_cline_height = n;
105#ifdef FEAT_FOLDING
106 wp->w_cline_folded = folded;
107#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100108 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
110 }
111 if (done + n > wp->w_height)
112 break;
113 done += n;
114#ifdef FEAT_FOLDING
115 lnum = last;
116#endif
117 }
118
119 /* wp->w_botline is the line that is just below the window */
120 wp->w_botline = lnum;
121 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
122
123 set_empty_rows(wp, done);
124}
125
Bram Moolenaar90a99792018-09-12 21:52:18 +0200126#ifdef FEAT_SYN_HL
127static linenr_T last_cursorline = 0;
128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100131 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
132 * set.
133 */
134 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100135redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100136{
137 if ((wp->w_p_rnu
138#ifdef FEAT_SYN_HL
139 || wp->w_p_cul
140#endif
141 )
142 && (wp->w_valid & VALID_CROW) == 0
143# ifdef FEAT_INS_EXPAND
144 && !pum_visible()
145# endif
146 )
Bram Moolenaar90a99792018-09-12 21:52:18 +0200147 {
148#ifdef FEAT_SYN_HL
149 if (!wp->w_p_rnu && wp->w_redr_type <= VALID && last_cursorline != 0)
150 {
151 // "last_cursorline" may be set for another window, worst case we
152 // redraw too much. This is optimized for moving the cursor around
153 // in the same window.
154 redrawWinline(wp, last_cursorline, FALSE);
155 redrawWinline(wp, wp->w_cursor.lnum, FALSE);
156 last_cursorline = wp->w_cursor.lnum;
157 redraw_win_later(wp, VALID);
158 }
159 else
160#endif
161 redraw_win_later(wp, SOME_VALID);
162 }
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
509#ifdef FEAT_VIRTUALEDIT
510 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
511#endif
512 )
513 {
514 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
515 wp->w_valid_cursor.col = wp->w_cursor.col;
516 wp->w_valid_leftcol = wp->w_leftcol;
517#ifdef FEAT_VIRTUALEDIT
518 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
519#endif
520 }
521}
522
523/*
524 * Call this function when some window settings have changed, which require
525 * the cursor position, botline and topline to be recomputed and the window to
526 * be redrawn. E.g, when changing the 'wrap' option or folding.
527 */
528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100529changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 changed_window_setting_win(curwin);
532}
533
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 wp->w_lines_valid = 0;
538 changed_line_abv_curs_win(wp);
539 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
540 redraw_win_later(wp, NOT_VALID);
541}
542
543/*
544 * Set wp->w_topline to a certain number.
545 */
546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549#ifdef FEAT_FOLDING
550 /* go to first of folded lines */
551 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
552#endif
553 /* Approximate the value of w_botline */
554 wp->w_botline += lnum - wp->w_topline;
555 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000556 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#ifdef FEAT_DIFF
558 wp->w_topfill = 0;
559#endif
560 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
561 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
562 redraw_later(VALID);
563}
564
565/*
566 * Call this function when the length of the cursor line (in screen
567 * characters) has changed, and the change is before the cursor.
568 * Need to take care of w_botline separately!
569 */
570 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100571changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
574 |VALID_CHEIGHT|VALID_TOPLINE);
575}
576
577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100578changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
581 |VALID_CHEIGHT|VALID_TOPLINE);
582}
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584/*
585 * Call this function when the length of a line (in screen characters) above
586 * the cursor have changed.
587 * Need to take care of w_botline separately!
588 */
589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100590changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
593 |VALID_CHEIGHT|VALID_TOPLINE);
594}
595
596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100597changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
600 |VALID_CHEIGHT|VALID_TOPLINE);
601}
602
603/*
604 * Make sure the value of curwin->w_botline is valid.
605 */
606 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100607validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 if (!(curwin->w_valid & VALID_BOTLINE))
610 comp_botline(curwin);
611}
612
613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * Mark curwin->w_botline as invalid (because of some change in the buffer).
615 */
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
620}
621
622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100623invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629approximate_botline_win(
630 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 wp->w_valid &= ~VALID_BOTLINE;
633}
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635/*
636 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
637 */
638 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100639cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 check_cursor_moved(curwin);
642 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
643 (VALID_WROW|VALID_WCOL));
644}
645
646/*
647 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
648 * w_topline must be valid, you may need to call update_topline() first!
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 check_cursor_moved(curwin);
654 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
655 curs_columns(TRUE);
656}
657
658#if defined(FEAT_GUI) || defined(PROTO)
659/*
660 * validate w_cline_row.
661 */
662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100663validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 /*
666 * First make sure that w_topline is valid (after moving the cursor).
667 */
668 update_topline();
669 check_cursor_moved(curwin);
670 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100671 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673#endif
674
675/*
676 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200677 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 */
679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 linenr_T lnum;
683 int i;
684 int all_invalid;
685 int valid;
686#ifdef FEAT_FOLDING
687 long fold_count;
688#endif
689
690 /* Check if wp->w_lines[].wl_size is invalid */
691 all_invalid = (!redrawing()
692 || wp->w_lines_valid == 0
693 || wp->w_lines[0].wl_lnum > wp->w_topline);
694 i = 0;
695 wp->w_cline_row = 0;
696 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
697 {
698 valid = FALSE;
699 if (!all_invalid && i < wp->w_lines_valid)
700 {
701 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
702 continue; /* skip changed or deleted lines */
703 if (wp->w_lines[i].wl_lnum == lnum)
704 {
705#ifdef FEAT_FOLDING
706 /* Check for newly inserted lines below this row, in which
707 * case we need to check for folded lines. */
708 if (!wp->w_buffer->b_mod_set
709 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
710 || wp->w_buffer->b_mod_top
711 > wp->w_lines[i].wl_lastlnum + 1)
712#endif
713 valid = TRUE;
714 }
715 else if (wp->w_lines[i].wl_lnum > lnum)
716 --i; /* hold at inserted lines */
717 }
718 if (valid
719#ifdef FEAT_DIFF
720 && (lnum != wp->w_topline || !wp->w_p_diff)
721#endif
722 )
723 {
724#ifdef FEAT_FOLDING
725 lnum = wp->w_lines[i].wl_lastlnum + 1;
726 /* Cursor inside folded lines, don't count this row */
727 if (lnum > wp->w_cursor.lnum)
728 break;
729#else
730 ++lnum;
731#endif
732 wp->w_cline_row += wp->w_lines[i].wl_size;
733 }
734 else
735 {
736#ifdef FEAT_FOLDING
737 fold_count = foldedCount(wp, lnum, NULL);
738 if (fold_count)
739 {
740 lnum += fold_count;
741 if (lnum > wp->w_cursor.lnum)
742 break;
743 ++wp->w_cline_row;
744 }
745 else
746#endif
747#ifdef FEAT_DIFF
748 if (lnum == wp->w_topline)
749 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
750 + wp->w_topfill;
751 else
752#endif
753 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
754 }
755 }
756
757 check_cursor_moved(wp);
758 if (!(wp->w_valid & VALID_CHEIGHT))
759 {
760 if (all_invalid
761 || i == wp->w_lines_valid
762 || (i < wp->w_lines_valid
763 && (!wp->w_lines[i].wl_valid
764 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
765 {
766#ifdef FEAT_DIFF
767 if (wp->w_cursor.lnum == wp->w_topline)
768 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
769 TRUE) + wp->w_topfill;
770 else
771#endif
772 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
773#ifdef FEAT_FOLDING
774 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
775 NULL, NULL, TRUE, NULL);
776#endif
777 }
778 else if (i > wp->w_lines_valid)
779 {
780 /* a line that is too long to fit on the last screen line */
781 wp->w_cline_height = 0;
782#ifdef FEAT_FOLDING
783 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
784 NULL, NULL, TRUE, NULL);
785#endif
786 }
787 else
788 {
789 wp->w_cline_height = wp->w_lines[i].wl_size;
790#ifdef FEAT_FOLDING
791 wp->w_cline_folded = wp->w_lines[i].wl_folded;
792#endif
793 }
794 }
795
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100796 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
802 * Validate curwin->w_virtcol only.
803 */
804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100805validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 validate_virtcol_win(curwin);
808}
809
810/*
811 * Validate wp->w_virtcol only.
812 */
813 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100814validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 check_cursor_moved(wp);
817 if (!(wp->w_valid & VALID_VIRTCOL))
818 {
819 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
820 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000821#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000822 if (wp->w_p_cuc
823# ifdef FEAT_INS_EXPAND
824 && !pum_visible()
825# endif
826 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000827 redraw_win_later(wp, SOME_VALID);
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830}
831
832/*
833 * Validate curwin->w_cline_height only.
834 */
835 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 check_cursor_moved(curwin);
839 if (!(curwin->w_valid & VALID_CHEIGHT))
840 {
841#ifdef FEAT_DIFF
842 if (curwin->w_cursor.lnum == curwin->w_topline)
843 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
844 + curwin->w_topfill;
845 else
846#endif
847 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
848#ifdef FEAT_FOLDING
849 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
850#endif
851 curwin->w_valid |= VALID_CHEIGHT;
852 }
853}
854
855/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000856 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 */
858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100859validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 colnr_T off;
862 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100863 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 validate_virtcol();
866 if (!(curwin->w_valid & VALID_WCOL))
867 {
868 col = curwin->w_virtcol;
869 off = curwin_col_off();
870 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200871 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000874 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200875 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100876 && width > 0)
877 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200878 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000879 if (col > (int)curwin->w_leftcol)
880 col -= curwin->w_leftcol;
881 else
882 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 curwin->w_valid |= VALID_WCOL;
886 }
887}
888
889/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200890 * Compute offset of a window, occupied by absolute or relative line number,
891 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 */
893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
Bram Moolenaar64486672010-05-16 15:46:46 +0200896 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#ifdef FEAT_CMDWIN
898 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
899#endif
900#ifdef FEAT_FOLDING
901 + wp->w_p_fdc
902#endif
903#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200904 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#endif
906 );
907}
908
909 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100910curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 return win_col_off(curwin);
913}
914
915/*
916 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200917 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
918 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 */
920 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100921win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
Bram Moolenaar64486672010-05-16 15:46:46 +0200923 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000924 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 return 0;
926}
927
928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100929curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 return win_col_off2(curwin);
932}
933
934/*
935 * compute curwin->w_wcol and curwin->w_virtcol.
936 * Also updates curwin->w_wrow and curwin->w_cline_row.
937 * Also updates curwin->w_leftcol.
938 */
939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100940curs_columns(
941 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 int diff;
944 int extra; /* offset for first screen line */
945 int off_left, off_right;
946 int n;
947 int p_lines;
948 int width = 0;
949 int textwidth;
950 int new_leftcol;
951 colnr_T startcol;
952 colnr_T endcol;
953 colnr_T prev_skipcol;
954
955 /*
956 * First make sure that w_topline is valid (after moving the cursor).
957 */
958 update_topline();
959
960 /*
961 * Next make sure that w_cline_row is valid.
962 */
963 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100964 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 /*
967 * Compute the number of virtual columns.
968 */
969#ifdef FEAT_FOLDING
970 if (curwin->w_cline_folded)
971 /* In a folded line the cursor is always in the first column */
972 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
973 else
974#endif
975 getvvcol(curwin, &curwin->w_cursor,
976 &startcol, &(curwin->w_virtcol), &endcol);
977
978 /* remove '$' from change command when cursor moves onto it */
979 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100980 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 extra = curwin_col_off();
983 curwin->w_wcol = curwin->w_virtcol + extra;
984 endcol += extra;
985
986 /*
987 * Now compute w_wrow, counting screen lines from w_cline_row.
988 */
989 curwin->w_wrow = curwin->w_cline_row;
990
Bram Moolenaar02631462017-09-22 15:20:32 +0200991 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (textwidth <= 0)
993 {
994 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200995 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 curwin->w_wrow = curwin->w_height - 1;
997 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200998 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
1000 width = textwidth + curwin_col_off2();
1001
1002 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +02001003 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001005 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +02001006 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 curwin->w_wcol -= n * width;
1008 curwin->w_wrow += n;
1009
1010#ifdef FEAT_LINEBREAK
1011 /* When cursor wraps to first char of next line in Insert
1012 * mode, the 'showbreak' string isn't shown, backup to first
1013 * column */
1014 if (*p_sbr && *ml_get_cursor() == NUL
1015 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1016 curwin->w_wcol = 0;
1017#endif
1018 }
1019 }
1020
1021 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1022 * is not folded.
1023 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001024 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_FOLDING
1026 && !curwin->w_cline_folded
1027#endif
1028 )
1029 {
1030 /*
1031 * If Cursor is left of the screen, scroll rightwards.
1032 * If Cursor is right of the screen, scroll leftwards
1033 * If we get closer to the edge than 'sidescrolloff', scroll a little
1034 * extra
1035 */
1036 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001037 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 - p_siso) + 1;
1039 if (off_left < 0 || off_right > 0)
1040 {
1041 if (off_left < 0)
1042 diff = -off_left;
1043 else
1044 diff = off_right;
1045
1046 /* When far off or not enough room on either side, put cursor in
1047 * middle of window. */
1048 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1049 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1050 else
1051 {
1052 if (diff < p_ss)
1053 diff = p_ss;
1054 if (off_left < 0)
1055 new_leftcol = curwin->w_leftcol - diff;
1056 else
1057 new_leftcol = curwin->w_leftcol + diff;
1058 }
1059 if (new_leftcol < 0)
1060 new_leftcol = 0;
1061 if (new_leftcol != (int)curwin->w_leftcol)
1062 {
1063 curwin->w_leftcol = new_leftcol;
1064 /* screen has to be redrawn with new curwin->w_leftcol */
1065 redraw_later(NOT_VALID);
1066 }
1067 }
1068 curwin->w_wcol -= curwin->w_leftcol;
1069 }
1070 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1071 curwin->w_wcol -= curwin->w_leftcol;
1072 else
1073 curwin->w_wcol = 0;
1074
1075#ifdef FEAT_DIFF
1076 /* Skip over filler lines. At the top use w_topfill, there
1077 * may be some filler lines above the window. */
1078 if (curwin->w_cursor.lnum == curwin->w_topline)
1079 curwin->w_wrow += curwin->w_topfill;
1080 else
1081 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1082#endif
1083
1084 prev_skipcol = curwin->w_skipcol;
1085
1086 p_lines = 0;
1087 if ((curwin->w_wrow >= curwin->w_height
1088 || ((prev_skipcol > 0
1089 || curwin->w_wrow + p_so >= curwin->w_height)
1090 && (p_lines =
1091#ifdef FEAT_DIFF
1092 plines_win_nofill
1093#else
1094 plines_win
1095#endif
1096 (curwin, curwin->w_cursor.lnum, FALSE))
1097 - 1 >= curwin->w_height))
1098 && curwin->w_height != 0
1099 && curwin->w_cursor.lnum == curwin->w_topline
1100 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001101 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 /* Cursor past end of screen. Happens with a single line that does
1104 * not fit on screen. Find a skipcol to show the text around the
1105 * cursor. Avoid scrolling all the time. compute value of "extra":
1106 * 1: Less than "p_so" lines above
1107 * 2: Less than "p_so" lines below
1108 * 3: both of them */
1109 extra = 0;
1110 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1111 extra = 1;
1112 /* Compute last display line of the buffer line that we want at the
1113 * bottom of the window. */
1114 if (p_lines == 0)
1115 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1116 --p_lines;
1117 if (p_lines > curwin->w_wrow + p_so)
1118 n = curwin->w_wrow + p_so;
1119 else
1120 n = p_lines;
1121 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1122 extra += 2;
1123
1124 if (extra == 3 || p_lines < p_so * 2)
1125 {
1126 /* not enough room for 'scrolloff', put cursor in the middle */
1127 n = curwin->w_virtcol / width;
1128 if (n > curwin->w_height / 2)
1129 n -= curwin->w_height / 2;
1130 else
1131 n = 0;
1132 /* don't skip more than necessary */
1133 if (n > p_lines - curwin->w_height + 1)
1134 n = p_lines - curwin->w_height + 1;
1135 curwin->w_skipcol = n * width;
1136 }
1137 else if (extra == 1)
1138 {
1139 /* less then 'scrolloff' lines above, decrease skipcol */
1140 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1141 + width - 1) / width;
1142 if (extra > 0)
1143 {
1144 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1145 extra = curwin->w_skipcol / width;
1146 curwin->w_skipcol -= extra * width;
1147 }
1148 }
1149 else if (extra == 2)
1150 {
1151 /* less then 'scrolloff' lines below, increase skipcol */
1152 endcol = (n - curwin->w_height + 1) * width;
1153 while (endcol > curwin->w_virtcol)
1154 endcol -= width;
1155 if (endcol > curwin->w_skipcol)
1156 curwin->w_skipcol = endcol;
1157 }
1158
1159 curwin->w_wrow -= curwin->w_skipcol / width;
1160 if (curwin->w_wrow >= curwin->w_height)
1161 {
1162 /* small window, make sure cursor is in it */
1163 extra = curwin->w_wrow - curwin->w_height + 1;
1164 curwin->w_skipcol += extra * width;
1165 curwin->w_wrow -= extra;
1166 }
1167
1168 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1169 if (extra > 0)
1170 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1171 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001172 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 else
1175 curwin->w_skipcol = 0;
1176 if (prev_skipcol != curwin->w_skipcol)
1177 redraw_later(NOT_VALID);
1178
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001179#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001180 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1181 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001182# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001183 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001184# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001185 )
1186 redraw_later(SOME_VALID);
1187#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1190}
1191
1192/*
1193 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001196scrolldown(
1197 long line_count,
1198 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 long done = 0; /* total # of physical lines done */
1201 int wrow;
1202 int moved = FALSE;
1203
1204#ifdef FEAT_FOLDING
1205 linenr_T first;
1206
1207 /* Make sure w_topline is at the first of a sequence of folded lines. */
1208 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1209#endif
1210 validate_cursor(); /* w_wrow needs to be valid */
1211 while (line_count-- > 0)
1212 {
1213#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001214 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1215 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
1217 ++curwin->w_topfill;
1218 ++done;
1219 }
1220 else
1221#endif
1222 {
1223 if (curwin->w_topline == 1)
1224 break;
1225 --curwin->w_topline;
1226#ifdef FEAT_DIFF
1227 curwin->w_topfill = 0;
1228#endif
1229#ifdef FEAT_FOLDING
1230 /* A sequence of folded lines only counts for one logical line */
1231 if (hasFolding(curwin->w_topline, &first, NULL))
1232 {
1233 ++done;
1234 if (!byfold)
1235 line_count -= curwin->w_topline - first - 1;
1236 curwin->w_botline -= curwin->w_topline - first;
1237 curwin->w_topline = first;
1238 }
1239 else
1240#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001241 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 --curwin->w_botline; /* approximate w_botline */
1244 invalidate_botline();
1245 }
1246 curwin->w_wrow += done; /* keep w_wrow updated */
1247 curwin->w_cline_row += done; /* keep w_cline_row updated */
1248
1249#ifdef FEAT_DIFF
1250 if (curwin->w_cursor.lnum == curwin->w_topline)
1251 curwin->w_cline_row = 0;
1252 check_topfill(curwin, TRUE);
1253#endif
1254
1255 /*
1256 * Compute the row number of the last row of the cursor line
1257 * and move the cursor onto the displayed part of the window.
1258 */
1259 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001260 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 validate_virtcol();
1263 validate_cheight();
1264 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001265 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1268 {
1269#ifdef FEAT_FOLDING
1270 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1271 {
1272 --wrow;
1273 if (first == 1)
1274 curwin->w_cursor.lnum = 1;
1275 else
1276 curwin->w_cursor.lnum = first - 1;
1277 }
1278 else
1279#endif
1280 wrow -= plines(curwin->w_cursor.lnum--);
1281 curwin->w_valid &=
1282 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1283 moved = TRUE;
1284 }
1285 if (moved)
1286 {
1287#ifdef FEAT_FOLDING
1288 /* Move cursor to first line of closed fold. */
1289 foldAdjustCursor();
1290#endif
1291 coladvance(curwin->w_curswant);
1292 }
1293}
1294
1295/*
1296 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299scrollup(
1300 long line_count,
1301 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1304 linenr_T lnum;
1305
1306 if (
1307# ifdef FEAT_FOLDING
1308 (byfold && hasAnyFolding(curwin))
1309# ifdef FEAT_DIFF
1310 ||
1311# endif
1312# endif
1313# ifdef FEAT_DIFF
1314 curwin->w_p_diff
1315# endif
1316 )
1317 {
1318 /* count each sequence of folded lines as one logical line */
1319 lnum = curwin->w_topline;
1320 while (line_count--)
1321 {
1322# ifdef FEAT_DIFF
1323 if (curwin->w_topfill > 0)
1324 --curwin->w_topfill;
1325 else
1326# endif
1327 {
1328# ifdef FEAT_FOLDING
1329 if (byfold)
1330 (void)hasFolding(lnum, NULL, &lnum);
1331# endif
1332 if (lnum >= curbuf->b_ml.ml_line_count)
1333 break;
1334 ++lnum;
1335# ifdef FEAT_DIFF
1336 curwin->w_topfill = diff_check_fill(curwin, lnum);
1337# endif
1338 }
1339 }
1340 /* approximate w_botline */
1341 curwin->w_botline += lnum - curwin->w_topline;
1342 curwin->w_topline = lnum;
1343 }
1344 else
1345#endif
1346 {
1347 curwin->w_topline += line_count;
1348 curwin->w_botline += line_count; /* approximate w_botline */
1349 }
1350
1351 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1352 curwin->w_topline = curbuf->b_ml.ml_line_count;
1353 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1354 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1355
1356#ifdef FEAT_DIFF
1357 check_topfill(curwin, FALSE);
1358#endif
1359
1360#ifdef FEAT_FOLDING
1361 if (hasAnyFolding(curwin))
1362 /* Make sure w_topline is at the first of a sequence of folded lines. */
1363 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1364#endif
1365
1366 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1367 if (curwin->w_cursor.lnum < curwin->w_topline)
1368 {
1369 curwin->w_cursor.lnum = curwin->w_topline;
1370 curwin->w_valid &=
1371 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1372 coladvance(curwin->w_curswant);
1373 }
1374}
1375
1376#ifdef FEAT_DIFF
1377/*
1378 * Don't end up with too many filler lines in the window.
1379 */
1380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001381check_topfill(
1382 win_T *wp,
1383 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 int n;
1386
1387 if (wp->w_topfill > 0)
1388 {
1389 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1390 if (wp->w_topfill + n > wp->w_height)
1391 {
1392 if (down && wp->w_topline > 1)
1393 {
1394 --wp->w_topline;
1395 wp->w_topfill = 0;
1396 }
1397 else
1398 {
1399 wp->w_topfill = wp->w_height - n;
1400 if (wp->w_topfill < 0)
1401 wp->w_topfill = 0;
1402 }
1403 }
1404 }
1405}
1406
1407/*
1408 * Use as many filler lines as possible for w_topline. Make sure w_topline
1409 * is still visible.
1410 */
1411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 int n;
1415
1416 n = plines_nofill(curwin->w_topline);
1417 if (n >= curwin->w_height)
1418 curwin->w_topfill = 0;
1419 else
1420 {
1421 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1422 if (curwin->w_topfill + n > curwin->w_height)
1423 curwin->w_topfill = curwin->w_height - n;
1424 }
1425}
1426#endif
1427
1428#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1429/*
1430 * Scroll the screen one line down, but don't do it if it would move the
1431 * cursor off the screen.
1432 */
1433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001434scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 int end_row;
1437#ifdef FEAT_DIFF
1438 int can_fill = (curwin->w_topfill
1439 < diff_check_fill(curwin, curwin->w_topline));
1440#endif
1441
1442 if (curwin->w_topline <= 1
1443#ifdef FEAT_DIFF
1444 && !can_fill
1445#endif
1446 )
1447 return;
1448
1449 validate_cursor(); /* w_wrow needs to be valid */
1450
1451 /*
1452 * Compute the row number of the last row of the cursor line
1453 * and make sure it doesn't go off the screen. Make sure the cursor
1454 * doesn't go past 'scrolloff' lines from the screen end.
1455 */
1456 end_row = curwin->w_wrow;
1457#ifdef FEAT_DIFF
1458 if (can_fill)
1459 ++end_row;
1460 else
1461 end_row += plines_nofill(curwin->w_topline - 1);
1462#else
1463 end_row += plines(curwin->w_topline - 1);
1464#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001465 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 validate_cheight();
1468 validate_virtcol();
1469 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001470 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472 if (end_row < curwin->w_height - p_so)
1473 {
1474#ifdef FEAT_DIFF
1475 if (can_fill)
1476 {
1477 ++curwin->w_topfill;
1478 check_topfill(curwin, TRUE);
1479 }
1480 else
1481 {
1482 --curwin->w_topline;
1483 curwin->w_topfill = 0;
1484 }
1485#else
1486 --curwin->w_topline;
1487#endif
1488#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001489 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491 --curwin->w_botline; /* approximate w_botline */
1492 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1493 }
1494}
1495
1496/*
1497 * Scroll the screen one line up, but don't do it if it would move the cursor
1498 * off the screen.
1499 */
1500 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001501scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 int start_row;
1504
1505 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1506#ifdef FEAT_DIFF
1507 && curwin->w_topfill == 0
1508#endif
1509 )
1510 return;
1511
1512 validate_cursor(); /* w_wrow needs to be valid */
1513
1514 /*
1515 * Compute the row number of the first row of the cursor line
1516 * and make sure it doesn't go off the screen. Make sure the cursor
1517 * doesn't go before 'scrolloff' lines from the screen start.
1518 */
1519#ifdef FEAT_DIFF
1520 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1521 - curwin->w_topfill;
1522#else
1523 start_row = curwin->w_wrow - plines(curwin->w_topline);
1524#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001525 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001528 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 if (start_row >= p_so)
1531 {
1532#ifdef FEAT_DIFF
1533 if (curwin->w_topfill > 0)
1534 --curwin->w_topfill;
1535 else
1536#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001537 {
1538#ifdef FEAT_FOLDING
1539 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 ++curwin->w_botline; /* approximate w_botline */
1544 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1545 }
1546}
1547#endif /* FEAT_INS_EXPAND */
1548
1549/*
1550 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1551 * a (wrapped) text line. Uses and sets "lp->fill".
1552 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001553 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
1555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001556topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
1558#ifdef FEAT_DIFF
1559 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1560 {
1561 /* Add a filler line. */
1562 ++lp->fill;
1563 lp->height = 1;
1564 }
1565 else
1566#endif
1567 {
1568 --lp->lnum;
1569#ifdef FEAT_DIFF
1570 lp->fill = 0;
1571#endif
1572 if (lp->lnum < 1)
1573 lp->height = MAXCOL;
1574 else
1575#ifdef FEAT_FOLDING
1576 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1577 /* Add a closed fold */
1578 lp->height = 1;
1579 else
1580#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001581 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583}
1584
1585/*
1586 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1587 * a (wrapped) text line. Uses and sets "lp->fill".
1588 * Returns the height of the added line in "lp->height".
1589 * Lines below the last one are incredibly high.
1590 */
1591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001592botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594#ifdef FEAT_DIFF
1595 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1596 {
1597 /* Add a filler line. */
1598 ++lp->fill;
1599 lp->height = 1;
1600 }
1601 else
1602#endif
1603 {
1604 ++lp->lnum;
1605#ifdef FEAT_DIFF
1606 lp->fill = 0;
1607#endif
1608 if (lp->lnum > curbuf->b_ml.ml_line_count)
1609 lp->height = MAXCOL;
1610 else
1611#ifdef FEAT_FOLDING
1612 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1613 /* Add a closed fold */
1614 lp->height = 1;
1615 else
1616#endif
1617 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001618 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 }
1621}
1622
1623#ifdef FEAT_DIFF
1624/*
1625 * Switch from including filler lines below lp->lnum to including filler
1626 * lines above loff.lnum + 1. This keeps pointing to the same line.
1627 * When there are no filler lines nothing changes.
1628 */
1629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001630botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 if (lp->fill > 0)
1633 {
1634 ++lp->lnum;
1635 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1636 }
1637}
1638
1639/*
1640 * Switch from including filler lines above lp->lnum to including filler
1641 * lines below loff.lnum - 1. This keeps pointing to the same line.
1642 * When there are no filler lines nothing changes.
1643 */
1644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001645topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 if (lp->fill > 0)
1648 {
1649 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1650 --lp->lnum;
1651 }
1652}
1653#endif
1654
1655/*
1656 * Recompute topline to put the cursor at the top of the window.
1657 * Scroll at least "min_scroll" lines.
1658 * If "always" is TRUE, always set topline (for "zt").
1659 */
1660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001661scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 int scrolled = 0;
1664 int extra = 0;
1665 int used;
1666 int i;
1667 linenr_T top; /* just above displayed lines */
1668 linenr_T bot; /* just below displayed lines */
1669 linenr_T old_topline = curwin->w_topline;
1670#ifdef FEAT_DIFF
1671 linenr_T old_topfill = curwin->w_topfill;
1672#endif
1673 linenr_T new_topline;
1674 int off = p_so;
1675
1676#ifdef FEAT_MOUSE
1677 if (mouse_dragging > 0)
1678 off = mouse_dragging - 1;
1679#endif
1680
1681 /*
1682 * Decrease topline until:
1683 * - it has become 1
1684 * - (part of) the cursor line is moved off the screen or
1685 * - moved at least 'scrolljump' lines and
1686 * - at least 'scrolloff' lines above and below the cursor
1687 */
1688 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001689 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (curwin->w_cursor.lnum < curwin->w_topline)
1691 scrolled = used;
1692
1693#ifdef FEAT_FOLDING
1694 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1695 {
1696 --top;
1697 ++bot;
1698 }
1699 else
1700#endif
1701 {
1702 top = curwin->w_cursor.lnum - 1;
1703 bot = curwin->w_cursor.lnum + 1;
1704 }
1705 new_topline = top + 1;
1706
1707#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001708 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001709 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001710 * Hide filler lines above cursor line by adding them to "extra". */
1711 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#endif
1713
1714 /*
1715 * Check if the lines from "top" to "bot" fit in the window. If they do,
1716 * set new_topline and advance "top" and "bot" to include more lines.
1717 */
1718 while (top > 0)
1719 {
1720#ifdef FEAT_FOLDING
1721 if (hasFolding(top, &top, NULL))
1722 /* count one logical line for a sequence of folded lines */
1723 i = 1;
1724 else
1725#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001726 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 used += i;
1728 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1729 {
1730#ifdef FEAT_FOLDING
1731 if (hasFolding(bot, NULL, &bot))
1732 /* count one logical line for a sequence of folded lines */
1733 ++used;
1734 else
1735#endif
1736 used += plines(bot);
1737 }
1738 if (used > curwin->w_height)
1739 break;
1740 if (top < curwin->w_topline)
1741 scrolled += i;
1742
1743 /*
1744 * If scrolling is needed, scroll at least 'sj' lines.
1745 */
1746 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1747 && extra >= off)
1748 break;
1749
1750 extra += i;
1751 new_topline = top;
1752 --top;
1753 ++bot;
1754 }
1755
1756 /*
1757 * If we don't have enough space, put cursor in the middle.
1758 * This makes sure we get the same position when using "k" and "j"
1759 * in a small window.
1760 */
1761 if (used > curwin->w_height)
1762 scroll_cursor_halfway(FALSE);
1763 else
1764 {
1765 /*
1766 * If "always" is FALSE, only adjust topline to a lower value, higher
1767 * value may happen with wrapping lines
1768 */
1769 if (new_topline < curwin->w_topline || always)
1770 curwin->w_topline = new_topline;
1771 if (curwin->w_topline > curwin->w_cursor.lnum)
1772 curwin->w_topline = curwin->w_cursor.lnum;
1773#ifdef FEAT_DIFF
1774 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1775 if (curwin->w_topfill > 0 && extra > off)
1776 {
1777 curwin->w_topfill -= extra - off;
1778 if (curwin->w_topfill < 0)
1779 curwin->w_topfill = 0;
1780 }
1781 check_topfill(curwin, FALSE);
1782#endif
1783 if (curwin->w_topline != old_topline
1784#ifdef FEAT_DIFF
1785 || curwin->w_topfill != old_topfill
1786#endif
1787 )
1788 curwin->w_valid &=
1789 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1790 curwin->w_valid |= VALID_TOPLINE;
1791 }
1792}
1793
1794/*
1795 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1796 * screen lines for text lines.
1797 */
1798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801#ifdef FEAT_DIFF
1802 wp->w_filler_rows = 0;
1803#endif
1804 if (used == 0)
1805 wp->w_empty_rows = 0; /* single line that doesn't fit */
1806 else
1807 {
1808 wp->w_empty_rows = wp->w_height - used;
1809#ifdef FEAT_DIFF
1810 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1811 {
1812 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1813 if (wp->w_empty_rows > wp->w_filler_rows)
1814 wp->w_empty_rows -= wp->w_filler_rows;
1815 else
1816 {
1817 wp->w_filler_rows = wp->w_empty_rows;
1818 wp->w_empty_rows = 0;
1819 }
1820 }
1821#endif
1822 }
1823}
1824
1825/*
1826 * Recompute topline to put the cursor at the bottom of the window.
1827 * Scroll at least "min_scroll" lines.
1828 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1829 * This is messy stuff!!!
1830 */
1831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001832scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 int used;
1835 int scrolled = 0;
1836 int extra = 0;
1837 int i;
1838 linenr_T line_count;
1839 linenr_T old_topline = curwin->w_topline;
1840 lineoff_T loff;
1841 lineoff_T boff;
1842#ifdef FEAT_DIFF
1843 int old_topfill = curwin->w_topfill;
1844 int fill_below_window;
1845#endif
1846 linenr_T old_botline = curwin->w_botline;
1847 linenr_T old_valid = curwin->w_valid;
1848 int old_empty_rows = curwin->w_empty_rows;
1849 linenr_T cln; /* Cursor Line Number */
1850
1851 cln = curwin->w_cursor.lnum;
1852 if (set_topbot)
1853 {
1854 used = 0;
1855 curwin->w_botline = cln + 1;
1856#ifdef FEAT_DIFF
1857 loff.fill = 0;
1858#endif
1859 for (curwin->w_topline = curwin->w_botline;
1860 curwin->w_topline > 1;
1861 curwin->w_topline = loff.lnum)
1862 {
1863 loff.lnum = curwin->w_topline;
1864 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001865 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 break;
1867 used += loff.height;
1868#ifdef FEAT_DIFF
1869 curwin->w_topfill = loff.fill;
1870#endif
1871 }
1872 set_empty_rows(curwin, used);
1873 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1874 if (curwin->w_topline != old_topline
1875#ifdef FEAT_DIFF
1876 || curwin->w_topfill != old_topfill
1877#endif
1878 )
1879 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1880 }
1881 else
1882 validate_botline();
1883
1884 /* The lines of the cursor line itself are always used. */
1885#ifdef FEAT_DIFF
1886 used = plines_nofill(cln);
1887#else
1888 validate_cheight();
1889 used = curwin->w_cline_height;
1890#endif
1891
1892 /* If the cursor is below botline, we will at least scroll by the height
1893 * of the cursor line. Correct for empty lines, which are really part of
1894 * botline. */
1895 if (cln >= curwin->w_botline)
1896 {
1897 scrolled = used;
1898 if (cln == curwin->w_botline)
1899 scrolled -= curwin->w_empty_rows;
1900 }
1901
1902 /*
1903 * Stop counting lines to scroll when
1904 * - hitting start of the file
1905 * - scrolled nothing or at least 'sj' lines
1906 * - at least 'so' lines below the cursor
1907 * - lines between botline and cursor have been counted
1908 */
1909#ifdef FEAT_FOLDING
1910 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1911#endif
1912 {
1913 loff.lnum = cln;
1914 boff.lnum = cln;
1915 }
1916#ifdef FEAT_DIFF
1917 loff.fill = 0;
1918 boff.fill = 0;
1919 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1920 - curwin->w_filler_rows;
1921#endif
1922
1923 while (loff.lnum > 1)
1924 {
1925 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1926 * context for 'scrolloff' and counted all lines below the window. */
1927 if ((((scrolled <= 0 || scrolled >= min_scroll)
1928 && extra >= (
1929#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001930 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#endif
1932 p_so))
1933 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1934 && loff.lnum <= curwin->w_botline
1935#ifdef FEAT_DIFF
1936 && (loff.lnum < curwin->w_botline
1937 || loff.fill >= fill_below_window)
1938#endif
1939 )
1940 break;
1941
1942 /* Add one line above */
1943 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001944 if (loff.height == MAXCOL)
1945 used = MAXCOL;
1946 else
1947 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (used > curwin->w_height)
1949 break;
1950 if (loff.lnum >= curwin->w_botline
1951#ifdef FEAT_DIFF
1952 && (loff.lnum > curwin->w_botline
1953 || loff.fill <= fill_below_window)
1954#endif
1955 )
1956 {
1957 /* Count screen lines that are below the window. */
1958 scrolled += loff.height;
1959 if (loff.lnum == curwin->w_botline
1960#ifdef FEAT_DIFF
1961 && boff.fill == 0
1962#endif
1963 )
1964 scrolled -= curwin->w_empty_rows;
1965 }
1966
1967 if (boff.lnum < curbuf->b_ml.ml_line_count)
1968 {
1969 /* Add one line below */
1970 botline_forw(&boff);
1971 used += boff.height;
1972 if (used > curwin->w_height)
1973 break;
1974 if (extra < (
1975#ifdef FEAT_MOUSE
1976 mouse_dragging > 0 ? mouse_dragging - 1 :
1977#endif
1978 p_so) || scrolled < min_scroll)
1979 {
1980 extra += boff.height;
1981 if (boff.lnum >= curwin->w_botline
1982#ifdef FEAT_DIFF
1983 || (boff.lnum + 1 == curwin->w_botline
1984 && boff.fill > curwin->w_filler_rows)
1985#endif
1986 )
1987 {
1988 /* Count screen lines that are below the window. */
1989 scrolled += boff.height;
1990 if (boff.lnum == curwin->w_botline
1991#ifdef FEAT_DIFF
1992 && boff.fill == 0
1993#endif
1994 )
1995 scrolled -= curwin->w_empty_rows;
1996 }
1997 }
1998 }
1999 }
2000
2001 /* curwin->w_empty_rows is larger, no need to scroll */
2002 if (scrolled <= 0)
2003 line_count = 0;
2004 /* more than a screenfull, don't scroll but redraw */
2005 else if (used > curwin->w_height)
2006 line_count = used;
2007 /* scroll minimal number of lines */
2008 else
2009 {
2010 line_count = 0;
2011#ifdef FEAT_DIFF
2012 boff.fill = curwin->w_topfill;
2013#endif
2014 boff.lnum = curwin->w_topline - 1;
2015 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2016 {
2017 botline_forw(&boff);
2018 i += boff.height;
2019 ++line_count;
2020 }
2021 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2022 line_count = 9999;
2023 }
2024
2025 /*
2026 * Scroll up if the cursor is off the bottom of the screen a bit.
2027 * Otherwise put it at 1/2 of the screen.
2028 */
2029 if (line_count >= curwin->w_height && line_count > min_scroll)
2030 scroll_cursor_halfway(FALSE);
2031 else
2032 scrollup(line_count, TRUE);
2033
2034 /*
2035 * If topline didn't change we need to restore w_botline and w_empty_rows
2036 * (we changed them).
2037 * If topline did change, update_screen() will set botline.
2038 */
2039 if (curwin->w_topline == old_topline && set_topbot)
2040 {
2041 curwin->w_botline = old_botline;
2042 curwin->w_empty_rows = old_empty_rows;
2043 curwin->w_valid = old_valid;
2044 }
2045 curwin->w_valid |= VALID_TOPLINE;
2046}
2047
2048/*
2049 * Recompute topline to put the cursor halfway the window
2050 * If "atend" is TRUE, also put it halfway at the end of the file.
2051 */
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 int above = 0;
2056 linenr_T topline;
2057#ifdef FEAT_DIFF
2058 int topfill = 0;
2059#endif
2060 int below = 0;
2061 int used;
2062 lineoff_T loff;
2063 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002064#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002065 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2069#ifdef FEAT_FOLDING
2070 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2071#endif
2072#ifdef FEAT_DIFF
2073 used = plines_nofill(loff.lnum);
2074 loff.fill = 0;
2075 boff.fill = 0;
2076#else
2077 used = plines(loff.lnum);
2078#endif
2079 topline = loff.lnum;
2080 while (topline > 1)
2081 {
2082 if (below <= above) /* add a line below the cursor first */
2083 {
2084 if (boff.lnum < curbuf->b_ml.ml_line_count)
2085 {
2086 botline_forw(&boff);
2087 used += boff.height;
2088 if (used > curwin->w_height)
2089 break;
2090 below += boff.height;
2091 }
2092 else
2093 {
2094 ++below; /* count a "~" line */
2095 if (atend)
2096 ++used;
2097 }
2098 }
2099
2100 if (below > above) /* add a line above the cursor */
2101 {
2102 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002103 if (loff.height == MAXCOL)
2104 used = MAXCOL;
2105 else
2106 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (used > curwin->w_height)
2108 break;
2109 above += loff.height;
2110 topline = loff.lnum;
2111#ifdef FEAT_DIFF
2112 topfill = loff.fill;
2113#endif
2114 }
2115 }
2116#ifdef FEAT_FOLDING
2117 if (!hasFolding(topline, &curwin->w_topline, NULL))
2118#endif
2119 curwin->w_topline = topline;
2120#ifdef FEAT_DIFF
2121 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002122 if (old_topline > curwin->w_topline + curwin->w_height)
2123 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 check_topfill(curwin, FALSE);
2125#endif
2126 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2127 curwin->w_valid |= VALID_TOPLINE;
2128}
2129
2130/*
2131 * Correct the cursor position so that it is in a part of the screen at least
2132 * 'so' lines from the top and bottom, if possible.
2133 * If not possible, put it at the same position as scroll_cursor_halfway().
2134 * When called topline must be valid!
2135 */
2136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 int above = 0; /* screen lines above topline */
2140 linenr_T topline;
2141 int below = 0; /* screen lines below botline */
2142 linenr_T botline;
2143 int above_wanted, below_wanted;
2144 linenr_T cln; /* Cursor Line Number */
2145 int max_off;
2146
2147 /*
2148 * How many lines we would like to have above/below the cursor depends on
2149 * whether the first/last line of the file is on screen.
2150 */
2151 above_wanted = p_so;
2152 below_wanted = p_so;
2153#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002154 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 above_wanted = mouse_dragging - 1;
2157 below_wanted = mouse_dragging - 1;
2158 }
2159#endif
2160 if (curwin->w_topline == 1)
2161 {
2162 above_wanted = 0;
2163 max_off = curwin->w_height / 2;
2164 if (below_wanted > max_off)
2165 below_wanted = max_off;
2166 }
2167 validate_botline();
2168 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2169#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002170 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172 )
2173 {
2174 below_wanted = 0;
2175 max_off = (curwin->w_height - 1) / 2;
2176 if (above_wanted > max_off)
2177 above_wanted = max_off;
2178 }
2179
2180 /*
2181 * If there are sufficient file-lines above and below the cursor, we can
2182 * return now.
2183 */
2184 cln = curwin->w_cursor.lnum;
2185 if (cln >= curwin->w_topline + above_wanted
2186 && cln < curwin->w_botline - below_wanted
2187#ifdef FEAT_FOLDING
2188 && !hasAnyFolding(curwin)
2189#endif
2190 )
2191 return;
2192
2193 /*
2194 * Narrow down the area where the cursor can be put by taking lines from
2195 * the top and the bottom until:
2196 * - the desired context lines are found
2197 * - the lines from the top is past the lines from the bottom
2198 */
2199 topline = curwin->w_topline;
2200 botline = curwin->w_botline - 1;
2201#ifdef FEAT_DIFF
2202 /* count filler lines as context */
2203 above = curwin->w_topfill;
2204 below = curwin->w_filler_rows;
2205#endif
2206 while ((above < above_wanted || below < below_wanted) && topline < botline)
2207 {
2208 if (below < below_wanted && (below <= above || above >= above_wanted))
2209 {
2210#ifdef FEAT_FOLDING
2211 if (hasFolding(botline, &botline, NULL))
2212 ++below;
2213 else
2214#endif
2215 below += plines(botline);
2216 --botline;
2217 }
2218 if (above < above_wanted && (above < below || below >= below_wanted))
2219 {
2220#ifdef FEAT_FOLDING
2221 if (hasFolding(topline, NULL, &topline))
2222 ++above;
2223 else
2224#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002225 above += PLINES_NOFILL(topline);
2226#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /* Count filler lines below this line as context. */
2228 if (topline < botline)
2229 above += diff_check_fill(curwin, topline + 1);
2230#endif
2231 ++topline;
2232 }
2233 }
2234 if (topline == botline || botline == 0)
2235 curwin->w_cursor.lnum = topline;
2236 else if (topline > botline)
2237 curwin->w_cursor.lnum = botline;
2238 else
2239 {
2240 if (cln < topline && curwin->w_topline > 1)
2241 {
2242 curwin->w_cursor.lnum = topline;
2243 curwin->w_valid &=
2244 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2245 }
2246 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2247 {
2248 curwin->w_cursor.lnum = botline;
2249 curwin->w_valid &=
2250 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2251 }
2252 }
2253 curwin->w_valid |= VALID_TOPLINE;
2254}
2255
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002256static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258/*
2259 * move screen 'count' pages up or down and update screen
2260 *
2261 * return FAIL for failure, OK otherwise
2262 */
2263 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 long n;
2267 int retval = OK;
2268 lineoff_T loff;
2269 linenr_T old_topline = curwin->w_topline;
2270
2271 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2272 {
2273 beep_flush();
2274 return FAIL;
2275 }
2276
2277 for ( ; count > 0; --count)
2278 {
2279 validate_botline();
2280 /*
2281 * It's an error to move a page up when the first line is already on
2282 * the screen. It's an error to move a page down when the last line
2283 * is on the screen and the topline is 'scrolloff' lines from the
2284 * last line.
2285 */
2286 if (dir == FORWARD
2287 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2288 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2289 : (curwin->w_topline == 1
2290#ifdef FEAT_DIFF
2291 && curwin->w_topfill ==
2292 diff_check_fill(curwin, curwin->w_topline)
2293#endif
2294 ))
2295 {
2296 beep_flush();
2297 retval = FAIL;
2298 break;
2299 }
2300
2301#ifdef FEAT_DIFF
2302 loff.fill = 0;
2303#endif
2304 if (dir == FORWARD)
2305 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002306 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002308 /* Vi compatible scrolling */
2309 if (p_window <= 2)
2310 ++curwin->w_topline;
2311 else
2312 curwin->w_topline += p_window - 2;
2313 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2314 curwin->w_topline = curbuf->b_ml.ml_line_count;
2315 curwin->w_cursor.lnum = curwin->w_topline;
2316 }
2317 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2318 {
2319 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 curwin->w_topline = curbuf->b_ml.ml_line_count;
2321#ifdef FEAT_DIFF
2322 curwin->w_topfill = 0;
2323#endif
2324 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2325 }
2326 else
2327 {
2328 /* For the overlap, start with the line just below the window
2329 * and go upwards. */
2330 loff.lnum = curwin->w_botline;
2331#ifdef FEAT_DIFF
2332 loff.fill = diff_check_fill(curwin, loff.lnum)
2333 - curwin->w_filler_rows;
2334#endif
2335 get_scroll_overlap(&loff, -1);
2336 curwin->w_topline = loff.lnum;
2337#ifdef FEAT_DIFF
2338 curwin->w_topfill = loff.fill;
2339 check_topfill(curwin, FALSE);
2340#endif
2341 curwin->w_cursor.lnum = curwin->w_topline;
2342 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2343 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2344 }
2345 }
2346 else /* dir == BACKWARDS */
2347 {
2348#ifdef FEAT_DIFF
2349 if (curwin->w_topline == 1)
2350 {
2351 /* Include max number of filler lines */
2352 max_topfill();
2353 continue;
2354 }
2355#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002356 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002357 {
2358 /* Vi compatible scrolling (sort of) */
2359 if (p_window <= 2)
2360 --curwin->w_topline;
2361 else
2362 curwin->w_topline -= p_window - 2;
2363 if (curwin->w_topline < 1)
2364 curwin->w_topline = 1;
2365 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2366 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2367 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2368 continue;
2369 }
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /* Find the line at the top of the window that is going to be the
2372 * line at the bottom of the window. Make sure this results in
2373 * the same line as before doing CTRL-F. */
2374 loff.lnum = curwin->w_topline - 1;
2375#ifdef FEAT_DIFF
2376 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2377 - curwin->w_topfill;
2378#endif
2379 get_scroll_overlap(&loff, 1);
2380
2381 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2382 {
2383 loff.lnum = curbuf->b_ml.ml_line_count;
2384#ifdef FEAT_DIFF
2385 loff.fill = 0;
2386 }
2387 else
2388 {
2389 botline_topline(&loff);
2390#endif
2391 }
2392 curwin->w_cursor.lnum = loff.lnum;
2393
2394 /* Find the line just above the new topline to get the right line
2395 * at the bottom of the window. */
2396 n = 0;
2397 while (n <= curwin->w_height && loff.lnum >= 1)
2398 {
2399 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002400 if (loff.height == MAXCOL)
2401 n = MAXCOL;
2402 else
2403 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002405 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 curwin->w_topline = 1;
2408#ifdef FEAT_DIFF
2409 max_topfill();
2410#endif
2411 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2412 }
2413 else
2414 {
2415 /* Go two lines forward again. */
2416#ifdef FEAT_DIFF
2417 topline_botline(&loff);
2418#endif
2419 botline_forw(&loff);
2420 botline_forw(&loff);
2421#ifdef FEAT_DIFF
2422 botline_topline(&loff);
2423#endif
2424#ifdef FEAT_FOLDING
2425 /* We're at the wrong end of a fold now. */
2426 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2427#endif
2428
2429 /* Always scroll at least one line. Avoid getting stuck on
2430 * very long lines. */
2431 if (loff.lnum >= curwin->w_topline
2432#ifdef FEAT_DIFF
2433 && (loff.lnum > curwin->w_topline
2434 || loff.fill >= curwin->w_topfill)
2435#endif
2436 )
2437 {
2438#ifdef FEAT_DIFF
2439 /* First try using the maximum number of filler lines. If
2440 * that's not enough, backup one line. */
2441 loff.fill = curwin->w_topfill;
2442 if (curwin->w_topfill < diff_check_fill(curwin,
2443 curwin->w_topline))
2444 max_topfill();
2445 if (curwin->w_topfill == loff.fill)
2446#endif
2447 {
2448 --curwin->w_topline;
2449#ifdef FEAT_DIFF
2450 curwin->w_topfill = 0;
2451#endif
2452 }
2453 comp_botline(curwin);
2454 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002455 curwin->w_valid &=
2456 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 else
2459 {
2460 curwin->w_topline = loff.lnum;
2461#ifdef FEAT_DIFF
2462 curwin->w_topfill = loff.fill;
2463 check_topfill(curwin, FALSE);
2464#endif
2465 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2466 }
2467 }
2468 }
2469 }
2470#ifdef FEAT_FOLDING
2471 foldAdjustCursor();
2472#endif
2473 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002474 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002475 if (retval == OK)
2476 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2478
Bram Moolenaar907dad72018-07-10 15:07:15 +02002479 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002481 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2482 // But make sure we scroll at least one line (happens with mix of long
2483 // wrapping lines and non-wrapping line).
2484 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002486 scroll_cursor_top(1, FALSE);
2487 if (curwin->w_topline <= old_topline
2488 && old_topline < curbuf->b_ml.ml_line_count)
2489 {
2490 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002492 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2493#endif
2494 }
2495 }
2496#ifdef FEAT_FOLDING
2497 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501
2502 redraw_later(VALID);
2503 return retval;
2504}
2505
2506/*
2507 * Decide how much overlap to use for page-up or page-down scrolling.
2508 * This is symmetric, so that doing both keeps the same lines displayed.
2509 * Three lines are examined:
2510 *
2511 * before CTRL-F after CTRL-F / before CTRL-B
2512 * etc. l1
2513 * l1 last but one line ------------
2514 * l2 last text line l2 top text line
2515 * ------------- l3 second text line
2516 * l3 etc.
2517 */
2518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002519get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 int h1, h2, h3, h4;
2522 int min_height = curwin->w_height - 2;
2523 lineoff_T loff0, loff1, loff2;
2524
2525#ifdef FEAT_DIFF
2526 if (lp->fill > 0)
2527 lp->height = 1;
2528 else
2529 lp->height = plines_nofill(lp->lnum);
2530#else
2531 lp->height = plines(lp->lnum);
2532#endif
2533 h1 = lp->height;
2534 if (h1 > min_height)
2535 return; /* no overlap */
2536
2537 loff0 = *lp;
2538 if (dir > 0)
2539 botline_forw(lp);
2540 else
2541 topline_back(lp);
2542 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002543 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 *lp = loff0; /* no overlap */
2546 return;
2547 }
2548
2549 loff1 = *lp;
2550 if (dir > 0)
2551 botline_forw(lp);
2552 else
2553 topline_back(lp);
2554 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002555 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 *lp = loff0; /* no overlap */
2558 return;
2559 }
2560
2561 loff2 = *lp;
2562 if (dir > 0)
2563 botline_forw(lp);
2564 else
2565 topline_back(lp);
2566 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002567 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 *lp = loff1; /* 1 line overlap */
2569 else
2570 *lp = loff2; /* 2 lines overlap */
2571 return;
2572}
2573
2574/* #define KEEP_SCREEN_LINE */
2575/*
2576 * Scroll 'scroll' lines up or down.
2577 */
2578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002579halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 long scrolled = 0;
2582 int i;
2583 int n;
2584 int room;
2585
2586 if (Prenum)
2587 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2588 curwin->w_height : Prenum;
2589 n = (curwin->w_p_scr <= curwin->w_height) ?
2590 curwin->w_p_scr : curwin->w_height;
2591
Bram Moolenaard5d37532017-03-27 23:02:07 +02002592 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 validate_botline();
2594 room = curwin->w_empty_rows;
2595#ifdef FEAT_DIFF
2596 room += curwin->w_filler_rows;
2597#endif
2598 if (flag)
2599 {
2600 /*
2601 * scroll the text up
2602 */
2603 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2604 {
2605#ifdef FEAT_DIFF
2606 if (curwin->w_topfill > 0)
2607 {
2608 i = 1;
2609 if (--n < 0 && scrolled > 0)
2610 break;
2611 --curwin->w_topfill;
2612 }
2613 else
2614#endif
2615 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002616 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 n -= i;
2618 if (n < 0 && scrolled > 0)
2619 break;
2620#ifdef FEAT_FOLDING
2621 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2622#endif
2623 ++curwin->w_topline;
2624#ifdef FEAT_DIFF
2625 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2626#endif
2627
2628#ifndef KEEP_SCREEN_LINE
2629 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2630 {
2631 ++curwin->w_cursor.lnum;
2632 curwin->w_valid &=
2633 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2634 }
2635#endif
2636 }
2637 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2638 scrolled += i;
2639
2640 /*
2641 * Correct w_botline for changed w_topline.
2642 * Won't work when there are filler lines.
2643 */
2644#ifdef FEAT_DIFF
2645 if (curwin->w_p_diff)
2646 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2647 else
2648#endif
2649 {
2650 room += i;
2651 do
2652 {
2653 i = plines(curwin->w_botline);
2654 if (i > room)
2655 break;
2656#ifdef FEAT_FOLDING
2657 (void)hasFolding(curwin->w_botline, NULL,
2658 &curwin->w_botline);
2659#endif
2660 ++curwin->w_botline;
2661 room -= i;
2662 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2663 }
2664 }
2665
2666#ifndef KEEP_SCREEN_LINE
2667 /*
2668 * When hit bottom of the file: move cursor down.
2669 */
2670 if (n > 0)
2671 {
2672# ifdef FEAT_FOLDING
2673 if (hasAnyFolding(curwin))
2674 {
2675 while (--n >= 0
2676 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2677 {
2678 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2679 &curwin->w_cursor.lnum);
2680 ++curwin->w_cursor.lnum;
2681 }
2682 }
2683 else
2684# endif
2685 curwin->w_cursor.lnum += n;
2686 check_cursor_lnum();
2687 }
2688#else
2689 /* try to put the cursor in the same screen line */
2690 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2691 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2692 {
2693 scrolled -= plines(curwin->w_cursor.lnum);
2694 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2695 break;
2696# ifdef FEAT_FOLDING
2697 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2698 &curwin->w_cursor.lnum);
2699# endif
2700 ++curwin->w_cursor.lnum;
2701 }
2702#endif
2703 }
2704 else
2705 {
2706 /*
2707 * scroll the text down
2708 */
2709 while (n > 0 && curwin->w_topline > 1)
2710 {
2711#ifdef FEAT_DIFF
2712 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2713 {
2714 i = 1;
2715 if (--n < 0 && scrolled > 0)
2716 break;
2717 ++curwin->w_topfill;
2718 }
2719 else
2720#endif
2721 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002722 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 n -= i;
2724 if (n < 0 && scrolled > 0)
2725 break;
2726 --curwin->w_topline;
2727#ifdef FEAT_FOLDING
2728 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2729#endif
2730#ifdef FEAT_DIFF
2731 curwin->w_topfill = 0;
2732#endif
2733 }
2734 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2735 VALID_BOTLINE|VALID_BOTLINE_AP);
2736 scrolled += i;
2737#ifndef KEEP_SCREEN_LINE
2738 if (curwin->w_cursor.lnum > 1)
2739 {
2740 --curwin->w_cursor.lnum;
2741 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2742 }
2743#endif
2744 }
2745#ifndef KEEP_SCREEN_LINE
2746 /*
2747 * When hit top of the file: move cursor up.
2748 */
2749 if (n > 0)
2750 {
2751 if (curwin->w_cursor.lnum <= (linenr_T)n)
2752 curwin->w_cursor.lnum = 1;
2753 else
2754# ifdef FEAT_FOLDING
2755 if (hasAnyFolding(curwin))
2756 {
2757 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2758 {
2759 --curwin->w_cursor.lnum;
2760 (void)hasFolding(curwin->w_cursor.lnum,
2761 &curwin->w_cursor.lnum, NULL);
2762 }
2763 }
2764 else
2765# endif
2766 curwin->w_cursor.lnum -= n;
2767 }
2768#else
2769 /* try to put the cursor in the same screen line */
2770 scrolled += n; /* move cursor when topline is 1 */
2771 while (curwin->w_cursor.lnum > curwin->w_topline
2772 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2773 {
2774 scrolled -= plines(curwin->w_cursor.lnum - 1);
2775 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2776 break;
2777 --curwin->w_cursor.lnum;
2778# ifdef FEAT_FOLDING
2779 foldAdjustCursor();
2780# endif
2781 }
2782#endif
2783 }
2784# ifdef FEAT_FOLDING
2785 /* Move cursor to first line of closed fold. */
2786 foldAdjustCursor();
2787# endif
2788#ifdef FEAT_DIFF
2789 check_topfill(curwin, !flag);
2790#endif
2791 cursor_correct();
2792 beginline(BL_SOL | BL_FIX);
2793 redraw_later(VALID);
2794}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002795
Bram Moolenaar860cae12010-06-05 23:22:07 +02002796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002797do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002798{
2799 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002800 colnr_T col = curwin->w_cursor.col;
2801# ifdef FEAT_VIRTUALEDIT
2802 colnr_T coladd = curwin->w_cursor.coladd;
2803# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002804 colnr_T curswant = curwin->w_curswant;
2805 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002806 win_T *old_curwin = curwin;
2807 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002808 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002809 int old_VIsual_select = VIsual_select;
2810 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002811
2812 /*
2813 * loop through the cursorbound windows
2814 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002815 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002816 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817 {
2818 curbuf = curwin->w_buffer;
2819 /* skip original window and windows with 'noscrollbind' */
2820 if (curwin != old_curwin && curwin->w_p_crb)
2821 {
2822# ifdef FEAT_DIFF
2823 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002824 curwin->w_cursor.lnum =
2825 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002826 else
2827# endif
2828 curwin->w_cursor.lnum = line;
2829 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002830# ifdef FEAT_VIRTUALEDIT
2831 curwin->w_cursor.coladd = coladd;
2832# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002833 curwin->w_curswant = curswant;
2834 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002835
Bram Moolenaar61452852011-02-01 18:01:11 +01002836 /* Make sure the cursor is in a valid position. Temporarily set
2837 * "restart_edit" to allow the cursor to be beyond the EOL. */
2838 restart_edit_save = restart_edit;
2839 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002841# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002842 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002843 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002844# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002845 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846# ifdef FEAT_MBYTE
2847 /* Correct cursor for multi-byte character. */
2848 if (has_mbyte)
2849 mb_adjust_cursor();
2850# endif
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}