blob: e9a3eb971e0095e82716eaf45678523fd45c58fa [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 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 if (wp->w_p_rnu)
149 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200150 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200151#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200152 if (wp->w_p_cul)
153 {
154 if (wp->w_redr_type <= VALID && last_cursorline != 0)
155 {
156 // "last_cursorline" may be set for another window, worst case
157 // we redraw too much. This is optimized for moving the cursor
158 // around in the same window.
159 redrawWinline(wp, last_cursorline, FALSE);
160 redrawWinline(wp, wp->w_cursor.lnum, FALSE);
161 redraw_win_later(wp, VALID);
162 }
163 else
164 redraw_win_later(wp, SOME_VALID);
165 last_cursorline = wp->w_cursor.lnum;
166 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200167#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200168 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169}
170
171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 * Update curwin->w_topline and redraw if necessary.
173 * Used to update the screen before printing a message.
174 */
175 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100176update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
178 update_topline();
179 if (must_redraw)
180 update_screen(0);
181}
182
183/*
184 * Update curwin->w_topline to move the cursor onto the screen.
185 */
186 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100187update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
189 long line_count;
190 int halfheight;
191 int n;
192 linenr_T old_topline;
193#ifdef FEAT_DIFF
194 int old_topfill;
195#endif
196#ifdef FEAT_FOLDING
197 linenr_T lnum;
198#endif
199 int check_topline = FALSE;
200 int check_botline = FALSE;
201#ifdef FEAT_MOUSE
202 int save_so = p_so;
203#endif
204
Bram Moolenaard5d37532017-03-27 23:02:07 +0200205 /* If there is no valid screen and when the window height is zero just use
206 * the cursor line. */
207 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200208 {
209 curwin->w_topline = curwin->w_cursor.lnum;
210 curwin->w_botline = curwin->w_topline;
211 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200212 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200213 return;
214 }
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 check_cursor_moved(curwin);
217 if (curwin->w_valid & VALID_TOPLINE)
218 return;
219
220#ifdef FEAT_MOUSE
221 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000222 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 p_so = mouse_dragging - 1;
224#endif
225
226 old_topline = curwin->w_topline;
227#ifdef FEAT_DIFF
228 old_topfill = curwin->w_topfill;
229#endif
230
231 /*
232 * If the buffer is empty, always set topline to 1.
233 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100234 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
236 if (curwin->w_topline != 1)
237 redraw_later(NOT_VALID);
238 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 curwin->w_botline = 2;
240 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 }
243
244 /*
245 * If the cursor is above or near the top of the window, scroll the window
246 * to show the line the cursor is in, with 'scrolloff' context.
247 */
248 else
249 {
250 if (curwin->w_topline > 1)
251 {
252 /* If the cursor is above topline, scrolling is always needed.
253 * If the cursor is far below topline and there is no folding,
254 * scrolling down is never needed. */
255 if (curwin->w_cursor.lnum < curwin->w_topline)
256 check_topline = TRUE;
257 else if (check_top_offset())
258 check_topline = TRUE;
259 }
260#ifdef FEAT_DIFF
261 /* Check if there are more filler lines than allowed. */
262 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
263 curwin->w_topline))
264 check_topline = TRUE;
265#endif
266
267 if (check_topline)
268 {
269 halfheight = curwin->w_height / 2 - 1;
270 if (halfheight < 2)
271 halfheight = 2;
272
273#ifdef FEAT_FOLDING
274 if (hasAnyFolding(curwin))
275 {
276 /* Count the number of logical lines between the cursor and
277 * topline + p_so (approximation of how much will be
278 * scrolled). */
279 n = 0;
280 for (lnum = curwin->w_cursor.lnum;
281 lnum < curwin->w_topline + p_so; ++lnum)
282 {
283 ++n;
284 /* stop at end of file or when we know we are far off */
285 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
286 break;
287 (void)hasFolding(lnum, NULL, &lnum);
288 }
289 }
290 else
291#endif
292 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
293
294 /* If we weren't very close to begin with, we scroll to put the
295 * cursor in the middle of the window. Otherwise put the cursor
296 * near the top of the window. */
297 if (n >= halfheight)
298 scroll_cursor_halfway(FALSE);
299 else
300 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000301 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 check_botline = TRUE;
303 }
304 }
305
306 else
307 {
308#ifdef FEAT_FOLDING
309 /* Make sure topline is the first line of a fold. */
310 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
311#endif
312 check_botline = TRUE;
313 }
314 }
315
316 /*
317 * If the cursor is below the bottom of the window, scroll the window
318 * to put the cursor on the window.
319 * When w_botline is invalid, recompute it first, to avoid a redraw later.
320 * If w_botline was approximated, we might need a redraw later in a few
321 * cases, but we don't want to spend (a lot of) time recomputing w_botline
322 * for every small change.
323 */
324 if (check_botline)
325 {
326 if (!(curwin->w_valid & VALID_BOTLINE_AP))
327 validate_botline();
328
329 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
330 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000331 if (curwin->w_cursor.lnum < curwin->w_botline)
332 {
333 if (((long)curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 >= (long)curwin->w_botline - p_so
335#ifdef FEAT_FOLDING
336 || hasAnyFolding(curwin)
337#endif
338 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 lineoff_T loff;
341
Bram Moolenaard4153d42008-11-15 15:06:17 +0000342 /* Cursor is (a few lines) above botline, check if there are
343 * 'scrolloff' window lines below the cursor. If not, need to
344 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 n = curwin->w_empty_rows;
346 loff.lnum = curwin->w_cursor.lnum;
347#ifdef FEAT_FOLDING
348 /* In a fold go to its last line. */
349 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
350#endif
351#ifdef FEAT_DIFF
352 loff.fill = 0;
353 n += curwin->w_filler_rows;
354#endif
355 loff.height = 0;
356 while (loff.lnum < curwin->w_botline
357#ifdef FEAT_DIFF
358 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
359#endif
360 )
361 {
362 n += loff.height;
363 if (n >= p_so)
364 break;
365 botline_forw(&loff);
366 }
367 if (n >= p_so)
368 /* sufficient context, no need to scroll */
369 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000370 }
371 else
372 /* sufficient context, no need to scroll */
373 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 if (check_botline)
376 {
377#ifdef FEAT_FOLDING
378 if (hasAnyFolding(curwin))
379 {
380 /* Count the number of logical lines between the cursor and
381 * botline - p_so (approximation of how much will be
382 * scrolled). */
383 line_count = 0;
384 for (lnum = curwin->w_cursor.lnum;
385 lnum >= curwin->w_botline - p_so; --lnum)
386 {
387 ++line_count;
388 /* stop at end of file or when we know we are far off */
389 if (lnum <= 0 || line_count > curwin->w_height + 1)
390 break;
391 (void)hasFolding(lnum, &lnum, NULL);
392 }
393 }
394 else
395#endif
396 line_count = curwin->w_cursor.lnum - curwin->w_botline
397 + 1 + p_so;
398 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 else
401 scroll_cursor_halfway(FALSE);
402 }
403 }
404 }
405 curwin->w_valid |= VALID_TOPLINE;
406
407 /*
408 * Need to redraw when topline changed.
409 */
410 if (curwin->w_topline != old_topline
411#ifdef FEAT_DIFF
412 || curwin->w_topfill != old_topfill
413#endif
414 )
415 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100416 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000417 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
419 curwin->w_skipcol = 0;
420 redraw_later(NOT_VALID);
421 }
422 else
423 redraw_later(VALID);
424 /* May need to set w_skipcol when cursor in w_topline. */
425 if (curwin->w_cursor.lnum == curwin->w_topline)
426 validate_cursor();
427 }
428
429#ifdef FEAT_MOUSE
430 p_so = save_so;
431#endif
432}
433
434/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000435 * Return the scrolljump value to use for the current window.
436 * When 'scrolljump' is positive use it as-is.
437 * When 'scrolljump' is negative use it as a percentage of the window height.
438 */
439 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000441{
442 if (p_sj >= 0)
443 return (int)p_sj;
444 return (curwin->w_height * -p_sj) / 100;
445}
446
447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
449 * current window.
450 */
451 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100452check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 lineoff_T loff;
455 int n;
456
457 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
458#ifdef FEAT_FOLDING
459 || hasAnyFolding(curwin)
460#endif
461 )
462 {
463 loff.lnum = curwin->w_cursor.lnum;
464#ifdef FEAT_DIFF
465 loff.fill = 0;
466 n = curwin->w_topfill; /* always have this context */
467#else
468 n = 0;
469#endif
470 /* Count the visible screen lines above the cursor line. */
471 while (n < p_so)
472 {
473 topline_back(&loff);
474 /* Stop when included a line above the window. */
475 if (loff.lnum < curwin->w_topline
476#ifdef FEAT_DIFF
477 || (loff.lnum == curwin->w_topline && loff.fill > 0)
478#endif
479 )
480 break;
481 n += loff.height;
482 }
483 if (n < p_so)
484 return TRUE;
485 }
486 return FALSE;
487}
488
489 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100490update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 if (curwin->w_set_curswant)
493 {
494 validate_virtcol();
495 curwin->w_curswant = curwin->w_virtcol;
496 curwin->w_set_curswant = FALSE;
497 }
498}
499
500/*
501 * Check if the cursor has moved. Set the w_valid flag accordingly.
502 */
503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100504check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505{
506 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
507 {
508 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
509 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
510 wp->w_valid_cursor = wp->w_cursor;
511 wp->w_valid_leftcol = wp->w_leftcol;
512 }
513 else if (wp->w_cursor.col != wp->w_valid_cursor.col
514 || wp->w_leftcol != wp->w_valid_leftcol
515#ifdef FEAT_VIRTUALEDIT
516 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
517#endif
518 )
519 {
520 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
521 wp->w_valid_cursor.col = wp->w_cursor.col;
522 wp->w_valid_leftcol = wp->w_leftcol;
523#ifdef FEAT_VIRTUALEDIT
524 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
525#endif
526 }
527}
528
529/*
530 * Call this function when some window settings have changed, which require
531 * the cursor position, botline and topline to be recomputed and the window to
532 * be redrawn. E.g, when changing the 'wrap' option or folding.
533 */
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 changed_window_setting_win(curwin);
538}
539
540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100541changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 wp->w_lines_valid = 0;
544 changed_line_abv_curs_win(wp);
545 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
546 redraw_win_later(wp, NOT_VALID);
547}
548
549/*
550 * Set wp->w_topline to a certain number.
551 */
552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100553set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555#ifdef FEAT_FOLDING
556 /* go to first of folded lines */
557 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
558#endif
559 /* Approximate the value of w_botline */
560 wp->w_botline += lnum - wp->w_topline;
561 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000562 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_DIFF
564 wp->w_topfill = 0;
565#endif
566 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
567 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
568 redraw_later(VALID);
569}
570
571/*
572 * Call this function when the length of the cursor line (in screen
573 * characters) has changed, and the change is before the cursor.
574 * Need to take care of w_botline separately!
575 */
576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100577changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
579 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
580 |VALID_CHEIGHT|VALID_TOPLINE);
581}
582
583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100584changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
587 |VALID_CHEIGHT|VALID_TOPLINE);
588}
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
591 * Call this function when the length of a line (in screen characters) above
592 * the cursor have changed.
593 * Need to take care of w_botline separately!
594 */
595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100596changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
599 |VALID_CHEIGHT|VALID_TOPLINE);
600}
601
602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
606 |VALID_CHEIGHT|VALID_TOPLINE);
607}
608
609/*
610 * Make sure the value of curwin->w_botline is valid.
611 */
612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100613validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 if (!(curwin->w_valid & VALID_BOTLINE))
616 comp_botline(curwin);
617}
618
619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * Mark curwin->w_botline as invalid (because of some change in the buffer).
621 */
622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100623invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
626}
627
628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100635approximate_botline_win(
636 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 wp->w_valid &= ~VALID_BOTLINE;
639}
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
642 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
643 */
644 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100645cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
647 check_cursor_moved(curwin);
648 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
649 (VALID_WROW|VALID_WCOL));
650}
651
652/*
653 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
654 * w_topline must be valid, you may need to call update_topline() first!
655 */
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 check_cursor_moved(curwin);
660 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
661 curs_columns(TRUE);
662}
663
664#if defined(FEAT_GUI) || defined(PROTO)
665/*
666 * validate w_cline_row.
667 */
668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 /*
672 * First make sure that w_topline is valid (after moving the cursor).
673 */
674 update_topline();
675 check_cursor_moved(curwin);
676 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100677 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678}
679#endif
680
681/*
682 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200683 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 linenr_T lnum;
689 int i;
690 int all_invalid;
691 int valid;
692#ifdef FEAT_FOLDING
693 long fold_count;
694#endif
695
696 /* Check if wp->w_lines[].wl_size is invalid */
697 all_invalid = (!redrawing()
698 || wp->w_lines_valid == 0
699 || wp->w_lines[0].wl_lnum > wp->w_topline);
700 i = 0;
701 wp->w_cline_row = 0;
702 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
703 {
704 valid = FALSE;
705 if (!all_invalid && i < wp->w_lines_valid)
706 {
707 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
708 continue; /* skip changed or deleted lines */
709 if (wp->w_lines[i].wl_lnum == lnum)
710 {
711#ifdef FEAT_FOLDING
712 /* Check for newly inserted lines below this row, in which
713 * case we need to check for folded lines. */
714 if (!wp->w_buffer->b_mod_set
715 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
716 || wp->w_buffer->b_mod_top
717 > wp->w_lines[i].wl_lastlnum + 1)
718#endif
719 valid = TRUE;
720 }
721 else if (wp->w_lines[i].wl_lnum > lnum)
722 --i; /* hold at inserted lines */
723 }
724 if (valid
725#ifdef FEAT_DIFF
726 && (lnum != wp->w_topline || !wp->w_p_diff)
727#endif
728 )
729 {
730#ifdef FEAT_FOLDING
731 lnum = wp->w_lines[i].wl_lastlnum + 1;
732 /* Cursor inside folded lines, don't count this row */
733 if (lnum > wp->w_cursor.lnum)
734 break;
735#else
736 ++lnum;
737#endif
738 wp->w_cline_row += wp->w_lines[i].wl_size;
739 }
740 else
741 {
742#ifdef FEAT_FOLDING
743 fold_count = foldedCount(wp, lnum, NULL);
744 if (fold_count)
745 {
746 lnum += fold_count;
747 if (lnum > wp->w_cursor.lnum)
748 break;
749 ++wp->w_cline_row;
750 }
751 else
752#endif
753#ifdef FEAT_DIFF
754 if (lnum == wp->w_topline)
755 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
756 + wp->w_topfill;
757 else
758#endif
759 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
760 }
761 }
762
763 check_cursor_moved(wp);
764 if (!(wp->w_valid & VALID_CHEIGHT))
765 {
766 if (all_invalid
767 || i == wp->w_lines_valid
768 || (i < wp->w_lines_valid
769 && (!wp->w_lines[i].wl_valid
770 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
771 {
772#ifdef FEAT_DIFF
773 if (wp->w_cursor.lnum == wp->w_topline)
774 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
775 TRUE) + wp->w_topfill;
776 else
777#endif
778 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
779#ifdef FEAT_FOLDING
780 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
781 NULL, NULL, TRUE, NULL);
782#endif
783 }
784 else if (i > wp->w_lines_valid)
785 {
786 /* a line that is too long to fit on the last screen line */
787 wp->w_cline_height = 0;
788#ifdef FEAT_FOLDING
789 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
790 NULL, NULL, TRUE, NULL);
791#endif
792 }
793 else
794 {
795 wp->w_cline_height = wp->w_lines[i].wl_size;
796#ifdef FEAT_FOLDING
797 wp->w_cline_folded = wp->w_lines[i].wl_folded;
798#endif
799 }
800 }
801
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100802 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Validate curwin->w_virtcol only.
809 */
810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 validate_virtcol_win(curwin);
814}
815
816/*
817 * Validate wp->w_virtcol only.
818 */
819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 check_cursor_moved(wp);
823 if (!(wp->w_valid & VALID_VIRTCOL))
824 {
825 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
826 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000827#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000828 if (wp->w_p_cuc
829# ifdef FEAT_INS_EXPAND
830 && !pum_visible()
831# endif
832 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000833 redraw_win_later(wp, SOME_VALID);
834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836}
837
838/*
839 * Validate curwin->w_cline_height only.
840 */
841 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100842validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 check_cursor_moved(curwin);
845 if (!(curwin->w_valid & VALID_CHEIGHT))
846 {
847#ifdef FEAT_DIFF
848 if (curwin->w_cursor.lnum == curwin->w_topline)
849 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
850 + curwin->w_topfill;
851 else
852#endif
853 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
854#ifdef FEAT_FOLDING
855 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
856#endif
857 curwin->w_valid |= VALID_CHEIGHT;
858 }
859}
860
861/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000862 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 */
864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100865validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867 colnr_T off;
868 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100869 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
871 validate_virtcol();
872 if (!(curwin->w_valid & VALID_WCOL))
873 {
874 col = curwin->w_virtcol;
875 off = curwin_col_off();
876 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200877 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000880 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200881 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100882 && width > 0)
883 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200884 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000885 if (col > (int)curwin->w_leftcol)
886 col -= curwin->w_leftcol;
887 else
888 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000890
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 curwin->w_valid |= VALID_WCOL;
892 }
893}
894
895/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200896 * Compute offset of a window, occupied by absolute or relative line number,
897 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 */
899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100900win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
Bram Moolenaar64486672010-05-16 15:46:46 +0200902 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#ifdef FEAT_CMDWIN
904 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
905#endif
906#ifdef FEAT_FOLDING
907 + wp->w_p_fdc
908#endif
909#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200910 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
912 );
913}
914
915 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100916curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 return win_col_off(curwin);
919}
920
921/*
922 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200923 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
924 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100927win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
Bram Moolenaar64486672010-05-16 15:46:46 +0200929 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000930 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 return 0;
932}
933
934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100935curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 return win_col_off2(curwin);
938}
939
940/*
941 * compute curwin->w_wcol and curwin->w_virtcol.
942 * Also updates curwin->w_wrow and curwin->w_cline_row.
943 * Also updates curwin->w_leftcol.
944 */
945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100946curs_columns(
947 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 int diff;
950 int extra; /* offset for first screen line */
951 int off_left, off_right;
952 int n;
953 int p_lines;
954 int width = 0;
955 int textwidth;
956 int new_leftcol;
957 colnr_T startcol;
958 colnr_T endcol;
959 colnr_T prev_skipcol;
960
961 /*
962 * First make sure that w_topline is valid (after moving the cursor).
963 */
964 update_topline();
965
966 /*
967 * Next make sure that w_cline_row is valid.
968 */
969 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100970 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * Compute the number of virtual columns.
974 */
975#ifdef FEAT_FOLDING
976 if (curwin->w_cline_folded)
977 /* In a folded line the cursor is always in the first column */
978 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
979 else
980#endif
981 getvvcol(curwin, &curwin->w_cursor,
982 &startcol, &(curwin->w_virtcol), &endcol);
983
984 /* remove '$' from change command when cursor moves onto it */
985 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100986 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 extra = curwin_col_off();
989 curwin->w_wcol = curwin->w_virtcol + extra;
990 endcol += extra;
991
992 /*
993 * Now compute w_wrow, counting screen lines from w_cline_row.
994 */
995 curwin->w_wrow = curwin->w_cline_row;
996
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (textwidth <= 0)
999 {
1000 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02001001 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 curwin->w_wrow = curwin->w_height - 1;
1003 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001004 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
1006 width = textwidth + curwin_col_off2();
1007
1008 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +02001009 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001011 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +02001012 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 curwin->w_wcol -= n * width;
1014 curwin->w_wrow += n;
1015
1016#ifdef FEAT_LINEBREAK
1017 /* When cursor wraps to first char of next line in Insert
1018 * mode, the 'showbreak' string isn't shown, backup to first
1019 * column */
1020 if (*p_sbr && *ml_get_cursor() == NUL
1021 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1022 curwin->w_wcol = 0;
1023#endif
1024 }
1025 }
1026
1027 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1028 * is not folded.
1029 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001030 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#ifdef FEAT_FOLDING
1032 && !curwin->w_cline_folded
1033#endif
1034 )
1035 {
1036 /*
1037 * If Cursor is left of the screen, scroll rightwards.
1038 * If Cursor is right of the screen, scroll leftwards
1039 * If we get closer to the edge than 'sidescrolloff', scroll a little
1040 * extra
1041 */
1042 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001043 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 - p_siso) + 1;
1045 if (off_left < 0 || off_right > 0)
1046 {
1047 if (off_left < 0)
1048 diff = -off_left;
1049 else
1050 diff = off_right;
1051
1052 /* When far off or not enough room on either side, put cursor in
1053 * middle of window. */
1054 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1055 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1056 else
1057 {
1058 if (diff < p_ss)
1059 diff = p_ss;
1060 if (off_left < 0)
1061 new_leftcol = curwin->w_leftcol - diff;
1062 else
1063 new_leftcol = curwin->w_leftcol + diff;
1064 }
1065 if (new_leftcol < 0)
1066 new_leftcol = 0;
1067 if (new_leftcol != (int)curwin->w_leftcol)
1068 {
1069 curwin->w_leftcol = new_leftcol;
1070 /* screen has to be redrawn with new curwin->w_leftcol */
1071 redraw_later(NOT_VALID);
1072 }
1073 }
1074 curwin->w_wcol -= curwin->w_leftcol;
1075 }
1076 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1077 curwin->w_wcol -= curwin->w_leftcol;
1078 else
1079 curwin->w_wcol = 0;
1080
1081#ifdef FEAT_DIFF
1082 /* Skip over filler lines. At the top use w_topfill, there
1083 * may be some filler lines above the window. */
1084 if (curwin->w_cursor.lnum == curwin->w_topline)
1085 curwin->w_wrow += curwin->w_topfill;
1086 else
1087 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1088#endif
1089
1090 prev_skipcol = curwin->w_skipcol;
1091
1092 p_lines = 0;
1093 if ((curwin->w_wrow >= curwin->w_height
1094 || ((prev_skipcol > 0
1095 || curwin->w_wrow + p_so >= curwin->w_height)
1096 && (p_lines =
1097#ifdef FEAT_DIFF
1098 plines_win_nofill
1099#else
1100 plines_win
1101#endif
1102 (curwin, curwin->w_cursor.lnum, FALSE))
1103 - 1 >= curwin->w_height))
1104 && curwin->w_height != 0
1105 && curwin->w_cursor.lnum == curwin->w_topline
1106 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001107 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109 /* Cursor past end of screen. Happens with a single line that does
1110 * not fit on screen. Find a skipcol to show the text around the
1111 * cursor. Avoid scrolling all the time. compute value of "extra":
1112 * 1: Less than "p_so" lines above
1113 * 2: Less than "p_so" lines below
1114 * 3: both of them */
1115 extra = 0;
1116 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1117 extra = 1;
1118 /* Compute last display line of the buffer line that we want at the
1119 * bottom of the window. */
1120 if (p_lines == 0)
1121 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1122 --p_lines;
1123 if (p_lines > curwin->w_wrow + p_so)
1124 n = curwin->w_wrow + p_so;
1125 else
1126 n = p_lines;
1127 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1128 extra += 2;
1129
1130 if (extra == 3 || p_lines < p_so * 2)
1131 {
1132 /* not enough room for 'scrolloff', put cursor in the middle */
1133 n = curwin->w_virtcol / width;
1134 if (n > curwin->w_height / 2)
1135 n -= curwin->w_height / 2;
1136 else
1137 n = 0;
1138 /* don't skip more than necessary */
1139 if (n > p_lines - curwin->w_height + 1)
1140 n = p_lines - curwin->w_height + 1;
1141 curwin->w_skipcol = n * width;
1142 }
1143 else if (extra == 1)
1144 {
1145 /* less then 'scrolloff' lines above, decrease skipcol */
1146 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1147 + width - 1) / width;
1148 if (extra > 0)
1149 {
1150 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1151 extra = curwin->w_skipcol / width;
1152 curwin->w_skipcol -= extra * width;
1153 }
1154 }
1155 else if (extra == 2)
1156 {
1157 /* less then 'scrolloff' lines below, increase skipcol */
1158 endcol = (n - curwin->w_height + 1) * width;
1159 while (endcol > curwin->w_virtcol)
1160 endcol -= width;
1161 if (endcol > curwin->w_skipcol)
1162 curwin->w_skipcol = endcol;
1163 }
1164
1165 curwin->w_wrow -= curwin->w_skipcol / width;
1166 if (curwin->w_wrow >= curwin->w_height)
1167 {
1168 /* small window, make sure cursor is in it */
1169 extra = curwin->w_wrow - curwin->w_height + 1;
1170 curwin->w_skipcol += extra * width;
1171 curwin->w_wrow -= extra;
1172 }
1173
1174 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1175 if (extra > 0)
1176 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1177 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001178 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 }
1180 else
1181 curwin->w_skipcol = 0;
1182 if (prev_skipcol != curwin->w_skipcol)
1183 redraw_later(NOT_VALID);
1184
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001185#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001186 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1187 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001188# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001189 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001190# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001191 )
1192 redraw_later(SOME_VALID);
1193#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1196}
1197
1198/*
1199 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001202scrolldown(
1203 long line_count,
1204 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 long done = 0; /* total # of physical lines done */
1207 int wrow;
1208 int moved = FALSE;
1209
1210#ifdef FEAT_FOLDING
1211 linenr_T first;
1212
1213 /* Make sure w_topline is at the first of a sequence of folded lines. */
1214 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1215#endif
1216 validate_cursor(); /* w_wrow needs to be valid */
1217 while (line_count-- > 0)
1218 {
1219#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001220 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1221 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
1223 ++curwin->w_topfill;
1224 ++done;
1225 }
1226 else
1227#endif
1228 {
1229 if (curwin->w_topline == 1)
1230 break;
1231 --curwin->w_topline;
1232#ifdef FEAT_DIFF
1233 curwin->w_topfill = 0;
1234#endif
1235#ifdef FEAT_FOLDING
1236 /* A sequence of folded lines only counts for one logical line */
1237 if (hasFolding(curwin->w_topline, &first, NULL))
1238 {
1239 ++done;
1240 if (!byfold)
1241 line_count -= curwin->w_topline - first - 1;
1242 curwin->w_botline -= curwin->w_topline - first;
1243 curwin->w_topline = first;
1244 }
1245 else
1246#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001247 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249 --curwin->w_botline; /* approximate w_botline */
1250 invalidate_botline();
1251 }
1252 curwin->w_wrow += done; /* keep w_wrow updated */
1253 curwin->w_cline_row += done; /* keep w_cline_row updated */
1254
1255#ifdef FEAT_DIFF
1256 if (curwin->w_cursor.lnum == curwin->w_topline)
1257 curwin->w_cline_row = 0;
1258 check_topfill(curwin, TRUE);
1259#endif
1260
1261 /*
1262 * Compute the row number of the last row of the cursor line
1263 * and move the cursor onto the displayed part of the window.
1264 */
1265 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001266 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268 validate_virtcol();
1269 validate_cheight();
1270 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001271 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1274 {
1275#ifdef FEAT_FOLDING
1276 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1277 {
1278 --wrow;
1279 if (first == 1)
1280 curwin->w_cursor.lnum = 1;
1281 else
1282 curwin->w_cursor.lnum = first - 1;
1283 }
1284 else
1285#endif
1286 wrow -= plines(curwin->w_cursor.lnum--);
1287 curwin->w_valid &=
1288 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1289 moved = TRUE;
1290 }
1291 if (moved)
1292 {
1293#ifdef FEAT_FOLDING
1294 /* Move cursor to first line of closed fold. */
1295 foldAdjustCursor();
1296#endif
1297 coladvance(curwin->w_curswant);
1298 }
1299}
1300
1301/*
1302 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001305scrollup(
1306 long line_count,
1307 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1310 linenr_T lnum;
1311
1312 if (
1313# ifdef FEAT_FOLDING
1314 (byfold && hasAnyFolding(curwin))
1315# ifdef FEAT_DIFF
1316 ||
1317# endif
1318# endif
1319# ifdef FEAT_DIFF
1320 curwin->w_p_diff
1321# endif
1322 )
1323 {
1324 /* count each sequence of folded lines as one logical line */
1325 lnum = curwin->w_topline;
1326 while (line_count--)
1327 {
1328# ifdef FEAT_DIFF
1329 if (curwin->w_topfill > 0)
1330 --curwin->w_topfill;
1331 else
1332# endif
1333 {
1334# ifdef FEAT_FOLDING
1335 if (byfold)
1336 (void)hasFolding(lnum, NULL, &lnum);
1337# endif
1338 if (lnum >= curbuf->b_ml.ml_line_count)
1339 break;
1340 ++lnum;
1341# ifdef FEAT_DIFF
1342 curwin->w_topfill = diff_check_fill(curwin, lnum);
1343# endif
1344 }
1345 }
1346 /* approximate w_botline */
1347 curwin->w_botline += lnum - curwin->w_topline;
1348 curwin->w_topline = lnum;
1349 }
1350 else
1351#endif
1352 {
1353 curwin->w_topline += line_count;
1354 curwin->w_botline += line_count; /* approximate w_botline */
1355 }
1356
1357 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1358 curwin->w_topline = curbuf->b_ml.ml_line_count;
1359 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1360 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1361
1362#ifdef FEAT_DIFF
1363 check_topfill(curwin, FALSE);
1364#endif
1365
1366#ifdef FEAT_FOLDING
1367 if (hasAnyFolding(curwin))
1368 /* Make sure w_topline is at the first of a sequence of folded lines. */
1369 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1370#endif
1371
1372 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1373 if (curwin->w_cursor.lnum < curwin->w_topline)
1374 {
1375 curwin->w_cursor.lnum = curwin->w_topline;
1376 curwin->w_valid &=
1377 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1378 coladvance(curwin->w_curswant);
1379 }
1380}
1381
1382#ifdef FEAT_DIFF
1383/*
1384 * Don't end up with too many filler lines in the window.
1385 */
1386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387check_topfill(
1388 win_T *wp,
1389 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 int n;
1392
1393 if (wp->w_topfill > 0)
1394 {
1395 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1396 if (wp->w_topfill + n > wp->w_height)
1397 {
1398 if (down && wp->w_topline > 1)
1399 {
1400 --wp->w_topline;
1401 wp->w_topfill = 0;
1402 }
1403 else
1404 {
1405 wp->w_topfill = wp->w_height - n;
1406 if (wp->w_topfill < 0)
1407 wp->w_topfill = 0;
1408 }
1409 }
1410 }
1411}
1412
1413/*
1414 * Use as many filler lines as possible for w_topline. Make sure w_topline
1415 * is still visible.
1416 */
1417 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 int n;
1421
1422 n = plines_nofill(curwin->w_topline);
1423 if (n >= curwin->w_height)
1424 curwin->w_topfill = 0;
1425 else
1426 {
1427 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1428 if (curwin->w_topfill + n > curwin->w_height)
1429 curwin->w_topfill = curwin->w_height - n;
1430 }
1431}
1432#endif
1433
1434#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1435/*
1436 * Scroll the screen one line down, but don't do it if it would move the
1437 * cursor off the screen.
1438 */
1439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001440scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 int end_row;
1443#ifdef FEAT_DIFF
1444 int can_fill = (curwin->w_topfill
1445 < diff_check_fill(curwin, curwin->w_topline));
1446#endif
1447
1448 if (curwin->w_topline <= 1
1449#ifdef FEAT_DIFF
1450 && !can_fill
1451#endif
1452 )
1453 return;
1454
1455 validate_cursor(); /* w_wrow needs to be valid */
1456
1457 /*
1458 * Compute the row number of the last row of the cursor line
1459 * and make sure it doesn't go off the screen. Make sure the cursor
1460 * doesn't go past 'scrolloff' lines from the screen end.
1461 */
1462 end_row = curwin->w_wrow;
1463#ifdef FEAT_DIFF
1464 if (can_fill)
1465 ++end_row;
1466 else
1467 end_row += plines_nofill(curwin->w_topline - 1);
1468#else
1469 end_row += plines(curwin->w_topline - 1);
1470#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001471 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 validate_cheight();
1474 validate_virtcol();
1475 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001476 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478 if (end_row < curwin->w_height - p_so)
1479 {
1480#ifdef FEAT_DIFF
1481 if (can_fill)
1482 {
1483 ++curwin->w_topfill;
1484 check_topfill(curwin, TRUE);
1485 }
1486 else
1487 {
1488 --curwin->w_topline;
1489 curwin->w_topfill = 0;
1490 }
1491#else
1492 --curwin->w_topline;
1493#endif
1494#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001495 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496#endif
1497 --curwin->w_botline; /* approximate w_botline */
1498 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1499 }
1500}
1501
1502/*
1503 * Scroll the screen one line up, but don't do it if it would move the cursor
1504 * off the screen.
1505 */
1506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 int start_row;
1510
1511 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1512#ifdef FEAT_DIFF
1513 && curwin->w_topfill == 0
1514#endif
1515 )
1516 return;
1517
1518 validate_cursor(); /* w_wrow needs to be valid */
1519
1520 /*
1521 * Compute the row number of the first row of the cursor line
1522 * and make sure it doesn't go off the screen. Make sure the cursor
1523 * doesn't go before 'scrolloff' lines from the screen start.
1524 */
1525#ifdef FEAT_DIFF
1526 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1527 - curwin->w_topfill;
1528#else
1529 start_row = curwin->w_wrow - plines(curwin->w_topline);
1530#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001531 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001534 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 if (start_row >= p_so)
1537 {
1538#ifdef FEAT_DIFF
1539 if (curwin->w_topfill > 0)
1540 --curwin->w_topfill;
1541 else
1542#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001543 {
1544#ifdef FEAT_FOLDING
1545 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 ++curwin->w_botline; /* approximate w_botline */
1550 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1551 }
1552}
1553#endif /* FEAT_INS_EXPAND */
1554
1555/*
1556 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1557 * a (wrapped) text line. Uses and sets "lp->fill".
1558 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001559 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001562topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564#ifdef FEAT_DIFF
1565 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1566 {
1567 /* Add a filler line. */
1568 ++lp->fill;
1569 lp->height = 1;
1570 }
1571 else
1572#endif
1573 {
1574 --lp->lnum;
1575#ifdef FEAT_DIFF
1576 lp->fill = 0;
1577#endif
1578 if (lp->lnum < 1)
1579 lp->height = MAXCOL;
1580 else
1581#ifdef FEAT_FOLDING
1582 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1583 /* Add a closed fold */
1584 lp->height = 1;
1585 else
1586#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001587 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589}
1590
1591/*
1592 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1593 * a (wrapped) text line. Uses and sets "lp->fill".
1594 * Returns the height of the added line in "lp->height".
1595 * Lines below the last one are incredibly high.
1596 */
1597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001598botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600#ifdef FEAT_DIFF
1601 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1602 {
1603 /* Add a filler line. */
1604 ++lp->fill;
1605 lp->height = 1;
1606 }
1607 else
1608#endif
1609 {
1610 ++lp->lnum;
1611#ifdef FEAT_DIFF
1612 lp->fill = 0;
1613#endif
1614 if (lp->lnum > curbuf->b_ml.ml_line_count)
1615 lp->height = MAXCOL;
1616 else
1617#ifdef FEAT_FOLDING
1618 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1619 /* Add a closed fold */
1620 lp->height = 1;
1621 else
1622#endif
1623 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001624 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627}
1628
1629#ifdef FEAT_DIFF
1630/*
1631 * Switch from including filler lines below lp->lnum to including filler
1632 * lines above loff.lnum + 1. This keeps pointing to the same line.
1633 * When there are no filler lines nothing changes.
1634 */
1635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001636botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 if (lp->fill > 0)
1639 {
1640 ++lp->lnum;
1641 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1642 }
1643}
1644
1645/*
1646 * Switch from including filler lines above lp->lnum to including filler
1647 * lines below loff.lnum - 1. This keeps pointing to the same line.
1648 * When there are no filler lines nothing changes.
1649 */
1650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001651topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 if (lp->fill > 0)
1654 {
1655 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1656 --lp->lnum;
1657 }
1658}
1659#endif
1660
1661/*
1662 * Recompute topline to put the cursor at the top of the window.
1663 * Scroll at least "min_scroll" lines.
1664 * If "always" is TRUE, always set topline (for "zt").
1665 */
1666 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001667scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 int scrolled = 0;
1670 int extra = 0;
1671 int used;
1672 int i;
1673 linenr_T top; /* just above displayed lines */
1674 linenr_T bot; /* just below displayed lines */
1675 linenr_T old_topline = curwin->w_topline;
1676#ifdef FEAT_DIFF
1677 linenr_T old_topfill = curwin->w_topfill;
1678#endif
1679 linenr_T new_topline;
1680 int off = p_so;
1681
1682#ifdef FEAT_MOUSE
1683 if (mouse_dragging > 0)
1684 off = mouse_dragging - 1;
1685#endif
1686
1687 /*
1688 * Decrease topline until:
1689 * - it has become 1
1690 * - (part of) the cursor line is moved off the screen or
1691 * - moved at least 'scrolljump' lines and
1692 * - at least 'scrolloff' lines above and below the cursor
1693 */
1694 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001695 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (curwin->w_cursor.lnum < curwin->w_topline)
1697 scrolled = used;
1698
1699#ifdef FEAT_FOLDING
1700 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1701 {
1702 --top;
1703 ++bot;
1704 }
1705 else
1706#endif
1707 {
1708 top = curwin->w_cursor.lnum - 1;
1709 bot = curwin->w_cursor.lnum + 1;
1710 }
1711 new_topline = top + 1;
1712
1713#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001714 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001715 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001716 * Hide filler lines above cursor line by adding them to "extra". */
1717 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718#endif
1719
1720 /*
1721 * Check if the lines from "top" to "bot" fit in the window. If they do,
1722 * set new_topline and advance "top" and "bot" to include more lines.
1723 */
1724 while (top > 0)
1725 {
1726#ifdef FEAT_FOLDING
1727 if (hasFolding(top, &top, NULL))
1728 /* count one logical line for a sequence of folded lines */
1729 i = 1;
1730 else
1731#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001732 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 used += i;
1734 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1735 {
1736#ifdef FEAT_FOLDING
1737 if (hasFolding(bot, NULL, &bot))
1738 /* count one logical line for a sequence of folded lines */
1739 ++used;
1740 else
1741#endif
1742 used += plines(bot);
1743 }
1744 if (used > curwin->w_height)
1745 break;
1746 if (top < curwin->w_topline)
1747 scrolled += i;
1748
1749 /*
1750 * If scrolling is needed, scroll at least 'sj' lines.
1751 */
1752 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1753 && extra >= off)
1754 break;
1755
1756 extra += i;
1757 new_topline = top;
1758 --top;
1759 ++bot;
1760 }
1761
1762 /*
1763 * If we don't have enough space, put cursor in the middle.
1764 * This makes sure we get the same position when using "k" and "j"
1765 * in a small window.
1766 */
1767 if (used > curwin->w_height)
1768 scroll_cursor_halfway(FALSE);
1769 else
1770 {
1771 /*
1772 * If "always" is FALSE, only adjust topline to a lower value, higher
1773 * value may happen with wrapping lines
1774 */
1775 if (new_topline < curwin->w_topline || always)
1776 curwin->w_topline = new_topline;
1777 if (curwin->w_topline > curwin->w_cursor.lnum)
1778 curwin->w_topline = curwin->w_cursor.lnum;
1779#ifdef FEAT_DIFF
1780 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1781 if (curwin->w_topfill > 0 && extra > off)
1782 {
1783 curwin->w_topfill -= extra - off;
1784 if (curwin->w_topfill < 0)
1785 curwin->w_topfill = 0;
1786 }
1787 check_topfill(curwin, FALSE);
1788#endif
1789 if (curwin->w_topline != old_topline
1790#ifdef FEAT_DIFF
1791 || curwin->w_topfill != old_topfill
1792#endif
1793 )
1794 curwin->w_valid &=
1795 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1796 curwin->w_valid |= VALID_TOPLINE;
1797 }
1798}
1799
1800/*
1801 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1802 * screen lines for text lines.
1803 */
1804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001805set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
1807#ifdef FEAT_DIFF
1808 wp->w_filler_rows = 0;
1809#endif
1810 if (used == 0)
1811 wp->w_empty_rows = 0; /* single line that doesn't fit */
1812 else
1813 {
1814 wp->w_empty_rows = wp->w_height - used;
1815#ifdef FEAT_DIFF
1816 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1817 {
1818 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1819 if (wp->w_empty_rows > wp->w_filler_rows)
1820 wp->w_empty_rows -= wp->w_filler_rows;
1821 else
1822 {
1823 wp->w_filler_rows = wp->w_empty_rows;
1824 wp->w_empty_rows = 0;
1825 }
1826 }
1827#endif
1828 }
1829}
1830
1831/*
1832 * Recompute topline to put the cursor at the bottom of the window.
1833 * Scroll at least "min_scroll" lines.
1834 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1835 * This is messy stuff!!!
1836 */
1837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001838scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 int used;
1841 int scrolled = 0;
1842 int extra = 0;
1843 int i;
1844 linenr_T line_count;
1845 linenr_T old_topline = curwin->w_topline;
1846 lineoff_T loff;
1847 lineoff_T boff;
1848#ifdef FEAT_DIFF
1849 int old_topfill = curwin->w_topfill;
1850 int fill_below_window;
1851#endif
1852 linenr_T old_botline = curwin->w_botline;
1853 linenr_T old_valid = curwin->w_valid;
1854 int old_empty_rows = curwin->w_empty_rows;
1855 linenr_T cln; /* Cursor Line Number */
1856
1857 cln = curwin->w_cursor.lnum;
1858 if (set_topbot)
1859 {
1860 used = 0;
1861 curwin->w_botline = cln + 1;
1862#ifdef FEAT_DIFF
1863 loff.fill = 0;
1864#endif
1865 for (curwin->w_topline = curwin->w_botline;
1866 curwin->w_topline > 1;
1867 curwin->w_topline = loff.lnum)
1868 {
1869 loff.lnum = curwin->w_topline;
1870 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001871 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 break;
1873 used += loff.height;
1874#ifdef FEAT_DIFF
1875 curwin->w_topfill = loff.fill;
1876#endif
1877 }
1878 set_empty_rows(curwin, used);
1879 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1880 if (curwin->w_topline != old_topline
1881#ifdef FEAT_DIFF
1882 || curwin->w_topfill != old_topfill
1883#endif
1884 )
1885 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1886 }
1887 else
1888 validate_botline();
1889
1890 /* The lines of the cursor line itself are always used. */
1891#ifdef FEAT_DIFF
1892 used = plines_nofill(cln);
1893#else
1894 validate_cheight();
1895 used = curwin->w_cline_height;
1896#endif
1897
1898 /* If the cursor is below botline, we will at least scroll by the height
1899 * of the cursor line. Correct for empty lines, which are really part of
1900 * botline. */
1901 if (cln >= curwin->w_botline)
1902 {
1903 scrolled = used;
1904 if (cln == curwin->w_botline)
1905 scrolled -= curwin->w_empty_rows;
1906 }
1907
1908 /*
1909 * Stop counting lines to scroll when
1910 * - hitting start of the file
1911 * - scrolled nothing or at least 'sj' lines
1912 * - at least 'so' lines below the cursor
1913 * - lines between botline and cursor have been counted
1914 */
1915#ifdef FEAT_FOLDING
1916 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1917#endif
1918 {
1919 loff.lnum = cln;
1920 boff.lnum = cln;
1921 }
1922#ifdef FEAT_DIFF
1923 loff.fill = 0;
1924 boff.fill = 0;
1925 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1926 - curwin->w_filler_rows;
1927#endif
1928
1929 while (loff.lnum > 1)
1930 {
1931 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1932 * context for 'scrolloff' and counted all lines below the window. */
1933 if ((((scrolled <= 0 || scrolled >= min_scroll)
1934 && extra >= (
1935#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001936 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#endif
1938 p_so))
1939 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1940 && loff.lnum <= curwin->w_botline
1941#ifdef FEAT_DIFF
1942 && (loff.lnum < curwin->w_botline
1943 || loff.fill >= fill_below_window)
1944#endif
1945 )
1946 break;
1947
1948 /* Add one line above */
1949 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001950 if (loff.height == MAXCOL)
1951 used = MAXCOL;
1952 else
1953 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (used > curwin->w_height)
1955 break;
1956 if (loff.lnum >= curwin->w_botline
1957#ifdef FEAT_DIFF
1958 && (loff.lnum > curwin->w_botline
1959 || loff.fill <= fill_below_window)
1960#endif
1961 )
1962 {
1963 /* Count screen lines that are below the window. */
1964 scrolled += loff.height;
1965 if (loff.lnum == curwin->w_botline
1966#ifdef FEAT_DIFF
1967 && boff.fill == 0
1968#endif
1969 )
1970 scrolled -= curwin->w_empty_rows;
1971 }
1972
1973 if (boff.lnum < curbuf->b_ml.ml_line_count)
1974 {
1975 /* Add one line below */
1976 botline_forw(&boff);
1977 used += boff.height;
1978 if (used > curwin->w_height)
1979 break;
1980 if (extra < (
1981#ifdef FEAT_MOUSE
1982 mouse_dragging > 0 ? mouse_dragging - 1 :
1983#endif
1984 p_so) || scrolled < min_scroll)
1985 {
1986 extra += boff.height;
1987 if (boff.lnum >= curwin->w_botline
1988#ifdef FEAT_DIFF
1989 || (boff.lnum + 1 == curwin->w_botline
1990 && boff.fill > curwin->w_filler_rows)
1991#endif
1992 )
1993 {
1994 /* Count screen lines that are below the window. */
1995 scrolled += boff.height;
1996 if (boff.lnum == curwin->w_botline
1997#ifdef FEAT_DIFF
1998 && boff.fill == 0
1999#endif
2000 )
2001 scrolled -= curwin->w_empty_rows;
2002 }
2003 }
2004 }
2005 }
2006
2007 /* curwin->w_empty_rows is larger, no need to scroll */
2008 if (scrolled <= 0)
2009 line_count = 0;
2010 /* more than a screenfull, don't scroll but redraw */
2011 else if (used > curwin->w_height)
2012 line_count = used;
2013 /* scroll minimal number of lines */
2014 else
2015 {
2016 line_count = 0;
2017#ifdef FEAT_DIFF
2018 boff.fill = curwin->w_topfill;
2019#endif
2020 boff.lnum = curwin->w_topline - 1;
2021 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2022 {
2023 botline_forw(&boff);
2024 i += boff.height;
2025 ++line_count;
2026 }
2027 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2028 line_count = 9999;
2029 }
2030
2031 /*
2032 * Scroll up if the cursor is off the bottom of the screen a bit.
2033 * Otherwise put it at 1/2 of the screen.
2034 */
2035 if (line_count >= curwin->w_height && line_count > min_scroll)
2036 scroll_cursor_halfway(FALSE);
2037 else
2038 scrollup(line_count, TRUE);
2039
2040 /*
2041 * If topline didn't change we need to restore w_botline and w_empty_rows
2042 * (we changed them).
2043 * If topline did change, update_screen() will set botline.
2044 */
2045 if (curwin->w_topline == old_topline && set_topbot)
2046 {
2047 curwin->w_botline = old_botline;
2048 curwin->w_empty_rows = old_empty_rows;
2049 curwin->w_valid = old_valid;
2050 }
2051 curwin->w_valid |= VALID_TOPLINE;
2052}
2053
2054/*
2055 * Recompute topline to put the cursor halfway the window
2056 * If "atend" is TRUE, also put it halfway at the end of the file.
2057 */
2058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002059scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 int above = 0;
2062 linenr_T topline;
2063#ifdef FEAT_DIFF
2064 int topfill = 0;
2065#endif
2066 int below = 0;
2067 int used;
2068 lineoff_T loff;
2069 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002070#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002071 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2075#ifdef FEAT_FOLDING
2076 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2077#endif
2078#ifdef FEAT_DIFF
2079 used = plines_nofill(loff.lnum);
2080 loff.fill = 0;
2081 boff.fill = 0;
2082#else
2083 used = plines(loff.lnum);
2084#endif
2085 topline = loff.lnum;
2086 while (topline > 1)
2087 {
2088 if (below <= above) /* add a line below the cursor first */
2089 {
2090 if (boff.lnum < curbuf->b_ml.ml_line_count)
2091 {
2092 botline_forw(&boff);
2093 used += boff.height;
2094 if (used > curwin->w_height)
2095 break;
2096 below += boff.height;
2097 }
2098 else
2099 {
2100 ++below; /* count a "~" line */
2101 if (atend)
2102 ++used;
2103 }
2104 }
2105
2106 if (below > above) /* add a line above the cursor */
2107 {
2108 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002109 if (loff.height == MAXCOL)
2110 used = MAXCOL;
2111 else
2112 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if (used > curwin->w_height)
2114 break;
2115 above += loff.height;
2116 topline = loff.lnum;
2117#ifdef FEAT_DIFF
2118 topfill = loff.fill;
2119#endif
2120 }
2121 }
2122#ifdef FEAT_FOLDING
2123 if (!hasFolding(topline, &curwin->w_topline, NULL))
2124#endif
2125 curwin->w_topline = topline;
2126#ifdef FEAT_DIFF
2127 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002128 if (old_topline > curwin->w_topline + curwin->w_height)
2129 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 check_topfill(curwin, FALSE);
2131#endif
2132 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2133 curwin->w_valid |= VALID_TOPLINE;
2134}
2135
2136/*
2137 * Correct the cursor position so that it is in a part of the screen at least
2138 * 'so' lines from the top and bottom, if possible.
2139 * If not possible, put it at the same position as scroll_cursor_halfway().
2140 * When called topline must be valid!
2141 */
2142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002143cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int above = 0; /* screen lines above topline */
2146 linenr_T topline;
2147 int below = 0; /* screen lines below botline */
2148 linenr_T botline;
2149 int above_wanted, below_wanted;
2150 linenr_T cln; /* Cursor Line Number */
2151 int max_off;
2152
2153 /*
2154 * How many lines we would like to have above/below the cursor depends on
2155 * whether the first/last line of the file is on screen.
2156 */
2157 above_wanted = p_so;
2158 below_wanted = p_so;
2159#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002160 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 above_wanted = mouse_dragging - 1;
2163 below_wanted = mouse_dragging - 1;
2164 }
2165#endif
2166 if (curwin->w_topline == 1)
2167 {
2168 above_wanted = 0;
2169 max_off = curwin->w_height / 2;
2170 if (below_wanted > max_off)
2171 below_wanted = max_off;
2172 }
2173 validate_botline();
2174 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2175#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002176 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#endif
2178 )
2179 {
2180 below_wanted = 0;
2181 max_off = (curwin->w_height - 1) / 2;
2182 if (above_wanted > max_off)
2183 above_wanted = max_off;
2184 }
2185
2186 /*
2187 * If there are sufficient file-lines above and below the cursor, we can
2188 * return now.
2189 */
2190 cln = curwin->w_cursor.lnum;
2191 if (cln >= curwin->w_topline + above_wanted
2192 && cln < curwin->w_botline - below_wanted
2193#ifdef FEAT_FOLDING
2194 && !hasAnyFolding(curwin)
2195#endif
2196 )
2197 return;
2198
2199 /*
2200 * Narrow down the area where the cursor can be put by taking lines from
2201 * the top and the bottom until:
2202 * - the desired context lines are found
2203 * - the lines from the top is past the lines from the bottom
2204 */
2205 topline = curwin->w_topline;
2206 botline = curwin->w_botline - 1;
2207#ifdef FEAT_DIFF
2208 /* count filler lines as context */
2209 above = curwin->w_topfill;
2210 below = curwin->w_filler_rows;
2211#endif
2212 while ((above < above_wanted || below < below_wanted) && topline < botline)
2213 {
2214 if (below < below_wanted && (below <= above || above >= above_wanted))
2215 {
2216#ifdef FEAT_FOLDING
2217 if (hasFolding(botline, &botline, NULL))
2218 ++below;
2219 else
2220#endif
2221 below += plines(botline);
2222 --botline;
2223 }
2224 if (above < above_wanted && (above < below || below >= below_wanted))
2225 {
2226#ifdef FEAT_FOLDING
2227 if (hasFolding(topline, NULL, &topline))
2228 ++above;
2229 else
2230#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002231 above += PLINES_NOFILL(topline);
2232#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /* Count filler lines below this line as context. */
2234 if (topline < botline)
2235 above += diff_check_fill(curwin, topline + 1);
2236#endif
2237 ++topline;
2238 }
2239 }
2240 if (topline == botline || botline == 0)
2241 curwin->w_cursor.lnum = topline;
2242 else if (topline > botline)
2243 curwin->w_cursor.lnum = botline;
2244 else
2245 {
2246 if (cln < topline && curwin->w_topline > 1)
2247 {
2248 curwin->w_cursor.lnum = topline;
2249 curwin->w_valid &=
2250 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2251 }
2252 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2253 {
2254 curwin->w_cursor.lnum = botline;
2255 curwin->w_valid &=
2256 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2257 }
2258 }
2259 curwin->w_valid |= VALID_TOPLINE;
2260}
2261
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002262static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264/*
2265 * move screen 'count' pages up or down and update screen
2266 *
2267 * return FAIL for failure, OK otherwise
2268 */
2269 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 long n;
2273 int retval = OK;
2274 lineoff_T loff;
2275 linenr_T old_topline = curwin->w_topline;
2276
2277 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2278 {
2279 beep_flush();
2280 return FAIL;
2281 }
2282
2283 for ( ; count > 0; --count)
2284 {
2285 validate_botline();
2286 /*
2287 * It's an error to move a page up when the first line is already on
2288 * the screen. It's an error to move a page down when the last line
2289 * is on the screen and the topline is 'scrolloff' lines from the
2290 * last line.
2291 */
2292 if (dir == FORWARD
2293 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2294 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2295 : (curwin->w_topline == 1
2296#ifdef FEAT_DIFF
2297 && curwin->w_topfill ==
2298 diff_check_fill(curwin, curwin->w_topline)
2299#endif
2300 ))
2301 {
2302 beep_flush();
2303 retval = FAIL;
2304 break;
2305 }
2306
2307#ifdef FEAT_DIFF
2308 loff.fill = 0;
2309#endif
2310 if (dir == FORWARD)
2311 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002312 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002314 /* Vi compatible scrolling */
2315 if (p_window <= 2)
2316 ++curwin->w_topline;
2317 else
2318 curwin->w_topline += p_window - 2;
2319 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2320 curwin->w_topline = curbuf->b_ml.ml_line_count;
2321 curwin->w_cursor.lnum = curwin->w_topline;
2322 }
2323 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2324 {
2325 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 curwin->w_topline = curbuf->b_ml.ml_line_count;
2327#ifdef FEAT_DIFF
2328 curwin->w_topfill = 0;
2329#endif
2330 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2331 }
2332 else
2333 {
2334 /* For the overlap, start with the line just below the window
2335 * and go upwards. */
2336 loff.lnum = curwin->w_botline;
2337#ifdef FEAT_DIFF
2338 loff.fill = diff_check_fill(curwin, loff.lnum)
2339 - curwin->w_filler_rows;
2340#endif
2341 get_scroll_overlap(&loff, -1);
2342 curwin->w_topline = loff.lnum;
2343#ifdef FEAT_DIFF
2344 curwin->w_topfill = loff.fill;
2345 check_topfill(curwin, FALSE);
2346#endif
2347 curwin->w_cursor.lnum = curwin->w_topline;
2348 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2349 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2350 }
2351 }
2352 else /* dir == BACKWARDS */
2353 {
2354#ifdef FEAT_DIFF
2355 if (curwin->w_topline == 1)
2356 {
2357 /* Include max number of filler lines */
2358 max_topfill();
2359 continue;
2360 }
2361#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002362 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002363 {
2364 /* Vi compatible scrolling (sort of) */
2365 if (p_window <= 2)
2366 --curwin->w_topline;
2367 else
2368 curwin->w_topline -= p_window - 2;
2369 if (curwin->w_topline < 1)
2370 curwin->w_topline = 1;
2371 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2372 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2373 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2374 continue;
2375 }
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /* Find the line at the top of the window that is going to be the
2378 * line at the bottom of the window. Make sure this results in
2379 * the same line as before doing CTRL-F. */
2380 loff.lnum = curwin->w_topline - 1;
2381#ifdef FEAT_DIFF
2382 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2383 - curwin->w_topfill;
2384#endif
2385 get_scroll_overlap(&loff, 1);
2386
2387 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2388 {
2389 loff.lnum = curbuf->b_ml.ml_line_count;
2390#ifdef FEAT_DIFF
2391 loff.fill = 0;
2392 }
2393 else
2394 {
2395 botline_topline(&loff);
2396#endif
2397 }
2398 curwin->w_cursor.lnum = loff.lnum;
2399
2400 /* Find the line just above the new topline to get the right line
2401 * at the bottom of the window. */
2402 n = 0;
2403 while (n <= curwin->w_height && loff.lnum >= 1)
2404 {
2405 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002406 if (loff.height == MAXCOL)
2407 n = MAXCOL;
2408 else
2409 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002411 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413 curwin->w_topline = 1;
2414#ifdef FEAT_DIFF
2415 max_topfill();
2416#endif
2417 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2418 }
2419 else
2420 {
2421 /* Go two lines forward again. */
2422#ifdef FEAT_DIFF
2423 topline_botline(&loff);
2424#endif
2425 botline_forw(&loff);
2426 botline_forw(&loff);
2427#ifdef FEAT_DIFF
2428 botline_topline(&loff);
2429#endif
2430#ifdef FEAT_FOLDING
2431 /* We're at the wrong end of a fold now. */
2432 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2433#endif
2434
2435 /* Always scroll at least one line. Avoid getting stuck on
2436 * very long lines. */
2437 if (loff.lnum >= curwin->w_topline
2438#ifdef FEAT_DIFF
2439 && (loff.lnum > curwin->w_topline
2440 || loff.fill >= curwin->w_topfill)
2441#endif
2442 )
2443 {
2444#ifdef FEAT_DIFF
2445 /* First try using the maximum number of filler lines. If
2446 * that's not enough, backup one line. */
2447 loff.fill = curwin->w_topfill;
2448 if (curwin->w_topfill < diff_check_fill(curwin,
2449 curwin->w_topline))
2450 max_topfill();
2451 if (curwin->w_topfill == loff.fill)
2452#endif
2453 {
2454 --curwin->w_topline;
2455#ifdef FEAT_DIFF
2456 curwin->w_topfill = 0;
2457#endif
2458 }
2459 comp_botline(curwin);
2460 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002461 curwin->w_valid &=
2462 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 else
2465 {
2466 curwin->w_topline = loff.lnum;
2467#ifdef FEAT_DIFF
2468 curwin->w_topfill = loff.fill;
2469 check_topfill(curwin, FALSE);
2470#endif
2471 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2472 }
2473 }
2474 }
2475 }
2476#ifdef FEAT_FOLDING
2477 foldAdjustCursor();
2478#endif
2479 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002480 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002481 if (retval == OK)
2482 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2484
Bram Moolenaar907dad72018-07-10 15:07:15 +02002485 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002487 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2488 // But make sure we scroll at least one line (happens with mix of long
2489 // wrapping lines and non-wrapping line).
2490 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002492 scroll_cursor_top(1, FALSE);
2493 if (curwin->w_topline <= old_topline
2494 && old_topline < curbuf->b_ml.ml_line_count)
2495 {
2496 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002498 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2499#endif
2500 }
2501 }
2502#ifdef FEAT_FOLDING
2503 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507
2508 redraw_later(VALID);
2509 return retval;
2510}
2511
2512/*
2513 * Decide how much overlap to use for page-up or page-down scrolling.
2514 * This is symmetric, so that doing both keeps the same lines displayed.
2515 * Three lines are examined:
2516 *
2517 * before CTRL-F after CTRL-F / before CTRL-B
2518 * etc. l1
2519 * l1 last but one line ------------
2520 * l2 last text line l2 top text line
2521 * ------------- l3 second text line
2522 * l3 etc.
2523 */
2524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002525get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 int h1, h2, h3, h4;
2528 int min_height = curwin->w_height - 2;
2529 lineoff_T loff0, loff1, loff2;
2530
2531#ifdef FEAT_DIFF
2532 if (lp->fill > 0)
2533 lp->height = 1;
2534 else
2535 lp->height = plines_nofill(lp->lnum);
2536#else
2537 lp->height = plines(lp->lnum);
2538#endif
2539 h1 = lp->height;
2540 if (h1 > min_height)
2541 return; /* no overlap */
2542
2543 loff0 = *lp;
2544 if (dir > 0)
2545 botline_forw(lp);
2546 else
2547 topline_back(lp);
2548 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002549 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 *lp = loff0; /* no overlap */
2552 return;
2553 }
2554
2555 loff1 = *lp;
2556 if (dir > 0)
2557 botline_forw(lp);
2558 else
2559 topline_back(lp);
2560 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002561 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 *lp = loff0; /* no overlap */
2564 return;
2565 }
2566
2567 loff2 = *lp;
2568 if (dir > 0)
2569 botline_forw(lp);
2570 else
2571 topline_back(lp);
2572 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002573 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 *lp = loff1; /* 1 line overlap */
2575 else
2576 *lp = loff2; /* 2 lines overlap */
2577 return;
2578}
2579
2580/* #define KEEP_SCREEN_LINE */
2581/*
2582 * Scroll 'scroll' lines up or down.
2583 */
2584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002585halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 long scrolled = 0;
2588 int i;
2589 int n;
2590 int room;
2591
2592 if (Prenum)
2593 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2594 curwin->w_height : Prenum;
2595 n = (curwin->w_p_scr <= curwin->w_height) ?
2596 curwin->w_p_scr : curwin->w_height;
2597
Bram Moolenaard5d37532017-03-27 23:02:07 +02002598 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 validate_botline();
2600 room = curwin->w_empty_rows;
2601#ifdef FEAT_DIFF
2602 room += curwin->w_filler_rows;
2603#endif
2604 if (flag)
2605 {
2606 /*
2607 * scroll the text up
2608 */
2609 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2610 {
2611#ifdef FEAT_DIFF
2612 if (curwin->w_topfill > 0)
2613 {
2614 i = 1;
2615 if (--n < 0 && scrolled > 0)
2616 break;
2617 --curwin->w_topfill;
2618 }
2619 else
2620#endif
2621 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002622 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 n -= i;
2624 if (n < 0 && scrolled > 0)
2625 break;
2626#ifdef FEAT_FOLDING
2627 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2628#endif
2629 ++curwin->w_topline;
2630#ifdef FEAT_DIFF
2631 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2632#endif
2633
2634#ifndef KEEP_SCREEN_LINE
2635 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2636 {
2637 ++curwin->w_cursor.lnum;
2638 curwin->w_valid &=
2639 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2640 }
2641#endif
2642 }
2643 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2644 scrolled += i;
2645
2646 /*
2647 * Correct w_botline for changed w_topline.
2648 * Won't work when there are filler lines.
2649 */
2650#ifdef FEAT_DIFF
2651 if (curwin->w_p_diff)
2652 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2653 else
2654#endif
2655 {
2656 room += i;
2657 do
2658 {
2659 i = plines(curwin->w_botline);
2660 if (i > room)
2661 break;
2662#ifdef FEAT_FOLDING
2663 (void)hasFolding(curwin->w_botline, NULL,
2664 &curwin->w_botline);
2665#endif
2666 ++curwin->w_botline;
2667 room -= i;
2668 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2669 }
2670 }
2671
2672#ifndef KEEP_SCREEN_LINE
2673 /*
2674 * When hit bottom of the file: move cursor down.
2675 */
2676 if (n > 0)
2677 {
2678# ifdef FEAT_FOLDING
2679 if (hasAnyFolding(curwin))
2680 {
2681 while (--n >= 0
2682 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2683 {
2684 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2685 &curwin->w_cursor.lnum);
2686 ++curwin->w_cursor.lnum;
2687 }
2688 }
2689 else
2690# endif
2691 curwin->w_cursor.lnum += n;
2692 check_cursor_lnum();
2693 }
2694#else
2695 /* try to put the cursor in the same screen line */
2696 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2697 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2698 {
2699 scrolled -= plines(curwin->w_cursor.lnum);
2700 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2701 break;
2702# ifdef FEAT_FOLDING
2703 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2704 &curwin->w_cursor.lnum);
2705# endif
2706 ++curwin->w_cursor.lnum;
2707 }
2708#endif
2709 }
2710 else
2711 {
2712 /*
2713 * scroll the text down
2714 */
2715 while (n > 0 && curwin->w_topline > 1)
2716 {
2717#ifdef FEAT_DIFF
2718 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2719 {
2720 i = 1;
2721 if (--n < 0 && scrolled > 0)
2722 break;
2723 ++curwin->w_topfill;
2724 }
2725 else
2726#endif
2727 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002728 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 n -= i;
2730 if (n < 0 && scrolled > 0)
2731 break;
2732 --curwin->w_topline;
2733#ifdef FEAT_FOLDING
2734 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2735#endif
2736#ifdef FEAT_DIFF
2737 curwin->w_topfill = 0;
2738#endif
2739 }
2740 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2741 VALID_BOTLINE|VALID_BOTLINE_AP);
2742 scrolled += i;
2743#ifndef KEEP_SCREEN_LINE
2744 if (curwin->w_cursor.lnum > 1)
2745 {
2746 --curwin->w_cursor.lnum;
2747 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2748 }
2749#endif
2750 }
2751#ifndef KEEP_SCREEN_LINE
2752 /*
2753 * When hit top of the file: move cursor up.
2754 */
2755 if (n > 0)
2756 {
2757 if (curwin->w_cursor.lnum <= (linenr_T)n)
2758 curwin->w_cursor.lnum = 1;
2759 else
2760# ifdef FEAT_FOLDING
2761 if (hasAnyFolding(curwin))
2762 {
2763 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2764 {
2765 --curwin->w_cursor.lnum;
2766 (void)hasFolding(curwin->w_cursor.lnum,
2767 &curwin->w_cursor.lnum, NULL);
2768 }
2769 }
2770 else
2771# endif
2772 curwin->w_cursor.lnum -= n;
2773 }
2774#else
2775 /* try to put the cursor in the same screen line */
2776 scrolled += n; /* move cursor when topline is 1 */
2777 while (curwin->w_cursor.lnum > curwin->w_topline
2778 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2779 {
2780 scrolled -= plines(curwin->w_cursor.lnum - 1);
2781 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2782 break;
2783 --curwin->w_cursor.lnum;
2784# ifdef FEAT_FOLDING
2785 foldAdjustCursor();
2786# endif
2787 }
2788#endif
2789 }
2790# ifdef FEAT_FOLDING
2791 /* Move cursor to first line of closed fold. */
2792 foldAdjustCursor();
2793# endif
2794#ifdef FEAT_DIFF
2795 check_topfill(curwin, !flag);
2796#endif
2797 cursor_correct();
2798 beginline(BL_SOL | BL_FIX);
2799 redraw_later(VALID);
2800}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002801
Bram Moolenaar860cae12010-06-05 23:22:07 +02002802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002803do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002804{
2805 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002806 colnr_T col = curwin->w_cursor.col;
2807# ifdef FEAT_VIRTUALEDIT
2808 colnr_T coladd = curwin->w_cursor.coladd;
2809# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002810 colnr_T curswant = curwin->w_curswant;
2811 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002812 win_T *old_curwin = curwin;
2813 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002814 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002815 int old_VIsual_select = VIsual_select;
2816 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817
2818 /*
2819 * loop through the cursorbound windows
2820 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002821 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002822 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823 {
2824 curbuf = curwin->w_buffer;
2825 /* skip original window and windows with 'noscrollbind' */
2826 if (curwin != old_curwin && curwin->w_p_crb)
2827 {
2828# ifdef FEAT_DIFF
2829 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002830 curwin->w_cursor.lnum =
2831 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002832 else
2833# endif
2834 curwin->w_cursor.lnum = line;
2835 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002836# ifdef FEAT_VIRTUALEDIT
2837 curwin->w_cursor.coladd = coladd;
2838# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002839 curwin->w_curswant = curswant;
2840 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002841
Bram Moolenaar61452852011-02-01 18:01:11 +01002842 /* Make sure the cursor is in a valid position. Temporarily set
2843 * "restart_edit" to allow the cursor to be beyond the EOL. */
2844 restart_edit_save = restart_edit;
2845 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002847# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002848 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002849 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002850# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002851 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002852# ifdef FEAT_MBYTE
2853 /* Correct cursor for multi-byte character. */
2854 if (has_mbyte)
2855 mb_adjust_cursor();
2856# endif
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002857 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002858
2859 /* Only scroll when 'scrollbind' hasn't done this. */
2860 if (!curwin->w_p_scb)
2861 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002863 }
2864 }
2865
2866 /*
2867 * reset current-window
2868 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869 VIsual_select = old_VIsual_select;
2870 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 curwin = old_curwin;
2872 curbuf = old_curbuf;
2873}