blob: b2b84868acfc77fbd45b47a27264f400ddc38c15 [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
126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
139# ifdef FEAT_INS_EXPAND
140 && !pum_visible()
141# endif
142 )
143 redraw_win_later(wp, SOME_VALID);
144}
145
146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 * Update curwin->w_topline and redraw if necessary.
148 * Used to update the screen before printing a message.
149 */
150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100151update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152{
153 update_topline();
154 if (must_redraw)
155 update_screen(0);
156}
157
158/*
159 * Update curwin->w_topline to move the cursor onto the screen.
160 */
161 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100162update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
164 long line_count;
165 int halfheight;
166 int n;
167 linenr_T old_topline;
168#ifdef FEAT_DIFF
169 int old_topfill;
170#endif
171#ifdef FEAT_FOLDING
172 linenr_T lnum;
173#endif
174 int check_topline = FALSE;
175 int check_botline = FALSE;
176#ifdef FEAT_MOUSE
177 int save_so = p_so;
178#endif
179
Bram Moolenaard5d37532017-03-27 23:02:07 +0200180 /* If there is no valid screen and when the window height is zero just use
181 * the cursor line. */
182 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200183 {
184 curwin->w_topline = curwin->w_cursor.lnum;
185 curwin->w_botline = curwin->w_topline;
186 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200187 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200188 return;
189 }
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 check_cursor_moved(curwin);
192 if (curwin->w_valid & VALID_TOPLINE)
193 return;
194
195#ifdef FEAT_MOUSE
196 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000197 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 p_so = mouse_dragging - 1;
199#endif
200
201 old_topline = curwin->w_topline;
202#ifdef FEAT_DIFF
203 old_topfill = curwin->w_topfill;
204#endif
205
206 /*
207 * If the buffer is empty, always set topline to 1.
208 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100209 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
211 if (curwin->w_topline != 1)
212 redraw_later(NOT_VALID);
213 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 curwin->w_botline = 2;
215 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 }
218
219 /*
220 * If the cursor is above or near the top of the window, scroll the window
221 * to show the line the cursor is in, with 'scrolloff' context.
222 */
223 else
224 {
225 if (curwin->w_topline > 1)
226 {
227 /* If the cursor is above topline, scrolling is always needed.
228 * If the cursor is far below topline and there is no folding,
229 * scrolling down is never needed. */
230 if (curwin->w_cursor.lnum < curwin->w_topline)
231 check_topline = TRUE;
232 else if (check_top_offset())
233 check_topline = TRUE;
234 }
235#ifdef FEAT_DIFF
236 /* Check if there are more filler lines than allowed. */
237 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
238 curwin->w_topline))
239 check_topline = TRUE;
240#endif
241
242 if (check_topline)
243 {
244 halfheight = curwin->w_height / 2 - 1;
245 if (halfheight < 2)
246 halfheight = 2;
247
248#ifdef FEAT_FOLDING
249 if (hasAnyFolding(curwin))
250 {
251 /* Count the number of logical lines between the cursor and
252 * topline + p_so (approximation of how much will be
253 * scrolled). */
254 n = 0;
255 for (lnum = curwin->w_cursor.lnum;
256 lnum < curwin->w_topline + p_so; ++lnum)
257 {
258 ++n;
259 /* stop at end of file or when we know we are far off */
260 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
261 break;
262 (void)hasFolding(lnum, NULL, &lnum);
263 }
264 }
265 else
266#endif
267 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
268
269 /* If we weren't very close to begin with, we scroll to put the
270 * cursor in the middle of the window. Otherwise put the cursor
271 * near the top of the window. */
272 if (n >= halfheight)
273 scroll_cursor_halfway(FALSE);
274 else
275 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000276 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 check_botline = TRUE;
278 }
279 }
280
281 else
282 {
283#ifdef FEAT_FOLDING
284 /* Make sure topline is the first line of a fold. */
285 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
286#endif
287 check_botline = TRUE;
288 }
289 }
290
291 /*
292 * If the cursor is below the bottom of the window, scroll the window
293 * to put the cursor on the window.
294 * When w_botline is invalid, recompute it first, to avoid a redraw later.
295 * If w_botline was approximated, we might need a redraw later in a few
296 * cases, but we don't want to spend (a lot of) time recomputing w_botline
297 * for every small change.
298 */
299 if (check_botline)
300 {
301 if (!(curwin->w_valid & VALID_BOTLINE_AP))
302 validate_botline();
303
304 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
305 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000306 if (curwin->w_cursor.lnum < curwin->w_botline)
307 {
308 if (((long)curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 >= (long)curwin->w_botline - p_so
310#ifdef FEAT_FOLDING
311 || hasAnyFolding(curwin)
312#endif
313 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 lineoff_T loff;
316
Bram Moolenaard4153d42008-11-15 15:06:17 +0000317 /* Cursor is (a few lines) above botline, check if there are
318 * 'scrolloff' window lines below the cursor. If not, need to
319 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 n = curwin->w_empty_rows;
321 loff.lnum = curwin->w_cursor.lnum;
322#ifdef FEAT_FOLDING
323 /* In a fold go to its last line. */
324 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
325#endif
326#ifdef FEAT_DIFF
327 loff.fill = 0;
328 n += curwin->w_filler_rows;
329#endif
330 loff.height = 0;
331 while (loff.lnum < curwin->w_botline
332#ifdef FEAT_DIFF
333 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
334#endif
335 )
336 {
337 n += loff.height;
338 if (n >= p_so)
339 break;
340 botline_forw(&loff);
341 }
342 if (n >= p_so)
343 /* sufficient context, no need to scroll */
344 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000345 }
346 else
347 /* sufficient context, no need to scroll */
348 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 }
350 if (check_botline)
351 {
352#ifdef FEAT_FOLDING
353 if (hasAnyFolding(curwin))
354 {
355 /* Count the number of logical lines between the cursor and
356 * botline - p_so (approximation of how much will be
357 * scrolled). */
358 line_count = 0;
359 for (lnum = curwin->w_cursor.lnum;
360 lnum >= curwin->w_botline - p_so; --lnum)
361 {
362 ++line_count;
363 /* stop at end of file or when we know we are far off */
364 if (lnum <= 0 || line_count > curwin->w_height + 1)
365 break;
366 (void)hasFolding(lnum, &lnum, NULL);
367 }
368 }
369 else
370#endif
371 line_count = curwin->w_cursor.lnum - curwin->w_botline
372 + 1 + p_so;
373 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000374 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 else
376 scroll_cursor_halfway(FALSE);
377 }
378 }
379 }
380 curwin->w_valid |= VALID_TOPLINE;
381
382 /*
383 * Need to redraw when topline changed.
384 */
385 if (curwin->w_topline != old_topline
386#ifdef FEAT_DIFF
387 || curwin->w_topfill != old_topfill
388#endif
389 )
390 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100391 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000392 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {
394 curwin->w_skipcol = 0;
395 redraw_later(NOT_VALID);
396 }
397 else
398 redraw_later(VALID);
399 /* May need to set w_skipcol when cursor in w_topline. */
400 if (curwin->w_cursor.lnum == curwin->w_topline)
401 validate_cursor();
402 }
403
404#ifdef FEAT_MOUSE
405 p_so = save_so;
406#endif
407}
408
409/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000410 * Return the scrolljump value to use for the current window.
411 * When 'scrolljump' is positive use it as-is.
412 * When 'scrolljump' is negative use it as a percentage of the window height.
413 */
414 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100415scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000416{
417 if (p_sj >= 0)
418 return (int)p_sj;
419 return (curwin->w_height * -p_sj) / 100;
420}
421
422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
424 * current window.
425 */
426 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 lineoff_T loff;
430 int n;
431
432 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
433#ifdef FEAT_FOLDING
434 || hasAnyFolding(curwin)
435#endif
436 )
437 {
438 loff.lnum = curwin->w_cursor.lnum;
439#ifdef FEAT_DIFF
440 loff.fill = 0;
441 n = curwin->w_topfill; /* always have this context */
442#else
443 n = 0;
444#endif
445 /* Count the visible screen lines above the cursor line. */
446 while (n < p_so)
447 {
448 topline_back(&loff);
449 /* Stop when included a line above the window. */
450 if (loff.lnum < curwin->w_topline
451#ifdef FEAT_DIFF
452 || (loff.lnum == curwin->w_topline && loff.fill > 0)
453#endif
454 )
455 break;
456 n += loff.height;
457 }
458 if (n < p_so)
459 return TRUE;
460 }
461 return FALSE;
462}
463
464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100465update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 if (curwin->w_set_curswant)
468 {
469 validate_virtcol();
470 curwin->w_curswant = curwin->w_virtcol;
471 curwin->w_set_curswant = FALSE;
472 }
473}
474
475/*
476 * Check if the cursor has moved. Set the w_valid flag accordingly.
477 */
478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100479check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
482 {
483 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
484 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
485 wp->w_valid_cursor = wp->w_cursor;
486 wp->w_valid_leftcol = wp->w_leftcol;
487 }
488 else if (wp->w_cursor.col != wp->w_valid_cursor.col
489 || wp->w_leftcol != wp->w_valid_leftcol
490#ifdef FEAT_VIRTUALEDIT
491 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
492#endif
493 )
494 {
495 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
496 wp->w_valid_cursor.col = wp->w_cursor.col;
497 wp->w_valid_leftcol = wp->w_leftcol;
498#ifdef FEAT_VIRTUALEDIT
499 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
500#endif
501 }
502}
503
504/*
505 * Call this function when some window settings have changed, which require
506 * the cursor position, botline and topline to be recomputed and the window to
507 * be redrawn. E.g, when changing the 'wrap' option or folding.
508 */
509 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100510changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 changed_window_setting_win(curwin);
513}
514
515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100516changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 wp->w_lines_valid = 0;
519 changed_line_abv_curs_win(wp);
520 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
521 redraw_win_later(wp, NOT_VALID);
522}
523
524/*
525 * Set wp->w_topline to a certain number.
526 */
527 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100528set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530#ifdef FEAT_FOLDING
531 /* go to first of folded lines */
532 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
533#endif
534 /* Approximate the value of w_botline */
535 wp->w_botline += lnum - wp->w_topline;
536 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000537 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#ifdef FEAT_DIFF
539 wp->w_topfill = 0;
540#endif
541 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
542 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
543 redraw_later(VALID);
544}
545
546/*
547 * Call this function when the length of the cursor line (in screen
548 * characters) has changed, and the change is before the cursor.
549 * Need to take care of w_botline separately!
550 */
551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
555 |VALID_CHEIGHT|VALID_TOPLINE);
556}
557
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
562 |VALID_CHEIGHT|VALID_TOPLINE);
563}
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565/*
566 * Call this function when the length of a line (in screen characters) above
567 * the cursor have changed.
568 * Need to take care of w_botline separately!
569 */
570 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100571changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
574 |VALID_CHEIGHT|VALID_TOPLINE);
575}
576
577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100578changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
581 |VALID_CHEIGHT|VALID_TOPLINE);
582}
583
584/*
585 * Make sure the value of curwin->w_botline is valid.
586 */
587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100588validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 if (!(curwin->w_valid & VALID_BOTLINE))
591 comp_botline(curwin);
592}
593
594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * Mark curwin->w_botline as invalid (because of some change in the buffer).
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
601}
602
603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
607}
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100610approximate_botline_win(
611 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 wp->w_valid &= ~VALID_BOTLINE;
614}
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616/*
617 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
618 */
619 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100620cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 check_cursor_moved(curwin);
623 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
624 (VALID_WROW|VALID_WCOL));
625}
626
627/*
628 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
629 * w_topline must be valid, you may need to call update_topline() first!
630 */
631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 check_cursor_moved(curwin);
635 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
636 curs_columns(TRUE);
637}
638
639#if defined(FEAT_GUI) || defined(PROTO)
640/*
641 * validate w_cline_row.
642 */
643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 /*
647 * First make sure that w_topline is valid (after moving the cursor).
648 */
649 update_topline();
650 check_cursor_moved(curwin);
651 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100652 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654#endif
655
656/*
657 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200658 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 */
660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 linenr_T lnum;
664 int i;
665 int all_invalid;
666 int valid;
667#ifdef FEAT_FOLDING
668 long fold_count;
669#endif
670
671 /* Check if wp->w_lines[].wl_size is invalid */
672 all_invalid = (!redrawing()
673 || wp->w_lines_valid == 0
674 || wp->w_lines[0].wl_lnum > wp->w_topline);
675 i = 0;
676 wp->w_cline_row = 0;
677 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
678 {
679 valid = FALSE;
680 if (!all_invalid && i < wp->w_lines_valid)
681 {
682 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
683 continue; /* skip changed or deleted lines */
684 if (wp->w_lines[i].wl_lnum == lnum)
685 {
686#ifdef FEAT_FOLDING
687 /* Check for newly inserted lines below this row, in which
688 * case we need to check for folded lines. */
689 if (!wp->w_buffer->b_mod_set
690 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
691 || wp->w_buffer->b_mod_top
692 > wp->w_lines[i].wl_lastlnum + 1)
693#endif
694 valid = TRUE;
695 }
696 else if (wp->w_lines[i].wl_lnum > lnum)
697 --i; /* hold at inserted lines */
698 }
699 if (valid
700#ifdef FEAT_DIFF
701 && (lnum != wp->w_topline || !wp->w_p_diff)
702#endif
703 )
704 {
705#ifdef FEAT_FOLDING
706 lnum = wp->w_lines[i].wl_lastlnum + 1;
707 /* Cursor inside folded lines, don't count this row */
708 if (lnum > wp->w_cursor.lnum)
709 break;
710#else
711 ++lnum;
712#endif
713 wp->w_cline_row += wp->w_lines[i].wl_size;
714 }
715 else
716 {
717#ifdef FEAT_FOLDING
718 fold_count = foldedCount(wp, lnum, NULL);
719 if (fold_count)
720 {
721 lnum += fold_count;
722 if (lnum > wp->w_cursor.lnum)
723 break;
724 ++wp->w_cline_row;
725 }
726 else
727#endif
728#ifdef FEAT_DIFF
729 if (lnum == wp->w_topline)
730 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
731 + wp->w_topfill;
732 else
733#endif
734 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
735 }
736 }
737
738 check_cursor_moved(wp);
739 if (!(wp->w_valid & VALID_CHEIGHT))
740 {
741 if (all_invalid
742 || i == wp->w_lines_valid
743 || (i < wp->w_lines_valid
744 && (!wp->w_lines[i].wl_valid
745 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
746 {
747#ifdef FEAT_DIFF
748 if (wp->w_cursor.lnum == wp->w_topline)
749 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
750 TRUE) + wp->w_topfill;
751 else
752#endif
753 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
754#ifdef FEAT_FOLDING
755 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
756 NULL, NULL, TRUE, NULL);
757#endif
758 }
759 else if (i > wp->w_lines_valid)
760 {
761 /* a line that is too long to fit on the last screen line */
762 wp->w_cline_height = 0;
763#ifdef FEAT_FOLDING
764 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
765 NULL, NULL, TRUE, NULL);
766#endif
767 }
768 else
769 {
770 wp->w_cline_height = wp->w_lines[i].wl_size;
771#ifdef FEAT_FOLDING
772 wp->w_cline_folded = wp->w_lines[i].wl_folded;
773#endif
774 }
775 }
776
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100777 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
783 * Validate curwin->w_virtcol only.
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 validate_virtcol_win(curwin);
789}
790
791/*
792 * Validate wp->w_virtcol only.
793 */
794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 check_cursor_moved(wp);
798 if (!(wp->w_valid & VALID_VIRTCOL))
799 {
800 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
801 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000802#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000803 if (wp->w_p_cuc
804# ifdef FEAT_INS_EXPAND
805 && !pum_visible()
806# endif
807 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000808 redraw_win_later(wp, SOME_VALID);
809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811}
812
813/*
814 * Validate curwin->w_cline_height only.
815 */
816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100817validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
819 check_cursor_moved(curwin);
820 if (!(curwin->w_valid & VALID_CHEIGHT))
821 {
822#ifdef FEAT_DIFF
823 if (curwin->w_cursor.lnum == curwin->w_topline)
824 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
825 + curwin->w_topfill;
826 else
827#endif
828 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
829#ifdef FEAT_FOLDING
830 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
831#endif
832 curwin->w_valid |= VALID_CHEIGHT;
833 }
834}
835
836/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000837 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 */
839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100840validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842 colnr_T off;
843 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100844 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846 validate_virtcol();
847 if (!(curwin->w_valid & VALID_WCOL))
848 {
849 col = curwin->w_virtcol;
850 off = curwin_col_off();
851 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200852 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000855 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200856 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100857 && width > 0)
858 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200859 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000860 if (col > (int)curwin->w_leftcol)
861 col -= curwin->w_leftcol;
862 else
863 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 curwin->w_valid |= VALID_WCOL;
867 }
868}
869
870/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200871 * Compute offset of a window, occupied by absolute or relative line number,
872 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 */
874 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar64486672010-05-16 15:46:46 +0200877 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_CMDWIN
879 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
880#endif
881#ifdef FEAT_FOLDING
882 + wp->w_p_fdc
883#endif
884#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200885 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#endif
887 );
888}
889
890 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 return win_col_off(curwin);
894}
895
896/*
897 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200898 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
899 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 */
901 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100902win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
Bram Moolenaar64486672010-05-16 15:46:46 +0200904 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000905 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 return 0;
907}
908
909 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100910curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 return win_col_off2(curwin);
913}
914
915/*
916 * compute curwin->w_wcol and curwin->w_virtcol.
917 * Also updates curwin->w_wrow and curwin->w_cline_row.
918 * Also updates curwin->w_leftcol.
919 */
920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100921curs_columns(
922 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 int diff;
925 int extra; /* offset for first screen line */
926 int off_left, off_right;
927 int n;
928 int p_lines;
929 int width = 0;
930 int textwidth;
931 int new_leftcol;
932 colnr_T startcol;
933 colnr_T endcol;
934 colnr_T prev_skipcol;
935
936 /*
937 * First make sure that w_topline is valid (after moving the cursor).
938 */
939 update_topline();
940
941 /*
942 * Next make sure that w_cline_row is valid.
943 */
944 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100945 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 /*
948 * Compute the number of virtual columns.
949 */
950#ifdef FEAT_FOLDING
951 if (curwin->w_cline_folded)
952 /* In a folded line the cursor is always in the first column */
953 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
954 else
955#endif
956 getvvcol(curwin, &curwin->w_cursor,
957 &startcol, &(curwin->w_virtcol), &endcol);
958
959 /* remove '$' from change command when cursor moves onto it */
960 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100961 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
963 extra = curwin_col_off();
964 curwin->w_wcol = curwin->w_virtcol + extra;
965 endcol += extra;
966
967 /*
968 * Now compute w_wrow, counting screen lines from w_cline_row.
969 */
970 curwin->w_wrow = curwin->w_cline_row;
971
Bram Moolenaar02631462017-09-22 15:20:32 +0200972 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (textwidth <= 0)
974 {
975 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200976 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 curwin->w_wrow = curwin->w_height - 1;
978 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200979 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {
981 width = textwidth + curwin_col_off2();
982
983 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +0200984 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaar6427c602010-02-03 17:43:07 +0100986 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200987 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 curwin->w_wcol -= n * width;
989 curwin->w_wrow += n;
990
991#ifdef FEAT_LINEBREAK
992 /* When cursor wraps to first char of next line in Insert
993 * mode, the 'showbreak' string isn't shown, backup to first
994 * column */
995 if (*p_sbr && *ml_get_cursor() == NUL
996 && curwin->w_wcol == (int)vim_strsize(p_sbr))
997 curwin->w_wcol = 0;
998#endif
999 }
1000 }
1001
1002 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1003 * is not folded.
1004 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001005 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#ifdef FEAT_FOLDING
1007 && !curwin->w_cline_folded
1008#endif
1009 )
1010 {
1011 /*
1012 * If Cursor is left of the screen, scroll rightwards.
1013 * If Cursor is right of the screen, scroll leftwards
1014 * If we get closer to the edge than 'sidescrolloff', scroll a little
1015 * extra
1016 */
1017 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001018 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 - p_siso) + 1;
1020 if (off_left < 0 || off_right > 0)
1021 {
1022 if (off_left < 0)
1023 diff = -off_left;
1024 else
1025 diff = off_right;
1026
1027 /* When far off or not enough room on either side, put cursor in
1028 * middle of window. */
1029 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1030 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1031 else
1032 {
1033 if (diff < p_ss)
1034 diff = p_ss;
1035 if (off_left < 0)
1036 new_leftcol = curwin->w_leftcol - diff;
1037 else
1038 new_leftcol = curwin->w_leftcol + diff;
1039 }
1040 if (new_leftcol < 0)
1041 new_leftcol = 0;
1042 if (new_leftcol != (int)curwin->w_leftcol)
1043 {
1044 curwin->w_leftcol = new_leftcol;
1045 /* screen has to be redrawn with new curwin->w_leftcol */
1046 redraw_later(NOT_VALID);
1047 }
1048 }
1049 curwin->w_wcol -= curwin->w_leftcol;
1050 }
1051 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1052 curwin->w_wcol -= curwin->w_leftcol;
1053 else
1054 curwin->w_wcol = 0;
1055
1056#ifdef FEAT_DIFF
1057 /* Skip over filler lines. At the top use w_topfill, there
1058 * may be some filler lines above the window. */
1059 if (curwin->w_cursor.lnum == curwin->w_topline)
1060 curwin->w_wrow += curwin->w_topfill;
1061 else
1062 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1063#endif
1064
1065 prev_skipcol = curwin->w_skipcol;
1066
1067 p_lines = 0;
1068 if ((curwin->w_wrow >= curwin->w_height
1069 || ((prev_skipcol > 0
1070 || curwin->w_wrow + p_so >= curwin->w_height)
1071 && (p_lines =
1072#ifdef FEAT_DIFF
1073 plines_win_nofill
1074#else
1075 plines_win
1076#endif
1077 (curwin, curwin->w_cursor.lnum, FALSE))
1078 - 1 >= curwin->w_height))
1079 && curwin->w_height != 0
1080 && curwin->w_cursor.lnum == curwin->w_topline
1081 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001082 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 /* Cursor past end of screen. Happens with a single line that does
1085 * not fit on screen. Find a skipcol to show the text around the
1086 * cursor. Avoid scrolling all the time. compute value of "extra":
1087 * 1: Less than "p_so" lines above
1088 * 2: Less than "p_so" lines below
1089 * 3: both of them */
1090 extra = 0;
1091 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1092 extra = 1;
1093 /* Compute last display line of the buffer line that we want at the
1094 * bottom of the window. */
1095 if (p_lines == 0)
1096 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1097 --p_lines;
1098 if (p_lines > curwin->w_wrow + p_so)
1099 n = curwin->w_wrow + p_so;
1100 else
1101 n = p_lines;
1102 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1103 extra += 2;
1104
1105 if (extra == 3 || p_lines < p_so * 2)
1106 {
1107 /* not enough room for 'scrolloff', put cursor in the middle */
1108 n = curwin->w_virtcol / width;
1109 if (n > curwin->w_height / 2)
1110 n -= curwin->w_height / 2;
1111 else
1112 n = 0;
1113 /* don't skip more than necessary */
1114 if (n > p_lines - curwin->w_height + 1)
1115 n = p_lines - curwin->w_height + 1;
1116 curwin->w_skipcol = n * width;
1117 }
1118 else if (extra == 1)
1119 {
1120 /* less then 'scrolloff' lines above, decrease skipcol */
1121 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1122 + width - 1) / width;
1123 if (extra > 0)
1124 {
1125 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1126 extra = curwin->w_skipcol / width;
1127 curwin->w_skipcol -= extra * width;
1128 }
1129 }
1130 else if (extra == 2)
1131 {
1132 /* less then 'scrolloff' lines below, increase skipcol */
1133 endcol = (n - curwin->w_height + 1) * width;
1134 while (endcol > curwin->w_virtcol)
1135 endcol -= width;
1136 if (endcol > curwin->w_skipcol)
1137 curwin->w_skipcol = endcol;
1138 }
1139
1140 curwin->w_wrow -= curwin->w_skipcol / width;
1141 if (curwin->w_wrow >= curwin->w_height)
1142 {
1143 /* small window, make sure cursor is in it */
1144 extra = curwin->w_wrow - curwin->w_height + 1;
1145 curwin->w_skipcol += extra * width;
1146 curwin->w_wrow -= extra;
1147 }
1148
1149 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1150 if (extra > 0)
1151 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1152 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001153 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 else
1156 curwin->w_skipcol = 0;
1157 if (prev_skipcol != curwin->w_skipcol)
1158 redraw_later(NOT_VALID);
1159
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001160#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001161 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1162 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001163# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001164 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001165# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001166 )
1167 redraw_later(SOME_VALID);
1168#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1171}
1172
1173/*
1174 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001177scrolldown(
1178 long line_count,
1179 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 long done = 0; /* total # of physical lines done */
1182 int wrow;
1183 int moved = FALSE;
1184
1185#ifdef FEAT_FOLDING
1186 linenr_T first;
1187
1188 /* Make sure w_topline is at the first of a sequence of folded lines. */
1189 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1190#endif
1191 validate_cursor(); /* w_wrow needs to be valid */
1192 while (line_count-- > 0)
1193 {
1194#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001195 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1196 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 ++curwin->w_topfill;
1199 ++done;
1200 }
1201 else
1202#endif
1203 {
1204 if (curwin->w_topline == 1)
1205 break;
1206 --curwin->w_topline;
1207#ifdef FEAT_DIFF
1208 curwin->w_topfill = 0;
1209#endif
1210#ifdef FEAT_FOLDING
1211 /* A sequence of folded lines only counts for one logical line */
1212 if (hasFolding(curwin->w_topline, &first, NULL))
1213 {
1214 ++done;
1215 if (!byfold)
1216 line_count -= curwin->w_topline - first - 1;
1217 curwin->w_botline -= curwin->w_topline - first;
1218 curwin->w_topline = first;
1219 }
1220 else
1221#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001222 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 --curwin->w_botline; /* approximate w_botline */
1225 invalidate_botline();
1226 }
1227 curwin->w_wrow += done; /* keep w_wrow updated */
1228 curwin->w_cline_row += done; /* keep w_cline_row updated */
1229
1230#ifdef FEAT_DIFF
1231 if (curwin->w_cursor.lnum == curwin->w_topline)
1232 curwin->w_cline_row = 0;
1233 check_topfill(curwin, TRUE);
1234#endif
1235
1236 /*
1237 * Compute the row number of the last row of the cursor line
1238 * and move the cursor onto the displayed part of the window.
1239 */
1240 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001241 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 validate_virtcol();
1244 validate_cheight();
1245 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001246 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1249 {
1250#ifdef FEAT_FOLDING
1251 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1252 {
1253 --wrow;
1254 if (first == 1)
1255 curwin->w_cursor.lnum = 1;
1256 else
1257 curwin->w_cursor.lnum = first - 1;
1258 }
1259 else
1260#endif
1261 wrow -= plines(curwin->w_cursor.lnum--);
1262 curwin->w_valid &=
1263 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1264 moved = TRUE;
1265 }
1266 if (moved)
1267 {
1268#ifdef FEAT_FOLDING
1269 /* Move cursor to first line of closed fold. */
1270 foldAdjustCursor();
1271#endif
1272 coladvance(curwin->w_curswant);
1273 }
1274}
1275
1276/*
1277 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280scrollup(
1281 long line_count,
1282 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1285 linenr_T lnum;
1286
1287 if (
1288# ifdef FEAT_FOLDING
1289 (byfold && hasAnyFolding(curwin))
1290# ifdef FEAT_DIFF
1291 ||
1292# endif
1293# endif
1294# ifdef FEAT_DIFF
1295 curwin->w_p_diff
1296# endif
1297 )
1298 {
1299 /* count each sequence of folded lines as one logical line */
1300 lnum = curwin->w_topline;
1301 while (line_count--)
1302 {
1303# ifdef FEAT_DIFF
1304 if (curwin->w_topfill > 0)
1305 --curwin->w_topfill;
1306 else
1307# endif
1308 {
1309# ifdef FEAT_FOLDING
1310 if (byfold)
1311 (void)hasFolding(lnum, NULL, &lnum);
1312# endif
1313 if (lnum >= curbuf->b_ml.ml_line_count)
1314 break;
1315 ++lnum;
1316# ifdef FEAT_DIFF
1317 curwin->w_topfill = diff_check_fill(curwin, lnum);
1318# endif
1319 }
1320 }
1321 /* approximate w_botline */
1322 curwin->w_botline += lnum - curwin->w_topline;
1323 curwin->w_topline = lnum;
1324 }
1325 else
1326#endif
1327 {
1328 curwin->w_topline += line_count;
1329 curwin->w_botline += line_count; /* approximate w_botline */
1330 }
1331
1332 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1333 curwin->w_topline = curbuf->b_ml.ml_line_count;
1334 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1335 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1336
1337#ifdef FEAT_DIFF
1338 check_topfill(curwin, FALSE);
1339#endif
1340
1341#ifdef FEAT_FOLDING
1342 if (hasAnyFolding(curwin))
1343 /* Make sure w_topline is at the first of a sequence of folded lines. */
1344 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1345#endif
1346
1347 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1348 if (curwin->w_cursor.lnum < curwin->w_topline)
1349 {
1350 curwin->w_cursor.lnum = curwin->w_topline;
1351 curwin->w_valid &=
1352 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1353 coladvance(curwin->w_curswant);
1354 }
1355}
1356
1357#ifdef FEAT_DIFF
1358/*
1359 * Don't end up with too many filler lines in the window.
1360 */
1361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001362check_topfill(
1363 win_T *wp,
1364 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int n;
1367
1368 if (wp->w_topfill > 0)
1369 {
1370 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1371 if (wp->w_topfill + n > wp->w_height)
1372 {
1373 if (down && wp->w_topline > 1)
1374 {
1375 --wp->w_topline;
1376 wp->w_topfill = 0;
1377 }
1378 else
1379 {
1380 wp->w_topfill = wp->w_height - n;
1381 if (wp->w_topfill < 0)
1382 wp->w_topfill = 0;
1383 }
1384 }
1385 }
1386}
1387
1388/*
1389 * Use as many filler lines as possible for w_topline. Make sure w_topline
1390 * is still visible.
1391 */
1392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001393max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 int n;
1396
1397 n = plines_nofill(curwin->w_topline);
1398 if (n >= curwin->w_height)
1399 curwin->w_topfill = 0;
1400 else
1401 {
1402 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1403 if (curwin->w_topfill + n > curwin->w_height)
1404 curwin->w_topfill = curwin->w_height - n;
1405 }
1406}
1407#endif
1408
1409#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1410/*
1411 * Scroll the screen one line down, but don't do it if it would move the
1412 * cursor off the screen.
1413 */
1414 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001415scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 int end_row;
1418#ifdef FEAT_DIFF
1419 int can_fill = (curwin->w_topfill
1420 < diff_check_fill(curwin, curwin->w_topline));
1421#endif
1422
1423 if (curwin->w_topline <= 1
1424#ifdef FEAT_DIFF
1425 && !can_fill
1426#endif
1427 )
1428 return;
1429
1430 validate_cursor(); /* w_wrow needs to be valid */
1431
1432 /*
1433 * Compute the row number of the last row of the cursor line
1434 * and make sure it doesn't go off the screen. Make sure the cursor
1435 * doesn't go past 'scrolloff' lines from the screen end.
1436 */
1437 end_row = curwin->w_wrow;
1438#ifdef FEAT_DIFF
1439 if (can_fill)
1440 ++end_row;
1441 else
1442 end_row += plines_nofill(curwin->w_topline - 1);
1443#else
1444 end_row += plines(curwin->w_topline - 1);
1445#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001446 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 {
1448 validate_cheight();
1449 validate_virtcol();
1450 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001451 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453 if (end_row < curwin->w_height - p_so)
1454 {
1455#ifdef FEAT_DIFF
1456 if (can_fill)
1457 {
1458 ++curwin->w_topfill;
1459 check_topfill(curwin, TRUE);
1460 }
1461 else
1462 {
1463 --curwin->w_topline;
1464 curwin->w_topfill = 0;
1465 }
1466#else
1467 --curwin->w_topline;
1468#endif
1469#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001470 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#endif
1472 --curwin->w_botline; /* approximate w_botline */
1473 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1474 }
1475}
1476
1477/*
1478 * Scroll the screen one line up, but don't do it if it would move the cursor
1479 * off the screen.
1480 */
1481 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001482scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 int start_row;
1485
1486 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1487#ifdef FEAT_DIFF
1488 && curwin->w_topfill == 0
1489#endif
1490 )
1491 return;
1492
1493 validate_cursor(); /* w_wrow needs to be valid */
1494
1495 /*
1496 * Compute the row number of the first row of the cursor line
1497 * and make sure it doesn't go off the screen. Make sure the cursor
1498 * doesn't go before 'scrolloff' lines from the screen start.
1499 */
1500#ifdef FEAT_DIFF
1501 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1502 - curwin->w_topfill;
1503#else
1504 start_row = curwin->w_wrow - plines(curwin->w_topline);
1505#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001506 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001509 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 if (start_row >= p_so)
1512 {
1513#ifdef FEAT_DIFF
1514 if (curwin->w_topfill > 0)
1515 --curwin->w_topfill;
1516 else
1517#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001518 {
1519#ifdef FEAT_FOLDING
1520 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 ++curwin->w_botline; /* approximate w_botline */
1525 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1526 }
1527}
1528#endif /* FEAT_INS_EXPAND */
1529
1530/*
1531 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1532 * a (wrapped) text line. Uses and sets "lp->fill".
1533 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001534 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 */
1536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539#ifdef FEAT_DIFF
1540 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1541 {
1542 /* Add a filler line. */
1543 ++lp->fill;
1544 lp->height = 1;
1545 }
1546 else
1547#endif
1548 {
1549 --lp->lnum;
1550#ifdef FEAT_DIFF
1551 lp->fill = 0;
1552#endif
1553 if (lp->lnum < 1)
1554 lp->height = MAXCOL;
1555 else
1556#ifdef FEAT_FOLDING
1557 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1558 /* Add a closed fold */
1559 lp->height = 1;
1560 else
1561#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001562 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564}
1565
1566/*
1567 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1568 * a (wrapped) text line. Uses and sets "lp->fill".
1569 * Returns the height of the added line in "lp->height".
1570 * Lines below the last one are incredibly high.
1571 */
1572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001573botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575#ifdef FEAT_DIFF
1576 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1577 {
1578 /* Add a filler line. */
1579 ++lp->fill;
1580 lp->height = 1;
1581 }
1582 else
1583#endif
1584 {
1585 ++lp->lnum;
1586#ifdef FEAT_DIFF
1587 lp->fill = 0;
1588#endif
1589 if (lp->lnum > curbuf->b_ml.ml_line_count)
1590 lp->height = MAXCOL;
1591 else
1592#ifdef FEAT_FOLDING
1593 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1594 /* Add a closed fold */
1595 lp->height = 1;
1596 else
1597#endif
1598 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001599 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602}
1603
1604#ifdef FEAT_DIFF
1605/*
1606 * Switch from including filler lines below lp->lnum to including filler
1607 * lines above loff.lnum + 1. This keeps pointing to the same line.
1608 * When there are no filler lines nothing changes.
1609 */
1610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001611botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 if (lp->fill > 0)
1614 {
1615 ++lp->lnum;
1616 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1617 }
1618}
1619
1620/*
1621 * Switch from including filler lines above lp->lnum to including filler
1622 * lines below loff.lnum - 1. This keeps pointing to the same line.
1623 * When there are no filler lines nothing changes.
1624 */
1625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001626topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
1628 if (lp->fill > 0)
1629 {
1630 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1631 --lp->lnum;
1632 }
1633}
1634#endif
1635
1636/*
1637 * Recompute topline to put the cursor at the top of the window.
1638 * Scroll at least "min_scroll" lines.
1639 * If "always" is TRUE, always set topline (for "zt").
1640 */
1641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001642scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 int scrolled = 0;
1645 int extra = 0;
1646 int used;
1647 int i;
1648 linenr_T top; /* just above displayed lines */
1649 linenr_T bot; /* just below displayed lines */
1650 linenr_T old_topline = curwin->w_topline;
1651#ifdef FEAT_DIFF
1652 linenr_T old_topfill = curwin->w_topfill;
1653#endif
1654 linenr_T new_topline;
1655 int off = p_so;
1656
1657#ifdef FEAT_MOUSE
1658 if (mouse_dragging > 0)
1659 off = mouse_dragging - 1;
1660#endif
1661
1662 /*
1663 * Decrease topline until:
1664 * - it has become 1
1665 * - (part of) the cursor line is moved off the screen or
1666 * - moved at least 'scrolljump' lines and
1667 * - at least 'scrolloff' lines above and below the cursor
1668 */
1669 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001670 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (curwin->w_cursor.lnum < curwin->w_topline)
1672 scrolled = used;
1673
1674#ifdef FEAT_FOLDING
1675 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1676 {
1677 --top;
1678 ++bot;
1679 }
1680 else
1681#endif
1682 {
1683 top = curwin->w_cursor.lnum - 1;
1684 bot = curwin->w_cursor.lnum + 1;
1685 }
1686 new_topline = top + 1;
1687
1688#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001689 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001690 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001691 * Hide filler lines above cursor line by adding them to "extra". */
1692 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
1694
1695 /*
1696 * Check if the lines from "top" to "bot" fit in the window. If they do,
1697 * set new_topline and advance "top" and "bot" to include more lines.
1698 */
1699 while (top > 0)
1700 {
1701#ifdef FEAT_FOLDING
1702 if (hasFolding(top, &top, NULL))
1703 /* count one logical line for a sequence of folded lines */
1704 i = 1;
1705 else
1706#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001707 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 used += i;
1709 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1710 {
1711#ifdef FEAT_FOLDING
1712 if (hasFolding(bot, NULL, &bot))
1713 /* count one logical line for a sequence of folded lines */
1714 ++used;
1715 else
1716#endif
1717 used += plines(bot);
1718 }
1719 if (used > curwin->w_height)
1720 break;
1721 if (top < curwin->w_topline)
1722 scrolled += i;
1723
1724 /*
1725 * If scrolling is needed, scroll at least 'sj' lines.
1726 */
1727 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1728 && extra >= off)
1729 break;
1730
1731 extra += i;
1732 new_topline = top;
1733 --top;
1734 ++bot;
1735 }
1736
1737 /*
1738 * If we don't have enough space, put cursor in the middle.
1739 * This makes sure we get the same position when using "k" and "j"
1740 * in a small window.
1741 */
1742 if (used > curwin->w_height)
1743 scroll_cursor_halfway(FALSE);
1744 else
1745 {
1746 /*
1747 * If "always" is FALSE, only adjust topline to a lower value, higher
1748 * value may happen with wrapping lines
1749 */
1750 if (new_topline < curwin->w_topline || always)
1751 curwin->w_topline = new_topline;
1752 if (curwin->w_topline > curwin->w_cursor.lnum)
1753 curwin->w_topline = curwin->w_cursor.lnum;
1754#ifdef FEAT_DIFF
1755 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1756 if (curwin->w_topfill > 0 && extra > off)
1757 {
1758 curwin->w_topfill -= extra - off;
1759 if (curwin->w_topfill < 0)
1760 curwin->w_topfill = 0;
1761 }
1762 check_topfill(curwin, FALSE);
1763#endif
1764 if (curwin->w_topline != old_topline
1765#ifdef FEAT_DIFF
1766 || curwin->w_topfill != old_topfill
1767#endif
1768 )
1769 curwin->w_valid &=
1770 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1771 curwin->w_valid |= VALID_TOPLINE;
1772 }
1773}
1774
1775/*
1776 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1777 * screen lines for text lines.
1778 */
1779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782#ifdef FEAT_DIFF
1783 wp->w_filler_rows = 0;
1784#endif
1785 if (used == 0)
1786 wp->w_empty_rows = 0; /* single line that doesn't fit */
1787 else
1788 {
1789 wp->w_empty_rows = wp->w_height - used;
1790#ifdef FEAT_DIFF
1791 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1792 {
1793 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1794 if (wp->w_empty_rows > wp->w_filler_rows)
1795 wp->w_empty_rows -= wp->w_filler_rows;
1796 else
1797 {
1798 wp->w_filler_rows = wp->w_empty_rows;
1799 wp->w_empty_rows = 0;
1800 }
1801 }
1802#endif
1803 }
1804}
1805
1806/*
1807 * Recompute topline to put the cursor at the bottom of the window.
1808 * Scroll at least "min_scroll" lines.
1809 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1810 * This is messy stuff!!!
1811 */
1812 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001813scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 int used;
1816 int scrolled = 0;
1817 int extra = 0;
1818 int i;
1819 linenr_T line_count;
1820 linenr_T old_topline = curwin->w_topline;
1821 lineoff_T loff;
1822 lineoff_T boff;
1823#ifdef FEAT_DIFF
1824 int old_topfill = curwin->w_topfill;
1825 int fill_below_window;
1826#endif
1827 linenr_T old_botline = curwin->w_botline;
1828 linenr_T old_valid = curwin->w_valid;
1829 int old_empty_rows = curwin->w_empty_rows;
1830 linenr_T cln; /* Cursor Line Number */
1831
1832 cln = curwin->w_cursor.lnum;
1833 if (set_topbot)
1834 {
1835 used = 0;
1836 curwin->w_botline = cln + 1;
1837#ifdef FEAT_DIFF
1838 loff.fill = 0;
1839#endif
1840 for (curwin->w_topline = curwin->w_botline;
1841 curwin->w_topline > 1;
1842 curwin->w_topline = loff.lnum)
1843 {
1844 loff.lnum = curwin->w_topline;
1845 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001846 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 break;
1848 used += loff.height;
1849#ifdef FEAT_DIFF
1850 curwin->w_topfill = loff.fill;
1851#endif
1852 }
1853 set_empty_rows(curwin, used);
1854 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1855 if (curwin->w_topline != old_topline
1856#ifdef FEAT_DIFF
1857 || curwin->w_topfill != old_topfill
1858#endif
1859 )
1860 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1861 }
1862 else
1863 validate_botline();
1864
1865 /* The lines of the cursor line itself are always used. */
1866#ifdef FEAT_DIFF
1867 used = plines_nofill(cln);
1868#else
1869 validate_cheight();
1870 used = curwin->w_cline_height;
1871#endif
1872
1873 /* If the cursor is below botline, we will at least scroll by the height
1874 * of the cursor line. Correct for empty lines, which are really part of
1875 * botline. */
1876 if (cln >= curwin->w_botline)
1877 {
1878 scrolled = used;
1879 if (cln == curwin->w_botline)
1880 scrolled -= curwin->w_empty_rows;
1881 }
1882
1883 /*
1884 * Stop counting lines to scroll when
1885 * - hitting start of the file
1886 * - scrolled nothing or at least 'sj' lines
1887 * - at least 'so' lines below the cursor
1888 * - lines between botline and cursor have been counted
1889 */
1890#ifdef FEAT_FOLDING
1891 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1892#endif
1893 {
1894 loff.lnum = cln;
1895 boff.lnum = cln;
1896 }
1897#ifdef FEAT_DIFF
1898 loff.fill = 0;
1899 boff.fill = 0;
1900 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1901 - curwin->w_filler_rows;
1902#endif
1903
1904 while (loff.lnum > 1)
1905 {
1906 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1907 * context for 'scrolloff' and counted all lines below the window. */
1908 if ((((scrolled <= 0 || scrolled >= min_scroll)
1909 && extra >= (
1910#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00001911 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912#endif
1913 p_so))
1914 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1915 && loff.lnum <= curwin->w_botline
1916#ifdef FEAT_DIFF
1917 && (loff.lnum < curwin->w_botline
1918 || loff.fill >= fill_below_window)
1919#endif
1920 )
1921 break;
1922
1923 /* Add one line above */
1924 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001925 if (loff.height == MAXCOL)
1926 used = MAXCOL;
1927 else
1928 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (used > curwin->w_height)
1930 break;
1931 if (loff.lnum >= curwin->w_botline
1932#ifdef FEAT_DIFF
1933 && (loff.lnum > curwin->w_botline
1934 || loff.fill <= fill_below_window)
1935#endif
1936 )
1937 {
1938 /* Count screen lines that are below the window. */
1939 scrolled += loff.height;
1940 if (loff.lnum == curwin->w_botline
1941#ifdef FEAT_DIFF
1942 && boff.fill == 0
1943#endif
1944 )
1945 scrolled -= curwin->w_empty_rows;
1946 }
1947
1948 if (boff.lnum < curbuf->b_ml.ml_line_count)
1949 {
1950 /* Add one line below */
1951 botline_forw(&boff);
1952 used += boff.height;
1953 if (used > curwin->w_height)
1954 break;
1955 if (extra < (
1956#ifdef FEAT_MOUSE
1957 mouse_dragging > 0 ? mouse_dragging - 1 :
1958#endif
1959 p_so) || scrolled < min_scroll)
1960 {
1961 extra += boff.height;
1962 if (boff.lnum >= curwin->w_botline
1963#ifdef FEAT_DIFF
1964 || (boff.lnum + 1 == curwin->w_botline
1965 && boff.fill > curwin->w_filler_rows)
1966#endif
1967 )
1968 {
1969 /* Count screen lines that are below the window. */
1970 scrolled += boff.height;
1971 if (boff.lnum == curwin->w_botline
1972#ifdef FEAT_DIFF
1973 && boff.fill == 0
1974#endif
1975 )
1976 scrolled -= curwin->w_empty_rows;
1977 }
1978 }
1979 }
1980 }
1981
1982 /* curwin->w_empty_rows is larger, no need to scroll */
1983 if (scrolled <= 0)
1984 line_count = 0;
1985 /* more than a screenfull, don't scroll but redraw */
1986 else if (used > curwin->w_height)
1987 line_count = used;
1988 /* scroll minimal number of lines */
1989 else
1990 {
1991 line_count = 0;
1992#ifdef FEAT_DIFF
1993 boff.fill = curwin->w_topfill;
1994#endif
1995 boff.lnum = curwin->w_topline - 1;
1996 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
1997 {
1998 botline_forw(&boff);
1999 i += boff.height;
2000 ++line_count;
2001 }
2002 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2003 line_count = 9999;
2004 }
2005
2006 /*
2007 * Scroll up if the cursor is off the bottom of the screen a bit.
2008 * Otherwise put it at 1/2 of the screen.
2009 */
2010 if (line_count >= curwin->w_height && line_count > min_scroll)
2011 scroll_cursor_halfway(FALSE);
2012 else
2013 scrollup(line_count, TRUE);
2014
2015 /*
2016 * If topline didn't change we need to restore w_botline and w_empty_rows
2017 * (we changed them).
2018 * If topline did change, update_screen() will set botline.
2019 */
2020 if (curwin->w_topline == old_topline && set_topbot)
2021 {
2022 curwin->w_botline = old_botline;
2023 curwin->w_empty_rows = old_empty_rows;
2024 curwin->w_valid = old_valid;
2025 }
2026 curwin->w_valid |= VALID_TOPLINE;
2027}
2028
2029/*
2030 * Recompute topline to put the cursor halfway the window
2031 * If "atend" is TRUE, also put it halfway at the end of the file.
2032 */
2033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int above = 0;
2037 linenr_T topline;
2038#ifdef FEAT_DIFF
2039 int topfill = 0;
2040#endif
2041 int below = 0;
2042 int used;
2043 lineoff_T loff;
2044 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002045#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002046 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2050#ifdef FEAT_FOLDING
2051 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2052#endif
2053#ifdef FEAT_DIFF
2054 used = plines_nofill(loff.lnum);
2055 loff.fill = 0;
2056 boff.fill = 0;
2057#else
2058 used = plines(loff.lnum);
2059#endif
2060 topline = loff.lnum;
2061 while (topline > 1)
2062 {
2063 if (below <= above) /* add a line below the cursor first */
2064 {
2065 if (boff.lnum < curbuf->b_ml.ml_line_count)
2066 {
2067 botline_forw(&boff);
2068 used += boff.height;
2069 if (used > curwin->w_height)
2070 break;
2071 below += boff.height;
2072 }
2073 else
2074 {
2075 ++below; /* count a "~" line */
2076 if (atend)
2077 ++used;
2078 }
2079 }
2080
2081 if (below > above) /* add a line above the cursor */
2082 {
2083 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002084 if (loff.height == MAXCOL)
2085 used = MAXCOL;
2086 else
2087 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (used > curwin->w_height)
2089 break;
2090 above += loff.height;
2091 topline = loff.lnum;
2092#ifdef FEAT_DIFF
2093 topfill = loff.fill;
2094#endif
2095 }
2096 }
2097#ifdef FEAT_FOLDING
2098 if (!hasFolding(topline, &curwin->w_topline, NULL))
2099#endif
2100 curwin->w_topline = topline;
2101#ifdef FEAT_DIFF
2102 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002103 if (old_topline > curwin->w_topline + curwin->w_height)
2104 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 check_topfill(curwin, FALSE);
2106#endif
2107 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2108 curwin->w_valid |= VALID_TOPLINE;
2109}
2110
2111/*
2112 * Correct the cursor position so that it is in a part of the screen at least
2113 * 'so' lines from the top and bottom, if possible.
2114 * If not possible, put it at the same position as scroll_cursor_halfway().
2115 * When called topline must be valid!
2116 */
2117 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 int above = 0; /* screen lines above topline */
2121 linenr_T topline;
2122 int below = 0; /* screen lines below botline */
2123 linenr_T botline;
2124 int above_wanted, below_wanted;
2125 linenr_T cln; /* Cursor Line Number */
2126 int max_off;
2127
2128 /*
2129 * How many lines we would like to have above/below the cursor depends on
2130 * whether the first/last line of the file is on screen.
2131 */
2132 above_wanted = p_so;
2133 below_wanted = p_so;
2134#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002135 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 above_wanted = mouse_dragging - 1;
2138 below_wanted = mouse_dragging - 1;
2139 }
2140#endif
2141 if (curwin->w_topline == 1)
2142 {
2143 above_wanted = 0;
2144 max_off = curwin->w_height / 2;
2145 if (below_wanted > max_off)
2146 below_wanted = max_off;
2147 }
2148 validate_botline();
2149 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2150#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002151 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152#endif
2153 )
2154 {
2155 below_wanted = 0;
2156 max_off = (curwin->w_height - 1) / 2;
2157 if (above_wanted > max_off)
2158 above_wanted = max_off;
2159 }
2160
2161 /*
2162 * If there are sufficient file-lines above and below the cursor, we can
2163 * return now.
2164 */
2165 cln = curwin->w_cursor.lnum;
2166 if (cln >= curwin->w_topline + above_wanted
2167 && cln < curwin->w_botline - below_wanted
2168#ifdef FEAT_FOLDING
2169 && !hasAnyFolding(curwin)
2170#endif
2171 )
2172 return;
2173
2174 /*
2175 * Narrow down the area where the cursor can be put by taking lines from
2176 * the top and the bottom until:
2177 * - the desired context lines are found
2178 * - the lines from the top is past the lines from the bottom
2179 */
2180 topline = curwin->w_topline;
2181 botline = curwin->w_botline - 1;
2182#ifdef FEAT_DIFF
2183 /* count filler lines as context */
2184 above = curwin->w_topfill;
2185 below = curwin->w_filler_rows;
2186#endif
2187 while ((above < above_wanted || below < below_wanted) && topline < botline)
2188 {
2189 if (below < below_wanted && (below <= above || above >= above_wanted))
2190 {
2191#ifdef FEAT_FOLDING
2192 if (hasFolding(botline, &botline, NULL))
2193 ++below;
2194 else
2195#endif
2196 below += plines(botline);
2197 --botline;
2198 }
2199 if (above < above_wanted && (above < below || below >= below_wanted))
2200 {
2201#ifdef FEAT_FOLDING
2202 if (hasFolding(topline, NULL, &topline))
2203 ++above;
2204 else
2205#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002206 above += PLINES_NOFILL(topline);
2207#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /* Count filler lines below this line as context. */
2209 if (topline < botline)
2210 above += diff_check_fill(curwin, topline + 1);
2211#endif
2212 ++topline;
2213 }
2214 }
2215 if (topline == botline || botline == 0)
2216 curwin->w_cursor.lnum = topline;
2217 else if (topline > botline)
2218 curwin->w_cursor.lnum = botline;
2219 else
2220 {
2221 if (cln < topline && curwin->w_topline > 1)
2222 {
2223 curwin->w_cursor.lnum = topline;
2224 curwin->w_valid &=
2225 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2226 }
2227 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2228 {
2229 curwin->w_cursor.lnum = botline;
2230 curwin->w_valid &=
2231 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2232 }
2233 }
2234 curwin->w_valid |= VALID_TOPLINE;
2235}
2236
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002237static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239/*
2240 * move screen 'count' pages up or down and update screen
2241 *
2242 * return FAIL for failure, OK otherwise
2243 */
2244 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002245onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 long n;
2248 int retval = OK;
2249 lineoff_T loff;
2250 linenr_T old_topline = curwin->w_topline;
2251
2252 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2253 {
2254 beep_flush();
2255 return FAIL;
2256 }
2257
2258 for ( ; count > 0; --count)
2259 {
2260 validate_botline();
2261 /*
2262 * It's an error to move a page up when the first line is already on
2263 * the screen. It's an error to move a page down when the last line
2264 * is on the screen and the topline is 'scrolloff' lines from the
2265 * last line.
2266 */
2267 if (dir == FORWARD
2268 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2269 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2270 : (curwin->w_topline == 1
2271#ifdef FEAT_DIFF
2272 && curwin->w_topfill ==
2273 diff_check_fill(curwin, curwin->w_topline)
2274#endif
2275 ))
2276 {
2277 beep_flush();
2278 retval = FAIL;
2279 break;
2280 }
2281
2282#ifdef FEAT_DIFF
2283 loff.fill = 0;
2284#endif
2285 if (dir == FORWARD)
2286 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002287 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002289 /* Vi compatible scrolling */
2290 if (p_window <= 2)
2291 ++curwin->w_topline;
2292 else
2293 curwin->w_topline += p_window - 2;
2294 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2295 curwin->w_topline = curbuf->b_ml.ml_line_count;
2296 curwin->w_cursor.lnum = curwin->w_topline;
2297 }
2298 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2299 {
2300 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 curwin->w_topline = curbuf->b_ml.ml_line_count;
2302#ifdef FEAT_DIFF
2303 curwin->w_topfill = 0;
2304#endif
2305 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2306 }
2307 else
2308 {
2309 /* For the overlap, start with the line just below the window
2310 * and go upwards. */
2311 loff.lnum = curwin->w_botline;
2312#ifdef FEAT_DIFF
2313 loff.fill = diff_check_fill(curwin, loff.lnum)
2314 - curwin->w_filler_rows;
2315#endif
2316 get_scroll_overlap(&loff, -1);
2317 curwin->w_topline = loff.lnum;
2318#ifdef FEAT_DIFF
2319 curwin->w_topfill = loff.fill;
2320 check_topfill(curwin, FALSE);
2321#endif
2322 curwin->w_cursor.lnum = curwin->w_topline;
2323 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2324 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2325 }
2326 }
2327 else /* dir == BACKWARDS */
2328 {
2329#ifdef FEAT_DIFF
2330 if (curwin->w_topline == 1)
2331 {
2332 /* Include max number of filler lines */
2333 max_topfill();
2334 continue;
2335 }
2336#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002337 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002338 {
2339 /* Vi compatible scrolling (sort of) */
2340 if (p_window <= 2)
2341 --curwin->w_topline;
2342 else
2343 curwin->w_topline -= p_window - 2;
2344 if (curwin->w_topline < 1)
2345 curwin->w_topline = 1;
2346 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2347 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2348 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2349 continue;
2350 }
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* Find the line at the top of the window that is going to be the
2353 * line at the bottom of the window. Make sure this results in
2354 * the same line as before doing CTRL-F. */
2355 loff.lnum = curwin->w_topline - 1;
2356#ifdef FEAT_DIFF
2357 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2358 - curwin->w_topfill;
2359#endif
2360 get_scroll_overlap(&loff, 1);
2361
2362 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2363 {
2364 loff.lnum = curbuf->b_ml.ml_line_count;
2365#ifdef FEAT_DIFF
2366 loff.fill = 0;
2367 }
2368 else
2369 {
2370 botline_topline(&loff);
2371#endif
2372 }
2373 curwin->w_cursor.lnum = loff.lnum;
2374
2375 /* Find the line just above the new topline to get the right line
2376 * at the bottom of the window. */
2377 n = 0;
2378 while (n <= curwin->w_height && loff.lnum >= 1)
2379 {
2380 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002381 if (loff.height == MAXCOL)
2382 n = MAXCOL;
2383 else
2384 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002386 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 curwin->w_topline = 1;
2389#ifdef FEAT_DIFF
2390 max_topfill();
2391#endif
2392 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2393 }
2394 else
2395 {
2396 /* Go two lines forward again. */
2397#ifdef FEAT_DIFF
2398 topline_botline(&loff);
2399#endif
2400 botline_forw(&loff);
2401 botline_forw(&loff);
2402#ifdef FEAT_DIFF
2403 botline_topline(&loff);
2404#endif
2405#ifdef FEAT_FOLDING
2406 /* We're at the wrong end of a fold now. */
2407 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2408#endif
2409
2410 /* Always scroll at least one line. Avoid getting stuck on
2411 * very long lines. */
2412 if (loff.lnum >= curwin->w_topline
2413#ifdef FEAT_DIFF
2414 && (loff.lnum > curwin->w_topline
2415 || loff.fill >= curwin->w_topfill)
2416#endif
2417 )
2418 {
2419#ifdef FEAT_DIFF
2420 /* First try using the maximum number of filler lines. If
2421 * that's not enough, backup one line. */
2422 loff.fill = curwin->w_topfill;
2423 if (curwin->w_topfill < diff_check_fill(curwin,
2424 curwin->w_topline))
2425 max_topfill();
2426 if (curwin->w_topfill == loff.fill)
2427#endif
2428 {
2429 --curwin->w_topline;
2430#ifdef FEAT_DIFF
2431 curwin->w_topfill = 0;
2432#endif
2433 }
2434 comp_botline(curwin);
2435 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002436 curwin->w_valid &=
2437 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 else
2440 {
2441 curwin->w_topline = loff.lnum;
2442#ifdef FEAT_DIFF
2443 curwin->w_topfill = loff.fill;
2444 check_topfill(curwin, FALSE);
2445#endif
2446 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2447 }
2448 }
2449 }
2450 }
2451#ifdef FEAT_FOLDING
2452 foldAdjustCursor();
2453#endif
2454 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002455 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002456 if (retval == OK)
2457 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2459
Bram Moolenaar907dad72018-07-10 15:07:15 +02002460 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002462 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2463 // But make sure we scroll at least one line (happens with mix of long
2464 // wrapping lines and non-wrapping line).
2465 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002467 scroll_cursor_top(1, FALSE);
2468 if (curwin->w_topline <= old_topline
2469 && old_topline < curbuf->b_ml.ml_line_count)
2470 {
2471 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002473 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2474#endif
2475 }
2476 }
2477#ifdef FEAT_FOLDING
2478 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 redraw_later(VALID);
2484 return retval;
2485}
2486
2487/*
2488 * Decide how much overlap to use for page-up or page-down scrolling.
2489 * This is symmetric, so that doing both keeps the same lines displayed.
2490 * Three lines are examined:
2491 *
2492 * before CTRL-F after CTRL-F / before CTRL-B
2493 * etc. l1
2494 * l1 last but one line ------------
2495 * l2 last text line l2 top text line
2496 * ------------- l3 second text line
2497 * l3 etc.
2498 */
2499 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002500get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
2502 int h1, h2, h3, h4;
2503 int min_height = curwin->w_height - 2;
2504 lineoff_T loff0, loff1, loff2;
2505
2506#ifdef FEAT_DIFF
2507 if (lp->fill > 0)
2508 lp->height = 1;
2509 else
2510 lp->height = plines_nofill(lp->lnum);
2511#else
2512 lp->height = plines(lp->lnum);
2513#endif
2514 h1 = lp->height;
2515 if (h1 > min_height)
2516 return; /* no overlap */
2517
2518 loff0 = *lp;
2519 if (dir > 0)
2520 botline_forw(lp);
2521 else
2522 topline_back(lp);
2523 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002524 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 *lp = loff0; /* no overlap */
2527 return;
2528 }
2529
2530 loff1 = *lp;
2531 if (dir > 0)
2532 botline_forw(lp);
2533 else
2534 topline_back(lp);
2535 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002536 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 *lp = loff0; /* no overlap */
2539 return;
2540 }
2541
2542 loff2 = *lp;
2543 if (dir > 0)
2544 botline_forw(lp);
2545 else
2546 topline_back(lp);
2547 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002548 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 *lp = loff1; /* 1 line overlap */
2550 else
2551 *lp = loff2; /* 2 lines overlap */
2552 return;
2553}
2554
2555/* #define KEEP_SCREEN_LINE */
2556/*
2557 * Scroll 'scroll' lines up or down.
2558 */
2559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002560halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 long scrolled = 0;
2563 int i;
2564 int n;
2565 int room;
2566
2567 if (Prenum)
2568 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2569 curwin->w_height : Prenum;
2570 n = (curwin->w_p_scr <= curwin->w_height) ?
2571 curwin->w_p_scr : curwin->w_height;
2572
Bram Moolenaard5d37532017-03-27 23:02:07 +02002573 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 validate_botline();
2575 room = curwin->w_empty_rows;
2576#ifdef FEAT_DIFF
2577 room += curwin->w_filler_rows;
2578#endif
2579 if (flag)
2580 {
2581 /*
2582 * scroll the text up
2583 */
2584 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2585 {
2586#ifdef FEAT_DIFF
2587 if (curwin->w_topfill > 0)
2588 {
2589 i = 1;
2590 if (--n < 0 && scrolled > 0)
2591 break;
2592 --curwin->w_topfill;
2593 }
2594 else
2595#endif
2596 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002597 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 n -= i;
2599 if (n < 0 && scrolled > 0)
2600 break;
2601#ifdef FEAT_FOLDING
2602 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2603#endif
2604 ++curwin->w_topline;
2605#ifdef FEAT_DIFF
2606 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2607#endif
2608
2609#ifndef KEEP_SCREEN_LINE
2610 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2611 {
2612 ++curwin->w_cursor.lnum;
2613 curwin->w_valid &=
2614 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2615 }
2616#endif
2617 }
2618 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2619 scrolled += i;
2620
2621 /*
2622 * Correct w_botline for changed w_topline.
2623 * Won't work when there are filler lines.
2624 */
2625#ifdef FEAT_DIFF
2626 if (curwin->w_p_diff)
2627 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2628 else
2629#endif
2630 {
2631 room += i;
2632 do
2633 {
2634 i = plines(curwin->w_botline);
2635 if (i > room)
2636 break;
2637#ifdef FEAT_FOLDING
2638 (void)hasFolding(curwin->w_botline, NULL,
2639 &curwin->w_botline);
2640#endif
2641 ++curwin->w_botline;
2642 room -= i;
2643 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2644 }
2645 }
2646
2647#ifndef KEEP_SCREEN_LINE
2648 /*
2649 * When hit bottom of the file: move cursor down.
2650 */
2651 if (n > 0)
2652 {
2653# ifdef FEAT_FOLDING
2654 if (hasAnyFolding(curwin))
2655 {
2656 while (--n >= 0
2657 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2658 {
2659 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2660 &curwin->w_cursor.lnum);
2661 ++curwin->w_cursor.lnum;
2662 }
2663 }
2664 else
2665# endif
2666 curwin->w_cursor.lnum += n;
2667 check_cursor_lnum();
2668 }
2669#else
2670 /* try to put the cursor in the same screen line */
2671 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2672 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2673 {
2674 scrolled -= plines(curwin->w_cursor.lnum);
2675 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2676 break;
2677# ifdef FEAT_FOLDING
2678 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2679 &curwin->w_cursor.lnum);
2680# endif
2681 ++curwin->w_cursor.lnum;
2682 }
2683#endif
2684 }
2685 else
2686 {
2687 /*
2688 * scroll the text down
2689 */
2690 while (n > 0 && curwin->w_topline > 1)
2691 {
2692#ifdef FEAT_DIFF
2693 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2694 {
2695 i = 1;
2696 if (--n < 0 && scrolled > 0)
2697 break;
2698 ++curwin->w_topfill;
2699 }
2700 else
2701#endif
2702 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002703 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 n -= i;
2705 if (n < 0 && scrolled > 0)
2706 break;
2707 --curwin->w_topline;
2708#ifdef FEAT_FOLDING
2709 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2710#endif
2711#ifdef FEAT_DIFF
2712 curwin->w_topfill = 0;
2713#endif
2714 }
2715 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2716 VALID_BOTLINE|VALID_BOTLINE_AP);
2717 scrolled += i;
2718#ifndef KEEP_SCREEN_LINE
2719 if (curwin->w_cursor.lnum > 1)
2720 {
2721 --curwin->w_cursor.lnum;
2722 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2723 }
2724#endif
2725 }
2726#ifndef KEEP_SCREEN_LINE
2727 /*
2728 * When hit top of the file: move cursor up.
2729 */
2730 if (n > 0)
2731 {
2732 if (curwin->w_cursor.lnum <= (linenr_T)n)
2733 curwin->w_cursor.lnum = 1;
2734 else
2735# ifdef FEAT_FOLDING
2736 if (hasAnyFolding(curwin))
2737 {
2738 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2739 {
2740 --curwin->w_cursor.lnum;
2741 (void)hasFolding(curwin->w_cursor.lnum,
2742 &curwin->w_cursor.lnum, NULL);
2743 }
2744 }
2745 else
2746# endif
2747 curwin->w_cursor.lnum -= n;
2748 }
2749#else
2750 /* try to put the cursor in the same screen line */
2751 scrolled += n; /* move cursor when topline is 1 */
2752 while (curwin->w_cursor.lnum > curwin->w_topline
2753 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2754 {
2755 scrolled -= plines(curwin->w_cursor.lnum - 1);
2756 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2757 break;
2758 --curwin->w_cursor.lnum;
2759# ifdef FEAT_FOLDING
2760 foldAdjustCursor();
2761# endif
2762 }
2763#endif
2764 }
2765# ifdef FEAT_FOLDING
2766 /* Move cursor to first line of closed fold. */
2767 foldAdjustCursor();
2768# endif
2769#ifdef FEAT_DIFF
2770 check_topfill(curwin, !flag);
2771#endif
2772 cursor_correct();
2773 beginline(BL_SOL | BL_FIX);
2774 redraw_later(VALID);
2775}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776
Bram Moolenaar860cae12010-06-05 23:22:07 +02002777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002778do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002779{
2780 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002781 colnr_T col = curwin->w_cursor.col;
2782# ifdef FEAT_VIRTUALEDIT
2783 colnr_T coladd = curwin->w_cursor.coladd;
2784# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002785 colnr_T curswant = curwin->w_curswant;
2786 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002787 win_T *old_curwin = curwin;
2788 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002789 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002790 int old_VIsual_select = VIsual_select;
2791 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002792
2793 /*
2794 * loop through the cursorbound windows
2795 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002796 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002797 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002798 {
2799 curbuf = curwin->w_buffer;
2800 /* skip original window and windows with 'noscrollbind' */
2801 if (curwin != old_curwin && curwin->w_p_crb)
2802 {
2803# ifdef FEAT_DIFF
2804 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002805 curwin->w_cursor.lnum =
2806 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002807 else
2808# endif
2809 curwin->w_cursor.lnum = line;
2810 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002811# ifdef FEAT_VIRTUALEDIT
2812 curwin->w_cursor.coladd = coladd;
2813# endif
Bram Moolenaar524780d2012-03-28 14:19:50 +02002814 curwin->w_curswant = curswant;
2815 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002816
Bram Moolenaar61452852011-02-01 18:01:11 +01002817 /* Make sure the cursor is in a valid position. Temporarily set
2818 * "restart_edit" to allow the cursor to be beyond the EOL. */
2819 restart_edit_save = restart_edit;
2820 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002821 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002822# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002823 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002824 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002825# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002826 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002827# ifdef FEAT_MBYTE
2828 /* Correct cursor for multi-byte character. */
2829 if (has_mbyte)
2830 mb_adjust_cursor();
2831# endif
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002832 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002833
2834 /* Only scroll when 'scrollbind' hasn't done this. */
2835 if (!curwin->w_p_scb)
2836 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002838 }
2839 }
2840
2841 /*
2842 * reset current-window
2843 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002844 VIsual_select = old_VIsual_select;
2845 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 curwin = old_curwin;
2847 curbuf = old_curbuf;
2848}