blob: 223ed5417e4ef21545df0d81a212e774109e02b3 [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 Moolenaar12034e22019-08-25 22:25:02 +02001182#if (defined(FEAT_EVAL) || defined(FEAT_TEXT_PROP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001183/*
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 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001187 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001188textpos2screenpos(
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
Bram Moolenaar12034e22019-08-25 22:25:02 +02001216 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001217 if (wp->w_p_wrap
1218 && col >= (colnr_T)wp->w_width
1219 && width > 0)
1220 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001221 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001222 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}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001239#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001240
Bram Moolenaar12034e22019-08-25 22:25:02 +02001241#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001242/*
1243 * "screenpos({winid}, {lnum}, {col})" function
1244 */
1245 void
1246f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1247{
1248 dict_T *dict;
1249 win_T *wp;
1250 pos_T pos;
1251 int row = 0;
1252 int scol = 0, ccol = 0, ecol = 0;
1253
1254 if (rettv_dict_alloc(rettv) != OK)
1255 return;
1256 dict = rettv->vval.v_dict;
1257
1258 wp = find_win_by_nr_or_id(&argvars[0]);
1259 if (wp == NULL)
1260 return;
1261
1262 pos.lnum = tv_get_number(&argvars[1]);
1263 pos.col = tv_get_number(&argvars[2]) - 1;
1264 pos.coladd = 0;
1265 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1266
1267 dict_add_number(dict, "row", row);
1268 dict_add_number(dict, "col", scol);
1269 dict_add_number(dict, "curscol", ccol);
1270 dict_add_number(dict, "endcol", ecol);
1271}
1272#endif
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
1275 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001278scrolldown(
1279 long line_count,
1280 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 long done = 0; /* total # of physical lines done */
1283 int wrow;
1284 int moved = FALSE;
1285
1286#ifdef FEAT_FOLDING
1287 linenr_T first;
1288
1289 /* Make sure w_topline is at the first of a sequence of folded lines. */
1290 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1291#endif
1292 validate_cursor(); /* w_wrow needs to be valid */
1293 while (line_count-- > 0)
1294 {
1295#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001296 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1297 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 ++curwin->w_topfill;
1300 ++done;
1301 }
1302 else
1303#endif
1304 {
1305 if (curwin->w_topline == 1)
1306 break;
1307 --curwin->w_topline;
1308#ifdef FEAT_DIFF
1309 curwin->w_topfill = 0;
1310#endif
1311#ifdef FEAT_FOLDING
1312 /* A sequence of folded lines only counts for one logical line */
1313 if (hasFolding(curwin->w_topline, &first, NULL))
1314 {
1315 ++done;
1316 if (!byfold)
1317 line_count -= curwin->w_topline - first - 1;
1318 curwin->w_botline -= curwin->w_topline - first;
1319 curwin->w_topline = first;
1320 }
1321 else
1322#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001323 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 --curwin->w_botline; /* approximate w_botline */
1326 invalidate_botline();
1327 }
1328 curwin->w_wrow += done; /* keep w_wrow updated */
1329 curwin->w_cline_row += done; /* keep w_cline_row updated */
1330
1331#ifdef FEAT_DIFF
1332 if (curwin->w_cursor.lnum == curwin->w_topline)
1333 curwin->w_cline_row = 0;
1334 check_topfill(curwin, TRUE);
1335#endif
1336
1337 /*
1338 * Compute the row number of the last row of the cursor line
1339 * and move the cursor onto the displayed part of the window.
1340 */
1341 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001342 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
1344 validate_virtcol();
1345 validate_cheight();
1346 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001347 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1350 {
1351#ifdef FEAT_FOLDING
1352 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1353 {
1354 --wrow;
1355 if (first == 1)
1356 curwin->w_cursor.lnum = 1;
1357 else
1358 curwin->w_cursor.lnum = first - 1;
1359 }
1360 else
1361#endif
1362 wrow -= plines(curwin->w_cursor.lnum--);
1363 curwin->w_valid &=
1364 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1365 moved = TRUE;
1366 }
1367 if (moved)
1368 {
1369#ifdef FEAT_FOLDING
1370 /* Move cursor to first line of closed fold. */
1371 foldAdjustCursor();
1372#endif
1373 coladvance(curwin->w_curswant);
1374 }
1375}
1376
1377/*
1378 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001381scrollup(
1382 long line_count,
1383 int byfold UNUSED) /* TRUE: count a closed fold as one line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1386 linenr_T lnum;
1387
1388 if (
1389# ifdef FEAT_FOLDING
1390 (byfold && hasAnyFolding(curwin))
1391# ifdef FEAT_DIFF
1392 ||
1393# endif
1394# endif
1395# ifdef FEAT_DIFF
1396 curwin->w_p_diff
1397# endif
1398 )
1399 {
1400 /* count each sequence of folded lines as one logical line */
1401 lnum = curwin->w_topline;
1402 while (line_count--)
1403 {
1404# ifdef FEAT_DIFF
1405 if (curwin->w_topfill > 0)
1406 --curwin->w_topfill;
1407 else
1408# endif
1409 {
1410# ifdef FEAT_FOLDING
1411 if (byfold)
1412 (void)hasFolding(lnum, NULL, &lnum);
1413# endif
1414 if (lnum >= curbuf->b_ml.ml_line_count)
1415 break;
1416 ++lnum;
1417# ifdef FEAT_DIFF
1418 curwin->w_topfill = diff_check_fill(curwin, lnum);
1419# endif
1420 }
1421 }
1422 /* approximate w_botline */
1423 curwin->w_botline += lnum - curwin->w_topline;
1424 curwin->w_topline = lnum;
1425 }
1426 else
1427#endif
1428 {
1429 curwin->w_topline += line_count;
1430 curwin->w_botline += line_count; /* approximate w_botline */
1431 }
1432
1433 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1434 curwin->w_topline = curbuf->b_ml.ml_line_count;
1435 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1436 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1437
1438#ifdef FEAT_DIFF
1439 check_topfill(curwin, FALSE);
1440#endif
1441
1442#ifdef FEAT_FOLDING
1443 if (hasAnyFolding(curwin))
1444 /* Make sure w_topline is at the first of a sequence of folded lines. */
1445 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1446#endif
1447
1448 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1449 if (curwin->w_cursor.lnum < curwin->w_topline)
1450 {
1451 curwin->w_cursor.lnum = curwin->w_topline;
1452 curwin->w_valid &=
1453 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1454 coladvance(curwin->w_curswant);
1455 }
1456}
1457
1458#ifdef FEAT_DIFF
1459/*
1460 * Don't end up with too many filler lines in the window.
1461 */
1462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001463check_topfill(
1464 win_T *wp,
1465 int down) /* when TRUE scroll down when not enough space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 int n;
1468
1469 if (wp->w_topfill > 0)
1470 {
1471 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1472 if (wp->w_topfill + n > wp->w_height)
1473 {
1474 if (down && wp->w_topline > 1)
1475 {
1476 --wp->w_topline;
1477 wp->w_topfill = 0;
1478 }
1479 else
1480 {
1481 wp->w_topfill = wp->w_height - n;
1482 if (wp->w_topfill < 0)
1483 wp->w_topfill = 0;
1484 }
1485 }
1486 }
1487}
1488
1489/*
1490 * Use as many filler lines as possible for w_topline. Make sure w_topline
1491 * is still visible.
1492 */
1493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001494max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 int n;
1497
1498 n = plines_nofill(curwin->w_topline);
1499 if (n >= curwin->w_height)
1500 curwin->w_topfill = 0;
1501 else
1502 {
1503 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1504 if (curwin->w_topfill + n > curwin->w_height)
1505 curwin->w_topfill = curwin->w_height - n;
1506 }
1507}
1508#endif
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
1511 * Scroll the screen one line down, but don't do it if it would move the
1512 * cursor off the screen.
1513 */
1514 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001515scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 int end_row;
1518#ifdef FEAT_DIFF
1519 int can_fill = (curwin->w_topfill
1520 < diff_check_fill(curwin, curwin->w_topline));
1521#endif
1522
1523 if (curwin->w_topline <= 1
1524#ifdef FEAT_DIFF
1525 && !can_fill
1526#endif
1527 )
1528 return;
1529
1530 validate_cursor(); /* w_wrow needs to be valid */
1531
1532 /*
1533 * Compute the row number of the last row of the cursor line
1534 * and make sure it doesn't go off the screen. Make sure the cursor
1535 * doesn't go past 'scrolloff' lines from the screen end.
1536 */
1537 end_row = curwin->w_wrow;
1538#ifdef FEAT_DIFF
1539 if (can_fill)
1540 ++end_row;
1541 else
1542 end_row += plines_nofill(curwin->w_topline - 1);
1543#else
1544 end_row += plines(curwin->w_topline - 1);
1545#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001546 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548 validate_cheight();
1549 validate_virtcol();
1550 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001551 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001553 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555#ifdef FEAT_DIFF
1556 if (can_fill)
1557 {
1558 ++curwin->w_topfill;
1559 check_topfill(curwin, TRUE);
1560 }
1561 else
1562 {
1563 --curwin->w_topline;
1564 curwin->w_topfill = 0;
1565 }
1566#else
1567 --curwin->w_topline;
1568#endif
1569#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001570 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#endif
1572 --curwin->w_botline; /* approximate w_botline */
1573 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1574 }
1575}
1576
1577/*
1578 * Scroll the screen one line up, but don't do it if it would move the cursor
1579 * off the screen.
1580 */
1581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001582scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 int start_row;
1585
1586 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1587#ifdef FEAT_DIFF
1588 && curwin->w_topfill == 0
1589#endif
1590 )
1591 return;
1592
1593 validate_cursor(); /* w_wrow needs to be valid */
1594
1595 /*
1596 * Compute the row number of the first row of the cursor line
1597 * and make sure it doesn't go off the screen. Make sure the cursor
1598 * doesn't go before 'scrolloff' lines from the screen start.
1599 */
1600#ifdef FEAT_DIFF
1601 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1602 - curwin->w_topfill;
1603#else
1604 start_row = curwin->w_wrow - plines(curwin->w_topline);
1605#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001606 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001609 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001611 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613#ifdef FEAT_DIFF
1614 if (curwin->w_topfill > 0)
1615 --curwin->w_topfill;
1616 else
1617#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001618 {
1619#ifdef FEAT_FOLDING
1620 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 ++curwin->w_botline; /* approximate w_botline */
1625 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1626 }
1627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629/*
1630 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1631 * a (wrapped) text line. Uses and sets "lp->fill".
1632 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001633 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
1635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001636topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638#ifdef FEAT_DIFF
1639 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1640 {
1641 /* Add a filler line. */
1642 ++lp->fill;
1643 lp->height = 1;
1644 }
1645 else
1646#endif
1647 {
1648 --lp->lnum;
1649#ifdef FEAT_DIFF
1650 lp->fill = 0;
1651#endif
1652 if (lp->lnum < 1)
1653 lp->height = MAXCOL;
1654 else
1655#ifdef FEAT_FOLDING
1656 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1657 /* Add a closed fold */
1658 lp->height = 1;
1659 else
1660#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001661 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663}
1664
1665/*
1666 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1667 * a (wrapped) text line. Uses and sets "lp->fill".
1668 * Returns the height of the added line in "lp->height".
1669 * Lines below the last one are incredibly high.
1670 */
1671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001672botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674#ifdef FEAT_DIFF
1675 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1676 {
1677 /* Add a filler line. */
1678 ++lp->fill;
1679 lp->height = 1;
1680 }
1681 else
1682#endif
1683 {
1684 ++lp->lnum;
1685#ifdef FEAT_DIFF
1686 lp->fill = 0;
1687#endif
1688 if (lp->lnum > curbuf->b_ml.ml_line_count)
1689 lp->height = MAXCOL;
1690 else
1691#ifdef FEAT_FOLDING
1692 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1693 /* Add a closed fold */
1694 lp->height = 1;
1695 else
1696#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001697 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699}
1700
1701#ifdef FEAT_DIFF
1702/*
1703 * Switch from including filler lines below lp->lnum to including filler
1704 * lines above loff.lnum + 1. This keeps pointing to the same line.
1705 * When there are no filler lines nothing changes.
1706 */
1707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001708botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 if (lp->fill > 0)
1711 {
1712 ++lp->lnum;
1713 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1714 }
1715}
1716
1717/*
1718 * Switch from including filler lines above lp->lnum to including filler
1719 * lines below loff.lnum - 1. This keeps pointing to the same line.
1720 * When there are no filler lines nothing changes.
1721 */
1722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001723topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 if (lp->fill > 0)
1726 {
1727 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1728 --lp->lnum;
1729 }
1730}
1731#endif
1732
1733/*
1734 * Recompute topline to put the cursor at the top of the window.
1735 * Scroll at least "min_scroll" lines.
1736 * If "always" is TRUE, always set topline (for "zt").
1737 */
1738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 int scrolled = 0;
1742 int extra = 0;
1743 int used;
1744 int i;
1745 linenr_T top; /* just above displayed lines */
1746 linenr_T bot; /* just below displayed lines */
1747 linenr_T old_topline = curwin->w_topline;
1748#ifdef FEAT_DIFF
1749 linenr_T old_topfill = curwin->w_topfill;
1750#endif
1751 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001752 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754#ifdef FEAT_MOUSE
1755 if (mouse_dragging > 0)
1756 off = mouse_dragging - 1;
1757#endif
1758
1759 /*
1760 * Decrease topline until:
1761 * - it has become 1
1762 * - (part of) the cursor line is moved off the screen or
1763 * - moved at least 'scrolljump' lines and
1764 * - at least 'scrolloff' lines above and below the cursor
1765 */
1766 validate_cheight();
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001767 used = curwin->w_cline_height; /* includes filler lines above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (curwin->w_cursor.lnum < curwin->w_topline)
1769 scrolled = used;
1770
1771#ifdef FEAT_FOLDING
1772 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1773 {
1774 --top;
1775 ++bot;
1776 }
1777 else
1778#endif
1779 {
1780 top = curwin->w_cursor.lnum - 1;
1781 bot = curwin->w_cursor.lnum + 1;
1782 }
1783 new_topline = top + 1;
1784
1785#ifdef FEAT_DIFF
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001786 /* "used" already contains the number of filler lines above, don't add it
Bram Moolenaarcf619da2015-09-01 20:53:24 +02001787 * again.
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001788 * Hide filler lines above cursor line by adding them to "extra". */
1789 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#endif
1791
1792 /*
1793 * Check if the lines from "top" to "bot" fit in the window. If they do,
1794 * set new_topline and advance "top" and "bot" to include more lines.
1795 */
1796 while (top > 0)
1797 {
1798#ifdef FEAT_FOLDING
1799 if (hasFolding(top, &top, NULL))
1800 /* count one logical line for a sequence of folded lines */
1801 i = 1;
1802 else
1803#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001804 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 used += i;
1806 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1807 {
1808#ifdef FEAT_FOLDING
1809 if (hasFolding(bot, NULL, &bot))
1810 /* count one logical line for a sequence of folded lines */
1811 ++used;
1812 else
1813#endif
1814 used += plines(bot);
1815 }
1816 if (used > curwin->w_height)
1817 break;
1818 if (top < curwin->w_topline)
1819 scrolled += i;
1820
1821 /*
1822 * If scrolling is needed, scroll at least 'sj' lines.
1823 */
1824 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1825 && extra >= off)
1826 break;
1827
1828 extra += i;
1829 new_topline = top;
1830 --top;
1831 ++bot;
1832 }
1833
1834 /*
1835 * If we don't have enough space, put cursor in the middle.
1836 * This makes sure we get the same position when using "k" and "j"
1837 * in a small window.
1838 */
1839 if (used > curwin->w_height)
1840 scroll_cursor_halfway(FALSE);
1841 else
1842 {
1843 /*
1844 * If "always" is FALSE, only adjust topline to a lower value, higher
1845 * value may happen with wrapping lines
1846 */
1847 if (new_topline < curwin->w_topline || always)
1848 curwin->w_topline = new_topline;
1849 if (curwin->w_topline > curwin->w_cursor.lnum)
1850 curwin->w_topline = curwin->w_cursor.lnum;
1851#ifdef FEAT_DIFF
1852 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1853 if (curwin->w_topfill > 0 && extra > off)
1854 {
1855 curwin->w_topfill -= extra - off;
1856 if (curwin->w_topfill < 0)
1857 curwin->w_topfill = 0;
1858 }
1859 check_topfill(curwin, FALSE);
1860#endif
1861 if (curwin->w_topline != old_topline
1862#ifdef FEAT_DIFF
1863 || curwin->w_topfill != old_topfill
1864#endif
1865 )
1866 curwin->w_valid &=
1867 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1868 curwin->w_valid |= VALID_TOPLINE;
1869 }
1870}
1871
1872/*
1873 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1874 * screen lines for text lines.
1875 */
1876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001877set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
1879#ifdef FEAT_DIFF
1880 wp->w_filler_rows = 0;
1881#endif
1882 if (used == 0)
1883 wp->w_empty_rows = 0; /* single line that doesn't fit */
1884 else
1885 {
1886 wp->w_empty_rows = wp->w_height - used;
1887#ifdef FEAT_DIFF
1888 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1889 {
1890 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1891 if (wp->w_empty_rows > wp->w_filler_rows)
1892 wp->w_empty_rows -= wp->w_filler_rows;
1893 else
1894 {
1895 wp->w_filler_rows = wp->w_empty_rows;
1896 wp->w_empty_rows = 0;
1897 }
1898 }
1899#endif
1900 }
1901}
1902
1903/*
1904 * Recompute topline to put the cursor at the bottom of the window.
1905 * Scroll at least "min_scroll" lines.
1906 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1907 * This is messy stuff!!!
1908 */
1909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001910scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 int used;
1913 int scrolled = 0;
1914 int extra = 0;
1915 int i;
1916 linenr_T line_count;
1917 linenr_T old_topline = curwin->w_topline;
1918 lineoff_T loff;
1919 lineoff_T boff;
1920#ifdef FEAT_DIFF
1921 int old_topfill = curwin->w_topfill;
1922 int fill_below_window;
1923#endif
1924 linenr_T old_botline = curwin->w_botline;
1925 linenr_T old_valid = curwin->w_valid;
1926 int old_empty_rows = curwin->w_empty_rows;
1927 linenr_T cln; /* Cursor Line Number */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001928 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 cln = curwin->w_cursor.lnum;
1931 if (set_topbot)
1932 {
1933 used = 0;
1934 curwin->w_botline = cln + 1;
1935#ifdef FEAT_DIFF
1936 loff.fill = 0;
1937#endif
1938 for (curwin->w_topline = curwin->w_botline;
1939 curwin->w_topline > 1;
1940 curwin->w_topline = loff.lnum)
1941 {
1942 loff.lnum = curwin->w_topline;
1943 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001944 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 break;
1946 used += loff.height;
1947#ifdef FEAT_DIFF
1948 curwin->w_topfill = loff.fill;
1949#endif
1950 }
1951 set_empty_rows(curwin, used);
1952 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1953 if (curwin->w_topline != old_topline
1954#ifdef FEAT_DIFF
1955 || curwin->w_topfill != old_topfill
1956#endif
1957 )
1958 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1959 }
1960 else
1961 validate_botline();
1962
1963 /* The lines of the cursor line itself are always used. */
1964#ifdef FEAT_DIFF
1965 used = plines_nofill(cln);
1966#else
1967 validate_cheight();
1968 used = curwin->w_cline_height;
1969#endif
1970
1971 /* If the cursor is below botline, we will at least scroll by the height
1972 * of the cursor line. Correct for empty lines, which are really part of
1973 * botline. */
1974 if (cln >= curwin->w_botline)
1975 {
1976 scrolled = used;
1977 if (cln == curwin->w_botline)
1978 scrolled -= curwin->w_empty_rows;
1979 }
1980
1981 /*
1982 * Stop counting lines to scroll when
1983 * - hitting start of the file
1984 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001985 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 * - lines between botline and cursor have been counted
1987 */
1988#ifdef FEAT_FOLDING
1989 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1990#endif
1991 {
1992 loff.lnum = cln;
1993 boff.lnum = cln;
1994 }
1995#ifdef FEAT_DIFF
1996 loff.fill = 0;
1997 boff.fill = 0;
1998 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1999 - curwin->w_filler_rows;
2000#endif
2001
2002 while (loff.lnum > 1)
2003 {
2004 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
2005 * context for 'scrolloff' and counted all lines below the window. */
2006 if ((((scrolled <= 0 || scrolled >= min_scroll)
2007 && extra >= (
2008#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002009 mouse_dragging > 0 ? mouse_dragging - 1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002011 so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2013 && loff.lnum <= curwin->w_botline
2014#ifdef FEAT_DIFF
2015 && (loff.lnum < curwin->w_botline
2016 || loff.fill >= fill_below_window)
2017#endif
2018 )
2019 break;
2020
2021 /* Add one line above */
2022 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002023 if (loff.height == MAXCOL)
2024 used = MAXCOL;
2025 else
2026 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (used > curwin->w_height)
2028 break;
2029 if (loff.lnum >= curwin->w_botline
2030#ifdef FEAT_DIFF
2031 && (loff.lnum > curwin->w_botline
2032 || loff.fill <= fill_below_window)
2033#endif
2034 )
2035 {
2036 /* Count screen lines that are below the window. */
2037 scrolled += loff.height;
2038 if (loff.lnum == curwin->w_botline
2039#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002040 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041#endif
2042 )
2043 scrolled -= curwin->w_empty_rows;
2044 }
2045
2046 if (boff.lnum < curbuf->b_ml.ml_line_count)
2047 {
2048 /* Add one line below */
2049 botline_forw(&boff);
2050 used += boff.height;
2051 if (used > curwin->w_height)
2052 break;
2053 if (extra < (
2054#ifdef FEAT_MOUSE
2055 mouse_dragging > 0 ? mouse_dragging - 1 :
2056#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002057 so) || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 extra += boff.height;
2060 if (boff.lnum >= curwin->w_botline
2061#ifdef FEAT_DIFF
2062 || (boff.lnum + 1 == curwin->w_botline
2063 && boff.fill > curwin->w_filler_rows)
2064#endif
2065 )
2066 {
2067 /* Count screen lines that are below the window. */
2068 scrolled += boff.height;
2069 if (boff.lnum == curwin->w_botline
2070#ifdef FEAT_DIFF
2071 && boff.fill == 0
2072#endif
2073 )
2074 scrolled -= curwin->w_empty_rows;
2075 }
2076 }
2077 }
2078 }
2079
2080 /* curwin->w_empty_rows is larger, no need to scroll */
2081 if (scrolled <= 0)
2082 line_count = 0;
2083 /* more than a screenfull, don't scroll but redraw */
2084 else if (used > curwin->w_height)
2085 line_count = used;
2086 /* scroll minimal number of lines */
2087 else
2088 {
2089 line_count = 0;
2090#ifdef FEAT_DIFF
2091 boff.fill = curwin->w_topfill;
2092#endif
2093 boff.lnum = curwin->w_topline - 1;
2094 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2095 {
2096 botline_forw(&boff);
2097 i += boff.height;
2098 ++line_count;
2099 }
2100 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2101 line_count = 9999;
2102 }
2103
2104 /*
2105 * Scroll up if the cursor is off the bottom of the screen a bit.
2106 * Otherwise put it at 1/2 of the screen.
2107 */
2108 if (line_count >= curwin->w_height && line_count > min_scroll)
2109 scroll_cursor_halfway(FALSE);
2110 else
2111 scrollup(line_count, TRUE);
2112
2113 /*
2114 * If topline didn't change we need to restore w_botline and w_empty_rows
2115 * (we changed them).
2116 * If topline did change, update_screen() will set botline.
2117 */
2118 if (curwin->w_topline == old_topline && set_topbot)
2119 {
2120 curwin->w_botline = old_botline;
2121 curwin->w_empty_rows = old_empty_rows;
2122 curwin->w_valid = old_valid;
2123 }
2124 curwin->w_valid |= VALID_TOPLINE;
2125}
2126
2127/*
2128 * Recompute topline to put the cursor halfway the window
2129 * If "atend" is TRUE, also put it halfway at the end of the file.
2130 */
2131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int above = 0;
2135 linenr_T topline;
2136#ifdef FEAT_DIFF
2137 int topfill = 0;
2138#endif
2139 int below = 0;
2140 int used;
2141 lineoff_T loff;
2142 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002143#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002144 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2148#ifdef FEAT_FOLDING
2149 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2150#endif
2151#ifdef FEAT_DIFF
2152 used = plines_nofill(loff.lnum);
2153 loff.fill = 0;
2154 boff.fill = 0;
2155#else
2156 used = plines(loff.lnum);
2157#endif
2158 topline = loff.lnum;
2159 while (topline > 1)
2160 {
2161 if (below <= above) /* add a line below the cursor first */
2162 {
2163 if (boff.lnum < curbuf->b_ml.ml_line_count)
2164 {
2165 botline_forw(&boff);
2166 used += boff.height;
2167 if (used > curwin->w_height)
2168 break;
2169 below += boff.height;
2170 }
2171 else
2172 {
2173 ++below; /* count a "~" line */
2174 if (atend)
2175 ++used;
2176 }
2177 }
2178
2179 if (below > above) /* add a line above the cursor */
2180 {
2181 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002182 if (loff.height == MAXCOL)
2183 used = MAXCOL;
2184 else
2185 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (used > curwin->w_height)
2187 break;
2188 above += loff.height;
2189 topline = loff.lnum;
2190#ifdef FEAT_DIFF
2191 topfill = loff.fill;
2192#endif
2193 }
2194 }
2195#ifdef FEAT_FOLDING
2196 if (!hasFolding(topline, &curwin->w_topline, NULL))
2197#endif
2198 curwin->w_topline = topline;
2199#ifdef FEAT_DIFF
2200 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002201 if (old_topline > curwin->w_topline + curwin->w_height)
2202 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 check_topfill(curwin, FALSE);
2204#endif
2205 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2206 curwin->w_valid |= VALID_TOPLINE;
2207}
2208
2209/*
2210 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002211 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * If not possible, put it at the same position as scroll_cursor_halfway().
2213 * When called topline must be valid!
2214 */
2215 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 int above = 0; /* screen lines above topline */
2219 linenr_T topline;
2220 int below = 0; /* screen lines below botline */
2221 linenr_T botline;
2222 int above_wanted, below_wanted;
2223 linenr_T cln; /* Cursor Line Number */
2224 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002225 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 /*
2228 * How many lines we would like to have above/below the cursor depends on
2229 * whether the first/last line of the file is on screen.
2230 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002231 above_wanted = so;
2232 below_wanted = so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002234 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 above_wanted = mouse_dragging - 1;
2237 below_wanted = mouse_dragging - 1;
2238 }
2239#endif
2240 if (curwin->w_topline == 1)
2241 {
2242 above_wanted = 0;
2243 max_off = curwin->w_height / 2;
2244 if (below_wanted > max_off)
2245 below_wanted = max_off;
2246 }
2247 validate_botline();
2248 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2249#ifdef FEAT_MOUSE
Bram Moolenaar9964e462007-05-05 17:54:07 +00002250 && mouse_dragging == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252 )
2253 {
2254 below_wanted = 0;
2255 max_off = (curwin->w_height - 1) / 2;
2256 if (above_wanted > max_off)
2257 above_wanted = max_off;
2258 }
2259
2260 /*
2261 * If there are sufficient file-lines above and below the cursor, we can
2262 * return now.
2263 */
2264 cln = curwin->w_cursor.lnum;
2265 if (cln >= curwin->w_topline + above_wanted
2266 && cln < curwin->w_botline - below_wanted
2267#ifdef FEAT_FOLDING
2268 && !hasAnyFolding(curwin)
2269#endif
2270 )
2271 return;
2272
2273 /*
2274 * Narrow down the area where the cursor can be put by taking lines from
2275 * the top and the bottom until:
2276 * - the desired context lines are found
2277 * - the lines from the top is past the lines from the bottom
2278 */
2279 topline = curwin->w_topline;
2280 botline = curwin->w_botline - 1;
2281#ifdef FEAT_DIFF
2282 /* count filler lines as context */
2283 above = curwin->w_topfill;
2284 below = curwin->w_filler_rows;
2285#endif
2286 while ((above < above_wanted || below < below_wanted) && topline < botline)
2287 {
2288 if (below < below_wanted && (below <= above || above >= above_wanted))
2289 {
2290#ifdef FEAT_FOLDING
2291 if (hasFolding(botline, &botline, NULL))
2292 ++below;
2293 else
2294#endif
2295 below += plines(botline);
2296 --botline;
2297 }
2298 if (above < above_wanted && (above < below || below >= below_wanted))
2299 {
2300#ifdef FEAT_FOLDING
2301 if (hasFolding(topline, NULL, &topline))
2302 ++above;
2303 else
2304#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002305 above += PLINES_NOFILL(topline);
2306#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 /* Count filler lines below this line as context. */
2308 if (topline < botline)
2309 above += diff_check_fill(curwin, topline + 1);
2310#endif
2311 ++topline;
2312 }
2313 }
2314 if (topline == botline || botline == 0)
2315 curwin->w_cursor.lnum = topline;
2316 else if (topline > botline)
2317 curwin->w_cursor.lnum = botline;
2318 else
2319 {
2320 if (cln < topline && curwin->w_topline > 1)
2321 {
2322 curwin->w_cursor.lnum = topline;
2323 curwin->w_valid &=
2324 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2325 }
2326 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2327 {
2328 curwin->w_cursor.lnum = botline;
2329 curwin->w_valid &=
2330 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2331 }
2332 }
2333 curwin->w_valid |= VALID_TOPLINE;
2334}
2335
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002336static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338/*
2339 * move screen 'count' pages up or down and update screen
2340 *
2341 * return FAIL for failure, OK otherwise
2342 */
2343 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002344onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 long n;
2347 int retval = OK;
2348 lineoff_T loff;
2349 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002350 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2353 {
2354 beep_flush();
2355 return FAIL;
2356 }
2357
2358 for ( ; count > 0; --count)
2359 {
2360 validate_botline();
2361 /*
2362 * It's an error to move a page up when the first line is already on
2363 * the screen. It's an error to move a page down when the last line
2364 * is on the screen and the topline is 'scrolloff' lines from the
2365 * last line.
2366 */
2367 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002368 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2370 : (curwin->w_topline == 1
2371#ifdef FEAT_DIFF
2372 && curwin->w_topfill ==
2373 diff_check_fill(curwin, curwin->w_topline)
2374#endif
2375 ))
2376 {
2377 beep_flush();
2378 retval = FAIL;
2379 break;
2380 }
2381
2382#ifdef FEAT_DIFF
2383 loff.fill = 0;
2384#endif
2385 if (dir == FORWARD)
2386 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002387 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002389 /* Vi compatible scrolling */
2390 if (p_window <= 2)
2391 ++curwin->w_topline;
2392 else
2393 curwin->w_topline += p_window - 2;
2394 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2395 curwin->w_topline = curbuf->b_ml.ml_line_count;
2396 curwin->w_cursor.lnum = curwin->w_topline;
2397 }
2398 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2399 {
2400 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 curwin->w_topline = curbuf->b_ml.ml_line_count;
2402#ifdef FEAT_DIFF
2403 curwin->w_topfill = 0;
2404#endif
2405 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2406 }
2407 else
2408 {
2409 /* For the overlap, start with the line just below the window
2410 * and go upwards. */
2411 loff.lnum = curwin->w_botline;
2412#ifdef FEAT_DIFF
2413 loff.fill = diff_check_fill(curwin, loff.lnum)
2414 - curwin->w_filler_rows;
2415#endif
2416 get_scroll_overlap(&loff, -1);
2417 curwin->w_topline = loff.lnum;
2418#ifdef FEAT_DIFF
2419 curwin->w_topfill = loff.fill;
2420 check_topfill(curwin, FALSE);
2421#endif
2422 curwin->w_cursor.lnum = curwin->w_topline;
2423 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2424 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2425 }
2426 }
2427 else /* dir == BACKWARDS */
2428 {
2429#ifdef FEAT_DIFF
2430 if (curwin->w_topline == 1)
2431 {
2432 /* Include max number of filler lines */
2433 max_topfill();
2434 continue;
2435 }
2436#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002437 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002438 {
2439 /* Vi compatible scrolling (sort of) */
2440 if (p_window <= 2)
2441 --curwin->w_topline;
2442 else
2443 curwin->w_topline -= p_window - 2;
2444 if (curwin->w_topline < 1)
2445 curwin->w_topline = 1;
2446 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2447 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2448 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2449 continue;
2450 }
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 /* Find the line at the top of the window that is going to be the
2453 * line at the bottom of the window. Make sure this results in
2454 * the same line as before doing CTRL-F. */
2455 loff.lnum = curwin->w_topline - 1;
2456#ifdef FEAT_DIFF
2457 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2458 - curwin->w_topfill;
2459#endif
2460 get_scroll_overlap(&loff, 1);
2461
2462 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2463 {
2464 loff.lnum = curbuf->b_ml.ml_line_count;
2465#ifdef FEAT_DIFF
2466 loff.fill = 0;
2467 }
2468 else
2469 {
2470 botline_topline(&loff);
2471#endif
2472 }
2473 curwin->w_cursor.lnum = loff.lnum;
2474
2475 /* Find the line just above the new topline to get the right line
2476 * at the bottom of the window. */
2477 n = 0;
2478 while (n <= curwin->w_height && loff.lnum >= 1)
2479 {
2480 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002481 if (loff.height == MAXCOL)
2482 n = MAXCOL;
2483 else
2484 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002486 if (loff.lnum < 1) /* at begin of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 curwin->w_topline = 1;
2489#ifdef FEAT_DIFF
2490 max_topfill();
2491#endif
2492 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2493 }
2494 else
2495 {
2496 /* Go two lines forward again. */
2497#ifdef FEAT_DIFF
2498 topline_botline(&loff);
2499#endif
2500 botline_forw(&loff);
2501 botline_forw(&loff);
2502#ifdef FEAT_DIFF
2503 botline_topline(&loff);
2504#endif
2505#ifdef FEAT_FOLDING
2506 /* We're at the wrong end of a fold now. */
2507 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2508#endif
2509
2510 /* Always scroll at least one line. Avoid getting stuck on
2511 * very long lines. */
2512 if (loff.lnum >= curwin->w_topline
2513#ifdef FEAT_DIFF
2514 && (loff.lnum > curwin->w_topline
2515 || loff.fill >= curwin->w_topfill)
2516#endif
2517 )
2518 {
2519#ifdef FEAT_DIFF
2520 /* First try using the maximum number of filler lines. If
2521 * that's not enough, backup one line. */
2522 loff.fill = curwin->w_topfill;
2523 if (curwin->w_topfill < diff_check_fill(curwin,
2524 curwin->w_topline))
2525 max_topfill();
2526 if (curwin->w_topfill == loff.fill)
2527#endif
2528 {
2529 --curwin->w_topline;
2530#ifdef FEAT_DIFF
2531 curwin->w_topfill = 0;
2532#endif
2533 }
2534 comp_botline(curwin);
2535 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002536 curwin->w_valid &=
2537 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 else
2540 {
2541 curwin->w_topline = loff.lnum;
2542#ifdef FEAT_DIFF
2543 curwin->w_topfill = loff.fill;
2544 check_topfill(curwin, FALSE);
2545#endif
2546 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2547 }
2548 }
2549 }
2550 }
2551#ifdef FEAT_FOLDING
2552 foldAdjustCursor();
2553#endif
2554 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002555 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002556 if (retval == OK)
2557 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2559
Bram Moolenaar907dad72018-07-10 15:07:15 +02002560 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002562 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2563 // But make sure we scroll at least one line (happens with mix of long
2564 // wrapping lines and non-wrapping line).
2565 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002567 scroll_cursor_top(1, FALSE);
2568 if (curwin->w_topline <= old_topline
2569 && old_topline < curbuf->b_ml.ml_line_count)
2570 {
2571 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002573 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2574#endif
2575 }
2576 }
2577#ifdef FEAT_FOLDING
2578 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
2583 redraw_later(VALID);
2584 return retval;
2585}
2586
2587/*
2588 * Decide how much overlap to use for page-up or page-down scrolling.
2589 * This is symmetric, so that doing both keeps the same lines displayed.
2590 * Three lines are examined:
2591 *
2592 * before CTRL-F after CTRL-F / before CTRL-B
2593 * etc. l1
2594 * l1 last but one line ------------
2595 * l2 last text line l2 top text line
2596 * ------------- l3 second text line
2597 * l3 etc.
2598 */
2599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002600get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 int h1, h2, h3, h4;
2603 int min_height = curwin->w_height - 2;
2604 lineoff_T loff0, loff1, loff2;
2605
2606#ifdef FEAT_DIFF
2607 if (lp->fill > 0)
2608 lp->height = 1;
2609 else
2610 lp->height = plines_nofill(lp->lnum);
2611#else
2612 lp->height = plines(lp->lnum);
2613#endif
2614 h1 = lp->height;
2615 if (h1 > min_height)
2616 return; /* no overlap */
2617
2618 loff0 = *lp;
2619 if (dir > 0)
2620 botline_forw(lp);
2621 else
2622 topline_back(lp);
2623 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002624 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626 *lp = loff0; /* no overlap */
2627 return;
2628 }
2629
2630 loff1 = *lp;
2631 if (dir > 0)
2632 botline_forw(lp);
2633 else
2634 topline_back(lp);
2635 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002636 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 *lp = loff0; /* no overlap */
2639 return;
2640 }
2641
2642 loff2 = *lp;
2643 if (dir > 0)
2644 botline_forw(lp);
2645 else
2646 topline_back(lp);
2647 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002648 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 *lp = loff1; /* 1 line overlap */
2650 else
2651 *lp = loff2; /* 2 lines overlap */
2652 return;
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
2656 * Scroll 'scroll' lines up or down.
2657 */
2658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002659halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 long scrolled = 0;
2662 int i;
2663 int n;
2664 int room;
2665
2666 if (Prenum)
2667 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2668 curwin->w_height : Prenum;
2669 n = (curwin->w_p_scr <= curwin->w_height) ?
2670 curwin->w_p_scr : curwin->w_height;
2671
Bram Moolenaard5d37532017-03-27 23:02:07 +02002672 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 validate_botline();
2674 room = curwin->w_empty_rows;
2675#ifdef FEAT_DIFF
2676 room += curwin->w_filler_rows;
2677#endif
2678 if (flag)
2679 {
2680 /*
2681 * scroll the text up
2682 */
2683 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2684 {
2685#ifdef FEAT_DIFF
2686 if (curwin->w_topfill > 0)
2687 {
2688 i = 1;
2689 if (--n < 0 && scrolled > 0)
2690 break;
2691 --curwin->w_topfill;
2692 }
2693 else
2694#endif
2695 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002696 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 n -= i;
2698 if (n < 0 && scrolled > 0)
2699 break;
2700#ifdef FEAT_FOLDING
2701 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2702#endif
2703 ++curwin->w_topline;
2704#ifdef FEAT_DIFF
2705 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2706#endif
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2709 {
2710 ++curwin->w_cursor.lnum;
2711 curwin->w_valid &=
2712 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2716 scrolled += i;
2717
2718 /*
2719 * Correct w_botline for changed w_topline.
2720 * Won't work when there are filler lines.
2721 */
2722#ifdef FEAT_DIFF
2723 if (curwin->w_p_diff)
2724 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2725 else
2726#endif
2727 {
2728 room += i;
2729 do
2730 {
2731 i = plines(curwin->w_botline);
2732 if (i > room)
2733 break;
2734#ifdef FEAT_FOLDING
2735 (void)hasFolding(curwin->w_botline, NULL,
2736 &curwin->w_botline);
2737#endif
2738 ++curwin->w_botline;
2739 room -= i;
2740 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2741 }
2742 }
2743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 /*
2745 * When hit bottom of the file: move cursor down.
2746 */
2747 if (n > 0)
2748 {
2749# ifdef FEAT_FOLDING
2750 if (hasAnyFolding(curwin))
2751 {
2752 while (--n >= 0
2753 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2754 {
2755 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2756 &curwin->w_cursor.lnum);
2757 ++curwin->w_cursor.lnum;
2758 }
2759 }
2760 else
2761# endif
2762 curwin->w_cursor.lnum += n;
2763 check_cursor_lnum();
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766 else
2767 {
2768 /*
2769 * scroll the text down
2770 */
2771 while (n > 0 && curwin->w_topline > 1)
2772 {
2773#ifdef FEAT_DIFF
2774 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2775 {
2776 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002777 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 ++curwin->w_topfill;
2779 }
2780 else
2781#endif
2782 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002783 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 n -= i;
2785 if (n < 0 && scrolled > 0)
2786 break;
2787 --curwin->w_topline;
2788#ifdef FEAT_FOLDING
2789 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2790#endif
2791#ifdef FEAT_DIFF
2792 curwin->w_topfill = 0;
2793#endif
2794 }
2795 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2796 VALID_BOTLINE|VALID_BOTLINE_AP);
2797 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (curwin->w_cursor.lnum > 1)
2799 {
2800 --curwin->w_cursor.lnum;
2801 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 /*
2806 * When hit top of the file: move cursor up.
2807 */
2808 if (n > 0)
2809 {
2810 if (curwin->w_cursor.lnum <= (linenr_T)n)
2811 curwin->w_cursor.lnum = 1;
2812 else
2813# ifdef FEAT_FOLDING
2814 if (hasAnyFolding(curwin))
2815 {
2816 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2817 {
2818 --curwin->w_cursor.lnum;
2819 (void)hasFolding(curwin->w_cursor.lnum,
2820 &curwin->w_cursor.lnum, NULL);
2821 }
2822 }
2823 else
2824# endif
2825 curwin->w_cursor.lnum -= n;
2826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828# ifdef FEAT_FOLDING
2829 /* Move cursor to first line of closed fold. */
2830 foldAdjustCursor();
2831# endif
2832#ifdef FEAT_DIFF
2833 check_topfill(curwin, !flag);
2834#endif
2835 cursor_correct();
2836 beginline(BL_SOL | BL_FIX);
2837 redraw_later(VALID);
2838}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002839
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002842{
2843 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002844 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002845 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002846 colnr_T curswant = curwin->w_curswant;
2847 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002848 win_T *old_curwin = curwin;
2849 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002850 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002851 int old_VIsual_select = VIsual_select;
2852 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002853
2854 /*
2855 * loop through the cursorbound windows
2856 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002857 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002858 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002859 {
2860 curbuf = curwin->w_buffer;
2861 /* skip original window and windows with 'noscrollbind' */
2862 if (curwin != old_curwin && curwin->w_p_crb)
2863 {
2864# ifdef FEAT_DIFF
2865 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002866 curwin->w_cursor.lnum =
2867 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002868 else
2869# endif
2870 curwin->w_cursor.lnum = line;
2871 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002872 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002873 curwin->w_curswant = curswant;
2874 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002875
Bram Moolenaar61452852011-02-01 18:01:11 +01002876 /* Make sure the cursor is in a valid position. Temporarily set
2877 * "restart_edit" to allow the cursor to be beyond the EOL. */
2878 restart_edit_save = restart_edit;
2879 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002881# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002882 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002883 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002884# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002885 restart_edit = restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886 /* Correct cursor for multi-byte character. */
2887 if (has_mbyte)
2888 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002889 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002890
2891 /* Only scroll when 'scrollbind' hasn't done this. */
2892 if (!curwin->w_p_scb)
2893 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002895 }
2896 }
2897
2898 /*
2899 * reset current-window
2900 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 VIsual_select = old_VIsual_select;
2902 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002903 curwin = old_curwin;
2904 curbuf = old_curbuf;
2905}