blob: 6bd9aa8469d6498b84bf5e98a4fdce55a87070b3 [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
62plines_correct_topline(win_T *wp, linenr_T lnum)
63{
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);
Bram Moolenaard5337ef2022-10-20 20:15:47 +010073 if (n > wp->w_height)
74 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 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100122 n = plines_correct_topline(wp, lnum);
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 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100924 wp->w_cline_row += plines_correct_topline(wp, lnum);
925 ++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/*
1605 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001608scrolldown(
1609 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001612 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 int wrow;
1614 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001615 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001616 int width1 = 0;
1617 int width2 = 0;
1618
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001619 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001620 {
1621 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001622 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
1625#ifdef FEAT_FOLDING
1626 linenr_T first;
1627
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1630#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001631 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001632 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001635 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1636 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 ++curwin->w_topfill;
1639 ++done;
1640 }
1641 else
1642#endif
1643 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001644 // break when at the very top
1645 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001646 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001648 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001650 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001651 if (curwin->w_skipcol >= width1 + width2)
1652 curwin->w_skipcol -= width2;
1653 else
1654 curwin->w_skipcol -= width1;
1655 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001659 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001660 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001661 --curwin->w_topline;
1662 curwin->w_skipcol = 0;
1663#ifdef FEAT_DIFF
1664 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001666#ifdef FEAT_FOLDING
1667 // A sequence of folded lines only counts for one logical line
1668 if (hasFolding(curwin->w_topline, &first, NULL))
1669 {
1670 ++done;
1671 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001672 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001673 curwin->w_botline -= curwin->w_topline - first;
1674 curwin->w_topline = first;
1675 }
1676 else
1677#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001678 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001679 {
1680 int size = win_linetabsize(curwin, curwin->w_topline,
1681 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1682 if (size > width1)
1683 {
1684 curwin->w_skipcol = width1;
1685 size -= width1;
1686 redraw_later(UPD_NOT_VALID);
1687 }
1688 while (size > width2)
1689 {
1690 curwin->w_skipcol += width2;
1691 size -= width2;
1692 }
1693 ++done;
1694 }
1695 else
1696 done += PLINES_NOFILL(curwin->w_topline);
1697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 invalidate_botline();
1701 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001702 curwin->w_wrow += done; // keep w_wrow updated
1703 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705#ifdef FEAT_DIFF
1706 if (curwin->w_cursor.lnum == curwin->w_topline)
1707 curwin->w_cline_row = 0;
1708 check_topfill(curwin, TRUE);
1709#endif
1710
1711 /*
1712 * Compute the row number of the last row of the cursor line
1713 * and move the cursor onto the displayed part of the window.
1714 */
1715 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001716 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 validate_virtcol();
1719 validate_cheight();
1720 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001721 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1724 {
1725#ifdef FEAT_FOLDING
1726 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1727 {
1728 --wrow;
1729 if (first == 1)
1730 curwin->w_cursor.lnum = 1;
1731 else
1732 curwin->w_cursor.lnum = first - 1;
1733 }
1734 else
1735#endif
1736 wrow -= plines(curwin->w_cursor.lnum--);
1737 curwin->w_valid &=
1738 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1739 moved = TRUE;
1740 }
1741 if (moved)
1742 {
1743#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001744 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 foldAdjustCursor();
1746#endif
1747 coladvance(curwin->w_curswant);
1748 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001749
1750 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1751 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001752 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001753 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1754
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001755 // make sure the cursor is in the visible text
1756 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001757 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001758 int row = 0;
1759 if (col >= width1)
1760 {
1761 col -= width1;
1762 ++row;
1763 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001764 if (col > width2 && width2 > 0)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001765 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001766 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001767 // even so col is not used anymore,
1768 // make sure it is correct, just in case
1769 col = col % width2;
1770 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001771 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001772 {
1773 curwin->w_curswant = curwin->w_virtcol
1774 - (row - curwin->w_height + 1) * width2;
1775 coladvance(curwin->w_curswant);
1776 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778}
1779
1780/*
1781 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784scrollup(
1785 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001786 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001788 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001790 if (do_sms
1791# ifdef FEAT_FOLDING
1792 || (byfold && hasAnyFolding(curwin))
1793# endif
1794# ifdef FEAT_DIFF
1795 || (curwin->w_p_diff && !curwin->w_p_wrap)
1796# endif
1797 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001799 int width1 = curwin->w_width - curwin_col_off();
1800 int width2 = width1 + curwin_col_off2();
1801 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001802 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001804 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001805 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001806
1807 // diff mode: first consume "topfill"
1808 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1809 // the line, then advance to the next line.
1810 // folding: count each sequence of folded lines as one logical line.
1811 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813# ifdef FEAT_DIFF
1814 if (curwin->w_topfill > 0)
1815 --curwin->w_topfill;
1816 else
1817# endif
1818 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001819 linenr_T lnum = curwin->w_topline;
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821# ifdef FEAT_FOLDING
1822 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001823 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 (void)hasFolding(lnum, NULL, &lnum);
1825# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001826 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001827 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001828 // 'smoothscroll': increase "w_skipcol" until it goes over
1829 // the end of the line, then advance to the next line.
1830 int add = curwin->w_skipcol > 0 ? width2 : width1;
1831 curwin->w_skipcol += add;
1832 if (curwin->w_skipcol >= size)
1833 {
1834 if (lnum == curbuf->b_ml.ml_line_count)
1835 {
1836 // at the last screen line, can't scroll further
1837 curwin->w_skipcol -= add;
1838 break;
1839 }
1840 ++lnum;
1841 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001843 else
1844 {
1845 if (lnum >= curbuf->b_ml.ml_line_count)
1846 break;
1847 ++lnum;
1848 }
1849
1850 if (lnum > curwin->w_topline)
1851 {
1852 // approximate w_botline
1853 curwin->w_botline += lnum - curwin->w_topline;
1854 curwin->w_topline = lnum;
1855# ifdef FEAT_DIFF
1856 curwin->w_topfill = diff_check_fill(curwin, lnum);
1857# endif
1858 curwin->w_skipcol = 0;
Luuk van Baalcb204e62024-04-02 20:49:45 +02001859 // Adjusting the cursor later should not adjust skipcol:
1860 // bring it to the first screenline on this new topline.
1861 curwin->w_curswant %= width1;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001862 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001863 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001864 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001865 }
1866 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001867
zeertzjqd9a92dc2023-06-05 18:41:35 +01001868 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1869 // need to redraw more, because wl_size of the (new) topline may
1870 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001871 redraw_later(UPD_NOT_VALID);
1872 }
1873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001876 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878
1879 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1880 curwin->w_topline = curbuf->b_ml.ml_line_count;
1881 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1882 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1883
1884#ifdef FEAT_DIFF
1885 check_topfill(curwin, FALSE);
1886#endif
1887
1888#ifdef FEAT_FOLDING
1889 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001890 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1892#endif
1893
1894 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1895 if (curwin->w_cursor.lnum < curwin->w_topline)
1896 {
1897 curwin->w_cursor.lnum = curwin->w_topline;
1898 curwin->w_valid &=
1899 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1900 coladvance(curwin->w_curswant);
1901 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001902 if (curwin->w_cursor.lnum == curwin->w_topline
1903 && do_sms && curwin->w_skipcol > 0)
1904 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001905 int col_off = curwin_col_off();
1906 int col_off2 = curwin_col_off2();
1907
1908 int width1 = curwin->w_width - col_off;
1909 int width2 = width1 + col_off2;
1910 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001911 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001912 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001913 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001914
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001915 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001916 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001917 int overlap = scrolloff_cols != 0 ? 0
1918 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001919
Bram Moolenaar118c2352022-10-09 17:19:27 +01001920 // Make sure the cursor is in a visible part of the line, taking
1921 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001922 // If there are not enough screen lines put the cursor in the middle.
1923 if (scrolloff_cols > space_cols / 2)
1924 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001925 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001926 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001927 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001928 colnr_T col = curwin->w_virtcol;
1929
1930 if (col < width1)
1931 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001932 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001933 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001934 curwin->w_curswant = col;
1935 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001936
1937 // validate_virtcol() marked various things as valid, but after
1938 // moving the cursor they need to be recomputed
1939 curwin->w_valid &=
1940 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001941 }
1942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943}
1944
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001945/*
1946 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1947 * valid for 'smoothscroll'.
1948 */
1949 void
1950adjust_skipcol(void)
1951{
1952 if (!curwin->w_p_wrap
1953 || !curwin->w_p_sms
1954 || curwin->w_cursor.lnum != curwin->w_topline)
1955 return;
1956
1957 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001958 if (width1 <= 0)
1959 return; // no text will be displayed
1960
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001961 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001962 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001963 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1964 int scrolled = FALSE;
1965
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001966 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001967 if (curwin->w_cline_height == curwin->w_height
1968 // w_cline_height may be capped at w_height, check there aren't
1969 // actually more lines.
1970 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1971 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001972 {
1973 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001974 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001975 return;
1976 }
1977
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001978 validate_virtcol();
Luuk van Baalcb204e62024-04-02 20:49:45 +02001979 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001980 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001981 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001982 {
1983 // scroll a screen line down
1984 if (curwin->w_skipcol >= width1 + width2)
1985 curwin->w_skipcol -= width2;
1986 else
1987 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001988 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001989 }
1990 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001991 {
1992 validate_virtcol();
1993 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001994 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001995 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001996
1997 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1998 int row = 0;
1999 if (col >= width1)
2000 {
2001 col -= width1;
2002 ++row;
2003 }
2004 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002005 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002006 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002007 // col may no longer be used, but make
2008 // sure it is correct anyhow, just in case
2009 col = col % width2;
2010 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002011 if (row >= curwin->w_height)
2012 {
2013 if (curwin->w_skipcol == 0)
2014 {
2015 curwin->w_skipcol += width1;
2016 --row;
2017 }
2018 if (row >= curwin->w_height)
2019 curwin->w_skipcol += (row - curwin->w_height) * width2;
2020 redraw_later(UPD_NOT_VALID);
2021 }
2022}
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#ifdef FEAT_DIFF
2025/*
2026 * Don't end up with too many filler lines in the window.
2027 */
2028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002029check_topfill(
2030 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002031 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 int n;
2034
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002035 if (wp->w_topfill <= 0)
2036 return;
2037
2038 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2039 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002041 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002043 --wp->w_topline;
2044 wp->w_topfill = 0;
2045 }
2046 else
2047 {
2048 wp->w_topfill = wp->w_height - n;
2049 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 }
2053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#endif
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056/*
2057 * Scroll the screen one line down, but don't do it if it would move the
2058 * cursor off the screen.
2059 */
2060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002061scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 int end_row;
2064#ifdef FEAT_DIFF
2065 int can_fill = (curwin->w_topfill
2066 < diff_check_fill(curwin, curwin->w_topline));
2067#endif
2068
2069 if (curwin->w_topline <= 1
2070#ifdef FEAT_DIFF
2071 && !can_fill
2072#endif
2073 )
2074 return;
2075
Bram Moolenaar85a20022019-12-21 18:25:54 +01002076 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Compute the row number of the last row of the cursor line
2080 * and make sure it doesn't go off the screen. Make sure the cursor
2081 * doesn't go past 'scrolloff' lines from the screen end.
2082 */
2083 end_row = curwin->w_wrow;
2084#ifdef FEAT_DIFF
2085 if (can_fill)
2086 ++end_row;
2087 else
2088 end_row += plines_nofill(curwin->w_topline - 1);
2089#else
2090 end_row += plines(curwin->w_topline - 1);
2091#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002092 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 validate_cheight();
2095 validate_virtcol();
2096 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002097 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002099 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101#ifdef FEAT_DIFF
2102 if (can_fill)
2103 {
2104 ++curwin->w_topfill;
2105 check_topfill(curwin, TRUE);
2106 }
2107 else
2108 {
2109 --curwin->w_topline;
2110 curwin->w_topfill = 0;
2111 }
2112#else
2113 --curwin->w_topline;
2114#endif
2115#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002116 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2120 }
2121}
2122
2123/*
2124 * Scroll the screen one line up, but don't do it if it would move the cursor
2125 * off the screen.
2126 */
2127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 int start_row;
2131
2132 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2133#ifdef FEAT_DIFF
2134 && curwin->w_topfill == 0
2135#endif
2136 )
2137 return;
2138
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
2142 * Compute the row number of the first row of the cursor line
2143 * and make sure it doesn't go off the screen. Make sure the cursor
2144 * doesn't go before 'scrolloff' lines from the screen start.
2145 */
2146#ifdef FEAT_DIFF
2147 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2148 - curwin->w_topfill;
2149#else
2150 start_row = curwin->w_wrow - plines(curwin->w_topline);
2151#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002152 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002155 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002157 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159#ifdef FEAT_DIFF
2160 if (curwin->w_topfill > 0)
2161 --curwin->w_topfill;
2162 else
2163#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002164 {
2165#ifdef FEAT_FOLDING
2166 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002169 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002170 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2172 }
2173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175/*
2176 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2177 * a (wrapped) text line. Uses and sets "lp->fill".
2178 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002179 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002182topline_back_winheight(
2183 lineoff_T *lp,
2184 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186#ifdef FEAT_DIFF
2187 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2188 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ++lp->fill;
2191 lp->height = 1;
2192 }
2193 else
2194#endif
2195 {
2196 --lp->lnum;
2197#ifdef FEAT_DIFF
2198 lp->fill = 0;
2199#endif
2200 if (lp->lnum < 1)
2201 lp->height = MAXCOL;
2202 else
2203#ifdef FEAT_FOLDING
2204 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002205 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 lp->height = 1;
2207 else
2208#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002209 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211}
2212
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002213 static void
2214topline_back(lineoff_T *lp)
2215{
2216 topline_back_winheight(lp, TRUE);
2217}
2218
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220/*
2221 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2222 * a (wrapped) text line. Uses and sets "lp->fill".
2223 * Returns the height of the added line in "lp->height".
2224 * Lines below the last one are incredibly high.
2225 */
2226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002227botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229#ifdef FEAT_DIFF
2230 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002232 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ++lp->fill;
2234 lp->height = 1;
2235 }
2236 else
2237#endif
2238 {
2239 ++lp->lnum;
2240#ifdef FEAT_DIFF
2241 lp->fill = 0;
2242#endif
2243 if (lp->lnum > curbuf->b_ml.ml_line_count)
2244 lp->height = MAXCOL;
2245 else
2246#ifdef FEAT_FOLDING
2247 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002248 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 lp->height = 1;
2250 else
2251#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002252 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254}
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256/*
2257 * Recompute topline to put the cursor at the top of the window.
2258 * Scroll at least "min_scroll" lines.
2259 * If "always" is TRUE, always set topline (for "zt").
2260 */
2261 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002262scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 int scrolled = 0;
2265 int extra = 0;
2266 int used;
2267 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002268 linenr_T top; // just above displayed lines
2269 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002271 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#ifdef FEAT_DIFF
2273 linenr_T old_topfill = curwin->w_topfill;
2274#endif
2275 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002276 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (mouse_dragging > 0)
2279 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281 /*
2282 * Decrease topline until:
2283 * - it has become 1
2284 * - (part of) the cursor line is moved off the screen or
2285 * - moved at least 'scrolljump' lines and
2286 * - at least 'scrolloff' lines above and below the cursor
2287 */
2288 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002289 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (curwin->w_cursor.lnum < curwin->w_topline)
2291 scrolled = used;
2292
2293#ifdef FEAT_FOLDING
2294 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2295 {
2296 --top;
2297 ++bot;
2298 }
2299 else
2300#endif
2301 {
2302 top = curwin->w_cursor.lnum - 1;
2303 bot = curwin->w_cursor.lnum + 1;
2304 }
2305 new_topline = top + 1;
2306
2307#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 // "used" already contains the number of filler lines above, don't add it
2309 // again.
2310 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002311 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#endif
2313
2314 /*
2315 * Check if the lines from "top" to "bot" fit in the window. If they do,
2316 * set new_topline and advance "top" and "bot" to include more lines.
2317 */
2318 while (top > 0)
2319 {
2320#ifdef FEAT_FOLDING
2321 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002322 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 i = 1;
2324 else
2325#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002326 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002327 if (top < curwin->w_topline)
2328 scrolled += i;
2329
2330 // If scrolling is needed, scroll at least 'sj' lines.
2331 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2332 && extra >= off)
2333 break;
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 used += i;
2336 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2337 {
2338#ifdef FEAT_FOLDING
2339 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ++used;
2342 else
2343#endif
2344 used += plines(bot);
2345 }
2346 if (used > curwin->w_height)
2347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
2349 extra += i;
2350 new_topline = top;
2351 --top;
2352 ++bot;
2353 }
2354
2355 /*
2356 * If we don't have enough space, put cursor in the middle.
2357 * This makes sure we get the same position when using "k" and "j"
2358 * in a small window.
2359 */
2360 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002361 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else
2363 {
2364 /*
2365 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002366 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
2368 if (new_topline < curwin->w_topline || always)
2369 curwin->w_topline = new_topline;
2370 if (curwin->w_topline > curwin->w_cursor.lnum)
2371 curwin->w_topline = curwin->w_cursor.lnum;
2372#ifdef FEAT_DIFF
2373 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2374 if (curwin->w_topfill > 0 && extra > off)
2375 {
2376 curwin->w_topfill -= extra - off;
2377 if (curwin->w_topfill < 0)
2378 curwin->w_topfill = 0;
2379 }
2380 check_topfill(curwin, FALSE);
2381#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002382 if (curwin->w_topline != old_topline)
2383 reset_skipcol();
2384 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002385 {
2386 validate_virtcol();
2387 if (curwin->w_skipcol >= curwin->w_virtcol)
2388 // TODO: if the line doesn't fit may optimize w_skipcol instead
2389 // of making it zero
2390 reset_skipcol();
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002393 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#ifdef FEAT_DIFF
2395 || curwin->w_topfill != old_topfill
2396#endif
2397 )
2398 curwin->w_valid &=
2399 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2400 curwin->w_valid |= VALID_TOPLINE;
2401 }
2402}
2403
2404/*
2405 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2406 * screen lines for text lines.
2407 */
2408 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002409set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411#ifdef FEAT_DIFF
2412 wp->w_filler_rows = 0;
2413#endif
2414 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002415 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 else
2417 {
2418 wp->w_empty_rows = wp->w_height - used;
2419#ifdef FEAT_DIFF
2420 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2421 {
2422 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2423 if (wp->w_empty_rows > wp->w_filler_rows)
2424 wp->w_empty_rows -= wp->w_filler_rows;
2425 else
2426 {
2427 wp->w_filler_rows = wp->w_empty_rows;
2428 wp->w_empty_rows = 0;
2429 }
2430 }
2431#endif
2432 }
2433}
2434
2435/*
2436 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002437 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2439 * This is messy stuff!!!
2440 */
2441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 int used;
2445 int scrolled = 0;
2446 int extra = 0;
2447 int i;
2448 linenr_T line_count;
2449 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002450 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 lineoff_T loff;
2452 lineoff_T boff;
2453#ifdef FEAT_DIFF
2454 int old_topfill = curwin->w_topfill;
2455 int fill_below_window;
2456#endif
2457 linenr_T old_botline = curwin->w_botline;
2458 linenr_T old_valid = curwin->w_valid;
2459 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002460 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002461 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002462 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 cln = curwin->w_cursor.lnum;
2465 if (set_topbot)
2466 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002467 int set_skipcol = FALSE;
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 used = 0;
2470 curwin->w_botline = cln + 1;
2471#ifdef FEAT_DIFF
2472 loff.fill = 0;
2473#endif
2474 for (curwin->w_topline = curwin->w_botline;
2475 curwin->w_topline > 1;
2476 curwin->w_topline = loff.lnum)
2477 {
2478 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002479 topline_back_winheight(&loff, FALSE);
2480 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002482 if (used + loff.height > curwin->w_height)
2483 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002484 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002485 {
2486 // 'smoothscroll' and 'wrap' are set. The above line is
2487 // too long to show in its entirety, so we show just a part
2488 // of it.
2489 if (used < curwin->w_height)
2490 {
2491 int plines_offset = used + loff.height
2492 - curwin->w_height;
Luuk van Baalcb204e62024-04-02 20:49:45 +02002493 int overlap = sms_marker_overlap(curwin, -1);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002494 used = curwin->w_height;
2495#ifdef FEAT_DIFF
2496 curwin->w_topfill = loff.fill;
2497#endif
2498 curwin->w_topline = loff.lnum;
2499 curwin->w_skipcol = skipcol_from_plines(
2500 curwin, plines_offset);
Luuk van Baalcb204e62024-04-02 20:49:45 +02002501 curwin->w_cursor.col = curwin->w_skipcol + overlap;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 set_skipcol = TRUE;
2503 }
2504 }
2505 break;
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 used += loff.height;
2508#ifdef FEAT_DIFF
2509 curwin->w_topfill = loff.fill;
2510#endif
2511 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02002512 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2513 curwin->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 set_empty_rows(curwin, used);
2515 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2516 if (curwin->w_topline != old_topline
2517#ifdef FEAT_DIFF
2518 || curwin->w_topfill != old_topfill
2519#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002520 || set_skipcol
2521 || curwin->w_skipcol != 0)
2522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002524 if (set_skipcol)
2525 redraw_later(UPD_NOT_VALID);
2526 else
2527 reset_skipcol();
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530 else
2531 validate_botline();
2532
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#ifdef FEAT_DIFF
2535 used = plines_nofill(cln);
2536#else
2537 validate_cheight();
2538 used = curwin->w_cline_height;
2539#endif
2540
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002541 // If the cursor is on or below botline, we will at least scroll by the
2542 // height of the cursor line, which is "used". Correct for empty lines,
2543 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (cln >= curwin->w_botline)
2545 {
2546 scrolled = used;
2547 if (cln == curwin->w_botline)
2548 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002549 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002550 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002551 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002552 // Calculate how many screen lines the current top line of window
2553 // occupies. If it is occupying more than the entire window, we
2554 // need to scroll the additional clipped lines to scroll past the
2555 // top line before we can move on to the other lines.
2556 int top_plines =
2557#ifdef FEAT_DIFF
2558 plines_win_nofill
2559#else
2560 plines_win
2561#endif
2562 (curwin, curwin->w_topline, FALSE);
2563 int skip_lines = 0;
2564 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002565 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002566 {
fullwaywang8154e642023-06-24 21:58:09 +01002567 int width2 = width1 + curwin_col_off2();
2568 // similar formula is used in curs_columns()
2569 if (curwin->w_skipcol > width1)
2570 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2571 else if (curwin->w_skipcol > 0)
2572 skip_lines = 1;
2573
2574 top_plines -= skip_lines;
2575 if (top_plines > curwin->w_height)
2576 {
2577 scrolled += (top_plines - curwin->w_height);
2578 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002579 }
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
2583 /*
2584 * Stop counting lines to scroll when
2585 * - hitting start of the file
2586 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002587 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 * - lines between botline and cursor have been counted
2589 */
2590#ifdef FEAT_FOLDING
2591 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2592#endif
2593 {
2594 loff.lnum = cln;
2595 boff.lnum = cln;
2596 }
2597#ifdef FEAT_DIFF
2598 loff.fill = 0;
2599 boff.fill = 0;
2600 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2601 - curwin->w_filler_rows;
2602#endif
2603
2604 while (loff.lnum > 1)
2605 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002606 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2607 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002609 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2611 && loff.lnum <= curwin->w_botline
2612#ifdef FEAT_DIFF
2613 && (loff.lnum < curwin->w_botline
2614 || loff.fill >= fill_below_window)
2615#endif
2616 )
2617 break;
2618
Bram Moolenaar85a20022019-12-21 18:25:54 +01002619 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002621 if (loff.height == MAXCOL)
2622 used = MAXCOL;
2623 else
2624 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if (used > curwin->w_height)
2626 break;
2627 if (loff.lnum >= curwin->w_botline
2628#ifdef FEAT_DIFF
2629 && (loff.lnum > curwin->w_botline
2630 || loff.fill <= fill_below_window)
2631#endif
2632 )
2633 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002634 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 scrolled += loff.height;
2636 if (loff.lnum == curwin->w_botline
2637#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002638 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639#endif
2640 )
2641 scrolled -= curwin->w_empty_rows;
2642 }
2643
2644 if (boff.lnum < curbuf->b_ml.ml_line_count)
2645 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002646 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 botline_forw(&boff);
2648 used += boff.height;
2649 if (used > curwin->w_height)
2650 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002651 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2652 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 extra += boff.height;
2655 if (boff.lnum >= curwin->w_botline
2656#ifdef FEAT_DIFF
2657 || (boff.lnum + 1 == curwin->w_botline
2658 && boff.fill > curwin->w_filler_rows)
2659#endif
2660 )
2661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 scrolled += boff.height;
2664 if (boff.lnum == curwin->w_botline
2665#ifdef FEAT_DIFF
2666 && boff.fill == 0
2667#endif
2668 )
2669 scrolled -= curwin->w_empty_rows;
2670 }
2671 }
2672 }
2673 }
2674
Bram Moolenaar85a20022019-12-21 18:25:54 +01002675 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (scrolled <= 0)
2677 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else if (used > curwin->w_height)
2680 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 else
2683 {
2684 line_count = 0;
2685#ifdef FEAT_DIFF
2686 boff.fill = curwin->w_topfill;
2687#endif
2688 boff.lnum = curwin->w_topline - 1;
2689 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2690 {
2691 botline_forw(&boff);
2692 i += boff.height;
2693 ++line_count;
2694 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002695 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 line_count = 9999;
2697 }
2698
2699 /*
2700 * Scroll up if the cursor is off the bottom of the screen a bit.
2701 * Otherwise put it at 1/2 of the screen.
2702 */
2703 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002704 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002705 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002706 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002707 if (do_sms)
2708 scrollup(scrolled, TRUE); // TODO
2709 else
2710 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 /*
2714 * If topline didn't change we need to restore w_botline and w_empty_rows
2715 * (we changed them).
2716 * If topline did change, update_screen() will set botline.
2717 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002718 if (curwin->w_topline == old_topline
2719 && curwin->w_skipcol == old_skipcol
2720 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 curwin->w_botline = old_botline;
2723 curwin->w_empty_rows = old_empty_rows;
2724 curwin->w_valid = old_valid;
2725 }
2726 curwin->w_valid |= VALID_TOPLINE;
2727}
2728
2729/*
2730 * Recompute topline to put the cursor halfway the window
2731 * If "atend" is TRUE, also put it halfway at the end of the file.
2732 */
2733 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002734scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 int above = 0;
2737 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002738 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#ifdef FEAT_DIFF
2740 int topfill = 0;
2741#endif
2742 int below = 0;
2743 int used;
2744 lineoff_T loff;
2745 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002746#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002747 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002750#ifdef FEAT_PROP_POPUP
2751 // if the width changed this needs to be updated first
2752 may_update_popup_position();
2753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2755#ifdef FEAT_FOLDING
2756 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2757#endif
2758#ifdef FEAT_DIFF
2759 used = plines_nofill(loff.lnum);
2760 loff.fill = 0;
2761 boff.fill = 0;
2762#else
2763 used = plines(loff.lnum);
2764#endif
2765 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002766
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002767 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002768 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2769 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002770 {
2771 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002772 if (atend)
2773 {
2774 want_height = (curwin->w_height - used) / 2;
2775 used = 0;
2776 }
2777 else
2778 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002779 }
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 while (topline > 1)
2782 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002783 // If using smoothscroll, we can precisely scroll to the
2784 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002785 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002786 {
2787 topline_back_winheight(&loff, FALSE);
2788 if (loff.height == MAXCOL)
2789 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002790 used += loff.height;
2791 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002792 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002793 botline_forw(&boff);
2794 used += boff.height;
2795 }
2796 if (used > want_height)
2797 {
2798 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002799 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002800 topline = loff.lnum;
2801#ifdef FEAT_DIFF
2802 topfill = loff.fill;
2803#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002804 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002805 }
2806 break;
2807 }
2808 topline = loff.lnum;
2809#ifdef FEAT_DIFF
2810 topfill = loff.fill;
2811#endif
2812 continue;
2813 }
2814
2815 // If not using smoothscroll, we have to iteratively find how many
2816 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002817 // This may not be right in the middle if the lines'
2818 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002819
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002820 // Depending on "prefer_above" we add a line above or below first.
2821 // Loop twice to avoid duplicating code.
2822 int done = FALSE;
2823 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002825 if (prefer_above ? (round == 2 && below < above)
2826 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002828 // add a line below the cursor
2829 if (boff.lnum < curbuf->b_ml.ml_line_count)
2830 {
2831 botline_forw(&boff);
2832 used += boff.height;
2833 if (used > curwin->w_height)
2834 {
2835 done = TRUE;
2836 break;
2837 }
2838 below += boff.height;
2839 }
2840 else
2841 {
2842 ++below; // count a "~" line
2843 if (atend)
2844 ++used;
2845 }
2846 }
2847
2848 if (prefer_above ? (round == 1 && below >= above)
2849 : (round == 1 && below > above))
2850 {
2851 // add a line above the cursor
2852 topline_back(&loff);
2853 if (loff.height == MAXCOL)
2854 used = MAXCOL;
2855 else
2856 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002858 {
2859 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002861 }
2862 above += loff.height;
2863 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002865 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002869 if (done)
2870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#ifdef FEAT_FOLDING
2874 if (!hasFolding(topline, &curwin->w_topline, NULL))
2875#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002876 {
2877 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002878 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002879 || curwin->w_skipcol != 0)
2880 {
2881 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002882 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002883 {
2884 curwin->w_skipcol = skipcol;
2885 redraw_later(UPD_NOT_VALID);
2886 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002887 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002888 reset_skipcol();
2889 }
2890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891#ifdef FEAT_DIFF
2892 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002893 if (old_topline > curwin->w_topline + curwin->w_height)
2894 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 check_topfill(curwin, FALSE);
2896#endif
2897 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2898 curwin->w_valid |= VALID_TOPLINE;
2899}
2900
2901/*
2902 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002903 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 * If not possible, put it at the same position as scroll_cursor_halfway().
2905 * When called topline must be valid!
2906 */
2907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002910 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 linenr_T botline;
2914 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002917 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 /*
2920 * How many lines we would like to have above/below the cursor depends on
2921 * whether the first/last line of the file is on screen.
2922 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002923 above_wanted = so;
2924 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002925 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 above_wanted = mouse_dragging - 1;
2928 below_wanted = mouse_dragging - 1;
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (curwin->w_topline == 1)
2931 {
2932 above_wanted = 0;
2933 max_off = curwin->w_height / 2;
2934 if (below_wanted > max_off)
2935 below_wanted = max_off;
2936 }
2937 validate_botline();
2938 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002939 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 below_wanted = 0;
2942 max_off = (curwin->w_height - 1) / 2;
2943 if (above_wanted > max_off)
2944 above_wanted = max_off;
2945 }
2946
2947 /*
2948 * If there are sufficient file-lines above and below the cursor, we can
2949 * return now.
2950 */
2951 cln = curwin->w_cursor.lnum;
2952 if (cln >= curwin->w_topline + above_wanted
2953 && cln < curwin->w_botline - below_wanted
2954#ifdef FEAT_FOLDING
2955 && !hasAnyFolding(curwin)
2956#endif
2957 )
2958 return;
2959
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002960 if (curwin->w_p_sms && !curwin->w_p_wrap)
2961 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002962 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002963 if (curwin->w_cline_height == curwin->w_height)
2964 {
2965 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002966 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002967 return;
2968 }
2969 // TODO: If the cursor line doesn't fit in the window then only adjust
2970 // w_skipcol.
2971 }
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 /*
2974 * Narrow down the area where the cursor can be put by taking lines from
2975 * the top and the bottom until:
2976 * - the desired context lines are found
2977 * - the lines from the top is past the lines from the bottom
2978 */
2979 topline = curwin->w_topline;
2980 botline = curwin->w_botline - 1;
2981#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002982 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 above = curwin->w_topfill;
2984 below = curwin->w_filler_rows;
2985#endif
2986 while ((above < above_wanted || below < below_wanted) && topline < botline)
2987 {
2988 if (below < below_wanted && (below <= above || above >= above_wanted))
2989 {
2990#ifdef FEAT_FOLDING
2991 if (hasFolding(botline, &botline, NULL))
2992 ++below;
2993 else
2994#endif
2995 below += plines(botline);
2996 --botline;
2997 }
2998 if (above < above_wanted && (above < below || below >= below_wanted))
2999 {
3000#ifdef FEAT_FOLDING
3001 if (hasFolding(topline, NULL, &topline))
3002 ++above;
3003 else
3004#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003005 above += PLINES_NOFILL(topline);
3006#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003007 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (topline < botline)
3009 above += diff_check_fill(curwin, topline + 1);
3010#endif
3011 ++topline;
3012 }
3013 }
3014 if (topline == botline || botline == 0)
3015 curwin->w_cursor.lnum = topline;
3016 else if (topline > botline)
3017 curwin->w_cursor.lnum = botline;
3018 else
3019 {
3020 if (cln < topline && curwin->w_topline > 1)
3021 {
3022 curwin->w_cursor.lnum = topline;
3023 curwin->w_valid &=
3024 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3025 }
3026 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3027 {
3028 curwin->w_cursor.lnum = botline;
3029 curwin->w_valid &=
3030 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3031 }
3032 }
3033 curwin->w_valid |= VALID_TOPLINE;
3034}
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003037 * Decide how much overlap to use for page-up or page-down scrolling.
3038 * This is symmetric, so that doing both keeps the same lines displayed.
3039 * Three lines are examined:
3040 *
3041 * before CTRL-F after CTRL-F / before CTRL-B
3042 * etc. l1
3043 * l1 last but one line ------------
3044 * l2 last text line l2 top text line
3045 * ------------- l3 second text line
3046 * l3 etc.
3047 */
3048static int get_scroll_overlap(int dir)
3049{
3050 lineoff_T loff;
3051 int min_height = curwin->w_height - 2;
3052
3053 validate_botline();
3054 if (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count)
3055 return min_height + 2; // no overlap, still handle 'smoothscroll'
3056
3057 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3058#ifdef FEAT_DIFF
3059 loff.fill = diff_check_fill(curwin, loff.lnum + dir == BACKWARD)
3060 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3061 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3062#else
3063 loff.height = plines(loff.lnum);
3064#endif
3065
3066 int h1 = loff.height;
3067 if (h1 > min_height)
3068 return min_height + 2; // no overlap
3069 if (dir == FORWARD)
3070 topline_back(&loff);
3071 else
3072 botline_forw(&loff);
3073
3074 int h2 = loff.height;
3075 if (h2 == MAXCOL || h2 + h1 > min_height)
3076 return min_height + 2; // no overlap
3077 if (dir == FORWARD)
3078 topline_back(&loff);
3079 else
3080 botline_forw(&loff);
3081
3082 int h3 = loff.height;
3083 if (h3 == MAXCOL || h3 + h2 > min_height)
3084 return min_height + 2; // no overlap
3085 if (dir == FORWARD)
3086 topline_back(&loff);
3087 else
3088 botline_forw(&loff);
3089
3090 int h4 = loff.height;
3091 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3092 return min_height + 1; // 1 line overlap
3093 else
3094 return min_height; // 2 lines overlap
3095}
3096
3097/*
Luuk van Baalcb204e62024-04-02 20:49:45 +02003098 * Scroll "count" lines with 'smoothscroll' in direction "dir". Adjust "count"
3099 * when scrolling more than "count" and return TRUE when scrolling happened.
3100 */
3101static int scroll_with_sms(int dir, long *count)
3102{
3103 int prev_sms = curwin->w_p_sms;
3104 colnr_T prev_skipcol = curwin->w_skipcol;
3105 linenr_T prev_topline = curwin->w_topline;
3106#ifdef FEAT_DIFF
3107 int prev_topfill = curwin->w_topfill;
3108#endif
3109
3110 curwin->w_p_sms = TRUE;
3111 scroll_redraw(dir == FORWARD, *count);
3112
3113 // Not actually smoothscrolling but ended up with partially visible line.
3114 // Continue scrolling and update "count" so that cursor can be moved
3115 // accordingly for half-page scrolling.
3116 if (!prev_sms && curwin->w_skipcol > 0)
3117 {
3118 int fixdir = dir;
3119 // Reverse the scroll direction when topline already changed. One line
3120 // extra for scrolling backward so that consuming skipcol is symmetric.
3121 if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD))
3122 fixdir = dir * -1;
3123 validate_cursor();
3124 while (curwin->w_skipcol > 0
3125 && curwin->w_topline < curbuf->b_ml.ml_line_count)
3126 {
3127 scroll_redraw(fixdir == FORWARD, 1);
3128 *count += (fixdir == dir ? 1 : -1);
3129 }
3130 }
3131 curwin->w_p_sms = prev_sms;
3132
3133 return curwin->w_topline == prev_topline
3134#ifdef FEAT_DIFF
3135 && curwin->w_topfill == prev_topfill
3136#endif
3137 && curwin->w_skipcol == prev_skipcol;
3138}
3139
3140/*
3141 * Move screen "count" (half) pages up ("dir" is BACKWARD) or down ("dir" is
3142 * FORWARD) and update the screen. Handle moving the cursor and not scrolling
3143 * to reveal end of buffer lines for half-page scrolling with CTRL-D and CTRL-U.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003145 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003148pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Luuk van Baalcb204e62024-04-02 20:49:45 +02003150 int nochange = TRUE;
3151 int buflen = curbuf->b_ml.ml_line_count;
3152 colnr_T prev_col = curwin->w_cursor.col;
3153 colnr_T prev_curswant = curwin->w_curswant;
3154 linenr_T prev_lnum = curwin->w_cursor.lnum;
3155 oparg_T oa = { 0 };
3156 cmdarg_T ca = { 0 };
3157 ca.oap = &oa;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003159 if (half)
3160 {
3161 // Scroll [count], 'scroll' or current window height lines.
3162 if (count)
3163 curwin->w_p_scr = MIN(curwin->w_height, count);
3164 count = MIN(curwin->w_height, curwin->w_p_scr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Luuk van Baalcb204e62024-04-02 20:49:45 +02003166 // Don't scroll if we already know that it will reveal end of buffer lines.
3167 if (dir == BACKWARD
3168 || (curwin->w_botline - 1 < buflen)
3169 || (curwin->w_p_sms && curwin->w_botline - 1 == buflen
3170 && curwin->w_skipcol < linetabsize(curwin, buflen)))
3171 {
3172 nochange = scroll_with_sms(dir, &count);
3173 validate_botline();
3174 // Hide any potentially revealed end of buffer lines.
3175 if (!nochange && curwin->w_botline - 1 == buflen)
3176 {
3177 curwin->w_cursor.lnum = buflen;
3178 scroll_cursor_bot(0, TRUE);
3179 }
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
Luuk van Baalcb204e62024-04-02 20:49:45 +02003182 // Move the cursor "count" screen lines.
3183 curwin->w_curswant = MAXCOL;
3184 curwin->w_cursor.col = prev_col;
3185 curwin->w_cursor.lnum = prev_lnum;
3186 if (curwin->w_p_wrap)
3187 nv_screengo(&oa, dir, count);
3188 else if (dir == FORWARD)
3189 cursor_down_inner(curwin, count);
3190 else
3191 cursor_up_inner(curwin, count);
3192 curwin->w_curswant = prev_curswant;
3193
3194 if (get_scrolloff_value())
3195 cursor_correct();
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003196#ifdef FEAT_FOLDING
3197 // Move cursor to first line of closed fold.
3198 foldAdjustCursor();
3199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
Luuk van Baalcb204e62024-04-02 20:49:45 +02003201 nochange = nochange
3202 && prev_col == curwin->w_cursor.col
3203 && prev_lnum == curwin->w_cursor.lnum;
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003204 }
Luuk van Baalcb204e62024-04-02 20:49:45 +02003205 else
3206 {
3207 // Scroll [count] times 'window' or current window height lines.
3208 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3209 MAX(1, p_window - 2) : get_scroll_overlap(dir));
3210 nochange = scroll_with_sms(dir, &count);
3211 }
3212
3213 // Error if both the viewport and cursor did not change.
3214 if (nochange)
3215 beep_flush();
3216 else if (!curwin->w_p_sms)
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003217 beginline(BL_SOL | BL_FIX);
Luuk van Baalcb204e62024-04-02 20:49:45 +02003218 else if (p_sol)
3219 nv_g_home_m_cmd(&ca);
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003220
3221 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222}
3223
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003225do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003226{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003227 static win_T *prev_curwin = NULL;
3228 static pos_T prev_cursor = {0, 0, 0};
3229
3230 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3231 return;
3232 prev_curwin = curwin;
3233 prev_cursor = curwin->w_cursor;
3234
Bram Moolenaar860cae12010-06-05 23:22:07 +02003235 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003236 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003237 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003238 colnr_T curswant = curwin->w_curswant;
3239 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003240 win_T *old_curwin = curwin;
3241 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003242 int old_VIsual_select = VIsual_select;
3243 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003244
3245 /*
3246 * loop through the cursorbound windows
3247 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003248 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003249 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003250 {
3251 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003252 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003253 if (curwin != old_curwin && curwin->w_p_crb)
3254 {
3255# ifdef FEAT_DIFF
3256 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003257 curwin->w_cursor.lnum =
3258 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003259 else
3260# endif
3261 curwin->w_cursor.lnum = line;
3262 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003263 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003264 curwin->w_curswant = curswant;
3265 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003266
Bram Moolenaar85a20022019-12-21 18:25:54 +01003267 // Make sure the cursor is in a valid position. Temporarily set
3268 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003269 int restart_edit_save = restart_edit;
3270 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003271 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003272
3273 // Avoid a scroll here for the cursor position, 'scrollbind' is
3274 // more important.
3275 if (!curwin->w_p_scb)
3276 validate_cursor();
3277
Bram Moolenaar61452852011-02-01 18:01:11 +01003278 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003279 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003280 if (has_mbyte)
3281 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003282 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003283
Bram Moolenaar85a20022019-12-21 18:25:54 +01003284 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003285 if (!curwin->w_p_scb)
3286 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003287 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003288 }
3289 }
3290
3291 /*
3292 * reset current-window
3293 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003294 VIsual_select = old_VIsual_select;
3295 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003296 curwin = old_curwin;
3297 curbuf = old_curbuf;
3298}