blob: 4cd4d1c2bcf0f221217a573c4740f4e20bf6ad2c [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 Moolenaar46b54742022-10-06 15:46:49 +01002417 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002418 if (curwin->w_topline == curwin->w_cursor.lnum
2419 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002420 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002422 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#ifdef FEAT_DIFF
2424 || curwin->w_topfill != old_topfill
2425#endif
2426 )
2427 curwin->w_valid &=
2428 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2429 curwin->w_valid |= VALID_TOPLINE;
2430 }
2431}
2432
2433/*
2434 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2435 * screen lines for text lines.
2436 */
2437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440#ifdef FEAT_DIFF
2441 wp->w_filler_rows = 0;
2442#endif
2443 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 else
2446 {
2447 wp->w_empty_rows = wp->w_height - used;
2448#ifdef FEAT_DIFF
2449 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2450 {
2451 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2452 if (wp->w_empty_rows > wp->w_filler_rows)
2453 wp->w_empty_rows -= wp->w_filler_rows;
2454 else
2455 {
2456 wp->w_filler_rows = wp->w_empty_rows;
2457 wp->w_empty_rows = 0;
2458 }
2459 }
2460#endif
2461 }
2462}
2463
2464/*
2465 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002466 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2468 * This is messy stuff!!!
2469 */
2470 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002471scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 int used;
2474 int scrolled = 0;
2475 int extra = 0;
2476 int i;
2477 linenr_T line_count;
2478 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002479 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 lineoff_T loff;
2481 lineoff_T boff;
2482#ifdef FEAT_DIFF
2483 int old_topfill = curwin->w_topfill;
2484 int fill_below_window;
2485#endif
2486 linenr_T old_botline = curwin->w_botline;
2487 linenr_T old_valid = curwin->w_valid;
2488 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002489 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002490 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002491 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 cln = curwin->w_cursor.lnum;
2494 if (set_topbot)
2495 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002496 int set_skipcol = FALSE;
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 used = 0;
2499 curwin->w_botline = cln + 1;
2500#ifdef FEAT_DIFF
2501 loff.fill = 0;
2502#endif
2503 for (curwin->w_topline = curwin->w_botline;
2504 curwin->w_topline > 1;
2505 curwin->w_topline = loff.lnum)
2506 {
2507 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002508 topline_back_winheight(&loff, FALSE);
2509 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002511 if (used + loff.height > curwin->w_height)
2512 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002513 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002514 {
2515 // 'smoothscroll' and 'wrap' are set. The above line is
2516 // too long to show in its entirety, so we show just a part
2517 // of it.
2518 if (used < curwin->w_height)
2519 {
2520 int plines_offset = used + loff.height
2521 - curwin->w_height;
2522 used = curwin->w_height;
2523#ifdef FEAT_DIFF
2524 curwin->w_topfill = loff.fill;
2525#endif
2526 curwin->w_topline = loff.lnum;
2527 curwin->w_skipcol = skipcol_from_plines(
2528 curwin, plines_offset);
2529 set_skipcol = TRUE;
2530 }
2531 }
2532 break;
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 used += loff.height;
2535#ifdef FEAT_DIFF
2536 curwin->w_topfill = loff.fill;
2537#endif
2538 }
2539 set_empty_rows(curwin, used);
2540 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2541 if (curwin->w_topline != old_topline
2542#ifdef FEAT_DIFF
2543 || curwin->w_topfill != old_topfill
2544#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002545 || set_skipcol
2546 || curwin->w_skipcol != 0)
2547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002549 if (set_skipcol)
2550 redraw_later(UPD_NOT_VALID);
2551 else
2552 reset_skipcol();
2553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 else
2556 validate_botline();
2557
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#ifdef FEAT_DIFF
2560 used = plines_nofill(cln);
2561#else
2562 validate_cheight();
2563 used = curwin->w_cline_height;
2564#endif
2565
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002566 // If the cursor is on or below botline, we will at least scroll by the
2567 // height of the cursor line, which is "used". Correct for empty lines,
2568 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (cln >= curwin->w_botline)
2570 {
2571 scrolled = used;
2572 if (cln == curwin->w_botline)
2573 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002574 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002575 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002576 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002577 // Calculate how many screen lines the current top line of window
2578 // occupies. If it is occupying more than the entire window, we
2579 // need to scroll the additional clipped lines to scroll past the
2580 // top line before we can move on to the other lines.
2581 int top_plines =
2582#ifdef FEAT_DIFF
2583 plines_win_nofill
2584#else
2585 plines_win
2586#endif
2587 (curwin, curwin->w_topline, FALSE);
2588 int skip_lines = 0;
2589 int width1 = curwin->w_width - curwin_col_off();
2590 int width2 = width1 + curwin_col_off2();
2591 // similar formula is used in curs_columns()
2592 if (curwin->w_skipcol > width1)
2593 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2594 else if (curwin->w_skipcol > 0)
2595 skip_lines = 1;
2596
2597 top_plines -= skip_lines;
2598 if (top_plines > curwin->w_height)
2599 {
2600 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002601 }
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
2605 /*
2606 * Stop counting lines to scroll when
2607 * - hitting start of the file
2608 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002609 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * - lines between botline and cursor have been counted
2611 */
2612#ifdef FEAT_FOLDING
2613 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2614#endif
2615 {
2616 loff.lnum = cln;
2617 boff.lnum = cln;
2618 }
2619#ifdef FEAT_DIFF
2620 loff.fill = 0;
2621 boff.fill = 0;
2622 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2623 - curwin->w_filler_rows;
2624#endif
2625
2626 while (loff.lnum > 1)
2627 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002628 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2629 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002631 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2633 && loff.lnum <= curwin->w_botline
2634#ifdef FEAT_DIFF
2635 && (loff.lnum < curwin->w_botline
2636 || loff.fill >= fill_below_window)
2637#endif
2638 )
2639 break;
2640
Bram Moolenaar85a20022019-12-21 18:25:54 +01002641 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002643 if (loff.height == MAXCOL)
2644 used = MAXCOL;
2645 else
2646 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (used > curwin->w_height)
2648 break;
2649 if (loff.lnum >= curwin->w_botline
2650#ifdef FEAT_DIFF
2651 && (loff.lnum > curwin->w_botline
2652 || loff.fill <= fill_below_window)
2653#endif
2654 )
2655 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002656 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 scrolled += loff.height;
2658 if (loff.lnum == curwin->w_botline
2659#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002660 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662 )
2663 scrolled -= curwin->w_empty_rows;
2664 }
2665
2666 if (boff.lnum < curbuf->b_ml.ml_line_count)
2667 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002668 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 botline_forw(&boff);
2670 used += boff.height;
2671 if (used > curwin->w_height)
2672 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002673 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2674 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 extra += boff.height;
2677 if (boff.lnum >= curwin->w_botline
2678#ifdef FEAT_DIFF
2679 || (boff.lnum + 1 == curwin->w_botline
2680 && boff.fill > curwin->w_filler_rows)
2681#endif
2682 )
2683 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 scrolled += boff.height;
2686 if (boff.lnum == curwin->w_botline
2687#ifdef FEAT_DIFF
2688 && boff.fill == 0
2689#endif
2690 )
2691 scrolled -= curwin->w_empty_rows;
2692 }
2693 }
2694 }
2695 }
2696
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (scrolled <= 0)
2699 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 else if (used > curwin->w_height)
2702 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002703 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 else
2705 {
2706 line_count = 0;
2707#ifdef FEAT_DIFF
2708 boff.fill = curwin->w_topfill;
2709#endif
2710 boff.lnum = curwin->w_topline - 1;
2711 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2712 {
2713 botline_forw(&boff);
2714 i += boff.height;
2715 ++line_count;
2716 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002717 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 line_count = 9999;
2719 }
2720
2721 /*
2722 * Scroll up if the cursor is off the bottom of the screen a bit.
2723 * Otherwise put it at 1/2 of the screen.
2724 */
2725 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002726 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002727 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002728 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002729 if (do_sms)
2730 scrollup(scrolled, TRUE); // TODO
2731 else
2732 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 /*
2736 * If topline didn't change we need to restore w_botline and w_empty_rows
2737 * (we changed them).
2738 * If topline did change, update_screen() will set botline.
2739 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002740 if (curwin->w_topline == old_topline
2741 && curwin->w_skipcol == old_skipcol
2742 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
2744 curwin->w_botline = old_botline;
2745 curwin->w_empty_rows = old_empty_rows;
2746 curwin->w_valid = old_valid;
2747 }
2748 curwin->w_valid |= VALID_TOPLINE;
2749}
2750
2751/*
2752 * Recompute topline to put the cursor halfway the window
2753 * If "atend" is TRUE, also put it halfway at the end of the file.
2754 */
2755 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002756scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 int above = 0;
2759 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002760 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#ifdef FEAT_DIFF
2762 int topfill = 0;
2763#endif
2764 int below = 0;
2765 int used;
2766 lineoff_T loff;
2767 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002768#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002769 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002772#ifdef FEAT_PROP_POPUP
2773 // if the width changed this needs to be updated first
2774 may_update_popup_position();
2775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2777#ifdef FEAT_FOLDING
2778 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2779#endif
2780#ifdef FEAT_DIFF
2781 used = plines_nofill(loff.lnum);
2782 loff.fill = 0;
2783 boff.fill = 0;
2784#else
2785 used = plines(loff.lnum);
2786#endif
2787 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002788
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002789 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002790 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2791 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002792 {
2793 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002794 if (atend)
2795 {
2796 want_height = (curwin->w_height - used) / 2;
2797 used = 0;
2798 }
2799 else
2800 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002801 }
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 while (topline > 1)
2804 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002805 // If using smoothscroll, we can precisely scroll to the
2806 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002807 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002808 {
2809 topline_back_winheight(&loff, FALSE);
2810 if (loff.height == MAXCOL)
2811 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002812 used += loff.height;
2813 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002814 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002815 botline_forw(&boff);
2816 used += boff.height;
2817 }
2818 if (used > want_height)
2819 {
2820 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002821 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002822 topline = loff.lnum;
2823#ifdef FEAT_DIFF
2824 topfill = loff.fill;
2825#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002826 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002827 }
2828 break;
2829 }
2830 topline = loff.lnum;
2831#ifdef FEAT_DIFF
2832 topfill = loff.fill;
2833#endif
2834 continue;
2835 }
2836
2837 // If not using smoothscroll, we have to iteratively find how many
2838 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002839 // This may not be right in the middle if the lines'
2840 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002841
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002842 // Depending on "prefer_above" we add a line above or below first.
2843 // Loop twice to avoid duplicating code.
2844 int done = FALSE;
2845 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002847 if (prefer_above ? (round == 2 && below < above)
2848 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002850 // add a line below the cursor
2851 if (boff.lnum < curbuf->b_ml.ml_line_count)
2852 {
2853 botline_forw(&boff);
2854 used += boff.height;
2855 if (used > curwin->w_height)
2856 {
2857 done = TRUE;
2858 break;
2859 }
2860 below += boff.height;
2861 }
2862 else
2863 {
2864 ++below; // count a "~" line
2865 if (atend)
2866 ++used;
2867 }
2868 }
2869
2870 if (prefer_above ? (round == 1 && below >= above)
2871 : (round == 1 && below > above))
2872 {
2873 // add a line above the cursor
2874 topline_back(&loff);
2875 if (loff.height == MAXCOL)
2876 used = MAXCOL;
2877 else
2878 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002880 {
2881 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883 }
2884 above += loff.height;
2885 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002887 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002891 if (done)
2892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#ifdef FEAT_FOLDING
2896 if (!hasFolding(topline, &curwin->w_topline, NULL))
2897#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002898 {
2899 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002900 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002901 || curwin->w_skipcol != 0)
2902 {
2903 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002904 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002905 {
2906 curwin->w_skipcol = skipcol;
2907 redraw_later(UPD_NOT_VALID);
2908 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002909 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002910 reset_skipcol();
2911 }
2912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#ifdef FEAT_DIFF
2914 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002915 if (old_topline > curwin->w_topline + curwin->w_height)
2916 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 check_topfill(curwin, FALSE);
2918#endif
2919 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2920 curwin->w_valid |= VALID_TOPLINE;
2921}
2922
2923/*
2924 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002925 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 * If not possible, put it at the same position as scroll_cursor_halfway().
2927 * When called topline must be valid!
2928 */
2929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002930cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 linenr_T botline;
2936 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002937 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002939 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 /*
2942 * How many lines we would like to have above/below the cursor depends on
2943 * whether the first/last line of the file is on screen.
2944 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002945 above_wanted = so;
2946 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002947 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 above_wanted = mouse_dragging - 1;
2950 below_wanted = mouse_dragging - 1;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (curwin->w_topline == 1)
2953 {
2954 above_wanted = 0;
2955 max_off = curwin->w_height / 2;
2956 if (below_wanted > max_off)
2957 below_wanted = max_off;
2958 }
2959 validate_botline();
2960 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002961 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 below_wanted = 0;
2964 max_off = (curwin->w_height - 1) / 2;
2965 if (above_wanted > max_off)
2966 above_wanted = max_off;
2967 }
2968
2969 /*
2970 * If there are sufficient file-lines above and below the cursor, we can
2971 * return now.
2972 */
2973 cln = curwin->w_cursor.lnum;
2974 if (cln >= curwin->w_topline + above_wanted
2975 && cln < curwin->w_botline - below_wanted
2976#ifdef FEAT_FOLDING
2977 && !hasAnyFolding(curwin)
2978#endif
2979 )
2980 return;
2981
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002982 if (curwin->w_p_sms && !curwin->w_p_wrap)
2983 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002984 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002985 if (curwin->w_cline_height == curwin->w_height)
2986 {
2987 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002988 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002989 return;
2990 }
2991 // TODO: If the cursor line doesn't fit in the window then only adjust
2992 // w_skipcol.
2993 }
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 /*
2996 * Narrow down the area where the cursor can be put by taking lines from
2997 * the top and the bottom until:
2998 * - the desired context lines are found
2999 * - the lines from the top is past the lines from the bottom
3000 */
3001 topline = curwin->w_topline;
3002 botline = curwin->w_botline - 1;
3003#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003004 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 above = curwin->w_topfill;
3006 below = curwin->w_filler_rows;
3007#endif
3008 while ((above < above_wanted || below < below_wanted) && topline < botline)
3009 {
3010 if (below < below_wanted && (below <= above || above >= above_wanted))
3011 {
3012#ifdef FEAT_FOLDING
3013 if (hasFolding(botline, &botline, NULL))
3014 ++below;
3015 else
3016#endif
3017 below += plines(botline);
3018 --botline;
3019 }
3020 if (above < above_wanted && (above < below || below >= below_wanted))
3021 {
3022#ifdef FEAT_FOLDING
3023 if (hasFolding(topline, NULL, &topline))
3024 ++above;
3025 else
3026#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003027 above += PLINES_NOFILL(topline);
3028#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003029 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (topline < botline)
3031 above += diff_check_fill(curwin, topline + 1);
3032#endif
3033 ++topline;
3034 }
3035 }
3036 if (topline == botline || botline == 0)
3037 curwin->w_cursor.lnum = topline;
3038 else if (topline > botline)
3039 curwin->w_cursor.lnum = botline;
3040 else
3041 {
3042 if (cln < topline && curwin->w_topline > 1)
3043 {
3044 curwin->w_cursor.lnum = topline;
3045 curwin->w_valid &=
3046 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3047 }
3048 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3049 {
3050 curwin->w_cursor.lnum = botline;
3051 curwin->w_valid &=
3052 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3053 }
3054 }
3055 curwin->w_valid |= VALID_TOPLINE;
3056}
3057
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003058static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003061 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3062 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003064 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
3066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 long n;
3070 int retval = OK;
3071 lineoff_T loff;
3072 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003073 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaar85a20022019-12-21 18:25:54 +01003075 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
3077 beep_flush();
3078 return FAIL;
3079 }
3080
3081 for ( ; count > 0; --count)
3082 {
3083 validate_botline();
3084 /*
3085 * It's an error to move a page up when the first line is already on
3086 * the screen. It's an error to move a page down when the last line
3087 * is on the screen and the topline is 'scrolloff' lines from the
3088 * last line.
3089 */
3090 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003091 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3093 : (curwin->w_topline == 1
3094#ifdef FEAT_DIFF
3095 && curwin->w_topfill ==
3096 diff_check_fill(curwin, curwin->w_topline)
3097#endif
3098 ))
3099 {
3100 beep_flush();
3101 retval = FAIL;
3102 break;
3103 }
3104
3105#ifdef FEAT_DIFF
3106 loff.fill = 0;
3107#endif
3108 if (dir == FORWARD)
3109 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003110 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003113 if (p_window <= 2)
3114 ++curwin->w_topline;
3115 else
3116 curwin->w_topline += p_window - 2;
3117 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3118 curwin->w_topline = curbuf->b_ml.ml_line_count;
3119 curwin->w_cursor.lnum = curwin->w_topline;
3120 }
3121 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3122 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003123 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 curwin->w_topline = curbuf->b_ml.ml_line_count;
3125#ifdef FEAT_DIFF
3126 curwin->w_topfill = 0;
3127#endif
3128 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3129 }
3130 else
3131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003132 // For the overlap, start with the line just below the window
3133 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 loff.lnum = curwin->w_botline;
3135#ifdef FEAT_DIFF
3136 loff.fill = diff_check_fill(curwin, loff.lnum)
3137 - curwin->w_filler_rows;
3138#endif
3139 get_scroll_overlap(&loff, -1);
3140 curwin->w_topline = loff.lnum;
3141#ifdef FEAT_DIFF
3142 curwin->w_topfill = loff.fill;
3143 check_topfill(curwin, FALSE);
3144#endif
3145 curwin->w_cursor.lnum = curwin->w_topline;
3146 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3147 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3148 }
3149 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003150 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
3152#ifdef FEAT_DIFF
3153 if (curwin->w_topline == 1)
3154 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003155 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 max_topfill();
3157 continue;
3158 }
3159#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003160 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003161 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003162 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003163 if (p_window <= 2)
3164 --curwin->w_topline;
3165 else
3166 curwin->w_topline -= p_window - 2;
3167 if (curwin->w_topline < 1)
3168 curwin->w_topline = 1;
3169 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3170 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3171 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3172 continue;
3173 }
3174
Bram Moolenaar85a20022019-12-21 18:25:54 +01003175 // Find the line at the top of the window that is going to be the
3176 // line at the bottom of the window. Make sure this results in
3177 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 loff.lnum = curwin->w_topline - 1;
3179#ifdef FEAT_DIFF
3180 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3181 - curwin->w_topfill;
3182#endif
3183 get_scroll_overlap(&loff, 1);
3184
3185 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3186 {
3187 loff.lnum = curbuf->b_ml.ml_line_count;
3188#ifdef FEAT_DIFF
3189 loff.fill = 0;
3190 }
3191 else
3192 {
3193 botline_topline(&loff);
3194#endif
3195 }
3196 curwin->w_cursor.lnum = loff.lnum;
3197
Bram Moolenaar85a20022019-12-21 18:25:54 +01003198 // Find the line just above the new topline to get the right line
3199 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 n = 0;
3201 while (n <= curwin->w_height && loff.lnum >= 1)
3202 {
3203 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003204 if (loff.height == MAXCOL)
3205 n = MAXCOL;
3206 else
3207 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003209 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 curwin->w_topline = 1;
3212#ifdef FEAT_DIFF
3213 max_topfill();
3214#endif
3215 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3216 }
3217 else
3218 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003219 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#ifdef FEAT_DIFF
3221 topline_botline(&loff);
3222#endif
3223 botline_forw(&loff);
3224 botline_forw(&loff);
3225#ifdef FEAT_DIFF
3226 botline_topline(&loff);
3227#endif
3228#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003229 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3231#endif
3232
Bram Moolenaar85a20022019-12-21 18:25:54 +01003233 // Always scroll at least one line. Avoid getting stuck on
3234 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (loff.lnum >= curwin->w_topline
3236#ifdef FEAT_DIFF
3237 && (loff.lnum > curwin->w_topline
3238 || loff.fill >= curwin->w_topfill)
3239#endif
3240 )
3241 {
3242#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003243 // First try using the maximum number of filler lines. If
3244 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 loff.fill = curwin->w_topfill;
3246 if (curwin->w_topfill < diff_check_fill(curwin,
3247 curwin->w_topline))
3248 max_topfill();
3249 if (curwin->w_topfill == loff.fill)
3250#endif
3251 {
3252 --curwin->w_topline;
3253#ifdef FEAT_DIFF
3254 curwin->w_topfill = 0;
3255#endif
3256 }
3257 comp_botline(curwin);
3258 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003259 curwin->w_valid &=
3260 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else
3263 {
3264 curwin->w_topline = loff.lnum;
3265#ifdef FEAT_DIFF
3266 curwin->w_topfill = loff.fill;
3267 check_topfill(curwin, FALSE);
3268#endif
3269 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3270 }
3271 }
3272 }
3273 }
3274#ifdef FEAT_FOLDING
3275 foldAdjustCursor();
3276#endif
3277 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003278 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003279 if (retval == OK)
3280 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3282
Bram Moolenaar907dad72018-07-10 15:07:15 +02003283 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003285 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3286 // But make sure we scroll at least one line (happens with mix of long
3287 // wrapping lines and non-wrapping line).
3288 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003290 scroll_cursor_top(1, FALSE);
3291 if (curwin->w_topline <= old_topline
3292 && old_topline < curbuf->b_ml.ml_line_count)
3293 {
3294 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003296 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3297#endif
3298 }
3299 }
3300#ifdef FEAT_FOLDING
3301 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003306 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return retval;
3308}
3309
3310/*
3311 * Decide how much overlap to use for page-up or page-down scrolling.
3312 * This is symmetric, so that doing both keeps the same lines displayed.
3313 * Three lines are examined:
3314 *
3315 * before CTRL-F after CTRL-F / before CTRL-B
3316 * etc. l1
3317 * l1 last but one line ------------
3318 * l2 last text line l2 top text line
3319 * ------------- l3 second text line
3320 * l3 etc.
3321 */
3322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003323get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 int h1, h2, h3, h4;
3326 int min_height = curwin->w_height - 2;
3327 lineoff_T loff0, loff1, loff2;
3328
3329#ifdef FEAT_DIFF
3330 if (lp->fill > 0)
3331 lp->height = 1;
3332 else
3333 lp->height = plines_nofill(lp->lnum);
3334#else
3335 lp->height = plines(lp->lnum);
3336#endif
3337 h1 = lp->height;
3338 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 loff0 = *lp;
3342 if (dir > 0)
3343 botline_forw(lp);
3344 else
3345 topline_back(lp);
3346 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003347 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003349 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return;
3351 }
3352
3353 loff1 = *lp;
3354 if (dir > 0)
3355 botline_forw(lp);
3356 else
3357 topline_back(lp);
3358 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003359 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003361 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 return;
3363 }
3364
3365 loff2 = *lp;
3366 if (dir > 0)
3367 botline_forw(lp);
3368 else
3369 topline_back(lp);
3370 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003371 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003372 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003374 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
3378 * Scroll 'scroll' lines up or down.
3379 */
3380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003381halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 long scrolled = 0;
3384 int i;
3385 int n;
3386 int room;
3387
3388 if (Prenum)
3389 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3390 curwin->w_height : Prenum;
3391 n = (curwin->w_p_scr <= curwin->w_height) ?
3392 curwin->w_p_scr : curwin->w_height;
3393
Bram Moolenaard5d37532017-03-27 23:02:07 +02003394 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 validate_botline();
3396 room = curwin->w_empty_rows;
3397#ifdef FEAT_DIFF
3398 room += curwin->w_filler_rows;
3399#endif
3400 if (flag)
3401 {
3402 /*
3403 * scroll the text up
3404 */
3405 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3406 {
3407#ifdef FEAT_DIFF
3408 if (curwin->w_topfill > 0)
3409 {
3410 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003411 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 --curwin->w_topfill;
3413 }
3414 else
3415#endif
3416 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003417 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 n -= i;
3419 if (n < 0 && scrolled > 0)
3420 break;
3421#ifdef FEAT_FOLDING
3422 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3423#endif
3424 ++curwin->w_topline;
3425#ifdef FEAT_DIFF
3426 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3427#endif
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3430 {
3431 ++curwin->w_cursor.lnum;
3432 curwin->w_valid &=
3433 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3437 scrolled += i;
3438
3439 /*
3440 * Correct w_botline for changed w_topline.
3441 * Won't work when there are filler lines.
3442 */
3443#ifdef FEAT_DIFF
3444 if (curwin->w_p_diff)
3445 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3446 else
3447#endif
3448 {
3449 room += i;
3450 do
3451 {
3452 i = plines(curwin->w_botline);
3453 if (i > room)
3454 break;
3455#ifdef FEAT_FOLDING
3456 (void)hasFolding(curwin->w_botline, NULL,
3457 &curwin->w_botline);
3458#endif
3459 ++curwin->w_botline;
3460 room -= i;
3461 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3462 }
3463 }
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /*
3466 * When hit bottom of the file: move cursor down.
3467 */
3468 if (n > 0)
3469 {
3470# ifdef FEAT_FOLDING
3471 if (hasAnyFolding(curwin))
3472 {
3473 while (--n >= 0
3474 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3475 {
3476 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3477 &curwin->w_cursor.lnum);
3478 ++curwin->w_cursor.lnum;
3479 }
3480 }
3481 else
3482# endif
3483 curwin->w_cursor.lnum += n;
3484 check_cursor_lnum();
3485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 else
3488 {
3489 /*
3490 * scroll the text down
3491 */
3492 while (n > 0 && curwin->w_topline > 1)
3493 {
3494#ifdef FEAT_DIFF
3495 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3496 {
3497 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003498 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 ++curwin->w_topfill;
3500 }
3501 else
3502#endif
3503 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003504 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 n -= i;
3506 if (n < 0 && scrolled > 0)
3507 break;
3508 --curwin->w_topline;
3509#ifdef FEAT_FOLDING
3510 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3511#endif
3512#ifdef FEAT_DIFF
3513 curwin->w_topfill = 0;
3514#endif
3515 }
3516 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3517 VALID_BOTLINE|VALID_BOTLINE_AP);
3518 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (curwin->w_cursor.lnum > 1)
3520 {
3521 --curwin->w_cursor.lnum;
3522 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /*
3527 * When hit top of the file: move cursor up.
3528 */
3529 if (n > 0)
3530 {
3531 if (curwin->w_cursor.lnum <= (linenr_T)n)
3532 curwin->w_cursor.lnum = 1;
3533 else
3534# ifdef FEAT_FOLDING
3535 if (hasAnyFolding(curwin))
3536 {
3537 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3538 {
3539 --curwin->w_cursor.lnum;
3540 (void)hasFolding(curwin->w_cursor.lnum,
3541 &curwin->w_cursor.lnum, NULL);
3542 }
3543 }
3544 else
3545# endif
3546 curwin->w_cursor.lnum -= n;
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003550 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 foldAdjustCursor();
3552# endif
3553#ifdef FEAT_DIFF
3554 check_topfill(curwin, !flag);
3555#endif
3556 cursor_correct();
3557 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003558 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560
Bram Moolenaar860cae12010-06-05 23:22:07 +02003561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003562do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563{
3564 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003565 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003566 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003567 colnr_T curswant = curwin->w_curswant;
3568 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 win_T *old_curwin = curwin;
3570 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 int old_VIsual_select = VIsual_select;
3572 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573
3574 /*
3575 * loop through the cursorbound windows
3576 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003578 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 {
3580 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003581 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582 if (curwin != old_curwin && curwin->w_p_crb)
3583 {
3584# ifdef FEAT_DIFF
3585 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003586 curwin->w_cursor.lnum =
3587 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003588 else
3589# endif
3590 curwin->w_cursor.lnum = line;
3591 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003592 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003593 curwin->w_curswant = curswant;
3594 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595
Bram Moolenaar85a20022019-12-21 18:25:54 +01003596 // Make sure the cursor is in a valid position. Temporarily set
3597 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003598 int restart_edit_save = restart_edit;
3599 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003601
3602 // Avoid a scroll here for the cursor position, 'scrollbind' is
3603 // more important.
3604 if (!curwin->w_p_scb)
3605 validate_cursor();
3606
Bram Moolenaar61452852011-02-01 18:01:11 +01003607 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003608 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 if (has_mbyte)
3610 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003611 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003612
Bram Moolenaar85a20022019-12-21 18:25:54 +01003613 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003614 if (!curwin->w_p_scb)
3615 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003616 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 }
3618 }
3619
3620 /*
3621 * reset current-window
3622 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003623 VIsual_select = old_VIsual_select;
3624 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625 curwin = old_curwin;
3626 curbuf = old_curbuf;
3627}