blob: 9ec7798b9f2ef529c5ef994ba07ee6c8d82c7873 [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 */
984 update_topline();
985
986 /*
987 * Next make sure that w_cline_row is valid.
988 */
989 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100990 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100992#ifdef FEAT_PROP_POPUP
993 // will be set by getvvcol() but not reset
994 curwin->w_virtcol_first_char = 0;
995#endif
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /*
998 * Compute the number of virtual columns.
999 */
1000#ifdef FEAT_FOLDING
1001 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1004 else
1005#endif
1006 getvvcol(curwin, &curwin->w_cursor,
1007 &startcol, &(curwin->w_virtcol), &endcol);
1008
Bram Moolenaar85a20022019-12-21 18:25:54 +01001009 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001011 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 extra = curwin_col_off();
1014 curwin->w_wcol = curwin->w_virtcol + extra;
1015 endcol += extra;
1016
1017 /*
1018 * Now compute w_wrow, counting screen lines from w_cline_row.
1019 */
1020 curwin->w_wrow = curwin->w_cline_row;
1021
Bram Moolenaar02631462017-09-22 15:20:32 +02001022 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (textwidth <= 0)
1024 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001026 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001027 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001028 if (curwin->w_p_wrap)
1029 curwin->w_wrow = curwin->w_height - 1;
1030 else
1031 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001033 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 {
1035 width = textwidth + curwin_col_off2();
1036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001038 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001040#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001041 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001042#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001043
Bram Moolenaar85a20022019-12-21 18:25:54 +01001044 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001045 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 curwin->w_wcol -= n * width;
1047 curwin->w_wrow += n;
1048
1049#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001050 // When cursor wraps to first char of next line in Insert
1051 // mode, the 'showbreak' string isn't shown, backup to first
1052 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001053 sbr = get_showbreak_value(curwin);
1054 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001055 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 curwin->w_wcol = 0;
1057#endif
1058 }
1059 }
1060
Bram Moolenaar85a20022019-12-21 18:25:54 +01001061 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1062 // is not folded.
1063 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001064 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_FOLDING
1066 && !curwin->w_cline_folded
1067#endif
1068 )
1069 {
1070 /*
1071 * If Cursor is left of the screen, scroll rightwards.
1072 * If Cursor is right of the screen, scroll leftwards
1073 * If we get closer to the edge than 'sidescrolloff', scroll a little
1074 * extra
1075 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001076 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001077 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001078 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (off_left < 0 || off_right > 0)
1080 {
1081 if (off_left < 0)
1082 diff = -off_left;
1083 else
1084 diff = off_right;
1085
Bram Moolenaar85a20022019-12-21 18:25:54 +01001086 // When far off or not enough room on either side, put cursor in
1087 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1089 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1090 else
1091 {
1092 if (diff < p_ss)
1093 diff = p_ss;
1094 if (off_left < 0)
1095 new_leftcol = curwin->w_leftcol - diff;
1096 else
1097 new_leftcol = curwin->w_leftcol + diff;
1098 }
1099 if (new_leftcol < 0)
1100 new_leftcol = 0;
1101 if (new_leftcol != (int)curwin->w_leftcol)
1102 {
1103 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001104 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001105 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108 curwin->w_wcol -= curwin->w_leftcol;
1109 }
1110 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1111 curwin->w_wcol -= curwin->w_leftcol;
1112 else
1113 curwin->w_wcol = 0;
1114
1115#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 // Skip over filler lines. At the top use w_topfill, there
1117 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (curwin->w_cursor.lnum == curwin->w_topline)
1119 curwin->w_wrow += curwin->w_topfill;
1120 else
1121 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1122#endif
1123
1124 prev_skipcol = curwin->w_skipcol;
1125
1126 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if ((curwin->w_wrow >= curwin->w_height
1129 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001130 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 && (p_lines =
1132#ifdef FEAT_DIFF
1133 plines_win_nofill
1134#else
1135 plines_win
1136#endif
1137 (curwin, curwin->w_cursor.lnum, FALSE))
1138 - 1 >= curwin->w_height))
1139 && curwin->w_height != 0
1140 && curwin->w_cursor.lnum == curwin->w_topline
1141 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001142 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // Cursor past end of screen. Happens with a single line that does
1145 // not fit on screen. Find a skipcol to show the text around the
1146 // cursor. Avoid scrolling all the time. compute value of "extra":
1147 // 1: Less than 'scrolloff' lines above
1148 // 2: Less than 'scrolloff' lines below
1149 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001151 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // Compute last display line of the buffer line that we want at the
1154 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (p_lines == 0)
1156 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1157 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001158 if (p_lines > curwin->w_wrow + so)
1159 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 else
1161 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001162 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 extra += 2;
1164
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001165 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001167 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 n = curwin->w_virtcol / width;
1169 if (n > curwin->w_height / 2)
1170 n -= curwin->w_height / 2;
1171 else
1172 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (n > p_lines - curwin->w_height + 1)
1175 n = p_lines - curwin->w_height + 1;
1176 curwin->w_skipcol = n * width;
1177 }
1178 else if (extra == 1)
1179 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001180 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001181 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 + width - 1) / width;
1183 if (extra > 0)
1184 {
1185 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1186 extra = curwin->w_skipcol / width;
1187 curwin->w_skipcol -= extra * width;
1188 }
1189 }
1190 else if (extra == 2)
1191 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001192 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 endcol = (n - curwin->w_height + 1) * width;
1194 while (endcol > curwin->w_virtcol)
1195 endcol -= width;
1196 if (endcol > curwin->w_skipcol)
1197 curwin->w_skipcol = endcol;
1198 }
1199
1200 curwin->w_wrow -= curwin->w_skipcol / width;
1201 if (curwin->w_wrow >= curwin->w_height)
1202 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 extra = curwin->w_wrow - curwin->w_height + 1;
1205 curwin->w_skipcol += extra * width;
1206 curwin->w_wrow -= extra;
1207 }
1208
1209 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1210 if (extra > 0)
1211 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1212 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001213 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 else
1216 curwin->w_skipcol = 0;
1217 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001218 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001220#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001221 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001222#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001223#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1224 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1225 {
1226 curwin->w_wrow += popup_top_extra(curwin);
1227 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001228 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001229 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001230 else
1231 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001232#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001233
Bram Moolenaar08f23632019-11-16 14:19:33 +01001234 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1235 curwin->w_valid_leftcol = curwin->w_leftcol;
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1238}
1239
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001240#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001241/*
1242 * Compute the screen position of text character at "pos" in window "wp"
1243 * The resulting values are one-based, zero when character is not visible.
1244 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001245 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001246textpos2screenpos(
1247 win_T *wp,
1248 pos_T *pos,
1249 int *rowp, // screen row
1250 int *scolp, // start screen column
1251 int *ccolp, // cursor screen column
1252 int *ecolp) // end screen column
1253{
1254 colnr_T scol = 0, ccol = 0, ecol = 0;
1255 int row = 0;
1256 int rowoff = 0;
1257 colnr_T coloff = 0;
1258
Bram Moolenaar189663b2021-07-21 18:04:56 +02001259 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001260 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001261 colnr_T off;
1262 colnr_T col;
1263 int width;
1264 linenr_T lnum = pos->lnum;
1265#ifdef FEAT_FOLDING
1266 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001267
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001268 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1269#endif
1270 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1271#ifdef FEAT_FOLDING
1272 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001273 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001274 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001275 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001276 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001277 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001278#endif
1279 {
1280 getvcol(wp, pos, &scol, &ccol, &ecol);
1281
1282 // similar to what is done in validate_cursor_col()
1283 col = scol;
1284 off = win_col_off(wp);
1285 col += off;
1286 width = wp->w_width - off + win_col_off2(wp);
1287
1288 // long line wrapping, adjust row
1289 if (wp->w_p_wrap
1290 && col >= (colnr_T)wp->w_width
1291 && width > 0)
1292 {
1293 // use same formula as what is used in curs_columns()
1294 rowoff = ((col - wp->w_width) / width + 1);
1295 col -= rowoff * width;
1296 }
1297 col -= wp->w_leftcol;
1298 if (col >= wp->w_width)
1299 col = -1;
1300 if (col >= 0 && row + rowoff <= wp->w_height)
1301 {
1302 coloff = col - scol + wp->w_wincol + 1;
1303 row += W_WINROW(wp);
1304 }
1305 else
1306 // character is left, right or below of the window
1307 row = rowoff = scol = ccol = ecol = 0;
1308 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001309 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001310 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001311 *scolp = scol + coloff;
1312 *ccolp = ccol + coloff;
1313 *ecolp = ecol + coloff;
1314}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001315#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001316
Bram Moolenaar12034e22019-08-25 22:25:02 +02001317#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001318/*
1319 * "screenpos({winid}, {lnum}, {col})" function
1320 */
1321 void
1322f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1323{
1324 dict_T *dict;
1325 win_T *wp;
1326 pos_T pos;
1327 int row = 0;
1328 int scol = 0, ccol = 0, ecol = 0;
1329
Bram Moolenaar93a10962022-06-16 11:42:09 +01001330 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001331 return;
1332 dict = rettv->vval.v_dict;
1333
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001334 if (in_vim9script()
1335 && (check_for_number_arg(argvars, 0) == FAIL
1336 || check_for_number_arg(argvars, 1) == FAIL
1337 || check_for_number_arg(argvars, 2) == FAIL))
1338 return;
1339
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001340 wp = find_win_by_nr_or_id(&argvars[0]);
1341 if (wp == NULL)
1342 return;
1343
1344 pos.lnum = tv_get_number(&argvars[1]);
1345 pos.col = tv_get_number(&argvars[2]) - 1;
1346 pos.coladd = 0;
1347 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1348
1349 dict_add_number(dict, "row", row);
1350 dict_add_number(dict, "col", scol);
1351 dict_add_number(dict, "curscol", ccol);
1352 dict_add_number(dict, "endcol", ecol);
1353}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001354
1355/*
1356 * "virtcol2col({winid}, {lnum}, {col})" function
1357 */
1358 void
1359f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1360{
1361 win_T *wp;
1362 linenr_T lnum;
1363 int screencol;
1364 int error = FALSE;
1365
1366 rettv->vval.v_number = -1;
1367
1368 if (check_for_number_arg(argvars, 0) == FAIL
1369 || check_for_number_arg(argvars, 1) == FAIL
1370 || check_for_number_arg(argvars, 2) == FAIL)
1371 return;
1372
1373 wp = find_win_by_nr_or_id(&argvars[0]);
1374 if (wp == NULL)
1375 return;
1376
1377 lnum = tv_get_number_chk(&argvars[1], &error);
1378 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1379 return;
1380
1381 screencol = tv_get_number_chk(&argvars[2], &error);
1382 if (error || screencol < 0)
1383 return;
1384
1385 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1386}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001387#endif
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389/*
1390 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001393scrolldown(
1394 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001395 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 int wrow;
1399 int moved = FALSE;
1400
1401#ifdef FEAT_FOLDING
1402 linenr_T first;
1403
Bram Moolenaar85a20022019-12-21 18:25:54 +01001404 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1406#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001407 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 while (line_count-- > 0)
1409 {
1410#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001411 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1412 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
1414 ++curwin->w_topfill;
1415 ++done;
1416 }
1417 else
1418#endif
1419 {
1420 if (curwin->w_topline == 1)
1421 break;
1422 --curwin->w_topline;
1423#ifdef FEAT_DIFF
1424 curwin->w_topfill = 0;
1425#endif
1426#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001427 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (hasFolding(curwin->w_topline, &first, NULL))
1429 {
1430 ++done;
1431 if (!byfold)
1432 line_count -= curwin->w_topline - first - 1;
1433 curwin->w_botline -= curwin->w_topline - first;
1434 curwin->w_topline = first;
1435 }
1436 else
1437#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001438 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001440 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 invalidate_botline();
1442 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 curwin->w_wrow += done; // keep w_wrow updated
1444 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446#ifdef FEAT_DIFF
1447 if (curwin->w_cursor.lnum == curwin->w_topline)
1448 curwin->w_cline_row = 0;
1449 check_topfill(curwin, TRUE);
1450#endif
1451
1452 /*
1453 * Compute the row number of the last row of the cursor line
1454 * and move the cursor onto the displayed part of the window.
1455 */
1456 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001457 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 validate_virtcol();
1460 validate_cheight();
1461 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001462 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1465 {
1466#ifdef FEAT_FOLDING
1467 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1468 {
1469 --wrow;
1470 if (first == 1)
1471 curwin->w_cursor.lnum = 1;
1472 else
1473 curwin->w_cursor.lnum = first - 1;
1474 }
1475 else
1476#endif
1477 wrow -= plines(curwin->w_cursor.lnum--);
1478 curwin->w_valid &=
1479 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1480 moved = TRUE;
1481 }
1482 if (moved)
1483 {
1484#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001485 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 foldAdjustCursor();
1487#endif
1488 coladvance(curwin->w_curswant);
1489 }
1490}
1491
1492/*
1493 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001496scrollup(
1497 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001498 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1501 linenr_T lnum;
1502
1503 if (
1504# ifdef FEAT_FOLDING
1505 (byfold && hasAnyFolding(curwin))
1506# ifdef FEAT_DIFF
1507 ||
1508# endif
1509# endif
1510# ifdef FEAT_DIFF
1511 curwin->w_p_diff
1512# endif
1513 )
1514 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 lnum = curwin->w_topline;
1517 while (line_count--)
1518 {
1519# ifdef FEAT_DIFF
1520 if (curwin->w_topfill > 0)
1521 --curwin->w_topfill;
1522 else
1523# endif
1524 {
1525# ifdef FEAT_FOLDING
1526 if (byfold)
1527 (void)hasFolding(lnum, NULL, &lnum);
1528# endif
1529 if (lnum >= curbuf->b_ml.ml_line_count)
1530 break;
1531 ++lnum;
1532# ifdef FEAT_DIFF
1533 curwin->w_topfill = diff_check_fill(curwin, lnum);
1534# endif
1535 }
1536 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001537 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 curwin->w_botline += lnum - curwin->w_topline;
1539 curwin->w_topline = lnum;
1540 }
1541 else
1542#endif
1543 {
1544 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001545 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547
1548 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1549 curwin->w_topline = curbuf->b_ml.ml_line_count;
1550 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1551 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1552
1553#ifdef FEAT_DIFF
1554 check_topfill(curwin, FALSE);
1555#endif
1556
1557#ifdef FEAT_FOLDING
1558 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001559 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1561#endif
1562
1563 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1564 if (curwin->w_cursor.lnum < curwin->w_topline)
1565 {
1566 curwin->w_cursor.lnum = curwin->w_topline;
1567 curwin->w_valid &=
1568 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1569 coladvance(curwin->w_curswant);
1570 }
1571}
1572
1573#ifdef FEAT_DIFF
1574/*
1575 * Don't end up with too many filler lines in the window.
1576 */
1577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001578check_topfill(
1579 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001580 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 int n;
1583
1584 if (wp->w_topfill > 0)
1585 {
1586 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1587 if (wp->w_topfill + n > wp->w_height)
1588 {
1589 if (down && wp->w_topline > 1)
1590 {
1591 --wp->w_topline;
1592 wp->w_topfill = 0;
1593 }
1594 else
1595 {
1596 wp->w_topfill = wp->w_height - n;
1597 if (wp->w_topfill < 0)
1598 wp->w_topfill = 0;
1599 }
1600 }
1601 }
1602}
1603
1604/*
1605 * Use as many filler lines as possible for w_topline. Make sure w_topline
1606 * is still visible.
1607 */
1608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001609max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 int n;
1612
1613 n = plines_nofill(curwin->w_topline);
1614 if (n >= curwin->w_height)
1615 curwin->w_topfill = 0;
1616 else
1617 {
1618 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1619 if (curwin->w_topfill + n > curwin->w_height)
1620 curwin->w_topfill = curwin->w_height - n;
1621 }
1622}
1623#endif
1624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625/*
1626 * Scroll the screen one line down, but don't do it if it would move the
1627 * cursor off the screen.
1628 */
1629 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001630scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 int end_row;
1633#ifdef FEAT_DIFF
1634 int can_fill = (curwin->w_topfill
1635 < diff_check_fill(curwin, curwin->w_topline));
1636#endif
1637
1638 if (curwin->w_topline <= 1
1639#ifdef FEAT_DIFF
1640 && !can_fill
1641#endif
1642 )
1643 return;
1644
Bram Moolenaar85a20022019-12-21 18:25:54 +01001645 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 /*
1648 * Compute the row number of the last row of the cursor line
1649 * and make sure it doesn't go off the screen. Make sure the cursor
1650 * doesn't go past 'scrolloff' lines from the screen end.
1651 */
1652 end_row = curwin->w_wrow;
1653#ifdef FEAT_DIFF
1654 if (can_fill)
1655 ++end_row;
1656 else
1657 end_row += plines_nofill(curwin->w_topline - 1);
1658#else
1659 end_row += plines(curwin->w_topline - 1);
1660#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001661 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 validate_cheight();
1664 validate_virtcol();
1665 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001666 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001668 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670#ifdef FEAT_DIFF
1671 if (can_fill)
1672 {
1673 ++curwin->w_topfill;
1674 check_topfill(curwin, TRUE);
1675 }
1676 else
1677 {
1678 --curwin->w_topline;
1679 curwin->w_topfill = 0;
1680 }
1681#else
1682 --curwin->w_topline;
1683#endif
1684#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001685 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001687 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1689 }
1690}
1691
1692/*
1693 * Scroll the screen one line up, but don't do it if it would move the cursor
1694 * off the screen.
1695 */
1696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001697scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 int start_row;
1700
1701 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1702#ifdef FEAT_DIFF
1703 && curwin->w_topfill == 0
1704#endif
1705 )
1706 return;
1707
Bram Moolenaar85a20022019-12-21 18:25:54 +01001708 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 /*
1711 * Compute the row number of the first row of the cursor line
1712 * and make sure it doesn't go off the screen. Make sure the cursor
1713 * doesn't go before 'scrolloff' lines from the screen start.
1714 */
1715#ifdef FEAT_DIFF
1716 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1717 - curwin->w_topfill;
1718#else
1719 start_row = curwin->w_wrow - plines(curwin->w_topline);
1720#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001721 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
1723 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001724 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001726 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
1728#ifdef FEAT_DIFF
1729 if (curwin->w_topfill > 0)
1730 --curwin->w_topfill;
1731 else
1732#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001733 {
1734#ifdef FEAT_FOLDING
1735 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001738 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001739 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1741 }
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744/*
1745 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1746 * a (wrapped) text line. Uses and sets "lp->fill".
1747 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001748 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 */
1750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001751topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753#ifdef FEAT_DIFF
1754 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1755 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001756 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 ++lp->fill;
1758 lp->height = 1;
1759 }
1760 else
1761#endif
1762 {
1763 --lp->lnum;
1764#ifdef FEAT_DIFF
1765 lp->fill = 0;
1766#endif
1767 if (lp->lnum < 1)
1768 lp->height = MAXCOL;
1769 else
1770#ifdef FEAT_FOLDING
1771 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001772 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 lp->height = 1;
1774 else
1775#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001776 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 }
1778}
1779
1780/*
1781 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1782 * a (wrapped) text line. Uses and sets "lp->fill".
1783 * Returns the height of the added line in "lp->height".
1784 * Lines below the last one are incredibly high.
1785 */
1786 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001787botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789#ifdef FEAT_DIFF
1790 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1791 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 ++lp->fill;
1794 lp->height = 1;
1795 }
1796 else
1797#endif
1798 {
1799 ++lp->lnum;
1800#ifdef FEAT_DIFF
1801 lp->fill = 0;
1802#endif
1803 if (lp->lnum > curbuf->b_ml.ml_line_count)
1804 lp->height = MAXCOL;
1805 else
1806#ifdef FEAT_FOLDING
1807 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001808 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 lp->height = 1;
1810 else
1811#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001812 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814}
1815
1816#ifdef FEAT_DIFF
1817/*
1818 * Switch from including filler lines below lp->lnum to including filler
1819 * lines above loff.lnum + 1. This keeps pointing to the same line.
1820 * When there are no filler lines nothing changes.
1821 */
1822 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001823botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 if (lp->fill > 0)
1826 {
1827 ++lp->lnum;
1828 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1829 }
1830}
1831
1832/*
1833 * Switch from including filler lines above lp->lnum to including filler
1834 * lines below loff.lnum - 1. This keeps pointing to the same line.
1835 * When there are no filler lines nothing changes.
1836 */
1837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001838topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 if (lp->fill > 0)
1841 {
1842 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1843 --lp->lnum;
1844 }
1845}
1846#endif
1847
1848/*
1849 * Recompute topline to put the cursor at the top of the window.
1850 * Scroll at least "min_scroll" lines.
1851 * If "always" is TRUE, always set topline (for "zt").
1852 */
1853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001854scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 int scrolled = 0;
1857 int extra = 0;
1858 int used;
1859 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001860 linenr_T top; // just above displayed lines
1861 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 linenr_T old_topline = curwin->w_topline;
1863#ifdef FEAT_DIFF
1864 linenr_T old_topfill = curwin->w_topfill;
1865#endif
1866 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001867 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (mouse_dragging > 0)
1870 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 /*
1873 * Decrease topline until:
1874 * - it has become 1
1875 * - (part of) the cursor line is moved off the screen or
1876 * - moved at least 'scrolljump' lines and
1877 * - at least 'scrolloff' lines above and below the cursor
1878 */
1879 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001880 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (curwin->w_cursor.lnum < curwin->w_topline)
1882 scrolled = used;
1883
1884#ifdef FEAT_FOLDING
1885 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1886 {
1887 --top;
1888 ++bot;
1889 }
1890 else
1891#endif
1892 {
1893 top = curwin->w_cursor.lnum - 1;
1894 bot = curwin->w_cursor.lnum + 1;
1895 }
1896 new_topline = top + 1;
1897
1898#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001899 // "used" already contains the number of filler lines above, don't add it
1900 // again.
1901 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001902 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#endif
1904
1905 /*
1906 * Check if the lines from "top" to "bot" fit in the window. If they do,
1907 * set new_topline and advance "top" and "bot" to include more lines.
1908 */
1909 while (top > 0)
1910 {
1911#ifdef FEAT_FOLDING
1912 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001913 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 i = 1;
1915 else
1916#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001917 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 used += i;
1919 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1920 {
1921#ifdef FEAT_FOLDING
1922 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001923 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 ++used;
1925 else
1926#endif
1927 used += plines(bot);
1928 }
1929 if (used > curwin->w_height)
1930 break;
1931 if (top < curwin->w_topline)
1932 scrolled += i;
1933
1934 /*
1935 * If scrolling is needed, scroll at least 'sj' lines.
1936 */
1937 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1938 && extra >= off)
1939 break;
1940
1941 extra += i;
1942 new_topline = top;
1943 --top;
1944 ++bot;
1945 }
1946
1947 /*
1948 * If we don't have enough space, put cursor in the middle.
1949 * This makes sure we get the same position when using "k" and "j"
1950 * in a small window.
1951 */
1952 if (used > curwin->w_height)
1953 scroll_cursor_halfway(FALSE);
1954 else
1955 {
1956 /*
1957 * If "always" is FALSE, only adjust topline to a lower value, higher
1958 * value may happen with wrapping lines
1959 */
1960 if (new_topline < curwin->w_topline || always)
1961 curwin->w_topline = new_topline;
1962 if (curwin->w_topline > curwin->w_cursor.lnum)
1963 curwin->w_topline = curwin->w_cursor.lnum;
1964#ifdef FEAT_DIFF
1965 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1966 if (curwin->w_topfill > 0 && extra > off)
1967 {
1968 curwin->w_topfill -= extra - off;
1969 if (curwin->w_topfill < 0)
1970 curwin->w_topfill = 0;
1971 }
1972 check_topfill(curwin, FALSE);
1973#endif
1974 if (curwin->w_topline != old_topline
1975#ifdef FEAT_DIFF
1976 || curwin->w_topfill != old_topfill
1977#endif
1978 )
1979 curwin->w_valid &=
1980 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1981 curwin->w_valid |= VALID_TOPLINE;
1982 }
1983}
1984
1985/*
1986 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1987 * screen lines for text lines.
1988 */
1989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001990set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992#ifdef FEAT_DIFF
1993 wp->w_filler_rows = 0;
1994#endif
1995 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001996 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 else
1998 {
1999 wp->w_empty_rows = wp->w_height - used;
2000#ifdef FEAT_DIFF
2001 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2002 {
2003 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2004 if (wp->w_empty_rows > wp->w_filler_rows)
2005 wp->w_empty_rows -= wp->w_filler_rows;
2006 else
2007 {
2008 wp->w_filler_rows = wp->w_empty_rows;
2009 wp->w_empty_rows = 0;
2010 }
2011 }
2012#endif
2013 }
2014}
2015
2016/*
2017 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002018 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2020 * This is messy stuff!!!
2021 */
2022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002023scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int used;
2026 int scrolled = 0;
2027 int extra = 0;
2028 int i;
2029 linenr_T line_count;
2030 linenr_T old_topline = curwin->w_topline;
2031 lineoff_T loff;
2032 lineoff_T boff;
2033#ifdef FEAT_DIFF
2034 int old_topfill = curwin->w_topfill;
2035 int fill_below_window;
2036#endif
2037 linenr_T old_botline = curwin->w_botline;
2038 linenr_T old_valid = curwin->w_valid;
2039 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002040 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002041 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 cln = curwin->w_cursor.lnum;
2044 if (set_topbot)
2045 {
2046 used = 0;
2047 curwin->w_botline = cln + 1;
2048#ifdef FEAT_DIFF
2049 loff.fill = 0;
2050#endif
2051 for (curwin->w_topline = curwin->w_botline;
2052 curwin->w_topline > 1;
2053 curwin->w_topline = loff.lnum)
2054 {
2055 loff.lnum = curwin->w_topline;
2056 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002057 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 break;
2059 used += loff.height;
2060#ifdef FEAT_DIFF
2061 curwin->w_topfill = loff.fill;
2062#endif
2063 }
2064 set_empty_rows(curwin, used);
2065 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2066 if (curwin->w_topline != old_topline
2067#ifdef FEAT_DIFF
2068 || curwin->w_topfill != old_topfill
2069#endif
2070 )
2071 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2072 }
2073 else
2074 validate_botline();
2075
Bram Moolenaar85a20022019-12-21 18:25:54 +01002076 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#ifdef FEAT_DIFF
2078 used = plines_nofill(cln);
2079#else
2080 validate_cheight();
2081 used = curwin->w_cline_height;
2082#endif
2083
Bram Moolenaar85a20022019-12-21 18:25:54 +01002084 // If the cursor is below botline, we will at least scroll by the height
2085 // of the cursor line. Correct for empty lines, which are really part of
2086 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (cln >= curwin->w_botline)
2088 {
2089 scrolled = used;
2090 if (cln == curwin->w_botline)
2091 scrolled -= curwin->w_empty_rows;
2092 }
2093
2094 /*
2095 * Stop counting lines to scroll when
2096 * - hitting start of the file
2097 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002098 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 * - lines between botline and cursor have been counted
2100 */
2101#ifdef FEAT_FOLDING
2102 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2103#endif
2104 {
2105 loff.lnum = cln;
2106 boff.lnum = cln;
2107 }
2108#ifdef FEAT_DIFF
2109 loff.fill = 0;
2110 boff.fill = 0;
2111 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2112 - curwin->w_filler_rows;
2113#endif
2114
2115 while (loff.lnum > 1)
2116 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002117 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2118 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002120 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2122 && loff.lnum <= curwin->w_botline
2123#ifdef FEAT_DIFF
2124 && (loff.lnum < curwin->w_botline
2125 || loff.fill >= fill_below_window)
2126#endif
2127 )
2128 break;
2129
Bram Moolenaar85a20022019-12-21 18:25:54 +01002130 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002132 if (loff.height == MAXCOL)
2133 used = MAXCOL;
2134 else
2135 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (used > curwin->w_height)
2137 break;
2138 if (loff.lnum >= curwin->w_botline
2139#ifdef FEAT_DIFF
2140 && (loff.lnum > curwin->w_botline
2141 || loff.fill <= fill_below_window)
2142#endif
2143 )
2144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002145 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 scrolled += loff.height;
2147 if (loff.lnum == curwin->w_botline
2148#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002149 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150#endif
2151 )
2152 scrolled -= curwin->w_empty_rows;
2153 }
2154
2155 if (boff.lnum < curbuf->b_ml.ml_line_count)
2156 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 botline_forw(&boff);
2159 used += boff.height;
2160 if (used > curwin->w_height)
2161 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002162 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2163 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 extra += boff.height;
2166 if (boff.lnum >= curwin->w_botline
2167#ifdef FEAT_DIFF
2168 || (boff.lnum + 1 == curwin->w_botline
2169 && boff.fill > curwin->w_filler_rows)
2170#endif
2171 )
2172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002173 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 scrolled += boff.height;
2175 if (boff.lnum == curwin->w_botline
2176#ifdef FEAT_DIFF
2177 && boff.fill == 0
2178#endif
2179 )
2180 scrolled -= curwin->w_empty_rows;
2181 }
2182 }
2183 }
2184 }
2185
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (scrolled <= 0)
2188 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 else if (used > curwin->w_height)
2191 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else
2194 {
2195 line_count = 0;
2196#ifdef FEAT_DIFF
2197 boff.fill = curwin->w_topfill;
2198#endif
2199 boff.lnum = curwin->w_topline - 1;
2200 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2201 {
2202 botline_forw(&boff);
2203 i += boff.height;
2204 ++line_count;
2205 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 line_count = 9999;
2208 }
2209
2210 /*
2211 * Scroll up if the cursor is off the bottom of the screen a bit.
2212 * Otherwise put it at 1/2 of the screen.
2213 */
2214 if (line_count >= curwin->w_height && line_count > min_scroll)
2215 scroll_cursor_halfway(FALSE);
2216 else
2217 scrollup(line_count, TRUE);
2218
2219 /*
2220 * If topline didn't change we need to restore w_botline and w_empty_rows
2221 * (we changed them).
2222 * If topline did change, update_screen() will set botline.
2223 */
2224 if (curwin->w_topline == old_topline && set_topbot)
2225 {
2226 curwin->w_botline = old_botline;
2227 curwin->w_empty_rows = old_empty_rows;
2228 curwin->w_valid = old_valid;
2229 }
2230 curwin->w_valid |= VALID_TOPLINE;
2231}
2232
2233/*
2234 * Recompute topline to put the cursor halfway the window
2235 * If "atend" is TRUE, also put it halfway at the end of the file.
2236 */
2237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002238scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 int above = 0;
2241 linenr_T topline;
2242#ifdef FEAT_DIFF
2243 int topfill = 0;
2244#endif
2245 int below = 0;
2246 int used;
2247 lineoff_T loff;
2248 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002249#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002250 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002253#ifdef FEAT_PROP_POPUP
2254 // if the width changed this needs to be updated first
2255 may_update_popup_position();
2256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2258#ifdef FEAT_FOLDING
2259 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2260#endif
2261#ifdef FEAT_DIFF
2262 used = plines_nofill(loff.lnum);
2263 loff.fill = 0;
2264 boff.fill = 0;
2265#else
2266 used = plines(loff.lnum);
2267#endif
2268 topline = loff.lnum;
2269 while (topline > 1)
2270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002271 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (boff.lnum < curbuf->b_ml.ml_line_count)
2274 {
2275 botline_forw(&boff);
2276 used += boff.height;
2277 if (used > curwin->w_height)
2278 break;
2279 below += boff.height;
2280 }
2281 else
2282 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002283 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (atend)
2285 ++used;
2286 }
2287 }
2288
Bram Moolenaar85a20022019-12-21 18:25:54 +01002289 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002292 if (loff.height == MAXCOL)
2293 used = MAXCOL;
2294 else
2295 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (used > curwin->w_height)
2297 break;
2298 above += loff.height;
2299 topline = loff.lnum;
2300#ifdef FEAT_DIFF
2301 topfill = loff.fill;
2302#endif
2303 }
2304 }
2305#ifdef FEAT_FOLDING
2306 if (!hasFolding(topline, &curwin->w_topline, NULL))
2307#endif
2308 curwin->w_topline = topline;
2309#ifdef FEAT_DIFF
2310 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002311 if (old_topline > curwin->w_topline + curwin->w_height)
2312 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 check_topfill(curwin, FALSE);
2314#endif
2315 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2316 curwin->w_valid |= VALID_TOPLINE;
2317}
2318
2319/*
2320 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002321 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * If not possible, put it at the same position as scroll_cursor_halfway().
2323 * When called topline must be valid!
2324 */
2325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002326cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002328 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002330 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 linenr_T botline;
2332 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002335 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /*
2338 * How many lines we would like to have above/below the cursor depends on
2339 * whether the first/last line of the file is on screen.
2340 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002341 above_wanted = so;
2342 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002343 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 above_wanted = mouse_dragging - 1;
2346 below_wanted = mouse_dragging - 1;
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (curwin->w_topline == 1)
2349 {
2350 above_wanted = 0;
2351 max_off = curwin->w_height / 2;
2352 if (below_wanted > max_off)
2353 below_wanted = max_off;
2354 }
2355 validate_botline();
2356 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002357 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 below_wanted = 0;
2360 max_off = (curwin->w_height - 1) / 2;
2361 if (above_wanted > max_off)
2362 above_wanted = max_off;
2363 }
2364
2365 /*
2366 * If there are sufficient file-lines above and below the cursor, we can
2367 * return now.
2368 */
2369 cln = curwin->w_cursor.lnum;
2370 if (cln >= curwin->w_topline + above_wanted
2371 && cln < curwin->w_botline - below_wanted
2372#ifdef FEAT_FOLDING
2373 && !hasAnyFolding(curwin)
2374#endif
2375 )
2376 return;
2377
2378 /*
2379 * Narrow down the area where the cursor can be put by taking lines from
2380 * the top and the bottom until:
2381 * - the desired context lines are found
2382 * - the lines from the top is past the lines from the bottom
2383 */
2384 topline = curwin->w_topline;
2385 botline = curwin->w_botline - 1;
2386#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 above = curwin->w_topfill;
2389 below = curwin->w_filler_rows;
2390#endif
2391 while ((above < above_wanted || below < below_wanted) && topline < botline)
2392 {
2393 if (below < below_wanted && (below <= above || above >= above_wanted))
2394 {
2395#ifdef FEAT_FOLDING
2396 if (hasFolding(botline, &botline, NULL))
2397 ++below;
2398 else
2399#endif
2400 below += plines(botline);
2401 --botline;
2402 }
2403 if (above < above_wanted && (above < below || below >= below_wanted))
2404 {
2405#ifdef FEAT_FOLDING
2406 if (hasFolding(topline, NULL, &topline))
2407 ++above;
2408 else
2409#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002410 above += PLINES_NOFILL(topline);
2411#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002412 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (topline < botline)
2414 above += diff_check_fill(curwin, topline + 1);
2415#endif
2416 ++topline;
2417 }
2418 }
2419 if (topline == botline || botline == 0)
2420 curwin->w_cursor.lnum = topline;
2421 else if (topline > botline)
2422 curwin->w_cursor.lnum = botline;
2423 else
2424 {
2425 if (cln < topline && curwin->w_topline > 1)
2426 {
2427 curwin->w_cursor.lnum = topline;
2428 curwin->w_valid &=
2429 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2430 }
2431 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2432 {
2433 curwin->w_cursor.lnum = botline;
2434 curwin->w_valid &=
2435 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2436 }
2437 }
2438 curwin->w_valid |= VALID_TOPLINE;
2439}
2440
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002441static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443/*
2444 * move screen 'count' pages up or down and update screen
2445 *
2446 * return FAIL for failure, OK otherwise
2447 */
2448 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002449onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
2451 long n;
2452 int retval = OK;
2453 lineoff_T loff;
2454 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002455 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar85a20022019-12-21 18:25:54 +01002457 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 beep_flush();
2460 return FAIL;
2461 }
2462
2463 for ( ; count > 0; --count)
2464 {
2465 validate_botline();
2466 /*
2467 * It's an error to move a page up when the first line is already on
2468 * the screen. It's an error to move a page down when the last line
2469 * is on the screen and the topline is 'scrolloff' lines from the
2470 * last line.
2471 */
2472 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002473 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2475 : (curwin->w_topline == 1
2476#ifdef FEAT_DIFF
2477 && curwin->w_topfill ==
2478 diff_check_fill(curwin, curwin->w_topline)
2479#endif
2480 ))
2481 {
2482 beep_flush();
2483 retval = FAIL;
2484 break;
2485 }
2486
2487#ifdef FEAT_DIFF
2488 loff.fill = 0;
2489#endif
2490 if (dir == FORWARD)
2491 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002492 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002495 if (p_window <= 2)
2496 ++curwin->w_topline;
2497 else
2498 curwin->w_topline += p_window - 2;
2499 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2500 curwin->w_topline = curbuf->b_ml.ml_line_count;
2501 curwin->w_cursor.lnum = curwin->w_topline;
2502 }
2503 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2504 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002505 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 curwin->w_topline = curbuf->b_ml.ml_line_count;
2507#ifdef FEAT_DIFF
2508 curwin->w_topfill = 0;
2509#endif
2510 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2511 }
2512 else
2513 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002514 // For the overlap, start with the line just below the window
2515 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 loff.lnum = curwin->w_botline;
2517#ifdef FEAT_DIFF
2518 loff.fill = diff_check_fill(curwin, loff.lnum)
2519 - curwin->w_filler_rows;
2520#endif
2521 get_scroll_overlap(&loff, -1);
2522 curwin->w_topline = loff.lnum;
2523#ifdef FEAT_DIFF
2524 curwin->w_topfill = loff.fill;
2525 check_topfill(curwin, FALSE);
2526#endif
2527 curwin->w_cursor.lnum = curwin->w_topline;
2528 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2529 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2530 }
2531 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002532 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534#ifdef FEAT_DIFF
2535 if (curwin->w_topline == 1)
2536 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002537 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 max_topfill();
2539 continue;
2540 }
2541#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002542 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002543 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002545 if (p_window <= 2)
2546 --curwin->w_topline;
2547 else
2548 curwin->w_topline -= p_window - 2;
2549 if (curwin->w_topline < 1)
2550 curwin->w_topline = 1;
2551 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2552 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2553 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2554 continue;
2555 }
2556
Bram Moolenaar85a20022019-12-21 18:25:54 +01002557 // Find the line at the top of the window that is going to be the
2558 // line at the bottom of the window. Make sure this results in
2559 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 loff.lnum = curwin->w_topline - 1;
2561#ifdef FEAT_DIFF
2562 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2563 - curwin->w_topfill;
2564#endif
2565 get_scroll_overlap(&loff, 1);
2566
2567 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2568 {
2569 loff.lnum = curbuf->b_ml.ml_line_count;
2570#ifdef FEAT_DIFF
2571 loff.fill = 0;
2572 }
2573 else
2574 {
2575 botline_topline(&loff);
2576#endif
2577 }
2578 curwin->w_cursor.lnum = loff.lnum;
2579
Bram Moolenaar85a20022019-12-21 18:25:54 +01002580 // Find the line just above the new topline to get the right line
2581 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 n = 0;
2583 while (n <= curwin->w_height && loff.lnum >= 1)
2584 {
2585 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002586 if (loff.height == MAXCOL)
2587 n = MAXCOL;
2588 else
2589 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 curwin->w_topline = 1;
2594#ifdef FEAT_DIFF
2595 max_topfill();
2596#endif
2597 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2598 }
2599 else
2600 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002601 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#ifdef FEAT_DIFF
2603 topline_botline(&loff);
2604#endif
2605 botline_forw(&loff);
2606 botline_forw(&loff);
2607#ifdef FEAT_DIFF
2608 botline_topline(&loff);
2609#endif
2610#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002611 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2613#endif
2614
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 // Always scroll at least one line. Avoid getting stuck on
2616 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (loff.lnum >= curwin->w_topline
2618#ifdef FEAT_DIFF
2619 && (loff.lnum > curwin->w_topline
2620 || loff.fill >= curwin->w_topfill)
2621#endif
2622 )
2623 {
2624#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // First try using the maximum number of filler lines. If
2626 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 loff.fill = curwin->w_topfill;
2628 if (curwin->w_topfill < diff_check_fill(curwin,
2629 curwin->w_topline))
2630 max_topfill();
2631 if (curwin->w_topfill == loff.fill)
2632#endif
2633 {
2634 --curwin->w_topline;
2635#ifdef FEAT_DIFF
2636 curwin->w_topfill = 0;
2637#endif
2638 }
2639 comp_botline(curwin);
2640 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002641 curwin->w_valid &=
2642 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 else
2645 {
2646 curwin->w_topline = loff.lnum;
2647#ifdef FEAT_DIFF
2648 curwin->w_topfill = loff.fill;
2649 check_topfill(curwin, FALSE);
2650#endif
2651 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2652 }
2653 }
2654 }
2655 }
2656#ifdef FEAT_FOLDING
2657 foldAdjustCursor();
2658#endif
2659 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002660 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002661 if (retval == OK)
2662 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2664
Bram Moolenaar907dad72018-07-10 15:07:15 +02002665 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002667 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2668 // But make sure we scroll at least one line (happens with mix of long
2669 // wrapping lines and non-wrapping line).
2670 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002672 scroll_cursor_top(1, FALSE);
2673 if (curwin->w_topline <= old_topline
2674 && old_topline < curbuf->b_ml.ml_line_count)
2675 {
2676 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002678 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2679#endif
2680 }
2681 }
2682#ifdef FEAT_FOLDING
2683 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002688 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 return retval;
2690}
2691
2692/*
2693 * Decide how much overlap to use for page-up or page-down scrolling.
2694 * This is symmetric, so that doing both keeps the same lines displayed.
2695 * Three lines are examined:
2696 *
2697 * before CTRL-F after CTRL-F / before CTRL-B
2698 * etc. l1
2699 * l1 last but one line ------------
2700 * l2 last text line l2 top text line
2701 * ------------- l3 second text line
2702 * l3 etc.
2703 */
2704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002705get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 int h1, h2, h3, h4;
2708 int min_height = curwin->w_height - 2;
2709 lineoff_T loff0, loff1, loff2;
2710
2711#ifdef FEAT_DIFF
2712 if (lp->fill > 0)
2713 lp->height = 1;
2714 else
2715 lp->height = plines_nofill(lp->lnum);
2716#else
2717 lp->height = plines(lp->lnum);
2718#endif
2719 h1 = lp->height;
2720 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 loff0 = *lp;
2724 if (dir > 0)
2725 botline_forw(lp);
2726 else
2727 topline_back(lp);
2728 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002729 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 return;
2733 }
2734
2735 loff1 = *lp;
2736 if (dir > 0)
2737 botline_forw(lp);
2738 else
2739 topline_back(lp);
2740 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002741 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002743 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 return;
2745 }
2746
2747 loff2 = *lp;
2748 if (dir > 0)
2749 botline_forw(lp);
2750 else
2751 topline_back(lp);
2752 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002753 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002754 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002756 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757}
2758
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759/*
2760 * Scroll 'scroll' lines up or down.
2761 */
2762 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002763halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 long scrolled = 0;
2766 int i;
2767 int n;
2768 int room;
2769
2770 if (Prenum)
2771 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2772 curwin->w_height : Prenum;
2773 n = (curwin->w_p_scr <= curwin->w_height) ?
2774 curwin->w_p_scr : curwin->w_height;
2775
Bram Moolenaard5d37532017-03-27 23:02:07 +02002776 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 validate_botline();
2778 room = curwin->w_empty_rows;
2779#ifdef FEAT_DIFF
2780 room += curwin->w_filler_rows;
2781#endif
2782 if (flag)
2783 {
2784 /*
2785 * scroll the text up
2786 */
2787 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2788 {
2789#ifdef FEAT_DIFF
2790 if (curwin->w_topfill > 0)
2791 {
2792 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002793 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 --curwin->w_topfill;
2795 }
2796 else
2797#endif
2798 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002799 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 n -= i;
2801 if (n < 0 && scrolled > 0)
2802 break;
2803#ifdef FEAT_FOLDING
2804 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2805#endif
2806 ++curwin->w_topline;
2807#ifdef FEAT_DIFF
2808 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2809#endif
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2812 {
2813 ++curwin->w_cursor.lnum;
2814 curwin->w_valid &=
2815 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
2818 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2819 scrolled += i;
2820
2821 /*
2822 * Correct w_botline for changed w_topline.
2823 * Won't work when there are filler lines.
2824 */
2825#ifdef FEAT_DIFF
2826 if (curwin->w_p_diff)
2827 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2828 else
2829#endif
2830 {
2831 room += i;
2832 do
2833 {
2834 i = plines(curwin->w_botline);
2835 if (i > room)
2836 break;
2837#ifdef FEAT_FOLDING
2838 (void)hasFolding(curwin->w_botline, NULL,
2839 &curwin->w_botline);
2840#endif
2841 ++curwin->w_botline;
2842 room -= i;
2843 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2844 }
2845 }
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 /*
2848 * When hit bottom of the file: move cursor down.
2849 */
2850 if (n > 0)
2851 {
2852# ifdef FEAT_FOLDING
2853 if (hasAnyFolding(curwin))
2854 {
2855 while (--n >= 0
2856 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2857 {
2858 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2859 &curwin->w_cursor.lnum);
2860 ++curwin->w_cursor.lnum;
2861 }
2862 }
2863 else
2864# endif
2865 curwin->w_cursor.lnum += n;
2866 check_cursor_lnum();
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 else
2870 {
2871 /*
2872 * scroll the text down
2873 */
2874 while (n > 0 && curwin->w_topline > 1)
2875 {
2876#ifdef FEAT_DIFF
2877 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2878 {
2879 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002880 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 ++curwin->w_topfill;
2882 }
2883 else
2884#endif
2885 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002886 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 n -= i;
2888 if (n < 0 && scrolled > 0)
2889 break;
2890 --curwin->w_topline;
2891#ifdef FEAT_FOLDING
2892 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2893#endif
2894#ifdef FEAT_DIFF
2895 curwin->w_topfill = 0;
2896#endif
2897 }
2898 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2899 VALID_BOTLINE|VALID_BOTLINE_AP);
2900 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (curwin->w_cursor.lnum > 1)
2902 {
2903 --curwin->w_cursor.lnum;
2904 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /*
2909 * When hit top of the file: move cursor up.
2910 */
2911 if (n > 0)
2912 {
2913 if (curwin->w_cursor.lnum <= (linenr_T)n)
2914 curwin->w_cursor.lnum = 1;
2915 else
2916# ifdef FEAT_FOLDING
2917 if (hasAnyFolding(curwin))
2918 {
2919 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2920 {
2921 --curwin->w_cursor.lnum;
2922 (void)hasFolding(curwin->w_cursor.lnum,
2923 &curwin->w_cursor.lnum, NULL);
2924 }
2925 }
2926 else
2927# endif
2928 curwin->w_cursor.lnum -= n;
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 foldAdjustCursor();
2934# endif
2935#ifdef FEAT_DIFF
2936 check_topfill(curwin, !flag);
2937#endif
2938 cursor_correct();
2939 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002940 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002942
Bram Moolenaar860cae12010-06-05 23:22:07 +02002943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002944do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002945{
2946 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002947 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002948 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002949 colnr_T curswant = curwin->w_curswant;
2950 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002951 win_T *old_curwin = curwin;
2952 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002953 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002954 int old_VIsual_select = VIsual_select;
2955 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002956
2957 /*
2958 * loop through the cursorbound windows
2959 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002960 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002961 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002962 {
2963 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002964 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002965 if (curwin != old_curwin && curwin->w_p_crb)
2966 {
2967# ifdef FEAT_DIFF
2968 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002969 curwin->w_cursor.lnum =
2970 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002971 else
2972# endif
2973 curwin->w_cursor.lnum = line;
2974 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002975 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002976 curwin->w_curswant = curswant;
2977 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002978
Bram Moolenaar85a20022019-12-21 18:25:54 +01002979 // Make sure the cursor is in a valid position. Temporarily set
2980 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002981 restart_edit_save = restart_edit;
2982 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002983 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01002984
2985 // Avoid a scroll here for the cursor position, 'scrollbind' is
2986 // more important.
2987 if (!curwin->w_p_scb)
2988 validate_cursor();
2989
Bram Moolenaar61452852011-02-01 18:01:11 +01002990 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002991 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002992 if (has_mbyte)
2993 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002994 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002995
Bram Moolenaar85a20022019-12-21 18:25:54 +01002996 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002997 if (!curwin->w_p_scb)
2998 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002999 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003000 }
3001 }
3002
3003 /*
3004 * reset current-window
3005 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003006 VIsual_select = old_VIsual_select;
3007 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003008 curwin = old_curwin;
3009 curbuf = old_curbuf;
3010}