blob: 8660d89055eb409d38cad7bff857e3adfe2957e0 [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{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} 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
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
118/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100119 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
120 * set.
121 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100123redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100124{
125 if ((wp->w_p_rnu
126#ifdef FEAT_SYN_HL
127 || wp->w_p_cul
128#endif
129 )
130 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200131 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200132 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000133 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100134 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200135 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100136}
137
zeertzjq3e559cd2022-03-27 19:26:55 +0100138#ifdef FEAT_SYN_HL
139/*
140 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
141 * contains "screenline".
142 */
143 static void
144redraw_for_cursorcolumn(win_T *wp)
145{
146 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
147 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100148 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100149 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100150 redraw_win_later(wp, UPD_SOME_VALID);
151 // When 'cursorlineopt' contains "screenline" need to redraw with
152 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100153 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100154 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100155 }
156}
157#endif
158
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Update curwin->w_topline and redraw if necessary.
161 * Used to update the screen before printing a message.
162 */
163 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100164update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 update_topline();
167 if (must_redraw)
168 update_screen(0);
169}
170
171/*
172 * Update curwin->w_topline to move the cursor onto the screen.
173 */
174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100175update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
177 long line_count;
178 int halfheight;
179 int n;
180 linenr_T old_topline;
181#ifdef FEAT_DIFF
182 int old_topfill;
183#endif
184#ifdef FEAT_FOLDING
185 linenr_T lnum;
186#endif
187 int check_topline = FALSE;
188 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100189 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100190 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar85a20022019-12-21 18:25:54 +0100192 // If there is no valid screen and when the window height is zero just use
193 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200194 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200195 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100196 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200197 curwin->w_topline = curwin->w_cursor.lnum;
198 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200199 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 return;
201 }
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 check_cursor_moved(curwin);
204 if (curwin->w_valid & VALID_TOPLINE)
205 return;
206
Bram Moolenaar85a20022019-12-21 18:25:54 +0100207 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000208 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100209 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211 old_topline = curwin->w_topline;
212#ifdef FEAT_DIFF
213 old_topfill = curwin->w_topfill;
214#endif
215
216 /*
217 * If the buffer is empty, always set topline to 1.
218 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
221 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100222 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 curwin->w_botline = 2;
225 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228
229 /*
230 * If the cursor is above or near the top of the window, scroll the window
231 * to show the line the cursor is in, with 'scrolloff' context.
232 */
233 else
234 {
235 if (curwin->w_topline > 1)
236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100237 // If the cursor is above topline, scrolling is always needed.
238 // If the cursor is far below topline and there is no folding,
239 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (curwin->w_cursor.lnum < curwin->w_topline)
241 check_topline = TRUE;
242 else if (check_top_offset())
243 check_topline = TRUE;
244 }
245#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100246 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
248 curwin->w_topline))
249 check_topline = TRUE;
250#endif
251
252 if (check_topline)
253 {
254 halfheight = curwin->w_height / 2 - 1;
255 if (halfheight < 2)
256 halfheight = 2;
257
258#ifdef FEAT_FOLDING
259 if (hasAnyFolding(curwin))
260 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100261 // Count the number of logical lines between the cursor and
262 // topline + scrolloff (approximation of how much will be
263 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 n = 0;
265 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100266 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 {
268 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
271 break;
272 (void)hasFolding(lnum, NULL, &lnum);
273 }
274 }
275 else
276#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100277 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar85a20022019-12-21 18:25:54 +0100279 // If we weren't very close to begin with, we scroll to put the
280 // cursor in the middle of the window. Otherwise put the cursor
281 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 if (n >= halfheight)
283 scroll_cursor_halfway(FALSE);
284 else
285 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000286 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 check_botline = TRUE;
288 }
289 }
290
291 else
292 {
293#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
296#endif
297 check_botline = TRUE;
298 }
299 }
300
301 /*
302 * If the cursor is below the bottom of the window, scroll the window
303 * to put the cursor on the window.
304 * When w_botline is invalid, recompute it first, to avoid a redraw later.
305 * If w_botline was approximated, we might need a redraw later in a few
306 * cases, but we don't want to spend (a lot of) time recomputing w_botline
307 * for every small change.
308 */
309 if (check_botline)
310 {
311 if (!(curwin->w_valid & VALID_BOTLINE_AP))
312 validate_botline();
313
314 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
315 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000316 if (curwin->w_cursor.lnum < curwin->w_botline)
317 {
318 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100319 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_FOLDING
321 || hasAnyFolding(curwin)
322#endif
323 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 lineoff_T loff;
326
Bram Moolenaar85a20022019-12-21 18:25:54 +0100327 // Cursor is (a few lines) above botline, check if there are
328 // 'scrolloff' window lines below the cursor. If not, need to
329 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 n = curwin->w_empty_rows;
331 loff.lnum = curwin->w_cursor.lnum;
332#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
335#endif
336#ifdef FEAT_DIFF
337 loff.fill = 0;
338 n += curwin->w_filler_rows;
339#endif
340 loff.height = 0;
341 while (loff.lnum < curwin->w_botline
342#ifdef FEAT_DIFF
343 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
344#endif
345 )
346 {
347 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100348 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 break;
350 botline_forw(&loff);
351 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100352 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000355 }
356 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360 if (check_botline)
361 {
362#ifdef FEAT_FOLDING
363 if (hasAnyFolding(curwin))
364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100365 // Count the number of logical lines between the cursor and
366 // botline - scrolloff (approximation of how much will be
367 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 line_count = 0;
369 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (lnum <= 0 || line_count > curwin->w_height + 1)
375 break;
376 (void)hasFolding(lnum, &lnum, NULL);
377 }
378 }
379 else
380#endif
381 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100382 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000384 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 else
386 scroll_cursor_halfway(FALSE);
387 }
388 }
389 }
390 curwin->w_valid |= VALID_TOPLINE;
391
392 /*
393 * Need to redraw when topline changed.
394 */
395 if (curwin->w_topline != old_topline
396#ifdef FEAT_DIFF
397 || curwin->w_topfill != old_topfill
398#endif
399 )
400 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100401 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000402 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
404 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100405 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100408 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (curwin->w_cursor.lnum == curwin->w_topline)
411 validate_cursor();
412 }
413
Bram Moolenaar375e3392019-01-31 18:26:10 +0100414 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415}
416
417/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000418 * Return the scrolljump value to use for the current window.
419 * When 'scrolljump' is positive use it as-is.
420 * When 'scrolljump' is negative use it as a percentage of the window height.
421 */
422 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100423scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000424{
425 if (p_sj >= 0)
426 return (int)p_sj;
427 return (curwin->w_height * -p_sj) / 100;
428}
429
430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
432 * current window.
433 */
434 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100435check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 lineoff_T loff;
438 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100439 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
Bram Moolenaar375e3392019-01-31 18:26:10 +0100441 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442#ifdef FEAT_FOLDING
443 || hasAnyFolding(curwin)
444#endif
445 )
446 {
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_DIFF
449 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100450 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#else
452 n = 0;
453#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100455 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100458 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (loff.lnum < curwin->w_topline
460#ifdef FEAT_DIFF
461 || (loff.lnum == curwin->w_topline && loff.fill > 0)
462#endif
463 )
464 break;
465 n += loff.height;
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 return TRUE;
469 }
470 return FALSE;
471}
472
473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100474update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 if (curwin->w_set_curswant)
477 {
478 validate_virtcol();
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100479 curwin->w_curswant = curwin->w_virtcol
480#ifdef FEAT_PROP_POPUP
481 - curwin->w_virtcol_first_char
482#endif
483 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 curwin->w_set_curswant = FALSE;
485 }
486}
487
488/*
489 * Check if the cursor has moved. Set the w_valid flag accordingly.
490 */
491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
495 {
496 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100497 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
498 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 wp->w_valid_cursor = wp->w_cursor;
500 wp->w_valid_leftcol = wp->w_leftcol;
501 }
502 else if (wp->w_cursor.col != wp->w_valid_cursor.col
503 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100504 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
506 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
507 wp->w_valid_cursor.col = wp->w_cursor.col;
508 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511}
512
513/*
514 * Call this function when some window settings have changed, which require
515 * the cursor position, botline and topline to be recomputed and the window to
516 * be redrawn. E.g, when changing the 'wrap' option or folding.
517 */
518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100519changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 changed_window_setting_win(curwin);
522}
523
524 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100525changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 wp->w_lines_valid = 0;
528 changed_line_abv_curs_win(wp);
529 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100530 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533/*
534 * Set wp->w_topline to a certain number.
535 */
536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100537set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200539#ifdef FEAT_DIFF
540 linenr_T prev_topline = wp->w_topline;
541#endif
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100544 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
546#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100547 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100549 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
550 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000552 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200554 if (lnum != prev_topline)
555 // Keep the filler lines when the topline didn't change.
556 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100559 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100560 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561}
562
563/*
564 * Call this function when the length of the cursor line (in screen
565 * characters) has changed, and the change is before the cursor.
566 * Need to take care of w_botline separately!
567 */
568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
572 |VALID_CHEIGHT|VALID_TOPLINE);
573}
574
575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
579 |VALID_CHEIGHT|VALID_TOPLINE);
580}
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/*
583 * Call this function when the length of a line (in screen characters) above
584 * the cursor have changed.
585 * Need to take care of w_botline separately!
586 */
587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100588changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
591 |VALID_CHEIGHT|VALID_TOPLINE);
592}
593
594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100595changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
597 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
598 |VALID_CHEIGHT|VALID_TOPLINE);
599}
600
601/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100602 * Display of line has changed for "buf", invalidate cursor position and
603 * w_botline.
604 */
605 void
606changed_line_display_buf(buf_T *buf)
607{
608 win_T *wp;
609
610 FOR_ALL_WINDOWS(wp)
611 if (wp->w_buffer == buf)
612 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
613 |VALID_CROW|VALID_CHEIGHT
614 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
615}
616
617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 * Make sure the value of curwin->w_botline is valid.
619 */
620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100621validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100623 validate_botline_win(curwin);
624}
625
626/*
627 * Make sure the value of wp->w_botline is valid.
628 */
629 void
630validate_botline_win(win_T *wp)
631{
632 if (!(wp->w_valid & VALID_BOTLINE))
633 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 * Mark curwin->w_botline as invalid (because of some change in the buffer).
638 */
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
643}
644
645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652approximate_botline_win(
653 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 wp->w_valid &= ~VALID_BOTLINE;
656}
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658/*
659 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
660 */
661 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 check_cursor_moved(curwin);
665 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
666 (VALID_WROW|VALID_WCOL));
667}
668
669/*
670 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
671 * w_topline must be valid, you may need to call update_topline() first!
672 */
673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100674validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 check_cursor_moved(curwin);
677 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
678 curs_columns(TRUE);
679}
680
681#if defined(FEAT_GUI) || defined(PROTO)
682/*
683 * validate w_cline_row.
684 */
685 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 /*
689 * First make sure that w_topline is valid (after moving the cursor).
690 */
691 update_topline();
692 check_cursor_moved(curwin);
693 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100694 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695}
696#endif
697
698/*
699 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200700 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 */
702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100703curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 linenr_T lnum;
706 int i;
707 int all_invalid;
708 int valid;
709#ifdef FEAT_FOLDING
710 long fold_count;
711#endif
712
Bram Moolenaar85a20022019-12-21 18:25:54 +0100713 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 all_invalid = (!redrawing()
715 || wp->w_lines_valid == 0
716 || wp->w_lines[0].wl_lnum > wp->w_topline);
717 i = 0;
718 wp->w_cline_row = 0;
719 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
720 {
721 valid = FALSE;
722 if (!all_invalid && i < wp->w_lines_valid)
723 {
724 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100725 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (wp->w_lines[i].wl_lnum == lnum)
727 {
728#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 // Check for newly inserted lines below this row, in which
730 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (!wp->w_buffer->b_mod_set
732 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
733 || wp->w_buffer->b_mod_top
734 > wp->w_lines[i].wl_lastlnum + 1)
735#endif
736 valid = TRUE;
737 }
738 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100739 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741 if (valid
742#ifdef FEAT_DIFF
743 && (lnum != wp->w_topline || !wp->w_p_diff)
744#endif
745 )
746 {
747#ifdef FEAT_FOLDING
748 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (lnum > wp->w_cursor.lnum)
751 break;
752#else
753 ++lnum;
754#endif
755 wp->w_cline_row += wp->w_lines[i].wl_size;
756 }
757 else
758 {
759#ifdef FEAT_FOLDING
760 fold_count = foldedCount(wp, lnum, NULL);
761 if (fold_count)
762 {
763 lnum += fold_count;
764 if (lnum > wp->w_cursor.lnum)
765 break;
766 ++wp->w_cline_row;
767 }
768 else
769#endif
770#ifdef FEAT_DIFF
771 if (lnum == wp->w_topline)
772 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
773 + wp->w_topfill;
774 else
775#endif
776 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
777 }
778 }
779
780 check_cursor_moved(wp);
781 if (!(wp->w_valid & VALID_CHEIGHT))
782 {
783 if (all_invalid
784 || i == wp->w_lines_valid
785 || (i < wp->w_lines_valid
786 && (!wp->w_lines[i].wl_valid
787 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
788 {
789#ifdef FEAT_DIFF
790 if (wp->w_cursor.lnum == wp->w_topline)
791 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
792 TRUE) + wp->w_topfill;
793 else
794#endif
795 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
796#ifdef FEAT_FOLDING
797 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
798 NULL, NULL, TRUE, NULL);
799#endif
800 }
801 else if (i > wp->w_lines_valid)
802 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100803 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 wp->w_cline_height = 0;
805#ifdef FEAT_FOLDING
806 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
807 NULL, NULL, TRUE, NULL);
808#endif
809 }
810 else
811 {
812 wp->w_cline_height = wp->w_lines[i].wl_size;
813#ifdef FEAT_FOLDING
814 wp->w_cline_folded = wp->w_lines[i].wl_folded;
815#endif
816 }
817 }
818
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100819 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Validate curwin->w_virtcol only.
826 */
827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100828validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
830 validate_virtcol_win(curwin);
831}
832
833/*
834 * Validate wp->w_virtcol only.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 check_cursor_moved(wp);
840 if (!(wp->w_valid & VALID_VIRTCOL))
841 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100842#ifdef FEAT_PROP_POPUP
843 wp->w_virtcol_first_char = 0;
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000846#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100847 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000848#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100849 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851}
852
853/*
854 * Validate curwin->w_cline_height only.
855 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100856 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100857validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
859 check_cursor_moved(curwin);
860 if (!(curwin->w_valid & VALID_CHEIGHT))
861 {
862#ifdef FEAT_DIFF
863 if (curwin->w_cursor.lnum == curwin->w_topline)
864 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
865 + curwin->w_topfill;
866 else
867#endif
868 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
869#ifdef FEAT_FOLDING
870 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
871#endif
872 curwin->w_valid |= VALID_CHEIGHT;
873 }
874}
875
876/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000877 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 */
879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100880validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 colnr_T off;
883 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100884 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 validate_virtcol();
887 if (!(curwin->w_valid & VALID_WCOL))
888 {
889 col = curwin->w_virtcol;
890 off = curwin_col_off();
891 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200892 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000895 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200896 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100897 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200899 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000900 if (col > (int)curwin->w_leftcol)
901 col -= curwin->w_leftcol;
902 else
903 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100907#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100908 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911}
912
913/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200914 * Compute offset of a window, occupied by absolute or relative line number,
915 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
917 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100918win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaar64486672010-05-16 15:46:46 +0200920 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_CMDWIN
922 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
923#endif
924#ifdef FEAT_FOLDING
925 + wp->w_p_fdc
926#endif
927#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200928 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#endif
930 );
931}
932
933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100934curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 return win_col_off(curwin);
937}
938
939/*
940 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200941 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
942 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Bram Moolenaar64486672010-05-16 15:46:46 +0200947 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000948 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return 0;
950}
951
952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100953curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
955 return win_col_off2(curwin);
956}
957
958/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100959 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * Also updates curwin->w_wrow and curwin->w_cline_row.
961 * Also updates curwin->w_leftcol.
962 */
963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int off_left, off_right;
970 int n;
971 int p_lines;
972 int width = 0;
973 int textwidth;
974 int new_leftcol;
975 colnr_T startcol;
976 colnr_T endcol;
977 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100978 long so = get_scrolloff_value();
979 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 /*
982 * First make sure that w_topline is valid (after moving the cursor).
983 */
Luuk van Baal29ab5242022-09-11 16:59:53 +0100984 if (p_spsc)
985 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * Next make sure that w_cline_row is valid.
989 */
990 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100991 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#ifdef FEAT_PROP_POPUP
994 // will be set by getvvcol() but not reset
995 curwin->w_virtcol_first_char = 0;
996#endif
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /*
999 * Compute the number of virtual columns.
1000 */
1001#ifdef FEAT_FOLDING
1002 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001003 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1005 else
1006#endif
1007 getvvcol(curwin, &curwin->w_cursor,
1008 &startcol, &(curwin->w_virtcol), &endcol);
1009
Bram Moolenaar85a20022019-12-21 18:25:54 +01001010 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001012 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 extra = curwin_col_off();
1015 curwin->w_wcol = curwin->w_virtcol + extra;
1016 endcol += extra;
1017
1018 /*
1019 * Now compute w_wrow, counting screen lines from w_cline_row.
1020 */
1021 curwin->w_wrow = curwin->w_cline_row;
1022
Bram Moolenaar02631462017-09-22 15:20:32 +02001023 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (textwidth <= 0)
1025 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001026 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001027 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001028 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001029 if (curwin->w_p_wrap)
1030 curwin->w_wrow = curwin->w_height - 1;
1031 else
1032 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001034 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 width = textwidth + curwin_col_off2();
1037
Bram Moolenaar85a20022019-12-21 18:25:54 +01001038 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001039 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001041#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001042 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001043#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001044
Bram Moolenaar85a20022019-12-21 18:25:54 +01001045 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001046 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 curwin->w_wcol -= n * width;
1048 curwin->w_wrow += n;
1049
1050#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001051 // When cursor wraps to first char of next line in Insert
1052 // mode, the 'showbreak' string isn't shown, backup to first
1053 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001054 sbr = get_showbreak_value(curwin);
1055 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001056 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 curwin->w_wcol = 0;
1058#endif
1059 }
1060 }
1061
Bram Moolenaar85a20022019-12-21 18:25:54 +01001062 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1063 // is not folded.
1064 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001065 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_FOLDING
1067 && !curwin->w_cline_folded
1068#endif
1069 )
1070 {
1071 /*
1072 * If Cursor is left of the screen, scroll rightwards.
1073 * If Cursor is right of the screen, scroll leftwards
1074 * If we get closer to the edge than 'sidescrolloff', scroll a little
1075 * extra
1076 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001077 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001078 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001079 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (off_left < 0 || off_right > 0)
1081 {
1082 if (off_left < 0)
1083 diff = -off_left;
1084 else
1085 diff = off_right;
1086
Bram Moolenaar85a20022019-12-21 18:25:54 +01001087 // When far off or not enough room on either side, put cursor in
1088 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1090 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1091 else
1092 {
1093 if (diff < p_ss)
1094 diff = p_ss;
1095 if (off_left < 0)
1096 new_leftcol = curwin->w_leftcol - diff;
1097 else
1098 new_leftcol = curwin->w_leftcol + diff;
1099 }
1100 if (new_leftcol < 0)
1101 new_leftcol = 0;
1102 if (new_leftcol != (int)curwin->w_leftcol)
1103 {
1104 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001105 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001106 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 }
1109 curwin->w_wcol -= curwin->w_leftcol;
1110 }
1111 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1112 curwin->w_wcol -= curwin->w_leftcol;
1113 else
1114 curwin->w_wcol = 0;
1115
1116#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001117 // Skip over filler lines. At the top use w_topfill, there
1118 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (curwin->w_cursor.lnum == curwin->w_topline)
1120 curwin->w_wrow += curwin->w_topfill;
1121 else
1122 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1123#endif
1124
1125 prev_skipcol = curwin->w_skipcol;
1126
1127 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if ((curwin->w_wrow >= curwin->w_height
1130 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001131 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 && (p_lines =
1133#ifdef FEAT_DIFF
1134 plines_win_nofill
1135#else
1136 plines_win
1137#endif
1138 (curwin, curwin->w_cursor.lnum, FALSE))
1139 - 1 >= curwin->w_height))
1140 && curwin->w_height != 0
1141 && curwin->w_cursor.lnum == curwin->w_topline
1142 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001143 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001145 // Cursor past end of screen. Happens with a single line that does
1146 // not fit on screen. Find a skipcol to show the text around the
1147 // cursor. Avoid scrolling all the time. compute value of "extra":
1148 // 1: Less than 'scrolloff' lines above
1149 // 2: Less than 'scrolloff' lines below
1150 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001152 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // Compute last display line of the buffer line that we want at the
1155 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (p_lines == 0)
1157 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1158 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001159 if (p_lines > curwin->w_wrow + so)
1160 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 else
1162 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001163 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 extra += 2;
1165
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001166 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 n = curwin->w_virtcol / width;
1170 if (n > curwin->w_height / 2)
1171 n -= curwin->w_height / 2;
1172 else
1173 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (n > p_lines - curwin->w_height + 1)
1176 n = p_lines - curwin->w_height + 1;
1177 curwin->w_skipcol = n * width;
1178 }
1179 else if (extra == 1)
1180 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001181 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001182 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 + width - 1) / width;
1184 if (extra > 0)
1185 {
1186 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1187 extra = curwin->w_skipcol / width;
1188 curwin->w_skipcol -= extra * width;
1189 }
1190 }
1191 else if (extra == 2)
1192 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001193 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 endcol = (n - curwin->w_height + 1) * width;
1195 while (endcol > curwin->w_virtcol)
1196 endcol -= width;
1197 if (endcol > curwin->w_skipcol)
1198 curwin->w_skipcol = endcol;
1199 }
1200
1201 curwin->w_wrow -= curwin->w_skipcol / width;
1202 if (curwin->w_wrow >= curwin->w_height)
1203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 extra = curwin->w_wrow - curwin->w_height + 1;
1206 curwin->w_skipcol += extra * width;
1207 curwin->w_wrow -= extra;
1208 }
1209
1210 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1211 if (extra > 0)
1212 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1213 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001214 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 else
1217 curwin->w_skipcol = 0;
1218 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001219 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001221#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001222 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001223#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001224#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1225 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1226 {
1227 curwin->w_wrow += popup_top_extra(curwin);
1228 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001229 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001230 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001231 else
1232 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001233#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001234
Bram Moolenaar08f23632019-11-16 14:19:33 +01001235 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1236 curwin->w_valid_leftcol = curwin->w_leftcol;
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1239}
1240
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001241#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001242/*
1243 * Compute the screen position of text character at "pos" in window "wp"
1244 * The resulting values are one-based, zero when character is not visible.
1245 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001246 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001247textpos2screenpos(
1248 win_T *wp,
1249 pos_T *pos,
1250 int *rowp, // screen row
1251 int *scolp, // start screen column
1252 int *ccolp, // cursor screen column
1253 int *ecolp) // end screen column
1254{
1255 colnr_T scol = 0, ccol = 0, ecol = 0;
1256 int row = 0;
1257 int rowoff = 0;
1258 colnr_T coloff = 0;
1259
Bram Moolenaar189663b2021-07-21 18:04:56 +02001260 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001261 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001262 colnr_T off;
1263 colnr_T col;
1264 int width;
1265 linenr_T lnum = pos->lnum;
1266#ifdef FEAT_FOLDING
1267 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001268
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001269 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1270#endif
1271 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1272#ifdef FEAT_FOLDING
1273 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001274 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001275 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001276 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001277 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001278 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001279#endif
1280 {
1281 getvcol(wp, pos, &scol, &ccol, &ecol);
1282
1283 // similar to what is done in validate_cursor_col()
1284 col = scol;
1285 off = win_col_off(wp);
1286 col += off;
1287 width = wp->w_width - off + win_col_off2(wp);
1288
1289 // long line wrapping, adjust row
1290 if (wp->w_p_wrap
1291 && col >= (colnr_T)wp->w_width
1292 && width > 0)
1293 {
1294 // use same formula as what is used in curs_columns()
1295 rowoff = ((col - wp->w_width) / width + 1);
1296 col -= rowoff * width;
1297 }
1298 col -= wp->w_leftcol;
1299 if (col >= wp->w_width)
1300 col = -1;
1301 if (col >= 0 && row + rowoff <= wp->w_height)
1302 {
1303 coloff = col - scol + wp->w_wincol + 1;
1304 row += W_WINROW(wp);
1305 }
1306 else
1307 // character is left, right or below of the window
1308 row = rowoff = scol = ccol = ecol = 0;
1309 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001310 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001311 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001312 *scolp = scol + coloff;
1313 *ccolp = ccol + coloff;
1314 *ecolp = ecol + coloff;
1315}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001316#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001317
Bram Moolenaar12034e22019-08-25 22:25:02 +02001318#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001319/*
1320 * "screenpos({winid}, {lnum}, {col})" function
1321 */
1322 void
1323f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1324{
1325 dict_T *dict;
1326 win_T *wp;
1327 pos_T pos;
1328 int row = 0;
1329 int scol = 0, ccol = 0, ecol = 0;
1330
Bram Moolenaar93a10962022-06-16 11:42:09 +01001331 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001332 return;
1333 dict = rettv->vval.v_dict;
1334
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001335 if (in_vim9script()
1336 && (check_for_number_arg(argvars, 0) == FAIL
1337 || check_for_number_arg(argvars, 1) == FAIL
1338 || check_for_number_arg(argvars, 2) == FAIL))
1339 return;
1340
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001341 wp = find_win_by_nr_or_id(&argvars[0]);
1342 if (wp == NULL)
1343 return;
1344
1345 pos.lnum = tv_get_number(&argvars[1]);
1346 pos.col = tv_get_number(&argvars[2]) - 1;
1347 pos.coladd = 0;
1348 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1349
1350 dict_add_number(dict, "row", row);
1351 dict_add_number(dict, "col", scol);
1352 dict_add_number(dict, "curscol", ccol);
1353 dict_add_number(dict, "endcol", ecol);
1354}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001355
1356/*
1357 * "virtcol2col({winid}, {lnum}, {col})" function
1358 */
1359 void
1360f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1361{
1362 win_T *wp;
1363 linenr_T lnum;
1364 int screencol;
1365 int error = FALSE;
1366
1367 rettv->vval.v_number = -1;
1368
1369 if (check_for_number_arg(argvars, 0) == FAIL
1370 || check_for_number_arg(argvars, 1) == FAIL
1371 || check_for_number_arg(argvars, 2) == FAIL)
1372 return;
1373
1374 wp = find_win_by_nr_or_id(&argvars[0]);
1375 if (wp == NULL)
1376 return;
1377
1378 lnum = tv_get_number_chk(&argvars[1], &error);
1379 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1380 return;
1381
1382 screencol = tv_get_number_chk(&argvars[2], &error);
1383 if (error || screencol < 0)
1384 return;
1385
1386 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1387}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001388#endif
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390/*
1391 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001394scrolldown(
1395 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001396 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001398 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 int wrow;
1400 int moved = FALSE;
1401
1402#ifdef FEAT_FOLDING
1403 linenr_T first;
1404
Bram Moolenaar85a20022019-12-21 18:25:54 +01001405 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1407#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001408 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 while (line_count-- > 0)
1410 {
1411#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001412 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1413 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
1415 ++curwin->w_topfill;
1416 ++done;
1417 }
1418 else
1419#endif
1420 {
1421 if (curwin->w_topline == 1)
1422 break;
1423 --curwin->w_topline;
1424#ifdef FEAT_DIFF
1425 curwin->w_topfill = 0;
1426#endif
1427#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001428 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 if (hasFolding(curwin->w_topline, &first, NULL))
1430 {
1431 ++done;
1432 if (!byfold)
1433 line_count -= curwin->w_topline - first - 1;
1434 curwin->w_botline -= curwin->w_topline - first;
1435 curwin->w_topline = first;
1436 }
1437 else
1438#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001439 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001441 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 invalidate_botline();
1443 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 curwin->w_wrow += done; // keep w_wrow updated
1445 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447#ifdef FEAT_DIFF
1448 if (curwin->w_cursor.lnum == curwin->w_topline)
1449 curwin->w_cline_row = 0;
1450 check_topfill(curwin, TRUE);
1451#endif
1452
1453 /*
1454 * Compute the row number of the last row of the cursor line
1455 * and move the cursor onto the displayed part of the window.
1456 */
1457 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001458 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 validate_virtcol();
1461 validate_cheight();
1462 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001463 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1466 {
1467#ifdef FEAT_FOLDING
1468 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1469 {
1470 --wrow;
1471 if (first == 1)
1472 curwin->w_cursor.lnum = 1;
1473 else
1474 curwin->w_cursor.lnum = first - 1;
1475 }
1476 else
1477#endif
1478 wrow -= plines(curwin->w_cursor.lnum--);
1479 curwin->w_valid &=
1480 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1481 moved = TRUE;
1482 }
1483 if (moved)
1484 {
1485#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 foldAdjustCursor();
1488#endif
1489 coladvance(curwin->w_curswant);
1490 }
1491}
1492
1493/*
1494 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001497scrollup(
1498 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001499 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1502 linenr_T lnum;
1503
1504 if (
1505# ifdef FEAT_FOLDING
1506 (byfold && hasAnyFolding(curwin))
1507# ifdef FEAT_DIFF
1508 ||
1509# endif
1510# endif
1511# ifdef FEAT_DIFF
1512 curwin->w_p_diff
1513# endif
1514 )
1515 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001516 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 lnum = curwin->w_topline;
1518 while (line_count--)
1519 {
1520# ifdef FEAT_DIFF
1521 if (curwin->w_topfill > 0)
1522 --curwin->w_topfill;
1523 else
1524# endif
1525 {
1526# ifdef FEAT_FOLDING
1527 if (byfold)
1528 (void)hasFolding(lnum, NULL, &lnum);
1529# endif
1530 if (lnum >= curbuf->b_ml.ml_line_count)
1531 break;
1532 ++lnum;
1533# ifdef FEAT_DIFF
1534 curwin->w_topfill = diff_check_fill(curwin, lnum);
1535# endif
1536 }
1537 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001538 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 curwin->w_botline += lnum - curwin->w_topline;
1540 curwin->w_topline = lnum;
1541 }
1542 else
1543#endif
1544 {
1545 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001546 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548
1549 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1550 curwin->w_topline = curbuf->b_ml.ml_line_count;
1551 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1552 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1553
1554#ifdef FEAT_DIFF
1555 check_topfill(curwin, FALSE);
1556#endif
1557
1558#ifdef FEAT_FOLDING
1559 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001560 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1562#endif
1563
1564 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1565 if (curwin->w_cursor.lnum < curwin->w_topline)
1566 {
1567 curwin->w_cursor.lnum = curwin->w_topline;
1568 curwin->w_valid &=
1569 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1570 coladvance(curwin->w_curswant);
1571 }
1572}
1573
1574#ifdef FEAT_DIFF
1575/*
1576 * Don't end up with too many filler lines in the window.
1577 */
1578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001579check_topfill(
1580 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001581 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
1583 int n;
1584
1585 if (wp->w_topfill > 0)
1586 {
1587 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1588 if (wp->w_topfill + n > wp->w_height)
1589 {
1590 if (down && wp->w_topline > 1)
1591 {
1592 --wp->w_topline;
1593 wp->w_topfill = 0;
1594 }
1595 else
1596 {
1597 wp->w_topfill = wp->w_height - n;
1598 if (wp->w_topfill < 0)
1599 wp->w_topfill = 0;
1600 }
1601 }
1602 }
1603}
1604
1605/*
1606 * Use as many filler lines as possible for w_topline. Make sure w_topline
1607 * is still visible.
1608 */
1609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001610max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 int n;
1613
1614 n = plines_nofill(curwin->w_topline);
1615 if (n >= curwin->w_height)
1616 curwin->w_topfill = 0;
1617 else
1618 {
1619 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1620 if (curwin->w_topfill + n > curwin->w_height)
1621 curwin->w_topfill = curwin->w_height - n;
1622 }
1623}
1624#endif
1625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626/*
1627 * Scroll the screen one line down, but don't do it if it would move the
1628 * cursor off the screen.
1629 */
1630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001631scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633 int end_row;
1634#ifdef FEAT_DIFF
1635 int can_fill = (curwin->w_topfill
1636 < diff_check_fill(curwin, curwin->w_topline));
1637#endif
1638
1639 if (curwin->w_topline <= 1
1640#ifdef FEAT_DIFF
1641 && !can_fill
1642#endif
1643 )
1644 return;
1645
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 /*
1649 * Compute the row number of the last row of the cursor line
1650 * and make sure it doesn't go off the screen. Make sure the cursor
1651 * doesn't go past 'scrolloff' lines from the screen end.
1652 */
1653 end_row = curwin->w_wrow;
1654#ifdef FEAT_DIFF
1655 if (can_fill)
1656 ++end_row;
1657 else
1658 end_row += plines_nofill(curwin->w_topline - 1);
1659#else
1660 end_row += plines(curwin->w_topline - 1);
1661#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001662 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 validate_cheight();
1665 validate_virtcol();
1666 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001667 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001669 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671#ifdef FEAT_DIFF
1672 if (can_fill)
1673 {
1674 ++curwin->w_topfill;
1675 check_topfill(curwin, TRUE);
1676 }
1677 else
1678 {
1679 --curwin->w_topline;
1680 curwin->w_topfill = 0;
1681 }
1682#else
1683 --curwin->w_topline;
1684#endif
1685#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001686 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1690 }
1691}
1692
1693/*
1694 * Scroll the screen one line up, but don't do it if it would move the cursor
1695 * off the screen.
1696 */
1697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001698scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 int start_row;
1701
1702 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1703#ifdef FEAT_DIFF
1704 && curwin->w_topfill == 0
1705#endif
1706 )
1707 return;
1708
Bram Moolenaar85a20022019-12-21 18:25:54 +01001709 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 /*
1712 * Compute the row number of the first row of the cursor line
1713 * and make sure it doesn't go off the screen. Make sure the cursor
1714 * doesn't go before 'scrolloff' lines from the screen start.
1715 */
1716#ifdef FEAT_DIFF
1717 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1718 - curwin->w_topfill;
1719#else
1720 start_row = curwin->w_wrow - plines(curwin->w_topline);
1721#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001722 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001725 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001727 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
1729#ifdef FEAT_DIFF
1730 if (curwin->w_topfill > 0)
1731 --curwin->w_topfill;
1732 else
1733#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001734 {
1735#ifdef FEAT_FOLDING
1736 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001739 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1742 }
1743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745/*
1746 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1747 * a (wrapped) text line. Uses and sets "lp->fill".
1748 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001749 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 */
1751 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001752topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754#ifdef FEAT_DIFF
1755 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1756 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001757 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 ++lp->fill;
1759 lp->height = 1;
1760 }
1761 else
1762#endif
1763 {
1764 --lp->lnum;
1765#ifdef FEAT_DIFF
1766 lp->fill = 0;
1767#endif
1768 if (lp->lnum < 1)
1769 lp->height = MAXCOL;
1770 else
1771#ifdef FEAT_FOLDING
1772 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 lp->height = 1;
1775 else
1776#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001777 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779}
1780
1781/*
1782 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1783 * a (wrapped) text line. Uses and sets "lp->fill".
1784 * Returns the height of the added line in "lp->height".
1785 * Lines below the last one are incredibly high.
1786 */
1787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790#ifdef FEAT_DIFF
1791 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1792 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001793 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 ++lp->fill;
1795 lp->height = 1;
1796 }
1797 else
1798#endif
1799 {
1800 ++lp->lnum;
1801#ifdef FEAT_DIFF
1802 lp->fill = 0;
1803#endif
1804 if (lp->lnum > curbuf->b_ml.ml_line_count)
1805 lp->height = MAXCOL;
1806 else
1807#ifdef FEAT_FOLDING
1808 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001809 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 lp->height = 1;
1811 else
1812#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001813 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815}
1816
1817#ifdef FEAT_DIFF
1818/*
1819 * Switch from including filler lines below lp->lnum to including filler
1820 * lines above loff.lnum + 1. This keeps pointing to the same line.
1821 * When there are no filler lines nothing changes.
1822 */
1823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001824botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 if (lp->fill > 0)
1827 {
1828 ++lp->lnum;
1829 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1830 }
1831}
1832
1833/*
1834 * Switch from including filler lines above lp->lnum to including filler
1835 * lines below loff.lnum - 1. This keeps pointing to the same line.
1836 * When there are no filler lines nothing changes.
1837 */
1838 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001839topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 if (lp->fill > 0)
1842 {
1843 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1844 --lp->lnum;
1845 }
1846}
1847#endif
1848
1849/*
1850 * Recompute topline to put the cursor at the top of the window.
1851 * Scroll at least "min_scroll" lines.
1852 * If "always" is TRUE, always set topline (for "zt").
1853 */
1854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001855scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
1857 int scrolled = 0;
1858 int extra = 0;
1859 int used;
1860 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001861 linenr_T top; // just above displayed lines
1862 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 linenr_T old_topline = curwin->w_topline;
1864#ifdef FEAT_DIFF
1865 linenr_T old_topfill = curwin->w_topfill;
1866#endif
1867 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001868 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (mouse_dragging > 0)
1871 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 /*
1874 * Decrease topline until:
1875 * - it has become 1
1876 * - (part of) the cursor line is moved off the screen or
1877 * - moved at least 'scrolljump' lines and
1878 * - at least 'scrolloff' lines above and below the cursor
1879 */
1880 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001881 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (curwin->w_cursor.lnum < curwin->w_topline)
1883 scrolled = used;
1884
1885#ifdef FEAT_FOLDING
1886 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1887 {
1888 --top;
1889 ++bot;
1890 }
1891 else
1892#endif
1893 {
1894 top = curwin->w_cursor.lnum - 1;
1895 bot = curwin->w_cursor.lnum + 1;
1896 }
1897 new_topline = top + 1;
1898
1899#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001900 // "used" already contains the number of filler lines above, don't add it
1901 // again.
1902 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001903 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904#endif
1905
1906 /*
1907 * Check if the lines from "top" to "bot" fit in the window. If they do,
1908 * set new_topline and advance "top" and "bot" to include more lines.
1909 */
1910 while (top > 0)
1911 {
1912#ifdef FEAT_FOLDING
1913 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001914 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 i = 1;
1916 else
1917#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001918 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 used += i;
1920 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1921 {
1922#ifdef FEAT_FOLDING
1923 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001924 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 ++used;
1926 else
1927#endif
1928 used += plines(bot);
1929 }
1930 if (used > curwin->w_height)
1931 break;
1932 if (top < curwin->w_topline)
1933 scrolled += i;
1934
1935 /*
1936 * If scrolling is needed, scroll at least 'sj' lines.
1937 */
1938 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1939 && extra >= off)
1940 break;
1941
1942 extra += i;
1943 new_topline = top;
1944 --top;
1945 ++bot;
1946 }
1947
1948 /*
1949 * If we don't have enough space, put cursor in the middle.
1950 * This makes sure we get the same position when using "k" and "j"
1951 * in a small window.
1952 */
1953 if (used > curwin->w_height)
1954 scroll_cursor_halfway(FALSE);
1955 else
1956 {
1957 /*
1958 * If "always" is FALSE, only adjust topline to a lower value, higher
1959 * value may happen with wrapping lines
1960 */
1961 if (new_topline < curwin->w_topline || always)
1962 curwin->w_topline = new_topline;
1963 if (curwin->w_topline > curwin->w_cursor.lnum)
1964 curwin->w_topline = curwin->w_cursor.lnum;
1965#ifdef FEAT_DIFF
1966 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1967 if (curwin->w_topfill > 0 && extra > off)
1968 {
1969 curwin->w_topfill -= extra - off;
1970 if (curwin->w_topfill < 0)
1971 curwin->w_topfill = 0;
1972 }
1973 check_topfill(curwin, FALSE);
1974#endif
1975 if (curwin->w_topline != old_topline
1976#ifdef FEAT_DIFF
1977 || curwin->w_topfill != old_topfill
1978#endif
1979 )
1980 curwin->w_valid &=
1981 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1982 curwin->w_valid |= VALID_TOPLINE;
1983 }
1984}
1985
1986/*
1987 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1988 * screen lines for text lines.
1989 */
1990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001991set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993#ifdef FEAT_DIFF
1994 wp->w_filler_rows = 0;
1995#endif
1996 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001997 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 {
2000 wp->w_empty_rows = wp->w_height - used;
2001#ifdef FEAT_DIFF
2002 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2003 {
2004 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2005 if (wp->w_empty_rows > wp->w_filler_rows)
2006 wp->w_empty_rows -= wp->w_filler_rows;
2007 else
2008 {
2009 wp->w_filler_rows = wp->w_empty_rows;
2010 wp->w_empty_rows = 0;
2011 }
2012 }
2013#endif
2014 }
2015}
2016
2017/*
2018 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002019 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2021 * This is messy stuff!!!
2022 */
2023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002024scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 int used;
2027 int scrolled = 0;
2028 int extra = 0;
2029 int i;
2030 linenr_T line_count;
2031 linenr_T old_topline = curwin->w_topline;
2032 lineoff_T loff;
2033 lineoff_T boff;
2034#ifdef FEAT_DIFF
2035 int old_topfill = curwin->w_topfill;
2036 int fill_below_window;
2037#endif
2038 linenr_T old_botline = curwin->w_botline;
2039 linenr_T old_valid = curwin->w_valid;
2040 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002041 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002042 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 cln = curwin->w_cursor.lnum;
2045 if (set_topbot)
2046 {
2047 used = 0;
2048 curwin->w_botline = cln + 1;
2049#ifdef FEAT_DIFF
2050 loff.fill = 0;
2051#endif
2052 for (curwin->w_topline = curwin->w_botline;
2053 curwin->w_topline > 1;
2054 curwin->w_topline = loff.lnum)
2055 {
2056 loff.lnum = curwin->w_topline;
2057 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002058 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
2060 used += loff.height;
2061#ifdef FEAT_DIFF
2062 curwin->w_topfill = loff.fill;
2063#endif
2064 }
2065 set_empty_rows(curwin, used);
2066 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2067 if (curwin->w_topline != old_topline
2068#ifdef FEAT_DIFF
2069 || curwin->w_topfill != old_topfill
2070#endif
2071 )
2072 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2073 }
2074 else
2075 validate_botline();
2076
Bram Moolenaar85a20022019-12-21 18:25:54 +01002077 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef FEAT_DIFF
2079 used = plines_nofill(cln);
2080#else
2081 validate_cheight();
2082 used = curwin->w_cline_height;
2083#endif
2084
Bram Moolenaar85a20022019-12-21 18:25:54 +01002085 // If the cursor is below botline, we will at least scroll by the height
2086 // of the cursor line. Correct for empty lines, which are really part of
2087 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (cln >= curwin->w_botline)
2089 {
2090 scrolled = used;
2091 if (cln == curwin->w_botline)
2092 scrolled -= curwin->w_empty_rows;
2093 }
2094
2095 /*
2096 * Stop counting lines to scroll when
2097 * - hitting start of the file
2098 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002099 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 * - lines between botline and cursor have been counted
2101 */
2102#ifdef FEAT_FOLDING
2103 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2104#endif
2105 {
2106 loff.lnum = cln;
2107 boff.lnum = cln;
2108 }
2109#ifdef FEAT_DIFF
2110 loff.fill = 0;
2111 boff.fill = 0;
2112 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2113 - curwin->w_filler_rows;
2114#endif
2115
2116 while (loff.lnum > 1)
2117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2119 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002121 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2123 && loff.lnum <= curwin->w_botline
2124#ifdef FEAT_DIFF
2125 && (loff.lnum < curwin->w_botline
2126 || loff.fill >= fill_below_window)
2127#endif
2128 )
2129 break;
2130
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002133 if (loff.height == MAXCOL)
2134 used = MAXCOL;
2135 else
2136 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (used > curwin->w_height)
2138 break;
2139 if (loff.lnum >= curwin->w_botline
2140#ifdef FEAT_DIFF
2141 && (loff.lnum > curwin->w_botline
2142 || loff.fill <= fill_below_window)
2143#endif
2144 )
2145 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002146 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 scrolled += loff.height;
2148 if (loff.lnum == curwin->w_botline
2149#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002150 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#endif
2152 )
2153 scrolled -= curwin->w_empty_rows;
2154 }
2155
2156 if (boff.lnum < curbuf->b_ml.ml_line_count)
2157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002158 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 botline_forw(&boff);
2160 used += boff.height;
2161 if (used > curwin->w_height)
2162 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002163 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2164 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 extra += boff.height;
2167 if (boff.lnum >= curwin->w_botline
2168#ifdef FEAT_DIFF
2169 || (boff.lnum + 1 == curwin->w_botline
2170 && boff.fill > curwin->w_filler_rows)
2171#endif
2172 )
2173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002174 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 scrolled += boff.height;
2176 if (boff.lnum == curwin->w_botline
2177#ifdef FEAT_DIFF
2178 && boff.fill == 0
2179#endif
2180 )
2181 scrolled -= curwin->w_empty_rows;
2182 }
2183 }
2184 }
2185 }
2186
Bram Moolenaar85a20022019-12-21 18:25:54 +01002187 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (scrolled <= 0)
2189 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002190 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 else if (used > curwin->w_height)
2192 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002193 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 else
2195 {
2196 line_count = 0;
2197#ifdef FEAT_DIFF
2198 boff.fill = curwin->w_topfill;
2199#endif
2200 boff.lnum = curwin->w_topline - 1;
2201 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2202 {
2203 botline_forw(&boff);
2204 i += boff.height;
2205 ++line_count;
2206 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002207 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 line_count = 9999;
2209 }
2210
2211 /*
2212 * Scroll up if the cursor is off the bottom of the screen a bit.
2213 * Otherwise put it at 1/2 of the screen.
2214 */
2215 if (line_count >= curwin->w_height && line_count > min_scroll)
2216 scroll_cursor_halfway(FALSE);
2217 else
2218 scrollup(line_count, TRUE);
2219
2220 /*
2221 * If topline didn't change we need to restore w_botline and w_empty_rows
2222 * (we changed them).
2223 * If topline did change, update_screen() will set botline.
2224 */
2225 if (curwin->w_topline == old_topline && set_topbot)
2226 {
2227 curwin->w_botline = old_botline;
2228 curwin->w_empty_rows = old_empty_rows;
2229 curwin->w_valid = old_valid;
2230 }
2231 curwin->w_valid |= VALID_TOPLINE;
2232}
2233
2234/*
2235 * Recompute topline to put the cursor halfway the window
2236 * If "atend" is TRUE, also put it halfway at the end of the file.
2237 */
2238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 int above = 0;
2242 linenr_T topline;
2243#ifdef FEAT_DIFF
2244 int topfill = 0;
2245#endif
2246 int below = 0;
2247 int used;
2248 lineoff_T loff;
2249 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002250#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002251 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002254#ifdef FEAT_PROP_POPUP
2255 // if the width changed this needs to be updated first
2256 may_update_popup_position();
2257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2259#ifdef FEAT_FOLDING
2260 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2261#endif
2262#ifdef FEAT_DIFF
2263 used = plines_nofill(loff.lnum);
2264 loff.fill = 0;
2265 boff.fill = 0;
2266#else
2267 used = plines(loff.lnum);
2268#endif
2269 topline = loff.lnum;
2270 while (topline > 1)
2271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002272 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 if (boff.lnum < curbuf->b_ml.ml_line_count)
2275 {
2276 botline_forw(&boff);
2277 used += boff.height;
2278 if (used > curwin->w_height)
2279 break;
2280 below += boff.height;
2281 }
2282 else
2283 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002284 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (atend)
2286 ++used;
2287 }
2288 }
2289
Bram Moolenaar85a20022019-12-21 18:25:54 +01002290 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002293 if (loff.height == MAXCOL)
2294 used = MAXCOL;
2295 else
2296 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (used > curwin->w_height)
2298 break;
2299 above += loff.height;
2300 topline = loff.lnum;
2301#ifdef FEAT_DIFF
2302 topfill = loff.fill;
2303#endif
2304 }
2305 }
2306#ifdef FEAT_FOLDING
2307 if (!hasFolding(topline, &curwin->w_topline, NULL))
2308#endif
2309 curwin->w_topline = topline;
2310#ifdef FEAT_DIFF
2311 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002312 if (old_topline > curwin->w_topline + curwin->w_height)
2313 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 check_topfill(curwin, FALSE);
2315#endif
2316 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2317 curwin->w_valid |= VALID_TOPLINE;
2318}
2319
2320/*
2321 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002322 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * If not possible, put it at the same position as scroll_cursor_halfway().
2324 * When called topline must be valid!
2325 */
2326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002327cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002329 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002331 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 linenr_T botline;
2333 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002334 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002336 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /*
2339 * How many lines we would like to have above/below the cursor depends on
2340 * whether the first/last line of the file is on screen.
2341 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002342 above_wanted = so;
2343 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002344 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 above_wanted = mouse_dragging - 1;
2347 below_wanted = mouse_dragging - 1;
2348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (curwin->w_topline == 1)
2350 {
2351 above_wanted = 0;
2352 max_off = curwin->w_height / 2;
2353 if (below_wanted > max_off)
2354 below_wanted = max_off;
2355 }
2356 validate_botline();
2357 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002358 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 below_wanted = 0;
2361 max_off = (curwin->w_height - 1) / 2;
2362 if (above_wanted > max_off)
2363 above_wanted = max_off;
2364 }
2365
2366 /*
2367 * If there are sufficient file-lines above and below the cursor, we can
2368 * return now.
2369 */
2370 cln = curwin->w_cursor.lnum;
2371 if (cln >= curwin->w_topline + above_wanted
2372 && cln < curwin->w_botline - below_wanted
2373#ifdef FEAT_FOLDING
2374 && !hasAnyFolding(curwin)
2375#endif
2376 )
2377 return;
2378
2379 /*
2380 * Narrow down the area where the cursor can be put by taking lines from
2381 * the top and the bottom until:
2382 * - the desired context lines are found
2383 * - the lines from the top is past the lines from the bottom
2384 */
2385 topline = curwin->w_topline;
2386 botline = curwin->w_botline - 1;
2387#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 above = curwin->w_topfill;
2390 below = curwin->w_filler_rows;
2391#endif
2392 while ((above < above_wanted || below < below_wanted) && topline < botline)
2393 {
2394 if (below < below_wanted && (below <= above || above >= above_wanted))
2395 {
2396#ifdef FEAT_FOLDING
2397 if (hasFolding(botline, &botline, NULL))
2398 ++below;
2399 else
2400#endif
2401 below += plines(botline);
2402 --botline;
2403 }
2404 if (above < above_wanted && (above < below || below >= below_wanted))
2405 {
2406#ifdef FEAT_FOLDING
2407 if (hasFolding(topline, NULL, &topline))
2408 ++above;
2409 else
2410#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002411 above += PLINES_NOFILL(topline);
2412#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (topline < botline)
2415 above += diff_check_fill(curwin, topline + 1);
2416#endif
2417 ++topline;
2418 }
2419 }
2420 if (topline == botline || botline == 0)
2421 curwin->w_cursor.lnum = topline;
2422 else if (topline > botline)
2423 curwin->w_cursor.lnum = botline;
2424 else
2425 {
2426 if (cln < topline && curwin->w_topline > 1)
2427 {
2428 curwin->w_cursor.lnum = topline;
2429 curwin->w_valid &=
2430 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2431 }
2432 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2433 {
2434 curwin->w_cursor.lnum = botline;
2435 curwin->w_valid &=
2436 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2437 }
2438 }
2439 curwin->w_valid |= VALID_TOPLINE;
2440}
2441
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002442static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444/*
2445 * move screen 'count' pages up or down and update screen
2446 *
2447 * return FAIL for failure, OK otherwise
2448 */
2449 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002450onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 long n;
2453 int retval = OK;
2454 lineoff_T loff;
2455 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002456 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 beep_flush();
2461 return FAIL;
2462 }
2463
2464 for ( ; count > 0; --count)
2465 {
2466 validate_botline();
2467 /*
2468 * It's an error to move a page up when the first line is already on
2469 * the screen. It's an error to move a page down when the last line
2470 * is on the screen and the topline is 'scrolloff' lines from the
2471 * last line.
2472 */
2473 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002474 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2476 : (curwin->w_topline == 1
2477#ifdef FEAT_DIFF
2478 && curwin->w_topfill ==
2479 diff_check_fill(curwin, curwin->w_topline)
2480#endif
2481 ))
2482 {
2483 beep_flush();
2484 retval = FAIL;
2485 break;
2486 }
2487
2488#ifdef FEAT_DIFF
2489 loff.fill = 0;
2490#endif
2491 if (dir == FORWARD)
2492 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002493 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002495 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002496 if (p_window <= 2)
2497 ++curwin->w_topline;
2498 else
2499 curwin->w_topline += p_window - 2;
2500 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2501 curwin->w_topline = curbuf->b_ml.ml_line_count;
2502 curwin->w_cursor.lnum = curwin->w_topline;
2503 }
2504 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2505 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002506 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 curwin->w_topline = curbuf->b_ml.ml_line_count;
2508#ifdef FEAT_DIFF
2509 curwin->w_topfill = 0;
2510#endif
2511 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2512 }
2513 else
2514 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002515 // For the overlap, start with the line just below the window
2516 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 loff.lnum = curwin->w_botline;
2518#ifdef FEAT_DIFF
2519 loff.fill = diff_check_fill(curwin, loff.lnum)
2520 - curwin->w_filler_rows;
2521#endif
2522 get_scroll_overlap(&loff, -1);
2523 curwin->w_topline = loff.lnum;
2524#ifdef FEAT_DIFF
2525 curwin->w_topfill = loff.fill;
2526 check_topfill(curwin, FALSE);
2527#endif
2528 curwin->w_cursor.lnum = curwin->w_topline;
2529 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2530 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2531 }
2532 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535#ifdef FEAT_DIFF
2536 if (curwin->w_topline == 1)
2537 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 max_topfill();
2540 continue;
2541 }
2542#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002543 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002544 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002546 if (p_window <= 2)
2547 --curwin->w_topline;
2548 else
2549 curwin->w_topline -= p_window - 2;
2550 if (curwin->w_topline < 1)
2551 curwin->w_topline = 1;
2552 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2553 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2554 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2555 continue;
2556 }
2557
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 // Find the line at the top of the window that is going to be the
2559 // line at the bottom of the window. Make sure this results in
2560 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 loff.lnum = curwin->w_topline - 1;
2562#ifdef FEAT_DIFF
2563 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2564 - curwin->w_topfill;
2565#endif
2566 get_scroll_overlap(&loff, 1);
2567
2568 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2569 {
2570 loff.lnum = curbuf->b_ml.ml_line_count;
2571#ifdef FEAT_DIFF
2572 loff.fill = 0;
2573 }
2574 else
2575 {
2576 botline_topline(&loff);
2577#endif
2578 }
2579 curwin->w_cursor.lnum = loff.lnum;
2580
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // Find the line just above the new topline to get the right line
2582 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 n = 0;
2584 while (n <= curwin->w_height && loff.lnum >= 1)
2585 {
2586 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002587 if (loff.height == MAXCOL)
2588 n = MAXCOL;
2589 else
2590 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002592 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 curwin->w_topline = 1;
2595#ifdef FEAT_DIFF
2596 max_topfill();
2597#endif
2598 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2599 }
2600 else
2601 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002602 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#ifdef FEAT_DIFF
2604 topline_botline(&loff);
2605#endif
2606 botline_forw(&loff);
2607 botline_forw(&loff);
2608#ifdef FEAT_DIFF
2609 botline_topline(&loff);
2610#endif
2611#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002612 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2614#endif
2615
Bram Moolenaar85a20022019-12-21 18:25:54 +01002616 // Always scroll at least one line. Avoid getting stuck on
2617 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (loff.lnum >= curwin->w_topline
2619#ifdef FEAT_DIFF
2620 && (loff.lnum > curwin->w_topline
2621 || loff.fill >= curwin->w_topfill)
2622#endif
2623 )
2624 {
2625#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 // First try using the maximum number of filler lines. If
2627 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 loff.fill = curwin->w_topfill;
2629 if (curwin->w_topfill < diff_check_fill(curwin,
2630 curwin->w_topline))
2631 max_topfill();
2632 if (curwin->w_topfill == loff.fill)
2633#endif
2634 {
2635 --curwin->w_topline;
2636#ifdef FEAT_DIFF
2637 curwin->w_topfill = 0;
2638#endif
2639 }
2640 comp_botline(curwin);
2641 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002642 curwin->w_valid &=
2643 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645 else
2646 {
2647 curwin->w_topline = loff.lnum;
2648#ifdef FEAT_DIFF
2649 curwin->w_topfill = loff.fill;
2650 check_topfill(curwin, FALSE);
2651#endif
2652 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2653 }
2654 }
2655 }
2656 }
2657#ifdef FEAT_FOLDING
2658 foldAdjustCursor();
2659#endif
2660 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002661 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002662 if (retval == OK)
2663 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2665
Bram Moolenaar907dad72018-07-10 15:07:15 +02002666 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002668 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2669 // But make sure we scroll at least one line (happens with mix of long
2670 // wrapping lines and non-wrapping line).
2671 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002673 scroll_cursor_top(1, FALSE);
2674 if (curwin->w_topline <= old_topline
2675 && old_topline < curbuf->b_ml.ml_line_count)
2676 {
2677 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002679 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2680#endif
2681 }
2682 }
2683#ifdef FEAT_FOLDING
2684 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002689 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 return retval;
2691}
2692
2693/*
2694 * Decide how much overlap to use for page-up or page-down scrolling.
2695 * This is symmetric, so that doing both keeps the same lines displayed.
2696 * Three lines are examined:
2697 *
2698 * before CTRL-F after CTRL-F / before CTRL-B
2699 * etc. l1
2700 * l1 last but one line ------------
2701 * l2 last text line l2 top text line
2702 * ------------- l3 second text line
2703 * l3 etc.
2704 */
2705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002706get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 int h1, h2, h3, h4;
2709 int min_height = curwin->w_height - 2;
2710 lineoff_T loff0, loff1, loff2;
2711
2712#ifdef FEAT_DIFF
2713 if (lp->fill > 0)
2714 lp->height = 1;
2715 else
2716 lp->height = plines_nofill(lp->lnum);
2717#else
2718 lp->height = plines(lp->lnum);
2719#endif
2720 h1 = lp->height;
2721 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002722 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 loff0 = *lp;
2725 if (dir > 0)
2726 botline_forw(lp);
2727 else
2728 topline_back(lp);
2729 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002730 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002732 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return;
2734 }
2735
2736 loff1 = *lp;
2737 if (dir > 0)
2738 botline_forw(lp);
2739 else
2740 topline_back(lp);
2741 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002742 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002744 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return;
2746 }
2747
2748 loff2 = *lp;
2749 if (dir > 0)
2750 botline_forw(lp);
2751 else
2752 topline_back(lp);
2753 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002754 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002755 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002757 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758}
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
2761 * Scroll 'scroll' lines up or down.
2762 */
2763 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002764halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 long scrolled = 0;
2767 int i;
2768 int n;
2769 int room;
2770
2771 if (Prenum)
2772 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2773 curwin->w_height : Prenum;
2774 n = (curwin->w_p_scr <= curwin->w_height) ?
2775 curwin->w_p_scr : curwin->w_height;
2776
Bram Moolenaard5d37532017-03-27 23:02:07 +02002777 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 validate_botline();
2779 room = curwin->w_empty_rows;
2780#ifdef FEAT_DIFF
2781 room += curwin->w_filler_rows;
2782#endif
2783 if (flag)
2784 {
2785 /*
2786 * scroll the text up
2787 */
2788 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2789 {
2790#ifdef FEAT_DIFF
2791 if (curwin->w_topfill > 0)
2792 {
2793 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002794 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 --curwin->w_topfill;
2796 }
2797 else
2798#endif
2799 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002800 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 n -= i;
2802 if (n < 0 && scrolled > 0)
2803 break;
2804#ifdef FEAT_FOLDING
2805 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2806#endif
2807 ++curwin->w_topline;
2808#ifdef FEAT_DIFF
2809 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2810#endif
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2813 {
2814 ++curwin->w_cursor.lnum;
2815 curwin->w_valid &=
2816 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2820 scrolled += i;
2821
2822 /*
2823 * Correct w_botline for changed w_topline.
2824 * Won't work when there are filler lines.
2825 */
2826#ifdef FEAT_DIFF
2827 if (curwin->w_p_diff)
2828 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2829 else
2830#endif
2831 {
2832 room += i;
2833 do
2834 {
2835 i = plines(curwin->w_botline);
2836 if (i > room)
2837 break;
2838#ifdef FEAT_FOLDING
2839 (void)hasFolding(curwin->w_botline, NULL,
2840 &curwin->w_botline);
2841#endif
2842 ++curwin->w_botline;
2843 room -= i;
2844 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2845 }
2846 }
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 /*
2849 * When hit bottom of the file: move cursor down.
2850 */
2851 if (n > 0)
2852 {
2853# ifdef FEAT_FOLDING
2854 if (hasAnyFolding(curwin))
2855 {
2856 while (--n >= 0
2857 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2858 {
2859 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2860 &curwin->w_cursor.lnum);
2861 ++curwin->w_cursor.lnum;
2862 }
2863 }
2864 else
2865# endif
2866 curwin->w_cursor.lnum += n;
2867 check_cursor_lnum();
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870 else
2871 {
2872 /*
2873 * scroll the text down
2874 */
2875 while (n > 0 && curwin->w_topline > 1)
2876 {
2877#ifdef FEAT_DIFF
2878 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2879 {
2880 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002881 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 ++curwin->w_topfill;
2883 }
2884 else
2885#endif
2886 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002887 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 n -= i;
2889 if (n < 0 && scrolled > 0)
2890 break;
2891 --curwin->w_topline;
2892#ifdef FEAT_FOLDING
2893 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2894#endif
2895#ifdef FEAT_DIFF
2896 curwin->w_topfill = 0;
2897#endif
2898 }
2899 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2900 VALID_BOTLINE|VALID_BOTLINE_AP);
2901 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (curwin->w_cursor.lnum > 1)
2903 {
2904 --curwin->w_cursor.lnum;
2905 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 /*
2910 * When hit top of the file: move cursor up.
2911 */
2912 if (n > 0)
2913 {
2914 if (curwin->w_cursor.lnum <= (linenr_T)n)
2915 curwin->w_cursor.lnum = 1;
2916 else
2917# ifdef FEAT_FOLDING
2918 if (hasAnyFolding(curwin))
2919 {
2920 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2921 {
2922 --curwin->w_cursor.lnum;
2923 (void)hasFolding(curwin->w_cursor.lnum,
2924 &curwin->w_cursor.lnum, NULL);
2925 }
2926 }
2927 else
2928# endif
2929 curwin->w_cursor.lnum -= n;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002933 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 foldAdjustCursor();
2935# endif
2936#ifdef FEAT_DIFF
2937 check_topfill(curwin, !flag);
2938#endif
2939 cursor_correct();
2940 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002941 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002943
Bram Moolenaar860cae12010-06-05 23:22:07 +02002944 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002945do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002946{
2947 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002948 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002949 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002950 colnr_T curswant = curwin->w_curswant;
2951 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002952 win_T *old_curwin = curwin;
2953 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002954 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002955 int old_VIsual_select = VIsual_select;
2956 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002957
2958 /*
2959 * loop through the cursorbound windows
2960 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002961 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002962 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002963 {
2964 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002965 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966 if (curwin != old_curwin && curwin->w_p_crb)
2967 {
2968# ifdef FEAT_DIFF
2969 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002970 curwin->w_cursor.lnum =
2971 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002972 else
2973# endif
2974 curwin->w_cursor.lnum = line;
2975 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002976 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002977 curwin->w_curswant = curswant;
2978 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979
Bram Moolenaar85a20022019-12-21 18:25:54 +01002980 // Make sure the cursor is in a valid position. Temporarily set
2981 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002982 restart_edit_save = restart_edit;
2983 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002984 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01002985
2986 // Avoid a scroll here for the cursor position, 'scrollbind' is
2987 // more important.
2988 if (!curwin->w_p_scb)
2989 validate_cursor();
2990
Bram Moolenaar61452852011-02-01 18:01:11 +01002991 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002992 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002993 if (has_mbyte)
2994 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002995 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002996
Bram Moolenaar85a20022019-12-21 18:25:54 +01002997 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002998 if (!curwin->w_p_scb)
2999 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003000 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003001 }
3002 }
3003
3004 /*
3005 * reset current-window
3006 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003007 VIsual_select = old_VIsual_select;
3008 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003009 curwin = old_curwin;
3010 curbuf = old_curbuf;
3011}