blob: 0df4fe5ba4ded23b71a861db98ab6ee62aee54d8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
28 linenr_T lnum; /* line number */
29#ifdef FEAT_DIFF
30 int fill; /* filler lines */
31#endif
32 int height; /* height of added line */
33} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
111 /* wp->w_botline is the line that is just below the window */
112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100139#ifdef FEAT_INS_EXPAND
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100140 && !pum_visible()
Bram Moolenaar6f7e5552019-01-11 11:55:16 +0100141#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100142 )
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200144 if (wp->w_p_rnu)
145 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200146 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200147#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 if (wp->w_p_cul)
149 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200150 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200151 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200152 // "w_last_cursorline" may be outdated, worst case we redraw
153 // too much. This is optimized for moving the cursor around in
154 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100155 redrawWinline(wp, wp->w_last_cursorline);
156 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
158 else
159 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200160 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200161#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200162 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100163}
164
165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 * Update curwin->w_topline and redraw if necessary.
167 * Used to update the screen before printing a message.
168 */
169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100170update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 update_topline();
173 if (must_redraw)
174 update_screen(0);
175}
176
177/*
178 * Update curwin->w_topline to move the cursor onto the screen.
179 */
180 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100181update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 long line_count;
184 int halfheight;
185 int n;
186 linenr_T old_topline;
187#ifdef FEAT_DIFF
188 int old_topfill;
189#endif
190#ifdef FEAT_FOLDING
191 linenr_T lnum;
192#endif
193 int check_topline = FALSE;
194 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100195 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#ifdef FEAT_MOUSE
Bram Moolenaar375e3392019-01-31 18:26:10 +0100197 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
199
Bram Moolenaard5d37532017-03-27 23:02:07 +0200200 /* If there is no valid screen and when the window height is zero just use
201 * the cursor line. */
202 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 {
204 curwin->w_topline = curwin->w_cursor.lnum;
205 curwin->w_botline = curwin->w_topline;
206 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200207 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200208 return;
209 }
210
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 check_cursor_moved(curwin);
212 if (curwin->w_valid & VALID_TOPLINE)
213 return;
214
215#ifdef FEAT_MOUSE
216 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000217 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100218 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#endif
220
221 old_topline = curwin->w_topline;
222#ifdef FEAT_DIFF
223 old_topfill = curwin->w_topfill;
224#endif
225
226 /*
227 * If the buffer is empty, always set topline to 1.
228 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100229 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 {
231 if (curwin->w_topline != 1)
232 redraw_later(NOT_VALID);
233 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 curwin->w_botline = 2;
235 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 }
238
239 /*
240 * If the cursor is above or near the top of the window, scroll the window
241 * to show the line the cursor is in, with 'scrolloff' context.
242 */
243 else
244 {
245 if (curwin->w_topline > 1)
246 {
247 /* If the cursor is above topline, scrolling is always needed.
248 * If the cursor is far below topline and there is no folding,
249 * scrolling down is never needed. */
250 if (curwin->w_cursor.lnum < curwin->w_topline)
251 check_topline = TRUE;
252 else if (check_top_offset())
253 check_topline = TRUE;
254 }
255#ifdef FEAT_DIFF
256 /* Check if there are more filler lines than allowed. */
257 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
258 curwin->w_topline))
259 check_topline = TRUE;
260#endif
261
262 if (check_topline)
263 {
264 halfheight = curwin->w_height / 2 - 1;
265 if (halfheight < 2)
266 halfheight = 2;
267
268#ifdef FEAT_FOLDING
269 if (hasAnyFolding(curwin))
270 {
271 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100272 * topline + scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 * scrolled). */
274 n = 0;
275 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100276 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
278 ++n;
279 /* stop at end of file or when we know we are far off */
280 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
281 break;
282 (void)hasFolding(lnum, NULL, &lnum);
283 }
284 }
285 else
286#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100287 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288
289 /* If we weren't very close to begin with, we scroll to put the
290 * cursor in the middle of the window. Otherwise put the cursor
291 * near the top of the window. */
292 if (n >= halfheight)
293 scroll_cursor_halfway(FALSE);
294 else
295 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000296 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 check_botline = TRUE;
298 }
299 }
300
301 else
302 {
303#ifdef FEAT_FOLDING
304 /* Make sure topline is the first line of a fold. */
305 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
306#endif
307 check_botline = TRUE;
308 }
309 }
310
311 /*
312 * If the cursor is below the bottom of the window, scroll the window
313 * to put the cursor on the window.
314 * When w_botline is invalid, recompute it first, to avoid a redraw later.
315 * If w_botline was approximated, we might need a redraw later in a few
316 * cases, but we don't want to spend (a lot of) time recomputing w_botline
317 * for every small change.
318 */
319 if (check_botline)
320 {
321 if (!(curwin->w_valid & VALID_BOTLINE_AP))
322 validate_botline();
323
324 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
325 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000326 if (curwin->w_cursor.lnum < curwin->w_botline)
327 {
328 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100329 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_FOLDING
331 || hasAnyFolding(curwin)
332#endif
333 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 lineoff_T loff;
336
Bram Moolenaard4153d42008-11-15 15:06:17 +0000337 /* Cursor is (a few lines) above botline, check if there are
338 * 'scrolloff' window lines below the cursor. If not, need to
339 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 n = curwin->w_empty_rows;
341 loff.lnum = curwin->w_cursor.lnum;
342#ifdef FEAT_FOLDING
343 /* In a fold go to its last line. */
344 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
345#endif
346#ifdef FEAT_DIFF
347 loff.fill = 0;
348 n += curwin->w_filler_rows;
349#endif
350 loff.height = 0;
351 while (loff.lnum < curwin->w_botline
352#ifdef FEAT_DIFF
353 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
354#endif
355 )
356 {
357 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100358 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 break;
360 botline_forw(&loff);
361 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100362 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 /* sufficient context, no need to scroll */
364 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000365 }
366 else
367 /* sufficient context, no need to scroll */
368 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
370 if (check_botline)
371 {
372#ifdef FEAT_FOLDING
373 if (hasAnyFolding(curwin))
374 {
375 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100376 * botline - scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * scrolled). */
378 line_count = 0;
379 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100380 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
382 ++line_count;
383 /* stop at end of file or when we know we are far off */
384 if (lnum <= 0 || line_count > curwin->w_height + 1)
385 break;
386 (void)hasFolding(lnum, &lnum, NULL);
387 }
388 }
389 else
390#endif
391 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000394 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 else
396 scroll_cursor_halfway(FALSE);
397 }
398 }
399 }
400 curwin->w_valid |= VALID_TOPLINE;
401
402 /*
403 * Need to redraw when topline changed.
404 */
405 if (curwin->w_topline != old_topline
406#ifdef FEAT_DIFF
407 || curwin->w_topfill != old_topfill
408#endif
409 )
410 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100411 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000412 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
414 curwin->w_skipcol = 0;
415 redraw_later(NOT_VALID);
416 }
417 else
418 redraw_later(VALID);
419 /* May need to set w_skipcol when cursor in w_topline. */
420 if (curwin->w_cursor.lnum == curwin->w_topline)
421 validate_cursor();
422 }
423
424#ifdef FEAT_MOUSE
Bram Moolenaar375e3392019-01-31 18:26:10 +0100425 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
427}
428
429/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000430 * Return the scrolljump value to use for the current window.
431 * When 'scrolljump' is positive use it as-is.
432 * When 'scrolljump' is negative use it as a percentage of the window height.
433 */
434 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100435scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000436{
437 if (p_sj >= 0)
438 return (int)p_sj;
439 return (curwin->w_height * -p_sj) / 100;
440}
441
442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
444 * current window.
445 */
446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 lineoff_T loff;
450 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100451 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaar375e3392019-01-31 18:26:10 +0100453 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#ifdef FEAT_FOLDING
455 || hasAnyFolding(curwin)
456#endif
457 )
458 {
459 loff.lnum = curwin->w_cursor.lnum;
460#ifdef FEAT_DIFF
461 loff.fill = 0;
462 n = curwin->w_topfill; /* always have this context */
463#else
464 n = 0;
465#endif
466 /* Count the visible screen lines above the cursor line. */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {
469 topline_back(&loff);
470 /* Stop when included a line above the window. */
471 if (loff.lnum < curwin->w_topline
472#ifdef FEAT_DIFF
473 || (loff.lnum == curwin->w_topline && loff.fill > 0)
474#endif
475 )
476 break;
477 n += loff.height;
478 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100479 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 return TRUE;
481 }
482 return FALSE;
483}
484
485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100486update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 if (curwin->w_set_curswant)
489 {
490 validate_virtcol();
491 curwin->w_curswant = curwin->w_virtcol;
492 curwin->w_set_curswant = FALSE;
493 }
494}
495
496/*
497 * Check if the cursor has moved. Set the w_valid flag accordingly.
498 */
499 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100500check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
503 {
504 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
505 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
506 wp->w_valid_cursor = wp->w_cursor;
507 wp->w_valid_leftcol = wp->w_leftcol;
508 }
509 else if (wp->w_cursor.col != wp->w_valid_cursor.col
510 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100511 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
513 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
514 wp->w_valid_cursor.col = wp->w_cursor.col;
515 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518}
519
520/*
521 * Call this function when some window settings have changed, which require
522 * the cursor position, botline and topline to be recomputed and the window to
523 * be redrawn. E.g, when changing the 'wrap' option or folding.
524 */
525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100526changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 changed_window_setting_win(curwin);
529}
530
531 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100532changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 wp->w_lines_valid = 0;
535 changed_line_abv_curs_win(wp);
536 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
537 redraw_win_later(wp, NOT_VALID);
538}
539
540/*
541 * Set wp->w_topline to a certain number.
542 */
543 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546#ifdef FEAT_FOLDING
547 /* go to first of folded lines */
548 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
549#endif
550 /* Approximate the value of w_botline */
551 wp->w_botline += lnum - wp->w_topline;
552 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000553 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_DIFF
555 wp->w_topfill = 0;
556#endif
557 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
558 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
559 redraw_later(VALID);
560}
561
562/*
563 * Call this function when the length of the cursor line (in screen
564 * characters) has changed, and the change is before the cursor.
565 * Need to take care of w_botline separately!
566 */
567 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100568changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
571 |VALID_CHEIGHT|VALID_TOPLINE);
572}
573
574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100575changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
578 |VALID_CHEIGHT|VALID_TOPLINE);
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Call this function when the length of a line (in screen characters) above
583 * the cursor have changed.
584 * Need to take care of w_botline separately!
585 */
586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100587changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
590 |VALID_CHEIGHT|VALID_TOPLINE);
591}
592
593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
596 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
597 |VALID_CHEIGHT|VALID_TOPLINE);
598}
599
600/*
601 * Make sure the value of curwin->w_botline is valid.
602 */
603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 if (!(curwin->w_valid & VALID_BOTLINE))
607 comp_botline(curwin);
608}
609
610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * Mark curwin->w_botline as invalid (because of some change in the buffer).
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
617}
618
619 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100620invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
623}
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100626approximate_botline_win(
627 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 wp->w_valid &= ~VALID_BOTLINE;
630}
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632/*
633 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
634 */
635 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100636cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 check_cursor_moved(curwin);
639 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
640 (VALID_WROW|VALID_WCOL));
641}
642
643/*
644 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
645 * w_topline must be valid, you may need to call update_topline() first!
646 */
647 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 check_cursor_moved(curwin);
651 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
652 curs_columns(TRUE);
653}
654
655#if defined(FEAT_GUI) || defined(PROTO)
656/*
657 * validate w_cline_row.
658 */
659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100660validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 /*
663 * First make sure that w_topline is valid (after moving the cursor).
664 */
665 update_topline();
666 check_cursor_moved(curwin);
667 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100668 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669}
670#endif
671
672/*
673 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200674 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 */
676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100677curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
679 linenr_T lnum;
680 int i;
681 int all_invalid;
682 int valid;
683#ifdef FEAT_FOLDING
684 long fold_count;
685#endif
686
687 /* Check if wp->w_lines[].wl_size is invalid */
688 all_invalid = (!redrawing()
689 || wp->w_lines_valid == 0
690 || wp->w_lines[0].wl_lnum > wp->w_topline);
691 i = 0;
692 wp->w_cline_row = 0;
693 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
694 {
695 valid = FALSE;
696 if (!all_invalid && i < wp->w_lines_valid)
697 {
698 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
699 continue; /* skip changed or deleted lines */
700 if (wp->w_lines[i].wl_lnum == lnum)
701 {
702#ifdef FEAT_FOLDING
703 /* Check for newly inserted lines below this row, in which
704 * case we need to check for folded lines. */
705 if (!wp->w_buffer->b_mod_set
706 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
707 || wp->w_buffer->b_mod_top
708 > wp->w_lines[i].wl_lastlnum + 1)
709#endif
710 valid = TRUE;
711 }
712 else if (wp->w_lines[i].wl_lnum > lnum)
713 --i; /* hold at inserted lines */
714 }
715 if (valid
716#ifdef FEAT_DIFF
717 && (lnum != wp->w_topline || !wp->w_p_diff)
718#endif
719 )
720 {
721#ifdef FEAT_FOLDING
722 lnum = wp->w_lines[i].wl_lastlnum + 1;
723 /* Cursor inside folded lines, don't count this row */
724 if (lnum > wp->w_cursor.lnum)
725 break;
726#else
727 ++lnum;
728#endif
729 wp->w_cline_row += wp->w_lines[i].wl_size;
730 }
731 else
732 {
733#ifdef FEAT_FOLDING
734 fold_count = foldedCount(wp, lnum, NULL);
735 if (fold_count)
736 {
737 lnum += fold_count;
738 if (lnum > wp->w_cursor.lnum)
739 break;
740 ++wp->w_cline_row;
741 }
742 else
743#endif
744#ifdef FEAT_DIFF
745 if (lnum == wp->w_topline)
746 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
747 + wp->w_topfill;
748 else
749#endif
750 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
751 }
752 }
753
754 check_cursor_moved(wp);
755 if (!(wp->w_valid & VALID_CHEIGHT))
756 {
757 if (all_invalid
758 || i == wp->w_lines_valid
759 || (i < wp->w_lines_valid
760 && (!wp->w_lines[i].wl_valid
761 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
762 {
763#ifdef FEAT_DIFF
764 if (wp->w_cursor.lnum == wp->w_topline)
765 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
766 TRUE) + wp->w_topfill;
767 else
768#endif
769 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
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 if (i > wp->w_lines_valid)
776 {
777 /* a line that is too long to fit on the last screen line */
778 wp->w_cline_height = 0;
779#ifdef FEAT_FOLDING
780 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
781 NULL, NULL, TRUE, NULL);
782#endif
783 }
784 else
785 {
786 wp->w_cline_height = wp->w_lines[i].wl_size;
787#ifdef FEAT_FOLDING
788 wp->w_cline_folded = wp->w_lines[i].wl_folded;
789#endif
790 }
791 }
792
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100793 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Validate curwin->w_virtcol only.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 validate_virtcol_win(curwin);
805}
806
807/*
808 * Validate wp->w_virtcol only.
809 */
810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 check_cursor_moved(wp);
814 if (!(wp->w_valid & VALID_VIRTCOL))
815 {
816 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
817 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000818#ifdef FEAT_SYN_HL
Bram Moolenaar019ff682006-03-13 22:10:45 +0000819 if (wp->w_p_cuc
820# ifdef FEAT_INS_EXPAND
821 && !pum_visible()
822# endif
823 )
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000824 redraw_win_later(wp, SOME_VALID);
825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
827}
828
829/*
830 * Validate curwin->w_cline_height only.
831 */
832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 check_cursor_moved(curwin);
836 if (!(curwin->w_valid & VALID_CHEIGHT))
837 {
838#ifdef FEAT_DIFF
839 if (curwin->w_cursor.lnum == curwin->w_topline)
840 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
841 + curwin->w_topfill;
842 else
843#endif
844 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
845#ifdef FEAT_FOLDING
846 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
847#endif
848 curwin->w_valid |= VALID_CHEIGHT;
849 }
850}
851
852/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000853 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100856validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
858 colnr_T off;
859 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100860 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 validate_virtcol();
863 if (!(curwin->w_valid & VALID_WCOL))
864 {
865 col = curwin->w_virtcol;
866 off = curwin_col_off();
867 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200868 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000871 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200872 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100873 && width > 0)
874 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200875 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000876 if (col > (int)curwin->w_leftcol)
877 col -= curwin->w_leftcol;
878 else
879 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 curwin->w_valid |= VALID_WCOL;
883 }
884}
885
886/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200887 * Compute offset of a window, occupied by absolute or relative line number,
888 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 */
890 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
Bram Moolenaar64486672010-05-16 15:46:46 +0200893 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef FEAT_CMDWIN
895 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
896#endif
897#ifdef FEAT_FOLDING
898 + wp->w_p_fdc
899#endif
900#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200901 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
903 );
904}
905
906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 return win_col_off(curwin);
910}
911
912/*
913 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200914 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
915 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
917 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100918win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaar64486672010-05-16 15:46:46 +0200920 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000921 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 return 0;
923}
924
925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 return win_col_off2(curwin);
929}
930
931/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100932 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * Also updates curwin->w_wrow and curwin->w_cline_row.
934 * Also updates curwin->w_leftcol.
935 */
936 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100937curs_columns(
938 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 int diff;
941 int extra; /* offset for first screen line */
942 int off_left, off_right;
943 int n;
944 int p_lines;
945 int width = 0;
946 int textwidth;
947 int new_leftcol;
948 colnr_T startcol;
949 colnr_T endcol;
950 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100951 long so = get_scrolloff_value();
952 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
954 /*
955 * First make sure that w_topline is valid (after moving the cursor).
956 */
957 update_topline();
958
959 /*
960 * Next make sure that w_cline_row is valid.
961 */
962 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100963 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 /*
966 * Compute the number of virtual columns.
967 */
968#ifdef FEAT_FOLDING
969 if (curwin->w_cline_folded)
970 /* In a folded line the cursor is always in the first column */
971 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
972 else
973#endif
974 getvvcol(curwin, &curwin->w_cursor,
975 &startcol, &(curwin->w_virtcol), &endcol);
976
977 /* remove '$' from change command when cursor moves onto it */
978 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100979 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 extra = curwin_col_off();
982 curwin->w_wcol = curwin->w_virtcol + extra;
983 endcol += extra;
984
985 /*
986 * Now compute w_wrow, counting screen lines from w_cline_row.
987 */
988 curwin->w_wrow = curwin->w_cline_row;
989
Bram Moolenaar02631462017-09-22 15:20:32 +0200990 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (textwidth <= 0)
992 {
993 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200994 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 curwin->w_wrow = curwin->w_height - 1;
996 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200997 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 width = textwidth + curwin_col_off2();
1000
1001 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +02001002 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 {
Bram Moolenaar6427c602010-02-03 17:43:07 +01001004 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +02001005 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 curwin->w_wcol -= n * width;
1007 curwin->w_wrow += n;
1008
1009#ifdef FEAT_LINEBREAK
1010 /* When cursor wraps to first char of next line in Insert
1011 * mode, the 'showbreak' string isn't shown, backup to first
1012 * column */
1013 if (*p_sbr && *ml_get_cursor() == NUL
1014 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1015 curwin->w_wcol = 0;
1016#endif
1017 }
1018 }
1019
1020 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1021 * is not folded.
1022 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001023 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef FEAT_FOLDING
1025 && !curwin->w_cline_folded
1026#endif
1027 )
1028 {
1029 /*
1030 * If Cursor is left of the screen, scroll rightwards.
1031 * If Cursor is right of the screen, scroll leftwards
1032 * If we get closer to the edge than 'sidescrolloff', scroll a little
1033 * extra
1034 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001035 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001036 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001037 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (off_left < 0 || off_right > 0)
1039 {
1040 if (off_left < 0)
1041 diff = -off_left;
1042 else
1043 diff = off_right;
1044
1045 /* When far off or not enough room on either side, put cursor in
1046 * middle of window. */
1047 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1048 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1049 else
1050 {
1051 if (diff < p_ss)
1052 diff = p_ss;
1053 if (off_left < 0)
1054 new_leftcol = curwin->w_leftcol - diff;
1055 else
1056 new_leftcol = curwin->w_leftcol + diff;
1057 }
1058 if (new_leftcol < 0)
1059 new_leftcol = 0;
1060 if (new_leftcol != (int)curwin->w_leftcol)
1061 {
1062 curwin->w_leftcol = new_leftcol;
1063 /* screen has to be redrawn with new curwin->w_leftcol */
1064 redraw_later(NOT_VALID);
1065 }
1066 }
1067 curwin->w_wcol -= curwin->w_leftcol;
1068 }
1069 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1070 curwin->w_wcol -= curwin->w_leftcol;
1071 else
1072 curwin->w_wcol = 0;
1073
1074#ifdef FEAT_DIFF
1075 /* Skip over filler lines. At the top use w_topfill, there
1076 * may be some filler lines above the window. */
1077 if (curwin->w_cursor.lnum == curwin->w_topline)
1078 curwin->w_wrow += curwin->w_topfill;
1079 else
1080 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1081#endif
1082
1083 prev_skipcol = curwin->w_skipcol;
1084
1085 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if ((curwin->w_wrow >= curwin->w_height
1088 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001089 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 && (p_lines =
1091#ifdef FEAT_DIFF
1092 plines_win_nofill
1093#else
1094 plines_win
1095#endif
1096 (curwin, curwin->w_cursor.lnum, FALSE))
1097 - 1 >= curwin->w_height))
1098 && curwin->w_height != 0
1099 && curwin->w_cursor.lnum == curwin->w_topline
1100 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001101 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 /* Cursor past end of screen. Happens with a single line that does
1104 * not fit on screen. Find a skipcol to show the text around the
1105 * cursor. Avoid scrolling all the time. compute value of "extra":
Bram Moolenaar375e3392019-01-31 18:26:10 +01001106 * 1: Less than 'scrolloff' lines above
1107 * 2: Less than 'scrolloff' lines below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * 3: both of them */
1109 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001110 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 extra = 1;
1112 /* Compute last display line of the buffer line that we want at the
1113 * bottom of the window. */
1114 if (p_lines == 0)
1115 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1116 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001117 if (p_lines > curwin->w_wrow + so)
1118 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 else
1120 n = p_lines;
1121 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1122 extra += 2;
1123
Bram Moolenaar375e3392019-01-31 18:26:10 +01001124 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
1126 /* not enough room for 'scrolloff', put cursor in the middle */
1127 n = curwin->w_virtcol / width;
1128 if (n > curwin->w_height / 2)
1129 n -= curwin->w_height / 2;
1130 else
1131 n = 0;
1132 /* don't skip more than necessary */
1133 if (n > p_lines - curwin->w_height + 1)
1134 n = p_lines - curwin->w_height + 1;
1135 curwin->w_skipcol = n * width;
1136 }
1137 else if (extra == 1)
1138 {
1139 /* less then 'scrolloff' lines above, decrease skipcol */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001140 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 + width - 1) / width;
1142 if (extra > 0)
1143 {
1144 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1145 extra = curwin->w_skipcol / width;
1146 curwin->w_skipcol -= extra * width;
1147 }
1148 }
1149 else if (extra == 2)
1150 {
1151 /* less then 'scrolloff' lines below, increase skipcol */
1152 endcol = (n - curwin->w_height + 1) * width;
1153 while (endcol > curwin->w_virtcol)
1154 endcol -= width;
1155 if (endcol > curwin->w_skipcol)
1156 curwin->w_skipcol = endcol;
1157 }
1158
1159 curwin->w_wrow -= curwin->w_skipcol / width;
1160 if (curwin->w_wrow >= curwin->w_height)
1161 {
1162 /* small window, make sure cursor is in it */
1163 extra = curwin->w_wrow - curwin->w_height + 1;
1164 curwin->w_skipcol += extra * width;
1165 curwin->w_wrow -= extra;
1166 }
1167
1168 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1169 if (extra > 0)
1170 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1171 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001172 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 else
1175 curwin->w_skipcol = 0;
1176 if (prev_skipcol != curwin->w_skipcol)
1177 redraw_later(NOT_VALID);
1178
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001179#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001180 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1181 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaar64486672010-05-16 15:46:46 +02001182# ifdef FEAT_INS_EXPAND
Bram Moolenaarb6798752014-03-27 12:11:48 +01001183 && !pum_visible()
Bram Moolenaar64486672010-05-16 15:46:46 +02001184# endif
Bram Moolenaarb6798752014-03-27 12:11:48 +01001185 )
1186 redraw_later(SOME_VALID);
1187#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1190}
1191
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001192#if defined(FEAT_EVAL) || defined(PROTO)
1193/*
1194 * Compute the screen position of text character at "pos" in window "wp"
1195 * The resulting values are one-based, zero when character is not visible.
1196 */
1197 static void
1198textpos2screenpos(
1199 win_T *wp,
1200 pos_T *pos,
1201 int *rowp, // screen row
1202 int *scolp, // start screen column
1203 int *ccolp, // cursor screen column
1204 int *ecolp) // end screen column
1205{
1206 colnr_T scol = 0, ccol = 0, ecol = 0;
1207 int row = 0;
1208 int rowoff = 0;
1209 colnr_T coloff = 0;
1210
1211 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1212 {
1213 colnr_T off;
1214 colnr_T col;
1215 int width;
1216
1217 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1218 getvcol(wp, pos, &scol, &ccol, &ecol);
1219
1220 // similar to what is done in validate_cursor_col()
1221 col = scol;
1222 off = win_col_off(wp);
1223 col += off;
1224 width = wp->w_width - off + win_col_off2(wp);
1225
1226 /* long line wrapping, adjust row */
1227 if (wp->w_p_wrap
1228 && col >= (colnr_T)wp->w_width
1229 && width > 0)
1230 {
1231 /* use same formula as what is used in curs_columns() */
1232 rowoff = ((col - wp->w_width) / width + 1);
1233 col -= rowoff * width;
1234 }
1235 col -= wp->w_leftcol;
1236 if (col >= width)
1237 col = -1;
1238 if (col >= 0)
1239 coloff = col - scol + wp->w_wincol + 1;
1240 else
1241 // character is left or right of the window
1242 row = scol = ccol = ecol = 0;
1243 }
1244 *rowp = wp->w_winrow + row + rowoff;
1245 *scolp = scol + coloff;
1246 *ccolp = ccol + coloff;
1247 *ecolp = ecol + coloff;
1248}
1249
1250/*
1251 * "screenpos({winid}, {lnum}, {col})" function
1252 */
1253 void
1254f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1255{
1256 dict_T *dict;
1257 win_T *wp;
1258 pos_T pos;
1259 int row = 0;
1260 int scol = 0, ccol = 0, ecol = 0;
1261
1262 if (rettv_dict_alloc(rettv) != OK)
1263 return;
1264 dict = rettv->vval.v_dict;
1265
1266 wp = find_win_by_nr_or_id(&argvars[0]);
1267 if (wp == NULL)
1268 return;
1269
1270 pos.lnum = tv_get_number(&argvars[1]);
1271 pos.col = tv_get_number(&argvars[2]) - 1;
1272 pos.coladd = 0;
1273 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1274
1275 dict_add_number(dict, "row", row);
1276 dict_add_number(dict, "col", scol);
1277 dict_add_number(dict, "curscol", ccol);
1278 dict_add_number(dict, "endcol", ecol);
1279}
1280#endif
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282/*
1283 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001286scrolldown(
1287 long line_count,
1288 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 long done = 0; /* total # of physical lines done */
1291 int wrow;
1292 int moved = FALSE;
1293
1294#ifdef FEAT_FOLDING
1295 linenr_T first;
1296
1297 /* Make sure w_topline is at the first of a sequence of folded lines. */
1298 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1299#endif
1300 validate_cursor(); /* w_wrow needs to be valid */
1301 while (line_count-- > 0)
1302 {
1303#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001304 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1305 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
1307 ++curwin->w_topfill;
1308 ++done;
1309 }
1310 else
1311#endif
1312 {
1313 if (curwin->w_topline == 1)
1314 break;
1315 --curwin->w_topline;
1316#ifdef FEAT_DIFF
1317 curwin->w_topfill = 0;
1318#endif
1319#ifdef FEAT_FOLDING
1320 /* A sequence of folded lines only counts for one logical line */
1321 if (hasFolding(curwin->w_topline, &first, NULL))
1322 {
1323 ++done;
1324 if (!byfold)
1325 line_count -= curwin->w_topline - first - 1;
1326 curwin->w_botline -= curwin->w_topline - first;
1327 curwin->w_topline = first;
1328 }
1329 else
1330#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001331 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 --curwin->w_botline; /* approximate w_botline */
1334 invalidate_botline();
1335 }
1336 curwin->w_wrow += done; /* keep w_wrow updated */
1337 curwin->w_cline_row += done; /* keep w_cline_row updated */
1338
1339#ifdef FEAT_DIFF
1340 if (curwin->w_cursor.lnum == curwin->w_topline)
1341 curwin->w_cline_row = 0;
1342 check_topfill(curwin, TRUE);
1343#endif
1344
1345 /*
1346 * Compute the row number of the last row of the cursor line
1347 * and move the cursor onto the displayed part of the window.
1348 */
1349 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001350 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 validate_virtcol();
1353 validate_cheight();
1354 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001355 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1358 {
1359#ifdef FEAT_FOLDING
1360 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1361 {
1362 --wrow;
1363 if (first == 1)
1364 curwin->w_cursor.lnum = 1;
1365 else
1366 curwin->w_cursor.lnum = first - 1;
1367 }
1368 else
1369#endif
1370 wrow -= plines(curwin->w_cursor.lnum--);
1371 curwin->w_valid &=
1372 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1373 moved = TRUE;
1374 }
1375 if (moved)
1376 {
1377#ifdef FEAT_FOLDING
1378 /* Move cursor to first line of closed fold. */
1379 foldAdjustCursor();
1380#endif
1381 coladvance(curwin->w_curswant);
1382 }
1383}
1384
1385/*
1386 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001389scrollup(
1390 long line_count,
1391 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1394 linenr_T lnum;
1395
1396 if (
1397# ifdef FEAT_FOLDING
1398 (byfold && hasAnyFolding(curwin))
1399# ifdef FEAT_DIFF
1400 ||
1401# endif
1402# endif
1403# ifdef FEAT_DIFF
1404 curwin->w_p_diff
1405# endif
1406 )
1407 {
1408 /* count each sequence of folded lines as one logical line */
1409 lnum = curwin->w_topline;
1410 while (line_count--)
1411 {
1412# ifdef FEAT_DIFF
1413 if (curwin->w_topfill > 0)
1414 --curwin->w_topfill;
1415 else
1416# endif
1417 {
1418# ifdef FEAT_FOLDING
1419 if (byfold)
1420 (void)hasFolding(lnum, NULL, &lnum);
1421# endif
1422 if (lnum >= curbuf->b_ml.ml_line_count)
1423 break;
1424 ++lnum;
1425# ifdef FEAT_DIFF
1426 curwin->w_topfill = diff_check_fill(curwin, lnum);
1427# endif
1428 }
1429 }
1430 /* approximate w_botline */
1431 curwin->w_botline += lnum - curwin->w_topline;
1432 curwin->w_topline = lnum;
1433 }
1434 else
1435#endif
1436 {
1437 curwin->w_topline += line_count;
1438 curwin->w_botline += line_count; /* approximate w_botline */
1439 }
1440
1441 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1442 curwin->w_topline = curbuf->b_ml.ml_line_count;
1443 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1444 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1445
1446#ifdef FEAT_DIFF
1447 check_topfill(curwin, FALSE);
1448#endif
1449
1450#ifdef FEAT_FOLDING
1451 if (hasAnyFolding(curwin))
1452 /* Make sure w_topline is at the first of a sequence of folded lines. */
1453 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1454#endif
1455
1456 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1457 if (curwin->w_cursor.lnum < curwin->w_topline)
1458 {
1459 curwin->w_cursor.lnum = curwin->w_topline;
1460 curwin->w_valid &=
1461 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1462 coladvance(curwin->w_curswant);
1463 }
1464}
1465
1466#ifdef FEAT_DIFF
1467/*
1468 * Don't end up with too many filler lines in the window.
1469 */
1470 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001471check_topfill(
1472 win_T *wp,
1473 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 int n;
1476
1477 if (wp->w_topfill > 0)
1478 {
1479 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1480 if (wp->w_topfill + n > wp->w_height)
1481 {
1482 if (down && wp->w_topline > 1)
1483 {
1484 --wp->w_topline;
1485 wp->w_topfill = 0;
1486 }
1487 else
1488 {
1489 wp->w_topfill = wp->w_height - n;
1490 if (wp->w_topfill < 0)
1491 wp->w_topfill = 0;
1492 }
1493 }
1494 }
1495}
1496
1497/*
1498 * Use as many filler lines as possible for w_topline. Make sure w_topline
1499 * is still visible.
1500 */
1501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001502max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 int n;
1505
1506 n = plines_nofill(curwin->w_topline);
1507 if (n >= curwin->w_height)
1508 curwin->w_topfill = 0;
1509 else
1510 {
1511 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1512 if (curwin->w_topfill + n > curwin->w_height)
1513 curwin->w_topfill = curwin->w_height - n;
1514 }
1515}
1516#endif
1517
1518#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1519/*
1520 * Scroll the screen one line down, but don't do it if it would move the
1521 * cursor off the screen.
1522 */
1523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001524scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int end_row;
1527#ifdef FEAT_DIFF
1528 int can_fill = (curwin->w_topfill
1529 < diff_check_fill(curwin, curwin->w_topline));
1530#endif
1531
1532 if (curwin->w_topline <= 1
1533#ifdef FEAT_DIFF
1534 && !can_fill
1535#endif
1536 )
1537 return;
1538
1539 validate_cursor(); /* w_wrow needs to be valid */
1540
1541 /*
1542 * Compute the row number of the last row of the cursor line
1543 * and make sure it doesn't go off the screen. Make sure the cursor
1544 * doesn't go past 'scrolloff' lines from the screen end.
1545 */
1546 end_row = curwin->w_wrow;
1547#ifdef FEAT_DIFF
1548 if (can_fill)
1549 ++end_row;
1550 else
1551 end_row += plines_nofill(curwin->w_topline - 1);
1552#else
1553 end_row += plines(curwin->w_topline - 1);
1554#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001555 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 validate_cheight();
1558 validate_virtcol();
1559 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001560 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001562 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564#ifdef FEAT_DIFF
1565 if (can_fill)
1566 {
1567 ++curwin->w_topfill;
1568 check_topfill(curwin, TRUE);
1569 }
1570 else
1571 {
1572 --curwin->w_topline;
1573 curwin->w_topfill = 0;
1574 }
1575#else
1576 --curwin->w_topline;
1577#endif
1578#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001579 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#endif
1581 --curwin->w_botline; /* approximate w_botline */
1582 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1583 }
1584}
1585
1586/*
1587 * Scroll the screen one line up, but don't do it if it would move the cursor
1588 * off the screen.
1589 */
1590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001591scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 int start_row;
1594
1595 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1596#ifdef FEAT_DIFF
1597 && curwin->w_topfill == 0
1598#endif
1599 )
1600 return;
1601
1602 validate_cursor(); /* w_wrow needs to be valid */
1603
1604 /*
1605 * Compute the row number of the first row of the cursor line
1606 * and make sure it doesn't go off the screen. Make sure the cursor
1607 * doesn't go before 'scrolloff' lines from the screen start.
1608 */
1609#ifdef FEAT_DIFF
1610 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1611 - curwin->w_topfill;
1612#else
1613 start_row = curwin->w_wrow - plines(curwin->w_topline);
1614#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001615 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001618 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001620 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622#ifdef FEAT_DIFF
1623 if (curwin->w_topfill > 0)
1624 --curwin->w_topfill;
1625 else
1626#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001627 {
1628#ifdef FEAT_FOLDING
1629 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 ++curwin->w_botline; /* approximate w_botline */
1634 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1635 }
1636}
1637#endif /* FEAT_INS_EXPAND */
1638
1639/*
1640 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1641 * a (wrapped) text line. Uses and sets "lp->fill".
1642 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001643 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
1645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001646topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648#ifdef FEAT_DIFF
1649 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1650 {
1651 /* Add a filler line. */
1652 ++lp->fill;
1653 lp->height = 1;
1654 }
1655 else
1656#endif
1657 {
1658 --lp->lnum;
1659#ifdef FEAT_DIFF
1660 lp->fill = 0;
1661#endif
1662 if (lp->lnum < 1)
1663 lp->height = MAXCOL;
1664 else
1665#ifdef FEAT_FOLDING
1666 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1667 /* Add a closed fold */
1668 lp->height = 1;
1669 else
1670#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001671 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673}
1674
1675/*
1676 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1677 * a (wrapped) text line. Uses and sets "lp->fill".
1678 * Returns the height of the added line in "lp->height".
1679 * Lines below the last one are incredibly high.
1680 */
1681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684#ifdef FEAT_DIFF
1685 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1686 {
1687 /* Add a filler line. */
1688 ++lp->fill;
1689 lp->height = 1;
1690 }
1691 else
1692#endif
1693 {
1694 ++lp->lnum;
1695#ifdef FEAT_DIFF
1696 lp->fill = 0;
1697#endif
1698 if (lp->lnum > curbuf->b_ml.ml_line_count)
1699 lp->height = MAXCOL;
1700 else
1701#ifdef FEAT_FOLDING
1702 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1703 /* Add a closed fold */
1704 lp->height = 1;
1705 else
1706#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001707 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709}
1710
1711#ifdef FEAT_DIFF
1712/*
1713 * Switch from including filler lines below lp->lnum to including filler
1714 * lines above loff.lnum + 1. This keeps pointing to the same line.
1715 * When there are no filler lines nothing changes.
1716 */
1717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001718botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 if (lp->fill > 0)
1721 {
1722 ++lp->lnum;
1723 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1724 }
1725}
1726
1727/*
1728 * Switch from including filler lines above lp->lnum to including filler
1729 * lines below loff.lnum - 1. This keeps pointing to the same line.
1730 * When there are no filler lines nothing changes.
1731 */
1732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001733topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 if (lp->fill > 0)
1736 {
1737 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1738 --lp->lnum;
1739 }
1740}
1741#endif
1742
1743/*
1744 * Recompute topline to put the cursor at the top of the window.
1745 * Scroll at least "min_scroll" lines.
1746 * If "always" is TRUE, always set topline (for "zt").
1747 */
1748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 int scrolled = 0;
1752 int extra = 0;
1753 int used;
1754 int i;
1755 linenr_T top; /* just above displayed lines */
1756 linenr_T bot; /* just below displayed lines */
1757 linenr_T old_topline = curwin->w_topline;
1758#ifdef FEAT_DIFF
1759 linenr_T old_topfill = curwin->w_topfill;
1760#endif
1761 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001762 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#ifdef FEAT_MOUSE
1765 if (mouse_dragging > 0)
1766 off = mouse_dragging - 1;
1767#endif
1768
1769 /*
1770 * Decrease topline until:
1771 * - it has become 1
1772 * - (part of) the cursor line is moved off the screen or
1773 * - moved at least 'scrolljump' lines and
1774 * - at least 'scrolloff' lines above and below the cursor
1775 */
1776 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001777 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (curwin->w_cursor.lnum < curwin->w_topline)
1779 scrolled = used;
1780
1781#ifdef FEAT_FOLDING
1782 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1783 {
1784 --top;
1785 ++bot;
1786 }
1787 else
1788#endif
1789 {
1790 top = curwin->w_cursor.lnum - 1;
1791 bot = curwin->w_cursor.lnum + 1;
1792 }
1793 new_topline = top + 1;
1794
1795#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001796 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001797 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001798 * Hide filler lines above cursor line by adding them to "extra". */
1799 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
1801
1802 /*
1803 * Check if the lines from "top" to "bot" fit in the window. If they do,
1804 * set new_topline and advance "top" and "bot" to include more lines.
1805 */
1806 while (top > 0)
1807 {
1808#ifdef FEAT_FOLDING
1809 if (hasFolding(top, &top, NULL))
1810 /* count one logical line for a sequence of folded lines */
1811 i = 1;
1812 else
1813#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001814 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 used += i;
1816 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1817 {
1818#ifdef FEAT_FOLDING
1819 if (hasFolding(bot, NULL, &bot))
1820 /* count one logical line for a sequence of folded lines */
1821 ++used;
1822 else
1823#endif
1824 used += plines(bot);
1825 }
1826 if (used > curwin->w_height)
1827 break;
1828 if (top < curwin->w_topline)
1829 scrolled += i;
1830
1831 /*
1832 * If scrolling is needed, scroll at least 'sj' lines.
1833 */
1834 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1835 && extra >= off)
1836 break;
1837
1838 extra += i;
1839 new_topline = top;
1840 --top;
1841 ++bot;
1842 }
1843
1844 /*
1845 * If we don't have enough space, put cursor in the middle.
1846 * This makes sure we get the same position when using "k" and "j"
1847 * in a small window.
1848 */
1849 if (used > curwin->w_height)
1850 scroll_cursor_halfway(FALSE);
1851 else
1852 {
1853 /*
1854 * If "always" is FALSE, only adjust topline to a lower value, higher
1855 * value may happen with wrapping lines
1856 */
1857 if (new_topline < curwin->w_topline || always)
1858 curwin->w_topline = new_topline;
1859 if (curwin->w_topline > curwin->w_cursor.lnum)
1860 curwin->w_topline = curwin->w_cursor.lnum;
1861#ifdef FEAT_DIFF
1862 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1863 if (curwin->w_topfill > 0 && extra > off)
1864 {
1865 curwin->w_topfill -= extra - off;
1866 if (curwin->w_topfill < 0)
1867 curwin->w_topfill = 0;
1868 }
1869 check_topfill(curwin, FALSE);
1870#endif
1871 if (curwin->w_topline != old_topline
1872#ifdef FEAT_DIFF
1873 || curwin->w_topfill != old_topfill
1874#endif
1875 )
1876 curwin->w_valid &=
1877 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1878 curwin->w_valid |= VALID_TOPLINE;
1879 }
1880}
1881
1882/*
1883 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1884 * screen lines for text lines.
1885 */
1886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001887set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889#ifdef FEAT_DIFF
1890 wp->w_filler_rows = 0;
1891#endif
1892 if (used == 0)
1893 wp->w_empty_rows = 0; /* single line that doesn't fit */
1894 else
1895 {
1896 wp->w_empty_rows = wp->w_height - used;
1897#ifdef FEAT_DIFF
1898 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1899 {
1900 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1901 if (wp->w_empty_rows > wp->w_filler_rows)
1902 wp->w_empty_rows -= wp->w_filler_rows;
1903 else
1904 {
1905 wp->w_filler_rows = wp->w_empty_rows;
1906 wp->w_empty_rows = 0;
1907 }
1908 }
1909#endif
1910 }
1911}
1912
1913/*
1914 * Recompute topline to put the cursor at the bottom of the window.
1915 * Scroll at least "min_scroll" lines.
1916 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1917 * This is messy stuff!!!
1918 */
1919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001920scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 int used;
1923 int scrolled = 0;
1924 int extra = 0;
1925 int i;
1926 linenr_T line_count;
1927 linenr_T old_topline = curwin->w_topline;
1928 lineoff_T loff;
1929 lineoff_T boff;
1930#ifdef FEAT_DIFF
1931 int old_topfill = curwin->w_topfill;
1932 int fill_below_window;
1933#endif
1934 linenr_T old_botline = curwin->w_botline;
1935 linenr_T old_valid = curwin->w_valid;
1936 int old_empty_rows = curwin->w_empty_rows;
1937 linenr_T cln; /* Cursor Line Number */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001938 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 cln = curwin->w_cursor.lnum;
1941 if (set_topbot)
1942 {
1943 used = 0;
1944 curwin->w_botline = cln + 1;
1945#ifdef FEAT_DIFF
1946 loff.fill = 0;
1947#endif
1948 for (curwin->w_topline = curwin->w_botline;
1949 curwin->w_topline > 1;
1950 curwin->w_topline = loff.lnum)
1951 {
1952 loff.lnum = curwin->w_topline;
1953 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001954 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 break;
1956 used += loff.height;
1957#ifdef FEAT_DIFF
1958 curwin->w_topfill = loff.fill;
1959#endif
1960 }
1961 set_empty_rows(curwin, used);
1962 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1963 if (curwin->w_topline != old_topline
1964#ifdef FEAT_DIFF
1965 || curwin->w_topfill != old_topfill
1966#endif
1967 )
1968 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1969 }
1970 else
1971 validate_botline();
1972
1973 /* The lines of the cursor line itself are always used. */
1974#ifdef FEAT_DIFF
1975 used = plines_nofill(cln);
1976#else
1977 validate_cheight();
1978 used = curwin->w_cline_height;
1979#endif
1980
1981 /* If the cursor is below botline, we will at least scroll by the height
1982 * of the cursor line. Correct for empty lines, which are really part of
1983 * botline. */
1984 if (cln >= curwin->w_botline)
1985 {
1986 scrolled = used;
1987 if (cln == curwin->w_botline)
1988 scrolled -= curwin->w_empty_rows;
1989 }
1990
1991 /*
1992 * Stop counting lines to scroll when
1993 * - hitting start of the file
1994 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001995 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 * - lines between botline and cursor have been counted
1997 */
1998#ifdef FEAT_FOLDING
1999 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2000#endif
2001 {
2002 loff.lnum = cln;
2003 boff.lnum = cln;
2004 }
2005#ifdef FEAT_DIFF
2006 loff.fill = 0;
2007 boff.fill = 0;
2008 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2009 - curwin->w_filler_rows;
2010#endif
2011
2012 while (loff.lnum > 1)
2013 {
2014 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
2015 * context for 'scrolloff' and counted all lines below the window. */
2016 if ((((scrolled <= 0 || scrolled >= min_scroll)
2017 && extra >= (
2018#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002019 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002021 so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2023 && loff.lnum <= curwin->w_botline
2024#ifdef FEAT_DIFF
2025 && (loff.lnum < curwin->w_botline
2026 || loff.fill >= fill_below_window)
2027#endif
2028 )
2029 break;
2030
2031 /* Add one line above */
2032 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002033 if (loff.height == MAXCOL)
2034 used = MAXCOL;
2035 else
2036 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (used > curwin->w_height)
2038 break;
2039 if (loff.lnum >= curwin->w_botline
2040#ifdef FEAT_DIFF
2041 && (loff.lnum > curwin->w_botline
2042 || loff.fill <= fill_below_window)
2043#endif
2044 )
2045 {
2046 /* Count screen lines that are below the window. */
2047 scrolled += loff.height;
2048 if (loff.lnum == curwin->w_botline
2049#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002050 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#endif
2052 )
2053 scrolled -= curwin->w_empty_rows;
2054 }
2055
2056 if (boff.lnum < curbuf->b_ml.ml_line_count)
2057 {
2058 /* Add one line below */
2059 botline_forw(&boff);
2060 used += boff.height;
2061 if (used > curwin->w_height)
2062 break;
2063 if (extra < (
2064#ifdef FEAT_MOUSE
2065 mouse_dragging > 0 ? mouse_dragging - 1 :
2066#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002067 so) || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
2069 extra += boff.height;
2070 if (boff.lnum >= curwin->w_botline
2071#ifdef FEAT_DIFF
2072 || (boff.lnum + 1 == curwin->w_botline
2073 && boff.fill > curwin->w_filler_rows)
2074#endif
2075 )
2076 {
2077 /* Count screen lines that are below the window. */
2078 scrolled += boff.height;
2079 if (boff.lnum == curwin->w_botline
2080#ifdef FEAT_DIFF
2081 && boff.fill == 0
2082#endif
2083 )
2084 scrolled -= curwin->w_empty_rows;
2085 }
2086 }
2087 }
2088 }
2089
2090 /* curwin->w_empty_rows is larger, no need to scroll */
2091 if (scrolled <= 0)
2092 line_count = 0;
2093 /* more than a screenfull, don't scroll but redraw */
2094 else if (used > curwin->w_height)
2095 line_count = used;
2096 /* scroll minimal number of lines */
2097 else
2098 {
2099 line_count = 0;
2100#ifdef FEAT_DIFF
2101 boff.fill = curwin->w_topfill;
2102#endif
2103 boff.lnum = curwin->w_topline - 1;
2104 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2105 {
2106 botline_forw(&boff);
2107 i += boff.height;
2108 ++line_count;
2109 }
2110 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2111 line_count = 9999;
2112 }
2113
2114 /*
2115 * Scroll up if the cursor is off the bottom of the screen a bit.
2116 * Otherwise put it at 1/2 of the screen.
2117 */
2118 if (line_count >= curwin->w_height && line_count > min_scroll)
2119 scroll_cursor_halfway(FALSE);
2120 else
2121 scrollup(line_count, TRUE);
2122
2123 /*
2124 * If topline didn't change we need to restore w_botline and w_empty_rows
2125 * (we changed them).
2126 * If topline did change, update_screen() will set botline.
2127 */
2128 if (curwin->w_topline == old_topline && set_topbot)
2129 {
2130 curwin->w_botline = old_botline;
2131 curwin->w_empty_rows = old_empty_rows;
2132 curwin->w_valid = old_valid;
2133 }
2134 curwin->w_valid |= VALID_TOPLINE;
2135}
2136
2137/*
2138 * Recompute topline to put the cursor halfway the window
2139 * If "atend" is TRUE, also put it halfway at the end of the file.
2140 */
2141 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 int above = 0;
2145 linenr_T topline;
2146#ifdef FEAT_DIFF
2147 int topfill = 0;
2148#endif
2149 int below = 0;
2150 int used;
2151 lineoff_T loff;
2152 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002153#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002154 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2158#ifdef FEAT_FOLDING
2159 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2160#endif
2161#ifdef FEAT_DIFF
2162 used = plines_nofill(loff.lnum);
2163 loff.fill = 0;
2164 boff.fill = 0;
2165#else
2166 used = plines(loff.lnum);
2167#endif
2168 topline = loff.lnum;
2169 while (topline > 1)
2170 {
2171 if (below <= above) /* add a line below the cursor first */
2172 {
2173 if (boff.lnum < curbuf->b_ml.ml_line_count)
2174 {
2175 botline_forw(&boff);
2176 used += boff.height;
2177 if (used > curwin->w_height)
2178 break;
2179 below += boff.height;
2180 }
2181 else
2182 {
2183 ++below; /* count a "~" line */
2184 if (atend)
2185 ++used;
2186 }
2187 }
2188
2189 if (below > above) /* add a line above the cursor */
2190 {
2191 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002192 if (loff.height == MAXCOL)
2193 used = MAXCOL;
2194 else
2195 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (used > curwin->w_height)
2197 break;
2198 above += loff.height;
2199 topline = loff.lnum;
2200#ifdef FEAT_DIFF
2201 topfill = loff.fill;
2202#endif
2203 }
2204 }
2205#ifdef FEAT_FOLDING
2206 if (!hasFolding(topline, &curwin->w_topline, NULL))
2207#endif
2208 curwin->w_topline = topline;
2209#ifdef FEAT_DIFF
2210 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002211 if (old_topline > curwin->w_topline + curwin->w_height)
2212 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 check_topfill(curwin, FALSE);
2214#endif
2215 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2216 curwin->w_valid |= VALID_TOPLINE;
2217}
2218
2219/*
2220 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002221 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 * If not possible, put it at the same position as scroll_cursor_halfway().
2223 * When called topline must be valid!
2224 */
2225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002226cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 int above = 0; /* screen lines above topline */
2229 linenr_T topline;
2230 int below = 0; /* screen lines below botline */
2231 linenr_T botline;
2232 int above_wanted, below_wanted;
2233 linenr_T cln; /* Cursor Line Number */
2234 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002235 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 /*
2238 * How many lines we would like to have above/below the cursor depends on
2239 * whether the first/last line of the file is on screen.
2240 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002241 above_wanted = so;
2242 below_wanted = so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002244 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 above_wanted = mouse_dragging - 1;
2247 below_wanted = mouse_dragging - 1;
2248 }
2249#endif
2250 if (curwin->w_topline == 1)
2251 {
2252 above_wanted = 0;
2253 max_off = curwin->w_height / 2;
2254 if (below_wanted > max_off)
2255 below_wanted = max_off;
2256 }
2257 validate_botline();
2258 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2259#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002260 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262 )
2263 {
2264 below_wanted = 0;
2265 max_off = (curwin->w_height - 1) / 2;
2266 if (above_wanted > max_off)
2267 above_wanted = max_off;
2268 }
2269
2270 /*
2271 * If there are sufficient file-lines above and below the cursor, we can
2272 * return now.
2273 */
2274 cln = curwin->w_cursor.lnum;
2275 if (cln >= curwin->w_topline + above_wanted
2276 && cln < curwin->w_botline - below_wanted
2277#ifdef FEAT_FOLDING
2278 && !hasAnyFolding(curwin)
2279#endif
2280 )
2281 return;
2282
2283 /*
2284 * Narrow down the area where the cursor can be put by taking lines from
2285 * the top and the bottom until:
2286 * - the desired context lines are found
2287 * - the lines from the top is past the lines from the bottom
2288 */
2289 topline = curwin->w_topline;
2290 botline = curwin->w_botline - 1;
2291#ifdef FEAT_DIFF
2292 /* count filler lines as context */
2293 above = curwin->w_topfill;
2294 below = curwin->w_filler_rows;
2295#endif
2296 while ((above < above_wanted || below < below_wanted) && topline < botline)
2297 {
2298 if (below < below_wanted && (below <= above || above >= above_wanted))
2299 {
2300#ifdef FEAT_FOLDING
2301 if (hasFolding(botline, &botline, NULL))
2302 ++below;
2303 else
2304#endif
2305 below += plines(botline);
2306 --botline;
2307 }
2308 if (above < above_wanted && (above < below || below >= below_wanted))
2309 {
2310#ifdef FEAT_FOLDING
2311 if (hasFolding(topline, NULL, &topline))
2312 ++above;
2313 else
2314#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002315 above += PLINES_NOFILL(topline);
2316#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 /* Count filler lines below this line as context. */
2318 if (topline < botline)
2319 above += diff_check_fill(curwin, topline + 1);
2320#endif
2321 ++topline;
2322 }
2323 }
2324 if (topline == botline || botline == 0)
2325 curwin->w_cursor.lnum = topline;
2326 else if (topline > botline)
2327 curwin->w_cursor.lnum = botline;
2328 else
2329 {
2330 if (cln < topline && curwin->w_topline > 1)
2331 {
2332 curwin->w_cursor.lnum = topline;
2333 curwin->w_valid &=
2334 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2335 }
2336 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2337 {
2338 curwin->w_cursor.lnum = botline;
2339 curwin->w_valid &=
2340 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2341 }
2342 }
2343 curwin->w_valid |= VALID_TOPLINE;
2344}
2345
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002346static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348/*
2349 * move screen 'count' pages up or down and update screen
2350 *
2351 * return FAIL for failure, OK otherwise
2352 */
2353 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 long n;
2357 int retval = OK;
2358 lineoff_T loff;
2359 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002360 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2363 {
2364 beep_flush();
2365 return FAIL;
2366 }
2367
2368 for ( ; count > 0; --count)
2369 {
2370 validate_botline();
2371 /*
2372 * It's an error to move a page up when the first line is already on
2373 * the screen. It's an error to move a page down when the last line
2374 * is on the screen and the topline is 'scrolloff' lines from the
2375 * last line.
2376 */
2377 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002378 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2380 : (curwin->w_topline == 1
2381#ifdef FEAT_DIFF
2382 && curwin->w_topfill ==
2383 diff_check_fill(curwin, curwin->w_topline)
2384#endif
2385 ))
2386 {
2387 beep_flush();
2388 retval = FAIL;
2389 break;
2390 }
2391
2392#ifdef FEAT_DIFF
2393 loff.fill = 0;
2394#endif
2395 if (dir == FORWARD)
2396 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002397 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002399 /* Vi compatible scrolling */
2400 if (p_window <= 2)
2401 ++curwin->w_topline;
2402 else
2403 curwin->w_topline += p_window - 2;
2404 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2405 curwin->w_topline = curbuf->b_ml.ml_line_count;
2406 curwin->w_cursor.lnum = curwin->w_topline;
2407 }
2408 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2409 {
2410 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 curwin->w_topline = curbuf->b_ml.ml_line_count;
2412#ifdef FEAT_DIFF
2413 curwin->w_topfill = 0;
2414#endif
2415 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2416 }
2417 else
2418 {
2419 /* For the overlap, start with the line just below the window
2420 * and go upwards. */
2421 loff.lnum = curwin->w_botline;
2422#ifdef FEAT_DIFF
2423 loff.fill = diff_check_fill(curwin, loff.lnum)
2424 - curwin->w_filler_rows;
2425#endif
2426 get_scroll_overlap(&loff, -1);
2427 curwin->w_topline = loff.lnum;
2428#ifdef FEAT_DIFF
2429 curwin->w_topfill = loff.fill;
2430 check_topfill(curwin, FALSE);
2431#endif
2432 curwin->w_cursor.lnum = curwin->w_topline;
2433 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2434 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2435 }
2436 }
2437 else /* dir == BACKWARDS */
2438 {
2439#ifdef FEAT_DIFF
2440 if (curwin->w_topline == 1)
2441 {
2442 /* Include max number of filler lines */
2443 max_topfill();
2444 continue;
2445 }
2446#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002447 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002448 {
2449 /* Vi compatible scrolling (sort of) */
2450 if (p_window <= 2)
2451 --curwin->w_topline;
2452 else
2453 curwin->w_topline -= p_window - 2;
2454 if (curwin->w_topline < 1)
2455 curwin->w_topline = 1;
2456 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2457 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2458 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2459 continue;
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /* Find the line at the top of the window that is going to be the
2463 * line at the bottom of the window. Make sure this results in
2464 * the same line as before doing CTRL-F. */
2465 loff.lnum = curwin->w_topline - 1;
2466#ifdef FEAT_DIFF
2467 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2468 - curwin->w_topfill;
2469#endif
2470 get_scroll_overlap(&loff, 1);
2471
2472 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2473 {
2474 loff.lnum = curbuf->b_ml.ml_line_count;
2475#ifdef FEAT_DIFF
2476 loff.fill = 0;
2477 }
2478 else
2479 {
2480 botline_topline(&loff);
2481#endif
2482 }
2483 curwin->w_cursor.lnum = loff.lnum;
2484
2485 /* Find the line just above the new topline to get the right line
2486 * at the bottom of the window. */
2487 n = 0;
2488 while (n <= curwin->w_height && loff.lnum >= 1)
2489 {
2490 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002491 if (loff.height == MAXCOL)
2492 n = MAXCOL;
2493 else
2494 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002496 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 curwin->w_topline = 1;
2499#ifdef FEAT_DIFF
2500 max_topfill();
2501#endif
2502 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2503 }
2504 else
2505 {
2506 /* Go two lines forward again. */
2507#ifdef FEAT_DIFF
2508 topline_botline(&loff);
2509#endif
2510 botline_forw(&loff);
2511 botline_forw(&loff);
2512#ifdef FEAT_DIFF
2513 botline_topline(&loff);
2514#endif
2515#ifdef FEAT_FOLDING
2516 /* We're at the wrong end of a fold now. */
2517 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2518#endif
2519
2520 /* Always scroll at least one line. Avoid getting stuck on
2521 * very long lines. */
2522 if (loff.lnum >= curwin->w_topline
2523#ifdef FEAT_DIFF
2524 && (loff.lnum > curwin->w_topline
2525 || loff.fill >= curwin->w_topfill)
2526#endif
2527 )
2528 {
2529#ifdef FEAT_DIFF
2530 /* First try using the maximum number of filler lines. If
2531 * that's not enough, backup one line. */
2532 loff.fill = curwin->w_topfill;
2533 if (curwin->w_topfill < diff_check_fill(curwin,
2534 curwin->w_topline))
2535 max_topfill();
2536 if (curwin->w_topfill == loff.fill)
2537#endif
2538 {
2539 --curwin->w_topline;
2540#ifdef FEAT_DIFF
2541 curwin->w_topfill = 0;
2542#endif
2543 }
2544 comp_botline(curwin);
2545 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002546 curwin->w_valid &=
2547 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 {
2551 curwin->w_topline = loff.lnum;
2552#ifdef FEAT_DIFF
2553 curwin->w_topfill = loff.fill;
2554 check_topfill(curwin, FALSE);
2555#endif
2556 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2557 }
2558 }
2559 }
2560 }
2561#ifdef FEAT_FOLDING
2562 foldAdjustCursor();
2563#endif
2564 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002565 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002566 if (retval == OK)
2567 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2569
Bram Moolenaar907dad72018-07-10 15:07:15 +02002570 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002572 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2573 // But make sure we scroll at least one line (happens with mix of long
2574 // wrapping lines and non-wrapping line).
2575 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002577 scroll_cursor_top(1, FALSE);
2578 if (curwin->w_topline <= old_topline
2579 && old_topline < curbuf->b_ml.ml_line_count)
2580 {
2581 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002583 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2584#endif
2585 }
2586 }
2587#ifdef FEAT_FOLDING
2588 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593 redraw_later(VALID);
2594 return retval;
2595}
2596
2597/*
2598 * Decide how much overlap to use for page-up or page-down scrolling.
2599 * This is symmetric, so that doing both keeps the same lines displayed.
2600 * Three lines are examined:
2601 *
2602 * before CTRL-F after CTRL-F / before CTRL-B
2603 * etc. l1
2604 * l1 last but one line ------------
2605 * l2 last text line l2 top text line
2606 * ------------- l3 second text line
2607 * l3 etc.
2608 */
2609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002610get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 int h1, h2, h3, h4;
2613 int min_height = curwin->w_height - 2;
2614 lineoff_T loff0, loff1, loff2;
2615
2616#ifdef FEAT_DIFF
2617 if (lp->fill > 0)
2618 lp->height = 1;
2619 else
2620 lp->height = plines_nofill(lp->lnum);
2621#else
2622 lp->height = plines(lp->lnum);
2623#endif
2624 h1 = lp->height;
2625 if (h1 > min_height)
2626 return; /* no overlap */
2627
2628 loff0 = *lp;
2629 if (dir > 0)
2630 botline_forw(lp);
2631 else
2632 topline_back(lp);
2633 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002634 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 *lp = loff0; /* no overlap */
2637 return;
2638 }
2639
2640 loff1 = *lp;
2641 if (dir > 0)
2642 botline_forw(lp);
2643 else
2644 topline_back(lp);
2645 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002646 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 *lp = loff0; /* no overlap */
2649 return;
2650 }
2651
2652 loff2 = *lp;
2653 if (dir > 0)
2654 botline_forw(lp);
2655 else
2656 topline_back(lp);
2657 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002658 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 *lp = loff1; /* 1 line overlap */
2660 else
2661 *lp = loff2; /* 2 lines overlap */
2662 return;
2663}
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * Scroll 'scroll' lines up or down.
2667 */
2668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002669halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 long scrolled = 0;
2672 int i;
2673 int n;
2674 int room;
2675
2676 if (Prenum)
2677 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2678 curwin->w_height : Prenum;
2679 n = (curwin->w_p_scr <= curwin->w_height) ?
2680 curwin->w_p_scr : curwin->w_height;
2681
Bram Moolenaard5d37532017-03-27 23:02:07 +02002682 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 validate_botline();
2684 room = curwin->w_empty_rows;
2685#ifdef FEAT_DIFF
2686 room += curwin->w_filler_rows;
2687#endif
2688 if (flag)
2689 {
2690 /*
2691 * scroll the text up
2692 */
2693 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2694 {
2695#ifdef FEAT_DIFF
2696 if (curwin->w_topfill > 0)
2697 {
2698 i = 1;
2699 if (--n < 0 && scrolled > 0)
2700 break;
2701 --curwin->w_topfill;
2702 }
2703 else
2704#endif
2705 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002706 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 n -= i;
2708 if (n < 0 && scrolled > 0)
2709 break;
2710#ifdef FEAT_FOLDING
2711 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2712#endif
2713 ++curwin->w_topline;
2714#ifdef FEAT_DIFF
2715 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2716#endif
2717
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2719 {
2720 ++curwin->w_cursor.lnum;
2721 curwin->w_valid &=
2722 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2726 scrolled += i;
2727
2728 /*
2729 * Correct w_botline for changed w_topline.
2730 * Won't work when there are filler lines.
2731 */
2732#ifdef FEAT_DIFF
2733 if (curwin->w_p_diff)
2734 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2735 else
2736#endif
2737 {
2738 room += i;
2739 do
2740 {
2741 i = plines(curwin->w_botline);
2742 if (i > room)
2743 break;
2744#ifdef FEAT_FOLDING
2745 (void)hasFolding(curwin->w_botline, NULL,
2746 &curwin->w_botline);
2747#endif
2748 ++curwin->w_botline;
2749 room -= i;
2750 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2751 }
2752 }
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 /*
2755 * When hit bottom of the file: move cursor down.
2756 */
2757 if (n > 0)
2758 {
2759# ifdef FEAT_FOLDING
2760 if (hasAnyFolding(curwin))
2761 {
2762 while (--n >= 0
2763 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2764 {
2765 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2766 &curwin->w_cursor.lnum);
2767 ++curwin->w_cursor.lnum;
2768 }
2769 }
2770 else
2771# endif
2772 curwin->w_cursor.lnum += n;
2773 check_cursor_lnum();
2774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776 else
2777 {
2778 /*
2779 * scroll the text down
2780 */
2781 while (n > 0 && curwin->w_topline > 1)
2782 {
2783#ifdef FEAT_DIFF
2784 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2785 {
2786 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002787 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 ++curwin->w_topfill;
2789 }
2790 else
2791#endif
2792 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002793 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 n -= i;
2795 if (n < 0 && scrolled > 0)
2796 break;
2797 --curwin->w_topline;
2798#ifdef FEAT_FOLDING
2799 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2800#endif
2801#ifdef FEAT_DIFF
2802 curwin->w_topfill = 0;
2803#endif
2804 }
2805 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2806 VALID_BOTLINE|VALID_BOTLINE_AP);
2807 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (curwin->w_cursor.lnum > 1)
2809 {
2810 --curwin->w_cursor.lnum;
2811 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /*
2816 * When hit top of the file: move cursor up.
2817 */
2818 if (n > 0)
2819 {
2820 if (curwin->w_cursor.lnum <= (linenr_T)n)
2821 curwin->w_cursor.lnum = 1;
2822 else
2823# ifdef FEAT_FOLDING
2824 if (hasAnyFolding(curwin))
2825 {
2826 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2827 {
2828 --curwin->w_cursor.lnum;
2829 (void)hasFolding(curwin->w_cursor.lnum,
2830 &curwin->w_cursor.lnum, NULL);
2831 }
2832 }
2833 else
2834# endif
2835 curwin->w_cursor.lnum -= n;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838# ifdef FEAT_FOLDING
2839 /* Move cursor to first line of closed fold. */
2840 foldAdjustCursor();
2841# endif
2842#ifdef FEAT_DIFF
2843 check_topfill(curwin, !flag);
2844#endif
2845 cursor_correct();
2846 beginline(BL_SOL | BL_FIX);
2847 redraw_later(VALID);
2848}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849
Bram Moolenaar860cae12010-06-05 23:22:07 +02002850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002852{
2853 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002854 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002855 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002856 colnr_T curswant = curwin->w_curswant;
2857 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002858 win_T *old_curwin = curwin;
2859 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002860 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002861 int old_VIsual_select = VIsual_select;
2862 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002863
2864 /*
2865 * loop through the cursorbound windows
2866 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002867 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002868 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869 {
2870 curbuf = curwin->w_buffer;
2871 /* skip original window and windows with 'noscrollbind' */
2872 if (curwin != old_curwin && curwin->w_p_crb)
2873 {
2874# ifdef FEAT_DIFF
2875 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002876 curwin->w_cursor.lnum =
2877 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878 else
2879# endif
2880 curwin->w_cursor.lnum = line;
2881 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002882 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002883 curwin->w_curswant = curswant;
2884 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002885
Bram Moolenaar61452852011-02-01 18:01:11 +01002886 /* Make sure the cursor is in a valid position. Temporarily set
2887 * "restart_edit" to allow the cursor to be beyond the EOL. */
2888 restart_edit_save = restart_edit;
2889 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002890 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002891# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002892 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002893 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002894# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002895 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002896 /* Correct cursor for multi-byte character. */
2897 if (has_mbyte)
2898 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002899 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002900
2901 /* Only scroll when 'scrollbind' hasn't done this. */
2902 if (!curwin->w_p_scb)
2903 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002904 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905 }
2906 }
2907
2908 /*
2909 * reset current-window
2910 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002911 VIsual_select = old_VIsual_select;
2912 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002913 curwin = old_curwin;
2914 curbuf = old_curbuf;
2915}