blob: 9bdd46f2cd78f430fb1af6991d5af59e0ccc5a71 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
zeertzjq3f1b5312024-02-06 10:43:36 +010022static void redraw_for_cursorline(win_T *wp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
Bram Moolenaar85a20022019-12-21 18:25:54 +010029 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010031 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010033 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000034} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
zeertzjq6235a102023-08-19 14:12:42 +020040 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010041 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010042 int
zeertzjq6235a102023-08-19 14:12:42 +020043adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010044{
45 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020046 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047
Bram Moolenaarf6196f42022-10-02 21:29:55 +010048 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010049 int w2 = width + win_col_off2(wp);
50 if (wp->w_skipcol >= width && w2 > 0)
51 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020052
53 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054}
55
56/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010057 * Return how many lines "lnum" will take on the screen, taking into account
58 * whether it is the first line, whether w_skipcol is non-zero and limiting to
59 * the window height.
60 */
61 static int
Luuk van Baal9148ba82024-04-08 22:27:41 +020062plines_correct_topline(win_T *wp, linenr_T lnum, int limit_winheight)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010063{
64 int n;
65#ifdef FEAT_DIFF
66 if (lnum == wp->w_topline)
67 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
68 else
69#endif
70 n = plines_win(wp, lnum, FALSE);
71 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020072 n -= adjust_plines_for_skipcol(wp);
Luuk van Baal9148ba82024-04-08 22:27:41 +020073 if (limit_winheight && n > wp->w_height)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010074 n = wp->w_height;
75 return n;
76}
77
78/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Compute wp->w_botline for the current wp->w_topline. Can be called after
80 * wp->w_topline changed.
81 */
82 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010083comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int n;
86 linenr_T lnum;
87 int done;
88#ifdef FEAT_FOLDING
89 linenr_T last;
90 int folded;
91#endif
92
93 /*
94 * If w_cline_row is valid, start there.
95 * Otherwise have to start at w_topline.
96 */
97 check_cursor_moved(wp);
98 if (wp->w_valid & VALID_CROW)
99 {
100 lnum = wp->w_cursor.lnum;
101 done = wp->w_cline_row;
102 }
103 else
104 {
105 lnum = wp->w_topline;
106 done = 0;
107 }
108
109 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
110 {
111#ifdef FEAT_FOLDING
112 last = lnum;
113 folded = FALSE;
114 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
115 {
116 n = 1;
117 folded = TRUE;
118 }
119 else
120#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200122 n = plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if (
125#ifdef FEAT_FOLDING
126 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
127#else
128 lnum == wp->w_cursor.lnum
129#endif
130 )
131 {
132 wp->w_cline_row = done;
133 wp->w_cline_height = n;
134#ifdef FEAT_FOLDING
135 wp->w_cline_folded = folded;
136#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100137 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
139 }
140 if (done + n > wp->w_height)
141 break;
142 done += n;
143#ifdef FEAT_FOLDING
144 lnum = last;
145#endif
146 }
147
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 wp->w_botline = lnum;
150 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
151
152 set_empty_rows(wp, done);
153}
154
155/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100156 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
157 * set.
158 */
zeertzjq3f1b5312024-02-06 10:43:36 +0100159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100160redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100161{
162 if ((wp->w_p_rnu
163#ifdef FEAT_SYN_HL
164 || wp->w_p_cul
165#endif
166 )
167 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200168 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200169 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000170 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100171 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200172 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100173}
174
zeertzjq3e559cd2022-03-27 19:26:55 +0100175#ifdef FEAT_SYN_HL
176/*
177 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
178 * contains "screenline".
179 */
180 static void
181redraw_for_cursorcolumn(win_T *wp)
182{
183 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
184 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100186 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100187 redraw_win_later(wp, UPD_SOME_VALID);
188 // When 'cursorlineopt' contains "screenline" need to redraw with
189 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100192 }
193}
194#endif
195
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100196/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100197 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
198 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000199 * Parameter "extra2" should be the padding on the 2nd line, not the first
Luuk van Baalcb204e62024-04-02 20:49:45 +0200200 * line. When "extra2" is -1 calculate the padding.
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000201 * Returns the number of columns of overlap with buffer text, excluding the
202 * extra padding on the ledge.
203 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100204 int
205sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000206{
Luuk van Baalcb204e62024-04-02 20:49:45 +0200207 if (extra2 == -1)
208 extra2 = win_col_off(wp) - win_col_off2(wp);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100210 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000211 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000213 return 0;
214#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100215 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
216 if (wp->w_p_list && wp->w_lcs_chars.prec)
217 return 1;
218
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return extra2 > 3 ? 0 : 3 - extra2;
220}
221
222/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000223 * Calculates the skipcol offset for window "wp" given how many
224 * physical lines we want to scroll down.
225 */
226 static int
227skipcol_from_plines(win_T *wp, int plines_off)
228{
229 int width1 = wp->w_width - win_col_off(wp);
230
231 int skipcol = 0;
232 if (plines_off > 0)
233 skipcol += width1;
234 if (plines_off > 1)
235 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
236 return skipcol;
237}
238
239/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100240 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000241 */
242 static void
243reset_skipcol(void)
244{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000245 if (curwin->w_skipcol == 0)
246 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000248 curwin->w_skipcol = 0;
249
250 // Should use the least expensive way that displays all that changed.
251 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
252 // enough when the top line gets another screen line.
253 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000254}
255
256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * Update curwin->w_topline and redraw if necessary.
258 * Used to update the screen before printing a message.
259 */
260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100261update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 update_topline();
264 if (must_redraw)
265 update_screen(0);
266}
267
268/*
269 * Update curwin->w_topline to move the cursor onto the screen.
270 */
271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100272update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 long line_count;
275 int halfheight;
276 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifdef FEAT_FOLDING
278 linenr_T lnum;
279#endif
280 int check_topline = FALSE;
281 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100282 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100283 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100285 // Cursor is updated instead when this is TRUE for 'splitkeep'.
286 if (skip_update_topline)
287 return;
288
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // If there is no valid screen and when the window height is zero just use
290 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200291 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100293 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 curwin->w_topline = curwin->w_cursor.lnum;
295 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200296 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200297 return;
298 }
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 check_cursor_moved(curwin);
301 if (curwin->w_valid & VALID_TOPLINE)
302 return;
303
Bram Moolenaar85a20022019-12-21 18:25:54 +0100304 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000305 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100306 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100308 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100310 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312
313 /*
314 * If the buffer is empty, always set topline to 1.
315 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100316 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
318 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100319 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 curwin->w_botline = 2;
322 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325
326 /*
327 * If the cursor is above or near the top of the window, scroll the window
328 * to show the line the cursor is in, with 'scrolloff' context.
329 */
330 else
331 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100332 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100334 // If the cursor is above topline, scrolling is always needed.
335 // If the cursor is far below topline and there is no folding,
336 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 if (curwin->w_cursor.lnum < curwin->w_topline)
338 check_topline = TRUE;
339 else if (check_top_offset())
340 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100341 else if (curwin->w_skipcol > 0
342 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100343 {
344 colnr_T vcol;
Luuk van Baalcb204e62024-04-02 20:49:45 +0200345 int overlap;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100346
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000347 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100348 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baalcb204e62024-04-02 20:49:45 +0200350 overlap = sms_marker_overlap(curwin, -1);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100351 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100352 check_topline = TRUE;
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
358 curwin->w_topline))
359 check_topline = TRUE;
360#endif
361
362 if (check_topline)
363 {
364 halfheight = curwin->w_height / 2 - 1;
365 if (halfheight < 2)
366 halfheight = 2;
367
368#ifdef FEAT_FOLDING
369 if (hasAnyFolding(curwin))
370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 // Count the number of logical lines between the cursor and
372 // topline + scrolloff (approximation of how much will be
373 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 n = 0;
375 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {
378 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100379 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
381 break;
382 (void)hasFolding(lnum, NULL, &lnum);
383 }
384 }
385 else
386#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100387 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaar85a20022019-12-21 18:25:54 +0100389 // If we weren't very close to begin with, we scroll to put the
390 // cursor in the middle of the window. Otherwise put the cursor
391 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000393 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 else
395 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000396 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 check_botline = TRUE;
398 }
399 }
400
401 else
402 {
403#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100404 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
406#endif
407 check_botline = TRUE;
408 }
409 }
410
411 /*
412 * If the cursor is below the bottom of the window, scroll the window
413 * to put the cursor on the window.
414 * When w_botline is invalid, recompute it first, to avoid a redraw later.
415 * If w_botline was approximated, we might need a redraw later in a few
416 * cases, but we don't want to spend (a lot of) time recomputing w_botline
417 * for every small change.
418 */
419 if (check_botline)
420 {
421 if (!(curwin->w_valid & VALID_BOTLINE_AP))
422 validate_botline();
423
424 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
425 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000426 if (curwin->w_cursor.lnum < curwin->w_botline)
427 {
428 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100429 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef FEAT_FOLDING
431 || hasAnyFolding(curwin)
432#endif
433 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 lineoff_T loff;
436
Bram Moolenaar85a20022019-12-21 18:25:54 +0100437 // Cursor is (a few lines) above botline, check if there are
438 // 'scrolloff' window lines below the cursor. If not, need to
439 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 n = curwin->w_empty_rows;
441 loff.lnum = curwin->w_cursor.lnum;
442#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
445#endif
446#ifdef FEAT_DIFF
447 loff.fill = 0;
448 n += curwin->w_filler_rows;
449#endif
450 loff.height = 0;
451 while (loff.lnum < curwin->w_botline
452#ifdef FEAT_DIFF
453 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
454#endif
455 )
456 {
457 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 break;
460 botline_forw(&loff);
461 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100462 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100463 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000465 }
466 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100467 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000468 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 }
470 if (check_botline)
471 {
472#ifdef FEAT_FOLDING
473 if (hasAnyFolding(curwin))
474 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100475 // Count the number of logical lines between the cursor and
476 // botline - scrolloff (approximation of how much will be
477 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 line_count = 0;
479 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100483 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (lnum <= 0 || line_count > curwin->w_height + 1)
485 break;
486 (void)hasFolding(lnum, &lnum, NULL);
487 }
488 }
489 else
490#endif
491 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100492 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000494 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000496 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 }
499 }
500 curwin->w_valid |= VALID_TOPLINE;
501
502 /*
503 * Need to redraw when topline changed.
504 */
505 if (curwin->w_topline != old_topline
506#ifdef FEAT_DIFF
507 || curwin->w_topfill != old_topfill
508#endif
509 )
510 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100511 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000512 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100513
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100514 // When 'smoothscroll' is not set, should reset w_skipcol.
515 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100516 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100517 else if (curwin->w_skipcol != 0)
518 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000519
Bram Moolenaar85a20022019-12-21 18:25:54 +0100520 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (curwin->w_cursor.lnum == curwin->w_topline)
522 validate_cursor();
523 }
524
Bram Moolenaar375e3392019-01-31 18:26:10 +0100525 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
528/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000529 * Return the scrolljump value to use for the current window.
530 * When 'scrolljump' is positive use it as-is.
531 * When 'scrolljump' is negative use it as a percentage of the window height.
532 */
533 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100534scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000535{
536 if (p_sj >= 0)
537 return (int)p_sj;
538 return (curwin->w_height * -p_sj) / 100;
539}
540
541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
543 * current window.
544 */
545 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 lineoff_T loff;
549 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100550 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar375e3392019-01-31 18:26:10 +0100552 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_FOLDING
554 || hasAnyFolding(curwin)
555#endif
556 )
557 {
558 loff.lnum = curwin->w_cursor.lnum;
559#ifdef FEAT_DIFF
560 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100561 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#else
563 n = 0;
564#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100565 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100566 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
568 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100569 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (loff.lnum < curwin->w_topline
571#ifdef FEAT_DIFF
572 || (loff.lnum == curwin->w_topline && loff.fill > 0)
573#endif
574 )
575 break;
576 n += loff.height;
577 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100578 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return TRUE;
580 }
581 return FALSE;
582}
583
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100584/*
585 * Update w_curswant.
586 */
587 void
588update_curswant_force(void)
589{
590 validate_virtcol();
591 curwin->w_curswant = curwin->w_virtcol
592#ifdef FEAT_PROP_POPUP
593 - curwin->w_virtcol_first_char
594#endif
595 ;
596 curwin->w_set_curswant = FALSE;
597}
598
599/*
600 * Update w_curswant if w_set_curswant is set.
601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100606 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607}
608
609/*
610 * Check if the cursor has moved. Set the w_valid flag accordingly.
611 */
612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100613check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
616 {
617 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100618 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
619 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 wp->w_valid_cursor = wp->w_cursor;
621 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100622 wp->w_valid_skipcol = wp->w_skipcol;
623 }
624 else if (wp->w_skipcol != wp->w_valid_skipcol)
625 {
626 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
627 |VALID_CHEIGHT|VALID_CROW
628 |VALID_BOTLINE|VALID_BOTLINE_AP);
629 wp->w_valid_cursor = wp->w_cursor;
630 wp->w_valid_leftcol = wp->w_leftcol;
631 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
633 else if (wp->w_cursor.col != wp->w_valid_cursor.col
634 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100635 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
638 wp->w_valid_cursor.col = wp->w_cursor.col;
639 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642}
643
644/*
645 * Call this function when some window settings have changed, which require
646 * the cursor position, botline and topline to be recomputed and the window to
647 * be redrawn. E.g, when changing the 'wrap' option or folding.
648 */
649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 changed_window_setting_win(curwin);
653}
654
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 wp->w_lines_valid = 0;
659 changed_line_abv_curs_win(wp);
660 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100661 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
Dominique Pellee764d1b2023-03-12 21:20:59 +0000664#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000666 * Call changed_window_setting_win() for every window containing "buf".
667 */
668 void
669changed_window_setting_buf(buf_T *buf)
670{
671 tabpage_T *tp;
672 win_T *wp;
673
674 FOR_ALL_TAB_WINDOWS(tp, wp)
675 if (wp->w_buffer == buf)
676 changed_window_setting_win(wp);
677}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000678#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000679
680/*
zeertzjq05aacec2024-04-14 18:52:49 +0200681 * Call changed_window_setting_win() for every window.
682 */
683 void
684changed_window_setting_all(void)
685{
686 tabpage_T *tp;
687 win_T *wp;
688
689 FOR_ALL_TAB_WINDOWS(tp, wp)
690 changed_window_setting_win(wp);
691}
692
693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 * Set wp->w_topline to a certain number.
695 */
696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100697set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200699#ifdef FEAT_DIFF
700 linenr_T prev_topline = wp->w_topline;
701#endif
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
706#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100709 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
710 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000712 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200714 if (lnum != prev_topline)
715 // Keep the filler lines when the topline didn't change.
716 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#endif
718 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100719 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100720 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722
723/*
724 * Call this function when the length of the cursor line (in screen
725 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100726 * If the line length changed the number of screen lines might change,
727 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 * Need to take care of w_botline separately!
729 */
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar85090142023-06-01 19:27:08 +0100733 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 |VALID_CHEIGHT|VALID_TOPLINE);
735}
736
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
Bram Moolenaar85090142023-06-01 19:27:08 +0100740 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 |VALID_CHEIGHT|VALID_TOPLINE);
742}
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
745 * Call this function when the length of a line (in screen characters) above
746 * the cursor have changed.
747 * Need to take care of w_botline separately!
748 */
749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
753 |VALID_CHEIGHT|VALID_TOPLINE);
754}
755
756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100757changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
759 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
760 |VALID_CHEIGHT|VALID_TOPLINE);
761}
762
Dominique Pellee764d1b2023-03-12 21:20:59 +0000763#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100765 * Display of line has changed for "buf", invalidate cursor position and
766 * w_botline.
767 */
768 void
769changed_line_display_buf(buf_T *buf)
770{
771 win_T *wp;
772
773 FOR_ALL_WINDOWS(wp)
774 if (wp->w_buffer == buf)
775 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
776 |VALID_CROW|VALID_CHEIGHT
777 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
778}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000779#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100780
781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * Make sure the value of curwin->w_botline is valid.
783 */
784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100785validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100787 validate_botline_win(curwin);
788}
789
790/*
791 * Make sure the value of wp->w_botline is valid.
792 */
793 void
794validate_botline_win(win_T *wp)
795{
796 if (!(wp->w_valid & VALID_BOTLINE))
797 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * Mark curwin->w_botline as invalid (because of some change in the buffer).
802 */
803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100804invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
807}
808
809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100810invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816approximate_botline_win(
817 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
819 wp->w_valid &= ~VALID_BOTLINE;
820}
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822/*
823 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
824 */
825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100826cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 check_cursor_moved(curwin);
829 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
830 (VALID_WROW|VALID_WCOL));
831}
832
833/*
834 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
835 * w_topline must be valid, you may need to call update_topline() first!
836 */
837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100840 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 check_cursor_moved(curwin);
842 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
843 curs_columns(TRUE);
844}
845
846#if defined(FEAT_GUI) || defined(PROTO)
847/*
848 * validate w_cline_row.
849 */
850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100851validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 /*
854 * First make sure that w_topline is valid (after moving the cursor).
855 */
856 update_topline();
857 check_cursor_moved(curwin);
858 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100859 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861#endif
862
863/*
864 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200865 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 */
867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100868curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 linenr_T lnum;
871 int i;
872 int all_invalid;
873 int valid;
874#ifdef FEAT_FOLDING
875 long fold_count;
876#endif
877
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 all_invalid = (!redrawing()
880 || wp->w_lines_valid == 0
881 || wp->w_lines[0].wl_lnum > wp->w_topline);
882 i = 0;
883 wp->w_cline_row = 0;
884 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
885 {
886 valid = FALSE;
887 if (!all_invalid && i < wp->w_lines_valid)
888 {
889 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (wp->w_lines[i].wl_lnum == lnum)
892 {
893#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894 // Check for newly inserted lines below this row, in which
895 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (!wp->w_buffer->b_mod_set
897 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
898 || wp->w_buffer->b_mod_top
899 > wp->w_lines[i].wl_lastlnum + 1)
900#endif
901 valid = TRUE;
902 }
903 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100904 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100907 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100909 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100911 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
913#ifdef FEAT_FOLDING
914 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100915 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (lnum > wp->w_cursor.lnum)
917 break;
918#else
919 ++lnum;
920#endif
921 wp->w_cline_row += wp->w_lines[i].wl_size;
922 }
923 else
924 {
925#ifdef FEAT_FOLDING
926 fold_count = foldedCount(wp, lnum, NULL);
927 if (fold_count)
928 {
929 lnum += fold_count;
930 if (lnum > wp->w_cursor.lnum)
931 break;
932 ++wp->w_cline_row;
933 }
934 else
935#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100936 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200937 wp->w_cline_row += plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100938 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941 }
942
943 check_cursor_moved(wp);
944 if (!(wp->w_valid & VALID_CHEIGHT))
945 {
946 if (all_invalid
947 || i == wp->w_lines_valid
948 || (i < wp->w_lines_valid
949 && (!wp->w_lines[i].wl_valid
950 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
951 {
952#ifdef FEAT_DIFF
953 if (wp->w_cursor.lnum == wp->w_topline)
954 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
955 TRUE) + wp->w_topfill;
956 else
957#endif
958 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
959#ifdef FEAT_FOLDING
960 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
961 NULL, NULL, TRUE, NULL);
962#endif
963 }
964 else if (i > wp->w_lines_valid)
965 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100966 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 wp->w_cline_height = 0;
968#ifdef FEAT_FOLDING
969 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
970 NULL, NULL, TRUE, NULL);
971#endif
972 }
973 else
974 {
975 wp->w_cline_height = wp->w_lines[i].wl_size;
976#ifdef FEAT_FOLDING
977 wp->w_cline_folded = wp->w_lines[i].wl_folded;
978#endif
979 }
980 }
981
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100982 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985
986/*
987 * Validate curwin->w_virtcol only.
988 */
989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100990validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 validate_virtcol_win(curwin);
993}
994
995/*
996 * Validate wp->w_virtcol only.
997 */
998 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100999validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001002
1003 if (wp->w_valid & VALID_VIRTCOL)
1004 return;
1005
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001006#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001007 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001008#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001009 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001010#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001011 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001012#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001013 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014}
1015
1016/*
1017 * Validate curwin->w_cline_height only.
1018 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001023
1024 if (curwin->w_valid & VALID_CHEIGHT)
1025 return;
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001028 if (curwin->w_cursor.lnum == curwin->w_topline)
1029 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1030 + curwin->w_topfill;
1031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001033 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001035 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001037 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001041 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
1043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001044validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 colnr_T off;
1047 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001048 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001052 if (curwin->w_valid & VALID_WCOL)
1053 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001054
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001055 col = curwin->w_virtcol;
1056 off = curwin_col_off();
1057 col += off;
1058 width = curwin->w_width - off + curwin_col_off2();
1059
1060 // long line wrapping, adjust curwin->w_wrow
1061 if (curwin->w_p_wrap
1062 && col >= (colnr_T)curwin->w_width
1063 && width > 0)
1064 // use same formula as what is used in curs_columns()
1065 col -= ((col - curwin->w_width) / width + 1) * width;
1066 if (col > (int)curwin->w_leftcol)
1067 col -= curwin->w_leftcol;
1068 else
1069 col = 0;
1070 curwin->w_wcol = col;
1071
1072 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001073#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001074 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076}
1077
1078/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001079 * Compute offset of a window, occupied by absolute or relative line number,
1080 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
1082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001083win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
Bram Moolenaar64486672010-05-16 15:46:46 +02001085 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001086 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_FOLDING
1088 + wp->w_p_fdc
1089#endif
1090#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001091 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093 );
1094}
1095
1096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001097curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 return win_col_off(curwin);
1100}
1101
1102/*
1103 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001104 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1105 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 */
1107 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
Bram Moolenaar64486672010-05-16 15:46:46 +02001110 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001111 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 return 0;
1113}
1114
1115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001116curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 return win_col_off2(curwin);
1119}
1120
1121/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001122 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 * Also updates curwin->w_wrow and curwin->w_cline_row.
1124 * Also updates curwin->w_leftcol.
1125 */
1126 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001127curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
1130 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001131 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int off_left, off_right;
1133 int n;
1134 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001135 int width1; // text width for first screen line
1136 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 int new_leftcol;
1138 colnr_T startcol;
1139 colnr_T endcol;
1140 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001141 long so = get_scrolloff_value();
1142 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001143 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 /*
1146 * First make sure that w_topline is valid (after moving the cursor).
1147 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001148 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 /*
1151 * Next make sure that w_cline_row is valid.
1152 */
1153 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001154 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001156#ifdef FEAT_PROP_POPUP
1157 // will be set by getvvcol() but not reset
1158 curwin->w_virtcol_first_char = 0;
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 /*
1162 * Compute the number of virtual columns.
1163 */
1164#ifdef FEAT_FOLDING
1165 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001166 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1168 else
1169#endif
1170 getvvcol(curwin, &curwin->w_cursor,
1171 &startcol, &(curwin->w_virtcol), &endcol);
1172
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001175 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
1177 extra = curwin_col_off();
1178 curwin->w_wcol = curwin->w_virtcol + extra;
1179 endcol += extra;
1180
1181 /*
1182 * Now compute w_wrow, counting screen lines from w_cline_row.
1183 */
1184 curwin->w_wrow = curwin->w_cline_row;
1185
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001186 width1 = curwin->w_width - extra;
1187 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001190 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001191 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001192 if (curwin->w_p_wrap)
1193 curwin->w_wrow = curwin->w_height - 1;
1194 else
1195 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001197 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001199 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001201 // skip columns that are not visible
1202 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001203 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001204 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001205 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001206 // Deduct by multiples of width2. This allows the long line
1207 // wrapping formula below to correctly calculate the w_wcol value
1208 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001209 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001210 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001211 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001212 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001213 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001214
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001215 did_sub_skipcol = TRUE;
1216 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001217
Bram Moolenaar85a20022019-12-21 18:25:54 +01001218 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001219 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001221 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001222 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1223 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 }
1227
Bram Moolenaar85a20022019-12-21 18:25:54 +01001228 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1229 // is not folded.
1230 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001231 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#ifdef FEAT_FOLDING
1233 && !curwin->w_cline_folded
1234#endif
1235 )
1236 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001237#ifdef FEAT_PROP_POPUP
1238 if (curwin->w_virtcol_first_char > 0)
1239 {
1240 int cols = (curwin->w_width - extra);
1241 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1242
1243 // each "above" text prop shifts the text one row down
1244 curwin->w_wrow += rows;
1245 curwin->w_wcol -= rows * cols;
1246 endcol -= rows * cols;
1247 curwin->w_cline_height = rows + 1;
1248 }
1249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 /*
1251 * If Cursor is left of the screen, scroll rightwards.
1252 * If Cursor is right of the screen, scroll leftwards
1253 * If we get closer to the edge than 'sidescrolloff', scroll a little
1254 * extra
1255 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001256 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001257 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001258 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (off_left < 0 || off_right > 0)
1260 {
1261 if (off_left < 0)
1262 diff = -off_left;
1263 else
1264 diff = off_right;
1265
Bram Moolenaar85a20022019-12-21 18:25:54 +01001266 // When far off or not enough room on either side, put cursor in
1267 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001268 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1269 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 else
1271 {
1272 if (diff < p_ss)
1273 diff = p_ss;
1274 if (off_left < 0)
1275 new_leftcol = curwin->w_leftcol - diff;
1276 else
1277 new_leftcol = curwin->w_leftcol + diff;
1278 }
1279 if (new_leftcol < 0)
1280 new_leftcol = 0;
1281 if (new_leftcol != (int)curwin->w_leftcol)
1282 {
1283 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001284 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001285 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288 curwin->w_wcol -= curwin->w_leftcol;
1289 }
1290 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1291 curwin->w_wcol -= curwin->w_leftcol;
1292 else
1293 curwin->w_wcol = 0;
1294
1295#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 // Skip over filler lines. At the top use w_topfill, there
1297 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (curwin->w_cursor.lnum == curwin->w_topline)
1299 curwin->w_wrow += curwin->w_topfill;
1300 else
1301 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1302#endif
1303
1304 prev_skipcol = curwin->w_skipcol;
1305
1306 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if ((curwin->w_wrow >= curwin->w_height
1309 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001310 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 && (p_lines =
1312#ifdef FEAT_DIFF
1313 plines_win_nofill
1314#else
1315 plines_win
1316#endif
1317 (curwin, curwin->w_cursor.lnum, FALSE))
1318 - 1 >= curwin->w_height))
1319 && curwin->w_height != 0
1320 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001321 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001322 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 // Cursor past end of screen. Happens with a single line that does
1325 // not fit on screen. Find a skipcol to show the text around the
1326 // cursor. Avoid scrolling all the time. compute value of "extra":
1327 // 1: Less than 'scrolloff' lines above
1328 // 2: Less than 'scrolloff' lines below
1329 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001331 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 // Compute last display line of the buffer line that we want at the
1334 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (p_lines == 0)
1336 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1337 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001338 if (p_lines > curwin->w_wrow + so)
1339 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001342 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 extra += 2;
1344
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001345 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001347 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001348 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (n > curwin->w_height / 2)
1350 n -= curwin->w_height / 2;
1351 else
1352 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001353 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (n > p_lines - curwin->w_height + 1)
1355 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001356 if (n > 0)
1357 curwin->w_skipcol = width1 + (n - 1) * width2;
1358 else
1359 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else if (extra == 1)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1365 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (extra > 0)
1367 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1369 extra = curwin->w_skipcol / width2;
1370 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 }
1373 else if (extra == 2)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (endcol > curwin->w_skipcol)
1380 curwin->w_skipcol = endcol;
1381 }
1382
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001383 // adjust w_wrow for the changed w_skipcol
1384 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001386 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001387 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (curwin->w_wrow >= curwin->w_height)
1390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001393 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 curwin->w_wrow -= extra;
1395 }
1396
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001397 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (extra > 0)
1399 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1400 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001401 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001403 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_skipcol = 0;
1405 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001406 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001408#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001409 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001410#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001411#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1412 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1413 {
1414 curwin->w_wrow += popup_top_extra(curwin);
1415 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001416 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001417 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001418 else
1419 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001420#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001421
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001422 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1423 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001424 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001425 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1428}
1429
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001430#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001431/*
1432 * Compute the screen position of text character at "pos" in window "wp"
1433 * The resulting values are one-based, zero when character is not visible.
1434 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001435 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436textpos2screenpos(
1437 win_T *wp,
1438 pos_T *pos,
1439 int *rowp, // screen row
1440 int *scolp, // start screen column
1441 int *ccolp, // cursor screen column
1442 int *ecolp) // end screen column
1443{
1444 colnr_T scol = 0, ccol = 0, ecol = 0;
1445 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001446 colnr_T coloff = 0;
1447
Bram Moolenaar189663b2021-07-21 18:04:56 +02001448 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001449 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001450 colnr_T col;
1451 int width;
1452 linenr_T lnum = pos->lnum;
1453#ifdef FEAT_FOLDING
1454 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001456 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1457#endif
Luuk van Baal32d701f2024-04-28 16:24:02 +02001458 row = plines_m_win(wp, wp->w_topline, lnum - 1, INT_MAX);
zeertzjqbfe377b2023-08-17 22:58:53 +02001459 // "row" should be the screen line where line "lnum" begins, which can
1460 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001461 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001462
1463#ifdef FEAT_DIFF
1464 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001465 row += lnum == wp->w_topline ? wp->w_topfill
1466 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001467#endif
1468
zeertzjqba2d1912022-12-18 12:28:59 +00001469 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001470#ifdef FEAT_FOLDING
1471 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001472 {
zeertzjq6235a102023-08-19 14:12:42 +02001473 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001474 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001475 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001476 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001477#endif
1478 {
1479 getvcol(wp, pos, &scol, &ccol, &ecol);
1480
1481 // similar to what is done in validate_cursor_col()
1482 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001483 col += off;
1484 width = wp->w_width - off + win_col_off2(wp);
1485
1486 // long line wrapping, adjust row
1487 if (wp->w_p_wrap
1488 && col >= (colnr_T)wp->w_width
1489 && width > 0)
1490 {
1491 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001492 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001493 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001494 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001495 }
1496 col -= wp->w_leftcol;
1497 if (col >= wp->w_width)
1498 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001499 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001500 {
1501 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001502 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001503 }
1504 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001505 // character is out of the window
1506 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001507 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001509 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510 *scolp = scol + coloff;
1511 *ccolp = ccol + coloff;
1512 *ecolp = ecol + coloff;
1513}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001514#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515
Bram Moolenaar12034e22019-08-25 22:25:02 +02001516#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001517/*
1518 * "screenpos({winid}, {lnum}, {col})" function
1519 */
1520 void
1521f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1522{
1523 dict_T *dict;
1524 win_T *wp;
1525 pos_T pos;
1526 int row = 0;
1527 int scol = 0, ccol = 0, ecol = 0;
1528
Bram Moolenaar93a10962022-06-16 11:42:09 +01001529 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530 return;
1531 dict = rettv->vval.v_dict;
1532
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001533 if (in_vim9script()
1534 && (check_for_number_arg(argvars, 0) == FAIL
1535 || check_for_number_arg(argvars, 1) == FAIL
1536 || check_for_number_arg(argvars, 2) == FAIL))
1537 return;
1538
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp == NULL)
1541 return;
1542
1543 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001544 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1545 {
1546 semsg(_(e_invalid_line_number_nr), pos.lnum);
1547 return;
1548 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001549 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001550 if (pos.col < 0)
1551 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001552 pos.coladd = 0;
1553 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1554
1555 dict_add_number(dict, "row", row);
1556 dict_add_number(dict, "col", scol);
1557 dict_add_number(dict, "curscol", ccol);
1558 dict_add_number(dict, "endcol", ecol);
1559}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001560
1561/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001562 * Convert a virtual (screen) column to a character column. The first column
1563 * is one. For a multibyte character, the column number of the first byte is
1564 * returned.
1565 */
1566 static int
1567virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1568{
zeertzjqf5a94d52023-10-15 10:03:30 +02001569 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001570 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1571 char_u *p = line + offset;
1572
zeertzjqb583eda2023-10-14 11:32:28 +02001573 if (*p == NUL)
1574 {
1575 if (p == line) // empty line
1576 return 0;
1577 // Move to the first byte of the last char.
1578 MB_PTR_BACK(line, p);
1579 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001580 return (int)(p - line + 1);
1581}
1582
1583/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001584 * "virtcol2col({winid}, {lnum}, {col})" function
1585 */
1586 void
1587f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1588{
1589 win_T *wp;
1590 linenr_T lnum;
1591 int screencol;
1592 int error = FALSE;
1593
1594 rettv->vval.v_number = -1;
1595
1596 if (check_for_number_arg(argvars, 0) == FAIL
1597 || check_for_number_arg(argvars, 1) == FAIL
1598 || check_for_number_arg(argvars, 2) == FAIL)
1599 return;
1600
1601 wp = find_win_by_nr_or_id(&argvars[0]);
1602 if (wp == NULL)
1603 return;
1604
1605 lnum = tv_get_number_chk(&argvars[1], &error);
1606 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1607 return;
1608
1609 screencol = tv_get_number_chk(&argvars[2], &error);
1610 if (error || screencol < 0)
1611 return;
1612
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001613 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001614}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001615#endif
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02001618 * Make sure the cursor is in the visible part of the topline after scrolling
1619 * the screen with 'smoothscroll'.
1620 */
1621static void cursor_correct_sms(void)
1622{
1623 if (!curwin->w_p_sms ||!curwin->w_p_wrap
1624 || curwin->w_cursor.lnum != curwin->w_topline)
1625 return;
1626
1627 long so = get_scrolloff_value();
1628 int width1 = curwin->w_width - curwin_col_off();
1629 int width2 = width1 + curwin_col_off2();
1630 int so_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1631 int space_cols = (curwin->w_height - 1) * width2;
1632 int size = so == 0 ? 0 : win_linetabsize(curwin, curwin->w_topline,
1633 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1634
1635 if (curwin->w_topline == 1 && curwin->w_skipcol == 0)
1636 so_cols = 0; // Ignore 'scrolloff' at top of buffer.
1637 else if (so_cols > space_cols / 2)
1638 so_cols = space_cols / 2; // Not enough room: put cursor in the middle.
1639
1640 // Not enough screen lines in topline: ignore 'scrolloff'.
1641 while (so_cols > size && so_cols - width2 >= width1)
1642 so_cols -= width2;
1643 if (so_cols >= width1 && so_cols > size)
1644 so_cols -= width1;
1645
1646 // If there is no marker or we have non-zero scrolloff, just ignore it.
1647 int overlap = (curwin->w_skipcol == 0 || so_cols != 0) ? 0
1648 : sms_marker_overlap(curwin, -1);
1649 int top = curwin->w_skipcol + overlap + so_cols;
1650 int bot = curwin->w_skipcol + width1 + (curwin->w_height - 1) * width2
1651 - so_cols;
1652 validate_virtcol();
1653 colnr_T col = curwin->w_virtcol;
1654
1655 if (col < top)
1656 {
1657 if (col < width1)
1658 col += width1;
1659 while (width2 > 0 && col < top)
1660 col += width2;
1661 }
1662 else
1663 while (width2 > 0 && col >= bot)
1664 col -= width2;
1665
1666 if (col != curwin->w_virtcol)
1667 {
1668 curwin->w_curswant = col;
1669 coladvance(curwin->w_curswant);
1670 // validate_virtcol() marked various things as valid, but after
1671 // moving the cursor they need to be recomputed
1672 curwin->w_valid &=
1673 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1674 }
1675}
1676
1677/*
1678 * Scroll "count" lines up or down, and redraw.
1679 */
1680 void
1681scroll_redraw(int up, long count)
1682{
1683 linenr_T prev_topline = curwin->w_topline;
1684 int prev_skipcol = curwin->w_skipcol;
1685#ifdef FEAT_DIFF
1686 int prev_topfill = curwin->w_topfill;
1687#endif
1688 linenr_T prev_lnum = curwin->w_cursor.lnum;
1689
1690 if (up)
1691 scrollup(count, TRUE);
1692 else
1693 scrolldown(count, TRUE);
1694 if (get_scrolloff_value() > 0)
1695 {
1696 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
1697 // valid, otherwise the screen jumps back at the end of the file.
1698 cursor_correct();
1699 check_cursor_moved(curwin);
1700 curwin->w_valid |= VALID_TOPLINE;
1701
1702 // If moved back to where we were, at least move the cursor, otherwise
1703 // we get stuck at one position. Don't move the cursor up if the
1704 // first line of the buffer is already on the screen
1705 while (curwin->w_topline == prev_topline
1706 && curwin->w_skipcol == prev_skipcol
1707#ifdef FEAT_DIFF
1708 && curwin->w_topfill == prev_topfill
1709#endif
1710 )
1711 {
1712 if (up)
1713 {
1714 if (curwin->w_cursor.lnum > prev_lnum
1715 || cursor_down(1L, FALSE) == FAIL)
1716 break;
1717 }
1718 else
1719 {
1720 if (curwin->w_cursor.lnum < prev_lnum
1721 || prev_topline == 1L
1722 || cursor_up(1L, FALSE) == FAIL)
1723 break;
1724 }
1725 // Mark w_topline as valid, otherwise the screen jumps back at the
1726 // end of the file.
1727 check_cursor_moved(curwin);
1728 curwin->w_valid |= VALID_TOPLINE;
1729 }
1730 }
1731
1732 cursor_correct_sms();
1733 if (curwin->w_cursor.lnum != prev_lnum)
1734 coladvance(curwin->w_curswant);
1735 redraw_later(UPD_VALID);
1736}
1737
1738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001742scrolldown(
1743 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 int wrow;
1748 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001749 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001750 int width1 = 0;
1751 int width2 = 0;
1752
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001753 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001754 {
1755 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001756 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759#ifdef FEAT_FOLDING
1760 linenr_T first;
1761
Bram Moolenaar85a20022019-12-21 18:25:54 +01001762 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1764#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001765 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001766 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
1768#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001769 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1770 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 ++curwin->w_topfill;
1773 ++done;
1774 }
1775 else
1776#endif
1777 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001778 // break when at the very top
1779 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001780 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001782 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001784 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001785 if (curwin->w_skipcol >= width1 + width2)
1786 curwin->w_skipcol -= width2;
1787 else
1788 curwin->w_skipcol -= width1;
1789 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001793 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001794 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001795 --curwin->w_topline;
1796 curwin->w_skipcol = 0;
1797#ifdef FEAT_DIFF
1798 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001800#ifdef FEAT_FOLDING
1801 // A sequence of folded lines only counts for one logical line
1802 if (hasFolding(curwin->w_topline, &first, NULL))
1803 {
1804 ++done;
1805 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001806 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001807 curwin->w_botline -= curwin->w_topline - first;
1808 curwin->w_topline = first;
1809 }
1810 else
1811#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001812 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001813 {
1814 int size = win_linetabsize(curwin, curwin->w_topline,
1815 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1816 if (size > width1)
1817 {
1818 curwin->w_skipcol = width1;
1819 size -= width1;
1820 redraw_later(UPD_NOT_VALID);
1821 }
1822 while (size > width2)
1823 {
1824 curwin->w_skipcol += width2;
1825 size -= width2;
1826 }
1827 ++done;
1828 }
1829 else
1830 done += PLINES_NOFILL(curwin->w_topline);
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001833 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 invalidate_botline();
1835 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001836 curwin->w_wrow += done; // keep w_wrow updated
1837 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839#ifdef FEAT_DIFF
1840 if (curwin->w_cursor.lnum == curwin->w_topline)
1841 curwin->w_cline_row = 0;
1842 check_topfill(curwin, TRUE);
1843#endif
1844
1845 /*
1846 * Compute the row number of the last row of the cursor line
1847 * and move the cursor onto the displayed part of the window.
1848 */
1849 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001850 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 validate_virtcol();
1853 validate_cheight();
1854 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001855 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1858 {
1859#ifdef FEAT_FOLDING
1860 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1861 {
1862 --wrow;
1863 if (first == 1)
1864 curwin->w_cursor.lnum = 1;
1865 else
1866 curwin->w_cursor.lnum = first - 1;
1867 }
1868 else
1869#endif
1870 wrow -= plines(curwin->w_cursor.lnum--);
1871 curwin->w_valid &=
1872 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1873 moved = TRUE;
1874 }
1875 if (moved)
1876 {
1877#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001878 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 foldAdjustCursor();
1880#endif
1881 coladvance(curwin->w_curswant);
1882 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001883 if (curwin->w_cursor.lnum < curwin->w_topline)
1884 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885}
1886
1887/*
1888 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001891scrollup(
1892 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001893 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001895 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001897 if (do_sms
1898# ifdef FEAT_FOLDING
1899 || (byfold && hasAnyFolding(curwin))
1900# endif
1901# ifdef FEAT_DIFF
1902 || (curwin->w_p_diff && !curwin->w_p_wrap)
1903# endif
1904 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001906 int width1 = curwin->w_width - curwin_col_off();
1907 int width2 = width1 + curwin_col_off2();
1908 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001909 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001910
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001911 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001912 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001913
1914 // diff mode: first consume "topfill"
1915 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1916 // the line, then advance to the next line.
1917 // folding: count each sequence of folded lines as one logical line.
1918 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920# ifdef FEAT_DIFF
1921 if (curwin->w_topfill > 0)
1922 --curwin->w_topfill;
1923 else
1924# endif
1925 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001926 linenr_T lnum = curwin->w_topline;
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928# ifdef FEAT_FOLDING
1929 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001930 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 (void)hasFolding(lnum, NULL, &lnum);
1932# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001933 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001934 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001935 // 'smoothscroll': increase "w_skipcol" until it goes over
1936 // the end of the line, then advance to the next line.
1937 int add = curwin->w_skipcol > 0 ? width2 : width1;
1938 curwin->w_skipcol += add;
1939 if (curwin->w_skipcol >= size)
1940 {
1941 if (lnum == curbuf->b_ml.ml_line_count)
1942 {
1943 // at the last screen line, can't scroll further
1944 curwin->w_skipcol -= add;
1945 break;
1946 }
1947 ++lnum;
1948 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001949 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001950 else
1951 {
1952 if (lnum >= curbuf->b_ml.ml_line_count)
1953 break;
1954 ++lnum;
1955 }
1956
1957 if (lnum > curwin->w_topline)
1958 {
1959 // approximate w_botline
1960 curwin->w_botline += lnum - curwin->w_topline;
1961 curwin->w_topline = lnum;
1962# ifdef FEAT_DIFF
1963 curwin->w_topfill = diff_check_fill(curwin, lnum);
1964# endif
1965 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001966 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001967 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001968 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001969 }
1970 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001971
zeertzjqd9a92dc2023-06-05 18:41:35 +01001972 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1973 // need to redraw more, because wl_size of the (new) topline may
1974 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001975 redraw_later(UPD_NOT_VALID);
1976 }
1977 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001980 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1984 curwin->w_topline = curbuf->b_ml.ml_line_count;
1985 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1986 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1987
1988#ifdef FEAT_DIFF
1989 check_topfill(curwin, FALSE);
1990#endif
1991
1992#ifdef FEAT_FOLDING
1993 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001994 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1996#endif
1997
1998 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1999 if (curwin->w_cursor.lnum < curwin->w_topline)
2000 {
2001 curwin->w_cursor.lnum = curwin->w_topline;
2002 curwin->w_valid &=
2003 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
2004 coladvance(curwin->w_curswant);
2005 }
2006}
2007
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002008/*
2009 * Called after changing the cursor column: make sure that curwin->w_skipcol is
2010 * valid for 'smoothscroll'.
2011 */
2012 void
2013adjust_skipcol(void)
2014{
2015 if (!curwin->w_p_wrap
2016 || !curwin->w_p_sms
2017 || curwin->w_cursor.lnum != curwin->w_topline)
2018 return;
2019
2020 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00002021 if (width1 <= 0)
2022 return; // no text will be displayed
2023
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002024 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002025 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002026 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
2027 int scrolled = FALSE;
2028
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002029 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00002030 if (curwin->w_cline_height == curwin->w_height
2031 // w_cline_height may be capped at w_height, check there aren't
2032 // actually more lines.
2033 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
2034 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002035 {
2036 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002037 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002038 return;
2039 }
2040
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002041 validate_virtcol();
Luuk van Baalcb204e62024-04-02 20:49:45 +02002042 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002043 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01002044 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002045 {
2046 // scroll a screen line down
2047 if (curwin->w_skipcol >= width1 + width2)
2048 curwin->w_skipcol -= width2;
2049 else
2050 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002051 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002052 }
2053 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01002054 {
2055 validate_virtcol();
2056 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002057 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002058 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002059
2060 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2061 int row = 0;
2062 if (col >= width1)
2063 {
2064 col -= width1;
2065 ++row;
2066 }
2067 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002068 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002069 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002070 // col may no longer be used, but make
2071 // sure it is correct anyhow, just in case
2072 col = col % width2;
2073 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002074 if (row >= curwin->w_height)
2075 {
2076 if (curwin->w_skipcol == 0)
2077 {
2078 curwin->w_skipcol += width1;
2079 --row;
2080 }
2081 if (row >= curwin->w_height)
2082 curwin->w_skipcol += (row - curwin->w_height) * width2;
2083 redraw_later(UPD_NOT_VALID);
2084 }
2085}
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087#ifdef FEAT_DIFF
2088/*
2089 * Don't end up with too many filler lines in the window.
2090 */
2091 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002092check_topfill(
2093 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002094 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
2096 int n;
2097
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002098 if (wp->w_topfill <= 0)
2099 return;
2100
2101 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2102 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002104 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002106 --wp->w_topline;
2107 wp->w_topfill = 0;
2108 }
2109 else
2110 {
2111 wp->w_topfill = wp->w_height - n;
2112 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115 }
2116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119/*
2120 * Scroll the screen one line down, but don't do it if it would move the
2121 * cursor off the screen.
2122 */
2123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 int end_row;
2127#ifdef FEAT_DIFF
2128 int can_fill = (curwin->w_topfill
2129 < diff_check_fill(curwin, curwin->w_topline));
2130#endif
2131
2132 if (curwin->w_topline <= 1
2133#ifdef FEAT_DIFF
2134 && !can_fill
2135#endif
2136 )
2137 return;
2138
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
2142 * Compute the row number of the last row of the cursor line
2143 * and make sure it doesn't go off the screen. Make sure the cursor
2144 * doesn't go past 'scrolloff' lines from the screen end.
2145 */
2146 end_row = curwin->w_wrow;
2147#ifdef FEAT_DIFF
2148 if (can_fill)
2149 ++end_row;
2150 else
2151 end_row += plines_nofill(curwin->w_topline - 1);
2152#else
2153 end_row += plines(curwin->w_topline - 1);
2154#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002155 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 validate_cheight();
2158 validate_virtcol();
2159 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002160 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002162 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164#ifdef FEAT_DIFF
2165 if (can_fill)
2166 {
2167 ++curwin->w_topfill;
2168 check_topfill(curwin, TRUE);
2169 }
2170 else
2171 {
2172 --curwin->w_topline;
2173 curwin->w_topfill = 0;
2174 }
2175#else
2176 --curwin->w_topline;
2177#endif
2178#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002179 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002181 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2183 }
2184}
2185
2186/*
2187 * Scroll the screen one line up, but don't do it if it would move the cursor
2188 * off the screen.
2189 */
2190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002191scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 int start_row;
2194
2195 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2196#ifdef FEAT_DIFF
2197 && curwin->w_topfill == 0
2198#endif
2199 )
2200 return;
2201
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 /*
2205 * Compute the row number of the first row of the cursor line
2206 * and make sure it doesn't go off the screen. Make sure the cursor
2207 * doesn't go before 'scrolloff' lines from the screen start.
2208 */
2209#ifdef FEAT_DIFF
2210 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2211 - curwin->w_topfill;
2212#else
2213 start_row = curwin->w_wrow - plines(curwin->w_topline);
2214#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002215 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
2217 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002218 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002220 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222#ifdef FEAT_DIFF
2223 if (curwin->w_topfill > 0)
2224 --curwin->w_topfill;
2225 else
2226#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002227 {
2228#ifdef FEAT_FOLDING
2229 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002232 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2235 }
2236}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238/*
2239 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2240 * a (wrapped) text line. Uses and sets "lp->fill".
2241 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002242 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
2244 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002245topline_back_winheight(
2246 lineoff_T *lp,
2247 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249#ifdef FEAT_DIFF
2250 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++lp->fill;
2254 lp->height = 1;
2255 }
2256 else
2257#endif
2258 {
2259 --lp->lnum;
2260#ifdef FEAT_DIFF
2261 lp->fill = 0;
2262#endif
2263 if (lp->lnum < 1)
2264 lp->height = MAXCOL;
2265 else
2266#ifdef FEAT_FOLDING
2267 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002268 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 lp->height = 1;
2270 else
2271#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002272 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
2274}
2275
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002276 static void
2277topline_back(lineoff_T *lp)
2278{
2279 topline_back_winheight(lp, TRUE);
2280}
2281
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283/*
2284 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2285 * a (wrapped) text line. Uses and sets "lp->fill".
2286 * Returns the height of the added line in "lp->height".
2287 * Lines below the last one are incredibly high.
2288 */
2289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292#ifdef FEAT_DIFF
2293 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2294 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ++lp->fill;
2297 lp->height = 1;
2298 }
2299 else
2300#endif
2301 {
2302 ++lp->lnum;
2303#ifdef FEAT_DIFF
2304 lp->fill = 0;
2305#endif
2306 if (lp->lnum > curbuf->b_ml.ml_line_count)
2307 lp->height = MAXCOL;
2308 else
2309#ifdef FEAT_FOLDING
2310 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002311 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 lp->height = 1;
2313 else
2314#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002315 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317}
2318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319/*
2320 * Recompute topline to put the cursor at the top of the window.
2321 * Scroll at least "min_scroll" lines.
2322 * If "always" is TRUE, always set topline (for "zt").
2323 */
2324 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002325scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 int scrolled = 0;
2328 int extra = 0;
2329 int used;
2330 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002331 linenr_T top; // just above displayed lines
2332 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002334 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#ifdef FEAT_DIFF
2336 linenr_T old_topfill = curwin->w_topfill;
2337#endif
2338 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002339 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (mouse_dragging > 0)
2342 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 /*
2345 * Decrease topline until:
2346 * - it has become 1
2347 * - (part of) the cursor line is moved off the screen or
2348 * - moved at least 'scrolljump' lines and
2349 * - at least 'scrolloff' lines above and below the cursor
2350 */
2351 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (curwin->w_cursor.lnum < curwin->w_topline)
2354 scrolled = used;
2355
2356#ifdef FEAT_FOLDING
2357 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2358 {
2359 --top;
2360 ++bot;
2361 }
2362 else
2363#endif
2364 {
2365 top = curwin->w_cursor.lnum - 1;
2366 bot = curwin->w_cursor.lnum + 1;
2367 }
2368 new_topline = top + 1;
2369
2370#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002371 // "used" already contains the number of filler lines above, don't add it
2372 // again.
2373 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002374 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375#endif
2376
2377 /*
2378 * Check if the lines from "top" to "bot" fit in the window. If they do,
2379 * set new_topline and advance "top" and "bot" to include more lines.
2380 */
2381 while (top > 0)
2382 {
2383#ifdef FEAT_FOLDING
2384 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002385 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 i = 1;
2387 else
2388#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002389 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002390 if (top < curwin->w_topline)
2391 scrolled += i;
2392
2393 // If scrolling is needed, scroll at least 'sj' lines.
2394 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2395 && extra >= off)
2396 break;
2397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 used += i;
2399 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2400 {
2401#ifdef FEAT_FOLDING
2402 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002403 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 ++used;
2405 else
2406#endif
2407 used += plines(bot);
2408 }
2409 if (used > curwin->w_height)
2410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 extra += i;
2413 new_topline = top;
2414 --top;
2415 ++bot;
2416 }
2417
2418 /*
2419 * If we don't have enough space, put cursor in the middle.
2420 * This makes sure we get the same position when using "k" and "j"
2421 * in a small window.
2422 */
2423 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002424 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else
2426 {
2427 /*
2428 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002429 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
2431 if (new_topline < curwin->w_topline || always)
2432 curwin->w_topline = new_topline;
2433 if (curwin->w_topline > curwin->w_cursor.lnum)
2434 curwin->w_topline = curwin->w_cursor.lnum;
2435#ifdef FEAT_DIFF
2436 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2437 if (curwin->w_topfill > 0 && extra > off)
2438 {
2439 curwin->w_topfill -= extra - off;
2440 if (curwin->w_topfill < 0)
2441 curwin->w_topfill = 0;
2442 }
2443 check_topfill(curwin, FALSE);
2444#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002445 if (curwin->w_topline != old_topline)
2446 reset_skipcol();
2447 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002448 {
2449 validate_virtcol();
2450 if (curwin->w_skipcol >= curwin->w_virtcol)
2451 // TODO: if the line doesn't fit may optimize w_skipcol instead
2452 // of making it zero
2453 reset_skipcol();
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002456 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#ifdef FEAT_DIFF
2458 || curwin->w_topfill != old_topfill
2459#endif
2460 )
2461 curwin->w_valid &=
2462 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2463 curwin->w_valid |= VALID_TOPLINE;
2464 }
2465}
2466
2467/*
2468 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2469 * screen lines for text lines.
2470 */
2471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002472set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474#ifdef FEAT_DIFF
2475 wp->w_filler_rows = 0;
2476#endif
2477 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002478 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 else
2480 {
2481 wp->w_empty_rows = wp->w_height - used;
2482#ifdef FEAT_DIFF
2483 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2484 {
2485 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2486 if (wp->w_empty_rows > wp->w_filler_rows)
2487 wp->w_empty_rows -= wp->w_filler_rows;
2488 else
2489 {
2490 wp->w_filler_rows = wp->w_empty_rows;
2491 wp->w_empty_rows = 0;
2492 }
2493 }
2494#endif
2495 }
2496}
2497
2498/*
2499 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002500 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2502 * This is messy stuff!!!
2503 */
2504 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002505scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507 int used;
2508 int scrolled = 0;
2509 int extra = 0;
2510 int i;
2511 linenr_T line_count;
2512 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002513 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 lineoff_T loff;
2515 lineoff_T boff;
2516#ifdef FEAT_DIFF
2517 int old_topfill = curwin->w_topfill;
2518 int fill_below_window;
2519#endif
2520 linenr_T old_botline = curwin->w_botline;
2521 linenr_T old_valid = curwin->w_valid;
2522 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002523 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002524 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002525 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 cln = curwin->w_cursor.lnum;
2528 if (set_topbot)
2529 {
2530 used = 0;
2531 curwin->w_botline = cln + 1;
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002532 loff.lnum = cln + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#ifdef FEAT_DIFF
2534 loff.fill = 0;
2535#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002536 while (TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002538 topline_back_winheight(&loff, FALSE);
2539 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002541 if (used + loff.height > curwin->w_height)
2542 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002543 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002544 {
2545 // 'smoothscroll' and 'wrap' are set. The above line is
2546 // too long to show in its entirety, so we show just a part
2547 // of it.
2548 if (used < curwin->w_height)
2549 {
2550 int plines_offset = used + loff.height
2551 - curwin->w_height;
2552 used = curwin->w_height;
2553#ifdef FEAT_DIFF
2554 curwin->w_topfill = loff.fill;
2555#endif
2556 curwin->w_topline = loff.lnum;
2557 curwin->w_skipcol = skipcol_from_plines(
2558 curwin, plines_offset);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002559 }
2560 }
2561 break;
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#ifdef FEAT_DIFF
2564 curwin->w_topfill = loff.fill;
2565#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002566 curwin->w_topline = loff.lnum;
2567 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 set_empty_rows(curwin, used);
2571 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2572 if (curwin->w_topline != old_topline
2573#ifdef FEAT_DIFF
2574 || curwin->w_topfill != old_topfill
2575#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002576 || curwin->w_skipcol != old_skipcol
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002577 || curwin->w_skipcol != 0)
2578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002580 if (curwin->w_skipcol != old_skipcol)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002581 redraw_later(UPD_NOT_VALID);
2582 else
2583 reset_skipcol();
2584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 else
2587 validate_botline();
2588
Bram Moolenaar85a20022019-12-21 18:25:54 +01002589 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#ifdef FEAT_DIFF
2591 used = plines_nofill(cln);
2592#else
2593 validate_cheight();
2594 used = curwin->w_cline_height;
2595#endif
2596
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002597 // If the cursor is on or below botline, we will at least scroll by the
2598 // height of the cursor line, which is "used". Correct for empty lines,
2599 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (cln >= curwin->w_botline)
2601 {
2602 scrolled = used;
2603 if (cln == curwin->w_botline)
2604 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002605 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002606 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002607 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002608 // Calculate how many screen lines the current top line of window
2609 // occupies. If it is occupying more than the entire window, we
2610 // need to scroll the additional clipped lines to scroll past the
2611 // top line before we can move on to the other lines.
2612 int top_plines =
2613#ifdef FEAT_DIFF
2614 plines_win_nofill
2615#else
2616 plines_win
2617#endif
2618 (curwin, curwin->w_topline, FALSE);
2619 int skip_lines = 0;
2620 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002621 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002622 {
fullwaywang8154e642023-06-24 21:58:09 +01002623 int width2 = width1 + curwin_col_off2();
2624 // similar formula is used in curs_columns()
2625 if (curwin->w_skipcol > width1)
2626 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2627 else if (curwin->w_skipcol > 0)
2628 skip_lines = 1;
2629
2630 top_plines -= skip_lines;
2631 if (top_plines > curwin->w_height)
2632 {
2633 scrolled += (top_plines - curwin->w_height);
2634 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002635 }
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638
2639 /*
2640 * Stop counting lines to scroll when
2641 * - hitting start of the file
2642 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002643 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * - lines between botline and cursor have been counted
2645 */
2646#ifdef FEAT_FOLDING
2647 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2648#endif
2649 {
2650 loff.lnum = cln;
2651 boff.lnum = cln;
2652 }
2653#ifdef FEAT_DIFF
2654 loff.fill = 0;
2655 boff.fill = 0;
2656 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2657 - curwin->w_filler_rows;
2658#endif
2659
2660 while (loff.lnum > 1)
2661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2663 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002665 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2667 && loff.lnum <= curwin->w_botline
2668#ifdef FEAT_DIFF
2669 && (loff.lnum < curwin->w_botline
2670 || loff.fill >= fill_below_window)
2671#endif
2672 )
2673 break;
2674
Bram Moolenaar85a20022019-12-21 18:25:54 +01002675 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002677 if (loff.height == MAXCOL)
2678 used = MAXCOL;
2679 else
2680 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (used > curwin->w_height)
2682 break;
2683 if (loff.lnum >= curwin->w_botline
2684#ifdef FEAT_DIFF
2685 && (loff.lnum > curwin->w_botline
2686 || loff.fill <= fill_below_window)
2687#endif
2688 )
2689 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002690 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 scrolled += loff.height;
2692 if (loff.lnum == curwin->w_botline
2693#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002694 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#endif
2696 )
2697 scrolled -= curwin->w_empty_rows;
2698 }
2699
2700 if (boff.lnum < curbuf->b_ml.ml_line_count)
2701 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 botline_forw(&boff);
2704 used += boff.height;
2705 if (used > curwin->w_height)
2706 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002707 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2708 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 extra += boff.height;
2711 if (boff.lnum >= curwin->w_botline
2712#ifdef FEAT_DIFF
2713 || (boff.lnum + 1 == curwin->w_botline
2714 && boff.fill > curwin->w_filler_rows)
2715#endif
2716 )
2717 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 scrolled += boff.height;
2720 if (boff.lnum == curwin->w_botline
2721#ifdef FEAT_DIFF
2722 && boff.fill == 0
2723#endif
2724 )
2725 scrolled -= curwin->w_empty_rows;
2726 }
2727 }
2728 }
2729 }
2730
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (scrolled <= 0)
2733 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002734 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 else if (used > curwin->w_height)
2736 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002737 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 else
2739 {
2740 line_count = 0;
2741#ifdef FEAT_DIFF
2742 boff.fill = curwin->w_topfill;
2743#endif
2744 boff.lnum = curwin->w_topline - 1;
2745 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2746 {
2747 botline_forw(&boff);
2748 i += boff.height;
2749 ++line_count;
2750 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002751 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 line_count = 9999;
2753 }
2754
2755 /*
2756 * Scroll up if the cursor is off the bottom of the screen a bit.
2757 * Otherwise put it at 1/2 of the screen.
2758 */
2759 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002760 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002761 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002762 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002763 if (do_sms)
2764 scrollup(scrolled, TRUE); // TODO
2765 else
2766 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * If topline didn't change we need to restore w_botline and w_empty_rows
2771 * (we changed them).
2772 * If topline did change, update_screen() will set botline.
2773 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002774 if (curwin->w_topline == old_topline
2775 && curwin->w_skipcol == old_skipcol
2776 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
2778 curwin->w_botline = old_botline;
2779 curwin->w_empty_rows = old_empty_rows;
2780 curwin->w_valid = old_valid;
2781 }
2782 curwin->w_valid |= VALID_TOPLINE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02002783
2784 cursor_correct_sms();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785}
2786
2787/*
2788 * Recompute topline to put the cursor halfway the window
2789 * If "atend" is TRUE, also put it halfway at the end of the file.
2790 */
2791 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002792scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 int above = 0;
2795 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002796 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#ifdef FEAT_DIFF
2798 int topfill = 0;
2799#endif
2800 int below = 0;
2801 int used;
2802 lineoff_T loff;
2803 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002804#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002805 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002808#ifdef FEAT_PROP_POPUP
2809 // if the width changed this needs to be updated first
2810 may_update_popup_position();
2811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2813#ifdef FEAT_FOLDING
2814 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2815#endif
2816#ifdef FEAT_DIFF
2817 used = plines_nofill(loff.lnum);
2818 loff.fill = 0;
2819 boff.fill = 0;
2820#else
2821 used = plines(loff.lnum);
2822#endif
2823 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002825 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002826 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2827 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002828 {
2829 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002830 if (atend)
2831 {
2832 want_height = (curwin->w_height - used) / 2;
2833 used = 0;
2834 }
2835 else
2836 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002837 }
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 while (topline > 1)
2840 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002841 // If using smoothscroll, we can precisely scroll to the
2842 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002843 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002844 {
2845 topline_back_winheight(&loff, FALSE);
2846 if (loff.height == MAXCOL)
2847 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002848 used += loff.height;
2849 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002850 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002851 botline_forw(&boff);
2852 used += boff.height;
2853 }
2854 if (used > want_height)
2855 {
2856 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002857 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002858 topline = loff.lnum;
2859#ifdef FEAT_DIFF
2860 topfill = loff.fill;
2861#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002862 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002863 }
2864 break;
2865 }
2866 topline = loff.lnum;
2867#ifdef FEAT_DIFF
2868 topfill = loff.fill;
2869#endif
2870 continue;
2871 }
2872
2873 // If not using smoothscroll, we have to iteratively find how many
2874 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002875 // This may not be right in the middle if the lines'
2876 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002877
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002878 // Depending on "prefer_above" we add a line above or below first.
2879 // Loop twice to avoid duplicating code.
2880 int done = FALSE;
2881 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883 if (prefer_above ? (round == 2 && below < above)
2884 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002886 // add a line below the cursor
2887 if (boff.lnum < curbuf->b_ml.ml_line_count)
2888 {
2889 botline_forw(&boff);
2890 used += boff.height;
2891 if (used > curwin->w_height)
2892 {
2893 done = TRUE;
2894 break;
2895 }
2896 below += boff.height;
2897 }
2898 else
2899 {
2900 ++below; // count a "~" line
2901 if (atend)
2902 ++used;
2903 }
2904 }
2905
2906 if (prefer_above ? (round == 1 && below >= above)
2907 : (round == 1 && below > above))
2908 {
2909 // add a line above the cursor
2910 topline_back(&loff);
2911 if (loff.height == MAXCOL)
2912 used = MAXCOL;
2913 else
2914 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002916 {
2917 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002919 }
2920 above += loff.height;
2921 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002923 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002927 if (done)
2928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#ifdef FEAT_FOLDING
2932 if (!hasFolding(topline, &curwin->w_topline, NULL))
2933#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002934 {
2935 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002936 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002937 || curwin->w_skipcol != 0)
2938 {
2939 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002940 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002941 {
2942 curwin->w_skipcol = skipcol;
2943 redraw_later(UPD_NOT_VALID);
2944 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002945 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002946 reset_skipcol();
2947 }
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#ifdef FEAT_DIFF
2950 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002951 if (old_topline > curwin->w_topline + curwin->w_height)
2952 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 check_topfill(curwin, FALSE);
2954#endif
2955 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2956 curwin->w_valid |= VALID_TOPLINE;
2957}
2958
2959/*
2960 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002961 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 * If not possible, put it at the same position as scroll_cursor_halfway().
2963 * When called topline must be valid!
2964 */
2965 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002966cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 linenr_T botline;
2972 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002973 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002975 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
2977 /*
2978 * How many lines we would like to have above/below the cursor depends on
2979 * whether the first/last line of the file is on screen.
2980 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002981 above_wanted = so;
2982 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002983 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
2985 above_wanted = mouse_dragging - 1;
2986 below_wanted = mouse_dragging - 1;
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if (curwin->w_topline == 1)
2989 {
2990 above_wanted = 0;
2991 max_off = curwin->w_height / 2;
2992 if (below_wanted > max_off)
2993 below_wanted = max_off;
2994 }
2995 validate_botline();
2996 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002997 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
2999 below_wanted = 0;
3000 max_off = (curwin->w_height - 1) / 2;
3001 if (above_wanted > max_off)
3002 above_wanted = max_off;
3003 }
3004
3005 /*
3006 * If there are sufficient file-lines above and below the cursor, we can
3007 * return now.
3008 */
3009 cln = curwin->w_cursor.lnum;
3010 if (cln >= curwin->w_topline + above_wanted
3011 && cln < curwin->w_botline - below_wanted
3012#ifdef FEAT_FOLDING
3013 && !hasAnyFolding(curwin)
3014#endif
3015 )
3016 return;
3017
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003018 if (curwin->w_p_sms && !curwin->w_p_wrap)
3019 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003020 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003021 if (curwin->w_cline_height == curwin->w_height)
3022 {
3023 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003024 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003025 return;
3026 }
3027 // TODO: If the cursor line doesn't fit in the window then only adjust
3028 // w_skipcol.
3029 }
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 /*
3032 * Narrow down the area where the cursor can be put by taking lines from
3033 * the top and the bottom until:
3034 * - the desired context lines are found
3035 * - the lines from the top is past the lines from the bottom
3036 */
3037 topline = curwin->w_topline;
3038 botline = curwin->w_botline - 1;
3039#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003040 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 above = curwin->w_topfill;
3042 below = curwin->w_filler_rows;
3043#endif
3044 while ((above < above_wanted || below < below_wanted) && topline < botline)
3045 {
3046 if (below < below_wanted && (below <= above || above >= above_wanted))
3047 {
3048#ifdef FEAT_FOLDING
3049 if (hasFolding(botline, &botline, NULL))
3050 ++below;
3051 else
3052#endif
3053 below += plines(botline);
3054 --botline;
3055 }
3056 if (above < above_wanted && (above < below || below >= below_wanted))
3057 {
3058#ifdef FEAT_FOLDING
3059 if (hasFolding(topline, NULL, &topline))
3060 ++above;
3061 else
3062#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003063 above += PLINES_NOFILL(topline);
3064#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003065 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if (topline < botline)
3067 above += diff_check_fill(curwin, topline + 1);
3068#endif
3069 ++topline;
3070 }
3071 }
3072 if (topline == botline || botline == 0)
3073 curwin->w_cursor.lnum = topline;
3074 else if (topline > botline)
3075 curwin->w_cursor.lnum = botline;
3076 else
3077 {
3078 if (cln < topline && curwin->w_topline > 1)
3079 {
3080 curwin->w_cursor.lnum = topline;
3081 curwin->w_valid &=
3082 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3083 }
3084 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3085 {
3086 curwin->w_cursor.lnum = botline;
3087 curwin->w_valid &=
3088 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3089 }
3090 }
3091 curwin->w_valid |= VALID_TOPLINE;
3092}
3093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003095 * Decide how much overlap to use for page-up or page-down scrolling.
3096 * This is symmetric, so that doing both keeps the same lines displayed.
3097 * Three lines are examined:
3098 *
3099 * before CTRL-F after CTRL-F / before CTRL-B
3100 * etc. l1
3101 * l1 last but one line ------------
3102 * l2 last text line l2 top text line
3103 * ------------- l3 second text line
3104 * l3 etc.
3105 */
3106static int get_scroll_overlap(int dir)
3107{
3108 lineoff_T loff;
3109 int min_height = curwin->w_height - 2;
3110
3111 validate_botline();
Luuk van Baalbd28cae2024-04-03 22:50:40 +02003112 if ((dir == BACKWARD && curwin->w_topline == 1)
3113 || (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003114 return min_height + 2; // no overlap, still handle 'smoothscroll'
3115
3116 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3117#ifdef FEAT_DIFF
zeertzjq92325542024-04-12 18:38:38 +02003118 loff.fill = diff_check_fill(curwin, loff.lnum + (dir == BACKWARD))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003119 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3120 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3121#else
3122 loff.height = plines(loff.lnum);
3123#endif
3124
3125 int h1 = loff.height;
3126 if (h1 > min_height)
3127 return min_height + 2; // no overlap
3128 if (dir == FORWARD)
3129 topline_back(&loff);
3130 else
3131 botline_forw(&loff);
3132
3133 int h2 = loff.height;
3134 if (h2 == MAXCOL || h2 + h1 > min_height)
3135 return min_height + 2; // no overlap
3136 if (dir == FORWARD)
3137 topline_back(&loff);
3138 else
3139 botline_forw(&loff);
3140
3141 int h3 = loff.height;
3142 if (h3 == MAXCOL || h3 + h2 > min_height)
3143 return min_height + 2; // no overlap
3144 if (dir == FORWARD)
3145 topline_back(&loff);
3146 else
3147 botline_forw(&loff);
3148
3149 int h4 = loff.height;
3150 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3151 return min_height + 1; // 1 line overlap
3152 else
3153 return min_height; // 2 lines overlap
3154}
3155
3156/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02003157 * Scroll "count" lines with 'smoothscroll' in direction "dir". Return TRUE
3158 * when scrolling happened.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003159 */
Luuk van Baal9148ba82024-04-08 22:27:41 +02003160static int scroll_with_sms(int dir, long count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003161{
3162 int prev_sms = curwin->w_p_sms;
3163 colnr_T prev_skipcol = curwin->w_skipcol;
3164 linenr_T prev_topline = curwin->w_topline;
3165#ifdef FEAT_DIFF
3166 int prev_topfill = curwin->w_topfill;
3167#endif
3168
3169 curwin->w_p_sms = TRUE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003170 scroll_redraw(dir == FORWARD, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003171
3172 // Not actually smoothscrolling but ended up with partially visible line.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003173 // Continue scrolling until skipcol is zero.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003174 if (!prev_sms && curwin->w_skipcol > 0)
3175 {
3176 int fixdir = dir;
3177 // Reverse the scroll direction when topline already changed. One line
3178 // extra for scrolling backward so that consuming skipcol is symmetric.
3179 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3180 fixdir = dir * -1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003181 while (curwin->w_skipcol > 0
3182 && curwin->w_topline < curbuf->b_ml.ml_line_count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003183 scroll_redraw(fixdir == FORWARD, 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003184 }
3185 curwin->w_p_sms = prev_sms;
3186
3187 return curwin->w_topline == prev_topline
3188#ifdef FEAT_DIFF
3189 && curwin->w_topfill == prev_topfill
3190#endif
3191 && curwin->w_skipcol == prev_skipcol;
3192}
3193
3194/*
3195 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3196 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3197 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003199 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003202pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003204 int nochange = TRUE;
3205 int buflen = curbuf->b_ml.ml_line_count;
3206 colnr_T prev_col = curwin->w_cursor.col;
Luuk van Baal78c51502024-04-09 21:30:19 +02003207 colnr_T prev_curswant = curwin->w_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003208 linenr_T prev_lnum = curwin->w_cursor.lnum;
3209 oparg_T oa = { 0 };
3210 cmdarg_T ca = { 0 };
3211 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003213 if (half)
3214 {
3215 // Scroll [count], 'scroll' or current window height lines.
3216 if (count)
3217 curwin->w_p_scr = MIN(curwin->w_height, count);
3218 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Luuk van Baal9148ba82024-04-08 22:27:41 +02003220 int curscount = count;
3221 // Adjust count so as to not reveal end of buffer lines.
Luuk van Baal32d701f2024-04-28 16:24:02 +02003222 if (dir == FORWARD
3223 && (curwin->w_topline + curwin->w_height + count > buflen
3224#ifdef FEAT_FOLDING
3225 || hasAnyFolding(curwin)
3226#endif
3227 ))
Luuk van Baalcb204e62024-04-02 20:49:45 +02003228 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003229 int n = plines_correct_topline(curwin, curwin->w_topline, FALSE);
3230 if (n - count < curwin->w_height && curwin->w_topline < buflen)
Luuk van Baal32d701f2024-04-28 16:24:02 +02003231 n += plines_m_win(curwin, curwin->w_topline + 1, buflen,
3232 curwin->w_height + count);
3233 if (n < curwin->w_height + count)
Luuk van Baal9148ba82024-04-08 22:27:41 +02003234 count = n - curwin->w_height;
3235 }
3236
Luuk van Baal78c51502024-04-09 21:30:19 +02003237 // (Try to) scroll the window unless already at the end of the buffer.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003238 if (count > 0)
3239 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003240 nochange = scroll_with_sms(dir, count);
Luuk van Baal78c51502024-04-09 21:30:19 +02003241 curwin->w_cursor.lnum = prev_lnum;
3242 curwin->w_cursor.col = prev_col;
3243 curwin->w_curswant = prev_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Luuk van Baal78c51502024-04-09 21:30:19 +02003246 // Move the cursor the same amount of screen lines.
3247 if (curwin->w_p_wrap)
3248 nv_screengo(&oa, dir, curscount);
3249 else if (dir == FORWARD)
3250 cursor_down_inner(curwin, curscount);
3251 else
3252 cursor_up_inner(curwin, curscount);
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003253 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003254 else
3255 {
3256 // Scroll [count] times 'window' or current window height lines.
3257 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3258 MAX(1, p_window - 2) : get_scroll_overlap(dir));
Luuk van Baal9148ba82024-04-08 22:27:41 +02003259 nochange = scroll_with_sms(dir, count);
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003260
3261 // Place cursor at top or bottom of window.
3262 validate_botline();
3263 curwin->w_cursor.lnum = (dir == FORWARD ? curwin->w_topline
3264 : curwin->w_botline - 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003265 }
3266
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003267 if (get_scrolloff_value() > 0)
3268 cursor_correct();
3269#ifdef FEAT_FOLDING
3270 // Move cursor to first line of closed fold.
3271 foldAdjustCursor();
3272#endif
3273 nochange = nochange
3274 && prev_col == curwin->w_cursor.col
3275 && prev_lnum == curwin->w_cursor.lnum;
3276
Luuk van Baalcb204e62024-04-02 20:49:45 +02003277 // Error if both the viewport and cursor did not change.
3278 if (nochange)
3279 beep_flush();
3280 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003281 beginline(BL_SOL | BL_FIX);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003282 else if (p_sol)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003283 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003284
3285 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
3287
Bram Moolenaar860cae12010-06-05 23:22:07 +02003288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003290{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003291 static win_T *prev_curwin = NULL;
3292 static pos_T prev_cursor = {0, 0, 0};
3293
3294 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3295 return;
3296 prev_curwin = curwin;
3297 prev_cursor = curwin->w_cursor;
3298
Bram Moolenaar860cae12010-06-05 23:22:07 +02003299 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003300 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003301 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003302 colnr_T curswant = curwin->w_curswant;
3303 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003304 win_T *old_curwin = curwin;
3305 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003306 int old_VIsual_select = VIsual_select;
3307 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308
3309 /*
3310 * loop through the cursorbound windows
3311 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003313 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003314 {
3315 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003316 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317 if (curwin != old_curwin && curwin->w_p_crb)
3318 {
3319# ifdef FEAT_DIFF
3320 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003321 curwin->w_cursor.lnum =
3322 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003323 else
3324# endif
3325 curwin->w_cursor.lnum = line;
3326 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003327 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003328 curwin->w_curswant = curswant;
3329 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003330
Bram Moolenaar85a20022019-12-21 18:25:54 +01003331 // Make sure the cursor is in a valid position. Temporarily set
3332 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003333 int restart_edit_save = restart_edit;
3334 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003335 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003336
3337 // Avoid a scroll here for the cursor position, 'scrollbind' is
3338 // more important.
3339 if (!curwin->w_p_scb)
3340 validate_cursor();
3341
Bram Moolenaar61452852011-02-01 18:01:11 +01003342 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003344 if (has_mbyte)
3345 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003346 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003347
Bram Moolenaar85a20022019-12-21 18:25:54 +01003348 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003349 if (!curwin->w_p_scb)
3350 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003351 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352 }
3353 }
3354
3355 /*
3356 * reset current-window
3357 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003358 VIsual_select = old_VIsual_select;
3359 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003360 curwin = old_curwin;
3361 curbuf = old_curbuf;
3362}