blob: ce06dc3394689ea9e4e7e660b10d187381337813 [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/*
zeertzjq6235a102023-08-19 14:12:42 +020039 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010040 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
zeertzjq6235a102023-08-19 14:12:42 +020042adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010043{
44 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020045 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010046
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047 int width = wp->w_width - win_col_off(wp);
48 if (wp->w_skipcol >= width)
zeertzjq6235a102023-08-19 14:12:42 +020049 return (wp->w_skipcol - width) / (width + win_col_off2(wp)) + 1;
50
51 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010052}
53
54/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010055 * Return how many lines "lnum" will take on the screen, taking into account
56 * whether it is the first line, whether w_skipcol is non-zero and limiting to
57 * the window height.
58 */
59 static int
60plines_correct_topline(win_T *wp, linenr_T lnum)
61{
62 int n;
63#ifdef FEAT_DIFF
64 if (lnum == wp->w_topline)
65 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
66 else
67#endif
68 n = plines_win(wp, lnum, FALSE);
69 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020070 n -= adjust_plines_for_skipcol(wp);
Bram Moolenaard5337ef2022-10-20 20:15:47 +010071 if (n > wp->w_height)
72 n = wp->w_height;
73 return n;
74}
75
76/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 * Compute wp->w_botline for the current wp->w_topline. Can be called after
78 * wp->w_topline changed.
79 */
80 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010081comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000082{
83 int n;
84 linenr_T lnum;
85 int done;
86#ifdef FEAT_FOLDING
87 linenr_T last;
88 int folded;
89#endif
90
91 /*
92 * If w_cline_row is valid, start there.
93 * Otherwise have to start at w_topline.
94 */
95 check_cursor_moved(wp);
96 if (wp->w_valid & VALID_CROW)
97 {
98 lnum = wp->w_cursor.lnum;
99 done = wp->w_cline_row;
100 }
101 else
102 {
103 lnum = wp->w_topline;
104 done = 0;
105 }
106
107 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
108 {
109#ifdef FEAT_FOLDING
110 last = lnum;
111 folded = FALSE;
112 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
113 {
114 n = 1;
115 folded = TRUE;
116 }
117 else
118#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100119 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100120 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 if (
123#ifdef FEAT_FOLDING
124 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
125#else
126 lnum == wp->w_cursor.lnum
127#endif
128 )
129 {
130 wp->w_cline_row = done;
131 wp->w_cline_height = n;
132#ifdef FEAT_FOLDING
133 wp->w_cline_folded = folded;
134#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100135 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
137 }
138 if (done + n > wp->w_height)
139 break;
140 done += n;
141#ifdef FEAT_FOLDING
142 lnum = last;
143#endif
144 }
145
Bram Moolenaar85a20022019-12-21 18:25:54 +0100146 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 wp->w_botline = lnum;
148 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
149
150 set_empty_rows(wp, done);
151}
152
153/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100154 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
155 * set.
156 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100157 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100158redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100159{
160 if ((wp->w_p_rnu
161#ifdef FEAT_SYN_HL
162 || wp->w_p_cul
163#endif
164 )
165 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200166 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200167 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000168 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100169 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200170 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100171}
172
zeertzjq3e559cd2022-03-27 19:26:55 +0100173#ifdef FEAT_SYN_HL
174/*
175 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
176 * contains "screenline".
177 */
178 static void
179redraw_for_cursorcolumn(win_T *wp)
180{
181 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
182 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100183 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100184 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 redraw_win_later(wp, UPD_SOME_VALID);
186 // When 'cursorlineopt' contains "screenline" need to redraw with
187 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100188 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100189 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 }
191}
192#endif
193
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100194/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100195 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
196 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000197 * Parameter "extra2" should be the padding on the 2nd line, not the first
198 * line.
199 * Returns the number of columns of overlap with buffer text, excluding the
200 * extra padding on the ledge.
201 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100202 int
203sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000204{
205#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100206 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100208 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209 return 0;
210#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100211 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
212 if (wp->w_p_list && wp->w_lcs_chars.prec)
213 return 1;
214
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000215 return extra2 > 3 ? 0 : 3 - extra2;
216}
217
218/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000219 * Calculates the skipcol offset for window "wp" given how many
220 * physical lines we want to scroll down.
221 */
222 static int
223skipcol_from_plines(win_T *wp, int plines_off)
224{
225 int width1 = wp->w_width - win_col_off(wp);
226
227 int skipcol = 0;
228 if (plines_off > 0)
229 skipcol += width1;
230 if (plines_off > 1)
231 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
232 return skipcol;
233}
234
235/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100236 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000237 */
238 static void
239reset_skipcol(void)
240{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000241 if (curwin->w_skipcol == 0)
242 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000243
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000244 curwin->w_skipcol = 0;
245
246 // Should use the least expensive way that displays all that changed.
247 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
248 // enough when the top line gets another screen line.
249 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000250}
251
252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 * Update curwin->w_topline and redraw if necessary.
254 * Used to update the screen before printing a message.
255 */
256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100257update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 update_topline();
260 if (must_redraw)
261 update_screen(0);
262}
263
264/*
265 * Update curwin->w_topline to move the cursor onto the screen.
266 */
267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100268update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269{
270 long line_count;
271 int halfheight;
272 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifdef FEAT_FOLDING
274 linenr_T lnum;
275#endif
276 int check_topline = FALSE;
277 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100278 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100279 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100281 // Cursor is updated instead when this is TRUE for 'splitkeep'.
282 if (skip_update_topline)
283 return;
284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // If there is no valid screen and when the window height is zero just use
286 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200287 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200288 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100289 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200290 curwin->w_topline = curwin->w_cursor.lnum;
291 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200293 return;
294 }
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 check_cursor_moved(curwin);
297 if (curwin->w_valid & VALID_TOPLINE)
298 return;
299
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000301 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100302 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100304 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100306 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
308
309 /*
310 * If the buffer is empty, always set topline to 1.
311 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {
314 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100315 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 curwin->w_botline = 2;
318 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321
322 /*
323 * If the cursor is above or near the top of the window, scroll the window
324 * to show the line the cursor is in, with 'scrolloff' context.
325 */
326 else
327 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100328 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // If the cursor is above topline, scrolling is always needed.
331 // If the cursor is far below topline and there is no folding,
332 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (curwin->w_cursor.lnum < curwin->w_topline)
334 check_topline = TRUE;
335 else if (check_top_offset())
336 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100337 else if (curwin->w_skipcol > 0
338 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100339 {
340 colnr_T vcol;
341
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000342 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100343 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100344 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100345 int overlap = sms_marker_overlap(curwin, curwin_col_off()
346 - curwin_col_off2());
347 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100348 check_topline = TRUE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
354 curwin->w_topline))
355 check_topline = TRUE;
356#endif
357
358 if (check_topline)
359 {
360 halfheight = curwin->w_height / 2 - 1;
361 if (halfheight < 2)
362 halfheight = 2;
363
364#ifdef FEAT_FOLDING
365 if (hasAnyFolding(curwin))
366 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100367 // Count the number of logical lines between the cursor and
368 // topline + scrolloff (approximation of how much will be
369 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 n = 0;
371 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {
374 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100375 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
377 break;
378 (void)hasFolding(lnum, NULL, &lnum);
379 }
380 }
381 else
382#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100383 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // If we weren't very close to begin with, we scroll to put the
386 // cursor in the middle of the window. Otherwise put the cursor
387 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000389 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 else
391 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 check_botline = TRUE;
394 }
395 }
396
397 else
398 {
399#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100400 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
402#endif
403 check_botline = TRUE;
404 }
405 }
406
407 /*
408 * If the cursor is below the bottom of the window, scroll the window
409 * to put the cursor on the window.
410 * When w_botline is invalid, recompute it first, to avoid a redraw later.
411 * If w_botline was approximated, we might need a redraw later in a few
412 * cases, but we don't want to spend (a lot of) time recomputing w_botline
413 * for every small change.
414 */
415 if (check_botline)
416 {
417 if (!(curwin->w_valid & VALID_BOTLINE_AP))
418 validate_botline();
419
420 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
421 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000422 if (curwin->w_cursor.lnum < curwin->w_botline)
423 {
424 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100425 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef FEAT_FOLDING
427 || hasAnyFolding(curwin)
428#endif
429 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 lineoff_T loff;
432
Bram Moolenaar85a20022019-12-21 18:25:54 +0100433 // Cursor is (a few lines) above botline, check if there are
434 // 'scrolloff' window lines below the cursor. If not, need to
435 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 n = curwin->w_empty_rows;
437 loff.lnum = curwin->w_cursor.lnum;
438#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100439 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
441#endif
442#ifdef FEAT_DIFF
443 loff.fill = 0;
444 n += curwin->w_filler_rows;
445#endif
446 loff.height = 0;
447 while (loff.lnum < curwin->w_botline
448#ifdef FEAT_DIFF
449 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
450#endif
451 )
452 {
453 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100454 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 break;
456 botline_forw(&loff);
457 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100459 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000461 }
462 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100463 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000464 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466 if (check_botline)
467 {
468#ifdef FEAT_FOLDING
469 if (hasAnyFolding(curwin))
470 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100471 // Count the number of logical lines between the cursor and
472 // botline - scrolloff (approximation of how much will be
473 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 line_count = 0;
475 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100476 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
478 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100479 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (lnum <= 0 || line_count > curwin->w_height + 1)
481 break;
482 (void)hasFolding(lnum, &lnum, NULL);
483 }
484 }
485 else
486#endif
487 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100488 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000490 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000492 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 }
494 }
495 }
496 curwin->w_valid |= VALID_TOPLINE;
497
498 /*
499 * Need to redraw when topline changed.
500 */
501 if (curwin->w_topline != old_topline
502#ifdef FEAT_DIFF
503 || curwin->w_topfill != old_topfill
504#endif
505 )
506 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100507 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000508 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100509
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100510 // When 'smoothscroll' is not set, should reset w_skipcol.
511 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100512 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100513 else if (curwin->w_skipcol != 0)
514 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000515
Bram Moolenaar85a20022019-12-21 18:25:54 +0100516 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (curwin->w_cursor.lnum == curwin->w_topline)
518 validate_cursor();
519 }
520
Bram Moolenaar375e3392019-01-31 18:26:10 +0100521 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
524/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000525 * Return the scrolljump value to use for the current window.
526 * When 'scrolljump' is positive use it as-is.
527 * When 'scrolljump' is negative use it as a percentage of the window height.
528 */
529 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100530scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000531{
532 if (p_sj >= 0)
533 return (int)p_sj;
534 return (curwin->w_height * -p_sj) / 100;
535}
536
537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
539 * current window.
540 */
541 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 lineoff_T loff;
545 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100546 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar375e3392019-01-31 18:26:10 +0100548 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#ifdef FEAT_FOLDING
550 || hasAnyFolding(curwin)
551#endif
552 )
553 {
554 loff.lnum = curwin->w_cursor.lnum;
555#ifdef FEAT_DIFF
556 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#else
559 n = 0;
560#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100561 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100562 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
564 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100565 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (loff.lnum < curwin->w_topline
567#ifdef FEAT_DIFF
568 || (loff.lnum == curwin->w_topline && loff.fill > 0)
569#endif
570 )
571 break;
572 n += loff.height;
573 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100574 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 return TRUE;
576 }
577 return FALSE;
578}
579
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100580/*
581 * Update w_curswant.
582 */
583 void
584update_curswant_force(void)
585{
586 validate_virtcol();
587 curwin->w_curswant = curwin->w_virtcol
588#ifdef FEAT_PROP_POPUP
589 - curwin->w_virtcol_first_char
590#endif
591 ;
592 curwin->w_set_curswant = FALSE;
593}
594
595/*
596 * Update w_curswant if w_set_curswant is set.
597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100602 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603}
604
605/*
606 * Check if the cursor has moved. Set the w_valid flag accordingly.
607 */
608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
612 {
613 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100614 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
615 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 wp->w_valid_cursor = wp->w_cursor;
617 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100618 wp->w_valid_skipcol = wp->w_skipcol;
619 }
620 else if (wp->w_skipcol != wp->w_valid_skipcol)
621 {
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
623 |VALID_CHEIGHT|VALID_CROW
624 |VALID_BOTLINE|VALID_BOTLINE_AP);
625 wp->w_valid_cursor = wp->w_cursor;
626 wp->w_valid_leftcol = wp->w_leftcol;
627 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 else if (wp->w_cursor.col != wp->w_valid_cursor.col
630 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100631 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
634 wp->w_valid_cursor.col = wp->w_cursor.col;
635 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638}
639
640/*
641 * Call this function when some window settings have changed, which require
642 * the cursor position, botline and topline to be recomputed and the window to
643 * be redrawn. E.g, when changing the 'wrap' option or folding.
644 */
645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 changed_window_setting_win(curwin);
649}
650
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 wp->w_lines_valid = 0;
655 changed_line_abv_curs_win(wp);
656 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100657 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658}
659
Dominique Pellee764d1b2023-03-12 21:20:59 +0000660#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000662 * Call changed_window_setting_win() for every window containing "buf".
663 */
664 void
665changed_window_setting_buf(buf_T *buf)
666{
667 tabpage_T *tp;
668 win_T *wp;
669
670 FOR_ALL_TAB_WINDOWS(tp, wp)
671 if (wp->w_buffer == buf)
672 changed_window_setting_win(wp);
673}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000674#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Set wp->w_topline to a certain number.
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200682#ifdef FEAT_DIFF
683 linenr_T prev_topline = wp->w_topline;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100687 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
689#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100692 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
693 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000695 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200697 if (lnum != prev_topline)
698 // Keep the filler lines when the topline didn't change.
699 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
701 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100703 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704}
705
706/*
707 * Call this function when the length of the cursor line (in screen
708 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100709 * If the line length changed the number of screen lines might change,
710 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 * Need to take care of w_botline separately!
712 */
713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
Bram Moolenaar85090142023-06-01 19:27:08 +0100716 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 |VALID_CHEIGHT|VALID_TOPLINE);
718}
719
720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
Bram Moolenaar85090142023-06-01 19:27:08 +0100723 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 |VALID_CHEIGHT|VALID_TOPLINE);
725}
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727/*
728 * Call this function when the length of a line (in screen characters) above
729 * the cursor have changed.
730 * Need to take care of w_botline separately!
731 */
732 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100733changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
736 |VALID_CHEIGHT|VALID_TOPLINE);
737}
738
739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100740changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
743 |VALID_CHEIGHT|VALID_TOPLINE);
744}
745
Dominique Pellee764d1b2023-03-12 21:20:59 +0000746#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100748 * Display of line has changed for "buf", invalidate cursor position and
749 * w_botline.
750 */
751 void
752changed_line_display_buf(buf_T *buf)
753{
754 win_T *wp;
755
756 FOR_ALL_WINDOWS(wp)
757 if (wp->w_buffer == buf)
758 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
759 |VALID_CROW|VALID_CHEIGHT
760 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
761}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000762#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100763
764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * Make sure the value of curwin->w_botline is valid.
766 */
767 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100770 validate_botline_win(curwin);
771}
772
773/*
774 * Make sure the value of wp->w_botline is valid.
775 */
776 void
777validate_botline_win(win_T *wp)
778{
779 if (!(wp->w_valid & VALID_BOTLINE))
780 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * Mark curwin->w_botline as invalid (because of some change in the buffer).
785 */
786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100787invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
789 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
790}
791
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
796}
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100799approximate_botline_win(
800 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 wp->w_valid &= ~VALID_BOTLINE;
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
807 */
808 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 check_cursor_moved(curwin);
812 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
813 (VALID_WROW|VALID_WCOL));
814}
815
816/*
817 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
818 * w_topline must be valid, you may need to call update_topline() first!
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100823 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 check_cursor_moved(curwin);
825 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
826 curs_columns(TRUE);
827}
828
829#if defined(FEAT_GUI) || defined(PROTO)
830/*
831 * validate w_cline_row.
832 */
833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100834validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 /*
837 * First make sure that w_topline is valid (after moving the cursor).
838 */
839 update_topline();
840 check_cursor_moved(curwin);
841 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100842 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844#endif
845
846/*
847 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200848 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 */
850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100851curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 linenr_T lnum;
854 int i;
855 int all_invalid;
856 int valid;
857#ifdef FEAT_FOLDING
858 long fold_count;
859#endif
860
Bram Moolenaar85a20022019-12-21 18:25:54 +0100861 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 all_invalid = (!redrawing()
863 || wp->w_lines_valid == 0
864 || wp->w_lines[0].wl_lnum > wp->w_topline);
865 i = 0;
866 wp->w_cline_row = 0;
867 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
868 {
869 valid = FALSE;
870 if (!all_invalid && i < wp->w_lines_valid)
871 {
872 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (wp->w_lines[i].wl_lnum == lnum)
875 {
876#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 // Check for newly inserted lines below this row, in which
878 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (!wp->w_buffer->b_mod_set
880 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
881 || wp->w_buffer->b_mod_top
882 > wp->w_lines[i].wl_lastlnum + 1)
883#endif
884 valid = TRUE;
885 }
886 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100890 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100892 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100894 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
896#ifdef FEAT_FOLDING
897 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (lnum > wp->w_cursor.lnum)
900 break;
901#else
902 ++lnum;
903#endif
904 wp->w_cline_row += wp->w_lines[i].wl_size;
905 }
906 else
907 {
908#ifdef FEAT_FOLDING
909 fold_count = foldedCount(wp, lnum, NULL);
910 if (fold_count)
911 {
912 lnum += fold_count;
913 if (lnum > wp->w_cursor.lnum)
914 break;
915 ++wp->w_cline_row;
916 }
917 else
918#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100919 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100920 wp->w_cline_row += plines_correct_topline(wp, lnum);
921 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 }
925
926 check_cursor_moved(wp);
927 if (!(wp->w_valid & VALID_CHEIGHT))
928 {
929 if (all_invalid
930 || i == wp->w_lines_valid
931 || (i < wp->w_lines_valid
932 && (!wp->w_lines[i].wl_valid
933 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
934 {
935#ifdef FEAT_DIFF
936 if (wp->w_cursor.lnum == wp->w_topline)
937 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
938 TRUE) + wp->w_topfill;
939 else
940#endif
941 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
942#ifdef FEAT_FOLDING
943 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
944 NULL, NULL, TRUE, NULL);
945#endif
946 }
947 else if (i > wp->w_lines_valid)
948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100949 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 wp->w_cline_height = 0;
951#ifdef FEAT_FOLDING
952 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
953 NULL, NULL, TRUE, NULL);
954#endif
955 }
956 else
957 {
958 wp->w_cline_height = wp->w_lines[i].wl_size;
959#ifdef FEAT_FOLDING
960 wp->w_cline_folded = wp->w_lines[i].wl_folded;
961#endif
962 }
963 }
964
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100965 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Validate curwin->w_virtcol only.
971 */
972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100973validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975 validate_virtcol_win(curwin);
976}
977
978/*
979 * Validate wp->w_virtcol only.
980 */
981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100982validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000985
986 if (wp->w_valid & VALID_VIRTCOL)
987 return;
988
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100989#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000990 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100991#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000993#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000995#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
998
999/*
1000 * Validate curwin->w_cline_height only.
1001 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001003validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001006
1007 if (curwin->w_valid & VALID_CHEIGHT)
1008 return;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001011 if (curwin->w_cursor.lnum == curwin->w_topline)
1012 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1013 + curwin->w_topfill;
1014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001024 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 */
1026 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001027validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 colnr_T off;
1030 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001031 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001035 if (curwin->w_valid & VALID_WCOL)
1036 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001037
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001038 col = curwin->w_virtcol;
1039 off = curwin_col_off();
1040 col += off;
1041 width = curwin->w_width - off + curwin_col_off2();
1042
1043 // long line wrapping, adjust curwin->w_wrow
1044 if (curwin->w_p_wrap
1045 && col >= (colnr_T)curwin->w_width
1046 && width > 0)
1047 // use same formula as what is used in curs_columns()
1048 col -= ((col - curwin->w_width) / width + 1) * width;
1049 if (col > (int)curwin->w_leftcol)
1050 col -= curwin->w_leftcol;
1051 else
1052 col = 0;
1053 curwin->w_wcol = col;
1054
1055 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001056#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001057 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059}
1060
1061/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001062 * Compute offset of a window, occupied by absolute or relative line number,
1063 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
1065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001066win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar64486672010-05-16 15:46:46 +02001068 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#ifdef FEAT_FOLDING
1071 + wp->w_p_fdc
1072#endif
1073#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001074 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
1076 );
1077}
1078
1079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 return win_col_off(curwin);
1083}
1084
1085/*
1086 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001087 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1088 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001091win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar64486672010-05-16 15:46:46 +02001093 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001094 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 return 0;
1096}
1097
1098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001099curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 return win_col_off2(curwin);
1102}
1103
1104/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001105 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * Also updates curwin->w_wrow and curwin->w_cline_row.
1107 * Also updates curwin->w_leftcol.
1108 */
1109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001110curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001111 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001114 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int off_left, off_right;
1116 int n;
1117 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001118 int width1; // text width for first screen line
1119 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int new_leftcol;
1121 colnr_T startcol;
1122 colnr_T endcol;
1123 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001124 long so = get_scrolloff_value();
1125 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001126 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 /*
1129 * First make sure that w_topline is valid (after moving the cursor).
1130 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001131 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
1133 /*
1134 * Next make sure that w_cline_row is valid.
1135 */
1136 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001137 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001139#ifdef FEAT_PROP_POPUP
1140 // will be set by getvvcol() but not reset
1141 curwin->w_virtcol_first_char = 0;
1142#endif
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
1145 * Compute the number of virtual columns.
1146 */
1147#ifdef FEAT_FOLDING
1148 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1151 else
1152#endif
1153 getvvcol(curwin, &curwin->w_cursor,
1154 &startcol, &(curwin->w_virtcol), &endcol);
1155
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001158 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 extra = curwin_col_off();
1161 curwin->w_wcol = curwin->w_virtcol + extra;
1162 endcol += extra;
1163
1164 /*
1165 * Now compute w_wrow, counting screen lines from w_cline_row.
1166 */
1167 curwin->w_wrow = curwin->w_cline_row;
1168
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001169 width1 = curwin->w_width - extra;
1170 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001173 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001174 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001175 if (curwin->w_p_wrap)
1176 curwin->w_wrow = curwin->w_height - 1;
1177 else
1178 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001180 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001182 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001184 // skip columns that are not visible
1185 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001186 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001187 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001188 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001189 // Deduct by multiples of width2. This allows the long line
1190 // wrapping formula below to correctly calculate the w_wcol value
1191 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001198 did_sub_skipcol = TRUE;
1199 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001200
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001202 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001205 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1206 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209 }
1210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1212 // is not folded.
1213 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001214 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#ifdef FEAT_FOLDING
1216 && !curwin->w_cline_folded
1217#endif
1218 )
1219 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001220#ifdef FEAT_PROP_POPUP
1221 if (curwin->w_virtcol_first_char > 0)
1222 {
1223 int cols = (curwin->w_width - extra);
1224 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1225
1226 // each "above" text prop shifts the text one row down
1227 curwin->w_wrow += rows;
1228 curwin->w_wcol -= rows * cols;
1229 endcol -= rows * cols;
1230 curwin->w_cline_height = rows + 1;
1231 }
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /*
1234 * If Cursor is left of the screen, scroll rightwards.
1235 * If Cursor is right of the screen, scroll leftwards
1236 * If we get closer to the edge than 'sidescrolloff', scroll a little
1237 * extra
1238 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001239 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001240 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001241 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (off_left < 0 || off_right > 0)
1243 {
1244 if (off_left < 0)
1245 diff = -off_left;
1246 else
1247 diff = off_right;
1248
Bram Moolenaar85a20022019-12-21 18:25:54 +01001249 // When far off or not enough room on either side, put cursor in
1250 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001251 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1252 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 else
1254 {
1255 if (diff < p_ss)
1256 diff = p_ss;
1257 if (off_left < 0)
1258 new_leftcol = curwin->w_leftcol - diff;
1259 else
1260 new_leftcol = curwin->w_leftcol + diff;
1261 }
1262 if (new_leftcol < 0)
1263 new_leftcol = 0;
1264 if (new_leftcol != (int)curwin->w_leftcol)
1265 {
1266 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001268 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 }
1271 curwin->w_wcol -= curwin->w_leftcol;
1272 }
1273 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1274 curwin->w_wcol -= curwin->w_leftcol;
1275 else
1276 curwin->w_wcol = 0;
1277
1278#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // Skip over filler lines. At the top use w_topfill, there
1280 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (curwin->w_cursor.lnum == curwin->w_topline)
1282 curwin->w_wrow += curwin->w_topfill;
1283 else
1284 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1285#endif
1286
1287 prev_skipcol = curwin->w_skipcol;
1288
1289 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if ((curwin->w_wrow >= curwin->w_height
1292 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001293 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && (p_lines =
1295#ifdef FEAT_DIFF
1296 plines_win_nofill
1297#else
1298 plines_win
1299#endif
1300 (curwin, curwin->w_cursor.lnum, FALSE))
1301 - 1 >= curwin->w_height))
1302 && curwin->w_height != 0
1303 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001304 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001305 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001307 // Cursor past end of screen. Happens with a single line that does
1308 // not fit on screen. Find a skipcol to show the text around the
1309 // cursor. Avoid scrolling all the time. compute value of "extra":
1310 // 1: Less than 'scrolloff' lines above
1311 // 2: Less than 'scrolloff' lines below
1312 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001314 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 // Compute last display line of the buffer line that we want at the
1317 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (p_lines == 0)
1319 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1320 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001321 if (p_lines > curwin->w_wrow + so)
1322 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else
1324 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001325 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 extra += 2;
1327
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001328 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001331 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (n > curwin->w_height / 2)
1333 n -= curwin->w_height / 2;
1334 else
1335 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (n > p_lines - curwin->w_height + 1)
1338 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001339 if (n > 0)
1340 curwin->w_skipcol = width1 + (n - 1) * width2;
1341 else
1342 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 else if (extra == 1)
1345 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001346 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001347 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1348 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (extra > 0)
1350 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1352 extra = curwin->w_skipcol / width2;
1353 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 }
1356 else if (extra == 2)
1357 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001358 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001359 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001361 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (endcol > curwin->w_skipcol)
1363 curwin->w_skipcol = endcol;
1364 }
1365
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001366 // adjust w_wrow for the changed w_skipcol
1367 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001369 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (curwin->w_wrow >= curwin->w_height)
1373 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 curwin->w_wrow -= extra;
1378 }
1379
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001380 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (extra > 0)
1382 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1383 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001384 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001386 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 curwin->w_skipcol = 0;
1388 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001389 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001391#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001392 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001393#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001394#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1395 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1396 {
1397 curwin->w_wrow += popup_top_extra(curwin);
1398 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001399 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001400 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001401 else
1402 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001403#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001404
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001405 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1406 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001407 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001408 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1411}
1412
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001413#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001414/*
1415 * Compute the screen position of text character at "pos" in window "wp"
1416 * The resulting values are one-based, zero when character is not visible.
1417 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001418 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001419textpos2screenpos(
1420 win_T *wp,
1421 pos_T *pos,
1422 int *rowp, // screen row
1423 int *scolp, // start screen column
1424 int *ccolp, // cursor screen column
1425 int *ecolp) // end screen column
1426{
1427 colnr_T scol = 0, ccol = 0, ecol = 0;
1428 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001429 colnr_T coloff = 0;
1430
Bram Moolenaar189663b2021-07-21 18:04:56 +02001431 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001433 colnr_T col;
1434 int width;
1435 linenr_T lnum = pos->lnum;
1436#ifdef FEAT_FOLDING
1437 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001439 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1440#endif
zeertzjq6235a102023-08-19 14:12:42 +02001441 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE);
zeertzjqbfe377b2023-08-17 22:58:53 +02001442 // "row" should be the screen line where line "lnum" begins, which can
1443 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001444 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001445
1446#ifdef FEAT_DIFF
1447 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001448 row += lnum == wp->w_topline ? wp->w_topfill
1449 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001450#endif
1451
zeertzjqba2d1912022-12-18 12:28:59 +00001452 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001453#ifdef FEAT_FOLDING
1454 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455 {
zeertzjq6235a102023-08-19 14:12:42 +02001456 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001457 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001458 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001460#endif
1461 {
1462 getvcol(wp, pos, &scol, &ccol, &ecol);
1463
1464 // similar to what is done in validate_cursor_col()
1465 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001466 col += off;
1467 width = wp->w_width - off + win_col_off2(wp);
1468
1469 // long line wrapping, adjust row
1470 if (wp->w_p_wrap
1471 && col >= (colnr_T)wp->w_width
1472 && width > 0)
1473 {
1474 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001475 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001476 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001477 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001478 }
1479 col -= wp->w_leftcol;
1480 if (col >= wp->w_width)
1481 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001482 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001483 {
1484 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001485 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001486 }
1487 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001488 // character is out of the window
1489 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001490 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001491 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001492 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493 *scolp = scol + coloff;
1494 *ccolp = ccol + coloff;
1495 *ecolp = ecol + coloff;
1496}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001497#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498
Bram Moolenaar12034e22019-08-25 22:25:02 +02001499#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001500/*
1501 * "screenpos({winid}, {lnum}, {col})" function
1502 */
1503 void
1504f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1505{
1506 dict_T *dict;
1507 win_T *wp;
1508 pos_T pos;
1509 int row = 0;
1510 int scol = 0, ccol = 0, ecol = 0;
1511
Bram Moolenaar93a10962022-06-16 11:42:09 +01001512 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001513 return;
1514 dict = rettv->vval.v_dict;
1515
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001516 if (in_vim9script()
1517 && (check_for_number_arg(argvars, 0) == FAIL
1518 || check_for_number_arg(argvars, 1) == FAIL
1519 || check_for_number_arg(argvars, 2) == FAIL))
1520 return;
1521
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001522 wp = find_win_by_nr_or_id(&argvars[0]);
1523 if (wp == NULL)
1524 return;
1525
1526 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001527 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1528 {
1529 semsg(_(e_invalid_line_number_nr), pos.lnum);
1530 return;
1531 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001532 pos.col = tv_get_number(&argvars[2]) - 1;
1533 pos.coladd = 0;
1534 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1535
1536 dict_add_number(dict, "row", row);
1537 dict_add_number(dict, "col", scol);
1538 dict_add_number(dict, "curscol", ccol);
1539 dict_add_number(dict, "endcol", ecol);
1540}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001541
1542/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001543 * Convert a virtual (screen) column to a character column. The first column
1544 * is one. For a multibyte character, the column number of the first byte is
1545 * returned.
1546 */
1547 static int
1548virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1549{
zeertzjqf5a94d52023-10-15 10:03:30 +02001550 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001551 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1552 char_u *p = line + offset;
1553
zeertzjqb583eda2023-10-14 11:32:28 +02001554 if (*p == NUL)
1555 {
1556 if (p == line) // empty line
1557 return 0;
1558 // Move to the first byte of the last char.
1559 MB_PTR_BACK(line, p);
1560 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001561 return (int)(p - line + 1);
1562}
1563
1564/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001565 * "virtcol2col({winid}, {lnum}, {col})" function
1566 */
1567 void
1568f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1569{
1570 win_T *wp;
1571 linenr_T lnum;
1572 int screencol;
1573 int error = FALSE;
1574
1575 rettv->vval.v_number = -1;
1576
1577 if (check_for_number_arg(argvars, 0) == FAIL
1578 || check_for_number_arg(argvars, 1) == FAIL
1579 || check_for_number_arg(argvars, 2) == FAIL)
1580 return;
1581
1582 wp = find_win_by_nr_or_id(&argvars[0]);
1583 if (wp == NULL)
1584 return;
1585
1586 lnum = tv_get_number_chk(&argvars[1], &error);
1587 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1588 return;
1589
1590 screencol = tv_get_number_chk(&argvars[2], &error);
1591 if (error || screencol < 0)
1592 return;
1593
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001594 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001595}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
1599 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602scrolldown(
1603 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int wrow;
1608 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001609 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001610 int width1 = 0;
1611 int width2 = 0;
1612
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001613 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001614 {
1615 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001616 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619#ifdef FEAT_FOLDING
1620 linenr_T first;
1621
Bram Moolenaar85a20022019-12-21 18:25:54 +01001622 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1624#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001626 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001629 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1630 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 ++curwin->w_topfill;
1633 ++done;
1634 }
1635 else
1636#endif
1637 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001638 // break when at the very top
1639 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001640 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001642 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001644 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001645 if (curwin->w_skipcol >= width1 + width2)
1646 curwin->w_skipcol -= width2;
1647 else
1648 curwin->w_skipcol -= width1;
1649 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001653 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001654 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001655 --curwin->w_topline;
1656 curwin->w_skipcol = 0;
1657#ifdef FEAT_DIFF
1658 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001660#ifdef FEAT_FOLDING
1661 // A sequence of folded lines only counts for one logical line
1662 if (hasFolding(curwin->w_topline, &first, NULL))
1663 {
1664 ++done;
1665 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001666 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001667 curwin->w_botline -= curwin->w_topline - first;
1668 curwin->w_topline = first;
1669 }
1670 else
1671#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001672 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001673 {
1674 int size = win_linetabsize(curwin, curwin->w_topline,
1675 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1676 if (size > width1)
1677 {
1678 curwin->w_skipcol = width1;
1679 size -= width1;
1680 redraw_later(UPD_NOT_VALID);
1681 }
1682 while (size > width2)
1683 {
1684 curwin->w_skipcol += width2;
1685 size -= width2;
1686 }
1687 ++done;
1688 }
1689 else
1690 done += PLINES_NOFILL(curwin->w_topline);
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 invalidate_botline();
1695 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 curwin->w_wrow += done; // keep w_wrow updated
1697 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699#ifdef FEAT_DIFF
1700 if (curwin->w_cursor.lnum == curwin->w_topline)
1701 curwin->w_cline_row = 0;
1702 check_topfill(curwin, TRUE);
1703#endif
1704
1705 /*
1706 * Compute the row number of the last row of the cursor line
1707 * and move the cursor onto the displayed part of the window.
1708 */
1709 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001710 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
1712 validate_virtcol();
1713 validate_cheight();
1714 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001715 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1718 {
1719#ifdef FEAT_FOLDING
1720 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1721 {
1722 --wrow;
1723 if (first == 1)
1724 curwin->w_cursor.lnum = 1;
1725 else
1726 curwin->w_cursor.lnum = first - 1;
1727 }
1728 else
1729#endif
1730 wrow -= plines(curwin->w_cursor.lnum--);
1731 curwin->w_valid &=
1732 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1733 moved = TRUE;
1734 }
1735 if (moved)
1736 {
1737#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 foldAdjustCursor();
1740#endif
1741 coladvance(curwin->w_curswant);
1742 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001743
1744 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1745 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001746 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001747 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1748
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001749 // make sure the cursor is in the visible text
1750 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001751 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 int row = 0;
1753 if (col >= width1)
1754 {
1755 col -= width1;
1756 ++row;
1757 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001758 if (col > width2 && width2 > 0)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001759 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001760 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001761 // even so col is not used anymore,
1762 // make sure it is correct, just in case
1763 col = col % width2;
1764 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001765 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001766 {
1767 curwin->w_curswant = curwin->w_virtcol
1768 - (row - curwin->w_height + 1) * width2;
1769 coladvance(curwin->w_curswant);
1770 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772}
1773
1774/*
1775 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001778scrollup(
1779 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001780 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001782 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001784 if (do_sms
1785# ifdef FEAT_FOLDING
1786 || (byfold && hasAnyFolding(curwin))
1787# endif
1788# ifdef FEAT_DIFF
1789 || (curwin->w_p_diff && !curwin->w_p_wrap)
1790# endif
1791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001793 int width1 = curwin->w_width - curwin_col_off();
1794 int width2 = width1 + curwin_col_off2();
1795 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001796 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001797
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001798 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001799 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800
1801 // diff mode: first consume "topfill"
1802 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1803 // the line, then advance to the next line.
1804 // folding: count each sequence of folded lines as one logical line.
1805 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807# ifdef FEAT_DIFF
1808 if (curwin->w_topfill > 0)
1809 --curwin->w_topfill;
1810 else
1811# endif
1812 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001813 linenr_T lnum = curwin->w_topline;
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815# ifdef FEAT_FOLDING
1816 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001817 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 (void)hasFolding(lnum, NULL, &lnum);
1819# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001820 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001821 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001822 // 'smoothscroll': increase "w_skipcol" until it goes over
1823 // the end of the line, then advance to the next line.
1824 int add = curwin->w_skipcol > 0 ? width2 : width1;
1825 curwin->w_skipcol += add;
1826 if (curwin->w_skipcol >= size)
1827 {
1828 if (lnum == curbuf->b_ml.ml_line_count)
1829 {
1830 // at the last screen line, can't scroll further
1831 curwin->w_skipcol -= add;
1832 break;
1833 }
1834 ++lnum;
1835 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001836 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001837 else
1838 {
1839 if (lnum >= curbuf->b_ml.ml_line_count)
1840 break;
1841 ++lnum;
1842 }
1843
1844 if (lnum > curwin->w_topline)
1845 {
1846 // approximate w_botline
1847 curwin->w_botline += lnum - curwin->w_topline;
1848 curwin->w_topline = lnum;
1849# ifdef FEAT_DIFF
1850 curwin->w_topfill = diff_check_fill(curwin, lnum);
1851# endif
1852 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001853 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001854 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001855 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001856 }
1857 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001858
zeertzjqd9a92dc2023-06-05 18:41:35 +01001859 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1860 // need to redraw more, because wl_size of the (new) topline may
1861 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001862 redraw_later(UPD_NOT_VALID);
1863 }
1864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001867 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
1870 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1871 curwin->w_topline = curbuf->b_ml.ml_line_count;
1872 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1873 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1874
1875#ifdef FEAT_DIFF
1876 check_topfill(curwin, FALSE);
1877#endif
1878
1879#ifdef FEAT_FOLDING
1880 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001881 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1883#endif
1884
1885 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1886 if (curwin->w_cursor.lnum < curwin->w_topline)
1887 {
1888 curwin->w_cursor.lnum = curwin->w_topline;
1889 curwin->w_valid &=
1890 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1891 coladvance(curwin->w_curswant);
1892 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001893 if (curwin->w_cursor.lnum == curwin->w_topline
1894 && do_sms && curwin->w_skipcol > 0)
1895 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001896 int col_off = curwin_col_off();
1897 int col_off2 = curwin_col_off2();
1898
1899 int width1 = curwin->w_width - col_off;
1900 int width2 = width1 + col_off2;
1901 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001902 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001903 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001904 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001905
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001906 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001907 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001908 int overlap = scrolloff_cols != 0 ? 0
1909 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001910
Bram Moolenaar118c2352022-10-09 17:19:27 +01001911 // Make sure the cursor is in a visible part of the line, taking
1912 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001913 // If there are not enough screen lines put the cursor in the middle.
1914 if (scrolloff_cols > space_cols / 2)
1915 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001916 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001917 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001918 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 colnr_T col = curwin->w_virtcol;
1920
1921 if (col < width1)
1922 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001923 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001924 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001925 curwin->w_curswant = col;
1926 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001927
1928 // validate_virtcol() marked various things as valid, but after
1929 // moving the cursor they need to be recomputed
1930 curwin->w_valid &=
1931 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001932 }
1933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001936/*
1937 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1938 * valid for 'smoothscroll'.
1939 */
1940 void
1941adjust_skipcol(void)
1942{
1943 if (!curwin->w_p_wrap
1944 || !curwin->w_p_sms
1945 || curwin->w_cursor.lnum != curwin->w_topline)
1946 return;
1947
1948 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001949 if (width1 <= 0)
1950 return; // no text will be displayed
1951
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001952 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001953 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001954 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1955 int scrolled = FALSE;
1956
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001957 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001958 if (curwin->w_cline_height == curwin->w_height
1959 // w_cline_height may be capped at w_height, check there aren't
1960 // actually more lines.
1961 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1962 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001963 {
1964 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001965 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001966 return;
1967 }
1968
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001969 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001970 int overlap = sms_marker_overlap(curwin,
1971 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001972 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001973 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001974 {
1975 // scroll a screen line down
1976 if (curwin->w_skipcol >= width1 + width2)
1977 curwin->w_skipcol -= width2;
1978 else
1979 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001980 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001981 }
1982 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001983 {
1984 validate_virtcol();
1985 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001986 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001987 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001988
1989 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1990 int row = 0;
1991 if (col >= width1)
1992 {
1993 col -= width1;
1994 ++row;
1995 }
1996 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001997 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001998 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001999 // col may no longer be used, but make
2000 // sure it is correct anyhow, just in case
2001 col = col % width2;
2002 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002003 if (row >= curwin->w_height)
2004 {
2005 if (curwin->w_skipcol == 0)
2006 {
2007 curwin->w_skipcol += width1;
2008 --row;
2009 }
2010 if (row >= curwin->w_height)
2011 curwin->w_skipcol += (row - curwin->w_height) * width2;
2012 redraw_later(UPD_NOT_VALID);
2013 }
2014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#ifdef FEAT_DIFF
2017/*
2018 * Don't end up with too many filler lines in the window.
2019 */
2020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021check_topfill(
2022 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002023 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int n;
2026
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002027 if (wp->w_topfill <= 0)
2028 return;
2029
2030 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2031 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002033 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002035 --wp->w_topline;
2036 wp->w_topfill = 0;
2037 }
2038 else
2039 {
2040 wp->w_topfill = wp->w_height - n;
2041 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 }
2045}
2046
2047/*
2048 * Use as many filler lines as possible for w_topline. Make sure w_topline
2049 * is still visible.
2050 */
2051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 int n;
2055
2056 n = plines_nofill(curwin->w_topline);
2057 if (n >= curwin->w_height)
2058 curwin->w_topfill = 0;
2059 else
2060 {
2061 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2062 if (curwin->w_topfill + n > curwin->w_height)
2063 curwin->w_topfill = curwin->w_height - n;
2064 }
2065}
2066#endif
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068/*
2069 * Scroll the screen one line down, but don't do it if it would move the
2070 * cursor off the screen.
2071 */
2072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 int end_row;
2076#ifdef FEAT_DIFF
2077 int can_fill = (curwin->w_topfill
2078 < diff_check_fill(curwin, curwin->w_topline));
2079#endif
2080
2081 if (curwin->w_topline <= 1
2082#ifdef FEAT_DIFF
2083 && !can_fill
2084#endif
2085 )
2086 return;
2087
Bram Moolenaar85a20022019-12-21 18:25:54 +01002088 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /*
2091 * Compute the row number of the last row of the cursor line
2092 * and make sure it doesn't go off the screen. Make sure the cursor
2093 * doesn't go past 'scrolloff' lines from the screen end.
2094 */
2095 end_row = curwin->w_wrow;
2096#ifdef FEAT_DIFF
2097 if (can_fill)
2098 ++end_row;
2099 else
2100 end_row += plines_nofill(curwin->w_topline - 1);
2101#else
2102 end_row += plines(curwin->w_topline - 1);
2103#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002104 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 validate_cheight();
2107 validate_virtcol();
2108 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002109 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002111 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113#ifdef FEAT_DIFF
2114 if (can_fill)
2115 {
2116 ++curwin->w_topfill;
2117 check_topfill(curwin, TRUE);
2118 }
2119 else
2120 {
2121 --curwin->w_topline;
2122 curwin->w_topfill = 0;
2123 }
2124#else
2125 --curwin->w_topline;
2126#endif
2127#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002128 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002130 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2132 }
2133}
2134
2135/*
2136 * Scroll the screen one line up, but don't do it if it would move the cursor
2137 * off the screen.
2138 */
2139 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002140scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 int start_row;
2143
2144 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2145#ifdef FEAT_DIFF
2146 && curwin->w_topfill == 0
2147#endif
2148 )
2149 return;
2150
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 /*
2154 * Compute the row number of the first row of the cursor line
2155 * and make sure it doesn't go off the screen. Make sure the cursor
2156 * doesn't go before 'scrolloff' lines from the screen start.
2157 */
2158#ifdef FEAT_DIFF
2159 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2160 - curwin->w_topfill;
2161#else
2162 start_row = curwin->w_wrow - plines(curwin->w_topline);
2163#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002164 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002167 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002169 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171#ifdef FEAT_DIFF
2172 if (curwin->w_topfill > 0)
2173 --curwin->w_topfill;
2174 else
2175#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002176 {
2177#ifdef FEAT_FOLDING
2178 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002181 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2184 }
2185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187/*
2188 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2189 * a (wrapped) text line. Uses and sets "lp->fill".
2190 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002191 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
2193 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002194topline_back_winheight(
2195 lineoff_T *lp,
2196 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198#ifdef FEAT_DIFF
2199 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2200 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ++lp->fill;
2203 lp->height = 1;
2204 }
2205 else
2206#endif
2207 {
2208 --lp->lnum;
2209#ifdef FEAT_DIFF
2210 lp->fill = 0;
2211#endif
2212 if (lp->lnum < 1)
2213 lp->height = MAXCOL;
2214 else
2215#ifdef FEAT_FOLDING
2216 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 lp->height = 1;
2219 else
2220#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002221 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223}
2224
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002225 static void
2226topline_back(lineoff_T *lp)
2227{
2228 topline_back_winheight(lp, TRUE);
2229}
2230
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232/*
2233 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2234 * a (wrapped) text line. Uses and sets "lp->fill".
2235 * Returns the height of the added line in "lp->height".
2236 * Lines below the last one are incredibly high.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241#ifdef FEAT_DIFF
2242 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2243 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002244 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++lp->fill;
2246 lp->height = 1;
2247 }
2248 else
2249#endif
2250 {
2251 ++lp->lnum;
2252#ifdef FEAT_DIFF
2253 lp->fill = 0;
2254#endif
2255 if (lp->lnum > curbuf->b_ml.ml_line_count)
2256 lp->height = MAXCOL;
2257 else
2258#ifdef FEAT_FOLDING
2259 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002260 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 lp->height = 1;
2262 else
2263#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002264 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266}
2267
2268#ifdef FEAT_DIFF
2269/*
2270 * Switch from including filler lines below lp->lnum to including filler
2271 * lines above loff.lnum + 1. This keeps pointing to the same line.
2272 * When there are no filler lines nothing changes.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 if (lp->fill > 0)
2278 {
2279 ++lp->lnum;
2280 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2281 }
2282}
2283
2284/*
2285 * Switch from including filler lines above lp->lnum to including filler
2286 * lines below loff.lnum - 1. This keeps pointing to the same line.
2287 * When there are no filler lines nothing changes.
2288 */
2289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 if (lp->fill > 0)
2293 {
2294 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2295 --lp->lnum;
2296 }
2297}
2298#endif
2299
2300/*
2301 * Recompute topline to put the cursor at the top of the window.
2302 * Scroll at least "min_scroll" lines.
2303 * If "always" is TRUE, always set topline (for "zt").
2304 */
2305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002306scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 int scrolled = 0;
2309 int extra = 0;
2310 int used;
2311 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 linenr_T top; // just above displayed lines
2313 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002315 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_DIFF
2317 linenr_T old_topfill = curwin->w_topfill;
2318#endif
2319 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002320 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (mouse_dragging > 0)
2323 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 /*
2326 * Decrease topline until:
2327 * - it has become 1
2328 * - (part of) the cursor line is moved off the screen or
2329 * - moved at least 'scrolljump' lines and
2330 * - at least 'scrolloff' lines above and below the cursor
2331 */
2332 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (curwin->w_cursor.lnum < curwin->w_topline)
2335 scrolled = used;
2336
2337#ifdef FEAT_FOLDING
2338 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2339 {
2340 --top;
2341 ++bot;
2342 }
2343 else
2344#endif
2345 {
2346 top = curwin->w_cursor.lnum - 1;
2347 bot = curwin->w_cursor.lnum + 1;
2348 }
2349 new_topline = top + 1;
2350
2351#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 // "used" already contains the number of filler lines above, don't add it
2353 // again.
2354 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002355 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357
2358 /*
2359 * Check if the lines from "top" to "bot" fit in the window. If they do,
2360 * set new_topline and advance "top" and "bot" to include more lines.
2361 */
2362 while (top > 0)
2363 {
2364#ifdef FEAT_FOLDING
2365 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002366 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 i = 1;
2368 else
2369#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002370 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002371 if (top < curwin->w_topline)
2372 scrolled += i;
2373
2374 // If scrolling is needed, scroll at least 'sj' lines.
2375 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2376 && extra >= off)
2377 break;
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 used += i;
2380 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2381 {
2382#ifdef FEAT_FOLDING
2383 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002384 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 ++used;
2386 else
2387#endif
2388 used += plines(bot);
2389 }
2390 if (used > curwin->w_height)
2391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 extra += i;
2394 new_topline = top;
2395 --top;
2396 ++bot;
2397 }
2398
2399 /*
2400 * If we don't have enough space, put cursor in the middle.
2401 * This makes sure we get the same position when using "k" and "j"
2402 * in a small window.
2403 */
2404 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002405 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
2407 {
2408 /*
2409 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002410 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 */
2412 if (new_topline < curwin->w_topline || always)
2413 curwin->w_topline = new_topline;
2414 if (curwin->w_topline > curwin->w_cursor.lnum)
2415 curwin->w_topline = curwin->w_cursor.lnum;
2416#ifdef FEAT_DIFF
2417 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2418 if (curwin->w_topfill > 0 && extra > off)
2419 {
2420 curwin->w_topfill -= extra - off;
2421 if (curwin->w_topfill < 0)
2422 curwin->w_topfill = 0;
2423 }
2424 check_topfill(curwin, FALSE);
2425#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002426 if (curwin->w_topline != old_topline)
2427 reset_skipcol();
2428 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002429 {
2430 validate_virtcol();
2431 if (curwin->w_skipcol >= curwin->w_virtcol)
2432 // TODO: if the line doesn't fit may optimize w_skipcol instead
2433 // of making it zero
2434 reset_skipcol();
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002437 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#ifdef FEAT_DIFF
2439 || curwin->w_topfill != old_topfill
2440#endif
2441 )
2442 curwin->w_valid &=
2443 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2444 curwin->w_valid |= VALID_TOPLINE;
2445 }
2446}
2447
2448/*
2449 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2450 * screen lines for text lines.
2451 */
2452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002453set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455#ifdef FEAT_DIFF
2456 wp->w_filler_rows = 0;
2457#endif
2458 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002459 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 else
2461 {
2462 wp->w_empty_rows = wp->w_height - used;
2463#ifdef FEAT_DIFF
2464 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2465 {
2466 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2467 if (wp->w_empty_rows > wp->w_filler_rows)
2468 wp->w_empty_rows -= wp->w_filler_rows;
2469 else
2470 {
2471 wp->w_filler_rows = wp->w_empty_rows;
2472 wp->w_empty_rows = 0;
2473 }
2474 }
2475#endif
2476 }
2477}
2478
2479/*
2480 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002481 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2483 * This is messy stuff!!!
2484 */
2485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002486scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 int used;
2489 int scrolled = 0;
2490 int extra = 0;
2491 int i;
2492 linenr_T line_count;
2493 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002494 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 lineoff_T loff;
2496 lineoff_T boff;
2497#ifdef FEAT_DIFF
2498 int old_topfill = curwin->w_topfill;
2499 int fill_below_window;
2500#endif
2501 linenr_T old_botline = curwin->w_botline;
2502 linenr_T old_valid = curwin->w_valid;
2503 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002504 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002505 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002506 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508 cln = curwin->w_cursor.lnum;
2509 if (set_topbot)
2510 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002511 int set_skipcol = FALSE;
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 used = 0;
2514 curwin->w_botline = cln + 1;
2515#ifdef FEAT_DIFF
2516 loff.fill = 0;
2517#endif
2518 for (curwin->w_topline = curwin->w_botline;
2519 curwin->w_topline > 1;
2520 curwin->w_topline = loff.lnum)
2521 {
2522 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002523 topline_back_winheight(&loff, FALSE);
2524 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002526 if (used + loff.height > curwin->w_height)
2527 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002528 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002529 {
2530 // 'smoothscroll' and 'wrap' are set. The above line is
2531 // too long to show in its entirety, so we show just a part
2532 // of it.
2533 if (used < curwin->w_height)
2534 {
2535 int plines_offset = used + loff.height
2536 - curwin->w_height;
2537 used = curwin->w_height;
2538#ifdef FEAT_DIFF
2539 curwin->w_topfill = loff.fill;
2540#endif
2541 curwin->w_topline = loff.lnum;
2542 curwin->w_skipcol = skipcol_from_plines(
2543 curwin, plines_offset);
2544 set_skipcol = TRUE;
2545 }
2546 }
2547 break;
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 used += loff.height;
2550#ifdef FEAT_DIFF
2551 curwin->w_topfill = loff.fill;
2552#endif
2553 }
2554 set_empty_rows(curwin, used);
2555 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2556 if (curwin->w_topline != old_topline
2557#ifdef FEAT_DIFF
2558 || curwin->w_topfill != old_topfill
2559#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002560 || set_skipcol
2561 || curwin->w_skipcol != 0)
2562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002564 if (set_skipcol)
2565 redraw_later(UPD_NOT_VALID);
2566 else
2567 reset_skipcol();
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 else
2571 validate_botline();
2572
Bram Moolenaar85a20022019-12-21 18:25:54 +01002573 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574#ifdef FEAT_DIFF
2575 used = plines_nofill(cln);
2576#else
2577 validate_cheight();
2578 used = curwin->w_cline_height;
2579#endif
2580
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002581 // If the cursor is on or below botline, we will at least scroll by the
2582 // height of the cursor line, which is "used". Correct for empty lines,
2583 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (cln >= curwin->w_botline)
2585 {
2586 scrolled = used;
2587 if (cln == curwin->w_botline)
2588 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002589 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002590 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002591 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002592 // Calculate how many screen lines the current top line of window
2593 // occupies. If it is occupying more than the entire window, we
2594 // need to scroll the additional clipped lines to scroll past the
2595 // top line before we can move on to the other lines.
2596 int top_plines =
2597#ifdef FEAT_DIFF
2598 plines_win_nofill
2599#else
2600 plines_win
2601#endif
2602 (curwin, curwin->w_topline, FALSE);
2603 int skip_lines = 0;
2604 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002605 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002606 {
fullwaywang8154e642023-06-24 21:58:09 +01002607 int width2 = width1 + curwin_col_off2();
2608 // similar formula is used in curs_columns()
2609 if (curwin->w_skipcol > width1)
2610 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2611 else if (curwin->w_skipcol > 0)
2612 skip_lines = 1;
2613
2614 top_plines -= skip_lines;
2615 if (top_plines > curwin->w_height)
2616 {
2617 scrolled += (top_plines - curwin->w_height);
2618 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002619 }
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622
2623 /*
2624 * Stop counting lines to scroll when
2625 * - hitting start of the file
2626 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002627 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * - lines between botline and cursor have been counted
2629 */
2630#ifdef FEAT_FOLDING
2631 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2632#endif
2633 {
2634 loff.lnum = cln;
2635 boff.lnum = cln;
2636 }
2637#ifdef FEAT_DIFF
2638 loff.fill = 0;
2639 boff.fill = 0;
2640 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2641 - curwin->w_filler_rows;
2642#endif
2643
2644 while (loff.lnum > 1)
2645 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002646 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2647 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002649 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2651 && loff.lnum <= curwin->w_botline
2652#ifdef FEAT_DIFF
2653 && (loff.lnum < curwin->w_botline
2654 || loff.fill >= fill_below_window)
2655#endif
2656 )
2657 break;
2658
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002661 if (loff.height == MAXCOL)
2662 used = MAXCOL;
2663 else
2664 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (used > curwin->w_height)
2666 break;
2667 if (loff.lnum >= curwin->w_botline
2668#ifdef FEAT_DIFF
2669 && (loff.lnum > curwin->w_botline
2670 || loff.fill <= fill_below_window)
2671#endif
2672 )
2673 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002674 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 scrolled += loff.height;
2676 if (loff.lnum == curwin->w_botline
2677#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002678 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#endif
2680 )
2681 scrolled -= curwin->w_empty_rows;
2682 }
2683
2684 if (boff.lnum < curbuf->b_ml.ml_line_count)
2685 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 botline_forw(&boff);
2688 used += boff.height;
2689 if (used > curwin->w_height)
2690 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002691 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2692 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
2694 extra += boff.height;
2695 if (boff.lnum >= curwin->w_botline
2696#ifdef FEAT_DIFF
2697 || (boff.lnum + 1 == curwin->w_botline
2698 && boff.fill > curwin->w_filler_rows)
2699#endif
2700 )
2701 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 scrolled += boff.height;
2704 if (boff.lnum == curwin->w_botline
2705#ifdef FEAT_DIFF
2706 && boff.fill == 0
2707#endif
2708 )
2709 scrolled -= curwin->w_empty_rows;
2710 }
2711 }
2712 }
2713 }
2714
Bram Moolenaar85a20022019-12-21 18:25:54 +01002715 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (scrolled <= 0)
2717 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 else if (used > curwin->w_height)
2720 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 else
2723 {
2724 line_count = 0;
2725#ifdef FEAT_DIFF
2726 boff.fill = curwin->w_topfill;
2727#endif
2728 boff.lnum = curwin->w_topline - 1;
2729 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2730 {
2731 botline_forw(&boff);
2732 i += boff.height;
2733 ++line_count;
2734 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002735 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 line_count = 9999;
2737 }
2738
2739 /*
2740 * Scroll up if the cursor is off the bottom of the screen a bit.
2741 * Otherwise put it at 1/2 of the screen.
2742 */
2743 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002744 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002745 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002746 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002747 if (do_sms)
2748 scrollup(scrolled, TRUE); // TODO
2749 else
2750 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 /*
2754 * If topline didn't change we need to restore w_botline and w_empty_rows
2755 * (we changed them).
2756 * If topline did change, update_screen() will set botline.
2757 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002758 if (curwin->w_topline == old_topline
2759 && curwin->w_skipcol == old_skipcol
2760 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
2762 curwin->w_botline = old_botline;
2763 curwin->w_empty_rows = old_empty_rows;
2764 curwin->w_valid = old_valid;
2765 }
2766 curwin->w_valid |= VALID_TOPLINE;
2767}
2768
2769/*
2770 * Recompute topline to put the cursor halfway the window
2771 * If "atend" is TRUE, also put it halfway at the end of the file.
2772 */
2773 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002774scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 int above = 0;
2777 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002778 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779#ifdef FEAT_DIFF
2780 int topfill = 0;
2781#endif
2782 int below = 0;
2783 int used;
2784 lineoff_T loff;
2785 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002786#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002787 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002790#ifdef FEAT_PROP_POPUP
2791 // if the width changed this needs to be updated first
2792 may_update_popup_position();
2793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2795#ifdef FEAT_FOLDING
2796 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2797#endif
2798#ifdef FEAT_DIFF
2799 used = plines_nofill(loff.lnum);
2800 loff.fill = 0;
2801 boff.fill = 0;
2802#else
2803 used = plines(loff.lnum);
2804#endif
2805 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002806
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002807 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002808 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2809 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002810 {
2811 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002812 if (atend)
2813 {
2814 want_height = (curwin->w_height - used) / 2;
2815 used = 0;
2816 }
2817 else
2818 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002819 }
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 while (topline > 1)
2822 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002823 // If using smoothscroll, we can precisely scroll to the
2824 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002825 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002826 {
2827 topline_back_winheight(&loff, FALSE);
2828 if (loff.height == MAXCOL)
2829 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002830 used += loff.height;
2831 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002832 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002833 botline_forw(&boff);
2834 used += boff.height;
2835 }
2836 if (used > want_height)
2837 {
2838 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002839 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002840 topline = loff.lnum;
2841#ifdef FEAT_DIFF
2842 topfill = loff.fill;
2843#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002844 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002845 }
2846 break;
2847 }
2848 topline = loff.lnum;
2849#ifdef FEAT_DIFF
2850 topfill = loff.fill;
2851#endif
2852 continue;
2853 }
2854
2855 // If not using smoothscroll, we have to iteratively find how many
2856 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002857 // This may not be right in the middle if the lines'
2858 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002859
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002860 // Depending on "prefer_above" we add a line above or below first.
2861 // Loop twice to avoid duplicating code.
2862 int done = FALSE;
2863 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002865 if (prefer_above ? (round == 2 && below < above)
2866 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002868 // add a line below the cursor
2869 if (boff.lnum < curbuf->b_ml.ml_line_count)
2870 {
2871 botline_forw(&boff);
2872 used += boff.height;
2873 if (used > curwin->w_height)
2874 {
2875 done = TRUE;
2876 break;
2877 }
2878 below += boff.height;
2879 }
2880 else
2881 {
2882 ++below; // count a "~" line
2883 if (atend)
2884 ++used;
2885 }
2886 }
2887
2888 if (prefer_above ? (round == 1 && below >= above)
2889 : (round == 1 && below > above))
2890 {
2891 // add a line above the cursor
2892 topline_back(&loff);
2893 if (loff.height == MAXCOL)
2894 used = MAXCOL;
2895 else
2896 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002898 {
2899 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002901 }
2902 above += loff.height;
2903 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002905 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002909 if (done)
2910 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#ifdef FEAT_FOLDING
2914 if (!hasFolding(topline, &curwin->w_topline, NULL))
2915#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002916 {
2917 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002918 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002919 || curwin->w_skipcol != 0)
2920 {
2921 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002922 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002923 {
2924 curwin->w_skipcol = skipcol;
2925 redraw_later(UPD_NOT_VALID);
2926 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002927 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002928 reset_skipcol();
2929 }
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#ifdef FEAT_DIFF
2932 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002933 if (old_topline > curwin->w_topline + curwin->w_height)
2934 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 check_topfill(curwin, FALSE);
2936#endif
2937 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2938 curwin->w_valid |= VALID_TOPLINE;
2939}
2940
2941/*
2942 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002943 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 * If not possible, put it at the same position as scroll_cursor_halfway().
2945 * When called topline must be valid!
2946 */
2947 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002948cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002950 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002952 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 linenr_T botline;
2954 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002957 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /*
2960 * How many lines we would like to have above/below the cursor depends on
2961 * whether the first/last line of the file is on screen.
2962 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002963 above_wanted = so;
2964 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002965 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
2967 above_wanted = mouse_dragging - 1;
2968 below_wanted = mouse_dragging - 1;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (curwin->w_topline == 1)
2971 {
2972 above_wanted = 0;
2973 max_off = curwin->w_height / 2;
2974 if (below_wanted > max_off)
2975 below_wanted = max_off;
2976 }
2977 validate_botline();
2978 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002979 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
2981 below_wanted = 0;
2982 max_off = (curwin->w_height - 1) / 2;
2983 if (above_wanted > max_off)
2984 above_wanted = max_off;
2985 }
2986
2987 /*
2988 * If there are sufficient file-lines above and below the cursor, we can
2989 * return now.
2990 */
2991 cln = curwin->w_cursor.lnum;
2992 if (cln >= curwin->w_topline + above_wanted
2993 && cln < curwin->w_botline - below_wanted
2994#ifdef FEAT_FOLDING
2995 && !hasAnyFolding(curwin)
2996#endif
2997 )
2998 return;
2999
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003000 if (curwin->w_p_sms && !curwin->w_p_wrap)
3001 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003002 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003003 if (curwin->w_cline_height == curwin->w_height)
3004 {
3005 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003006 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003007 return;
3008 }
3009 // TODO: If the cursor line doesn't fit in the window then only adjust
3010 // w_skipcol.
3011 }
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 /*
3014 * Narrow down the area where the cursor can be put by taking lines from
3015 * the top and the bottom until:
3016 * - the desired context lines are found
3017 * - the lines from the top is past the lines from the bottom
3018 */
3019 topline = curwin->w_topline;
3020 botline = curwin->w_botline - 1;
3021#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003022 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 above = curwin->w_topfill;
3024 below = curwin->w_filler_rows;
3025#endif
3026 while ((above < above_wanted || below < below_wanted) && topline < botline)
3027 {
3028 if (below < below_wanted && (below <= above || above >= above_wanted))
3029 {
3030#ifdef FEAT_FOLDING
3031 if (hasFolding(botline, &botline, NULL))
3032 ++below;
3033 else
3034#endif
3035 below += plines(botline);
3036 --botline;
3037 }
3038 if (above < above_wanted && (above < below || below >= below_wanted))
3039 {
3040#ifdef FEAT_FOLDING
3041 if (hasFolding(topline, NULL, &topline))
3042 ++above;
3043 else
3044#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003045 above += PLINES_NOFILL(topline);
3046#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003047 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (topline < botline)
3049 above += diff_check_fill(curwin, topline + 1);
3050#endif
3051 ++topline;
3052 }
3053 }
3054 if (topline == botline || botline == 0)
3055 curwin->w_cursor.lnum = topline;
3056 else if (topline > botline)
3057 curwin->w_cursor.lnum = botline;
3058 else
3059 {
3060 if (cln < topline && curwin->w_topline > 1)
3061 {
3062 curwin->w_cursor.lnum = topline;
3063 curwin->w_valid &=
3064 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3065 }
3066 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3067 {
3068 curwin->w_cursor.lnum = botline;
3069 curwin->w_valid &=
3070 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3071 }
3072 }
3073 curwin->w_valid |= VALID_TOPLINE;
3074}
3075
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003076static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003079 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3080 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003082 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 */
3084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003085onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 long n;
3088 int retval = OK;
3089 lineoff_T loff;
3090 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003091 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar85a20022019-12-21 18:25:54 +01003093 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 beep_flush();
3096 return FAIL;
3097 }
3098
3099 for ( ; count > 0; --count)
3100 {
3101 validate_botline();
3102 /*
3103 * It's an error to move a page up when the first line is already on
3104 * the screen. It's an error to move a page down when the last line
3105 * is on the screen and the topline is 'scrolloff' lines from the
3106 * last line.
3107 */
3108 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003109 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3111 : (curwin->w_topline == 1
3112#ifdef FEAT_DIFF
3113 && curwin->w_topfill ==
3114 diff_check_fill(curwin, curwin->w_topline)
3115#endif
3116 ))
3117 {
3118 beep_flush();
3119 retval = FAIL;
3120 break;
3121 }
3122
3123#ifdef FEAT_DIFF
3124 loff.fill = 0;
3125#endif
3126 if (dir == FORWARD)
3127 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003128 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003130 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003131 if (p_window <= 2)
3132 ++curwin->w_topline;
3133 else
3134 curwin->w_topline += p_window - 2;
3135 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3136 curwin->w_topline = curbuf->b_ml.ml_line_count;
3137 curwin->w_cursor.lnum = curwin->w_topline;
3138 }
3139 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 curwin->w_topline = curbuf->b_ml.ml_line_count;
3143#ifdef FEAT_DIFF
3144 curwin->w_topfill = 0;
3145#endif
3146 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3147 }
3148 else
3149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003150 // For the overlap, start with the line just below the window
3151 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 loff.lnum = curwin->w_botline;
3153#ifdef FEAT_DIFF
3154 loff.fill = diff_check_fill(curwin, loff.lnum)
3155 - curwin->w_filler_rows;
3156#endif
3157 get_scroll_overlap(&loff, -1);
3158 curwin->w_topline = loff.lnum;
3159#ifdef FEAT_DIFF
3160 curwin->w_topfill = loff.fill;
3161 check_topfill(curwin, FALSE);
3162#endif
3163 curwin->w_cursor.lnum = curwin->w_topline;
3164 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3165 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3166 }
3167 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003168 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170#ifdef FEAT_DIFF
3171 if (curwin->w_topline == 1)
3172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003173 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 max_topfill();
3175 continue;
3176 }
3177#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003178 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003179 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003180 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003181 if (p_window <= 2)
3182 --curwin->w_topline;
3183 else
3184 curwin->w_topline -= p_window - 2;
3185 if (curwin->w_topline < 1)
3186 curwin->w_topline = 1;
3187 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3188 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3189 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3190 continue;
3191 }
3192
Bram Moolenaar85a20022019-12-21 18:25:54 +01003193 // Find the line at the top of the window that is going to be the
3194 // line at the bottom of the window. Make sure this results in
3195 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 loff.lnum = curwin->w_topline - 1;
3197#ifdef FEAT_DIFF
3198 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3199 - curwin->w_topfill;
3200#endif
3201 get_scroll_overlap(&loff, 1);
3202
3203 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3204 {
3205 loff.lnum = curbuf->b_ml.ml_line_count;
3206#ifdef FEAT_DIFF
3207 loff.fill = 0;
3208 }
3209 else
3210 {
3211 botline_topline(&loff);
3212#endif
3213 }
3214 curwin->w_cursor.lnum = loff.lnum;
3215
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 // Find the line just above the new topline to get the right line
3217 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 n = 0;
3219 while (n <= curwin->w_height && loff.lnum >= 1)
3220 {
3221 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003222 if (loff.height == MAXCOL)
3223 n = MAXCOL;
3224 else
3225 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003227 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 curwin->w_topline = 1;
3230#ifdef FEAT_DIFF
3231 max_topfill();
3232#endif
3233 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3234 }
3235 else
3236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003237 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#ifdef FEAT_DIFF
3239 topline_botline(&loff);
3240#endif
3241 botline_forw(&loff);
3242 botline_forw(&loff);
3243#ifdef FEAT_DIFF
3244 botline_topline(&loff);
3245#endif
3246#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3249#endif
3250
Bram Moolenaar85a20022019-12-21 18:25:54 +01003251 // Always scroll at least one line. Avoid getting stuck on
3252 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (loff.lnum >= curwin->w_topline
3254#ifdef FEAT_DIFF
3255 && (loff.lnum > curwin->w_topline
3256 || loff.fill >= curwin->w_topfill)
3257#endif
3258 )
3259 {
3260#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003261 // First try using the maximum number of filler lines. If
3262 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 loff.fill = curwin->w_topfill;
3264 if (curwin->w_topfill < diff_check_fill(curwin,
3265 curwin->w_topline))
3266 max_topfill();
3267 if (curwin->w_topfill == loff.fill)
3268#endif
3269 {
3270 --curwin->w_topline;
3271#ifdef FEAT_DIFF
3272 curwin->w_topfill = 0;
3273#endif
zeertzjq05834912023-10-04 20:12:37 +02003274 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 comp_botline(curwin);
3277 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003278 curwin->w_valid &=
3279 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 else
3282 {
3283 curwin->w_topline = loff.lnum;
3284#ifdef FEAT_DIFF
3285 curwin->w_topfill = loff.fill;
3286 check_topfill(curwin, FALSE);
3287#endif
3288 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3289 }
3290 }
3291 }
3292 }
3293#ifdef FEAT_FOLDING
3294 foldAdjustCursor();
3295#endif
3296 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003297 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003298 if (retval == OK)
3299 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3301
Bram Moolenaar907dad72018-07-10 15:07:15 +02003302 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003304 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3305 // But make sure we scroll at least one line (happens with mix of long
3306 // wrapping lines and non-wrapping line).
3307 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003309 scroll_cursor_top(1, FALSE);
3310 if (curwin->w_topline <= old_topline
3311 && old_topline < curbuf->b_ml.ml_line_count)
3312 {
3313 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003315 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3316#endif
3317 }
3318 }
3319#ifdef FEAT_FOLDING
3320 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003325 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return retval;
3327}
3328
3329/*
3330 * Decide how much overlap to use for page-up or page-down scrolling.
3331 * This is symmetric, so that doing both keeps the same lines displayed.
3332 * Three lines are examined:
3333 *
3334 * before CTRL-F after CTRL-F / before CTRL-B
3335 * etc. l1
3336 * l1 last but one line ------------
3337 * l2 last text line l2 top text line
3338 * ------------- l3 second text line
3339 * l3 etc.
3340 */
3341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003342get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int h1, h2, h3, h4;
3345 int min_height = curwin->w_height - 2;
3346 lineoff_T loff0, loff1, loff2;
3347
3348#ifdef FEAT_DIFF
3349 if (lp->fill > 0)
3350 lp->height = 1;
3351 else
3352 lp->height = plines_nofill(lp->lnum);
3353#else
3354 lp->height = plines(lp->lnum);
3355#endif
3356 h1 = lp->height;
3357 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 loff0 = *lp;
3361 if (dir > 0)
3362 botline_forw(lp);
3363 else
3364 topline_back(lp);
3365 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003366 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 return;
3370 }
3371
3372 loff1 = *lp;
3373 if (dir > 0)
3374 botline_forw(lp);
3375 else
3376 topline_back(lp);
3377 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003378 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003380 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 return;
3382 }
3383
3384 loff2 = *lp;
3385 if (dir > 0)
3386 botline_forw(lp);
3387 else
3388 topline_back(lp);
3389 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003390 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003391 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003393 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394}
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396/*
3397 * Scroll 'scroll' lines up or down.
3398 */
3399 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003400halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 long scrolled = 0;
3403 int i;
3404 int n;
3405 int room;
3406
3407 if (Prenum)
3408 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3409 curwin->w_height : Prenum;
3410 n = (curwin->w_p_scr <= curwin->w_height) ?
3411 curwin->w_p_scr : curwin->w_height;
3412
Bram Moolenaard5d37532017-03-27 23:02:07 +02003413 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 validate_botline();
3415 room = curwin->w_empty_rows;
3416#ifdef FEAT_DIFF
3417 room += curwin->w_filler_rows;
3418#endif
3419 if (flag)
3420 {
3421 /*
3422 * scroll the text up
3423 */
3424 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3425 {
3426#ifdef FEAT_DIFF
3427 if (curwin->w_topfill > 0)
3428 {
3429 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003430 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 --curwin->w_topfill;
3432 }
3433 else
3434#endif
3435 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003436 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 n -= i;
3438 if (n < 0 && scrolled > 0)
3439 break;
3440#ifdef FEAT_FOLDING
3441 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3442#endif
3443 ++curwin->w_topline;
3444#ifdef FEAT_DIFF
3445 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3446#endif
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3449 {
3450 ++curwin->w_cursor.lnum;
3451 curwin->w_valid &=
3452 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3456 scrolled += i;
3457
3458 /*
3459 * Correct w_botline for changed w_topline.
3460 * Won't work when there are filler lines.
3461 */
3462#ifdef FEAT_DIFF
3463 if (curwin->w_p_diff)
3464 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3465 else
3466#endif
3467 {
3468 room += i;
3469 do
3470 {
3471 i = plines(curwin->w_botline);
3472 if (i > room)
3473 break;
3474#ifdef FEAT_FOLDING
3475 (void)hasFolding(curwin->w_botline, NULL,
3476 &curwin->w_botline);
3477#endif
3478 ++curwin->w_botline;
3479 room -= i;
3480 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3481 }
3482 }
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /*
3485 * When hit bottom of the file: move cursor down.
3486 */
3487 if (n > 0)
3488 {
3489# ifdef FEAT_FOLDING
3490 if (hasAnyFolding(curwin))
3491 {
3492 while (--n >= 0
3493 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3494 {
3495 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3496 &curwin->w_cursor.lnum);
3497 ++curwin->w_cursor.lnum;
3498 }
3499 }
3500 else
3501# endif
3502 curwin->w_cursor.lnum += n;
3503 check_cursor_lnum();
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 else
3507 {
3508 /*
3509 * scroll the text down
3510 */
3511 while (n > 0 && curwin->w_topline > 1)
3512 {
3513#ifdef FEAT_DIFF
3514 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3515 {
3516 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003517 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 ++curwin->w_topfill;
3519 }
3520 else
3521#endif
3522 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003523 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 n -= i;
3525 if (n < 0 && scrolled > 0)
3526 break;
3527 --curwin->w_topline;
3528#ifdef FEAT_FOLDING
3529 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3530#endif
3531#ifdef FEAT_DIFF
3532 curwin->w_topfill = 0;
3533#endif
3534 }
3535 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3536 VALID_BOTLINE|VALID_BOTLINE_AP);
3537 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (curwin->w_cursor.lnum > 1)
3539 {
3540 --curwin->w_cursor.lnum;
3541 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /*
3546 * When hit top of the file: move cursor up.
3547 */
3548 if (n > 0)
3549 {
3550 if (curwin->w_cursor.lnum <= (linenr_T)n)
3551 curwin->w_cursor.lnum = 1;
3552 else
3553# ifdef FEAT_FOLDING
3554 if (hasAnyFolding(curwin))
3555 {
3556 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3557 {
3558 --curwin->w_cursor.lnum;
3559 (void)hasFolding(curwin->w_cursor.lnum,
3560 &curwin->w_cursor.lnum, NULL);
3561 }
3562 }
3563 else
3564# endif
3565 curwin->w_cursor.lnum -= n;
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003569 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 foldAdjustCursor();
3571# endif
3572#ifdef FEAT_DIFF
3573 check_topfill(curwin, !flag);
3574#endif
3575 cursor_correct();
3576 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003577 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579
Bram Moolenaar860cae12010-06-05 23:22:07 +02003580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003581do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003583 static win_T *prev_curwin = NULL;
3584 static pos_T prev_cursor = {0, 0, 0};
3585
3586 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3587 return;
3588 prev_curwin = curwin;
3589 prev_cursor = curwin->w_cursor;
3590
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003592 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003593 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003594 colnr_T curswant = curwin->w_curswant;
3595 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 win_T *old_curwin = curwin;
3597 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 int old_VIsual_select = VIsual_select;
3599 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600
3601 /*
3602 * loop through the cursorbound windows
3603 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003605 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 {
3607 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003608 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 if (curwin != old_curwin && curwin->w_p_crb)
3610 {
3611# ifdef FEAT_DIFF
3612 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003613 curwin->w_cursor.lnum =
3614 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003615 else
3616# endif
3617 curwin->w_cursor.lnum = line;
3618 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003619 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003620 curwin->w_curswant = curswant;
3621 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622
Bram Moolenaar85a20022019-12-21 18:25:54 +01003623 // Make sure the cursor is in a valid position. Temporarily set
3624 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003625 int restart_edit_save = restart_edit;
3626 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003628
3629 // Avoid a scroll here for the cursor position, 'scrollbind' is
3630 // more important.
3631 if (!curwin->w_p_scb)
3632 validate_cursor();
3633
Bram Moolenaar61452852011-02-01 18:01:11 +01003634 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003635 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003636 if (has_mbyte)
3637 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003638 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003639
Bram Moolenaar85a20022019-12-21 18:25:54 +01003640 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003641 if (!curwin->w_p_scb)
3642 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003643 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003644 }
3645 }
3646
3647 /*
3648 * reset current-window
3649 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003650 VIsual_select = old_VIsual_select;
3651 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003652 curwin = old_curwin;
3653 curbuf = old_curbuf;
3654}