blob: e435bb08bb11a18b3b0b9891a902184bfd0f986d [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;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100352 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100353 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100354 int overlap = sms_marker_overlap(curwin, curwin_col_off()
355 - curwin_col_off2());
356 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100357 check_topline = TRUE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
363 curwin->w_topline))
364 check_topline = TRUE;
365#endif
366
367 if (check_topline)
368 {
369 halfheight = curwin->w_height / 2 - 1;
370 if (halfheight < 2)
371 halfheight = 2;
372
373#ifdef FEAT_FOLDING
374 if (hasAnyFolding(curwin))
375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // Count the number of logical lines between the cursor and
377 // topline + scrolloff (approximation of how much will be
378 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 n = 0;
380 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100381 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
383 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100384 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
386 break;
387 (void)hasFolding(lnum, NULL, &lnum);
388 }
389 }
390 else
391#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar85a20022019-12-21 18:25:54 +0100394 // If we weren't very close to begin with, we scroll to put the
395 // cursor in the middle of the window. Otherwise put the cursor
396 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000398 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 else
400 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 check_botline = TRUE;
403 }
404 }
405
406 else
407 {
408#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
411#endif
412 check_botline = TRUE;
413 }
414 }
415
416 /*
417 * If the cursor is below the bottom of the window, scroll the window
418 * to put the cursor on the window.
419 * When w_botline is invalid, recompute it first, to avoid a redraw later.
420 * If w_botline was approximated, we might need a redraw later in a few
421 * cases, but we don't want to spend (a lot of) time recomputing w_botline
422 * for every small change.
423 */
424 if (check_botline)
425 {
426 if (!(curwin->w_valid & VALID_BOTLINE_AP))
427 validate_botline();
428
429 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
430 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000431 if (curwin->w_cursor.lnum < curwin->w_botline)
432 {
433 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100434 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#ifdef FEAT_FOLDING
436 || hasAnyFolding(curwin)
437#endif
438 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 lineoff_T loff;
441
Bram Moolenaar85a20022019-12-21 18:25:54 +0100442 // Cursor is (a few lines) above botline, check if there are
443 // 'scrolloff' window lines below the cursor. If not, need to
444 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 n = curwin->w_empty_rows;
446 loff.lnum = curwin->w_cursor.lnum;
447#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100448 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
450#endif
451#ifdef FEAT_DIFF
452 loff.fill = 0;
453 n += curwin->w_filler_rows;
454#endif
455 loff.height = 0;
456 while (loff.lnum < curwin->w_botline
457#ifdef FEAT_DIFF
458 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
459#endif
460 )
461 {
462 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100463 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 break;
465 botline_forw(&loff);
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000470 }
471 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100472 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000473 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475 if (check_botline)
476 {
477#ifdef FEAT_FOLDING
478 if (hasAnyFolding(curwin))
479 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // Count the number of logical lines between the cursor and
481 // botline - scrolloff (approximation of how much will be
482 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 line_count = 0;
484 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100488 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (lnum <= 0 || line_count > curwin->w_height + 1)
490 break;
491 (void)hasFolding(lnum, &lnum, NULL);
492 }
493 }
494 else
495#endif
496 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100497 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000499 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000501 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 }
504 }
505 curwin->w_valid |= VALID_TOPLINE;
506
507 /*
508 * Need to redraw when topline changed.
509 */
510 if (curwin->w_topline != old_topline
511#ifdef FEAT_DIFF
512 || curwin->w_topfill != old_topfill
513#endif
514 )
515 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100516 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000517 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100518
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100519 // When 'smoothscroll' is not set, should reset w_skipcol.
520 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100521 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100522 else if (curwin->w_skipcol != 0)
523 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000524
Bram Moolenaar85a20022019-12-21 18:25:54 +0100525 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (curwin->w_cursor.lnum == curwin->w_topline)
527 validate_cursor();
528 }
529
Bram Moolenaar375e3392019-01-31 18:26:10 +0100530 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000534 * Return the scrolljump value to use for the current window.
535 * When 'scrolljump' is positive use it as-is.
536 * When 'scrolljump' is negative use it as a percentage of the window height.
537 */
538 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100539scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000540{
541 if (p_sj >= 0)
542 return (int)p_sj;
543 return (curwin->w_height * -p_sj) / 100;
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
548 * current window.
549 */
550 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
553 lineoff_T loff;
554 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100555 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar375e3392019-01-31 18:26:10 +0100557 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#ifdef FEAT_FOLDING
559 || hasAnyFolding(curwin)
560#endif
561 )
562 {
563 loff.lnum = curwin->w_cursor.lnum;
564#ifdef FEAT_DIFF
565 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#else
568 n = 0;
569#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100571 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100574 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (loff.lnum < curwin->w_topline
576#ifdef FEAT_DIFF
577 || (loff.lnum == curwin->w_topline && loff.fill > 0)
578#endif
579 )
580 break;
581 n += loff.height;
582 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100583 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return TRUE;
585 }
586 return FALSE;
587}
588
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100589/*
590 * Update w_curswant.
591 */
592 void
593update_curswant_force(void)
594{
595 validate_virtcol();
596 curwin->w_curswant = curwin->w_virtcol
597#ifdef FEAT_PROP_POPUP
598 - curwin->w_virtcol_first_char
599#endif
600 ;
601 curwin->w_set_curswant = FALSE;
602}
603
604/*
605 * Update w_curswant if w_set_curswant is set.
606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100608update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100611 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612}
613
614/*
615 * Check if the cursor has moved. Set the w_valid flag accordingly.
616 */
617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
621 {
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100623 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
624 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 wp->w_valid_cursor = wp->w_cursor;
626 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100627 wp->w_valid_skipcol = wp->w_skipcol;
628 }
629 else if (wp->w_skipcol != wp->w_valid_skipcol)
630 {
631 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
632 |VALID_CHEIGHT|VALID_CROW
633 |VALID_BOTLINE|VALID_BOTLINE_AP);
634 wp->w_valid_cursor = wp->w_cursor;
635 wp->w_valid_leftcol = wp->w_leftcol;
636 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638 else if (wp->w_cursor.col != wp->w_valid_cursor.col
639 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100640 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
642 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
643 wp->w_valid_cursor.col = wp->w_cursor.col;
644 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647}
648
649/*
650 * Call this function when some window settings have changed, which require
651 * the cursor position, botline and topline to be recomputed and the window to
652 * be redrawn. E.g, when changing the 'wrap' option or folding.
653 */
654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100655changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 changed_window_setting_win(curwin);
658}
659
660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 wp->w_lines_valid = 0;
664 changed_line_abv_curs_win(wp);
665 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100666 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667}
668
Dominique Pellee764d1b2023-03-12 21:20:59 +0000669#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000671 * Call changed_window_setting_win() for every window containing "buf".
672 */
673 void
674changed_window_setting_buf(buf_T *buf)
675{
676 tabpage_T *tp;
677 win_T *wp;
678
679 FOR_ALL_TAB_WINDOWS(tp, wp)
680 if (wp->w_buffer == buf)
681 changed_window_setting_win(wp);
682}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000683#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000684
685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * Set wp->w_topline to a certain number.
687 */
688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100689set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200691#ifdef FEAT_DIFF
692 linenr_T prev_topline = wp->w_topline;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100696 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
698#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100699 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100701 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
702 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000704 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200706 if (lnum != prev_topline)
707 // Keep the filler lines when the topline didn't change.
708 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100712 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
716 * Call this function when the length of the cursor line (in screen
717 * characters) has changed, and the change is before the cursor.
718 * Need to take care of w_botline separately!
719 */
720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
724 |VALID_CHEIGHT|VALID_TOPLINE);
725}
726
727 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100728changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
731 |VALID_CHEIGHT|VALID_TOPLINE);
732}
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734/*
735 * Call this function when the length of a line (in screen characters) above
736 * the cursor have changed.
737 * Need to take care of w_botline separately!
738 */
739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100740changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
743 |VALID_CHEIGHT|VALID_TOPLINE);
744}
745
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
750 |VALID_CHEIGHT|VALID_TOPLINE);
751}
752
Dominique Pellee764d1b2023-03-12 21:20:59 +0000753#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100755 * Display of line has changed for "buf", invalidate cursor position and
756 * w_botline.
757 */
758 void
759changed_line_display_buf(buf_T *buf)
760{
761 win_T *wp;
762
763 FOR_ALL_WINDOWS(wp)
764 if (wp->w_buffer == buf)
765 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
766 |VALID_CROW|VALID_CHEIGHT
767 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
768}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000769#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100770
771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * Make sure the value of curwin->w_botline is valid.
773 */
774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100775validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100777 validate_botline_win(curwin);
778}
779
780/*
781 * Make sure the value of wp->w_botline is valid.
782 */
783 void
784validate_botline_win(win_T *wp)
785{
786 if (!(wp->w_valid & VALID_BOTLINE))
787 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * Mark curwin->w_botline as invalid (because of some change in the buffer).
792 */
793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100794invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
797}
798
799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100800invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100806approximate_botline_win(
807 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
809 wp->w_valid &= ~VALID_BOTLINE;
810}
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812/*
813 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
814 */
815 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 check_cursor_moved(curwin);
819 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
820 (VALID_WROW|VALID_WCOL));
821}
822
823/*
824 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
825 * w_topline must be valid, you may need to call update_topline() first!
826 */
827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100828validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100830 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 check_cursor_moved(curwin);
832 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
833 curs_columns(TRUE);
834}
835
836#if defined(FEAT_GUI) || defined(PROTO)
837/*
838 * validate w_cline_row.
839 */
840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100841validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 /*
844 * First make sure that w_topline is valid (after moving the cursor).
845 */
846 update_topline();
847 check_cursor_moved(curwin);
848 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100849 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851#endif
852
853/*
854 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200855 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 */
857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100858curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
860 linenr_T lnum;
861 int i;
862 int all_invalid;
863 int valid;
864#ifdef FEAT_FOLDING
865 long fold_count;
866#endif
867
Bram Moolenaar85a20022019-12-21 18:25:54 +0100868 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 all_invalid = (!redrawing()
870 || wp->w_lines_valid == 0
871 || wp->w_lines[0].wl_lnum > wp->w_topline);
872 i = 0;
873 wp->w_cline_row = 0;
874 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
875 {
876 valid = FALSE;
877 if (!all_invalid && i < wp->w_lines_valid)
878 {
879 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (wp->w_lines[i].wl_lnum == lnum)
882 {
883#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100884 // Check for newly inserted lines below this row, in which
885 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (!wp->w_buffer->b_mod_set
887 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
888 || wp->w_buffer->b_mod_top
889 > wp->w_lines[i].wl_lastlnum + 1)
890#endif
891 valid = TRUE;
892 }
893 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100897 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100899 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100901 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
903#ifdef FEAT_FOLDING
904 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (lnum > wp->w_cursor.lnum)
907 break;
908#else
909 ++lnum;
910#endif
911 wp->w_cline_row += wp->w_lines[i].wl_size;
912 }
913 else
914 {
915#ifdef FEAT_FOLDING
916 fold_count = foldedCount(wp, lnum, NULL);
917 if (fold_count)
918 {
919 lnum += fold_count;
920 if (lnum > wp->w_cursor.lnum)
921 break;
922 ++wp->w_cline_row;
923 }
924 else
925#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100926 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100927 wp->w_cline_row += plines_correct_topline(wp, lnum);
928 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931 }
932
933 check_cursor_moved(wp);
934 if (!(wp->w_valid & VALID_CHEIGHT))
935 {
936 if (all_invalid
937 || i == wp->w_lines_valid
938 || (i < wp->w_lines_valid
939 && (!wp->w_lines[i].wl_valid
940 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
941 {
942#ifdef FEAT_DIFF
943 if (wp->w_cursor.lnum == wp->w_topline)
944 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
945 TRUE) + wp->w_topfill;
946 else
947#endif
948 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
949#ifdef FEAT_FOLDING
950 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
951 NULL, NULL, TRUE, NULL);
952#endif
953 }
954 else if (i > wp->w_lines_valid)
955 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100956 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 wp->w_cline_height = 0;
958#ifdef FEAT_FOLDING
959 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
960 NULL, NULL, TRUE, NULL);
961#endif
962 }
963 else
964 {
965 wp->w_cline_height = wp->w_lines[i].wl_size;
966#ifdef FEAT_FOLDING
967 wp->w_cline_folded = wp->w_lines[i].wl_folded;
968#endif
969 }
970 }
971
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100972 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Validate curwin->w_virtcol only.
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 validate_virtcol_win(curwin);
983}
984
985/*
986 * Validate wp->w_virtcol only.
987 */
988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100989validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992
993 if (wp->w_valid & VALID_VIRTCOL)
994 return;
995
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100996#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000997 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100998#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000999 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001000#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001001 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001002#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001003 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004}
1005
1006/*
1007 * Validate curwin->w_cline_height only.
1008 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001010validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
1012 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001013
1014 if (curwin->w_valid & VALID_CHEIGHT)
1015 return;
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 if (curwin->w_cursor.lnum == curwin->w_topline)
1019 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1020 + curwin->w_topfill;
1021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001023 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001025 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001027 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001031 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 */
1033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001034validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 colnr_T off;
1037 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001038 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001042 if (curwin->w_valid & VALID_WCOL)
1043 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001044
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001045 col = curwin->w_virtcol;
1046 off = curwin_col_off();
1047 col += off;
1048 width = curwin->w_width - off + curwin_col_off2();
1049
1050 // long line wrapping, adjust curwin->w_wrow
1051 if (curwin->w_p_wrap
1052 && col >= (colnr_T)curwin->w_width
1053 && width > 0)
1054 // use same formula as what is used in curs_columns()
1055 col -= ((col - curwin->w_width) / width + 1) * width;
1056 if (col > (int)curwin->w_leftcol)
1057 col -= curwin->w_leftcol;
1058 else
1059 col = 0;
1060 curwin->w_wcol = col;
1061
1062 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001063#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001064 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066}
1067
1068/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001069 * Compute offset of a window, occupied by absolute or relative line number,
1070 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
1072 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001073win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar64486672010-05-16 15:46:46 +02001075 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#ifdef FEAT_FOLDING
1078 + wp->w_p_fdc
1079#endif
1080#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001081 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#endif
1083 );
1084}
1085
1086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 return win_col_off(curwin);
1090}
1091
1092/*
1093 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001094 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1095 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
1097 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001098win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
Bram Moolenaar64486672010-05-16 15:46:46 +02001100 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001101 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 return 0;
1103}
1104
1105 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001106curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 return win_col_off2(curwin);
1109}
1110
1111/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001112 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 * Also updates curwin->w_wrow and curwin->w_cline_row.
1114 * Also updates curwin->w_leftcol.
1115 */
1116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001121 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int off_left, off_right;
1123 int n;
1124 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001125 int width1; // text width for first screen line
1126 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int new_leftcol;
1128 colnr_T startcol;
1129 colnr_T endcol;
1130 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001131 long so = get_scrolloff_value();
1132 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001133 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 /*
1136 * First make sure that w_topline is valid (after moving the cursor).
1137 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001138 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 /*
1141 * Next make sure that w_cline_row is valid.
1142 */
1143 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001144 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001146#ifdef FEAT_PROP_POPUP
1147 // will be set by getvvcol() but not reset
1148 curwin->w_virtcol_first_char = 0;
1149#endif
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /*
1152 * Compute the number of virtual columns.
1153 */
1154#ifdef FEAT_FOLDING
1155 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1158 else
1159#endif
1160 getvvcol(curwin, &curwin->w_cursor,
1161 &startcol, &(curwin->w_virtcol), &endcol);
1162
Bram Moolenaar85a20022019-12-21 18:25:54 +01001163 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001165 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 extra = curwin_col_off();
1168 curwin->w_wcol = curwin->w_virtcol + extra;
1169 endcol += extra;
1170
1171 /*
1172 * Now compute w_wrow, counting screen lines from w_cline_row.
1173 */
1174 curwin->w_wrow = curwin->w_cline_row;
1175
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001176 width1 = curwin->w_width - extra;
1177 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001180 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001181 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001182 if (curwin->w_p_wrap)
1183 curwin->w_wrow = curwin->w_height - 1;
1184 else
1185 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001187 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001189 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001191 // skip columns that are not visible
1192 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001193 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001194 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001195 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001196 // Deduct by multiples of width2. This allows the long line
1197 // wrapping formula below to correctly calculate the w_wcol value
1198 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001199 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001200 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001201 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001202 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001203 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001204
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001205 did_sub_skipcol = TRUE;
1206 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001207
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001209 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001212 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1213 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 curwin->w_wrow += n;
1215
1216#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001217 // When cursor wraps to first char of next line in Insert
1218 // mode, the 'showbreak' string isn't shown, backup to first
1219 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001220 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001221 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001222 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 curwin->w_wcol = 0;
1224#endif
1225 }
1226 }
1227
Bram Moolenaar85a20022019-12-21 18:25:54 +01001228 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1229 // is not folded.
1230 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001231 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#ifdef FEAT_FOLDING
1233 && !curwin->w_cline_folded
1234#endif
1235 )
1236 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001237#ifdef FEAT_PROP_POPUP
1238 if (curwin->w_virtcol_first_char > 0)
1239 {
1240 int cols = (curwin->w_width - extra);
1241 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1242
1243 // each "above" text prop shifts the text one row down
1244 curwin->w_wrow += rows;
1245 curwin->w_wcol -= rows * cols;
1246 endcol -= rows * cols;
1247 curwin->w_cline_height = rows + 1;
1248 }
1249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 /*
1251 * If Cursor is left of the screen, scroll rightwards.
1252 * If Cursor is right of the screen, scroll leftwards
1253 * If we get closer to the edge than 'sidescrolloff', scroll a little
1254 * extra
1255 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001256 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001257 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001258 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (off_left < 0 || off_right > 0)
1260 {
1261 if (off_left < 0)
1262 diff = -off_left;
1263 else
1264 diff = off_right;
1265
Bram Moolenaar85a20022019-12-21 18:25:54 +01001266 // When far off or not enough room on either side, put cursor in
1267 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001268 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1269 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 else
1271 {
1272 if (diff < p_ss)
1273 diff = p_ss;
1274 if (off_left < 0)
1275 new_leftcol = curwin->w_leftcol - diff;
1276 else
1277 new_leftcol = curwin->w_leftcol + diff;
1278 }
1279 if (new_leftcol < 0)
1280 new_leftcol = 0;
1281 if (new_leftcol != (int)curwin->w_leftcol)
1282 {
1283 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001284 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001285 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288 curwin->w_wcol -= curwin->w_leftcol;
1289 }
1290 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1291 curwin->w_wcol -= curwin->w_leftcol;
1292 else
1293 curwin->w_wcol = 0;
1294
1295#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 // Skip over filler lines. At the top use w_topfill, there
1297 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (curwin->w_cursor.lnum == curwin->w_topline)
1299 curwin->w_wrow += curwin->w_topfill;
1300 else
1301 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1302#endif
1303
1304 prev_skipcol = curwin->w_skipcol;
1305
1306 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if ((curwin->w_wrow >= curwin->w_height
1309 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001310 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 && (p_lines =
1312#ifdef FEAT_DIFF
1313 plines_win_nofill
1314#else
1315 plines_win
1316#endif
1317 (curwin, curwin->w_cursor.lnum, FALSE))
1318 - 1 >= curwin->w_height))
1319 && curwin->w_height != 0
1320 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001321 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001322 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 // Cursor past end of screen. Happens with a single line that does
1325 // not fit on screen. Find a skipcol to show the text around the
1326 // cursor. Avoid scrolling all the time. compute value of "extra":
1327 // 1: Less than 'scrolloff' lines above
1328 // 2: Less than 'scrolloff' lines below
1329 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001331 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 // Compute last display line of the buffer line that we want at the
1334 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (p_lines == 0)
1336 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1337 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001338 if (p_lines > curwin->w_wrow + so)
1339 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001342 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 extra += 2;
1344
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001345 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001347 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001348 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (n > curwin->w_height / 2)
1350 n -= curwin->w_height / 2;
1351 else
1352 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001353 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (n > p_lines - curwin->w_height + 1)
1355 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001356 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 else if (extra == 1)
1359 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001360 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001361 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1362 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (extra > 0)
1364 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001365 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1366 extra = curwin->w_skipcol / width2;
1367 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 }
1370 else if (extra == 2)
1371 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001372 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001373 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001375 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (endcol > curwin->w_skipcol)
1377 curwin->w_skipcol = endcol;
1378 }
1379
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001380 // adjust w_wrow for the changed w_skipcol
1381 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001382 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001383 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001384 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (curwin->w_wrow >= curwin->w_height)
1387 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001390 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 curwin->w_wrow -= extra;
1392 }
1393
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001394 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (extra > 0)
1396 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1397 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001398 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001400 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 curwin->w_skipcol = 0;
1402 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001403 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001405#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001406 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001407#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001408#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1409 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1410 {
1411 curwin->w_wrow += popup_top_extra(curwin);
1412 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001413 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001414 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001415 else
1416 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001417#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001418
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001419 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1420 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001421 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001422 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1425}
1426
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001427#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001428/*
1429 * Compute the screen position of text character at "pos" in window "wp"
1430 * The resulting values are one-based, zero when character is not visible.
1431 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001432 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001433textpos2screenpos(
1434 win_T *wp,
1435 pos_T *pos,
1436 int *rowp, // screen row
1437 int *scolp, // start screen column
1438 int *ccolp, // cursor screen column
1439 int *ecolp) // end screen column
1440{
1441 colnr_T scol = 0, ccol = 0, ecol = 0;
1442 int row = 0;
1443 int rowoff = 0;
1444 colnr_T coloff = 0;
1445
Bram Moolenaar189663b2021-07-21 18:04:56 +02001446 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001447 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001448 colnr_T col;
1449 int width;
1450 linenr_T lnum = pos->lnum;
1451#ifdef FEAT_FOLDING
1452 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001453
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001454 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1455#endif
1456 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001457
1458#ifdef FEAT_DIFF
1459 // Add filler lines above this buffer line.
1460 row += diff_check_fill(wp, lnum);
1461#endif
1462
zeertzjqba2d1912022-12-18 12:28:59 +00001463 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001464#ifdef FEAT_FOLDING
1465 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001466 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001467 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001468 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001469 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001471#endif
1472 {
1473 getvcol(wp, pos, &scol, &ccol, &ecol);
1474
1475 // similar to what is done in validate_cursor_col()
1476 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001477 col += off;
1478 width = wp->w_width - off + win_col_off2(wp);
1479
1480 // long line wrapping, adjust row
1481 if (wp->w_p_wrap
1482 && col >= (colnr_T)wp->w_width
1483 && width > 0)
1484 {
1485 // use same formula as what is used in curs_columns()
1486 rowoff = ((col - wp->w_width) / width + 1);
1487 col -= rowoff * width;
1488 }
1489 col -= wp->w_leftcol;
1490 if (col >= wp->w_width)
1491 col = -1;
1492 if (col >= 0 && row + rowoff <= wp->w_height)
1493 {
1494 coloff = col - scol + wp->w_wincol + 1;
1495 row += W_WINROW(wp);
1496 }
1497 else
1498 // character is left, right or below of the window
1499 row = rowoff = scol = ccol = ecol = 0;
1500 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001501 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001502 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503 *scolp = scol + coloff;
1504 *ccolp = ccol + coloff;
1505 *ecolp = ecol + coloff;
1506}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001507#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508
Bram Moolenaar12034e22019-08-25 22:25:02 +02001509#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510/*
1511 * "screenpos({winid}, {lnum}, {col})" function
1512 */
1513 void
1514f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1515{
1516 dict_T *dict;
1517 win_T *wp;
1518 pos_T pos;
1519 int row = 0;
1520 int scol = 0, ccol = 0, ecol = 0;
1521
Bram Moolenaar93a10962022-06-16 11:42:09 +01001522 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001523 return;
1524 dict = rettv->vval.v_dict;
1525
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001526 if (in_vim9script()
1527 && (check_for_number_arg(argvars, 0) == FAIL
1528 || check_for_number_arg(argvars, 1) == FAIL
1529 || check_for_number_arg(argvars, 2) == FAIL))
1530 return;
1531
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001532 wp = find_win_by_nr_or_id(&argvars[0]);
1533 if (wp == NULL)
1534 return;
1535
1536 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001537 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1538 {
1539 semsg(_(e_invalid_line_number_nr), pos.lnum);
1540 return;
1541 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001542 pos.col = tv_get_number(&argvars[2]) - 1;
1543 pos.coladd = 0;
1544 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1545
1546 dict_add_number(dict, "row", row);
1547 dict_add_number(dict, "col", scol);
1548 dict_add_number(dict, "curscol", ccol);
1549 dict_add_number(dict, "endcol", ecol);
1550}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001551
1552/*
1553 * "virtcol2col({winid}, {lnum}, {col})" function
1554 */
1555 void
1556f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1557{
1558 win_T *wp;
1559 linenr_T lnum;
1560 int screencol;
1561 int error = FALSE;
1562
1563 rettv->vval.v_number = -1;
1564
1565 if (check_for_number_arg(argvars, 0) == FAIL
1566 || check_for_number_arg(argvars, 1) == FAIL
1567 || check_for_number_arg(argvars, 2) == FAIL)
1568 return;
1569
1570 wp = find_win_by_nr_or_id(&argvars[0]);
1571 if (wp == NULL)
1572 return;
1573
1574 lnum = tv_get_number_chk(&argvars[1], &error);
1575 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1576 return;
1577
1578 screencol = tv_get_number_chk(&argvars[2], &error);
1579 if (error || screencol < 0)
1580 return;
1581
1582 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1583}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001584#endif
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
1587 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001590scrolldown(
1591 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001592 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 int wrow;
1596 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001597 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001598 int width1 = 0;
1599 int width2 = 0;
1600
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001601 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001602 {
1603 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001604 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607#ifdef FEAT_FOLDING
1608 linenr_T first;
1609
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1612#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001613 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001614 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001617 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1618 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 ++curwin->w_topfill;
1621 ++done;
1622 }
1623 else
1624#endif
1625 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001626 // break when at the very top
1627 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001628 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001630 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001632 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001633 if (curwin->w_skipcol >= width1 + width2)
1634 curwin->w_skipcol -= width2;
1635 else
1636 curwin->w_skipcol -= width1;
1637 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001641 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001642 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001643 --curwin->w_topline;
1644 curwin->w_skipcol = 0;
1645#ifdef FEAT_DIFF
1646 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001648#ifdef FEAT_FOLDING
1649 // A sequence of folded lines only counts for one logical line
1650 if (hasFolding(curwin->w_topline, &first, NULL))
1651 {
1652 ++done;
1653 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001654 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001655 curwin->w_botline -= curwin->w_topline - first;
1656 curwin->w_topline = first;
1657 }
1658 else
1659#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001660 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001661 {
1662 int size = win_linetabsize(curwin, curwin->w_topline,
1663 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1664 if (size > width1)
1665 {
1666 curwin->w_skipcol = width1;
1667 size -= width1;
1668 redraw_later(UPD_NOT_VALID);
1669 }
1670 while (size > width2)
1671 {
1672 curwin->w_skipcol += width2;
1673 size -= width2;
1674 }
1675 ++done;
1676 }
1677 else
1678 done += PLINES_NOFILL(curwin->w_topline);
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001681 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 invalidate_botline();
1683 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001684 curwin->w_wrow += done; // keep w_wrow updated
1685 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687#ifdef FEAT_DIFF
1688 if (curwin->w_cursor.lnum == curwin->w_topline)
1689 curwin->w_cline_row = 0;
1690 check_topfill(curwin, TRUE);
1691#endif
1692
1693 /*
1694 * Compute the row number of the last row of the cursor line
1695 * and move the cursor onto the displayed part of the window.
1696 */
1697 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001698 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 validate_virtcol();
1701 validate_cheight();
1702 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001703 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
1705 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1706 {
1707#ifdef FEAT_FOLDING
1708 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1709 {
1710 --wrow;
1711 if (first == 1)
1712 curwin->w_cursor.lnum = 1;
1713 else
1714 curwin->w_cursor.lnum = first - 1;
1715 }
1716 else
1717#endif
1718 wrow -= plines(curwin->w_cursor.lnum--);
1719 curwin->w_valid &=
1720 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1721 moved = TRUE;
1722 }
1723 if (moved)
1724 {
1725#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001726 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 foldAdjustCursor();
1728#endif
1729 coladvance(curwin->w_curswant);
1730 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001731
1732 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1733 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001734 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001735 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1736
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001737 // make sure the cursor is in the visible text
1738 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001739 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001740 int row = 0;
1741 if (col >= width1)
1742 {
1743 col -= width1;
1744 ++row;
1745 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001746 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001747 {
1748 row += col / width2;
1749 col = col % width2;
1750 }
1751 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001752 {
1753 curwin->w_curswant = curwin->w_virtcol
1754 - (row - curwin->w_height + 1) * width2;
1755 coladvance(curwin->w_curswant);
1756 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760/*
1761 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001764scrollup(
1765 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001766 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001768 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001770 if (do_sms
1771# ifdef FEAT_FOLDING
1772 || (byfold && hasAnyFolding(curwin))
1773# endif
1774# ifdef FEAT_DIFF
1775 || (curwin->w_p_diff && !curwin->w_p_wrap)
1776# endif
1777 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001779 int width1 = curwin->w_width - curwin_col_off();
1780 int width2 = width1 + curwin_col_off2();
1781 int size = 0;
1782 linenr_T prev_topline = curwin->w_topline;
1783
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001784 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001785 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001786
1787 // diff mode: first consume "topfill"
1788 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1789 // the line, then advance to the next line.
1790 // folding: count each sequence of folded lines as one logical line.
1791 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
1793# ifdef FEAT_DIFF
1794 if (curwin->w_topfill > 0)
1795 --curwin->w_topfill;
1796 else
1797# endif
1798 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001799 linenr_T lnum = curwin->w_topline;
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801# ifdef FEAT_FOLDING
1802 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 (void)hasFolding(lnum, NULL, &lnum);
1805# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001806 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001807 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 // 'smoothscroll': increase "w_skipcol" until it goes over
1809 // the end of the line, then advance to the next line.
1810 int add = curwin->w_skipcol > 0 ? width2 : width1;
1811 curwin->w_skipcol += add;
1812 if (curwin->w_skipcol >= size)
1813 {
1814 if (lnum == curbuf->b_ml.ml_line_count)
1815 {
1816 // at the last screen line, can't scroll further
1817 curwin->w_skipcol -= add;
1818 break;
1819 }
1820 ++lnum;
1821 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001822 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001823 else
1824 {
1825 if (lnum >= curbuf->b_ml.ml_line_count)
1826 break;
1827 ++lnum;
1828 }
1829
1830 if (lnum > curwin->w_topline)
1831 {
1832 // approximate w_botline
1833 curwin->w_botline += lnum - curwin->w_topline;
1834 curwin->w_topline = lnum;
1835# ifdef FEAT_DIFF
1836 curwin->w_topfill = diff_check_fill(curwin, lnum);
1837# endif
1838 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001839 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001840 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 }
1843 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001844
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001845 if (curwin->w_topline == prev_topline)
1846 // need to redraw even though w_topline didn't change
1847 redraw_later(UPD_NOT_VALID);
1848 }
1849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001852 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
1855 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1856 curwin->w_topline = curbuf->b_ml.ml_line_count;
1857 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1858 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1859
1860#ifdef FEAT_DIFF
1861 check_topfill(curwin, FALSE);
1862#endif
1863
1864#ifdef FEAT_FOLDING
1865 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001866 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1868#endif
1869
1870 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1871 if (curwin->w_cursor.lnum < curwin->w_topline)
1872 {
1873 curwin->w_cursor.lnum = curwin->w_topline;
1874 curwin->w_valid &=
1875 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1876 coladvance(curwin->w_curswant);
1877 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001878 if (curwin->w_cursor.lnum == curwin->w_topline
1879 && do_sms && curwin->w_skipcol > 0)
1880 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001881 int col_off = curwin_col_off();
1882 int col_off2 = curwin_col_off2();
1883
1884 int width1 = curwin->w_width - col_off;
1885 int width2 = width1 + col_off2;
1886 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001887 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001888 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001889 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001890
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001891 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001892 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001893 int overlap = scrolloff_cols != 0 ? 0
1894 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001895
Bram Moolenaar118c2352022-10-09 17:19:27 +01001896 // Make sure the cursor is in a visible part of the line, taking
1897 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001898 // If there are not enough screen lines put the cursor in the middle.
1899 if (scrolloff_cols > space_cols / 2)
1900 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001901 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001902 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001903 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001904 colnr_T col = curwin->w_virtcol;
1905
1906 if (col < width1)
1907 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001908 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001909 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001910 curwin->w_curswant = col;
1911 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001912
1913 // validate_virtcol() marked various things as valid, but after
1914 // moving the cursor they need to be recomputed
1915 curwin->w_valid &=
1916 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001917 }
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919}
1920
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001921/*
1922 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1923 * valid for 'smoothscroll'.
1924 */
1925 void
1926adjust_skipcol(void)
1927{
1928 if (!curwin->w_p_wrap
1929 || !curwin->w_p_sms
1930 || curwin->w_cursor.lnum != curwin->w_topline)
1931 return;
1932
1933 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001934 if (width1 <= 0)
1935 return; // no text will be displayed
1936
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001937 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001938 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001939 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1940 int scrolled = FALSE;
1941
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001942 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001943 if (curwin->w_cline_height == curwin->w_height
1944 // w_cline_height may be capped at w_height, check there aren't
1945 // actually more lines.
1946 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1947 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001948 {
1949 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001950 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001951 return;
1952 }
1953
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001954 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001955 int overlap = sms_marker_overlap(curwin,
1956 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001957 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001958 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001959 {
1960 // scroll a screen line down
1961 if (curwin->w_skipcol >= width1 + width2)
1962 curwin->w_skipcol -= width2;
1963 else
1964 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001965 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001966 }
1967 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001968 {
1969 validate_virtcol();
1970 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001971 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001972 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001973
1974 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1975 int row = 0;
1976 if (col >= width1)
1977 {
1978 col -= width1;
1979 ++row;
1980 }
1981 if (col > width2)
1982 {
1983 row += col / width2;
1984 col = col % width2;
1985 }
1986 if (row >= curwin->w_height)
1987 {
1988 if (curwin->w_skipcol == 0)
1989 {
1990 curwin->w_skipcol += width1;
1991 --row;
1992 }
1993 if (row >= curwin->w_height)
1994 curwin->w_skipcol += (row - curwin->w_height) * width2;
1995 redraw_later(UPD_NOT_VALID);
1996 }
1997}
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#ifdef FEAT_DIFF
2000/*
2001 * Don't end up with too many filler lines in the window.
2002 */
2003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004check_topfill(
2005 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002006 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int n;
2009
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002010 if (wp->w_topfill <= 0)
2011 return;
2012
2013 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2014 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002016 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002018 --wp->w_topline;
2019 wp->w_topfill = 0;
2020 }
2021 else
2022 {
2023 wp->w_topfill = wp->w_height - n;
2024 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 }
2028}
2029
2030/*
2031 * Use as many filler lines as possible for w_topline. Make sure w_topline
2032 * is still visible.
2033 */
2034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002035max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 int n;
2038
2039 n = plines_nofill(curwin->w_topline);
2040 if (n >= curwin->w_height)
2041 curwin->w_topfill = 0;
2042 else
2043 {
2044 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2045 if (curwin->w_topfill + n > curwin->w_height)
2046 curwin->w_topfill = curwin->w_height - n;
2047 }
2048}
2049#endif
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051/*
2052 * Scroll the screen one line down, but don't do it if it would move the
2053 * cursor off the screen.
2054 */
2055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002056scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int end_row;
2059#ifdef FEAT_DIFF
2060 int can_fill = (curwin->w_topfill
2061 < diff_check_fill(curwin, curwin->w_topline));
2062#endif
2063
2064 if (curwin->w_topline <= 1
2065#ifdef FEAT_DIFF
2066 && !can_fill
2067#endif
2068 )
2069 return;
2070
Bram Moolenaar85a20022019-12-21 18:25:54 +01002071 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /*
2074 * Compute the row number of the last row of the cursor line
2075 * and make sure it doesn't go off the screen. Make sure the cursor
2076 * doesn't go past 'scrolloff' lines from the screen end.
2077 */
2078 end_row = curwin->w_wrow;
2079#ifdef FEAT_DIFF
2080 if (can_fill)
2081 ++end_row;
2082 else
2083 end_row += plines_nofill(curwin->w_topline - 1);
2084#else
2085 end_row += plines(curwin->w_topline - 1);
2086#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002087 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 validate_cheight();
2090 validate_virtcol();
2091 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002092 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002094 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096#ifdef FEAT_DIFF
2097 if (can_fill)
2098 {
2099 ++curwin->w_topfill;
2100 check_topfill(curwin, TRUE);
2101 }
2102 else
2103 {
2104 --curwin->w_topline;
2105 curwin->w_topfill = 0;
2106 }
2107#else
2108 --curwin->w_topline;
2109#endif
2110#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002111 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002113 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2115 }
2116}
2117
2118/*
2119 * Scroll the screen one line up, but don't do it if it would move the cursor
2120 * off the screen.
2121 */
2122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 int start_row;
2126
2127 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2128#ifdef FEAT_DIFF
2129 && curwin->w_topfill == 0
2130#endif
2131 )
2132 return;
2133
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 /*
2137 * Compute the row number of the first row of the cursor line
2138 * and make sure it doesn't go off the screen. Make sure the cursor
2139 * doesn't go before 'scrolloff' lines from the screen start.
2140 */
2141#ifdef FEAT_DIFF
2142 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2143 - curwin->w_topfill;
2144#else
2145 start_row = curwin->w_wrow - plines(curwin->w_topline);
2146#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002147 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002150 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002152 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154#ifdef FEAT_DIFF
2155 if (curwin->w_topfill > 0)
2156 --curwin->w_topfill;
2157 else
2158#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002159 {
2160#ifdef FEAT_FOLDING
2161 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002164 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2167 }
2168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170/*
2171 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2172 * a (wrapped) text line. Uses and sets "lp->fill".
2173 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002174 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
2176 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002177topline_back_winheight(
2178 lineoff_T *lp,
2179 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
2181#ifdef FEAT_DIFF
2182 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002184 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 ++lp->fill;
2186 lp->height = 1;
2187 }
2188 else
2189#endif
2190 {
2191 --lp->lnum;
2192#ifdef FEAT_DIFF
2193 lp->fill = 0;
2194#endif
2195 if (lp->lnum < 1)
2196 lp->height = MAXCOL;
2197 else
2198#ifdef FEAT_FOLDING
2199 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002200 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 lp->height = 1;
2202 else
2203#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002204 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206}
2207
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002208 static void
2209topline_back(lineoff_T *lp)
2210{
2211 topline_back_winheight(lp, TRUE);
2212}
2213
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215/*
2216 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2217 * a (wrapped) text line. Uses and sets "lp->fill".
2218 * Returns the height of the added line in "lp->height".
2219 * Lines below the last one are incredibly high.
2220 */
2221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224#ifdef FEAT_DIFF
2225 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2226 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ++lp->fill;
2229 lp->height = 1;
2230 }
2231 else
2232#endif
2233 {
2234 ++lp->lnum;
2235#ifdef FEAT_DIFF
2236 lp->fill = 0;
2237#endif
2238 if (lp->lnum > curbuf->b_ml.ml_line_count)
2239 lp->height = MAXCOL;
2240 else
2241#ifdef FEAT_FOLDING
2242 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002243 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 lp->height = 1;
2245 else
2246#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002247 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249}
2250
2251#ifdef FEAT_DIFF
2252/*
2253 * Switch from including filler lines below lp->lnum to including filler
2254 * lines above loff.lnum + 1. This keeps pointing to the same line.
2255 * When there are no filler lines nothing changes.
2256 */
2257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002258botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 if (lp->fill > 0)
2261 {
2262 ++lp->lnum;
2263 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2264 }
2265}
2266
2267/*
2268 * Switch from including filler lines above lp->lnum to including filler
2269 * lines below loff.lnum - 1. This keeps pointing to the same line.
2270 * When there are no filler lines nothing changes.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 if (lp->fill > 0)
2276 {
2277 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2278 --lp->lnum;
2279 }
2280}
2281#endif
2282
2283/*
2284 * Recompute topline to put the cursor at the top of the window.
2285 * Scroll at least "min_scroll" lines.
2286 * If "always" is TRUE, always set topline (for "zt").
2287 */
2288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002289scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 int scrolled = 0;
2292 int extra = 0;
2293 int used;
2294 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 linenr_T top; // just above displayed lines
2296 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002298 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#ifdef FEAT_DIFF
2300 linenr_T old_topfill = curwin->w_topfill;
2301#endif
2302 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002303 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (mouse_dragging > 0)
2306 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 /*
2309 * Decrease topline until:
2310 * - it has become 1
2311 * - (part of) the cursor line is moved off the screen or
2312 * - moved at least 'scrolljump' lines and
2313 * - at least 'scrolloff' lines above and below the cursor
2314 */
2315 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002316 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (curwin->w_cursor.lnum < curwin->w_topline)
2318 scrolled = used;
2319
2320#ifdef FEAT_FOLDING
2321 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2322 {
2323 --top;
2324 ++bot;
2325 }
2326 else
2327#endif
2328 {
2329 top = curwin->w_cursor.lnum - 1;
2330 bot = curwin->w_cursor.lnum + 1;
2331 }
2332 new_topline = top + 1;
2333
2334#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002335 // "used" already contains the number of filler lines above, don't add it
2336 // again.
2337 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002338 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#endif
2340
2341 /*
2342 * Check if the lines from "top" to "bot" fit in the window. If they do,
2343 * set new_topline and advance "top" and "bot" to include more lines.
2344 */
2345 while (top > 0)
2346 {
2347#ifdef FEAT_FOLDING
2348 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002349 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 i = 1;
2351 else
2352#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002353 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002354 if (top < curwin->w_topline)
2355 scrolled += i;
2356
2357 // If scrolling is needed, scroll at least 'sj' lines.
2358 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2359 && extra >= off)
2360 break;
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 used += i;
2363 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2364 {
2365#ifdef FEAT_FOLDING
2366 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002367 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 ++used;
2369 else
2370#endif
2371 used += plines(bot);
2372 }
2373 if (used > curwin->w_height)
2374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 extra += i;
2377 new_topline = top;
2378 --top;
2379 ++bot;
2380 }
2381
2382 /*
2383 * If we don't have enough space, put cursor in the middle.
2384 * This makes sure we get the same position when using "k" and "j"
2385 * in a small window.
2386 */
2387 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002388 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 else
2390 {
2391 /*
2392 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002393 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
2395 if (new_topline < curwin->w_topline || always)
2396 curwin->w_topline = new_topline;
2397 if (curwin->w_topline > curwin->w_cursor.lnum)
2398 curwin->w_topline = curwin->w_cursor.lnum;
2399#ifdef FEAT_DIFF
2400 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2401 if (curwin->w_topfill > 0 && extra > off)
2402 {
2403 curwin->w_topfill -= extra - off;
2404 if (curwin->w_topfill < 0)
2405 curwin->w_topfill = 0;
2406 }
2407 check_topfill(curwin, FALSE);
2408#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002409 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002410 if (curwin->w_topline == curwin->w_cursor.lnum
2411 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002412 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002414 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#ifdef FEAT_DIFF
2416 || curwin->w_topfill != old_topfill
2417#endif
2418 )
2419 curwin->w_valid &=
2420 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2421 curwin->w_valid |= VALID_TOPLINE;
2422 }
2423}
2424
2425/*
2426 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2427 * screen lines for text lines.
2428 */
2429 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002430set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432#ifdef FEAT_DIFF
2433 wp->w_filler_rows = 0;
2434#endif
2435 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 else
2438 {
2439 wp->w_empty_rows = wp->w_height - used;
2440#ifdef FEAT_DIFF
2441 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2442 {
2443 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2444 if (wp->w_empty_rows > wp->w_filler_rows)
2445 wp->w_empty_rows -= wp->w_filler_rows;
2446 else
2447 {
2448 wp->w_filler_rows = wp->w_empty_rows;
2449 wp->w_empty_rows = 0;
2450 }
2451 }
2452#endif
2453 }
2454}
2455
2456/*
2457 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002458 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2460 * This is messy stuff!!!
2461 */
2462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 int used;
2466 int scrolled = 0;
2467 int extra = 0;
2468 int i;
2469 linenr_T line_count;
2470 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002471 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 lineoff_T loff;
2473 lineoff_T boff;
2474#ifdef FEAT_DIFF
2475 int old_topfill = curwin->w_topfill;
2476 int fill_below_window;
2477#endif
2478 linenr_T old_botline = curwin->w_botline;
2479 linenr_T old_valid = curwin->w_valid;
2480 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002482 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002483 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 cln = curwin->w_cursor.lnum;
2486 if (set_topbot)
2487 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002488 int set_skipcol = FALSE;
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 used = 0;
2491 curwin->w_botline = cln + 1;
2492#ifdef FEAT_DIFF
2493 loff.fill = 0;
2494#endif
2495 for (curwin->w_topline = curwin->w_botline;
2496 curwin->w_topline > 1;
2497 curwin->w_topline = loff.lnum)
2498 {
2499 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002500 topline_back_winheight(&loff, FALSE);
2501 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002503 if (used + loff.height > curwin->w_height)
2504 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002505 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002506 {
2507 // 'smoothscroll' and 'wrap' are set. The above line is
2508 // too long to show in its entirety, so we show just a part
2509 // of it.
2510 if (used < curwin->w_height)
2511 {
2512 int plines_offset = used + loff.height
2513 - curwin->w_height;
2514 used = curwin->w_height;
2515#ifdef FEAT_DIFF
2516 curwin->w_topfill = loff.fill;
2517#endif
2518 curwin->w_topline = loff.lnum;
2519 curwin->w_skipcol = skipcol_from_plines(
2520 curwin, plines_offset);
2521 set_skipcol = TRUE;
2522 }
2523 }
2524 break;
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 used += loff.height;
2527#ifdef FEAT_DIFF
2528 curwin->w_topfill = loff.fill;
2529#endif
2530 }
2531 set_empty_rows(curwin, used);
2532 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2533 if (curwin->w_topline != old_topline
2534#ifdef FEAT_DIFF
2535 || curwin->w_topfill != old_topfill
2536#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002537 || set_skipcol
2538 || curwin->w_skipcol != 0)
2539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002541 if (set_skipcol)
2542 redraw_later(UPD_NOT_VALID);
2543 else
2544 reset_skipcol();
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547 else
2548 validate_botline();
2549
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#ifdef FEAT_DIFF
2552 used = plines_nofill(cln);
2553#else
2554 validate_cheight();
2555 used = curwin->w_cline_height;
2556#endif
2557
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002558 // If the cursor is on or below botline, we will at least scroll by the
2559 // height of the cursor line, which is "used". Correct for empty lines,
2560 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (cln >= curwin->w_botline)
2562 {
2563 scrolled = used;
2564 if (cln == curwin->w_botline)
2565 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002566 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002567 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002568 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002569 // Calculate how many screen lines the current top line of window
2570 // occupies. If it is occupying more than the entire window, we
2571 // need to scroll the additional clipped lines to scroll past the
2572 // top line before we can move on to the other lines.
2573 int top_plines =
2574#ifdef FEAT_DIFF
2575 plines_win_nofill
2576#else
2577 plines_win
2578#endif
2579 (curwin, curwin->w_topline, FALSE);
2580 int skip_lines = 0;
2581 int width1 = curwin->w_width - curwin_col_off();
2582 int width2 = width1 + curwin_col_off2();
2583 // similar formula is used in curs_columns()
2584 if (curwin->w_skipcol > width1)
2585 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2586 else if (curwin->w_skipcol > 0)
2587 skip_lines = 1;
2588
2589 top_plines -= skip_lines;
2590 if (top_plines > curwin->w_height)
2591 {
2592 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002593 }
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
2597 /*
2598 * Stop counting lines to scroll when
2599 * - hitting start of the file
2600 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002601 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 * - lines between botline and cursor have been counted
2603 */
2604#ifdef FEAT_FOLDING
2605 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2606#endif
2607 {
2608 loff.lnum = cln;
2609 boff.lnum = cln;
2610 }
2611#ifdef FEAT_DIFF
2612 loff.fill = 0;
2613 boff.fill = 0;
2614 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2615 - curwin->w_filler_rows;
2616#endif
2617
2618 while (loff.lnum > 1)
2619 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002620 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2621 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002623 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2625 && loff.lnum <= curwin->w_botline
2626#ifdef FEAT_DIFF
2627 && (loff.lnum < curwin->w_botline
2628 || loff.fill >= fill_below_window)
2629#endif
2630 )
2631 break;
2632
Bram Moolenaar85a20022019-12-21 18:25:54 +01002633 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002635 if (loff.height == MAXCOL)
2636 used = MAXCOL;
2637 else
2638 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (used > curwin->w_height)
2640 break;
2641 if (loff.lnum >= curwin->w_botline
2642#ifdef FEAT_DIFF
2643 && (loff.lnum > curwin->w_botline
2644 || loff.fill <= fill_below_window)
2645#endif
2646 )
2647 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 scrolled += loff.height;
2650 if (loff.lnum == curwin->w_botline
2651#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002652 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#endif
2654 )
2655 scrolled -= curwin->w_empty_rows;
2656 }
2657
2658 if (boff.lnum < curbuf->b_ml.ml_line_count)
2659 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 botline_forw(&boff);
2662 used += boff.height;
2663 if (used > curwin->w_height)
2664 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002665 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2666 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 extra += boff.height;
2669 if (boff.lnum >= curwin->w_botline
2670#ifdef FEAT_DIFF
2671 || (boff.lnum + 1 == curwin->w_botline
2672 && boff.fill > curwin->w_filler_rows)
2673#endif
2674 )
2675 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002676 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 scrolled += boff.height;
2678 if (boff.lnum == curwin->w_botline
2679#ifdef FEAT_DIFF
2680 && boff.fill == 0
2681#endif
2682 )
2683 scrolled -= curwin->w_empty_rows;
2684 }
2685 }
2686 }
2687 }
2688
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (scrolled <= 0)
2691 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 else if (used > curwin->w_height)
2694 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002695 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 else
2697 {
2698 line_count = 0;
2699#ifdef FEAT_DIFF
2700 boff.fill = curwin->w_topfill;
2701#endif
2702 boff.lnum = curwin->w_topline - 1;
2703 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2704 {
2705 botline_forw(&boff);
2706 i += boff.height;
2707 ++line_count;
2708 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002709 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 line_count = 9999;
2711 }
2712
2713 /*
2714 * Scroll up if the cursor is off the bottom of the screen a bit.
2715 * Otherwise put it at 1/2 of the screen.
2716 */
2717 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002718 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002719 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002720 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002721 if (do_sms)
2722 scrollup(scrolled, TRUE); // TODO
2723 else
2724 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
2727 /*
2728 * If topline didn't change we need to restore w_botline and w_empty_rows
2729 * (we changed them).
2730 * If topline did change, update_screen() will set botline.
2731 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002732 if (curwin->w_topline == old_topline
2733 && curwin->w_skipcol == old_skipcol
2734 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 curwin->w_botline = old_botline;
2737 curwin->w_empty_rows = old_empty_rows;
2738 curwin->w_valid = old_valid;
2739 }
2740 curwin->w_valid |= VALID_TOPLINE;
2741}
2742
2743/*
2744 * Recompute topline to put the cursor halfway the window
2745 * If "atend" is TRUE, also put it halfway at the end of the file.
2746 */
2747 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002748scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 int above = 0;
2751 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002752 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#ifdef FEAT_DIFF
2754 int topfill = 0;
2755#endif
2756 int below = 0;
2757 int used;
2758 lineoff_T loff;
2759 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002760#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002761 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002764#ifdef FEAT_PROP_POPUP
2765 // if the width changed this needs to be updated first
2766 may_update_popup_position();
2767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2769#ifdef FEAT_FOLDING
2770 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2771#endif
2772#ifdef FEAT_DIFF
2773 used = plines_nofill(loff.lnum);
2774 loff.fill = 0;
2775 boff.fill = 0;
2776#else
2777 used = plines(loff.lnum);
2778#endif
2779 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002780
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002781 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002782 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2783 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002784 {
2785 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002786 if (atend)
2787 {
2788 want_height = (curwin->w_height - used) / 2;
2789 used = 0;
2790 }
2791 else
2792 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002793 }
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 while (topline > 1)
2796 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002797 // If using smoothscroll, we can precisely scroll to the
2798 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002799 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002800 {
2801 topline_back_winheight(&loff, FALSE);
2802 if (loff.height == MAXCOL)
2803 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002804 used += loff.height;
2805 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002806 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002807 botline_forw(&boff);
2808 used += boff.height;
2809 }
2810 if (used > want_height)
2811 {
2812 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002813 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002814 topline = loff.lnum;
2815#ifdef FEAT_DIFF
2816 topfill = loff.fill;
2817#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002818 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002819 }
2820 break;
2821 }
2822 topline = loff.lnum;
2823#ifdef FEAT_DIFF
2824 topfill = loff.fill;
2825#endif
2826 continue;
2827 }
2828
2829 // If not using smoothscroll, we have to iteratively find how many
2830 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002831 // This may not be right in the middle if the lines'
2832 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002833
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002834 // Depending on "prefer_above" we add a line above or below first.
2835 // Loop twice to avoid duplicating code.
2836 int done = FALSE;
2837 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002839 if (prefer_above ? (round == 2 && below < above)
2840 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002842 // add a line below the cursor
2843 if (boff.lnum < curbuf->b_ml.ml_line_count)
2844 {
2845 botline_forw(&boff);
2846 used += boff.height;
2847 if (used > curwin->w_height)
2848 {
2849 done = TRUE;
2850 break;
2851 }
2852 below += boff.height;
2853 }
2854 else
2855 {
2856 ++below; // count a "~" line
2857 if (atend)
2858 ++used;
2859 }
2860 }
2861
2862 if (prefer_above ? (round == 1 && below >= above)
2863 : (round == 1 && below > above))
2864 {
2865 // add a line above the cursor
2866 topline_back(&loff);
2867 if (loff.height == MAXCOL)
2868 used = MAXCOL;
2869 else
2870 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002872 {
2873 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002875 }
2876 above += loff.height;
2877 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002879 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883 if (done)
2884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887#ifdef FEAT_FOLDING
2888 if (!hasFolding(topline, &curwin->w_topline, NULL))
2889#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002890 {
2891 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002892 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002893 || curwin->w_skipcol != 0)
2894 {
2895 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002896 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002897 {
2898 curwin->w_skipcol = skipcol;
2899 redraw_later(UPD_NOT_VALID);
2900 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002901 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002902 reset_skipcol();
2903 }
2904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905#ifdef FEAT_DIFF
2906 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002907 if (old_topline > curwin->w_topline + curwin->w_height)
2908 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 check_topfill(curwin, FALSE);
2910#endif
2911 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2912 curwin->w_valid |= VALID_TOPLINE;
2913}
2914
2915/*
2916 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002917 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 * If not possible, put it at the same position as scroll_cursor_halfway().
2919 * When called topline must be valid!
2920 */
2921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002922cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 linenr_T botline;
2928 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002929 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002931 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 /*
2934 * How many lines we would like to have above/below the cursor depends on
2935 * whether the first/last line of the file is on screen.
2936 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002937 above_wanted = so;
2938 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002939 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 above_wanted = mouse_dragging - 1;
2942 below_wanted = mouse_dragging - 1;
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (curwin->w_topline == 1)
2945 {
2946 above_wanted = 0;
2947 max_off = curwin->w_height / 2;
2948 if (below_wanted > max_off)
2949 below_wanted = max_off;
2950 }
2951 validate_botline();
2952 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002953 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 below_wanted = 0;
2956 max_off = (curwin->w_height - 1) / 2;
2957 if (above_wanted > max_off)
2958 above_wanted = max_off;
2959 }
2960
2961 /*
2962 * If there are sufficient file-lines above and below the cursor, we can
2963 * return now.
2964 */
2965 cln = curwin->w_cursor.lnum;
2966 if (cln >= curwin->w_topline + above_wanted
2967 && cln < curwin->w_botline - below_wanted
2968#ifdef FEAT_FOLDING
2969 && !hasAnyFolding(curwin)
2970#endif
2971 )
2972 return;
2973
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002974 if (curwin->w_p_sms && !curwin->w_p_wrap)
2975 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002976 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002977 if (curwin->w_cline_height == curwin->w_height)
2978 {
2979 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002980 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002981 return;
2982 }
2983 // TODO: If the cursor line doesn't fit in the window then only adjust
2984 // w_skipcol.
2985 }
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 /*
2988 * Narrow down the area where the cursor can be put by taking lines from
2989 * the top and the bottom until:
2990 * - the desired context lines are found
2991 * - the lines from the top is past the lines from the bottom
2992 */
2993 topline = curwin->w_topline;
2994 botline = curwin->w_botline - 1;
2995#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002996 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 above = curwin->w_topfill;
2998 below = curwin->w_filler_rows;
2999#endif
3000 while ((above < above_wanted || below < below_wanted) && topline < botline)
3001 {
3002 if (below < below_wanted && (below <= above || above >= above_wanted))
3003 {
3004#ifdef FEAT_FOLDING
3005 if (hasFolding(botline, &botline, NULL))
3006 ++below;
3007 else
3008#endif
3009 below += plines(botline);
3010 --botline;
3011 }
3012 if (above < above_wanted && (above < below || below >= below_wanted))
3013 {
3014#ifdef FEAT_FOLDING
3015 if (hasFolding(topline, NULL, &topline))
3016 ++above;
3017 else
3018#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003019 above += PLINES_NOFILL(topline);
3020#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (topline < botline)
3023 above += diff_check_fill(curwin, topline + 1);
3024#endif
3025 ++topline;
3026 }
3027 }
3028 if (topline == botline || botline == 0)
3029 curwin->w_cursor.lnum = topline;
3030 else if (topline > botline)
3031 curwin->w_cursor.lnum = botline;
3032 else
3033 {
3034 if (cln < topline && curwin->w_topline > 1)
3035 {
3036 curwin->w_cursor.lnum = topline;
3037 curwin->w_valid &=
3038 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3039 }
3040 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3041 {
3042 curwin->w_cursor.lnum = botline;
3043 curwin->w_valid &=
3044 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3045 }
3046 }
3047 curwin->w_valid |= VALID_TOPLINE;
3048}
3049
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003050static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
3052/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003053 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3054 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003056 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 */
3058 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003059onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 long n;
3062 int retval = OK;
3063 lineoff_T loff;
3064 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003065 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar85a20022019-12-21 18:25:54 +01003067 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 beep_flush();
3070 return FAIL;
3071 }
3072
3073 for ( ; count > 0; --count)
3074 {
3075 validate_botline();
3076 /*
3077 * It's an error to move a page up when the first line is already on
3078 * the screen. It's an error to move a page down when the last line
3079 * is on the screen and the topline is 'scrolloff' lines from the
3080 * last line.
3081 */
3082 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003083 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3085 : (curwin->w_topline == 1
3086#ifdef FEAT_DIFF
3087 && curwin->w_topfill ==
3088 diff_check_fill(curwin, curwin->w_topline)
3089#endif
3090 ))
3091 {
3092 beep_flush();
3093 retval = FAIL;
3094 break;
3095 }
3096
3097#ifdef FEAT_DIFF
3098 loff.fill = 0;
3099#endif
3100 if (dir == FORWARD)
3101 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003102 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003104 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003105 if (p_window <= 2)
3106 ++curwin->w_topline;
3107 else
3108 curwin->w_topline += p_window - 2;
3109 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3110 curwin->w_topline = curbuf->b_ml.ml_line_count;
3111 curwin->w_cursor.lnum = curwin->w_topline;
3112 }
3113 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3114 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 curwin->w_topline = curbuf->b_ml.ml_line_count;
3117#ifdef FEAT_DIFF
3118 curwin->w_topfill = 0;
3119#endif
3120 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3121 }
3122 else
3123 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003124 // For the overlap, start with the line just below the window
3125 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 loff.lnum = curwin->w_botline;
3127#ifdef FEAT_DIFF
3128 loff.fill = diff_check_fill(curwin, loff.lnum)
3129 - curwin->w_filler_rows;
3130#endif
3131 get_scroll_overlap(&loff, -1);
3132 curwin->w_topline = loff.lnum;
3133#ifdef FEAT_DIFF
3134 curwin->w_topfill = loff.fill;
3135 check_topfill(curwin, FALSE);
3136#endif
3137 curwin->w_cursor.lnum = curwin->w_topline;
3138 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3139 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3140 }
3141 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003142 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144#ifdef FEAT_DIFF
3145 if (curwin->w_topline == 1)
3146 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003147 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 max_topfill();
3149 continue;
3150 }
3151#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003152 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003154 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003155 if (p_window <= 2)
3156 --curwin->w_topline;
3157 else
3158 curwin->w_topline -= p_window - 2;
3159 if (curwin->w_topline < 1)
3160 curwin->w_topline = 1;
3161 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3162 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3163 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3164 continue;
3165 }
3166
Bram Moolenaar85a20022019-12-21 18:25:54 +01003167 // Find the line at the top of the window that is going to be the
3168 // line at the bottom of the window. Make sure this results in
3169 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 loff.lnum = curwin->w_topline - 1;
3171#ifdef FEAT_DIFF
3172 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3173 - curwin->w_topfill;
3174#endif
3175 get_scroll_overlap(&loff, 1);
3176
3177 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3178 {
3179 loff.lnum = curbuf->b_ml.ml_line_count;
3180#ifdef FEAT_DIFF
3181 loff.fill = 0;
3182 }
3183 else
3184 {
3185 botline_topline(&loff);
3186#endif
3187 }
3188 curwin->w_cursor.lnum = loff.lnum;
3189
Bram Moolenaar85a20022019-12-21 18:25:54 +01003190 // Find the line just above the new topline to get the right line
3191 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 n = 0;
3193 while (n <= curwin->w_height && loff.lnum >= 1)
3194 {
3195 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003196 if (loff.height == MAXCOL)
3197 n = MAXCOL;
3198 else
3199 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 curwin->w_topline = 1;
3204#ifdef FEAT_DIFF
3205 max_topfill();
3206#endif
3207 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3208 }
3209 else
3210 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003211 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#ifdef FEAT_DIFF
3213 topline_botline(&loff);
3214#endif
3215 botline_forw(&loff);
3216 botline_forw(&loff);
3217#ifdef FEAT_DIFF
3218 botline_topline(&loff);
3219#endif
3220#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003221 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3223#endif
3224
Bram Moolenaar85a20022019-12-21 18:25:54 +01003225 // Always scroll at least one line. Avoid getting stuck on
3226 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (loff.lnum >= curwin->w_topline
3228#ifdef FEAT_DIFF
3229 && (loff.lnum > curwin->w_topline
3230 || loff.fill >= curwin->w_topfill)
3231#endif
3232 )
3233 {
3234#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 // First try using the maximum number of filler lines. If
3236 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 loff.fill = curwin->w_topfill;
3238 if (curwin->w_topfill < diff_check_fill(curwin,
3239 curwin->w_topline))
3240 max_topfill();
3241 if (curwin->w_topfill == loff.fill)
3242#endif
3243 {
3244 --curwin->w_topline;
3245#ifdef FEAT_DIFF
3246 curwin->w_topfill = 0;
3247#endif
3248 }
3249 comp_botline(curwin);
3250 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003251 curwin->w_valid &=
3252 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else
3255 {
3256 curwin->w_topline = loff.lnum;
3257#ifdef FEAT_DIFF
3258 curwin->w_topfill = loff.fill;
3259 check_topfill(curwin, FALSE);
3260#endif
3261 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3262 }
3263 }
3264 }
3265 }
3266#ifdef FEAT_FOLDING
3267 foldAdjustCursor();
3268#endif
3269 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003270 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003271 if (retval == OK)
3272 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3274
Bram Moolenaar907dad72018-07-10 15:07:15 +02003275 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003277 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3278 // But make sure we scroll at least one line (happens with mix of long
3279 // wrapping lines and non-wrapping line).
3280 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003282 scroll_cursor_top(1, FALSE);
3283 if (curwin->w_topline <= old_topline
3284 && old_topline < curbuf->b_ml.ml_line_count)
3285 {
3286 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003288 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3289#endif
3290 }
3291 }
3292#ifdef FEAT_FOLDING
3293 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003298 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return retval;
3300}
3301
3302/*
3303 * Decide how much overlap to use for page-up or page-down scrolling.
3304 * This is symmetric, so that doing both keeps the same lines displayed.
3305 * Three lines are examined:
3306 *
3307 * before CTRL-F after CTRL-F / before CTRL-B
3308 * etc. l1
3309 * l1 last but one line ------------
3310 * l2 last text line l2 top text line
3311 * ------------- l3 second text line
3312 * l3 etc.
3313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 int h1, h2, h3, h4;
3318 int min_height = curwin->w_height - 2;
3319 lineoff_T loff0, loff1, loff2;
3320
3321#ifdef FEAT_DIFF
3322 if (lp->fill > 0)
3323 lp->height = 1;
3324 else
3325 lp->height = plines_nofill(lp->lnum);
3326#else
3327 lp->height = plines(lp->lnum);
3328#endif
3329 h1 = lp->height;
3330 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003331 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 loff0 = *lp;
3334 if (dir > 0)
3335 botline_forw(lp);
3336 else
3337 topline_back(lp);
3338 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003339 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return;
3343 }
3344
3345 loff1 = *lp;
3346 if (dir > 0)
3347 botline_forw(lp);
3348 else
3349 topline_back(lp);
3350 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003351 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003353 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 return;
3355 }
3356
3357 loff2 = *lp;
3358 if (dir > 0)
3359 botline_forw(lp);
3360 else
3361 topline_back(lp);
3362 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003363 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003364 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369/*
3370 * Scroll 'scroll' lines up or down.
3371 */
3372 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 long scrolled = 0;
3376 int i;
3377 int n;
3378 int room;
3379
3380 if (Prenum)
3381 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3382 curwin->w_height : Prenum;
3383 n = (curwin->w_p_scr <= curwin->w_height) ?
3384 curwin->w_p_scr : curwin->w_height;
3385
Bram Moolenaard5d37532017-03-27 23:02:07 +02003386 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 validate_botline();
3388 room = curwin->w_empty_rows;
3389#ifdef FEAT_DIFF
3390 room += curwin->w_filler_rows;
3391#endif
3392 if (flag)
3393 {
3394 /*
3395 * scroll the text up
3396 */
3397 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3398 {
3399#ifdef FEAT_DIFF
3400 if (curwin->w_topfill > 0)
3401 {
3402 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003403 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 --curwin->w_topfill;
3405 }
3406 else
3407#endif
3408 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003409 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 n -= i;
3411 if (n < 0 && scrolled > 0)
3412 break;
3413#ifdef FEAT_FOLDING
3414 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3415#endif
3416 ++curwin->w_topline;
3417#ifdef FEAT_DIFF
3418 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3419#endif
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3422 {
3423 ++curwin->w_cursor.lnum;
3424 curwin->w_valid &=
3425 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3429 scrolled += i;
3430
3431 /*
3432 * Correct w_botline for changed w_topline.
3433 * Won't work when there are filler lines.
3434 */
3435#ifdef FEAT_DIFF
3436 if (curwin->w_p_diff)
3437 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3438 else
3439#endif
3440 {
3441 room += i;
3442 do
3443 {
3444 i = plines(curwin->w_botline);
3445 if (i > room)
3446 break;
3447#ifdef FEAT_FOLDING
3448 (void)hasFolding(curwin->w_botline, NULL,
3449 &curwin->w_botline);
3450#endif
3451 ++curwin->w_botline;
3452 room -= i;
3453 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3454 }
3455 }
3456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /*
3458 * When hit bottom of the file: move cursor down.
3459 */
3460 if (n > 0)
3461 {
3462# ifdef FEAT_FOLDING
3463 if (hasAnyFolding(curwin))
3464 {
3465 while (--n >= 0
3466 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3467 {
3468 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3469 &curwin->w_cursor.lnum);
3470 ++curwin->w_cursor.lnum;
3471 }
3472 }
3473 else
3474# endif
3475 curwin->w_cursor.lnum += n;
3476 check_cursor_lnum();
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 else
3480 {
3481 /*
3482 * scroll the text down
3483 */
3484 while (n > 0 && curwin->w_topline > 1)
3485 {
3486#ifdef FEAT_DIFF
3487 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3488 {
3489 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003490 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 ++curwin->w_topfill;
3492 }
3493 else
3494#endif
3495 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003496 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 n -= i;
3498 if (n < 0 && scrolled > 0)
3499 break;
3500 --curwin->w_topline;
3501#ifdef FEAT_FOLDING
3502 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3503#endif
3504#ifdef FEAT_DIFF
3505 curwin->w_topfill = 0;
3506#endif
3507 }
3508 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3509 VALID_BOTLINE|VALID_BOTLINE_AP);
3510 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (curwin->w_cursor.lnum > 1)
3512 {
3513 --curwin->w_cursor.lnum;
3514 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /*
3519 * When hit top of the file: move cursor up.
3520 */
3521 if (n > 0)
3522 {
3523 if (curwin->w_cursor.lnum <= (linenr_T)n)
3524 curwin->w_cursor.lnum = 1;
3525 else
3526# ifdef FEAT_FOLDING
3527 if (hasAnyFolding(curwin))
3528 {
3529 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3530 {
3531 --curwin->w_cursor.lnum;
3532 (void)hasFolding(curwin->w_cursor.lnum,
3533 &curwin->w_cursor.lnum, NULL);
3534 }
3535 }
3536 else
3537# endif
3538 curwin->w_cursor.lnum -= n;
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003542 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 foldAdjustCursor();
3544# endif
3545#ifdef FEAT_DIFF
3546 check_topfill(curwin, !flag);
3547#endif
3548 cursor_correct();
3549 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003550 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552
Bram Moolenaar860cae12010-06-05 23:22:07 +02003553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003554do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555{
3556 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003557 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003558 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003559 colnr_T curswant = curwin->w_curswant;
3560 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003561 win_T *old_curwin = curwin;
3562 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 int old_VIsual_select = VIsual_select;
3564 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565
3566 /*
3567 * loop through the cursorbound windows
3568 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003570 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 {
3572 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003573 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003574 if (curwin != old_curwin && curwin->w_p_crb)
3575 {
3576# ifdef FEAT_DIFF
3577 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003578 curwin->w_cursor.lnum =
3579 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003580 else
3581# endif
3582 curwin->w_cursor.lnum = line;
3583 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003584 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003585 curwin->w_curswant = curswant;
3586 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587
Bram Moolenaar85a20022019-12-21 18:25:54 +01003588 // Make sure the cursor is in a valid position. Temporarily set
3589 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003590 int restart_edit_save = restart_edit;
3591 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003593
3594 // Avoid a scroll here for the cursor position, 'scrollbind' is
3595 // more important.
3596 if (!curwin->w_p_scb)
3597 validate_cursor();
3598
Bram Moolenaar61452852011-02-01 18:01:11 +01003599 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003600 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 if (has_mbyte)
3602 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003603 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003604
Bram Moolenaar85a20022019-12-21 18:25:54 +01003605 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003606 if (!curwin->w_p_scb)
3607 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003608 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 }
3610 }
3611
3612 /*
3613 * reset current-window
3614 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003615 VIsual_select = old_VIsual_select;
3616 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 curwin = old_curwin;
3618 curbuf = old_curbuf;
3619}