blob: 797184812ad75431fc51fae9bf176fc5cd469210 [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
zeertzjq3f1b5312024-02-06 10:43:36 +010022static void redraw_for_cursorline(win_T *wp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
Bram Moolenaar85a20022019-12-21 18:25:54 +010029 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010031 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010033 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000034} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
zeertzjq6235a102023-08-19 14:12:42 +020040 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010041 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010042 int
zeertzjq6235a102023-08-19 14:12:42 +020043adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010044{
45 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020046 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047
Bram Moolenaarf6196f42022-10-02 21:29:55 +010048 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010049 int w2 = width + win_col_off2(wp);
50 if (wp->w_skipcol >= width && w2 > 0)
51 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020052
53 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054}
55
56/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010057 * Return how many lines "lnum" will take on the screen, taking into account
58 * whether it is the first line, whether w_skipcol is non-zero and limiting to
59 * the window height.
60 */
61 static int
Luuk van Baal9148ba82024-04-08 22:27:41 +020062plines_correct_topline(win_T *wp, linenr_T lnum, int limit_winheight)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010063{
64 int n;
65#ifdef FEAT_DIFF
66 if (lnum == wp->w_topline)
67 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
68 else
69#endif
70 n = plines_win(wp, lnum, FALSE);
71 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020072 n -= adjust_plines_for_skipcol(wp);
Luuk van Baal9148ba82024-04-08 22:27:41 +020073 if (limit_winheight && n > wp->w_height)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010074 n = wp->w_height;
75 return n;
76}
77
78/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Compute wp->w_botline for the current wp->w_topline. Can be called after
80 * wp->w_topline changed.
81 */
82 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010083comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int n;
86 linenr_T lnum;
87 int done;
88#ifdef FEAT_FOLDING
89 linenr_T last;
90 int folded;
91#endif
92
93 /*
94 * If w_cline_row is valid, start there.
95 * Otherwise have to start at w_topline.
96 */
97 check_cursor_moved(wp);
98 if (wp->w_valid & VALID_CROW)
99 {
100 lnum = wp->w_cursor.lnum;
101 done = wp->w_cline_row;
102 }
103 else
104 {
105 lnum = wp->w_topline;
106 done = 0;
107 }
108
109 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
110 {
111#ifdef FEAT_FOLDING
112 last = lnum;
113 folded = FALSE;
114 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
115 {
116 n = 1;
117 folded = TRUE;
118 }
119 else
120#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200122 n = plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if (
125#ifdef FEAT_FOLDING
126 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
127#else
128 lnum == wp->w_cursor.lnum
129#endif
130 )
131 {
132 wp->w_cline_row = done;
133 wp->w_cline_height = n;
134#ifdef FEAT_FOLDING
135 wp->w_cline_folded = folded;
136#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100137 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
139 }
140 if (done + n > wp->w_height)
141 break;
142 done += n;
143#ifdef FEAT_FOLDING
144 lnum = last;
145#endif
146 }
147
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 wp->w_botline = lnum;
150 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
151
152 set_empty_rows(wp, done);
153}
154
155/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100156 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
157 * set.
158 */
zeertzjq3f1b5312024-02-06 10:43:36 +0100159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100160redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100161{
162 if ((wp->w_p_rnu
163#ifdef FEAT_SYN_HL
164 || wp->w_p_cul
165#endif
166 )
167 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200168 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200169 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000170 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100171 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200172 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100173}
174
zeertzjq3e559cd2022-03-27 19:26:55 +0100175#ifdef FEAT_SYN_HL
176/*
177 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
178 * contains "screenline".
179 */
180 static void
181redraw_for_cursorcolumn(win_T *wp)
182{
183 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
184 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100186 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100187 redraw_win_later(wp, UPD_SOME_VALID);
188 // When 'cursorlineopt' contains "screenline" need to redraw with
189 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100192 }
193}
194#endif
195
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100196/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100197 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
198 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000199 * Parameter "extra2" should be the padding on the 2nd line, not the first
Luuk van Baalcb204e62024-04-02 20:49:45 +0200200 * line. When "extra2" is -1 calculate the padding.
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000201 * Returns the number of columns of overlap with buffer text, excluding the
202 * extra padding on the ledge.
203 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100204 int
205sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000206{
Luuk van Baalcb204e62024-04-02 20:49:45 +0200207 if (extra2 == -1)
208 extra2 = win_col_off(wp) - win_col_off2(wp);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100210 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000211 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000213 return 0;
214#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100215 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
216 if (wp->w_p_list && wp->w_lcs_chars.prec)
217 return 1;
218
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return extra2 > 3 ? 0 : 3 - extra2;
220}
221
222/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000223 * Calculates the skipcol offset for window "wp" given how many
224 * physical lines we want to scroll down.
225 */
226 static int
227skipcol_from_plines(win_T *wp, int plines_off)
228{
229 int width1 = wp->w_width - win_col_off(wp);
230
231 int skipcol = 0;
232 if (plines_off > 0)
233 skipcol += width1;
234 if (plines_off > 1)
235 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
236 return skipcol;
237}
238
239/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100240 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000241 */
242 static void
243reset_skipcol(void)
244{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000245 if (curwin->w_skipcol == 0)
246 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000248 curwin->w_skipcol = 0;
249
250 // Should use the least expensive way that displays all that changed.
251 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
252 // enough when the top line gets another screen line.
253 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000254}
255
256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * Update curwin->w_topline and redraw if necessary.
258 * Used to update the screen before printing a message.
259 */
260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100261update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 update_topline();
264 if (must_redraw)
265 update_screen(0);
266}
267
268/*
269 * Update curwin->w_topline to move the cursor onto the screen.
270 */
271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100272update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 long line_count;
275 int halfheight;
276 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifdef FEAT_FOLDING
278 linenr_T lnum;
279#endif
280 int check_topline = FALSE;
281 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100282 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100283 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100285 // Cursor is updated instead when this is TRUE for 'splitkeep'.
286 if (skip_update_topline)
287 return;
288
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // If there is no valid screen and when the window height is zero just use
290 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200291 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100293 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 curwin->w_topline = curwin->w_cursor.lnum;
295 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200296 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200297 return;
298 }
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 check_cursor_moved(curwin);
301 if (curwin->w_valid & VALID_TOPLINE)
302 return;
303
Bram Moolenaar85a20022019-12-21 18:25:54 +0100304 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000305 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100306 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100308 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100310 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312
313 /*
314 * If the buffer is empty, always set topline to 1.
315 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100316 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
318 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100319 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 curwin->w_botline = 2;
zeertzjqc7a8eb52024-05-08 20:22:40 +0200322 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
326
327 /*
328 * If the cursor is above or near the top of the window, scroll the window
329 * to show the line the cursor is in, with 'scrolloff' context.
330 */
331 else
332 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100333 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100335 // If the cursor is above topline, scrolling is always needed.
336 // If the cursor is far below topline and there is no folding,
337 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (curwin->w_cursor.lnum < curwin->w_topline)
339 check_topline = TRUE;
340 else if (check_top_offset())
341 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100342 else if (curwin->w_skipcol > 0
343 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100344 {
345 colnr_T vcol;
Luuk van Baalcb204e62024-04-02 20:49:45 +0200346 int overlap;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000348 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100349 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100350 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baalcb204e62024-04-02 20:49:45 +0200351 overlap = sms_marker_overlap(curwin, -1);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100352 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100353 check_topline = TRUE;
354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
359 curwin->w_topline))
360 check_topline = TRUE;
361#endif
362
363 if (check_topline)
364 {
365 halfheight = curwin->w_height / 2 - 1;
366 if (halfheight < 2)
367 halfheight = 2;
368
369#ifdef FEAT_FOLDING
370 if (hasAnyFolding(curwin))
371 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 // Count the number of logical lines between the cursor and
373 // topline + scrolloff (approximation of how much will be
374 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 n = 0;
376 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100380 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
382 break;
383 (void)hasFolding(lnum, NULL, &lnum);
384 }
385 }
386 else
387#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100388 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar85a20022019-12-21 18:25:54 +0100390 // If we weren't very close to begin with, we scroll to put the
391 // cursor in the middle of the window. Otherwise put the cursor
392 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000394 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 else
396 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000397 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 check_botline = TRUE;
399 }
400 }
401
402 else
403 {
404#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100405 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
407#endif
408 check_botline = TRUE;
409 }
410 }
411
412 /*
413 * If the cursor is below the bottom of the window, scroll the window
414 * to put the cursor on the window.
415 * When w_botline is invalid, recompute it first, to avoid a redraw later.
416 * If w_botline was approximated, we might need a redraw later in a few
417 * cases, but we don't want to spend (a lot of) time recomputing w_botline
418 * for every small change.
419 */
420 if (check_botline)
421 {
422 if (!(curwin->w_valid & VALID_BOTLINE_AP))
423 validate_botline();
424
425 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
426 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000427 if (curwin->w_cursor.lnum < curwin->w_botline)
428 {
429 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100430 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_FOLDING
432 || hasAnyFolding(curwin)
433#endif
434 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 lineoff_T loff;
437
Bram Moolenaar85a20022019-12-21 18:25:54 +0100438 // Cursor is (a few lines) above botline, check if there are
439 // 'scrolloff' window lines below the cursor. If not, need to
440 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 n = curwin->w_empty_rows;
442 loff.lnum = curwin->w_cursor.lnum;
443#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100444 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
446#endif
447#ifdef FEAT_DIFF
448 loff.fill = 0;
449 n += curwin->w_filler_rows;
450#endif
451 loff.height = 0;
452 while (loff.lnum < curwin->w_botline
453#ifdef FEAT_DIFF
454 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
455#endif
456 )
457 {
458 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100459 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 break;
461 botline_forw(&loff);
462 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100463 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100464 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000466 }
467 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000469 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 }
471 if (check_botline)
472 {
473#ifdef FEAT_FOLDING
474 if (hasAnyFolding(curwin))
475 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100476 // Count the number of logical lines between the cursor and
477 // botline - scrolloff (approximation of how much will be
478 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 line_count = 0;
480 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100484 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (lnum <= 0 || line_count > curwin->w_height + 1)
486 break;
487 (void)hasFolding(lnum, &lnum, NULL);
488 }
489 }
490 else
491#endif
492 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100493 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000495 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000497 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 }
500 }
501 curwin->w_valid |= VALID_TOPLINE;
502
503 /*
504 * Need to redraw when topline changed.
505 */
506 if (curwin->w_topline != old_topline
507#ifdef FEAT_DIFF
508 || curwin->w_topfill != old_topfill
509#endif
510 )
511 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100512 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000513 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100514
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100515 // When 'smoothscroll' is not set, should reset w_skipcol.
516 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100517 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100518 else if (curwin->w_skipcol != 0)
519 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000520
Bram Moolenaar85a20022019-12-21 18:25:54 +0100521 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (curwin->w_cursor.lnum == curwin->w_topline)
523 validate_cursor();
524 }
525
Bram Moolenaar375e3392019-01-31 18:26:10 +0100526 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000530 * Return the scrolljump value to use for the current window.
531 * When 'scrolljump' is positive use it as-is.
532 * When 'scrolljump' is negative use it as a percentage of the window height.
533 */
534 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000536{
537 if (p_sj >= 0)
538 return (int)p_sj;
539 return (curwin->w_height * -p_sj) / 100;
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
544 * current window.
545 */
546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 lineoff_T loff;
550 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100551 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar375e3392019-01-31 18:26:10 +0100553 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555 || hasAnyFolding(curwin)
556#endif
557 )
558 {
559 loff.lnum = curwin->w_cursor.lnum;
560#ifdef FEAT_DIFF
561 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100562 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#else
564 n = 0;
565#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100567 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (loff.lnum < curwin->w_topline
572#ifdef FEAT_DIFF
573 || (loff.lnum == curwin->w_topline && loff.fill > 0)
574#endif
575 )
576 break;
577 n += loff.height;
578 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100579 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return TRUE;
581 }
582 return FALSE;
583}
584
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100585/*
586 * Update w_curswant.
587 */
588 void
589update_curswant_force(void)
590{
591 validate_virtcol();
592 curwin->w_curswant = curwin->w_virtcol
593#ifdef FEAT_PROP_POPUP
594 - curwin->w_virtcol_first_char
595#endif
596 ;
597 curwin->w_set_curswant = FALSE;
598}
599
600/*
601 * Update w_curswant if w_set_curswant is set.
602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100607 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Check if the cursor has moved. Set the w_valid flag accordingly.
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
617 {
618 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100619 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
620 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 wp->w_valid_cursor = wp->w_cursor;
622 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100623 wp->w_valid_skipcol = wp->w_skipcol;
624 }
625 else if (wp->w_skipcol != wp->w_valid_skipcol)
626 {
627 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
628 |VALID_CHEIGHT|VALID_CROW
629 |VALID_BOTLINE|VALID_BOTLINE_AP);
630 wp->w_valid_cursor = wp->w_cursor;
631 wp->w_valid_leftcol = wp->w_leftcol;
632 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 else if (wp->w_cursor.col != wp->w_valid_cursor.col
635 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100636 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
639 wp->w_valid_cursor.col = wp->w_cursor.col;
640 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643}
644
645/*
646 * Call this function when some window settings have changed, which require
647 * the cursor position, botline and topline to be recomputed and the window to
648 * be redrawn. E.g, when changing the 'wrap' option or folding.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 changed_window_setting_win(curwin);
654}
655
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 wp->w_lines_valid = 0;
660 changed_line_abv_curs_win(wp);
661 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100662 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663}
664
Dominique Pellee764d1b2023-03-12 21:20:59 +0000665#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000667 * Call changed_window_setting_win() for every window containing "buf".
668 */
669 void
670changed_window_setting_buf(buf_T *buf)
671{
672 tabpage_T *tp;
673 win_T *wp;
674
675 FOR_ALL_TAB_WINDOWS(tp, wp)
676 if (wp->w_buffer == buf)
677 changed_window_setting_win(wp);
678}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000679#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000680
681/*
zeertzjq05aacec2024-04-14 18:52:49 +0200682 * Call changed_window_setting_win() for every window.
683 */
684 void
685changed_window_setting_all(void)
686{
687 tabpage_T *tp;
688 win_T *wp;
689
690 FOR_ALL_TAB_WINDOWS(tp, wp)
691 changed_window_setting_win(wp);
692}
693
694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 * Set wp->w_topline to a certain number.
696 */
697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100698set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200700#ifdef FEAT_DIFF
701 linenr_T prev_topline = wp->w_topline;
702#endif
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
707#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100710 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
711 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000713 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200715 if (lnum != prev_topline)
716 // Keep the filler lines when the topline didn't change.
717 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100720 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100721 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
724/*
725 * Call this function when the length of the cursor line (in screen
726 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100727 * If the line length changed the number of screen lines might change,
728 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * Need to take care of w_botline separately!
730 */
731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100732changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
Bram Moolenaar85090142023-06-01 19:27:08 +0100734 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 |VALID_CHEIGHT|VALID_TOPLINE);
736}
737
738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100739changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
Bram Moolenaar85090142023-06-01 19:27:08 +0100741 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 |VALID_CHEIGHT|VALID_TOPLINE);
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * Call this function when the length of a line (in screen characters) above
747 * the cursor have changed.
748 * Need to take care of w_botline separately!
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
754 |VALID_CHEIGHT|VALID_TOPLINE);
755}
756
757 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
761 |VALID_CHEIGHT|VALID_TOPLINE);
762}
763
Dominique Pellee764d1b2023-03-12 21:20:59 +0000764#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100766 * Display of line has changed for "buf", invalidate cursor position and
767 * w_botline.
768 */
769 void
770changed_line_display_buf(buf_T *buf)
771{
772 win_T *wp;
773
774 FOR_ALL_WINDOWS(wp)
775 if (wp->w_buffer == buf)
776 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
777 |VALID_CROW|VALID_CHEIGHT
778 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
779}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000780#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100781
782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Make sure the value of curwin->w_botline is valid.
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100788 validate_botline_win(curwin);
789}
790
791/*
792 * Make sure the value of wp->w_botline is valid.
793 */
794 void
795validate_botline_win(win_T *wp)
796{
797 if (!(wp->w_valid & VALID_BOTLINE))
798 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Mark curwin->w_botline as invalid (because of some change in the buffer).
803 */
804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100805invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
808}
809
810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100817approximate_botline_win(
818 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 wp->w_valid &= ~VALID_BOTLINE;
821}
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823/*
824 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
825 */
826 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100827cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
829 check_cursor_moved(curwin);
830 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
831 (VALID_WROW|VALID_WCOL));
832}
833
834/*
835 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
836 * w_topline must be valid, you may need to call update_topline() first!
837 */
838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100839validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100841 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 check_cursor_moved(curwin);
843 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
844 curs_columns(TRUE);
845}
846
847#if defined(FEAT_GUI) || defined(PROTO)
848/*
849 * validate w_cline_row.
850 */
851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 /*
855 * First make sure that w_topline is valid (after moving the cursor).
856 */
857 update_topline();
858 check_cursor_moved(curwin);
859 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100860 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862#endif
863
864/*
865 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200866 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 */
868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100869curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 linenr_T lnum;
872 int i;
873 int all_invalid;
874 int valid;
875#ifdef FEAT_FOLDING
876 long fold_count;
877#endif
878
Bram Moolenaar85a20022019-12-21 18:25:54 +0100879 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 all_invalid = (!redrawing()
881 || wp->w_lines_valid == 0
882 || wp->w_lines[0].wl_lnum > wp->w_topline);
883 i = 0;
884 wp->w_cline_row = 0;
885 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
886 {
887 valid = FALSE;
888 if (!all_invalid && i < wp->w_lines_valid)
889 {
890 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (wp->w_lines[i].wl_lnum == lnum)
893 {
894#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 // Check for newly inserted lines below this row, in which
896 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (!wp->w_buffer->b_mod_set
898 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
899 || wp->w_buffer->b_mod_top
900 > wp->w_lines[i].wl_lastlnum + 1)
901#endif
902 valid = TRUE;
903 }
904 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100908 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100910 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100912 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914#ifdef FEAT_FOLDING
915 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (lnum > wp->w_cursor.lnum)
918 break;
919#else
920 ++lnum;
921#endif
922 wp->w_cline_row += wp->w_lines[i].wl_size;
923 }
924 else
925 {
926#ifdef FEAT_FOLDING
927 fold_count = foldedCount(wp, lnum, NULL);
928 if (fold_count)
929 {
930 lnum += fold_count;
931 if (lnum > wp->w_cursor.lnum)
932 break;
933 ++wp->w_cline_row;
934 }
935 else
936#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100937 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200938 wp->w_cline_row += plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100939 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
942 }
943
944 check_cursor_moved(wp);
945 if (!(wp->w_valid & VALID_CHEIGHT))
946 {
947 if (all_invalid
948 || i == wp->w_lines_valid
949 || (i < wp->w_lines_valid
950 && (!wp->w_lines[i].wl_valid
951 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
952 {
953#ifdef FEAT_DIFF
954 if (wp->w_cursor.lnum == wp->w_topline)
955 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
956 TRUE) + wp->w_topfill;
957 else
958#endif
959 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
960#ifdef FEAT_FOLDING
961 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
962 NULL, NULL, TRUE, NULL);
963#endif
964 }
965 else if (i > wp->w_lines_valid)
966 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 wp->w_cline_height = 0;
969#ifdef FEAT_FOLDING
970 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
971 NULL, NULL, TRUE, NULL);
972#endif
973 }
974 else
975 {
976 wp->w_cline_height = wp->w_lines[i].wl_size;
977#ifdef FEAT_FOLDING
978 wp->w_cline_folded = wp->w_lines[i].wl_folded;
979#endif
980 }
981 }
982
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100983 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986
987/*
988 * Validate curwin->w_virtcol only.
989 */
990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100991validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 validate_virtcol_win(curwin);
994}
995
996/*
997 * Validate wp->w_virtcol only.
998 */
999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001003
1004 if (wp->w_valid & VALID_VIRTCOL)
1005 return;
1006
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001007#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001008 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001009#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001010 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001011#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001012 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001013#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001014 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015}
1016
1017/*
1018 * Validate curwin->w_cline_height only.
1019 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001024
1025 if (curwin->w_valid & VALID_CHEIGHT)
1026 return;
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001029 if (curwin->w_cursor.lnum == curwin->w_topline)
1030 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1031 + curwin->w_topfill;
1032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001034 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001036 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001038 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001042 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 */
1044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001045validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
1047 colnr_T off;
1048 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001049 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001053 if (curwin->w_valid & VALID_WCOL)
1054 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001055
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001056 col = curwin->w_virtcol;
1057 off = curwin_col_off();
1058 col += off;
1059 width = curwin->w_width - off + curwin_col_off2();
1060
1061 // long line wrapping, adjust curwin->w_wrow
1062 if (curwin->w_p_wrap
1063 && col >= (colnr_T)curwin->w_width
1064 && width > 0)
1065 // use same formula as what is used in curs_columns()
1066 col -= ((col - curwin->w_width) / width + 1) * width;
1067 if (col > (int)curwin->w_leftcol)
1068 col -= curwin->w_leftcol;
1069 else
1070 col = 0;
1071 curwin->w_wcol = col;
1072
1073 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001074#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001075 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001080 * Compute offset of a window, occupied by absolute or relative line number,
1081 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
1083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001084win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
Bram Moolenaar64486672010-05-16 15:46:46 +02001086 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001087 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#ifdef FEAT_FOLDING
1089 + wp->w_p_fdc
1090#endif
1091#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001092 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#endif
1094 );
1095}
1096
1097 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001098curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 return win_col_off(curwin);
1101}
1102
1103/*
1104 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001105 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1106 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
1108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001109win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
Bram Moolenaar64486672010-05-16 15:46:46 +02001111 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001112 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 return 0;
1114}
1115
1116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 return win_col_off2(curwin);
1120}
1121
1122/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001123 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 * Also updates curwin->w_wrow and curwin->w_cline_row.
1125 * Also updates curwin->w_leftcol.
1126 */
1127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001128curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int off_left, off_right;
1134 int n;
1135 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001136 int width1; // text width for first screen line
1137 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 int new_leftcol;
1139 colnr_T startcol;
1140 colnr_T endcol;
1141 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001142 long so = get_scrolloff_value();
1143 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001144 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 /*
1147 * First make sure that w_topline is valid (after moving the cursor).
1148 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001149 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 /*
1152 * Next make sure that w_cline_row is valid.
1153 */
1154 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001155 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001157#ifdef FEAT_PROP_POPUP
1158 // will be set by getvvcol() but not reset
1159 curwin->w_virtcol_first_char = 0;
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /*
1163 * Compute the number of virtual columns.
1164 */
1165#ifdef FEAT_FOLDING
1166 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001167 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1169 else
1170#endif
1171 getvvcol(curwin, &curwin->w_cursor,
1172 &startcol, &(curwin->w_virtcol), &endcol);
1173
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001176 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 extra = curwin_col_off();
1179 curwin->w_wcol = curwin->w_virtcol + extra;
1180 endcol += extra;
1181
1182 /*
1183 * Now compute w_wrow, counting screen lines from w_cline_row.
1184 */
1185 curwin->w_wrow = curwin->w_cline_row;
1186
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001187 width1 = curwin->w_width - extra;
1188 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001190 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001191 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001192 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001193 if (curwin->w_p_wrap)
1194 curwin->w_wrow = curwin->w_height - 1;
1195 else
1196 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001198 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001202 // skip columns that are not visible
1203 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001204 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001205 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001206 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001207 // Deduct by multiples of width2. This allows the long line
1208 // wrapping formula below to correctly calculate the w_wcol value
1209 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001210 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001211 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001212 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001213 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001214 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001215
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001216 did_sub_skipcol = TRUE;
1217 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001218
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001220 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001223 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1224 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 }
1228
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1230 // is not folded.
1231 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001232 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_FOLDING
1234 && !curwin->w_cline_folded
1235#endif
1236 )
1237 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001238#ifdef FEAT_PROP_POPUP
1239 if (curwin->w_virtcol_first_char > 0)
1240 {
1241 int cols = (curwin->w_width - extra);
1242 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1243
1244 // each "above" text prop shifts the text one row down
1245 curwin->w_wrow += rows;
1246 curwin->w_wcol -= rows * cols;
1247 endcol -= rows * cols;
1248 curwin->w_cline_height = rows + 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 /*
1252 * If Cursor is left of the screen, scroll rightwards.
1253 * If Cursor is right of the screen, scroll leftwards
1254 * If we get closer to the edge than 'sidescrolloff', scroll a little
1255 * extra
1256 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001257 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001258 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001259 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (off_left < 0 || off_right > 0)
1261 {
1262 if (off_left < 0)
1263 diff = -off_left;
1264 else
1265 diff = off_right;
1266
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 // When far off or not enough room on either side, put cursor in
1268 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001269 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1270 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 if (diff < p_ss)
1274 diff = p_ss;
1275 if (off_left < 0)
1276 new_leftcol = curwin->w_leftcol - diff;
1277 else
1278 new_leftcol = curwin->w_leftcol + diff;
1279 }
1280 if (new_leftcol < 0)
1281 new_leftcol = 0;
1282 if (new_leftcol != (int)curwin->w_leftcol)
1283 {
1284 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001285 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001286 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 }
1289 curwin->w_wcol -= curwin->w_leftcol;
1290 }
1291 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1292 curwin->w_wcol -= curwin->w_leftcol;
1293 else
1294 curwin->w_wcol = 0;
1295
1296#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001297 // Skip over filler lines. At the top use w_topfill, there
1298 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (curwin->w_cursor.lnum == curwin->w_topline)
1300 curwin->w_wrow += curwin->w_topfill;
1301 else
1302 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1303#endif
1304
1305 prev_skipcol = curwin->w_skipcol;
1306
1307 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if ((curwin->w_wrow >= curwin->w_height
1310 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001311 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && (p_lines =
1313#ifdef FEAT_DIFF
1314 plines_win_nofill
1315#else
1316 plines_win
1317#endif
1318 (curwin, curwin->w_cursor.lnum, FALSE))
1319 - 1 >= curwin->w_height))
1320 && curwin->w_height != 0
1321 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001323 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 // Cursor past end of screen. Happens with a single line that does
1326 // not fit on screen. Find a skipcol to show the text around the
1327 // cursor. Avoid scrolling all the time. compute value of "extra":
1328 // 1: Less than 'scrolloff' lines above
1329 // 2: Less than 'scrolloff' lines below
1330 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001332 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 // Compute last display line of the buffer line that we want at the
1335 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (p_lines == 0)
1337 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1338 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001339 if (p_lines > curwin->w_wrow + so)
1340 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 else
1342 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001343 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 extra += 2;
1345
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001346 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001349 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (n > curwin->w_height / 2)
1351 n -= curwin->w_height / 2;
1352 else
1353 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (n > p_lines - curwin->w_height + 1)
1356 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001357 if (n > 0)
1358 curwin->w_skipcol = width1 + (n - 1) * width2;
1359 else
1360 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362 else if (extra == 1)
1363 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001364 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001365 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1366 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (extra > 0)
1368 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001369 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1370 extra = curwin->w_skipcol / width2;
1371 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 }
1374 else if (extra == 2)
1375 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001376 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (endcol > curwin->w_skipcol)
1381 curwin->w_skipcol = endcol;
1382 }
1383
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001384 // adjust w_wrow for the changed w_skipcol
1385 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001386 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001387 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001388 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (curwin->w_wrow >= curwin->w_height)
1391 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001394 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 curwin->w_wrow -= extra;
1396 }
1397
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001398 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (extra > 0)
1400 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1401 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001402 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001404 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 curwin->w_skipcol = 0;
1406 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001407 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001409#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001410 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001411#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001412#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1413 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1414 {
1415 curwin->w_wrow += popup_top_extra(curwin);
1416 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001417 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001418 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001419 else
1420 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001421#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001422
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001423 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1424 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001425 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001426 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1429}
1430
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001431#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432/*
1433 * Compute the screen position of text character at "pos" in window "wp"
1434 * The resulting values are one-based, zero when character is not visible.
1435 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001436 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001437textpos2screenpos(
1438 win_T *wp,
1439 pos_T *pos,
1440 int *rowp, // screen row
1441 int *scolp, // start screen column
1442 int *ccolp, // cursor screen column
1443 int *ecolp) // end screen column
1444{
1445 colnr_T scol = 0, ccol = 0, ecol = 0;
1446 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001447 colnr_T coloff = 0;
1448
Bram Moolenaar189663b2021-07-21 18:04:56 +02001449 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 colnr_T col;
1452 int width;
1453 linenr_T lnum = pos->lnum;
1454#ifdef FEAT_FOLDING
1455 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1458#endif
Luuk van Baal32d701f2024-04-28 16:24:02 +02001459 row = plines_m_win(wp, wp->w_topline, lnum - 1, INT_MAX);
zeertzjqbfe377b2023-08-17 22:58:53 +02001460 // "row" should be the screen line where line "lnum" begins, which can
1461 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001462 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001463
1464#ifdef FEAT_DIFF
1465 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001466 row += lnum == wp->w_topline ? wp->w_topfill
1467 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001468#endif
1469
zeertzjqba2d1912022-12-18 12:28:59 +00001470 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001471#ifdef FEAT_FOLDING
1472 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001473 {
zeertzjq6235a102023-08-19 14:12:42 +02001474 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001475 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001476 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001477 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001478#endif
1479 {
1480 getvcol(wp, pos, &scol, &ccol, &ecol);
1481
1482 // similar to what is done in validate_cursor_col()
1483 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001484 col += off;
1485 width = wp->w_width - off + win_col_off2(wp);
1486
1487 // long line wrapping, adjust row
1488 if (wp->w_p_wrap
1489 && col >= (colnr_T)wp->w_width
1490 && width > 0)
1491 {
1492 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001493 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001494 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001495 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001496 }
1497 col -= wp->w_leftcol;
1498 if (col >= wp->w_width)
1499 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001500 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001501 {
1502 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001503 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001504 }
1505 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001506 // character is out of the window
1507 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001508 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001509 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001510 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001511 *scolp = scol + coloff;
1512 *ccolp = ccol + coloff;
1513 *ecolp = ecol + coloff;
1514}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001515#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001516
Bram Moolenaar12034e22019-08-25 22:25:02 +02001517#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518/*
1519 * "screenpos({winid}, {lnum}, {col})" function
1520 */
1521 void
1522f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1523{
1524 dict_T *dict;
1525 win_T *wp;
1526 pos_T pos;
1527 int row = 0;
1528 int scol = 0, ccol = 0, ecol = 0;
1529
Bram Moolenaar93a10962022-06-16 11:42:09 +01001530 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001531 return;
1532 dict = rettv->vval.v_dict;
1533
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001534 if (in_vim9script()
1535 && (check_for_number_arg(argvars, 0) == FAIL
1536 || check_for_number_arg(argvars, 1) == FAIL
1537 || check_for_number_arg(argvars, 2) == FAIL))
1538 return;
1539
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001540 wp = find_win_by_nr_or_id(&argvars[0]);
1541 if (wp == NULL)
1542 return;
1543
1544 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001545 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1546 {
1547 semsg(_(e_invalid_line_number_nr), pos.lnum);
1548 return;
1549 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001550 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001551 if (pos.col < 0)
1552 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001553 pos.coladd = 0;
1554 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1555
1556 dict_add_number(dict, "row", row);
1557 dict_add_number(dict, "col", scol);
1558 dict_add_number(dict, "curscol", ccol);
1559 dict_add_number(dict, "endcol", ecol);
1560}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001561
1562/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001563 * Convert a virtual (screen) column to a character column. The first column
1564 * is one. For a multibyte character, the column number of the first byte is
1565 * returned.
1566 */
1567 static int
1568virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1569{
zeertzjqf5a94d52023-10-15 10:03:30 +02001570 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001571 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1572 char_u *p = line + offset;
1573
zeertzjqb583eda2023-10-14 11:32:28 +02001574 if (*p == NUL)
1575 {
1576 if (p == line) // empty line
1577 return 0;
1578 // Move to the first byte of the last char.
1579 MB_PTR_BACK(line, p);
1580 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001581 return (int)(p - line + 1);
1582}
1583
1584/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001585 * "virtcol2col({winid}, {lnum}, {col})" function
1586 */
1587 void
1588f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1589{
1590 win_T *wp;
1591 linenr_T lnum;
1592 int screencol;
1593 int error = FALSE;
1594
1595 rettv->vval.v_number = -1;
1596
1597 if (check_for_number_arg(argvars, 0) == FAIL
1598 || check_for_number_arg(argvars, 1) == FAIL
1599 || check_for_number_arg(argvars, 2) == FAIL)
1600 return;
1601
1602 wp = find_win_by_nr_or_id(&argvars[0]);
1603 if (wp == NULL)
1604 return;
1605
1606 lnum = tv_get_number_chk(&argvars[1], &error);
1607 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1608 return;
1609
1610 screencol = tv_get_number_chk(&argvars[2], &error);
1611 if (error || screencol < 0)
1612 return;
1613
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001614 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001615}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001616#endif
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02001619 * Make sure the cursor is in the visible part of the topline after scrolling
1620 * the screen with 'smoothscroll'.
1621 */
1622static void cursor_correct_sms(void)
1623{
1624 if (!curwin->w_p_sms ||!curwin->w_p_wrap
1625 || curwin->w_cursor.lnum != curwin->w_topline)
1626 return;
1627
1628 long so = get_scrolloff_value();
1629 int width1 = curwin->w_width - curwin_col_off();
1630 int width2 = width1 + curwin_col_off2();
1631 int so_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1632 int space_cols = (curwin->w_height - 1) * width2;
1633 int size = so == 0 ? 0 : win_linetabsize(curwin, curwin->w_topline,
1634 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1635
1636 if (curwin->w_topline == 1 && curwin->w_skipcol == 0)
1637 so_cols = 0; // Ignore 'scrolloff' at top of buffer.
1638 else if (so_cols > space_cols / 2)
1639 so_cols = space_cols / 2; // Not enough room: put cursor in the middle.
1640
1641 // Not enough screen lines in topline: ignore 'scrolloff'.
1642 while (so_cols > size && so_cols - width2 >= width1)
1643 so_cols -= width2;
1644 if (so_cols >= width1 && so_cols > size)
1645 so_cols -= width1;
1646
1647 // If there is no marker or we have non-zero scrolloff, just ignore it.
1648 int overlap = (curwin->w_skipcol == 0 || so_cols != 0) ? 0
1649 : sms_marker_overlap(curwin, -1);
1650 int top = curwin->w_skipcol + overlap + so_cols;
1651 int bot = curwin->w_skipcol + width1 + (curwin->w_height - 1) * width2
1652 - so_cols;
1653 validate_virtcol();
1654 colnr_T col = curwin->w_virtcol;
1655
1656 if (col < top)
1657 {
1658 if (col < width1)
1659 col += width1;
1660 while (width2 > 0 && col < top)
1661 col += width2;
1662 }
1663 else
1664 while (width2 > 0 && col >= bot)
1665 col -= width2;
1666
1667 if (col != curwin->w_virtcol)
1668 {
1669 curwin->w_curswant = col;
1670 coladvance(curwin->w_curswant);
1671 // validate_virtcol() marked various things as valid, but after
1672 // moving the cursor they need to be recomputed
1673 curwin->w_valid &=
1674 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1675 }
1676}
1677
1678/*
1679 * Scroll "count" lines up or down, and redraw.
1680 */
1681 void
1682scroll_redraw(int up, long count)
1683{
1684 linenr_T prev_topline = curwin->w_topline;
1685 int prev_skipcol = curwin->w_skipcol;
1686#ifdef FEAT_DIFF
1687 int prev_topfill = curwin->w_topfill;
1688#endif
1689 linenr_T prev_lnum = curwin->w_cursor.lnum;
1690
1691 if (up)
1692 scrollup(count, TRUE);
1693 else
1694 scrolldown(count, TRUE);
1695 if (get_scrolloff_value() > 0)
1696 {
1697 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
1698 // valid, otherwise the screen jumps back at the end of the file.
1699 cursor_correct();
1700 check_cursor_moved(curwin);
1701 curwin->w_valid |= VALID_TOPLINE;
1702
1703 // If moved back to where we were, at least move the cursor, otherwise
1704 // we get stuck at one position. Don't move the cursor up if the
1705 // first line of the buffer is already on the screen
1706 while (curwin->w_topline == prev_topline
1707 && curwin->w_skipcol == prev_skipcol
1708#ifdef FEAT_DIFF
1709 && curwin->w_topfill == prev_topfill
1710#endif
1711 )
1712 {
1713 if (up)
1714 {
1715 if (curwin->w_cursor.lnum > prev_lnum
1716 || cursor_down(1L, FALSE) == FAIL)
1717 break;
1718 }
1719 else
1720 {
1721 if (curwin->w_cursor.lnum < prev_lnum
1722 || prev_topline == 1L
1723 || cursor_up(1L, FALSE) == FAIL)
1724 break;
1725 }
1726 // Mark w_topline as valid, otherwise the screen jumps back at the
1727 // end of the file.
1728 check_cursor_moved(curwin);
1729 curwin->w_valid |= VALID_TOPLINE;
1730 }
1731 }
1732
1733 cursor_correct_sms();
1734 if (curwin->w_cursor.lnum != prev_lnum)
1735 coladvance(curwin->w_curswant);
1736 redraw_later(UPD_VALID);
1737}
1738
1739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001743scrolldown(
1744 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001745 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001747 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 int wrow;
1749 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001750 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001751 int width1 = 0;
1752 int width2 = 0;
1753
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001754 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001755 {
1756 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001757 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760#ifdef FEAT_FOLDING
1761 linenr_T first;
1762
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1765#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001766 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001767 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001770 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1771 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
1773 ++curwin->w_topfill;
1774 ++done;
1775 }
1776 else
1777#endif
1778 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001779 // break when at the very top
1780 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001781 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001783 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001785 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001786 if (curwin->w_skipcol >= width1 + width2)
1787 curwin->w_skipcol -= width2;
1788 else
1789 curwin->w_skipcol -= width1;
1790 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001794 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001795 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001796 --curwin->w_topline;
1797 curwin->w_skipcol = 0;
1798#ifdef FEAT_DIFF
1799 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001801#ifdef FEAT_FOLDING
1802 // A sequence of folded lines only counts for one logical line
1803 if (hasFolding(curwin->w_topline, &first, NULL))
1804 {
1805 ++done;
1806 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001807 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001808 curwin->w_botline -= curwin->w_topline - first;
1809 curwin->w_topline = first;
1810 }
1811 else
1812#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001813 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001814 {
1815 int size = win_linetabsize(curwin, curwin->w_topline,
1816 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1817 if (size > width1)
1818 {
1819 curwin->w_skipcol = width1;
1820 size -= width1;
1821 redraw_later(UPD_NOT_VALID);
1822 }
1823 while (size > width2)
1824 {
1825 curwin->w_skipcol += width2;
1826 size -= width2;
1827 }
1828 ++done;
1829 }
1830 else
1831 done += PLINES_NOFILL(curwin->w_topline);
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001834 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 invalidate_botline();
1836 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001837 curwin->w_wrow += done; // keep w_wrow updated
1838 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840#ifdef FEAT_DIFF
1841 if (curwin->w_cursor.lnum == curwin->w_topline)
1842 curwin->w_cline_row = 0;
1843 check_topfill(curwin, TRUE);
1844#endif
1845
1846 /*
1847 * Compute the row number of the last row of the cursor line
1848 * and move the cursor onto the displayed part of the window.
1849 */
1850 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001851 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 validate_virtcol();
1854 validate_cheight();
1855 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001856 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1859 {
1860#ifdef FEAT_FOLDING
1861 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1862 {
1863 --wrow;
1864 if (first == 1)
1865 curwin->w_cursor.lnum = 1;
1866 else
1867 curwin->w_cursor.lnum = first - 1;
1868 }
1869 else
1870#endif
1871 wrow -= plines(curwin->w_cursor.lnum--);
1872 curwin->w_valid &=
1873 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1874 moved = TRUE;
1875 }
1876 if (moved)
1877 {
1878#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001879 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 foldAdjustCursor();
1881#endif
1882 coladvance(curwin->w_curswant);
1883 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001884 if (curwin->w_cursor.lnum < curwin->w_topline)
1885 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886}
1887
1888/*
1889 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001892scrollup(
1893 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001894 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001896 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001898 if (do_sms
1899# ifdef FEAT_FOLDING
1900 || (byfold && hasAnyFolding(curwin))
1901# endif
1902# ifdef FEAT_DIFF
1903 || (curwin->w_p_diff && !curwin->w_p_wrap)
1904# endif
1905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001907 int width1 = curwin->w_width - curwin_col_off();
1908 int width2 = width1 + curwin_col_off2();
1909 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001910 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001911
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001912 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001913 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001914
1915 // diff mode: first consume "topfill"
1916 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1917 // the line, then advance to the next line.
1918 // folding: count each sequence of folded lines as one logical line.
1919 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921# ifdef FEAT_DIFF
1922 if (curwin->w_topfill > 0)
1923 --curwin->w_topfill;
1924 else
1925# endif
1926 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001927 linenr_T lnum = curwin->w_topline;
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929# ifdef FEAT_FOLDING
1930 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001931 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 (void)hasFolding(lnum, NULL, &lnum);
1933# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001934 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001935 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001936 // 'smoothscroll': increase "w_skipcol" until it goes over
1937 // the end of the line, then advance to the next line.
1938 int add = curwin->w_skipcol > 0 ? width2 : width1;
1939 curwin->w_skipcol += add;
1940 if (curwin->w_skipcol >= size)
1941 {
1942 if (lnum == curbuf->b_ml.ml_line_count)
1943 {
1944 // at the last screen line, can't scroll further
1945 curwin->w_skipcol -= add;
1946 break;
1947 }
1948 ++lnum;
1949 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001950 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001951 else
1952 {
1953 if (lnum >= curbuf->b_ml.ml_line_count)
1954 break;
1955 ++lnum;
1956 }
1957
1958 if (lnum > curwin->w_topline)
1959 {
1960 // approximate w_botline
1961 curwin->w_botline += lnum - curwin->w_topline;
1962 curwin->w_topline = lnum;
1963# ifdef FEAT_DIFF
1964 curwin->w_topfill = diff_check_fill(curwin, lnum);
1965# endif
1966 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001967 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001968 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001969 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001970 }
1971 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001972
zeertzjqd9a92dc2023-06-05 18:41:35 +01001973 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1974 // need to redraw more, because wl_size of the (new) topline may
1975 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001976 redraw_later(UPD_NOT_VALID);
1977 }
1978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001981 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
1984 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1985 curwin->w_topline = curbuf->b_ml.ml_line_count;
1986 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1987 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1988
1989#ifdef FEAT_DIFF
1990 check_topfill(curwin, FALSE);
1991#endif
1992
1993#ifdef FEAT_FOLDING
1994 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001995 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1997#endif
1998
1999 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2000 if (curwin->w_cursor.lnum < curwin->w_topline)
2001 {
2002 curwin->w_cursor.lnum = curwin->w_topline;
2003 curwin->w_valid &=
2004 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
2005 coladvance(curwin->w_curswant);
2006 }
2007}
2008
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002009/*
2010 * Called after changing the cursor column: make sure that curwin->w_skipcol is
2011 * valid for 'smoothscroll'.
2012 */
2013 void
2014adjust_skipcol(void)
2015{
2016 if (!curwin->w_p_wrap
2017 || !curwin->w_p_sms
2018 || curwin->w_cursor.lnum != curwin->w_topline)
2019 return;
2020
2021 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00002022 if (width1 <= 0)
2023 return; // no text will be displayed
2024
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002025 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002026 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002027 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
2028 int scrolled = FALSE;
2029
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002030 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00002031 if (curwin->w_cline_height == curwin->w_height
2032 // w_cline_height may be capped at w_height, check there aren't
2033 // actually more lines.
2034 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
2035 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002036 {
2037 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002038 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002039 return;
2040 }
2041
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002042 validate_virtcol();
Luuk van Baalcb204e62024-04-02 20:49:45 +02002043 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002044 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01002045 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002046 {
2047 // scroll a screen line down
2048 if (curwin->w_skipcol >= width1 + width2)
2049 curwin->w_skipcol -= width2;
2050 else
2051 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002052 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002053 }
2054 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01002055 {
2056 validate_virtcol();
2057 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002058 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002059 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002060
2061 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2062 int row = 0;
2063 if (col >= width1)
2064 {
2065 col -= width1;
2066 ++row;
2067 }
2068 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002069 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002070 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002071 // col may no longer be used, but make
2072 // sure it is correct anyhow, just in case
2073 col = col % width2;
2074 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002075 if (row >= curwin->w_height)
2076 {
2077 if (curwin->w_skipcol == 0)
2078 {
2079 curwin->w_skipcol += width1;
2080 --row;
2081 }
2082 if (row >= curwin->w_height)
2083 curwin->w_skipcol += (row - curwin->w_height) * width2;
2084 redraw_later(UPD_NOT_VALID);
2085 }
2086}
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#ifdef FEAT_DIFF
2089/*
2090 * Don't end up with too many filler lines in the window.
2091 */
2092 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002093check_topfill(
2094 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002095 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 int n;
2098
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002099 if (wp->w_topfill <= 0)
2100 return;
2101
2102 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2103 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002105 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002107 --wp->w_topline;
2108 wp->w_topfill = 0;
2109 }
2110 else
2111 {
2112 wp->w_topfill = wp->w_height - n;
2113 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 }
2117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#endif
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120/*
2121 * Scroll the screen one line down, but don't do it if it would move the
2122 * cursor off the screen.
2123 */
2124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002125scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int end_row;
2128#ifdef FEAT_DIFF
2129 int can_fill = (curwin->w_topfill
2130 < diff_check_fill(curwin, curwin->w_topline));
2131#endif
2132
2133 if (curwin->w_topline <= 1
2134#ifdef FEAT_DIFF
2135 && !can_fill
2136#endif
2137 )
2138 return;
2139
Bram Moolenaar85a20022019-12-21 18:25:54 +01002140 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
2143 * Compute the row number of the last row of the cursor line
2144 * and make sure it doesn't go off the screen. Make sure the cursor
2145 * doesn't go past 'scrolloff' lines from the screen end.
2146 */
2147 end_row = curwin->w_wrow;
2148#ifdef FEAT_DIFF
2149 if (can_fill)
2150 ++end_row;
2151 else
2152 end_row += plines_nofill(curwin->w_topline - 1);
2153#else
2154 end_row += plines(curwin->w_topline - 1);
2155#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002156 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 validate_cheight();
2159 validate_virtcol();
2160 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002161 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002163 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165#ifdef FEAT_DIFF
2166 if (can_fill)
2167 {
2168 ++curwin->w_topfill;
2169 check_topfill(curwin, TRUE);
2170 }
2171 else
2172 {
2173 --curwin->w_topline;
2174 curwin->w_topfill = 0;
2175 }
2176#else
2177 --curwin->w_topline;
2178#endif
2179#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002180 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#endif
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}
2186
2187/*
2188 * Scroll the screen one line up, but don't do it if it would move the cursor
2189 * off the screen.
2190 */
2191 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002192scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 int start_row;
2195
2196 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2197#ifdef FEAT_DIFF
2198 && curwin->w_topfill == 0
2199#endif
2200 )
2201 return;
2202
Bram Moolenaar85a20022019-12-21 18:25:54 +01002203 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 /*
2206 * Compute the row number of the first row of the cursor line
2207 * and make sure it doesn't go off the screen. Make sure the cursor
2208 * doesn't go before 'scrolloff' lines from the screen start.
2209 */
2210#ifdef FEAT_DIFF
2211 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2212 - curwin->w_topfill;
2213#else
2214 start_row = curwin->w_wrow - plines(curwin->w_topline);
2215#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002216 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002219 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002221 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223#ifdef FEAT_DIFF
2224 if (curwin->w_topfill > 0)
2225 --curwin->w_topfill;
2226 else
2227#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002228 {
2229#ifdef FEAT_FOLDING
2230 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002233 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2236 }
2237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239/*
2240 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2241 * a (wrapped) text line. Uses and sets "lp->fill".
2242 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002243 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
2245 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002246topline_back_winheight(
2247 lineoff_T *lp,
2248 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250#ifdef FEAT_DIFF
2251 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2252 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 ++lp->fill;
2255 lp->height = 1;
2256 }
2257 else
2258#endif
2259 {
2260 --lp->lnum;
2261#ifdef FEAT_DIFF
2262 lp->fill = 0;
2263#endif
2264 if (lp->lnum < 1)
2265 lp->height = MAXCOL;
2266 else
2267#ifdef FEAT_FOLDING
2268 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 lp->height = 1;
2271 else
2272#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002273 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275}
2276
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002277 static void
2278topline_back(lineoff_T *lp)
2279{
2280 topline_back_winheight(lp, TRUE);
2281}
2282
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284/*
2285 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2286 * a (wrapped) text line. Uses and sets "lp->fill".
2287 * Returns the height of the added line in "lp->height".
2288 * Lines below the last one are incredibly high.
2289 */
2290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293#ifdef FEAT_DIFF
2294 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002296 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ++lp->fill;
2298 lp->height = 1;
2299 }
2300 else
2301#endif
2302 {
2303 ++lp->lnum;
2304#ifdef FEAT_DIFF
2305 lp->fill = 0;
2306#endif
2307 if (lp->lnum > curbuf->b_ml.ml_line_count)
2308 lp->height = MAXCOL;
2309 else
2310#ifdef FEAT_FOLDING
2311 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 lp->height = 1;
2314 else
2315#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002316 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318}
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320/*
2321 * Recompute topline to put the cursor at the top of the window.
2322 * Scroll at least "min_scroll" lines.
2323 * If "always" is TRUE, always set topline (for "zt").
2324 */
2325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002326scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 int scrolled = 0;
2329 int extra = 0;
2330 int used;
2331 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 linenr_T top; // just above displayed lines
2333 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002335 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#ifdef FEAT_DIFF
2337 linenr_T old_topfill = curwin->w_topfill;
2338#endif
2339 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002340 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (mouse_dragging > 0)
2343 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 /*
2346 * Decrease topline until:
2347 * - it has become 1
2348 * - (part of) the cursor line is moved off the screen or
2349 * - moved at least 'scrolljump' lines and
2350 * - at least 'scrolloff' lines above and below the cursor
2351 */
2352 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002353 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (curwin->w_cursor.lnum < curwin->w_topline)
2355 scrolled = used;
2356
2357#ifdef FEAT_FOLDING
2358 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2359 {
2360 --top;
2361 ++bot;
2362 }
2363 else
2364#endif
2365 {
2366 top = curwin->w_cursor.lnum - 1;
2367 bot = curwin->w_cursor.lnum + 1;
2368 }
2369 new_topline = top + 1;
2370
2371#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002372 // "used" already contains the number of filler lines above, don't add it
2373 // again.
2374 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002375 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#endif
2377
2378 /*
2379 * Check if the lines from "top" to "bot" fit in the window. If they do,
2380 * set new_topline and advance "top" and "bot" to include more lines.
2381 */
2382 while (top > 0)
2383 {
2384#ifdef FEAT_FOLDING
2385 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002386 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 i = 1;
2388 else
2389#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002390 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002391 if (top < curwin->w_topline)
2392 scrolled += i;
2393
2394 // If scrolling is needed, scroll at least 'sj' lines.
2395 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2396 && extra >= off)
2397 break;
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 used += i;
2400 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2401 {
2402#ifdef FEAT_FOLDING
2403 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002404 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 ++used;
2406 else
2407#endif
2408 used += plines(bot);
2409 }
2410 if (used > curwin->w_height)
2411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 extra += i;
2414 new_topline = top;
2415 --top;
2416 ++bot;
2417 }
2418
2419 /*
2420 * If we don't have enough space, put cursor in the middle.
2421 * This makes sure we get the same position when using "k" and "j"
2422 * in a small window.
2423 */
2424 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002425 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 else
2427 {
2428 /*
2429 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002430 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 */
2432 if (new_topline < curwin->w_topline || always)
2433 curwin->w_topline = new_topline;
2434 if (curwin->w_topline > curwin->w_cursor.lnum)
2435 curwin->w_topline = curwin->w_cursor.lnum;
2436#ifdef FEAT_DIFF
2437 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2438 if (curwin->w_topfill > 0 && extra > off)
2439 {
2440 curwin->w_topfill -= extra - off;
2441 if (curwin->w_topfill < 0)
2442 curwin->w_topfill = 0;
2443 }
2444 check_topfill(curwin, FALSE);
2445#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002446 if (curwin->w_topline != old_topline)
2447 reset_skipcol();
2448 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002449 {
2450 validate_virtcol();
2451 if (curwin->w_skipcol >= curwin->w_virtcol)
2452 // TODO: if the line doesn't fit may optimize w_skipcol instead
2453 // of making it zero
2454 reset_skipcol();
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002457 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458#ifdef FEAT_DIFF
2459 || curwin->w_topfill != old_topfill
2460#endif
2461 )
2462 curwin->w_valid &=
2463 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2464 curwin->w_valid |= VALID_TOPLINE;
2465 }
2466}
2467
2468/*
2469 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2470 * screen lines for text lines.
2471 */
2472 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002473set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475#ifdef FEAT_DIFF
2476 wp->w_filler_rows = 0;
2477#endif
2478 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002479 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 else
2481 {
2482 wp->w_empty_rows = wp->w_height - used;
2483#ifdef FEAT_DIFF
2484 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2485 {
2486 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2487 if (wp->w_empty_rows > wp->w_filler_rows)
2488 wp->w_empty_rows -= wp->w_filler_rows;
2489 else
2490 {
2491 wp->w_filler_rows = wp->w_empty_rows;
2492 wp->w_empty_rows = 0;
2493 }
2494 }
2495#endif
2496 }
2497}
2498
2499/*
2500 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002501 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2503 * This is messy stuff!!!
2504 */
2505 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002506scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 int used;
2509 int scrolled = 0;
2510 int extra = 0;
2511 int i;
2512 linenr_T line_count;
2513 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002514 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 lineoff_T loff;
2516 lineoff_T boff;
2517#ifdef FEAT_DIFF
2518 int old_topfill = curwin->w_topfill;
2519 int fill_below_window;
2520#endif
2521 linenr_T old_botline = curwin->w_botline;
2522 linenr_T old_valid = curwin->w_valid;
2523 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002524 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002525 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002526 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 cln = curwin->w_cursor.lnum;
2529 if (set_topbot)
2530 {
2531 used = 0;
2532 curwin->w_botline = cln + 1;
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002533 loff.lnum = cln + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#ifdef FEAT_DIFF
2535 loff.fill = 0;
2536#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002537 while (TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002539 topline_back_winheight(&loff, FALSE);
2540 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002542 if (used + loff.height > curwin->w_height)
2543 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002544 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002545 {
2546 // 'smoothscroll' and 'wrap' are set. The above line is
2547 // too long to show in its entirety, so we show just a part
2548 // of it.
2549 if (used < curwin->w_height)
2550 {
2551 int plines_offset = used + loff.height
2552 - curwin->w_height;
2553 used = curwin->w_height;
2554#ifdef FEAT_DIFF
2555 curwin->w_topfill = loff.fill;
2556#endif
2557 curwin->w_topline = loff.lnum;
2558 curwin->w_skipcol = skipcol_from_plines(
2559 curwin, plines_offset);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002560 }
2561 }
2562 break;
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#ifdef FEAT_DIFF
2565 curwin->w_topfill = loff.fill;
2566#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002567 curwin->w_topline = loff.lnum;
2568 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 set_empty_rows(curwin, used);
2572 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2573 if (curwin->w_topline != old_topline
2574#ifdef FEAT_DIFF
2575 || curwin->w_topfill != old_topfill
2576#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002577 || curwin->w_skipcol != old_skipcol
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002578 || curwin->w_skipcol != 0)
2579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002581 if (curwin->w_skipcol != old_skipcol)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002582 redraw_later(UPD_NOT_VALID);
2583 else
2584 reset_skipcol();
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 else
2588 validate_botline();
2589
Bram Moolenaar85a20022019-12-21 18:25:54 +01002590 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#ifdef FEAT_DIFF
2592 used = plines_nofill(cln);
2593#else
2594 validate_cheight();
2595 used = curwin->w_cline_height;
2596#endif
2597
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002598 // If the cursor is on or below botline, we will at least scroll by the
2599 // height of the cursor line, which is "used". Correct for empty lines,
2600 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (cln >= curwin->w_botline)
2602 {
2603 scrolled = used;
2604 if (cln == curwin->w_botline)
2605 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002606 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002607 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002608 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002609 // Calculate how many screen lines the current top line of window
2610 // occupies. If it is occupying more than the entire window, we
2611 // need to scroll the additional clipped lines to scroll past the
2612 // top line before we can move on to the other lines.
2613 int top_plines =
2614#ifdef FEAT_DIFF
2615 plines_win_nofill
2616#else
2617 plines_win
2618#endif
2619 (curwin, curwin->w_topline, FALSE);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002620 int width1 = curwin->w_width - curwin_col_off();
zeertzjq031a7452024-05-11 11:23:37 +02002621
fullwaywang8154e642023-06-24 21:58:09 +01002622 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002623 {
fullwaywang8154e642023-06-24 21:58:09 +01002624 int width2 = width1 + curwin_col_off2();
zeertzjq031a7452024-05-11 11:23:37 +02002625 int skip_lines = 0;
2626
2627 // A similar formula is used in curs_columns().
fullwaywang8154e642023-06-24 21:58:09 +01002628 if (curwin->w_skipcol > width1)
2629 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2630 else if (curwin->w_skipcol > 0)
2631 skip_lines = 1;
2632
2633 top_plines -= skip_lines;
2634 if (top_plines > curwin->w_height)
2635 {
2636 scrolled += (top_plines - curwin->w_height);
2637 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002638 }
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641
2642 /*
2643 * Stop counting lines to scroll when
2644 * - hitting start of the file
2645 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002646 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * - lines between botline and cursor have been counted
2648 */
2649#ifdef FEAT_FOLDING
2650 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2651#endif
2652 {
2653 loff.lnum = cln;
2654 boff.lnum = cln;
2655 }
2656#ifdef FEAT_DIFF
2657 loff.fill = 0;
2658 boff.fill = 0;
2659 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2660 - curwin->w_filler_rows;
2661#endif
2662
2663 while (loff.lnum > 1)
2664 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002665 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2666 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002668 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2670 && loff.lnum <= curwin->w_botline
2671#ifdef FEAT_DIFF
2672 && (loff.lnum < curwin->w_botline
2673 || loff.fill >= fill_below_window)
2674#endif
2675 )
2676 break;
2677
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002680 if (loff.height == MAXCOL)
2681 used = MAXCOL;
2682 else
2683 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (used > curwin->w_height)
2685 break;
2686 if (loff.lnum >= curwin->w_botline
2687#ifdef FEAT_DIFF
2688 && (loff.lnum > curwin->w_botline
2689 || loff.fill <= fill_below_window)
2690#endif
2691 )
2692 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 scrolled += loff.height;
2695 if (loff.lnum == curwin->w_botline
2696#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002697 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#endif
2699 )
2700 scrolled -= curwin->w_empty_rows;
2701 }
2702
2703 if (boff.lnum < curbuf->b_ml.ml_line_count)
2704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 botline_forw(&boff);
2707 used += boff.height;
2708 if (used > curwin->w_height)
2709 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002710 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2711 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
2713 extra += boff.height;
2714 if (boff.lnum >= curwin->w_botline
2715#ifdef FEAT_DIFF
2716 || (boff.lnum + 1 == curwin->w_botline
2717 && boff.fill > curwin->w_filler_rows)
2718#endif
2719 )
2720 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 scrolled += boff.height;
2723 if (boff.lnum == curwin->w_botline
2724#ifdef FEAT_DIFF
2725 && boff.fill == 0
2726#endif
2727 )
2728 scrolled -= curwin->w_empty_rows;
2729 }
2730 }
2731 }
2732 }
2733
Bram Moolenaar85a20022019-12-21 18:25:54 +01002734 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (scrolled <= 0)
2736 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002737 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 else if (used > curwin->w_height)
2739 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002740 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 else
2742 {
2743 line_count = 0;
2744#ifdef FEAT_DIFF
2745 boff.fill = curwin->w_topfill;
2746#endif
2747 boff.lnum = curwin->w_topline - 1;
2748 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2749 {
2750 botline_forw(&boff);
2751 i += boff.height;
2752 ++line_count;
2753 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002754 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 line_count = 9999;
2756 }
2757
2758 /*
2759 * Scroll up if the cursor is off the bottom of the screen a bit.
2760 * Otherwise put it at 1/2 of the screen.
2761 */
2762 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002763 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002764 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002765 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002766 if (do_sms)
2767 scrollup(scrolled, TRUE); // TODO
2768 else
2769 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 /*
2773 * If topline didn't change we need to restore w_botline and w_empty_rows
2774 * (we changed them).
2775 * If topline did change, update_screen() will set botline.
2776 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002777 if (curwin->w_topline == old_topline
2778 && curwin->w_skipcol == old_skipcol
2779 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
2781 curwin->w_botline = old_botline;
2782 curwin->w_empty_rows = old_empty_rows;
2783 curwin->w_valid = old_valid;
2784 }
2785 curwin->w_valid |= VALID_TOPLINE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02002786
2787 cursor_correct_sms();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788}
2789
2790/*
2791 * Recompute topline to put the cursor halfway the window
2792 * If "atend" is TRUE, also put it halfway at the end of the file.
2793 */
2794 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002795scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
2797 int above = 0;
2798 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002799 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800#ifdef FEAT_DIFF
2801 int topfill = 0;
2802#endif
2803 int below = 0;
2804 int used;
2805 lineoff_T loff;
2806 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002807#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002808 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002811#ifdef FEAT_PROP_POPUP
2812 // if the width changed this needs to be updated first
2813 may_update_popup_position();
2814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2816#ifdef FEAT_FOLDING
2817 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2818#endif
2819#ifdef FEAT_DIFF
2820 used = plines_nofill(loff.lnum);
2821 loff.fill = 0;
2822 boff.fill = 0;
2823#else
2824 used = plines(loff.lnum);
2825#endif
2826 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002827
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002828 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002829 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2830 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002831 {
2832 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002833 if (atend)
2834 {
2835 want_height = (curwin->w_height - used) / 2;
2836 used = 0;
2837 }
2838 else
2839 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002840 }
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 while (topline > 1)
2843 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002844 // If using smoothscroll, we can precisely scroll to the
2845 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002846 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002847 {
2848 topline_back_winheight(&loff, FALSE);
2849 if (loff.height == MAXCOL)
2850 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002851 used += loff.height;
2852 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002853 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002854 botline_forw(&boff);
2855 used += boff.height;
2856 }
2857 if (used > want_height)
2858 {
2859 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002860 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002861 topline = loff.lnum;
2862#ifdef FEAT_DIFF
2863 topfill = loff.fill;
2864#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002865 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002866 }
2867 break;
2868 }
2869 topline = loff.lnum;
2870#ifdef FEAT_DIFF
2871 topfill = loff.fill;
2872#endif
2873 continue;
2874 }
2875
2876 // If not using smoothscroll, we have to iteratively find how many
2877 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002878 // This may not be right in the middle if the lines'
2879 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002880
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002881 // Depending on "prefer_above" we add a line above or below first.
2882 // Loop twice to avoid duplicating code.
2883 int done = FALSE;
2884 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002886 if (prefer_above ? (round == 2 && below < above)
2887 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002889 // add a line below the cursor
2890 if (boff.lnum < curbuf->b_ml.ml_line_count)
2891 {
2892 botline_forw(&boff);
2893 used += boff.height;
2894 if (used > curwin->w_height)
2895 {
2896 done = TRUE;
2897 break;
2898 }
2899 below += boff.height;
2900 }
2901 else
2902 {
2903 ++below; // count a "~" line
2904 if (atend)
2905 ++used;
2906 }
2907 }
2908
2909 if (prefer_above ? (round == 1 && below >= above)
2910 : (round == 1 && below > above))
2911 {
2912 // add a line above the cursor
2913 topline_back(&loff);
2914 if (loff.height == MAXCOL)
2915 used = MAXCOL;
2916 else
2917 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002919 {
2920 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002922 }
2923 above += loff.height;
2924 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002926 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002930 if (done)
2931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#ifdef FEAT_FOLDING
2935 if (!hasFolding(topline, &curwin->w_topline, NULL))
2936#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002937 {
2938 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002939 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002940 || curwin->w_skipcol != 0)
2941 {
2942 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002943 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002944 {
2945 curwin->w_skipcol = skipcol;
2946 redraw_later(UPD_NOT_VALID);
2947 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002948 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002949 reset_skipcol();
2950 }
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#ifdef FEAT_DIFF
2953 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002954 if (old_topline > curwin->w_topline + curwin->w_height)
2955 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 check_topfill(curwin, FALSE);
2957#endif
2958 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2959 curwin->w_valid |= VALID_TOPLINE;
2960}
2961
2962/*
2963 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002964 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 * If not possible, put it at the same position as scroll_cursor_halfway().
2966 * When called topline must be valid!
2967 */
2968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002969cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002971 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002973 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 linenr_T botline;
2975 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002976 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002978 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /*
2981 * How many lines we would like to have above/below the cursor depends on
2982 * whether the first/last line of the file is on screen.
2983 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002984 above_wanted = so;
2985 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002986 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 above_wanted = mouse_dragging - 1;
2989 below_wanted = mouse_dragging - 1;
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (curwin->w_topline == 1)
2992 {
2993 above_wanted = 0;
2994 max_off = curwin->w_height / 2;
2995 if (below_wanted > max_off)
2996 below_wanted = max_off;
2997 }
2998 validate_botline();
2999 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003000 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
3002 below_wanted = 0;
3003 max_off = (curwin->w_height - 1) / 2;
3004 if (above_wanted > max_off)
3005 above_wanted = max_off;
3006 }
3007
3008 /*
3009 * If there are sufficient file-lines above and below the cursor, we can
3010 * return now.
3011 */
3012 cln = curwin->w_cursor.lnum;
3013 if (cln >= curwin->w_topline + above_wanted
3014 && cln < curwin->w_botline - below_wanted
3015#ifdef FEAT_FOLDING
3016 && !hasAnyFolding(curwin)
3017#endif
3018 )
3019 return;
3020
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003021 if (curwin->w_p_sms && !curwin->w_p_wrap)
3022 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003023 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003024 if (curwin->w_cline_height == curwin->w_height)
3025 {
3026 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003027 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003028 return;
3029 }
3030 // TODO: If the cursor line doesn't fit in the window then only adjust
3031 // w_skipcol.
3032 }
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 /*
3035 * Narrow down the area where the cursor can be put by taking lines from
3036 * the top and the bottom until:
3037 * - the desired context lines are found
3038 * - the lines from the top is past the lines from the bottom
3039 */
3040 topline = curwin->w_topline;
3041 botline = curwin->w_botline - 1;
3042#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003043 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 above = curwin->w_topfill;
3045 below = curwin->w_filler_rows;
3046#endif
3047 while ((above < above_wanted || below < below_wanted) && topline < botline)
3048 {
3049 if (below < below_wanted && (below <= above || above >= above_wanted))
3050 {
3051#ifdef FEAT_FOLDING
3052 if (hasFolding(botline, &botline, NULL))
3053 ++below;
3054 else
3055#endif
3056 below += plines(botline);
3057 --botline;
3058 }
3059 if (above < above_wanted && (above < below || below >= below_wanted))
3060 {
3061#ifdef FEAT_FOLDING
3062 if (hasFolding(topline, NULL, &topline))
3063 ++above;
3064 else
3065#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003066 above += PLINES_NOFILL(topline);
3067#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003068 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (topline < botline)
3070 above += diff_check_fill(curwin, topline + 1);
3071#endif
3072 ++topline;
3073 }
3074 }
3075 if (topline == botline || botline == 0)
3076 curwin->w_cursor.lnum = topline;
3077 else if (topline > botline)
3078 curwin->w_cursor.lnum = botline;
3079 else
3080 {
3081 if (cln < topline && curwin->w_topline > 1)
3082 {
3083 curwin->w_cursor.lnum = topline;
3084 curwin->w_valid &=
3085 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3086 }
3087 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3088 {
3089 curwin->w_cursor.lnum = botline;
3090 curwin->w_valid &=
3091 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3092 }
3093 }
3094 curwin->w_valid |= VALID_TOPLINE;
3095}
3096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003098 * Decide how much overlap to use for page-up or page-down scrolling.
3099 * This is symmetric, so that doing both keeps the same lines displayed.
3100 * Three lines are examined:
3101 *
3102 * before CTRL-F after CTRL-F / before CTRL-B
3103 * etc. l1
3104 * l1 last but one line ------------
3105 * l2 last text line l2 top text line
3106 * ------------- l3 second text line
3107 * l3 etc.
3108 */
3109static int get_scroll_overlap(int dir)
3110{
3111 lineoff_T loff;
3112 int min_height = curwin->w_height - 2;
3113
3114 validate_botline();
Luuk van Baalbd28cae2024-04-03 22:50:40 +02003115 if ((dir == BACKWARD && curwin->w_topline == 1)
3116 || (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003117 return min_height + 2; // no overlap, still handle 'smoothscroll'
3118
3119 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3120#ifdef FEAT_DIFF
zeertzjq92325542024-04-12 18:38:38 +02003121 loff.fill = diff_check_fill(curwin, loff.lnum + (dir == BACKWARD))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003122 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3123 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3124#else
3125 loff.height = plines(loff.lnum);
3126#endif
3127
3128 int h1 = loff.height;
3129 if (h1 > min_height)
3130 return min_height + 2; // no overlap
3131 if (dir == FORWARD)
3132 topline_back(&loff);
3133 else
3134 botline_forw(&loff);
3135
3136 int h2 = loff.height;
3137 if (h2 == MAXCOL || h2 + h1 > min_height)
3138 return min_height + 2; // no overlap
3139 if (dir == FORWARD)
3140 topline_back(&loff);
3141 else
3142 botline_forw(&loff);
3143
3144 int h3 = loff.height;
3145 if (h3 == MAXCOL || h3 + h2 > min_height)
3146 return min_height + 2; // no overlap
3147 if (dir == FORWARD)
3148 topline_back(&loff);
3149 else
3150 botline_forw(&loff);
3151
3152 int h4 = loff.height;
3153 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3154 return min_height + 1; // 1 line overlap
3155 else
3156 return min_height; // 2 lines overlap
3157}
3158
3159/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02003160 * Scroll "count" lines with 'smoothscroll' in direction "dir". Return TRUE
3161 * when scrolling happened.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003162 */
Luuk van Baal9148ba82024-04-08 22:27:41 +02003163static int scroll_with_sms(int dir, long count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003164{
3165 int prev_sms = curwin->w_p_sms;
3166 colnr_T prev_skipcol = curwin->w_skipcol;
3167 linenr_T prev_topline = curwin->w_topline;
3168#ifdef FEAT_DIFF
3169 int prev_topfill = curwin->w_topfill;
3170#endif
3171
3172 curwin->w_p_sms = TRUE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003173 scroll_redraw(dir == FORWARD, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003174
3175 // Not actually smoothscrolling but ended up with partially visible line.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003176 // Continue scrolling until skipcol is zero.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003177 if (!prev_sms && curwin->w_skipcol > 0)
3178 {
3179 int fixdir = dir;
3180 // Reverse the scroll direction when topline already changed. One line
3181 // extra for scrolling backward so that consuming skipcol is symmetric.
3182 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3183 fixdir = dir * -1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003184 while (curwin->w_skipcol > 0
3185 && curwin->w_topline < curbuf->b_ml.ml_line_count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003186 scroll_redraw(fixdir == FORWARD, 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003187 }
3188 curwin->w_p_sms = prev_sms;
3189
3190 return curwin->w_topline == prev_topline
3191#ifdef FEAT_DIFF
3192 && curwin->w_topfill == prev_topfill
3193#endif
3194 && curwin->w_skipcol == prev_skipcol;
3195}
3196
3197/*
3198 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3199 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3200 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003202 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
3204 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003205pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003207 int nochange = TRUE;
3208 int buflen = curbuf->b_ml.ml_line_count;
3209 colnr_T prev_col = curwin->w_cursor.col;
Luuk van Baal78c51502024-04-09 21:30:19 +02003210 colnr_T prev_curswant = curwin->w_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003211 linenr_T prev_lnum = curwin->w_cursor.lnum;
3212 oparg_T oa = { 0 };
3213 cmdarg_T ca = { 0 };
3214 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003216 if (half)
3217 {
3218 // Scroll [count], 'scroll' or current window height lines.
3219 if (count)
3220 curwin->w_p_scr = MIN(curwin->w_height, count);
3221 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Luuk van Baal9148ba82024-04-08 22:27:41 +02003223 int curscount = count;
3224 // Adjust count so as to not reveal end of buffer lines.
Luuk van Baal32d701f2024-04-28 16:24:02 +02003225 if (dir == FORWARD
3226 && (curwin->w_topline + curwin->w_height + count > buflen
3227#ifdef FEAT_FOLDING
3228 || hasAnyFolding(curwin)
3229#endif
3230 ))
Luuk van Baalcb204e62024-04-02 20:49:45 +02003231 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003232 int n = plines_correct_topline(curwin, curwin->w_topline, FALSE);
3233 if (n - count < curwin->w_height && curwin->w_topline < buflen)
Luuk van Baal32d701f2024-04-28 16:24:02 +02003234 n += plines_m_win(curwin, curwin->w_topline + 1, buflen,
3235 curwin->w_height + count);
3236 if (n < curwin->w_height + count)
Luuk van Baal9148ba82024-04-08 22:27:41 +02003237 count = n - curwin->w_height;
3238 }
3239
Luuk van Baal78c51502024-04-09 21:30:19 +02003240 // (Try to) scroll the window unless already at the end of the buffer.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003241 if (count > 0)
3242 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003243 nochange = scroll_with_sms(dir, count);
Luuk van Baal78c51502024-04-09 21:30:19 +02003244 curwin->w_cursor.lnum = prev_lnum;
3245 curwin->w_cursor.col = prev_col;
3246 curwin->w_curswant = prev_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Luuk van Baal78c51502024-04-09 21:30:19 +02003249 // Move the cursor the same amount of screen lines.
3250 if (curwin->w_p_wrap)
3251 nv_screengo(&oa, dir, curscount);
3252 else if (dir == FORWARD)
3253 cursor_down_inner(curwin, curscount);
3254 else
3255 cursor_up_inner(curwin, curscount);
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003256 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003257 else
3258 {
3259 // Scroll [count] times 'window' or current window height lines.
3260 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3261 MAX(1, p_window - 2) : get_scroll_overlap(dir));
Luuk van Baal9148ba82024-04-08 22:27:41 +02003262 nochange = scroll_with_sms(dir, count);
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003263
3264 // Place cursor at top or bottom of window.
3265 validate_botline();
3266 curwin->w_cursor.lnum = (dir == FORWARD ? curwin->w_topline
3267 : curwin->w_botline - 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003268 }
3269
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003270 if (get_scrolloff_value() > 0)
3271 cursor_correct();
3272#ifdef FEAT_FOLDING
3273 // Move cursor to first line of closed fold.
3274 foldAdjustCursor();
3275#endif
3276 nochange = nochange
3277 && prev_col == curwin->w_cursor.col
3278 && prev_lnum == curwin->w_cursor.lnum;
3279
Luuk van Baalcb204e62024-04-02 20:49:45 +02003280 // Error if both the viewport and cursor did not change.
3281 if (nochange)
3282 beep_flush();
3283 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003284 beginline(BL_SOL | BL_FIX);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003285 else if (p_sol)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003286 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003287
3288 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289}
3290
Bram Moolenaar860cae12010-06-05 23:22:07 +02003291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003292do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003293{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003294 static win_T *prev_curwin = NULL;
3295 static pos_T prev_cursor = {0, 0, 0};
3296
3297 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3298 return;
3299 prev_curwin = curwin;
3300 prev_cursor = curwin->w_cursor;
3301
Bram Moolenaar860cae12010-06-05 23:22:07 +02003302 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003303 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003304 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003305 colnr_T curswant = curwin->w_curswant;
3306 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003307 win_T *old_curwin = curwin;
3308 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003309 int old_VIsual_select = VIsual_select;
3310 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003311
3312 /*
3313 * loop through the cursorbound windows
3314 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003315 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003316 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317 {
3318 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003319 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320 if (curwin != old_curwin && curwin->w_p_crb)
3321 {
3322# ifdef FEAT_DIFF
3323 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003324 curwin->w_cursor.lnum =
3325 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003326 else
3327# endif
3328 curwin->w_cursor.lnum = line;
3329 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003330 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003331 curwin->w_curswant = curswant;
3332 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003333
Bram Moolenaar85a20022019-12-21 18:25:54 +01003334 // Make sure the cursor is in a valid position. Temporarily set
3335 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003336 int restart_edit_save = restart_edit;
3337 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003338 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003339
3340 // Avoid a scroll here for the cursor position, 'scrollbind' is
3341 // more important.
3342 if (!curwin->w_p_scb)
3343 validate_cursor();
3344
Bram Moolenaar61452852011-02-01 18:01:11 +01003345 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003346 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003347 if (has_mbyte)
3348 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003349 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003350
Bram Moolenaar85a20022019-12-21 18:25:54 +01003351 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003352 if (!curwin->w_p_scb)
3353 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003354 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003355 }
3356 }
3357
3358 /*
3359 * reset current-window
3360 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003361 VIsual_select = old_VIsual_select;
3362 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003363 curwin = old_curwin;
3364 curbuf = old_curbuf;
3365}