blob: dd232686e9985406dc316780c79e2bbaa3f996ab [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
zeertzjq3f1b5312024-02-06 10:43:36 +010022static void redraw_for_cursorline(win_T *wp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
Bram Moolenaar85a20022019-12-21 18:25:54 +010029 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010031 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010033 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000034} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
zeertzjq6235a102023-08-19 14:12:42 +020040 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010041 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010042 int
zeertzjq6235a102023-08-19 14:12:42 +020043adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010044{
45 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020046 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047
Bram Moolenaarf6196f42022-10-02 21:29:55 +010048 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010049 int w2 = width + win_col_off2(wp);
50 if (wp->w_skipcol >= width && w2 > 0)
51 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020052
53 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054}
55
56/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010057 * Return how many lines "lnum" will take on the screen, taking into account
58 * whether it is the first line, whether w_skipcol is non-zero and limiting to
59 * the window height.
60 */
61 static int
Luuk van Baal9148ba82024-04-08 22:27:41 +020062plines_correct_topline(win_T *wp, linenr_T lnum, int limit_winheight)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010063{
64 int n;
65#ifdef FEAT_DIFF
66 if (lnum == wp->w_topline)
67 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
68 else
69#endif
70 n = plines_win(wp, lnum, FALSE);
71 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020072 n -= adjust_plines_for_skipcol(wp);
Luuk van Baal9148ba82024-04-08 22:27:41 +020073 if (limit_winheight && n > wp->w_height)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010074 n = wp->w_height;
75 return n;
76}
77
78/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Compute wp->w_botline for the current wp->w_topline. Can be called after
80 * wp->w_topline changed.
81 */
82 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010083comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int n;
86 linenr_T lnum;
87 int done;
88#ifdef FEAT_FOLDING
89 linenr_T last;
90 int folded;
91#endif
92
93 /*
94 * If w_cline_row is valid, start there.
95 * Otherwise have to start at w_topline.
96 */
97 check_cursor_moved(wp);
98 if (wp->w_valid & VALID_CROW)
99 {
100 lnum = wp->w_cursor.lnum;
101 done = wp->w_cline_row;
102 }
103 else
104 {
105 lnum = wp->w_topline;
106 done = 0;
107 }
108
109 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
110 {
111#ifdef FEAT_FOLDING
112 last = lnum;
113 folded = FALSE;
114 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
115 {
116 n = 1;
117 folded = TRUE;
118 }
119 else
120#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200122 n = plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if (
125#ifdef FEAT_FOLDING
126 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
127#else
128 lnum == wp->w_cursor.lnum
129#endif
130 )
131 {
132 wp->w_cline_row = done;
133 wp->w_cline_height = n;
134#ifdef FEAT_FOLDING
135 wp->w_cline_folded = folded;
136#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100137 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
139 }
140 if (done + n > wp->w_height)
141 break;
142 done += n;
143#ifdef FEAT_FOLDING
144 lnum = last;
145#endif
146 }
147
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 wp->w_botline = lnum;
150 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
151
152 set_empty_rows(wp, done);
153}
154
155/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100156 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
157 * set.
158 */
zeertzjq3f1b5312024-02-06 10:43:36 +0100159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100160redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100161{
162 if ((wp->w_p_rnu
163#ifdef FEAT_SYN_HL
164 || wp->w_p_cul
165#endif
166 )
167 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200168 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200169 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000170 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100171 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200172 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100173}
174
zeertzjq3e559cd2022-03-27 19:26:55 +0100175#ifdef FEAT_SYN_HL
176/*
177 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
178 * contains "screenline".
179 */
180 static void
181redraw_for_cursorcolumn(win_T *wp)
182{
183 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
184 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100186 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100187 redraw_win_later(wp, UPD_SOME_VALID);
188 // When 'cursorlineopt' contains "screenline" need to redraw with
189 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100192 }
193}
194#endif
195
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100196/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100197 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
198 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000199 * Parameter "extra2" should be the padding on the 2nd line, not the first
Luuk van Baalcb204e62024-04-02 20:49:45 +0200200 * line. When "extra2" is -1 calculate the padding.
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000201 * Returns the number of columns of overlap with buffer text, excluding the
202 * extra padding on the ledge.
203 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100204 int
205sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000206{
Luuk van Baalcb204e62024-04-02 20:49:45 +0200207 if (extra2 == -1)
208 extra2 = win_col_off(wp) - win_col_off2(wp);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100210 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000211 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000213 return 0;
214#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100215 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
216 if (wp->w_p_list && wp->w_lcs_chars.prec)
217 return 1;
218
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return extra2 > 3 ? 0 : 3 - extra2;
220}
221
222/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000223 * Calculates the skipcol offset for window "wp" given how many
224 * physical lines we want to scroll down.
225 */
226 static int
227skipcol_from_plines(win_T *wp, int plines_off)
228{
229 int width1 = wp->w_width - win_col_off(wp);
230
231 int skipcol = 0;
232 if (plines_off > 0)
233 skipcol += width1;
234 if (plines_off > 1)
235 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
236 return skipcol;
237}
238
239/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100240 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000241 */
242 static void
243reset_skipcol(void)
244{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000245 if (curwin->w_skipcol == 0)
246 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000248 curwin->w_skipcol = 0;
249
250 // Should use the least expensive way that displays all that changed.
251 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
252 // enough when the top line gets another screen line.
253 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000254}
255
256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * Update curwin->w_topline and redraw if necessary.
258 * Used to update the screen before printing a message.
259 */
260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100261update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 update_topline();
264 if (must_redraw)
265 update_screen(0);
266}
267
268/*
269 * Update curwin->w_topline to move the cursor onto the screen.
270 */
271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100272update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 long line_count;
275 int halfheight;
276 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifdef FEAT_FOLDING
278 linenr_T lnum;
279#endif
280 int check_topline = FALSE;
281 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100282 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100283 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100285 // Cursor is updated instead when this is TRUE for 'splitkeep'.
286 if (skip_update_topline)
287 return;
288
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // If there is no valid screen and when the window height is zero just use
290 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200291 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100293 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 curwin->w_topline = curwin->w_cursor.lnum;
295 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200296 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200297 return;
298 }
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 check_cursor_moved(curwin);
301 if (curwin->w_valid & VALID_TOPLINE)
302 return;
303
Bram Moolenaar85a20022019-12-21 18:25:54 +0100304 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000305 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100306 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100308 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100310 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312
313 /*
314 * If the buffer is empty, always set topline to 1.
315 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100316 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
318 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100319 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 curwin->w_botline = 2;
zeertzjqc7a8eb52024-05-08 20:22:40 +0200322 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
326
327 /*
328 * If the cursor is above or near the top of the window, scroll the window
329 * to show the line the cursor is in, with 'scrolloff' context.
330 */
331 else
332 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100333 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100335 // If the cursor is above topline, scrolling is always needed.
336 // If the cursor is far below topline and there is no folding,
337 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (curwin->w_cursor.lnum < curwin->w_topline)
339 check_topline = TRUE;
340 else if (check_top_offset())
341 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100342 else if (curwin->w_skipcol > 0
343 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100344 {
345 colnr_T vcol;
Luuk van Baalcb204e62024-04-02 20:49:45 +0200346 int overlap;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000348 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100349 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100350 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baalcb204e62024-04-02 20:49:45 +0200351 overlap = sms_marker_overlap(curwin, -1);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100352 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100353 check_topline = TRUE;
354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
359 curwin->w_topline))
360 check_topline = TRUE;
361#endif
362
363 if (check_topline)
364 {
365 halfheight = curwin->w_height / 2 - 1;
366 if (halfheight < 2)
367 halfheight = 2;
368
369#ifdef FEAT_FOLDING
370 if (hasAnyFolding(curwin))
371 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 // Count the number of logical lines between the cursor and
373 // topline + scrolloff (approximation of how much will be
374 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 n = 0;
376 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100380 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
382 break;
383 (void)hasFolding(lnum, NULL, &lnum);
384 }
385 }
386 else
387#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100388 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar85a20022019-12-21 18:25:54 +0100390 // If we weren't very close to begin with, we scroll to put the
391 // cursor in the middle of the window. Otherwise put the cursor
392 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000394 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 else
396 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000397 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 check_botline = TRUE;
399 }
400 }
401
402 else
403 {
404#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100405 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
407#endif
408 check_botline = TRUE;
409 }
410 }
411
412 /*
413 * If the cursor is below the bottom of the window, scroll the window
414 * to put the cursor on the window.
415 * When w_botline is invalid, recompute it first, to avoid a redraw later.
416 * If w_botline was approximated, we might need a redraw later in a few
417 * cases, but we don't want to spend (a lot of) time recomputing w_botline
418 * for every small change.
419 */
420 if (check_botline)
421 {
422 if (!(curwin->w_valid & VALID_BOTLINE_AP))
423 validate_botline();
424
425 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
426 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000427 if (curwin->w_cursor.lnum < curwin->w_botline)
428 {
429 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100430 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_FOLDING
432 || hasAnyFolding(curwin)
433#endif
434 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 lineoff_T loff;
437
Bram Moolenaar85a20022019-12-21 18:25:54 +0100438 // Cursor is (a few lines) above botline, check if there are
439 // 'scrolloff' window lines below the cursor. If not, need to
440 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 n = curwin->w_empty_rows;
442 loff.lnum = curwin->w_cursor.lnum;
443#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100444 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
446#endif
447#ifdef FEAT_DIFF
448 loff.fill = 0;
449 n += curwin->w_filler_rows;
450#endif
451 loff.height = 0;
452 while (loff.lnum < curwin->w_botline
453#ifdef FEAT_DIFF
454 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
455#endif
456 )
457 {
458 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100459 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 break;
461 botline_forw(&loff);
462 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100463 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100464 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000466 }
467 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000469 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 }
471 if (check_botline)
472 {
473#ifdef FEAT_FOLDING
474 if (hasAnyFolding(curwin))
475 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100476 // Count the number of logical lines between the cursor and
477 // botline - scrolloff (approximation of how much will be
478 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 line_count = 0;
480 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
483 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100484 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (lnum <= 0 || line_count > curwin->w_height + 1)
486 break;
487 (void)hasFolding(lnum, &lnum, NULL);
488 }
489 }
490 else
491#endif
492 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100493 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000495 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000497 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 }
500 }
501 curwin->w_valid |= VALID_TOPLINE;
502
503 /*
504 * Need to redraw when topline changed.
505 */
506 if (curwin->w_topline != old_topline
507#ifdef FEAT_DIFF
508 || curwin->w_topfill != old_topfill
509#endif
510 )
511 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100512 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000513 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100514
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100515 // When 'smoothscroll' is not set, should reset w_skipcol.
516 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100517 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100518 else if (curwin->w_skipcol != 0)
519 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000520
Bram Moolenaar85a20022019-12-21 18:25:54 +0100521 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (curwin->w_cursor.lnum == curwin->w_topline)
523 validate_cursor();
524 }
525
Bram Moolenaar375e3392019-01-31 18:26:10 +0100526 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000530 * Return the scrolljump value to use for the current window.
531 * When 'scrolljump' is positive use it as-is.
532 * When 'scrolljump' is negative use it as a percentage of the window height.
533 */
534 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000536{
537 if (p_sj >= 0)
538 return (int)p_sj;
539 return (curwin->w_height * -p_sj) / 100;
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
544 * current window.
545 */
546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 lineoff_T loff;
550 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100551 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar375e3392019-01-31 18:26:10 +0100553 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555 || hasAnyFolding(curwin)
556#endif
557 )
558 {
559 loff.lnum = curwin->w_cursor.lnum;
560#ifdef FEAT_DIFF
561 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100562 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#else
564 n = 0;
565#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100567 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (loff.lnum < curwin->w_topline
572#ifdef FEAT_DIFF
573 || (loff.lnum == curwin->w_topline && loff.fill > 0)
574#endif
575 )
576 break;
577 n += loff.height;
578 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100579 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return TRUE;
581 }
582 return FALSE;
583}
584
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100585/*
586 * Update w_curswant.
587 */
588 void
589update_curswant_force(void)
590{
591 validate_virtcol();
592 curwin->w_curswant = curwin->w_virtcol
593#ifdef FEAT_PROP_POPUP
594 - curwin->w_virtcol_first_char
595#endif
596 ;
597 curwin->w_set_curswant = FALSE;
598}
599
600/*
601 * Update w_curswant if w_set_curswant is set.
602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100607 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Check if the cursor has moved. Set the w_valid flag accordingly.
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
617 {
618 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100619 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
620 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 wp->w_valid_cursor = wp->w_cursor;
622 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100623 wp->w_valid_skipcol = wp->w_skipcol;
624 }
625 else if (wp->w_skipcol != wp->w_valid_skipcol)
626 {
627 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
628 |VALID_CHEIGHT|VALID_CROW
629 |VALID_BOTLINE|VALID_BOTLINE_AP);
630 wp->w_valid_cursor = wp->w_cursor;
631 wp->w_valid_leftcol = wp->w_leftcol;
632 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 else if (wp->w_cursor.col != wp->w_valid_cursor.col
635 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100636 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
639 wp->w_valid_cursor.col = wp->w_cursor.col;
640 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643}
644
645/*
646 * Call this function when some window settings have changed, which require
647 * the cursor position, botline and topline to be recomputed and the window to
648 * be redrawn. E.g, when changing the 'wrap' option or folding.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 changed_window_setting_win(curwin);
654}
655
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 wp->w_lines_valid = 0;
660 changed_line_abv_curs_win(wp);
661 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100662 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663}
664
Dominique Pellee764d1b2023-03-12 21:20:59 +0000665#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000667 * Call changed_window_setting_win() for every window containing "buf".
668 */
669 void
670changed_window_setting_buf(buf_T *buf)
671{
672 tabpage_T *tp;
673 win_T *wp;
674
675 FOR_ALL_TAB_WINDOWS(tp, wp)
676 if (wp->w_buffer == buf)
677 changed_window_setting_win(wp);
678}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000679#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000680
681/*
zeertzjq05aacec2024-04-14 18:52:49 +0200682 * Call changed_window_setting_win() for every window.
683 */
684 void
685changed_window_setting_all(void)
686{
687 tabpage_T *tp;
688 win_T *wp;
689
690 FOR_ALL_TAB_WINDOWS(tp, wp)
691 changed_window_setting_win(wp);
692}
693
694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 * Set wp->w_topline to a certain number.
696 */
697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100698set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200700#ifdef FEAT_DIFF
701 linenr_T prev_topline = wp->w_topline;
702#endif
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
707#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100710 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
711 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000713 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200715 if (lnum != prev_topline)
716 // Keep the filler lines when the topline didn't change.
717 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100720 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100721 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
724/*
725 * Call this function when the length of the cursor line (in screen
726 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100727 * If the line length changed the number of screen lines might change,
728 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * Need to take care of w_botline separately!
730 */
731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100732changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
Bram Moolenaar85090142023-06-01 19:27:08 +0100734 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 |VALID_CHEIGHT|VALID_TOPLINE);
736}
737
738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100739changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
Bram Moolenaar85090142023-06-01 19:27:08 +0100741 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 |VALID_CHEIGHT|VALID_TOPLINE);
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * Call this function when the length of a line (in screen characters) above
747 * the cursor have changed.
748 * Need to take care of w_botline separately!
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
754 |VALID_CHEIGHT|VALID_TOPLINE);
755}
756
757 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
761 |VALID_CHEIGHT|VALID_TOPLINE);
762}
763
Dominique Pellee764d1b2023-03-12 21:20:59 +0000764#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100766 * Display of line has changed for "buf", invalidate cursor position and
767 * w_botline.
768 */
769 void
770changed_line_display_buf(buf_T *buf)
771{
772 win_T *wp;
773
774 FOR_ALL_WINDOWS(wp)
775 if (wp->w_buffer == buf)
776 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
777 |VALID_CROW|VALID_CHEIGHT
778 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
779}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000780#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100781
782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Make sure the value of curwin->w_botline is valid.
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100788 validate_botline_win(curwin);
789}
790
791/*
792 * Make sure the value of wp->w_botline is valid.
793 */
794 void
795validate_botline_win(win_T *wp)
796{
797 if (!(wp->w_valid & VALID_BOTLINE))
798 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Mark curwin->w_botline as invalid (because of some change in the buffer).
803 */
804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100805invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
808}
809
810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100817approximate_botline_win(
818 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 wp->w_valid &= ~VALID_BOTLINE;
821}
822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823/*
824 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
825 */
826 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100827cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
829 check_cursor_moved(curwin);
830 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
831 (VALID_WROW|VALID_WCOL));
832}
833
834/*
835 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
836 * w_topline must be valid, you may need to call update_topline() first!
837 */
838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100839validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100841 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 check_cursor_moved(curwin);
843 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
844 curs_columns(TRUE);
845}
846
847#if defined(FEAT_GUI) || defined(PROTO)
848/*
849 * validate w_cline_row.
850 */
851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 /*
855 * First make sure that w_topline is valid (after moving the cursor).
856 */
857 update_topline();
858 check_cursor_moved(curwin);
859 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100860 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862#endif
863
864/*
865 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200866 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 */
868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100869curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 linenr_T lnum;
872 int i;
873 int all_invalid;
874 int valid;
875#ifdef FEAT_FOLDING
876 long fold_count;
877#endif
878
Bram Moolenaar85a20022019-12-21 18:25:54 +0100879 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 all_invalid = (!redrawing()
881 || wp->w_lines_valid == 0
882 || wp->w_lines[0].wl_lnum > wp->w_topline);
883 i = 0;
884 wp->w_cline_row = 0;
885 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
886 {
887 valid = FALSE;
888 if (!all_invalid && i < wp->w_lines_valid)
889 {
890 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (wp->w_lines[i].wl_lnum == lnum)
893 {
894#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 // Check for newly inserted lines below this row, in which
896 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (!wp->w_buffer->b_mod_set
898 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
899 || wp->w_buffer->b_mod_top
900 > wp->w_lines[i].wl_lastlnum + 1)
901#endif
902 valid = TRUE;
903 }
904 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100908 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100910 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100912 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914#ifdef FEAT_FOLDING
915 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (lnum > wp->w_cursor.lnum)
918 break;
919#else
920 ++lnum;
921#endif
922 wp->w_cline_row += wp->w_lines[i].wl_size;
923 }
924 else
925 {
926#ifdef FEAT_FOLDING
927 fold_count = foldedCount(wp, lnum, NULL);
928 if (fold_count)
929 {
930 lnum += fold_count;
931 if (lnum > wp->w_cursor.lnum)
932 break;
933 ++wp->w_cline_row;
934 }
935 else
936#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100937 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200938 wp->w_cline_row += plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100939 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
942 }
943
944 check_cursor_moved(wp);
945 if (!(wp->w_valid & VALID_CHEIGHT))
946 {
947 if (all_invalid
948 || i == wp->w_lines_valid
949 || (i < wp->w_lines_valid
950 && (!wp->w_lines[i].wl_valid
951 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
952 {
953#ifdef FEAT_DIFF
954 if (wp->w_cursor.lnum == wp->w_topline)
955 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
956 TRUE) + wp->w_topfill;
957 else
958#endif
959 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
960#ifdef FEAT_FOLDING
961 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
962 NULL, NULL, TRUE, NULL);
963#endif
964 }
965 else if (i > wp->w_lines_valid)
966 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 wp->w_cline_height = 0;
969#ifdef FEAT_FOLDING
970 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
971 NULL, NULL, TRUE, NULL);
972#endif
973 }
974 else
975 {
976 wp->w_cline_height = wp->w_lines[i].wl_size;
977#ifdef FEAT_FOLDING
978 wp->w_cline_folded = wp->w_lines[i].wl_folded;
979#endif
980 }
981 }
982
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100983 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986
987/*
988 * Validate curwin->w_virtcol only.
989 */
990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100991validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 validate_virtcol_win(curwin);
994}
995
996/*
997 * Validate wp->w_virtcol only.
998 */
999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001003
1004 if (wp->w_valid & VALID_VIRTCOL)
1005 return;
1006
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001007#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001008 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001009#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001010 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001011#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001012 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001013#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001014 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015}
1016
1017/*
1018 * Validate curwin->w_cline_height only.
1019 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001024
1025 if (curwin->w_valid & VALID_CHEIGHT)
1026 return;
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001029 if (curwin->w_cursor.lnum == curwin->w_topline)
1030 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1031 + curwin->w_topfill;
1032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001034 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001036 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001038 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001042 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 */
1044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001045validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
1047 colnr_T off;
1048 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001049 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001053 if (curwin->w_valid & VALID_WCOL)
1054 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001055
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001056 col = curwin->w_virtcol;
1057 off = curwin_col_off();
1058 col += off;
1059 width = curwin->w_width - off + curwin_col_off2();
1060
1061 // long line wrapping, adjust curwin->w_wrow
1062 if (curwin->w_p_wrap
1063 && col >= (colnr_T)curwin->w_width
1064 && width > 0)
1065 // use same formula as what is used in curs_columns()
1066 col -= ((col - curwin->w_width) / width + 1) * width;
1067 if (col > (int)curwin->w_leftcol)
1068 col -= curwin->w_leftcol;
1069 else
1070 col = 0;
1071 curwin->w_wcol = col;
1072
1073 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001074#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001075 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001080 * Compute offset of a window, occupied by absolute or relative line number,
1081 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
1083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001084win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
Bram Moolenaar64486672010-05-16 15:46:46 +02001086 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001087 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#ifdef FEAT_FOLDING
1089 + wp->w_p_fdc
1090#endif
1091#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001092 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#endif
1094 );
1095}
1096
1097 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001098curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 return win_col_off(curwin);
1101}
1102
1103/*
1104 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001105 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1106 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
1108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001109win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
Bram Moolenaar64486672010-05-16 15:46:46 +02001111 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001112 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 return 0;
1114}
1115
1116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 return win_col_off2(curwin);
1120}
1121
1122/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001123 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 * Also updates curwin->w_wrow and curwin->w_cline_row.
1125 * Also updates curwin->w_leftcol.
1126 */
1127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001128curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int off_left, off_right;
1134 int n;
1135 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001136 int width1; // text width for first screen line
1137 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 int new_leftcol;
1139 colnr_T startcol;
1140 colnr_T endcol;
1141 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001142 long so = get_scrolloff_value();
1143 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001144 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 /*
1147 * First make sure that w_topline is valid (after moving the cursor).
1148 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001149 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 /*
1152 * Next make sure that w_cline_row is valid.
1153 */
1154 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001155 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001157#ifdef FEAT_PROP_POPUP
1158 // will be set by getvvcol() but not reset
1159 curwin->w_virtcol_first_char = 0;
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /*
1163 * Compute the number of virtual columns.
1164 */
1165#ifdef FEAT_FOLDING
1166 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001167 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1169 else
1170#endif
1171 getvvcol(curwin, &curwin->w_cursor,
1172 &startcol, &(curwin->w_virtcol), &endcol);
1173
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001176 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 extra = curwin_col_off();
1179 curwin->w_wcol = curwin->w_virtcol + extra;
1180 endcol += extra;
1181
1182 /*
1183 * Now compute w_wrow, counting screen lines from w_cline_row.
1184 */
1185 curwin->w_wrow = curwin->w_cline_row;
1186
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001187 width1 = curwin->w_width - extra;
1188 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001190 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001191 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001192 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001193 if (curwin->w_p_wrap)
1194 curwin->w_wrow = curwin->w_height - 1;
1195 else
1196 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001198 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001202 // skip columns that are not visible
1203 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001204 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001205 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001206 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001207 // Deduct by multiples of width2. This allows the long line
1208 // wrapping formula below to correctly calculate the w_wcol value
1209 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001210 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001211 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001212 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001213 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001214 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001215
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001216 did_sub_skipcol = TRUE;
1217 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001218
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001220 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001223 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1224 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 }
1228
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1230 // is not folded.
1231 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001232 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_FOLDING
1234 && !curwin->w_cline_folded
1235#endif
1236 )
1237 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001238#ifdef FEAT_PROP_POPUP
1239 if (curwin->w_virtcol_first_char > 0)
1240 {
1241 int cols = (curwin->w_width - extra);
1242 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1243
1244 // each "above" text prop shifts the text one row down
1245 curwin->w_wrow += rows;
1246 curwin->w_wcol -= rows * cols;
1247 endcol -= rows * cols;
1248 curwin->w_cline_height = rows + 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 /*
1252 * If Cursor is left of the screen, scroll rightwards.
1253 * If Cursor is right of the screen, scroll leftwards
1254 * If we get closer to the edge than 'sidescrolloff', scroll a little
1255 * extra
1256 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001257 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001258 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001259 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (off_left < 0 || off_right > 0)
1261 {
1262 if (off_left < 0)
1263 diff = -off_left;
1264 else
1265 diff = off_right;
1266
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 // When far off or not enough room on either side, put cursor in
1268 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001269 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1270 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 if (diff < p_ss)
1274 diff = p_ss;
1275 if (off_left < 0)
1276 new_leftcol = curwin->w_leftcol - diff;
1277 else
1278 new_leftcol = curwin->w_leftcol + diff;
1279 }
1280 if (new_leftcol < 0)
1281 new_leftcol = 0;
1282 if (new_leftcol != (int)curwin->w_leftcol)
1283 {
1284 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001285 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001286 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 }
1289 curwin->w_wcol -= curwin->w_leftcol;
1290 }
1291 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1292 curwin->w_wcol -= curwin->w_leftcol;
1293 else
1294 curwin->w_wcol = 0;
1295
1296#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001297 // Skip over filler lines. At the top use w_topfill, there
1298 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (curwin->w_cursor.lnum == curwin->w_topline)
1300 curwin->w_wrow += curwin->w_topfill;
1301 else
1302 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1303#endif
1304
1305 prev_skipcol = curwin->w_skipcol;
1306
1307 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if ((curwin->w_wrow >= curwin->w_height
1310 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001311 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && (p_lines =
1313#ifdef FEAT_DIFF
1314 plines_win_nofill
1315#else
1316 plines_win
1317#endif
1318 (curwin, curwin->w_cursor.lnum, FALSE))
1319 - 1 >= curwin->w_height))
1320 && curwin->w_height != 0
1321 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001323 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 // Cursor past end of screen. Happens with a single line that does
1326 // not fit on screen. Find a skipcol to show the text around the
1327 // cursor. Avoid scrolling all the time. compute value of "extra":
1328 // 1: Less than 'scrolloff' lines above
1329 // 2: Less than 'scrolloff' lines below
1330 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001332 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 // Compute last display line of the buffer line that we want at the
1335 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (p_lines == 0)
1337 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1338 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001339 if (p_lines > curwin->w_wrow + so)
1340 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 else
1342 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001343 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 extra += 2;
1345
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001346 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001349 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (n > curwin->w_height / 2)
1351 n -= curwin->w_height / 2;
1352 else
1353 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (n > p_lines - curwin->w_height + 1)
1356 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001357 if (n > 0)
1358 curwin->w_skipcol = width1 + (n - 1) * width2;
1359 else
1360 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362 else if (extra == 1)
1363 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001364 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001365 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1366 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (extra > 0)
1368 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001369 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1370 extra = curwin->w_skipcol / width2;
1371 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 }
1374 else if (extra == 2)
1375 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001376 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (endcol > curwin->w_skipcol)
1381 curwin->w_skipcol = endcol;
1382 }
1383
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001384 // adjust w_wrow for the changed w_skipcol
1385 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001386 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001387 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001388 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (curwin->w_wrow >= curwin->w_height)
1391 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001394 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 curwin->w_wrow -= extra;
1396 }
1397
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001398 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (extra > 0)
1400 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1401 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001402 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001404 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 curwin->w_skipcol = 0;
1406 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001407 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001409#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001410 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001411#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001412#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1413 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1414 {
1415 curwin->w_wrow += popup_top_extra(curwin);
1416 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001417 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001418 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001419 else
1420 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001421#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001422
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001423 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1424 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001425 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001426 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1429}
1430
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001431#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432/*
1433 * Compute the screen position of text character at "pos" in window "wp"
1434 * The resulting values are one-based, zero when character is not visible.
1435 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001436 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001437textpos2screenpos(
1438 win_T *wp,
1439 pos_T *pos,
1440 int *rowp, // screen row
1441 int *scolp, // start screen column
1442 int *ccolp, // cursor screen column
1443 int *ecolp) // end screen column
1444{
1445 colnr_T scol = 0, ccol = 0, ecol = 0;
1446 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001447 colnr_T coloff = 0;
1448
Bram Moolenaar189663b2021-07-21 18:04:56 +02001449 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 colnr_T col;
1452 int width;
1453 linenr_T lnum = pos->lnum;
1454#ifdef FEAT_FOLDING
1455 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1458#endif
Luuk van Baal32d701f2024-04-28 16:24:02 +02001459 row = plines_m_win(wp, wp->w_topline, lnum - 1, INT_MAX);
zeertzjqbfe377b2023-08-17 22:58:53 +02001460 // "row" should be the screen line where line "lnum" begins, which can
1461 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001462 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001463
1464#ifdef FEAT_DIFF
1465 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001466 row += lnum == wp->w_topline ? wp->w_topfill
1467 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001468#endif
1469
zeertzjqba2d1912022-12-18 12:28:59 +00001470 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001471#ifdef FEAT_FOLDING
1472 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001473 {
zeertzjq6235a102023-08-19 14:12:42 +02001474 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001475 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001476 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001477 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001478#endif
1479 {
1480 getvcol(wp, pos, &scol, &ccol, &ecol);
1481
1482 // similar to what is done in validate_cursor_col()
1483 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001484 col += off;
1485 width = wp->w_width - off + win_col_off2(wp);
1486
1487 // long line wrapping, adjust row
1488 if (wp->w_p_wrap
1489 && col >= (colnr_T)wp->w_width
1490 && width > 0)
1491 {
1492 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001493 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001494 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001495 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001496 }
1497 col -= wp->w_leftcol;
1498 if (col >= wp->w_width)
1499 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001500 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001501 {
1502 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001503 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001504 }
1505 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001506 // character is out of the window
1507 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001508 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001509 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001510 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001511 *scolp = scol + coloff;
1512 *ccolp = ccol + coloff;
1513 *ecolp = ecol + coloff;
1514}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001515#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001516
Bram Moolenaar12034e22019-08-25 22:25:02 +02001517#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518/*
1519 * "screenpos({winid}, {lnum}, {col})" function
1520 */
1521 void
1522f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1523{
1524 dict_T *dict;
1525 win_T *wp;
1526 pos_T pos;
1527 int row = 0;
1528 int scol = 0, ccol = 0, ecol = 0;
1529
Bram Moolenaar93a10962022-06-16 11:42:09 +01001530 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001531 return;
1532 dict = rettv->vval.v_dict;
1533
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001534 if (in_vim9script()
1535 && (check_for_number_arg(argvars, 0) == FAIL
1536 || check_for_number_arg(argvars, 1) == FAIL
1537 || check_for_number_arg(argvars, 2) == FAIL))
1538 return;
1539
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001540 wp = find_win_by_nr_or_id(&argvars[0]);
1541 if (wp == NULL)
1542 return;
1543
1544 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001545 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1546 {
1547 semsg(_(e_invalid_line_number_nr), pos.lnum);
1548 return;
1549 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001550 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001551 if (pos.col < 0)
1552 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001553 pos.coladd = 0;
1554 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1555
1556 dict_add_number(dict, "row", row);
1557 dict_add_number(dict, "col", scol);
1558 dict_add_number(dict, "curscol", ccol);
1559 dict_add_number(dict, "endcol", ecol);
1560}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001561
1562/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001563 * Convert a virtual (screen) column to a character column. The first column
1564 * is one. For a multibyte character, the column number of the first byte is
1565 * returned.
1566 */
1567 static int
1568virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1569{
zeertzjqf5a94d52023-10-15 10:03:30 +02001570 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001571 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1572 char_u *p = line + offset;
1573
zeertzjqb583eda2023-10-14 11:32:28 +02001574 if (*p == NUL)
1575 {
1576 if (p == line) // empty line
1577 return 0;
1578 // Move to the first byte of the last char.
1579 MB_PTR_BACK(line, p);
1580 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001581 return (int)(p - line + 1);
1582}
1583
1584/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001585 * "virtcol2col({winid}, {lnum}, {col})" function
1586 */
1587 void
1588f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1589{
1590 win_T *wp;
1591 linenr_T lnum;
1592 int screencol;
1593 int error = FALSE;
1594
1595 rettv->vval.v_number = -1;
1596
1597 if (check_for_number_arg(argvars, 0) == FAIL
1598 || check_for_number_arg(argvars, 1) == FAIL
1599 || check_for_number_arg(argvars, 2) == FAIL)
1600 return;
1601
1602 wp = find_win_by_nr_or_id(&argvars[0]);
1603 if (wp == NULL)
1604 return;
1605
1606 lnum = tv_get_number_chk(&argvars[1], &error);
1607 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1608 return;
1609
1610 screencol = tv_get_number_chk(&argvars[2], &error);
1611 if (error || screencol < 0)
1612 return;
1613
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001614 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001615}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001616#endif
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02001619 * Make sure the cursor is in the visible part of the topline after scrolling
1620 * the screen with 'smoothscroll'.
1621 */
1622static void cursor_correct_sms(void)
1623{
1624 if (!curwin->w_p_sms ||!curwin->w_p_wrap
1625 || curwin->w_cursor.lnum != curwin->w_topline)
1626 return;
1627
1628 long so = get_scrolloff_value();
1629 int width1 = curwin->w_width - curwin_col_off();
1630 int width2 = width1 + curwin_col_off2();
1631 int so_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1632 int space_cols = (curwin->w_height - 1) * width2;
Christian Brabandteff20eb2024-05-15 21:35:36 +02001633 int overlap, top, bot;
Luuk van Baal9148ba82024-04-08 22:27:41 +02001634 int size = so == 0 ? 0 : win_linetabsize(curwin, curwin->w_topline,
1635 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1636
1637 if (curwin->w_topline == 1 && curwin->w_skipcol == 0)
1638 so_cols = 0; // Ignore 'scrolloff' at top of buffer.
1639 else if (so_cols > space_cols / 2)
1640 so_cols = space_cols / 2; // Not enough room: put cursor in the middle.
1641
1642 // Not enough screen lines in topline: ignore 'scrolloff'.
Christian Brabandteff20eb2024-05-15 21:35:36 +02001643 while (so_cols > size && so_cols - width2 >= width1 && width1 > 0)
Luuk van Baal9148ba82024-04-08 22:27:41 +02001644 so_cols -= width2;
1645 if (so_cols >= width1 && so_cols > size)
1646 so_cols -= width1;
1647
1648 // If there is no marker or we have non-zero scrolloff, just ignore it.
Christian Brabandteff20eb2024-05-15 21:35:36 +02001649 overlap = (curwin->w_skipcol == 0 || so_cols != 0) ? 0
Luuk van Baal9148ba82024-04-08 22:27:41 +02001650 : sms_marker_overlap(curwin, -1);
Christian Brabandteff20eb2024-05-15 21:35:36 +02001651 top = curwin->w_skipcol + overlap + so_cols;
1652 bot = curwin->w_skipcol + width1 + (curwin->w_height - 1) * width2
Luuk van Baal9148ba82024-04-08 22:27:41 +02001653 - so_cols;
1654 validate_virtcol();
1655 colnr_T col = curwin->w_virtcol;
1656
1657 if (col < top)
1658 {
1659 if (col < width1)
1660 col += width1;
1661 while (width2 > 0 && col < top)
1662 col += width2;
1663 }
1664 else
1665 while (width2 > 0 && col >= bot)
1666 col -= width2;
1667
1668 if (col != curwin->w_virtcol)
1669 {
1670 curwin->w_curswant = col;
1671 coladvance(curwin->w_curswant);
1672 // validate_virtcol() marked various things as valid, but after
1673 // moving the cursor they need to be recomputed
1674 curwin->w_valid &=
1675 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1676 }
1677}
1678
1679/*
1680 * Scroll "count" lines up or down, and redraw.
1681 */
1682 void
1683scroll_redraw(int up, long count)
1684{
1685 linenr_T prev_topline = curwin->w_topline;
1686 int prev_skipcol = curwin->w_skipcol;
1687#ifdef FEAT_DIFF
1688 int prev_topfill = curwin->w_topfill;
1689#endif
1690 linenr_T prev_lnum = curwin->w_cursor.lnum;
1691
1692 if (up)
1693 scrollup(count, TRUE);
1694 else
1695 scrolldown(count, TRUE);
1696 if (get_scrolloff_value() > 0)
1697 {
1698 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
1699 // valid, otherwise the screen jumps back at the end of the file.
1700 cursor_correct();
1701 check_cursor_moved(curwin);
1702 curwin->w_valid |= VALID_TOPLINE;
1703
1704 // If moved back to where we were, at least move the cursor, otherwise
1705 // we get stuck at one position. Don't move the cursor up if the
1706 // first line of the buffer is already on the screen
1707 while (curwin->w_topline == prev_topline
1708 && curwin->w_skipcol == prev_skipcol
1709#ifdef FEAT_DIFF
1710 && curwin->w_topfill == prev_topfill
1711#endif
1712 )
1713 {
1714 if (up)
1715 {
1716 if (curwin->w_cursor.lnum > prev_lnum
1717 || cursor_down(1L, FALSE) == FAIL)
1718 break;
1719 }
1720 else
1721 {
1722 if (curwin->w_cursor.lnum < prev_lnum
1723 || prev_topline == 1L
1724 || cursor_up(1L, FALSE) == FAIL)
1725 break;
1726 }
1727 // Mark w_topline as valid, otherwise the screen jumps back at the
1728 // end of the file.
1729 check_cursor_moved(curwin);
1730 curwin->w_valid |= VALID_TOPLINE;
1731 }
1732 }
1733
1734 cursor_correct_sms();
1735 if (curwin->w_cursor.lnum != prev_lnum)
1736 coladvance(curwin->w_curswant);
1737 redraw_later(UPD_VALID);
1738}
1739
1740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001744scrolldown(
1745 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001748 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 int wrow;
1750 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001751 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001752 int width1 = 0;
1753 int width2 = 0;
1754
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001755 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001756 {
1757 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001758 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761#ifdef FEAT_FOLDING
1762 linenr_T first;
1763
Bram Moolenaar85a20022019-12-21 18:25:54 +01001764 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1766#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001767 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001768 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001771 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1772 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 ++curwin->w_topfill;
1775 ++done;
1776 }
1777 else
1778#endif
1779 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001780 // break when at the very top
1781 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001782 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001784 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001786 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001787 if (curwin->w_skipcol >= width1 + width2)
1788 curwin->w_skipcol -= width2;
1789 else
1790 curwin->w_skipcol -= width1;
1791 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001795 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001796 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001797 --curwin->w_topline;
1798 curwin->w_skipcol = 0;
1799#ifdef FEAT_DIFF
1800 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001802#ifdef FEAT_FOLDING
1803 // A sequence of folded lines only counts for one logical line
1804 if (hasFolding(curwin->w_topline, &first, NULL))
1805 {
1806 ++done;
1807 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001808 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001809 curwin->w_botline -= curwin->w_topline - first;
1810 curwin->w_topline = first;
1811 }
1812 else
1813#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001814 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001815 {
1816 int size = win_linetabsize(curwin, curwin->w_topline,
1817 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1818 if (size > width1)
1819 {
1820 curwin->w_skipcol = width1;
1821 size -= width1;
1822 redraw_later(UPD_NOT_VALID);
1823 }
1824 while (size > width2)
1825 {
1826 curwin->w_skipcol += width2;
1827 size -= width2;
1828 }
1829 ++done;
1830 }
1831 else
1832 done += PLINES_NOFILL(curwin->w_topline);
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001835 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 invalidate_botline();
1837 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001838 curwin->w_wrow += done; // keep w_wrow updated
1839 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841#ifdef FEAT_DIFF
1842 if (curwin->w_cursor.lnum == curwin->w_topline)
1843 curwin->w_cline_row = 0;
1844 check_topfill(curwin, TRUE);
1845#endif
1846
1847 /*
1848 * Compute the row number of the last row of the cursor line
1849 * and move the cursor onto the displayed part of the window.
1850 */
1851 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001852 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 validate_virtcol();
1855 validate_cheight();
1856 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001857 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1860 {
1861#ifdef FEAT_FOLDING
1862 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1863 {
1864 --wrow;
1865 if (first == 1)
1866 curwin->w_cursor.lnum = 1;
1867 else
1868 curwin->w_cursor.lnum = first - 1;
1869 }
1870 else
1871#endif
1872 wrow -= plines(curwin->w_cursor.lnum--);
1873 curwin->w_valid &=
1874 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1875 moved = TRUE;
1876 }
1877 if (moved)
1878 {
1879#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001880 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 foldAdjustCursor();
1882#endif
1883 coladvance(curwin->w_curswant);
1884 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001885 if (curwin->w_cursor.lnum < curwin->w_topline)
1886 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887}
1888
1889/*
1890 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893scrollup(
1894 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001895 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001897 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001899 if (do_sms
1900# ifdef FEAT_FOLDING
1901 || (byfold && hasAnyFolding(curwin))
1902# endif
1903# ifdef FEAT_DIFF
1904 || (curwin->w_p_diff && !curwin->w_p_wrap)
1905# endif
1906 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001908 int width1 = curwin->w_width - curwin_col_off();
1909 int width2 = width1 + curwin_col_off2();
1910 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001911 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001912
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001913 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001914 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001915
1916 // diff mode: first consume "topfill"
1917 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1918 // the line, then advance to the next line.
1919 // folding: count each sequence of folded lines as one logical line.
1920 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
1922# ifdef FEAT_DIFF
1923 if (curwin->w_topfill > 0)
1924 --curwin->w_topfill;
1925 else
1926# endif
1927 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001928 linenr_T lnum = curwin->w_topline;
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930# ifdef FEAT_FOLDING
1931 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001932 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 (void)hasFolding(lnum, NULL, &lnum);
1934# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001935 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001936 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001937 // 'smoothscroll': increase "w_skipcol" until it goes over
1938 // the end of the line, then advance to the next line.
1939 int add = curwin->w_skipcol > 0 ? width2 : width1;
1940 curwin->w_skipcol += add;
1941 if (curwin->w_skipcol >= size)
1942 {
1943 if (lnum == curbuf->b_ml.ml_line_count)
1944 {
1945 // at the last screen line, can't scroll further
1946 curwin->w_skipcol -= add;
1947 break;
1948 }
1949 ++lnum;
1950 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001951 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001952 else
1953 {
1954 if (lnum >= curbuf->b_ml.ml_line_count)
1955 break;
1956 ++lnum;
1957 }
1958
1959 if (lnum > curwin->w_topline)
1960 {
1961 // approximate w_botline
1962 curwin->w_botline += lnum - curwin->w_topline;
1963 curwin->w_topline = lnum;
1964# ifdef FEAT_DIFF
1965 curwin->w_topfill = diff_check_fill(curwin, lnum);
1966# endif
1967 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001968 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001969 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001970 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001971 }
1972 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001973
zeertzjqd9a92dc2023-06-05 18:41:35 +01001974 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1975 // need to redraw more, because wl_size of the (new) topline may
1976 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001977 redraw_later(UPD_NOT_VALID);
1978 }
1979 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001982 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
1984
1985 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1986 curwin->w_topline = curbuf->b_ml.ml_line_count;
1987 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1988 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1989
1990#ifdef FEAT_DIFF
1991 check_topfill(curwin, FALSE);
1992#endif
1993
1994#ifdef FEAT_FOLDING
1995 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001996 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1998#endif
1999
2000 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2001 if (curwin->w_cursor.lnum < curwin->w_topline)
2002 {
2003 curwin->w_cursor.lnum = curwin->w_topline;
2004 curwin->w_valid &=
2005 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
2006 coladvance(curwin->w_curswant);
2007 }
2008}
2009
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002010/*
2011 * Called after changing the cursor column: make sure that curwin->w_skipcol is
2012 * valid for 'smoothscroll'.
2013 */
2014 void
2015adjust_skipcol(void)
2016{
2017 if (!curwin->w_p_wrap
2018 || !curwin->w_p_sms
2019 || curwin->w_cursor.lnum != curwin->w_topline)
2020 return;
2021
2022 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00002023 if (width1 <= 0)
2024 return; // no text will be displayed
2025
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002026 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002027 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002028 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
2029 int scrolled = FALSE;
2030
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002031 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00002032 if (curwin->w_cline_height == curwin->w_height
2033 // w_cline_height may be capped at w_height, check there aren't
2034 // actually more lines.
2035 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
2036 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002037 {
2038 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002039 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002040 return;
2041 }
2042
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002043 validate_virtcol();
Luuk van Baalcb204e62024-04-02 20:49:45 +02002044 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002045 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01002046 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002047 {
2048 // scroll a screen line down
2049 if (curwin->w_skipcol >= width1 + width2)
2050 curwin->w_skipcol -= width2;
2051 else
2052 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002053 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002054 }
2055 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01002056 {
2057 validate_virtcol();
2058 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002059 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002060 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002061
2062 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2063 int row = 0;
2064 if (col >= width1)
2065 {
2066 col -= width1;
2067 ++row;
2068 }
2069 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002070 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002071 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002072 // col may no longer be used, but make
2073 // sure it is correct anyhow, just in case
2074 col = col % width2;
2075 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002076 if (row >= curwin->w_height)
2077 {
2078 if (curwin->w_skipcol == 0)
2079 {
2080 curwin->w_skipcol += width1;
2081 --row;
2082 }
2083 if (row >= curwin->w_height)
2084 curwin->w_skipcol += (row - curwin->w_height) * width2;
2085 redraw_later(UPD_NOT_VALID);
2086 }
2087}
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#ifdef FEAT_DIFF
2090/*
2091 * Don't end up with too many filler lines in the window.
2092 */
2093 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002094check_topfill(
2095 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002096 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int n;
2099
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002100 if (wp->w_topfill <= 0)
2101 return;
2102
2103 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2104 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002106 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002108 --wp->w_topline;
2109 wp->w_topfill = 0;
2110 }
2111 else
2112 {
2113 wp->w_topfill = wp->w_height - n;
2114 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117 }
2118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#endif
2120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121/*
2122 * Scroll the screen one line down, but don't do it if it would move the
2123 * cursor off the screen.
2124 */
2125 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002126scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 int end_row;
2129#ifdef FEAT_DIFF
2130 int can_fill = (curwin->w_topfill
2131 < diff_check_fill(curwin, curwin->w_topline));
2132#endif
2133
2134 if (curwin->w_topline <= 1
2135#ifdef FEAT_DIFF
2136 && !can_fill
2137#endif
2138 )
2139 return;
2140
Bram Moolenaar85a20022019-12-21 18:25:54 +01002141 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /*
2144 * Compute the row number of the last row of the cursor line
2145 * and make sure it doesn't go off the screen. Make sure the cursor
2146 * doesn't go past 'scrolloff' lines from the screen end.
2147 */
2148 end_row = curwin->w_wrow;
2149#ifdef FEAT_DIFF
2150 if (can_fill)
2151 ++end_row;
2152 else
2153 end_row += plines_nofill(curwin->w_topline - 1);
2154#else
2155 end_row += plines(curwin->w_topline - 1);
2156#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002157 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 validate_cheight();
2160 validate_virtcol();
2161 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002162 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002164 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166#ifdef FEAT_DIFF
2167 if (can_fill)
2168 {
2169 ++curwin->w_topfill;
2170 check_topfill(curwin, TRUE);
2171 }
2172 else
2173 {
2174 --curwin->w_topline;
2175 curwin->w_topfill = 0;
2176 }
2177#else
2178 --curwin->w_topline;
2179#endif
2180#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002181 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002183 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2185 }
2186}
2187
2188/*
2189 * Scroll the screen one line up, but don't do it if it would move the cursor
2190 * off the screen.
2191 */
2192 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002193scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 int start_row;
2196
2197 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2198#ifdef FEAT_DIFF
2199 && curwin->w_topfill == 0
2200#endif
2201 )
2202 return;
2203
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
2206 /*
2207 * Compute the row number of the first row of the cursor line
2208 * and make sure it doesn't go off the screen. Make sure the cursor
2209 * doesn't go before 'scrolloff' lines from the screen start.
2210 */
2211#ifdef FEAT_DIFF
2212 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2213 - curwin->w_topfill;
2214#else
2215 start_row = curwin->w_wrow - plines(curwin->w_topline);
2216#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002217 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002220 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002222 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224#ifdef FEAT_DIFF
2225 if (curwin->w_topfill > 0)
2226 --curwin->w_topfill;
2227 else
2228#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002229 {
2230#ifdef FEAT_FOLDING
2231 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002234 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2237 }
2238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240/*
2241 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2242 * a (wrapped) text line. Uses and sets "lp->fill".
2243 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002244 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
2246 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002247topline_back_winheight(
2248 lineoff_T *lp,
2249 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251#ifdef FEAT_DIFF
2252 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 ++lp->fill;
2256 lp->height = 1;
2257 }
2258 else
2259#endif
2260 {
2261 --lp->lnum;
2262#ifdef FEAT_DIFF
2263 lp->fill = 0;
2264#endif
2265 if (lp->lnum < 1)
2266 lp->height = MAXCOL;
2267 else
2268#ifdef FEAT_FOLDING
2269 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 lp->height = 1;
2272 else
2273#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002274 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276}
2277
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002278 static void
2279topline_back(lineoff_T *lp)
2280{
2281 topline_back_winheight(lp, TRUE);
2282}
2283
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285/*
2286 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2287 * a (wrapped) text line. Uses and sets "lp->fill".
2288 * Returns the height of the added line in "lp->height".
2289 * Lines below the last one are incredibly high.
2290 */
2291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002292botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294#ifdef FEAT_DIFF
2295 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 ++lp->fill;
2299 lp->height = 1;
2300 }
2301 else
2302#endif
2303 {
2304 ++lp->lnum;
2305#ifdef FEAT_DIFF
2306 lp->fill = 0;
2307#endif
2308 if (lp->lnum > curbuf->b_ml.ml_line_count)
2309 lp->height = MAXCOL;
2310 else
2311#ifdef FEAT_FOLDING
2312 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002313 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 lp->height = 1;
2315 else
2316#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002317 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319}
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321/*
2322 * Recompute topline to put the cursor at the top of the window.
2323 * Scroll at least "min_scroll" lines.
2324 * If "always" is TRUE, always set topline (for "zt").
2325 */
2326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002327scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 int scrolled = 0;
2330 int extra = 0;
2331 int used;
2332 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 linenr_T top; // just above displayed lines
2334 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002336 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#ifdef FEAT_DIFF
2338 linenr_T old_topfill = curwin->w_topfill;
2339#endif
2340 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002341 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (mouse_dragging > 0)
2344 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 /*
2347 * Decrease topline until:
2348 * - it has become 1
2349 * - (part of) the cursor line is moved off the screen or
2350 * - moved at least 'scrolljump' lines and
2351 * - at least 'scrolloff' lines above and below the cursor
2352 */
2353 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002354 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (curwin->w_cursor.lnum < curwin->w_topline)
2356 scrolled = used;
2357
2358#ifdef FEAT_FOLDING
2359 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2360 {
2361 --top;
2362 ++bot;
2363 }
2364 else
2365#endif
2366 {
2367 top = curwin->w_cursor.lnum - 1;
2368 bot = curwin->w_cursor.lnum + 1;
2369 }
2370 new_topline = top + 1;
2371
2372#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // "used" already contains the number of filler lines above, don't add it
2374 // again.
2375 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002376 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#endif
2378
2379 /*
2380 * Check if the lines from "top" to "bot" fit in the window. If they do,
2381 * set new_topline and advance "top" and "bot" to include more lines.
2382 */
2383 while (top > 0)
2384 {
2385#ifdef FEAT_FOLDING
2386 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 i = 1;
2389 else
2390#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002391 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002392 if (top < curwin->w_topline)
2393 scrolled += i;
2394
2395 // If scrolling is needed, scroll at least 'sj' lines.
2396 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2397 && extra >= off)
2398 break;
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 used += i;
2401 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2402 {
2403#ifdef FEAT_FOLDING
2404 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002405 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 ++used;
2407 else
2408#endif
2409 used += plines(bot);
2410 }
2411 if (used > curwin->w_height)
2412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 extra += i;
2415 new_topline = top;
2416 --top;
2417 ++bot;
2418 }
2419
2420 /*
2421 * If we don't have enough space, put cursor in the middle.
2422 * This makes sure we get the same position when using "k" and "j"
2423 * in a small window.
2424 */
2425 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002426 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 else
2428 {
2429 /*
2430 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002431 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
2433 if (new_topline < curwin->w_topline || always)
2434 curwin->w_topline = new_topline;
2435 if (curwin->w_topline > curwin->w_cursor.lnum)
2436 curwin->w_topline = curwin->w_cursor.lnum;
2437#ifdef FEAT_DIFF
2438 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2439 if (curwin->w_topfill > 0 && extra > off)
2440 {
2441 curwin->w_topfill -= extra - off;
2442 if (curwin->w_topfill < 0)
2443 curwin->w_topfill = 0;
2444 }
2445 check_topfill(curwin, FALSE);
2446#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002447 if (curwin->w_topline != old_topline)
2448 reset_skipcol();
2449 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002450 {
2451 validate_virtcol();
2452 if (curwin->w_skipcol >= curwin->w_virtcol)
2453 // TODO: if the line doesn't fit may optimize w_skipcol instead
2454 // of making it zero
2455 reset_skipcol();
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002458 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#ifdef FEAT_DIFF
2460 || curwin->w_topfill != old_topfill
2461#endif
2462 )
2463 curwin->w_valid &=
2464 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2465 curwin->w_valid |= VALID_TOPLINE;
2466 }
2467}
2468
2469/*
2470 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2471 * screen lines for text lines.
2472 */
2473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002474set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476#ifdef FEAT_DIFF
2477 wp->w_filler_rows = 0;
2478#endif
2479 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002480 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 else
2482 {
2483 wp->w_empty_rows = wp->w_height - used;
2484#ifdef FEAT_DIFF
2485 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2486 {
2487 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2488 if (wp->w_empty_rows > wp->w_filler_rows)
2489 wp->w_empty_rows -= wp->w_filler_rows;
2490 else
2491 {
2492 wp->w_filler_rows = wp->w_empty_rows;
2493 wp->w_empty_rows = 0;
2494 }
2495 }
2496#endif
2497 }
2498}
2499
2500/*
2501 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002502 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2504 * This is messy stuff!!!
2505 */
2506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002507scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 int used;
2510 int scrolled = 0;
2511 int extra = 0;
2512 int i;
2513 linenr_T line_count;
2514 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002515 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 lineoff_T loff;
2517 lineoff_T boff;
2518#ifdef FEAT_DIFF
2519 int old_topfill = curwin->w_topfill;
2520 int fill_below_window;
2521#endif
2522 linenr_T old_botline = curwin->w_botline;
2523 linenr_T old_valid = curwin->w_valid;
2524 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002526 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002527 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 cln = curwin->w_cursor.lnum;
2530 if (set_topbot)
2531 {
2532 used = 0;
2533 curwin->w_botline = cln + 1;
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002534 loff.lnum = cln + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#ifdef FEAT_DIFF
2536 loff.fill = 0;
2537#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002538 while (TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002540 topline_back_winheight(&loff, FALSE);
2541 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002543 if (used + loff.height > curwin->w_height)
2544 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002545 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002546 {
2547 // 'smoothscroll' and 'wrap' are set. The above line is
2548 // too long to show in its entirety, so we show just a part
2549 // of it.
2550 if (used < curwin->w_height)
2551 {
2552 int plines_offset = used + loff.height
2553 - curwin->w_height;
2554 used = curwin->w_height;
2555#ifdef FEAT_DIFF
2556 curwin->w_topfill = loff.fill;
2557#endif
2558 curwin->w_topline = loff.lnum;
2559 curwin->w_skipcol = skipcol_from_plines(
2560 curwin, plines_offset);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002561 }
2562 }
2563 break;
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#ifdef FEAT_DIFF
2566 curwin->w_topfill = loff.fill;
2567#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002568 curwin->w_topline = loff.lnum;
2569 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 set_empty_rows(curwin, used);
2573 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2574 if (curwin->w_topline != old_topline
2575#ifdef FEAT_DIFF
2576 || curwin->w_topfill != old_topfill
2577#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002578 || curwin->w_skipcol != old_skipcol
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002579 || curwin->w_skipcol != 0)
2580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002582 if (curwin->w_skipcol != old_skipcol)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002583 redraw_later(UPD_NOT_VALID);
2584 else
2585 reset_skipcol();
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 else
2589 validate_botline();
2590
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#ifdef FEAT_DIFF
2593 used = plines_nofill(cln);
2594#else
2595 validate_cheight();
2596 used = curwin->w_cline_height;
2597#endif
2598
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002599 // If the cursor is on or below botline, we will at least scroll by the
2600 // height of the cursor line, which is "used". Correct for empty lines,
2601 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (cln >= curwin->w_botline)
2603 {
2604 scrolled = used;
2605 if (cln == curwin->w_botline)
2606 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002607 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002608 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002609 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002610 // Calculate how many screen lines the current top line of window
2611 // occupies. If it is occupying more than the entire window, we
2612 // need to scroll the additional clipped lines to scroll past the
2613 // top line before we can move on to the other lines.
2614 int top_plines =
2615#ifdef FEAT_DIFF
2616 plines_win_nofill
2617#else
2618 plines_win
2619#endif
2620 (curwin, curwin->w_topline, FALSE);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002621 int width1 = curwin->w_width - curwin_col_off();
zeertzjq031a7452024-05-11 11:23:37 +02002622
fullwaywang8154e642023-06-24 21:58:09 +01002623 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002624 {
fullwaywang8154e642023-06-24 21:58:09 +01002625 int width2 = width1 + curwin_col_off2();
zeertzjq031a7452024-05-11 11:23:37 +02002626 int skip_lines = 0;
2627
2628 // A similar formula is used in curs_columns().
fullwaywang8154e642023-06-24 21:58:09 +01002629 if (curwin->w_skipcol > width1)
2630 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2631 else if (curwin->w_skipcol > 0)
2632 skip_lines = 1;
2633
2634 top_plines -= skip_lines;
2635 if (top_plines > curwin->w_height)
2636 {
2637 scrolled += (top_plines - curwin->w_height);
2638 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002639 }
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642
2643 /*
2644 * Stop counting lines to scroll when
2645 * - hitting start of the file
2646 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002647 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * - lines between botline and cursor have been counted
2649 */
2650#ifdef FEAT_FOLDING
2651 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2652#endif
2653 {
2654 loff.lnum = cln;
2655 boff.lnum = cln;
2656 }
2657#ifdef FEAT_DIFF
2658 loff.fill = 0;
2659 boff.fill = 0;
2660 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2661 - curwin->w_filler_rows;
2662#endif
2663
2664 while (loff.lnum > 1)
2665 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002666 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2667 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002669 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2671 && loff.lnum <= curwin->w_botline
2672#ifdef FEAT_DIFF
2673 && (loff.lnum < curwin->w_botline
2674 || loff.fill >= fill_below_window)
2675#endif
2676 )
2677 break;
2678
Bram Moolenaar85a20022019-12-21 18:25:54 +01002679 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002681 if (loff.height == MAXCOL)
2682 used = MAXCOL;
2683 else
2684 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (used > curwin->w_height)
2686 break;
2687 if (loff.lnum >= curwin->w_botline
2688#ifdef FEAT_DIFF
2689 && (loff.lnum > curwin->w_botline
2690 || loff.fill <= fill_below_window)
2691#endif
2692 )
2693 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 scrolled += loff.height;
2696 if (loff.lnum == curwin->w_botline
2697#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002698 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#endif
2700 )
2701 scrolled -= curwin->w_empty_rows;
2702 }
2703
2704 if (boff.lnum < curbuf->b_ml.ml_line_count)
2705 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002706 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 botline_forw(&boff);
2708 used += boff.height;
2709 if (used > curwin->w_height)
2710 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002711 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2712 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 extra += boff.height;
2715 if (boff.lnum >= curwin->w_botline
2716#ifdef FEAT_DIFF
2717 || (boff.lnum + 1 == curwin->w_botline
2718 && boff.fill > curwin->w_filler_rows)
2719#endif
2720 )
2721 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002722 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 scrolled += boff.height;
2724 if (boff.lnum == curwin->w_botline
2725#ifdef FEAT_DIFF
2726 && boff.fill == 0
2727#endif
2728 )
2729 scrolled -= curwin->w_empty_rows;
2730 }
2731 }
2732 }
2733 }
2734
Bram Moolenaar85a20022019-12-21 18:25:54 +01002735 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (scrolled <= 0)
2737 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002738 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 else if (used > curwin->w_height)
2740 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002741 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
2743 {
2744 line_count = 0;
2745#ifdef FEAT_DIFF
2746 boff.fill = curwin->w_topfill;
2747#endif
2748 boff.lnum = curwin->w_topline - 1;
2749 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2750 {
2751 botline_forw(&boff);
2752 i += boff.height;
2753 ++line_count;
2754 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002755 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 line_count = 9999;
2757 }
2758
2759 /*
2760 * Scroll up if the cursor is off the bottom of the screen a bit.
2761 * Otherwise put it at 1/2 of the screen.
2762 */
2763 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002764 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002765 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002766 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002767 if (do_sms)
2768 scrollup(scrolled, TRUE); // TODO
2769 else
2770 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /*
2774 * If topline didn't change we need to restore w_botline and w_empty_rows
2775 * (we changed them).
2776 * If topline did change, update_screen() will set botline.
2777 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002778 if (curwin->w_topline == old_topline
2779 && curwin->w_skipcol == old_skipcol
2780 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
2782 curwin->w_botline = old_botline;
2783 curwin->w_empty_rows = old_empty_rows;
2784 curwin->w_valid = old_valid;
2785 }
2786 curwin->w_valid |= VALID_TOPLINE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02002787
2788 cursor_correct_sms();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791/*
2792 * Recompute topline to put the cursor halfway the window
2793 * If "atend" is TRUE, also put it halfway at the end of the file.
2794 */
2795 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002796scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 int above = 0;
2799 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002800 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#ifdef FEAT_DIFF
2802 int topfill = 0;
2803#endif
2804 int below = 0;
2805 int used;
2806 lineoff_T loff;
2807 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002808#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002809 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002812#ifdef FEAT_PROP_POPUP
2813 // if the width changed this needs to be updated first
2814 may_update_popup_position();
2815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2817#ifdef FEAT_FOLDING
2818 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2819#endif
2820#ifdef FEAT_DIFF
2821 used = plines_nofill(loff.lnum);
2822 loff.fill = 0;
2823 boff.fill = 0;
2824#else
2825 used = plines(loff.lnum);
2826#endif
2827 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002828
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002829 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002830 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2831 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002832 {
2833 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002834 if (atend)
2835 {
2836 want_height = (curwin->w_height - used) / 2;
2837 used = 0;
2838 }
2839 else
2840 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002841 }
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 while (topline > 1)
2844 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002845 // If using smoothscroll, we can precisely scroll to the
2846 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002847 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002848 {
2849 topline_back_winheight(&loff, FALSE);
2850 if (loff.height == MAXCOL)
2851 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002852 used += loff.height;
2853 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002854 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002855 botline_forw(&boff);
2856 used += boff.height;
2857 }
2858 if (used > want_height)
2859 {
2860 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002861 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002862 topline = loff.lnum;
2863#ifdef FEAT_DIFF
2864 topfill = loff.fill;
2865#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002866 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002867 }
2868 break;
2869 }
2870 topline = loff.lnum;
2871#ifdef FEAT_DIFF
2872 topfill = loff.fill;
2873#endif
2874 continue;
2875 }
2876
2877 // If not using smoothscroll, we have to iteratively find how many
2878 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002879 // This may not be right in the middle if the lines'
2880 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002881
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002882 // Depending on "prefer_above" we add a line above or below first.
2883 // Loop twice to avoid duplicating code.
2884 int done = FALSE;
2885 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002887 if (prefer_above ? (round == 2 && below < above)
2888 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002890 // add a line below the cursor
2891 if (boff.lnum < curbuf->b_ml.ml_line_count)
2892 {
2893 botline_forw(&boff);
2894 used += boff.height;
2895 if (used > curwin->w_height)
2896 {
2897 done = TRUE;
2898 break;
2899 }
2900 below += boff.height;
2901 }
2902 else
2903 {
2904 ++below; // count a "~" line
2905 if (atend)
2906 ++used;
2907 }
2908 }
2909
2910 if (prefer_above ? (round == 1 && below >= above)
2911 : (round == 1 && below > above))
2912 {
2913 // add a line above the cursor
2914 topline_back(&loff);
2915 if (loff.height == MAXCOL)
2916 used = MAXCOL;
2917 else
2918 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002920 {
2921 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002923 }
2924 above += loff.height;
2925 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002927 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002931 if (done)
2932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#ifdef FEAT_FOLDING
2936 if (!hasFolding(topline, &curwin->w_topline, NULL))
2937#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002938 {
2939 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002940 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002941 || curwin->w_skipcol != 0)
2942 {
2943 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002944 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002945 {
2946 curwin->w_skipcol = skipcol;
2947 redraw_later(UPD_NOT_VALID);
2948 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002949 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002950 reset_skipcol();
2951 }
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953#ifdef FEAT_DIFF
2954 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002955 if (old_topline > curwin->w_topline + curwin->w_height)
2956 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 check_topfill(curwin, FALSE);
2958#endif
2959 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2960 curwin->w_valid |= VALID_TOPLINE;
2961}
2962
2963/*
2964 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002965 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 * If not possible, put it at the same position as scroll_cursor_halfway().
2967 * When called topline must be valid!
2968 */
2969 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002970cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002972 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002974 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 linenr_T botline;
2976 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002977 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002979 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 /*
2982 * How many lines we would like to have above/below the cursor depends on
2983 * whether the first/last line of the file is on screen.
2984 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002985 above_wanted = so;
2986 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002987 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 above_wanted = mouse_dragging - 1;
2990 below_wanted = mouse_dragging - 1;
2991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (curwin->w_topline == 1)
2993 {
2994 above_wanted = 0;
2995 max_off = curwin->w_height / 2;
2996 if (below_wanted > max_off)
2997 below_wanted = max_off;
2998 }
2999 validate_botline();
3000 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003001 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 {
3003 below_wanted = 0;
3004 max_off = (curwin->w_height - 1) / 2;
3005 if (above_wanted > max_off)
3006 above_wanted = max_off;
3007 }
3008
3009 /*
3010 * If there are sufficient file-lines above and below the cursor, we can
3011 * return now.
3012 */
3013 cln = curwin->w_cursor.lnum;
3014 if (cln >= curwin->w_topline + above_wanted
3015 && cln < curwin->w_botline - below_wanted
3016#ifdef FEAT_FOLDING
3017 && !hasAnyFolding(curwin)
3018#endif
3019 )
3020 return;
3021
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003022 if (curwin->w_p_sms && !curwin->w_p_wrap)
3023 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003024 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003025 if (curwin->w_cline_height == curwin->w_height)
3026 {
3027 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003028 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003029 return;
3030 }
3031 // TODO: If the cursor line doesn't fit in the window then only adjust
3032 // w_skipcol.
3033 }
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 /*
3036 * Narrow down the area where the cursor can be put by taking lines from
3037 * the top and the bottom until:
3038 * - the desired context lines are found
3039 * - the lines from the top is past the lines from the bottom
3040 */
3041 topline = curwin->w_topline;
3042 botline = curwin->w_botline - 1;
3043#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003044 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 above = curwin->w_topfill;
3046 below = curwin->w_filler_rows;
3047#endif
3048 while ((above < above_wanted || below < below_wanted) && topline < botline)
3049 {
3050 if (below < below_wanted && (below <= above || above >= above_wanted))
3051 {
3052#ifdef FEAT_FOLDING
3053 if (hasFolding(botline, &botline, NULL))
3054 ++below;
3055 else
3056#endif
3057 below += plines(botline);
3058 --botline;
3059 }
3060 if (above < above_wanted && (above < below || below >= below_wanted))
3061 {
3062#ifdef FEAT_FOLDING
3063 if (hasFolding(topline, NULL, &topline))
3064 ++above;
3065 else
3066#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003067 above += PLINES_NOFILL(topline);
3068#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003069 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (topline < botline)
3071 above += diff_check_fill(curwin, topline + 1);
3072#endif
3073 ++topline;
3074 }
3075 }
3076 if (topline == botline || botline == 0)
3077 curwin->w_cursor.lnum = topline;
3078 else if (topline > botline)
3079 curwin->w_cursor.lnum = botline;
3080 else
3081 {
3082 if (cln < topline && curwin->w_topline > 1)
3083 {
3084 curwin->w_cursor.lnum = topline;
3085 curwin->w_valid &=
3086 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3087 }
3088 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3089 {
3090 curwin->w_cursor.lnum = botline;
3091 curwin->w_valid &=
3092 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3093 }
3094 }
3095 curwin->w_valid |= VALID_TOPLINE;
3096}
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003099 * Decide how much overlap to use for page-up or page-down scrolling.
3100 * This is symmetric, so that doing both keeps the same lines displayed.
3101 * Three lines are examined:
3102 *
3103 * before CTRL-F after CTRL-F / before CTRL-B
3104 * etc. l1
3105 * l1 last but one line ------------
3106 * l2 last text line l2 top text line
3107 * ------------- l3 second text line
3108 * l3 etc.
3109 */
3110static int get_scroll_overlap(int dir)
3111{
3112 lineoff_T loff;
3113 int min_height = curwin->w_height - 2;
3114
3115 validate_botline();
Luuk van Baalbd28cae2024-04-03 22:50:40 +02003116 if ((dir == BACKWARD && curwin->w_topline == 1)
3117 || (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003118 return min_height + 2; // no overlap, still handle 'smoothscroll'
3119
3120 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3121#ifdef FEAT_DIFF
zeertzjq92325542024-04-12 18:38:38 +02003122 loff.fill = diff_check_fill(curwin, loff.lnum + (dir == BACKWARD))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003123 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3124 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3125#else
3126 loff.height = plines(loff.lnum);
3127#endif
3128
3129 int h1 = loff.height;
3130 if (h1 > min_height)
3131 return min_height + 2; // no overlap
3132 if (dir == FORWARD)
3133 topline_back(&loff);
3134 else
3135 botline_forw(&loff);
3136
3137 int h2 = loff.height;
3138 if (h2 == MAXCOL || h2 + h1 > min_height)
3139 return min_height + 2; // no overlap
3140 if (dir == FORWARD)
3141 topline_back(&loff);
3142 else
3143 botline_forw(&loff);
3144
3145 int h3 = loff.height;
3146 if (h3 == MAXCOL || h3 + h2 > min_height)
3147 return min_height + 2; // no overlap
3148 if (dir == FORWARD)
3149 topline_back(&loff);
3150 else
3151 botline_forw(&loff);
3152
3153 int h4 = loff.height;
3154 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3155 return min_height + 1; // 1 line overlap
3156 else
3157 return min_height; // 2 lines overlap
3158}
3159
3160/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02003161 * Scroll "count" lines with 'smoothscroll' in direction "dir". Return TRUE
Luuk van Baal58448e02024-05-11 11:27:52 +02003162 * when scrolling happened. Adjust "curscount" for scrolling different amount of
3163 * lines when 'smoothscroll' is disabled.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003164 */
Luuk van Baal58448e02024-05-11 11:27:52 +02003165static int scroll_with_sms(int dir, long count, long *curscount)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003166{
3167 int prev_sms = curwin->w_p_sms;
3168 colnr_T prev_skipcol = curwin->w_skipcol;
3169 linenr_T prev_topline = curwin->w_topline;
3170#ifdef FEAT_DIFF
3171 int prev_topfill = curwin->w_topfill;
3172#endif
3173
3174 curwin->w_p_sms = TRUE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003175 scroll_redraw(dir == FORWARD, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003176
3177 // Not actually smoothscrolling but ended up with partially visible line.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003178 // Continue scrolling until skipcol is zero.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003179 if (!prev_sms && curwin->w_skipcol > 0)
3180 {
3181 int fixdir = dir;
3182 // Reverse the scroll direction when topline already changed. One line
3183 // extra for scrolling backward so that consuming skipcol is symmetric.
3184 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3185 fixdir = dir * -1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003186 while (curwin->w_skipcol > 0
3187 && curwin->w_topline < curbuf->b_ml.ml_line_count)
Luuk van Baal58448e02024-05-11 11:27:52 +02003188 {
Luuk van Baalcb204e62024-04-02 20:49:45 +02003189 scroll_redraw(fixdir == FORWARD, 1);
Luuk van Baal58448e02024-05-11 11:27:52 +02003190 *curscount += (fixdir == dir ? 1 : -1);
3191 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003192 }
3193 curwin->w_p_sms = prev_sms;
3194
3195 return curwin->w_topline == prev_topline
3196#ifdef FEAT_DIFF
3197 && curwin->w_topfill == prev_topfill
3198#endif
3199 && curwin->w_skipcol == prev_skipcol;
3200}
3201
3202/*
3203 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3204 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3205 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003207 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 */
3209 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003210pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003212 int nochange = TRUE;
3213 int buflen = curbuf->b_ml.ml_line_count;
3214 colnr_T prev_col = curwin->w_cursor.col;
Luuk van Baal78c51502024-04-09 21:30:19 +02003215 colnr_T prev_curswant = curwin->w_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003216 linenr_T prev_lnum = curwin->w_cursor.lnum;
3217 oparg_T oa = { 0 };
3218 cmdarg_T ca = { 0 };
3219 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003221 if (half)
3222 {
3223 // Scroll [count], 'scroll' or current window height lines.
3224 if (count)
3225 curwin->w_p_scr = MIN(curwin->w_height, count);
3226 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Luuk van Baal58448e02024-05-11 11:27:52 +02003228 long curscount = count;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003229 // Adjust count so as to not reveal end of buffer lines.
Luuk van Baal32d701f2024-04-28 16:24:02 +02003230 if (dir == FORWARD
3231 && (curwin->w_topline + curwin->w_height + count > buflen
3232#ifdef FEAT_FOLDING
3233 || hasAnyFolding(curwin)
3234#endif
3235 ))
Luuk van Baalcb204e62024-04-02 20:49:45 +02003236 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003237 int n = plines_correct_topline(curwin, curwin->w_topline, FALSE);
3238 if (n - count < curwin->w_height && curwin->w_topline < buflen)
Luuk van Baal32d701f2024-04-28 16:24:02 +02003239 n += plines_m_win(curwin, curwin->w_topline + 1, buflen,
3240 curwin->w_height + count);
3241 if (n < curwin->w_height + count)
Luuk van Baal9148ba82024-04-08 22:27:41 +02003242 count = n - curwin->w_height;
3243 }
3244
Luuk van Baal78c51502024-04-09 21:30:19 +02003245 // (Try to) scroll the window unless already at the end of the buffer.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003246 if (count > 0)
3247 {
Luuk van Baal58448e02024-05-11 11:27:52 +02003248 nochange = scroll_with_sms(dir, count, &curscount);
Luuk van Baal78c51502024-04-09 21:30:19 +02003249 curwin->w_cursor.lnum = prev_lnum;
3250 curwin->w_cursor.col = prev_col;
3251 curwin->w_curswant = prev_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Luuk van Baal78c51502024-04-09 21:30:19 +02003254 // Move the cursor the same amount of screen lines.
3255 if (curwin->w_p_wrap)
3256 nv_screengo(&oa, dir, curscount);
3257 else if (dir == FORWARD)
3258 cursor_down_inner(curwin, curscount);
3259 else
3260 cursor_up_inner(curwin, curscount);
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003261 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003262 else
3263 {
3264 // Scroll [count] times 'window' or current window height lines.
3265 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3266 MAX(1, p_window - 2) : get_scroll_overlap(dir));
Luuk van Baal58448e02024-05-11 11:27:52 +02003267 nochange = scroll_with_sms(dir, count, &count);
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003268
3269 // Place cursor at top or bottom of window.
3270 validate_botline();
3271 curwin->w_cursor.lnum = (dir == FORWARD ? curwin->w_topline
3272 : curwin->w_botline - 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003273 }
3274
Luuk van Baal4b6b0c42024-04-20 17:38:20 +02003275 if (get_scrolloff_value() > 0)
3276 cursor_correct();
3277#ifdef FEAT_FOLDING
3278 // Move cursor to first line of closed fold.
3279 foldAdjustCursor();
3280#endif
3281 nochange = nochange
3282 && prev_col == curwin->w_cursor.col
3283 && prev_lnum == curwin->w_cursor.lnum;
3284
Luuk van Baalcb204e62024-04-02 20:49:45 +02003285 // Error if both the viewport and cursor did not change.
3286 if (nochange)
3287 beep_flush();
3288 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003289 beginline(BL_SOL | BL_FIX);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003290 else if (p_sol)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003291 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003292
3293 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294}
3295
Bram Moolenaar860cae12010-06-05 23:22:07 +02003296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003297do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003298{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003299 static win_T *prev_curwin = NULL;
3300 static pos_T prev_cursor = {0, 0, 0};
3301
3302 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3303 return;
3304 prev_curwin = curwin;
3305 prev_cursor = curwin->w_cursor;
3306
Bram Moolenaar860cae12010-06-05 23:22:07 +02003307 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003308 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003309 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003310 colnr_T curswant = curwin->w_curswant;
3311 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 win_T *old_curwin = curwin;
3313 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003314 int old_VIsual_select = VIsual_select;
3315 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003316
3317 /*
3318 * loop through the cursorbound windows
3319 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003321 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003322 {
3323 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003324 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003325 if (curwin != old_curwin && curwin->w_p_crb)
3326 {
3327# ifdef FEAT_DIFF
3328 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003329 curwin->w_cursor.lnum =
3330 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003331 else
3332# endif
3333 curwin->w_cursor.lnum = line;
3334 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003335 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003336 curwin->w_curswant = curswant;
3337 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003338
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339 // Make sure the cursor is in a valid position. Temporarily set
3340 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003341 int restart_edit_save = restart_edit;
3342 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003343 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003344
3345 // Avoid a scroll here for the cursor position, 'scrollbind' is
3346 // more important.
3347 if (!curwin->w_p_scb)
3348 validate_cursor();
3349
Bram Moolenaar61452852011-02-01 18:01:11 +01003350 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003351 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352 if (has_mbyte)
3353 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003354 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003355
Bram Moolenaar85a20022019-12-21 18:25:54 +01003356 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003357 if (!curwin->w_p_scb)
3358 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003359 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003360 }
3361 }
3362
3363 /*
3364 * reset current-window
3365 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003366 VIsual_select = old_VIsual_select;
3367 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003368 curwin = old_curwin;
3369 curbuf = old_curbuf;
3370}