blob: 4b823ba574f29d15a8a726a808d6dba93e2e477e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
118/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100119 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
120 * set.
121 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100123redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100124{
125 if ((wp->w_p_rnu
126#ifdef FEAT_SYN_HL
127 || wp->w_p_cul
128#endif
129 )
130 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200131 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200132 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000133 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100134 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200135 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100136}
137
zeertzjq3e559cd2022-03-27 19:26:55 +0100138#ifdef FEAT_SYN_HL
139/*
140 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
141 * contains "screenline".
142 */
143 static void
144redraw_for_cursorcolumn(win_T *wp)
145{
146 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
147 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100148 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100149 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100150 redraw_win_later(wp, UPD_SOME_VALID);
151 // When 'cursorlineopt' contains "screenline" need to redraw with
152 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100153 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100154 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100155 }
156}
157#endif
158
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Update curwin->w_topline and redraw if necessary.
161 * Used to update the screen before printing a message.
162 */
163 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100164update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 update_topline();
167 if (must_redraw)
168 update_screen(0);
169}
170
171/*
172 * Update curwin->w_topline to move the cursor onto the screen.
173 */
174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100175update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
177 long line_count;
178 int halfheight;
179 int n;
180 linenr_T old_topline;
181#ifdef FEAT_DIFF
182 int old_topfill;
183#endif
184#ifdef FEAT_FOLDING
185 linenr_T lnum;
186#endif
187 int check_topline = FALSE;
188 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100189 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100190 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar85a20022019-12-21 18:25:54 +0100192 // If there is no valid screen and when the window height is zero just use
193 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200194 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200195 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100196 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200197 curwin->w_topline = curwin->w_cursor.lnum;
198 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200199 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 return;
201 }
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 check_cursor_moved(curwin);
204 if (curwin->w_valid & VALID_TOPLINE)
205 return;
206
Bram Moolenaar85a20022019-12-21 18:25:54 +0100207 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000208 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100209 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211 old_topline = curwin->w_topline;
212#ifdef FEAT_DIFF
213 old_topfill = curwin->w_topfill;
214#endif
215
216 /*
217 * If the buffer is empty, always set topline to 1.
218 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
221 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100222 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 curwin->w_botline = 2;
225 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228
229 /*
230 * If the cursor is above or near the top of the window, scroll the window
231 * to show the line the cursor is in, with 'scrolloff' context.
232 */
233 else
234 {
235 if (curwin->w_topline > 1)
236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100237 // If the cursor is above topline, scrolling is always needed.
238 // If the cursor is far below topline and there is no folding,
239 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (curwin->w_cursor.lnum < curwin->w_topline)
241 check_topline = TRUE;
242 else if (check_top_offset())
243 check_topline = TRUE;
244 }
245#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100246 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
248 curwin->w_topline))
249 check_topline = TRUE;
250#endif
251
252 if (check_topline)
253 {
254 halfheight = curwin->w_height / 2 - 1;
255 if (halfheight < 2)
256 halfheight = 2;
257
258#ifdef FEAT_FOLDING
259 if (hasAnyFolding(curwin))
260 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100261 // Count the number of logical lines between the cursor and
262 // topline + scrolloff (approximation of how much will be
263 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 n = 0;
265 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100266 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 {
268 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
271 break;
272 (void)hasFolding(lnum, NULL, &lnum);
273 }
274 }
275 else
276#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100277 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar85a20022019-12-21 18:25:54 +0100279 // If we weren't very close to begin with, we scroll to put the
280 // cursor in the middle of the window. Otherwise put the cursor
281 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 if (n >= halfheight)
283 scroll_cursor_halfway(FALSE);
284 else
285 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000286 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 check_botline = TRUE;
288 }
289 }
290
291 else
292 {
293#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
296#endif
297 check_botline = TRUE;
298 }
299 }
300
301 /*
302 * If the cursor is below the bottom of the window, scroll the window
303 * to put the cursor on the window.
304 * When w_botline is invalid, recompute it first, to avoid a redraw later.
305 * If w_botline was approximated, we might need a redraw later in a few
306 * cases, but we don't want to spend (a lot of) time recomputing w_botline
307 * for every small change.
308 */
309 if (check_botline)
310 {
311 if (!(curwin->w_valid & VALID_BOTLINE_AP))
312 validate_botline();
313
314 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
315 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000316 if (curwin->w_cursor.lnum < curwin->w_botline)
317 {
318 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100319 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_FOLDING
321 || hasAnyFolding(curwin)
322#endif
323 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 lineoff_T loff;
326
Bram Moolenaar85a20022019-12-21 18:25:54 +0100327 // Cursor is (a few lines) above botline, check if there are
328 // 'scrolloff' window lines below the cursor. If not, need to
329 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 n = curwin->w_empty_rows;
331 loff.lnum = curwin->w_cursor.lnum;
332#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
335#endif
336#ifdef FEAT_DIFF
337 loff.fill = 0;
338 n += curwin->w_filler_rows;
339#endif
340 loff.height = 0;
341 while (loff.lnum < curwin->w_botline
342#ifdef FEAT_DIFF
343 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
344#endif
345 )
346 {
347 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100348 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 break;
350 botline_forw(&loff);
351 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100352 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000355 }
356 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360 if (check_botline)
361 {
362#ifdef FEAT_FOLDING
363 if (hasAnyFolding(curwin))
364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100365 // Count the number of logical lines between the cursor and
366 // botline - scrolloff (approximation of how much will be
367 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 line_count = 0;
369 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (lnum <= 0 || line_count > curwin->w_height + 1)
375 break;
376 (void)hasFolding(lnum, &lnum, NULL);
377 }
378 }
379 else
380#endif
381 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100382 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000384 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 else
386 scroll_cursor_halfway(FALSE);
387 }
388 }
389 }
390 curwin->w_valid |= VALID_TOPLINE;
391
392 /*
393 * Need to redraw when topline changed.
394 */
395 if (curwin->w_topline != old_topline
396#ifdef FEAT_DIFF
397 || curwin->w_topfill != old_topfill
398#endif
399 )
400 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100401 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000402 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
404 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100405 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100408 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (curwin->w_cursor.lnum == curwin->w_topline)
411 validate_cursor();
412 }
413
Bram Moolenaar375e3392019-01-31 18:26:10 +0100414 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415}
416
417/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000418 * Return the scrolljump value to use for the current window.
419 * When 'scrolljump' is positive use it as-is.
420 * When 'scrolljump' is negative use it as a percentage of the window height.
421 */
422 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100423scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000424{
425 if (p_sj >= 0)
426 return (int)p_sj;
427 return (curwin->w_height * -p_sj) / 100;
428}
429
430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
432 * current window.
433 */
434 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100435check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 lineoff_T loff;
438 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100439 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
Bram Moolenaar375e3392019-01-31 18:26:10 +0100441 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442#ifdef FEAT_FOLDING
443 || hasAnyFolding(curwin)
444#endif
445 )
446 {
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_DIFF
449 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100450 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#else
452 n = 0;
453#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100455 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100458 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (loff.lnum < curwin->w_topline
460#ifdef FEAT_DIFF
461 || (loff.lnum == curwin->w_topline && loff.fill > 0)
462#endif
463 )
464 break;
465 n += loff.height;
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 return TRUE;
469 }
470 return FALSE;
471}
472
473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100474update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 if (curwin->w_set_curswant)
477 {
478 validate_virtcol();
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100479 curwin->w_curswant = curwin->w_virtcol
480#ifdef FEAT_PROP_POPUP
481 - curwin->w_virtcol_first_char
482#endif
483 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 curwin->w_set_curswant = FALSE;
485 }
486}
487
488/*
489 * Check if the cursor has moved. Set the w_valid flag accordingly.
490 */
491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
495 {
496 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100497 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
498 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 wp->w_valid_cursor = wp->w_cursor;
500 wp->w_valid_leftcol = wp->w_leftcol;
501 }
502 else if (wp->w_cursor.col != wp->w_valid_cursor.col
503 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100504 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
506 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
507 wp->w_valid_cursor.col = wp->w_cursor.col;
508 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511}
512
513/*
514 * Call this function when some window settings have changed, which require
515 * the cursor position, botline and topline to be recomputed and the window to
516 * be redrawn. E.g, when changing the 'wrap' option or folding.
517 */
518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100519changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 changed_window_setting_win(curwin);
522}
523
524 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100525changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 wp->w_lines_valid = 0;
528 changed_line_abv_curs_win(wp);
529 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100530 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533/*
534 * Set wp->w_topline to a certain number.
535 */
536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100537set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200539#ifdef FEAT_DIFF
540 linenr_T prev_topline = wp->w_topline;
541#endif
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100544 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
546#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100547 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100549 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
550 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000552 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200554 if (lnum != prev_topline)
555 // Keep the filler lines when the topline didn't change.
556 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100559 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100560 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561}
562
563/*
564 * Call this function when the length of the cursor line (in screen
565 * characters) has changed, and the change is before the cursor.
566 * Need to take care of w_botline separately!
567 */
568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
572 |VALID_CHEIGHT|VALID_TOPLINE);
573}
574
575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
579 |VALID_CHEIGHT|VALID_TOPLINE);
580}
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/*
583 * Call this function when the length of a line (in screen characters) above
584 * the cursor have changed.
585 * Need to take care of w_botline separately!
586 */
587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100588changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
591 |VALID_CHEIGHT|VALID_TOPLINE);
592}
593
594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100595changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
597 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
598 |VALID_CHEIGHT|VALID_TOPLINE);
599}
600
601/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100602 * Display of line has changed for "buf", invalidate cursor position and
603 * w_botline.
604 */
605 void
606changed_line_display_buf(buf_T *buf)
607{
608 win_T *wp;
609
610 FOR_ALL_WINDOWS(wp)
611 if (wp->w_buffer == buf)
612 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
613 |VALID_CROW|VALID_CHEIGHT
614 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
615}
616
617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 * Make sure the value of curwin->w_botline is valid.
619 */
620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100621validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100623 validate_botline_win(curwin);
624}
625
626/*
627 * Make sure the value of wp->w_botline is valid.
628 */
629 void
630validate_botline_win(win_T *wp)
631{
632 if (!(wp->w_valid & VALID_BOTLINE))
633 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 * Mark curwin->w_botline as invalid (because of some change in the buffer).
638 */
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
643}
644
645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652approximate_botline_win(
653 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 wp->w_valid &= ~VALID_BOTLINE;
656}
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658/*
659 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
660 */
661 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 check_cursor_moved(curwin);
665 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
666 (VALID_WROW|VALID_WCOL));
667}
668
669/*
670 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
671 * w_topline must be valid, you may need to call update_topline() first!
672 */
673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100674validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 check_cursor_moved(curwin);
677 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
678 curs_columns(TRUE);
679}
680
681#if defined(FEAT_GUI) || defined(PROTO)
682/*
683 * validate w_cline_row.
684 */
685 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 /*
689 * First make sure that w_topline is valid (after moving the cursor).
690 */
691 update_topline();
692 check_cursor_moved(curwin);
693 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100694 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695}
696#endif
697
698/*
699 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200700 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 */
702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100703curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 linenr_T lnum;
706 int i;
707 int all_invalid;
708 int valid;
709#ifdef FEAT_FOLDING
710 long fold_count;
711#endif
712
Bram Moolenaar85a20022019-12-21 18:25:54 +0100713 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 all_invalid = (!redrawing()
715 || wp->w_lines_valid == 0
716 || wp->w_lines[0].wl_lnum > wp->w_topline);
717 i = 0;
718 wp->w_cline_row = 0;
719 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
720 {
721 valid = FALSE;
722 if (!all_invalid && i < wp->w_lines_valid)
723 {
724 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100725 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (wp->w_lines[i].wl_lnum == lnum)
727 {
728#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 // Check for newly inserted lines below this row, in which
730 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (!wp->w_buffer->b_mod_set
732 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
733 || wp->w_buffer->b_mod_top
734 > wp->w_lines[i].wl_lastlnum + 1)
735#endif
736 valid = TRUE;
737 }
738 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100739 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741 if (valid
742#ifdef FEAT_DIFF
743 && (lnum != wp->w_topline || !wp->w_p_diff)
744#endif
745 )
746 {
747#ifdef FEAT_FOLDING
748 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (lnum > wp->w_cursor.lnum)
751 break;
752#else
753 ++lnum;
754#endif
755 wp->w_cline_row += wp->w_lines[i].wl_size;
756 }
757 else
758 {
759#ifdef FEAT_FOLDING
760 fold_count = foldedCount(wp, lnum, NULL);
761 if (fold_count)
762 {
763 lnum += fold_count;
764 if (lnum > wp->w_cursor.lnum)
765 break;
766 ++wp->w_cline_row;
767 }
768 else
769#endif
770#ifdef FEAT_DIFF
771 if (lnum == wp->w_topline)
772 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
773 + wp->w_topfill;
774 else
775#endif
776 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
777 }
778 }
779
780 check_cursor_moved(wp);
781 if (!(wp->w_valid & VALID_CHEIGHT))
782 {
783 if (all_invalid
784 || i == wp->w_lines_valid
785 || (i < wp->w_lines_valid
786 && (!wp->w_lines[i].wl_valid
787 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
788 {
789#ifdef FEAT_DIFF
790 if (wp->w_cursor.lnum == wp->w_topline)
791 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
792 TRUE) + wp->w_topfill;
793 else
794#endif
795 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
796#ifdef FEAT_FOLDING
797 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
798 NULL, NULL, TRUE, NULL);
799#endif
800 }
801 else if (i > wp->w_lines_valid)
802 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100803 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 wp->w_cline_height = 0;
805#ifdef FEAT_FOLDING
806 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
807 NULL, NULL, TRUE, NULL);
808#endif
809 }
810 else
811 {
812 wp->w_cline_height = wp->w_lines[i].wl_size;
813#ifdef FEAT_FOLDING
814 wp->w_cline_folded = wp->w_lines[i].wl_folded;
815#endif
816 }
817 }
818
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100819 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Validate curwin->w_virtcol only.
826 */
827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100828validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
830 validate_virtcol_win(curwin);
831}
832
833/*
834 * Validate wp->w_virtcol only.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 check_cursor_moved(wp);
840 if (!(wp->w_valid & VALID_VIRTCOL))
841 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100842#ifdef FEAT_PROP_POPUP
843 wp->w_virtcol_first_char = 0;
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000846#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100847 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000848#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100849 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851}
852
853/*
854 * Validate curwin->w_cline_height only.
855 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100856 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100857validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
859 check_cursor_moved(curwin);
860 if (!(curwin->w_valid & VALID_CHEIGHT))
861 {
862#ifdef FEAT_DIFF
863 if (curwin->w_cursor.lnum == curwin->w_topline)
864 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
865 + curwin->w_topfill;
866 else
867#endif
868 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
869#ifdef FEAT_FOLDING
870 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
871#endif
872 curwin->w_valid |= VALID_CHEIGHT;
873 }
874}
875
876/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000877 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 */
879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100880validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 colnr_T off;
883 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100884 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 validate_virtcol();
887 if (!(curwin->w_valid & VALID_WCOL))
888 {
889 col = curwin->w_virtcol;
890 off = curwin_col_off();
891 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200892 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000895 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200896 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100897 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200899 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000900 if (col > (int)curwin->w_leftcol)
901 col -= curwin->w_leftcol;
902 else
903 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100907#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100908 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911}
912
913/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200914 * Compute offset of a window, occupied by absolute or relative line number,
915 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
917 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100918win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaar64486672010-05-16 15:46:46 +0200920 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_CMDWIN
922 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
923#endif
924#ifdef FEAT_FOLDING
925 + wp->w_p_fdc
926#endif
927#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200928 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#endif
930 );
931}
932
933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100934curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 return win_col_off(curwin);
937}
938
939/*
940 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200941 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
942 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Bram Moolenaar64486672010-05-16 15:46:46 +0200947 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000948 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return 0;
950}
951
952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100953curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
955 return win_col_off2(curwin);
956}
957
958/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100959 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * Also updates curwin->w_wrow and curwin->w_cline_row.
961 * Also updates curwin->w_leftcol.
962 */
963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int off_left, off_right;
970 int n;
971 int p_lines;
972 int width = 0;
973 int textwidth;
974 int new_leftcol;
975 colnr_T startcol;
976 colnr_T endcol;
977 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100978 long so = get_scrolloff_value();
979 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 /*
982 * First make sure that w_topline is valid (after moving the cursor).
983 */
Luuk van Baal29ab5242022-09-11 16:59:53 +0100984 if (p_spsc)
985 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * Next make sure that w_cline_row is valid.
989 */
990 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100991 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#ifdef FEAT_PROP_POPUP
994 // will be set by getvvcol() but not reset
995 curwin->w_virtcol_first_char = 0;
996#endif
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /*
999 * Compute the number of virtual columns.
1000 */
1001#ifdef FEAT_FOLDING
1002 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001003 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1005 else
1006#endif
1007 getvvcol(curwin, &curwin->w_cursor,
1008 &startcol, &(curwin->w_virtcol), &endcol);
1009
Bram Moolenaar85a20022019-12-21 18:25:54 +01001010 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001012 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 extra = curwin_col_off();
1015 curwin->w_wcol = curwin->w_virtcol + extra;
1016 endcol += extra;
1017
1018 /*
1019 * Now compute w_wrow, counting screen lines from w_cline_row.
1020 */
1021 curwin->w_wrow = curwin->w_cline_row;
1022
Bram Moolenaar02631462017-09-22 15:20:32 +02001023 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (textwidth <= 0)
1025 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001026 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001027 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001028 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001029 if (curwin->w_p_wrap)
1030 curwin->w_wrow = curwin->w_height - 1;
1031 else
1032 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001034 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 width = textwidth + curwin_col_off2();
1037
Bram Moolenaar85a20022019-12-21 18:25:54 +01001038 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001039 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001041#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001042 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001043#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001044
Bram Moolenaar85a20022019-12-21 18:25:54 +01001045 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001046 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 curwin->w_wcol -= n * width;
1048 curwin->w_wrow += n;
1049
1050#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001051 // When cursor wraps to first char of next line in Insert
1052 // mode, the 'showbreak' string isn't shown, backup to first
1053 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001054 sbr = get_showbreak_value(curwin);
1055 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001056 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 curwin->w_wcol = 0;
1058#endif
1059 }
1060 }
1061
Bram Moolenaar85a20022019-12-21 18:25:54 +01001062 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1063 // is not folded.
1064 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001065 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_FOLDING
1067 && !curwin->w_cline_folded
1068#endif
1069 )
1070 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001071#ifdef FEAT_PROP_POPUP
1072 if (curwin->w_virtcol_first_char > 0)
1073 {
1074 int cols = (curwin->w_width - extra);
1075 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1076
1077 // each "above" text prop shifts the text one row down
1078 curwin->w_wrow += rows;
1079 curwin->w_wcol -= rows * cols;
1080 endcol -= rows * cols;
1081 curwin->w_cline_height = rows + 1;
1082 }
1083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 /*
1085 * If Cursor is left of the screen, scroll rightwards.
1086 * If Cursor is right of the screen, scroll leftwards
1087 * If we get closer to the edge than 'sidescrolloff', scroll a little
1088 * extra
1089 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001090 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001091 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001092 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (off_left < 0 || off_right > 0)
1094 {
1095 if (off_left < 0)
1096 diff = -off_left;
1097 else
1098 diff = off_right;
1099
Bram Moolenaar85a20022019-12-21 18:25:54 +01001100 // When far off or not enough room on either side, put cursor in
1101 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1103 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1104 else
1105 {
1106 if (diff < p_ss)
1107 diff = p_ss;
1108 if (off_left < 0)
1109 new_leftcol = curwin->w_leftcol - diff;
1110 else
1111 new_leftcol = curwin->w_leftcol + diff;
1112 }
1113 if (new_leftcol < 0)
1114 new_leftcol = 0;
1115 if (new_leftcol != (int)curwin->w_leftcol)
1116 {
1117 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001119 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121 }
1122 curwin->w_wcol -= curwin->w_leftcol;
1123 }
1124 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1125 curwin->w_wcol -= curwin->w_leftcol;
1126 else
1127 curwin->w_wcol = 0;
1128
1129#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001130 // Skip over filler lines. At the top use w_topfill, there
1131 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (curwin->w_cursor.lnum == curwin->w_topline)
1133 curwin->w_wrow += curwin->w_topfill;
1134 else
1135 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1136#endif
1137
1138 prev_skipcol = curwin->w_skipcol;
1139
1140 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if ((curwin->w_wrow >= curwin->w_height
1143 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001144 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 && (p_lines =
1146#ifdef FEAT_DIFF
1147 plines_win_nofill
1148#else
1149 plines_win
1150#endif
1151 (curwin, curwin->w_cursor.lnum, FALSE))
1152 - 1 >= curwin->w_height))
1153 && curwin->w_height != 0
1154 && curwin->w_cursor.lnum == curwin->w_topline
1155 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // Cursor past end of screen. Happens with a single line that does
1159 // not fit on screen. Find a skipcol to show the text around the
1160 // cursor. Avoid scrolling all the time. compute value of "extra":
1161 // 1: Less than 'scrolloff' lines above
1162 // 2: Less than 'scrolloff' lines below
1163 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001165 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001167 // Compute last display line of the buffer line that we want at the
1168 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (p_lines == 0)
1170 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1171 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001172 if (p_lines > curwin->w_wrow + so)
1173 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 else
1175 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001176 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 extra += 2;
1178
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001179 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 n = curwin->w_virtcol / width;
1183 if (n > curwin->w_height / 2)
1184 n -= curwin->w_height / 2;
1185 else
1186 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (n > p_lines - curwin->w_height + 1)
1189 n = p_lines - curwin->w_height + 1;
1190 curwin->w_skipcol = n * width;
1191 }
1192 else if (extra == 1)
1193 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001194 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001195 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 + width - 1) / width;
1197 if (extra > 0)
1198 {
1199 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1200 extra = curwin->w_skipcol / width;
1201 curwin->w_skipcol -= extra * width;
1202 }
1203 }
1204 else if (extra == 2)
1205 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001206 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 endcol = (n - curwin->w_height + 1) * width;
1208 while (endcol > curwin->w_virtcol)
1209 endcol -= width;
1210 if (endcol > curwin->w_skipcol)
1211 curwin->w_skipcol = endcol;
1212 }
1213
1214 curwin->w_wrow -= curwin->w_skipcol / width;
1215 if (curwin->w_wrow >= curwin->w_height)
1216 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001217 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 extra = curwin->w_wrow - curwin->w_height + 1;
1219 curwin->w_skipcol += extra * width;
1220 curwin->w_wrow -= extra;
1221 }
1222
1223 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1224 if (extra > 0)
1225 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1226 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001227 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229 else
1230 curwin->w_skipcol = 0;
1231 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001232 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001234#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001235 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001236#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001237#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1238 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1239 {
1240 curwin->w_wrow += popup_top_extra(curwin);
1241 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001242 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001243 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001244 else
1245 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001246#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001247
Bram Moolenaar08f23632019-11-16 14:19:33 +01001248 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1249 curwin->w_valid_leftcol = curwin->w_leftcol;
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1252}
1253
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001254#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001255/*
1256 * Compute the screen position of text character at "pos" in window "wp"
1257 * The resulting values are one-based, zero when character is not visible.
1258 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001259 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001260textpos2screenpos(
1261 win_T *wp,
1262 pos_T *pos,
1263 int *rowp, // screen row
1264 int *scolp, // start screen column
1265 int *ccolp, // cursor screen column
1266 int *ecolp) // end screen column
1267{
1268 colnr_T scol = 0, ccol = 0, ecol = 0;
1269 int row = 0;
1270 int rowoff = 0;
1271 colnr_T coloff = 0;
1272
Bram Moolenaar189663b2021-07-21 18:04:56 +02001273 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001274 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001275 colnr_T off;
1276 colnr_T col;
1277 int width;
1278 linenr_T lnum = pos->lnum;
1279#ifdef FEAT_FOLDING
1280 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001281
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001282 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1283#endif
1284 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1285#ifdef FEAT_FOLDING
1286 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001287 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001288 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001289 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001290 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001291 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001292#endif
1293 {
1294 getvcol(wp, pos, &scol, &ccol, &ecol);
1295
1296 // similar to what is done in validate_cursor_col()
1297 col = scol;
1298 off = win_col_off(wp);
1299 col += off;
1300 width = wp->w_width - off + win_col_off2(wp);
1301
1302 // long line wrapping, adjust row
1303 if (wp->w_p_wrap
1304 && col >= (colnr_T)wp->w_width
1305 && width > 0)
1306 {
1307 // use same formula as what is used in curs_columns()
1308 rowoff = ((col - wp->w_width) / width + 1);
1309 col -= rowoff * width;
1310 }
1311 col -= wp->w_leftcol;
1312 if (col >= wp->w_width)
1313 col = -1;
1314 if (col >= 0 && row + rowoff <= wp->w_height)
1315 {
1316 coloff = col - scol + wp->w_wincol + 1;
1317 row += W_WINROW(wp);
1318 }
1319 else
1320 // character is left, right or below of the window
1321 row = rowoff = scol = ccol = ecol = 0;
1322 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001323 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001324 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001325 *scolp = scol + coloff;
1326 *ccolp = ccol + coloff;
1327 *ecolp = ecol + coloff;
1328}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001329#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001330
Bram Moolenaar12034e22019-08-25 22:25:02 +02001331#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001332/*
1333 * "screenpos({winid}, {lnum}, {col})" function
1334 */
1335 void
1336f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1337{
1338 dict_T *dict;
1339 win_T *wp;
1340 pos_T pos;
1341 int row = 0;
1342 int scol = 0, ccol = 0, ecol = 0;
1343
Bram Moolenaar93a10962022-06-16 11:42:09 +01001344 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001345 return;
1346 dict = rettv->vval.v_dict;
1347
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001348 if (in_vim9script()
1349 && (check_for_number_arg(argvars, 0) == FAIL
1350 || check_for_number_arg(argvars, 1) == FAIL
1351 || check_for_number_arg(argvars, 2) == FAIL))
1352 return;
1353
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001354 wp = find_win_by_nr_or_id(&argvars[0]);
1355 if (wp == NULL)
1356 return;
1357
1358 pos.lnum = tv_get_number(&argvars[1]);
1359 pos.col = tv_get_number(&argvars[2]) - 1;
1360 pos.coladd = 0;
1361 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1362
1363 dict_add_number(dict, "row", row);
1364 dict_add_number(dict, "col", scol);
1365 dict_add_number(dict, "curscol", ccol);
1366 dict_add_number(dict, "endcol", ecol);
1367}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001368
1369/*
1370 * "virtcol2col({winid}, {lnum}, {col})" function
1371 */
1372 void
1373f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1374{
1375 win_T *wp;
1376 linenr_T lnum;
1377 int screencol;
1378 int error = FALSE;
1379
1380 rettv->vval.v_number = -1;
1381
1382 if (check_for_number_arg(argvars, 0) == FAIL
1383 || check_for_number_arg(argvars, 1) == FAIL
1384 || check_for_number_arg(argvars, 2) == FAIL)
1385 return;
1386
1387 wp = find_win_by_nr_or_id(&argvars[0]);
1388 if (wp == NULL)
1389 return;
1390
1391 lnum = tv_get_number_chk(&argvars[1], &error);
1392 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1393 return;
1394
1395 screencol = tv_get_number_chk(&argvars[2], &error);
1396 if (error || screencol < 0)
1397 return;
1398
1399 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1400}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001401#endif
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001407scrolldown(
1408 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001409 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001411 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int wrow;
1413 int moved = FALSE;
1414
1415#ifdef FEAT_FOLDING
1416 linenr_T first;
1417
Bram Moolenaar85a20022019-12-21 18:25:54 +01001418 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1420#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001421 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 while (line_count-- > 0)
1423 {
1424#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001425 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1426 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 ++curwin->w_topfill;
1429 ++done;
1430 }
1431 else
1432#endif
1433 {
1434 if (curwin->w_topline == 1)
1435 break;
1436 --curwin->w_topline;
1437#ifdef FEAT_DIFF
1438 curwin->w_topfill = 0;
1439#endif
1440#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001441 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (hasFolding(curwin->w_topline, &first, NULL))
1443 {
1444 ++done;
1445 if (!byfold)
1446 line_count -= curwin->w_topline - first - 1;
1447 curwin->w_botline -= curwin->w_topline - first;
1448 curwin->w_topline = first;
1449 }
1450 else
1451#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001452 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001454 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 invalidate_botline();
1456 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001457 curwin->w_wrow += done; // keep w_wrow updated
1458 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460#ifdef FEAT_DIFF
1461 if (curwin->w_cursor.lnum == curwin->w_topline)
1462 curwin->w_cline_row = 0;
1463 check_topfill(curwin, TRUE);
1464#endif
1465
1466 /*
1467 * Compute the row number of the last row of the cursor line
1468 * and move the cursor onto the displayed part of the window.
1469 */
1470 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001471 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 validate_virtcol();
1474 validate_cheight();
1475 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001476 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1479 {
1480#ifdef FEAT_FOLDING
1481 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1482 {
1483 --wrow;
1484 if (first == 1)
1485 curwin->w_cursor.lnum = 1;
1486 else
1487 curwin->w_cursor.lnum = first - 1;
1488 }
1489 else
1490#endif
1491 wrow -= plines(curwin->w_cursor.lnum--);
1492 curwin->w_valid &=
1493 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1494 moved = TRUE;
1495 }
1496 if (moved)
1497 {
1498#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001499 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 foldAdjustCursor();
1501#endif
1502 coladvance(curwin->w_curswant);
1503 }
1504}
1505
1506/*
1507 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510scrollup(
1511 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001512 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1515 linenr_T lnum;
1516
1517 if (
1518# ifdef FEAT_FOLDING
1519 (byfold && hasAnyFolding(curwin))
1520# ifdef FEAT_DIFF
1521 ||
1522# endif
1523# endif
1524# ifdef FEAT_DIFF
1525 curwin->w_p_diff
1526# endif
1527 )
1528 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001529 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 lnum = curwin->w_topline;
1531 while (line_count--)
1532 {
1533# ifdef FEAT_DIFF
1534 if (curwin->w_topfill > 0)
1535 --curwin->w_topfill;
1536 else
1537# endif
1538 {
1539# ifdef FEAT_FOLDING
1540 if (byfold)
1541 (void)hasFolding(lnum, NULL, &lnum);
1542# endif
1543 if (lnum >= curbuf->b_ml.ml_line_count)
1544 break;
1545 ++lnum;
1546# ifdef FEAT_DIFF
1547 curwin->w_topfill = diff_check_fill(curwin, lnum);
1548# endif
1549 }
1550 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001551 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 curwin->w_botline += lnum - curwin->w_topline;
1553 curwin->w_topline = lnum;
1554 }
1555 else
1556#endif
1557 {
1558 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001559 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 }
1561
1562 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1563 curwin->w_topline = curbuf->b_ml.ml_line_count;
1564 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1565 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1566
1567#ifdef FEAT_DIFF
1568 check_topfill(curwin, FALSE);
1569#endif
1570
1571#ifdef FEAT_FOLDING
1572 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001573 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1575#endif
1576
1577 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1578 if (curwin->w_cursor.lnum < curwin->w_topline)
1579 {
1580 curwin->w_cursor.lnum = curwin->w_topline;
1581 curwin->w_valid &=
1582 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1583 coladvance(curwin->w_curswant);
1584 }
1585}
1586
1587#ifdef FEAT_DIFF
1588/*
1589 * Don't end up with too many filler lines in the window.
1590 */
1591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001592check_topfill(
1593 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 int n;
1597
1598 if (wp->w_topfill > 0)
1599 {
1600 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1601 if (wp->w_topfill + n > wp->w_height)
1602 {
1603 if (down && wp->w_topline > 1)
1604 {
1605 --wp->w_topline;
1606 wp->w_topfill = 0;
1607 }
1608 else
1609 {
1610 wp->w_topfill = wp->w_height - n;
1611 if (wp->w_topfill < 0)
1612 wp->w_topfill = 0;
1613 }
1614 }
1615 }
1616}
1617
1618/*
1619 * Use as many filler lines as possible for w_topline. Make sure w_topline
1620 * is still visible.
1621 */
1622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001623max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 int n;
1626
1627 n = plines_nofill(curwin->w_topline);
1628 if (n >= curwin->w_height)
1629 curwin->w_topfill = 0;
1630 else
1631 {
1632 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1633 if (curwin->w_topfill + n > curwin->w_height)
1634 curwin->w_topfill = curwin->w_height - n;
1635 }
1636}
1637#endif
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
1640 * Scroll the screen one line down, but don't do it if it would move the
1641 * cursor off the screen.
1642 */
1643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001644scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 int end_row;
1647#ifdef FEAT_DIFF
1648 int can_fill = (curwin->w_topfill
1649 < diff_check_fill(curwin, curwin->w_topline));
1650#endif
1651
1652 if (curwin->w_topline <= 1
1653#ifdef FEAT_DIFF
1654 && !can_fill
1655#endif
1656 )
1657 return;
1658
Bram Moolenaar85a20022019-12-21 18:25:54 +01001659 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
1661 /*
1662 * Compute the row number of the last row of the cursor line
1663 * and make sure it doesn't go off the screen. Make sure the cursor
1664 * doesn't go past 'scrolloff' lines from the screen end.
1665 */
1666 end_row = curwin->w_wrow;
1667#ifdef FEAT_DIFF
1668 if (can_fill)
1669 ++end_row;
1670 else
1671 end_row += plines_nofill(curwin->w_topline - 1);
1672#else
1673 end_row += plines(curwin->w_topline - 1);
1674#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001675 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
1677 validate_cheight();
1678 validate_virtcol();
1679 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001680 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001682 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 {
1684#ifdef FEAT_DIFF
1685 if (can_fill)
1686 {
1687 ++curwin->w_topfill;
1688 check_topfill(curwin, TRUE);
1689 }
1690 else
1691 {
1692 --curwin->w_topline;
1693 curwin->w_topfill = 0;
1694 }
1695#else
1696 --curwin->w_topline;
1697#endif
1698#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001699 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001701 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1703 }
1704}
1705
1706/*
1707 * Scroll the screen one line up, but don't do it if it would move the cursor
1708 * off the screen.
1709 */
1710 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001711scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int start_row;
1714
1715 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1716#ifdef FEAT_DIFF
1717 && curwin->w_topfill == 0
1718#endif
1719 )
1720 return;
1721
Bram Moolenaar85a20022019-12-21 18:25:54 +01001722 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 /*
1725 * Compute the row number of the first row of the cursor line
1726 * and make sure it doesn't go off the screen. Make sure the cursor
1727 * doesn't go before 'scrolloff' lines from the screen start.
1728 */
1729#ifdef FEAT_DIFF
1730 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1731 - curwin->w_topfill;
1732#else
1733 start_row = curwin->w_wrow - plines(curwin->w_topline);
1734#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001735 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001738 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001740 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 {
1742#ifdef FEAT_DIFF
1743 if (curwin->w_topfill > 0)
1744 --curwin->w_topfill;
1745 else
1746#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001747 {
1748#ifdef FEAT_FOLDING
1749 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001752 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1755 }
1756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758/*
1759 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1760 * a (wrapped) text line. Uses and sets "lp->fill".
1761 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001762 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 */
1764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001765topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767#ifdef FEAT_DIFF
1768 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1769 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 ++lp->fill;
1772 lp->height = 1;
1773 }
1774 else
1775#endif
1776 {
1777 --lp->lnum;
1778#ifdef FEAT_DIFF
1779 lp->fill = 0;
1780#endif
1781 if (lp->lnum < 1)
1782 lp->height = MAXCOL;
1783 else
1784#ifdef FEAT_FOLDING
1785 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001786 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 lp->height = 1;
1788 else
1789#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001790 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792}
1793
1794/*
1795 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1796 * a (wrapped) text line. Uses and sets "lp->fill".
1797 * Returns the height of the added line in "lp->height".
1798 * Lines below the last one are incredibly high.
1799 */
1800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001801botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803#ifdef FEAT_DIFF
1804 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1805 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001806 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 ++lp->fill;
1808 lp->height = 1;
1809 }
1810 else
1811#endif
1812 {
1813 ++lp->lnum;
1814#ifdef FEAT_DIFF
1815 lp->fill = 0;
1816#endif
1817 if (lp->lnum > curbuf->b_ml.ml_line_count)
1818 lp->height = MAXCOL;
1819 else
1820#ifdef FEAT_FOLDING
1821 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 lp->height = 1;
1824 else
1825#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001826 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828}
1829
1830#ifdef FEAT_DIFF
1831/*
1832 * Switch from including filler lines below lp->lnum to including filler
1833 * lines above loff.lnum + 1. This keeps pointing to the same line.
1834 * When there are no filler lines nothing changes.
1835 */
1836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001837botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 if (lp->fill > 0)
1840 {
1841 ++lp->lnum;
1842 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1843 }
1844}
1845
1846/*
1847 * Switch from including filler lines above lp->lnum to including filler
1848 * lines below loff.lnum - 1. This keeps pointing to the same line.
1849 * When there are no filler lines nothing changes.
1850 */
1851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001852topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 if (lp->fill > 0)
1855 {
1856 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1857 --lp->lnum;
1858 }
1859}
1860#endif
1861
1862/*
1863 * Recompute topline to put the cursor at the top of the window.
1864 * Scroll at least "min_scroll" lines.
1865 * If "always" is TRUE, always set topline (for "zt").
1866 */
1867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001868scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 int scrolled = 0;
1871 int extra = 0;
1872 int used;
1873 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001874 linenr_T top; // just above displayed lines
1875 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 linenr_T old_topline = curwin->w_topline;
1877#ifdef FEAT_DIFF
1878 linenr_T old_topfill = curwin->w_topfill;
1879#endif
1880 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001881 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (mouse_dragging > 0)
1884 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 /*
1887 * Decrease topline until:
1888 * - it has become 1
1889 * - (part of) the cursor line is moved off the screen or
1890 * - moved at least 'scrolljump' lines and
1891 * - at least 'scrolloff' lines above and below the cursor
1892 */
1893 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001894 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (curwin->w_cursor.lnum < curwin->w_topline)
1896 scrolled = used;
1897
1898#ifdef FEAT_FOLDING
1899 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1900 {
1901 --top;
1902 ++bot;
1903 }
1904 else
1905#endif
1906 {
1907 top = curwin->w_cursor.lnum - 1;
1908 bot = curwin->w_cursor.lnum + 1;
1909 }
1910 new_topline = top + 1;
1911
1912#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001913 // "used" already contains the number of filler lines above, don't add it
1914 // again.
1915 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001916 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
1918
1919 /*
1920 * Check if the lines from "top" to "bot" fit in the window. If they do,
1921 * set new_topline and advance "top" and "bot" to include more lines.
1922 */
1923 while (top > 0)
1924 {
1925#ifdef FEAT_FOLDING
1926 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001927 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 i = 1;
1929 else
1930#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001931 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 used += i;
1933 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1934 {
1935#ifdef FEAT_FOLDING
1936 if (hasFolding(bot, NULL, &bot))
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 ++used;
1939 else
1940#endif
1941 used += plines(bot);
1942 }
1943 if (used > curwin->w_height)
1944 break;
1945 if (top < curwin->w_topline)
1946 scrolled += i;
1947
1948 /*
1949 * If scrolling is needed, scroll at least 'sj' lines.
1950 */
1951 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1952 && extra >= off)
1953 break;
1954
1955 extra += i;
1956 new_topline = top;
1957 --top;
1958 ++bot;
1959 }
1960
1961 /*
1962 * If we don't have enough space, put cursor in the middle.
1963 * This makes sure we get the same position when using "k" and "j"
1964 * in a small window.
1965 */
1966 if (used > curwin->w_height)
1967 scroll_cursor_halfway(FALSE);
1968 else
1969 {
1970 /*
1971 * If "always" is FALSE, only adjust topline to a lower value, higher
1972 * value may happen with wrapping lines
1973 */
1974 if (new_topline < curwin->w_topline || always)
1975 curwin->w_topline = new_topline;
1976 if (curwin->w_topline > curwin->w_cursor.lnum)
1977 curwin->w_topline = curwin->w_cursor.lnum;
1978#ifdef FEAT_DIFF
1979 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1980 if (curwin->w_topfill > 0 && extra > off)
1981 {
1982 curwin->w_topfill -= extra - off;
1983 if (curwin->w_topfill < 0)
1984 curwin->w_topfill = 0;
1985 }
1986 check_topfill(curwin, FALSE);
1987#endif
1988 if (curwin->w_topline != old_topline
1989#ifdef FEAT_DIFF
1990 || curwin->w_topfill != old_topfill
1991#endif
1992 )
1993 curwin->w_valid &=
1994 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1995 curwin->w_valid |= VALID_TOPLINE;
1996 }
1997}
1998
1999/*
2000 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2001 * screen lines for text lines.
2002 */
2003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006#ifdef FEAT_DIFF
2007 wp->w_filler_rows = 0;
2008#endif
2009 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002010 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 else
2012 {
2013 wp->w_empty_rows = wp->w_height - used;
2014#ifdef FEAT_DIFF
2015 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2016 {
2017 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2018 if (wp->w_empty_rows > wp->w_filler_rows)
2019 wp->w_empty_rows -= wp->w_filler_rows;
2020 else
2021 {
2022 wp->w_filler_rows = wp->w_empty_rows;
2023 wp->w_empty_rows = 0;
2024 }
2025 }
2026#endif
2027 }
2028}
2029
2030/*
2031 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002032 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2034 * This is messy stuff!!!
2035 */
2036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int used;
2040 int scrolled = 0;
2041 int extra = 0;
2042 int i;
2043 linenr_T line_count;
2044 linenr_T old_topline = curwin->w_topline;
2045 lineoff_T loff;
2046 lineoff_T boff;
2047#ifdef FEAT_DIFF
2048 int old_topfill = curwin->w_topfill;
2049 int fill_below_window;
2050#endif
2051 linenr_T old_botline = curwin->w_botline;
2052 linenr_T old_valid = curwin->w_valid;
2053 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002055 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 cln = curwin->w_cursor.lnum;
2058 if (set_topbot)
2059 {
2060 used = 0;
2061 curwin->w_botline = cln + 1;
2062#ifdef FEAT_DIFF
2063 loff.fill = 0;
2064#endif
2065 for (curwin->w_topline = curwin->w_botline;
2066 curwin->w_topline > 1;
2067 curwin->w_topline = loff.lnum)
2068 {
2069 loff.lnum = curwin->w_topline;
2070 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002071 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 break;
2073 used += loff.height;
2074#ifdef FEAT_DIFF
2075 curwin->w_topfill = loff.fill;
2076#endif
2077 }
2078 set_empty_rows(curwin, used);
2079 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2080 if (curwin->w_topline != old_topline
2081#ifdef FEAT_DIFF
2082 || curwin->w_topfill != old_topfill
2083#endif
2084 )
2085 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2086 }
2087 else
2088 validate_botline();
2089
Bram Moolenaar85a20022019-12-21 18:25:54 +01002090 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#ifdef FEAT_DIFF
2092 used = plines_nofill(cln);
2093#else
2094 validate_cheight();
2095 used = curwin->w_cline_height;
2096#endif
2097
Bram Moolenaar85a20022019-12-21 18:25:54 +01002098 // If the cursor is below botline, we will at least scroll by the height
2099 // of the cursor line. Correct for empty lines, which are really part of
2100 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (cln >= curwin->w_botline)
2102 {
2103 scrolled = used;
2104 if (cln == curwin->w_botline)
2105 scrolled -= curwin->w_empty_rows;
2106 }
2107
2108 /*
2109 * Stop counting lines to scroll when
2110 * - hitting start of the file
2111 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002112 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 * - lines between botline and cursor have been counted
2114 */
2115#ifdef FEAT_FOLDING
2116 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2117#endif
2118 {
2119 loff.lnum = cln;
2120 boff.lnum = cln;
2121 }
2122#ifdef FEAT_DIFF
2123 loff.fill = 0;
2124 boff.fill = 0;
2125 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2126 - curwin->w_filler_rows;
2127#endif
2128
2129 while (loff.lnum > 1)
2130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2132 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002134 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2136 && loff.lnum <= curwin->w_botline
2137#ifdef FEAT_DIFF
2138 && (loff.lnum < curwin->w_botline
2139 || loff.fill >= fill_below_window)
2140#endif
2141 )
2142 break;
2143
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002146 if (loff.height == MAXCOL)
2147 used = MAXCOL;
2148 else
2149 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (used > curwin->w_height)
2151 break;
2152 if (loff.lnum >= curwin->w_botline
2153#ifdef FEAT_DIFF
2154 && (loff.lnum > curwin->w_botline
2155 || loff.fill <= fill_below_window)
2156#endif
2157 )
2158 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002159 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 scrolled += loff.height;
2161 if (loff.lnum == curwin->w_botline
2162#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002163 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#endif
2165 )
2166 scrolled -= curwin->w_empty_rows;
2167 }
2168
2169 if (boff.lnum < curbuf->b_ml.ml_line_count)
2170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002171 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 botline_forw(&boff);
2173 used += boff.height;
2174 if (used > curwin->w_height)
2175 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002176 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2177 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 extra += boff.height;
2180 if (boff.lnum >= curwin->w_botline
2181#ifdef FEAT_DIFF
2182 || (boff.lnum + 1 == curwin->w_botline
2183 && boff.fill > curwin->w_filler_rows)
2184#endif
2185 )
2186 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002187 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 scrolled += boff.height;
2189 if (boff.lnum == curwin->w_botline
2190#ifdef FEAT_DIFF
2191 && boff.fill == 0
2192#endif
2193 )
2194 scrolled -= curwin->w_empty_rows;
2195 }
2196 }
2197 }
2198 }
2199
Bram Moolenaar85a20022019-12-21 18:25:54 +01002200 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (scrolled <= 0)
2202 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002203 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else if (used > curwin->w_height)
2205 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 else
2208 {
2209 line_count = 0;
2210#ifdef FEAT_DIFF
2211 boff.fill = curwin->w_topfill;
2212#endif
2213 boff.lnum = curwin->w_topline - 1;
2214 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2215 {
2216 botline_forw(&boff);
2217 i += boff.height;
2218 ++line_count;
2219 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 line_count = 9999;
2222 }
2223
2224 /*
2225 * Scroll up if the cursor is off the bottom of the screen a bit.
2226 * Otherwise put it at 1/2 of the screen.
2227 */
2228 if (line_count >= curwin->w_height && line_count > min_scroll)
2229 scroll_cursor_halfway(FALSE);
2230 else
2231 scrollup(line_count, TRUE);
2232
2233 /*
2234 * If topline didn't change we need to restore w_botline and w_empty_rows
2235 * (we changed them).
2236 * If topline did change, update_screen() will set botline.
2237 */
2238 if (curwin->w_topline == old_topline && set_topbot)
2239 {
2240 curwin->w_botline = old_botline;
2241 curwin->w_empty_rows = old_empty_rows;
2242 curwin->w_valid = old_valid;
2243 }
2244 curwin->w_valid |= VALID_TOPLINE;
2245}
2246
2247/*
2248 * Recompute topline to put the cursor halfway the window
2249 * If "atend" is TRUE, also put it halfway at the end of the file.
2250 */
2251 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002252scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254 int above = 0;
2255 linenr_T topline;
2256#ifdef FEAT_DIFF
2257 int topfill = 0;
2258#endif
2259 int below = 0;
2260 int used;
2261 lineoff_T loff;
2262 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002263#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002264 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002267#ifdef FEAT_PROP_POPUP
2268 // if the width changed this needs to be updated first
2269 may_update_popup_position();
2270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2272#ifdef FEAT_FOLDING
2273 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2274#endif
2275#ifdef FEAT_DIFF
2276 used = plines_nofill(loff.lnum);
2277 loff.fill = 0;
2278 boff.fill = 0;
2279#else
2280 used = plines(loff.lnum);
2281#endif
2282 topline = loff.lnum;
2283 while (topline > 1)
2284 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002285 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 if (boff.lnum < curbuf->b_ml.ml_line_count)
2288 {
2289 botline_forw(&boff);
2290 used += boff.height;
2291 if (used > curwin->w_height)
2292 break;
2293 below += boff.height;
2294 }
2295 else
2296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (atend)
2299 ++used;
2300 }
2301 }
2302
Bram Moolenaar85a20022019-12-21 18:25:54 +01002303 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002306 if (loff.height == MAXCOL)
2307 used = MAXCOL;
2308 else
2309 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (used > curwin->w_height)
2311 break;
2312 above += loff.height;
2313 topline = loff.lnum;
2314#ifdef FEAT_DIFF
2315 topfill = loff.fill;
2316#endif
2317 }
2318 }
2319#ifdef FEAT_FOLDING
2320 if (!hasFolding(topline, &curwin->w_topline, NULL))
2321#endif
2322 curwin->w_topline = topline;
2323#ifdef FEAT_DIFF
2324 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002325 if (old_topline > curwin->w_topline + curwin->w_height)
2326 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 check_topfill(curwin, FALSE);
2328#endif
2329 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2330 curwin->w_valid |= VALID_TOPLINE;
2331}
2332
2333/*
2334 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002335 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 * If not possible, put it at the same position as scroll_cursor_halfway().
2337 * When called topline must be valid!
2338 */
2339 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002340cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002342 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 linenr_T botline;
2346 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002347 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002349 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
2352 * How many lines we would like to have above/below the cursor depends on
2353 * whether the first/last line of the file is on screen.
2354 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002355 above_wanted = so;
2356 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002357 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 above_wanted = mouse_dragging - 1;
2360 below_wanted = mouse_dragging - 1;
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (curwin->w_topline == 1)
2363 {
2364 above_wanted = 0;
2365 max_off = curwin->w_height / 2;
2366 if (below_wanted > max_off)
2367 below_wanted = max_off;
2368 }
2369 validate_botline();
2370 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002371 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 below_wanted = 0;
2374 max_off = (curwin->w_height - 1) / 2;
2375 if (above_wanted > max_off)
2376 above_wanted = max_off;
2377 }
2378
2379 /*
2380 * If there are sufficient file-lines above and below the cursor, we can
2381 * return now.
2382 */
2383 cln = curwin->w_cursor.lnum;
2384 if (cln >= curwin->w_topline + above_wanted
2385 && cln < curwin->w_botline - below_wanted
2386#ifdef FEAT_FOLDING
2387 && !hasAnyFolding(curwin)
2388#endif
2389 )
2390 return;
2391
2392 /*
2393 * Narrow down the area where the cursor can be put by taking lines from
2394 * the top and the bottom until:
2395 * - the desired context lines are found
2396 * - the lines from the top is past the lines from the bottom
2397 */
2398 topline = curwin->w_topline;
2399 botline = curwin->w_botline - 1;
2400#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002401 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 above = curwin->w_topfill;
2403 below = curwin->w_filler_rows;
2404#endif
2405 while ((above < above_wanted || below < below_wanted) && topline < botline)
2406 {
2407 if (below < below_wanted && (below <= above || above >= above_wanted))
2408 {
2409#ifdef FEAT_FOLDING
2410 if (hasFolding(botline, &botline, NULL))
2411 ++below;
2412 else
2413#endif
2414 below += plines(botline);
2415 --botline;
2416 }
2417 if (above < above_wanted && (above < below || below >= below_wanted))
2418 {
2419#ifdef FEAT_FOLDING
2420 if (hasFolding(topline, NULL, &topline))
2421 ++above;
2422 else
2423#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002424 above += PLINES_NOFILL(topline);
2425#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002426 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (topline < botline)
2428 above += diff_check_fill(curwin, topline + 1);
2429#endif
2430 ++topline;
2431 }
2432 }
2433 if (topline == botline || botline == 0)
2434 curwin->w_cursor.lnum = topline;
2435 else if (topline > botline)
2436 curwin->w_cursor.lnum = botline;
2437 else
2438 {
2439 if (cln < topline && curwin->w_topline > 1)
2440 {
2441 curwin->w_cursor.lnum = topline;
2442 curwin->w_valid &=
2443 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2444 }
2445 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2446 {
2447 curwin->w_cursor.lnum = botline;
2448 curwin->w_valid &=
2449 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2450 }
2451 }
2452 curwin->w_valid |= VALID_TOPLINE;
2453}
2454
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002455static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457/*
2458 * move screen 'count' pages up or down and update screen
2459 *
2460 * return FAIL for failure, OK otherwise
2461 */
2462 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 long n;
2466 int retval = OK;
2467 lineoff_T loff;
2468 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002469 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaar85a20022019-12-21 18:25:54 +01002471 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 beep_flush();
2474 return FAIL;
2475 }
2476
2477 for ( ; count > 0; --count)
2478 {
2479 validate_botline();
2480 /*
2481 * It's an error to move a page up when the first line is already on
2482 * the screen. It's an error to move a page down when the last line
2483 * is on the screen and the topline is 'scrolloff' lines from the
2484 * last line.
2485 */
2486 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002487 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2489 : (curwin->w_topline == 1
2490#ifdef FEAT_DIFF
2491 && curwin->w_topfill ==
2492 diff_check_fill(curwin, curwin->w_topline)
2493#endif
2494 ))
2495 {
2496 beep_flush();
2497 retval = FAIL;
2498 break;
2499 }
2500
2501#ifdef FEAT_DIFF
2502 loff.fill = 0;
2503#endif
2504 if (dir == FORWARD)
2505 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002506 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002508 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002509 if (p_window <= 2)
2510 ++curwin->w_topline;
2511 else
2512 curwin->w_topline += p_window - 2;
2513 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2514 curwin->w_topline = curbuf->b_ml.ml_line_count;
2515 curwin->w_cursor.lnum = curwin->w_topline;
2516 }
2517 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2518 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 curwin->w_topline = curbuf->b_ml.ml_line_count;
2521#ifdef FEAT_DIFF
2522 curwin->w_topfill = 0;
2523#endif
2524 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2525 }
2526 else
2527 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 // For the overlap, start with the line just below the window
2529 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 loff.lnum = curwin->w_botline;
2531#ifdef FEAT_DIFF
2532 loff.fill = diff_check_fill(curwin, loff.lnum)
2533 - curwin->w_filler_rows;
2534#endif
2535 get_scroll_overlap(&loff, -1);
2536 curwin->w_topline = loff.lnum;
2537#ifdef FEAT_DIFF
2538 curwin->w_topfill = loff.fill;
2539 check_topfill(curwin, FALSE);
2540#endif
2541 curwin->w_cursor.lnum = curwin->w_topline;
2542 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2543 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2544 }
2545 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002546 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548#ifdef FEAT_DIFF
2549 if (curwin->w_topline == 1)
2550 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 max_topfill();
2553 continue;
2554 }
2555#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002556 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002557 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002559 if (p_window <= 2)
2560 --curwin->w_topline;
2561 else
2562 curwin->w_topline -= p_window - 2;
2563 if (curwin->w_topline < 1)
2564 curwin->w_topline = 1;
2565 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2566 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2567 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2568 continue;
2569 }
2570
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 // Find the line at the top of the window that is going to be the
2572 // line at the bottom of the window. Make sure this results in
2573 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 loff.lnum = curwin->w_topline - 1;
2575#ifdef FEAT_DIFF
2576 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2577 - curwin->w_topfill;
2578#endif
2579 get_scroll_overlap(&loff, 1);
2580
2581 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2582 {
2583 loff.lnum = curbuf->b_ml.ml_line_count;
2584#ifdef FEAT_DIFF
2585 loff.fill = 0;
2586 }
2587 else
2588 {
2589 botline_topline(&loff);
2590#endif
2591 }
2592 curwin->w_cursor.lnum = loff.lnum;
2593
Bram Moolenaar85a20022019-12-21 18:25:54 +01002594 // Find the line just above the new topline to get the right line
2595 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 n = 0;
2597 while (n <= curwin->w_height && loff.lnum >= 1)
2598 {
2599 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002600 if (loff.height == MAXCOL)
2601 n = MAXCOL;
2602 else
2603 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002605 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 curwin->w_topline = 1;
2608#ifdef FEAT_DIFF
2609 max_topfill();
2610#endif
2611 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2612 }
2613 else
2614 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#ifdef FEAT_DIFF
2617 topline_botline(&loff);
2618#endif
2619 botline_forw(&loff);
2620 botline_forw(&loff);
2621#ifdef FEAT_DIFF
2622 botline_topline(&loff);
2623#endif
2624#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2627#endif
2628
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 // Always scroll at least one line. Avoid getting stuck on
2630 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (loff.lnum >= curwin->w_topline
2632#ifdef FEAT_DIFF
2633 && (loff.lnum > curwin->w_topline
2634 || loff.fill >= curwin->w_topfill)
2635#endif
2636 )
2637 {
2638#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002639 // First try using the maximum number of filler lines. If
2640 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 loff.fill = curwin->w_topfill;
2642 if (curwin->w_topfill < diff_check_fill(curwin,
2643 curwin->w_topline))
2644 max_topfill();
2645 if (curwin->w_topfill == loff.fill)
2646#endif
2647 {
2648 --curwin->w_topline;
2649#ifdef FEAT_DIFF
2650 curwin->w_topfill = 0;
2651#endif
2652 }
2653 comp_botline(curwin);
2654 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002655 curwin->w_valid &=
2656 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 else
2659 {
2660 curwin->w_topline = loff.lnum;
2661#ifdef FEAT_DIFF
2662 curwin->w_topfill = loff.fill;
2663 check_topfill(curwin, FALSE);
2664#endif
2665 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2666 }
2667 }
2668 }
2669 }
2670#ifdef FEAT_FOLDING
2671 foldAdjustCursor();
2672#endif
2673 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002674 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002675 if (retval == OK)
2676 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2678
Bram Moolenaar907dad72018-07-10 15:07:15 +02002679 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002681 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2682 // But make sure we scroll at least one line (happens with mix of long
2683 // wrapping lines and non-wrapping line).
2684 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002686 scroll_cursor_top(1, FALSE);
2687 if (curwin->w_topline <= old_topline
2688 && old_topline < curbuf->b_ml.ml_line_count)
2689 {
2690 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002692 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2693#endif
2694 }
2695 }
2696#ifdef FEAT_FOLDING
2697 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002702 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return retval;
2704}
2705
2706/*
2707 * Decide how much overlap to use for page-up or page-down scrolling.
2708 * This is symmetric, so that doing both keeps the same lines displayed.
2709 * Three lines are examined:
2710 *
2711 * before CTRL-F after CTRL-F / before CTRL-B
2712 * etc. l1
2713 * l1 last but one line ------------
2714 * l2 last text line l2 top text line
2715 * ------------- l3 second text line
2716 * l3 etc.
2717 */
2718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002719get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 int h1, h2, h3, h4;
2722 int min_height = curwin->w_height - 2;
2723 lineoff_T loff0, loff1, loff2;
2724
2725#ifdef FEAT_DIFF
2726 if (lp->fill > 0)
2727 lp->height = 1;
2728 else
2729 lp->height = plines_nofill(lp->lnum);
2730#else
2731 lp->height = plines(lp->lnum);
2732#endif
2733 h1 = lp->height;
2734 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002735 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 loff0 = *lp;
2738 if (dir > 0)
2739 botline_forw(lp);
2740 else
2741 topline_back(lp);
2742 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002743 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002745 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return;
2747 }
2748
2749 loff1 = *lp;
2750 if (dir > 0)
2751 botline_forw(lp);
2752 else
2753 topline_back(lp);
2754 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002755 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002757 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 return;
2759 }
2760
2761 loff2 = *lp;
2762 if (dir > 0)
2763 botline_forw(lp);
2764 else
2765 topline_back(lp);
2766 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002767 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002768 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002770 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773/*
2774 * Scroll 'scroll' lines up or down.
2775 */
2776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002777halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 long scrolled = 0;
2780 int i;
2781 int n;
2782 int room;
2783
2784 if (Prenum)
2785 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2786 curwin->w_height : Prenum;
2787 n = (curwin->w_p_scr <= curwin->w_height) ?
2788 curwin->w_p_scr : curwin->w_height;
2789
Bram Moolenaard5d37532017-03-27 23:02:07 +02002790 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 validate_botline();
2792 room = curwin->w_empty_rows;
2793#ifdef FEAT_DIFF
2794 room += curwin->w_filler_rows;
2795#endif
2796 if (flag)
2797 {
2798 /*
2799 * scroll the text up
2800 */
2801 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2802 {
2803#ifdef FEAT_DIFF
2804 if (curwin->w_topfill > 0)
2805 {
2806 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002807 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 --curwin->w_topfill;
2809 }
2810 else
2811#endif
2812 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002813 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 n -= i;
2815 if (n < 0 && scrolled > 0)
2816 break;
2817#ifdef FEAT_FOLDING
2818 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2819#endif
2820 ++curwin->w_topline;
2821#ifdef FEAT_DIFF
2822 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2823#endif
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2826 {
2827 ++curwin->w_cursor.lnum;
2828 curwin->w_valid &=
2829 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2833 scrolled += i;
2834
2835 /*
2836 * Correct w_botline for changed w_topline.
2837 * Won't work when there are filler lines.
2838 */
2839#ifdef FEAT_DIFF
2840 if (curwin->w_p_diff)
2841 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2842 else
2843#endif
2844 {
2845 room += i;
2846 do
2847 {
2848 i = plines(curwin->w_botline);
2849 if (i > room)
2850 break;
2851#ifdef FEAT_FOLDING
2852 (void)hasFolding(curwin->w_botline, NULL,
2853 &curwin->w_botline);
2854#endif
2855 ++curwin->w_botline;
2856 room -= i;
2857 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2858 }
2859 }
2860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 /*
2862 * When hit bottom of the file: move cursor down.
2863 */
2864 if (n > 0)
2865 {
2866# ifdef FEAT_FOLDING
2867 if (hasAnyFolding(curwin))
2868 {
2869 while (--n >= 0
2870 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2871 {
2872 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2873 &curwin->w_cursor.lnum);
2874 ++curwin->w_cursor.lnum;
2875 }
2876 }
2877 else
2878# endif
2879 curwin->w_cursor.lnum += n;
2880 check_cursor_lnum();
2881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 else
2884 {
2885 /*
2886 * scroll the text down
2887 */
2888 while (n > 0 && curwin->w_topline > 1)
2889 {
2890#ifdef FEAT_DIFF
2891 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2892 {
2893 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002894 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 ++curwin->w_topfill;
2896 }
2897 else
2898#endif
2899 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002900 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 n -= i;
2902 if (n < 0 && scrolled > 0)
2903 break;
2904 --curwin->w_topline;
2905#ifdef FEAT_FOLDING
2906 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2907#endif
2908#ifdef FEAT_DIFF
2909 curwin->w_topfill = 0;
2910#endif
2911 }
2912 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2913 VALID_BOTLINE|VALID_BOTLINE_AP);
2914 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (curwin->w_cursor.lnum > 1)
2916 {
2917 --curwin->w_cursor.lnum;
2918 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 /*
2923 * When hit top of the file: move cursor up.
2924 */
2925 if (n > 0)
2926 {
2927 if (curwin->w_cursor.lnum <= (linenr_T)n)
2928 curwin->w_cursor.lnum = 1;
2929 else
2930# ifdef FEAT_FOLDING
2931 if (hasAnyFolding(curwin))
2932 {
2933 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2934 {
2935 --curwin->w_cursor.lnum;
2936 (void)hasFolding(curwin->w_cursor.lnum,
2937 &curwin->w_cursor.lnum, NULL);
2938 }
2939 }
2940 else
2941# endif
2942 curwin->w_cursor.lnum -= n;
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 foldAdjustCursor();
2948# endif
2949#ifdef FEAT_DIFF
2950 check_topfill(curwin, !flag);
2951#endif
2952 cursor_correct();
2953 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002954 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002956
Bram Moolenaar860cae12010-06-05 23:22:07 +02002957 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002958do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002959{
2960 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002961 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002962 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002963 colnr_T curswant = curwin->w_curswant;
2964 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002965 win_T *old_curwin = curwin;
2966 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002967 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002968 int old_VIsual_select = VIsual_select;
2969 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002970
2971 /*
2972 * loop through the cursorbound windows
2973 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002974 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002975 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002976 {
2977 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002978 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 if (curwin != old_curwin && curwin->w_p_crb)
2980 {
2981# ifdef FEAT_DIFF
2982 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002983 curwin->w_cursor.lnum =
2984 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002985 else
2986# endif
2987 curwin->w_cursor.lnum = line;
2988 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002989 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002990 curwin->w_curswant = curswant;
2991 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002992
Bram Moolenaar85a20022019-12-21 18:25:54 +01002993 // Make sure the cursor is in a valid position. Temporarily set
2994 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002995 restart_edit_save = restart_edit;
2996 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002997 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01002998
2999 // Avoid a scroll here for the cursor position, 'scrollbind' is
3000 // more important.
3001 if (!curwin->w_p_scb)
3002 validate_cursor();
3003
Bram Moolenaar61452852011-02-01 18:01:11 +01003004 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003005 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003006 if (has_mbyte)
3007 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003008 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003009
Bram Moolenaar85a20022019-12-21 18:25:54 +01003010 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003011 if (!curwin->w_p_scb)
3012 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003013 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003014 }
3015 }
3016
3017 /*
3018 * reset current-window
3019 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003020 VIsual_select = old_VIsual_select;
3021 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003022 curwin = old_curwin;
3023 curbuf = old_curbuf;
3024}