blob: b3475a8022e507b4f03f7e68bf07761aeb9c0c66 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
28 linenr_T lnum; /* line number */
29#ifdef FEAT_DIFF
30 int fill; /* filler lines */
31#endif
32 int height; /* height of added line */
33} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
111 /* wp->w_botline is the line that is just below the window */
112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200139 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200140 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200141 if (wp->w_p_rnu)
142 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200144#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_cul)
146 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200147 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200149 // "w_last_cursorline" may be outdated, worst case we redraw
150 // too much. This is optimized for moving the cursor around in
151 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100152 redrawWinline(wp, wp->w_last_cursorline);
153 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200154 }
155 else
156 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200158#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200159 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160}
161
162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Update curwin->w_topline and redraw if necessary.
164 * Used to update the screen before printing a message.
165 */
166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100167update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 update_topline();
170 if (must_redraw)
171 update_screen(0);
172}
173
174/*
175 * Update curwin->w_topline to move the cursor onto the screen.
176 */
177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100178update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 long line_count;
181 int halfheight;
182 int n;
183 linenr_T old_topline;
184#ifdef FEAT_DIFF
185 int old_topfill;
186#endif
187#ifdef FEAT_FOLDING
188 linenr_T lnum;
189#endif
190 int check_topline = FALSE;
191 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100192 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifdef FEAT_MOUSE
Bram Moolenaar375e3392019-01-31 18:26:10 +0100194 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196
Bram Moolenaard5d37532017-03-27 23:02:07 +0200197 /* If there is no valid screen and when the window height is zero just use
198 * the cursor line. */
199 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 {
201 curwin->w_topline = curwin->w_cursor.lnum;
202 curwin->w_botline = curwin->w_topline;
203 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200204 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200205 return;
206 }
207
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 check_cursor_moved(curwin);
209 if (curwin->w_valid & VALID_TOPLINE)
210 return;
211
212#ifdef FEAT_MOUSE
213 /* When dragging with the mouse, don't scroll that quickly */
Bram Moolenaar9964e462007-05-05 17:54:07 +0000214 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100215 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#endif
217
218 old_topline = curwin->w_topline;
219#ifdef FEAT_DIFF
220 old_topfill = curwin->w_topfill;
221#endif
222
223 /*
224 * If the buffer is empty, always set topline to 1.
225 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100226 if (BUFEMPTY()) /* special case - file is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {
228 if (curwin->w_topline != 1)
229 redraw_later(NOT_VALID);
230 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 curwin->w_botline = 2;
232 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 }
235
236 /*
237 * If the cursor is above or near the top of the window, scroll the window
238 * to show the line the cursor is in, with 'scrolloff' context.
239 */
240 else
241 {
242 if (curwin->w_topline > 1)
243 {
244 /* If the cursor is above topline, scrolling is always needed.
245 * If the cursor is far below topline and there is no folding,
246 * scrolling down is never needed. */
247 if (curwin->w_cursor.lnum < curwin->w_topline)
248 check_topline = TRUE;
249 else if (check_top_offset())
250 check_topline = TRUE;
251 }
252#ifdef FEAT_DIFF
253 /* Check if there are more filler lines than allowed. */
254 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
255 curwin->w_topline))
256 check_topline = TRUE;
257#endif
258
259 if (check_topline)
260 {
261 halfheight = curwin->w_height / 2 - 1;
262 if (halfheight < 2)
263 halfheight = 2;
264
265#ifdef FEAT_FOLDING
266 if (hasAnyFolding(curwin))
267 {
268 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100269 * topline + scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 * scrolled). */
271 n = 0;
272 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100273 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
275 ++n;
276 /* stop at end of file or when we know we are far off */
277 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
278 break;
279 (void)hasFolding(lnum, NULL, &lnum);
280 }
281 }
282 else
283#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100284 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
286 /* If we weren't very close to begin with, we scroll to put the
287 * cursor in the middle of the window. Otherwise put the cursor
288 * near the top of the window. */
289 if (n >= halfheight)
290 scroll_cursor_halfway(FALSE);
291 else
292 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000293 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 check_botline = TRUE;
295 }
296 }
297
298 else
299 {
300#ifdef FEAT_FOLDING
301 /* Make sure topline is the first line of a fold. */
302 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
303#endif
304 check_botline = TRUE;
305 }
306 }
307
308 /*
309 * If the cursor is below the bottom of the window, scroll the window
310 * to put the cursor on the window.
311 * When w_botline is invalid, recompute it first, to avoid a redraw later.
312 * If w_botline was approximated, we might need a redraw later in a few
313 * cases, but we don't want to spend (a lot of) time recomputing w_botline
314 * for every small change.
315 */
316 if (check_botline)
317 {
318 if (!(curwin->w_valid & VALID_BOTLINE_AP))
319 validate_botline();
320
321 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
322 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000323 if (curwin->w_cursor.lnum < curwin->w_botline)
324 {
325 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100326 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_FOLDING
328 || hasAnyFolding(curwin)
329#endif
330 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 lineoff_T loff;
333
Bram Moolenaard4153d42008-11-15 15:06:17 +0000334 /* Cursor is (a few lines) above botline, check if there are
335 * 'scrolloff' window lines below the cursor. If not, need to
336 * scroll. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 n = curwin->w_empty_rows;
338 loff.lnum = curwin->w_cursor.lnum;
339#ifdef FEAT_FOLDING
340 /* In a fold go to its last line. */
341 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
342#endif
343#ifdef FEAT_DIFF
344 loff.fill = 0;
345 n += curwin->w_filler_rows;
346#endif
347 loff.height = 0;
348 while (loff.lnum < curwin->w_botline
349#ifdef FEAT_DIFF
350 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
351#endif
352 )
353 {
354 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100355 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 break;
357 botline_forw(&loff);
358 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100359 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 /* sufficient context, no need to scroll */
361 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000362 }
363 else
364 /* sufficient context, no need to scroll */
365 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 }
367 if (check_botline)
368 {
369#ifdef FEAT_FOLDING
370 if (hasAnyFolding(curwin))
371 {
372 /* Count the number of logical lines between the cursor and
Bram Moolenaar375e3392019-01-31 18:26:10 +0100373 * botline - scrolloff (approximation of how much will be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 * scrolled). */
375 line_count = 0;
376 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100377 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 ++line_count;
380 /* stop at end of file or when we know we are far off */
381 if (lnum <= 0 || line_count > curwin->w_height + 1)
382 break;
383 (void)hasFolding(lnum, &lnum, NULL);
384 }
385 }
386 else
387#endif
388 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100389 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000391 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 else
393 scroll_cursor_halfway(FALSE);
394 }
395 }
396 }
397 curwin->w_valid |= VALID_TOPLINE;
398
399 /*
400 * Need to redraw when topline changed.
401 */
402 if (curwin->w_topline != old_topline
403#ifdef FEAT_DIFF
404 || curwin->w_topfill != old_topfill
405#endif
406 )
407 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100408 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000409 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {
411 curwin->w_skipcol = 0;
412 redraw_later(NOT_VALID);
413 }
414 else
415 redraw_later(VALID);
416 /* May need to set w_skipcol when cursor in w_topline. */
417 if (curwin->w_cursor.lnum == curwin->w_topline)
418 validate_cursor();
419 }
420
421#ifdef FEAT_MOUSE
Bram Moolenaar375e3392019-01-31 18:26:10 +0100422 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
424}
425
426/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000427 * Return the scrolljump value to use for the current window.
428 * When 'scrolljump' is positive use it as-is.
429 * When 'scrolljump' is negative use it as a percentage of the window height.
430 */
431 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100432scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000433{
434 if (p_sj >= 0)
435 return (int)p_sj;
436 return (curwin->w_height * -p_sj) / 100;
437}
438
439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
441 * current window.
442 */
443 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100444check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
446 lineoff_T loff;
447 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100448 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
Bram Moolenaar375e3392019-01-31 18:26:10 +0100450 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#ifdef FEAT_FOLDING
452 || hasAnyFolding(curwin)
453#endif
454 )
455 {
456 loff.lnum = curwin->w_cursor.lnum;
457#ifdef FEAT_DIFF
458 loff.fill = 0;
459 n = curwin->w_topfill; /* always have this context */
460#else
461 n = 0;
462#endif
463 /* Count the visible screen lines above the cursor line. */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
466 topline_back(&loff);
467 /* Stop when included a line above the window. */
468 if (loff.lnum < curwin->w_topline
469#ifdef FEAT_DIFF
470 || (loff.lnum == curwin->w_topline && loff.fill > 0)
471#endif
472 )
473 break;
474 n += loff.height;
475 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100476 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 return TRUE;
478 }
479 return FALSE;
480}
481
482 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100483update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484{
485 if (curwin->w_set_curswant)
486 {
487 validate_virtcol();
488 curwin->w_curswant = curwin->w_virtcol;
489 curwin->w_set_curswant = FALSE;
490 }
491}
492
493/*
494 * Check if the cursor has moved. Set the w_valid flag accordingly.
495 */
496 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100497check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
500 {
501 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
502 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
503 wp->w_valid_cursor = wp->w_cursor;
504 wp->w_valid_leftcol = wp->w_leftcol;
505 }
506 else if (wp->w_cursor.col != wp->w_valid_cursor.col
507 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100508 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
510 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
511 wp->w_valid_cursor.col = wp->w_cursor.col;
512 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
515}
516
517/*
518 * Call this function when some window settings have changed, which require
519 * the cursor position, botline and topline to be recomputed and the window to
520 * be redrawn. E.g, when changing the 'wrap' option or folding.
521 */
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 changed_window_setting_win(curwin);
526}
527
528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100529changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 wp->w_lines_valid = 0;
532 changed_line_abv_curs_win(wp);
533 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
534 redraw_win_later(wp, NOT_VALID);
535}
536
537/*
538 * Set wp->w_topline to a certain number.
539 */
540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100541set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543#ifdef FEAT_FOLDING
544 /* go to first of folded lines */
545 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
546#endif
547 /* Approximate the value of w_botline */
548 wp->w_botline += lnum - wp->w_topline;
549 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000550 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#ifdef FEAT_DIFF
552 wp->w_topfill = 0;
553#endif
554 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
555 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
556 redraw_later(VALID);
557}
558
559/*
560 * Call this function when the length of the cursor line (in screen
561 * characters) has changed, and the change is before the cursor.
562 * Need to take care of w_botline separately!
563 */
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
568 |VALID_CHEIGHT|VALID_TOPLINE);
569}
570
571 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100572changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
575 |VALID_CHEIGHT|VALID_TOPLINE);
576}
577
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578/*
579 * Call this function when the length of a line (in screen characters) above
580 * the cursor have changed.
581 * Need to take care of w_botline separately!
582 */
583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100584changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
587 |VALID_CHEIGHT|VALID_TOPLINE);
588}
589
590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100591changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
594 |VALID_CHEIGHT|VALID_TOPLINE);
595}
596
597/*
598 * Make sure the value of curwin->w_botline is valid.
599 */
600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 if (!(curwin->w_valid & VALID_BOTLINE))
604 comp_botline(curwin);
605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * Mark curwin->w_botline as invalid (because of some change in the buffer).
609 */
610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100611invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
614}
615
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100623approximate_botline_win(
624 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 wp->w_valid &= ~VALID_BOTLINE;
627}
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629/*
630 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
631 */
632 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100633cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 check_cursor_moved(curwin);
636 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
637 (VALID_WROW|VALID_WCOL));
638}
639
640/*
641 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
642 * w_topline must be valid, you may need to call update_topline() first!
643 */
644 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100645validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
647 check_cursor_moved(curwin);
648 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
649 curs_columns(TRUE);
650}
651
652#if defined(FEAT_GUI) || defined(PROTO)
653/*
654 * validate w_cline_row.
655 */
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 /*
660 * First make sure that w_topline is valid (after moving the cursor).
661 */
662 update_topline();
663 check_cursor_moved(curwin);
664 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100665 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667#endif
668
669/*
670 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200671 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100674curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 linenr_T lnum;
677 int i;
678 int all_invalid;
679 int valid;
680#ifdef FEAT_FOLDING
681 long fold_count;
682#endif
683
684 /* Check if wp->w_lines[].wl_size is invalid */
685 all_invalid = (!redrawing()
686 || wp->w_lines_valid == 0
687 || wp->w_lines[0].wl_lnum > wp->w_topline);
688 i = 0;
689 wp->w_cline_row = 0;
690 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
691 {
692 valid = FALSE;
693 if (!all_invalid && i < wp->w_lines_valid)
694 {
695 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
696 continue; /* skip changed or deleted lines */
697 if (wp->w_lines[i].wl_lnum == lnum)
698 {
699#ifdef FEAT_FOLDING
700 /* Check for newly inserted lines below this row, in which
701 * case we need to check for folded lines. */
702 if (!wp->w_buffer->b_mod_set
703 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
704 || wp->w_buffer->b_mod_top
705 > wp->w_lines[i].wl_lastlnum + 1)
706#endif
707 valid = TRUE;
708 }
709 else if (wp->w_lines[i].wl_lnum > lnum)
710 --i; /* hold at inserted lines */
711 }
712 if (valid
713#ifdef FEAT_DIFF
714 && (lnum != wp->w_topline || !wp->w_p_diff)
715#endif
716 )
717 {
718#ifdef FEAT_FOLDING
719 lnum = wp->w_lines[i].wl_lastlnum + 1;
720 /* Cursor inside folded lines, don't count this row */
721 if (lnum > wp->w_cursor.lnum)
722 break;
723#else
724 ++lnum;
725#endif
726 wp->w_cline_row += wp->w_lines[i].wl_size;
727 }
728 else
729 {
730#ifdef FEAT_FOLDING
731 fold_count = foldedCount(wp, lnum, NULL);
732 if (fold_count)
733 {
734 lnum += fold_count;
735 if (lnum > wp->w_cursor.lnum)
736 break;
737 ++wp->w_cline_row;
738 }
739 else
740#endif
741#ifdef FEAT_DIFF
742 if (lnum == wp->w_topline)
743 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
744 + wp->w_topfill;
745 else
746#endif
747 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
748 }
749 }
750
751 check_cursor_moved(wp);
752 if (!(wp->w_valid & VALID_CHEIGHT))
753 {
754 if (all_invalid
755 || i == wp->w_lines_valid
756 || (i < wp->w_lines_valid
757 && (!wp->w_lines[i].wl_valid
758 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
759 {
760#ifdef FEAT_DIFF
761 if (wp->w_cursor.lnum == wp->w_topline)
762 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
763 TRUE) + wp->w_topfill;
764 else
765#endif
766 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
767#ifdef FEAT_FOLDING
768 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
769 NULL, NULL, TRUE, NULL);
770#endif
771 }
772 else if (i > wp->w_lines_valid)
773 {
774 /* a line that is too long to fit on the last screen line */
775 wp->w_cline_height = 0;
776#ifdef FEAT_FOLDING
777 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
778 NULL, NULL, TRUE, NULL);
779#endif
780 }
781 else
782 {
783 wp->w_cline_height = wp->w_lines[i].wl_size;
784#ifdef FEAT_FOLDING
785 wp->w_cline_folded = wp->w_lines[i].wl_folded;
786#endif
787 }
788 }
789
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100790 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
794
795/*
796 * Validate curwin->w_virtcol only.
797 */
798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100799validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 validate_virtcol_win(curwin);
802}
803
804/*
805 * Validate wp->w_virtcol only.
806 */
807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100808validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
810 check_cursor_moved(wp);
811 if (!(wp->w_valid & VALID_VIRTCOL))
812 {
813 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
814 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000815#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200816 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000817 redraw_win_later(wp, SOME_VALID);
818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820}
821
822/*
823 * Validate curwin->w_cline_height only.
824 */
825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100826validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 check_cursor_moved(curwin);
829 if (!(curwin->w_valid & VALID_CHEIGHT))
830 {
831#ifdef FEAT_DIFF
832 if (curwin->w_cursor.lnum == curwin->w_topline)
833 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
834 + curwin->w_topfill;
835 else
836#endif
837 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
838#ifdef FEAT_FOLDING
839 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
840#endif
841 curwin->w_valid |= VALID_CHEIGHT;
842 }
843}
844
845/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000846 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100849validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 colnr_T off;
852 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100853 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
855 validate_virtcol();
856 if (!(curwin->w_valid & VALID_WCOL))
857 {
858 col = curwin->w_virtcol;
859 off = curwin_col_off();
860 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200861 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaarc236c162008-07-13 17:41:49 +0000864 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200865 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100866 && width > 0)
867 /* use same formula as what is used in curs_columns() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200868 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000869 if (col > (int)curwin->w_leftcol)
870 col -= curwin->w_leftcol;
871 else
872 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 curwin->w_valid |= VALID_WCOL;
876 }
877}
878
879/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200880 * Compute offset of a window, occupied by absolute or relative line number,
881 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 */
883 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100884win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
Bram Moolenaar64486672010-05-16 15:46:46 +0200886 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#ifdef FEAT_CMDWIN
888 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
889#endif
890#ifdef FEAT_FOLDING
891 + wp->w_p_fdc
892#endif
893#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200894 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#endif
896 );
897}
898
899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100900curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
902 return win_col_off(curwin);
903}
904
905/*
906 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
908 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 */
910 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100911win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
Bram Moolenaar64486672010-05-16 15:46:46 +0200913 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000914 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return 0;
916}
917
918 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100919curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 return win_col_off2(curwin);
922}
923
924/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100925 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * Also updates curwin->w_wrow and curwin->w_cline_row.
927 * Also updates curwin->w_leftcol.
928 */
929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100930curs_columns(
931 int may_scroll) /* when TRUE, may scroll horizontally */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
933 int diff;
934 int extra; /* offset for first screen line */
935 int off_left, off_right;
936 int n;
937 int p_lines;
938 int width = 0;
939 int textwidth;
940 int new_leftcol;
941 colnr_T startcol;
942 colnr_T endcol;
943 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100944 long so = get_scrolloff_value();
945 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 /*
948 * First make sure that w_topline is valid (after moving the cursor).
949 */
950 update_topline();
951
952 /*
953 * Next make sure that w_cline_row is valid.
954 */
955 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100956 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 /*
959 * Compute the number of virtual columns.
960 */
961#ifdef FEAT_FOLDING
962 if (curwin->w_cline_folded)
963 /* In a folded line the cursor is always in the first column */
964 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
965 else
966#endif
967 getvvcol(curwin, &curwin->w_cursor,
968 &startcol, &(curwin->w_virtcol), &endcol);
969
970 /* remove '$' from change command when cursor moves onto it */
971 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100972 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 extra = curwin_col_off();
975 curwin->w_wcol = curwin->w_virtcol + extra;
976 endcol += extra;
977
978 /*
979 * Now compute w_wrow, counting screen lines from w_cline_row.
980 */
981 curwin->w_wrow = curwin->w_cline_row;
982
Bram Moolenaar02631462017-09-22 15:20:32 +0200983 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (textwidth <= 0)
985 {
986 /* No room for text, put cursor in last char of window. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200987 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 curwin->w_wrow = curwin->w_height - 1;
989 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200990 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 width = textwidth + curwin_col_off2();
993
994 /* long line wrapping, adjust curwin->w_wrow */
Bram Moolenaar02631462017-09-22 15:20:32 +0200995 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
Bram Moolenaar6427c602010-02-03 17:43:07 +0100997 /* this same formula is used in validate_cursor_col() */
Bram Moolenaar02631462017-09-22 15:20:32 +0200998 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 curwin->w_wcol -= n * width;
1000 curwin->w_wrow += n;
1001
1002#ifdef FEAT_LINEBREAK
1003 /* When cursor wraps to first char of next line in Insert
1004 * mode, the 'showbreak' string isn't shown, backup to first
1005 * column */
1006 if (*p_sbr && *ml_get_cursor() == NUL
1007 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1008 curwin->w_wcol = 0;
1009#endif
1010 }
1011 }
1012
1013 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1014 * is not folded.
1015 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001016 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_FOLDING
1018 && !curwin->w_cline_folded
1019#endif
1020 )
1021 {
1022 /*
1023 * If Cursor is left of the screen, scroll rightwards.
1024 * If Cursor is right of the screen, scroll leftwards
1025 * If we get closer to the edge than 'sidescrolloff', scroll a little
1026 * extra
1027 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001028 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001029 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001030 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (off_left < 0 || off_right > 0)
1032 {
1033 if (off_left < 0)
1034 diff = -off_left;
1035 else
1036 diff = off_right;
1037
1038 /* When far off or not enough room on either side, put cursor in
1039 * middle of window. */
1040 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1041 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1042 else
1043 {
1044 if (diff < p_ss)
1045 diff = p_ss;
1046 if (off_left < 0)
1047 new_leftcol = curwin->w_leftcol - diff;
1048 else
1049 new_leftcol = curwin->w_leftcol + diff;
1050 }
1051 if (new_leftcol < 0)
1052 new_leftcol = 0;
1053 if (new_leftcol != (int)curwin->w_leftcol)
1054 {
1055 curwin->w_leftcol = new_leftcol;
1056 /* screen has to be redrawn with new curwin->w_leftcol */
1057 redraw_later(NOT_VALID);
1058 }
1059 }
1060 curwin->w_wcol -= curwin->w_leftcol;
1061 }
1062 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1063 curwin->w_wcol -= curwin->w_leftcol;
1064 else
1065 curwin->w_wcol = 0;
1066
1067#ifdef FEAT_DIFF
1068 /* Skip over filler lines. At the top use w_topfill, there
1069 * may be some filler lines above the window. */
1070 if (curwin->w_cursor.lnum == curwin->w_topline)
1071 curwin->w_wrow += curwin->w_topfill;
1072 else
1073 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1074#endif
1075
1076 prev_skipcol = curwin->w_skipcol;
1077
1078 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if ((curwin->w_wrow >= curwin->w_height
1081 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001082 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 && (p_lines =
1084#ifdef FEAT_DIFF
1085 plines_win_nofill
1086#else
1087 plines_win
1088#endif
1089 (curwin, curwin->w_cursor.lnum, FALSE))
1090 - 1 >= curwin->w_height))
1091 && curwin->w_height != 0
1092 && curwin->w_cursor.lnum == curwin->w_topline
1093 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001094 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 /* Cursor past end of screen. Happens with a single line that does
1097 * not fit on screen. Find a skipcol to show the text around the
1098 * cursor. Avoid scrolling all the time. compute value of "extra":
Bram Moolenaar375e3392019-01-31 18:26:10 +01001099 * 1: Less than 'scrolloff' lines above
1100 * 2: Less than 'scrolloff' lines below
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 * 3: both of them */
1102 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001103 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 extra = 1;
1105 /* Compute last display line of the buffer line that we want at the
1106 * bottom of the window. */
1107 if (p_lines == 0)
1108 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1109 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001110 if (p_lines > curwin->w_wrow + so)
1111 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 else
1113 n = p_lines;
1114 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1115 extra += 2;
1116
Bram Moolenaar375e3392019-01-31 18:26:10 +01001117 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 /* not enough room for 'scrolloff', put cursor in the middle */
1120 n = curwin->w_virtcol / width;
1121 if (n > curwin->w_height / 2)
1122 n -= curwin->w_height / 2;
1123 else
1124 n = 0;
1125 /* don't skip more than necessary */
1126 if (n > p_lines - curwin->w_height + 1)
1127 n = p_lines - curwin->w_height + 1;
1128 curwin->w_skipcol = n * width;
1129 }
1130 else if (extra == 1)
1131 {
1132 /* less then 'scrolloff' lines above, decrease skipcol */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001133 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 + width - 1) / width;
1135 if (extra > 0)
1136 {
1137 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1138 extra = curwin->w_skipcol / width;
1139 curwin->w_skipcol -= extra * width;
1140 }
1141 }
1142 else if (extra == 2)
1143 {
1144 /* less then 'scrolloff' lines below, increase skipcol */
1145 endcol = (n - curwin->w_height + 1) * width;
1146 while (endcol > curwin->w_virtcol)
1147 endcol -= width;
1148 if (endcol > curwin->w_skipcol)
1149 curwin->w_skipcol = endcol;
1150 }
1151
1152 curwin->w_wrow -= curwin->w_skipcol / width;
1153 if (curwin->w_wrow >= curwin->w_height)
1154 {
1155 /* small window, make sure cursor is in it */
1156 extra = curwin->w_wrow - curwin->w_height + 1;
1157 curwin->w_skipcol += extra * width;
1158 curwin->w_wrow -= extra;
1159 }
1160
1161 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1162 if (extra > 0)
1163 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1164 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001165 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 else
1168 curwin->w_skipcol = 0;
1169 if (prev_skipcol != curwin->w_skipcol)
1170 redraw_later(NOT_VALID);
1171
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001172#ifdef FEAT_SYN_HL
Bram Moolenaarb6798752014-03-27 12:11:48 +01001173 /* Redraw when w_virtcol changes and 'cursorcolumn' is set */
1174 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001175 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001176 redraw_later(SOME_VALID);
1177#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1180}
1181
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001182#if defined(FEAT_EVAL) || defined(PROTO)
1183/*
1184 * Compute the screen position of text character at "pos" in window "wp"
1185 * The resulting values are one-based, zero when character is not visible.
1186 */
1187 static void
1188textpos2screenpos(
1189 win_T *wp,
1190 pos_T *pos,
1191 int *rowp, // screen row
1192 int *scolp, // start screen column
1193 int *ccolp, // cursor screen column
1194 int *ecolp) // end screen column
1195{
1196 colnr_T scol = 0, ccol = 0, ecol = 0;
1197 int row = 0;
1198 int rowoff = 0;
1199 colnr_T coloff = 0;
1200
1201 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1202 {
1203 colnr_T off;
1204 colnr_T col;
1205 int width;
1206
1207 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1208 getvcol(wp, pos, &scol, &ccol, &ecol);
1209
1210 // similar to what is done in validate_cursor_col()
1211 col = scol;
1212 off = win_col_off(wp);
1213 col += off;
1214 width = wp->w_width - off + win_col_off2(wp);
1215
1216 /* long line wrapping, adjust row */
1217 if (wp->w_p_wrap
1218 && col >= (colnr_T)wp->w_width
1219 && width > 0)
1220 {
1221 /* use same formula as what is used in curs_columns() */
1222 rowoff = ((col - wp->w_width) / width + 1);
1223 col -= rowoff * width;
1224 }
1225 col -= wp->w_leftcol;
1226 if (col >= width)
1227 col = -1;
1228 if (col >= 0)
1229 coloff = col - scol + wp->w_wincol + 1;
1230 else
1231 // character is left or right of the window
1232 row = scol = ccol = ecol = 0;
1233 }
1234 *rowp = wp->w_winrow + row + rowoff;
1235 *scolp = scol + coloff;
1236 *ccolp = ccol + coloff;
1237 *ecolp = ecol + coloff;
1238}
1239
1240/*
1241 * "screenpos({winid}, {lnum}, {col})" function
1242 */
1243 void
1244f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1245{
1246 dict_T *dict;
1247 win_T *wp;
1248 pos_T pos;
1249 int row = 0;
1250 int scol = 0, ccol = 0, ecol = 0;
1251
1252 if (rettv_dict_alloc(rettv) != OK)
1253 return;
1254 dict = rettv->vval.v_dict;
1255
1256 wp = find_win_by_nr_or_id(&argvars[0]);
1257 if (wp == NULL)
1258 return;
1259
1260 pos.lnum = tv_get_number(&argvars[1]);
1261 pos.col = tv_get_number(&argvars[2]) - 1;
1262 pos.coladd = 0;
1263 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1264
1265 dict_add_number(dict, "row", row);
1266 dict_add_number(dict, "col", scol);
1267 dict_add_number(dict, "curscol", ccol);
1268 dict_add_number(dict, "endcol", ecol);
1269}
1270#endif
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001276scrolldown(
1277 long line_count,
1278 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 long done = 0; /* total # of physical lines done */
1281 int wrow;
1282 int moved = FALSE;
1283
1284#ifdef FEAT_FOLDING
1285 linenr_T first;
1286
1287 /* Make sure w_topline is at the first of a sequence of folded lines. */
1288 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1289#endif
1290 validate_cursor(); /* w_wrow needs to be valid */
1291 while (line_count-- > 0)
1292 {
1293#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001294 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1295 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 ++curwin->w_topfill;
1298 ++done;
1299 }
1300 else
1301#endif
1302 {
1303 if (curwin->w_topline == 1)
1304 break;
1305 --curwin->w_topline;
1306#ifdef FEAT_DIFF
1307 curwin->w_topfill = 0;
1308#endif
1309#ifdef FEAT_FOLDING
1310 /* A sequence of folded lines only counts for one logical line */
1311 if (hasFolding(curwin->w_topline, &first, NULL))
1312 {
1313 ++done;
1314 if (!byfold)
1315 line_count -= curwin->w_topline - first - 1;
1316 curwin->w_botline -= curwin->w_topline - first;
1317 curwin->w_topline = first;
1318 }
1319 else
1320#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001321 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 --curwin->w_botline; /* approximate w_botline */
1324 invalidate_botline();
1325 }
1326 curwin->w_wrow += done; /* keep w_wrow updated */
1327 curwin->w_cline_row += done; /* keep w_cline_row updated */
1328
1329#ifdef FEAT_DIFF
1330 if (curwin->w_cursor.lnum == curwin->w_topline)
1331 curwin->w_cline_row = 0;
1332 check_topfill(curwin, TRUE);
1333#endif
1334
1335 /*
1336 * Compute the row number of the last row of the cursor line
1337 * and move the cursor onto the displayed part of the window.
1338 */
1339 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001340 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 validate_virtcol();
1343 validate_cheight();
1344 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001345 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1348 {
1349#ifdef FEAT_FOLDING
1350 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1351 {
1352 --wrow;
1353 if (first == 1)
1354 curwin->w_cursor.lnum = 1;
1355 else
1356 curwin->w_cursor.lnum = first - 1;
1357 }
1358 else
1359#endif
1360 wrow -= plines(curwin->w_cursor.lnum--);
1361 curwin->w_valid &=
1362 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1363 moved = TRUE;
1364 }
1365 if (moved)
1366 {
1367#ifdef FEAT_FOLDING
1368 /* Move cursor to first line of closed fold. */
1369 foldAdjustCursor();
1370#endif
1371 coladvance(curwin->w_curswant);
1372 }
1373}
1374
1375/*
1376 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001379scrollup(
1380 long line_count,
1381 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1384 linenr_T lnum;
1385
1386 if (
1387# ifdef FEAT_FOLDING
1388 (byfold && hasAnyFolding(curwin))
1389# ifdef FEAT_DIFF
1390 ||
1391# endif
1392# endif
1393# ifdef FEAT_DIFF
1394 curwin->w_p_diff
1395# endif
1396 )
1397 {
1398 /* count each sequence of folded lines as one logical line */
1399 lnum = curwin->w_topline;
1400 while (line_count--)
1401 {
1402# ifdef FEAT_DIFF
1403 if (curwin->w_topfill > 0)
1404 --curwin->w_topfill;
1405 else
1406# endif
1407 {
1408# ifdef FEAT_FOLDING
1409 if (byfold)
1410 (void)hasFolding(lnum, NULL, &lnum);
1411# endif
1412 if (lnum >= curbuf->b_ml.ml_line_count)
1413 break;
1414 ++lnum;
1415# ifdef FEAT_DIFF
1416 curwin->w_topfill = diff_check_fill(curwin, lnum);
1417# endif
1418 }
1419 }
1420 /* approximate w_botline */
1421 curwin->w_botline += lnum - curwin->w_topline;
1422 curwin->w_topline = lnum;
1423 }
1424 else
1425#endif
1426 {
1427 curwin->w_topline += line_count;
1428 curwin->w_botline += line_count; /* approximate w_botline */
1429 }
1430
1431 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1432 curwin->w_topline = curbuf->b_ml.ml_line_count;
1433 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1434 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1435
1436#ifdef FEAT_DIFF
1437 check_topfill(curwin, FALSE);
1438#endif
1439
1440#ifdef FEAT_FOLDING
1441 if (hasAnyFolding(curwin))
1442 /* Make sure w_topline is at the first of a sequence of folded lines. */
1443 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1444#endif
1445
1446 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1447 if (curwin->w_cursor.lnum < curwin->w_topline)
1448 {
1449 curwin->w_cursor.lnum = curwin->w_topline;
1450 curwin->w_valid &=
1451 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1452 coladvance(curwin->w_curswant);
1453 }
1454}
1455
1456#ifdef FEAT_DIFF
1457/*
1458 * Don't end up with too many filler lines in the window.
1459 */
1460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001461check_topfill(
1462 win_T *wp,
1463 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 int n;
1466
1467 if (wp->w_topfill > 0)
1468 {
1469 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1470 if (wp->w_topfill + n > wp->w_height)
1471 {
1472 if (down && wp->w_topline > 1)
1473 {
1474 --wp->w_topline;
1475 wp->w_topfill = 0;
1476 }
1477 else
1478 {
1479 wp->w_topfill = wp->w_height - n;
1480 if (wp->w_topfill < 0)
1481 wp->w_topfill = 0;
1482 }
1483 }
1484 }
1485}
1486
1487/*
1488 * Use as many filler lines as possible for w_topline. Make sure w_topline
1489 * is still visible.
1490 */
1491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001492max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494 int n;
1495
1496 n = plines_nofill(curwin->w_topline);
1497 if (n >= curwin->w_height)
1498 curwin->w_topfill = 0;
1499 else
1500 {
1501 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1502 if (curwin->w_topfill + n > curwin->w_height)
1503 curwin->w_topfill = curwin->w_height - n;
1504 }
1505}
1506#endif
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508/*
1509 * Scroll the screen one line down, but don't do it if it would move the
1510 * cursor off the screen.
1511 */
1512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001513scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 int end_row;
1516#ifdef FEAT_DIFF
1517 int can_fill = (curwin->w_topfill
1518 < diff_check_fill(curwin, curwin->w_topline));
1519#endif
1520
1521 if (curwin->w_topline <= 1
1522#ifdef FEAT_DIFF
1523 && !can_fill
1524#endif
1525 )
1526 return;
1527
1528 validate_cursor(); /* w_wrow needs to be valid */
1529
1530 /*
1531 * Compute the row number of the last row of the cursor line
1532 * and make sure it doesn't go off the screen. Make sure the cursor
1533 * doesn't go past 'scrolloff' lines from the screen end.
1534 */
1535 end_row = curwin->w_wrow;
1536#ifdef FEAT_DIFF
1537 if (can_fill)
1538 ++end_row;
1539 else
1540 end_row += plines_nofill(curwin->w_topline - 1);
1541#else
1542 end_row += plines(curwin->w_topline - 1);
1543#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001544 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
1546 validate_cheight();
1547 validate_virtcol();
1548 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001549 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001551 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553#ifdef FEAT_DIFF
1554 if (can_fill)
1555 {
1556 ++curwin->w_topfill;
1557 check_topfill(curwin, TRUE);
1558 }
1559 else
1560 {
1561 --curwin->w_topline;
1562 curwin->w_topfill = 0;
1563 }
1564#else
1565 --curwin->w_topline;
1566#endif
1567#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001568 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
1570 --curwin->w_botline; /* approximate w_botline */
1571 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1572 }
1573}
1574
1575/*
1576 * Scroll the screen one line up, but don't do it if it would move the cursor
1577 * off the screen.
1578 */
1579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001580scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 int start_row;
1583
1584 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1585#ifdef FEAT_DIFF
1586 && curwin->w_topfill == 0
1587#endif
1588 )
1589 return;
1590
1591 validate_cursor(); /* w_wrow needs to be valid */
1592
1593 /*
1594 * Compute the row number of the first row of the cursor line
1595 * and make sure it doesn't go off the screen. Make sure the cursor
1596 * doesn't go before 'scrolloff' lines from the screen start.
1597 */
1598#ifdef FEAT_DIFF
1599 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1600 - curwin->w_topfill;
1601#else
1602 start_row = curwin->w_wrow - plines(curwin->w_topline);
1603#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001604 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {
1606 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001607 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001609 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611#ifdef FEAT_DIFF
1612 if (curwin->w_topfill > 0)
1613 --curwin->w_topfill;
1614 else
1615#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001616 {
1617#ifdef FEAT_FOLDING
1618 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 ++curwin->w_botline; /* approximate w_botline */
1623 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1624 }
1625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627/*
1628 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1629 * a (wrapped) text line. Uses and sets "lp->fill".
1630 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001631 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 */
1633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001634topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636#ifdef FEAT_DIFF
1637 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1638 {
1639 /* Add a filler line. */
1640 ++lp->fill;
1641 lp->height = 1;
1642 }
1643 else
1644#endif
1645 {
1646 --lp->lnum;
1647#ifdef FEAT_DIFF
1648 lp->fill = 0;
1649#endif
1650 if (lp->lnum < 1)
1651 lp->height = MAXCOL;
1652 else
1653#ifdef FEAT_FOLDING
1654 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1655 /* Add a closed fold */
1656 lp->height = 1;
1657 else
1658#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001659 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661}
1662
1663/*
1664 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1665 * a (wrapped) text line. Uses and sets "lp->fill".
1666 * Returns the height of the added line in "lp->height".
1667 * Lines below the last one are incredibly high.
1668 */
1669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001670botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672#ifdef FEAT_DIFF
1673 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1674 {
1675 /* Add a filler line. */
1676 ++lp->fill;
1677 lp->height = 1;
1678 }
1679 else
1680#endif
1681 {
1682 ++lp->lnum;
1683#ifdef FEAT_DIFF
1684 lp->fill = 0;
1685#endif
1686 if (lp->lnum > curbuf->b_ml.ml_line_count)
1687 lp->height = MAXCOL;
1688 else
1689#ifdef FEAT_FOLDING
1690 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1691 /* Add a closed fold */
1692 lp->height = 1;
1693 else
1694#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001695 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
1697}
1698
1699#ifdef FEAT_DIFF
1700/*
1701 * Switch from including filler lines below lp->lnum to including filler
1702 * lines above loff.lnum + 1. This keeps pointing to the same line.
1703 * When there are no filler lines nothing changes.
1704 */
1705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 if (lp->fill > 0)
1709 {
1710 ++lp->lnum;
1711 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1712 }
1713}
1714
1715/*
1716 * Switch from including filler lines above lp->lnum to including filler
1717 * lines below loff.lnum - 1. This keeps pointing to the same line.
1718 * When there are no filler lines nothing changes.
1719 */
1720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001721topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 if (lp->fill > 0)
1724 {
1725 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1726 --lp->lnum;
1727 }
1728}
1729#endif
1730
1731/*
1732 * Recompute topline to put the cursor at the top of the window.
1733 * Scroll at least "min_scroll" lines.
1734 * If "always" is TRUE, always set topline (for "zt").
1735 */
1736 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001737scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 int scrolled = 0;
1740 int extra = 0;
1741 int used;
1742 int i;
1743 linenr_T top; /* just above displayed lines */
1744 linenr_T bot; /* just below displayed lines */
1745 linenr_T old_topline = curwin->w_topline;
1746#ifdef FEAT_DIFF
1747 linenr_T old_topfill = curwin->w_topfill;
1748#endif
1749 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001750 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752#ifdef FEAT_MOUSE
1753 if (mouse_dragging > 0)
1754 off = mouse_dragging - 1;
1755#endif
1756
1757 /*
1758 * Decrease topline until:
1759 * - it has become 1
1760 * - (part of) the cursor line is moved off the screen or
1761 * - moved at least 'scrolljump' lines and
1762 * - at least 'scrolloff' lines above and below the cursor
1763 */
1764 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001765 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (curwin->w_cursor.lnum < curwin->w_topline)
1767 scrolled = used;
1768
1769#ifdef FEAT_FOLDING
1770 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1771 {
1772 --top;
1773 ++bot;
1774 }
1775 else
1776#endif
1777 {
1778 top = curwin->w_cursor.lnum - 1;
1779 bot = curwin->w_cursor.lnum + 1;
1780 }
1781 new_topline = top + 1;
1782
1783#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001784 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001785 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001786 * Hide filler lines above cursor line by adding them to "extra". */
1787 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789
1790 /*
1791 * Check if the lines from "top" to "bot" fit in the window. If they do,
1792 * set new_topline and advance "top" and "bot" to include more lines.
1793 */
1794 while (top > 0)
1795 {
1796#ifdef FEAT_FOLDING
1797 if (hasFolding(top, &top, NULL))
1798 /* count one logical line for a sequence of folded lines */
1799 i = 1;
1800 else
1801#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001802 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 used += i;
1804 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1805 {
1806#ifdef FEAT_FOLDING
1807 if (hasFolding(bot, NULL, &bot))
1808 /* count one logical line for a sequence of folded lines */
1809 ++used;
1810 else
1811#endif
1812 used += plines(bot);
1813 }
1814 if (used > curwin->w_height)
1815 break;
1816 if (top < curwin->w_topline)
1817 scrolled += i;
1818
1819 /*
1820 * If scrolling is needed, scroll at least 'sj' lines.
1821 */
1822 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1823 && extra >= off)
1824 break;
1825
1826 extra += i;
1827 new_topline = top;
1828 --top;
1829 ++bot;
1830 }
1831
1832 /*
1833 * If we don't have enough space, put cursor in the middle.
1834 * This makes sure we get the same position when using "k" and "j"
1835 * in a small window.
1836 */
1837 if (used > curwin->w_height)
1838 scroll_cursor_halfway(FALSE);
1839 else
1840 {
1841 /*
1842 * If "always" is FALSE, only adjust topline to a lower value, higher
1843 * value may happen with wrapping lines
1844 */
1845 if (new_topline < curwin->w_topline || always)
1846 curwin->w_topline = new_topline;
1847 if (curwin->w_topline > curwin->w_cursor.lnum)
1848 curwin->w_topline = curwin->w_cursor.lnum;
1849#ifdef FEAT_DIFF
1850 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1851 if (curwin->w_topfill > 0 && extra > off)
1852 {
1853 curwin->w_topfill -= extra - off;
1854 if (curwin->w_topfill < 0)
1855 curwin->w_topfill = 0;
1856 }
1857 check_topfill(curwin, FALSE);
1858#endif
1859 if (curwin->w_topline != old_topline
1860#ifdef FEAT_DIFF
1861 || curwin->w_topfill != old_topfill
1862#endif
1863 )
1864 curwin->w_valid &=
1865 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1866 curwin->w_valid |= VALID_TOPLINE;
1867 }
1868}
1869
1870/*
1871 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1872 * screen lines for text lines.
1873 */
1874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001875set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877#ifdef FEAT_DIFF
1878 wp->w_filler_rows = 0;
1879#endif
1880 if (used == 0)
1881 wp->w_empty_rows = 0; /* single line that doesn't fit */
1882 else
1883 {
1884 wp->w_empty_rows = wp->w_height - used;
1885#ifdef FEAT_DIFF
1886 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1887 {
1888 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1889 if (wp->w_empty_rows > wp->w_filler_rows)
1890 wp->w_empty_rows -= wp->w_filler_rows;
1891 else
1892 {
1893 wp->w_filler_rows = wp->w_empty_rows;
1894 wp->w_empty_rows = 0;
1895 }
1896 }
1897#endif
1898 }
1899}
1900
1901/*
1902 * Recompute topline to put the cursor at the bottom of the window.
1903 * Scroll at least "min_scroll" lines.
1904 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1905 * This is messy stuff!!!
1906 */
1907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001908scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 int used;
1911 int scrolled = 0;
1912 int extra = 0;
1913 int i;
1914 linenr_T line_count;
1915 linenr_T old_topline = curwin->w_topline;
1916 lineoff_T loff;
1917 lineoff_T boff;
1918#ifdef FEAT_DIFF
1919 int old_topfill = curwin->w_topfill;
1920 int fill_below_window;
1921#endif
1922 linenr_T old_botline = curwin->w_botline;
1923 linenr_T old_valid = curwin->w_valid;
1924 int old_empty_rows = curwin->w_empty_rows;
1925 linenr_T cln; /* Cursor Line Number */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001926 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 cln = curwin->w_cursor.lnum;
1929 if (set_topbot)
1930 {
1931 used = 0;
1932 curwin->w_botline = cln + 1;
1933#ifdef FEAT_DIFF
1934 loff.fill = 0;
1935#endif
1936 for (curwin->w_topline = curwin->w_botline;
1937 curwin->w_topline > 1;
1938 curwin->w_topline = loff.lnum)
1939 {
1940 loff.lnum = curwin->w_topline;
1941 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001942 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 break;
1944 used += loff.height;
1945#ifdef FEAT_DIFF
1946 curwin->w_topfill = loff.fill;
1947#endif
1948 }
1949 set_empty_rows(curwin, used);
1950 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1951 if (curwin->w_topline != old_topline
1952#ifdef FEAT_DIFF
1953 || curwin->w_topfill != old_topfill
1954#endif
1955 )
1956 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1957 }
1958 else
1959 validate_botline();
1960
1961 /* The lines of the cursor line itself are always used. */
1962#ifdef FEAT_DIFF
1963 used = plines_nofill(cln);
1964#else
1965 validate_cheight();
1966 used = curwin->w_cline_height;
1967#endif
1968
1969 /* If the cursor is below botline, we will at least scroll by the height
1970 * of the cursor line. Correct for empty lines, which are really part of
1971 * botline. */
1972 if (cln >= curwin->w_botline)
1973 {
1974 scrolled = used;
1975 if (cln == curwin->w_botline)
1976 scrolled -= curwin->w_empty_rows;
1977 }
1978
1979 /*
1980 * Stop counting lines to scroll when
1981 * - hitting start of the file
1982 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001983 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 * - lines between botline and cursor have been counted
1985 */
1986#ifdef FEAT_FOLDING
1987 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1988#endif
1989 {
1990 loff.lnum = cln;
1991 boff.lnum = cln;
1992 }
1993#ifdef FEAT_DIFF
1994 loff.fill = 0;
1995 boff.fill = 0;
1996 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1997 - curwin->w_filler_rows;
1998#endif
1999
2000 while (loff.lnum > 1)
2001 {
2002 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
2003 * context for 'scrolloff' and counted all lines below the window. */
2004 if ((((scrolled <= 0 || scrolled >= min_scroll)
2005 && extra >= (
2006#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002007 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002009 so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2011 && loff.lnum <= curwin->w_botline
2012#ifdef FEAT_DIFF
2013 && (loff.lnum < curwin->w_botline
2014 || loff.fill >= fill_below_window)
2015#endif
2016 )
2017 break;
2018
2019 /* Add one line above */
2020 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002021 if (loff.height == MAXCOL)
2022 used = MAXCOL;
2023 else
2024 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (used > curwin->w_height)
2026 break;
2027 if (loff.lnum >= curwin->w_botline
2028#ifdef FEAT_DIFF
2029 && (loff.lnum > curwin->w_botline
2030 || loff.fill <= fill_below_window)
2031#endif
2032 )
2033 {
2034 /* Count screen lines that are below the window. */
2035 scrolled += loff.height;
2036 if (loff.lnum == curwin->w_botline
2037#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002038 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040 )
2041 scrolled -= curwin->w_empty_rows;
2042 }
2043
2044 if (boff.lnum < curbuf->b_ml.ml_line_count)
2045 {
2046 /* Add one line below */
2047 botline_forw(&boff);
2048 used += boff.height;
2049 if (used > curwin->w_height)
2050 break;
2051 if (extra < (
2052#ifdef FEAT_MOUSE
2053 mouse_dragging > 0 ? mouse_dragging - 1 :
2054#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002055 so) || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 extra += boff.height;
2058 if (boff.lnum >= curwin->w_botline
2059#ifdef FEAT_DIFF
2060 || (boff.lnum + 1 == curwin->w_botline
2061 && boff.fill > curwin->w_filler_rows)
2062#endif
2063 )
2064 {
2065 /* Count screen lines that are below the window. */
2066 scrolled += boff.height;
2067 if (boff.lnum == curwin->w_botline
2068#ifdef FEAT_DIFF
2069 && boff.fill == 0
2070#endif
2071 )
2072 scrolled -= curwin->w_empty_rows;
2073 }
2074 }
2075 }
2076 }
2077
2078 /* curwin->w_empty_rows is larger, no need to scroll */
2079 if (scrolled <= 0)
2080 line_count = 0;
2081 /* more than a screenfull, don't scroll but redraw */
2082 else if (used > curwin->w_height)
2083 line_count = used;
2084 /* scroll minimal number of lines */
2085 else
2086 {
2087 line_count = 0;
2088#ifdef FEAT_DIFF
2089 boff.fill = curwin->w_topfill;
2090#endif
2091 boff.lnum = curwin->w_topline - 1;
2092 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2093 {
2094 botline_forw(&boff);
2095 i += boff.height;
2096 ++line_count;
2097 }
2098 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2099 line_count = 9999;
2100 }
2101
2102 /*
2103 * Scroll up if the cursor is off the bottom of the screen a bit.
2104 * Otherwise put it at 1/2 of the screen.
2105 */
2106 if (line_count >= curwin->w_height && line_count > min_scroll)
2107 scroll_cursor_halfway(FALSE);
2108 else
2109 scrollup(line_count, TRUE);
2110
2111 /*
2112 * If topline didn't change we need to restore w_botline and w_empty_rows
2113 * (we changed them).
2114 * If topline did change, update_screen() will set botline.
2115 */
2116 if (curwin->w_topline == old_topline && set_topbot)
2117 {
2118 curwin->w_botline = old_botline;
2119 curwin->w_empty_rows = old_empty_rows;
2120 curwin->w_valid = old_valid;
2121 }
2122 curwin->w_valid |= VALID_TOPLINE;
2123}
2124
2125/*
2126 * Recompute topline to put the cursor halfway the window
2127 * If "atend" is TRUE, also put it halfway at the end of the file.
2128 */
2129 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 int above = 0;
2133 linenr_T topline;
2134#ifdef FEAT_DIFF
2135 int topfill = 0;
2136#endif
2137 int below = 0;
2138 int used;
2139 lineoff_T loff;
2140 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002141#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002142 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2146#ifdef FEAT_FOLDING
2147 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2148#endif
2149#ifdef FEAT_DIFF
2150 used = plines_nofill(loff.lnum);
2151 loff.fill = 0;
2152 boff.fill = 0;
2153#else
2154 used = plines(loff.lnum);
2155#endif
2156 topline = loff.lnum;
2157 while (topline > 1)
2158 {
2159 if (below <= above) /* add a line below the cursor first */
2160 {
2161 if (boff.lnum < curbuf->b_ml.ml_line_count)
2162 {
2163 botline_forw(&boff);
2164 used += boff.height;
2165 if (used > curwin->w_height)
2166 break;
2167 below += boff.height;
2168 }
2169 else
2170 {
2171 ++below; /* count a "~" line */
2172 if (atend)
2173 ++used;
2174 }
2175 }
2176
2177 if (below > above) /* add a line above the cursor */
2178 {
2179 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002180 if (loff.height == MAXCOL)
2181 used = MAXCOL;
2182 else
2183 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (used > curwin->w_height)
2185 break;
2186 above += loff.height;
2187 topline = loff.lnum;
2188#ifdef FEAT_DIFF
2189 topfill = loff.fill;
2190#endif
2191 }
2192 }
2193#ifdef FEAT_FOLDING
2194 if (!hasFolding(topline, &curwin->w_topline, NULL))
2195#endif
2196 curwin->w_topline = topline;
2197#ifdef FEAT_DIFF
2198 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002199 if (old_topline > curwin->w_topline + curwin->w_height)
2200 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 check_topfill(curwin, FALSE);
2202#endif
2203 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2204 curwin->w_valid |= VALID_TOPLINE;
2205}
2206
2207/*
2208 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002209 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * If not possible, put it at the same position as scroll_cursor_halfway().
2211 * When called topline must be valid!
2212 */
2213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002214cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int above = 0; /* screen lines above topline */
2217 linenr_T topline;
2218 int below = 0; /* screen lines below botline */
2219 linenr_T botline;
2220 int above_wanted, below_wanted;
2221 linenr_T cln; /* Cursor Line Number */
2222 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002223 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 /*
2226 * How many lines we would like to have above/below the cursor depends on
2227 * whether the first/last line of the file is on screen.
2228 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002229 above_wanted = so;
2230 below_wanted = so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002232 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
2234 above_wanted = mouse_dragging - 1;
2235 below_wanted = mouse_dragging - 1;
2236 }
2237#endif
2238 if (curwin->w_topline == 1)
2239 {
2240 above_wanted = 0;
2241 max_off = curwin->w_height / 2;
2242 if (below_wanted > max_off)
2243 below_wanted = max_off;
2244 }
2245 validate_botline();
2246 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2247#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002248 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250 )
2251 {
2252 below_wanted = 0;
2253 max_off = (curwin->w_height - 1) / 2;
2254 if (above_wanted > max_off)
2255 above_wanted = max_off;
2256 }
2257
2258 /*
2259 * If there are sufficient file-lines above and below the cursor, we can
2260 * return now.
2261 */
2262 cln = curwin->w_cursor.lnum;
2263 if (cln >= curwin->w_topline + above_wanted
2264 && cln < curwin->w_botline - below_wanted
2265#ifdef FEAT_FOLDING
2266 && !hasAnyFolding(curwin)
2267#endif
2268 )
2269 return;
2270
2271 /*
2272 * Narrow down the area where the cursor can be put by taking lines from
2273 * the top and the bottom until:
2274 * - the desired context lines are found
2275 * - the lines from the top is past the lines from the bottom
2276 */
2277 topline = curwin->w_topline;
2278 botline = curwin->w_botline - 1;
2279#ifdef FEAT_DIFF
2280 /* count filler lines as context */
2281 above = curwin->w_topfill;
2282 below = curwin->w_filler_rows;
2283#endif
2284 while ((above < above_wanted || below < below_wanted) && topline < botline)
2285 {
2286 if (below < below_wanted && (below <= above || above >= above_wanted))
2287 {
2288#ifdef FEAT_FOLDING
2289 if (hasFolding(botline, &botline, NULL))
2290 ++below;
2291 else
2292#endif
2293 below += plines(botline);
2294 --botline;
2295 }
2296 if (above < above_wanted && (above < below || below >= below_wanted))
2297 {
2298#ifdef FEAT_FOLDING
2299 if (hasFolding(topline, NULL, &topline))
2300 ++above;
2301 else
2302#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002303 above += PLINES_NOFILL(topline);
2304#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 /* Count filler lines below this line as context. */
2306 if (topline < botline)
2307 above += diff_check_fill(curwin, topline + 1);
2308#endif
2309 ++topline;
2310 }
2311 }
2312 if (topline == botline || botline == 0)
2313 curwin->w_cursor.lnum = topline;
2314 else if (topline > botline)
2315 curwin->w_cursor.lnum = botline;
2316 else
2317 {
2318 if (cln < topline && curwin->w_topline > 1)
2319 {
2320 curwin->w_cursor.lnum = topline;
2321 curwin->w_valid &=
2322 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2323 }
2324 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2325 {
2326 curwin->w_cursor.lnum = botline;
2327 curwin->w_valid &=
2328 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2329 }
2330 }
2331 curwin->w_valid |= VALID_TOPLINE;
2332}
2333
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002334static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336/*
2337 * move screen 'count' pages up or down and update screen
2338 *
2339 * return FAIL for failure, OK otherwise
2340 */
2341 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002342onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 long n;
2345 int retval = OK;
2346 lineoff_T loff;
2347 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002348 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2351 {
2352 beep_flush();
2353 return FAIL;
2354 }
2355
2356 for ( ; count > 0; --count)
2357 {
2358 validate_botline();
2359 /*
2360 * It's an error to move a page up when the first line is already on
2361 * the screen. It's an error to move a page down when the last line
2362 * is on the screen and the topline is 'scrolloff' lines from the
2363 * last line.
2364 */
2365 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002366 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2368 : (curwin->w_topline == 1
2369#ifdef FEAT_DIFF
2370 && curwin->w_topfill ==
2371 diff_check_fill(curwin, curwin->w_topline)
2372#endif
2373 ))
2374 {
2375 beep_flush();
2376 retval = FAIL;
2377 break;
2378 }
2379
2380#ifdef FEAT_DIFF
2381 loff.fill = 0;
2382#endif
2383 if (dir == FORWARD)
2384 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002385 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002387 /* Vi compatible scrolling */
2388 if (p_window <= 2)
2389 ++curwin->w_topline;
2390 else
2391 curwin->w_topline += p_window - 2;
2392 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2393 curwin->w_topline = curbuf->b_ml.ml_line_count;
2394 curwin->w_cursor.lnum = curwin->w_topline;
2395 }
2396 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2397 {
2398 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 curwin->w_topline = curbuf->b_ml.ml_line_count;
2400#ifdef FEAT_DIFF
2401 curwin->w_topfill = 0;
2402#endif
2403 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2404 }
2405 else
2406 {
2407 /* For the overlap, start with the line just below the window
2408 * and go upwards. */
2409 loff.lnum = curwin->w_botline;
2410#ifdef FEAT_DIFF
2411 loff.fill = diff_check_fill(curwin, loff.lnum)
2412 - curwin->w_filler_rows;
2413#endif
2414 get_scroll_overlap(&loff, -1);
2415 curwin->w_topline = loff.lnum;
2416#ifdef FEAT_DIFF
2417 curwin->w_topfill = loff.fill;
2418 check_topfill(curwin, FALSE);
2419#endif
2420 curwin->w_cursor.lnum = curwin->w_topline;
2421 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2422 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2423 }
2424 }
2425 else /* dir == BACKWARDS */
2426 {
2427#ifdef FEAT_DIFF
2428 if (curwin->w_topline == 1)
2429 {
2430 /* Include max number of filler lines */
2431 max_topfill();
2432 continue;
2433 }
2434#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002435 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002436 {
2437 /* Vi compatible scrolling (sort of) */
2438 if (p_window <= 2)
2439 --curwin->w_topline;
2440 else
2441 curwin->w_topline -= p_window - 2;
2442 if (curwin->w_topline < 1)
2443 curwin->w_topline = 1;
2444 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2445 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2446 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2447 continue;
2448 }
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 /* Find the line at the top of the window that is going to be the
2451 * line at the bottom of the window. Make sure this results in
2452 * the same line as before doing CTRL-F. */
2453 loff.lnum = curwin->w_topline - 1;
2454#ifdef FEAT_DIFF
2455 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2456 - curwin->w_topfill;
2457#endif
2458 get_scroll_overlap(&loff, 1);
2459
2460 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2461 {
2462 loff.lnum = curbuf->b_ml.ml_line_count;
2463#ifdef FEAT_DIFF
2464 loff.fill = 0;
2465 }
2466 else
2467 {
2468 botline_topline(&loff);
2469#endif
2470 }
2471 curwin->w_cursor.lnum = loff.lnum;
2472
2473 /* Find the line just above the new topline to get the right line
2474 * at the bottom of the window. */
2475 n = 0;
2476 while (n <= curwin->w_height && loff.lnum >= 1)
2477 {
2478 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002479 if (loff.height == MAXCOL)
2480 n = MAXCOL;
2481 else
2482 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002484 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 curwin->w_topline = 1;
2487#ifdef FEAT_DIFF
2488 max_topfill();
2489#endif
2490 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2491 }
2492 else
2493 {
2494 /* Go two lines forward again. */
2495#ifdef FEAT_DIFF
2496 topline_botline(&loff);
2497#endif
2498 botline_forw(&loff);
2499 botline_forw(&loff);
2500#ifdef FEAT_DIFF
2501 botline_topline(&loff);
2502#endif
2503#ifdef FEAT_FOLDING
2504 /* We're at the wrong end of a fold now. */
2505 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2506#endif
2507
2508 /* Always scroll at least one line. Avoid getting stuck on
2509 * very long lines. */
2510 if (loff.lnum >= curwin->w_topline
2511#ifdef FEAT_DIFF
2512 && (loff.lnum > curwin->w_topline
2513 || loff.fill >= curwin->w_topfill)
2514#endif
2515 )
2516 {
2517#ifdef FEAT_DIFF
2518 /* First try using the maximum number of filler lines. If
2519 * that's not enough, backup one line. */
2520 loff.fill = curwin->w_topfill;
2521 if (curwin->w_topfill < diff_check_fill(curwin,
2522 curwin->w_topline))
2523 max_topfill();
2524 if (curwin->w_topfill == loff.fill)
2525#endif
2526 {
2527 --curwin->w_topline;
2528#ifdef FEAT_DIFF
2529 curwin->w_topfill = 0;
2530#endif
2531 }
2532 comp_botline(curwin);
2533 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002534 curwin->w_valid &=
2535 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 else
2538 {
2539 curwin->w_topline = loff.lnum;
2540#ifdef FEAT_DIFF
2541 curwin->w_topfill = loff.fill;
2542 check_topfill(curwin, FALSE);
2543#endif
2544 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2545 }
2546 }
2547 }
2548 }
2549#ifdef FEAT_FOLDING
2550 foldAdjustCursor();
2551#endif
2552 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002553 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002554 if (retval == OK)
2555 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2557
Bram Moolenaar907dad72018-07-10 15:07:15 +02002558 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002560 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2561 // But make sure we scroll at least one line (happens with mix of long
2562 // wrapping lines and non-wrapping line).
2563 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002565 scroll_cursor_top(1, FALSE);
2566 if (curwin->w_topline <= old_topline
2567 && old_topline < curbuf->b_ml.ml_line_count)
2568 {
2569 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002571 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2572#endif
2573 }
2574 }
2575#ifdef FEAT_FOLDING
2576 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
2581 redraw_later(VALID);
2582 return retval;
2583}
2584
2585/*
2586 * Decide how much overlap to use for page-up or page-down scrolling.
2587 * This is symmetric, so that doing both keeps the same lines displayed.
2588 * Three lines are examined:
2589 *
2590 * before CTRL-F after CTRL-F / before CTRL-B
2591 * etc. l1
2592 * l1 last but one line ------------
2593 * l2 last text line l2 top text line
2594 * ------------- l3 second text line
2595 * l3 etc.
2596 */
2597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002598get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 int h1, h2, h3, h4;
2601 int min_height = curwin->w_height - 2;
2602 lineoff_T loff0, loff1, loff2;
2603
2604#ifdef FEAT_DIFF
2605 if (lp->fill > 0)
2606 lp->height = 1;
2607 else
2608 lp->height = plines_nofill(lp->lnum);
2609#else
2610 lp->height = plines(lp->lnum);
2611#endif
2612 h1 = lp->height;
2613 if (h1 > min_height)
2614 return; /* no overlap */
2615
2616 loff0 = *lp;
2617 if (dir > 0)
2618 botline_forw(lp);
2619 else
2620 topline_back(lp);
2621 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002622 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
2624 *lp = loff0; /* no overlap */
2625 return;
2626 }
2627
2628 loff1 = *lp;
2629 if (dir > 0)
2630 botline_forw(lp);
2631 else
2632 topline_back(lp);
2633 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002634 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 *lp = loff0; /* no overlap */
2637 return;
2638 }
2639
2640 loff2 = *lp;
2641 if (dir > 0)
2642 botline_forw(lp);
2643 else
2644 topline_back(lp);
2645 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002646 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 *lp = loff1; /* 1 line overlap */
2648 else
2649 *lp = loff2; /* 2 lines overlap */
2650 return;
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
2654 * Scroll 'scroll' lines up or down.
2655 */
2656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002657halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 long scrolled = 0;
2660 int i;
2661 int n;
2662 int room;
2663
2664 if (Prenum)
2665 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2666 curwin->w_height : Prenum;
2667 n = (curwin->w_p_scr <= curwin->w_height) ?
2668 curwin->w_p_scr : curwin->w_height;
2669
Bram Moolenaard5d37532017-03-27 23:02:07 +02002670 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 validate_botline();
2672 room = curwin->w_empty_rows;
2673#ifdef FEAT_DIFF
2674 room += curwin->w_filler_rows;
2675#endif
2676 if (flag)
2677 {
2678 /*
2679 * scroll the text up
2680 */
2681 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2682 {
2683#ifdef FEAT_DIFF
2684 if (curwin->w_topfill > 0)
2685 {
2686 i = 1;
2687 if (--n < 0 && scrolled > 0)
2688 break;
2689 --curwin->w_topfill;
2690 }
2691 else
2692#endif
2693 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002694 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 n -= i;
2696 if (n < 0 && scrolled > 0)
2697 break;
2698#ifdef FEAT_FOLDING
2699 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2700#endif
2701 ++curwin->w_topline;
2702#ifdef FEAT_DIFF
2703 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2704#endif
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2707 {
2708 ++curwin->w_cursor.lnum;
2709 curwin->w_valid &=
2710 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2714 scrolled += i;
2715
2716 /*
2717 * Correct w_botline for changed w_topline.
2718 * Won't work when there are filler lines.
2719 */
2720#ifdef FEAT_DIFF
2721 if (curwin->w_p_diff)
2722 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2723 else
2724#endif
2725 {
2726 room += i;
2727 do
2728 {
2729 i = plines(curwin->w_botline);
2730 if (i > room)
2731 break;
2732#ifdef FEAT_FOLDING
2733 (void)hasFolding(curwin->w_botline, NULL,
2734 &curwin->w_botline);
2735#endif
2736 ++curwin->w_botline;
2737 room -= i;
2738 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2739 }
2740 }
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /*
2743 * When hit bottom of the file: move cursor down.
2744 */
2745 if (n > 0)
2746 {
2747# ifdef FEAT_FOLDING
2748 if (hasAnyFolding(curwin))
2749 {
2750 while (--n >= 0
2751 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2752 {
2753 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2754 &curwin->w_cursor.lnum);
2755 ++curwin->w_cursor.lnum;
2756 }
2757 }
2758 else
2759# endif
2760 curwin->w_cursor.lnum += n;
2761 check_cursor_lnum();
2762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 else
2765 {
2766 /*
2767 * scroll the text down
2768 */
2769 while (n > 0 && curwin->w_topline > 1)
2770 {
2771#ifdef FEAT_DIFF
2772 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2773 {
2774 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002775 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 ++curwin->w_topfill;
2777 }
2778 else
2779#endif
2780 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002781 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 n -= i;
2783 if (n < 0 && scrolled > 0)
2784 break;
2785 --curwin->w_topline;
2786#ifdef FEAT_FOLDING
2787 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2788#endif
2789#ifdef FEAT_DIFF
2790 curwin->w_topfill = 0;
2791#endif
2792 }
2793 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2794 VALID_BOTLINE|VALID_BOTLINE_AP);
2795 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (curwin->w_cursor.lnum > 1)
2797 {
2798 --curwin->w_cursor.lnum;
2799 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 /*
2804 * When hit top of the file: move cursor up.
2805 */
2806 if (n > 0)
2807 {
2808 if (curwin->w_cursor.lnum <= (linenr_T)n)
2809 curwin->w_cursor.lnum = 1;
2810 else
2811# ifdef FEAT_FOLDING
2812 if (hasAnyFolding(curwin))
2813 {
2814 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2815 {
2816 --curwin->w_cursor.lnum;
2817 (void)hasFolding(curwin->w_cursor.lnum,
2818 &curwin->w_cursor.lnum, NULL);
2819 }
2820 }
2821 else
2822# endif
2823 curwin->w_cursor.lnum -= n;
2824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826# ifdef FEAT_FOLDING
2827 /* Move cursor to first line of closed fold. */
2828 foldAdjustCursor();
2829# endif
2830#ifdef FEAT_DIFF
2831 check_topfill(curwin, !flag);
2832#endif
2833 cursor_correct();
2834 beginline(BL_SOL | BL_FIX);
2835 redraw_later(VALID);
2836}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837
Bram Moolenaar860cae12010-06-05 23:22:07 +02002838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002839do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840{
2841 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002842 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002843 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002844 colnr_T curswant = curwin->w_curswant;
2845 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 win_T *old_curwin = curwin;
2847 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002848 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849 int old_VIsual_select = VIsual_select;
2850 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002851
2852 /*
2853 * loop through the cursorbound windows
2854 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002855 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002856 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002857 {
2858 curbuf = curwin->w_buffer;
2859 /* skip original window and windows with 'noscrollbind' */
2860 if (curwin != old_curwin && curwin->w_p_crb)
2861 {
2862# ifdef FEAT_DIFF
2863 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002864 curwin->w_cursor.lnum =
2865 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002866 else
2867# endif
2868 curwin->w_cursor.lnum = line;
2869 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002870 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002871 curwin->w_curswant = curswant;
2872 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873
Bram Moolenaar61452852011-02-01 18:01:11 +01002874 /* Make sure the cursor is in a valid position. Temporarily set
2875 * "restart_edit" to allow the cursor to be beyond the EOL. */
2876 restart_edit_save = restart_edit;
2877 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002879# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002880 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002881 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002882# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002883 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002884 /* Correct cursor for multi-byte character. */
2885 if (has_mbyte)
2886 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002887 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002888
2889 /* Only scroll when 'scrollbind' hasn't done this. */
2890 if (!curwin->w_p_scb)
2891 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002893 }
2894 }
2895
2896 /*
2897 * reset current-window
2898 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 VIsual_select = old_VIsual_select;
2900 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 curwin = old_curwin;
2902 curbuf = old_curbuf;
2903}