blob: 97f5ac1b7dd37f6e89875a43bdc772fac19b3708 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
28 linenr_T lnum; /* line number */
29#ifdef FEAT_DIFF
30 int fill; /* filler lines */
31#endif
32 int height; /* height of added line */
33} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
111 /* wp->w_botline is the line that is just below the window */
112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200139 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200140 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200141 if (wp->w_p_rnu)
142 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200144#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_cul)
146 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200147 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200149 // "w_last_cursorline" may be outdated, worst case we redraw
150 // too much. This is optimized for moving the cursor around in
151 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100152 redrawWinline(wp, wp->w_last_cursorline);
153 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200154 }
155 else
156 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200158#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200159 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160}
161
162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Update curwin->w_topline and redraw if necessary.
164 * Used to update the screen before printing a message.
165 */
166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100167update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 update_topline();
170 if (must_redraw)
171 update_screen(0);
172}
173
174/*
175 * Update curwin->w_topline to move the cursor onto the screen.
176 */
177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100178update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 long line_count;
181 int halfheight;
182 int n;
183 linenr_T old_topline;
184#ifdef FEAT_DIFF
185 int old_topfill;
186#endif
187#ifdef FEAT_FOLDING
188 linenr_T lnum;
189#endif
190 int check_topline = FALSE;
191 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100192 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100193 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaard5d37532017-03-27 23:02:07 +0200195 /* If there is no valid screen and when the window height is zero just use
196 * the cursor line. */
197 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 {
199 curwin->w_topline = curwin->w_cursor.lnum;
200 curwin->w_botline = curwin->w_topline;
201 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200202 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 return;
204 }
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 check_cursor_moved(curwin);
207 if (curwin->w_valid & VALID_TOPLINE)
208 return;
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000211 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100212 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 old_topline = curwin->w_topline;
215#ifdef FEAT_DIFF
216 old_topfill = curwin->w_topfill;
217#endif
218
219 /*
220 * If the buffer is empty, always set topline to 1.
221 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100222 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {
224 if (curwin->w_topline != 1)
225 redraw_later(NOT_VALID);
226 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 curwin->w_botline = 2;
228 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231
232 /*
233 * If the cursor is above or near the top of the window, scroll the window
234 * to show the line the cursor is in, with 'scrolloff' context.
235 */
236 else
237 {
238 if (curwin->w_topline > 1)
239 {
240 /* If the cursor is above topline, scrolling is always needed.
241 * If the cursor is far below topline and there is no folding,
242 * scrolling down is never needed. */
243 if (curwin->w_cursor.lnum < curwin->w_topline)
244 check_topline = TRUE;
245 else if (check_top_offset())
246 check_topline = TRUE;
247 }
248#ifdef FEAT_DIFF
249 /* Check if there are more filler lines than allowed. */
250 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
251 curwin->w_topline))
252 check_topline = TRUE;
253#endif
254
255 if (check_topline)
256 {
257 halfheight = curwin->w_height / 2 - 1;
258 if (halfheight < 2)
259 halfheight = 2;
260
261#ifdef FEAT_FOLDING
262 if (hasAnyFolding(curwin))
263 {
264 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100265 * topline + scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * scrolled). */
267 n = 0;
268 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100269 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
271 ++n;
272 /* stop at end of file or when we know we are far off */
273 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
274 break;
275 (void)hasFolding(lnum, NULL, &lnum);
276 }
277 }
278 else
279#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100280 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
282 /* If we weren't very close to begin with, we scroll to put the
283 * cursor in the middle of the window. Otherwise put the cursor
284 * near the top of the window. */
285 if (n >= halfheight)
286 scroll_cursor_halfway(FALSE);
287 else
288 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000289 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 check_botline = TRUE;
291 }
292 }
293
294 else
295 {
296#ifdef FEAT_FOLDING
297 /* Make sure topline is the first line of a fold. */
298 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
299#endif
300 check_botline = TRUE;
301 }
302 }
303
304 /*
305 * If the cursor is below the bottom of the window, scroll the window
306 * to put the cursor on the window.
307 * When w_botline is invalid, recompute it first, to avoid a redraw later.
308 * If w_botline was approximated, we might need a redraw later in a few
309 * cases, but we don't want to spend (a lot of) time recomputing w_botline
310 * for every small change.
311 */
312 if (check_botline)
313 {
314 if (!(curwin->w_valid & VALID_BOTLINE_AP))
315 validate_botline();
316
317 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
318 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000319 if (curwin->w_cursor.lnum < curwin->w_botline)
320 {
321 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100322 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_FOLDING
324 || hasAnyFolding(curwin)
325#endif
326 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 lineoff_T loff;
329
Bram Moolenaard4153d42008-11-15 15:06:17 +0000330 /* Cursor is (a few lines) above botline, check if there are
331 * 'scrolloff' window lines below the cursor. If not, need to
332 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 n = curwin->w_empty_rows;
334 loff.lnum = curwin->w_cursor.lnum;
335#ifdef FEAT_FOLDING
336 /* In a fold go to its last line. */
337 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
338#endif
339#ifdef FEAT_DIFF
340 loff.fill = 0;
341 n += curwin->w_filler_rows;
342#endif
343 loff.height = 0;
344 while (loff.lnum < curwin->w_botline
345#ifdef FEAT_DIFF
346 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
347#endif
348 )
349 {
350 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100351 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 break;
353 botline_forw(&loff);
354 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100355 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 /* sufficient context, no need to scroll */
357 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 }
359 else
360 /* sufficient context, no need to scroll */
361 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
363 if (check_botline)
364 {
365#ifdef FEAT_FOLDING
366 if (hasAnyFolding(curwin))
367 {
368 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100369 * botline - scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 * scrolled). */
371 line_count = 0;
372 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100373 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 ++line_count;
376 /* stop at end of file or when we know we are far off */
377 if (lnum <= 0 || line_count > curwin->w_height + 1)
378 break;
379 (void)hasFolding(lnum, &lnum, NULL);
380 }
381 }
382 else
383#endif
384 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100385 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000387 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 else
389 scroll_cursor_halfway(FALSE);
390 }
391 }
392 }
393 curwin->w_valid |= VALID_TOPLINE;
394
395 /*
396 * Need to redraw when topline changed.
397 */
398 if (curwin->w_topline != old_topline
399#ifdef FEAT_DIFF
400 || curwin->w_topfill != old_topfill
401#endif
402 )
403 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100404 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000405 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
407 curwin->w_skipcol = 0;
408 redraw_later(NOT_VALID);
409 }
410 else
411 redraw_later(VALID);
412 /* May need to set w_skipcol when cursor in w_topline. */
413 if (curwin->w_cursor.lnum == curwin->w_topline)
414 validate_cursor();
415 }
416
Bram Moolenaar375e3392019-01-31 18:26:10 +0100417 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418}
419
420/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000421 * Return the scrolljump value to use for the current window.
422 * When 'scrolljump' is positive use it as-is.
423 * When 'scrolljump' is negative use it as a percentage of the window height.
424 */
425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100426scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000427{
428 if (p_sj >= 0)
429 return (int)p_sj;
430 return (curwin->w_height * -p_sj) / 100;
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
435 * current window.
436 */
437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 lineoff_T loff;
441 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100442 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar375e3392019-01-31 18:26:10 +0100444 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#ifdef FEAT_FOLDING
446 || hasAnyFolding(curwin)
447#endif
448 )
449 {
450 loff.lnum = curwin->w_cursor.lnum;
451#ifdef FEAT_DIFF
452 loff.fill = 0;
453 n = curwin->w_topfill; /* always have this context */
454#else
455 n = 0;
456#endif
457 /* Count the visible screen lines above the cursor line. */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
460 topline_back(&loff);
461 /* Stop when included a line above the window. */
462 if (loff.lnum < curwin->w_topline
463#ifdef FEAT_DIFF
464 || (loff.lnum == curwin->w_topline && loff.fill > 0)
465#endif
466 )
467 break;
468 n += loff.height;
469 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100470 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100477update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 if (curwin->w_set_curswant)
480 {
481 validate_virtcol();
482 curwin->w_curswant = curwin->w_virtcol;
483 curwin->w_set_curswant = FALSE;
484 }
485}
486
487/*
488 * Check if the cursor has moved. Set the w_valid flag accordingly.
489 */
490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
494 {
495 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
496 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
497 wp->w_valid_cursor = wp->w_cursor;
498 wp->w_valid_leftcol = wp->w_leftcol;
499 }
500 else if (wp->w_cursor.col != wp->w_valid_cursor.col
501 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100502 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
504 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
505 wp->w_valid_cursor.col = wp->w_cursor.col;
506 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509}
510
511/*
512 * Call this function when some window settings have changed, which require
513 * the cursor position, botline and topline to be recomputed and the window to
514 * be redrawn. E.g, when changing the 'wrap' option or folding.
515 */
516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100517changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 changed_window_setting_win(curwin);
520}
521
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 wp->w_lines_valid = 0;
526 changed_line_abv_curs_win(wp);
527 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
528 redraw_win_later(wp, NOT_VALID);
529}
530
531/*
532 * Set wp->w_topline to a certain number.
533 */
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537#ifdef FEAT_FOLDING
538 /* go to first of folded lines */
539 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
540#endif
541 /* Approximate the value of w_botline */
542 wp->w_botline += lnum - wp->w_topline;
543 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000544 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#ifdef FEAT_DIFF
546 wp->w_topfill = 0;
547#endif
548 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
549 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
550 redraw_later(VALID);
551}
552
553/*
554 * Call this function when the length of the cursor line (in screen
555 * characters) has changed, and the change is before the cursor.
556 * Need to take care of w_botline separately!
557 */
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
562 |VALID_CHEIGHT|VALID_TOPLINE);
563}
564
565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
569 |VALID_CHEIGHT|VALID_TOPLINE);
570}
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572/*
573 * Call this function when the length of a line (in screen characters) above
574 * the cursor have changed.
575 * Need to take care of w_botline separately!
576 */
577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100578changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
581 |VALID_CHEIGHT|VALID_TOPLINE);
582}
583
584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100585changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
588 |VALID_CHEIGHT|VALID_TOPLINE);
589}
590
591/*
592 * Make sure the value of curwin->w_botline is valid.
593 */
594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100595validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
597 if (!(curwin->w_valid & VALID_BOTLINE))
598 comp_botline(curwin);
599}
600
601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * Mark curwin->w_botline as invalid (because of some change in the buffer).
603 */
604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100605invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
608}
609
610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100611invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
614}
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617approximate_botline_win(
618 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 wp->w_valid &= ~VALID_BOTLINE;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
624 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
625 */
626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100627cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 check_cursor_moved(curwin);
630 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
631 (VALID_WROW|VALID_WCOL));
632}
633
634/*
635 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
636 * w_topline must be valid, you may need to call update_topline() first!
637 */
638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100639validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 check_cursor_moved(curwin);
642 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
643 curs_columns(TRUE);
644}
645
646#if defined(FEAT_GUI) || defined(PROTO)
647/*
648 * validate w_cline_row.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 /*
654 * First make sure that w_topline is valid (after moving the cursor).
655 */
656 update_topline();
657 check_cursor_moved(curwin);
658 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100659 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661#endif
662
663/*
664 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200665 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 */
667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 linenr_T lnum;
671 int i;
672 int all_invalid;
673 int valid;
674#ifdef FEAT_FOLDING
675 long fold_count;
676#endif
677
678 /* Check if wp->w_lines[].wl_size is invalid */
679 all_invalid = (!redrawing()
680 || wp->w_lines_valid == 0
681 || wp->w_lines[0].wl_lnum > wp->w_topline);
682 i = 0;
683 wp->w_cline_row = 0;
684 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
685 {
686 valid = FALSE;
687 if (!all_invalid && i < wp->w_lines_valid)
688 {
689 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
690 continue; /* skip changed or deleted lines */
691 if (wp->w_lines[i].wl_lnum == lnum)
692 {
693#ifdef FEAT_FOLDING
694 /* Check for newly inserted lines below this row, in which
695 * case we need to check for folded lines. */
696 if (!wp->w_buffer->b_mod_set
697 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
698 || wp->w_buffer->b_mod_top
699 > wp->w_lines[i].wl_lastlnum + 1)
700#endif
701 valid = TRUE;
702 }
703 else if (wp->w_lines[i].wl_lnum > lnum)
704 --i; /* hold at inserted lines */
705 }
706 if (valid
707#ifdef FEAT_DIFF
708 && (lnum != wp->w_topline || !wp->w_p_diff)
709#endif
710 )
711 {
712#ifdef FEAT_FOLDING
713 lnum = wp->w_lines[i].wl_lastlnum + 1;
714 /* Cursor inside folded lines, don't count this row */
715 if (lnum > wp->w_cursor.lnum)
716 break;
717#else
718 ++lnum;
719#endif
720 wp->w_cline_row += wp->w_lines[i].wl_size;
721 }
722 else
723 {
724#ifdef FEAT_FOLDING
725 fold_count = foldedCount(wp, lnum, NULL);
726 if (fold_count)
727 {
728 lnum += fold_count;
729 if (lnum > wp->w_cursor.lnum)
730 break;
731 ++wp->w_cline_row;
732 }
733 else
734#endif
735#ifdef FEAT_DIFF
736 if (lnum == wp->w_topline)
737 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
738 + wp->w_topfill;
739 else
740#endif
741 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
742 }
743 }
744
745 check_cursor_moved(wp);
746 if (!(wp->w_valid & VALID_CHEIGHT))
747 {
748 if (all_invalid
749 || i == wp->w_lines_valid
750 || (i < wp->w_lines_valid
751 && (!wp->w_lines[i].wl_valid
752 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
753 {
754#ifdef FEAT_DIFF
755 if (wp->w_cursor.lnum == wp->w_topline)
756 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
757 TRUE) + wp->w_topfill;
758 else
759#endif
760 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
761#ifdef FEAT_FOLDING
762 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
763 NULL, NULL, TRUE, NULL);
764#endif
765 }
766 else if (i > wp->w_lines_valid)
767 {
768 /* a line that is too long to fit on the last screen line */
769 wp->w_cline_height = 0;
770#ifdef FEAT_FOLDING
771 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
772 NULL, NULL, TRUE, NULL);
773#endif
774 }
775 else
776 {
777 wp->w_cline_height = wp->w_lines[i].wl_size;
778#ifdef FEAT_FOLDING
779 wp->w_cline_folded = wp->w_lines[i].wl_folded;
780#endif
781 }
782 }
783
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100784 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
789/*
790 * Validate curwin->w_virtcol only.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 validate_virtcol_win(curwin);
796}
797
798/*
799 * Validate wp->w_virtcol only.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 check_cursor_moved(wp);
805 if (!(wp->w_valid & VALID_VIRTCOL))
806 {
807 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
808 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000809#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200810 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000811 redraw_win_later(wp, SOME_VALID);
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814}
815
816/*
817 * Validate curwin->w_cline_height only.
818 */
819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 check_cursor_moved(curwin);
823 if (!(curwin->w_valid & VALID_CHEIGHT))
824 {
825#ifdef FEAT_DIFF
826 if (curwin->w_cursor.lnum == curwin->w_topline)
827 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
828 + curwin->w_topfill;
829 else
830#endif
831 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
832#ifdef FEAT_FOLDING
833 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
834#endif
835 curwin->w_valid |= VALID_CHEIGHT;
836 }
837}
838
839/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000840 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 */
842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100843validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845 colnr_T off;
846 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100847 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 validate_virtcol();
850 if (!(curwin->w_valid & VALID_WCOL))
851 {
852 col = curwin->w_virtcol;
853 off = curwin_col_off();
854 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200855 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000858 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200859 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100860 && width > 0)
861 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200862 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000863 if (col > (int)curwin->w_leftcol)
864 col -= curwin->w_leftcol;
865 else
866 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 curwin->w_valid |= VALID_WCOL;
870 }
871}
872
873/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200874 * Compute offset of a window, occupied by absolute or relative line number,
875 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 */
877 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100878win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
Bram Moolenaar64486672010-05-16 15:46:46 +0200880 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#ifdef FEAT_CMDWIN
882 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
883#endif
884#ifdef FEAT_FOLDING
885 + wp->w_p_fdc
886#endif
887#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200888 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#endif
890 );
891}
892
893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 return win_col_off(curwin);
897}
898
899/*
900 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200901 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
902 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 */
904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100905win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000908 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 return 0;
910}
911
912 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100913curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 return win_col_off2(curwin);
916}
917
918/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100919 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * Also updates curwin->w_wrow and curwin->w_cline_row.
921 * Also updates curwin->w_leftcol.
922 */
923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100924curs_columns(
925 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 int diff;
928 int extra; /* offset for first screen line */
929 int off_left, off_right;
930 int n;
931 int p_lines;
932 int width = 0;
933 int textwidth;
934 int new_leftcol;
935 colnr_T startcol;
936 colnr_T endcol;
937 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100938 long so = get_scrolloff_value();
939 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 /*
942 * First make sure that w_topline is valid (after moving the cursor).
943 */
944 update_topline();
945
946 /*
947 * Next make sure that w_cline_row is valid.
948 */
949 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100950 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 /*
953 * Compute the number of virtual columns.
954 */
955#ifdef FEAT_FOLDING
956 if (curwin->w_cline_folded)
957 /* In a folded line the cursor is always in the first column */
958 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
959 else
960#endif
961 getvvcol(curwin, &curwin->w_cursor,
962 &startcol, &(curwin->w_virtcol), &endcol);
963
964 /* remove '$' from change command when cursor moves onto it */
965 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100966 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 extra = curwin_col_off();
969 curwin->w_wcol = curwin->w_virtcol + extra;
970 endcol += extra;
971
972 /*
973 * Now compute w_wrow, counting screen lines from w_cline_row.
974 */
975 curwin->w_wrow = curwin->w_cline_row;
976
Bram Moolenaar02631462017-09-22 15:20:32 +0200977 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (textwidth <= 0)
979 {
980 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200981 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 curwin->w_wrow = curwin->w_height - 1;
983 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200984 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 width = textwidth + curwin_col_off2();
987
988 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +0200989 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar6427c602010-02-03 17:43:07 +0100991 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200992 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 curwin->w_wcol -= n * width;
994 curwin->w_wrow += n;
995
996#ifdef FEAT_LINEBREAK
997 /* When cursor wraps to first char of next line in Insert
998 * mode, the 'showbreak' string isn't shown, backup to first
999 * column */
1000 if (*p_sbr && *ml_get_cursor() == NUL
1001 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1002 curwin->w_wcol = 0;
1003#endif
1004 }
1005 }
1006
1007 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1008 * is not folded.
1009 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001010 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_FOLDING
1012 && !curwin->w_cline_folded
1013#endif
1014 )
1015 {
1016 /*
1017 * If Cursor is left of the screen, scroll rightwards.
1018 * If Cursor is right of the screen, scroll leftwards
1019 * If we get closer to the edge than 'sidescrolloff', scroll a little
1020 * extra
1021 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001022 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001023 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001024 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (off_left < 0 || off_right > 0)
1026 {
1027 if (off_left < 0)
1028 diff = -off_left;
1029 else
1030 diff = off_right;
1031
1032 /* When far off or not enough room on either side, put cursor in
1033 * middle of window. */
1034 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1035 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1036 else
1037 {
1038 if (diff < p_ss)
1039 diff = p_ss;
1040 if (off_left < 0)
1041 new_leftcol = curwin->w_leftcol - diff;
1042 else
1043 new_leftcol = curwin->w_leftcol + diff;
1044 }
1045 if (new_leftcol < 0)
1046 new_leftcol = 0;
1047 if (new_leftcol != (int)curwin->w_leftcol)
1048 {
1049 curwin->w_leftcol = new_leftcol;
1050 /* screen has to be redrawn with new curwin->w_leftcol */
1051 redraw_later(NOT_VALID);
1052 }
1053 }
1054 curwin->w_wcol -= curwin->w_leftcol;
1055 }
1056 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1057 curwin->w_wcol -= curwin->w_leftcol;
1058 else
1059 curwin->w_wcol = 0;
1060
1061#ifdef FEAT_DIFF
1062 /* Skip over filler lines. At the top use w_topfill, there
1063 * may be some filler lines above the window. */
1064 if (curwin->w_cursor.lnum == curwin->w_topline)
1065 curwin->w_wrow += curwin->w_topfill;
1066 else
1067 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1068#endif
1069
1070 prev_skipcol = curwin->w_skipcol;
1071
1072 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if ((curwin->w_wrow >= curwin->w_height
1075 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001076 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && (p_lines =
1078#ifdef FEAT_DIFF
1079 plines_win_nofill
1080#else
1081 plines_win
1082#endif
1083 (curwin, curwin->w_cursor.lnum, FALSE))
1084 - 1 >= curwin->w_height))
1085 && curwin->w_height != 0
1086 && curwin->w_cursor.lnum == curwin->w_topline
1087 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001088 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 /* Cursor past end of screen. Happens with a single line that does
1091 * not fit on screen. Find a skipcol to show the text around the
1092 * cursor. Avoid scrolling all the time. compute value of "extra":
Bram Moolenaar375e3392019-01-31 18:26:10 +01001093 * 1: Less than 'scrolloff' lines above
1094 * 2: Less than 'scrolloff' lines below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 * 3: both of them */
1096 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001097 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 extra = 1;
1099 /* Compute last display line of the buffer line that we want at the
1100 * bottom of the window. */
1101 if (p_lines == 0)
1102 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1103 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001104 if (p_lines > curwin->w_wrow + so)
1105 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 else
1107 n = p_lines;
1108 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1109 extra += 2;
1110
Bram Moolenaar375e3392019-01-31 18:26:10 +01001111 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 /* not enough room for 'scrolloff', put cursor in the middle */
1114 n = curwin->w_virtcol / width;
1115 if (n > curwin->w_height / 2)
1116 n -= curwin->w_height / 2;
1117 else
1118 n = 0;
1119 /* don't skip more than necessary */
1120 if (n > p_lines - curwin->w_height + 1)
1121 n = p_lines - curwin->w_height + 1;
1122 curwin->w_skipcol = n * width;
1123 }
1124 else if (extra == 1)
1125 {
1126 /* less then 'scrolloff' lines above, decrease skipcol */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001127 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 + width - 1) / width;
1129 if (extra > 0)
1130 {
1131 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1132 extra = curwin->w_skipcol / width;
1133 curwin->w_skipcol -= extra * width;
1134 }
1135 }
1136 else if (extra == 2)
1137 {
1138 /* less then 'scrolloff' lines below, increase skipcol */
1139 endcol = (n - curwin->w_height + 1) * width;
1140 while (endcol > curwin->w_virtcol)
1141 endcol -= width;
1142 if (endcol > curwin->w_skipcol)
1143 curwin->w_skipcol = endcol;
1144 }
1145
1146 curwin->w_wrow -= curwin->w_skipcol / width;
1147 if (curwin->w_wrow >= curwin->w_height)
1148 {
1149 /* small window, make sure cursor is in it */
1150 extra = curwin->w_wrow - curwin->w_height + 1;
1151 curwin->w_skipcol += extra * width;
1152 curwin->w_wrow -= extra;
1153 }
1154
1155 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1156 if (extra > 0)
1157 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1158 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001159 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 else
1162 curwin->w_skipcol = 0;
1163 if (prev_skipcol != curwin->w_skipcol)
1164 redraw_later(NOT_VALID);
1165
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001166#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001167 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1168 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001169 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001170 redraw_later(SOME_VALID);
1171#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1174}
1175
Bram Moolenaar12034e22019-08-25 22:25:02 +02001176#if (defined(FEAT_EVAL) || defined(FEAT_TEXT_PROP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001177/*
1178 * Compute the screen position of text character at "pos" in window "wp"
1179 * The resulting values are one-based, zero when character is not visible.
1180 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001181 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001182textpos2screenpos(
1183 win_T *wp,
1184 pos_T *pos,
1185 int *rowp, // screen row
1186 int *scolp, // start screen column
1187 int *ccolp, // cursor screen column
1188 int *ecolp) // end screen column
1189{
1190 colnr_T scol = 0, ccol = 0, ecol = 0;
1191 int row = 0;
1192 int rowoff = 0;
1193 colnr_T coloff = 0;
1194
1195 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1196 {
1197 colnr_T off;
1198 colnr_T col;
1199 int width;
1200
1201 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1202 getvcol(wp, pos, &scol, &ccol, &ecol);
1203
1204 // similar to what is done in validate_cursor_col()
1205 col = scol;
1206 off = win_col_off(wp);
1207 col += off;
1208 width = wp->w_width - off + win_col_off2(wp);
1209
Bram Moolenaar12034e22019-08-25 22:25:02 +02001210 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001211 if (wp->w_p_wrap
1212 && col >= (colnr_T)wp->w_width
1213 && width > 0)
1214 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001216 rowoff = ((col - wp->w_width) / width + 1);
1217 col -= rowoff * width;
1218 }
1219 col -= wp->w_leftcol;
1220 if (col >= width)
1221 col = -1;
1222 if (col >= 0)
1223 coloff = col - scol + wp->w_wincol + 1;
1224 else
1225 // character is left or right of the window
1226 row = scol = ccol = ecol = 0;
1227 }
1228 *rowp = wp->w_winrow + row + rowoff;
1229 *scolp = scol + coloff;
1230 *ccolp = ccol + coloff;
1231 *ecolp = ecol + coloff;
1232}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001233#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001234
Bram Moolenaar12034e22019-08-25 22:25:02 +02001235#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001236/*
1237 * "screenpos({winid}, {lnum}, {col})" function
1238 */
1239 void
1240f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1241{
1242 dict_T *dict;
1243 win_T *wp;
1244 pos_T pos;
1245 int row = 0;
1246 int scol = 0, ccol = 0, ecol = 0;
1247
1248 if (rettv_dict_alloc(rettv) != OK)
1249 return;
1250 dict = rettv->vval.v_dict;
1251
1252 wp = find_win_by_nr_or_id(&argvars[0]);
1253 if (wp == NULL)
1254 return;
1255
1256 pos.lnum = tv_get_number(&argvars[1]);
1257 pos.col = tv_get_number(&argvars[2]) - 1;
1258 pos.coladd = 0;
1259 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1260
1261 dict_add_number(dict, "row", row);
1262 dict_add_number(dict, "col", scol);
1263 dict_add_number(dict, "curscol", ccol);
1264 dict_add_number(dict, "endcol", ecol);
1265}
1266#endif
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001272scrolldown(
1273 long line_count,
1274 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 long done = 0; /* total # of physical lines done */
1277 int wrow;
1278 int moved = FALSE;
1279
1280#ifdef FEAT_FOLDING
1281 linenr_T first;
1282
1283 /* Make sure w_topline is at the first of a sequence of folded lines. */
1284 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1285#endif
1286 validate_cursor(); /* w_wrow needs to be valid */
1287 while (line_count-- > 0)
1288 {
1289#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001290 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1291 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 ++curwin->w_topfill;
1294 ++done;
1295 }
1296 else
1297#endif
1298 {
1299 if (curwin->w_topline == 1)
1300 break;
1301 --curwin->w_topline;
1302#ifdef FEAT_DIFF
1303 curwin->w_topfill = 0;
1304#endif
1305#ifdef FEAT_FOLDING
1306 /* A sequence of folded lines only counts for one logical line */
1307 if (hasFolding(curwin->w_topline, &first, NULL))
1308 {
1309 ++done;
1310 if (!byfold)
1311 line_count -= curwin->w_topline - first - 1;
1312 curwin->w_botline -= curwin->w_topline - first;
1313 curwin->w_topline = first;
1314 }
1315 else
1316#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001317 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 --curwin->w_botline; /* approximate w_botline */
1320 invalidate_botline();
1321 }
1322 curwin->w_wrow += done; /* keep w_wrow updated */
1323 curwin->w_cline_row += done; /* keep w_cline_row updated */
1324
1325#ifdef FEAT_DIFF
1326 if (curwin->w_cursor.lnum == curwin->w_topline)
1327 curwin->w_cline_row = 0;
1328 check_topfill(curwin, TRUE);
1329#endif
1330
1331 /*
1332 * Compute the row number of the last row of the cursor line
1333 * and move the cursor onto the displayed part of the window.
1334 */
1335 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001336 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 validate_virtcol();
1339 validate_cheight();
1340 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001341 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1344 {
1345#ifdef FEAT_FOLDING
1346 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1347 {
1348 --wrow;
1349 if (first == 1)
1350 curwin->w_cursor.lnum = 1;
1351 else
1352 curwin->w_cursor.lnum = first - 1;
1353 }
1354 else
1355#endif
1356 wrow -= plines(curwin->w_cursor.lnum--);
1357 curwin->w_valid &=
1358 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1359 moved = TRUE;
1360 }
1361 if (moved)
1362 {
1363#ifdef FEAT_FOLDING
1364 /* Move cursor to first line of closed fold. */
1365 foldAdjustCursor();
1366#endif
1367 coladvance(curwin->w_curswant);
1368 }
1369}
1370
1371/*
1372 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001375scrollup(
1376 long line_count,
1377 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1380 linenr_T lnum;
1381
1382 if (
1383# ifdef FEAT_FOLDING
1384 (byfold && hasAnyFolding(curwin))
1385# ifdef FEAT_DIFF
1386 ||
1387# endif
1388# endif
1389# ifdef FEAT_DIFF
1390 curwin->w_p_diff
1391# endif
1392 )
1393 {
1394 /* count each sequence of folded lines as one logical line */
1395 lnum = curwin->w_topline;
1396 while (line_count--)
1397 {
1398# ifdef FEAT_DIFF
1399 if (curwin->w_topfill > 0)
1400 --curwin->w_topfill;
1401 else
1402# endif
1403 {
1404# ifdef FEAT_FOLDING
1405 if (byfold)
1406 (void)hasFolding(lnum, NULL, &lnum);
1407# endif
1408 if (lnum >= curbuf->b_ml.ml_line_count)
1409 break;
1410 ++lnum;
1411# ifdef FEAT_DIFF
1412 curwin->w_topfill = diff_check_fill(curwin, lnum);
1413# endif
1414 }
1415 }
1416 /* approximate w_botline */
1417 curwin->w_botline += lnum - curwin->w_topline;
1418 curwin->w_topline = lnum;
1419 }
1420 else
1421#endif
1422 {
1423 curwin->w_topline += line_count;
1424 curwin->w_botline += line_count; /* approximate w_botline */
1425 }
1426
1427 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1428 curwin->w_topline = curbuf->b_ml.ml_line_count;
1429 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1430 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1431
1432#ifdef FEAT_DIFF
1433 check_topfill(curwin, FALSE);
1434#endif
1435
1436#ifdef FEAT_FOLDING
1437 if (hasAnyFolding(curwin))
1438 /* Make sure w_topline is at the first of a sequence of folded lines. */
1439 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1440#endif
1441
1442 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1443 if (curwin->w_cursor.lnum < curwin->w_topline)
1444 {
1445 curwin->w_cursor.lnum = curwin->w_topline;
1446 curwin->w_valid &=
1447 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1448 coladvance(curwin->w_curswant);
1449 }
1450}
1451
1452#ifdef FEAT_DIFF
1453/*
1454 * Don't end up with too many filler lines in the window.
1455 */
1456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001457check_topfill(
1458 win_T *wp,
1459 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 int n;
1462
1463 if (wp->w_topfill > 0)
1464 {
1465 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1466 if (wp->w_topfill + n > wp->w_height)
1467 {
1468 if (down && wp->w_topline > 1)
1469 {
1470 --wp->w_topline;
1471 wp->w_topfill = 0;
1472 }
1473 else
1474 {
1475 wp->w_topfill = wp->w_height - n;
1476 if (wp->w_topfill < 0)
1477 wp->w_topfill = 0;
1478 }
1479 }
1480 }
1481}
1482
1483/*
1484 * Use as many filler lines as possible for w_topline. Make sure w_topline
1485 * is still visible.
1486 */
1487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001488max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 int n;
1491
1492 n = plines_nofill(curwin->w_topline);
1493 if (n >= curwin->w_height)
1494 curwin->w_topfill = 0;
1495 else
1496 {
1497 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1498 if (curwin->w_topfill + n > curwin->w_height)
1499 curwin->w_topfill = curwin->w_height - n;
1500 }
1501}
1502#endif
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504/*
1505 * Scroll the screen one line down, but don't do it if it would move the
1506 * cursor off the screen.
1507 */
1508 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001509scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 int end_row;
1512#ifdef FEAT_DIFF
1513 int can_fill = (curwin->w_topfill
1514 < diff_check_fill(curwin, curwin->w_topline));
1515#endif
1516
1517 if (curwin->w_topline <= 1
1518#ifdef FEAT_DIFF
1519 && !can_fill
1520#endif
1521 )
1522 return;
1523
1524 validate_cursor(); /* w_wrow needs to be valid */
1525
1526 /*
1527 * Compute the row number of the last row of the cursor line
1528 * and make sure it doesn't go off the screen. Make sure the cursor
1529 * doesn't go past 'scrolloff' lines from the screen end.
1530 */
1531 end_row = curwin->w_wrow;
1532#ifdef FEAT_DIFF
1533 if (can_fill)
1534 ++end_row;
1535 else
1536 end_row += plines_nofill(curwin->w_topline - 1);
1537#else
1538 end_row += plines(curwin->w_topline - 1);
1539#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001540 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
1542 validate_cheight();
1543 validate_virtcol();
1544 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001545 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001547 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549#ifdef FEAT_DIFF
1550 if (can_fill)
1551 {
1552 ++curwin->w_topfill;
1553 check_topfill(curwin, TRUE);
1554 }
1555 else
1556 {
1557 --curwin->w_topline;
1558 curwin->w_topfill = 0;
1559 }
1560#else
1561 --curwin->w_topline;
1562#endif
1563#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001564 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
1566 --curwin->w_botline; /* approximate w_botline */
1567 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1568 }
1569}
1570
1571/*
1572 * Scroll the screen one line up, but don't do it if it would move the cursor
1573 * off the screen.
1574 */
1575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001576scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 int start_row;
1579
1580 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1581#ifdef FEAT_DIFF
1582 && curwin->w_topfill == 0
1583#endif
1584 )
1585 return;
1586
1587 validate_cursor(); /* w_wrow needs to be valid */
1588
1589 /*
1590 * Compute the row number of the first row of the cursor line
1591 * and make sure it doesn't go off the screen. Make sure the cursor
1592 * doesn't go before 'scrolloff' lines from the screen start.
1593 */
1594#ifdef FEAT_DIFF
1595 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1596 - curwin->w_topfill;
1597#else
1598 start_row = curwin->w_wrow - plines(curwin->w_topline);
1599#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001600 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001603 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001605 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607#ifdef FEAT_DIFF
1608 if (curwin->w_topfill > 0)
1609 --curwin->w_topfill;
1610 else
1611#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001612 {
1613#ifdef FEAT_FOLDING
1614 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 ++curwin->w_botline; /* approximate w_botline */
1619 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1620 }
1621}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623/*
1624 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1625 * a (wrapped) text line. Uses and sets "lp->fill".
1626 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001627 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
1629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001630topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632#ifdef FEAT_DIFF
1633 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1634 {
1635 /* Add a filler line. */
1636 ++lp->fill;
1637 lp->height = 1;
1638 }
1639 else
1640#endif
1641 {
1642 --lp->lnum;
1643#ifdef FEAT_DIFF
1644 lp->fill = 0;
1645#endif
1646 if (lp->lnum < 1)
1647 lp->height = MAXCOL;
1648 else
1649#ifdef FEAT_FOLDING
1650 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1651 /* Add a closed fold */
1652 lp->height = 1;
1653 else
1654#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001655 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 }
1657}
1658
1659/*
1660 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1661 * a (wrapped) text line. Uses and sets "lp->fill".
1662 * Returns the height of the added line in "lp->height".
1663 * Lines below the last one are incredibly high.
1664 */
1665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668#ifdef FEAT_DIFF
1669 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1670 {
1671 /* Add a filler line. */
1672 ++lp->fill;
1673 lp->height = 1;
1674 }
1675 else
1676#endif
1677 {
1678 ++lp->lnum;
1679#ifdef FEAT_DIFF
1680 lp->fill = 0;
1681#endif
1682 if (lp->lnum > curbuf->b_ml.ml_line_count)
1683 lp->height = MAXCOL;
1684 else
1685#ifdef FEAT_FOLDING
1686 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1687 /* Add a closed fold */
1688 lp->height = 1;
1689 else
1690#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001691 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693}
1694
1695#ifdef FEAT_DIFF
1696/*
1697 * Switch from including filler lines below lp->lnum to including filler
1698 * lines above loff.lnum + 1. This keeps pointing to the same line.
1699 * When there are no filler lines nothing changes.
1700 */
1701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001702botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 if (lp->fill > 0)
1705 {
1706 ++lp->lnum;
1707 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1708 }
1709}
1710
1711/*
1712 * Switch from including filler lines above lp->lnum to including filler
1713 * lines below loff.lnum - 1. This keeps pointing to the same line.
1714 * When there are no filler lines nothing changes.
1715 */
1716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (lp->fill > 0)
1720 {
1721 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1722 --lp->lnum;
1723 }
1724}
1725#endif
1726
1727/*
1728 * Recompute topline to put the cursor at the top of the window.
1729 * Scroll at least "min_scroll" lines.
1730 * If "always" is TRUE, always set topline (for "zt").
1731 */
1732 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001733scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 int scrolled = 0;
1736 int extra = 0;
1737 int used;
1738 int i;
1739 linenr_T top; /* just above displayed lines */
1740 linenr_T bot; /* just below displayed lines */
1741 linenr_T old_topline = curwin->w_topline;
1742#ifdef FEAT_DIFF
1743 linenr_T old_topfill = curwin->w_topfill;
1744#endif
1745 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001746 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (mouse_dragging > 0)
1749 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 /*
1752 * Decrease topline until:
1753 * - it has become 1
1754 * - (part of) the cursor line is moved off the screen or
1755 * - moved at least 'scrolljump' lines and
1756 * - at least 'scrolloff' lines above and below the cursor
1757 */
1758 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001759 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (curwin->w_cursor.lnum < curwin->w_topline)
1761 scrolled = used;
1762
1763#ifdef FEAT_FOLDING
1764 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1765 {
1766 --top;
1767 ++bot;
1768 }
1769 else
1770#endif
1771 {
1772 top = curwin->w_cursor.lnum - 1;
1773 bot = curwin->w_cursor.lnum + 1;
1774 }
1775 new_topline = top + 1;
1776
1777#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001778 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001779 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001780 * Hide filler lines above cursor line by adding them to "extra". */
1781 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#endif
1783
1784 /*
1785 * Check if the lines from "top" to "bot" fit in the window. If they do,
1786 * set new_topline and advance "top" and "bot" to include more lines.
1787 */
1788 while (top > 0)
1789 {
1790#ifdef FEAT_FOLDING
1791 if (hasFolding(top, &top, NULL))
1792 /* count one logical line for a sequence of folded lines */
1793 i = 1;
1794 else
1795#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001796 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 used += i;
1798 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1799 {
1800#ifdef FEAT_FOLDING
1801 if (hasFolding(bot, NULL, &bot))
1802 /* count one logical line for a sequence of folded lines */
1803 ++used;
1804 else
1805#endif
1806 used += plines(bot);
1807 }
1808 if (used > curwin->w_height)
1809 break;
1810 if (top < curwin->w_topline)
1811 scrolled += i;
1812
1813 /*
1814 * If scrolling is needed, scroll at least 'sj' lines.
1815 */
1816 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1817 && extra >= off)
1818 break;
1819
1820 extra += i;
1821 new_topline = top;
1822 --top;
1823 ++bot;
1824 }
1825
1826 /*
1827 * If we don't have enough space, put cursor in the middle.
1828 * This makes sure we get the same position when using "k" and "j"
1829 * in a small window.
1830 */
1831 if (used > curwin->w_height)
1832 scroll_cursor_halfway(FALSE);
1833 else
1834 {
1835 /*
1836 * If "always" is FALSE, only adjust topline to a lower value, higher
1837 * value may happen with wrapping lines
1838 */
1839 if (new_topline < curwin->w_topline || always)
1840 curwin->w_topline = new_topline;
1841 if (curwin->w_topline > curwin->w_cursor.lnum)
1842 curwin->w_topline = curwin->w_cursor.lnum;
1843#ifdef FEAT_DIFF
1844 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1845 if (curwin->w_topfill > 0 && extra > off)
1846 {
1847 curwin->w_topfill -= extra - off;
1848 if (curwin->w_topfill < 0)
1849 curwin->w_topfill = 0;
1850 }
1851 check_topfill(curwin, FALSE);
1852#endif
1853 if (curwin->w_topline != old_topline
1854#ifdef FEAT_DIFF
1855 || curwin->w_topfill != old_topfill
1856#endif
1857 )
1858 curwin->w_valid &=
1859 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1860 curwin->w_valid |= VALID_TOPLINE;
1861 }
1862}
1863
1864/*
1865 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1866 * screen lines for text lines.
1867 */
1868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001869set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871#ifdef FEAT_DIFF
1872 wp->w_filler_rows = 0;
1873#endif
1874 if (used == 0)
1875 wp->w_empty_rows = 0; /* single line that doesn't fit */
1876 else
1877 {
1878 wp->w_empty_rows = wp->w_height - used;
1879#ifdef FEAT_DIFF
1880 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1881 {
1882 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1883 if (wp->w_empty_rows > wp->w_filler_rows)
1884 wp->w_empty_rows -= wp->w_filler_rows;
1885 else
1886 {
1887 wp->w_filler_rows = wp->w_empty_rows;
1888 wp->w_empty_rows = 0;
1889 }
1890 }
1891#endif
1892 }
1893}
1894
1895/*
1896 * Recompute topline to put the cursor at the bottom of the window.
1897 * Scroll at least "min_scroll" lines.
1898 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1899 * This is messy stuff!!!
1900 */
1901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001902scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 int used;
1905 int scrolled = 0;
1906 int extra = 0;
1907 int i;
1908 linenr_T line_count;
1909 linenr_T old_topline = curwin->w_topline;
1910 lineoff_T loff;
1911 lineoff_T boff;
1912#ifdef FEAT_DIFF
1913 int old_topfill = curwin->w_topfill;
1914 int fill_below_window;
1915#endif
1916 linenr_T old_botline = curwin->w_botline;
1917 linenr_T old_valid = curwin->w_valid;
1918 int old_empty_rows = curwin->w_empty_rows;
1919 linenr_T cln; /* Cursor Line Number */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001920 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 cln = curwin->w_cursor.lnum;
1923 if (set_topbot)
1924 {
1925 used = 0;
1926 curwin->w_botline = cln + 1;
1927#ifdef FEAT_DIFF
1928 loff.fill = 0;
1929#endif
1930 for (curwin->w_topline = curwin->w_botline;
1931 curwin->w_topline > 1;
1932 curwin->w_topline = loff.lnum)
1933 {
1934 loff.lnum = curwin->w_topline;
1935 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001936 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 break;
1938 used += loff.height;
1939#ifdef FEAT_DIFF
1940 curwin->w_topfill = loff.fill;
1941#endif
1942 }
1943 set_empty_rows(curwin, used);
1944 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1945 if (curwin->w_topline != old_topline
1946#ifdef FEAT_DIFF
1947 || curwin->w_topfill != old_topfill
1948#endif
1949 )
1950 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1951 }
1952 else
1953 validate_botline();
1954
1955 /* The lines of the cursor line itself are always used. */
1956#ifdef FEAT_DIFF
1957 used = plines_nofill(cln);
1958#else
1959 validate_cheight();
1960 used = curwin->w_cline_height;
1961#endif
1962
1963 /* If the cursor is below botline, we will at least scroll by the height
1964 * of the cursor line. Correct for empty lines, which are really part of
1965 * botline. */
1966 if (cln >= curwin->w_botline)
1967 {
1968 scrolled = used;
1969 if (cln == curwin->w_botline)
1970 scrolled -= curwin->w_empty_rows;
1971 }
1972
1973 /*
1974 * Stop counting lines to scroll when
1975 * - hitting start of the file
1976 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001977 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 * - lines between botline and cursor have been counted
1979 */
1980#ifdef FEAT_FOLDING
1981 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1982#endif
1983 {
1984 loff.lnum = cln;
1985 boff.lnum = cln;
1986 }
1987#ifdef FEAT_DIFF
1988 loff.fill = 0;
1989 boff.fill = 0;
1990 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1991 - curwin->w_filler_rows;
1992#endif
1993
1994 while (loff.lnum > 1)
1995 {
1996 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1997 * context for 'scrolloff' and counted all lines below the window. */
1998 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001999 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2001 && loff.lnum <= curwin->w_botline
2002#ifdef FEAT_DIFF
2003 && (loff.lnum < curwin->w_botline
2004 || loff.fill >= fill_below_window)
2005#endif
2006 )
2007 break;
2008
2009 /* Add one line above */
2010 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002011 if (loff.height == MAXCOL)
2012 used = MAXCOL;
2013 else
2014 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (used > curwin->w_height)
2016 break;
2017 if (loff.lnum >= curwin->w_botline
2018#ifdef FEAT_DIFF
2019 && (loff.lnum > curwin->w_botline
2020 || loff.fill <= fill_below_window)
2021#endif
2022 )
2023 {
2024 /* Count screen lines that are below the window. */
2025 scrolled += loff.height;
2026 if (loff.lnum == curwin->w_botline
2027#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002028 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#endif
2030 )
2031 scrolled -= curwin->w_empty_rows;
2032 }
2033
2034 if (boff.lnum < curbuf->b_ml.ml_line_count)
2035 {
2036 /* Add one line below */
2037 botline_forw(&boff);
2038 used += boff.height;
2039 if (used > curwin->w_height)
2040 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002041 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2042 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
2044 extra += boff.height;
2045 if (boff.lnum >= curwin->w_botline
2046#ifdef FEAT_DIFF
2047 || (boff.lnum + 1 == curwin->w_botline
2048 && boff.fill > curwin->w_filler_rows)
2049#endif
2050 )
2051 {
2052 /* Count screen lines that are below the window. */
2053 scrolled += boff.height;
2054 if (boff.lnum == curwin->w_botline
2055#ifdef FEAT_DIFF
2056 && boff.fill == 0
2057#endif
2058 )
2059 scrolled -= curwin->w_empty_rows;
2060 }
2061 }
2062 }
2063 }
2064
2065 /* curwin->w_empty_rows is larger, no need to scroll */
2066 if (scrolled <= 0)
2067 line_count = 0;
2068 /* more than a screenfull, don't scroll but redraw */
2069 else if (used > curwin->w_height)
2070 line_count = used;
2071 /* scroll minimal number of lines */
2072 else
2073 {
2074 line_count = 0;
2075#ifdef FEAT_DIFF
2076 boff.fill = curwin->w_topfill;
2077#endif
2078 boff.lnum = curwin->w_topline - 1;
2079 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2080 {
2081 botline_forw(&boff);
2082 i += boff.height;
2083 ++line_count;
2084 }
2085 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2086 line_count = 9999;
2087 }
2088
2089 /*
2090 * Scroll up if the cursor is off the bottom of the screen a bit.
2091 * Otherwise put it at 1/2 of the screen.
2092 */
2093 if (line_count >= curwin->w_height && line_count > min_scroll)
2094 scroll_cursor_halfway(FALSE);
2095 else
2096 scrollup(line_count, TRUE);
2097
2098 /*
2099 * If topline didn't change we need to restore w_botline and w_empty_rows
2100 * (we changed them).
2101 * If topline did change, update_screen() will set botline.
2102 */
2103 if (curwin->w_topline == old_topline && set_topbot)
2104 {
2105 curwin->w_botline = old_botline;
2106 curwin->w_empty_rows = old_empty_rows;
2107 curwin->w_valid = old_valid;
2108 }
2109 curwin->w_valid |= VALID_TOPLINE;
2110}
2111
2112/*
2113 * Recompute topline to put the cursor halfway the window
2114 * If "atend" is TRUE, also put it halfway at the end of the file.
2115 */
2116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002117scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 int above = 0;
2120 linenr_T topline;
2121#ifdef FEAT_DIFF
2122 int topfill = 0;
2123#endif
2124 int below = 0;
2125 int used;
2126 lineoff_T loff;
2127 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002128#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002129 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2133#ifdef FEAT_FOLDING
2134 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2135#endif
2136#ifdef FEAT_DIFF
2137 used = plines_nofill(loff.lnum);
2138 loff.fill = 0;
2139 boff.fill = 0;
2140#else
2141 used = plines(loff.lnum);
2142#endif
2143 topline = loff.lnum;
2144 while (topline > 1)
2145 {
2146 if (below <= above) /* add a line below the cursor first */
2147 {
2148 if (boff.lnum < curbuf->b_ml.ml_line_count)
2149 {
2150 botline_forw(&boff);
2151 used += boff.height;
2152 if (used > curwin->w_height)
2153 break;
2154 below += boff.height;
2155 }
2156 else
2157 {
2158 ++below; /* count a "~" line */
2159 if (atend)
2160 ++used;
2161 }
2162 }
2163
2164 if (below > above) /* add a line above the cursor */
2165 {
2166 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002167 if (loff.height == MAXCOL)
2168 used = MAXCOL;
2169 else
2170 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (used > curwin->w_height)
2172 break;
2173 above += loff.height;
2174 topline = loff.lnum;
2175#ifdef FEAT_DIFF
2176 topfill = loff.fill;
2177#endif
2178 }
2179 }
2180#ifdef FEAT_FOLDING
2181 if (!hasFolding(topline, &curwin->w_topline, NULL))
2182#endif
2183 curwin->w_topline = topline;
2184#ifdef FEAT_DIFF
2185 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002186 if (old_topline > curwin->w_topline + curwin->w_height)
2187 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 check_topfill(curwin, FALSE);
2189#endif
2190 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2191 curwin->w_valid |= VALID_TOPLINE;
2192}
2193
2194/*
2195 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002196 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 * If not possible, put it at the same position as scroll_cursor_halfway().
2198 * When called topline must be valid!
2199 */
2200 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002201cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 int above = 0; /* screen lines above topline */
2204 linenr_T topline;
2205 int below = 0; /* screen lines below botline */
2206 linenr_T botline;
2207 int above_wanted, below_wanted;
2208 linenr_T cln; /* Cursor Line Number */
2209 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002210 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 /*
2213 * How many lines we would like to have above/below the cursor depends on
2214 * whether the first/last line of the file is on screen.
2215 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002216 above_wanted = so;
2217 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002218 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 above_wanted = mouse_dragging - 1;
2221 below_wanted = mouse_dragging - 1;
2222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (curwin->w_topline == 1)
2224 {
2225 above_wanted = 0;
2226 max_off = curwin->w_height / 2;
2227 if (below_wanted > max_off)
2228 below_wanted = max_off;
2229 }
2230 validate_botline();
2231 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002232 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
2234 below_wanted = 0;
2235 max_off = (curwin->w_height - 1) / 2;
2236 if (above_wanted > max_off)
2237 above_wanted = max_off;
2238 }
2239
2240 /*
2241 * If there are sufficient file-lines above and below the cursor, we can
2242 * return now.
2243 */
2244 cln = curwin->w_cursor.lnum;
2245 if (cln >= curwin->w_topline + above_wanted
2246 && cln < curwin->w_botline - below_wanted
2247#ifdef FEAT_FOLDING
2248 && !hasAnyFolding(curwin)
2249#endif
2250 )
2251 return;
2252
2253 /*
2254 * Narrow down the area where the cursor can be put by taking lines from
2255 * the top and the bottom until:
2256 * - the desired context lines are found
2257 * - the lines from the top is past the lines from the bottom
2258 */
2259 topline = curwin->w_topline;
2260 botline = curwin->w_botline - 1;
2261#ifdef FEAT_DIFF
2262 /* count filler lines as context */
2263 above = curwin->w_topfill;
2264 below = curwin->w_filler_rows;
2265#endif
2266 while ((above < above_wanted || below < below_wanted) && topline < botline)
2267 {
2268 if (below < below_wanted && (below <= above || above >= above_wanted))
2269 {
2270#ifdef FEAT_FOLDING
2271 if (hasFolding(botline, &botline, NULL))
2272 ++below;
2273 else
2274#endif
2275 below += plines(botline);
2276 --botline;
2277 }
2278 if (above < above_wanted && (above < below || below >= below_wanted))
2279 {
2280#ifdef FEAT_FOLDING
2281 if (hasFolding(topline, NULL, &topline))
2282 ++above;
2283 else
2284#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002285 above += PLINES_NOFILL(topline);
2286#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 /* Count filler lines below this line as context. */
2288 if (topline < botline)
2289 above += diff_check_fill(curwin, topline + 1);
2290#endif
2291 ++topline;
2292 }
2293 }
2294 if (topline == botline || botline == 0)
2295 curwin->w_cursor.lnum = topline;
2296 else if (topline > botline)
2297 curwin->w_cursor.lnum = botline;
2298 else
2299 {
2300 if (cln < topline && curwin->w_topline > 1)
2301 {
2302 curwin->w_cursor.lnum = topline;
2303 curwin->w_valid &=
2304 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2305 }
2306 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2307 {
2308 curwin->w_cursor.lnum = botline;
2309 curwin->w_valid &=
2310 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2311 }
2312 }
2313 curwin->w_valid |= VALID_TOPLINE;
2314}
2315
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002316static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318/*
2319 * move screen 'count' pages up or down and update screen
2320 *
2321 * return FAIL for failure, OK otherwise
2322 */
2323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 long n;
2327 int retval = OK;
2328 lineoff_T loff;
2329 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002330 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2333 {
2334 beep_flush();
2335 return FAIL;
2336 }
2337
2338 for ( ; count > 0; --count)
2339 {
2340 validate_botline();
2341 /*
2342 * It's an error to move a page up when the first line is already on
2343 * the screen. It's an error to move a page down when the last line
2344 * is on the screen and the topline is 'scrolloff' lines from the
2345 * last line.
2346 */
2347 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002348 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2350 : (curwin->w_topline == 1
2351#ifdef FEAT_DIFF
2352 && curwin->w_topfill ==
2353 diff_check_fill(curwin, curwin->w_topline)
2354#endif
2355 ))
2356 {
2357 beep_flush();
2358 retval = FAIL;
2359 break;
2360 }
2361
2362#ifdef FEAT_DIFF
2363 loff.fill = 0;
2364#endif
2365 if (dir == FORWARD)
2366 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002367 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002369 /* Vi compatible scrolling */
2370 if (p_window <= 2)
2371 ++curwin->w_topline;
2372 else
2373 curwin->w_topline += p_window - 2;
2374 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2375 curwin->w_topline = curbuf->b_ml.ml_line_count;
2376 curwin->w_cursor.lnum = curwin->w_topline;
2377 }
2378 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2379 {
2380 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 curwin->w_topline = curbuf->b_ml.ml_line_count;
2382#ifdef FEAT_DIFF
2383 curwin->w_topfill = 0;
2384#endif
2385 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2386 }
2387 else
2388 {
2389 /* For the overlap, start with the line just below the window
2390 * and go upwards. */
2391 loff.lnum = curwin->w_botline;
2392#ifdef FEAT_DIFF
2393 loff.fill = diff_check_fill(curwin, loff.lnum)
2394 - curwin->w_filler_rows;
2395#endif
2396 get_scroll_overlap(&loff, -1);
2397 curwin->w_topline = loff.lnum;
2398#ifdef FEAT_DIFF
2399 curwin->w_topfill = loff.fill;
2400 check_topfill(curwin, FALSE);
2401#endif
2402 curwin->w_cursor.lnum = curwin->w_topline;
2403 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2404 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2405 }
2406 }
2407 else /* dir == BACKWARDS */
2408 {
2409#ifdef FEAT_DIFF
2410 if (curwin->w_topline == 1)
2411 {
2412 /* Include max number of filler lines */
2413 max_topfill();
2414 continue;
2415 }
2416#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002417 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002418 {
2419 /* Vi compatible scrolling (sort of) */
2420 if (p_window <= 2)
2421 --curwin->w_topline;
2422 else
2423 curwin->w_topline -= p_window - 2;
2424 if (curwin->w_topline < 1)
2425 curwin->w_topline = 1;
2426 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2427 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2428 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2429 continue;
2430 }
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /* Find the line at the top of the window that is going to be the
2433 * line at the bottom of the window. Make sure this results in
2434 * the same line as before doing CTRL-F. */
2435 loff.lnum = curwin->w_topline - 1;
2436#ifdef FEAT_DIFF
2437 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2438 - curwin->w_topfill;
2439#endif
2440 get_scroll_overlap(&loff, 1);
2441
2442 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2443 {
2444 loff.lnum = curbuf->b_ml.ml_line_count;
2445#ifdef FEAT_DIFF
2446 loff.fill = 0;
2447 }
2448 else
2449 {
2450 botline_topline(&loff);
2451#endif
2452 }
2453 curwin->w_cursor.lnum = loff.lnum;
2454
2455 /* Find the line just above the new topline to get the right line
2456 * at the bottom of the window. */
2457 n = 0;
2458 while (n <= curwin->w_height && loff.lnum >= 1)
2459 {
2460 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002461 if (loff.height == MAXCOL)
2462 n = MAXCOL;
2463 else
2464 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002466 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 curwin->w_topline = 1;
2469#ifdef FEAT_DIFF
2470 max_topfill();
2471#endif
2472 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2473 }
2474 else
2475 {
2476 /* Go two lines forward again. */
2477#ifdef FEAT_DIFF
2478 topline_botline(&loff);
2479#endif
2480 botline_forw(&loff);
2481 botline_forw(&loff);
2482#ifdef FEAT_DIFF
2483 botline_topline(&loff);
2484#endif
2485#ifdef FEAT_FOLDING
2486 /* We're at the wrong end of a fold now. */
2487 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2488#endif
2489
2490 /* Always scroll at least one line. Avoid getting stuck on
2491 * very long lines. */
2492 if (loff.lnum >= curwin->w_topline
2493#ifdef FEAT_DIFF
2494 && (loff.lnum > curwin->w_topline
2495 || loff.fill >= curwin->w_topfill)
2496#endif
2497 )
2498 {
2499#ifdef FEAT_DIFF
2500 /* First try using the maximum number of filler lines. If
2501 * that's not enough, backup one line. */
2502 loff.fill = curwin->w_topfill;
2503 if (curwin->w_topfill < diff_check_fill(curwin,
2504 curwin->w_topline))
2505 max_topfill();
2506 if (curwin->w_topfill == loff.fill)
2507#endif
2508 {
2509 --curwin->w_topline;
2510#ifdef FEAT_DIFF
2511 curwin->w_topfill = 0;
2512#endif
2513 }
2514 comp_botline(curwin);
2515 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002516 curwin->w_valid &=
2517 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519 else
2520 {
2521 curwin->w_topline = loff.lnum;
2522#ifdef FEAT_DIFF
2523 curwin->w_topfill = loff.fill;
2524 check_topfill(curwin, FALSE);
2525#endif
2526 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2527 }
2528 }
2529 }
2530 }
2531#ifdef FEAT_FOLDING
2532 foldAdjustCursor();
2533#endif
2534 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002535 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002536 if (retval == OK)
2537 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2539
Bram Moolenaar907dad72018-07-10 15:07:15 +02002540 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002542 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2543 // But make sure we scroll at least one line (happens with mix of long
2544 // wrapping lines and non-wrapping line).
2545 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002547 scroll_cursor_top(1, FALSE);
2548 if (curwin->w_topline <= old_topline
2549 && old_topline < curbuf->b_ml.ml_line_count)
2550 {
2551 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002553 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2554#endif
2555 }
2556 }
2557#ifdef FEAT_FOLDING
2558 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
2563 redraw_later(VALID);
2564 return retval;
2565}
2566
2567/*
2568 * Decide how much overlap to use for page-up or page-down scrolling.
2569 * This is symmetric, so that doing both keeps the same lines displayed.
2570 * Three lines are examined:
2571 *
2572 * before CTRL-F after CTRL-F / before CTRL-B
2573 * etc. l1
2574 * l1 last but one line ------------
2575 * l2 last text line l2 top text line
2576 * ------------- l3 second text line
2577 * l3 etc.
2578 */
2579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 int h1, h2, h3, h4;
2583 int min_height = curwin->w_height - 2;
2584 lineoff_T loff0, loff1, loff2;
2585
2586#ifdef FEAT_DIFF
2587 if (lp->fill > 0)
2588 lp->height = 1;
2589 else
2590 lp->height = plines_nofill(lp->lnum);
2591#else
2592 lp->height = plines(lp->lnum);
2593#endif
2594 h1 = lp->height;
2595 if (h1 > min_height)
2596 return; /* no overlap */
2597
2598 loff0 = *lp;
2599 if (dir > 0)
2600 botline_forw(lp);
2601 else
2602 topline_back(lp);
2603 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002604 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 *lp = loff0; /* no overlap */
2607 return;
2608 }
2609
2610 loff1 = *lp;
2611 if (dir > 0)
2612 botline_forw(lp);
2613 else
2614 topline_back(lp);
2615 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002616 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 *lp = loff0; /* no overlap */
2619 return;
2620 }
2621
2622 loff2 = *lp;
2623 if (dir > 0)
2624 botline_forw(lp);
2625 else
2626 topline_back(lp);
2627 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002628 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 *lp = loff1; /* 1 line overlap */
2630 else
2631 *lp = loff2; /* 2 lines overlap */
2632 return;
2633}
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635/*
2636 * Scroll 'scroll' lines up or down.
2637 */
2638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002639halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 long scrolled = 0;
2642 int i;
2643 int n;
2644 int room;
2645
2646 if (Prenum)
2647 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2648 curwin->w_height : Prenum;
2649 n = (curwin->w_p_scr <= curwin->w_height) ?
2650 curwin->w_p_scr : curwin->w_height;
2651
Bram Moolenaard5d37532017-03-27 23:02:07 +02002652 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 validate_botline();
2654 room = curwin->w_empty_rows;
2655#ifdef FEAT_DIFF
2656 room += curwin->w_filler_rows;
2657#endif
2658 if (flag)
2659 {
2660 /*
2661 * scroll the text up
2662 */
2663 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2664 {
2665#ifdef FEAT_DIFF
2666 if (curwin->w_topfill > 0)
2667 {
2668 i = 1;
2669 if (--n < 0 && scrolled > 0)
2670 break;
2671 --curwin->w_topfill;
2672 }
2673 else
2674#endif
2675 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002676 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 n -= i;
2678 if (n < 0 && scrolled > 0)
2679 break;
2680#ifdef FEAT_FOLDING
2681 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2682#endif
2683 ++curwin->w_topline;
2684#ifdef FEAT_DIFF
2685 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2686#endif
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2689 {
2690 ++curwin->w_cursor.lnum;
2691 curwin->w_valid &=
2692 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2696 scrolled += i;
2697
2698 /*
2699 * Correct w_botline for changed w_topline.
2700 * Won't work when there are filler lines.
2701 */
2702#ifdef FEAT_DIFF
2703 if (curwin->w_p_diff)
2704 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2705 else
2706#endif
2707 {
2708 room += i;
2709 do
2710 {
2711 i = plines(curwin->w_botline);
2712 if (i > room)
2713 break;
2714#ifdef FEAT_FOLDING
2715 (void)hasFolding(curwin->w_botline, NULL,
2716 &curwin->w_botline);
2717#endif
2718 ++curwin->w_botline;
2719 room -= i;
2720 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2721 }
2722 }
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 /*
2725 * When hit bottom of the file: move cursor down.
2726 */
2727 if (n > 0)
2728 {
2729# ifdef FEAT_FOLDING
2730 if (hasAnyFolding(curwin))
2731 {
2732 while (--n >= 0
2733 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2734 {
2735 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2736 &curwin->w_cursor.lnum);
2737 ++curwin->w_cursor.lnum;
2738 }
2739 }
2740 else
2741# endif
2742 curwin->w_cursor.lnum += n;
2743 check_cursor_lnum();
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 else
2747 {
2748 /*
2749 * scroll the text down
2750 */
2751 while (n > 0 && curwin->w_topline > 1)
2752 {
2753#ifdef FEAT_DIFF
2754 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2755 {
2756 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002757 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 ++curwin->w_topfill;
2759 }
2760 else
2761#endif
2762 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002763 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 n -= i;
2765 if (n < 0 && scrolled > 0)
2766 break;
2767 --curwin->w_topline;
2768#ifdef FEAT_FOLDING
2769 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2770#endif
2771#ifdef FEAT_DIFF
2772 curwin->w_topfill = 0;
2773#endif
2774 }
2775 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2776 VALID_BOTLINE|VALID_BOTLINE_AP);
2777 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (curwin->w_cursor.lnum > 1)
2779 {
2780 --curwin->w_cursor.lnum;
2781 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 /*
2786 * When hit top of the file: move cursor up.
2787 */
2788 if (n > 0)
2789 {
2790 if (curwin->w_cursor.lnum <= (linenr_T)n)
2791 curwin->w_cursor.lnum = 1;
2792 else
2793# ifdef FEAT_FOLDING
2794 if (hasAnyFolding(curwin))
2795 {
2796 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2797 {
2798 --curwin->w_cursor.lnum;
2799 (void)hasFolding(curwin->w_cursor.lnum,
2800 &curwin->w_cursor.lnum, NULL);
2801 }
2802 }
2803 else
2804# endif
2805 curwin->w_cursor.lnum -= n;
2806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808# ifdef FEAT_FOLDING
2809 /* Move cursor to first line of closed fold. */
2810 foldAdjustCursor();
2811# endif
2812#ifdef FEAT_DIFF
2813 check_topfill(curwin, !flag);
2814#endif
2815 cursor_correct();
2816 beginline(BL_SOL | BL_FIX);
2817 redraw_later(VALID);
2818}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819
Bram Moolenaar860cae12010-06-05 23:22:07 +02002820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002821do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002822{
2823 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002824 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002825 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002826 colnr_T curswant = curwin->w_curswant;
2827 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 win_T *old_curwin = curwin;
2829 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002830 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002831 int old_VIsual_select = VIsual_select;
2832 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002833
2834 /*
2835 * loop through the cursorbound windows
2836 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002838 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002839 {
2840 curbuf = curwin->w_buffer;
2841 /* skip original window and windows with 'noscrollbind' */
2842 if (curwin != old_curwin && curwin->w_p_crb)
2843 {
2844# ifdef FEAT_DIFF
2845 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002846 curwin->w_cursor.lnum =
2847 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002848 else
2849# endif
2850 curwin->w_cursor.lnum = line;
2851 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002852 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002853 curwin->w_curswant = curswant;
2854 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002855
Bram Moolenaar61452852011-02-01 18:01:11 +01002856 /* Make sure the cursor is in a valid position. Temporarily set
2857 * "restart_edit" to allow the cursor to be beyond the EOL. */
2858 restart_edit_save = restart_edit;
2859 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002860 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002861# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002862 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002863 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002864# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002865 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002866 /* Correct cursor for multi-byte character. */
2867 if (has_mbyte)
2868 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002869 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002870
2871 /* Only scroll when 'scrollbind' hasn't done this. */
2872 if (!curwin->w_p_scb)
2873 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002874 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002875 }
2876 }
2877
2878 /*
2879 * reset current-window
2880 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 VIsual_select = old_VIsual_select;
2882 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883 curwin = old_curwin;
2884 curbuf = old_curbuf;
2885}