blob: ea7d38005672483ca1f0222922e969e7e3f0f85e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
Bram Moolenaarf6196f42022-10-02 21:29:55 +010042adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
206 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 int
213sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000214{
215#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100216 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100218 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return 0;
220#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100221 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
222 if (wp->w_p_list && wp->w_lcs_chars.prec)
223 return 1;
224
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000225 return extra2 > 3 ? 0 : 3 - extra2;
226}
227
228/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000229 * Calculates the skipcol offset for window "wp" given how many
230 * physical lines we want to scroll down.
231 */
232 static int
233skipcol_from_plines(win_T *wp, int plines_off)
234{
235 int width1 = wp->w_width - win_col_off(wp);
236
237 int skipcol = 0;
238 if (plines_off > 0)
239 skipcol += width1;
240 if (plines_off > 1)
241 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
242 return skipcol;
243}
244
245/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100246 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247 */
248 static void
249reset_skipcol(void)
250{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000251 if (curwin->w_skipcol == 0)
252 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000253
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000254 curwin->w_skipcol = 0;
255
256 // Should use the least expensive way that displays all that changed.
257 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
258 // enough when the top line gets another screen line.
259 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000260}
261
262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * Update curwin->w_topline and redraw if necessary.
264 * Used to update the screen before printing a message.
265 */
266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100267update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 update_topline();
270 if (must_redraw)
271 update_screen(0);
272}
273
274/*
275 * Update curwin->w_topline to move the cursor onto the screen.
276 */
277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 long line_count;
281 int halfheight;
282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100316 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100347 else if (curwin->w_skipcol > 0
348 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 {
350 colnr_T vcol;
351
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000352 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100353 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100355 int overlap = sms_marker_overlap(curwin, curwin_col_off()
356 - curwin_col_off2());
357 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000399 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 else
401 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 check_botline = TRUE;
404 }
405 }
406
407 else
408 {
409#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100410 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
412#endif
413 check_botline = TRUE;
414 }
415 }
416
417 /*
418 * If the cursor is below the bottom of the window, scroll the window
419 * to put the cursor on the window.
420 * When w_botline is invalid, recompute it first, to avoid a redraw later.
421 * If w_botline was approximated, we might need a redraw later in a few
422 * cases, but we don't want to spend (a lot of) time recomputing w_botline
423 * for every small change.
424 */
425 if (check_botline)
426 {
427 if (!(curwin->w_valid & VALID_BOTLINE_AP))
428 validate_botline();
429
430 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
431 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 if (curwin->w_cursor.lnum < curwin->w_botline)
433 {
434 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100435 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_FOLDING
437 || hasAnyFolding(curwin)
438#endif
439 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lineoff_T loff;
442
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // Cursor is (a few lines) above botline, check if there are
444 // 'scrolloff' window lines below the cursor. If not, need to
445 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 n = curwin->w_empty_rows;
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
451#endif
452#ifdef FEAT_DIFF
453 loff.fill = 0;
454 n += curwin->w_filler_rows;
455#endif
456 loff.height = 0;
457 while (loff.lnum < curwin->w_botline
458#ifdef FEAT_DIFF
459 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
460#endif
461 )
462 {
463 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 botline_forw(&loff);
467 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100468 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 }
472 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000474 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 if (check_botline)
477 {
478#ifdef FEAT_FOLDING
479 if (hasAnyFolding(curwin))
480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // Count the number of logical lines between the cursor and
482 // botline - scrolloff (approximation of how much will be
483 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 line_count = 0;
485 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (lnum <= 0 || line_count > curwin->w_height + 1)
491 break;
492 (void)hasFolding(lnum, &lnum, NULL);
493 }
494 }
495 else
496#endif
497 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100498 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000502 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
504 }
505 }
506 curwin->w_valid |= VALID_TOPLINE;
507
508 /*
509 * Need to redraw when topline changed.
510 */
511 if (curwin->w_topline != old_topline
512#ifdef FEAT_DIFF
513 || curwin->w_topfill != old_topfill
514#endif
515 )
516 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100517 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000518 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100519
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100520 // When 'smoothscroll' is not set, should reset w_skipcol.
521 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100522 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100523 else if (curwin->w_skipcol != 0)
524 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000525
Bram Moolenaar85a20022019-12-21 18:25:54 +0100526 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (curwin->w_cursor.lnum == curwin->w_topline)
528 validate_cursor();
529 }
530
Bram Moolenaar375e3392019-01-31 18:26:10 +0100531 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
533
534/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000535 * Return the scrolljump value to use for the current window.
536 * When 'scrolljump' is positive use it as-is.
537 * When 'scrolljump' is negative use it as a percentage of the window height.
538 */
539 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000541{
542 if (p_sj >= 0)
543 return (int)p_sj;
544 return (curwin->w_height * -p_sj) / 100;
545}
546
547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
549 * current window.
550 */
551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 lineoff_T loff;
555 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100556 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar375e3392019-01-31 18:26:10 +0100558 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_FOLDING
560 || hasAnyFolding(curwin)
561#endif
562 )
563 {
564 loff.lnum = curwin->w_cursor.lnum;
565#ifdef FEAT_DIFF
566 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100567 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#else
569 n = 0;
570#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100571 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100572 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100575 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (loff.lnum < curwin->w_topline
577#ifdef FEAT_DIFF
578 || (loff.lnum == curwin->w_topline && loff.fill > 0)
579#endif
580 )
581 break;
582 n += loff.height;
583 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100584 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 return TRUE;
586 }
587 return FALSE;
588}
589
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100590/*
591 * Update w_curswant.
592 */
593 void
594update_curswant_force(void)
595{
596 validate_virtcol();
597 curwin->w_curswant = curwin->w_virtcol
598#ifdef FEAT_PROP_POPUP
599 - curwin->w_virtcol_first_char
600#endif
601 ;
602 curwin->w_set_curswant = FALSE;
603}
604
605/*
606 * Update w_curswant if w_set_curswant is set.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100612 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
615/*
616 * Check if the cursor has moved. Set the w_valid flag accordingly.
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
622 {
623 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100624 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
625 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 wp->w_valid_cursor = wp->w_cursor;
627 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100628 wp->w_valid_skipcol = wp->w_skipcol;
629 }
630 else if (wp->w_skipcol != wp->w_valid_skipcol)
631 {
632 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
633 |VALID_CHEIGHT|VALID_CROW
634 |VALID_BOTLINE|VALID_BOTLINE_AP);
635 wp->w_valid_cursor = wp->w_cursor;
636 wp->w_valid_leftcol = wp->w_leftcol;
637 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639 else if (wp->w_cursor.col != wp->w_valid_cursor.col
640 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100641 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
644 wp->w_valid_cursor.col = wp->w_cursor.col;
645 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648}
649
650/*
651 * Call this function when some window settings have changed, which require
652 * the cursor position, botline and topline to be recomputed and the window to
653 * be redrawn. E.g, when changing the 'wrap' option or folding.
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 changed_window_setting_win(curwin);
659}
660
661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 wp->w_lines_valid = 0;
665 changed_line_abv_curs_win(wp);
666 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100667 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668}
669
Dominique Pellee764d1b2023-03-12 21:20:59 +0000670#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000672 * Call changed_window_setting_win() for every window containing "buf".
673 */
674 void
675changed_window_setting_buf(buf_T *buf)
676{
677 tabpage_T *tp;
678 win_T *wp;
679
680 FOR_ALL_TAB_WINDOWS(tp, wp)
681 if (wp->w_buffer == buf)
682 changed_window_setting_win(wp);
683}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000684#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000685
686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * Set wp->w_topline to a certain number.
688 */
689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100690set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200692#ifdef FEAT_DIFF
693 linenr_T prev_topline = wp->w_topline;
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
699#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100700 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100702 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
703 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000705 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200707 if (lnum != prev_topline)
708 // Keep the filler lines when the topline didn't change.
709 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
711 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100713 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715
716/*
717 * Call this function when the length of the cursor line (in screen
718 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100719 * If the line length changed the number of screen lines might change,
720 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 * Need to take care of w_botline separately!
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaar85090142023-06-01 19:27:08 +0100726 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 |VALID_CHEIGHT|VALID_TOPLINE);
728}
729
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar85090142023-06-01 19:27:08 +0100733 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 |VALID_CHEIGHT|VALID_TOPLINE);
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Call this function when the length of a line (in screen characters) above
739 * the cursor have changed.
740 * Need to take care of w_botline separately!
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
746 |VALID_CHEIGHT|VALID_TOPLINE);
747}
748
749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
753 |VALID_CHEIGHT|VALID_TOPLINE);
754}
755
Dominique Pellee764d1b2023-03-12 21:20:59 +0000756#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100758 * Display of line has changed for "buf", invalidate cursor position and
759 * w_botline.
760 */
761 void
762changed_line_display_buf(buf_T *buf)
763{
764 win_T *wp;
765
766 FOR_ALL_WINDOWS(wp)
767 if (wp->w_buffer == buf)
768 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
769 |VALID_CROW|VALID_CHEIGHT
770 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
771}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000772#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100773
774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * Make sure the value of curwin->w_botline is valid.
776 */
777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100778validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100780 validate_botline_win(curwin);
781}
782
783/*
784 * Make sure the value of wp->w_botline is valid.
785 */
786 void
787validate_botline_win(win_T *wp)
788{
789 if (!(wp->w_valid & VALID_BOTLINE))
790 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * Mark curwin->w_botline as invalid (because of some change in the buffer).
795 */
796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100797invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
799 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
800}
801
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809approximate_botline_win(
810 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 wp->w_valid &= ~VALID_BOTLINE;
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
817 */
818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 check_cursor_moved(curwin);
822 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
823 (VALID_WROW|VALID_WCOL));
824}
825
826/*
827 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
828 * w_topline must be valid, you may need to call update_topline() first!
829 */
830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100831validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100833 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 check_cursor_moved(curwin);
835 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
836 curs_columns(TRUE);
837}
838
839#if defined(FEAT_GUI) || defined(PROTO)
840/*
841 * validate w_cline_row.
842 */
843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 /*
847 * First make sure that w_topline is valid (after moving the cursor).
848 */
849 update_topline();
850 check_cursor_moved(curwin);
851 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100852 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854#endif
855
856/*
857 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200858 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100861curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 linenr_T lnum;
864 int i;
865 int all_invalid;
866 int valid;
867#ifdef FEAT_FOLDING
868 long fold_count;
869#endif
870
Bram Moolenaar85a20022019-12-21 18:25:54 +0100871 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 all_invalid = (!redrawing()
873 || wp->w_lines_valid == 0
874 || wp->w_lines[0].wl_lnum > wp->w_topline);
875 i = 0;
876 wp->w_cline_row = 0;
877 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
878 {
879 valid = FALSE;
880 if (!all_invalid && i < wp->w_lines_valid)
881 {
882 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100883 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (wp->w_lines[i].wl_lnum == lnum)
885 {
886#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 // Check for newly inserted lines below this row, in which
888 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (!wp->w_buffer->b_mod_set
890 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
891 || wp->w_buffer->b_mod_top
892 > wp->w_lines[i].wl_lastlnum + 1)
893#endif
894 valid = TRUE;
895 }
896 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100897 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100900 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100902 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100904 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906#ifdef FEAT_FOLDING
907 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (lnum > wp->w_cursor.lnum)
910 break;
911#else
912 ++lnum;
913#endif
914 wp->w_cline_row += wp->w_lines[i].wl_size;
915 }
916 else
917 {
918#ifdef FEAT_FOLDING
919 fold_count = foldedCount(wp, lnum, NULL);
920 if (fold_count)
921 {
922 lnum += fold_count;
923 if (lnum > wp->w_cursor.lnum)
924 break;
925 ++wp->w_cline_row;
926 }
927 else
928#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100929 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100930 wp->w_cline_row += plines_correct_topline(wp, lnum);
931 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 }
935
936 check_cursor_moved(wp);
937 if (!(wp->w_valid & VALID_CHEIGHT))
938 {
939 if (all_invalid
940 || i == wp->w_lines_valid
941 || (i < wp->w_lines_valid
942 && (!wp->w_lines[i].wl_valid
943 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
944 {
945#ifdef FEAT_DIFF
946 if (wp->w_cursor.lnum == wp->w_topline)
947 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
948 TRUE) + wp->w_topfill;
949 else
950#endif
951 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
952#ifdef FEAT_FOLDING
953 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
954 NULL, NULL, TRUE, NULL);
955#endif
956 }
957 else if (i > wp->w_lines_valid)
958 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 wp->w_cline_height = 0;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
963 NULL, NULL, TRUE, NULL);
964#endif
965 }
966 else
967 {
968 wp->w_cline_height = wp->w_lines[i].wl_size;
969#ifdef FEAT_FOLDING
970 wp->w_cline_folded = wp->w_lines[i].wl_folded;
971#endif
972 }
973 }
974
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100975 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Validate curwin->w_virtcol only.
981 */
982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100983validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 validate_virtcol_win(curwin);
986}
987
988/*
989 * Validate wp->w_virtcol only.
990 */
991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100992validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000995
996 if (wp->w_valid & VALID_VIRTCOL)
997 return;
998
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100999#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001000 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001001#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001002 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001003#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001004 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001005#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001006 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/*
1010 * Validate curwin->w_cline_height only.
1011 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001012 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001013validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016
1017 if (curwin->w_valid & VALID_CHEIGHT)
1018 return;
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001021 if (curwin->w_cursor.lnum == curwin->w_topline)
1022 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1023 + curwin->w_topfill;
1024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001026 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001028 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001030 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001034 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 colnr_T off;
1040 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001041 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001045 if (curwin->w_valid & VALID_WCOL)
1046 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001047
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001048 col = curwin->w_virtcol;
1049 off = curwin_col_off();
1050 col += off;
1051 width = curwin->w_width - off + curwin_col_off2();
1052
1053 // long line wrapping, adjust curwin->w_wrow
1054 if (curwin->w_p_wrap
1055 && col >= (colnr_T)curwin->w_width
1056 && width > 0)
1057 // use same formula as what is used in curs_columns()
1058 col -= ((col - curwin->w_width) / width + 1) * width;
1059 if (col > (int)curwin->w_leftcol)
1060 col -= curwin->w_leftcol;
1061 else
1062 col = 0;
1063 curwin->w_wcol = col;
1064
1065 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001066#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001067 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001072 * Compute offset of a window, occupied by absolute or relative line number,
1073 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar64486672010-05-16 15:46:46 +02001078 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#ifdef FEAT_FOLDING
1081 + wp->w_p_fdc
1082#endif
1083#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001084 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#endif
1086 );
1087}
1088
1089 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 return win_col_off(curwin);
1093}
1094
1095/*
1096 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001097 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1098 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar64486672010-05-16 15:46:46 +02001103 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001104 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 return 0;
1106}
1107
1108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001109curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 return win_col_off2(curwin);
1112}
1113
1114/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001115 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * Also updates curwin->w_wrow and curwin->w_cline_row.
1117 * Also updates curwin->w_leftcol.
1118 */
1119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001120curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001121 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int off_left, off_right;
1126 int n;
1127 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001128 int width1; // text width for first screen line
1129 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int new_leftcol;
1131 colnr_T startcol;
1132 colnr_T endcol;
1133 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001134 long so = get_scrolloff_value();
1135 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001136 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 /*
1139 * First make sure that w_topline is valid (after moving the cursor).
1140 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001141 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 /*
1144 * Next make sure that w_cline_row is valid.
1145 */
1146 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001147 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001149#ifdef FEAT_PROP_POPUP
1150 // will be set by getvvcol() but not reset
1151 curwin->w_virtcol_first_char = 0;
1152#endif
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /*
1155 * Compute the number of virtual columns.
1156 */
1157#ifdef FEAT_FOLDING
1158 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1161 else
1162#endif
1163 getvvcol(curwin, &curwin->w_cursor,
1164 &startcol, &(curwin->w_virtcol), &endcol);
1165
Bram Moolenaar85a20022019-12-21 18:25:54 +01001166 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001168 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170 extra = curwin_col_off();
1171 curwin->w_wcol = curwin->w_virtcol + extra;
1172 endcol += extra;
1173
1174 /*
1175 * Now compute w_wrow, counting screen lines from w_cline_row.
1176 */
1177 curwin->w_wrow = curwin->w_cline_row;
1178
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001179 width1 = curwin->w_width - extra;
1180 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001183 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001184 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001185 if (curwin->w_p_wrap)
1186 curwin->w_wrow = curwin->w_height - 1;
1187 else
1188 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001190 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001194 // skip columns that are not visible
1195 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001197 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001198 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199 // Deduct by multiples of width2. This allows the long line
1200 // wrapping formula below to correctly calculate the w_wcol value
1201 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001202 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001203 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001204 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001205 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001206 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001207
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001208 did_sub_skipcol = TRUE;
1209 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001212 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001215 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1216 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 curwin->w_wrow += n;
1218
1219#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001220 // When cursor wraps to first char of next line in Insert
1221 // mode, the 'showbreak' string isn't shown, backup to first
1222 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001223 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001224 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001225 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 curwin->w_wcol = 0;
1227#endif
1228 }
1229 }
1230
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1232 // is not folded.
1233 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001234 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_FOLDING
1236 && !curwin->w_cline_folded
1237#endif
1238 )
1239 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001240#ifdef FEAT_PROP_POPUP
1241 if (curwin->w_virtcol_first_char > 0)
1242 {
1243 int cols = (curwin->w_width - extra);
1244 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1245
1246 // each "above" text prop shifts the text one row down
1247 curwin->w_wrow += rows;
1248 curwin->w_wcol -= rows * cols;
1249 endcol -= rows * cols;
1250 curwin->w_cline_height = rows + 1;
1251 }
1252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 /*
1254 * If Cursor is left of the screen, scroll rightwards.
1255 * If Cursor is right of the screen, scroll leftwards
1256 * If we get closer to the edge than 'sidescrolloff', scroll a little
1257 * extra
1258 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001259 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001260 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001261 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (off_left < 0 || off_right > 0)
1263 {
1264 if (off_left < 0)
1265 diff = -off_left;
1266 else
1267 diff = off_right;
1268
Bram Moolenaar85a20022019-12-21 18:25:54 +01001269 // When far off or not enough room on either side, put cursor in
1270 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001271 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1272 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 if (diff < p_ss)
1276 diff = p_ss;
1277 if (off_left < 0)
1278 new_leftcol = curwin->w_leftcol - diff;
1279 else
1280 new_leftcol = curwin->w_leftcol + diff;
1281 }
1282 if (new_leftcol < 0)
1283 new_leftcol = 0;
1284 if (new_leftcol != (int)curwin->w_leftcol)
1285 {
1286 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001288 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
1291 curwin->w_wcol -= curwin->w_leftcol;
1292 }
1293 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1294 curwin->w_wcol -= curwin->w_leftcol;
1295 else
1296 curwin->w_wcol = 0;
1297
1298#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001299 // Skip over filler lines. At the top use w_topfill, there
1300 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (curwin->w_cursor.lnum == curwin->w_topline)
1302 curwin->w_wrow += curwin->w_topfill;
1303 else
1304 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1305#endif
1306
1307 prev_skipcol = curwin->w_skipcol;
1308
1309 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if ((curwin->w_wrow >= curwin->w_height
1312 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001313 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && (p_lines =
1315#ifdef FEAT_DIFF
1316 plines_win_nofill
1317#else
1318 plines_win
1319#endif
1320 (curwin, curwin->w_cursor.lnum, FALSE))
1321 - 1 >= curwin->w_height))
1322 && curwin->w_height != 0
1323 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001324 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001325 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 // Cursor past end of screen. Happens with a single line that does
1328 // not fit on screen. Find a skipcol to show the text around the
1329 // cursor. Avoid scrolling all the time. compute value of "extra":
1330 // 1: Less than 'scrolloff' lines above
1331 // 2: Less than 'scrolloff' lines below
1332 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001334 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 // Compute last display line of the buffer line that we want at the
1337 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (p_lines == 0)
1339 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1340 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001341 if (p_lines > curwin->w_wrow + so)
1342 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001345 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 extra += 2;
1347
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001348 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (n > curwin->w_height / 2)
1353 n -= curwin->w_height / 2;
1354 else
1355 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001356 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (n > p_lines - curwin->w_height + 1)
1358 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001359 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else if (extra == 1)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1365 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (extra > 0)
1367 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1369 extra = curwin->w_skipcol / width2;
1370 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 }
1373 else if (extra == 2)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (endcol > curwin->w_skipcol)
1380 curwin->w_skipcol = endcol;
1381 }
1382
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001383 // adjust w_wrow for the changed w_skipcol
1384 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001386 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001387 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (curwin->w_wrow >= curwin->w_height)
1390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001393 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 curwin->w_wrow -= extra;
1395 }
1396
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001397 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (extra > 0)
1399 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1400 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001401 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001403 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_skipcol = 0;
1405 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001406 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001408#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001409 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001410#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001411#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1412 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1413 {
1414 curwin->w_wrow += popup_top_extra(curwin);
1415 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001416 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001417 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001418 else
1419 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001420#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001421
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001422 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1423 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001424 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001425 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1428}
1429
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001430#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001431/*
1432 * Compute the screen position of text character at "pos" in window "wp"
1433 * The resulting values are one-based, zero when character is not visible.
1434 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001435 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436textpos2screenpos(
1437 win_T *wp,
1438 pos_T *pos,
1439 int *rowp, // screen row
1440 int *scolp, // start screen column
1441 int *ccolp, // cursor screen column
1442 int *ecolp) // end screen column
1443{
1444 colnr_T scol = 0, ccol = 0, ecol = 0;
1445 int row = 0;
1446 int rowoff = 0;
1447 colnr_T coloff = 0;
1448
Bram Moolenaar189663b2021-07-21 18:04:56 +02001449 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 colnr_T col;
1452 int width;
1453 linenr_T lnum = pos->lnum;
1454#ifdef FEAT_FOLDING
1455 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1458#endif
1459 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001460
1461#ifdef FEAT_DIFF
1462 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001463 row += lnum == wp->w_topline ? wp->w_topfill
1464 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001465#endif
1466
zeertzjqba2d1912022-12-18 12:28:59 +00001467 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468#ifdef FEAT_FOLDING
1469 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001471 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001472 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001473 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001474 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001475#endif
1476 {
1477 getvcol(wp, pos, &scol, &ccol, &ecol);
1478
1479 // similar to what is done in validate_cursor_col()
1480 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001481 col += off;
1482 width = wp->w_width - off + win_col_off2(wp);
1483
zeertzjq55daae32023-06-04 19:29:22 +01001484 if (lnum == wp->w_topline)
zeertzjqf0e68c02023-06-03 17:11:47 +01001485 col -= wp->w_skipcol;
1486
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001487 // long line wrapping, adjust row
1488 if (wp->w_p_wrap
1489 && col >= (colnr_T)wp->w_width
1490 && width > 0)
1491 {
1492 // use same formula as what is used in curs_columns()
1493 rowoff = ((col - wp->w_width) / width + 1);
1494 col -= rowoff * width;
1495 }
1496 col -= wp->w_leftcol;
1497 if (col >= wp->w_width)
1498 col = -1;
1499 if (col >= 0 && row + rowoff <= wp->w_height)
1500 {
1501 coloff = col - scol + wp->w_wincol + 1;
1502 row += W_WINROW(wp);
1503 }
1504 else
1505 // character is left, right or below of the window
1506 row = rowoff = scol = ccol = ecol = 0;
1507 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001509 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510 *scolp = scol + coloff;
1511 *ccolp = ccol + coloff;
1512 *ecolp = ecol + coloff;
1513}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001514#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515
Bram Moolenaar12034e22019-08-25 22:25:02 +02001516#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001517/*
1518 * "screenpos({winid}, {lnum}, {col})" function
1519 */
1520 void
1521f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1522{
1523 dict_T *dict;
1524 win_T *wp;
1525 pos_T pos;
1526 int row = 0;
1527 int scol = 0, ccol = 0, ecol = 0;
1528
Bram Moolenaar93a10962022-06-16 11:42:09 +01001529 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530 return;
1531 dict = rettv->vval.v_dict;
1532
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001533 if (in_vim9script()
1534 && (check_for_number_arg(argvars, 0) == FAIL
1535 || check_for_number_arg(argvars, 1) == FAIL
1536 || check_for_number_arg(argvars, 2) == FAIL))
1537 return;
1538
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp == NULL)
1541 return;
1542
1543 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001544 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1545 {
1546 semsg(_(e_invalid_line_number_nr), pos.lnum);
1547 return;
1548 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001549 pos.col = tv_get_number(&argvars[2]) - 1;
1550 pos.coladd = 0;
1551 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1552
1553 dict_add_number(dict, "row", row);
1554 dict_add_number(dict, "col", scol);
1555 dict_add_number(dict, "curscol", ccol);
1556 dict_add_number(dict, "endcol", ecol);
1557}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001558
1559/*
1560 * "virtcol2col({winid}, {lnum}, {col})" function
1561 */
1562 void
1563f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1564{
1565 win_T *wp;
1566 linenr_T lnum;
1567 int screencol;
1568 int error = FALSE;
1569
1570 rettv->vval.v_number = -1;
1571
1572 if (check_for_number_arg(argvars, 0) == FAIL
1573 || check_for_number_arg(argvars, 1) == FAIL
1574 || check_for_number_arg(argvars, 2) == FAIL)
1575 return;
1576
1577 wp = find_win_by_nr_or_id(&argvars[0]);
1578 if (wp == NULL)
1579 return;
1580
1581 lnum = tv_get_number_chk(&argvars[1], &error);
1582 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1583 return;
1584
1585 screencol = tv_get_number_chk(&argvars[2], &error);
1586 if (error || screencol < 0)
1587 return;
1588
1589 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1590}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001591#endif
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
1594 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001597scrolldown(
1598 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001599 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int wrow;
1603 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001604 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001605 int width1 = 0;
1606 int width2 = 0;
1607
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001608 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001609 {
1610 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001611 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614#ifdef FEAT_FOLDING
1615 linenr_T first;
1616
Bram Moolenaar85a20022019-12-21 18:25:54 +01001617 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1619#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001620 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001621 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001624 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1625 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 ++curwin->w_topfill;
1628 ++done;
1629 }
1630 else
1631#endif
1632 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001633 // break when at the very top
1634 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001635 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001637 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001639 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001640 if (curwin->w_skipcol >= width1 + width2)
1641 curwin->w_skipcol -= width2;
1642 else
1643 curwin->w_skipcol -= width1;
1644 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001648 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001649 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650 --curwin->w_topline;
1651 curwin->w_skipcol = 0;
1652#ifdef FEAT_DIFF
1653 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001655#ifdef FEAT_FOLDING
1656 // A sequence of folded lines only counts for one logical line
1657 if (hasFolding(curwin->w_topline, &first, NULL))
1658 {
1659 ++done;
1660 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001661 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001662 curwin->w_botline -= curwin->w_topline - first;
1663 curwin->w_topline = first;
1664 }
1665 else
1666#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001667 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001668 {
1669 int size = win_linetabsize(curwin, curwin->w_topline,
1670 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1671 if (size > width1)
1672 {
1673 curwin->w_skipcol = width1;
1674 size -= width1;
1675 redraw_later(UPD_NOT_VALID);
1676 }
1677 while (size > width2)
1678 {
1679 curwin->w_skipcol += width2;
1680 size -= width2;
1681 }
1682 ++done;
1683 }
1684 else
1685 done += PLINES_NOFILL(curwin->w_topline);
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 invalidate_botline();
1690 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 curwin->w_wrow += done; // keep w_wrow updated
1692 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694#ifdef FEAT_DIFF
1695 if (curwin->w_cursor.lnum == curwin->w_topline)
1696 curwin->w_cline_row = 0;
1697 check_topfill(curwin, TRUE);
1698#endif
1699
1700 /*
1701 * Compute the row number of the last row of the cursor line
1702 * and move the cursor onto the displayed part of the window.
1703 */
1704 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001705 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 validate_virtcol();
1708 validate_cheight();
1709 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001710 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
1712 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1713 {
1714#ifdef FEAT_FOLDING
1715 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1716 {
1717 --wrow;
1718 if (first == 1)
1719 curwin->w_cursor.lnum = 1;
1720 else
1721 curwin->w_cursor.lnum = first - 1;
1722 }
1723 else
1724#endif
1725 wrow -= plines(curwin->w_cursor.lnum--);
1726 curwin->w_valid &=
1727 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1728 moved = TRUE;
1729 }
1730 if (moved)
1731 {
1732#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001733 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 foldAdjustCursor();
1735#endif
1736 coladvance(curwin->w_curswant);
1737 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001738
1739 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1740 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001741 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001742 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1743
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001744 // make sure the cursor is in the visible text
1745 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001746 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001747 int row = 0;
1748 if (col >= width1)
1749 {
1750 col -= width1;
1751 ++row;
1752 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001753 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001754 {
1755 row += col / width2;
1756 col = col % width2;
1757 }
1758 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001759 {
1760 curwin->w_curswant = curwin->w_virtcol
1761 - (row - curwin->w_height + 1) * width2;
1762 coladvance(curwin->w_curswant);
1763 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765}
1766
1767/*
1768 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771scrollup(
1772 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001775 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001777 if (do_sms
1778# ifdef FEAT_FOLDING
1779 || (byfold && hasAnyFolding(curwin))
1780# endif
1781# ifdef FEAT_DIFF
1782 || (curwin->w_p_diff && !curwin->w_p_wrap)
1783# endif
1784 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001786 int width1 = curwin->w_width - curwin_col_off();
1787 int width2 = width1 + curwin_col_off2();
1788 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001789 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001790
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001791 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001792 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001793
1794 // diff mode: first consume "topfill"
1795 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1796 // the line, then advance to the next line.
1797 // folding: count each sequence of folded lines as one logical line.
1798 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800# ifdef FEAT_DIFF
1801 if (curwin->w_topfill > 0)
1802 --curwin->w_topfill;
1803 else
1804# endif
1805 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001806 linenr_T lnum = curwin->w_topline;
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808# ifdef FEAT_FOLDING
1809 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001810 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 (void)hasFolding(lnum, NULL, &lnum);
1812# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001813 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001814 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001815 // 'smoothscroll': increase "w_skipcol" until it goes over
1816 // the end of the line, then advance to the next line.
1817 int add = curwin->w_skipcol > 0 ? width2 : width1;
1818 curwin->w_skipcol += add;
1819 if (curwin->w_skipcol >= size)
1820 {
1821 if (lnum == curbuf->b_ml.ml_line_count)
1822 {
1823 // at the last screen line, can't scroll further
1824 curwin->w_skipcol -= add;
1825 break;
1826 }
1827 ++lnum;
1828 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001829 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001830 else
1831 {
1832 if (lnum >= curbuf->b_ml.ml_line_count)
1833 break;
1834 ++lnum;
1835 }
1836
1837 if (lnum > curwin->w_topline)
1838 {
1839 // approximate w_botline
1840 curwin->w_botline += lnum - curwin->w_topline;
1841 curwin->w_topline = lnum;
1842# ifdef FEAT_DIFF
1843 curwin->w_topfill = diff_check_fill(curwin, lnum);
1844# endif
1845 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001846 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001847 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001848 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001849 }
1850 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001851
zeertzjqd9a92dc2023-06-05 18:41:35 +01001852 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1853 // need to redraw more, because wl_size of the (new) topline may
1854 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001855 redraw_later(UPD_NOT_VALID);
1856 }
1857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001860 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862
1863 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1864 curwin->w_topline = curbuf->b_ml.ml_line_count;
1865 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1866 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1867
1868#ifdef FEAT_DIFF
1869 check_topfill(curwin, FALSE);
1870#endif
1871
1872#ifdef FEAT_FOLDING
1873 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001874 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1876#endif
1877
1878 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1879 if (curwin->w_cursor.lnum < curwin->w_topline)
1880 {
1881 curwin->w_cursor.lnum = curwin->w_topline;
1882 curwin->w_valid &=
1883 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1884 coladvance(curwin->w_curswant);
1885 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001886 if (curwin->w_cursor.lnum == curwin->w_topline
1887 && do_sms && curwin->w_skipcol > 0)
1888 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001889 int col_off = curwin_col_off();
1890 int col_off2 = curwin_col_off2();
1891
1892 int width1 = curwin->w_width - col_off;
1893 int width2 = width1 + col_off2;
1894 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001895 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001896 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001897 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001898
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001899 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001900 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001901 int overlap = scrolloff_cols != 0 ? 0
1902 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001903
Bram Moolenaar118c2352022-10-09 17:19:27 +01001904 // Make sure the cursor is in a visible part of the line, taking
1905 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001906 // If there are not enough screen lines put the cursor in the middle.
1907 if (scrolloff_cols > space_cols / 2)
1908 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001909 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001910 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001912 colnr_T col = curwin->w_virtcol;
1913
1914 if (col < width1)
1915 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001916 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001917 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001918 curwin->w_curswant = col;
1919 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001920
1921 // validate_virtcol() marked various things as valid, but after
1922 // moving the cursor they need to be recomputed
1923 curwin->w_valid &=
1924 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001925 }
1926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001929/*
1930 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1931 * valid for 'smoothscroll'.
1932 */
1933 void
1934adjust_skipcol(void)
1935{
1936 if (!curwin->w_p_wrap
1937 || !curwin->w_p_sms
1938 || curwin->w_cursor.lnum != curwin->w_topline)
1939 return;
1940
1941 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001942 if (width1 <= 0)
1943 return; // no text will be displayed
1944
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001945 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001946 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001947 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1948 int scrolled = FALSE;
1949
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001950 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001951 if (curwin->w_cline_height == curwin->w_height
1952 // w_cline_height may be capped at w_height, check there aren't
1953 // actually more lines.
1954 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1955 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001956 {
1957 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001958 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001959 return;
1960 }
1961
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001962 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001963 int overlap = sms_marker_overlap(curwin,
1964 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001965 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001966 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001967 {
1968 // scroll a screen line down
1969 if (curwin->w_skipcol >= width1 + width2)
1970 curwin->w_skipcol -= width2;
1971 else
1972 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001973 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001974 }
1975 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001976 {
1977 validate_virtcol();
1978 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001979 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001980 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001981
1982 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1983 int row = 0;
1984 if (col >= width1)
1985 {
1986 col -= width1;
1987 ++row;
1988 }
1989 if (col > width2)
1990 {
1991 row += col / width2;
1992 col = col % width2;
1993 }
1994 if (row >= curwin->w_height)
1995 {
1996 if (curwin->w_skipcol == 0)
1997 {
1998 curwin->w_skipcol += width1;
1999 --row;
2000 }
2001 if (row >= curwin->w_height)
2002 curwin->w_skipcol += (row - curwin->w_height) * width2;
2003 redraw_later(UPD_NOT_VALID);
2004 }
2005}
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#ifdef FEAT_DIFF
2008/*
2009 * Don't end up with too many filler lines in the window.
2010 */
2011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012check_topfill(
2013 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002014 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 int n;
2017
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002018 if (wp->w_topfill <= 0)
2019 return;
2020
2021 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2022 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002024 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002026 --wp->w_topline;
2027 wp->w_topfill = 0;
2028 }
2029 else
2030 {
2031 wp->w_topfill = wp->w_height - n;
2032 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 }
2036}
2037
2038/*
2039 * Use as many filler lines as possible for w_topline. Make sure w_topline
2040 * is still visible.
2041 */
2042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int n;
2046
2047 n = plines_nofill(curwin->w_topline);
2048 if (n >= curwin->w_height)
2049 curwin->w_topfill = 0;
2050 else
2051 {
2052 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2053 if (curwin->w_topfill + n > curwin->w_height)
2054 curwin->w_topfill = curwin->w_height - n;
2055 }
2056}
2057#endif
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059/*
2060 * Scroll the screen one line down, but don't do it if it would move the
2061 * cursor off the screen.
2062 */
2063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 int end_row;
2067#ifdef FEAT_DIFF
2068 int can_fill = (curwin->w_topfill
2069 < diff_check_fill(curwin, curwin->w_topline));
2070#endif
2071
2072 if (curwin->w_topline <= 1
2073#ifdef FEAT_DIFF
2074 && !can_fill
2075#endif
2076 )
2077 return;
2078
Bram Moolenaar85a20022019-12-21 18:25:54 +01002079 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 /*
2082 * Compute the row number of the last row of the cursor line
2083 * and make sure it doesn't go off the screen. Make sure the cursor
2084 * doesn't go past 'scrolloff' lines from the screen end.
2085 */
2086 end_row = curwin->w_wrow;
2087#ifdef FEAT_DIFF
2088 if (can_fill)
2089 ++end_row;
2090 else
2091 end_row += plines_nofill(curwin->w_topline - 1);
2092#else
2093 end_row += plines(curwin->w_topline - 1);
2094#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002095 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 validate_cheight();
2098 validate_virtcol();
2099 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002100 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002102 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104#ifdef FEAT_DIFF
2105 if (can_fill)
2106 {
2107 ++curwin->w_topfill;
2108 check_topfill(curwin, TRUE);
2109 }
2110 else
2111 {
2112 --curwin->w_topline;
2113 curwin->w_topfill = 0;
2114 }
2115#else
2116 --curwin->w_topline;
2117#endif
2118#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002119 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002121 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2123 }
2124}
2125
2126/*
2127 * Scroll the screen one line up, but don't do it if it would move the cursor
2128 * off the screen.
2129 */
2130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 int start_row;
2134
2135 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2136#ifdef FEAT_DIFF
2137 && curwin->w_topfill == 0
2138#endif
2139 )
2140 return;
2141
Bram Moolenaar85a20022019-12-21 18:25:54 +01002142 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144 /*
2145 * Compute the row number of the first row of the cursor line
2146 * and make sure it doesn't go off the screen. Make sure the cursor
2147 * doesn't go before 'scrolloff' lines from the screen start.
2148 */
2149#ifdef FEAT_DIFF
2150 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2151 - curwin->w_topfill;
2152#else
2153 start_row = curwin->w_wrow - plines(curwin->w_topline);
2154#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002155 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002158 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002160 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162#ifdef FEAT_DIFF
2163 if (curwin->w_topfill > 0)
2164 --curwin->w_topfill;
2165 else
2166#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002167 {
2168#ifdef FEAT_FOLDING
2169 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002172 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002173 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2175 }
2176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178/*
2179 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2180 * a (wrapped) text line. Uses and sets "lp->fill".
2181 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002182 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 */
2184 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002185topline_back_winheight(
2186 lineoff_T *lp,
2187 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189#ifdef FEAT_DIFF
2190 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2191 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 ++lp->fill;
2194 lp->height = 1;
2195 }
2196 else
2197#endif
2198 {
2199 --lp->lnum;
2200#ifdef FEAT_DIFF
2201 lp->fill = 0;
2202#endif
2203 if (lp->lnum < 1)
2204 lp->height = MAXCOL;
2205 else
2206#ifdef FEAT_FOLDING
2207 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002208 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 lp->height = 1;
2210 else
2211#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002212 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214}
2215
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002216 static void
2217topline_back(lineoff_T *lp)
2218{
2219 topline_back_winheight(lp, TRUE);
2220}
2221
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223/*
2224 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2225 * a (wrapped) text line. Uses and sets "lp->fill".
2226 * Returns the height of the added line in "lp->height".
2227 * Lines below the last one are incredibly high.
2228 */
2229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232#ifdef FEAT_DIFF
2233 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2234 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ++lp->fill;
2237 lp->height = 1;
2238 }
2239 else
2240#endif
2241 {
2242 ++lp->lnum;
2243#ifdef FEAT_DIFF
2244 lp->fill = 0;
2245#endif
2246 if (lp->lnum > curbuf->b_ml.ml_line_count)
2247 lp->height = MAXCOL;
2248 else
2249#ifdef FEAT_FOLDING
2250 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 lp->height = 1;
2253 else
2254#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002255 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257}
2258
2259#ifdef FEAT_DIFF
2260/*
2261 * Switch from including filler lines below lp->lnum to including filler
2262 * lines above loff.lnum + 1. This keeps pointing to the same line.
2263 * When there are no filler lines nothing changes.
2264 */
2265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 if (lp->fill > 0)
2269 {
2270 ++lp->lnum;
2271 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2272 }
2273}
2274
2275/*
2276 * Switch from including filler lines above lp->lnum to including filler
2277 * lines below loff.lnum - 1. This keeps pointing to the same line.
2278 * When there are no filler lines nothing changes.
2279 */
2280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002281topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 if (lp->fill > 0)
2284 {
2285 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2286 --lp->lnum;
2287 }
2288}
2289#endif
2290
2291/*
2292 * Recompute topline to put the cursor at the top of the window.
2293 * Scroll at least "min_scroll" lines.
2294 * If "always" is TRUE, always set topline (for "zt").
2295 */
2296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int scrolled = 0;
2300 int extra = 0;
2301 int used;
2302 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002303 linenr_T top; // just above displayed lines
2304 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002306 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#ifdef FEAT_DIFF
2308 linenr_T old_topfill = curwin->w_topfill;
2309#endif
2310 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002311 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (mouse_dragging > 0)
2314 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316 /*
2317 * Decrease topline until:
2318 * - it has become 1
2319 * - (part of) the cursor line is moved off the screen or
2320 * - moved at least 'scrolljump' lines and
2321 * - at least 'scrolloff' lines above and below the cursor
2322 */
2323 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (curwin->w_cursor.lnum < curwin->w_topline)
2326 scrolled = used;
2327
2328#ifdef FEAT_FOLDING
2329 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2330 {
2331 --top;
2332 ++bot;
2333 }
2334 else
2335#endif
2336 {
2337 top = curwin->w_cursor.lnum - 1;
2338 bot = curwin->w_cursor.lnum + 1;
2339 }
2340 new_topline = top + 1;
2341
2342#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002343 // "used" already contains the number of filler lines above, don't add it
2344 // again.
2345 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002346 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348
2349 /*
2350 * Check if the lines from "top" to "bot" fit in the window. If they do,
2351 * set new_topline and advance "top" and "bot" to include more lines.
2352 */
2353 while (top > 0)
2354 {
2355#ifdef FEAT_FOLDING
2356 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002357 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 i = 1;
2359 else
2360#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002361 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002362 if (top < curwin->w_topline)
2363 scrolled += i;
2364
2365 // If scrolling is needed, scroll at least 'sj' lines.
2366 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2367 && extra >= off)
2368 break;
2369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 used += i;
2371 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2372 {
2373#ifdef FEAT_FOLDING
2374 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002375 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 ++used;
2377 else
2378#endif
2379 used += plines(bot);
2380 }
2381 if (used > curwin->w_height)
2382 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 extra += i;
2385 new_topline = top;
2386 --top;
2387 ++bot;
2388 }
2389
2390 /*
2391 * If we don't have enough space, put cursor in the middle.
2392 * This makes sure we get the same position when using "k" and "j"
2393 * in a small window.
2394 */
2395 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002396 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 else
2398 {
2399 /*
2400 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002401 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
2403 if (new_topline < curwin->w_topline || always)
2404 curwin->w_topline = new_topline;
2405 if (curwin->w_topline > curwin->w_cursor.lnum)
2406 curwin->w_topline = curwin->w_cursor.lnum;
2407#ifdef FEAT_DIFF
2408 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2409 if (curwin->w_topfill > 0 && extra > off)
2410 {
2411 curwin->w_topfill -= extra - off;
2412 if (curwin->w_topfill < 0)
2413 curwin->w_topfill = 0;
2414 }
2415 check_topfill(curwin, FALSE);
2416#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002417 if (curwin->w_topline == curwin->w_cursor.lnum)
2418 {
2419 validate_virtcol();
2420 if (curwin->w_skipcol >= curwin->w_virtcol)
2421 // TODO: if the line doesn't fit may optimize w_skipcol instead
2422 // of making it zero
2423 reset_skipcol();
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002426 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#ifdef FEAT_DIFF
2428 || curwin->w_topfill != old_topfill
2429#endif
2430 )
2431 curwin->w_valid &=
2432 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2433 curwin->w_valid |= VALID_TOPLINE;
2434 }
2435}
2436
2437/*
2438 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2439 * screen lines for text lines.
2440 */
2441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444#ifdef FEAT_DIFF
2445 wp->w_filler_rows = 0;
2446#endif
2447 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002448 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 else
2450 {
2451 wp->w_empty_rows = wp->w_height - used;
2452#ifdef FEAT_DIFF
2453 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2454 {
2455 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2456 if (wp->w_empty_rows > wp->w_filler_rows)
2457 wp->w_empty_rows -= wp->w_filler_rows;
2458 else
2459 {
2460 wp->w_filler_rows = wp->w_empty_rows;
2461 wp->w_empty_rows = 0;
2462 }
2463 }
2464#endif
2465 }
2466}
2467
2468/*
2469 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002470 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2472 * This is messy stuff!!!
2473 */
2474 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002475scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 int used;
2478 int scrolled = 0;
2479 int extra = 0;
2480 int i;
2481 linenr_T line_count;
2482 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002483 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 lineoff_T loff;
2485 lineoff_T boff;
2486#ifdef FEAT_DIFF
2487 int old_topfill = curwin->w_topfill;
2488 int fill_below_window;
2489#endif
2490 linenr_T old_botline = curwin->w_botline;
2491 linenr_T old_valid = curwin->w_valid;
2492 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002493 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002494 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002495 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 cln = curwin->w_cursor.lnum;
2498 if (set_topbot)
2499 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002500 int set_skipcol = FALSE;
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 used = 0;
2503 curwin->w_botline = cln + 1;
2504#ifdef FEAT_DIFF
2505 loff.fill = 0;
2506#endif
2507 for (curwin->w_topline = curwin->w_botline;
2508 curwin->w_topline > 1;
2509 curwin->w_topline = loff.lnum)
2510 {
2511 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002512 topline_back_winheight(&loff, FALSE);
2513 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002515 if (used + loff.height > curwin->w_height)
2516 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002517 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002518 {
2519 // 'smoothscroll' and 'wrap' are set. The above line is
2520 // too long to show in its entirety, so we show just a part
2521 // of it.
2522 if (used < curwin->w_height)
2523 {
2524 int plines_offset = used + loff.height
2525 - curwin->w_height;
2526 used = curwin->w_height;
2527#ifdef FEAT_DIFF
2528 curwin->w_topfill = loff.fill;
2529#endif
2530 curwin->w_topline = loff.lnum;
2531 curwin->w_skipcol = skipcol_from_plines(
2532 curwin, plines_offset);
2533 set_skipcol = TRUE;
2534 }
2535 }
2536 break;
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 used += loff.height;
2539#ifdef FEAT_DIFF
2540 curwin->w_topfill = loff.fill;
2541#endif
2542 }
2543 set_empty_rows(curwin, used);
2544 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2545 if (curwin->w_topline != old_topline
2546#ifdef FEAT_DIFF
2547 || curwin->w_topfill != old_topfill
2548#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002549 || set_skipcol
2550 || curwin->w_skipcol != 0)
2551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002553 if (set_skipcol)
2554 redraw_later(UPD_NOT_VALID);
2555 else
2556 reset_skipcol();
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559 else
2560 validate_botline();
2561
Bram Moolenaar85a20022019-12-21 18:25:54 +01002562 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#ifdef FEAT_DIFF
2564 used = plines_nofill(cln);
2565#else
2566 validate_cheight();
2567 used = curwin->w_cline_height;
2568#endif
2569
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002570 // If the cursor is on or below botline, we will at least scroll by the
2571 // height of the cursor line, which is "used". Correct for empty lines,
2572 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (cln >= curwin->w_botline)
2574 {
2575 scrolled = used;
2576 if (cln == curwin->w_botline)
2577 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002578 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002579 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002580 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002581 // Calculate how many screen lines the current top line of window
2582 // occupies. If it is occupying more than the entire window, we
2583 // need to scroll the additional clipped lines to scroll past the
2584 // top line before we can move on to the other lines.
2585 int top_plines =
2586#ifdef FEAT_DIFF
2587 plines_win_nofill
2588#else
2589 plines_win
2590#endif
2591 (curwin, curwin->w_topline, FALSE);
2592 int skip_lines = 0;
2593 int width1 = curwin->w_width - curwin_col_off();
2594 int width2 = width1 + curwin_col_off2();
2595 // similar formula is used in curs_columns()
2596 if (curwin->w_skipcol > width1)
2597 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2598 else if (curwin->w_skipcol > 0)
2599 skip_lines = 1;
2600
2601 top_plines -= skip_lines;
2602 if (top_plines > curwin->w_height)
2603 {
2604 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002605 }
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
2609 /*
2610 * Stop counting lines to scroll when
2611 * - hitting start of the file
2612 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002613 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 * - lines between botline and cursor have been counted
2615 */
2616#ifdef FEAT_FOLDING
2617 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2618#endif
2619 {
2620 loff.lnum = cln;
2621 boff.lnum = cln;
2622 }
2623#ifdef FEAT_DIFF
2624 loff.fill = 0;
2625 boff.fill = 0;
2626 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2627 - curwin->w_filler_rows;
2628#endif
2629
2630 while (loff.lnum > 1)
2631 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2633 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002635 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2637 && loff.lnum <= curwin->w_botline
2638#ifdef FEAT_DIFF
2639 && (loff.lnum < curwin->w_botline
2640 || loff.fill >= fill_below_window)
2641#endif
2642 )
2643 break;
2644
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002647 if (loff.height == MAXCOL)
2648 used = MAXCOL;
2649 else
2650 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (used > curwin->w_height)
2652 break;
2653 if (loff.lnum >= curwin->w_botline
2654#ifdef FEAT_DIFF
2655 && (loff.lnum > curwin->w_botline
2656 || loff.fill <= fill_below_window)
2657#endif
2658 )
2659 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 scrolled += loff.height;
2662 if (loff.lnum == curwin->w_botline
2663#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002664 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#endif
2666 )
2667 scrolled -= curwin->w_empty_rows;
2668 }
2669
2670 if (boff.lnum < curbuf->b_ml.ml_line_count)
2671 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002672 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 botline_forw(&boff);
2674 used += boff.height;
2675 if (used > curwin->w_height)
2676 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002677 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2678 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 extra += boff.height;
2681 if (boff.lnum >= curwin->w_botline
2682#ifdef FEAT_DIFF
2683 || (boff.lnum + 1 == curwin->w_botline
2684 && boff.fill > curwin->w_filler_rows)
2685#endif
2686 )
2687 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002688 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 scrolled += boff.height;
2690 if (boff.lnum == curwin->w_botline
2691#ifdef FEAT_DIFF
2692 && boff.fill == 0
2693#endif
2694 )
2695 scrolled -= curwin->w_empty_rows;
2696 }
2697 }
2698 }
2699 }
2700
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (scrolled <= 0)
2703 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002704 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else if (used > curwin->w_height)
2706 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002707 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 else
2709 {
2710 line_count = 0;
2711#ifdef FEAT_DIFF
2712 boff.fill = curwin->w_topfill;
2713#endif
2714 boff.lnum = curwin->w_topline - 1;
2715 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2716 {
2717 botline_forw(&boff);
2718 i += boff.height;
2719 ++line_count;
2720 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 line_count = 9999;
2723 }
2724
2725 /*
2726 * Scroll up if the cursor is off the bottom of the screen a bit.
2727 * Otherwise put it at 1/2 of the screen.
2728 */
2729 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002730 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002731 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002732 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002733 if (do_sms)
2734 scrollup(scrolled, TRUE); // TODO
2735 else
2736 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
2740 * If topline didn't change we need to restore w_botline and w_empty_rows
2741 * (we changed them).
2742 * If topline did change, update_screen() will set botline.
2743 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002744 if (curwin->w_topline == old_topline
2745 && curwin->w_skipcol == old_skipcol
2746 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
2748 curwin->w_botline = old_botline;
2749 curwin->w_empty_rows = old_empty_rows;
2750 curwin->w_valid = old_valid;
2751 }
2752 curwin->w_valid |= VALID_TOPLINE;
2753}
2754
2755/*
2756 * Recompute topline to put the cursor halfway the window
2757 * If "atend" is TRUE, also put it halfway at the end of the file.
2758 */
2759 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002760scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 int above = 0;
2763 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002764 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#ifdef FEAT_DIFF
2766 int topfill = 0;
2767#endif
2768 int below = 0;
2769 int used;
2770 lineoff_T loff;
2771 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002772#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002773 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002776#ifdef FEAT_PROP_POPUP
2777 // if the width changed this needs to be updated first
2778 may_update_popup_position();
2779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2781#ifdef FEAT_FOLDING
2782 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2783#endif
2784#ifdef FEAT_DIFF
2785 used = plines_nofill(loff.lnum);
2786 loff.fill = 0;
2787 boff.fill = 0;
2788#else
2789 used = plines(loff.lnum);
2790#endif
2791 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002792
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002793 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002794 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2795 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002796 {
2797 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002798 if (atend)
2799 {
2800 want_height = (curwin->w_height - used) / 2;
2801 used = 0;
2802 }
2803 else
2804 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002805 }
2806
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 while (topline > 1)
2808 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002809 // If using smoothscroll, we can precisely scroll to the
2810 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002811 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002812 {
2813 topline_back_winheight(&loff, FALSE);
2814 if (loff.height == MAXCOL)
2815 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002816 used += loff.height;
2817 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002818 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002819 botline_forw(&boff);
2820 used += boff.height;
2821 }
2822 if (used > want_height)
2823 {
2824 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002825 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002826 topline = loff.lnum;
2827#ifdef FEAT_DIFF
2828 topfill = loff.fill;
2829#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002830 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002831 }
2832 break;
2833 }
2834 topline = loff.lnum;
2835#ifdef FEAT_DIFF
2836 topfill = loff.fill;
2837#endif
2838 continue;
2839 }
2840
2841 // If not using smoothscroll, we have to iteratively find how many
2842 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002843 // This may not be right in the middle if the lines'
2844 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002845
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002846 // Depending on "prefer_above" we add a line above or below first.
2847 // Loop twice to avoid duplicating code.
2848 int done = FALSE;
2849 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002851 if (prefer_above ? (round == 2 && below < above)
2852 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002854 // add a line below the cursor
2855 if (boff.lnum < curbuf->b_ml.ml_line_count)
2856 {
2857 botline_forw(&boff);
2858 used += boff.height;
2859 if (used > curwin->w_height)
2860 {
2861 done = TRUE;
2862 break;
2863 }
2864 below += boff.height;
2865 }
2866 else
2867 {
2868 ++below; // count a "~" line
2869 if (atend)
2870 ++used;
2871 }
2872 }
2873
2874 if (prefer_above ? (round == 1 && below >= above)
2875 : (round == 1 && below > above))
2876 {
2877 // add a line above the cursor
2878 topline_back(&loff);
2879 if (loff.height == MAXCOL)
2880 used = MAXCOL;
2881 else
2882 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002884 {
2885 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002887 }
2888 above += loff.height;
2889 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002891 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002895 if (done)
2896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#ifdef FEAT_FOLDING
2900 if (!hasFolding(topline, &curwin->w_topline, NULL))
2901#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002902 {
2903 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002904 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002905 || curwin->w_skipcol != 0)
2906 {
2907 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002908 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002909 {
2910 curwin->w_skipcol = skipcol;
2911 redraw_later(UPD_NOT_VALID);
2912 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002913 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002914 reset_skipcol();
2915 }
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#ifdef FEAT_DIFF
2918 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002919 if (old_topline > curwin->w_topline + curwin->w_height)
2920 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 check_topfill(curwin, FALSE);
2922#endif
2923 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2924 curwin->w_valid |= VALID_TOPLINE;
2925}
2926
2927/*
2928 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002929 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 * If not possible, put it at the same position as scroll_cursor_halfway().
2931 * When called topline must be valid!
2932 */
2933 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002934cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002936 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002938 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 linenr_T botline;
2940 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002941 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002943 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 /*
2946 * How many lines we would like to have above/below the cursor depends on
2947 * whether the first/last line of the file is on screen.
2948 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002949 above_wanted = so;
2950 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002951 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
2953 above_wanted = mouse_dragging - 1;
2954 below_wanted = mouse_dragging - 1;
2955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (curwin->w_topline == 1)
2957 {
2958 above_wanted = 0;
2959 max_off = curwin->w_height / 2;
2960 if (below_wanted > max_off)
2961 below_wanted = max_off;
2962 }
2963 validate_botline();
2964 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002965 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
2967 below_wanted = 0;
2968 max_off = (curwin->w_height - 1) / 2;
2969 if (above_wanted > max_off)
2970 above_wanted = max_off;
2971 }
2972
2973 /*
2974 * If there are sufficient file-lines above and below the cursor, we can
2975 * return now.
2976 */
2977 cln = curwin->w_cursor.lnum;
2978 if (cln >= curwin->w_topline + above_wanted
2979 && cln < curwin->w_botline - below_wanted
2980#ifdef FEAT_FOLDING
2981 && !hasAnyFolding(curwin)
2982#endif
2983 )
2984 return;
2985
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002986 if (curwin->w_p_sms && !curwin->w_p_wrap)
2987 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002988 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002989 if (curwin->w_cline_height == curwin->w_height)
2990 {
2991 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002992 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002993 return;
2994 }
2995 // TODO: If the cursor line doesn't fit in the window then only adjust
2996 // w_skipcol.
2997 }
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 /*
3000 * Narrow down the area where the cursor can be put by taking lines from
3001 * the top and the bottom until:
3002 * - the desired context lines are found
3003 * - the lines from the top is past the lines from the bottom
3004 */
3005 topline = curwin->w_topline;
3006 botline = curwin->w_botline - 1;
3007#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003008 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 above = curwin->w_topfill;
3010 below = curwin->w_filler_rows;
3011#endif
3012 while ((above < above_wanted || below < below_wanted) && topline < botline)
3013 {
3014 if (below < below_wanted && (below <= above || above >= above_wanted))
3015 {
3016#ifdef FEAT_FOLDING
3017 if (hasFolding(botline, &botline, NULL))
3018 ++below;
3019 else
3020#endif
3021 below += plines(botline);
3022 --botline;
3023 }
3024 if (above < above_wanted && (above < below || below >= below_wanted))
3025 {
3026#ifdef FEAT_FOLDING
3027 if (hasFolding(topline, NULL, &topline))
3028 ++above;
3029 else
3030#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003031 above += PLINES_NOFILL(topline);
3032#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003033 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (topline < botline)
3035 above += diff_check_fill(curwin, topline + 1);
3036#endif
3037 ++topline;
3038 }
3039 }
3040 if (topline == botline || botline == 0)
3041 curwin->w_cursor.lnum = topline;
3042 else if (topline > botline)
3043 curwin->w_cursor.lnum = botline;
3044 else
3045 {
3046 if (cln < topline && curwin->w_topline > 1)
3047 {
3048 curwin->w_cursor.lnum = topline;
3049 curwin->w_valid &=
3050 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3051 }
3052 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3053 {
3054 curwin->w_cursor.lnum = botline;
3055 curwin->w_valid &=
3056 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3057 }
3058 }
3059 curwin->w_valid |= VALID_TOPLINE;
3060}
3061
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003062static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003065 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3066 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003068 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 */
3070 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003071onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 long n;
3074 int retval = OK;
3075 lineoff_T loff;
3076 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003077 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar85a20022019-12-21 18:25:54 +01003079 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 beep_flush();
3082 return FAIL;
3083 }
3084
3085 for ( ; count > 0; --count)
3086 {
3087 validate_botline();
3088 /*
3089 * It's an error to move a page up when the first line is already on
3090 * the screen. It's an error to move a page down when the last line
3091 * is on the screen and the topline is 'scrolloff' lines from the
3092 * last line.
3093 */
3094 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003095 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3097 : (curwin->w_topline == 1
3098#ifdef FEAT_DIFF
3099 && curwin->w_topfill ==
3100 diff_check_fill(curwin, curwin->w_topline)
3101#endif
3102 ))
3103 {
3104 beep_flush();
3105 retval = FAIL;
3106 break;
3107 }
3108
3109#ifdef FEAT_DIFF
3110 loff.fill = 0;
3111#endif
3112 if (dir == FORWARD)
3113 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003114 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003116 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003117 if (p_window <= 2)
3118 ++curwin->w_topline;
3119 else
3120 curwin->w_topline += p_window - 2;
3121 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3122 curwin->w_topline = curbuf->b_ml.ml_line_count;
3123 curwin->w_cursor.lnum = curwin->w_topline;
3124 }
3125 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3126 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003127 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 curwin->w_topline = curbuf->b_ml.ml_line_count;
3129#ifdef FEAT_DIFF
3130 curwin->w_topfill = 0;
3131#endif
3132 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3133 }
3134 else
3135 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 // For the overlap, start with the line just below the window
3137 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 loff.lnum = curwin->w_botline;
3139#ifdef FEAT_DIFF
3140 loff.fill = diff_check_fill(curwin, loff.lnum)
3141 - curwin->w_filler_rows;
3142#endif
3143 get_scroll_overlap(&loff, -1);
3144 curwin->w_topline = loff.lnum;
3145#ifdef FEAT_DIFF
3146 curwin->w_topfill = loff.fill;
3147 check_topfill(curwin, FALSE);
3148#endif
3149 curwin->w_cursor.lnum = curwin->w_topline;
3150 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3151 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3152 }
3153 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003154 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156#ifdef FEAT_DIFF
3157 if (curwin->w_topline == 1)
3158 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 max_topfill();
3161 continue;
3162 }
3163#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003164 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003165 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003166 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003167 if (p_window <= 2)
3168 --curwin->w_topline;
3169 else
3170 curwin->w_topline -= p_window - 2;
3171 if (curwin->w_topline < 1)
3172 curwin->w_topline = 1;
3173 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3174 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3175 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3176 continue;
3177 }
3178
Bram Moolenaar85a20022019-12-21 18:25:54 +01003179 // Find the line at the top of the window that is going to be the
3180 // line at the bottom of the window. Make sure this results in
3181 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 loff.lnum = curwin->w_topline - 1;
3183#ifdef FEAT_DIFF
3184 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3185 - curwin->w_topfill;
3186#endif
3187 get_scroll_overlap(&loff, 1);
3188
3189 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3190 {
3191 loff.lnum = curbuf->b_ml.ml_line_count;
3192#ifdef FEAT_DIFF
3193 loff.fill = 0;
3194 }
3195 else
3196 {
3197 botline_topline(&loff);
3198#endif
3199 }
3200 curwin->w_cursor.lnum = loff.lnum;
3201
Bram Moolenaar85a20022019-12-21 18:25:54 +01003202 // Find the line just above the new topline to get the right line
3203 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 n = 0;
3205 while (n <= curwin->w_height && loff.lnum >= 1)
3206 {
3207 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003208 if (loff.height == MAXCOL)
3209 n = MAXCOL;
3210 else
3211 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003213 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
3215 curwin->w_topline = 1;
3216#ifdef FEAT_DIFF
3217 max_topfill();
3218#endif
3219 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3220 }
3221 else
3222 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003223 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#ifdef FEAT_DIFF
3225 topline_botline(&loff);
3226#endif
3227 botline_forw(&loff);
3228 botline_forw(&loff);
3229#ifdef FEAT_DIFF
3230 botline_topline(&loff);
3231#endif
3232#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003233 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3235#endif
3236
Bram Moolenaar85a20022019-12-21 18:25:54 +01003237 // Always scroll at least one line. Avoid getting stuck on
3238 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (loff.lnum >= curwin->w_topline
3240#ifdef FEAT_DIFF
3241 && (loff.lnum > curwin->w_topline
3242 || loff.fill >= curwin->w_topfill)
3243#endif
3244 )
3245 {
3246#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 // First try using the maximum number of filler lines. If
3248 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 loff.fill = curwin->w_topfill;
3250 if (curwin->w_topfill < diff_check_fill(curwin,
3251 curwin->w_topline))
3252 max_topfill();
3253 if (curwin->w_topfill == loff.fill)
3254#endif
3255 {
3256 --curwin->w_topline;
3257#ifdef FEAT_DIFF
3258 curwin->w_topfill = 0;
3259#endif
3260 }
3261 comp_botline(curwin);
3262 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003263 curwin->w_valid &=
3264 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266 else
3267 {
3268 curwin->w_topline = loff.lnum;
3269#ifdef FEAT_DIFF
3270 curwin->w_topfill = loff.fill;
3271 check_topfill(curwin, FALSE);
3272#endif
3273 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3274 }
3275 }
3276 }
3277 }
3278#ifdef FEAT_FOLDING
3279 foldAdjustCursor();
3280#endif
3281 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003282 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003283 if (retval == OK)
3284 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3286
Bram Moolenaar907dad72018-07-10 15:07:15 +02003287 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003289 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3290 // But make sure we scroll at least one line (happens with mix of long
3291 // wrapping lines and non-wrapping line).
3292 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003294 scroll_cursor_top(1, FALSE);
3295 if (curwin->w_topline <= old_topline
3296 && old_topline < curbuf->b_ml.ml_line_count)
3297 {
3298 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003300 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3301#endif
3302 }
3303 }
3304#ifdef FEAT_FOLDING
3305 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003310 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return retval;
3312}
3313
3314/*
3315 * Decide how much overlap to use for page-up or page-down scrolling.
3316 * This is symmetric, so that doing both keeps the same lines displayed.
3317 * Three lines are examined:
3318 *
3319 * before CTRL-F after CTRL-F / before CTRL-B
3320 * etc. l1
3321 * l1 last but one line ------------
3322 * l2 last text line l2 top text line
3323 * ------------- l3 second text line
3324 * l3 etc.
3325 */
3326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int h1, h2, h3, h4;
3330 int min_height = curwin->w_height - 2;
3331 lineoff_T loff0, loff1, loff2;
3332
3333#ifdef FEAT_DIFF
3334 if (lp->fill > 0)
3335 lp->height = 1;
3336 else
3337 lp->height = plines_nofill(lp->lnum);
3338#else
3339 lp->height = plines(lp->lnum);
3340#endif
3341 h1 = lp->height;
3342 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 loff0 = *lp;
3346 if (dir > 0)
3347 botline_forw(lp);
3348 else
3349 topline_back(lp);
3350 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003351 if (h2 == MAXCOL || h2 + h1 > 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 loff1 = *lp;
3358 if (dir > 0)
3359 botline_forw(lp);
3360 else
3361 topline_back(lp);
3362 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003363 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 return;
3367 }
3368
3369 loff2 = *lp;
3370 if (dir > 0)
3371 botline_forw(lp);
3372 else
3373 topline_back(lp);
3374 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003375 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003376 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Scroll 'scroll' lines up or down.
3383 */
3384 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003385halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387 long scrolled = 0;
3388 int i;
3389 int n;
3390 int room;
3391
3392 if (Prenum)
3393 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3394 curwin->w_height : Prenum;
3395 n = (curwin->w_p_scr <= curwin->w_height) ?
3396 curwin->w_p_scr : curwin->w_height;
3397
Bram Moolenaard5d37532017-03-27 23:02:07 +02003398 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 validate_botline();
3400 room = curwin->w_empty_rows;
3401#ifdef FEAT_DIFF
3402 room += curwin->w_filler_rows;
3403#endif
3404 if (flag)
3405 {
3406 /*
3407 * scroll the text up
3408 */
3409 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3410 {
3411#ifdef FEAT_DIFF
3412 if (curwin->w_topfill > 0)
3413 {
3414 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003415 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 --curwin->w_topfill;
3417 }
3418 else
3419#endif
3420 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003421 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 n -= i;
3423 if (n < 0 && scrolled > 0)
3424 break;
3425#ifdef FEAT_FOLDING
3426 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3427#endif
3428 ++curwin->w_topline;
3429#ifdef FEAT_DIFF
3430 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3431#endif
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3434 {
3435 ++curwin->w_cursor.lnum;
3436 curwin->w_valid &=
3437 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3441 scrolled += i;
3442
3443 /*
3444 * Correct w_botline for changed w_topline.
3445 * Won't work when there are filler lines.
3446 */
3447#ifdef FEAT_DIFF
3448 if (curwin->w_p_diff)
3449 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3450 else
3451#endif
3452 {
3453 room += i;
3454 do
3455 {
3456 i = plines(curwin->w_botline);
3457 if (i > room)
3458 break;
3459#ifdef FEAT_FOLDING
3460 (void)hasFolding(curwin->w_botline, NULL,
3461 &curwin->w_botline);
3462#endif
3463 ++curwin->w_botline;
3464 room -= i;
3465 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3466 }
3467 }
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 /*
3470 * When hit bottom of the file: move cursor down.
3471 */
3472 if (n > 0)
3473 {
3474# ifdef FEAT_FOLDING
3475 if (hasAnyFolding(curwin))
3476 {
3477 while (--n >= 0
3478 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3479 {
3480 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3481 &curwin->w_cursor.lnum);
3482 ++curwin->w_cursor.lnum;
3483 }
3484 }
3485 else
3486# endif
3487 curwin->w_cursor.lnum += n;
3488 check_cursor_lnum();
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 else
3492 {
3493 /*
3494 * scroll the text down
3495 */
3496 while (n > 0 && curwin->w_topline > 1)
3497 {
3498#ifdef FEAT_DIFF
3499 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3500 {
3501 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003502 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 ++curwin->w_topfill;
3504 }
3505 else
3506#endif
3507 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003508 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 n -= i;
3510 if (n < 0 && scrolled > 0)
3511 break;
3512 --curwin->w_topline;
3513#ifdef FEAT_FOLDING
3514 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3515#endif
3516#ifdef FEAT_DIFF
3517 curwin->w_topfill = 0;
3518#endif
3519 }
3520 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3521 VALID_BOTLINE|VALID_BOTLINE_AP);
3522 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (curwin->w_cursor.lnum > 1)
3524 {
3525 --curwin->w_cursor.lnum;
3526 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 /*
3531 * When hit top of the file: move cursor up.
3532 */
3533 if (n > 0)
3534 {
3535 if (curwin->w_cursor.lnum <= (linenr_T)n)
3536 curwin->w_cursor.lnum = 1;
3537 else
3538# ifdef FEAT_FOLDING
3539 if (hasAnyFolding(curwin))
3540 {
3541 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3542 {
3543 --curwin->w_cursor.lnum;
3544 (void)hasFolding(curwin->w_cursor.lnum,
3545 &curwin->w_cursor.lnum, NULL);
3546 }
3547 }
3548 else
3549# endif
3550 curwin->w_cursor.lnum -= n;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003554 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 foldAdjustCursor();
3556# endif
3557#ifdef FEAT_DIFF
3558 check_topfill(curwin, !flag);
3559#endif
3560 cursor_correct();
3561 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003562 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003564
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003566do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567{
3568 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003569 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003570 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003571 colnr_T curswant = curwin->w_curswant;
3572 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 win_T *old_curwin = curwin;
3574 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575 int old_VIsual_select = VIsual_select;
3576 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577
3578 /*
3579 * loop through the cursorbound windows
3580 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003582 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583 {
3584 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003585 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586 if (curwin != old_curwin && curwin->w_p_crb)
3587 {
3588# ifdef FEAT_DIFF
3589 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003590 curwin->w_cursor.lnum =
3591 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 else
3593# endif
3594 curwin->w_cursor.lnum = line;
3595 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003596 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003597 curwin->w_curswant = curswant;
3598 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599
Bram Moolenaar85a20022019-12-21 18:25:54 +01003600 // Make sure the cursor is in a valid position. Temporarily set
3601 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003602 int restart_edit_save = restart_edit;
3603 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003605
3606 // Avoid a scroll here for the cursor position, 'scrollbind' is
3607 // more important.
3608 if (!curwin->w_p_scb)
3609 validate_cursor();
3610
Bram Moolenaar61452852011-02-01 18:01:11 +01003611 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003612 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003613 if (has_mbyte)
3614 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003615 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003616
Bram Moolenaar85a20022019-12-21 18:25:54 +01003617 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003618 if (!curwin->w_p_scb)
3619 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621 }
3622 }
3623
3624 /*
3625 * reset current-window
3626 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627 VIsual_select = old_VIsual_select;
3628 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003629 curwin = old_curwin;
3630 curbuf = old_curbuf;
3631}