blob: 96692ba7d75e67271e92f7e9acaf874752ac4be0 [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;
zeertzjqbfe377b2023-08-17 22:58:53 +02001359 if (n > 0)
1360 curwin->w_skipcol = width1 + (n - 1) * width2;
1361 else
1362 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 else if (extra == 1)
1365 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001366 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001367 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1368 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (extra > 0)
1370 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001371 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1372 extra = curwin->w_skipcol / width2;
1373 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 }
1376 else if (extra == 2)
1377 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001378 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001381 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (endcol > curwin->w_skipcol)
1383 curwin->w_skipcol = endcol;
1384 }
1385
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001386 // adjust w_wrow for the changed w_skipcol
1387 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001388 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001389 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001390 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (curwin->w_wrow >= curwin->w_height)
1393 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001394 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001396 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 curwin->w_wrow -= extra;
1398 }
1399
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001400 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (extra > 0)
1402 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1403 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001404 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001406 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 curwin->w_skipcol = 0;
1408 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001409 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001411#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001412 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001413#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001414#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1415 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1416 {
1417 curwin->w_wrow += popup_top_extra(curwin);
1418 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001419 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001420 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001421 else
1422 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001423#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001424
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001425 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1426 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001427 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001428 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1431}
1432
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001433#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001434/*
1435 * Compute the screen position of text character at "pos" in window "wp"
1436 * The resulting values are one-based, zero when character is not visible.
1437 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001438 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439textpos2screenpos(
1440 win_T *wp,
1441 pos_T *pos,
1442 int *rowp, // screen row
1443 int *scolp, // start screen column
1444 int *ccolp, // cursor screen column
1445 int *ecolp) // end screen column
1446{
1447 colnr_T scol = 0, ccol = 0, ecol = 0;
1448 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001449 colnr_T coloff = 0;
1450
Bram Moolenaar189663b2021-07-21 18:04:56 +02001451 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001452 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001453 colnr_T col;
1454 int width;
1455 linenr_T lnum = pos->lnum;
1456#ifdef FEAT_FOLDING
1457 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001458
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001459 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1460#endif
zeertzjqbfe377b2023-08-17 22:58:53 +02001461 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE) + 1;
1462 // "row" should be the screen line where line "lnum" begins, which can
1463 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
1464 row = adjust_plines_for_skipcol(wp, row);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001465
1466#ifdef FEAT_DIFF
1467 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001468 row += lnum == wp->w_topline ? wp->w_topfill
1469 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001470#endif
1471
zeertzjqba2d1912022-12-18 12:28:59 +00001472 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001473#ifdef FEAT_FOLDING
1474 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001475 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001476 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001477 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001478 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001479 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001480#endif
1481 {
1482 getvcol(wp, pos, &scol, &ccol, &ecol);
1483
1484 // similar to what is done in validate_cursor_col()
1485 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001486 col += off;
1487 width = wp->w_width - off + win_col_off2(wp);
1488
1489 // long line wrapping, adjust row
1490 if (wp->w_p_wrap
1491 && col >= (colnr_T)wp->w_width
1492 && width > 0)
1493 {
1494 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001495 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001496 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001497 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001498 }
1499 col -= wp->w_leftcol;
1500 if (col >= wp->w_width)
1501 col = -1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001502 if (col >= 0 && row > 0 && row <= wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001503 {
1504 coloff = col - scol + wp->w_wincol + 1;
1505 row += W_WINROW(wp);
1506 }
1507 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001508 // character is out of the window
1509 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001510 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001511 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001512 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001513 *scolp = scol + coloff;
1514 *ccolp = ccol + coloff;
1515 *ecolp = ecol + coloff;
1516}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001517#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518
Bram Moolenaar12034e22019-08-25 22:25:02 +02001519#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001520/*
1521 * "screenpos({winid}, {lnum}, {col})" function
1522 */
1523 void
1524f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1525{
1526 dict_T *dict;
1527 win_T *wp;
1528 pos_T pos;
1529 int row = 0;
1530 int scol = 0, ccol = 0, ecol = 0;
1531
Bram Moolenaar93a10962022-06-16 11:42:09 +01001532 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001533 return;
1534 dict = rettv->vval.v_dict;
1535
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001536 if (in_vim9script()
1537 && (check_for_number_arg(argvars, 0) == FAIL
1538 || check_for_number_arg(argvars, 1) == FAIL
1539 || check_for_number_arg(argvars, 2) == FAIL))
1540 return;
1541
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001542 wp = find_win_by_nr_or_id(&argvars[0]);
1543 if (wp == NULL)
1544 return;
1545
1546 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001547 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1548 {
1549 semsg(_(e_invalid_line_number_nr), pos.lnum);
1550 return;
1551 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001552 pos.col = tv_get_number(&argvars[2]) - 1;
1553 pos.coladd = 0;
1554 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1555
1556 dict_add_number(dict, "row", row);
1557 dict_add_number(dict, "col", scol);
1558 dict_add_number(dict, "curscol", ccol);
1559 dict_add_number(dict, "endcol", ecol);
1560}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001561
1562/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001563 * Convert a virtual (screen) column to a character column. The first column
1564 * is one. For a multibyte character, the column number of the first byte is
1565 * returned.
1566 */
1567 static int
1568virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1569{
1570 int offset = vcol2col(wp, lnum, vcol);
1571 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1572 char_u *p = line + offset;
1573
1574 // For a multibyte character, need to return the column number of the first
1575 // byte.
1576 MB_PTR_BACK(line, p);
1577
1578 return (int)(p - line + 1);
1579}
1580
1581/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001582 * "virtcol2col({winid}, {lnum}, {col})" function
1583 */
1584 void
1585f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1586{
1587 win_T *wp;
1588 linenr_T lnum;
1589 int screencol;
1590 int error = FALSE;
1591
1592 rettv->vval.v_number = -1;
1593
1594 if (check_for_number_arg(argvars, 0) == FAIL
1595 || check_for_number_arg(argvars, 1) == FAIL
1596 || check_for_number_arg(argvars, 2) == FAIL)
1597 return;
1598
1599 wp = find_win_by_nr_or_id(&argvars[0]);
1600 if (wp == NULL)
1601 return;
1602
1603 lnum = tv_get_number_chk(&argvars[1], &error);
1604 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1605 return;
1606
1607 screencol = tv_get_number_chk(&argvars[2], &error);
1608 if (error || screencol < 0)
1609 return;
1610
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001611 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001612}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001613#endif
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001619scrolldown(
1620 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001621 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001623 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int wrow;
1625 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001626 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001627 int width1 = 0;
1628 int width2 = 0;
1629
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001630 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001631 {
1632 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001633 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636#ifdef FEAT_FOLDING
1637 linenr_T first;
1638
Bram Moolenaar85a20022019-12-21 18:25:54 +01001639 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1641#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001642 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001643 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001646 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1647 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 ++curwin->w_topfill;
1650 ++done;
1651 }
1652 else
1653#endif
1654 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001655 // break when at the very top
1656 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001657 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001659 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001661 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001662 if (curwin->w_skipcol >= width1 + width2)
1663 curwin->w_skipcol -= width2;
1664 else
1665 curwin->w_skipcol -= width1;
1666 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001670 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001671 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001672 --curwin->w_topline;
1673 curwin->w_skipcol = 0;
1674#ifdef FEAT_DIFF
1675 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001677#ifdef FEAT_FOLDING
1678 // A sequence of folded lines only counts for one logical line
1679 if (hasFolding(curwin->w_topline, &first, NULL))
1680 {
1681 ++done;
1682 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001683 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001684 curwin->w_botline -= curwin->w_topline - first;
1685 curwin->w_topline = first;
1686 }
1687 else
1688#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001689 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001690 {
1691 int size = win_linetabsize(curwin, curwin->w_topline,
1692 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1693 if (size > width1)
1694 {
1695 curwin->w_skipcol = width1;
1696 size -= width1;
1697 redraw_later(UPD_NOT_VALID);
1698 }
1699 while (size > width2)
1700 {
1701 curwin->w_skipcol += width2;
1702 size -= width2;
1703 }
1704 ++done;
1705 }
1706 else
1707 done += PLINES_NOFILL(curwin->w_topline);
1708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 invalidate_botline();
1712 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001713 curwin->w_wrow += done; // keep w_wrow updated
1714 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
1716#ifdef FEAT_DIFF
1717 if (curwin->w_cursor.lnum == curwin->w_topline)
1718 curwin->w_cline_row = 0;
1719 check_topfill(curwin, TRUE);
1720#endif
1721
1722 /*
1723 * Compute the row number of the last row of the cursor line
1724 * and move the cursor onto the displayed part of the window.
1725 */
1726 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001727 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
1729 validate_virtcol();
1730 validate_cheight();
1731 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001732 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1735 {
1736#ifdef FEAT_FOLDING
1737 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1738 {
1739 --wrow;
1740 if (first == 1)
1741 curwin->w_cursor.lnum = 1;
1742 else
1743 curwin->w_cursor.lnum = first - 1;
1744 }
1745 else
1746#endif
1747 wrow -= plines(curwin->w_cursor.lnum--);
1748 curwin->w_valid &=
1749 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1750 moved = TRUE;
1751 }
1752 if (moved)
1753 {
1754#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001755 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 foldAdjustCursor();
1757#endif
1758 coladvance(curwin->w_curswant);
1759 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001760
1761 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1762 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001763 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001764 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1765
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001766 // make sure the cursor is in the visible text
1767 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001768 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001769 int row = 0;
1770 if (col >= width1)
1771 {
1772 col -= width1;
1773 ++row;
1774 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001775 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001776 {
1777 row += col / width2;
1778 col = col % width2;
1779 }
1780 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001781 {
1782 curwin->w_curswant = curwin->w_virtcol
1783 - (row - curwin->w_height + 1) * width2;
1784 coladvance(curwin->w_curswant);
1785 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787}
1788
1789/*
1790 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001793scrollup(
1794 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001797 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001799 if (do_sms
1800# ifdef FEAT_FOLDING
1801 || (byfold && hasAnyFolding(curwin))
1802# endif
1803# ifdef FEAT_DIFF
1804 || (curwin->w_p_diff && !curwin->w_p_wrap)
1805# endif
1806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 int width1 = curwin->w_width - curwin_col_off();
1809 int width2 = width1 + curwin_col_off2();
1810 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001811 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001812
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001813 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001814 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001815
1816 // diff mode: first consume "topfill"
1817 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1818 // the line, then advance to the next line.
1819 // folding: count each sequence of folded lines as one logical line.
1820 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
1822# ifdef FEAT_DIFF
1823 if (curwin->w_topfill > 0)
1824 --curwin->w_topfill;
1825 else
1826# endif
1827 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001828 linenr_T lnum = curwin->w_topline;
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830# ifdef FEAT_FOLDING
1831 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001832 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 (void)hasFolding(lnum, NULL, &lnum);
1834# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001835 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001836 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001837 // 'smoothscroll': increase "w_skipcol" until it goes over
1838 // the end of the line, then advance to the next line.
1839 int add = curwin->w_skipcol > 0 ? width2 : width1;
1840 curwin->w_skipcol += add;
1841 if (curwin->w_skipcol >= size)
1842 {
1843 if (lnum == curbuf->b_ml.ml_line_count)
1844 {
1845 // at the last screen line, can't scroll further
1846 curwin->w_skipcol -= add;
1847 break;
1848 }
1849 ++lnum;
1850 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001851 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001852 else
1853 {
1854 if (lnum >= curbuf->b_ml.ml_line_count)
1855 break;
1856 ++lnum;
1857 }
1858
1859 if (lnum > curwin->w_topline)
1860 {
1861 // approximate w_botline
1862 curwin->w_botline += lnum - curwin->w_topline;
1863 curwin->w_topline = lnum;
1864# ifdef FEAT_DIFF
1865 curwin->w_topfill = diff_check_fill(curwin, lnum);
1866# endif
1867 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001868 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001869 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001870 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001871 }
1872 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001873
zeertzjqd9a92dc2023-06-05 18:41:35 +01001874 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1875 // need to redraw more, because wl_size of the (new) topline may
1876 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001877 redraw_later(UPD_NOT_VALID);
1878 }
1879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001882 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884
1885 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1886 curwin->w_topline = curbuf->b_ml.ml_line_count;
1887 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1888 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1889
1890#ifdef FEAT_DIFF
1891 check_topfill(curwin, FALSE);
1892#endif
1893
1894#ifdef FEAT_FOLDING
1895 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001896 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1898#endif
1899
1900 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1901 if (curwin->w_cursor.lnum < curwin->w_topline)
1902 {
1903 curwin->w_cursor.lnum = curwin->w_topline;
1904 curwin->w_valid &=
1905 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1906 coladvance(curwin->w_curswant);
1907 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 if (curwin->w_cursor.lnum == curwin->w_topline
1909 && do_sms && curwin->w_skipcol > 0)
1910 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001911 int col_off = curwin_col_off();
1912 int col_off2 = curwin_col_off2();
1913
1914 int width1 = curwin->w_width - col_off;
1915 int width2 = width1 + col_off2;
1916 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001917 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001918 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001919 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001920
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001921 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001922 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001923 int overlap = scrolloff_cols != 0 ? 0
1924 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001925
Bram Moolenaar118c2352022-10-09 17:19:27 +01001926 // Make sure the cursor is in a visible part of the line, taking
1927 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001928 // If there are not enough screen lines put the cursor in the middle.
1929 if (scrolloff_cols > space_cols / 2)
1930 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001931 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001932 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001933 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001934 colnr_T col = curwin->w_virtcol;
1935
1936 if (col < width1)
1937 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001938 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001939 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001940 curwin->w_curswant = col;
1941 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001942
1943 // validate_virtcol() marked various things as valid, but after
1944 // moving the cursor they need to be recomputed
1945 curwin->w_valid &=
1946 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001947 }
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001951/*
1952 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1953 * valid for 'smoothscroll'.
1954 */
1955 void
1956adjust_skipcol(void)
1957{
1958 if (!curwin->w_p_wrap
1959 || !curwin->w_p_sms
1960 || curwin->w_cursor.lnum != curwin->w_topline)
1961 return;
1962
1963 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001964 if (width1 <= 0)
1965 return; // no text will be displayed
1966
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001967 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001968 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001969 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1970 int scrolled = FALSE;
1971
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001972 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001973 if (curwin->w_cline_height == curwin->w_height
1974 // w_cline_height may be capped at w_height, check there aren't
1975 // actually more lines.
1976 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1977 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001978 {
1979 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001980 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001981 return;
1982 }
1983
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001984 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001985 int overlap = sms_marker_overlap(curwin,
1986 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001987 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001988 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001989 {
1990 // scroll a screen line down
1991 if (curwin->w_skipcol >= width1 + width2)
1992 curwin->w_skipcol -= width2;
1993 else
1994 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001995 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001996 }
1997 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001998 {
1999 validate_virtcol();
2000 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002001 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01002002 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002003
2004 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
2005 int row = 0;
2006 if (col >= width1)
2007 {
2008 col -= width1;
2009 ++row;
2010 }
2011 if (col > width2)
2012 {
2013 row += col / width2;
2014 col = col % width2;
2015 }
2016 if (row >= curwin->w_height)
2017 {
2018 if (curwin->w_skipcol == 0)
2019 {
2020 curwin->w_skipcol += width1;
2021 --row;
2022 }
2023 if (row >= curwin->w_height)
2024 curwin->w_skipcol += (row - curwin->w_height) * width2;
2025 redraw_later(UPD_NOT_VALID);
2026 }
2027}
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#ifdef FEAT_DIFF
2030/*
2031 * Don't end up with too many filler lines in the window.
2032 */
2033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034check_topfill(
2035 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002036 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 int n;
2039
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002040 if (wp->w_topfill <= 0)
2041 return;
2042
2043 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2044 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002046 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002048 --wp->w_topline;
2049 wp->w_topfill = 0;
2050 }
2051 else
2052 {
2053 wp->w_topfill = wp->w_height - n;
2054 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057 }
2058}
2059
2060/*
2061 * Use as many filler lines as possible for w_topline. Make sure w_topline
2062 * is still visible.
2063 */
2064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002065max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 int n;
2068
2069 n = plines_nofill(curwin->w_topline);
2070 if (n >= curwin->w_height)
2071 curwin->w_topfill = 0;
2072 else
2073 {
2074 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2075 if (curwin->w_topfill + n > curwin->w_height)
2076 curwin->w_topfill = curwin->w_height - n;
2077 }
2078}
2079#endif
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081/*
2082 * Scroll the screen one line down, but don't do it if it would move the
2083 * cursor off the screen.
2084 */
2085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002086scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 int end_row;
2089#ifdef FEAT_DIFF
2090 int can_fill = (curwin->w_topfill
2091 < diff_check_fill(curwin, curwin->w_topline));
2092#endif
2093
2094 if (curwin->w_topline <= 1
2095#ifdef FEAT_DIFF
2096 && !can_fill
2097#endif
2098 )
2099 return;
2100
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 /*
2104 * Compute the row number of the last row of the cursor line
2105 * and make sure it doesn't go off the screen. Make sure the cursor
2106 * doesn't go past 'scrolloff' lines from the screen end.
2107 */
2108 end_row = curwin->w_wrow;
2109#ifdef FEAT_DIFF
2110 if (can_fill)
2111 ++end_row;
2112 else
2113 end_row += plines_nofill(curwin->w_topline - 1);
2114#else
2115 end_row += plines(curwin->w_topline - 1);
2116#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002117 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 validate_cheight();
2120 validate_virtcol();
2121 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002122 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002124 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126#ifdef FEAT_DIFF
2127 if (can_fill)
2128 {
2129 ++curwin->w_topfill;
2130 check_topfill(curwin, TRUE);
2131 }
2132 else
2133 {
2134 --curwin->w_topline;
2135 curwin->w_topfill = 0;
2136 }
2137#else
2138 --curwin->w_topline;
2139#endif
2140#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002141 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2145 }
2146}
2147
2148/*
2149 * Scroll the screen one line up, but don't do it if it would move the cursor
2150 * off the screen.
2151 */
2152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002153scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int start_row;
2156
2157 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2158#ifdef FEAT_DIFF
2159 && curwin->w_topfill == 0
2160#endif
2161 )
2162 return;
2163
Bram Moolenaar85a20022019-12-21 18:25:54 +01002164 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166 /*
2167 * Compute the row number of the first row of the cursor line
2168 * and make sure it doesn't go off the screen. Make sure the cursor
2169 * doesn't go before 'scrolloff' lines from the screen start.
2170 */
2171#ifdef FEAT_DIFF
2172 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2173 - curwin->w_topfill;
2174#else
2175 start_row = curwin->w_wrow - plines(curwin->w_topline);
2176#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002177 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002180 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002182 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
2184#ifdef FEAT_DIFF
2185 if (curwin->w_topfill > 0)
2186 --curwin->w_topfill;
2187 else
2188#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002189 {
2190#ifdef FEAT_FOLDING
2191 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002194 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2197 }
2198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200/*
2201 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2202 * a (wrapped) text line. Uses and sets "lp->fill".
2203 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002204 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 */
2206 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002207topline_back_winheight(
2208 lineoff_T *lp,
2209 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211#ifdef FEAT_DIFF
2212 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 ++lp->fill;
2216 lp->height = 1;
2217 }
2218 else
2219#endif
2220 {
2221 --lp->lnum;
2222#ifdef FEAT_DIFF
2223 lp->fill = 0;
2224#endif
2225 if (lp->lnum < 1)
2226 lp->height = MAXCOL;
2227 else
2228#ifdef FEAT_FOLDING
2229 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 lp->height = 1;
2232 else
2233#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002234 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236}
2237
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002238 static void
2239topline_back(lineoff_T *lp)
2240{
2241 topline_back_winheight(lp, TRUE);
2242}
2243
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245/*
2246 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2247 * a (wrapped) text line. Uses and sets "lp->fill".
2248 * Returns the height of the added line in "lp->height".
2249 * Lines below the last one are incredibly high.
2250 */
2251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002252botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254#ifdef FEAT_DIFF
2255 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2256 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 ++lp->fill;
2259 lp->height = 1;
2260 }
2261 else
2262#endif
2263 {
2264 ++lp->lnum;
2265#ifdef FEAT_DIFF
2266 lp->fill = 0;
2267#endif
2268 if (lp->lnum > curbuf->b_ml.ml_line_count)
2269 lp->height = MAXCOL;
2270 else
2271#ifdef FEAT_FOLDING
2272 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 lp->height = 1;
2275 else
2276#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002277 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279}
2280
2281#ifdef FEAT_DIFF
2282/*
2283 * Switch from including filler lines below lp->lnum to including filler
2284 * lines above loff.lnum + 1. This keeps pointing to the same line.
2285 * When there are no filler lines nothing changes.
2286 */
2287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002288botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 if (lp->fill > 0)
2291 {
2292 ++lp->lnum;
2293 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2294 }
2295}
2296
2297/*
2298 * Switch from including filler lines above lp->lnum to including filler
2299 * lines below loff.lnum - 1. This keeps pointing to the same line.
2300 * When there are no filler lines nothing changes.
2301 */
2302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002303topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 if (lp->fill > 0)
2306 {
2307 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2308 --lp->lnum;
2309 }
2310}
2311#endif
2312
2313/*
2314 * Recompute topline to put the cursor at the top of the window.
2315 * Scroll at least "min_scroll" lines.
2316 * If "always" is TRUE, always set topline (for "zt").
2317 */
2318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002319scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 int scrolled = 0;
2322 int extra = 0;
2323 int used;
2324 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002325 linenr_T top; // just above displayed lines
2326 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002328 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_DIFF
2330 linenr_T old_topfill = curwin->w_topfill;
2331#endif
2332 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002333 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (mouse_dragging > 0)
2336 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /*
2339 * Decrease topline until:
2340 * - it has become 1
2341 * - (part of) the cursor line is moved off the screen or
2342 * - moved at least 'scrolljump' lines and
2343 * - at least 'scrolloff' lines above and below the cursor
2344 */
2345 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002346 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (curwin->w_cursor.lnum < curwin->w_topline)
2348 scrolled = used;
2349
2350#ifdef FEAT_FOLDING
2351 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2352 {
2353 --top;
2354 ++bot;
2355 }
2356 else
2357#endif
2358 {
2359 top = curwin->w_cursor.lnum - 1;
2360 bot = curwin->w_cursor.lnum + 1;
2361 }
2362 new_topline = top + 1;
2363
2364#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002365 // "used" already contains the number of filler lines above, don't add it
2366 // again.
2367 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002368 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369#endif
2370
2371 /*
2372 * Check if the lines from "top" to "bot" fit in the window. If they do,
2373 * set new_topline and advance "top" and "bot" to include more lines.
2374 */
2375 while (top > 0)
2376 {
2377#ifdef FEAT_FOLDING
2378 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002379 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 i = 1;
2381 else
2382#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002383 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002384 if (top < curwin->w_topline)
2385 scrolled += i;
2386
2387 // If scrolling is needed, scroll at least 'sj' lines.
2388 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2389 && extra >= off)
2390 break;
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 used += i;
2393 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2394 {
2395#ifdef FEAT_FOLDING
2396 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002397 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 ++used;
2399 else
2400#endif
2401 used += plines(bot);
2402 }
2403 if (used > curwin->w_height)
2404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 extra += i;
2407 new_topline = top;
2408 --top;
2409 ++bot;
2410 }
2411
2412 /*
2413 * If we don't have enough space, put cursor in the middle.
2414 * This makes sure we get the same position when using "k" and "j"
2415 * in a small window.
2416 */
2417 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002418 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else
2420 {
2421 /*
2422 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002423 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 if (new_topline < curwin->w_topline || always)
2426 curwin->w_topline = new_topline;
2427 if (curwin->w_topline > curwin->w_cursor.lnum)
2428 curwin->w_topline = curwin->w_cursor.lnum;
2429#ifdef FEAT_DIFF
2430 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2431 if (curwin->w_topfill > 0 && extra > off)
2432 {
2433 curwin->w_topfill -= extra - off;
2434 if (curwin->w_topfill < 0)
2435 curwin->w_topfill = 0;
2436 }
2437 check_topfill(curwin, FALSE);
2438#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002439 if (curwin->w_topline == curwin->w_cursor.lnum)
2440 {
2441 validate_virtcol();
2442 if (curwin->w_skipcol >= curwin->w_virtcol)
2443 // TODO: if the line doesn't fit may optimize w_skipcol instead
2444 // of making it zero
2445 reset_skipcol();
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002448 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#ifdef FEAT_DIFF
2450 || curwin->w_topfill != old_topfill
2451#endif
2452 )
2453 curwin->w_valid &=
2454 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2455 curwin->w_valid |= VALID_TOPLINE;
2456 }
2457}
2458
2459/*
2460 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2461 * screen lines for text lines.
2462 */
2463 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002464set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466#ifdef FEAT_DIFF
2467 wp->w_filler_rows = 0;
2468#endif
2469 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002470 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else
2472 {
2473 wp->w_empty_rows = wp->w_height - used;
2474#ifdef FEAT_DIFF
2475 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2476 {
2477 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2478 if (wp->w_empty_rows > wp->w_filler_rows)
2479 wp->w_empty_rows -= wp->w_filler_rows;
2480 else
2481 {
2482 wp->w_filler_rows = wp->w_empty_rows;
2483 wp->w_empty_rows = 0;
2484 }
2485 }
2486#endif
2487 }
2488}
2489
2490/*
2491 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002492 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2494 * This is messy stuff!!!
2495 */
2496 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002497scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499 int used;
2500 int scrolled = 0;
2501 int extra = 0;
2502 int i;
2503 linenr_T line_count;
2504 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002505 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 lineoff_T loff;
2507 lineoff_T boff;
2508#ifdef FEAT_DIFF
2509 int old_topfill = curwin->w_topfill;
2510 int fill_below_window;
2511#endif
2512 linenr_T old_botline = curwin->w_botline;
2513 linenr_T old_valid = curwin->w_valid;
2514 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002515 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002516 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002517 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 cln = curwin->w_cursor.lnum;
2520 if (set_topbot)
2521 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002522 int set_skipcol = FALSE;
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 used = 0;
2525 curwin->w_botline = cln + 1;
2526#ifdef FEAT_DIFF
2527 loff.fill = 0;
2528#endif
2529 for (curwin->w_topline = curwin->w_botline;
2530 curwin->w_topline > 1;
2531 curwin->w_topline = loff.lnum)
2532 {
2533 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002534 topline_back_winheight(&loff, FALSE);
2535 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002537 if (used + loff.height > curwin->w_height)
2538 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002539 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002540 {
2541 // 'smoothscroll' and 'wrap' are set. The above line is
2542 // too long to show in its entirety, so we show just a part
2543 // of it.
2544 if (used < curwin->w_height)
2545 {
2546 int plines_offset = used + loff.height
2547 - curwin->w_height;
2548 used = curwin->w_height;
2549#ifdef FEAT_DIFF
2550 curwin->w_topfill = loff.fill;
2551#endif
2552 curwin->w_topline = loff.lnum;
2553 curwin->w_skipcol = skipcol_from_plines(
2554 curwin, plines_offset);
2555 set_skipcol = TRUE;
2556 }
2557 }
2558 break;
2559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 used += loff.height;
2561#ifdef FEAT_DIFF
2562 curwin->w_topfill = loff.fill;
2563#endif
2564 }
2565 set_empty_rows(curwin, used);
2566 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2567 if (curwin->w_topline != old_topline
2568#ifdef FEAT_DIFF
2569 || curwin->w_topfill != old_topfill
2570#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002571 || set_skipcol
2572 || curwin->w_skipcol != 0)
2573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002575 if (set_skipcol)
2576 redraw_later(UPD_NOT_VALID);
2577 else
2578 reset_skipcol();
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 else
2582 validate_botline();
2583
Bram Moolenaar85a20022019-12-21 18:25:54 +01002584 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585#ifdef FEAT_DIFF
2586 used = plines_nofill(cln);
2587#else
2588 validate_cheight();
2589 used = curwin->w_cline_height;
2590#endif
2591
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002592 // If the cursor is on or below botline, we will at least scroll by the
2593 // height of the cursor line, which is "used". Correct for empty lines,
2594 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (cln >= curwin->w_botline)
2596 {
2597 scrolled = used;
2598 if (cln == curwin->w_botline)
2599 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002600 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002601 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002602 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002603 // Calculate how many screen lines the current top line of window
2604 // occupies. If it is occupying more than the entire window, we
2605 // need to scroll the additional clipped lines to scroll past the
2606 // top line before we can move on to the other lines.
2607 int top_plines =
2608#ifdef FEAT_DIFF
2609 plines_win_nofill
2610#else
2611 plines_win
2612#endif
2613 (curwin, curwin->w_topline, FALSE);
2614 int skip_lines = 0;
2615 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002616 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002617 {
fullwaywang8154e642023-06-24 21:58:09 +01002618 int width2 = width1 + curwin_col_off2();
2619 // similar formula is used in curs_columns()
2620 if (curwin->w_skipcol > width1)
2621 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2622 else if (curwin->w_skipcol > 0)
2623 skip_lines = 1;
2624
2625 top_plines -= skip_lines;
2626 if (top_plines > curwin->w_height)
2627 {
2628 scrolled += (top_plines - curwin->w_height);
2629 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002630 }
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633
2634 /*
2635 * Stop counting lines to scroll when
2636 * - hitting start of the file
2637 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002638 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 * - lines between botline and cursor have been counted
2640 */
2641#ifdef FEAT_FOLDING
2642 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2643#endif
2644 {
2645 loff.lnum = cln;
2646 boff.lnum = cln;
2647 }
2648#ifdef FEAT_DIFF
2649 loff.fill = 0;
2650 boff.fill = 0;
2651 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2652 - curwin->w_filler_rows;
2653#endif
2654
2655 while (loff.lnum > 1)
2656 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2658 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002660 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2662 && loff.lnum <= curwin->w_botline
2663#ifdef FEAT_DIFF
2664 && (loff.lnum < curwin->w_botline
2665 || loff.fill >= fill_below_window)
2666#endif
2667 )
2668 break;
2669
Bram Moolenaar85a20022019-12-21 18:25:54 +01002670 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002672 if (loff.height == MAXCOL)
2673 used = MAXCOL;
2674 else
2675 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (used > curwin->w_height)
2677 break;
2678 if (loff.lnum >= curwin->w_botline
2679#ifdef FEAT_DIFF
2680 && (loff.lnum > curwin->w_botline
2681 || loff.fill <= fill_below_window)
2682#endif
2683 )
2684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 scrolled += loff.height;
2687 if (loff.lnum == curwin->w_botline
2688#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002689 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#endif
2691 )
2692 scrolled -= curwin->w_empty_rows;
2693 }
2694
2695 if (boff.lnum < curbuf->b_ml.ml_line_count)
2696 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 botline_forw(&boff);
2699 used += boff.height;
2700 if (used > curwin->w_height)
2701 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002702 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2703 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
2705 extra += boff.height;
2706 if (boff.lnum >= curwin->w_botline
2707#ifdef FEAT_DIFF
2708 || (boff.lnum + 1 == curwin->w_botline
2709 && boff.fill > curwin->w_filler_rows)
2710#endif
2711 )
2712 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002713 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 scrolled += boff.height;
2715 if (boff.lnum == curwin->w_botline
2716#ifdef FEAT_DIFF
2717 && boff.fill == 0
2718#endif
2719 )
2720 scrolled -= curwin->w_empty_rows;
2721 }
2722 }
2723 }
2724 }
2725
Bram Moolenaar85a20022019-12-21 18:25:54 +01002726 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (scrolled <= 0)
2728 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002729 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else if (used > curwin->w_height)
2731 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002732 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
2734 {
2735 line_count = 0;
2736#ifdef FEAT_DIFF
2737 boff.fill = curwin->w_topfill;
2738#endif
2739 boff.lnum = curwin->w_topline - 1;
2740 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2741 {
2742 botline_forw(&boff);
2743 i += boff.height;
2744 ++line_count;
2745 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002746 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 line_count = 9999;
2748 }
2749
2750 /*
2751 * Scroll up if the cursor is off the bottom of the screen a bit.
2752 * Otherwise put it at 1/2 of the screen.
2753 */
2754 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002755 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002756 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002757 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002758 if (do_sms)
2759 scrollup(scrolled, TRUE); // TODO
2760 else
2761 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
2764 /*
2765 * If topline didn't change we need to restore w_botline and w_empty_rows
2766 * (we changed them).
2767 * If topline did change, update_screen() will set botline.
2768 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002769 if (curwin->w_topline == old_topline
2770 && curwin->w_skipcol == old_skipcol
2771 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 curwin->w_botline = old_botline;
2774 curwin->w_empty_rows = old_empty_rows;
2775 curwin->w_valid = old_valid;
2776 }
2777 curwin->w_valid |= VALID_TOPLINE;
2778}
2779
2780/*
2781 * Recompute topline to put the cursor halfway the window
2782 * If "atend" is TRUE, also put it halfway at the end of the file.
2783 */
2784 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002785scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 int above = 0;
2788 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002789 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#ifdef FEAT_DIFF
2791 int topfill = 0;
2792#endif
2793 int below = 0;
2794 int used;
2795 lineoff_T loff;
2796 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002797#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002798 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002801#ifdef FEAT_PROP_POPUP
2802 // if the width changed this needs to be updated first
2803 may_update_popup_position();
2804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2806#ifdef FEAT_FOLDING
2807 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2808#endif
2809#ifdef FEAT_DIFF
2810 used = plines_nofill(loff.lnum);
2811 loff.fill = 0;
2812 boff.fill = 0;
2813#else
2814 used = plines(loff.lnum);
2815#endif
2816 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002817
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002818 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002819 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2820 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002821 {
2822 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002823 if (atend)
2824 {
2825 want_height = (curwin->w_height - used) / 2;
2826 used = 0;
2827 }
2828 else
2829 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002830 }
2831
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 while (topline > 1)
2833 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002834 // If using smoothscroll, we can precisely scroll to the
2835 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002836 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002837 {
2838 topline_back_winheight(&loff, FALSE);
2839 if (loff.height == MAXCOL)
2840 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002841 used += loff.height;
2842 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002843 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002844 botline_forw(&boff);
2845 used += boff.height;
2846 }
2847 if (used > want_height)
2848 {
2849 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002850 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002851 topline = loff.lnum;
2852#ifdef FEAT_DIFF
2853 topfill = loff.fill;
2854#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002855 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002856 }
2857 break;
2858 }
2859 topline = loff.lnum;
2860#ifdef FEAT_DIFF
2861 topfill = loff.fill;
2862#endif
2863 continue;
2864 }
2865
2866 // If not using smoothscroll, we have to iteratively find how many
2867 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002868 // This may not be right in the middle if the lines'
2869 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002870
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002871 // Depending on "prefer_above" we add a line above or below first.
2872 // Loop twice to avoid duplicating code.
2873 int done = FALSE;
2874 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002876 if (prefer_above ? (round == 2 && below < above)
2877 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002879 // add a line below the cursor
2880 if (boff.lnum < curbuf->b_ml.ml_line_count)
2881 {
2882 botline_forw(&boff);
2883 used += boff.height;
2884 if (used > curwin->w_height)
2885 {
2886 done = TRUE;
2887 break;
2888 }
2889 below += boff.height;
2890 }
2891 else
2892 {
2893 ++below; // count a "~" line
2894 if (atend)
2895 ++used;
2896 }
2897 }
2898
2899 if (prefer_above ? (round == 1 && below >= above)
2900 : (round == 1 && below > above))
2901 {
2902 // add a line above the cursor
2903 topline_back(&loff);
2904 if (loff.height == MAXCOL)
2905 used = MAXCOL;
2906 else
2907 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002909 {
2910 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002912 }
2913 above += loff.height;
2914 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002916 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002920 if (done)
2921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#ifdef FEAT_FOLDING
2925 if (!hasFolding(topline, &curwin->w_topline, NULL))
2926#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002927 {
2928 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002929 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002930 || curwin->w_skipcol != 0)
2931 {
2932 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002933 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002934 {
2935 curwin->w_skipcol = skipcol;
2936 redraw_later(UPD_NOT_VALID);
2937 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002938 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002939 reset_skipcol();
2940 }
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#ifdef FEAT_DIFF
2943 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002944 if (old_topline > curwin->w_topline + curwin->w_height)
2945 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 check_topfill(curwin, FALSE);
2947#endif
2948 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2949 curwin->w_valid |= VALID_TOPLINE;
2950}
2951
2952/*
2953 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002954 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 * If not possible, put it at the same position as scroll_cursor_halfway().
2956 * When called topline must be valid!
2957 */
2958 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002959cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 linenr_T botline;
2965 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002966 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002968 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 /*
2971 * How many lines we would like to have above/below the cursor depends on
2972 * whether the first/last line of the file is on screen.
2973 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002974 above_wanted = so;
2975 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002976 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 above_wanted = mouse_dragging - 1;
2979 below_wanted = mouse_dragging - 1;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (curwin->w_topline == 1)
2982 {
2983 above_wanted = 0;
2984 max_off = curwin->w_height / 2;
2985 if (below_wanted > max_off)
2986 below_wanted = max_off;
2987 }
2988 validate_botline();
2989 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002990 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
2992 below_wanted = 0;
2993 max_off = (curwin->w_height - 1) / 2;
2994 if (above_wanted > max_off)
2995 above_wanted = max_off;
2996 }
2997
2998 /*
2999 * If there are sufficient file-lines above and below the cursor, we can
3000 * return now.
3001 */
3002 cln = curwin->w_cursor.lnum;
3003 if (cln >= curwin->w_topline + above_wanted
3004 && cln < curwin->w_botline - below_wanted
3005#ifdef FEAT_FOLDING
3006 && !hasAnyFolding(curwin)
3007#endif
3008 )
3009 return;
3010
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003011 if (curwin->w_p_sms && !curwin->w_p_wrap)
3012 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003013 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003014 if (curwin->w_cline_height == curwin->w_height)
3015 {
3016 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003017 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003018 return;
3019 }
3020 // TODO: If the cursor line doesn't fit in the window then only adjust
3021 // w_skipcol.
3022 }
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 /*
3025 * Narrow down the area where the cursor can be put by taking lines from
3026 * the top and the bottom until:
3027 * - the desired context lines are found
3028 * - the lines from the top is past the lines from the bottom
3029 */
3030 topline = curwin->w_topline;
3031 botline = curwin->w_botline - 1;
3032#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003033 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 above = curwin->w_topfill;
3035 below = curwin->w_filler_rows;
3036#endif
3037 while ((above < above_wanted || below < below_wanted) && topline < botline)
3038 {
3039 if (below < below_wanted && (below <= above || above >= above_wanted))
3040 {
3041#ifdef FEAT_FOLDING
3042 if (hasFolding(botline, &botline, NULL))
3043 ++below;
3044 else
3045#endif
3046 below += plines(botline);
3047 --botline;
3048 }
3049 if (above < above_wanted && (above < below || below >= below_wanted))
3050 {
3051#ifdef FEAT_FOLDING
3052 if (hasFolding(topline, NULL, &topline))
3053 ++above;
3054 else
3055#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003056 above += PLINES_NOFILL(topline);
3057#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003058 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (topline < botline)
3060 above += diff_check_fill(curwin, topline + 1);
3061#endif
3062 ++topline;
3063 }
3064 }
3065 if (topline == botline || botline == 0)
3066 curwin->w_cursor.lnum = topline;
3067 else if (topline > botline)
3068 curwin->w_cursor.lnum = botline;
3069 else
3070 {
3071 if (cln < topline && curwin->w_topline > 1)
3072 {
3073 curwin->w_cursor.lnum = topline;
3074 curwin->w_valid &=
3075 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3076 }
3077 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3078 {
3079 curwin->w_cursor.lnum = botline;
3080 curwin->w_valid &=
3081 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3082 }
3083 }
3084 curwin->w_valid |= VALID_TOPLINE;
3085}
3086
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003087static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003090 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3091 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003093 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
3095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003096onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 long n;
3099 int retval = OK;
3100 lineoff_T loff;
3101 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003102 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar85a20022019-12-21 18:25:54 +01003104 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 beep_flush();
3107 return FAIL;
3108 }
3109
3110 for ( ; count > 0; --count)
3111 {
3112 validate_botline();
3113 /*
3114 * It's an error to move a page up when the first line is already on
3115 * the screen. It's an error to move a page down when the last line
3116 * is on the screen and the topline is 'scrolloff' lines from the
3117 * last line.
3118 */
3119 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003120 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3122 : (curwin->w_topline == 1
3123#ifdef FEAT_DIFF
3124 && curwin->w_topfill ==
3125 diff_check_fill(curwin, curwin->w_topline)
3126#endif
3127 ))
3128 {
3129 beep_flush();
3130 retval = FAIL;
3131 break;
3132 }
3133
3134#ifdef FEAT_DIFF
3135 loff.fill = 0;
3136#endif
3137 if (dir == FORWARD)
3138 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003139 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003142 if (p_window <= 2)
3143 ++curwin->w_topline;
3144 else
3145 curwin->w_topline += p_window - 2;
3146 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3147 curwin->w_topline = curbuf->b_ml.ml_line_count;
3148 curwin->w_cursor.lnum = curwin->w_topline;
3149 }
3150 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3151 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003152 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 curwin->w_topline = curbuf->b_ml.ml_line_count;
3154#ifdef FEAT_DIFF
3155 curwin->w_topfill = 0;
3156#endif
3157 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3158 }
3159 else
3160 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003161 // For the overlap, start with the line just below the window
3162 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 loff.lnum = curwin->w_botline;
3164#ifdef FEAT_DIFF
3165 loff.fill = diff_check_fill(curwin, loff.lnum)
3166 - curwin->w_filler_rows;
3167#endif
3168 get_scroll_overlap(&loff, -1);
3169 curwin->w_topline = loff.lnum;
3170#ifdef FEAT_DIFF
3171 curwin->w_topfill = loff.fill;
3172 check_topfill(curwin, FALSE);
3173#endif
3174 curwin->w_cursor.lnum = curwin->w_topline;
3175 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3176 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3177 }
3178 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003179 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181#ifdef FEAT_DIFF
3182 if (curwin->w_topline == 1)
3183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 max_topfill();
3186 continue;
3187 }
3188#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003189 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003190 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003191 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003192 if (p_window <= 2)
3193 --curwin->w_topline;
3194 else
3195 curwin->w_topline -= p_window - 2;
3196 if (curwin->w_topline < 1)
3197 curwin->w_topline = 1;
3198 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3199 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3200 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3201 continue;
3202 }
3203
Bram Moolenaar85a20022019-12-21 18:25:54 +01003204 // Find the line at the top of the window that is going to be the
3205 // line at the bottom of the window. Make sure this results in
3206 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 loff.lnum = curwin->w_topline - 1;
3208#ifdef FEAT_DIFF
3209 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3210 - curwin->w_topfill;
3211#endif
3212 get_scroll_overlap(&loff, 1);
3213
3214 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3215 {
3216 loff.lnum = curbuf->b_ml.ml_line_count;
3217#ifdef FEAT_DIFF
3218 loff.fill = 0;
3219 }
3220 else
3221 {
3222 botline_topline(&loff);
3223#endif
3224 }
3225 curwin->w_cursor.lnum = loff.lnum;
3226
Bram Moolenaar85a20022019-12-21 18:25:54 +01003227 // Find the line just above the new topline to get the right line
3228 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 n = 0;
3230 while (n <= curwin->w_height && loff.lnum >= 1)
3231 {
3232 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003233 if (loff.height == MAXCOL)
3234 n = MAXCOL;
3235 else
3236 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 curwin->w_topline = 1;
3241#ifdef FEAT_DIFF
3242 max_topfill();
3243#endif
3244 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3245 }
3246 else
3247 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003248 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#ifdef FEAT_DIFF
3250 topline_botline(&loff);
3251#endif
3252 botline_forw(&loff);
3253 botline_forw(&loff);
3254#ifdef FEAT_DIFF
3255 botline_topline(&loff);
3256#endif
3257#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003258 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3260#endif
3261
Bram Moolenaar85a20022019-12-21 18:25:54 +01003262 // Always scroll at least one line. Avoid getting stuck on
3263 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (loff.lnum >= curwin->w_topline
3265#ifdef FEAT_DIFF
3266 && (loff.lnum > curwin->w_topline
3267 || loff.fill >= curwin->w_topfill)
3268#endif
3269 )
3270 {
3271#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003272 // First try using the maximum number of filler lines. If
3273 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 loff.fill = curwin->w_topfill;
3275 if (curwin->w_topfill < diff_check_fill(curwin,
3276 curwin->w_topline))
3277 max_topfill();
3278 if (curwin->w_topfill == loff.fill)
3279#endif
3280 {
3281 --curwin->w_topline;
3282#ifdef FEAT_DIFF
3283 curwin->w_topfill = 0;
3284#endif
3285 }
3286 comp_botline(curwin);
3287 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003288 curwin->w_valid &=
3289 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else
3292 {
3293 curwin->w_topline = loff.lnum;
3294#ifdef FEAT_DIFF
3295 curwin->w_topfill = loff.fill;
3296 check_topfill(curwin, FALSE);
3297#endif
3298 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3299 }
3300 }
3301 }
3302 }
3303#ifdef FEAT_FOLDING
3304 foldAdjustCursor();
3305#endif
3306 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003307 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003308 if (retval == OK)
3309 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3311
Bram Moolenaar907dad72018-07-10 15:07:15 +02003312 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003314 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3315 // But make sure we scroll at least one line (happens with mix of long
3316 // wrapping lines and non-wrapping line).
3317 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003319 scroll_cursor_top(1, FALSE);
3320 if (curwin->w_topline <= old_topline
3321 && old_topline < curbuf->b_ml.ml_line_count)
3322 {
3323 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003325 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3326#endif
3327 }
3328 }
3329#ifdef FEAT_FOLDING
3330 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003335 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return retval;
3337}
3338
3339/*
3340 * Decide how much overlap to use for page-up or page-down scrolling.
3341 * This is symmetric, so that doing both keeps the same lines displayed.
3342 * Three lines are examined:
3343 *
3344 * before CTRL-F after CTRL-F / before CTRL-B
3345 * etc. l1
3346 * l1 last but one line ------------
3347 * l2 last text line l2 top text line
3348 * ------------- l3 second text line
3349 * l3 etc.
3350 */
3351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003352get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int h1, h2, h3, h4;
3355 int min_height = curwin->w_height - 2;
3356 lineoff_T loff0, loff1, loff2;
3357
3358#ifdef FEAT_DIFF
3359 if (lp->fill > 0)
3360 lp->height = 1;
3361 else
3362 lp->height = plines_nofill(lp->lnum);
3363#else
3364 lp->height = plines(lp->lnum);
3365#endif
3366 h1 = lp->height;
3367 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370 loff0 = *lp;
3371 if (dir > 0)
3372 botline_forw(lp);
3373 else
3374 topline_back(lp);
3375 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003376 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 return;
3380 }
3381
3382 loff1 = *lp;
3383 if (dir > 0)
3384 botline_forw(lp);
3385 else
3386 topline_back(lp);
3387 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003388 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003390 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return;
3392 }
3393
3394 loff2 = *lp;
3395 if (dir > 0)
3396 botline_forw(lp);
3397 else
3398 topline_back(lp);
3399 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003400 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003401 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003403 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404}
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406/*
3407 * Scroll 'scroll' lines up or down.
3408 */
3409 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003410halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 long scrolled = 0;
3413 int i;
3414 int n;
3415 int room;
3416
3417 if (Prenum)
3418 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3419 curwin->w_height : Prenum;
3420 n = (curwin->w_p_scr <= curwin->w_height) ?
3421 curwin->w_p_scr : curwin->w_height;
3422
Bram Moolenaard5d37532017-03-27 23:02:07 +02003423 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 validate_botline();
3425 room = curwin->w_empty_rows;
3426#ifdef FEAT_DIFF
3427 room += curwin->w_filler_rows;
3428#endif
3429 if (flag)
3430 {
3431 /*
3432 * scroll the text up
3433 */
3434 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3435 {
3436#ifdef FEAT_DIFF
3437 if (curwin->w_topfill > 0)
3438 {
3439 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003440 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 --curwin->w_topfill;
3442 }
3443 else
3444#endif
3445 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003446 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 n -= i;
3448 if (n < 0 && scrolled > 0)
3449 break;
3450#ifdef FEAT_FOLDING
3451 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3452#endif
3453 ++curwin->w_topline;
3454#ifdef FEAT_DIFF
3455 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3456#endif
3457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3459 {
3460 ++curwin->w_cursor.lnum;
3461 curwin->w_valid &=
3462 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3466 scrolled += i;
3467
3468 /*
3469 * Correct w_botline for changed w_topline.
3470 * Won't work when there are filler lines.
3471 */
3472#ifdef FEAT_DIFF
3473 if (curwin->w_p_diff)
3474 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3475 else
3476#endif
3477 {
3478 room += i;
3479 do
3480 {
3481 i = plines(curwin->w_botline);
3482 if (i > room)
3483 break;
3484#ifdef FEAT_FOLDING
3485 (void)hasFolding(curwin->w_botline, NULL,
3486 &curwin->w_botline);
3487#endif
3488 ++curwin->w_botline;
3489 room -= i;
3490 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3491 }
3492 }
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 /*
3495 * When hit bottom of the file: move cursor down.
3496 */
3497 if (n > 0)
3498 {
3499# ifdef FEAT_FOLDING
3500 if (hasAnyFolding(curwin))
3501 {
3502 while (--n >= 0
3503 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3504 {
3505 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3506 &curwin->w_cursor.lnum);
3507 ++curwin->w_cursor.lnum;
3508 }
3509 }
3510 else
3511# endif
3512 curwin->w_cursor.lnum += n;
3513 check_cursor_lnum();
3514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516 else
3517 {
3518 /*
3519 * scroll the text down
3520 */
3521 while (n > 0 && curwin->w_topline > 1)
3522 {
3523#ifdef FEAT_DIFF
3524 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3525 {
3526 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003527 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 ++curwin->w_topfill;
3529 }
3530 else
3531#endif
3532 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003533 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 n -= i;
3535 if (n < 0 && scrolled > 0)
3536 break;
3537 --curwin->w_topline;
3538#ifdef FEAT_FOLDING
3539 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3540#endif
3541#ifdef FEAT_DIFF
3542 curwin->w_topfill = 0;
3543#endif
3544 }
3545 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3546 VALID_BOTLINE|VALID_BOTLINE_AP);
3547 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (curwin->w_cursor.lnum > 1)
3549 {
3550 --curwin->w_cursor.lnum;
3551 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /*
3556 * When hit top of the file: move cursor up.
3557 */
3558 if (n > 0)
3559 {
3560 if (curwin->w_cursor.lnum <= (linenr_T)n)
3561 curwin->w_cursor.lnum = 1;
3562 else
3563# ifdef FEAT_FOLDING
3564 if (hasAnyFolding(curwin))
3565 {
3566 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3567 {
3568 --curwin->w_cursor.lnum;
3569 (void)hasFolding(curwin->w_cursor.lnum,
3570 &curwin->w_cursor.lnum, NULL);
3571 }
3572 }
3573 else
3574# endif
3575 curwin->w_cursor.lnum -= n;
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003579 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 foldAdjustCursor();
3581# endif
3582#ifdef FEAT_DIFF
3583 check_topfill(curwin, !flag);
3584#endif
3585 cursor_correct();
3586 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003587 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003591do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592{
3593 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003594 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003595 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003596 colnr_T curswant = curwin->w_curswant;
3597 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 win_T *old_curwin = curwin;
3599 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 int old_VIsual_select = VIsual_select;
3601 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602
3603 /*
3604 * loop through the cursorbound windows
3605 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003607 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003608 {
3609 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003610 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003611 if (curwin != old_curwin && curwin->w_p_crb)
3612 {
3613# ifdef FEAT_DIFF
3614 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003615 curwin->w_cursor.lnum =
3616 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 else
3618# endif
3619 curwin->w_cursor.lnum = line;
3620 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003621 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003622 curwin->w_curswant = curswant;
3623 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003624
Bram Moolenaar85a20022019-12-21 18:25:54 +01003625 // Make sure the cursor is in a valid position. Temporarily set
3626 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003627 int restart_edit_save = restart_edit;
3628 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003629 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003630
3631 // Avoid a scroll here for the cursor position, 'scrollbind' is
3632 // more important.
3633 if (!curwin->w_p_scb)
3634 validate_cursor();
3635
Bram Moolenaar61452852011-02-01 18:01:11 +01003636 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003637 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003638 if (has_mbyte)
3639 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003640 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003641
Bram Moolenaar85a20022019-12-21 18:25:54 +01003642 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003643 if (!curwin->w_p_scb)
3644 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003645 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003646 }
3647 }
3648
3649 /*
3650 * reset current-window
3651 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003652 VIsual_select = old_VIsual_select;
3653 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003654 curwin = old_curwin;
3655 curbuf = old_curbuf;
3656}