blob: 089ffca39934d995f3e291d16efb246e6442b9e7 [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();
479 curwin->w_curswant = curwin->w_virtcol;
480 curwin->w_set_curswant = FALSE;
481 }
482}
483
484/*
485 * Check if the cursor has moved. Set the w_valid flag accordingly.
486 */
487 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
491 {
492 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100493 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
494 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 wp->w_valid_cursor = wp->w_cursor;
496 wp->w_valid_leftcol = wp->w_leftcol;
497 }
498 else if (wp->w_cursor.col != wp->w_valid_cursor.col
499 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100500 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {
502 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
503 wp->w_valid_cursor.col = wp->w_cursor.col;
504 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507}
508
509/*
510 * Call this function when some window settings have changed, which require
511 * the cursor position, botline and topline to be recomputed and the window to
512 * be redrawn. E.g, when changing the 'wrap' option or folding.
513 */
514 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100515changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 changed_window_setting_win(curwin);
518}
519
520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100521changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 wp->w_lines_valid = 0;
524 changed_line_abv_curs_win(wp);
525 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100526 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529/*
530 * Set wp->w_topline to a certain number.
531 */
532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100533set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200535#ifdef FEAT_DIFF
536 linenr_T prev_topline = wp->w_topline;
537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100540 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
542#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100543 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100545 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
546 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000548 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200550 if (lnum != prev_topline)
551 // Keep the filler lines when the topline didn't change.
552 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100555 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100556 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557}
558
559/*
560 * Call this function when the length of the cursor line (in screen
561 * characters) has changed, and the change is before the cursor.
562 * Need to take care of w_botline separately!
563 */
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
568 |VALID_CHEIGHT|VALID_TOPLINE);
569}
570
571 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100572changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
575 |VALID_CHEIGHT|VALID_TOPLINE);
576}
577
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578/*
579 * Call this function when the length of a line (in screen characters) above
580 * the cursor have changed.
581 * Need to take care of w_botline separately!
582 */
583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100584changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
587 |VALID_CHEIGHT|VALID_TOPLINE);
588}
589
590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100591changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
594 |VALID_CHEIGHT|VALID_TOPLINE);
595}
596
597/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100598 * Display of line has changed for "buf", invalidate cursor position and
599 * w_botline.
600 */
601 void
602changed_line_display_buf(buf_T *buf)
603{
604 win_T *wp;
605
606 FOR_ALL_WINDOWS(wp)
607 if (wp->w_buffer == buf)
608 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
609 |VALID_CROW|VALID_CHEIGHT
610 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
611}
612
613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * Make sure the value of curwin->w_botline is valid.
615 */
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100619 validate_botline_win(curwin);
620}
621
622/*
623 * Make sure the value of wp->w_botline is valid.
624 */
625 void
626validate_botline_win(win_T *wp)
627{
628 if (!(wp->w_valid & VALID_BOTLINE))
629 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630}
631
632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 * Mark curwin->w_botline as invalid (because of some change in the buffer).
634 */
635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100636invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
639}
640
641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100642invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
644 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648approximate_botline_win(
649 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 wp->w_valid &= ~VALID_BOTLINE;
652}
653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654/*
655 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
656 */
657 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100658cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 check_cursor_moved(curwin);
661 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
662 (VALID_WROW|VALID_WCOL));
663}
664
665/*
666 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
667 * w_topline must be valid, you may need to call update_topline() first!
668 */
669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 check_cursor_moved(curwin);
673 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
674 curs_columns(TRUE);
675}
676
677#if defined(FEAT_GUI) || defined(PROTO)
678/*
679 * validate w_cline_row.
680 */
681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 /*
685 * First make sure that w_topline is valid (after moving the cursor).
686 */
687 update_topline();
688 check_cursor_moved(curwin);
689 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100690 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691}
692#endif
693
694/*
695 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200696 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100699curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 linenr_T lnum;
702 int i;
703 int all_invalid;
704 int valid;
705#ifdef FEAT_FOLDING
706 long fold_count;
707#endif
708
Bram Moolenaar85a20022019-12-21 18:25:54 +0100709 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 all_invalid = (!redrawing()
711 || wp->w_lines_valid == 0
712 || wp->w_lines[0].wl_lnum > wp->w_topline);
713 i = 0;
714 wp->w_cline_row = 0;
715 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
716 {
717 valid = FALSE;
718 if (!all_invalid && i < wp->w_lines_valid)
719 {
720 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (wp->w_lines[i].wl_lnum == lnum)
723 {
724#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100725 // Check for newly inserted lines below this row, in which
726 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 if (!wp->w_buffer->b_mod_set
728 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
729 || wp->w_buffer->b_mod_top
730 > wp->w_lines[i].wl_lastlnum + 1)
731#endif
732 valid = TRUE;
733 }
734 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100735 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737 if (valid
738#ifdef FEAT_DIFF
739 && (lnum != wp->w_topline || !wp->w_p_diff)
740#endif
741 )
742 {
743#ifdef FEAT_FOLDING
744 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100745 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (lnum > wp->w_cursor.lnum)
747 break;
748#else
749 ++lnum;
750#endif
751 wp->w_cline_row += wp->w_lines[i].wl_size;
752 }
753 else
754 {
755#ifdef FEAT_FOLDING
756 fold_count = foldedCount(wp, lnum, NULL);
757 if (fold_count)
758 {
759 lnum += fold_count;
760 if (lnum > wp->w_cursor.lnum)
761 break;
762 ++wp->w_cline_row;
763 }
764 else
765#endif
766#ifdef FEAT_DIFF
767 if (lnum == wp->w_topline)
768 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
769 + wp->w_topfill;
770 else
771#endif
772 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
773 }
774 }
775
776 check_cursor_moved(wp);
777 if (!(wp->w_valid & VALID_CHEIGHT))
778 {
779 if (all_invalid
780 || i == wp->w_lines_valid
781 || (i < wp->w_lines_valid
782 && (!wp->w_lines[i].wl_valid
783 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
784 {
785#ifdef FEAT_DIFF
786 if (wp->w_cursor.lnum == wp->w_topline)
787 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
788 TRUE) + wp->w_topfill;
789 else
790#endif
791 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
792#ifdef FEAT_FOLDING
793 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
794 NULL, NULL, TRUE, NULL);
795#endif
796 }
797 else if (i > wp->w_lines_valid)
798 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100799 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_cline_height = 0;
801#ifdef FEAT_FOLDING
802 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
803 NULL, NULL, TRUE, NULL);
804#endif
805 }
806 else
807 {
808 wp->w_cline_height = wp->w_lines[i].wl_size;
809#ifdef FEAT_FOLDING
810 wp->w_cline_folded = wp->w_lines[i].wl_folded;
811#endif
812 }
813 }
814
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100815 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/*
821 * Validate curwin->w_virtcol only.
822 */
823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100824validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
826 validate_virtcol_win(curwin);
827}
828
829/*
830 * Validate wp->w_virtcol only.
831 */
832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 check_cursor_moved(wp);
836 if (!(wp->w_valid & VALID_VIRTCOL))
837 {
838 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000839#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100840 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000841#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100842 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844}
845
846/*
847 * Validate curwin->w_cline_height only.
848 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 check_cursor_moved(curwin);
853 if (!(curwin->w_valid & VALID_CHEIGHT))
854 {
855#ifdef FEAT_DIFF
856 if (curwin->w_cursor.lnum == curwin->w_topline)
857 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
858 + curwin->w_topfill;
859 else
860#endif
861 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
862#ifdef FEAT_FOLDING
863 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
864#endif
865 curwin->w_valid |= VALID_CHEIGHT;
866 }
867}
868
869/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000870 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 */
872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100873validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 colnr_T off;
876 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100877 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 validate_virtcol();
880 if (!(curwin->w_valid & VALID_WCOL))
881 {
882 col = curwin->w_virtcol;
883 off = curwin_col_off();
884 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200885 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000888 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200889 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100890 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200892 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000893 if (col > (int)curwin->w_leftcol)
894 col -= curwin->w_leftcol;
895 else
896 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100900#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100901 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904}
905
906/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 * Compute offset of a window, occupied by absolute or relative line number,
908 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 */
910 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100911win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
Bram Moolenaar64486672010-05-16 15:46:46 +0200913 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#ifdef FEAT_CMDWIN
915 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
916#endif
917#ifdef FEAT_FOLDING
918 + wp->w_p_fdc
919#endif
920#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200921 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922#endif
923 );
924}
925
926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100927curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 return win_col_off(curwin);
930}
931
932/*
933 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200934 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
935 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
937 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100938win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
Bram Moolenaar64486672010-05-16 15:46:46 +0200940 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000941 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 return 0;
943}
944
945 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100946curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 return win_col_off2(curwin);
949}
950
951/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100952 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 * Also updates curwin->w_wrow and curwin->w_cline_row.
954 * Also updates curwin->w_leftcol.
955 */
956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100957curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100958 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959{
960 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100961 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 int off_left, off_right;
963 int n;
964 int p_lines;
965 int width = 0;
966 int textwidth;
967 int new_leftcol;
968 colnr_T startcol;
969 colnr_T endcol;
970 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100971 long so = get_scrolloff_value();
972 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 /*
975 * First make sure that w_topline is valid (after moving the cursor).
976 */
977 update_topline();
978
979 /*
980 * Next make sure that w_cline_row is valid.
981 */
982 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100983 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 /*
986 * Compute the number of virtual columns.
987 */
988#ifdef FEAT_FOLDING
989 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100990 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
992 else
993#endif
994 getvvcol(curwin, &curwin->w_cursor,
995 &startcol, &(curwin->w_virtcol), &endcol);
996
Bram Moolenaar85a20022019-12-21 18:25:54 +0100997 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100999 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 extra = curwin_col_off();
1002 curwin->w_wcol = curwin->w_virtcol + extra;
1003 endcol += extra;
1004
1005 /*
1006 * Now compute w_wrow, counting screen lines from w_cline_row.
1007 */
1008 curwin->w_wrow = curwin->w_cline_row;
1009
Bram Moolenaar02631462017-09-22 15:20:32 +02001010 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (textwidth <= 0)
1012 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001014 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001015 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001016 if (curwin->w_p_wrap)
1017 curwin->w_wrow = curwin->w_height - 1;
1018 else
1019 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001021 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 width = textwidth + curwin_col_off2();
1024
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001026 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001028#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001029 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001030#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001031
Bram Moolenaar85a20022019-12-21 18:25:54 +01001032 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001033 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 curwin->w_wcol -= n * width;
1035 curwin->w_wrow += n;
1036
1037#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001038 // When cursor wraps to first char of next line in Insert
1039 // mode, the 'showbreak' string isn't shown, backup to first
1040 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001041 sbr = get_showbreak_value(curwin);
1042 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001043 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 curwin->w_wcol = 0;
1045#endif
1046 }
1047 }
1048
Bram Moolenaar85a20022019-12-21 18:25:54 +01001049 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1050 // is not folded.
1051 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001052 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#ifdef FEAT_FOLDING
1054 && !curwin->w_cline_folded
1055#endif
1056 )
1057 {
1058 /*
1059 * If Cursor is left of the screen, scroll rightwards.
1060 * If Cursor is right of the screen, scroll leftwards
1061 * If we get closer to the edge than 'sidescrolloff', scroll a little
1062 * extra
1063 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001064 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001065 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001066 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (off_left < 0 || off_right > 0)
1068 {
1069 if (off_left < 0)
1070 diff = -off_left;
1071 else
1072 diff = off_right;
1073
Bram Moolenaar85a20022019-12-21 18:25:54 +01001074 // When far off or not enough room on either side, put cursor in
1075 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1077 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1078 else
1079 {
1080 if (diff < p_ss)
1081 diff = p_ss;
1082 if (off_left < 0)
1083 new_leftcol = curwin->w_leftcol - diff;
1084 else
1085 new_leftcol = curwin->w_leftcol + diff;
1086 }
1087 if (new_leftcol < 0)
1088 new_leftcol = 0;
1089 if (new_leftcol != (int)curwin->w_leftcol)
1090 {
1091 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001092 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001093 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 }
1096 curwin->w_wcol -= curwin->w_leftcol;
1097 }
1098 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1099 curwin->w_wcol -= curwin->w_leftcol;
1100 else
1101 curwin->w_wcol = 0;
1102
1103#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001104 // Skip over filler lines. At the top use w_topfill, there
1105 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (curwin->w_cursor.lnum == curwin->w_topline)
1107 curwin->w_wrow += curwin->w_topfill;
1108 else
1109 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1110#endif
1111
1112 prev_skipcol = curwin->w_skipcol;
1113
1114 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if ((curwin->w_wrow >= curwin->w_height
1117 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001118 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 && (p_lines =
1120#ifdef FEAT_DIFF
1121 plines_win_nofill
1122#else
1123 plines_win
1124#endif
1125 (curwin, curwin->w_cursor.lnum, FALSE))
1126 - 1 >= curwin->w_height))
1127 && curwin->w_height != 0
1128 && curwin->w_cursor.lnum == curwin->w_topline
1129 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001130 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 // Cursor past end of screen. Happens with a single line that does
1133 // not fit on screen. Find a skipcol to show the text around the
1134 // cursor. Avoid scrolling all the time. compute value of "extra":
1135 // 1: Less than 'scrolloff' lines above
1136 // 2: Less than 'scrolloff' lines below
1137 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001139 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001141 // Compute last display line of the buffer line that we want at the
1142 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (p_lines == 0)
1144 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1145 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001146 if (p_lines > curwin->w_wrow + so)
1147 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
1149 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001150 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 extra += 2;
1152
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001153 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 n = curwin->w_virtcol / width;
1157 if (n > curwin->w_height / 2)
1158 n -= curwin->w_height / 2;
1159 else
1160 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001161 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (n > p_lines - curwin->w_height + 1)
1163 n = p_lines - curwin->w_height + 1;
1164 curwin->w_skipcol = n * width;
1165 }
1166 else if (extra == 1)
1167 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001168 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001169 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 + width - 1) / width;
1171 if (extra > 0)
1172 {
1173 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1174 extra = curwin->w_skipcol / width;
1175 curwin->w_skipcol -= extra * width;
1176 }
1177 }
1178 else if (extra == 2)
1179 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001180 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 endcol = (n - curwin->w_height + 1) * width;
1182 while (endcol > curwin->w_virtcol)
1183 endcol -= width;
1184 if (endcol > curwin->w_skipcol)
1185 curwin->w_skipcol = endcol;
1186 }
1187
1188 curwin->w_wrow -= curwin->w_skipcol / width;
1189 if (curwin->w_wrow >= curwin->w_height)
1190 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 extra = curwin->w_wrow - curwin->w_height + 1;
1193 curwin->w_skipcol += extra * width;
1194 curwin->w_wrow -= extra;
1195 }
1196
1197 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1198 if (extra > 0)
1199 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1200 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001201 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203 else
1204 curwin->w_skipcol = 0;
1205 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001206 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001208#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001209 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001210#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001211#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1212 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1213 {
1214 curwin->w_wrow += popup_top_extra(curwin);
1215 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001216 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001217 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001218 else
1219 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001220#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001221
Bram Moolenaar08f23632019-11-16 14:19:33 +01001222 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1223 curwin->w_valid_leftcol = curwin->w_leftcol;
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1226}
1227
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001228#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001229/*
1230 * Compute the screen position of text character at "pos" in window "wp"
1231 * The resulting values are one-based, zero when character is not visible.
1232 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001233 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001234textpos2screenpos(
1235 win_T *wp,
1236 pos_T *pos,
1237 int *rowp, // screen row
1238 int *scolp, // start screen column
1239 int *ccolp, // cursor screen column
1240 int *ecolp) // end screen column
1241{
1242 colnr_T scol = 0, ccol = 0, ecol = 0;
1243 int row = 0;
1244 int rowoff = 0;
1245 colnr_T coloff = 0;
1246
Bram Moolenaar189663b2021-07-21 18:04:56 +02001247 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001248 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001249 colnr_T off;
1250 colnr_T col;
1251 int width;
1252 linenr_T lnum = pos->lnum;
1253#ifdef FEAT_FOLDING
1254 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001255
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001256 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1257#endif
1258 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1259#ifdef FEAT_FOLDING
1260 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001261 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001262 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001263 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001264 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001265 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001266#endif
1267 {
1268 getvcol(wp, pos, &scol, &ccol, &ecol);
1269
1270 // similar to what is done in validate_cursor_col()
1271 col = scol;
1272 off = win_col_off(wp);
1273 col += off;
1274 width = wp->w_width - off + win_col_off2(wp);
1275
1276 // long line wrapping, adjust row
1277 if (wp->w_p_wrap
1278 && col >= (colnr_T)wp->w_width
1279 && width > 0)
1280 {
1281 // use same formula as what is used in curs_columns()
1282 rowoff = ((col - wp->w_width) / width + 1);
1283 col -= rowoff * width;
1284 }
1285 col -= wp->w_leftcol;
1286 if (col >= wp->w_width)
1287 col = -1;
1288 if (col >= 0 && row + rowoff <= wp->w_height)
1289 {
1290 coloff = col - scol + wp->w_wincol + 1;
1291 row += W_WINROW(wp);
1292 }
1293 else
1294 // character is left, right or below of the window
1295 row = rowoff = scol = ccol = ecol = 0;
1296 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001297 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001298 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001299 *scolp = scol + coloff;
1300 *ccolp = ccol + coloff;
1301 *ecolp = ecol + coloff;
1302}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001303#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001304
Bram Moolenaar12034e22019-08-25 22:25:02 +02001305#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001306/*
1307 * "screenpos({winid}, {lnum}, {col})" function
1308 */
1309 void
1310f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1311{
1312 dict_T *dict;
1313 win_T *wp;
1314 pos_T pos;
1315 int row = 0;
1316 int scol = 0, ccol = 0, ecol = 0;
1317
Bram Moolenaar93a10962022-06-16 11:42:09 +01001318 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001319 return;
1320 dict = rettv->vval.v_dict;
1321
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001322 if (in_vim9script()
1323 && (check_for_number_arg(argvars, 0) == FAIL
1324 || check_for_number_arg(argvars, 1) == FAIL
1325 || check_for_number_arg(argvars, 2) == FAIL))
1326 return;
1327
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001328 wp = find_win_by_nr_or_id(&argvars[0]);
1329 if (wp == NULL)
1330 return;
1331
1332 pos.lnum = tv_get_number(&argvars[1]);
1333 pos.col = tv_get_number(&argvars[2]) - 1;
1334 pos.coladd = 0;
1335 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1336
1337 dict_add_number(dict, "row", row);
1338 dict_add_number(dict, "col", scol);
1339 dict_add_number(dict, "curscol", ccol);
1340 dict_add_number(dict, "endcol", ecol);
1341}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001342
1343/*
1344 * "virtcol2col({winid}, {lnum}, {col})" function
1345 */
1346 void
1347f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1348{
1349 win_T *wp;
1350 linenr_T lnum;
1351 int screencol;
1352 int error = FALSE;
1353
1354 rettv->vval.v_number = -1;
1355
1356 if (check_for_number_arg(argvars, 0) == FAIL
1357 || check_for_number_arg(argvars, 1) == FAIL
1358 || check_for_number_arg(argvars, 2) == FAIL)
1359 return;
1360
1361 wp = find_win_by_nr_or_id(&argvars[0]);
1362 if (wp == NULL)
1363 return;
1364
1365 lnum = tv_get_number_chk(&argvars[1], &error);
1366 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1367 return;
1368
1369 screencol = tv_get_number_chk(&argvars[2], &error);
1370 if (error || screencol < 0)
1371 return;
1372
1373 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1374}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001375#endif
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377/*
1378 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001381scrolldown(
1382 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001385 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 int wrow;
1387 int moved = FALSE;
1388
1389#ifdef FEAT_FOLDING
1390 linenr_T first;
1391
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1394#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001395 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 while (line_count-- > 0)
1397 {
1398#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001399 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1400 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
1402 ++curwin->w_topfill;
1403 ++done;
1404 }
1405 else
1406#endif
1407 {
1408 if (curwin->w_topline == 1)
1409 break;
1410 --curwin->w_topline;
1411#ifdef FEAT_DIFF
1412 curwin->w_topfill = 0;
1413#endif
1414#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001415 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (hasFolding(curwin->w_topline, &first, NULL))
1417 {
1418 ++done;
1419 if (!byfold)
1420 line_count -= curwin->w_topline - first - 1;
1421 curwin->w_botline -= curwin->w_topline - first;
1422 curwin->w_topline = first;
1423 }
1424 else
1425#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001426 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001428 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 invalidate_botline();
1430 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 curwin->w_wrow += done; // keep w_wrow updated
1432 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
1434#ifdef FEAT_DIFF
1435 if (curwin->w_cursor.lnum == curwin->w_topline)
1436 curwin->w_cline_row = 0;
1437 check_topfill(curwin, TRUE);
1438#endif
1439
1440 /*
1441 * Compute the row number of the last row of the cursor line
1442 * and move the cursor onto the displayed part of the window.
1443 */
1444 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001445 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
1447 validate_virtcol();
1448 validate_cheight();
1449 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001450 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1453 {
1454#ifdef FEAT_FOLDING
1455 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1456 {
1457 --wrow;
1458 if (first == 1)
1459 curwin->w_cursor.lnum = 1;
1460 else
1461 curwin->w_cursor.lnum = first - 1;
1462 }
1463 else
1464#endif
1465 wrow -= plines(curwin->w_cursor.lnum--);
1466 curwin->w_valid &=
1467 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1468 moved = TRUE;
1469 }
1470 if (moved)
1471 {
1472#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001473 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 foldAdjustCursor();
1475#endif
1476 coladvance(curwin->w_curswant);
1477 }
1478}
1479
1480/*
1481 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001484scrollup(
1485 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1489 linenr_T lnum;
1490
1491 if (
1492# ifdef FEAT_FOLDING
1493 (byfold && hasAnyFolding(curwin))
1494# ifdef FEAT_DIFF
1495 ||
1496# endif
1497# endif
1498# ifdef FEAT_DIFF
1499 curwin->w_p_diff
1500# endif
1501 )
1502 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001503 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 lnum = curwin->w_topline;
1505 while (line_count--)
1506 {
1507# ifdef FEAT_DIFF
1508 if (curwin->w_topfill > 0)
1509 --curwin->w_topfill;
1510 else
1511# endif
1512 {
1513# ifdef FEAT_FOLDING
1514 if (byfold)
1515 (void)hasFolding(lnum, NULL, &lnum);
1516# endif
1517 if (lnum >= curbuf->b_ml.ml_line_count)
1518 break;
1519 ++lnum;
1520# ifdef FEAT_DIFF
1521 curwin->w_topfill = diff_check_fill(curwin, lnum);
1522# endif
1523 }
1524 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001525 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 curwin->w_botline += lnum - curwin->w_topline;
1527 curwin->w_topline = lnum;
1528 }
1529 else
1530#endif
1531 {
1532 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001533 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535
1536 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1537 curwin->w_topline = curbuf->b_ml.ml_line_count;
1538 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1539 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1540
1541#ifdef FEAT_DIFF
1542 check_topfill(curwin, FALSE);
1543#endif
1544
1545#ifdef FEAT_FOLDING
1546 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001547 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1549#endif
1550
1551 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1552 if (curwin->w_cursor.lnum < curwin->w_topline)
1553 {
1554 curwin->w_cursor.lnum = curwin->w_topline;
1555 curwin->w_valid &=
1556 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1557 coladvance(curwin->w_curswant);
1558 }
1559}
1560
1561#ifdef FEAT_DIFF
1562/*
1563 * Don't end up with too many filler lines in the window.
1564 */
1565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001566check_topfill(
1567 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001568 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 int n;
1571
1572 if (wp->w_topfill > 0)
1573 {
1574 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1575 if (wp->w_topfill + n > wp->w_height)
1576 {
1577 if (down && wp->w_topline > 1)
1578 {
1579 --wp->w_topline;
1580 wp->w_topfill = 0;
1581 }
1582 else
1583 {
1584 wp->w_topfill = wp->w_height - n;
1585 if (wp->w_topfill < 0)
1586 wp->w_topfill = 0;
1587 }
1588 }
1589 }
1590}
1591
1592/*
1593 * Use as many filler lines as possible for w_topline. Make sure w_topline
1594 * is still visible.
1595 */
1596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001597max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 int n;
1600
1601 n = plines_nofill(curwin->w_topline);
1602 if (n >= curwin->w_height)
1603 curwin->w_topfill = 0;
1604 else
1605 {
1606 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1607 if (curwin->w_topfill + n > curwin->w_height)
1608 curwin->w_topfill = curwin->w_height - n;
1609 }
1610}
1611#endif
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
1614 * Scroll the screen one line down, but don't do it if it would move the
1615 * cursor off the screen.
1616 */
1617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001618scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 int end_row;
1621#ifdef FEAT_DIFF
1622 int can_fill = (curwin->w_topfill
1623 < diff_check_fill(curwin, curwin->w_topline));
1624#endif
1625
1626 if (curwin->w_topline <= 1
1627#ifdef FEAT_DIFF
1628 && !can_fill
1629#endif
1630 )
1631 return;
1632
Bram Moolenaar85a20022019-12-21 18:25:54 +01001633 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635 /*
1636 * Compute the row number of the last row of the cursor line
1637 * and make sure it doesn't go off the screen. Make sure the cursor
1638 * doesn't go past 'scrolloff' lines from the screen end.
1639 */
1640 end_row = curwin->w_wrow;
1641#ifdef FEAT_DIFF
1642 if (can_fill)
1643 ++end_row;
1644 else
1645 end_row += plines_nofill(curwin->w_topline - 1);
1646#else
1647 end_row += plines(curwin->w_topline - 1);
1648#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001649 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 validate_cheight();
1652 validate_virtcol();
1653 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001654 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001656 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658#ifdef FEAT_DIFF
1659 if (can_fill)
1660 {
1661 ++curwin->w_topfill;
1662 check_topfill(curwin, TRUE);
1663 }
1664 else
1665 {
1666 --curwin->w_topline;
1667 curwin->w_topfill = 0;
1668 }
1669#else
1670 --curwin->w_topline;
1671#endif
1672#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001673 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001675 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1677 }
1678}
1679
1680/*
1681 * Scroll the screen one line up, but don't do it if it would move the cursor
1682 * off the screen.
1683 */
1684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001685scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 int start_row;
1688
1689 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1690#ifdef FEAT_DIFF
1691 && curwin->w_topfill == 0
1692#endif
1693 )
1694 return;
1695
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698 /*
1699 * Compute the row number of the first row of the cursor line
1700 * and make sure it doesn't go off the screen. Make sure the cursor
1701 * doesn't go before 'scrolloff' lines from the screen start.
1702 */
1703#ifdef FEAT_DIFF
1704 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1705 - curwin->w_topfill;
1706#else
1707 start_row = curwin->w_wrow - plines(curwin->w_topline);
1708#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001709 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
1711 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001712 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001714 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
1716#ifdef FEAT_DIFF
1717 if (curwin->w_topfill > 0)
1718 --curwin->w_topfill;
1719 else
1720#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001721 {
1722#ifdef FEAT_FOLDING
1723 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001726 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001727 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1729 }
1730}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732/*
1733 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1734 * a (wrapped) text line. Uses and sets "lp->fill".
1735 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001736 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 */
1738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741#ifdef FEAT_DIFF
1742 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1743 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 ++lp->fill;
1746 lp->height = 1;
1747 }
1748 else
1749#endif
1750 {
1751 --lp->lnum;
1752#ifdef FEAT_DIFF
1753 lp->fill = 0;
1754#endif
1755 if (lp->lnum < 1)
1756 lp->height = MAXCOL;
1757 else
1758#ifdef FEAT_FOLDING
1759 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001760 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 lp->height = 1;
1762 else
1763#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001764 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766}
1767
1768/*
1769 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1770 * a (wrapped) text line. Uses and sets "lp->fill".
1771 * Returns the height of the added line in "lp->height".
1772 * Lines below the last one are incredibly high.
1773 */
1774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777#ifdef FEAT_DIFF
1778 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1779 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001780 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ++lp->fill;
1782 lp->height = 1;
1783 }
1784 else
1785#endif
1786 {
1787 ++lp->lnum;
1788#ifdef FEAT_DIFF
1789 lp->fill = 0;
1790#endif
1791 if (lp->lnum > curbuf->b_ml.ml_line_count)
1792 lp->height = MAXCOL;
1793 else
1794#ifdef FEAT_FOLDING
1795 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001796 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 lp->height = 1;
1798 else
1799#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001800 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802}
1803
1804#ifdef FEAT_DIFF
1805/*
1806 * Switch from including filler lines below lp->lnum to including filler
1807 * lines above loff.lnum + 1. This keeps pointing to the same line.
1808 * When there are no filler lines nothing changes.
1809 */
1810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813 if (lp->fill > 0)
1814 {
1815 ++lp->lnum;
1816 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1817 }
1818}
1819
1820/*
1821 * Switch from including filler lines above lp->lnum to including filler
1822 * lines below loff.lnum - 1. This keeps pointing to the same line.
1823 * When there are no filler lines nothing changes.
1824 */
1825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 if (lp->fill > 0)
1829 {
1830 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1831 --lp->lnum;
1832 }
1833}
1834#endif
1835
1836/*
1837 * Recompute topline to put the cursor at the top of the window.
1838 * Scroll at least "min_scroll" lines.
1839 * If "always" is TRUE, always set topline (for "zt").
1840 */
1841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001842scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
1844 int scrolled = 0;
1845 int extra = 0;
1846 int used;
1847 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001848 linenr_T top; // just above displayed lines
1849 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 linenr_T old_topline = curwin->w_topline;
1851#ifdef FEAT_DIFF
1852 linenr_T old_topfill = curwin->w_topfill;
1853#endif
1854 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001855 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (mouse_dragging > 0)
1858 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
1860 /*
1861 * Decrease topline until:
1862 * - it has become 1
1863 * - (part of) the cursor line is moved off the screen or
1864 * - moved at least 'scrolljump' lines and
1865 * - at least 'scrolloff' lines above and below the cursor
1866 */
1867 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001868 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (curwin->w_cursor.lnum < curwin->w_topline)
1870 scrolled = used;
1871
1872#ifdef FEAT_FOLDING
1873 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1874 {
1875 --top;
1876 ++bot;
1877 }
1878 else
1879#endif
1880 {
1881 top = curwin->w_cursor.lnum - 1;
1882 bot = curwin->w_cursor.lnum + 1;
1883 }
1884 new_topline = top + 1;
1885
1886#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001887 // "used" already contains the number of filler lines above, don't add it
1888 // again.
1889 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001890 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
1892
1893 /*
1894 * Check if the lines from "top" to "bot" fit in the window. If they do,
1895 * set new_topline and advance "top" and "bot" to include more lines.
1896 */
1897 while (top > 0)
1898 {
1899#ifdef FEAT_FOLDING
1900 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001901 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 i = 1;
1903 else
1904#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001905 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 used += i;
1907 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1908 {
1909#ifdef FEAT_FOLDING
1910 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001911 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 ++used;
1913 else
1914#endif
1915 used += plines(bot);
1916 }
1917 if (used > curwin->w_height)
1918 break;
1919 if (top < curwin->w_topline)
1920 scrolled += i;
1921
1922 /*
1923 * If scrolling is needed, scroll at least 'sj' lines.
1924 */
1925 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1926 && extra >= off)
1927 break;
1928
1929 extra += i;
1930 new_topline = top;
1931 --top;
1932 ++bot;
1933 }
1934
1935 /*
1936 * If we don't have enough space, put cursor in the middle.
1937 * This makes sure we get the same position when using "k" and "j"
1938 * in a small window.
1939 */
1940 if (used > curwin->w_height)
1941 scroll_cursor_halfway(FALSE);
1942 else
1943 {
1944 /*
1945 * If "always" is FALSE, only adjust topline to a lower value, higher
1946 * value may happen with wrapping lines
1947 */
1948 if (new_topline < curwin->w_topline || always)
1949 curwin->w_topline = new_topline;
1950 if (curwin->w_topline > curwin->w_cursor.lnum)
1951 curwin->w_topline = curwin->w_cursor.lnum;
1952#ifdef FEAT_DIFF
1953 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1954 if (curwin->w_topfill > 0 && extra > off)
1955 {
1956 curwin->w_topfill -= extra - off;
1957 if (curwin->w_topfill < 0)
1958 curwin->w_topfill = 0;
1959 }
1960 check_topfill(curwin, FALSE);
1961#endif
1962 if (curwin->w_topline != old_topline
1963#ifdef FEAT_DIFF
1964 || curwin->w_topfill != old_topfill
1965#endif
1966 )
1967 curwin->w_valid &=
1968 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1969 curwin->w_valid |= VALID_TOPLINE;
1970 }
1971}
1972
1973/*
1974 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1975 * screen lines for text lines.
1976 */
1977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001978set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980#ifdef FEAT_DIFF
1981 wp->w_filler_rows = 0;
1982#endif
1983 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001984 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 else
1986 {
1987 wp->w_empty_rows = wp->w_height - used;
1988#ifdef FEAT_DIFF
1989 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1990 {
1991 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1992 if (wp->w_empty_rows > wp->w_filler_rows)
1993 wp->w_empty_rows -= wp->w_filler_rows;
1994 else
1995 {
1996 wp->w_filler_rows = wp->w_empty_rows;
1997 wp->w_empty_rows = 0;
1998 }
1999 }
2000#endif
2001 }
2002}
2003
2004/*
2005 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002006 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2008 * This is messy stuff!!!
2009 */
2010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 int used;
2014 int scrolled = 0;
2015 int extra = 0;
2016 int i;
2017 linenr_T line_count;
2018 linenr_T old_topline = curwin->w_topline;
2019 lineoff_T loff;
2020 lineoff_T boff;
2021#ifdef FEAT_DIFF
2022 int old_topfill = curwin->w_topfill;
2023 int fill_below_window;
2024#endif
2025 linenr_T old_botline = curwin->w_botline;
2026 linenr_T old_valid = curwin->w_valid;
2027 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002028 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002029 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 cln = curwin->w_cursor.lnum;
2032 if (set_topbot)
2033 {
2034 used = 0;
2035 curwin->w_botline = cln + 1;
2036#ifdef FEAT_DIFF
2037 loff.fill = 0;
2038#endif
2039 for (curwin->w_topline = curwin->w_botline;
2040 curwin->w_topline > 1;
2041 curwin->w_topline = loff.lnum)
2042 {
2043 loff.lnum = curwin->w_topline;
2044 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002045 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
2047 used += loff.height;
2048#ifdef FEAT_DIFF
2049 curwin->w_topfill = loff.fill;
2050#endif
2051 }
2052 set_empty_rows(curwin, used);
2053 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2054 if (curwin->w_topline != old_topline
2055#ifdef FEAT_DIFF
2056 || curwin->w_topfill != old_topfill
2057#endif
2058 )
2059 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2060 }
2061 else
2062 validate_botline();
2063
Bram Moolenaar85a20022019-12-21 18:25:54 +01002064 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#ifdef FEAT_DIFF
2066 used = plines_nofill(cln);
2067#else
2068 validate_cheight();
2069 used = curwin->w_cline_height;
2070#endif
2071
Bram Moolenaar85a20022019-12-21 18:25:54 +01002072 // If the cursor is below botline, we will at least scroll by the height
2073 // of the cursor line. Correct for empty lines, which are really part of
2074 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (cln >= curwin->w_botline)
2076 {
2077 scrolled = used;
2078 if (cln == curwin->w_botline)
2079 scrolled -= curwin->w_empty_rows;
2080 }
2081
2082 /*
2083 * Stop counting lines to scroll when
2084 * - hitting start of the file
2085 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002086 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 * - lines between botline and cursor have been counted
2088 */
2089#ifdef FEAT_FOLDING
2090 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2091#endif
2092 {
2093 loff.lnum = cln;
2094 boff.lnum = cln;
2095 }
2096#ifdef FEAT_DIFF
2097 loff.fill = 0;
2098 boff.fill = 0;
2099 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2100 - curwin->w_filler_rows;
2101#endif
2102
2103 while (loff.lnum > 1)
2104 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002105 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2106 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002108 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2110 && loff.lnum <= curwin->w_botline
2111#ifdef FEAT_DIFF
2112 && (loff.lnum < curwin->w_botline
2113 || loff.fill >= fill_below_window)
2114#endif
2115 )
2116 break;
2117
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002120 if (loff.height == MAXCOL)
2121 used = MAXCOL;
2122 else
2123 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (used > curwin->w_height)
2125 break;
2126 if (loff.lnum >= curwin->w_botline
2127#ifdef FEAT_DIFF
2128 && (loff.lnum > curwin->w_botline
2129 || loff.fill <= fill_below_window)
2130#endif
2131 )
2132 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 scrolled += loff.height;
2135 if (loff.lnum == curwin->w_botline
2136#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002137 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138#endif
2139 )
2140 scrolled -= curwin->w_empty_rows;
2141 }
2142
2143 if (boff.lnum < curbuf->b_ml.ml_line_count)
2144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002145 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 botline_forw(&boff);
2147 used += boff.height;
2148 if (used > curwin->w_height)
2149 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002150 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2151 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 extra += boff.height;
2154 if (boff.lnum >= curwin->w_botline
2155#ifdef FEAT_DIFF
2156 || (boff.lnum + 1 == curwin->w_botline
2157 && boff.fill > curwin->w_filler_rows)
2158#endif
2159 )
2160 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002161 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 scrolled += boff.height;
2163 if (boff.lnum == curwin->w_botline
2164#ifdef FEAT_DIFF
2165 && boff.fill == 0
2166#endif
2167 )
2168 scrolled -= curwin->w_empty_rows;
2169 }
2170 }
2171 }
2172 }
2173
Bram Moolenaar85a20022019-12-21 18:25:54 +01002174 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (scrolled <= 0)
2176 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002177 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 else if (used > curwin->w_height)
2179 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002180 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 else
2182 {
2183 line_count = 0;
2184#ifdef FEAT_DIFF
2185 boff.fill = curwin->w_topfill;
2186#endif
2187 boff.lnum = curwin->w_topline - 1;
2188 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2189 {
2190 botline_forw(&boff);
2191 i += boff.height;
2192 ++line_count;
2193 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 line_count = 9999;
2196 }
2197
2198 /*
2199 * Scroll up if the cursor is off the bottom of the screen a bit.
2200 * Otherwise put it at 1/2 of the screen.
2201 */
2202 if (line_count >= curwin->w_height && line_count > min_scroll)
2203 scroll_cursor_halfway(FALSE);
2204 else
2205 scrollup(line_count, TRUE);
2206
2207 /*
2208 * If topline didn't change we need to restore w_botline and w_empty_rows
2209 * (we changed them).
2210 * If topline did change, update_screen() will set botline.
2211 */
2212 if (curwin->w_topline == old_topline && set_topbot)
2213 {
2214 curwin->w_botline = old_botline;
2215 curwin->w_empty_rows = old_empty_rows;
2216 curwin->w_valid = old_valid;
2217 }
2218 curwin->w_valid |= VALID_TOPLINE;
2219}
2220
2221/*
2222 * Recompute topline to put the cursor halfway the window
2223 * If "atend" is TRUE, also put it halfway at the end of the file.
2224 */
2225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002226scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 int above = 0;
2229 linenr_T topline;
2230#ifdef FEAT_DIFF
2231 int topfill = 0;
2232#endif
2233 int below = 0;
2234 int used;
2235 lineoff_T loff;
2236 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002237#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002238 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002241#ifdef FEAT_PROP_POPUP
2242 // if the width changed this needs to be updated first
2243 may_update_popup_position();
2244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2246#ifdef FEAT_FOLDING
2247 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2248#endif
2249#ifdef FEAT_DIFF
2250 used = plines_nofill(loff.lnum);
2251 loff.fill = 0;
2252 boff.fill = 0;
2253#else
2254 used = plines(loff.lnum);
2255#endif
2256 topline = loff.lnum;
2257 while (topline > 1)
2258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002259 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 if (boff.lnum < curbuf->b_ml.ml_line_count)
2262 {
2263 botline_forw(&boff);
2264 used += boff.height;
2265 if (used > curwin->w_height)
2266 break;
2267 below += boff.height;
2268 }
2269 else
2270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002271 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (atend)
2273 ++used;
2274 }
2275 }
2276
Bram Moolenaar85a20022019-12-21 18:25:54 +01002277 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002280 if (loff.height == MAXCOL)
2281 used = MAXCOL;
2282 else
2283 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (used > curwin->w_height)
2285 break;
2286 above += loff.height;
2287 topline = loff.lnum;
2288#ifdef FEAT_DIFF
2289 topfill = loff.fill;
2290#endif
2291 }
2292 }
2293#ifdef FEAT_FOLDING
2294 if (!hasFolding(topline, &curwin->w_topline, NULL))
2295#endif
2296 curwin->w_topline = topline;
2297#ifdef FEAT_DIFF
2298 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002299 if (old_topline > curwin->w_topline + curwin->w_height)
2300 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 check_topfill(curwin, FALSE);
2302#endif
2303 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2304 curwin->w_valid |= VALID_TOPLINE;
2305}
2306
2307/*
2308 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002309 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 * If not possible, put it at the same position as scroll_cursor_halfway().
2311 * When called topline must be valid!
2312 */
2313 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002314cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002316 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 linenr_T botline;
2320 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002321 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002323 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 /*
2326 * How many lines we would like to have above/below the cursor depends on
2327 * whether the first/last line of the file is on screen.
2328 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002329 above_wanted = so;
2330 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002331 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 above_wanted = mouse_dragging - 1;
2334 below_wanted = mouse_dragging - 1;
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (curwin->w_topline == 1)
2337 {
2338 above_wanted = 0;
2339 max_off = curwin->w_height / 2;
2340 if (below_wanted > max_off)
2341 below_wanted = max_off;
2342 }
2343 validate_botline();
2344 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002345 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 below_wanted = 0;
2348 max_off = (curwin->w_height - 1) / 2;
2349 if (above_wanted > max_off)
2350 above_wanted = max_off;
2351 }
2352
2353 /*
2354 * If there are sufficient file-lines above and below the cursor, we can
2355 * return now.
2356 */
2357 cln = curwin->w_cursor.lnum;
2358 if (cln >= curwin->w_topline + above_wanted
2359 && cln < curwin->w_botline - below_wanted
2360#ifdef FEAT_FOLDING
2361 && !hasAnyFolding(curwin)
2362#endif
2363 )
2364 return;
2365
2366 /*
2367 * Narrow down the area where the cursor can be put by taking lines from
2368 * the top and the bottom until:
2369 * - the desired context lines are found
2370 * - the lines from the top is past the lines from the bottom
2371 */
2372 topline = curwin->w_topline;
2373 botline = curwin->w_botline - 1;
2374#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002375 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 above = curwin->w_topfill;
2377 below = curwin->w_filler_rows;
2378#endif
2379 while ((above < above_wanted || below < below_wanted) && topline < botline)
2380 {
2381 if (below < below_wanted && (below <= above || above >= above_wanted))
2382 {
2383#ifdef FEAT_FOLDING
2384 if (hasFolding(botline, &botline, NULL))
2385 ++below;
2386 else
2387#endif
2388 below += plines(botline);
2389 --botline;
2390 }
2391 if (above < above_wanted && (above < below || below >= below_wanted))
2392 {
2393#ifdef FEAT_FOLDING
2394 if (hasFolding(topline, NULL, &topline))
2395 ++above;
2396 else
2397#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002398 above += PLINES_NOFILL(topline);
2399#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002400 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (topline < botline)
2402 above += diff_check_fill(curwin, topline + 1);
2403#endif
2404 ++topline;
2405 }
2406 }
2407 if (topline == botline || botline == 0)
2408 curwin->w_cursor.lnum = topline;
2409 else if (topline > botline)
2410 curwin->w_cursor.lnum = botline;
2411 else
2412 {
2413 if (cln < topline && curwin->w_topline > 1)
2414 {
2415 curwin->w_cursor.lnum = topline;
2416 curwin->w_valid &=
2417 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2418 }
2419 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2420 {
2421 curwin->w_cursor.lnum = botline;
2422 curwin->w_valid &=
2423 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2424 }
2425 }
2426 curwin->w_valid |= VALID_TOPLINE;
2427}
2428
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002429static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431/*
2432 * move screen 'count' pages up or down and update screen
2433 *
2434 * return FAIL for failure, OK otherwise
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long n;
2440 int retval = OK;
2441 lineoff_T loff;
2442 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002443 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar85a20022019-12-21 18:25:54 +01002445 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
2447 beep_flush();
2448 return FAIL;
2449 }
2450
2451 for ( ; count > 0; --count)
2452 {
2453 validate_botline();
2454 /*
2455 * It's an error to move a page up when the first line is already on
2456 * the screen. It's an error to move a page down when the last line
2457 * is on the screen and the topline is 'scrolloff' lines from the
2458 * last line.
2459 */
2460 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002461 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2463 : (curwin->w_topline == 1
2464#ifdef FEAT_DIFF
2465 && curwin->w_topfill ==
2466 diff_check_fill(curwin, curwin->w_topline)
2467#endif
2468 ))
2469 {
2470 beep_flush();
2471 retval = FAIL;
2472 break;
2473 }
2474
2475#ifdef FEAT_DIFF
2476 loff.fill = 0;
2477#endif
2478 if (dir == FORWARD)
2479 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002480 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002483 if (p_window <= 2)
2484 ++curwin->w_topline;
2485 else
2486 curwin->w_topline += p_window - 2;
2487 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2488 curwin->w_topline = curbuf->b_ml.ml_line_count;
2489 curwin->w_cursor.lnum = curwin->w_topline;
2490 }
2491 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2492 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002493 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 curwin->w_topline = curbuf->b_ml.ml_line_count;
2495#ifdef FEAT_DIFF
2496 curwin->w_topfill = 0;
2497#endif
2498 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2499 }
2500 else
2501 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002502 // For the overlap, start with the line just below the window
2503 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 loff.lnum = curwin->w_botline;
2505#ifdef FEAT_DIFF
2506 loff.fill = diff_check_fill(curwin, loff.lnum)
2507 - curwin->w_filler_rows;
2508#endif
2509 get_scroll_overlap(&loff, -1);
2510 curwin->w_topline = loff.lnum;
2511#ifdef FEAT_DIFF
2512 curwin->w_topfill = loff.fill;
2513 check_topfill(curwin, FALSE);
2514#endif
2515 curwin->w_cursor.lnum = curwin->w_topline;
2516 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2517 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2518 }
2519 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002520 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522#ifdef FEAT_DIFF
2523 if (curwin->w_topline == 1)
2524 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 max_topfill();
2527 continue;
2528 }
2529#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002530 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002531 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002532 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002533 if (p_window <= 2)
2534 --curwin->w_topline;
2535 else
2536 curwin->w_topline -= p_window - 2;
2537 if (curwin->w_topline < 1)
2538 curwin->w_topline = 1;
2539 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2540 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2541 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2542 continue;
2543 }
2544
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 // Find the line at the top of the window that is going to be the
2546 // line at the bottom of the window. Make sure this results in
2547 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 loff.lnum = curwin->w_topline - 1;
2549#ifdef FEAT_DIFF
2550 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2551 - curwin->w_topfill;
2552#endif
2553 get_scroll_overlap(&loff, 1);
2554
2555 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2556 {
2557 loff.lnum = curbuf->b_ml.ml_line_count;
2558#ifdef FEAT_DIFF
2559 loff.fill = 0;
2560 }
2561 else
2562 {
2563 botline_topline(&loff);
2564#endif
2565 }
2566 curwin->w_cursor.lnum = loff.lnum;
2567
Bram Moolenaar85a20022019-12-21 18:25:54 +01002568 // Find the line just above the new topline to get the right line
2569 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 n = 0;
2571 while (n <= curwin->w_height && loff.lnum >= 1)
2572 {
2573 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002574 if (loff.height == MAXCOL)
2575 n = MAXCOL;
2576 else
2577 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002579 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 curwin->w_topline = 1;
2582#ifdef FEAT_DIFF
2583 max_topfill();
2584#endif
2585 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2586 }
2587 else
2588 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002589 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#ifdef FEAT_DIFF
2591 topline_botline(&loff);
2592#endif
2593 botline_forw(&loff);
2594 botline_forw(&loff);
2595#ifdef FEAT_DIFF
2596 botline_topline(&loff);
2597#endif
2598#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002599 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2601#endif
2602
Bram Moolenaar85a20022019-12-21 18:25:54 +01002603 // Always scroll at least one line. Avoid getting stuck on
2604 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (loff.lnum >= curwin->w_topline
2606#ifdef FEAT_DIFF
2607 && (loff.lnum > curwin->w_topline
2608 || loff.fill >= curwin->w_topfill)
2609#endif
2610 )
2611 {
2612#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002613 // First try using the maximum number of filler lines. If
2614 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 loff.fill = curwin->w_topfill;
2616 if (curwin->w_topfill < diff_check_fill(curwin,
2617 curwin->w_topline))
2618 max_topfill();
2619 if (curwin->w_topfill == loff.fill)
2620#endif
2621 {
2622 --curwin->w_topline;
2623#ifdef FEAT_DIFF
2624 curwin->w_topfill = 0;
2625#endif
2626 }
2627 comp_botline(curwin);
2628 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002629 curwin->w_valid &=
2630 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 else
2633 {
2634 curwin->w_topline = loff.lnum;
2635#ifdef FEAT_DIFF
2636 curwin->w_topfill = loff.fill;
2637 check_topfill(curwin, FALSE);
2638#endif
2639 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2640 }
2641 }
2642 }
2643 }
2644#ifdef FEAT_FOLDING
2645 foldAdjustCursor();
2646#endif
2647 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002648 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002649 if (retval == OK)
2650 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2652
Bram Moolenaar907dad72018-07-10 15:07:15 +02002653 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002655 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2656 // But make sure we scroll at least one line (happens with mix of long
2657 // wrapping lines and non-wrapping line).
2658 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002660 scroll_cursor_top(1, FALSE);
2661 if (curwin->w_topline <= old_topline
2662 && old_topline < curbuf->b_ml.ml_line_count)
2663 {
2664 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002666 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2667#endif
2668 }
2669 }
2670#ifdef FEAT_FOLDING
2671 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002676 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return retval;
2678}
2679
2680/*
2681 * Decide how much overlap to use for page-up or page-down scrolling.
2682 * This is symmetric, so that doing both keeps the same lines displayed.
2683 * Three lines are examined:
2684 *
2685 * before CTRL-F after CTRL-F / before CTRL-B
2686 * etc. l1
2687 * l1 last but one line ------------
2688 * l2 last text line l2 top text line
2689 * ------------- l3 second text line
2690 * l3 etc.
2691 */
2692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002693get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 int h1, h2, h3, h4;
2696 int min_height = curwin->w_height - 2;
2697 lineoff_T loff0, loff1, loff2;
2698
2699#ifdef FEAT_DIFF
2700 if (lp->fill > 0)
2701 lp->height = 1;
2702 else
2703 lp->height = plines_nofill(lp->lnum);
2704#else
2705 lp->height = plines(lp->lnum);
2706#endif
2707 h1 = lp->height;
2708 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002709 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 loff0 = *lp;
2712 if (dir > 0)
2713 botline_forw(lp);
2714 else
2715 topline_back(lp);
2716 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002717 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002719 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return;
2721 }
2722
2723 loff1 = *lp;
2724 if (dir > 0)
2725 botline_forw(lp);
2726 else
2727 topline_back(lp);
2728 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002729 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 return;
2733 }
2734
2735 loff2 = *lp;
2736 if (dir > 0)
2737 botline_forw(lp);
2738 else
2739 topline_back(lp);
2740 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002741 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002742 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002744 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745}
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747/*
2748 * Scroll 'scroll' lines up or down.
2749 */
2750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002751halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 long scrolled = 0;
2754 int i;
2755 int n;
2756 int room;
2757
2758 if (Prenum)
2759 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2760 curwin->w_height : Prenum;
2761 n = (curwin->w_p_scr <= curwin->w_height) ?
2762 curwin->w_p_scr : curwin->w_height;
2763
Bram Moolenaard5d37532017-03-27 23:02:07 +02002764 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 validate_botline();
2766 room = curwin->w_empty_rows;
2767#ifdef FEAT_DIFF
2768 room += curwin->w_filler_rows;
2769#endif
2770 if (flag)
2771 {
2772 /*
2773 * scroll the text up
2774 */
2775 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2776 {
2777#ifdef FEAT_DIFF
2778 if (curwin->w_topfill > 0)
2779 {
2780 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002781 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 --curwin->w_topfill;
2783 }
2784 else
2785#endif
2786 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002787 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 n -= i;
2789 if (n < 0 && scrolled > 0)
2790 break;
2791#ifdef FEAT_FOLDING
2792 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2793#endif
2794 ++curwin->w_topline;
2795#ifdef FEAT_DIFF
2796 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2797#endif
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2800 {
2801 ++curwin->w_cursor.lnum;
2802 curwin->w_valid &=
2803 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2807 scrolled += i;
2808
2809 /*
2810 * Correct w_botline for changed w_topline.
2811 * Won't work when there are filler lines.
2812 */
2813#ifdef FEAT_DIFF
2814 if (curwin->w_p_diff)
2815 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2816 else
2817#endif
2818 {
2819 room += i;
2820 do
2821 {
2822 i = plines(curwin->w_botline);
2823 if (i > room)
2824 break;
2825#ifdef FEAT_FOLDING
2826 (void)hasFolding(curwin->w_botline, NULL,
2827 &curwin->w_botline);
2828#endif
2829 ++curwin->w_botline;
2830 room -= i;
2831 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2832 }
2833 }
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /*
2836 * When hit bottom of the file: move cursor down.
2837 */
2838 if (n > 0)
2839 {
2840# ifdef FEAT_FOLDING
2841 if (hasAnyFolding(curwin))
2842 {
2843 while (--n >= 0
2844 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2845 {
2846 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2847 &curwin->w_cursor.lnum);
2848 ++curwin->w_cursor.lnum;
2849 }
2850 }
2851 else
2852# endif
2853 curwin->w_cursor.lnum += n;
2854 check_cursor_lnum();
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 else
2858 {
2859 /*
2860 * scroll the text down
2861 */
2862 while (n > 0 && curwin->w_topline > 1)
2863 {
2864#ifdef FEAT_DIFF
2865 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2866 {
2867 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002868 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 ++curwin->w_topfill;
2870 }
2871 else
2872#endif
2873 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002874 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 n -= i;
2876 if (n < 0 && scrolled > 0)
2877 break;
2878 --curwin->w_topline;
2879#ifdef FEAT_FOLDING
2880 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2881#endif
2882#ifdef FEAT_DIFF
2883 curwin->w_topfill = 0;
2884#endif
2885 }
2886 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2887 VALID_BOTLINE|VALID_BOTLINE_AP);
2888 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (curwin->w_cursor.lnum > 1)
2890 {
2891 --curwin->w_cursor.lnum;
2892 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 /*
2897 * When hit top of the file: move cursor up.
2898 */
2899 if (n > 0)
2900 {
2901 if (curwin->w_cursor.lnum <= (linenr_T)n)
2902 curwin->w_cursor.lnum = 1;
2903 else
2904# ifdef FEAT_FOLDING
2905 if (hasAnyFolding(curwin))
2906 {
2907 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2908 {
2909 --curwin->w_cursor.lnum;
2910 (void)hasFolding(curwin->w_cursor.lnum,
2911 &curwin->w_cursor.lnum, NULL);
2912 }
2913 }
2914 else
2915# endif
2916 curwin->w_cursor.lnum -= n;
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002920 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 foldAdjustCursor();
2922# endif
2923#ifdef FEAT_DIFF
2924 check_topfill(curwin, !flag);
2925#endif
2926 cursor_correct();
2927 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002928 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002930
Bram Moolenaar860cae12010-06-05 23:22:07 +02002931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002933{
2934 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002935 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002936 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002937 colnr_T curswant = curwin->w_curswant;
2938 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002939 win_T *old_curwin = curwin;
2940 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002941 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002942 int old_VIsual_select = VIsual_select;
2943 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002944
2945 /*
2946 * loop through the cursorbound windows
2947 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002948 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002949 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002950 {
2951 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002952 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002953 if (curwin != old_curwin && curwin->w_p_crb)
2954 {
2955# ifdef FEAT_DIFF
2956 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002957 curwin->w_cursor.lnum =
2958 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002959 else
2960# endif
2961 curwin->w_cursor.lnum = line;
2962 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002963 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002964 curwin->w_curswant = curswant;
2965 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966
Bram Moolenaar85a20022019-12-21 18:25:54 +01002967 // Make sure the cursor is in a valid position. Temporarily set
2968 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002969 restart_edit_save = restart_edit;
2970 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002971 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01002972
2973 // Avoid a scroll here for the cursor position, 'scrollbind' is
2974 // more important.
2975 if (!curwin->w_p_scb)
2976 validate_cursor();
2977
Bram Moolenaar61452852011-02-01 18:01:11 +01002978 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002979 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002980 if (has_mbyte)
2981 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002982 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002983
Bram Moolenaar85a20022019-12-21 18:25:54 +01002984 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002985 if (!curwin->w_p_scb)
2986 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002987 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002988 }
2989 }
2990
2991 /*
2992 * reset current-window
2993 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002994 VIsual_select = old_VIsual_select;
2995 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002996 curwin = old_curwin;
2997 curbuf = old_curbuf;
2998}