blob: d27114f13c9f20d1d18f83ea8ef9357c2d4cac92 [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 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200204 int
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205sms_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{
zeertzjq2c47ab82025-02-13 20:34:34 +01001624 if (!curwin->w_p_sms || !curwin->w_p_wrap
Luuk van Baal9148ba82024-04-08 22:27:41 +02001625 || 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;
Christian Brabandteff20eb2024-05-15 21:35:36 +02001633 int overlap, top, bot;
zeertzjq2c47ab82025-02-13 20:34:34 +01001634 int size = so == 0 ? 0 : linetabsize_eol(curwin, curwin->w_topline);
Luuk van Baal9148ba82024-04-08 22:27:41 +02001635
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'.
Christian Brabandteff20eb2024-05-15 21:35:36 +02001642 while (so_cols > size && so_cols - width2 >= width1 && width1 > 0)
Luuk van Baal9148ba82024-04-08 22:27:41 +02001643 so_cols -= width2;
1644 if (so_cols >= width1 && so_cols > size)
1645 so_cols -= width1;
1646
zeertzjq2c47ab82025-02-13 20:34:34 +01001647 overlap = curwin->w_skipcol == 0 ? 0
1648 : sms_marker_overlap(curwin, curwin->w_width - width2);
1649 // If we have non-zero scrolloff, ignore marker overlap.
1650 top = curwin->w_skipcol + (so_cols != 0 ? so_cols : overlap);
Christian Brabandteff20eb2024-05-15 21:35:36 +02001651 bot = curwin->w_skipcol + width1 + (curwin->w_height - 1) * width2
Luuk van Baal9148ba82024-04-08 22:27:41 +02001652 - 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 {
zeertzjq2c47ab82025-02-13 20:34:34 +01001669 int rc;
1670
Luuk van Baal9148ba82024-04-08 22:27:41 +02001671 curwin->w_curswant = col;
zeertzjq2c47ab82025-02-13 20:34:34 +01001672 rc = coladvance(curwin->w_curswant);
Luuk van Baal9148ba82024-04-08 22:27:41 +02001673 // validate_virtcol() marked various things as valid, but after
1674 // moving the cursor they need to be recomputed
1675 curwin->w_valid &=
1676 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
zeertzjq2c47ab82025-02-13 20:34:34 +01001677 if (rc == FAIL && curwin->w_skipcol > 0
1678 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1679 {
1680 validate_virtcol();
1681 if (curwin->w_virtcol < curwin->w_skipcol + overlap)
1682 {
1683 // Cursor still not visible: move it to the next line instead.
1684 curwin->w_cursor.lnum++;
1685 curwin->w_cursor.col = 0;
1686 curwin->w_cursor.coladd = 0;
1687 curwin->w_curswant = 0;
1688 curwin->w_valid &= ~VALID_VIRTCOL;
1689 }
1690 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001691 }
1692}
1693
1694/*
1695 * Scroll "count" lines up or down, and redraw.
1696 */
1697 void
1698scroll_redraw(int up, long count)
1699{
1700 linenr_T prev_topline = curwin->w_topline;
1701 int prev_skipcol = curwin->w_skipcol;
1702#ifdef FEAT_DIFF
1703 int prev_topfill = curwin->w_topfill;
1704#endif
1705 linenr_T prev_lnum = curwin->w_cursor.lnum;
1706
1707 if (up)
1708 scrollup(count, TRUE);
1709 else
1710 scrolldown(count, TRUE);
1711 if (get_scrolloff_value() > 0)
1712 {
1713 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
1714 // valid, otherwise the screen jumps back at the end of the file.
1715 cursor_correct();
1716 check_cursor_moved(curwin);
1717 curwin->w_valid |= VALID_TOPLINE;
1718
1719 // If moved back to where we were, at least move the cursor, otherwise
1720 // we get stuck at one position. Don't move the cursor up if the
1721 // first line of the buffer is already on the screen
1722 while (curwin->w_topline == prev_topline
1723 && curwin->w_skipcol == prev_skipcol
1724#ifdef FEAT_DIFF
1725 && curwin->w_topfill == prev_topfill
1726#endif
1727 )
1728 {
1729 if (up)
1730 {
1731 if (curwin->w_cursor.lnum > prev_lnum
1732 || cursor_down(1L, FALSE) == FAIL)
1733 break;
1734 }
1735 else
1736 {
1737 if (curwin->w_cursor.lnum < prev_lnum
1738 || prev_topline == 1L
1739 || cursor_up(1L, FALSE) == FAIL)
1740 break;
1741 }
1742 // Mark w_topline as valid, otherwise the screen jumps back at the
1743 // end of the file.
1744 check_cursor_moved(curwin);
1745 curwin->w_valid |= VALID_TOPLINE;
1746 }
1747 }
1748
1749 cursor_correct_sms();
1750 if (curwin->w_cursor.lnum != prev_lnum)
1751 coladvance(curwin->w_curswant);
1752 redraw_later(UPD_VALID);
1753}
1754
1755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759scrolldown(
1760 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 int wrow;
1765 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001766 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001767 int width1 = 0;
1768 int width2 = 0;
1769
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001770 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001771 {
1772 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001773 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776#ifdef FEAT_FOLDING
1777 linenr_T first;
1778
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1781#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001782 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001783 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001786 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1787 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
1789 ++curwin->w_topfill;
1790 ++done;
1791 }
1792 else
1793#endif
1794 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001795 // break when at the very top
1796 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001797 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001799 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001801 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001802 if (curwin->w_skipcol >= width1 + width2)
1803 curwin->w_skipcol -= width2;
1804 else
1805 curwin->w_skipcol -= width1;
1806 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001810 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001811 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001812 --curwin->w_topline;
1813 curwin->w_skipcol = 0;
1814#ifdef FEAT_DIFF
1815 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001817#ifdef FEAT_FOLDING
1818 // A sequence of folded lines only counts for one logical line
1819 if (hasFolding(curwin->w_topline, &first, NULL))
1820 {
1821 ++done;
1822 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001823 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001824 curwin->w_botline -= curwin->w_topline - first;
1825 curwin->w_topline = first;
1826 }
1827 else
1828#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001829 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001830 {
zeertzjq2c47ab82025-02-13 20:34:34 +01001831 int size = linetabsize_eol(curwin, curwin->w_topline);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001832 if (size > width1)
1833 {
1834 curwin->w_skipcol = width1;
1835 size -= width1;
1836 redraw_later(UPD_NOT_VALID);
1837 }
1838 while (size > width2)
1839 {
1840 curwin->w_skipcol += width2;
1841 size -= width2;
1842 }
1843 ++done;
1844 }
1845 else
1846 done += PLINES_NOFILL(curwin->w_topline);
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001849 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 invalidate_botline();
1851 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001852 curwin->w_wrow += done; // keep w_wrow updated
1853 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855#ifdef FEAT_DIFF
1856 if (curwin->w_cursor.lnum == curwin->w_topline)
1857 curwin->w_cline_row = 0;
1858 check_topfill(curwin, TRUE);
1859#endif
1860
1861 /*
1862 * Compute the row number of the last row of the cursor line
1863 * and move the cursor onto the displayed part of the window.
1864 */
1865 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001866 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
1868 validate_virtcol();
1869 validate_cheight();
1870 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001871 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1874 {
1875#ifdef FEAT_FOLDING
1876 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1877 {
1878 --wrow;
1879 if (first == 1)
1880 curwin->w_cursor.lnum = 1;
1881 else
1882 curwin->w_cursor.lnum = first - 1;
1883 }
1884 else
1885#endif
1886 wrow -= plines(curwin->w_cursor.lnum--);
1887 curwin->w_valid &=
1888 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1889 moved = TRUE;
1890 }
1891 if (moved)
1892 {
1893#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001894 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 foldAdjustCursor();
1896#endif
1897 coladvance(curwin->w_curswant);
1898 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001899 if (curwin->w_cursor.lnum < curwin->w_topline)
1900 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901}
1902
1903/*
1904 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001907scrollup(
1908 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001909 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001911 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001913 if (do_sms
1914# ifdef FEAT_FOLDING
1915 || (byfold && hasAnyFolding(curwin))
1916# endif
1917# ifdef FEAT_DIFF
1918 || (curwin->w_p_diff && !curwin->w_p_wrap)
1919# endif
1920 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001922 int width1 = curwin->w_width - curwin_col_off();
1923 int width2 = width1 + curwin_col_off2();
1924 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001925 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001926
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001927 if (do_sms)
zeertzjq2c47ab82025-02-13 20:34:34 +01001928 size = linetabsize_eol(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001929
1930 // diff mode: first consume "topfill"
1931 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1932 // the line, then advance to the next line.
1933 // folding: count each sequence of folded lines as one logical line.
1934 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936# ifdef FEAT_DIFF
1937 if (curwin->w_topfill > 0)
1938 --curwin->w_topfill;
1939 else
1940# endif
1941 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001942 linenr_T lnum = curwin->w_topline;
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944# ifdef FEAT_FOLDING
1945 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001946 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 (void)hasFolding(lnum, NULL, &lnum);
1948# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001949 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001950 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001951 // 'smoothscroll': increase "w_skipcol" until it goes over
1952 // the end of the line, then advance to the next line.
1953 int add = curwin->w_skipcol > 0 ? width2 : width1;
1954 curwin->w_skipcol += add;
1955 if (curwin->w_skipcol >= size)
1956 {
1957 if (lnum == curbuf->b_ml.ml_line_count)
1958 {
1959 // at the last screen line, can't scroll further
1960 curwin->w_skipcol -= add;
1961 break;
1962 }
1963 ++lnum;
1964 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001965 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001966 else
1967 {
1968 if (lnum >= curbuf->b_ml.ml_line_count)
1969 break;
1970 ++lnum;
1971 }
1972
1973 if (lnum > curwin->w_topline)
1974 {
1975 // approximate w_botline
1976 curwin->w_botline += lnum - curwin->w_topline;
1977 curwin->w_topline = lnum;
1978# ifdef FEAT_DIFF
1979 curwin->w_topfill = diff_check_fill(curwin, lnum);
1980# endif
1981 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001982 if (todo > 1 && do_sms)
zeertzjq2c47ab82025-02-13 20:34:34 +01001983 size = linetabsize_eol(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001984 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001985 }
1986 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001987
zeertzjqd9a92dc2023-06-05 18:41:35 +01001988 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1989 // need to redraw more, because wl_size of the (new) topline may
1990 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001991 redraw_later(UPD_NOT_VALID);
1992 }
1993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001996 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2000 curwin->w_topline = curbuf->b_ml.ml_line_count;
2001 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
2002 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
2003
2004#ifdef FEAT_DIFF
2005 check_topfill(curwin, FALSE);
2006#endif
2007
2008#ifdef FEAT_FOLDING
2009 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002010 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2012#endif
2013
2014 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2015 if (curwin->w_cursor.lnum < curwin->w_topline)
2016 {
2017 curwin->w_cursor.lnum = curwin->w_topline;
2018 curwin->w_valid &=
2019 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
2020 coladvance(curwin->w_curswant);
2021 }
2022}
2023
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002024/*
2025 * Called after changing the cursor column: make sure that curwin->w_skipcol is
2026 * valid for 'smoothscroll'.
2027 */
2028 void
2029adjust_skipcol(void)
2030{
2031 if (!curwin->w_p_wrap
2032 || !curwin->w_p_sms
2033 || curwin->w_cursor.lnum != curwin->w_topline)
2034 return;
2035
2036 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00002037 if (width1 <= 0)
2038 return; // no text will be displayed
2039
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002040 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002041 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002042 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
2043 int scrolled = FALSE;
Luuk van Baalb32055e2024-05-16 20:44:09 +02002044 int row = 0;
2045 int overlap, col;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002046
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002047 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00002048 if (curwin->w_cline_height == curwin->w_height
2049 // w_cline_height may be capped at w_height, check there aren't
2050 // actually more lines.
2051 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
2052 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002053 {
2054 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002055 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002056 return;
2057 }
2058
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002059 validate_virtcol();
zeertzjq2c47ab82025-02-13 20:34:34 +01002060 overlap = sms_marker_overlap(curwin, curwin->w_width - width2);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002061 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01002062 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002063 {
2064 // scroll a screen line down
2065 if (curwin->w_skipcol >= width1 + width2)
2066 curwin->w_skipcol -= width2;
2067 else
2068 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002069 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002070 }
2071 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01002072 {
2073 validate_virtcol();
2074 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002075 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002076 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002077
Luuk van Baalb32055e2024-05-16 20:44:09 +02002078 col = curwin->w_virtcol + scrolloff_cols;
2079
2080 // Avoid adjusting for 'scrolloff' beyond the text line height.
2081 if (scrolloff_cols > 0)
2082 {
zeertzjq2c47ab82025-02-13 20:34:34 +01002083 int size = linetabsize_eol(curwin, curwin->w_topline);
Luuk van Baalb32055e2024-05-16 20:44:09 +02002084 size = width1 + width2 * ((size - width1 + width2 - 1) / width2);
2085 while (col > size)
2086 col -= width2;
2087 }
2088 col -= curwin->w_skipcol;
2089
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002090 if (col >= width1)
2091 {
2092 col -= width1;
2093 ++row;
2094 }
2095 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002096 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002097 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002098 // col may no longer be used, but make
2099 // sure it is correct anyhow, just in case
2100 col = col % width2;
2101 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002102 if (row >= curwin->w_height)
2103 {
2104 if (curwin->w_skipcol == 0)
2105 {
2106 curwin->w_skipcol += width1;
2107 --row;
2108 }
2109 if (row >= curwin->w_height)
2110 curwin->w_skipcol += (row - curwin->w_height) * width2;
2111 redraw_later(UPD_NOT_VALID);
2112 }
2113}
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#ifdef FEAT_DIFF
2116/*
2117 * Don't end up with too many filler lines in the window.
2118 */
2119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002120check_topfill(
2121 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002122 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 int n;
2125
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002126 if (wp->w_topfill <= 0)
2127 return;
2128
2129 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2130 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002132 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002134 --wp->w_topline;
2135 wp->w_topfill = 0;
2136 }
2137 else
2138 {
2139 wp->w_topfill = wp->w_height - n;
2140 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 }
2144}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#endif
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147/*
2148 * Scroll the screen one line down, but don't do it if it would move the
2149 * cursor off the screen.
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int end_row;
2155#ifdef FEAT_DIFF
2156 int can_fill = (curwin->w_topfill
2157 < diff_check_fill(curwin, curwin->w_topline));
2158#endif
2159
2160 if (curwin->w_topline <= 1
2161#ifdef FEAT_DIFF
2162 && !can_fill
2163#endif
2164 )
2165 return;
2166
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /*
2170 * Compute the row number of the last row of the cursor line
2171 * and make sure it doesn't go off the screen. Make sure the cursor
2172 * doesn't go past 'scrolloff' lines from the screen end.
2173 */
2174 end_row = curwin->w_wrow;
2175#ifdef FEAT_DIFF
2176 if (can_fill)
2177 ++end_row;
2178 else
2179 end_row += plines_nofill(curwin->w_topline - 1);
2180#else
2181 end_row += plines(curwin->w_topline - 1);
2182#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002183 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 validate_cheight();
2186 validate_virtcol();
2187 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002188 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002190 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192#ifdef FEAT_DIFF
2193 if (can_fill)
2194 {
2195 ++curwin->w_topfill;
2196 check_topfill(curwin, TRUE);
2197 }
2198 else
2199 {
2200 --curwin->w_topline;
2201 curwin->w_topfill = 0;
2202 }
2203#else
2204 --curwin->w_topline;
2205#endif
2206#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002207 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2211 }
2212}
2213
2214/*
2215 * Scroll the screen one line up, but don't do it if it would move the cursor
2216 * off the screen.
2217 */
2218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002219scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 int start_row;
2222
2223 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2224#ifdef FEAT_DIFF
2225 && curwin->w_topfill == 0
2226#endif
2227 )
2228 return;
2229
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 /*
2233 * Compute the row number of the first row of the cursor line
2234 * and make sure it doesn't go off the screen. Make sure the cursor
2235 * doesn't go before 'scrolloff' lines from the screen start.
2236 */
2237#ifdef FEAT_DIFF
2238 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2239 - curwin->w_topfill;
2240#else
2241 start_row = curwin->w_wrow - plines(curwin->w_topline);
2242#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002243 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002246 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002248 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250#ifdef FEAT_DIFF
2251 if (curwin->w_topfill > 0)
2252 --curwin->w_topfill;
2253 else
2254#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002255 {
2256#ifdef FEAT_FOLDING
2257 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002260 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002261 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2263 }
2264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
2266/*
2267 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2268 * a (wrapped) text line. Uses and sets "lp->fill".
2269 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002270 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
2272 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002273topline_back_winheight(
2274 lineoff_T *lp,
2275 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277#ifdef FEAT_DIFF
2278 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2279 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002280 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 ++lp->fill;
2282 lp->height = 1;
2283 }
2284 else
2285#endif
2286 {
2287 --lp->lnum;
2288#ifdef FEAT_DIFF
2289 lp->fill = 0;
2290#endif
2291 if (lp->lnum < 1)
2292 lp->height = MAXCOL;
2293 else
2294#ifdef FEAT_FOLDING
2295 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002296 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 lp->height = 1;
2298 else
2299#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002300 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302}
2303
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002304 static void
2305topline_back(lineoff_T *lp)
2306{
2307 topline_back_winheight(lp, TRUE);
2308}
2309
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311/*
2312 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2313 * a (wrapped) text line. Uses and sets "lp->fill".
2314 * Returns the height of the added line in "lp->height".
2315 * Lines below the last one are incredibly high.
2316 */
2317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002318botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320#ifdef FEAT_DIFF
2321 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2322 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002323 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 ++lp->fill;
2325 lp->height = 1;
2326 }
2327 else
2328#endif
2329 {
2330 ++lp->lnum;
2331#ifdef FEAT_DIFF
2332 lp->fill = 0;
2333#endif
2334 if (lp->lnum > curbuf->b_ml.ml_line_count)
2335 lp->height = MAXCOL;
2336 else
2337#ifdef FEAT_FOLDING
2338 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 lp->height = 1;
2341 else
2342#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002343 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345}
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347/*
2348 * Recompute topline to put the cursor at the top of the window.
2349 * Scroll at least "min_scroll" lines.
2350 * If "always" is TRUE, always set topline (for "zt").
2351 */
2352 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002353scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 int scrolled = 0;
2356 int extra = 0;
2357 int used;
2358 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 linenr_T top; // just above displayed lines
2360 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002362 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_DIFF
2364 linenr_T old_topfill = curwin->w_topfill;
2365#endif
2366 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002367 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (mouse_dragging > 0)
2370 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 /*
2373 * Decrease topline until:
2374 * - it has become 1
2375 * - (part of) the cursor line is moved off the screen or
2376 * - moved at least 'scrolljump' lines and
2377 * - at least 'scrolloff' lines above and below the cursor
2378 */
2379 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002380 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (curwin->w_cursor.lnum < curwin->w_topline)
2382 scrolled = used;
2383
2384#ifdef FEAT_FOLDING
2385 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2386 {
2387 --top;
2388 ++bot;
2389 }
2390 else
2391#endif
2392 {
2393 top = curwin->w_cursor.lnum - 1;
2394 bot = curwin->w_cursor.lnum + 1;
2395 }
2396 new_topline = top + 1;
2397
2398#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // "used" already contains the number of filler lines above, don't add it
2400 // again.
2401 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002402 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#endif
2404
2405 /*
2406 * Check if the lines from "top" to "bot" fit in the window. If they do,
2407 * set new_topline and advance "top" and "bot" to include more lines.
2408 */
2409 while (top > 0)
2410 {
2411#ifdef FEAT_FOLDING
2412 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 i = 1;
2415 else
2416#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002417 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002418 if (top < curwin->w_topline)
2419 scrolled += i;
2420
2421 // If scrolling is needed, scroll at least 'sj' lines.
2422 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2423 && extra >= off)
2424 break;
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 used += i;
2427 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2428 {
2429#ifdef FEAT_FOLDING
2430 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 ++used;
2433 else
2434#endif
2435 used += plines(bot);
2436 }
2437 if (used > curwin->w_height)
2438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 extra += i;
2441 new_topline = top;
2442 --top;
2443 ++bot;
2444 }
2445
2446 /*
2447 * If we don't have enough space, put cursor in the middle.
2448 * This makes sure we get the same position when using "k" and "j"
2449 * in a small window.
2450 */
2451 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002452 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
2454 {
2455 /*
2456 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002457 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
2459 if (new_topline < curwin->w_topline || always)
2460 curwin->w_topline = new_topline;
2461 if (curwin->w_topline > curwin->w_cursor.lnum)
2462 curwin->w_topline = curwin->w_cursor.lnum;
2463#ifdef FEAT_DIFF
2464 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2465 if (curwin->w_topfill > 0 && extra > off)
2466 {
2467 curwin->w_topfill -= extra - off;
2468 if (curwin->w_topfill < 0)
2469 curwin->w_topfill = 0;
2470 }
2471 check_topfill(curwin, FALSE);
2472#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002473 if (curwin->w_topline != old_topline)
2474 reset_skipcol();
2475 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002476 {
2477 validate_virtcol();
2478 if (curwin->w_skipcol >= curwin->w_virtcol)
2479 // TODO: if the line doesn't fit may optimize w_skipcol instead
2480 // of making it zero
2481 reset_skipcol();
2482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002484 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#ifdef FEAT_DIFF
2486 || curwin->w_topfill != old_topfill
2487#endif
2488 )
2489 curwin->w_valid &=
2490 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2491 curwin->w_valid |= VALID_TOPLINE;
2492 }
2493}
2494
2495/*
2496 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2497 * screen lines for text lines.
2498 */
2499 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002500set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
2502#ifdef FEAT_DIFF
2503 wp->w_filler_rows = 0;
2504#endif
2505 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002506 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 else
2508 {
2509 wp->w_empty_rows = wp->w_height - used;
2510#ifdef FEAT_DIFF
2511 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2512 {
2513 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2514 if (wp->w_empty_rows > wp->w_filler_rows)
2515 wp->w_empty_rows -= wp->w_filler_rows;
2516 else
2517 {
2518 wp->w_filler_rows = wp->w_empty_rows;
2519 wp->w_empty_rows = 0;
2520 }
2521 }
2522#endif
2523 }
2524}
2525
2526/*
2527 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002528 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2530 * This is messy stuff!!!
2531 */
2532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 int used;
2536 int scrolled = 0;
2537 int extra = 0;
2538 int i;
2539 linenr_T line_count;
2540 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002541 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 lineoff_T loff;
2543 lineoff_T boff;
2544#ifdef FEAT_DIFF
2545 int old_topfill = curwin->w_topfill;
2546 int fill_below_window;
2547#endif
2548 linenr_T old_botline = curwin->w_botline;
2549 linenr_T old_valid = curwin->w_valid;
2550 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002552 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002553 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 cln = curwin->w_cursor.lnum;
2556 if (set_topbot)
2557 {
2558 used = 0;
2559 curwin->w_botline = cln + 1;
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002560 loff.lnum = cln + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#ifdef FEAT_DIFF
2562 loff.fill = 0;
2563#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002564 while (TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002566 topline_back_winheight(&loff, FALSE);
2567 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002569 if (used + loff.height > curwin->w_height)
2570 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002571 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002572 {
2573 // 'smoothscroll' and 'wrap' are set. The above line is
2574 // too long to show in its entirety, so we show just a part
2575 // of it.
2576 if (used < curwin->w_height)
2577 {
2578 int plines_offset = used + loff.height
2579 - curwin->w_height;
2580 used = curwin->w_height;
2581#ifdef FEAT_DIFF
2582 curwin->w_topfill = loff.fill;
2583#endif
2584 curwin->w_topline = loff.lnum;
2585 curwin->w_skipcol = skipcol_from_plines(
2586 curwin, plines_offset);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002587 }
2588 }
2589 break;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#ifdef FEAT_DIFF
2592 curwin->w_topfill = loff.fill;
2593#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002594 curwin->w_topline = loff.lnum;
2595 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 set_empty_rows(curwin, used);
2599 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2600 if (curwin->w_topline != old_topline
2601#ifdef FEAT_DIFF
2602 || curwin->w_topfill != old_topfill
2603#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002604 || curwin->w_skipcol != old_skipcol
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002605 || curwin->w_skipcol != 0)
2606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002608 if (curwin->w_skipcol != old_skipcol)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002609 redraw_later(UPD_NOT_VALID);
2610 else
2611 reset_skipcol();
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614 else
2615 validate_botline();
2616
Bram Moolenaar85a20022019-12-21 18:25:54 +01002617 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#ifdef FEAT_DIFF
2619 used = plines_nofill(cln);
2620#else
2621 validate_cheight();
2622 used = curwin->w_cline_height;
2623#endif
2624
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002625 // If the cursor is on or below botline, we will at least scroll by the
2626 // height of the cursor line, which is "used". Correct for empty lines,
2627 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (cln >= curwin->w_botline)
2629 {
2630 scrolled = used;
2631 if (cln == curwin->w_botline)
2632 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002633 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002634 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002635 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002636 // Calculate how many screen lines the current top line of window
2637 // occupies. If it is occupying more than the entire window, we
2638 // need to scroll the additional clipped lines to scroll past the
2639 // top line before we can move on to the other lines.
2640 int top_plines =
2641#ifdef FEAT_DIFF
2642 plines_win_nofill
2643#else
2644 plines_win
2645#endif
2646 (curwin, curwin->w_topline, FALSE);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002647 int width1 = curwin->w_width - curwin_col_off();
zeertzjq031a7452024-05-11 11:23:37 +02002648
fullwaywang8154e642023-06-24 21:58:09 +01002649 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002650 {
fullwaywang8154e642023-06-24 21:58:09 +01002651 int width2 = width1 + curwin_col_off2();
zeertzjq031a7452024-05-11 11:23:37 +02002652 int skip_lines = 0;
2653
2654 // A similar formula is used in curs_columns().
fullwaywang8154e642023-06-24 21:58:09 +01002655 if (curwin->w_skipcol > width1)
2656 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2657 else if (curwin->w_skipcol > 0)
2658 skip_lines = 1;
2659
2660 top_plines -= skip_lines;
2661 if (top_plines > curwin->w_height)
2662 {
2663 scrolled += (top_plines - curwin->w_height);
2664 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002665 }
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668
2669 /*
2670 * Stop counting lines to scroll when
2671 * - hitting start of the file
2672 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002673 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 * - lines between botline and cursor have been counted
2675 */
2676#ifdef FEAT_FOLDING
2677 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2678#endif
2679 {
2680 loff.lnum = cln;
2681 boff.lnum = cln;
2682 }
2683#ifdef FEAT_DIFF
2684 loff.fill = 0;
2685 boff.fill = 0;
2686 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2687 - curwin->w_filler_rows;
2688#endif
2689
2690 while (loff.lnum > 1)
2691 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2693 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002695 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2697 && loff.lnum <= curwin->w_botline
2698#ifdef FEAT_DIFF
2699 && (loff.lnum < curwin->w_botline
2700 || loff.fill >= fill_below_window)
2701#endif
2702 )
2703 break;
2704
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002707 if (loff.height == MAXCOL)
2708 used = MAXCOL;
2709 else
2710 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (used > curwin->w_height)
2712 break;
2713 if (loff.lnum >= curwin->w_botline
2714#ifdef FEAT_DIFF
2715 && (loff.lnum > curwin->w_botline
2716 || loff.fill <= fill_below_window)
2717#endif
2718 )
2719 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002720 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 scrolled += loff.height;
2722 if (loff.lnum == curwin->w_botline
2723#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002724 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725#endif
2726 )
2727 scrolled -= curwin->w_empty_rows;
2728 }
2729
2730 if (boff.lnum < curbuf->b_ml.ml_line_count)
2731 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002732 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 botline_forw(&boff);
2734 used += boff.height;
2735 if (used > curwin->w_height)
2736 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002737 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2738 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 extra += boff.height;
2741 if (boff.lnum >= curwin->w_botline
2742#ifdef FEAT_DIFF
2743 || (boff.lnum + 1 == curwin->w_botline
2744 && boff.fill > curwin->w_filler_rows)
2745#endif
2746 )
2747 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002748 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 scrolled += boff.height;
2750 if (boff.lnum == curwin->w_botline
2751#ifdef FEAT_DIFF
2752 && boff.fill == 0
2753#endif
2754 )
2755 scrolled -= curwin->w_empty_rows;
2756 }
2757 }
2758 }
2759 }
2760
Bram Moolenaar85a20022019-12-21 18:25:54 +01002761 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (scrolled <= 0)
2763 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002764 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else if (used > curwin->w_height)
2766 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002767 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 else
2769 {
2770 line_count = 0;
2771#ifdef FEAT_DIFF
2772 boff.fill = curwin->w_topfill;
2773#endif
2774 boff.lnum = curwin->w_topline - 1;
2775 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2776 {
2777 botline_forw(&boff);
2778 i += boff.height;
2779 ++line_count;
2780 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002781 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 line_count = 9999;
2783 }
2784
2785 /*
2786 * Scroll up if the cursor is off the bottom of the screen a bit.
2787 * Otherwise put it at 1/2 of the screen.
2788 */
2789 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002790 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002791 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002792 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002793 if (do_sms)
2794 scrollup(scrolled, TRUE); // TODO
2795 else
2796 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
2800 * If topline didn't change we need to restore w_botline and w_empty_rows
2801 * (we changed them).
2802 * If topline did change, update_screen() will set botline.
2803 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002804 if (curwin->w_topline == old_topline
2805 && curwin->w_skipcol == old_skipcol
2806 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 curwin->w_botline = old_botline;
2809 curwin->w_empty_rows = old_empty_rows;
2810 curwin->w_valid = old_valid;
2811 }
2812 curwin->w_valid |= VALID_TOPLINE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02002813
Luuk van Baalb32055e2024-05-16 20:44:09 +02002814 // Make sure cursor is still visible after adjusting skipcol for "zb".
2815 if (set_topbot)
2816 cursor_correct_sms();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817}
2818
2819/*
2820 * Recompute topline to put the cursor halfway the window
2821 * If "atend" is TRUE, also put it halfway at the end of the file.
2822 */
2823 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002824scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 int above = 0;
2827 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002828 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#ifdef FEAT_DIFF
2830 int topfill = 0;
2831#endif
2832 int below = 0;
2833 int used;
2834 lineoff_T loff;
2835 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002836#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002837 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002840#ifdef FEAT_PROP_POPUP
2841 // if the width changed this needs to be updated first
2842 may_update_popup_position();
2843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2845#ifdef FEAT_FOLDING
2846 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2847#endif
2848#ifdef FEAT_DIFF
2849 used = plines_nofill(loff.lnum);
2850 loff.fill = 0;
2851 boff.fill = 0;
2852#else
2853 used = plines(loff.lnum);
2854#endif
2855 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002856
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002857 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002858 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2859 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002860 {
2861 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002862 if (atend)
2863 {
2864 want_height = (curwin->w_height - used) / 2;
2865 used = 0;
2866 }
2867 else
2868 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002869 }
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 while (topline > 1)
2872 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002873 // If using smoothscroll, we can precisely scroll to the
2874 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002875 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002876 {
2877 topline_back_winheight(&loff, FALSE);
2878 if (loff.height == MAXCOL)
2879 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002880 used += loff.height;
2881 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002882 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002883 botline_forw(&boff);
2884 used += boff.height;
2885 }
2886 if (used > want_height)
2887 {
2888 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002889 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002890 topline = loff.lnum;
2891#ifdef FEAT_DIFF
2892 topfill = loff.fill;
2893#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002894 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002895 }
2896 break;
2897 }
2898 topline = loff.lnum;
2899#ifdef FEAT_DIFF
2900 topfill = loff.fill;
2901#endif
2902 continue;
2903 }
2904
2905 // If not using smoothscroll, we have to iteratively find how many
2906 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002907 // This may not be right in the middle if the lines'
2908 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002909
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002910 // Depending on "prefer_above" we add a line above or below first.
2911 // Loop twice to avoid duplicating code.
2912 int done = FALSE;
2913 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002915 if (prefer_above ? (round == 2 && below < above)
2916 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002918 // add a line below the cursor
2919 if (boff.lnum < curbuf->b_ml.ml_line_count)
2920 {
2921 botline_forw(&boff);
2922 used += boff.height;
2923 if (used > curwin->w_height)
2924 {
2925 done = TRUE;
2926 break;
2927 }
2928 below += boff.height;
2929 }
2930 else
2931 {
2932 ++below; // count a "~" line
2933 if (atend)
2934 ++used;
2935 }
2936 }
2937
2938 if (prefer_above ? (round == 1 && below >= above)
2939 : (round == 1 && below > above))
2940 {
2941 // add a line above the cursor
2942 topline_back(&loff);
2943 if (loff.height == MAXCOL)
2944 used = MAXCOL;
2945 else
2946 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002948 {
2949 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002951 }
2952 above += loff.height;
2953 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002955 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002959 if (done)
2960 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#ifdef FEAT_FOLDING
2964 if (!hasFolding(topline, &curwin->w_topline, NULL))
2965#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002966 {
2967 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002968 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002969 || curwin->w_skipcol != 0)
2970 {
2971 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002972 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002973 {
2974 curwin->w_skipcol = skipcol;
2975 redraw_later(UPD_NOT_VALID);
2976 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002977 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002978 reset_skipcol();
2979 }
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#ifdef FEAT_DIFF
2982 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002983 if (old_topline > curwin->w_topline + curwin->w_height)
2984 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 check_topfill(curwin, FALSE);
2986#endif
2987 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2988 curwin->w_valid |= VALID_TOPLINE;
2989}
2990
2991/*
2992 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002993 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 * If not possible, put it at the same position as scroll_cursor_halfway().
2995 * When called topline must be valid!
2996 */
2997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002998cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003002 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 linenr_T botline;
3004 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003005 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003007 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 /*
3010 * How many lines we would like to have above/below the cursor depends on
3011 * whether the first/last line of the file is on screen.
3012 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01003013 above_wanted = so;
3014 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00003015 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
3017 above_wanted = mouse_dragging - 1;
3018 below_wanted = mouse_dragging - 1;
3019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (curwin->w_topline == 1)
3021 {
3022 above_wanted = 0;
3023 max_off = curwin->w_height / 2;
3024 if (below_wanted > max_off)
3025 below_wanted = max_off;
3026 }
3027 validate_botline();
3028 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003029 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 {
3031 below_wanted = 0;
3032 max_off = (curwin->w_height - 1) / 2;
3033 if (above_wanted > max_off)
3034 above_wanted = max_off;
3035 }
3036
3037 /*
3038 * If there are sufficient file-lines above and below the cursor, we can
3039 * return now.
3040 */
3041 cln = curwin->w_cursor.lnum;
3042 if (cln >= curwin->w_topline + above_wanted
3043 && cln < curwin->w_botline - below_wanted
3044#ifdef FEAT_FOLDING
3045 && !hasAnyFolding(curwin)
3046#endif
3047 )
3048 return;
3049
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003050 if (curwin->w_p_sms && !curwin->w_p_wrap)
3051 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003052 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003053 if (curwin->w_cline_height == curwin->w_height)
3054 {
3055 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003056 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003057 return;
3058 }
3059 // TODO: If the cursor line doesn't fit in the window then only adjust
3060 // w_skipcol.
3061 }
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 /*
3064 * Narrow down the area where the cursor can be put by taking lines from
3065 * the top and the bottom until:
3066 * - the desired context lines are found
3067 * - the lines from the top is past the lines from the bottom
3068 */
3069 topline = curwin->w_topline;
3070 botline = curwin->w_botline - 1;
3071#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003072 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 above = curwin->w_topfill;
3074 below = curwin->w_filler_rows;
3075#endif
3076 while ((above < above_wanted || below < below_wanted) && topline < botline)
3077 {
3078 if (below < below_wanted && (below <= above || above >= above_wanted))
3079 {
3080#ifdef FEAT_FOLDING
3081 if (hasFolding(botline, &botline, NULL))
3082 ++below;
3083 else
3084#endif
3085 below += plines(botline);
3086 --botline;
3087 }
3088 if (above < above_wanted && (above < below || below >= below_wanted))
3089 {
3090#ifdef FEAT_FOLDING
3091 if (hasFolding(topline, NULL, &topline))
3092 ++above;
3093 else
3094#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003095 above += PLINES_NOFILL(topline);
3096#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003097 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (topline < botline)
3099 above += diff_check_fill(curwin, topline + 1);
3100#endif
3101 ++topline;
3102 }
3103 }
3104 if (topline == botline || botline == 0)
3105 curwin->w_cursor.lnum = topline;
3106 else if (topline > botline)
3107 curwin->w_cursor.lnum = botline;
3108 else
3109 {
3110 if (cln < topline && curwin->w_topline > 1)
3111 {
3112 curwin->w_cursor.lnum = topline;
3113 curwin->w_valid &=
3114 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3115 }
3116 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3117 {
3118 curwin->w_cursor.lnum = botline;
3119 curwin->w_valid &=
3120 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3121 }
3122 }
Luuk van Baalc9825032025-04-13 17:45:34 +02003123 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 curwin->w_valid |= VALID_TOPLINE;
3125}
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003128 * Decide how much overlap to use for page-up or page-down scrolling.
3129 * This is symmetric, so that doing both keeps the same lines displayed.
3130 * Three lines are examined:
3131 *
3132 * before CTRL-F after CTRL-F / before CTRL-B
3133 * etc. l1
3134 * l1 last but one line ------------
3135 * l2 last text line l2 top text line
3136 * ------------- l3 second text line
3137 * l3 etc.
3138 */
3139static int get_scroll_overlap(int dir)
3140{
3141 lineoff_T loff;
3142 int min_height = curwin->w_height - 2;
3143
3144 validate_botline();
Luuk van Baalbd28cae2024-04-03 22:50:40 +02003145 if ((dir == BACKWARD && curwin->w_topline == 1)
3146 || (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003147 return min_height + 2; // no overlap, still handle 'smoothscroll'
3148
3149 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3150#ifdef FEAT_DIFF
zeertzjq92325542024-04-12 18:38:38 +02003151 loff.fill = diff_check_fill(curwin, loff.lnum + (dir == BACKWARD))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003152 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3153 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3154#else
3155 loff.height = plines(loff.lnum);
3156#endif
3157
3158 int h1 = loff.height;
3159 if (h1 > min_height)
3160 return min_height + 2; // no overlap
3161 if (dir == FORWARD)
3162 topline_back(&loff);
3163 else
3164 botline_forw(&loff);
3165
3166 int h2 = loff.height;
3167 if (h2 == MAXCOL || h2 + h1 > min_height)
3168 return min_height + 2; // no overlap
3169 if (dir == FORWARD)
3170 topline_back(&loff);
3171 else
3172 botline_forw(&loff);
3173
3174 int h3 = loff.height;
3175 if (h3 == MAXCOL || h3 + h2 > min_height)
3176 return min_height + 2; // no overlap
3177 if (dir == FORWARD)
3178 topline_back(&loff);
3179 else
3180 botline_forw(&loff);
3181
3182 int h4 = loff.height;
3183 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3184 return min_height + 1; // 1 line overlap
3185 else
3186 return min_height; // 2 lines overlap
3187}
3188
3189/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02003190 * Scroll "count" lines with 'smoothscroll' in direction "dir". Return TRUE
Luuk van Baal58448e02024-05-11 11:27:52 +02003191 * when scrolling happened. Adjust "curscount" for scrolling different amount of
3192 * lines when 'smoothscroll' is disabled.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003193 */
Luuk van Baal58448e02024-05-11 11:27:52 +02003194static int scroll_with_sms(int dir, long count, long *curscount)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003195{
3196 int prev_sms = curwin->w_p_sms;
3197 colnr_T prev_skipcol = curwin->w_skipcol;
3198 linenr_T prev_topline = curwin->w_topline;
3199#ifdef FEAT_DIFF
3200 int prev_topfill = curwin->w_topfill;
3201#endif
3202
3203 curwin->w_p_sms = TRUE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003204 scroll_redraw(dir == FORWARD, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003205
3206 // Not actually smoothscrolling but ended up with partially visible line.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003207 // Continue scrolling until skipcol is zero.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003208 if (!prev_sms && curwin->w_skipcol > 0)
3209 {
3210 int fixdir = dir;
3211 // Reverse the scroll direction when topline already changed. One line
3212 // extra for scrolling backward so that consuming skipcol is symmetric.
3213 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3214 fixdir = dir * -1;
Luuk van Baalacf0ebe2025-05-12 20:45:41 +02003215
3216 int width1 = curwin->w_width - curwin_col_off();
3217 int width2 = width1 + curwin_col_off2();
Luuk van Baalc6c72d12025-05-14 20:21:55 +02003218 count = 1 + (curwin->w_skipcol - width1 - 1) / width2;
Luuk van Baalacf0ebe2025-05-12 20:45:41 +02003219 if (fixdir == FORWARD)
Luuk van Baalc6c72d12025-05-14 20:21:55 +02003220 count = 1 + (linetabsize_eol(curwin, curwin->w_topline)
3221 - curwin->w_skipcol - width1 + width2 - 1) / width2;
Luuk van Baalacf0ebe2025-05-12 20:45:41 +02003222 scroll_redraw(fixdir == FORWARD, count);
3223 *curscount += count * (fixdir == dir ? 1 : -1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003224 }
3225 curwin->w_p_sms = prev_sms;
3226
3227 return curwin->w_topline == prev_topline
3228#ifdef FEAT_DIFF
3229 && curwin->w_topfill == prev_topfill
3230#endif
3231 && curwin->w_skipcol == prev_skipcol;
3232}
3233
3234/*
3235 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3236 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3237 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003239 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
3241 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003242pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003244 int nochange = TRUE;
3245 int buflen = curbuf->b_ml.ml_line_count;
3246 colnr_T prev_col = curwin->w_cursor.col;
Luuk van Baal78c51502024-04-09 21:30:19 +02003247 colnr_T prev_curswant = curwin->w_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003248 linenr_T prev_lnum = curwin->w_cursor.lnum;
3249 oparg_T oa = { 0 };
3250 cmdarg_T ca = { 0 };
3251 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003253 if (half)
3254 {
3255 // Scroll [count], 'scroll' or current window height lines.
3256 if (count)
3257 curwin->w_p_scr = MIN(curwin->w_height, count);
3258 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Luuk van Baal58448e02024-05-11 11:27:52 +02003260 long curscount = count;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003261 // Adjust count so as to not reveal end of buffer lines.
Luuk van Baal32d701f2024-04-28 16:24:02 +02003262 if (dir == FORWARD
3263 && (curwin->w_topline + curwin->w_height + count > buflen
3264#ifdef FEAT_FOLDING
3265 || hasAnyFolding(curwin)
3266#endif
3267 ))
Luuk van Baalcb204e62024-04-02 20:49:45 +02003268 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003269 int n = plines_correct_topline(curwin, curwin->w_topline, FALSE);
3270 if (n - count < curwin->w_height && curwin->w_topline < buflen)
Luuk van Baal32d701f2024-04-28 16:24:02 +02003271 n += plines_m_win(curwin, curwin->w_topline + 1, buflen,
3272 curwin->w_height + count);
3273 if (n < curwin->w_height + count)
Luuk van Baal9148ba82024-04-08 22:27:41 +02003274 count = n - curwin->w_height;
3275 }
3276
Luuk van Baal78c51502024-04-09 21:30:19 +02003277 // (Try to) scroll the window unless already at the end of the buffer.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003278 if (count > 0)
3279 {
Luuk van Baal58448e02024-05-11 11:27:52 +02003280 nochange = scroll_with_sms(dir, count, &curscount);
Luuk van Baal78c51502024-04-09 21:30:19 +02003281 curwin->w_cursor.lnum = prev_lnum;
3282 curwin->w_cursor.col = prev_col;
3283 curwin->w_curswant = prev_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Luuk van Baal78c51502024-04-09 21:30:19 +02003286 // Move the cursor the same amount of screen lines.
3287 if (curwin->w_p_wrap)
3288 nv_screengo(&oa, dir, curscount);
3289 else if (dir == FORWARD)
3290 cursor_down_inner(curwin, curscount);
3291 else
3292 cursor_up_inner(curwin, curscount);
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003293 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003294 else
3295 {
3296 // Scroll [count] times 'window' or current window height lines.
3297 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3298 MAX(1, p_window - 2) : get_scroll_overlap(dir));
Luuk van Baal58448e02024-05-11 11:27:52 +02003299 nochange = scroll_with_sms(dir, count, &count);
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003300
Luuk van Baal8ccb8902024-07-04 17:35:56 +02003301 if (!nochange)
3302 {
3303 // Place cursor at top or bottom of window.
3304 validate_botline();
zeertzjqdf098fe2025-01-22 22:27:30 +01003305 linenr_T lnum = (dir == FORWARD ? curwin->w_topline
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003306 : curwin->w_botline - 1);
zeertzjqdf098fe2025-01-22 22:27:30 +01003307 // In silent Ex mode the value of w_botline - 1 may be 0,
3308 // but cursor lnum needs to be at least 1.
3309 curwin->w_cursor.lnum = MAX(lnum, 1);
Luuk van Baal8ccb8902024-07-04 17:35:56 +02003310 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003311 }
3312
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003313 if (get_scrolloff_value() > 0)
3314 cursor_correct();
3315#ifdef FEAT_FOLDING
3316 // Move cursor to first line of closed fold.
3317 foldAdjustCursor();
3318#endif
3319 nochange = nochange
3320 && prev_col == curwin->w_cursor.col
3321 && prev_lnum == curwin->w_cursor.lnum;
3322
Luuk van Baalcb204e62024-04-02 20:49:45 +02003323 // Error if both the viewport and cursor did not change.
3324 if (nochange)
3325 beep_flush();
3326 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003327 beginline(BL_SOL | BL_FIX);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003328 else if (p_sol)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003329 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003330
3331 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332}
3333
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003335do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003336{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003337 static win_T *prev_curwin = NULL;
3338 static pos_T prev_cursor = {0, 0, 0};
3339
3340 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3341 return;
3342 prev_curwin = curwin;
3343 prev_cursor = curwin->w_cursor;
3344
Bram Moolenaar860cae12010-06-05 23:22:07 +02003345 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003346 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003347 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003348 colnr_T curswant = curwin->w_curswant;
3349 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003350 win_T *old_curwin = curwin;
3351 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352 int old_VIsual_select = VIsual_select;
3353 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003354
3355 /*
3356 * loop through the cursorbound windows
3357 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003358 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003359 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003360 {
3361 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003362 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003363 if (curwin != old_curwin && curwin->w_p_crb)
3364 {
3365# ifdef FEAT_DIFF
3366 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003367 curwin->w_cursor.lnum =
3368 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003369 else
3370# endif
3371 curwin->w_cursor.lnum = line;
3372 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003373 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003374 curwin->w_curswant = curswant;
3375 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003376
Bram Moolenaar85a20022019-12-21 18:25:54 +01003377 // Make sure the cursor is in a valid position. Temporarily set
3378 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003379 int restart_edit_save = restart_edit;
3380 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003381 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003382
3383 // Avoid a scroll here for the cursor position, 'scrollbind' is
3384 // more important.
3385 if (!curwin->w_p_scb)
3386 validate_cursor();
3387
Bram Moolenaar61452852011-02-01 18:01:11 +01003388 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003389 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003390 if (has_mbyte)
3391 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003392 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003393
Bram Moolenaar85a20022019-12-21 18:25:54 +01003394 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003395 if (!curwin->w_p_scb)
3396 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003397 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003398 }
3399 }
3400
3401 /*
3402 * reset current-window
3403 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003404 VIsual_select = old_VIsual_select;
3405 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003406 curwin = old_curwin;
3407 curbuf = old_curbuf;
3408}