blob: a51d2d128e86df2554024d7150ab3dad2c5e6632 [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
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100473/*
474 * Update w_curswant.
475 */
476 void
477update_curswant_force(void)
478{
479 validate_virtcol();
480 curwin->w_curswant = curwin->w_virtcol
481#ifdef FEAT_PROP_POPUP
482 - curwin->w_virtcol_first_char
483#endif
484 ;
485 curwin->w_set_curswant = FALSE;
486}
487
488/*
489 * Update w_curswant if w_set_curswant is set.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100495 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496}
497
498/*
499 * Check if the cursor has moved. Set the w_valid flag accordingly.
500 */
501 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100502check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
505 {
506 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100507 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
508 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 wp->w_valid_cursor = wp->w_cursor;
510 wp->w_valid_leftcol = wp->w_leftcol;
511 }
512 else if (wp->w_cursor.col != wp->w_valid_cursor.col
513 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100514 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
517 wp->w_valid_cursor.col = wp->w_cursor.col;
518 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521}
522
523/*
524 * Call this function when some window settings have changed, which require
525 * the cursor position, botline and topline to be recomputed and the window to
526 * be redrawn. E.g, when changing the 'wrap' option or folding.
527 */
528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100529changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 changed_window_setting_win(curwin);
532}
533
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 wp->w_lines_valid = 0;
538 changed_line_abv_curs_win(wp);
539 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100540 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
542
543/*
544 * Set wp->w_topline to a certain number.
545 */
546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200549#ifdef FEAT_DIFF
550 linenr_T prev_topline = wp->w_topline;
551#endif
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100554 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
556#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100559 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
560 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000562 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200564 if (lnum != prev_topline)
565 // Keep the filler lines when the topline didn't change.
566 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
568 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100569 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100570 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571}
572
573/*
574 * Call this function when the length of the cursor line (in screen
575 * characters) has changed, and the change is before the cursor.
576 * Need to take care of w_botline separately!
577 */
578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
582 |VALID_CHEIGHT|VALID_TOPLINE);
583}
584
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592/*
593 * Call this function when the length of a line (in screen characters) above
594 * the cursor have changed.
595 * Need to take care of w_botline separately!
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
601 |VALID_CHEIGHT|VALID_TOPLINE);
602}
603
604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100605changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
608 |VALID_CHEIGHT|VALID_TOPLINE);
609}
610
611/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100612 * Display of line has changed for "buf", invalidate cursor position and
613 * w_botline.
614 */
615 void
616changed_line_display_buf(buf_T *buf)
617{
618 win_T *wp;
619
620 FOR_ALL_WINDOWS(wp)
621 if (wp->w_buffer == buf)
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
623 |VALID_CROW|VALID_CHEIGHT
624 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
625}
626
627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 * Make sure the value of curwin->w_botline is valid.
629 */
630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100633 validate_botline_win(curwin);
634}
635
636/*
637 * Make sure the value of wp->w_botline is valid.
638 */
639 void
640validate_botline_win(win_T *wp)
641{
642 if (!(wp->w_valid & VALID_BOTLINE))
643 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644}
645
646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * Mark curwin->w_botline as invalid (because of some change in the buffer).
648 */
649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
653}
654
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
659}
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662approximate_botline_win(
663 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 wp->w_valid &= ~VALID_BOTLINE;
666}
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668/*
669 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
670 */
671 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100672cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 check_cursor_moved(curwin);
675 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
676 (VALID_WROW|VALID_WCOL));
677}
678
679/*
680 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
681 * w_topline must be valid, you may need to call update_topline() first!
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 check_cursor_moved(curwin);
687 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
688 curs_columns(TRUE);
689}
690
691#if defined(FEAT_GUI) || defined(PROTO)
692/*
693 * validate w_cline_row.
694 */
695 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100696validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
698 /*
699 * First make sure that w_topline is valid (after moving the cursor).
700 */
701 update_topline();
702 check_cursor_moved(curwin);
703 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100704 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705}
706#endif
707
708/*
709 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200710 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 */
712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100713curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 linenr_T lnum;
716 int i;
717 int all_invalid;
718 int valid;
719#ifdef FEAT_FOLDING
720 long fold_count;
721#endif
722
Bram Moolenaar85a20022019-12-21 18:25:54 +0100723 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 all_invalid = (!redrawing()
725 || wp->w_lines_valid == 0
726 || wp->w_lines[0].wl_lnum > wp->w_topline);
727 i = 0;
728 wp->w_cline_row = 0;
729 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
730 {
731 valid = FALSE;
732 if (!all_invalid && i < wp->w_lines_valid)
733 {
734 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100735 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 if (wp->w_lines[i].wl_lnum == lnum)
737 {
738#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100739 // Check for newly inserted lines below this row, in which
740 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (!wp->w_buffer->b_mod_set
742 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
743 || wp->w_buffer->b_mod_top
744 > wp->w_lines[i].wl_lastlnum + 1)
745#endif
746 valid = TRUE;
747 }
748 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 if (valid
752#ifdef FEAT_DIFF
753 && (lnum != wp->w_topline || !wp->w_p_diff)
754#endif
755 )
756 {
757#ifdef FEAT_FOLDING
758 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100759 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (lnum > wp->w_cursor.lnum)
761 break;
762#else
763 ++lnum;
764#endif
765 wp->w_cline_row += wp->w_lines[i].wl_size;
766 }
767 else
768 {
769#ifdef FEAT_FOLDING
770 fold_count = foldedCount(wp, lnum, NULL);
771 if (fold_count)
772 {
773 lnum += fold_count;
774 if (lnum > wp->w_cursor.lnum)
775 break;
776 ++wp->w_cline_row;
777 }
778 else
779#endif
780#ifdef FEAT_DIFF
781 if (lnum == wp->w_topline)
782 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
783 + wp->w_topfill;
784 else
785#endif
786 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
787 }
788 }
789
790 check_cursor_moved(wp);
791 if (!(wp->w_valid & VALID_CHEIGHT))
792 {
793 if (all_invalid
794 || i == wp->w_lines_valid
795 || (i < wp->w_lines_valid
796 && (!wp->w_lines[i].wl_valid
797 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
798 {
799#ifdef FEAT_DIFF
800 if (wp->w_cursor.lnum == wp->w_topline)
801 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
802 TRUE) + wp->w_topfill;
803 else
804#endif
805 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
806#ifdef FEAT_FOLDING
807 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
808 NULL, NULL, TRUE, NULL);
809#endif
810 }
811 else if (i > wp->w_lines_valid)
812 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100813 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 wp->w_cline_height = 0;
815#ifdef FEAT_FOLDING
816 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
817 NULL, NULL, TRUE, NULL);
818#endif
819 }
820 else
821 {
822 wp->w_cline_height = wp->w_lines[i].wl_size;
823#ifdef FEAT_FOLDING
824 wp->w_cline_folded = wp->w_lines[i].wl_folded;
825#endif
826 }
827 }
828
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100829 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Validate curwin->w_virtcol only.
836 */
837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 validate_virtcol_win(curwin);
841}
842
843/*
844 * Validate wp->w_virtcol only.
845 */
846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100847validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 check_cursor_moved(wp);
850 if (!(wp->w_valid & VALID_VIRTCOL))
851 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100852#ifdef FEAT_PROP_POPUP
853 wp->w_virtcol_first_char = 0;
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000856#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100857 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000858#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100859 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861}
862
863/*
864 * Validate curwin->w_cline_height only.
865 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
869 check_cursor_moved(curwin);
870 if (!(curwin->w_valid & VALID_CHEIGHT))
871 {
872#ifdef FEAT_DIFF
873 if (curwin->w_cursor.lnum == curwin->w_topline)
874 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
875 + curwin->w_topfill;
876 else
877#endif
878 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
879#ifdef FEAT_FOLDING
880 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
881#endif
882 curwin->w_valid |= VALID_CHEIGHT;
883 }
884}
885
886/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000887 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 */
889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100890validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 colnr_T off;
893 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100894 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896 validate_virtcol();
897 if (!(curwin->w_valid & VALID_WCOL))
898 {
899 col = curwin->w_virtcol;
900 off = curwin_col_off();
901 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200902 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaar85a20022019-12-21 18:25:54 +0100904 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000905 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200906 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100907 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200909 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000910 if (col > (int)curwin->w_leftcol)
911 col -= curwin->w_leftcol;
912 else
913 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100917#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100918 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921}
922
923/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200924 * Compute offset of a window, occupied by absolute or relative line number,
925 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 */
927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100928win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
Bram Moolenaar64486672010-05-16 15:46:46 +0200930 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_CMDWIN
932 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
933#endif
934#ifdef FEAT_FOLDING
935 + wp->w_p_fdc
936#endif
937#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200938 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940 );
941}
942
943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100944curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 return win_col_off(curwin);
947}
948
949/*
950 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200951 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
952 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 */
954 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100955win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
Bram Moolenaar64486672010-05-16 15:46:46 +0200957 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000958 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 return 0;
960}
961
962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 return win_col_off2(curwin);
966}
967
968/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100969 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * Also updates curwin->w_wrow and curwin->w_cline_row.
971 * Also updates curwin->w_leftcol.
972 */
973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100974curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100975 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100978 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 int off_left, off_right;
980 int n;
981 int p_lines;
982 int width = 0;
983 int textwidth;
984 int new_leftcol;
985 colnr_T startcol;
986 colnr_T endcol;
987 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100988 long so = get_scrolloff_value();
989 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991 /*
992 * First make sure that w_topline is valid (after moving the cursor).
993 */
Luuk van Baalfaf1d412022-09-19 16:45:29 +0100994 if (!skip_update_topline)
Luuk van Baal29ab5242022-09-11 16:59:53 +0100995 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 /*
998 * Next make sure that w_cline_row is valid.
999 */
1000 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001001 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001003#ifdef FEAT_PROP_POPUP
1004 // will be set by getvvcol() but not reset
1005 curwin->w_virtcol_first_char = 0;
1006#endif
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /*
1009 * Compute the number of virtual columns.
1010 */
1011#ifdef FEAT_FOLDING
1012 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1015 else
1016#endif
1017 getvvcol(curwin, &curwin->w_cursor,
1018 &startcol, &(curwin->w_virtcol), &endcol);
1019
Bram Moolenaar85a20022019-12-21 18:25:54 +01001020 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001022 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 extra = curwin_col_off();
1025 curwin->w_wcol = curwin->w_virtcol + extra;
1026 endcol += extra;
1027
1028 /*
1029 * Now compute w_wrow, counting screen lines from w_cline_row.
1030 */
1031 curwin->w_wrow = curwin->w_cline_row;
1032
Bram Moolenaar02631462017-09-22 15:20:32 +02001033 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (textwidth <= 0)
1035 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001036 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001037 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001038 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001039 if (curwin->w_p_wrap)
1040 curwin->w_wrow = curwin->w_height - 1;
1041 else
1042 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001044 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 width = textwidth + curwin_col_off2();
1047
Bram Moolenaar85a20022019-12-21 18:25:54 +01001048 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001049 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001051#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001052 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001053#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001054
Bram Moolenaar85a20022019-12-21 18:25:54 +01001055 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001056 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 curwin->w_wcol -= n * width;
1058 curwin->w_wrow += n;
1059
1060#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001061 // When cursor wraps to first char of next line in Insert
1062 // mode, the 'showbreak' string isn't shown, backup to first
1063 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001064 sbr = get_showbreak_value(curwin);
1065 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001066 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 curwin->w_wcol = 0;
1068#endif
1069 }
1070 }
1071
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1073 // is not folded.
1074 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001075 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#ifdef FEAT_FOLDING
1077 && !curwin->w_cline_folded
1078#endif
1079 )
1080 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001081#ifdef FEAT_PROP_POPUP
1082 if (curwin->w_virtcol_first_char > 0)
1083 {
1084 int cols = (curwin->w_width - extra);
1085 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1086
1087 // each "above" text prop shifts the text one row down
1088 curwin->w_wrow += rows;
1089 curwin->w_wcol -= rows * cols;
1090 endcol -= rows * cols;
1091 curwin->w_cline_height = rows + 1;
1092 }
1093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
1095 * If Cursor is left of the screen, scroll rightwards.
1096 * If Cursor is right of the screen, scroll leftwards
1097 * If we get closer to the edge than 'sidescrolloff', scroll a little
1098 * extra
1099 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001100 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001101 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001102 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (off_left < 0 || off_right > 0)
1104 {
1105 if (off_left < 0)
1106 diff = -off_left;
1107 else
1108 diff = off_right;
1109
Bram Moolenaar85a20022019-12-21 18:25:54 +01001110 // When far off or not enough room on either side, put cursor in
1111 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1113 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1114 else
1115 {
1116 if (diff < p_ss)
1117 diff = p_ss;
1118 if (off_left < 0)
1119 new_leftcol = curwin->w_leftcol - diff;
1120 else
1121 new_leftcol = curwin->w_leftcol + diff;
1122 }
1123 if (new_leftcol < 0)
1124 new_leftcol = 0;
1125 if (new_leftcol != (int)curwin->w_leftcol)
1126 {
1127 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001129 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132 curwin->w_wcol -= curwin->w_leftcol;
1133 }
1134 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1135 curwin->w_wcol -= curwin->w_leftcol;
1136 else
1137 curwin->w_wcol = 0;
1138
1139#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // Skip over filler lines. At the top use w_topfill, there
1141 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (curwin->w_cursor.lnum == curwin->w_topline)
1143 curwin->w_wrow += curwin->w_topfill;
1144 else
1145 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1146#endif
1147
1148 prev_skipcol = curwin->w_skipcol;
1149
1150 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if ((curwin->w_wrow >= curwin->w_height
1153 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001154 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && (p_lines =
1156#ifdef FEAT_DIFF
1157 plines_win_nofill
1158#else
1159 plines_win
1160#endif
1161 (curwin, curwin->w_cursor.lnum, FALSE))
1162 - 1 >= curwin->w_height))
1163 && curwin->w_height != 0
1164 && curwin->w_cursor.lnum == curwin->w_topline
1165 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001166 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // Cursor past end of screen. Happens with a single line that does
1169 // not fit on screen. Find a skipcol to show the text around the
1170 // cursor. Avoid scrolling all the time. compute value of "extra":
1171 // 1: Less than 'scrolloff' lines above
1172 // 2: Less than 'scrolloff' lines below
1173 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001175 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001177 // Compute last display line of the buffer line that we want at the
1178 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (p_lines == 0)
1180 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1181 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001182 if (p_lines > curwin->w_wrow + so)
1183 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 else
1185 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001186 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 extra += 2;
1188
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001189 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 n = curwin->w_virtcol / width;
1193 if (n > curwin->w_height / 2)
1194 n -= curwin->w_height / 2;
1195 else
1196 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (n > p_lines - curwin->w_height + 1)
1199 n = p_lines - curwin->w_height + 1;
1200 curwin->w_skipcol = n * width;
1201 }
1202 else if (extra == 1)
1203 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001204 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001205 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 + width - 1) / width;
1207 if (extra > 0)
1208 {
1209 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1210 extra = curwin->w_skipcol / width;
1211 curwin->w_skipcol -= extra * width;
1212 }
1213 }
1214 else if (extra == 2)
1215 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001216 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 endcol = (n - curwin->w_height + 1) * width;
1218 while (endcol > curwin->w_virtcol)
1219 endcol -= width;
1220 if (endcol > curwin->w_skipcol)
1221 curwin->w_skipcol = endcol;
1222 }
1223
1224 curwin->w_wrow -= curwin->w_skipcol / width;
1225 if (curwin->w_wrow >= curwin->w_height)
1226 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001227 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 extra = curwin->w_wrow - curwin->w_height + 1;
1229 curwin->w_skipcol += extra * width;
1230 curwin->w_wrow -= extra;
1231 }
1232
1233 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1234 if (extra > 0)
1235 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1236 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001237 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 else
1240 curwin->w_skipcol = 0;
1241 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001242 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001244#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001245 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001246#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001247#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1248 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1249 {
1250 curwin->w_wrow += popup_top_extra(curwin);
1251 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001252 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001253 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001254 else
1255 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001256#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001257
Bram Moolenaar08f23632019-11-16 14:19:33 +01001258 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1259 curwin->w_valid_leftcol = curwin->w_leftcol;
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1262}
1263
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001264#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001265/*
1266 * Compute the screen position of text character at "pos" in window "wp"
1267 * The resulting values are one-based, zero when character is not visible.
1268 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001269 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001270textpos2screenpos(
1271 win_T *wp,
1272 pos_T *pos,
1273 int *rowp, // screen row
1274 int *scolp, // start screen column
1275 int *ccolp, // cursor screen column
1276 int *ecolp) // end screen column
1277{
1278 colnr_T scol = 0, ccol = 0, ecol = 0;
1279 int row = 0;
1280 int rowoff = 0;
1281 colnr_T coloff = 0;
1282
Bram Moolenaar189663b2021-07-21 18:04:56 +02001283 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001284 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001285 colnr_T off;
1286 colnr_T col;
1287 int width;
1288 linenr_T lnum = pos->lnum;
1289#ifdef FEAT_FOLDING
1290 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001291
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001292 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1293#endif
1294 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1295#ifdef FEAT_FOLDING
1296 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001297 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001298 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001299 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001300 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001301 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001302#endif
1303 {
1304 getvcol(wp, pos, &scol, &ccol, &ecol);
1305
1306 // similar to what is done in validate_cursor_col()
1307 col = scol;
1308 off = win_col_off(wp);
1309 col += off;
1310 width = wp->w_width - off + win_col_off2(wp);
1311
1312 // long line wrapping, adjust row
1313 if (wp->w_p_wrap
1314 && col >= (colnr_T)wp->w_width
1315 && width > 0)
1316 {
1317 // use same formula as what is used in curs_columns()
1318 rowoff = ((col - wp->w_width) / width + 1);
1319 col -= rowoff * width;
1320 }
1321 col -= wp->w_leftcol;
1322 if (col >= wp->w_width)
1323 col = -1;
1324 if (col >= 0 && row + rowoff <= wp->w_height)
1325 {
1326 coloff = col - scol + wp->w_wincol + 1;
1327 row += W_WINROW(wp);
1328 }
1329 else
1330 // character is left, right or below of the window
1331 row = rowoff = scol = ccol = ecol = 0;
1332 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001333 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001334 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001335 *scolp = scol + coloff;
1336 *ccolp = ccol + coloff;
1337 *ecolp = ecol + coloff;
1338}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001339#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001340
Bram Moolenaar12034e22019-08-25 22:25:02 +02001341#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001342/*
1343 * "screenpos({winid}, {lnum}, {col})" function
1344 */
1345 void
1346f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1347{
1348 dict_T *dict;
1349 win_T *wp;
1350 pos_T pos;
1351 int row = 0;
1352 int scol = 0, ccol = 0, ecol = 0;
1353
Bram Moolenaar93a10962022-06-16 11:42:09 +01001354 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001355 return;
1356 dict = rettv->vval.v_dict;
1357
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001358 if (in_vim9script()
1359 && (check_for_number_arg(argvars, 0) == FAIL
1360 || check_for_number_arg(argvars, 1) == FAIL
1361 || check_for_number_arg(argvars, 2) == FAIL))
1362 return;
1363
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001364 wp = find_win_by_nr_or_id(&argvars[0]);
1365 if (wp == NULL)
1366 return;
1367
1368 pos.lnum = tv_get_number(&argvars[1]);
1369 pos.col = tv_get_number(&argvars[2]) - 1;
1370 pos.coladd = 0;
1371 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1372
1373 dict_add_number(dict, "row", row);
1374 dict_add_number(dict, "col", scol);
1375 dict_add_number(dict, "curscol", ccol);
1376 dict_add_number(dict, "endcol", ecol);
1377}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001378
1379/*
1380 * "virtcol2col({winid}, {lnum}, {col})" function
1381 */
1382 void
1383f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1384{
1385 win_T *wp;
1386 linenr_T lnum;
1387 int screencol;
1388 int error = FALSE;
1389
1390 rettv->vval.v_number = -1;
1391
1392 if (check_for_number_arg(argvars, 0) == FAIL
1393 || check_for_number_arg(argvars, 1) == FAIL
1394 || check_for_number_arg(argvars, 2) == FAIL)
1395 return;
1396
1397 wp = find_win_by_nr_or_id(&argvars[0]);
1398 if (wp == NULL)
1399 return;
1400
1401 lnum = tv_get_number_chk(&argvars[1], &error);
1402 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1403 return;
1404
1405 screencol = tv_get_number_chk(&argvars[2], &error);
1406 if (error || screencol < 0)
1407 return;
1408
1409 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1410}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413/*
1414 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417scrolldown(
1418 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001419 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001421 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 int wrow;
1423 int moved = FALSE;
1424
1425#ifdef FEAT_FOLDING
1426 linenr_T first;
1427
Bram Moolenaar85a20022019-12-21 18:25:54 +01001428 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1430#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 while (line_count-- > 0)
1433 {
1434#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001435 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1436 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 {
1438 ++curwin->w_topfill;
1439 ++done;
1440 }
1441 else
1442#endif
1443 {
1444 if (curwin->w_topline == 1)
1445 break;
1446 --curwin->w_topline;
1447#ifdef FEAT_DIFF
1448 curwin->w_topfill = 0;
1449#endif
1450#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001451 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (hasFolding(curwin->w_topline, &first, NULL))
1453 {
1454 ++done;
1455 if (!byfold)
1456 line_count -= curwin->w_topline - first - 1;
1457 curwin->w_botline -= curwin->w_topline - first;
1458 curwin->w_topline = first;
1459 }
1460 else
1461#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001462 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001464 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 invalidate_botline();
1466 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001467 curwin->w_wrow += done; // keep w_wrow updated
1468 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470#ifdef FEAT_DIFF
1471 if (curwin->w_cursor.lnum == curwin->w_topline)
1472 curwin->w_cline_row = 0;
1473 check_topfill(curwin, TRUE);
1474#endif
1475
1476 /*
1477 * Compute the row number of the last row of the cursor line
1478 * and move the cursor onto the displayed part of the window.
1479 */
1480 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001481 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
1483 validate_virtcol();
1484 validate_cheight();
1485 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001486 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1489 {
1490#ifdef FEAT_FOLDING
1491 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1492 {
1493 --wrow;
1494 if (first == 1)
1495 curwin->w_cursor.lnum = 1;
1496 else
1497 curwin->w_cursor.lnum = first - 1;
1498 }
1499 else
1500#endif
1501 wrow -= plines(curwin->w_cursor.lnum--);
1502 curwin->w_valid &=
1503 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1504 moved = TRUE;
1505 }
1506 if (moved)
1507 {
1508#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001509 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 foldAdjustCursor();
1511#endif
1512 coladvance(curwin->w_curswant);
1513 }
1514}
1515
1516/*
1517 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001520scrollup(
1521 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001522 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1525 linenr_T lnum;
1526
1527 if (
1528# ifdef FEAT_FOLDING
1529 (byfold && hasAnyFolding(curwin))
1530# ifdef FEAT_DIFF
1531 ||
1532# endif
1533# endif
1534# ifdef FEAT_DIFF
1535 curwin->w_p_diff
1536# endif
1537 )
1538 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001539 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 lnum = curwin->w_topline;
1541 while (line_count--)
1542 {
1543# ifdef FEAT_DIFF
1544 if (curwin->w_topfill > 0)
1545 --curwin->w_topfill;
1546 else
1547# endif
1548 {
1549# ifdef FEAT_FOLDING
1550 if (byfold)
1551 (void)hasFolding(lnum, NULL, &lnum);
1552# endif
1553 if (lnum >= curbuf->b_ml.ml_line_count)
1554 break;
1555 ++lnum;
1556# ifdef FEAT_DIFF
1557 curwin->w_topfill = diff_check_fill(curwin, lnum);
1558# endif
1559 }
1560 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001561 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 curwin->w_botline += lnum - curwin->w_topline;
1563 curwin->w_topline = lnum;
1564 }
1565 else
1566#endif
1567 {
1568 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571
1572 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1573 curwin->w_topline = curbuf->b_ml.ml_line_count;
1574 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1575 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1576
1577#ifdef FEAT_DIFF
1578 check_topfill(curwin, FALSE);
1579#endif
1580
1581#ifdef FEAT_FOLDING
1582 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1585#endif
1586
1587 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1588 if (curwin->w_cursor.lnum < curwin->w_topline)
1589 {
1590 curwin->w_cursor.lnum = curwin->w_topline;
1591 curwin->w_valid &=
1592 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1593 coladvance(curwin->w_curswant);
1594 }
1595}
1596
1597#ifdef FEAT_DIFF
1598/*
1599 * Don't end up with too many filler lines in the window.
1600 */
1601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602check_topfill(
1603 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 int n;
1607
1608 if (wp->w_topfill > 0)
1609 {
1610 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1611 if (wp->w_topfill + n > wp->w_height)
1612 {
1613 if (down && wp->w_topline > 1)
1614 {
1615 --wp->w_topline;
1616 wp->w_topfill = 0;
1617 }
1618 else
1619 {
1620 wp->w_topfill = wp->w_height - n;
1621 if (wp->w_topfill < 0)
1622 wp->w_topfill = 0;
1623 }
1624 }
1625 }
1626}
1627
1628/*
1629 * Use as many filler lines as possible for w_topline. Make sure w_topline
1630 * is still visible.
1631 */
1632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001633max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 int n;
1636
1637 n = plines_nofill(curwin->w_topline);
1638 if (n >= curwin->w_height)
1639 curwin->w_topfill = 0;
1640 else
1641 {
1642 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1643 if (curwin->w_topfill + n > curwin->w_height)
1644 curwin->w_topfill = curwin->w_height - n;
1645 }
1646}
1647#endif
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649/*
1650 * Scroll the screen one line down, but don't do it if it would move the
1651 * cursor off the screen.
1652 */
1653 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001654scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 int end_row;
1657#ifdef FEAT_DIFF
1658 int can_fill = (curwin->w_topfill
1659 < diff_check_fill(curwin, curwin->w_topline));
1660#endif
1661
1662 if (curwin->w_topline <= 1
1663#ifdef FEAT_DIFF
1664 && !can_fill
1665#endif
1666 )
1667 return;
1668
Bram Moolenaar85a20022019-12-21 18:25:54 +01001669 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 /*
1672 * Compute the row number of the last row of the cursor line
1673 * and make sure it doesn't go off the screen. Make sure the cursor
1674 * doesn't go past 'scrolloff' lines from the screen end.
1675 */
1676 end_row = curwin->w_wrow;
1677#ifdef FEAT_DIFF
1678 if (can_fill)
1679 ++end_row;
1680 else
1681 end_row += plines_nofill(curwin->w_topline - 1);
1682#else
1683 end_row += plines(curwin->w_topline - 1);
1684#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001685 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 validate_cheight();
1688 validate_virtcol();
1689 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001690 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001692 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694#ifdef FEAT_DIFF
1695 if (can_fill)
1696 {
1697 ++curwin->w_topfill;
1698 check_topfill(curwin, TRUE);
1699 }
1700 else
1701 {
1702 --curwin->w_topline;
1703 curwin->w_topfill = 0;
1704 }
1705#else
1706 --curwin->w_topline;
1707#endif
1708#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001709 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001711 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1713 }
1714}
1715
1716/*
1717 * Scroll the screen one line up, but don't do it if it would move the cursor
1718 * off the screen.
1719 */
1720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001721scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 int start_row;
1724
1725 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1726#ifdef FEAT_DIFF
1727 && curwin->w_topfill == 0
1728#endif
1729 )
1730 return;
1731
Bram Moolenaar85a20022019-12-21 18:25:54 +01001732 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734 /*
1735 * Compute the row number of the first row of the cursor line
1736 * and make sure it doesn't go off the screen. Make sure the cursor
1737 * doesn't go before 'scrolloff' lines from the screen start.
1738 */
1739#ifdef FEAT_DIFF
1740 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1741 - curwin->w_topfill;
1742#else
1743 start_row = curwin->w_wrow - plines(curwin->w_topline);
1744#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001745 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001748 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001750 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752#ifdef FEAT_DIFF
1753 if (curwin->w_topfill > 0)
1754 --curwin->w_topfill;
1755 else
1756#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001757 {
1758#ifdef FEAT_FOLDING
1759 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001762 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1765 }
1766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768/*
1769 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1770 * a (wrapped) text line. Uses and sets "lp->fill".
1771 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001772 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 */
1774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777#ifdef FEAT_DIFF
1778 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1779 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001780 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ++lp->fill;
1782 lp->height = 1;
1783 }
1784 else
1785#endif
1786 {
1787 --lp->lnum;
1788#ifdef FEAT_DIFF
1789 lp->fill = 0;
1790#endif
1791 if (lp->lnum < 1)
1792 lp->height = MAXCOL;
1793 else
1794#ifdef FEAT_FOLDING
1795 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001796 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 lp->height = 1;
1798 else
1799#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001800 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802}
1803
1804/*
1805 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1806 * a (wrapped) text line. Uses and sets "lp->fill".
1807 * Returns the height of the added line in "lp->height".
1808 * Lines below the last one are incredibly high.
1809 */
1810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813#ifdef FEAT_DIFF
1814 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1815 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001816 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 ++lp->fill;
1818 lp->height = 1;
1819 }
1820 else
1821#endif
1822 {
1823 ++lp->lnum;
1824#ifdef FEAT_DIFF
1825 lp->fill = 0;
1826#endif
1827 if (lp->lnum > curbuf->b_ml.ml_line_count)
1828 lp->height = MAXCOL;
1829 else
1830#ifdef FEAT_FOLDING
1831 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001832 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 lp->height = 1;
1834 else
1835#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001836 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838}
1839
1840#ifdef FEAT_DIFF
1841/*
1842 * Switch from including filler lines below lp->lnum to including filler
1843 * lines above loff.lnum + 1. This keeps pointing to the same line.
1844 * When there are no filler lines nothing changes.
1845 */
1846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001847botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 if (lp->fill > 0)
1850 {
1851 ++lp->lnum;
1852 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1853 }
1854}
1855
1856/*
1857 * Switch from including filler lines above lp->lnum to including filler
1858 * lines below loff.lnum - 1. This keeps pointing to the same line.
1859 * When there are no filler lines nothing changes.
1860 */
1861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001862topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 if (lp->fill > 0)
1865 {
1866 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1867 --lp->lnum;
1868 }
1869}
1870#endif
1871
1872/*
1873 * Recompute topline to put the cursor at the top of the window.
1874 * Scroll at least "min_scroll" lines.
1875 * If "always" is TRUE, always set topline (for "zt").
1876 */
1877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001878scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
1880 int scrolled = 0;
1881 int extra = 0;
1882 int used;
1883 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001884 linenr_T top; // just above displayed lines
1885 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 linenr_T old_topline = curwin->w_topline;
1887#ifdef FEAT_DIFF
1888 linenr_T old_topfill = curwin->w_topfill;
1889#endif
1890 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001891 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (mouse_dragging > 0)
1894 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 /*
1897 * Decrease topline until:
1898 * - it has become 1
1899 * - (part of) the cursor line is moved off the screen or
1900 * - moved at least 'scrolljump' lines and
1901 * - at least 'scrolloff' lines above and below the cursor
1902 */
1903 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001904 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (curwin->w_cursor.lnum < curwin->w_topline)
1906 scrolled = used;
1907
1908#ifdef FEAT_FOLDING
1909 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1910 {
1911 --top;
1912 ++bot;
1913 }
1914 else
1915#endif
1916 {
1917 top = curwin->w_cursor.lnum - 1;
1918 bot = curwin->w_cursor.lnum + 1;
1919 }
1920 new_topline = top + 1;
1921
1922#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001923 // "used" already contains the number of filler lines above, don't add it
1924 // again.
1925 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001926 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#endif
1928
1929 /*
1930 * Check if the lines from "top" to "bot" fit in the window. If they do,
1931 * set new_topline and advance "top" and "bot" to include more lines.
1932 */
1933 while (top > 0)
1934 {
1935#ifdef FEAT_FOLDING
1936 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001937 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 i = 1;
1939 else
1940#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001941 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 used += i;
1943 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1944 {
1945#ifdef FEAT_FOLDING
1946 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001947 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 ++used;
1949 else
1950#endif
1951 used += plines(bot);
1952 }
1953 if (used > curwin->w_height)
1954 break;
1955 if (top < curwin->w_topline)
1956 scrolled += i;
1957
1958 /*
1959 * If scrolling is needed, scroll at least 'sj' lines.
1960 */
1961 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1962 && extra >= off)
1963 break;
1964
1965 extra += i;
1966 new_topline = top;
1967 --top;
1968 ++bot;
1969 }
1970
1971 /*
1972 * If we don't have enough space, put cursor in the middle.
1973 * This makes sure we get the same position when using "k" and "j"
1974 * in a small window.
1975 */
1976 if (used > curwin->w_height)
1977 scroll_cursor_halfway(FALSE);
1978 else
1979 {
1980 /*
1981 * If "always" is FALSE, only adjust topline to a lower value, higher
1982 * value may happen with wrapping lines
1983 */
1984 if (new_topline < curwin->w_topline || always)
1985 curwin->w_topline = new_topline;
1986 if (curwin->w_topline > curwin->w_cursor.lnum)
1987 curwin->w_topline = curwin->w_cursor.lnum;
1988#ifdef FEAT_DIFF
1989 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1990 if (curwin->w_topfill > 0 && extra > off)
1991 {
1992 curwin->w_topfill -= extra - off;
1993 if (curwin->w_topfill < 0)
1994 curwin->w_topfill = 0;
1995 }
1996 check_topfill(curwin, FALSE);
1997#endif
1998 if (curwin->w_topline != old_topline
1999#ifdef FEAT_DIFF
2000 || curwin->w_topfill != old_topfill
2001#endif
2002 )
2003 curwin->w_valid &=
2004 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2005 curwin->w_valid |= VALID_TOPLINE;
2006 }
2007}
2008
2009/*
2010 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2011 * screen lines for text lines.
2012 */
2013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002014set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016#ifdef FEAT_DIFF
2017 wp->w_filler_rows = 0;
2018#endif
2019 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002020 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 else
2022 {
2023 wp->w_empty_rows = wp->w_height - used;
2024#ifdef FEAT_DIFF
2025 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2026 {
2027 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2028 if (wp->w_empty_rows > wp->w_filler_rows)
2029 wp->w_empty_rows -= wp->w_filler_rows;
2030 else
2031 {
2032 wp->w_filler_rows = wp->w_empty_rows;
2033 wp->w_empty_rows = 0;
2034 }
2035 }
2036#endif
2037 }
2038}
2039
2040/*
2041 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002042 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2044 * This is messy stuff!!!
2045 */
2046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002047scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 int used;
2050 int scrolled = 0;
2051 int extra = 0;
2052 int i;
2053 linenr_T line_count;
2054 linenr_T old_topline = curwin->w_topline;
2055 lineoff_T loff;
2056 lineoff_T boff;
2057#ifdef FEAT_DIFF
2058 int old_topfill = curwin->w_topfill;
2059 int fill_below_window;
2060#endif
2061 linenr_T old_botline = curwin->w_botline;
2062 linenr_T old_valid = curwin->w_valid;
2063 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002064 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002065 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 cln = curwin->w_cursor.lnum;
2068 if (set_topbot)
2069 {
2070 used = 0;
2071 curwin->w_botline = cln + 1;
2072#ifdef FEAT_DIFF
2073 loff.fill = 0;
2074#endif
2075 for (curwin->w_topline = curwin->w_botline;
2076 curwin->w_topline > 1;
2077 curwin->w_topline = loff.lnum)
2078 {
2079 loff.lnum = curwin->w_topline;
2080 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002081 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 break;
2083 used += loff.height;
2084#ifdef FEAT_DIFF
2085 curwin->w_topfill = loff.fill;
2086#endif
2087 }
2088 set_empty_rows(curwin, used);
2089 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2090 if (curwin->w_topline != old_topline
2091#ifdef FEAT_DIFF
2092 || curwin->w_topfill != old_topfill
2093#endif
2094 )
2095 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2096 }
2097 else
2098 validate_botline();
2099
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef FEAT_DIFF
2102 used = plines_nofill(cln);
2103#else
2104 validate_cheight();
2105 used = curwin->w_cline_height;
2106#endif
2107
Bram Moolenaar85a20022019-12-21 18:25:54 +01002108 // If the cursor is below botline, we will at least scroll by the height
2109 // of the cursor line. Correct for empty lines, which are really part of
2110 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (cln >= curwin->w_botline)
2112 {
2113 scrolled = used;
2114 if (cln == curwin->w_botline)
2115 scrolled -= curwin->w_empty_rows;
2116 }
2117
2118 /*
2119 * Stop counting lines to scroll when
2120 * - hitting start of the file
2121 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002122 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 * - lines between botline and cursor have been counted
2124 */
2125#ifdef FEAT_FOLDING
2126 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2127#endif
2128 {
2129 loff.lnum = cln;
2130 boff.lnum = cln;
2131 }
2132#ifdef FEAT_DIFF
2133 loff.fill = 0;
2134 boff.fill = 0;
2135 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2136 - curwin->w_filler_rows;
2137#endif
2138
2139 while (loff.lnum > 1)
2140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002141 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2142 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002144 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2146 && loff.lnum <= curwin->w_botline
2147#ifdef FEAT_DIFF
2148 && (loff.lnum < curwin->w_botline
2149 || loff.fill >= fill_below_window)
2150#endif
2151 )
2152 break;
2153
Bram Moolenaar85a20022019-12-21 18:25:54 +01002154 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002156 if (loff.height == MAXCOL)
2157 used = MAXCOL;
2158 else
2159 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (used > curwin->w_height)
2161 break;
2162 if (loff.lnum >= curwin->w_botline
2163#ifdef FEAT_DIFF
2164 && (loff.lnum > curwin->w_botline
2165 || loff.fill <= fill_below_window)
2166#endif
2167 )
2168 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002169 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 scrolled += loff.height;
2171 if (loff.lnum == curwin->w_botline
2172#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002173 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174#endif
2175 )
2176 scrolled -= curwin->w_empty_rows;
2177 }
2178
2179 if (boff.lnum < curbuf->b_ml.ml_line_count)
2180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002181 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 botline_forw(&boff);
2183 used += boff.height;
2184 if (used > curwin->w_height)
2185 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002186 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2187 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 extra += boff.height;
2190 if (boff.lnum >= curwin->w_botline
2191#ifdef FEAT_DIFF
2192 || (boff.lnum + 1 == curwin->w_botline
2193 && boff.fill > curwin->w_filler_rows)
2194#endif
2195 )
2196 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 scrolled += boff.height;
2199 if (boff.lnum == curwin->w_botline
2200#ifdef FEAT_DIFF
2201 && boff.fill == 0
2202#endif
2203 )
2204 scrolled -= curwin->w_empty_rows;
2205 }
2206 }
2207 }
2208 }
2209
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (scrolled <= 0)
2212 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002213 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else if (used > curwin->w_height)
2215 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002216 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 else
2218 {
2219 line_count = 0;
2220#ifdef FEAT_DIFF
2221 boff.fill = curwin->w_topfill;
2222#endif
2223 boff.lnum = curwin->w_topline - 1;
2224 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2225 {
2226 botline_forw(&boff);
2227 i += boff.height;
2228 ++line_count;
2229 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 line_count = 9999;
2232 }
2233
2234 /*
2235 * Scroll up if the cursor is off the bottom of the screen a bit.
2236 * Otherwise put it at 1/2 of the screen.
2237 */
2238 if (line_count >= curwin->w_height && line_count > min_scroll)
2239 scroll_cursor_halfway(FALSE);
2240 else
2241 scrollup(line_count, TRUE);
2242
2243 /*
2244 * If topline didn't change we need to restore w_botline and w_empty_rows
2245 * (we changed them).
2246 * If topline did change, update_screen() will set botline.
2247 */
2248 if (curwin->w_topline == old_topline && set_topbot)
2249 {
2250 curwin->w_botline = old_botline;
2251 curwin->w_empty_rows = old_empty_rows;
2252 curwin->w_valid = old_valid;
2253 }
2254 curwin->w_valid |= VALID_TOPLINE;
2255}
2256
2257/*
2258 * Recompute topline to put the cursor halfway the window
2259 * If "atend" is TRUE, also put it halfway at the end of the file.
2260 */
2261 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002262scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 int above = 0;
2265 linenr_T topline;
2266#ifdef FEAT_DIFF
2267 int topfill = 0;
2268#endif
2269 int below = 0;
2270 int used;
2271 lineoff_T loff;
2272 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002273#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002274 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002277#ifdef FEAT_PROP_POPUP
2278 // if the width changed this needs to be updated first
2279 may_update_popup_position();
2280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2282#ifdef FEAT_FOLDING
2283 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2284#endif
2285#ifdef FEAT_DIFF
2286 used = plines_nofill(loff.lnum);
2287 loff.fill = 0;
2288 boff.fill = 0;
2289#else
2290 used = plines(loff.lnum);
2291#endif
2292 topline = loff.lnum;
2293 while (topline > 1)
2294 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 if (boff.lnum < curbuf->b_ml.ml_line_count)
2298 {
2299 botline_forw(&boff);
2300 used += boff.height;
2301 if (used > curwin->w_height)
2302 break;
2303 below += boff.height;
2304 }
2305 else
2306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (atend)
2309 ++used;
2310 }
2311 }
2312
Bram Moolenaar85a20022019-12-21 18:25:54 +01002313 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002316 if (loff.height == MAXCOL)
2317 used = MAXCOL;
2318 else
2319 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (used > curwin->w_height)
2321 break;
2322 above += loff.height;
2323 topline = loff.lnum;
2324#ifdef FEAT_DIFF
2325 topfill = loff.fill;
2326#endif
2327 }
2328 }
2329#ifdef FEAT_FOLDING
2330 if (!hasFolding(topline, &curwin->w_topline, NULL))
2331#endif
2332 curwin->w_topline = topline;
2333#ifdef FEAT_DIFF
2334 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002335 if (old_topline > curwin->w_topline + curwin->w_height)
2336 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 check_topfill(curwin, FALSE);
2338#endif
2339 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2340 curwin->w_valid |= VALID_TOPLINE;
2341}
2342
2343/*
2344 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002345 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * If not possible, put it at the same position as scroll_cursor_halfway().
2347 * When called topline must be valid!
2348 */
2349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002350cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002354 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 linenr_T botline;
2356 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002357 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002359 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 /*
2362 * How many lines we would like to have above/below the cursor depends on
2363 * whether the first/last line of the file is on screen.
2364 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002365 above_wanted = so;
2366 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002367 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 above_wanted = mouse_dragging - 1;
2370 below_wanted = mouse_dragging - 1;
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (curwin->w_topline == 1)
2373 {
2374 above_wanted = 0;
2375 max_off = curwin->w_height / 2;
2376 if (below_wanted > max_off)
2377 below_wanted = max_off;
2378 }
2379 validate_botline();
2380 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002381 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 below_wanted = 0;
2384 max_off = (curwin->w_height - 1) / 2;
2385 if (above_wanted > max_off)
2386 above_wanted = max_off;
2387 }
2388
2389 /*
2390 * If there are sufficient file-lines above and below the cursor, we can
2391 * return now.
2392 */
2393 cln = curwin->w_cursor.lnum;
2394 if (cln >= curwin->w_topline + above_wanted
2395 && cln < curwin->w_botline - below_wanted
2396#ifdef FEAT_FOLDING
2397 && !hasAnyFolding(curwin)
2398#endif
2399 )
2400 return;
2401
2402 /*
2403 * Narrow down the area where the cursor can be put by taking lines from
2404 * the top and the bottom until:
2405 * - the desired context lines are found
2406 * - the lines from the top is past the lines from the bottom
2407 */
2408 topline = curwin->w_topline;
2409 botline = curwin->w_botline - 1;
2410#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002411 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 above = curwin->w_topfill;
2413 below = curwin->w_filler_rows;
2414#endif
2415 while ((above < above_wanted || below < below_wanted) && topline < botline)
2416 {
2417 if (below < below_wanted && (below <= above || above >= above_wanted))
2418 {
2419#ifdef FEAT_FOLDING
2420 if (hasFolding(botline, &botline, NULL))
2421 ++below;
2422 else
2423#endif
2424 below += plines(botline);
2425 --botline;
2426 }
2427 if (above < above_wanted && (above < below || below >= below_wanted))
2428 {
2429#ifdef FEAT_FOLDING
2430 if (hasFolding(topline, NULL, &topline))
2431 ++above;
2432 else
2433#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002434 above += PLINES_NOFILL(topline);
2435#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (topline < botline)
2438 above += diff_check_fill(curwin, topline + 1);
2439#endif
2440 ++topline;
2441 }
2442 }
2443 if (topline == botline || botline == 0)
2444 curwin->w_cursor.lnum = topline;
2445 else if (topline > botline)
2446 curwin->w_cursor.lnum = botline;
2447 else
2448 {
2449 if (cln < topline && curwin->w_topline > 1)
2450 {
2451 curwin->w_cursor.lnum = topline;
2452 curwin->w_valid &=
2453 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2454 }
2455 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2456 {
2457 curwin->w_cursor.lnum = botline;
2458 curwin->w_valid &=
2459 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2460 }
2461 }
2462 curwin->w_valid |= VALID_TOPLINE;
2463}
2464
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002465static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467/*
2468 * move screen 'count' pages up or down and update screen
2469 *
2470 * return FAIL for failure, OK otherwise
2471 */
2472 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002473onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 long n;
2476 int retval = OK;
2477 lineoff_T loff;
2478 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002479 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
2483 beep_flush();
2484 return FAIL;
2485 }
2486
2487 for ( ; count > 0; --count)
2488 {
2489 validate_botline();
2490 /*
2491 * It's an error to move a page up when the first line is already on
2492 * the screen. It's an error to move a page down when the last line
2493 * is on the screen and the topline is 'scrolloff' lines from the
2494 * last line.
2495 */
2496 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002497 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2499 : (curwin->w_topline == 1
2500#ifdef FEAT_DIFF
2501 && curwin->w_topfill ==
2502 diff_check_fill(curwin, curwin->w_topline)
2503#endif
2504 ))
2505 {
2506 beep_flush();
2507 retval = FAIL;
2508 break;
2509 }
2510
2511#ifdef FEAT_DIFF
2512 loff.fill = 0;
2513#endif
2514 if (dir == FORWARD)
2515 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002516 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002518 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002519 if (p_window <= 2)
2520 ++curwin->w_topline;
2521 else
2522 curwin->w_topline += p_window - 2;
2523 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2524 curwin->w_topline = curbuf->b_ml.ml_line_count;
2525 curwin->w_cursor.lnum = curwin->w_topline;
2526 }
2527 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2528 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002529 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 curwin->w_topline = curbuf->b_ml.ml_line_count;
2531#ifdef FEAT_DIFF
2532 curwin->w_topfill = 0;
2533#endif
2534 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2535 }
2536 else
2537 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 // For the overlap, start with the line just below the window
2539 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 loff.lnum = curwin->w_botline;
2541#ifdef FEAT_DIFF
2542 loff.fill = diff_check_fill(curwin, loff.lnum)
2543 - curwin->w_filler_rows;
2544#endif
2545 get_scroll_overlap(&loff, -1);
2546 curwin->w_topline = loff.lnum;
2547#ifdef FEAT_DIFF
2548 curwin->w_topfill = loff.fill;
2549 check_topfill(curwin, FALSE);
2550#endif
2551 curwin->w_cursor.lnum = curwin->w_topline;
2552 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2553 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2554 }
2555 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558#ifdef FEAT_DIFF
2559 if (curwin->w_topline == 1)
2560 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002561 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 max_topfill();
2563 continue;
2564 }
2565#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002566 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002567 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002568 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002569 if (p_window <= 2)
2570 --curwin->w_topline;
2571 else
2572 curwin->w_topline -= p_window - 2;
2573 if (curwin->w_topline < 1)
2574 curwin->w_topline = 1;
2575 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2576 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2577 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2578 continue;
2579 }
2580
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // Find the line at the top of the window that is going to be the
2582 // line at the bottom of the window. Make sure this results in
2583 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 loff.lnum = curwin->w_topline - 1;
2585#ifdef FEAT_DIFF
2586 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2587 - curwin->w_topfill;
2588#endif
2589 get_scroll_overlap(&loff, 1);
2590
2591 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2592 {
2593 loff.lnum = curbuf->b_ml.ml_line_count;
2594#ifdef FEAT_DIFF
2595 loff.fill = 0;
2596 }
2597 else
2598 {
2599 botline_topline(&loff);
2600#endif
2601 }
2602 curwin->w_cursor.lnum = loff.lnum;
2603
Bram Moolenaar85a20022019-12-21 18:25:54 +01002604 // Find the line just above the new topline to get the right line
2605 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 n = 0;
2607 while (n <= curwin->w_height && loff.lnum >= 1)
2608 {
2609 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002610 if (loff.height == MAXCOL)
2611 n = MAXCOL;
2612 else
2613 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 curwin->w_topline = 1;
2618#ifdef FEAT_DIFF
2619 max_topfill();
2620#endif
2621 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2622 }
2623 else
2624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#ifdef FEAT_DIFF
2627 topline_botline(&loff);
2628#endif
2629 botline_forw(&loff);
2630 botline_forw(&loff);
2631#ifdef FEAT_DIFF
2632 botline_topline(&loff);
2633#endif
2634#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2637#endif
2638
Bram Moolenaar85a20022019-12-21 18:25:54 +01002639 // Always scroll at least one line. Avoid getting stuck on
2640 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (loff.lnum >= curwin->w_topline
2642#ifdef FEAT_DIFF
2643 && (loff.lnum > curwin->w_topline
2644 || loff.fill >= curwin->w_topfill)
2645#endif
2646 )
2647 {
2648#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 // First try using the maximum number of filler lines. If
2650 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 loff.fill = curwin->w_topfill;
2652 if (curwin->w_topfill < diff_check_fill(curwin,
2653 curwin->w_topline))
2654 max_topfill();
2655 if (curwin->w_topfill == loff.fill)
2656#endif
2657 {
2658 --curwin->w_topline;
2659#ifdef FEAT_DIFF
2660 curwin->w_topfill = 0;
2661#endif
2662 }
2663 comp_botline(curwin);
2664 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002665 curwin->w_valid &=
2666 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668 else
2669 {
2670 curwin->w_topline = loff.lnum;
2671#ifdef FEAT_DIFF
2672 curwin->w_topfill = loff.fill;
2673 check_topfill(curwin, FALSE);
2674#endif
2675 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2676 }
2677 }
2678 }
2679 }
2680#ifdef FEAT_FOLDING
2681 foldAdjustCursor();
2682#endif
2683 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002684 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002685 if (retval == OK)
2686 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2688
Bram Moolenaar907dad72018-07-10 15:07:15 +02002689 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002691 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2692 // But make sure we scroll at least one line (happens with mix of long
2693 // wrapping lines and non-wrapping line).
2694 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002696 scroll_cursor_top(1, FALSE);
2697 if (curwin->w_topline <= old_topline
2698 && old_topline < curbuf->b_ml.ml_line_count)
2699 {
2700 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002702 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2703#endif
2704 }
2705 }
2706#ifdef FEAT_FOLDING
2707 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002712 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return retval;
2714}
2715
2716/*
2717 * Decide how much overlap to use for page-up or page-down scrolling.
2718 * This is symmetric, so that doing both keeps the same lines displayed.
2719 * Three lines are examined:
2720 *
2721 * before CTRL-F after CTRL-F / before CTRL-B
2722 * etc. l1
2723 * l1 last but one line ------------
2724 * l2 last text line l2 top text line
2725 * ------------- l3 second text line
2726 * l3 etc.
2727 */
2728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 int h1, h2, h3, h4;
2732 int min_height = curwin->w_height - 2;
2733 lineoff_T loff0, loff1, loff2;
2734
2735#ifdef FEAT_DIFF
2736 if (lp->fill > 0)
2737 lp->height = 1;
2738 else
2739 lp->height = plines_nofill(lp->lnum);
2740#else
2741 lp->height = plines(lp->lnum);
2742#endif
2743 h1 = lp->height;
2744 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002745 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 loff0 = *lp;
2748 if (dir > 0)
2749 botline_forw(lp);
2750 else
2751 topline_back(lp);
2752 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002753 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002755 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return;
2757 }
2758
2759 loff1 = *lp;
2760 if (dir > 0)
2761 botline_forw(lp);
2762 else
2763 topline_back(lp);
2764 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002765 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002767 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 return;
2769 }
2770
2771 loff2 = *lp;
2772 if (dir > 0)
2773 botline_forw(lp);
2774 else
2775 topline_back(lp);
2776 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002777 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002778 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002780 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Scroll 'scroll' lines up or down.
2785 */
2786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002787halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 long scrolled = 0;
2790 int i;
2791 int n;
2792 int room;
2793
2794 if (Prenum)
2795 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2796 curwin->w_height : Prenum;
2797 n = (curwin->w_p_scr <= curwin->w_height) ?
2798 curwin->w_p_scr : curwin->w_height;
2799
Bram Moolenaard5d37532017-03-27 23:02:07 +02002800 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 validate_botline();
2802 room = curwin->w_empty_rows;
2803#ifdef FEAT_DIFF
2804 room += curwin->w_filler_rows;
2805#endif
2806 if (flag)
2807 {
2808 /*
2809 * scroll the text up
2810 */
2811 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2812 {
2813#ifdef FEAT_DIFF
2814 if (curwin->w_topfill > 0)
2815 {
2816 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002817 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 --curwin->w_topfill;
2819 }
2820 else
2821#endif
2822 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002823 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 n -= i;
2825 if (n < 0 && scrolled > 0)
2826 break;
2827#ifdef FEAT_FOLDING
2828 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2829#endif
2830 ++curwin->w_topline;
2831#ifdef FEAT_DIFF
2832 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2833#endif
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2836 {
2837 ++curwin->w_cursor.lnum;
2838 curwin->w_valid &=
2839 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2843 scrolled += i;
2844
2845 /*
2846 * Correct w_botline for changed w_topline.
2847 * Won't work when there are filler lines.
2848 */
2849#ifdef FEAT_DIFF
2850 if (curwin->w_p_diff)
2851 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2852 else
2853#endif
2854 {
2855 room += i;
2856 do
2857 {
2858 i = plines(curwin->w_botline);
2859 if (i > room)
2860 break;
2861#ifdef FEAT_FOLDING
2862 (void)hasFolding(curwin->w_botline, NULL,
2863 &curwin->w_botline);
2864#endif
2865 ++curwin->w_botline;
2866 room -= i;
2867 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2868 }
2869 }
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /*
2872 * When hit bottom of the file: move cursor down.
2873 */
2874 if (n > 0)
2875 {
2876# ifdef FEAT_FOLDING
2877 if (hasAnyFolding(curwin))
2878 {
2879 while (--n >= 0
2880 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2881 {
2882 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2883 &curwin->w_cursor.lnum);
2884 ++curwin->w_cursor.lnum;
2885 }
2886 }
2887 else
2888# endif
2889 curwin->w_cursor.lnum += n;
2890 check_cursor_lnum();
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 else
2894 {
2895 /*
2896 * scroll the text down
2897 */
2898 while (n > 0 && curwin->w_topline > 1)
2899 {
2900#ifdef FEAT_DIFF
2901 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2902 {
2903 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002904 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 ++curwin->w_topfill;
2906 }
2907 else
2908#endif
2909 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002910 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 n -= i;
2912 if (n < 0 && scrolled > 0)
2913 break;
2914 --curwin->w_topline;
2915#ifdef FEAT_FOLDING
2916 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2917#endif
2918#ifdef FEAT_DIFF
2919 curwin->w_topfill = 0;
2920#endif
2921 }
2922 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2923 VALID_BOTLINE|VALID_BOTLINE_AP);
2924 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (curwin->w_cursor.lnum > 1)
2926 {
2927 --curwin->w_cursor.lnum;
2928 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 /*
2933 * When hit top of the file: move cursor up.
2934 */
2935 if (n > 0)
2936 {
2937 if (curwin->w_cursor.lnum <= (linenr_T)n)
2938 curwin->w_cursor.lnum = 1;
2939 else
2940# ifdef FEAT_FOLDING
2941 if (hasAnyFolding(curwin))
2942 {
2943 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2944 {
2945 --curwin->w_cursor.lnum;
2946 (void)hasFolding(curwin->w_cursor.lnum,
2947 &curwin->w_cursor.lnum, NULL);
2948 }
2949 }
2950 else
2951# endif
2952 curwin->w_cursor.lnum -= n;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002956 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 foldAdjustCursor();
2958# endif
2959#ifdef FEAT_DIFF
2960 check_topfill(curwin, !flag);
2961#endif
2962 cursor_correct();
2963 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002964 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966
Bram Moolenaar860cae12010-06-05 23:22:07 +02002967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002968do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002969{
2970 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002971 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002972 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002973 colnr_T curswant = curwin->w_curswant;
2974 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002975 win_T *old_curwin = curwin;
2976 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002977 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002978 int old_VIsual_select = VIsual_select;
2979 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002980
2981 /*
2982 * loop through the cursorbound windows
2983 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002984 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002985 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002986 {
2987 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002988 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002989 if (curwin != old_curwin && curwin->w_p_crb)
2990 {
2991# ifdef FEAT_DIFF
2992 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002993 curwin->w_cursor.lnum =
2994 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002995 else
2996# endif
2997 curwin->w_cursor.lnum = line;
2998 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002999 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003000 curwin->w_curswant = curswant;
3001 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003002
Bram Moolenaar85a20022019-12-21 18:25:54 +01003003 // Make sure the cursor is in a valid position. Temporarily set
3004 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003005 restart_edit_save = restart_edit;
3006 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003007 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003008
3009 // Avoid a scroll here for the cursor position, 'scrollbind' is
3010 // more important.
3011 if (!curwin->w_p_scb)
3012 validate_cursor();
3013
Bram Moolenaar61452852011-02-01 18:01:11 +01003014 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003015 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003016 if (has_mbyte)
3017 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003018 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003019
Bram Moolenaar85a20022019-12-21 18:25:54 +01003020 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003021 if (!curwin->w_p_scb)
3022 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003023 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003024 }
3025 }
3026
3027 /*
3028 * reset current-window
3029 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003030 VIsual_select = old_VIsual_select;
3031 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 curwin = old_curwin;
3033 curbuf = old_curbuf;
3034}