blob: 94dc7dc630f5361e37d581e2b00e716183e5ef53 [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
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
Bram Moolenaarf6196f42022-10-02 21:29:55 +010042adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
206 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 int
213sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000214{
215#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100216 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100218 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return 0;
220#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100221 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
222 if (wp->w_p_list && wp->w_lcs_chars.prec)
223 return 1;
224
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000225 return extra2 > 3 ? 0 : 3 - extra2;
226}
227
228/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000229 * Calculates the skipcol offset for window "wp" given how many
230 * physical lines we want to scroll down.
231 */
232 static int
233skipcol_from_plines(win_T *wp, int plines_off)
234{
235 int width1 = wp->w_width - win_col_off(wp);
236
237 int skipcol = 0;
238 if (plines_off > 0)
239 skipcol += width1;
240 if (plines_off > 1)
241 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
242 return skipcol;
243}
244
245/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100246 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247 */
248 static void
249reset_skipcol(void)
250{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000251 if (curwin->w_skipcol == 0)
252 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000253
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000254 curwin->w_skipcol = 0;
255
256 // Should use the least expensive way that displays all that changed.
257 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
258 // enough when the top line gets another screen line.
259 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000260}
261
262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * Update curwin->w_topline and redraw if necessary.
264 * Used to update the screen before printing a message.
265 */
266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100267update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 update_topline();
270 if (must_redraw)
271 update_screen(0);
272}
273
274/*
275 * Update curwin->w_topline to move the cursor onto the screen.
276 */
277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 long line_count;
281 int halfheight;
282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100316 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100347 else if (curwin->w_skipcol > 0
348 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 {
350 colnr_T vcol;
351
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000352 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100353 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100355 int overlap = sms_marker_overlap(curwin, curwin_col_off()
356 - curwin_col_off2());
357 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000399 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 else
401 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 check_botline = TRUE;
404 }
405 }
406
407 else
408 {
409#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100410 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
412#endif
413 check_botline = TRUE;
414 }
415 }
416
417 /*
418 * If the cursor is below the bottom of the window, scroll the window
419 * to put the cursor on the window.
420 * When w_botline is invalid, recompute it first, to avoid a redraw later.
421 * If w_botline was approximated, we might need a redraw later in a few
422 * cases, but we don't want to spend (a lot of) time recomputing w_botline
423 * for every small change.
424 */
425 if (check_botline)
426 {
427 if (!(curwin->w_valid & VALID_BOTLINE_AP))
428 validate_botline();
429
430 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
431 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 if (curwin->w_cursor.lnum < curwin->w_botline)
433 {
434 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100435 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_FOLDING
437 || hasAnyFolding(curwin)
438#endif
439 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lineoff_T loff;
442
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // Cursor is (a few lines) above botline, check if there are
444 // 'scrolloff' window lines below the cursor. If not, need to
445 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 n = curwin->w_empty_rows;
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
451#endif
452#ifdef FEAT_DIFF
453 loff.fill = 0;
454 n += curwin->w_filler_rows;
455#endif
456 loff.height = 0;
457 while (loff.lnum < curwin->w_botline
458#ifdef FEAT_DIFF
459 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
460#endif
461 )
462 {
463 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 botline_forw(&loff);
467 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100468 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 }
472 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000474 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 if (check_botline)
477 {
478#ifdef FEAT_FOLDING
479 if (hasAnyFolding(curwin))
480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // Count the number of logical lines between the cursor and
482 // botline - scrolloff (approximation of how much will be
483 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 line_count = 0;
485 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (lnum <= 0 || line_count > curwin->w_height + 1)
491 break;
492 (void)hasFolding(lnum, &lnum, NULL);
493 }
494 }
495 else
496#endif
497 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100498 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000502 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
504 }
505 }
506 curwin->w_valid |= VALID_TOPLINE;
507
508 /*
509 * Need to redraw when topline changed.
510 */
511 if (curwin->w_topline != old_topline
512#ifdef FEAT_DIFF
513 || curwin->w_topfill != old_topfill
514#endif
515 )
516 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100517 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000518 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100519
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100520 // When 'smoothscroll' is not set, should reset w_skipcol.
521 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100522 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100523 else if (curwin->w_skipcol != 0)
524 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000525
Bram Moolenaar85a20022019-12-21 18:25:54 +0100526 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (curwin->w_cursor.lnum == curwin->w_topline)
528 validate_cursor();
529 }
530
Bram Moolenaar375e3392019-01-31 18:26:10 +0100531 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
533
534/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000535 * Return the scrolljump value to use for the current window.
536 * When 'scrolljump' is positive use it as-is.
537 * When 'scrolljump' is negative use it as a percentage of the window height.
538 */
539 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000541{
542 if (p_sj >= 0)
543 return (int)p_sj;
544 return (curwin->w_height * -p_sj) / 100;
545}
546
547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
549 * current window.
550 */
551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 lineoff_T loff;
555 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100556 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar375e3392019-01-31 18:26:10 +0100558 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_FOLDING
560 || hasAnyFolding(curwin)
561#endif
562 )
563 {
564 loff.lnum = curwin->w_cursor.lnum;
565#ifdef FEAT_DIFF
566 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100567 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#else
569 n = 0;
570#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100571 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100572 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100575 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (loff.lnum < curwin->w_topline
577#ifdef FEAT_DIFF
578 || (loff.lnum == curwin->w_topline && loff.fill > 0)
579#endif
580 )
581 break;
582 n += loff.height;
583 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100584 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 return TRUE;
586 }
587 return FALSE;
588}
589
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100590/*
591 * Update w_curswant.
592 */
593 void
594update_curswant_force(void)
595{
596 validate_virtcol();
597 curwin->w_curswant = curwin->w_virtcol
598#ifdef FEAT_PROP_POPUP
599 - curwin->w_virtcol_first_char
600#endif
601 ;
602 curwin->w_set_curswant = FALSE;
603}
604
605/*
606 * Update w_curswant if w_set_curswant is set.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100612 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
615/*
616 * Check if the cursor has moved. Set the w_valid flag accordingly.
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
622 {
623 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100624 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
625 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 wp->w_valid_cursor = wp->w_cursor;
627 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100628 wp->w_valid_skipcol = wp->w_skipcol;
629 }
630 else if (wp->w_skipcol != wp->w_valid_skipcol)
631 {
632 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
633 |VALID_CHEIGHT|VALID_CROW
634 |VALID_BOTLINE|VALID_BOTLINE_AP);
635 wp->w_valid_cursor = wp->w_cursor;
636 wp->w_valid_leftcol = wp->w_leftcol;
637 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639 else if (wp->w_cursor.col != wp->w_valid_cursor.col
640 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100641 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
644 wp->w_valid_cursor.col = wp->w_cursor.col;
645 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648}
649
650/*
651 * Call this function when some window settings have changed, which require
652 * the cursor position, botline and topline to be recomputed and the window to
653 * be redrawn. E.g, when changing the 'wrap' option or folding.
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 changed_window_setting_win(curwin);
659}
660
661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 wp->w_lines_valid = 0;
665 changed_line_abv_curs_win(wp);
666 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100667 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668}
669
Dominique Pellee764d1b2023-03-12 21:20:59 +0000670#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000672 * Call changed_window_setting_win() for every window containing "buf".
673 */
674 void
675changed_window_setting_buf(buf_T *buf)
676{
677 tabpage_T *tp;
678 win_T *wp;
679
680 FOR_ALL_TAB_WINDOWS(tp, wp)
681 if (wp->w_buffer == buf)
682 changed_window_setting_win(wp);
683}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000684#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000685
686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * Set wp->w_topline to a certain number.
688 */
689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100690set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200692#ifdef FEAT_DIFF
693 linenr_T prev_topline = wp->w_topline;
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
699#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100700 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100702 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
703 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000705 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200707 if (lnum != prev_topline)
708 // Keep the filler lines when the topline didn't change.
709 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
711 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100713 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715
716/*
717 * Call this function when the length of the cursor line (in screen
718 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100719 * If the line length changed the number of screen lines might change,
720 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 * Need to take care of w_botline separately!
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaar85090142023-06-01 19:27:08 +0100726 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 |VALID_CHEIGHT|VALID_TOPLINE);
728}
729
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar85090142023-06-01 19:27:08 +0100733 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 |VALID_CHEIGHT|VALID_TOPLINE);
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Call this function when the length of a line (in screen characters) above
739 * the cursor have changed.
740 * Need to take care of w_botline separately!
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
746 |VALID_CHEIGHT|VALID_TOPLINE);
747}
748
749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
753 |VALID_CHEIGHT|VALID_TOPLINE);
754}
755
Dominique Pellee764d1b2023-03-12 21:20:59 +0000756#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100758 * Display of line has changed for "buf", invalidate cursor position and
759 * w_botline.
760 */
761 void
762changed_line_display_buf(buf_T *buf)
763{
764 win_T *wp;
765
766 FOR_ALL_WINDOWS(wp)
767 if (wp->w_buffer == buf)
768 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
769 |VALID_CROW|VALID_CHEIGHT
770 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
771}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000772#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100773
774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * Make sure the value of curwin->w_botline is valid.
776 */
777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100778validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100780 validate_botline_win(curwin);
781}
782
783/*
784 * Make sure the value of wp->w_botline is valid.
785 */
786 void
787validate_botline_win(win_T *wp)
788{
789 if (!(wp->w_valid & VALID_BOTLINE))
790 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * Mark curwin->w_botline as invalid (because of some change in the buffer).
795 */
796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100797invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
799 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
800}
801
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809approximate_botline_win(
810 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 wp->w_valid &= ~VALID_BOTLINE;
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
817 */
818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 check_cursor_moved(curwin);
822 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
823 (VALID_WROW|VALID_WCOL));
824}
825
826/*
827 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
828 * w_topline must be valid, you may need to call update_topline() first!
829 */
830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100831validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100833 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 check_cursor_moved(curwin);
835 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
836 curs_columns(TRUE);
837}
838
839#if defined(FEAT_GUI) || defined(PROTO)
840/*
841 * validate w_cline_row.
842 */
843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 /*
847 * First make sure that w_topline is valid (after moving the cursor).
848 */
849 update_topline();
850 check_cursor_moved(curwin);
851 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100852 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854#endif
855
856/*
857 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200858 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100861curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 linenr_T lnum;
864 int i;
865 int all_invalid;
866 int valid;
867#ifdef FEAT_FOLDING
868 long fold_count;
869#endif
870
Bram Moolenaar85a20022019-12-21 18:25:54 +0100871 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 all_invalid = (!redrawing()
873 || wp->w_lines_valid == 0
874 || wp->w_lines[0].wl_lnum > wp->w_topline);
875 i = 0;
876 wp->w_cline_row = 0;
877 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
878 {
879 valid = FALSE;
880 if (!all_invalid && i < wp->w_lines_valid)
881 {
882 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100883 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (wp->w_lines[i].wl_lnum == lnum)
885 {
886#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 // Check for newly inserted lines below this row, in which
888 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (!wp->w_buffer->b_mod_set
890 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
891 || wp->w_buffer->b_mod_top
892 > wp->w_lines[i].wl_lastlnum + 1)
893#endif
894 valid = TRUE;
895 }
896 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100897 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100900 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100902 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100904 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906#ifdef FEAT_FOLDING
907 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (lnum > wp->w_cursor.lnum)
910 break;
911#else
912 ++lnum;
913#endif
914 wp->w_cline_row += wp->w_lines[i].wl_size;
915 }
916 else
917 {
918#ifdef FEAT_FOLDING
919 fold_count = foldedCount(wp, lnum, NULL);
920 if (fold_count)
921 {
922 lnum += fold_count;
923 if (lnum > wp->w_cursor.lnum)
924 break;
925 ++wp->w_cline_row;
926 }
927 else
928#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100929 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100930 wp->w_cline_row += plines_correct_topline(wp, lnum);
931 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 }
935
936 check_cursor_moved(wp);
937 if (!(wp->w_valid & VALID_CHEIGHT))
938 {
939 if (all_invalid
940 || i == wp->w_lines_valid
941 || (i < wp->w_lines_valid
942 && (!wp->w_lines[i].wl_valid
943 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
944 {
945#ifdef FEAT_DIFF
946 if (wp->w_cursor.lnum == wp->w_topline)
947 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
948 TRUE) + wp->w_topfill;
949 else
950#endif
951 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
952#ifdef FEAT_FOLDING
953 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
954 NULL, NULL, TRUE, NULL);
955#endif
956 }
957 else if (i > wp->w_lines_valid)
958 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 wp->w_cline_height = 0;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
963 NULL, NULL, TRUE, NULL);
964#endif
965 }
966 else
967 {
968 wp->w_cline_height = wp->w_lines[i].wl_size;
969#ifdef FEAT_FOLDING
970 wp->w_cline_folded = wp->w_lines[i].wl_folded;
971#endif
972 }
973 }
974
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100975 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Validate curwin->w_virtcol only.
981 */
982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100983validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 validate_virtcol_win(curwin);
986}
987
988/*
989 * Validate wp->w_virtcol only.
990 */
991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100992validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000995
996 if (wp->w_valid & VALID_VIRTCOL)
997 return;
998
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100999#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001000 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001001#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001002 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001003#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001004 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001005#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001006 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/*
1010 * Validate curwin->w_cline_height only.
1011 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001012 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001013validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016
1017 if (curwin->w_valid & VALID_CHEIGHT)
1018 return;
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001021 if (curwin->w_cursor.lnum == curwin->w_topline)
1022 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1023 + curwin->w_topfill;
1024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001026 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001028 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001030 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001034 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 colnr_T off;
1040 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001041 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001045 if (curwin->w_valid & VALID_WCOL)
1046 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001047
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001048 col = curwin->w_virtcol;
1049 off = curwin_col_off();
1050 col += off;
1051 width = curwin->w_width - off + curwin_col_off2();
1052
1053 // long line wrapping, adjust curwin->w_wrow
1054 if (curwin->w_p_wrap
1055 && col >= (colnr_T)curwin->w_width
1056 && width > 0)
1057 // use same formula as what is used in curs_columns()
1058 col -= ((col - curwin->w_width) / width + 1) * width;
1059 if (col > (int)curwin->w_leftcol)
1060 col -= curwin->w_leftcol;
1061 else
1062 col = 0;
1063 curwin->w_wcol = col;
1064
1065 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001066#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001067 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001072 * Compute offset of a window, occupied by absolute or relative line number,
1073 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar64486672010-05-16 15:46:46 +02001078 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#ifdef FEAT_FOLDING
1081 + wp->w_p_fdc
1082#endif
1083#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001084 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#endif
1086 );
1087}
1088
1089 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 return win_col_off(curwin);
1093}
1094
1095/*
1096 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001097 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1098 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar64486672010-05-16 15:46:46 +02001103 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001104 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 return 0;
1106}
1107
1108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001109curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 return win_col_off2(curwin);
1112}
1113
1114/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001115 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * Also updates curwin->w_wrow and curwin->w_cline_row.
1117 * Also updates curwin->w_leftcol.
1118 */
1119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001120curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001121 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int off_left, off_right;
1126 int n;
1127 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001128 int width1; // text width for first screen line
1129 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int new_leftcol;
1131 colnr_T startcol;
1132 colnr_T endcol;
1133 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001134 long so = get_scrolloff_value();
1135 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001136 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 /*
1139 * First make sure that w_topline is valid (after moving the cursor).
1140 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001141 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 /*
1144 * Next make sure that w_cline_row is valid.
1145 */
1146 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001147 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001149#ifdef FEAT_PROP_POPUP
1150 // will be set by getvvcol() but not reset
1151 curwin->w_virtcol_first_char = 0;
1152#endif
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /*
1155 * Compute the number of virtual columns.
1156 */
1157#ifdef FEAT_FOLDING
1158 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1161 else
1162#endif
1163 getvvcol(curwin, &curwin->w_cursor,
1164 &startcol, &(curwin->w_virtcol), &endcol);
1165
Bram Moolenaar85a20022019-12-21 18:25:54 +01001166 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001168 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170 extra = curwin_col_off();
1171 curwin->w_wcol = curwin->w_virtcol + extra;
1172 endcol += extra;
1173
1174 /*
1175 * Now compute w_wrow, counting screen lines from w_cline_row.
1176 */
1177 curwin->w_wrow = curwin->w_cline_row;
1178
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001179 width1 = curwin->w_width - extra;
1180 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001183 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001184 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001185 if (curwin->w_p_wrap)
1186 curwin->w_wrow = curwin->w_height - 1;
1187 else
1188 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001190 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001194 // skip columns that are not visible
1195 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001197 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001198 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199 // Deduct by multiples of width2. This allows the long line
1200 // wrapping formula below to correctly calculate the w_wcol value
1201 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001202 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001203 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001204 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001205 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001206 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001207
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001208 did_sub_skipcol = TRUE;
1209 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001212 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001215 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1216 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 curwin->w_wrow += n;
1218
1219#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001220 // When cursor wraps to first char of next line in Insert
1221 // mode, the 'showbreak' string isn't shown, backup to first
1222 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001223 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001224 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001225 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 curwin->w_wcol = 0;
1227#endif
1228 }
1229 }
1230
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1232 // is not folded.
1233 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001234 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_FOLDING
1236 && !curwin->w_cline_folded
1237#endif
1238 )
1239 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001240#ifdef FEAT_PROP_POPUP
1241 if (curwin->w_virtcol_first_char > 0)
1242 {
1243 int cols = (curwin->w_width - extra);
1244 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1245
1246 // each "above" text prop shifts the text one row down
1247 curwin->w_wrow += rows;
1248 curwin->w_wcol -= rows * cols;
1249 endcol -= rows * cols;
1250 curwin->w_cline_height = rows + 1;
1251 }
1252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 /*
1254 * If Cursor is left of the screen, scroll rightwards.
1255 * If Cursor is right of the screen, scroll leftwards
1256 * If we get closer to the edge than 'sidescrolloff', scroll a little
1257 * extra
1258 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001259 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001260 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001261 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (off_left < 0 || off_right > 0)
1263 {
1264 if (off_left < 0)
1265 diff = -off_left;
1266 else
1267 diff = off_right;
1268
Bram Moolenaar85a20022019-12-21 18:25:54 +01001269 // When far off or not enough room on either side, put cursor in
1270 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001271 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1272 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 if (diff < p_ss)
1276 diff = p_ss;
1277 if (off_left < 0)
1278 new_leftcol = curwin->w_leftcol - diff;
1279 else
1280 new_leftcol = curwin->w_leftcol + diff;
1281 }
1282 if (new_leftcol < 0)
1283 new_leftcol = 0;
1284 if (new_leftcol != (int)curwin->w_leftcol)
1285 {
1286 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001288 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
1291 curwin->w_wcol -= curwin->w_leftcol;
1292 }
1293 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1294 curwin->w_wcol -= curwin->w_leftcol;
1295 else
1296 curwin->w_wcol = 0;
1297
1298#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001299 // Skip over filler lines. At the top use w_topfill, there
1300 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (curwin->w_cursor.lnum == curwin->w_topline)
1302 curwin->w_wrow += curwin->w_topfill;
1303 else
1304 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1305#endif
1306
1307 prev_skipcol = curwin->w_skipcol;
1308
1309 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if ((curwin->w_wrow >= curwin->w_height
1312 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001313 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && (p_lines =
1315#ifdef FEAT_DIFF
1316 plines_win_nofill
1317#else
1318 plines_win
1319#endif
1320 (curwin, curwin->w_cursor.lnum, FALSE))
1321 - 1 >= curwin->w_height))
1322 && curwin->w_height != 0
1323 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001324 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001325 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 // Cursor past end of screen. Happens with a single line that does
1328 // not fit on screen. Find a skipcol to show the text around the
1329 // cursor. Avoid scrolling all the time. compute value of "extra":
1330 // 1: Less than 'scrolloff' lines above
1331 // 2: Less than 'scrolloff' lines below
1332 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001334 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 // Compute last display line of the buffer line that we want at the
1337 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (p_lines == 0)
1339 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1340 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001341 if (p_lines > curwin->w_wrow + so)
1342 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001345 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 extra += 2;
1347
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001348 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (n > curwin->w_height / 2)
1353 n -= curwin->w_height / 2;
1354 else
1355 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001356 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (n > p_lines - curwin->w_height + 1)
1358 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001359 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else if (extra == 1)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1365 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (extra > 0)
1367 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1369 extra = curwin->w_skipcol / width2;
1370 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 }
1373 else if (extra == 2)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (endcol > curwin->w_skipcol)
1380 curwin->w_skipcol = endcol;
1381 }
1382
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001383 // adjust w_wrow for the changed w_skipcol
1384 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001386 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001387 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (curwin->w_wrow >= curwin->w_height)
1390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001393 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 curwin->w_wrow -= extra;
1395 }
1396
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001397 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (extra > 0)
1399 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1400 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001401 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001403 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_skipcol = 0;
1405 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001406 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001408#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001409 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001410#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001411#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1412 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1413 {
1414 curwin->w_wrow += popup_top_extra(curwin);
1415 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001416 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001417 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001418 else
1419 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001420#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001421
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001422 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1423 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001424 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001425 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1428}
1429
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001430#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001431/*
1432 * Compute the screen position of text character at "pos" in window "wp"
1433 * The resulting values are one-based, zero when character is not visible.
1434 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001435 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436textpos2screenpos(
1437 win_T *wp,
1438 pos_T *pos,
1439 int *rowp, // screen row
1440 int *scolp, // start screen column
1441 int *ccolp, // cursor screen column
1442 int *ecolp) // end screen column
1443{
1444 colnr_T scol = 0, ccol = 0, ecol = 0;
1445 int row = 0;
1446 int rowoff = 0;
1447 colnr_T coloff = 0;
1448
Bram Moolenaar189663b2021-07-21 18:04:56 +02001449 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 colnr_T col;
1452 int width;
1453 linenr_T lnum = pos->lnum;
1454#ifdef FEAT_FOLDING
1455 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1458#endif
1459 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001460
1461#ifdef FEAT_DIFF
1462 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001463 row += lnum == wp->w_topline ? wp->w_topfill
1464 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001465#endif
1466
zeertzjqba2d1912022-12-18 12:28:59 +00001467 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468#ifdef FEAT_FOLDING
1469 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001471 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001472 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001473 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001474 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001475#endif
1476 {
1477 getvcol(wp, pos, &scol, &ccol, &ecol);
1478
1479 // similar to what is done in validate_cursor_col()
1480 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001481 col += off;
1482 width = wp->w_width - off + win_col_off2(wp);
1483
zeertzjq55daae32023-06-04 19:29:22 +01001484 if (lnum == wp->w_topline)
zeertzjqf0e68c02023-06-03 17:11:47 +01001485 col -= wp->w_skipcol;
1486
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001487 // long line wrapping, adjust row
1488 if (wp->w_p_wrap
1489 && col >= (colnr_T)wp->w_width
1490 && width > 0)
1491 {
1492 // use same formula as what is used in curs_columns()
1493 rowoff = ((col - wp->w_width) / width + 1);
1494 col -= rowoff * width;
1495 }
1496 col -= wp->w_leftcol;
1497 if (col >= wp->w_width)
1498 col = -1;
1499 if (col >= 0 && row + rowoff <= wp->w_height)
1500 {
1501 coloff = col - scol + wp->w_wincol + 1;
1502 row += W_WINROW(wp);
1503 }
1504 else
1505 // character is left, right or below of the window
1506 row = rowoff = scol = ccol = ecol = 0;
1507 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001509 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510 *scolp = scol + coloff;
1511 *ccolp = ccol + coloff;
1512 *ecolp = ecol + coloff;
1513}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001514#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515
Bram Moolenaar12034e22019-08-25 22:25:02 +02001516#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001517/*
1518 * "screenpos({winid}, {lnum}, {col})" function
1519 */
1520 void
1521f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1522{
1523 dict_T *dict;
1524 win_T *wp;
1525 pos_T pos;
1526 int row = 0;
1527 int scol = 0, ccol = 0, ecol = 0;
1528
Bram Moolenaar93a10962022-06-16 11:42:09 +01001529 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530 return;
1531 dict = rettv->vval.v_dict;
1532
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001533 if (in_vim9script()
1534 && (check_for_number_arg(argvars, 0) == FAIL
1535 || check_for_number_arg(argvars, 1) == FAIL
1536 || check_for_number_arg(argvars, 2) == FAIL))
1537 return;
1538
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp == NULL)
1541 return;
1542
1543 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001544 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1545 {
1546 semsg(_(e_invalid_line_number_nr), pos.lnum);
1547 return;
1548 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001549 pos.col = tv_get_number(&argvars[2]) - 1;
1550 pos.coladd = 0;
1551 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1552
1553 dict_add_number(dict, "row", row);
1554 dict_add_number(dict, "col", scol);
1555 dict_add_number(dict, "curscol", ccol);
1556 dict_add_number(dict, "endcol", ecol);
1557}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001558
1559/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001560 * Convert a virtual (screen) column to a character column. The first column
1561 * is one. For a multibyte character, the column number of the first byte is
1562 * returned.
1563 */
1564 static int
1565virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1566{
1567 int offset = vcol2col(wp, lnum, vcol);
1568 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1569 char_u *p = line + offset;
1570
1571 // For a multibyte character, need to return the column number of the first
1572 // byte.
1573 MB_PTR_BACK(line, p);
1574
1575 return (int)(p - line + 1);
1576}
1577
1578/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001579 * "virtcol2col({winid}, {lnum}, {col})" function
1580 */
1581 void
1582f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1583{
1584 win_T *wp;
1585 linenr_T lnum;
1586 int screencol;
1587 int error = FALSE;
1588
1589 rettv->vval.v_number = -1;
1590
1591 if (check_for_number_arg(argvars, 0) == FAIL
1592 || check_for_number_arg(argvars, 1) == FAIL
1593 || check_for_number_arg(argvars, 2) == FAIL)
1594 return;
1595
1596 wp = find_win_by_nr_or_id(&argvars[0]);
1597 if (wp == NULL)
1598 return;
1599
1600 lnum = tv_get_number_chk(&argvars[1], &error);
1601 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1602 return;
1603
1604 screencol = tv_get_number_chk(&argvars[2], &error);
1605 if (error || screencol < 0)
1606 return;
1607
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001608 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001609}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001610#endif
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612/*
1613 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001616scrolldown(
1617 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001618 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001620 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 int wrow;
1622 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001623 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001624 int width1 = 0;
1625 int width2 = 0;
1626
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001627 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001628 {
1629 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001630 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633#ifdef FEAT_FOLDING
1634 linenr_T first;
1635
Bram Moolenaar85a20022019-12-21 18:25:54 +01001636 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1638#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001639 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001640 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001643 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1644 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 ++curwin->w_topfill;
1647 ++done;
1648 }
1649 else
1650#endif
1651 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001652 // break when at the very top
1653 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001654 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001656 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001658 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001659 if (curwin->w_skipcol >= width1 + width2)
1660 curwin->w_skipcol -= width2;
1661 else
1662 curwin->w_skipcol -= width1;
1663 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001667 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001668 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001669 --curwin->w_topline;
1670 curwin->w_skipcol = 0;
1671#ifdef FEAT_DIFF
1672 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001674#ifdef FEAT_FOLDING
1675 // A sequence of folded lines only counts for one logical line
1676 if (hasFolding(curwin->w_topline, &first, NULL))
1677 {
1678 ++done;
1679 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001680 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001681 curwin->w_botline -= curwin->w_topline - first;
1682 curwin->w_topline = first;
1683 }
1684 else
1685#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001686 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001687 {
1688 int size = win_linetabsize(curwin, curwin->w_topline,
1689 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1690 if (size > width1)
1691 {
1692 curwin->w_skipcol = width1;
1693 size -= width1;
1694 redraw_later(UPD_NOT_VALID);
1695 }
1696 while (size > width2)
1697 {
1698 curwin->w_skipcol += width2;
1699 size -= width2;
1700 }
1701 ++done;
1702 }
1703 else
1704 done += PLINES_NOFILL(curwin->w_topline);
1705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001707 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 invalidate_botline();
1709 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 curwin->w_wrow += done; // keep w_wrow updated
1711 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713#ifdef FEAT_DIFF
1714 if (curwin->w_cursor.lnum == curwin->w_topline)
1715 curwin->w_cline_row = 0;
1716 check_topfill(curwin, TRUE);
1717#endif
1718
1719 /*
1720 * Compute the row number of the last row of the cursor line
1721 * and move the cursor onto the displayed part of the window.
1722 */
1723 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001724 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
1726 validate_virtcol();
1727 validate_cheight();
1728 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001729 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
1731 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1732 {
1733#ifdef FEAT_FOLDING
1734 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1735 {
1736 --wrow;
1737 if (first == 1)
1738 curwin->w_cursor.lnum = 1;
1739 else
1740 curwin->w_cursor.lnum = first - 1;
1741 }
1742 else
1743#endif
1744 wrow -= plines(curwin->w_cursor.lnum--);
1745 curwin->w_valid &=
1746 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1747 moved = TRUE;
1748 }
1749 if (moved)
1750 {
1751#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001752 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 foldAdjustCursor();
1754#endif
1755 coladvance(curwin->w_curswant);
1756 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001757
1758 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1759 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001760 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001761 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1762
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001763 // make sure the cursor is in the visible text
1764 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001765 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001766 int row = 0;
1767 if (col >= width1)
1768 {
1769 col -= width1;
1770 ++row;
1771 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001772 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001773 {
1774 row += col / width2;
1775 col = col % width2;
1776 }
1777 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001778 {
1779 curwin->w_curswant = curwin->w_virtcol
1780 - (row - curwin->w_height + 1) * width2;
1781 coladvance(curwin->w_curswant);
1782 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784}
1785
1786/*
1787 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001790scrollup(
1791 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001794 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001796 if (do_sms
1797# ifdef FEAT_FOLDING
1798 || (byfold && hasAnyFolding(curwin))
1799# endif
1800# ifdef FEAT_DIFF
1801 || (curwin->w_p_diff && !curwin->w_p_wrap)
1802# endif
1803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001805 int width1 = curwin->w_width - curwin_col_off();
1806 int width2 = width1 + curwin_col_off2();
1807 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001808 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001809
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001810 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001811 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001812
1813 // diff mode: first consume "topfill"
1814 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1815 // the line, then advance to the next line.
1816 // folding: count each sequence of folded lines as one logical line.
1817 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
1819# ifdef FEAT_DIFF
1820 if (curwin->w_topfill > 0)
1821 --curwin->w_topfill;
1822 else
1823# endif
1824 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001825 linenr_T lnum = curwin->w_topline;
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827# ifdef FEAT_FOLDING
1828 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001829 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 (void)hasFolding(lnum, NULL, &lnum);
1831# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001832 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001833 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001834 // 'smoothscroll': increase "w_skipcol" until it goes over
1835 // the end of the line, then advance to the next line.
1836 int add = curwin->w_skipcol > 0 ? width2 : width1;
1837 curwin->w_skipcol += add;
1838 if (curwin->w_skipcol >= size)
1839 {
1840 if (lnum == curbuf->b_ml.ml_line_count)
1841 {
1842 // at the last screen line, can't scroll further
1843 curwin->w_skipcol -= add;
1844 break;
1845 }
1846 ++lnum;
1847 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001848 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001849 else
1850 {
1851 if (lnum >= curbuf->b_ml.ml_line_count)
1852 break;
1853 ++lnum;
1854 }
1855
1856 if (lnum > curwin->w_topline)
1857 {
1858 // approximate w_botline
1859 curwin->w_botline += lnum - curwin->w_topline;
1860 curwin->w_topline = lnum;
1861# ifdef FEAT_DIFF
1862 curwin->w_topfill = diff_check_fill(curwin, lnum);
1863# endif
1864 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001865 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001866 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001867 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001868 }
1869 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001870
zeertzjqd9a92dc2023-06-05 18:41:35 +01001871 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1872 // need to redraw more, because wl_size of the (new) topline may
1873 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001874 redraw_later(UPD_NOT_VALID);
1875 }
1876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001879 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881
1882 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1883 curwin->w_topline = curbuf->b_ml.ml_line_count;
1884 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1885 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1886
1887#ifdef FEAT_DIFF
1888 check_topfill(curwin, FALSE);
1889#endif
1890
1891#ifdef FEAT_FOLDING
1892 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001893 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1895#endif
1896
1897 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1898 if (curwin->w_cursor.lnum < curwin->w_topline)
1899 {
1900 curwin->w_cursor.lnum = curwin->w_topline;
1901 curwin->w_valid &=
1902 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1903 coladvance(curwin->w_curswant);
1904 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 if (curwin->w_cursor.lnum == curwin->w_topline
1906 && do_sms && curwin->w_skipcol > 0)
1907 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001908 int col_off = curwin_col_off();
1909 int col_off2 = curwin_col_off2();
1910
1911 int width1 = curwin->w_width - col_off;
1912 int width2 = width1 + col_off2;
1913 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001914 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001915 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001916 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001917
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001918 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001919 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001920 int overlap = scrolloff_cols != 0 ? 0
1921 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001922
Bram Moolenaar118c2352022-10-09 17:19:27 +01001923 // Make sure the cursor is in a visible part of the line, taking
1924 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001925 // If there are not enough screen lines put the cursor in the middle.
1926 if (scrolloff_cols > space_cols / 2)
1927 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001928 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001929 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001930 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001931 colnr_T col = curwin->w_virtcol;
1932
1933 if (col < width1)
1934 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001935 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001936 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001937 curwin->w_curswant = col;
1938 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001939
1940 // validate_virtcol() marked various things as valid, but after
1941 // moving the cursor they need to be recomputed
1942 curwin->w_valid &=
1943 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001944 }
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001948/*
1949 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1950 * valid for 'smoothscroll'.
1951 */
1952 void
1953adjust_skipcol(void)
1954{
1955 if (!curwin->w_p_wrap
1956 || !curwin->w_p_sms
1957 || curwin->w_cursor.lnum != curwin->w_topline)
1958 return;
1959
1960 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001961 if (width1 <= 0)
1962 return; // no text will be displayed
1963
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001964 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001965 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001966 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1967 int scrolled = FALSE;
1968
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001969 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001970 if (curwin->w_cline_height == curwin->w_height
1971 // w_cline_height may be capped at w_height, check there aren't
1972 // actually more lines.
1973 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1974 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001975 {
1976 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001977 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001978 return;
1979 }
1980
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001981 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001982 int overlap = sms_marker_overlap(curwin,
1983 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001984 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001985 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001986 {
1987 // scroll a screen line down
1988 if (curwin->w_skipcol >= width1 + width2)
1989 curwin->w_skipcol -= width2;
1990 else
1991 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001992 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001993 }
1994 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001995 {
1996 validate_virtcol();
1997 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001998 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001999 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002000
2001 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2002 int row = 0;
2003 if (col >= width1)
2004 {
2005 col -= width1;
2006 ++row;
2007 }
2008 if (col > width2)
2009 {
2010 row += col / width2;
2011 col = col % width2;
2012 }
2013 if (row >= curwin->w_height)
2014 {
2015 if (curwin->w_skipcol == 0)
2016 {
2017 curwin->w_skipcol += width1;
2018 --row;
2019 }
2020 if (row >= curwin->w_height)
2021 curwin->w_skipcol += (row - curwin->w_height) * width2;
2022 redraw_later(UPD_NOT_VALID);
2023 }
2024}
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026#ifdef FEAT_DIFF
2027/*
2028 * Don't end up with too many filler lines in the window.
2029 */
2030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031check_topfill(
2032 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002033 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 int n;
2036
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002037 if (wp->w_topfill <= 0)
2038 return;
2039
2040 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2041 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002043 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002045 --wp->w_topline;
2046 wp->w_topfill = 0;
2047 }
2048 else
2049 {
2050 wp->w_topfill = wp->w_height - n;
2051 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 }
2055}
2056
2057/*
2058 * Use as many filler lines as possible for w_topline. Make sure w_topline
2059 * is still visible.
2060 */
2061 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002062max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
2064 int n;
2065
2066 n = plines_nofill(curwin->w_topline);
2067 if (n >= curwin->w_height)
2068 curwin->w_topfill = 0;
2069 else
2070 {
2071 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2072 if (curwin->w_topfill + n > curwin->w_height)
2073 curwin->w_topfill = curwin->w_height - n;
2074 }
2075}
2076#endif
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078/*
2079 * Scroll the screen one line down, but don't do it if it would move the
2080 * cursor off the screen.
2081 */
2082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002083scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int end_row;
2086#ifdef FEAT_DIFF
2087 int can_fill = (curwin->w_topfill
2088 < diff_check_fill(curwin, curwin->w_topline));
2089#endif
2090
2091 if (curwin->w_topline <= 1
2092#ifdef FEAT_DIFF
2093 && !can_fill
2094#endif
2095 )
2096 return;
2097
Bram Moolenaar85a20022019-12-21 18:25:54 +01002098 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100 /*
2101 * Compute the row number of the last row of the cursor line
2102 * and make sure it doesn't go off the screen. Make sure the cursor
2103 * doesn't go past 'scrolloff' lines from the screen end.
2104 */
2105 end_row = curwin->w_wrow;
2106#ifdef FEAT_DIFF
2107 if (can_fill)
2108 ++end_row;
2109 else
2110 end_row += plines_nofill(curwin->w_topline - 1);
2111#else
2112 end_row += plines(curwin->w_topline - 1);
2113#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002114 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116 validate_cheight();
2117 validate_virtcol();
2118 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002119 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002121 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123#ifdef FEAT_DIFF
2124 if (can_fill)
2125 {
2126 ++curwin->w_topfill;
2127 check_topfill(curwin, TRUE);
2128 }
2129 else
2130 {
2131 --curwin->w_topline;
2132 curwin->w_topfill = 0;
2133 }
2134#else
2135 --curwin->w_topline;
2136#endif
2137#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002138 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002140 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2142 }
2143}
2144
2145/*
2146 * Scroll the screen one line up, but don't do it if it would move the cursor
2147 * off the screen.
2148 */
2149 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002150scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152 int start_row;
2153
2154 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2155#ifdef FEAT_DIFF
2156 && curwin->w_topfill == 0
2157#endif
2158 )
2159 return;
2160
Bram Moolenaar85a20022019-12-21 18:25:54 +01002161 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 /*
2164 * Compute the row number of the first row of the cursor line
2165 * and make sure it doesn't go off the screen. Make sure the cursor
2166 * doesn't go before 'scrolloff' lines from the screen start.
2167 */
2168#ifdef FEAT_DIFF
2169 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2170 - curwin->w_topfill;
2171#else
2172 start_row = curwin->w_wrow - plines(curwin->w_topline);
2173#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002174 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
2176 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002177 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002179 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181#ifdef FEAT_DIFF
2182 if (curwin->w_topfill > 0)
2183 --curwin->w_topfill;
2184 else
2185#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002186 {
2187#ifdef FEAT_FOLDING
2188 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002191 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2194 }
2195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197/*
2198 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2199 * a (wrapped) text line. Uses and sets "lp->fill".
2200 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002201 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
2203 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002204topline_back_winheight(
2205 lineoff_T *lp,
2206 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208#ifdef FEAT_DIFF
2209 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2210 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ++lp->fill;
2213 lp->height = 1;
2214 }
2215 else
2216#endif
2217 {
2218 --lp->lnum;
2219#ifdef FEAT_DIFF
2220 lp->fill = 0;
2221#endif
2222 if (lp->lnum < 1)
2223 lp->height = MAXCOL;
2224 else
2225#ifdef FEAT_FOLDING
2226 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 lp->height = 1;
2229 else
2230#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002231 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233}
2234
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002235 static void
2236topline_back(lineoff_T *lp)
2237{
2238 topline_back_winheight(lp, TRUE);
2239}
2240
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242/*
2243 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2244 * a (wrapped) text line. Uses and sets "lp->fill".
2245 * Returns the height of the added line in "lp->height".
2246 * Lines below the last one are incredibly high.
2247 */
2248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251#ifdef FEAT_DIFF
2252 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 ++lp->fill;
2256 lp->height = 1;
2257 }
2258 else
2259#endif
2260 {
2261 ++lp->lnum;
2262#ifdef FEAT_DIFF
2263 lp->fill = 0;
2264#endif
2265 if (lp->lnum > curbuf->b_ml.ml_line_count)
2266 lp->height = MAXCOL;
2267 else
2268#ifdef FEAT_FOLDING
2269 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 lp->height = 1;
2272 else
2273#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002274 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276}
2277
2278#ifdef FEAT_DIFF
2279/*
2280 * Switch from including filler lines below lp->lnum to including filler
2281 * lines above loff.lnum + 1. This keeps pointing to the same line.
2282 * When there are no filler lines nothing changes.
2283 */
2284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002285botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 if (lp->fill > 0)
2288 {
2289 ++lp->lnum;
2290 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2291 }
2292}
2293
2294/*
2295 * Switch from including filler lines above lp->lnum to including filler
2296 * lines below loff.lnum - 1. This keeps pointing to the same line.
2297 * When there are no filler lines nothing changes.
2298 */
2299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002300topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 if (lp->fill > 0)
2303 {
2304 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2305 --lp->lnum;
2306 }
2307}
2308#endif
2309
2310/*
2311 * Recompute topline to put the cursor at the top of the window.
2312 * Scroll at least "min_scroll" lines.
2313 * If "always" is TRUE, always set topline (for "zt").
2314 */
2315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 int scrolled = 0;
2319 int extra = 0;
2320 int used;
2321 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002322 linenr_T top; // just above displayed lines
2323 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002325 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifdef FEAT_DIFF
2327 linenr_T old_topfill = curwin->w_topfill;
2328#endif
2329 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002330 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (mouse_dragging > 0)
2333 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 /*
2336 * Decrease topline until:
2337 * - it has become 1
2338 * - (part of) the cursor line is moved off the screen or
2339 * - moved at least 'scrolljump' lines and
2340 * - at least 'scrolloff' lines above and below the cursor
2341 */
2342 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002343 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (curwin->w_cursor.lnum < curwin->w_topline)
2345 scrolled = used;
2346
2347#ifdef FEAT_FOLDING
2348 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2349 {
2350 --top;
2351 ++bot;
2352 }
2353 else
2354#endif
2355 {
2356 top = curwin->w_cursor.lnum - 1;
2357 bot = curwin->w_cursor.lnum + 1;
2358 }
2359 new_topline = top + 1;
2360
2361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002362 // "used" already contains the number of filler lines above, don't add it
2363 // again.
2364 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002365 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366#endif
2367
2368 /*
2369 * Check if the lines from "top" to "bot" fit in the window. If they do,
2370 * set new_topline and advance "top" and "bot" to include more lines.
2371 */
2372 while (top > 0)
2373 {
2374#ifdef FEAT_FOLDING
2375 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002376 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 i = 1;
2378 else
2379#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002380 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002381 if (top < curwin->w_topline)
2382 scrolled += i;
2383
2384 // If scrolling is needed, scroll at least 'sj' lines.
2385 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2386 && extra >= off)
2387 break;
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 used += i;
2390 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2391 {
2392#ifdef FEAT_FOLDING
2393 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002394 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 ++used;
2396 else
2397#endif
2398 used += plines(bot);
2399 }
2400 if (used > curwin->w_height)
2401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 extra += i;
2404 new_topline = top;
2405 --top;
2406 ++bot;
2407 }
2408
2409 /*
2410 * If we don't have enough space, put cursor in the middle.
2411 * This makes sure we get the same position when using "k" and "j"
2412 * in a small window.
2413 */
2414 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002415 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 else
2417 {
2418 /*
2419 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002420 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 */
2422 if (new_topline < curwin->w_topline || always)
2423 curwin->w_topline = new_topline;
2424 if (curwin->w_topline > curwin->w_cursor.lnum)
2425 curwin->w_topline = curwin->w_cursor.lnum;
2426#ifdef FEAT_DIFF
2427 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2428 if (curwin->w_topfill > 0 && extra > off)
2429 {
2430 curwin->w_topfill -= extra - off;
2431 if (curwin->w_topfill < 0)
2432 curwin->w_topfill = 0;
2433 }
2434 check_topfill(curwin, FALSE);
2435#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002436 if (curwin->w_topline == curwin->w_cursor.lnum)
2437 {
2438 validate_virtcol();
2439 if (curwin->w_skipcol >= curwin->w_virtcol)
2440 // TODO: if the line doesn't fit may optimize w_skipcol instead
2441 // of making it zero
2442 reset_skipcol();
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002445 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446#ifdef FEAT_DIFF
2447 || curwin->w_topfill != old_topfill
2448#endif
2449 )
2450 curwin->w_valid &=
2451 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2452 curwin->w_valid |= VALID_TOPLINE;
2453 }
2454}
2455
2456/*
2457 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2458 * screen lines for text lines.
2459 */
2460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463#ifdef FEAT_DIFF
2464 wp->w_filler_rows = 0;
2465#endif
2466 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002467 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 else
2469 {
2470 wp->w_empty_rows = wp->w_height - used;
2471#ifdef FEAT_DIFF
2472 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2473 {
2474 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2475 if (wp->w_empty_rows > wp->w_filler_rows)
2476 wp->w_empty_rows -= wp->w_filler_rows;
2477 else
2478 {
2479 wp->w_filler_rows = wp->w_empty_rows;
2480 wp->w_empty_rows = 0;
2481 }
2482 }
2483#endif
2484 }
2485}
2486
2487/*
2488 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002489 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2491 * This is messy stuff!!!
2492 */
2493 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002494scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 int used;
2497 int scrolled = 0;
2498 int extra = 0;
2499 int i;
2500 linenr_T line_count;
2501 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 lineoff_T loff;
2504 lineoff_T boff;
2505#ifdef FEAT_DIFF
2506 int old_topfill = curwin->w_topfill;
2507 int fill_below_window;
2508#endif
2509 linenr_T old_botline = curwin->w_botline;
2510 linenr_T old_valid = curwin->w_valid;
2511 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002512 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002513 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002514 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 cln = curwin->w_cursor.lnum;
2517 if (set_topbot)
2518 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002519 int set_skipcol = FALSE;
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 used = 0;
2522 curwin->w_botline = cln + 1;
2523#ifdef FEAT_DIFF
2524 loff.fill = 0;
2525#endif
2526 for (curwin->w_topline = curwin->w_botline;
2527 curwin->w_topline > 1;
2528 curwin->w_topline = loff.lnum)
2529 {
2530 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002531 topline_back_winheight(&loff, FALSE);
2532 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002534 if (used + loff.height > curwin->w_height)
2535 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002536 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002537 {
2538 // 'smoothscroll' and 'wrap' are set. The above line is
2539 // too long to show in its entirety, so we show just a part
2540 // of it.
2541 if (used < curwin->w_height)
2542 {
2543 int plines_offset = used + loff.height
2544 - curwin->w_height;
2545 used = curwin->w_height;
2546#ifdef FEAT_DIFF
2547 curwin->w_topfill = loff.fill;
2548#endif
2549 curwin->w_topline = loff.lnum;
2550 curwin->w_skipcol = skipcol_from_plines(
2551 curwin, plines_offset);
2552 set_skipcol = TRUE;
2553 }
2554 }
2555 break;
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 used += loff.height;
2558#ifdef FEAT_DIFF
2559 curwin->w_topfill = loff.fill;
2560#endif
2561 }
2562 set_empty_rows(curwin, used);
2563 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2564 if (curwin->w_topline != old_topline
2565#ifdef FEAT_DIFF
2566 || curwin->w_topfill != old_topfill
2567#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002568 || set_skipcol
2569 || curwin->w_skipcol != 0)
2570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002572 if (set_skipcol)
2573 redraw_later(UPD_NOT_VALID);
2574 else
2575 reset_skipcol();
2576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578 else
2579 validate_botline();
2580
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#ifdef FEAT_DIFF
2583 used = plines_nofill(cln);
2584#else
2585 validate_cheight();
2586 used = curwin->w_cline_height;
2587#endif
2588
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002589 // If the cursor is on or below botline, we will at least scroll by the
2590 // height of the cursor line, which is "used". Correct for empty lines,
2591 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (cln >= curwin->w_botline)
2593 {
2594 scrolled = used;
2595 if (cln == curwin->w_botline)
2596 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002597 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002598 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002599 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002600 // Calculate how many screen lines the current top line of window
2601 // occupies. If it is occupying more than the entire window, we
2602 // need to scroll the additional clipped lines to scroll past the
2603 // top line before we can move on to the other lines.
2604 int top_plines =
2605#ifdef FEAT_DIFF
2606 plines_win_nofill
2607#else
2608 plines_win
2609#endif
2610 (curwin, curwin->w_topline, FALSE);
2611 int skip_lines = 0;
2612 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002613 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002614 {
fullwaywang8154e642023-06-24 21:58:09 +01002615 int width2 = width1 + curwin_col_off2();
2616 // similar formula is used in curs_columns()
2617 if (curwin->w_skipcol > width1)
2618 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2619 else if (curwin->w_skipcol > 0)
2620 skip_lines = 1;
2621
2622 top_plines -= skip_lines;
2623 if (top_plines > curwin->w_height)
2624 {
2625 scrolled += (top_plines - curwin->w_height);
2626 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002627 }
2628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
2630
2631 /*
2632 * Stop counting lines to scroll when
2633 * - hitting start of the file
2634 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002635 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * - lines between botline and cursor have been counted
2637 */
2638#ifdef FEAT_FOLDING
2639 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2640#endif
2641 {
2642 loff.lnum = cln;
2643 boff.lnum = cln;
2644 }
2645#ifdef FEAT_DIFF
2646 loff.fill = 0;
2647 boff.fill = 0;
2648 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2649 - curwin->w_filler_rows;
2650#endif
2651
2652 while (loff.lnum > 1)
2653 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2655 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002657 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2659 && loff.lnum <= curwin->w_botline
2660#ifdef FEAT_DIFF
2661 && (loff.lnum < curwin->w_botline
2662 || loff.fill >= fill_below_window)
2663#endif
2664 )
2665 break;
2666
Bram Moolenaar85a20022019-12-21 18:25:54 +01002667 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002669 if (loff.height == MAXCOL)
2670 used = MAXCOL;
2671 else
2672 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (used > curwin->w_height)
2674 break;
2675 if (loff.lnum >= curwin->w_botline
2676#ifdef FEAT_DIFF
2677 && (loff.lnum > curwin->w_botline
2678 || loff.fill <= fill_below_window)
2679#endif
2680 )
2681 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 scrolled += loff.height;
2684 if (loff.lnum == curwin->w_botline
2685#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002686 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688 )
2689 scrolled -= curwin->w_empty_rows;
2690 }
2691
2692 if (boff.lnum < curbuf->b_ml.ml_line_count)
2693 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 botline_forw(&boff);
2696 used += boff.height;
2697 if (used > curwin->w_height)
2698 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002699 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2700 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
2702 extra += boff.height;
2703 if (boff.lnum >= curwin->w_botline
2704#ifdef FEAT_DIFF
2705 || (boff.lnum + 1 == curwin->w_botline
2706 && boff.fill > curwin->w_filler_rows)
2707#endif
2708 )
2709 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002710 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 scrolled += boff.height;
2712 if (boff.lnum == curwin->w_botline
2713#ifdef FEAT_DIFF
2714 && boff.fill == 0
2715#endif
2716 )
2717 scrolled -= curwin->w_empty_rows;
2718 }
2719 }
2720 }
2721 }
2722
Bram Moolenaar85a20022019-12-21 18:25:54 +01002723 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (scrolled <= 0)
2725 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002726 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 else if (used > curwin->w_height)
2728 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002729 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else
2731 {
2732 line_count = 0;
2733#ifdef FEAT_DIFF
2734 boff.fill = curwin->w_topfill;
2735#endif
2736 boff.lnum = curwin->w_topline - 1;
2737 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2738 {
2739 botline_forw(&boff);
2740 i += boff.height;
2741 ++line_count;
2742 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002743 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 line_count = 9999;
2745 }
2746
2747 /*
2748 * Scroll up if the cursor is off the bottom of the screen a bit.
2749 * Otherwise put it at 1/2 of the screen.
2750 */
2751 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002752 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002753 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002754 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002755 if (do_sms)
2756 scrollup(scrolled, TRUE); // TODO
2757 else
2758 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * If topline didn't change we need to restore w_botline and w_empty_rows
2763 * (we changed them).
2764 * If topline did change, update_screen() will set botline.
2765 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002766 if (curwin->w_topline == old_topline
2767 && curwin->w_skipcol == old_skipcol
2768 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
2770 curwin->w_botline = old_botline;
2771 curwin->w_empty_rows = old_empty_rows;
2772 curwin->w_valid = old_valid;
2773 }
2774 curwin->w_valid |= VALID_TOPLINE;
2775}
2776
2777/*
2778 * Recompute topline to put the cursor halfway the window
2779 * If "atend" is TRUE, also put it halfway at the end of the file.
2780 */
2781 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002782scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 int above = 0;
2785 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002786 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#ifdef FEAT_DIFF
2788 int topfill = 0;
2789#endif
2790 int below = 0;
2791 int used;
2792 lineoff_T loff;
2793 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002794#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002795 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002798#ifdef FEAT_PROP_POPUP
2799 // if the width changed this needs to be updated first
2800 may_update_popup_position();
2801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2803#ifdef FEAT_FOLDING
2804 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2805#endif
2806#ifdef FEAT_DIFF
2807 used = plines_nofill(loff.lnum);
2808 loff.fill = 0;
2809 boff.fill = 0;
2810#else
2811 used = plines(loff.lnum);
2812#endif
2813 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002814
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002815 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002816 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2817 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002818 {
2819 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002820 if (atend)
2821 {
2822 want_height = (curwin->w_height - used) / 2;
2823 used = 0;
2824 }
2825 else
2826 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002827 }
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 while (topline > 1)
2830 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002831 // If using smoothscroll, we can precisely scroll to the
2832 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002833 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002834 {
2835 topline_back_winheight(&loff, FALSE);
2836 if (loff.height == MAXCOL)
2837 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002838 used += loff.height;
2839 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002840 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002841 botline_forw(&boff);
2842 used += boff.height;
2843 }
2844 if (used > want_height)
2845 {
2846 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002847 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002848 topline = loff.lnum;
2849#ifdef FEAT_DIFF
2850 topfill = loff.fill;
2851#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002852 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002853 }
2854 break;
2855 }
2856 topline = loff.lnum;
2857#ifdef FEAT_DIFF
2858 topfill = loff.fill;
2859#endif
2860 continue;
2861 }
2862
2863 // If not using smoothscroll, we have to iteratively find how many
2864 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002865 // This may not be right in the middle if the lines'
2866 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002867
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002868 // Depending on "prefer_above" we add a line above or below first.
2869 // Loop twice to avoid duplicating code.
2870 int done = FALSE;
2871 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002873 if (prefer_above ? (round == 2 && below < above)
2874 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002876 // add a line below the cursor
2877 if (boff.lnum < curbuf->b_ml.ml_line_count)
2878 {
2879 botline_forw(&boff);
2880 used += boff.height;
2881 if (used > curwin->w_height)
2882 {
2883 done = TRUE;
2884 break;
2885 }
2886 below += boff.height;
2887 }
2888 else
2889 {
2890 ++below; // count a "~" line
2891 if (atend)
2892 ++used;
2893 }
2894 }
2895
2896 if (prefer_above ? (round == 1 && below >= above)
2897 : (round == 1 && below > above))
2898 {
2899 // add a line above the cursor
2900 topline_back(&loff);
2901 if (loff.height == MAXCOL)
2902 used = MAXCOL;
2903 else
2904 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002906 {
2907 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002909 }
2910 above += loff.height;
2911 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002913 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002917 if (done)
2918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#ifdef FEAT_FOLDING
2922 if (!hasFolding(topline, &curwin->w_topline, NULL))
2923#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002924 {
2925 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002926 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002927 || curwin->w_skipcol != 0)
2928 {
2929 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002930 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002931 {
2932 curwin->w_skipcol = skipcol;
2933 redraw_later(UPD_NOT_VALID);
2934 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002935 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002936 reset_skipcol();
2937 }
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939#ifdef FEAT_DIFF
2940 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002941 if (old_topline > curwin->w_topline + curwin->w_height)
2942 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 check_topfill(curwin, FALSE);
2944#endif
2945 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2946 curwin->w_valid |= VALID_TOPLINE;
2947}
2948
2949/*
2950 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002951 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 * If not possible, put it at the same position as scroll_cursor_halfway().
2953 * When called topline must be valid!
2954 */
2955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002956cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002958 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 linenr_T botline;
2962 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002965 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 /*
2968 * How many lines we would like to have above/below the cursor depends on
2969 * whether the first/last line of the file is on screen.
2970 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002971 above_wanted = so;
2972 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002973 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
2975 above_wanted = mouse_dragging - 1;
2976 below_wanted = mouse_dragging - 1;
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (curwin->w_topline == 1)
2979 {
2980 above_wanted = 0;
2981 max_off = curwin->w_height / 2;
2982 if (below_wanted > max_off)
2983 below_wanted = max_off;
2984 }
2985 validate_botline();
2986 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002987 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 below_wanted = 0;
2990 max_off = (curwin->w_height - 1) / 2;
2991 if (above_wanted > max_off)
2992 above_wanted = max_off;
2993 }
2994
2995 /*
2996 * If there are sufficient file-lines above and below the cursor, we can
2997 * return now.
2998 */
2999 cln = curwin->w_cursor.lnum;
3000 if (cln >= curwin->w_topline + above_wanted
3001 && cln < curwin->w_botline - below_wanted
3002#ifdef FEAT_FOLDING
3003 && !hasAnyFolding(curwin)
3004#endif
3005 )
3006 return;
3007
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003008 if (curwin->w_p_sms && !curwin->w_p_wrap)
3009 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003010 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003011 if (curwin->w_cline_height == curwin->w_height)
3012 {
3013 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003014 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003015 return;
3016 }
3017 // TODO: If the cursor line doesn't fit in the window then only adjust
3018 // w_skipcol.
3019 }
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 /*
3022 * Narrow down the area where the cursor can be put by taking lines from
3023 * the top and the bottom until:
3024 * - the desired context lines are found
3025 * - the lines from the top is past the lines from the bottom
3026 */
3027 topline = curwin->w_topline;
3028 botline = curwin->w_botline - 1;
3029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003030 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 above = curwin->w_topfill;
3032 below = curwin->w_filler_rows;
3033#endif
3034 while ((above < above_wanted || below < below_wanted) && topline < botline)
3035 {
3036 if (below < below_wanted && (below <= above || above >= above_wanted))
3037 {
3038#ifdef FEAT_FOLDING
3039 if (hasFolding(botline, &botline, NULL))
3040 ++below;
3041 else
3042#endif
3043 below += plines(botline);
3044 --botline;
3045 }
3046 if (above < above_wanted && (above < below || below >= below_wanted))
3047 {
3048#ifdef FEAT_FOLDING
3049 if (hasFolding(topline, NULL, &topline))
3050 ++above;
3051 else
3052#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003053 above += PLINES_NOFILL(topline);
3054#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003055 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (topline < botline)
3057 above += diff_check_fill(curwin, topline + 1);
3058#endif
3059 ++topline;
3060 }
3061 }
3062 if (topline == botline || botline == 0)
3063 curwin->w_cursor.lnum = topline;
3064 else if (topline > botline)
3065 curwin->w_cursor.lnum = botline;
3066 else
3067 {
3068 if (cln < topline && curwin->w_topline > 1)
3069 {
3070 curwin->w_cursor.lnum = topline;
3071 curwin->w_valid &=
3072 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3073 }
3074 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3075 {
3076 curwin->w_cursor.lnum = botline;
3077 curwin->w_valid &=
3078 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3079 }
3080 }
3081 curwin->w_valid |= VALID_TOPLINE;
3082}
3083
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003084static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003087 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3088 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003090 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 */
3092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003093onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 long n;
3096 int retval = OK;
3097 lineoff_T loff;
3098 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003099 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar85a20022019-12-21 18:25:54 +01003101 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 beep_flush();
3104 return FAIL;
3105 }
3106
3107 for ( ; count > 0; --count)
3108 {
3109 validate_botline();
3110 /*
3111 * It's an error to move a page up when the first line is already on
3112 * the screen. It's an error to move a page down when the last line
3113 * is on the screen and the topline is 'scrolloff' lines from the
3114 * last line.
3115 */
3116 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003117 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3119 : (curwin->w_topline == 1
3120#ifdef FEAT_DIFF
3121 && curwin->w_topfill ==
3122 diff_check_fill(curwin, curwin->w_topline)
3123#endif
3124 ))
3125 {
3126 beep_flush();
3127 retval = FAIL;
3128 break;
3129 }
3130
3131#ifdef FEAT_DIFF
3132 loff.fill = 0;
3133#endif
3134 if (dir == FORWARD)
3135 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003136 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003138 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003139 if (p_window <= 2)
3140 ++curwin->w_topline;
3141 else
3142 curwin->w_topline += p_window - 2;
3143 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3144 curwin->w_topline = curbuf->b_ml.ml_line_count;
3145 curwin->w_cursor.lnum = curwin->w_topline;
3146 }
3147 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3148 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003149 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 curwin->w_topline = curbuf->b_ml.ml_line_count;
3151#ifdef FEAT_DIFF
3152 curwin->w_topfill = 0;
3153#endif
3154 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3155 }
3156 else
3157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003158 // For the overlap, start with the line just below the window
3159 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 loff.lnum = curwin->w_botline;
3161#ifdef FEAT_DIFF
3162 loff.fill = diff_check_fill(curwin, loff.lnum)
3163 - curwin->w_filler_rows;
3164#endif
3165 get_scroll_overlap(&loff, -1);
3166 curwin->w_topline = loff.lnum;
3167#ifdef FEAT_DIFF
3168 curwin->w_topfill = loff.fill;
3169 check_topfill(curwin, FALSE);
3170#endif
3171 curwin->w_cursor.lnum = curwin->w_topline;
3172 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3173 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3174 }
3175 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003176 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
3178#ifdef FEAT_DIFF
3179 if (curwin->w_topline == 1)
3180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003181 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 max_topfill();
3183 continue;
3184 }
3185#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003186 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003187 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003188 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003189 if (p_window <= 2)
3190 --curwin->w_topline;
3191 else
3192 curwin->w_topline -= p_window - 2;
3193 if (curwin->w_topline < 1)
3194 curwin->w_topline = 1;
3195 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3196 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3197 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3198 continue;
3199 }
3200
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 // Find the line at the top of the window that is going to be the
3202 // line at the bottom of the window. Make sure this results in
3203 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 loff.lnum = curwin->w_topline - 1;
3205#ifdef FEAT_DIFF
3206 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3207 - curwin->w_topfill;
3208#endif
3209 get_scroll_overlap(&loff, 1);
3210
3211 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3212 {
3213 loff.lnum = curbuf->b_ml.ml_line_count;
3214#ifdef FEAT_DIFF
3215 loff.fill = 0;
3216 }
3217 else
3218 {
3219 botline_topline(&loff);
3220#endif
3221 }
3222 curwin->w_cursor.lnum = loff.lnum;
3223
Bram Moolenaar85a20022019-12-21 18:25:54 +01003224 // Find the line just above the new topline to get the right line
3225 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 n = 0;
3227 while (n <= curwin->w_height && loff.lnum >= 1)
3228 {
3229 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003230 if (loff.height == MAXCOL)
3231 n = MAXCOL;
3232 else
3233 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
3237 curwin->w_topline = 1;
3238#ifdef FEAT_DIFF
3239 max_topfill();
3240#endif
3241 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3242 }
3243 else
3244 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003245 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#ifdef FEAT_DIFF
3247 topline_botline(&loff);
3248#endif
3249 botline_forw(&loff);
3250 botline_forw(&loff);
3251#ifdef FEAT_DIFF
3252 botline_topline(&loff);
3253#endif
3254#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003255 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3257#endif
3258
Bram Moolenaar85a20022019-12-21 18:25:54 +01003259 // Always scroll at least one line. Avoid getting stuck on
3260 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (loff.lnum >= curwin->w_topline
3262#ifdef FEAT_DIFF
3263 && (loff.lnum > curwin->w_topline
3264 || loff.fill >= curwin->w_topfill)
3265#endif
3266 )
3267 {
3268#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269 // First try using the maximum number of filler lines. If
3270 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 loff.fill = curwin->w_topfill;
3272 if (curwin->w_topfill < diff_check_fill(curwin,
3273 curwin->w_topline))
3274 max_topfill();
3275 if (curwin->w_topfill == loff.fill)
3276#endif
3277 {
3278 --curwin->w_topline;
3279#ifdef FEAT_DIFF
3280 curwin->w_topfill = 0;
3281#endif
3282 }
3283 comp_botline(curwin);
3284 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003285 curwin->w_valid &=
3286 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 else
3289 {
3290 curwin->w_topline = loff.lnum;
3291#ifdef FEAT_DIFF
3292 curwin->w_topfill = loff.fill;
3293 check_topfill(curwin, FALSE);
3294#endif
3295 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3296 }
3297 }
3298 }
3299 }
3300#ifdef FEAT_FOLDING
3301 foldAdjustCursor();
3302#endif
3303 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003304 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003305 if (retval == OK)
3306 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3308
Bram Moolenaar907dad72018-07-10 15:07:15 +02003309 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003311 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3312 // But make sure we scroll at least one line (happens with mix of long
3313 // wrapping lines and non-wrapping line).
3314 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003316 scroll_cursor_top(1, FALSE);
3317 if (curwin->w_topline <= old_topline
3318 && old_topline < curbuf->b_ml.ml_line_count)
3319 {
3320 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003322 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3323#endif
3324 }
3325 }
3326#ifdef FEAT_FOLDING
3327 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003332 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 return retval;
3334}
3335
3336/*
3337 * Decide how much overlap to use for page-up or page-down scrolling.
3338 * This is symmetric, so that doing both keeps the same lines displayed.
3339 * Three lines are examined:
3340 *
3341 * before CTRL-F after CTRL-F / before CTRL-B
3342 * etc. l1
3343 * l1 last but one line ------------
3344 * l2 last text line l2 top text line
3345 * ------------- l3 second text line
3346 * l3 etc.
3347 */
3348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003349get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 int h1, h2, h3, h4;
3352 int min_height = curwin->w_height - 2;
3353 lineoff_T loff0, loff1, loff2;
3354
3355#ifdef FEAT_DIFF
3356 if (lp->fill > 0)
3357 lp->height = 1;
3358 else
3359 lp->height = plines_nofill(lp->lnum);
3360#else
3361 lp->height = plines(lp->lnum);
3362#endif
3363 h1 = lp->height;
3364 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 loff0 = *lp;
3368 if (dir > 0)
3369 botline_forw(lp);
3370 else
3371 topline_back(lp);
3372 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003373 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003375 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return;
3377 }
3378
3379 loff1 = *lp;
3380 if (dir > 0)
3381 botline_forw(lp);
3382 else
3383 topline_back(lp);
3384 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003385 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003387 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return;
3389 }
3390
3391 loff2 = *lp;
3392 if (dir > 0)
3393 botline_forw(lp);
3394 else
3395 topline_back(lp);
3396 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003397 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003398 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003400 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403/*
3404 * Scroll 'scroll' lines up or down.
3405 */
3406 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003407halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 long scrolled = 0;
3410 int i;
3411 int n;
3412 int room;
3413
3414 if (Prenum)
3415 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3416 curwin->w_height : Prenum;
3417 n = (curwin->w_p_scr <= curwin->w_height) ?
3418 curwin->w_p_scr : curwin->w_height;
3419
Bram Moolenaard5d37532017-03-27 23:02:07 +02003420 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 validate_botline();
3422 room = curwin->w_empty_rows;
3423#ifdef FEAT_DIFF
3424 room += curwin->w_filler_rows;
3425#endif
3426 if (flag)
3427 {
3428 /*
3429 * scroll the text up
3430 */
3431 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3432 {
3433#ifdef FEAT_DIFF
3434 if (curwin->w_topfill > 0)
3435 {
3436 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003437 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 --curwin->w_topfill;
3439 }
3440 else
3441#endif
3442 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003443 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 n -= i;
3445 if (n < 0 && scrolled > 0)
3446 break;
3447#ifdef FEAT_FOLDING
3448 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3449#endif
3450 ++curwin->w_topline;
3451#ifdef FEAT_DIFF
3452 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3453#endif
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3456 {
3457 ++curwin->w_cursor.lnum;
3458 curwin->w_valid &=
3459 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3463 scrolled += i;
3464
3465 /*
3466 * Correct w_botline for changed w_topline.
3467 * Won't work when there are filler lines.
3468 */
3469#ifdef FEAT_DIFF
3470 if (curwin->w_p_diff)
3471 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3472 else
3473#endif
3474 {
3475 room += i;
3476 do
3477 {
3478 i = plines(curwin->w_botline);
3479 if (i > room)
3480 break;
3481#ifdef FEAT_FOLDING
3482 (void)hasFolding(curwin->w_botline, NULL,
3483 &curwin->w_botline);
3484#endif
3485 ++curwin->w_botline;
3486 room -= i;
3487 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3488 }
3489 }
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /*
3492 * When hit bottom of the file: move cursor down.
3493 */
3494 if (n > 0)
3495 {
3496# ifdef FEAT_FOLDING
3497 if (hasAnyFolding(curwin))
3498 {
3499 while (--n >= 0
3500 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3501 {
3502 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3503 &curwin->w_cursor.lnum);
3504 ++curwin->w_cursor.lnum;
3505 }
3506 }
3507 else
3508# endif
3509 curwin->w_cursor.lnum += n;
3510 check_cursor_lnum();
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 else
3514 {
3515 /*
3516 * scroll the text down
3517 */
3518 while (n > 0 && curwin->w_topline > 1)
3519 {
3520#ifdef FEAT_DIFF
3521 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3522 {
3523 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003524 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 ++curwin->w_topfill;
3526 }
3527 else
3528#endif
3529 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003530 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 n -= i;
3532 if (n < 0 && scrolled > 0)
3533 break;
3534 --curwin->w_topline;
3535#ifdef FEAT_FOLDING
3536 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3537#endif
3538#ifdef FEAT_DIFF
3539 curwin->w_topfill = 0;
3540#endif
3541 }
3542 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3543 VALID_BOTLINE|VALID_BOTLINE_AP);
3544 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (curwin->w_cursor.lnum > 1)
3546 {
3547 --curwin->w_cursor.lnum;
3548 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /*
3553 * When hit top of the file: move cursor up.
3554 */
3555 if (n > 0)
3556 {
3557 if (curwin->w_cursor.lnum <= (linenr_T)n)
3558 curwin->w_cursor.lnum = 1;
3559 else
3560# ifdef FEAT_FOLDING
3561 if (hasAnyFolding(curwin))
3562 {
3563 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3564 {
3565 --curwin->w_cursor.lnum;
3566 (void)hasFolding(curwin->w_cursor.lnum,
3567 &curwin->w_cursor.lnum, NULL);
3568 }
3569 }
3570 else
3571# endif
3572 curwin->w_cursor.lnum -= n;
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003576 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 foldAdjustCursor();
3578# endif
3579#ifdef FEAT_DIFF
3580 check_topfill(curwin, !flag);
3581#endif
3582 cursor_correct();
3583 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003584 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003588do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589{
3590 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003591 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003592 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003593 colnr_T curswant = curwin->w_curswant;
3594 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 win_T *old_curwin = curwin;
3596 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 int old_VIsual_select = VIsual_select;
3598 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599
3600 /*
3601 * loop through the cursorbound windows
3602 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003603 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003604 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605 {
3606 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003608 if (curwin != old_curwin && curwin->w_p_crb)
3609 {
3610# ifdef FEAT_DIFF
3611 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003612 curwin->w_cursor.lnum =
3613 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003614 else
3615# endif
3616 curwin->w_cursor.lnum = line;
3617 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003618 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003619 curwin->w_curswant = curswant;
3620 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621
Bram Moolenaar85a20022019-12-21 18:25:54 +01003622 // Make sure the cursor is in a valid position. Temporarily set
3623 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003624 int restart_edit_save = restart_edit;
3625 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003627
3628 // Avoid a scroll here for the cursor position, 'scrollbind' is
3629 // more important.
3630 if (!curwin->w_p_scb)
3631 validate_cursor();
3632
Bram Moolenaar61452852011-02-01 18:01:11 +01003633 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003634 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 if (has_mbyte)
3636 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003637 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003638
Bram Moolenaar85a20022019-12-21 18:25:54 +01003639 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003640 if (!curwin->w_p_scb)
3641 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003642 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003643 }
3644 }
3645
3646 /*
3647 * reset current-window
3648 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003649 VIsual_select = old_VIsual_select;
3650 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003651 curwin = old_curwin;
3652 curbuf = old_curbuf;
3653}