blob: 8c6fbd6b88cbbb00d30d9f7b7f2bac1f77f8e2f8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
zeertzjq3f1b5312024-02-06 10:43:36 +010022static void redraw_for_cursorline(win_T *wp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
Bram Moolenaar85a20022019-12-21 18:25:54 +010029 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010031 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010033 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000034} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
zeertzjq6235a102023-08-19 14:12:42 +020040 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010041 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010042 int
zeertzjq6235a102023-08-19 14:12:42 +020043adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010044{
45 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020046 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047
Bram Moolenaarf6196f42022-10-02 21:29:55 +010048 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010049 int w2 = width + win_col_off2(wp);
50 if (wp->w_skipcol >= width && w2 > 0)
51 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020052
53 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054}
55
56/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010057 * Return how many lines "lnum" will take on the screen, taking into account
58 * whether it is the first line, whether w_skipcol is non-zero and limiting to
59 * the window height.
60 */
61 static int
Luuk van Baal9148ba82024-04-08 22:27:41 +020062plines_correct_topline(win_T *wp, linenr_T lnum, int limit_winheight)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010063{
64 int n;
65#ifdef FEAT_DIFF
66 if (lnum == wp->w_topline)
67 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
68 else
69#endif
70 n = plines_win(wp, lnum, FALSE);
71 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020072 n -= adjust_plines_for_skipcol(wp);
Luuk van Baal9148ba82024-04-08 22:27:41 +020073 if (limit_winheight && n > wp->w_height)
Bram Moolenaard5337ef2022-10-20 20:15:47 +010074 n = wp->w_height;
75 return n;
76}
77
78/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Compute wp->w_botline for the current wp->w_topline. Can be called after
80 * wp->w_topline changed.
81 */
82 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010083comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int n;
86 linenr_T lnum;
87 int done;
88#ifdef FEAT_FOLDING
89 linenr_T last;
90 int folded;
91#endif
92
93 /*
94 * If w_cline_row is valid, start there.
95 * Otherwise have to start at w_topline.
96 */
97 check_cursor_moved(wp);
98 if (wp->w_valid & VALID_CROW)
99 {
100 lnum = wp->w_cursor.lnum;
101 done = wp->w_cline_row;
102 }
103 else
104 {
105 lnum = wp->w_topline;
106 done = 0;
107 }
108
109 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
110 {
111#ifdef FEAT_FOLDING
112 last = lnum;
113 folded = FALSE;
114 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
115 {
116 n = 1;
117 folded = TRUE;
118 }
119 else
120#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200122 n = plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if (
125#ifdef FEAT_FOLDING
126 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
127#else
128 lnum == wp->w_cursor.lnum
129#endif
130 )
131 {
132 wp->w_cline_row = done;
133 wp->w_cline_height = n;
134#ifdef FEAT_FOLDING
135 wp->w_cline_folded = folded;
136#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100137 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
139 }
140 if (done + n > wp->w_height)
141 break;
142 done += n;
143#ifdef FEAT_FOLDING
144 lnum = last;
145#endif
146 }
147
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 wp->w_botline = lnum;
150 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
151
152 set_empty_rows(wp, done);
153}
154
155/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100156 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
157 * set.
158 */
zeertzjq3f1b5312024-02-06 10:43:36 +0100159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100160redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100161{
162 if ((wp->w_p_rnu
163#ifdef FEAT_SYN_HL
164 || wp->w_p_cul
165#endif
166 )
167 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200168 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200169 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000170 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100171 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200172 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100173}
174
zeertzjq3e559cd2022-03-27 19:26:55 +0100175#ifdef FEAT_SYN_HL
176/*
177 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
178 * contains "screenline".
179 */
180 static void
181redraw_for_cursorcolumn(win_T *wp)
182{
183 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
184 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100186 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100187 redraw_win_later(wp, UPD_SOME_VALID);
188 // When 'cursorlineopt' contains "screenline" need to redraw with
189 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100192 }
193}
194#endif
195
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100196/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100197 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
198 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000199 * Parameter "extra2" should be the padding on the 2nd line, not the first
Luuk van Baalcb204e62024-04-02 20:49:45 +0200200 * line. When "extra2" is -1 calculate the padding.
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000201 * Returns the number of columns of overlap with buffer text, excluding the
202 * extra padding on the ledge.
203 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100204 int
205sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000206{
Luuk van Baalcb204e62024-04-02 20:49:45 +0200207 if (extra2 == -1)
208 extra2 = win_col_off(wp) - win_col_off2(wp);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100210 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000211 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000213 return 0;
214#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100215 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
216 if (wp->w_p_list && wp->w_lcs_chars.prec)
217 return 1;
218
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return extra2 > 3 ? 0 : 3 - extra2;
220}
221
222/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000223 * Calculates the skipcol offset for window "wp" given how many
224 * physical lines we want to scroll down.
225 */
226 static int
227skipcol_from_plines(win_T *wp, int plines_off)
228{
229 int width1 = wp->w_width - win_col_off(wp);
230
231 int skipcol = 0;
232 if (plines_off > 0)
233 skipcol += width1;
234 if (plines_off > 1)
235 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
236 return skipcol;
237}
238
239/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100240 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000241 */
242 static void
243reset_skipcol(void)
244{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000245 if (curwin->w_skipcol == 0)
246 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000248 curwin->w_skipcol = 0;
249
250 // Should use the least expensive way that displays all that changed.
251 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
252 // enough when the top line gets another screen line.
253 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000254}
255
256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * Update curwin->w_topline and redraw if necessary.
258 * Used to update the screen before printing a message.
259 */
260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100261update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 update_topline();
264 if (must_redraw)
265 update_screen(0);
266}
267
268/*
269 * Update curwin->w_topline to move the cursor onto the screen.
270 */
271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100272update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 long line_count;
275 int halfheight;
276 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifdef FEAT_FOLDING
278 linenr_T lnum;
279#endif
280 int check_topline = FALSE;
281 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100282 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100283 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100285 // Cursor is updated instead when this is TRUE for 'splitkeep'.
286 if (skip_update_topline)
287 return;
288
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // If there is no valid screen and when the window height is zero just use
290 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200291 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100293 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 curwin->w_topline = curwin->w_cursor.lnum;
295 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200296 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200297 return;
298 }
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 check_cursor_moved(curwin);
301 if (curwin->w_valid & VALID_TOPLINE)
302 return;
303
Bram Moolenaar85a20022019-12-21 18:25:54 +0100304 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000305 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100306 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100308 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100310 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312
313 /*
314 * If the buffer is empty, always set topline to 1.
315 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100316 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
318 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100319 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 curwin->w_botline = 2;
322 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325
326 /*
327 * If the cursor is above or near the top of the window, scroll the window
328 * to show the line the cursor is in, with 'scrolloff' context.
329 */
330 else
331 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100332 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100334 // If the cursor is above topline, scrolling is always needed.
335 // If the cursor is far below topline and there is no folding,
336 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 if (curwin->w_cursor.lnum < curwin->w_topline)
338 check_topline = TRUE;
339 else if (check_top_offset())
340 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100341 else if (curwin->w_skipcol > 0
342 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100343 {
344 colnr_T vcol;
Luuk van Baalcb204e62024-04-02 20:49:45 +0200345 int overlap;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100346
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000347 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100348 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baalcb204e62024-04-02 20:49:45 +0200350 overlap = sms_marker_overlap(curwin, -1);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100351 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100352 check_topline = TRUE;
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
358 curwin->w_topline))
359 check_topline = TRUE;
360#endif
361
362 if (check_topline)
363 {
364 halfheight = curwin->w_height / 2 - 1;
365 if (halfheight < 2)
366 halfheight = 2;
367
368#ifdef FEAT_FOLDING
369 if (hasAnyFolding(curwin))
370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 // Count the number of logical lines between the cursor and
372 // topline + scrolloff (approximation of how much will be
373 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 n = 0;
375 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {
378 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100379 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
381 break;
382 (void)hasFolding(lnum, NULL, &lnum);
383 }
384 }
385 else
386#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100387 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaar85a20022019-12-21 18:25:54 +0100389 // If we weren't very close to begin with, we scroll to put the
390 // cursor in the middle of the window. Otherwise put the cursor
391 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000393 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 else
395 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000396 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 check_botline = TRUE;
398 }
399 }
400
401 else
402 {
403#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100404 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
406#endif
407 check_botline = TRUE;
408 }
409 }
410
411 /*
412 * If the cursor is below the bottom of the window, scroll the window
413 * to put the cursor on the window.
414 * When w_botline is invalid, recompute it first, to avoid a redraw later.
415 * If w_botline was approximated, we might need a redraw later in a few
416 * cases, but we don't want to spend (a lot of) time recomputing w_botline
417 * for every small change.
418 */
419 if (check_botline)
420 {
421 if (!(curwin->w_valid & VALID_BOTLINE_AP))
422 validate_botline();
423
424 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
425 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000426 if (curwin->w_cursor.lnum < curwin->w_botline)
427 {
428 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100429 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#ifdef FEAT_FOLDING
431 || hasAnyFolding(curwin)
432#endif
433 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 lineoff_T loff;
436
Bram Moolenaar85a20022019-12-21 18:25:54 +0100437 // Cursor is (a few lines) above botline, check if there are
438 // 'scrolloff' window lines below the cursor. If not, need to
439 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 n = curwin->w_empty_rows;
441 loff.lnum = curwin->w_cursor.lnum;
442#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
445#endif
446#ifdef FEAT_DIFF
447 loff.fill = 0;
448 n += curwin->w_filler_rows;
449#endif
450 loff.height = 0;
451 while (loff.lnum < curwin->w_botline
452#ifdef FEAT_DIFF
453 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
454#endif
455 )
456 {
457 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 break;
460 botline_forw(&loff);
461 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100462 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100463 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000465 }
466 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100467 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000468 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 }
470 if (check_botline)
471 {
472#ifdef FEAT_FOLDING
473 if (hasAnyFolding(curwin))
474 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100475 // Count the number of logical lines between the cursor and
476 // botline - scrolloff (approximation of how much will be
477 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 line_count = 0;
479 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100483 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (lnum <= 0 || line_count > curwin->w_height + 1)
485 break;
486 (void)hasFolding(lnum, &lnum, NULL);
487 }
488 }
489 else
490#endif
491 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100492 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000494 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000496 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 }
499 }
500 curwin->w_valid |= VALID_TOPLINE;
501
502 /*
503 * Need to redraw when topline changed.
504 */
505 if (curwin->w_topline != old_topline
506#ifdef FEAT_DIFF
507 || curwin->w_topfill != old_topfill
508#endif
509 )
510 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100511 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000512 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100513
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100514 // When 'smoothscroll' is not set, should reset w_skipcol.
515 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100516 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100517 else if (curwin->w_skipcol != 0)
518 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000519
Bram Moolenaar85a20022019-12-21 18:25:54 +0100520 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (curwin->w_cursor.lnum == curwin->w_topline)
522 validate_cursor();
523 }
524
Bram Moolenaar375e3392019-01-31 18:26:10 +0100525 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
528/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000529 * Return the scrolljump value to use for the current window.
530 * When 'scrolljump' is positive use it as-is.
531 * When 'scrolljump' is negative use it as a percentage of the window height.
532 */
533 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100534scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000535{
536 if (p_sj >= 0)
537 return (int)p_sj;
538 return (curwin->w_height * -p_sj) / 100;
539}
540
541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
543 * current window.
544 */
545 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 lineoff_T loff;
549 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100550 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar375e3392019-01-31 18:26:10 +0100552 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_FOLDING
554 || hasAnyFolding(curwin)
555#endif
556 )
557 {
558 loff.lnum = curwin->w_cursor.lnum;
559#ifdef FEAT_DIFF
560 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100561 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#else
563 n = 0;
564#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100565 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100566 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
568 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100569 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (loff.lnum < curwin->w_topline
571#ifdef FEAT_DIFF
572 || (loff.lnum == curwin->w_topline && loff.fill > 0)
573#endif
574 )
575 break;
576 n += loff.height;
577 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100578 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return TRUE;
580 }
581 return FALSE;
582}
583
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100584/*
585 * Update w_curswant.
586 */
587 void
588update_curswant_force(void)
589{
590 validate_virtcol();
591 curwin->w_curswant = curwin->w_virtcol
592#ifdef FEAT_PROP_POPUP
593 - curwin->w_virtcol_first_char
594#endif
595 ;
596 curwin->w_set_curswant = FALSE;
597}
598
599/*
600 * Update w_curswant if w_set_curswant is set.
601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100606 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607}
608
609/*
610 * Check if the cursor has moved. Set the w_valid flag accordingly.
611 */
612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100613check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
616 {
617 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100618 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
619 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 wp->w_valid_cursor = wp->w_cursor;
621 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100622 wp->w_valid_skipcol = wp->w_skipcol;
623 }
624 else if (wp->w_skipcol != wp->w_valid_skipcol)
625 {
626 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
627 |VALID_CHEIGHT|VALID_CROW
628 |VALID_BOTLINE|VALID_BOTLINE_AP);
629 wp->w_valid_cursor = wp->w_cursor;
630 wp->w_valid_leftcol = wp->w_leftcol;
631 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
633 else if (wp->w_cursor.col != wp->w_valid_cursor.col
634 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100635 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
638 wp->w_valid_cursor.col = wp->w_cursor.col;
639 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642}
643
644/*
645 * Call this function when some window settings have changed, which require
646 * the cursor position, botline and topline to be recomputed and the window to
647 * be redrawn. E.g, when changing the 'wrap' option or folding.
648 */
649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 changed_window_setting_win(curwin);
653}
654
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 wp->w_lines_valid = 0;
659 changed_line_abv_curs_win(wp);
660 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100661 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
Dominique Pellee764d1b2023-03-12 21:20:59 +0000664#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000666 * Call changed_window_setting_win() for every window containing "buf".
667 */
668 void
669changed_window_setting_buf(buf_T *buf)
670{
671 tabpage_T *tp;
672 win_T *wp;
673
674 FOR_ALL_TAB_WINDOWS(tp, wp)
675 if (wp->w_buffer == buf)
676 changed_window_setting_win(wp);
677}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000678#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000679
680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 * Set wp->w_topline to a certain number.
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200686#ifdef FEAT_DIFF
687 linenr_T prev_topline = wp->w_topline;
688#endif
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100691 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
693#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100694 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100696 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
697 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000699 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200701 if (lnum != prev_topline)
702 // Keep the filler lines when the topline didn't change.
703 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100706 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100707 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710/*
711 * Call this function when the length of the cursor line (in screen
712 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100713 * If the line length changed the number of screen lines might change,
714 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 * Need to take care of w_botline separately!
716 */
717 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100718changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaar85090142023-06-01 19:27:08 +0100720 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 |VALID_CHEIGHT|VALID_TOPLINE);
722}
723
724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100725changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
Bram Moolenaar85090142023-06-01 19:27:08 +0100727 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 |VALID_CHEIGHT|VALID_TOPLINE);
729}
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731/*
732 * Call this function when the length of a line (in screen characters) above
733 * the cursor have changed.
734 * Need to take care of w_botline separately!
735 */
736 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100737changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
740 |VALID_CHEIGHT|VALID_TOPLINE);
741}
742
743 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
747 |VALID_CHEIGHT|VALID_TOPLINE);
748}
749
Dominique Pellee764d1b2023-03-12 21:20:59 +0000750#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100752 * Display of line has changed for "buf", invalidate cursor position and
753 * w_botline.
754 */
755 void
756changed_line_display_buf(buf_T *buf)
757{
758 win_T *wp;
759
760 FOR_ALL_WINDOWS(wp)
761 if (wp->w_buffer == buf)
762 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
763 |VALID_CROW|VALID_CHEIGHT
764 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
765}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000766#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100767
768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Make sure the value of curwin->w_botline is valid.
770 */
771 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100772validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100774 validate_botline_win(curwin);
775}
776
777/*
778 * Make sure the value of wp->w_botline is valid.
779 */
780 void
781validate_botline_win(win_T *wp)
782{
783 if (!(wp->w_valid & VALID_BOTLINE))
784 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786
787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * Mark curwin->w_botline as invalid (because of some change in the buffer).
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
794}
795
796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100797invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
799 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
800}
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803approximate_botline_win(
804 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 wp->w_valid &= ~VALID_BOTLINE;
807}
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
810 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
811 */
812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100813cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 check_cursor_moved(curwin);
816 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
817 (VALID_WROW|VALID_WCOL));
818}
819
820/*
821 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
822 * w_topline must be valid, you may need to call update_topline() first!
823 */
824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100825validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100827 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 check_cursor_moved(curwin);
829 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
830 curs_columns(TRUE);
831}
832
833#if defined(FEAT_GUI) || defined(PROTO)
834/*
835 * validate w_cline_row.
836 */
837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 /*
841 * First make sure that w_topline is valid (after moving the cursor).
842 */
843 update_topline();
844 check_cursor_moved(curwin);
845 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100846 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848#endif
849
850/*
851 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200852 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 */
854 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100855curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 linenr_T lnum;
858 int i;
859 int all_invalid;
860 int valid;
861#ifdef FEAT_FOLDING
862 long fold_count;
863#endif
864
Bram Moolenaar85a20022019-12-21 18:25:54 +0100865 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 all_invalid = (!redrawing()
867 || wp->w_lines_valid == 0
868 || wp->w_lines[0].wl_lnum > wp->w_topline);
869 i = 0;
870 wp->w_cline_row = 0;
871 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
872 {
873 valid = FALSE;
874 if (!all_invalid && i < wp->w_lines_valid)
875 {
876 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (wp->w_lines[i].wl_lnum == lnum)
879 {
880#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100881 // Check for newly inserted lines below this row, in which
882 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (!wp->w_buffer->b_mod_set
884 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
885 || wp->w_buffer->b_mod_top
886 > wp->w_lines[i].wl_lastlnum + 1)
887#endif
888 valid = TRUE;
889 }
890 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100894 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100896 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100898 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
900#ifdef FEAT_FOLDING
901 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100902 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (lnum > wp->w_cursor.lnum)
904 break;
905#else
906 ++lnum;
907#endif
908 wp->w_cline_row += wp->w_lines[i].wl_size;
909 }
910 else
911 {
912#ifdef FEAT_FOLDING
913 fold_count = foldedCount(wp, lnum, NULL);
914 if (fold_count)
915 {
916 lnum += fold_count;
917 if (lnum > wp->w_cursor.lnum)
918 break;
919 ++wp->w_cline_row;
920 }
921 else
922#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100923 {
Luuk van Baal9148ba82024-04-08 22:27:41 +0200924 wp->w_cline_row += plines_correct_topline(wp, lnum, TRUE);
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100925 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 }
929
930 check_cursor_moved(wp);
931 if (!(wp->w_valid & VALID_CHEIGHT))
932 {
933 if (all_invalid
934 || i == wp->w_lines_valid
935 || (i < wp->w_lines_valid
936 && (!wp->w_lines[i].wl_valid
937 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
938 {
939#ifdef FEAT_DIFF
940 if (wp->w_cursor.lnum == wp->w_topline)
941 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
942 TRUE) + wp->w_topfill;
943 else
944#endif
945 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
946#ifdef FEAT_FOLDING
947 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
948 NULL, NULL, TRUE, NULL);
949#endif
950 }
951 else if (i > wp->w_lines_valid)
952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100953 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 wp->w_cline_height = 0;
955#ifdef FEAT_FOLDING
956 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
957 NULL, NULL, TRUE, NULL);
958#endif
959 }
960 else
961 {
962 wp->w_cline_height = wp->w_lines[i].wl_size;
963#ifdef FEAT_FOLDING
964 wp->w_cline_folded = wp->w_lines[i].wl_folded;
965#endif
966 }
967 }
968
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100969 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Validate curwin->w_virtcol only.
975 */
976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100977validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 validate_virtcol_win(curwin);
980}
981
982/*
983 * Validate wp->w_virtcol only.
984 */
985 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100986validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000989
990 if (wp->w_valid & VALID_VIRTCOL)
991 return;
992
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100995#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000997#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000998 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000999#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001000 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/*
1004 * Validate curwin->w_cline_height only.
1005 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001007validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001010
1011 if (curwin->w_valid & VALID_CHEIGHT)
1012 return;
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001015 if (curwin->w_cursor.lnum == curwin->w_topline)
1016 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1017 + curwin->w_topfill;
1018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001022 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001024 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001028 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 */
1030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001031validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
1033 colnr_T off;
1034 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001035 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001039 if (curwin->w_valid & VALID_WCOL)
1040 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001041
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001042 col = curwin->w_virtcol;
1043 off = curwin_col_off();
1044 col += off;
1045 width = curwin->w_width - off + curwin_col_off2();
1046
1047 // long line wrapping, adjust curwin->w_wrow
1048 if (curwin->w_p_wrap
1049 && col >= (colnr_T)curwin->w_width
1050 && width > 0)
1051 // use same formula as what is used in curs_columns()
1052 col -= ((col - curwin->w_width) / width + 1) * width;
1053 if (col > (int)curwin->w_leftcol)
1054 col -= curwin->w_leftcol;
1055 else
1056 col = 0;
1057 curwin->w_wcol = col;
1058
1059 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001060#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001061 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001066 * Compute offset of a window, occupied by absolute or relative line number,
1067 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar64486672010-05-16 15:46:46 +02001072 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001073 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_FOLDING
1075 + wp->w_p_fdc
1076#endif
1077#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001078 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
1080 );
1081}
1082
1083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001084curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 return win_col_off(curwin);
1087}
1088
1089/*
1090 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001091 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1092 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
1094 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001095win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar64486672010-05-16 15:46:46 +02001097 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001098 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 return 0;
1100}
1101
1102 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001103curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 return win_col_off2(curwin);
1106}
1107
1108/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001109 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Also updates curwin->w_wrow and curwin->w_cline_row.
1111 * Also updates curwin->w_leftcol.
1112 */
1113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001115 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int off_left, off_right;
1120 int n;
1121 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001122 int width1; // text width for first screen line
1123 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 int new_leftcol;
1125 colnr_T startcol;
1126 colnr_T endcol;
1127 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001128 long so = get_scrolloff_value();
1129 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001130 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 /*
1133 * First make sure that w_topline is valid (after moving the cursor).
1134 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001135 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 /*
1138 * Next make sure that w_cline_row is valid.
1139 */
1140 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001141 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001143#ifdef FEAT_PROP_POPUP
1144 // will be set by getvvcol() but not reset
1145 curwin->w_virtcol_first_char = 0;
1146#endif
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /*
1149 * Compute the number of virtual columns.
1150 */
1151#ifdef FEAT_FOLDING
1152 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1155 else
1156#endif
1157 getvvcol(curwin, &curwin->w_cursor,
1158 &startcol, &(curwin->w_virtcol), &endcol);
1159
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001162 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 extra = curwin_col_off();
1165 curwin->w_wcol = curwin->w_virtcol + extra;
1166 endcol += extra;
1167
1168 /*
1169 * Now compute w_wrow, counting screen lines from w_cline_row.
1170 */
1171 curwin->w_wrow = curwin->w_cline_row;
1172
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001173 width1 = curwin->w_width - extra;
1174 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001177 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001178 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001179 if (curwin->w_p_wrap)
1180 curwin->w_wrow = curwin->w_height - 1;
1181 else
1182 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001184 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001186 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001188 // skip columns that are not visible
1189 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001190 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001191 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001192 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 // Deduct by multiples of width2. This allows the long line
1194 // wrapping formula below to correctly calculate the w_wcol value
1195 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001198 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001201
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001202 did_sub_skipcol = TRUE;
1203 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001204
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001206 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001209 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1210 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 }
1214
Bram Moolenaar85a20022019-12-21 18:25:54 +01001215 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1216 // is not folded.
1217 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001218 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#ifdef FEAT_FOLDING
1220 && !curwin->w_cline_folded
1221#endif
1222 )
1223 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001224#ifdef FEAT_PROP_POPUP
1225 if (curwin->w_virtcol_first_char > 0)
1226 {
1227 int cols = (curwin->w_width - extra);
1228 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1229
1230 // each "above" text prop shifts the text one row down
1231 curwin->w_wrow += rows;
1232 curwin->w_wcol -= rows * cols;
1233 endcol -= rows * cols;
1234 curwin->w_cline_height = rows + 1;
1235 }
1236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 /*
1238 * If Cursor is left of the screen, scroll rightwards.
1239 * If Cursor is right of the screen, scroll leftwards
1240 * If we get closer to the edge than 'sidescrolloff', scroll a little
1241 * extra
1242 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001243 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001244 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001245 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (off_left < 0 || off_right > 0)
1247 {
1248 if (off_left < 0)
1249 diff = -off_left;
1250 else
1251 diff = off_right;
1252
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // When far off or not enough room on either side, put cursor in
1254 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001255 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1256 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else
1258 {
1259 if (diff < p_ss)
1260 diff = p_ss;
1261 if (off_left < 0)
1262 new_leftcol = curwin->w_leftcol - diff;
1263 else
1264 new_leftcol = curwin->w_leftcol + diff;
1265 }
1266 if (new_leftcol < 0)
1267 new_leftcol = 0;
1268 if (new_leftcol != (int)curwin->w_leftcol)
1269 {
1270 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001271 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001272 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275 curwin->w_wcol -= curwin->w_leftcol;
1276 }
1277 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1278 curwin->w_wcol -= curwin->w_leftcol;
1279 else
1280 curwin->w_wcol = 0;
1281
1282#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001283 // Skip over filler lines. At the top use w_topfill, there
1284 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (curwin->w_cursor.lnum == curwin->w_topline)
1286 curwin->w_wrow += curwin->w_topfill;
1287 else
1288 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1289#endif
1290
1291 prev_skipcol = curwin->w_skipcol;
1292
1293 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if ((curwin->w_wrow >= curwin->w_height
1296 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001297 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 && (p_lines =
1299#ifdef FEAT_DIFF
1300 plines_win_nofill
1301#else
1302 plines_win
1303#endif
1304 (curwin, curwin->w_cursor.lnum, FALSE))
1305 - 1 >= curwin->w_height))
1306 && curwin->w_height != 0
1307 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001308 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001309 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001311 // Cursor past end of screen. Happens with a single line that does
1312 // not fit on screen. Find a skipcol to show the text around the
1313 // cursor. Avoid scrolling all the time. compute value of "extra":
1314 // 1: Less than 'scrolloff' lines above
1315 // 2: Less than 'scrolloff' lines below
1316 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001318 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001320 // Compute last display line of the buffer line that we want at the
1321 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (p_lines == 0)
1323 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1324 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001325 if (p_lines > curwin->w_wrow + so)
1326 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 else
1328 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001329 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 extra += 2;
1331
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001332 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001335 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (n > curwin->w_height / 2)
1337 n -= curwin->w_height / 2;
1338 else
1339 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001340 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (n > p_lines - curwin->w_height + 1)
1342 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001343 if (n > 0)
1344 curwin->w_skipcol = width1 + (n - 1) * width2;
1345 else
1346 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 else if (extra == 1)
1349 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001350 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1352 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (extra > 0)
1354 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001355 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1356 extra = curwin->w_skipcol / width2;
1357 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360 else if (extra == 2)
1361 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001362 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001363 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001365 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (endcol > curwin->w_skipcol)
1367 curwin->w_skipcol = endcol;
1368 }
1369
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001370 // adjust w_wrow for the changed w_skipcol
1371 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001372 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001373 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001374 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (curwin->w_wrow >= curwin->w_height)
1377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001380 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 curwin->w_wrow -= extra;
1382 }
1383
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001384 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (extra > 0)
1386 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1387 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001388 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001390 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 curwin->w_skipcol = 0;
1392 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001393 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001395#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001396 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001397#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001398#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1399 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1400 {
1401 curwin->w_wrow += popup_top_extra(curwin);
1402 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001403 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001404 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001405 else
1406 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001407#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001408
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001409 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1410 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001411 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001412 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1415}
1416
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001417#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001418/*
1419 * Compute the screen position of text character at "pos" in window "wp"
1420 * The resulting values are one-based, zero when character is not visible.
1421 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001422 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001423textpos2screenpos(
1424 win_T *wp,
1425 pos_T *pos,
1426 int *rowp, // screen row
1427 int *scolp, // start screen column
1428 int *ccolp, // cursor screen column
1429 int *ecolp) // end screen column
1430{
1431 colnr_T scol = 0, ccol = 0, ecol = 0;
1432 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001433 colnr_T coloff = 0;
1434
Bram Moolenaar189663b2021-07-21 18:04:56 +02001435 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001437 colnr_T col;
1438 int width;
1439 linenr_T lnum = pos->lnum;
1440#ifdef FEAT_FOLDING
1441 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001442
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001443 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1444#endif
zeertzjq6235a102023-08-19 14:12:42 +02001445 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE);
zeertzjqbfe377b2023-08-17 22:58:53 +02001446 // "row" should be the screen line where line "lnum" begins, which can
1447 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001448 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001449
1450#ifdef FEAT_DIFF
1451 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001452 row += lnum == wp->w_topline ? wp->w_topfill
1453 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001454#endif
1455
zeertzjqba2d1912022-12-18 12:28:59 +00001456 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457#ifdef FEAT_FOLDING
1458 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459 {
zeertzjq6235a102023-08-19 14:12:42 +02001460 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001461 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001462 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001463 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001464#endif
1465 {
1466 getvcol(wp, pos, &scol, &ccol, &ecol);
1467
1468 // similar to what is done in validate_cursor_col()
1469 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001470 col += off;
1471 width = wp->w_width - off + win_col_off2(wp);
1472
1473 // long line wrapping, adjust row
1474 if (wp->w_p_wrap
1475 && col >= (colnr_T)wp->w_width
1476 && width > 0)
1477 {
1478 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001479 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001480 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001481 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001482 }
1483 col -= wp->w_leftcol;
1484 if (col >= wp->w_width)
1485 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001486 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001487 {
1488 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001489 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001490 }
1491 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001492 // character is out of the window
1493 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001494 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001495 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001496 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001497 *scolp = scol + coloff;
1498 *ccolp = ccol + coloff;
1499 *ecolp = ecol + coloff;
1500}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001501#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001502
Bram Moolenaar12034e22019-08-25 22:25:02 +02001503#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001504/*
1505 * "screenpos({winid}, {lnum}, {col})" function
1506 */
1507 void
1508f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1509{
1510 dict_T *dict;
1511 win_T *wp;
1512 pos_T pos;
1513 int row = 0;
1514 int scol = 0, ccol = 0, ecol = 0;
1515
Bram Moolenaar93a10962022-06-16 11:42:09 +01001516 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001517 return;
1518 dict = rettv->vval.v_dict;
1519
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001520 if (in_vim9script()
1521 && (check_for_number_arg(argvars, 0) == FAIL
1522 || check_for_number_arg(argvars, 1) == FAIL
1523 || check_for_number_arg(argvars, 2) == FAIL))
1524 return;
1525
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001526 wp = find_win_by_nr_or_id(&argvars[0]);
1527 if (wp == NULL)
1528 return;
1529
1530 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001531 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1532 {
1533 semsg(_(e_invalid_line_number_nr), pos.lnum);
1534 return;
1535 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001536 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001537 if (pos.col < 0)
1538 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 pos.coladd = 0;
1540 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1541
1542 dict_add_number(dict, "row", row);
1543 dict_add_number(dict, "col", scol);
1544 dict_add_number(dict, "curscol", ccol);
1545 dict_add_number(dict, "endcol", ecol);
1546}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001547
1548/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001549 * Convert a virtual (screen) column to a character column. The first column
1550 * is one. For a multibyte character, the column number of the first byte is
1551 * returned.
1552 */
1553 static int
1554virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1555{
zeertzjqf5a94d52023-10-15 10:03:30 +02001556 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001557 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1558 char_u *p = line + offset;
1559
zeertzjqb583eda2023-10-14 11:32:28 +02001560 if (*p == NUL)
1561 {
1562 if (p == line) // empty line
1563 return 0;
1564 // Move to the first byte of the last char.
1565 MB_PTR_BACK(line, p);
1566 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001567 return (int)(p - line + 1);
1568}
1569
1570/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001571 * "virtcol2col({winid}, {lnum}, {col})" function
1572 */
1573 void
1574f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1575{
1576 win_T *wp;
1577 linenr_T lnum;
1578 int screencol;
1579 int error = FALSE;
1580
1581 rettv->vval.v_number = -1;
1582
1583 if (check_for_number_arg(argvars, 0) == FAIL
1584 || check_for_number_arg(argvars, 1) == FAIL
1585 || check_for_number_arg(argvars, 2) == FAIL)
1586 return;
1587
1588 wp = find_win_by_nr_or_id(&argvars[0]);
1589 if (wp == NULL)
1590 return;
1591
1592 lnum = tv_get_number_chk(&argvars[1], &error);
1593 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1594 return;
1595
1596 screencol = tv_get_number_chk(&argvars[2], &error);
1597 if (error || screencol < 0)
1598 return;
1599
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001600 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001601}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001602#endif
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02001605 * Make sure the cursor is in the visible part of the topline after scrolling
1606 * the screen with 'smoothscroll'.
1607 */
1608static void cursor_correct_sms(void)
1609{
1610 if (!curwin->w_p_sms ||!curwin->w_p_wrap
1611 || curwin->w_cursor.lnum != curwin->w_topline)
1612 return;
1613
1614 long so = get_scrolloff_value();
1615 int width1 = curwin->w_width - curwin_col_off();
1616 int width2 = width1 + curwin_col_off2();
1617 int so_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1618 int space_cols = (curwin->w_height - 1) * width2;
1619 int size = so == 0 ? 0 : win_linetabsize(curwin, curwin->w_topline,
1620 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1621
1622 if (curwin->w_topline == 1 && curwin->w_skipcol == 0)
1623 so_cols = 0; // Ignore 'scrolloff' at top of buffer.
1624 else if (so_cols > space_cols / 2)
1625 so_cols = space_cols / 2; // Not enough room: put cursor in the middle.
1626
1627 // Not enough screen lines in topline: ignore 'scrolloff'.
1628 while (so_cols > size && so_cols - width2 >= width1)
1629 so_cols -= width2;
1630 if (so_cols >= width1 && so_cols > size)
1631 so_cols -= width1;
1632
1633 // If there is no marker or we have non-zero scrolloff, just ignore it.
1634 int overlap = (curwin->w_skipcol == 0 || so_cols != 0) ? 0
1635 : sms_marker_overlap(curwin, -1);
1636 int top = curwin->w_skipcol + overlap + so_cols;
1637 int bot = curwin->w_skipcol + width1 + (curwin->w_height - 1) * width2
1638 - so_cols;
1639 validate_virtcol();
1640 colnr_T col = curwin->w_virtcol;
1641
1642 if (col < top)
1643 {
1644 if (col < width1)
1645 col += width1;
1646 while (width2 > 0 && col < top)
1647 col += width2;
1648 }
1649 else
1650 while (width2 > 0 && col >= bot)
1651 col -= width2;
1652
1653 if (col != curwin->w_virtcol)
1654 {
1655 curwin->w_curswant = col;
1656 coladvance(curwin->w_curswant);
1657 // validate_virtcol() marked various things as valid, but after
1658 // moving the cursor they need to be recomputed
1659 curwin->w_valid &=
1660 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1661 }
1662}
1663
1664/*
1665 * Scroll "count" lines up or down, and redraw.
1666 */
1667 void
1668scroll_redraw(int up, long count)
1669{
1670 linenr_T prev_topline = curwin->w_topline;
1671 int prev_skipcol = curwin->w_skipcol;
1672#ifdef FEAT_DIFF
1673 int prev_topfill = curwin->w_topfill;
1674#endif
1675 linenr_T prev_lnum = curwin->w_cursor.lnum;
1676
1677 if (up)
1678 scrollup(count, TRUE);
1679 else
1680 scrolldown(count, TRUE);
1681 if (get_scrolloff_value() > 0)
1682 {
1683 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
1684 // valid, otherwise the screen jumps back at the end of the file.
1685 cursor_correct();
1686 check_cursor_moved(curwin);
1687 curwin->w_valid |= VALID_TOPLINE;
1688
1689 // If moved back to where we were, at least move the cursor, otherwise
1690 // we get stuck at one position. Don't move the cursor up if the
1691 // first line of the buffer is already on the screen
1692 while (curwin->w_topline == prev_topline
1693 && curwin->w_skipcol == prev_skipcol
1694#ifdef FEAT_DIFF
1695 && curwin->w_topfill == prev_topfill
1696#endif
1697 )
1698 {
1699 if (up)
1700 {
1701 if (curwin->w_cursor.lnum > prev_lnum
1702 || cursor_down(1L, FALSE) == FAIL)
1703 break;
1704 }
1705 else
1706 {
1707 if (curwin->w_cursor.lnum < prev_lnum
1708 || prev_topline == 1L
1709 || cursor_up(1L, FALSE) == FAIL)
1710 break;
1711 }
1712 // Mark w_topline as valid, otherwise the screen jumps back at the
1713 // end of the file.
1714 check_cursor_moved(curwin);
1715 curwin->w_valid |= VALID_TOPLINE;
1716 }
1717 }
1718
1719 cursor_correct_sms();
1720 if (curwin->w_cursor.lnum != prev_lnum)
1721 coladvance(curwin->w_curswant);
1722 redraw_later(UPD_VALID);
1723}
1724
1725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001729scrolldown(
1730 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001731 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001733 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 int wrow;
1735 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001736 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001737 int width1 = 0;
1738 int width2 = 0;
1739
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001740 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001741 {
1742 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001743 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746#ifdef FEAT_FOLDING
1747 linenr_T first;
1748
Bram Moolenaar85a20022019-12-21 18:25:54 +01001749 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1751#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001752 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001753 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001756 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1757 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 ++curwin->w_topfill;
1760 ++done;
1761 }
1762 else
1763#endif
1764 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001765 // break when at the very top
1766 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001767 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001769 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001771 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001772 if (curwin->w_skipcol >= width1 + width2)
1773 curwin->w_skipcol -= width2;
1774 else
1775 curwin->w_skipcol -= width1;
1776 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001780 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001781 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001782 --curwin->w_topline;
1783 curwin->w_skipcol = 0;
1784#ifdef FEAT_DIFF
1785 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001787#ifdef FEAT_FOLDING
1788 // A sequence of folded lines only counts for one logical line
1789 if (hasFolding(curwin->w_topline, &first, NULL))
1790 {
1791 ++done;
1792 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001793 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001794 curwin->w_botline -= curwin->w_topline - first;
1795 curwin->w_topline = first;
1796 }
1797 else
1798#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001799 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001800 {
1801 int size = win_linetabsize(curwin, curwin->w_topline,
1802 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1803 if (size > width1)
1804 {
1805 curwin->w_skipcol = width1;
1806 size -= width1;
1807 redraw_later(UPD_NOT_VALID);
1808 }
1809 while (size > width2)
1810 {
1811 curwin->w_skipcol += width2;
1812 size -= width2;
1813 }
1814 ++done;
1815 }
1816 else
1817 done += PLINES_NOFILL(curwin->w_topline);
1818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 invalidate_botline();
1822 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001823 curwin->w_wrow += done; // keep w_wrow updated
1824 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826#ifdef FEAT_DIFF
1827 if (curwin->w_cursor.lnum == curwin->w_topline)
1828 curwin->w_cline_row = 0;
1829 check_topfill(curwin, TRUE);
1830#endif
1831
1832 /*
1833 * Compute the row number of the last row of the cursor line
1834 * and move the cursor onto the displayed part of the window.
1835 */
1836 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001837 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 validate_virtcol();
1840 validate_cheight();
1841 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001842 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1845 {
1846#ifdef FEAT_FOLDING
1847 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1848 {
1849 --wrow;
1850 if (first == 1)
1851 curwin->w_cursor.lnum = 1;
1852 else
1853 curwin->w_cursor.lnum = first - 1;
1854 }
1855 else
1856#endif
1857 wrow -= plines(curwin->w_cursor.lnum--);
1858 curwin->w_valid &=
1859 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1860 moved = TRUE;
1861 }
1862 if (moved)
1863 {
1864#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001865 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 foldAdjustCursor();
1867#endif
1868 coladvance(curwin->w_curswant);
1869 }
Luuk van Baal9148ba82024-04-08 22:27:41 +02001870 if (curwin->w_cursor.lnum < curwin->w_topline)
1871 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872}
1873
1874/*
1875 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001878scrollup(
1879 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001880 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001882 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001884 if (do_sms
1885# ifdef FEAT_FOLDING
1886 || (byfold && hasAnyFolding(curwin))
1887# endif
1888# ifdef FEAT_DIFF
1889 || (curwin->w_p_diff && !curwin->w_p_wrap)
1890# endif
1891 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001893 int width1 = curwin->w_width - curwin_col_off();
1894 int width2 = width1 + curwin_col_off2();
1895 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001896 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001897
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001898 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001899 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001900
1901 // diff mode: first consume "topfill"
1902 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1903 // the line, then advance to the next line.
1904 // folding: count each sequence of folded lines as one logical line.
1905 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907# ifdef FEAT_DIFF
1908 if (curwin->w_topfill > 0)
1909 --curwin->w_topfill;
1910 else
1911# endif
1912 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001913 linenr_T lnum = curwin->w_topline;
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915# ifdef FEAT_FOLDING
1916 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001917 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 (void)hasFolding(lnum, NULL, &lnum);
1919# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001920 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001921 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001922 // 'smoothscroll': increase "w_skipcol" until it goes over
1923 // the end of the line, then advance to the next line.
1924 int add = curwin->w_skipcol > 0 ? width2 : width1;
1925 curwin->w_skipcol += add;
1926 if (curwin->w_skipcol >= size)
1927 {
1928 if (lnum == curbuf->b_ml.ml_line_count)
1929 {
1930 // at the last screen line, can't scroll further
1931 curwin->w_skipcol -= add;
1932 break;
1933 }
1934 ++lnum;
1935 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001936 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001937 else
1938 {
1939 if (lnum >= curbuf->b_ml.ml_line_count)
1940 break;
1941 ++lnum;
1942 }
1943
1944 if (lnum > curwin->w_topline)
1945 {
1946 // approximate w_botline
1947 curwin->w_botline += lnum - curwin->w_topline;
1948 curwin->w_topline = lnum;
1949# ifdef FEAT_DIFF
1950 curwin->w_topfill = diff_check_fill(curwin, lnum);
1951# endif
1952 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001953 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001954 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001955 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001956 }
1957 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001958
zeertzjqd9a92dc2023-06-05 18:41:35 +01001959 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1960 // need to redraw more, because wl_size of the (new) topline may
1961 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001962 redraw_later(UPD_NOT_VALID);
1963 }
1964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001967 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969
1970 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1971 curwin->w_topline = curbuf->b_ml.ml_line_count;
1972 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1973 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1974
1975#ifdef FEAT_DIFF
1976 check_topfill(curwin, FALSE);
1977#endif
1978
1979#ifdef FEAT_FOLDING
1980 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001981 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1983#endif
1984
1985 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1986 if (curwin->w_cursor.lnum < curwin->w_topline)
1987 {
1988 curwin->w_cursor.lnum = curwin->w_topline;
1989 curwin->w_valid &=
1990 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1991 coladvance(curwin->w_curswant);
1992 }
1993}
1994
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001995/*
1996 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1997 * valid for 'smoothscroll'.
1998 */
1999 void
2000adjust_skipcol(void)
2001{
2002 if (!curwin->w_p_wrap
2003 || !curwin->w_p_sms
2004 || curwin->w_cursor.lnum != curwin->w_topline)
2005 return;
2006
2007 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00002008 if (width1 <= 0)
2009 return; // no text will be displayed
2010
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002011 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002012 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002013 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
2014 int scrolled = FALSE;
2015
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002016 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00002017 if (curwin->w_cline_height == curwin->w_height
2018 // w_cline_height may be capped at w_height, check there aren't
2019 // actually more lines.
2020 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
2021 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002022 {
2023 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002024 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002025 return;
2026 }
2027
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002028 validate_virtcol();
Luuk van Baalcb204e62024-04-02 20:49:45 +02002029 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002030 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01002031 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002032 {
2033 // scroll a screen line down
2034 if (curwin->w_skipcol >= width1 + width2)
2035 curwin->w_skipcol -= width2;
2036 else
2037 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002038 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002039 }
2040 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01002041 {
2042 validate_virtcol();
2043 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002044 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002045 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002046
2047 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2048 int row = 0;
2049 if (col >= width1)
2050 {
2051 col -= width1;
2052 ++row;
2053 }
2054 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002055 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002056 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002057 // col may no longer be used, but make
2058 // sure it is correct anyhow, just in case
2059 col = col % width2;
2060 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002061 if (row >= curwin->w_height)
2062 {
2063 if (curwin->w_skipcol == 0)
2064 {
2065 curwin->w_skipcol += width1;
2066 --row;
2067 }
2068 if (row >= curwin->w_height)
2069 curwin->w_skipcol += (row - curwin->w_height) * width2;
2070 redraw_later(UPD_NOT_VALID);
2071 }
2072}
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074#ifdef FEAT_DIFF
2075/*
2076 * Don't end up with too many filler lines in the window.
2077 */
2078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002079check_topfill(
2080 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002081 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 int n;
2084
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002085 if (wp->w_topfill <= 0)
2086 return;
2087
2088 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2089 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002091 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002093 --wp->w_topline;
2094 wp->w_topfill = 0;
2095 }
2096 else
2097 {
2098 wp->w_topfill = wp->w_height - n;
2099 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 }
2103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106/*
2107 * Scroll the screen one line down, but don't do it if it would move the
2108 * cursor off the screen.
2109 */
2110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 int end_row;
2114#ifdef FEAT_DIFF
2115 int can_fill = (curwin->w_topfill
2116 < diff_check_fill(curwin, curwin->w_topline));
2117#endif
2118
2119 if (curwin->w_topline <= 1
2120#ifdef FEAT_DIFF
2121 && !can_fill
2122#endif
2123 )
2124 return;
2125
Bram Moolenaar85a20022019-12-21 18:25:54 +01002126 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 /*
2129 * Compute the row number of the last row of the cursor line
2130 * and make sure it doesn't go off the screen. Make sure the cursor
2131 * doesn't go past 'scrolloff' lines from the screen end.
2132 */
2133 end_row = curwin->w_wrow;
2134#ifdef FEAT_DIFF
2135 if (can_fill)
2136 ++end_row;
2137 else
2138 end_row += plines_nofill(curwin->w_topline - 1);
2139#else
2140 end_row += plines(curwin->w_topline - 1);
2141#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002142 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 validate_cheight();
2145 validate_virtcol();
2146 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002147 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002149 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151#ifdef FEAT_DIFF
2152 if (can_fill)
2153 {
2154 ++curwin->w_topfill;
2155 check_topfill(curwin, TRUE);
2156 }
2157 else
2158 {
2159 --curwin->w_topline;
2160 curwin->w_topfill = 0;
2161 }
2162#else
2163 --curwin->w_topline;
2164#endif
2165#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002166 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002168 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2170 }
2171}
2172
2173/*
2174 * Scroll the screen one line up, but don't do it if it would move the cursor
2175 * off the screen.
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 int start_row;
2181
2182 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2183#ifdef FEAT_DIFF
2184 && curwin->w_topfill == 0
2185#endif
2186 )
2187 return;
2188
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 /*
2192 * Compute the row number of the first row of the cursor line
2193 * and make sure it doesn't go off the screen. Make sure the cursor
2194 * doesn't go before 'scrolloff' lines from the screen start.
2195 */
2196#ifdef FEAT_DIFF
2197 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2198 - curwin->w_topfill;
2199#else
2200 start_row = curwin->w_wrow - plines(curwin->w_topline);
2201#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002202 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002205 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002207 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209#ifdef FEAT_DIFF
2210 if (curwin->w_topfill > 0)
2211 --curwin->w_topfill;
2212 else
2213#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002214 {
2215#ifdef FEAT_FOLDING
2216 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002219 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2222 }
2223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225/*
2226 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2227 * a (wrapped) text line. Uses and sets "lp->fill".
2228 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002229 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
2231 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002232topline_back_winheight(
2233 lineoff_T *lp,
2234 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236#ifdef FEAT_DIFF
2237 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2238 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ++lp->fill;
2241 lp->height = 1;
2242 }
2243 else
2244#endif
2245 {
2246 --lp->lnum;
2247#ifdef FEAT_DIFF
2248 lp->fill = 0;
2249#endif
2250 if (lp->lnum < 1)
2251 lp->height = MAXCOL;
2252 else
2253#ifdef FEAT_FOLDING
2254 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002255 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 lp->height = 1;
2257 else
2258#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002259 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261}
2262
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002263 static void
2264topline_back(lineoff_T *lp)
2265{
2266 topline_back_winheight(lp, TRUE);
2267}
2268
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2272 * a (wrapped) text line. Uses and sets "lp->fill".
2273 * Returns the height of the added line in "lp->height".
2274 * Lines below the last one are incredibly high.
2275 */
2276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002277botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279#ifdef FEAT_DIFF
2280 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 ++lp->fill;
2284 lp->height = 1;
2285 }
2286 else
2287#endif
2288 {
2289 ++lp->lnum;
2290#ifdef FEAT_DIFF
2291 lp->fill = 0;
2292#endif
2293 if (lp->lnum > curbuf->b_ml.ml_line_count)
2294 lp->height = MAXCOL;
2295 else
2296#ifdef FEAT_FOLDING
2297 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002298 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 lp->height = 1;
2300 else
2301#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002302 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304}
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Recompute topline to put the cursor at the top of the window.
2308 * Scroll at least "min_scroll" lines.
2309 * If "always" is TRUE, always set topline (for "zt").
2310 */
2311 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002312scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 int scrolled = 0;
2315 int extra = 0;
2316 int used;
2317 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 linenr_T top; // just above displayed lines
2319 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002321 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#ifdef FEAT_DIFF
2323 linenr_T old_topfill = curwin->w_topfill;
2324#endif
2325 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002326 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (mouse_dragging > 0)
2329 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 /*
2332 * Decrease topline until:
2333 * - it has become 1
2334 * - (part of) the cursor line is moved off the screen or
2335 * - moved at least 'scrolljump' lines and
2336 * - at least 'scrolloff' lines above and below the cursor
2337 */
2338 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (curwin->w_cursor.lnum < curwin->w_topline)
2341 scrolled = used;
2342
2343#ifdef FEAT_FOLDING
2344 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2345 {
2346 --top;
2347 ++bot;
2348 }
2349 else
2350#endif
2351 {
2352 top = curwin->w_cursor.lnum - 1;
2353 bot = curwin->w_cursor.lnum + 1;
2354 }
2355 new_topline = top + 1;
2356
2357#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002358 // "used" already contains the number of filler lines above, don't add it
2359 // again.
2360 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002361 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#endif
2363
2364 /*
2365 * Check if the lines from "top" to "bot" fit in the window. If they do,
2366 * set new_topline and advance "top" and "bot" to include more lines.
2367 */
2368 while (top > 0)
2369 {
2370#ifdef FEAT_FOLDING
2371 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002372 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 i = 1;
2374 else
2375#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002376 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002377 if (top < curwin->w_topline)
2378 scrolled += i;
2379
2380 // If scrolling is needed, scroll at least 'sj' lines.
2381 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2382 && extra >= off)
2383 break;
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 used += i;
2386 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2387 {
2388#ifdef FEAT_FOLDING
2389 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002390 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ++used;
2392 else
2393#endif
2394 used += plines(bot);
2395 }
2396 if (used > curwin->w_height)
2397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 extra += i;
2400 new_topline = top;
2401 --top;
2402 ++bot;
2403 }
2404
2405 /*
2406 * If we don't have enough space, put cursor in the middle.
2407 * This makes sure we get the same position when using "k" and "j"
2408 * in a small window.
2409 */
2410 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002411 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 else
2413 {
2414 /*
2415 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002416 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 */
2418 if (new_topline < curwin->w_topline || always)
2419 curwin->w_topline = new_topline;
2420 if (curwin->w_topline > curwin->w_cursor.lnum)
2421 curwin->w_topline = curwin->w_cursor.lnum;
2422#ifdef FEAT_DIFF
2423 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2424 if (curwin->w_topfill > 0 && extra > off)
2425 {
2426 curwin->w_topfill -= extra - off;
2427 if (curwin->w_topfill < 0)
2428 curwin->w_topfill = 0;
2429 }
2430 check_topfill(curwin, FALSE);
2431#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002432 if (curwin->w_topline != old_topline)
2433 reset_skipcol();
2434 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002435 {
2436 validate_virtcol();
2437 if (curwin->w_skipcol >= curwin->w_virtcol)
2438 // TODO: if the line doesn't fit may optimize w_skipcol instead
2439 // of making it zero
2440 reset_skipcol();
2441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002443 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#ifdef FEAT_DIFF
2445 || curwin->w_topfill != old_topfill
2446#endif
2447 )
2448 curwin->w_valid &=
2449 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2450 curwin->w_valid |= VALID_TOPLINE;
2451 }
2452}
2453
2454/*
2455 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2456 * screen lines for text lines.
2457 */
2458 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002459set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461#ifdef FEAT_DIFF
2462 wp->w_filler_rows = 0;
2463#endif
2464 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002465 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 else
2467 {
2468 wp->w_empty_rows = wp->w_height - used;
2469#ifdef FEAT_DIFF
2470 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2471 {
2472 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2473 if (wp->w_empty_rows > wp->w_filler_rows)
2474 wp->w_empty_rows -= wp->w_filler_rows;
2475 else
2476 {
2477 wp->w_filler_rows = wp->w_empty_rows;
2478 wp->w_empty_rows = 0;
2479 }
2480 }
2481#endif
2482 }
2483}
2484
2485/*
2486 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002487 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2489 * This is messy stuff!!!
2490 */
2491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002492scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 int used;
2495 int scrolled = 0;
2496 int extra = 0;
2497 int i;
2498 linenr_T line_count;
2499 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002500 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 lineoff_T loff;
2502 lineoff_T boff;
2503#ifdef FEAT_DIFF
2504 int old_topfill = curwin->w_topfill;
2505 int fill_below_window;
2506#endif
2507 linenr_T old_botline = curwin->w_botline;
2508 linenr_T old_valid = curwin->w_valid;
2509 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002511 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002512 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 cln = curwin->w_cursor.lnum;
2515 if (set_topbot)
2516 {
2517 used = 0;
2518 curwin->w_botline = cln + 1;
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002519 loff.lnum = cln + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520#ifdef FEAT_DIFF
2521 loff.fill = 0;
2522#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002523 while (TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002525 topline_back_winheight(&loff, FALSE);
2526 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002528 if (used + loff.height > curwin->w_height)
2529 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002530 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002531 {
2532 // 'smoothscroll' and 'wrap' are set. The above line is
2533 // too long to show in its entirety, so we show just a part
2534 // of it.
2535 if (used < curwin->w_height)
2536 {
2537 int plines_offset = used + loff.height
2538 - curwin->w_height;
2539 used = curwin->w_height;
2540#ifdef FEAT_DIFF
2541 curwin->w_topfill = loff.fill;
2542#endif
2543 curwin->w_topline = loff.lnum;
2544 curwin->w_skipcol = skipcol_from_plines(
2545 curwin, plines_offset);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002546 }
2547 }
2548 break;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#ifdef FEAT_DIFF
2551 curwin->w_topfill = loff.fill;
2552#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002553 curwin->w_topline = loff.lnum;
2554 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 set_empty_rows(curwin, used);
2558 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2559 if (curwin->w_topline != old_topline
2560#ifdef FEAT_DIFF
2561 || curwin->w_topfill != old_topfill
2562#endif
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002563 || curwin->w_skipcol != old_skipcol
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002564 || curwin->w_skipcol != 0)
2565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Luuk van Baalbd28cae2024-04-03 22:50:40 +02002567 if (curwin->w_skipcol != old_skipcol)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002568 redraw_later(UPD_NOT_VALID);
2569 else
2570 reset_skipcol();
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 else
2574 validate_botline();
2575
Bram Moolenaar85a20022019-12-21 18:25:54 +01002576 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#ifdef FEAT_DIFF
2578 used = plines_nofill(cln);
2579#else
2580 validate_cheight();
2581 used = curwin->w_cline_height;
2582#endif
2583
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002584 // If the cursor is on or below botline, we will at least scroll by the
2585 // height of the cursor line, which is "used". Correct for empty lines,
2586 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (cln >= curwin->w_botline)
2588 {
2589 scrolled = used;
2590 if (cln == curwin->w_botline)
2591 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002592 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002593 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002594 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002595 // Calculate how many screen lines the current top line of window
2596 // occupies. If it is occupying more than the entire window, we
2597 // need to scroll the additional clipped lines to scroll past the
2598 // top line before we can move on to the other lines.
2599 int top_plines =
2600#ifdef FEAT_DIFF
2601 plines_win_nofill
2602#else
2603 plines_win
2604#endif
2605 (curwin, curwin->w_topline, FALSE);
2606 int skip_lines = 0;
2607 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002608 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002609 {
fullwaywang8154e642023-06-24 21:58:09 +01002610 int width2 = width1 + curwin_col_off2();
2611 // similar formula is used in curs_columns()
2612 if (curwin->w_skipcol > width1)
2613 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2614 else if (curwin->w_skipcol > 0)
2615 skip_lines = 1;
2616
2617 top_plines -= skip_lines;
2618 if (top_plines > curwin->w_height)
2619 {
2620 scrolled += (top_plines - curwin->w_height);
2621 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002622 }
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 /*
2627 * Stop counting lines to scroll when
2628 * - hitting start of the file
2629 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002630 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * - lines between botline and cursor have been counted
2632 */
2633#ifdef FEAT_FOLDING
2634 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2635#endif
2636 {
2637 loff.lnum = cln;
2638 boff.lnum = cln;
2639 }
2640#ifdef FEAT_DIFF
2641 loff.fill = 0;
2642 boff.fill = 0;
2643 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2644 - curwin->w_filler_rows;
2645#endif
2646
2647 while (loff.lnum > 1)
2648 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2650 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002652 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2654 && loff.lnum <= curwin->w_botline
2655#ifdef FEAT_DIFF
2656 && (loff.lnum < curwin->w_botline
2657 || loff.fill >= fill_below_window)
2658#endif
2659 )
2660 break;
2661
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002664 if (loff.height == MAXCOL)
2665 used = MAXCOL;
2666 else
2667 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (used > curwin->w_height)
2669 break;
2670 if (loff.lnum >= curwin->w_botline
2671#ifdef FEAT_DIFF
2672 && (loff.lnum > curwin->w_botline
2673 || loff.fill <= fill_below_window)
2674#endif
2675 )
2676 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 scrolled += loff.height;
2679 if (loff.lnum == curwin->w_botline
2680#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002681 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#endif
2683 )
2684 scrolled -= curwin->w_empty_rows;
2685 }
2686
2687 if (boff.lnum < curbuf->b_ml.ml_line_count)
2688 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 botline_forw(&boff);
2691 used += boff.height;
2692 if (used > curwin->w_height)
2693 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002694 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2695 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 extra += boff.height;
2698 if (boff.lnum >= curwin->w_botline
2699#ifdef FEAT_DIFF
2700 || (boff.lnum + 1 == curwin->w_botline
2701 && boff.fill > curwin->w_filler_rows)
2702#endif
2703 )
2704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 scrolled += boff.height;
2707 if (boff.lnum == curwin->w_botline
2708#ifdef FEAT_DIFF
2709 && boff.fill == 0
2710#endif
2711 )
2712 scrolled -= curwin->w_empty_rows;
2713 }
2714 }
2715 }
2716 }
2717
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (scrolled <= 0)
2720 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 else if (used > curwin->w_height)
2723 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002724 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else
2726 {
2727 line_count = 0;
2728#ifdef FEAT_DIFF
2729 boff.fill = curwin->w_topfill;
2730#endif
2731 boff.lnum = curwin->w_topline - 1;
2732 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2733 {
2734 botline_forw(&boff);
2735 i += boff.height;
2736 ++line_count;
2737 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002738 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 line_count = 9999;
2740 }
2741
2742 /*
2743 * Scroll up if the cursor is off the bottom of the screen a bit.
2744 * Otherwise put it at 1/2 of the screen.
2745 */
2746 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002747 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002748 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002749 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002750 if (do_sms)
2751 scrollup(scrolled, TRUE); // TODO
2752 else
2753 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
2757 * If topline didn't change we need to restore w_botline and w_empty_rows
2758 * (we changed them).
2759 * If topline did change, update_screen() will set botline.
2760 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002761 if (curwin->w_topline == old_topline
2762 && curwin->w_skipcol == old_skipcol
2763 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
2765 curwin->w_botline = old_botline;
2766 curwin->w_empty_rows = old_empty_rows;
2767 curwin->w_valid = old_valid;
2768 }
2769 curwin->w_valid |= VALID_TOPLINE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02002770
2771 cursor_correct_sms();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
2774/*
2775 * Recompute topline to put the cursor halfway the window
2776 * If "atend" is TRUE, also put it halfway at the end of the file.
2777 */
2778 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002779scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 int above = 0;
2782 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002783 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#ifdef FEAT_DIFF
2785 int topfill = 0;
2786#endif
2787 int below = 0;
2788 int used;
2789 lineoff_T loff;
2790 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002791#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002792 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002795#ifdef FEAT_PROP_POPUP
2796 // if the width changed this needs to be updated first
2797 may_update_popup_position();
2798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2800#ifdef FEAT_FOLDING
2801 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2802#endif
2803#ifdef FEAT_DIFF
2804 used = plines_nofill(loff.lnum);
2805 loff.fill = 0;
2806 boff.fill = 0;
2807#else
2808 used = plines(loff.lnum);
2809#endif
2810 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002811
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002812 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002813 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2814 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002815 {
2816 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002817 if (atend)
2818 {
2819 want_height = (curwin->w_height - used) / 2;
2820 used = 0;
2821 }
2822 else
2823 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824 }
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 while (topline > 1)
2827 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002828 // If using smoothscroll, we can precisely scroll to the
2829 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002830 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002831 {
2832 topline_back_winheight(&loff, FALSE);
2833 if (loff.height == MAXCOL)
2834 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002835 used += loff.height;
2836 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002837 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002838 botline_forw(&boff);
2839 used += boff.height;
2840 }
2841 if (used > want_height)
2842 {
2843 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002844 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002845 topline = loff.lnum;
2846#ifdef FEAT_DIFF
2847 topfill = loff.fill;
2848#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002849 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002850 }
2851 break;
2852 }
2853 topline = loff.lnum;
2854#ifdef FEAT_DIFF
2855 topfill = loff.fill;
2856#endif
2857 continue;
2858 }
2859
2860 // If not using smoothscroll, we have to iteratively find how many
2861 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002862 // This may not be right in the middle if the lines'
2863 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002864
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002865 // Depending on "prefer_above" we add a line above or below first.
2866 // Loop twice to avoid duplicating code.
2867 int done = FALSE;
2868 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002870 if (prefer_above ? (round == 2 && below < above)
2871 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002873 // add a line below the cursor
2874 if (boff.lnum < curbuf->b_ml.ml_line_count)
2875 {
2876 botline_forw(&boff);
2877 used += boff.height;
2878 if (used > curwin->w_height)
2879 {
2880 done = TRUE;
2881 break;
2882 }
2883 below += boff.height;
2884 }
2885 else
2886 {
2887 ++below; // count a "~" line
2888 if (atend)
2889 ++used;
2890 }
2891 }
2892
2893 if (prefer_above ? (round == 1 && below >= above)
2894 : (round == 1 && below > above))
2895 {
2896 // add a line above the cursor
2897 topline_back(&loff);
2898 if (loff.height == MAXCOL)
2899 used = MAXCOL;
2900 else
2901 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002903 {
2904 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002906 }
2907 above += loff.height;
2908 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002910 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002914 if (done)
2915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#ifdef FEAT_FOLDING
2919 if (!hasFolding(topline, &curwin->w_topline, NULL))
2920#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002921 {
2922 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002923 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002924 || curwin->w_skipcol != 0)
2925 {
2926 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002927 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002928 {
2929 curwin->w_skipcol = skipcol;
2930 redraw_later(UPD_NOT_VALID);
2931 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002932 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002933 reset_skipcol();
2934 }
2935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936#ifdef FEAT_DIFF
2937 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002938 if (old_topline > curwin->w_topline + curwin->w_height)
2939 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 check_topfill(curwin, FALSE);
2941#endif
2942 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2943 curwin->w_valid |= VALID_TOPLINE;
2944}
2945
2946/*
2947 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002948 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 * If not possible, put it at the same position as scroll_cursor_halfway().
2950 * When called topline must be valid!
2951 */
2952 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002953cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 linenr_T botline;
2959 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002962 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 /*
2965 * How many lines we would like to have above/below the cursor depends on
2966 * whether the first/last line of the file is on screen.
2967 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002968 above_wanted = so;
2969 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002970 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 above_wanted = mouse_dragging - 1;
2973 below_wanted = mouse_dragging - 1;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (curwin->w_topline == 1)
2976 {
2977 above_wanted = 0;
2978 max_off = curwin->w_height / 2;
2979 if (below_wanted > max_off)
2980 below_wanted = max_off;
2981 }
2982 validate_botline();
2983 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002984 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 below_wanted = 0;
2987 max_off = (curwin->w_height - 1) / 2;
2988 if (above_wanted > max_off)
2989 above_wanted = max_off;
2990 }
2991
2992 /*
2993 * If there are sufficient file-lines above and below the cursor, we can
2994 * return now.
2995 */
2996 cln = curwin->w_cursor.lnum;
2997 if (cln >= curwin->w_topline + above_wanted
2998 && cln < curwin->w_botline - below_wanted
2999#ifdef FEAT_FOLDING
3000 && !hasAnyFolding(curwin)
3001#endif
3002 )
3003 return;
3004
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003005 if (curwin->w_p_sms && !curwin->w_p_wrap)
3006 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003007 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003008 if (curwin->w_cline_height == curwin->w_height)
3009 {
3010 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003011 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003012 return;
3013 }
3014 // TODO: If the cursor line doesn't fit in the window then only adjust
3015 // w_skipcol.
3016 }
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 /*
3019 * Narrow down the area where the cursor can be put by taking lines from
3020 * the top and the bottom until:
3021 * - the desired context lines are found
3022 * - the lines from the top is past the lines from the bottom
3023 */
3024 topline = curwin->w_topline;
3025 botline = curwin->w_botline - 1;
3026#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003027 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 above = curwin->w_topfill;
3029 below = curwin->w_filler_rows;
3030#endif
3031 while ((above < above_wanted || below < below_wanted) && topline < botline)
3032 {
3033 if (below < below_wanted && (below <= above || above >= above_wanted))
3034 {
3035#ifdef FEAT_FOLDING
3036 if (hasFolding(botline, &botline, NULL))
3037 ++below;
3038 else
3039#endif
3040 below += plines(botline);
3041 --botline;
3042 }
3043 if (above < above_wanted && (above < below || below >= below_wanted))
3044 {
3045#ifdef FEAT_FOLDING
3046 if (hasFolding(topline, NULL, &topline))
3047 ++above;
3048 else
3049#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003050 above += PLINES_NOFILL(topline);
3051#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003052 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (topline < botline)
3054 above += diff_check_fill(curwin, topline + 1);
3055#endif
3056 ++topline;
3057 }
3058 }
3059 if (topline == botline || botline == 0)
3060 curwin->w_cursor.lnum = topline;
3061 else if (topline > botline)
3062 curwin->w_cursor.lnum = botline;
3063 else
3064 {
3065 if (cln < topline && curwin->w_topline > 1)
3066 {
3067 curwin->w_cursor.lnum = topline;
3068 curwin->w_valid &=
3069 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3070 }
3071 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3072 {
3073 curwin->w_cursor.lnum = botline;
3074 curwin->w_valid &=
3075 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3076 }
3077 }
3078 curwin->w_valid |= VALID_TOPLINE;
3079}
3080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003082 * Decide how much overlap to use for page-up or page-down scrolling.
3083 * This is symmetric, so that doing both keeps the same lines displayed.
3084 * Three lines are examined:
3085 *
3086 * before CTRL-F after CTRL-F / before CTRL-B
3087 * etc. l1
3088 * l1 last but one line ------------
3089 * l2 last text line l2 top text line
3090 * ------------- l3 second text line
3091 * l3 etc.
3092 */
3093static int get_scroll_overlap(int dir)
3094{
3095 lineoff_T loff;
3096 int min_height = curwin->w_height - 2;
3097
3098 validate_botline();
Luuk van Baalbd28cae2024-04-03 22:50:40 +02003099 if ((dir == BACKWARD && curwin->w_topline == 1)
3100 || (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count))
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003101 return min_height + 2; // no overlap, still handle 'smoothscroll'
3102
3103 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3104#ifdef FEAT_DIFF
3105 loff.fill = diff_check_fill(curwin, loff.lnum + dir == BACKWARD)
3106 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3107 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3108#else
3109 loff.height = plines(loff.lnum);
3110#endif
3111
3112 int h1 = loff.height;
3113 if (h1 > min_height)
3114 return min_height + 2; // no overlap
3115 if (dir == FORWARD)
3116 topline_back(&loff);
3117 else
3118 botline_forw(&loff);
3119
3120 int h2 = loff.height;
3121 if (h2 == MAXCOL || h2 + h1 > min_height)
3122 return min_height + 2; // no overlap
3123 if (dir == FORWARD)
3124 topline_back(&loff);
3125 else
3126 botline_forw(&loff);
3127
3128 int h3 = loff.height;
3129 if (h3 == MAXCOL || h3 + h2 > min_height)
3130 return min_height + 2; // no overlap
3131 if (dir == FORWARD)
3132 topline_back(&loff);
3133 else
3134 botline_forw(&loff);
3135
3136 int h4 = loff.height;
3137 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3138 return min_height + 1; // 1 line overlap
3139 else
3140 return min_height; // 2 lines overlap
3141}
3142
3143/*
Luuk van Baal9148ba82024-04-08 22:27:41 +02003144 * Scroll "count" lines with 'smoothscroll' in direction "dir". Return TRUE
3145 * when scrolling happened.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003146 */
Luuk van Baal9148ba82024-04-08 22:27:41 +02003147static int scroll_with_sms(int dir, long count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003148{
3149 int prev_sms = curwin->w_p_sms;
3150 colnr_T prev_skipcol = curwin->w_skipcol;
3151 linenr_T prev_topline = curwin->w_topline;
3152#ifdef FEAT_DIFF
3153 int prev_topfill = curwin->w_topfill;
3154#endif
3155
3156 curwin->w_p_sms = TRUE;
Luuk van Baal9148ba82024-04-08 22:27:41 +02003157 scroll_redraw(dir == FORWARD, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003158
3159 // Not actually smoothscrolling but ended up with partially visible line.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003160 // Continue scrolling until skipcol is zero.
Luuk van Baalcb204e62024-04-02 20:49:45 +02003161 if (!prev_sms && curwin->w_skipcol > 0)
3162 {
3163 int fixdir = dir;
3164 // Reverse the scroll direction when topline already changed. One line
3165 // extra for scrolling backward so that consuming skipcol is symmetric.
3166 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3167 fixdir = dir * -1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003168 while (curwin->w_skipcol > 0
3169 && curwin->w_topline < curbuf->b_ml.ml_line_count)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003170 scroll_redraw(fixdir == FORWARD, 1);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003171 }
3172 curwin->w_p_sms = prev_sms;
3173
3174 return curwin->w_topline == prev_topline
3175#ifdef FEAT_DIFF
3176 && curwin->w_topfill == prev_topfill
3177#endif
3178 && curwin->w_skipcol == prev_skipcol;
3179}
3180
3181/*
3182 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3183 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3184 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003186 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 */
3188 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003189pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003191 int nochange = TRUE;
3192 int buflen = curbuf->b_ml.ml_line_count;
3193 colnr_T prev_col = curwin->w_cursor.col;
Luuk van Baal78c51502024-04-09 21:30:19 +02003194 colnr_T prev_curswant = curwin->w_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003195 linenr_T prev_lnum = curwin->w_cursor.lnum;
3196 oparg_T oa = { 0 };
3197 cmdarg_T ca = { 0 };
3198 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003200 if (half)
3201 {
3202 // Scroll [count], 'scroll' or current window height lines.
3203 if (count)
3204 curwin->w_p_scr = MIN(curwin->w_height, count);
3205 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Luuk van Baal9148ba82024-04-08 22:27:41 +02003207 int curscount = count;
3208 // Adjust count so as to not reveal end of buffer lines.
3209 if (dir == FORWARD)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003210 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003211 int n = plines_correct_topline(curwin, curwin->w_topline, FALSE);
3212 if (n - count < curwin->w_height && curwin->w_topline < buflen)
Luuk van Baal08b0f632024-04-09 22:43:49 +02003213 n += plines_m_win(curwin, curwin->w_topline + 1, buflen, FALSE);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003214 if (n - count < curwin->w_height)
3215 count = n - curwin->w_height;
3216 }
3217
Luuk van Baal78c51502024-04-09 21:30:19 +02003218 // (Try to) scroll the window unless already at the end of the buffer.
Luuk van Baal9148ba82024-04-08 22:27:41 +02003219 if (count > 0)
3220 {
Luuk van Baal9148ba82024-04-08 22:27:41 +02003221 nochange = scroll_with_sms(dir, count);
Luuk van Baal78c51502024-04-09 21:30:19 +02003222 curwin->w_cursor.lnum = prev_lnum;
3223 curwin->w_cursor.col = prev_col;
3224 curwin->w_curswant = prev_curswant;
Luuk van Baalcb204e62024-04-02 20:49:45 +02003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
Luuk van Baal78c51502024-04-09 21:30:19 +02003227 // Move the cursor the same amount of screen lines.
3228 if (curwin->w_p_wrap)
3229 nv_screengo(&oa, dir, curscount);
3230 else if (dir == FORWARD)
3231 cursor_down_inner(curwin, curscount);
3232 else
3233 cursor_up_inner(curwin, curscount);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003234
Luuk van Baal78c51502024-04-09 21:30:19 +02003235 if (get_scrolloff_value() > 0)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003236 cursor_correct();
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003237#ifdef FEAT_FOLDING
3238 // Move cursor to first line of closed fold.
3239 foldAdjustCursor();
3240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
Luuk van Baalcb204e62024-04-02 20:49:45 +02003242 nochange = nochange
3243 && prev_col == curwin->w_cursor.col
3244 && prev_lnum == curwin->w_cursor.lnum;
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003245 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003246 else
3247 {
3248 // Scroll [count] times 'window' or current window height lines.
3249 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3250 MAX(1, p_window - 2) : get_scroll_overlap(dir));
Luuk van Baal9148ba82024-04-08 22:27:41 +02003251 nochange = scroll_with_sms(dir, count);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003252 }
3253
3254 // Error if both the viewport and cursor did not change.
3255 if (nochange)
3256 beep_flush();
3257 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003258 beginline(BL_SOL | BL_FIX);
Luuk van Baal9148ba82024-04-08 22:27:41 +02003259 else if (p_sol)
Luuk van Baalcb204e62024-04-02 20:49:45 +02003260 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003261
3262 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263}
3264
Bram Moolenaar860cae12010-06-05 23:22:07 +02003265 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003266do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003267{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003268 static win_T *prev_curwin = NULL;
3269 static pos_T prev_cursor = {0, 0, 0};
3270
3271 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3272 return;
3273 prev_curwin = curwin;
3274 prev_cursor = curwin->w_cursor;
3275
Bram Moolenaar860cae12010-06-05 23:22:07 +02003276 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003277 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003278 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003279 colnr_T curswant = curwin->w_curswant;
3280 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003281 win_T *old_curwin = curwin;
3282 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003283 int old_VIsual_select = VIsual_select;
3284 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003285
3286 /*
3287 * loop through the cursorbound windows
3288 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003289 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003290 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003291 {
3292 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003293 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003294 if (curwin != old_curwin && curwin->w_p_crb)
3295 {
3296# ifdef FEAT_DIFF
3297 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003298 curwin->w_cursor.lnum =
3299 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003300 else
3301# endif
3302 curwin->w_cursor.lnum = line;
3303 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003304 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003305 curwin->w_curswant = curswant;
3306 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003307
Bram Moolenaar85a20022019-12-21 18:25:54 +01003308 // Make sure the cursor is in a valid position. Temporarily set
3309 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003310 int restart_edit_save = restart_edit;
3311 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003313
3314 // Avoid a scroll here for the cursor position, 'scrollbind' is
3315 // more important.
3316 if (!curwin->w_p_scb)
3317 validate_cursor();
3318
Bram Moolenaar61452852011-02-01 18:01:11 +01003319 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003320 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003321 if (has_mbyte)
3322 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003323 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003324
Bram Moolenaar85a20022019-12-21 18:25:54 +01003325 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003326 if (!curwin->w_p_scb)
3327 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003328 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329 }
3330 }
3331
3332 /*
3333 * reset current-window
3334 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003335 VIsual_select = old_VIsual_select;
3336 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003337 curwin = old_curwin;
3338 curbuf = old_curbuf;
3339}