blob: a1139199e6c64857962472dbb956ed13f834ee57 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
Bram Moolenaarf6196f42022-10-02 21:29:55 +010042adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
206 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 int
213sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000214{
215#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100216 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100218 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return 0;
220#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100221 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
222 if (wp->w_p_list && wp->w_lcs_chars.prec)
223 return 1;
224
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000225 return extra2 > 3 ? 0 : 3 - extra2;
226}
227
228/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000229 * Calculates the skipcol offset for window "wp" given how many
230 * physical lines we want to scroll down.
231 */
232 static int
233skipcol_from_plines(win_T *wp, int plines_off)
234{
235 int width1 = wp->w_width - win_col_off(wp);
236
237 int skipcol = 0;
238 if (plines_off > 0)
239 skipcol += width1;
240 if (plines_off > 1)
241 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
242 return skipcol;
243}
244
245/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100246 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247 */
248 static void
249reset_skipcol(void)
250{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000251 if (curwin->w_skipcol == 0)
252 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000253
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000254 curwin->w_skipcol = 0;
255
256 // Should use the least expensive way that displays all that changed.
257 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
258 // enough when the top line gets another screen line.
259 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000260}
261
262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * Update curwin->w_topline and redraw if necessary.
264 * Used to update the screen before printing a message.
265 */
266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100267update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 update_topline();
270 if (must_redraw)
271 update_screen(0);
272}
273
274/*
275 * Update curwin->w_topline to move the cursor onto the screen.
276 */
277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 long line_count;
281 int halfheight;
282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100316 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100352 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100353 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100354 int overlap = sms_marker_overlap(curwin, curwin_col_off()
355 - curwin_col_off2());
356 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100357 check_topline = TRUE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
363 curwin->w_topline))
364 check_topline = TRUE;
365#endif
366
367 if (check_topline)
368 {
369 halfheight = curwin->w_height / 2 - 1;
370 if (halfheight < 2)
371 halfheight = 2;
372
373#ifdef FEAT_FOLDING
374 if (hasAnyFolding(curwin))
375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // Count the number of logical lines between the cursor and
377 // topline + scrolloff (approximation of how much will be
378 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 n = 0;
380 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100381 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
383 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100384 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
386 break;
387 (void)hasFolding(lnum, NULL, &lnum);
388 }
389 }
390 else
391#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar85a20022019-12-21 18:25:54 +0100394 // If we weren't very close to begin with, we scroll to put the
395 // cursor in the middle of the window. Otherwise put the cursor
396 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000398 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 else
400 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 check_botline = TRUE;
403 }
404 }
405
406 else
407 {
408#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
411#endif
412 check_botline = TRUE;
413 }
414 }
415
416 /*
417 * If the cursor is below the bottom of the window, scroll the window
418 * to put the cursor on the window.
419 * When w_botline is invalid, recompute it first, to avoid a redraw later.
420 * If w_botline was approximated, we might need a redraw later in a few
421 * cases, but we don't want to spend (a lot of) time recomputing w_botline
422 * for every small change.
423 */
424 if (check_botline)
425 {
426 if (!(curwin->w_valid & VALID_BOTLINE_AP))
427 validate_botline();
428
429 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
430 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000431 if (curwin->w_cursor.lnum < curwin->w_botline)
432 {
433 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100434 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#ifdef FEAT_FOLDING
436 || hasAnyFolding(curwin)
437#endif
438 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 lineoff_T loff;
441
Bram Moolenaar85a20022019-12-21 18:25:54 +0100442 // Cursor is (a few lines) above botline, check if there are
443 // 'scrolloff' window lines below the cursor. If not, need to
444 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 n = curwin->w_empty_rows;
446 loff.lnum = curwin->w_cursor.lnum;
447#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100448 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
450#endif
451#ifdef FEAT_DIFF
452 loff.fill = 0;
453 n += curwin->w_filler_rows;
454#endif
455 loff.height = 0;
456 while (loff.lnum < curwin->w_botline
457#ifdef FEAT_DIFF
458 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
459#endif
460 )
461 {
462 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100463 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 break;
465 botline_forw(&loff);
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000470 }
471 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100472 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000473 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475 if (check_botline)
476 {
477#ifdef FEAT_FOLDING
478 if (hasAnyFolding(curwin))
479 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // Count the number of logical lines between the cursor and
481 // botline - scrolloff (approximation of how much will be
482 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 line_count = 0;
484 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100488 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (lnum <= 0 || line_count > curwin->w_height + 1)
490 break;
491 (void)hasFolding(lnum, &lnum, NULL);
492 }
493 }
494 else
495#endif
496 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100497 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000499 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000501 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 }
504 }
505 curwin->w_valid |= VALID_TOPLINE;
506
507 /*
508 * Need to redraw when topline changed.
509 */
510 if (curwin->w_topline != old_topline
511#ifdef FEAT_DIFF
512 || curwin->w_topfill != old_topfill
513#endif
514 )
515 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100516 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000517 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100518
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100519 // When 'smoothscroll' is not set, should reset w_skipcol.
520 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100521 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100522 else if (curwin->w_skipcol != 0)
523 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000524
Bram Moolenaar85a20022019-12-21 18:25:54 +0100525 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (curwin->w_cursor.lnum == curwin->w_topline)
527 validate_cursor();
528 }
529
Bram Moolenaar375e3392019-01-31 18:26:10 +0100530 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000534 * Return the scrolljump value to use for the current window.
535 * When 'scrolljump' is positive use it as-is.
536 * When 'scrolljump' is negative use it as a percentage of the window height.
537 */
538 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100539scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000540{
541 if (p_sj >= 0)
542 return (int)p_sj;
543 return (curwin->w_height * -p_sj) / 100;
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
548 * current window.
549 */
550 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
553 lineoff_T loff;
554 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100555 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar375e3392019-01-31 18:26:10 +0100557 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#ifdef FEAT_FOLDING
559 || hasAnyFolding(curwin)
560#endif
561 )
562 {
563 loff.lnum = curwin->w_cursor.lnum;
564#ifdef FEAT_DIFF
565 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#else
568 n = 0;
569#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100571 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100574 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (loff.lnum < curwin->w_topline
576#ifdef FEAT_DIFF
577 || (loff.lnum == curwin->w_topline && loff.fill > 0)
578#endif
579 )
580 break;
581 n += loff.height;
582 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100583 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return TRUE;
585 }
586 return FALSE;
587}
588
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100589/*
590 * Update w_curswant.
591 */
592 void
593update_curswant_force(void)
594{
595 validate_virtcol();
596 curwin->w_curswant = curwin->w_virtcol
597#ifdef FEAT_PROP_POPUP
598 - curwin->w_virtcol_first_char
599#endif
600 ;
601 curwin->w_set_curswant = FALSE;
602}
603
604/*
605 * Update w_curswant if w_set_curswant is set.
606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100608update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100611 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612}
613
614/*
615 * Check if the cursor has moved. Set the w_valid flag accordingly.
616 */
617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
621 {
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100623 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
624 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 wp->w_valid_cursor = wp->w_cursor;
626 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100627 wp->w_valid_skipcol = wp->w_skipcol;
628 }
629 else if (wp->w_skipcol != wp->w_valid_skipcol)
630 {
631 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
632 |VALID_CHEIGHT|VALID_CROW
633 |VALID_BOTLINE|VALID_BOTLINE_AP);
634 wp->w_valid_cursor = wp->w_cursor;
635 wp->w_valid_leftcol = wp->w_leftcol;
636 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638 else if (wp->w_cursor.col != wp->w_valid_cursor.col
639 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100640 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
642 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
643 wp->w_valid_cursor.col = wp->w_cursor.col;
644 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647}
648
649/*
650 * Call this function when some window settings have changed, which require
651 * the cursor position, botline and topline to be recomputed and the window to
652 * be redrawn. E.g, when changing the 'wrap' option or folding.
653 */
654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100655changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 changed_window_setting_win(curwin);
658}
659
660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 wp->w_lines_valid = 0;
664 changed_line_abv_curs_win(wp);
665 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100666 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667}
668
Dominique Pellee764d1b2023-03-12 21:20:59 +0000669#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000671 * Call changed_window_setting_win() for every window containing "buf".
672 */
673 void
674changed_window_setting_buf(buf_T *buf)
675{
676 tabpage_T *tp;
677 win_T *wp;
678
679 FOR_ALL_TAB_WINDOWS(tp, wp)
680 if (wp->w_buffer == buf)
681 changed_window_setting_win(wp);
682}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000683#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000684
685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * Set wp->w_topline to a certain number.
687 */
688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100689set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200691#ifdef FEAT_DIFF
692 linenr_T prev_topline = wp->w_topline;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100696 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
698#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100699 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100701 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
702 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000704 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200706 if (lnum != prev_topline)
707 // Keep the filler lines when the topline didn't change.
708 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100712 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
716 * Call this function when the length of the cursor line (in screen
717 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100718 * If the line length changed the number of screen lines might change,
719 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Need to take care of w_botline separately!
721 */
722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100723changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
Bram Moolenaar85090142023-06-01 19:27:08 +0100725 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 |VALID_CHEIGHT|VALID_TOPLINE);
727}
728
729 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100730changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
Bram Moolenaar85090142023-06-01 19:27:08 +0100732 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 |VALID_CHEIGHT|VALID_TOPLINE);
734}
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736/*
737 * Call this function when the length of a line (in screen characters) above
738 * the cursor have changed.
739 * Need to take care of w_botline separately!
740 */
741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
745 |VALID_CHEIGHT|VALID_TOPLINE);
746}
747
748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100749changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
752 |VALID_CHEIGHT|VALID_TOPLINE);
753}
754
Dominique Pellee764d1b2023-03-12 21:20:59 +0000755#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100757 * Display of line has changed for "buf", invalidate cursor position and
758 * w_botline.
759 */
760 void
761changed_line_display_buf(buf_T *buf)
762{
763 win_T *wp;
764
765 FOR_ALL_WINDOWS(wp)
766 if (wp->w_buffer == buf)
767 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
768 |VALID_CROW|VALID_CHEIGHT
769 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
770}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000771#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100772
773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 * Make sure the value of curwin->w_botline is valid.
775 */
776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100777validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100779 validate_botline_win(curwin);
780}
781
782/*
783 * Make sure the value of wp->w_botline is valid.
784 */
785 void
786validate_botline_win(win_T *wp)
787{
788 if (!(wp->w_valid & VALID_BOTLINE))
789 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 * Mark curwin->w_botline as invalid (because of some change in the buffer).
794 */
795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100796invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
799}
800
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
805}
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100808approximate_botline_win(
809 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 wp->w_valid &= ~VALID_BOTLINE;
812}
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/*
815 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
816 */
817 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100818cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 check_cursor_moved(curwin);
821 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
822 (VALID_WROW|VALID_WCOL));
823}
824
825/*
826 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
827 * w_topline must be valid, you may need to call update_topline() first!
828 */
829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100830validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100832 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 check_cursor_moved(curwin);
834 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
835 curs_columns(TRUE);
836}
837
838#if defined(FEAT_GUI) || defined(PROTO)
839/*
840 * validate w_cline_row.
841 */
842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100843validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845 /*
846 * First make sure that w_topline is valid (after moving the cursor).
847 */
848 update_topline();
849 check_cursor_moved(curwin);
850 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100851 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853#endif
854
855/*
856 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200857 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100860curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 linenr_T lnum;
863 int i;
864 int all_invalid;
865 int valid;
866#ifdef FEAT_FOLDING
867 long fold_count;
868#endif
869
Bram Moolenaar85a20022019-12-21 18:25:54 +0100870 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 all_invalid = (!redrawing()
872 || wp->w_lines_valid == 0
873 || wp->w_lines[0].wl_lnum > wp->w_topline);
874 i = 0;
875 wp->w_cline_row = 0;
876 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
877 {
878 valid = FALSE;
879 if (!all_invalid && i < wp->w_lines_valid)
880 {
881 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (wp->w_lines[i].wl_lnum == lnum)
884 {
885#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100886 // Check for newly inserted lines below this row, in which
887 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (!wp->w_buffer->b_mod_set
889 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
890 || wp->w_buffer->b_mod_top
891 > wp->w_lines[i].wl_lastlnum + 1)
892#endif
893 valid = TRUE;
894 }
895 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100896 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100899 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100901 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100903 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 {
905#ifdef FEAT_FOLDING
906 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100907 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (lnum > wp->w_cursor.lnum)
909 break;
910#else
911 ++lnum;
912#endif
913 wp->w_cline_row += wp->w_lines[i].wl_size;
914 }
915 else
916 {
917#ifdef FEAT_FOLDING
918 fold_count = foldedCount(wp, lnum, NULL);
919 if (fold_count)
920 {
921 lnum += fold_count;
922 if (lnum > wp->w_cursor.lnum)
923 break;
924 ++wp->w_cline_row;
925 }
926 else
927#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100928 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100929 wp->w_cline_row += plines_correct_topline(wp, lnum);
930 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933 }
934
935 check_cursor_moved(wp);
936 if (!(wp->w_valid & VALID_CHEIGHT))
937 {
938 if (all_invalid
939 || i == wp->w_lines_valid
940 || (i < wp->w_lines_valid
941 && (!wp->w_lines[i].wl_valid
942 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
943 {
944#ifdef FEAT_DIFF
945 if (wp->w_cursor.lnum == wp->w_topline)
946 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
947 TRUE) + wp->w_topfill;
948 else
949#endif
950 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
951#ifdef FEAT_FOLDING
952 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
953 NULL, NULL, TRUE, NULL);
954#endif
955 }
956 else if (i > wp->w_lines_valid)
957 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100958 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 wp->w_cline_height = 0;
960#ifdef FEAT_FOLDING
961 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
962 NULL, NULL, TRUE, NULL);
963#endif
964 }
965 else
966 {
967 wp->w_cline_height = wp->w_lines[i].wl_size;
968#ifdef FEAT_FOLDING
969 wp->w_cline_folded = wp->w_lines[i].wl_folded;
970#endif
971 }
972 }
973
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100974 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Validate curwin->w_virtcol only.
980 */
981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100982validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 validate_virtcol_win(curwin);
985}
986
987/*
988 * Validate wp->w_virtcol only.
989 */
990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100991validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994
995 if (wp->w_valid & VALID_VIRTCOL)
996 return;
997
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100998#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000999 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001000#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001001 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001002#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001003 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001004#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001005 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
1008/*
1009 * Validate curwin->w_cline_height only.
1010 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001015
1016 if (curwin->w_valid & VALID_CHEIGHT)
1017 return;
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 if (curwin->w_cursor.lnum == curwin->w_topline)
1021 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1022 + curwin->w_topfill;
1023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001025 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001027 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001029 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001033 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 */
1035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001036validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 colnr_T off;
1039 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001040 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001044 if (curwin->w_valid & VALID_WCOL)
1045 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001046
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001047 col = curwin->w_virtcol;
1048 off = curwin_col_off();
1049 col += off;
1050 width = curwin->w_width - off + curwin_col_off2();
1051
1052 // long line wrapping, adjust curwin->w_wrow
1053 if (curwin->w_p_wrap
1054 && col >= (colnr_T)curwin->w_width
1055 && width > 0)
1056 // use same formula as what is used in curs_columns()
1057 col -= ((col - curwin->w_width) / width + 1) * width;
1058 if (col > (int)curwin->w_leftcol)
1059 col -= curwin->w_leftcol;
1060 else
1061 col = 0;
1062 curwin->w_wcol = col;
1063
1064 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001065#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001066 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001071 * Compute offset of a window, occupied by absolute or relative line number,
1072 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
1074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar64486672010-05-16 15:46:46 +02001077 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#ifdef FEAT_FOLDING
1080 + wp->w_p_fdc
1081#endif
1082#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001083 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085 );
1086}
1087
1088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001089curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 return win_col_off(curwin);
1092}
1093
1094/*
1095 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001096 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1097 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
1099 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001100win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
Bram Moolenaar64486672010-05-16 15:46:46 +02001102 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001103 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return 0;
1105}
1106
1107 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 return win_col_off2(curwin);
1111}
1112
1113/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001114 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * Also updates curwin->w_wrow and curwin->w_cline_row.
1116 * Also updates curwin->w_leftcol.
1117 */
1118 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001119curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
1122 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001123 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 int off_left, off_right;
1125 int n;
1126 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001127 int width1; // text width for first screen line
1128 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int new_leftcol;
1130 colnr_T startcol;
1131 colnr_T endcol;
1132 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001133 long so = get_scrolloff_value();
1134 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001135 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 /*
1138 * First make sure that w_topline is valid (after moving the cursor).
1139 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001140 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142 /*
1143 * Next make sure that w_cline_row is valid.
1144 */
1145 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001146 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001148#ifdef FEAT_PROP_POPUP
1149 // will be set by getvvcol() but not reset
1150 curwin->w_virtcol_first_char = 0;
1151#endif
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 /*
1154 * Compute the number of virtual columns.
1155 */
1156#ifdef FEAT_FOLDING
1157 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1160 else
1161#endif
1162 getvvcol(curwin, &curwin->w_cursor,
1163 &startcol, &(curwin->w_virtcol), &endcol);
1164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001167 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169 extra = curwin_col_off();
1170 curwin->w_wcol = curwin->w_virtcol + extra;
1171 endcol += extra;
1172
1173 /*
1174 * Now compute w_wrow, counting screen lines from w_cline_row.
1175 */
1176 curwin->w_wrow = curwin->w_cline_row;
1177
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001178 width1 = curwin->w_width - extra;
1179 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001182 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001183 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001184 if (curwin->w_p_wrap)
1185 curwin->w_wrow = curwin->w_height - 1;
1186 else
1187 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001189 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001191 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001193 // skip columns that are not visible
1194 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001195 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001196 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001197 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001198 // Deduct by multiples of width2. This allows the long line
1199 // wrapping formula below to correctly calculate the w_wcol value
1200 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001201 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001202 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001203 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001204 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001205 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001206
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001207 did_sub_skipcol = TRUE;
1208 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001209
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001211 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001214 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1215 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 curwin->w_wrow += n;
1217
1218#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // When cursor wraps to first char of next line in Insert
1220 // mode, the 'showbreak' string isn't shown, backup to first
1221 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001222 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001223 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001224 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 curwin->w_wcol = 0;
1226#endif
1227 }
1228 }
1229
Bram Moolenaar85a20022019-12-21 18:25:54 +01001230 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1231 // is not folded.
1232 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001233 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#ifdef FEAT_FOLDING
1235 && !curwin->w_cline_folded
1236#endif
1237 )
1238 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001239#ifdef FEAT_PROP_POPUP
1240 if (curwin->w_virtcol_first_char > 0)
1241 {
1242 int cols = (curwin->w_width - extra);
1243 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1244
1245 // each "above" text prop shifts the text one row down
1246 curwin->w_wrow += rows;
1247 curwin->w_wcol -= rows * cols;
1248 endcol -= rows * cols;
1249 curwin->w_cline_height = rows + 1;
1250 }
1251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 /*
1253 * If Cursor is left of the screen, scroll rightwards.
1254 * If Cursor is right of the screen, scroll leftwards
1255 * If we get closer to the edge than 'sidescrolloff', scroll a little
1256 * extra
1257 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001258 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001259 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001260 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (off_left < 0 || off_right > 0)
1262 {
1263 if (off_left < 0)
1264 diff = -off_left;
1265 else
1266 diff = off_right;
1267
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 // When far off or not enough room on either side, put cursor in
1269 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001270 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1271 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 else
1273 {
1274 if (diff < p_ss)
1275 diff = p_ss;
1276 if (off_left < 0)
1277 new_leftcol = curwin->w_leftcol - diff;
1278 else
1279 new_leftcol = curwin->w_leftcol + diff;
1280 }
1281 if (new_leftcol < 0)
1282 new_leftcol = 0;
1283 if (new_leftcol != (int)curwin->w_leftcol)
1284 {
1285 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001286 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001287 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 }
1290 curwin->w_wcol -= curwin->w_leftcol;
1291 }
1292 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1293 curwin->w_wcol -= curwin->w_leftcol;
1294 else
1295 curwin->w_wcol = 0;
1296
1297#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001298 // Skip over filler lines. At the top use w_topfill, there
1299 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (curwin->w_cursor.lnum == curwin->w_topline)
1301 curwin->w_wrow += curwin->w_topfill;
1302 else
1303 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1304#endif
1305
1306 prev_skipcol = curwin->w_skipcol;
1307
1308 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if ((curwin->w_wrow >= curwin->w_height
1311 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001312 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 && (p_lines =
1314#ifdef FEAT_DIFF
1315 plines_win_nofill
1316#else
1317 plines_win
1318#endif
1319 (curwin, curwin->w_cursor.lnum, FALSE))
1320 - 1 >= curwin->w_height))
1321 && curwin->w_height != 0
1322 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001323 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001324 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001326 // Cursor past end of screen. Happens with a single line that does
1327 // not fit on screen. Find a skipcol to show the text around the
1328 // cursor. Avoid scrolling all the time. compute value of "extra":
1329 // 1: Less than 'scrolloff' lines above
1330 // 2: Less than 'scrolloff' lines below
1331 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001333 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 // Compute last display line of the buffer line that we want at the
1336 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (p_lines == 0)
1338 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1339 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001340 if (p_lines > curwin->w_wrow + so)
1341 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001344 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 extra += 2;
1346
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001347 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001349 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001350 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (n > curwin->w_height / 2)
1352 n -= curwin->w_height / 2;
1353 else
1354 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001355 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (n > p_lines - curwin->w_height + 1)
1357 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001358 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 else if (extra == 1)
1361 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001362 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001363 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1364 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (extra > 0)
1366 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001367 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1368 extra = curwin->w_skipcol / width2;
1369 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371 }
1372 else if (extra == 2)
1373 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001374 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001375 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (endcol > curwin->w_skipcol)
1379 curwin->w_skipcol = endcol;
1380 }
1381
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001382 // adjust w_wrow for the changed w_skipcol
1383 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001384 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001385 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001386 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (curwin->w_wrow >= curwin->w_height)
1389 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001390 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001392 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 curwin->w_wrow -= extra;
1394 }
1395
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001396 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (extra > 0)
1398 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1399 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001400 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001402 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 curwin->w_skipcol = 0;
1404 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001405 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001407#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001408 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001409#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001410#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1411 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1412 {
1413 curwin->w_wrow += popup_top_extra(curwin);
1414 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001415 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001416 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001417 else
1418 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001419#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001420
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001421 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1422 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001423 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001424 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1427}
1428
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001429#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430/*
1431 * Compute the screen position of text character at "pos" in window "wp"
1432 * The resulting values are one-based, zero when character is not visible.
1433 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001434 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001435textpos2screenpos(
1436 win_T *wp,
1437 pos_T *pos,
1438 int *rowp, // screen row
1439 int *scolp, // start screen column
1440 int *ccolp, // cursor screen column
1441 int *ecolp) // end screen column
1442{
1443 colnr_T scol = 0, ccol = 0, ecol = 0;
1444 int row = 0;
1445 int rowoff = 0;
1446 colnr_T coloff = 0;
1447
Bram Moolenaar189663b2021-07-21 18:04:56 +02001448 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001449 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001450 colnr_T col;
1451 int width;
1452 linenr_T lnum = pos->lnum;
1453#ifdef FEAT_FOLDING
1454 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001456 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1457#endif
1458 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001459
1460#ifdef FEAT_DIFF
1461 // Add filler lines above this buffer line.
1462 row += diff_check_fill(wp, lnum);
1463#endif
1464
zeertzjqba2d1912022-12-18 12:28:59 +00001465 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001466#ifdef FEAT_FOLDING
1467 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001468 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001469 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001470 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001471 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001472 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001473#endif
1474 {
1475 getvcol(wp, pos, &scol, &ccol, &ecol);
1476
1477 // similar to what is done in validate_cursor_col()
1478 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001479 col += off;
1480 width = wp->w_width - off + win_col_off2(wp);
1481
1482 // long line wrapping, adjust row
1483 if (wp->w_p_wrap
1484 && col >= (colnr_T)wp->w_width
1485 && width > 0)
1486 {
1487 // use same formula as what is used in curs_columns()
1488 rowoff = ((col - wp->w_width) / width + 1);
1489 col -= rowoff * width;
1490 }
1491 col -= wp->w_leftcol;
1492 if (col >= wp->w_width)
1493 col = -1;
1494 if (col >= 0 && row + rowoff <= wp->w_height)
1495 {
1496 coloff = col - scol + wp->w_wincol + 1;
1497 row += W_WINROW(wp);
1498 }
1499 else
1500 // character is left, right or below of the window
1501 row = rowoff = scol = ccol = ecol = 0;
1502 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001504 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001505 *scolp = scol + coloff;
1506 *ccolp = ccol + coloff;
1507 *ecolp = ecol + coloff;
1508}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001509#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510
Bram Moolenaar12034e22019-08-25 22:25:02 +02001511#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001512/*
1513 * "screenpos({winid}, {lnum}, {col})" function
1514 */
1515 void
1516f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1517{
1518 dict_T *dict;
1519 win_T *wp;
1520 pos_T pos;
1521 int row = 0;
1522 int scol = 0, ccol = 0, ecol = 0;
1523
Bram Moolenaar93a10962022-06-16 11:42:09 +01001524 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001525 return;
1526 dict = rettv->vval.v_dict;
1527
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001528 if (in_vim9script()
1529 && (check_for_number_arg(argvars, 0) == FAIL
1530 || check_for_number_arg(argvars, 1) == FAIL
1531 || check_for_number_arg(argvars, 2) == FAIL))
1532 return;
1533
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001534 wp = find_win_by_nr_or_id(&argvars[0]);
1535 if (wp == NULL)
1536 return;
1537
1538 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001539 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1540 {
1541 semsg(_(e_invalid_line_number_nr), pos.lnum);
1542 return;
1543 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001544 pos.col = tv_get_number(&argvars[2]) - 1;
1545 pos.coladd = 0;
1546 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1547
1548 dict_add_number(dict, "row", row);
1549 dict_add_number(dict, "col", scol);
1550 dict_add_number(dict, "curscol", ccol);
1551 dict_add_number(dict, "endcol", ecol);
1552}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001553
1554/*
1555 * "virtcol2col({winid}, {lnum}, {col})" function
1556 */
1557 void
1558f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1559{
1560 win_T *wp;
1561 linenr_T lnum;
1562 int screencol;
1563 int error = FALSE;
1564
1565 rettv->vval.v_number = -1;
1566
1567 if (check_for_number_arg(argvars, 0) == FAIL
1568 || check_for_number_arg(argvars, 1) == FAIL
1569 || check_for_number_arg(argvars, 2) == FAIL)
1570 return;
1571
1572 wp = find_win_by_nr_or_id(&argvars[0]);
1573 if (wp == NULL)
1574 return;
1575
1576 lnum = tv_get_number_chk(&argvars[1], &error);
1577 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1578 return;
1579
1580 screencol = tv_get_number_chk(&argvars[2], &error);
1581 if (error || screencol < 0)
1582 return;
1583
1584 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1585}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001586#endif
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588/*
1589 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001592scrolldown(
1593 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001596 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int wrow;
1598 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001599 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001600 int width1 = 0;
1601 int width2 = 0;
1602
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001603 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001604 {
1605 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001606 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609#ifdef FEAT_FOLDING
1610 linenr_T first;
1611
Bram Moolenaar85a20022019-12-21 18:25:54 +01001612 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1614#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001615 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001616 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001619 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1620 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 ++curwin->w_topfill;
1623 ++done;
1624 }
1625 else
1626#endif
1627 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001628 // break when at the very top
1629 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001630 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001632 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001634 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001635 if (curwin->w_skipcol >= width1 + width2)
1636 curwin->w_skipcol -= width2;
1637 else
1638 curwin->w_skipcol -= width1;
1639 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001643 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001644 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001645 --curwin->w_topline;
1646 curwin->w_skipcol = 0;
1647#ifdef FEAT_DIFF
1648 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650#ifdef FEAT_FOLDING
1651 // A sequence of folded lines only counts for one logical line
1652 if (hasFolding(curwin->w_topline, &first, NULL))
1653 {
1654 ++done;
1655 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001656 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001657 curwin->w_botline -= curwin->w_topline - first;
1658 curwin->w_topline = first;
1659 }
1660 else
1661#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001662 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001663 {
1664 int size = win_linetabsize(curwin, curwin->w_topline,
1665 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1666 if (size > width1)
1667 {
1668 curwin->w_skipcol = width1;
1669 size -= width1;
1670 redraw_later(UPD_NOT_VALID);
1671 }
1672 while (size > width2)
1673 {
1674 curwin->w_skipcol += width2;
1675 size -= width2;
1676 }
1677 ++done;
1678 }
1679 else
1680 done += PLINES_NOFILL(curwin->w_topline);
1681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 invalidate_botline();
1685 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001686 curwin->w_wrow += done; // keep w_wrow updated
1687 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
1689#ifdef FEAT_DIFF
1690 if (curwin->w_cursor.lnum == curwin->w_topline)
1691 curwin->w_cline_row = 0;
1692 check_topfill(curwin, TRUE);
1693#endif
1694
1695 /*
1696 * Compute the row number of the last row of the cursor line
1697 * and move the cursor onto the displayed part of the window.
1698 */
1699 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001700 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
1702 validate_virtcol();
1703 validate_cheight();
1704 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001705 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1708 {
1709#ifdef FEAT_FOLDING
1710 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1711 {
1712 --wrow;
1713 if (first == 1)
1714 curwin->w_cursor.lnum = 1;
1715 else
1716 curwin->w_cursor.lnum = first - 1;
1717 }
1718 else
1719#endif
1720 wrow -= plines(curwin->w_cursor.lnum--);
1721 curwin->w_valid &=
1722 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1723 moved = TRUE;
1724 }
1725 if (moved)
1726 {
1727#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001728 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 foldAdjustCursor();
1730#endif
1731 coladvance(curwin->w_curswant);
1732 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001733
1734 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1735 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001736 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001737 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1738
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001739 // make sure the cursor is in the visible text
1740 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001741 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001742 int row = 0;
1743 if (col >= width1)
1744 {
1745 col -= width1;
1746 ++row;
1747 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001748 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001749 {
1750 row += col / width2;
1751 col = col % width2;
1752 }
1753 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001754 {
1755 curwin->w_curswant = curwin->w_virtcol
1756 - (row - curwin->w_height + 1) * width2;
1757 coladvance(curwin->w_curswant);
1758 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
1762/*
1763 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766scrollup(
1767 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001768 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001770 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001772 if (do_sms
1773# ifdef FEAT_FOLDING
1774 || (byfold && hasAnyFolding(curwin))
1775# endif
1776# ifdef FEAT_DIFF
1777 || (curwin->w_p_diff && !curwin->w_p_wrap)
1778# endif
1779 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001781 int width1 = curwin->w_width - curwin_col_off();
1782 int width2 = width1 + curwin_col_off2();
1783 int size = 0;
1784 linenr_T prev_topline = curwin->w_topline;
1785
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001786 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001787 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001788
1789 // diff mode: first consume "topfill"
1790 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1791 // the line, then advance to the next line.
1792 // folding: count each sequence of folded lines as one logical line.
1793 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
1795# ifdef FEAT_DIFF
1796 if (curwin->w_topfill > 0)
1797 --curwin->w_topfill;
1798 else
1799# endif
1800 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001801 linenr_T lnum = curwin->w_topline;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803# ifdef FEAT_FOLDING
1804 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001805 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 (void)hasFolding(lnum, NULL, &lnum);
1807# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001808 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001809 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001810 // 'smoothscroll': increase "w_skipcol" until it goes over
1811 // the end of the line, then advance to the next line.
1812 int add = curwin->w_skipcol > 0 ? width2 : width1;
1813 curwin->w_skipcol += add;
1814 if (curwin->w_skipcol >= size)
1815 {
1816 if (lnum == curbuf->b_ml.ml_line_count)
1817 {
1818 // at the last screen line, can't scroll further
1819 curwin->w_skipcol -= add;
1820 break;
1821 }
1822 ++lnum;
1823 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001824 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001825 else
1826 {
1827 if (lnum >= curbuf->b_ml.ml_line_count)
1828 break;
1829 ++lnum;
1830 }
1831
1832 if (lnum > curwin->w_topline)
1833 {
1834 // approximate w_botline
1835 curwin->w_botline += lnum - curwin->w_topline;
1836 curwin->w_topline = lnum;
1837# ifdef FEAT_DIFF
1838 curwin->w_topfill = diff_check_fill(curwin, lnum);
1839# endif
1840 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001841 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001842 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001843 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001844 }
1845 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001846
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001847 if (curwin->w_topline == prev_topline)
1848 // need to redraw even though w_topline didn't change
1849 redraw_later(UPD_NOT_VALID);
1850 }
1851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001854 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856
1857 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1858 curwin->w_topline = curbuf->b_ml.ml_line_count;
1859 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1860 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1861
1862#ifdef FEAT_DIFF
1863 check_topfill(curwin, FALSE);
1864#endif
1865
1866#ifdef FEAT_FOLDING
1867 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001868 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1870#endif
1871
1872 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1873 if (curwin->w_cursor.lnum < curwin->w_topline)
1874 {
1875 curwin->w_cursor.lnum = curwin->w_topline;
1876 curwin->w_valid &=
1877 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1878 coladvance(curwin->w_curswant);
1879 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001880 if (curwin->w_cursor.lnum == curwin->w_topline
1881 && do_sms && curwin->w_skipcol > 0)
1882 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001883 int col_off = curwin_col_off();
1884 int col_off2 = curwin_col_off2();
1885
1886 int width1 = curwin->w_width - col_off;
1887 int width2 = width1 + col_off2;
1888 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001889 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001890 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001891 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001892
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001893 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001894 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001895 int overlap = scrolloff_cols != 0 ? 0
1896 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001897
Bram Moolenaar118c2352022-10-09 17:19:27 +01001898 // Make sure the cursor is in a visible part of the line, taking
1899 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001900 // If there are not enough screen lines put the cursor in the middle.
1901 if (scrolloff_cols > space_cols / 2)
1902 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001903 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001904 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001906 colnr_T col = curwin->w_virtcol;
1907
1908 if (col < width1)
1909 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001910 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001912 curwin->w_curswant = col;
1913 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001914
1915 // validate_virtcol() marked various things as valid, but after
1916 // moving the cursor they need to be recomputed
1917 curwin->w_valid &=
1918 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 }
1920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921}
1922
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001923/*
1924 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1925 * valid for 'smoothscroll'.
1926 */
1927 void
1928adjust_skipcol(void)
1929{
1930 if (!curwin->w_p_wrap
1931 || !curwin->w_p_sms
1932 || curwin->w_cursor.lnum != curwin->w_topline)
1933 return;
1934
1935 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001936 if (width1 <= 0)
1937 return; // no text will be displayed
1938
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001939 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001940 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001941 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1942 int scrolled = FALSE;
1943
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001944 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001945 if (curwin->w_cline_height == curwin->w_height
1946 // w_cline_height may be capped at w_height, check there aren't
1947 // actually more lines.
1948 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1949 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001950 {
1951 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001952 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001953 return;
1954 }
1955
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001956 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001957 int overlap = sms_marker_overlap(curwin,
1958 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001959 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001960 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001961 {
1962 // scroll a screen line down
1963 if (curwin->w_skipcol >= width1 + width2)
1964 curwin->w_skipcol -= width2;
1965 else
1966 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001967 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001968 }
1969 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001970 {
1971 validate_virtcol();
1972 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001973 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001974 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001975
1976 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1977 int row = 0;
1978 if (col >= width1)
1979 {
1980 col -= width1;
1981 ++row;
1982 }
1983 if (col > width2)
1984 {
1985 row += col / width2;
1986 col = col % width2;
1987 }
1988 if (row >= curwin->w_height)
1989 {
1990 if (curwin->w_skipcol == 0)
1991 {
1992 curwin->w_skipcol += width1;
1993 --row;
1994 }
1995 if (row >= curwin->w_height)
1996 curwin->w_skipcol += (row - curwin->w_height) * width2;
1997 redraw_later(UPD_NOT_VALID);
1998 }
1999}
2000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#ifdef FEAT_DIFF
2002/*
2003 * Don't end up with too many filler lines in the window.
2004 */
2005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006check_topfill(
2007 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002008 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 int n;
2011
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002012 if (wp->w_topfill <= 0)
2013 return;
2014
2015 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2016 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002018 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 --wp->w_topline;
2021 wp->w_topfill = 0;
2022 }
2023 else
2024 {
2025 wp->w_topfill = wp->w_height - n;
2026 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 }
2030}
2031
2032/*
2033 * Use as many filler lines as possible for w_topline. Make sure w_topline
2034 * is still visible.
2035 */
2036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int n;
2040
2041 n = plines_nofill(curwin->w_topline);
2042 if (n >= curwin->w_height)
2043 curwin->w_topfill = 0;
2044 else
2045 {
2046 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2047 if (curwin->w_topfill + n > curwin->w_height)
2048 curwin->w_topfill = curwin->w_height - n;
2049 }
2050}
2051#endif
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053/*
2054 * Scroll the screen one line down, but don't do it if it would move the
2055 * cursor off the screen.
2056 */
2057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int end_row;
2061#ifdef FEAT_DIFF
2062 int can_fill = (curwin->w_topfill
2063 < diff_check_fill(curwin, curwin->w_topline));
2064#endif
2065
2066 if (curwin->w_topline <= 1
2067#ifdef FEAT_DIFF
2068 && !can_fill
2069#endif
2070 )
2071 return;
2072
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 /*
2076 * Compute the row number of the last row of the cursor line
2077 * and make sure it doesn't go off the screen. Make sure the cursor
2078 * doesn't go past 'scrolloff' lines from the screen end.
2079 */
2080 end_row = curwin->w_wrow;
2081#ifdef FEAT_DIFF
2082 if (can_fill)
2083 ++end_row;
2084 else
2085 end_row += plines_nofill(curwin->w_topline - 1);
2086#else
2087 end_row += plines(curwin->w_topline - 1);
2088#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002089 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 validate_cheight();
2092 validate_virtcol();
2093 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002094 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002096 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
2098#ifdef FEAT_DIFF
2099 if (can_fill)
2100 {
2101 ++curwin->w_topfill;
2102 check_topfill(curwin, TRUE);
2103 }
2104 else
2105 {
2106 --curwin->w_topline;
2107 curwin->w_topfill = 0;
2108 }
2109#else
2110 --curwin->w_topline;
2111#endif
2112#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002113 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002115 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2117 }
2118}
2119
2120/*
2121 * Scroll the screen one line up, but don't do it if it would move the cursor
2122 * off the screen.
2123 */
2124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002125scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int start_row;
2128
2129 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2130#ifdef FEAT_DIFF
2131 && curwin->w_topfill == 0
2132#endif
2133 )
2134 return;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 /*
2139 * Compute the row number of the first row of the cursor line
2140 * and make sure it doesn't go off the screen. Make sure the cursor
2141 * doesn't go before 'scrolloff' lines from the screen start.
2142 */
2143#ifdef FEAT_DIFF
2144 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2145 - curwin->w_topfill;
2146#else
2147 start_row = curwin->w_wrow - plines(curwin->w_topline);
2148#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002149 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002152 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002154 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156#ifdef FEAT_DIFF
2157 if (curwin->w_topfill > 0)
2158 --curwin->w_topfill;
2159 else
2160#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002161 {
2162#ifdef FEAT_FOLDING
2163 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002166 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2169 }
2170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172/*
2173 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2174 * a (wrapped) text line. Uses and sets "lp->fill".
2175 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002176 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002179topline_back_winheight(
2180 lineoff_T *lp,
2181 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183#ifdef FEAT_DIFF
2184 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2185 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ++lp->fill;
2188 lp->height = 1;
2189 }
2190 else
2191#endif
2192 {
2193 --lp->lnum;
2194#ifdef FEAT_DIFF
2195 lp->fill = 0;
2196#endif
2197 if (lp->lnum < 1)
2198 lp->height = MAXCOL;
2199 else
2200#ifdef FEAT_FOLDING
2201 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 lp->height = 1;
2204 else
2205#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002206 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208}
2209
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002210 static void
2211topline_back(lineoff_T *lp)
2212{
2213 topline_back_winheight(lp, TRUE);
2214}
2215
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2219 * a (wrapped) text line. Uses and sets "lp->fill".
2220 * Returns the height of the added line in "lp->height".
2221 * Lines below the last one are incredibly high.
2222 */
2223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002224botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226#ifdef FEAT_DIFF
2227 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 ++lp->fill;
2231 lp->height = 1;
2232 }
2233 else
2234#endif
2235 {
2236 ++lp->lnum;
2237#ifdef FEAT_DIFF
2238 lp->fill = 0;
2239#endif
2240 if (lp->lnum > curbuf->b_ml.ml_line_count)
2241 lp->height = MAXCOL;
2242 else
2243#ifdef FEAT_FOLDING
2244 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 lp->height = 1;
2247 else
2248#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002249 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251}
2252
2253#ifdef FEAT_DIFF
2254/*
2255 * Switch from including filler lines below lp->lnum to including filler
2256 * lines above loff.lnum + 1. This keeps pointing to the same line.
2257 * When there are no filler lines nothing changes.
2258 */
2259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002260botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 if (lp->fill > 0)
2263 {
2264 ++lp->lnum;
2265 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2266 }
2267}
2268
2269/*
2270 * Switch from including filler lines above lp->lnum to including filler
2271 * lines below loff.lnum - 1. This keeps pointing to the same line.
2272 * When there are no filler lines nothing changes.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 if (lp->fill > 0)
2278 {
2279 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2280 --lp->lnum;
2281 }
2282}
2283#endif
2284
2285/*
2286 * Recompute topline to put the cursor at the top of the window.
2287 * Scroll at least "min_scroll" lines.
2288 * If "always" is TRUE, always set topline (for "zt").
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 int scrolled = 0;
2294 int extra = 0;
2295 int used;
2296 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 linenr_T top; // just above displayed lines
2298 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002300 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_DIFF
2302 linenr_T old_topfill = curwin->w_topfill;
2303#endif
2304 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002305 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (mouse_dragging > 0)
2308 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /*
2311 * Decrease topline until:
2312 * - it has become 1
2313 * - (part of) the cursor line is moved off the screen or
2314 * - moved at least 'scrolljump' lines and
2315 * - at least 'scrolloff' lines above and below the cursor
2316 */
2317 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (curwin->w_cursor.lnum < curwin->w_topline)
2320 scrolled = used;
2321
2322#ifdef FEAT_FOLDING
2323 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2324 {
2325 --top;
2326 ++bot;
2327 }
2328 else
2329#endif
2330 {
2331 top = curwin->w_cursor.lnum - 1;
2332 bot = curwin->w_cursor.lnum + 1;
2333 }
2334 new_topline = top + 1;
2335
2336#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 // "used" already contains the number of filler lines above, don't add it
2338 // again.
2339 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002340 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342
2343 /*
2344 * Check if the lines from "top" to "bot" fit in the window. If they do,
2345 * set new_topline and advance "top" and "bot" to include more lines.
2346 */
2347 while (top > 0)
2348 {
2349#ifdef FEAT_FOLDING
2350 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 i = 1;
2353 else
2354#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002355 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002356 if (top < curwin->w_topline)
2357 scrolled += i;
2358
2359 // If scrolling is needed, scroll at least 'sj' lines.
2360 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2361 && extra >= off)
2362 break;
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 used += i;
2365 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2366 {
2367#ifdef FEAT_FOLDING
2368 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 ++used;
2371 else
2372#endif
2373 used += plines(bot);
2374 }
2375 if (used > curwin->w_height)
2376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 extra += i;
2379 new_topline = top;
2380 --top;
2381 ++bot;
2382 }
2383
2384 /*
2385 * If we don't have enough space, put cursor in the middle.
2386 * This makes sure we get the same position when using "k" and "j"
2387 * in a small window.
2388 */
2389 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002390 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 {
2393 /*
2394 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002395 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 if (new_topline < curwin->w_topline || always)
2398 curwin->w_topline = new_topline;
2399 if (curwin->w_topline > curwin->w_cursor.lnum)
2400 curwin->w_topline = curwin->w_cursor.lnum;
2401#ifdef FEAT_DIFF
2402 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2403 if (curwin->w_topfill > 0 && extra > off)
2404 {
2405 curwin->w_topfill -= extra - off;
2406 if (curwin->w_topfill < 0)
2407 curwin->w_topfill = 0;
2408 }
2409 check_topfill(curwin, FALSE);
2410#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002411 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002412 if (curwin->w_topline == curwin->w_cursor.lnum
2413 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002414 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002416 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#ifdef FEAT_DIFF
2418 || curwin->w_topfill != old_topfill
2419#endif
2420 )
2421 curwin->w_valid &=
2422 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2423 curwin->w_valid |= VALID_TOPLINE;
2424 }
2425}
2426
2427/*
2428 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2429 * screen lines for text lines.
2430 */
2431 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002432set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434#ifdef FEAT_DIFF
2435 wp->w_filler_rows = 0;
2436#endif
2437 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 else
2440 {
2441 wp->w_empty_rows = wp->w_height - used;
2442#ifdef FEAT_DIFF
2443 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2444 {
2445 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2446 if (wp->w_empty_rows > wp->w_filler_rows)
2447 wp->w_empty_rows -= wp->w_filler_rows;
2448 else
2449 {
2450 wp->w_filler_rows = wp->w_empty_rows;
2451 wp->w_empty_rows = 0;
2452 }
2453 }
2454#endif
2455 }
2456}
2457
2458/*
2459 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002460 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2462 * This is messy stuff!!!
2463 */
2464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 int used;
2468 int scrolled = 0;
2469 int extra = 0;
2470 int i;
2471 linenr_T line_count;
2472 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002473 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 lineoff_T loff;
2475 lineoff_T boff;
2476#ifdef FEAT_DIFF
2477 int old_topfill = curwin->w_topfill;
2478 int fill_below_window;
2479#endif
2480 linenr_T old_botline = curwin->w_botline;
2481 linenr_T old_valid = curwin->w_valid;
2482 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002483 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002484 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002485 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 cln = curwin->w_cursor.lnum;
2488 if (set_topbot)
2489 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002490 int set_skipcol = FALSE;
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 used = 0;
2493 curwin->w_botline = cln + 1;
2494#ifdef FEAT_DIFF
2495 loff.fill = 0;
2496#endif
2497 for (curwin->w_topline = curwin->w_botline;
2498 curwin->w_topline > 1;
2499 curwin->w_topline = loff.lnum)
2500 {
2501 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 topline_back_winheight(&loff, FALSE);
2503 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002505 if (used + loff.height > curwin->w_height)
2506 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002507 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002508 {
2509 // 'smoothscroll' and 'wrap' are set. The above line is
2510 // too long to show in its entirety, so we show just a part
2511 // of it.
2512 if (used < curwin->w_height)
2513 {
2514 int plines_offset = used + loff.height
2515 - curwin->w_height;
2516 used = curwin->w_height;
2517#ifdef FEAT_DIFF
2518 curwin->w_topfill = loff.fill;
2519#endif
2520 curwin->w_topline = loff.lnum;
2521 curwin->w_skipcol = skipcol_from_plines(
2522 curwin, plines_offset);
2523 set_skipcol = TRUE;
2524 }
2525 }
2526 break;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 used += loff.height;
2529#ifdef FEAT_DIFF
2530 curwin->w_topfill = loff.fill;
2531#endif
2532 }
2533 set_empty_rows(curwin, used);
2534 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2535 if (curwin->w_topline != old_topline
2536#ifdef FEAT_DIFF
2537 || curwin->w_topfill != old_topfill
2538#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002539 || set_skipcol
2540 || curwin->w_skipcol != 0)
2541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002543 if (set_skipcol)
2544 redraw_later(UPD_NOT_VALID);
2545 else
2546 reset_skipcol();
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 validate_botline();
2551
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#ifdef FEAT_DIFF
2554 used = plines_nofill(cln);
2555#else
2556 validate_cheight();
2557 used = curwin->w_cline_height;
2558#endif
2559
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002560 // If the cursor is on or below botline, we will at least scroll by the
2561 // height of the cursor line, which is "used". Correct for empty lines,
2562 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (cln >= curwin->w_botline)
2564 {
2565 scrolled = used;
2566 if (cln == curwin->w_botline)
2567 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002568 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002569 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002570 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002571 // Calculate how many screen lines the current top line of window
2572 // occupies. If it is occupying more than the entire window, we
2573 // need to scroll the additional clipped lines to scroll past the
2574 // top line before we can move on to the other lines.
2575 int top_plines =
2576#ifdef FEAT_DIFF
2577 plines_win_nofill
2578#else
2579 plines_win
2580#endif
2581 (curwin, curwin->w_topline, FALSE);
2582 int skip_lines = 0;
2583 int width1 = curwin->w_width - curwin_col_off();
2584 int width2 = width1 + curwin_col_off2();
2585 // similar formula is used in curs_columns()
2586 if (curwin->w_skipcol > width1)
2587 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2588 else if (curwin->w_skipcol > 0)
2589 skip_lines = 1;
2590
2591 top_plines -= skip_lines;
2592 if (top_plines > curwin->w_height)
2593 {
2594 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002595 }
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598
2599 /*
2600 * Stop counting lines to scroll when
2601 * - hitting start of the file
2602 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002603 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * - lines between botline and cursor have been counted
2605 */
2606#ifdef FEAT_FOLDING
2607 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2608#endif
2609 {
2610 loff.lnum = cln;
2611 boff.lnum = cln;
2612 }
2613#ifdef FEAT_DIFF
2614 loff.fill = 0;
2615 boff.fill = 0;
2616 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2617 - curwin->w_filler_rows;
2618#endif
2619
2620 while (loff.lnum > 1)
2621 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002622 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2623 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002625 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2627 && loff.lnum <= curwin->w_botline
2628#ifdef FEAT_DIFF
2629 && (loff.lnum < curwin->w_botline
2630 || loff.fill >= fill_below_window)
2631#endif
2632 )
2633 break;
2634
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002637 if (loff.height == MAXCOL)
2638 used = MAXCOL;
2639 else
2640 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (used > curwin->w_height)
2642 break;
2643 if (loff.lnum >= curwin->w_botline
2644#ifdef FEAT_DIFF
2645 && (loff.lnum > curwin->w_botline
2646 || loff.fill <= fill_below_window)
2647#endif
2648 )
2649 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 scrolled += loff.height;
2652 if (loff.lnum == curwin->w_botline
2653#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002654 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#endif
2656 )
2657 scrolled -= curwin->w_empty_rows;
2658 }
2659
2660 if (boff.lnum < curbuf->b_ml.ml_line_count)
2661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 botline_forw(&boff);
2664 used += boff.height;
2665 if (used > curwin->w_height)
2666 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002667 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2668 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 extra += boff.height;
2671 if (boff.lnum >= curwin->w_botline
2672#ifdef FEAT_DIFF
2673 || (boff.lnum + 1 == curwin->w_botline
2674 && boff.fill > curwin->w_filler_rows)
2675#endif
2676 )
2677 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 scrolled += boff.height;
2680 if (boff.lnum == curwin->w_botline
2681#ifdef FEAT_DIFF
2682 && boff.fill == 0
2683#endif
2684 )
2685 scrolled -= curwin->w_empty_rows;
2686 }
2687 }
2688 }
2689 }
2690
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (scrolled <= 0)
2693 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 else if (used > curwin->w_height)
2696 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 {
2700 line_count = 0;
2701#ifdef FEAT_DIFF
2702 boff.fill = curwin->w_topfill;
2703#endif
2704 boff.lnum = curwin->w_topline - 1;
2705 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2706 {
2707 botline_forw(&boff);
2708 i += boff.height;
2709 ++line_count;
2710 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002711 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 line_count = 9999;
2713 }
2714
2715 /*
2716 * Scroll up if the cursor is off the bottom of the screen a bit.
2717 * Otherwise put it at 1/2 of the screen.
2718 */
2719 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002720 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002721 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002722 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002723 if (do_sms)
2724 scrollup(scrolled, TRUE); // TODO
2725 else
2726 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
2730 * If topline didn't change we need to restore w_botline and w_empty_rows
2731 * (we changed them).
2732 * If topline did change, update_screen() will set botline.
2733 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002734 if (curwin->w_topline == old_topline
2735 && curwin->w_skipcol == old_skipcol
2736 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 curwin->w_botline = old_botline;
2739 curwin->w_empty_rows = old_empty_rows;
2740 curwin->w_valid = old_valid;
2741 }
2742 curwin->w_valid |= VALID_TOPLINE;
2743}
2744
2745/*
2746 * Recompute topline to put the cursor halfway the window
2747 * If "atend" is TRUE, also put it halfway at the end of the file.
2748 */
2749 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002750scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 int above = 0;
2753 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002754 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef FEAT_DIFF
2756 int topfill = 0;
2757#endif
2758 int below = 0;
2759 int used;
2760 lineoff_T loff;
2761 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002762#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002763 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002766#ifdef FEAT_PROP_POPUP
2767 // if the width changed this needs to be updated first
2768 may_update_popup_position();
2769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2771#ifdef FEAT_FOLDING
2772 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2773#endif
2774#ifdef FEAT_DIFF
2775 used = plines_nofill(loff.lnum);
2776 loff.fill = 0;
2777 boff.fill = 0;
2778#else
2779 used = plines(loff.lnum);
2780#endif
2781 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002782
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002783 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002784 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2785 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002786 {
2787 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002788 if (atend)
2789 {
2790 want_height = (curwin->w_height - used) / 2;
2791 used = 0;
2792 }
2793 else
2794 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002795 }
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 while (topline > 1)
2798 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002799 // If using smoothscroll, we can precisely scroll to the
2800 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002801 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002802 {
2803 topline_back_winheight(&loff, FALSE);
2804 if (loff.height == MAXCOL)
2805 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002806 used += loff.height;
2807 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002808 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002809 botline_forw(&boff);
2810 used += boff.height;
2811 }
2812 if (used > want_height)
2813 {
2814 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002815 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002816 topline = loff.lnum;
2817#ifdef FEAT_DIFF
2818 topfill = loff.fill;
2819#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002820 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002821 }
2822 break;
2823 }
2824 topline = loff.lnum;
2825#ifdef FEAT_DIFF
2826 topfill = loff.fill;
2827#endif
2828 continue;
2829 }
2830
2831 // If not using smoothscroll, we have to iteratively find how many
2832 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002833 // This may not be right in the middle if the lines'
2834 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002835
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002836 // Depending on "prefer_above" we add a line above or below first.
2837 // Loop twice to avoid duplicating code.
2838 int done = FALSE;
2839 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002841 if (prefer_above ? (round == 2 && below < above)
2842 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002844 // add a line below the cursor
2845 if (boff.lnum < curbuf->b_ml.ml_line_count)
2846 {
2847 botline_forw(&boff);
2848 used += boff.height;
2849 if (used > curwin->w_height)
2850 {
2851 done = TRUE;
2852 break;
2853 }
2854 below += boff.height;
2855 }
2856 else
2857 {
2858 ++below; // count a "~" line
2859 if (atend)
2860 ++used;
2861 }
2862 }
2863
2864 if (prefer_above ? (round == 1 && below >= above)
2865 : (round == 1 && below > above))
2866 {
2867 // add a line above the cursor
2868 topline_back(&loff);
2869 if (loff.height == MAXCOL)
2870 used = MAXCOL;
2871 else
2872 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002874 {
2875 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002877 }
2878 above += loff.height;
2879 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002881 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002885 if (done)
2886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#ifdef FEAT_FOLDING
2890 if (!hasFolding(topline, &curwin->w_topline, NULL))
2891#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002892 {
2893 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002894 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002895 || curwin->w_skipcol != 0)
2896 {
2897 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002898 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002899 {
2900 curwin->w_skipcol = skipcol;
2901 redraw_later(UPD_NOT_VALID);
2902 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002903 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002904 reset_skipcol();
2905 }
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#ifdef FEAT_DIFF
2908 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002909 if (old_topline > curwin->w_topline + curwin->w_height)
2910 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 check_topfill(curwin, FALSE);
2912#endif
2913 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2914 curwin->w_valid |= VALID_TOPLINE;
2915}
2916
2917/*
2918 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002919 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 * If not possible, put it at the same position as scroll_cursor_halfway().
2921 * When called topline must be valid!
2922 */
2923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002924cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002928 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 linenr_T botline;
2930 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002933 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 /*
2936 * How many lines we would like to have above/below the cursor depends on
2937 * whether the first/last line of the file is on screen.
2938 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002939 above_wanted = so;
2940 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002941 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 above_wanted = mouse_dragging - 1;
2944 below_wanted = mouse_dragging - 1;
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (curwin->w_topline == 1)
2947 {
2948 above_wanted = 0;
2949 max_off = curwin->w_height / 2;
2950 if (below_wanted > max_off)
2951 below_wanted = max_off;
2952 }
2953 validate_botline();
2954 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002955 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 below_wanted = 0;
2958 max_off = (curwin->w_height - 1) / 2;
2959 if (above_wanted > max_off)
2960 above_wanted = max_off;
2961 }
2962
2963 /*
2964 * If there are sufficient file-lines above and below the cursor, we can
2965 * return now.
2966 */
2967 cln = curwin->w_cursor.lnum;
2968 if (cln >= curwin->w_topline + above_wanted
2969 && cln < curwin->w_botline - below_wanted
2970#ifdef FEAT_FOLDING
2971 && !hasAnyFolding(curwin)
2972#endif
2973 )
2974 return;
2975
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002976 if (curwin->w_p_sms && !curwin->w_p_wrap)
2977 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002978 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002979 if (curwin->w_cline_height == curwin->w_height)
2980 {
2981 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002982 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002983 return;
2984 }
2985 // TODO: If the cursor line doesn't fit in the window then only adjust
2986 // w_skipcol.
2987 }
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 /*
2990 * Narrow down the area where the cursor can be put by taking lines from
2991 * the top and the bottom until:
2992 * - the desired context lines are found
2993 * - the lines from the top is past the lines from the bottom
2994 */
2995 topline = curwin->w_topline;
2996 botline = curwin->w_botline - 1;
2997#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002998 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 above = curwin->w_topfill;
3000 below = curwin->w_filler_rows;
3001#endif
3002 while ((above < above_wanted || below < below_wanted) && topline < botline)
3003 {
3004 if (below < below_wanted && (below <= above || above >= above_wanted))
3005 {
3006#ifdef FEAT_FOLDING
3007 if (hasFolding(botline, &botline, NULL))
3008 ++below;
3009 else
3010#endif
3011 below += plines(botline);
3012 --botline;
3013 }
3014 if (above < above_wanted && (above < below || below >= below_wanted))
3015 {
3016#ifdef FEAT_FOLDING
3017 if (hasFolding(topline, NULL, &topline))
3018 ++above;
3019 else
3020#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003021 above += PLINES_NOFILL(topline);
3022#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003023 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (topline < botline)
3025 above += diff_check_fill(curwin, topline + 1);
3026#endif
3027 ++topline;
3028 }
3029 }
3030 if (topline == botline || botline == 0)
3031 curwin->w_cursor.lnum = topline;
3032 else if (topline > botline)
3033 curwin->w_cursor.lnum = botline;
3034 else
3035 {
3036 if (cln < topline && curwin->w_topline > 1)
3037 {
3038 curwin->w_cursor.lnum = topline;
3039 curwin->w_valid &=
3040 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3041 }
3042 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3043 {
3044 curwin->w_cursor.lnum = botline;
3045 curwin->w_valid &=
3046 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3047 }
3048 }
3049 curwin->w_valid |= VALID_TOPLINE;
3050}
3051
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003052static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003055 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3056 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003058 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
3060 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003061onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 long n;
3064 int retval = OK;
3065 lineoff_T loff;
3066 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003067 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaar85a20022019-12-21 18:25:54 +01003069 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 beep_flush();
3072 return FAIL;
3073 }
3074
3075 for ( ; count > 0; --count)
3076 {
3077 validate_botline();
3078 /*
3079 * It's an error to move a page up when the first line is already on
3080 * the screen. It's an error to move a page down when the last line
3081 * is on the screen and the topline is 'scrolloff' lines from the
3082 * last line.
3083 */
3084 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003085 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3087 : (curwin->w_topline == 1
3088#ifdef FEAT_DIFF
3089 && curwin->w_topfill ==
3090 diff_check_fill(curwin, curwin->w_topline)
3091#endif
3092 ))
3093 {
3094 beep_flush();
3095 retval = FAIL;
3096 break;
3097 }
3098
3099#ifdef FEAT_DIFF
3100 loff.fill = 0;
3101#endif
3102 if (dir == FORWARD)
3103 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003104 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003106 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003107 if (p_window <= 2)
3108 ++curwin->w_topline;
3109 else
3110 curwin->w_topline += p_window - 2;
3111 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3112 curwin->w_topline = curbuf->b_ml.ml_line_count;
3113 curwin->w_cursor.lnum = curwin->w_topline;
3114 }
3115 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3116 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003117 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 curwin->w_topline = curbuf->b_ml.ml_line_count;
3119#ifdef FEAT_DIFF
3120 curwin->w_topfill = 0;
3121#endif
3122 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3123 }
3124 else
3125 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003126 // For the overlap, start with the line just below the window
3127 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 loff.lnum = curwin->w_botline;
3129#ifdef FEAT_DIFF
3130 loff.fill = diff_check_fill(curwin, loff.lnum)
3131 - curwin->w_filler_rows;
3132#endif
3133 get_scroll_overlap(&loff, -1);
3134 curwin->w_topline = loff.lnum;
3135#ifdef FEAT_DIFF
3136 curwin->w_topfill = loff.fill;
3137 check_topfill(curwin, FALSE);
3138#endif
3139 curwin->w_cursor.lnum = curwin->w_topline;
3140 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3141 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3142 }
3143 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146#ifdef FEAT_DIFF
3147 if (curwin->w_topline == 1)
3148 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003149 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 max_topfill();
3151 continue;
3152 }
3153#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003154 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003155 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003156 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003157 if (p_window <= 2)
3158 --curwin->w_topline;
3159 else
3160 curwin->w_topline -= p_window - 2;
3161 if (curwin->w_topline < 1)
3162 curwin->w_topline = 1;
3163 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3164 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3165 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3166 continue;
3167 }
3168
Bram Moolenaar85a20022019-12-21 18:25:54 +01003169 // Find the line at the top of the window that is going to be the
3170 // line at the bottom of the window. Make sure this results in
3171 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 loff.lnum = curwin->w_topline - 1;
3173#ifdef FEAT_DIFF
3174 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3175 - curwin->w_topfill;
3176#endif
3177 get_scroll_overlap(&loff, 1);
3178
3179 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3180 {
3181 loff.lnum = curbuf->b_ml.ml_line_count;
3182#ifdef FEAT_DIFF
3183 loff.fill = 0;
3184 }
3185 else
3186 {
3187 botline_topline(&loff);
3188#endif
3189 }
3190 curwin->w_cursor.lnum = loff.lnum;
3191
Bram Moolenaar85a20022019-12-21 18:25:54 +01003192 // Find the line just above the new topline to get the right line
3193 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 n = 0;
3195 while (n <= curwin->w_height && loff.lnum >= 1)
3196 {
3197 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003198 if (loff.height == MAXCOL)
3199 n = MAXCOL;
3200 else
3201 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003203 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 curwin->w_topline = 1;
3206#ifdef FEAT_DIFF
3207 max_topfill();
3208#endif
3209 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3210 }
3211 else
3212 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003213 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#ifdef FEAT_DIFF
3215 topline_botline(&loff);
3216#endif
3217 botline_forw(&loff);
3218 botline_forw(&loff);
3219#ifdef FEAT_DIFF
3220 botline_topline(&loff);
3221#endif
3222#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003223 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3225#endif
3226
Bram Moolenaar85a20022019-12-21 18:25:54 +01003227 // Always scroll at least one line. Avoid getting stuck on
3228 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (loff.lnum >= curwin->w_topline
3230#ifdef FEAT_DIFF
3231 && (loff.lnum > curwin->w_topline
3232 || loff.fill >= curwin->w_topfill)
3233#endif
3234 )
3235 {
3236#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003237 // First try using the maximum number of filler lines. If
3238 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 loff.fill = curwin->w_topfill;
3240 if (curwin->w_topfill < diff_check_fill(curwin,
3241 curwin->w_topline))
3242 max_topfill();
3243 if (curwin->w_topfill == loff.fill)
3244#endif
3245 {
3246 --curwin->w_topline;
3247#ifdef FEAT_DIFF
3248 curwin->w_topfill = 0;
3249#endif
3250 }
3251 comp_botline(curwin);
3252 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003253 curwin->w_valid &=
3254 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256 else
3257 {
3258 curwin->w_topline = loff.lnum;
3259#ifdef FEAT_DIFF
3260 curwin->w_topfill = loff.fill;
3261 check_topfill(curwin, FALSE);
3262#endif
3263 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3264 }
3265 }
3266 }
3267 }
3268#ifdef FEAT_FOLDING
3269 foldAdjustCursor();
3270#endif
3271 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003272 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003273 if (retval == OK)
3274 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3276
Bram Moolenaar907dad72018-07-10 15:07:15 +02003277 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003279 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3280 // But make sure we scroll at least one line (happens with mix of long
3281 // wrapping lines and non-wrapping line).
3282 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003284 scroll_cursor_top(1, FALSE);
3285 if (curwin->w_topline <= old_topline
3286 && old_topline < curbuf->b_ml.ml_line_count)
3287 {
3288 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003290 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3291#endif
3292 }
3293 }
3294#ifdef FEAT_FOLDING
3295 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003300 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return retval;
3302}
3303
3304/*
3305 * Decide how much overlap to use for page-up or page-down scrolling.
3306 * This is symmetric, so that doing both keeps the same lines displayed.
3307 * Three lines are examined:
3308 *
3309 * before CTRL-F after CTRL-F / before CTRL-B
3310 * etc. l1
3311 * l1 last but one line ------------
3312 * l2 last text line l2 top text line
3313 * ------------- l3 second text line
3314 * l3 etc.
3315 */
3316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003317get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 int h1, h2, h3, h4;
3320 int min_height = curwin->w_height - 2;
3321 lineoff_T loff0, loff1, loff2;
3322
3323#ifdef FEAT_DIFF
3324 if (lp->fill > 0)
3325 lp->height = 1;
3326 else
3327 lp->height = plines_nofill(lp->lnum);
3328#else
3329 lp->height = plines(lp->lnum);
3330#endif
3331 h1 = lp->height;
3332 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003333 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335 loff0 = *lp;
3336 if (dir > 0)
3337 botline_forw(lp);
3338 else
3339 topline_back(lp);
3340 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003341 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return;
3345 }
3346
3347 loff1 = *lp;
3348 if (dir > 0)
3349 botline_forw(lp);
3350 else
3351 topline_back(lp);
3352 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003353 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003355 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 return;
3357 }
3358
3359 loff2 = *lp;
3360 if (dir > 0)
3361 botline_forw(lp);
3362 else
3363 topline_back(lp);
3364 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003365 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371/*
3372 * Scroll 'scroll' lines up or down.
3373 */
3374 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 long scrolled = 0;
3378 int i;
3379 int n;
3380 int room;
3381
3382 if (Prenum)
3383 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3384 curwin->w_height : Prenum;
3385 n = (curwin->w_p_scr <= curwin->w_height) ?
3386 curwin->w_p_scr : curwin->w_height;
3387
Bram Moolenaard5d37532017-03-27 23:02:07 +02003388 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 validate_botline();
3390 room = curwin->w_empty_rows;
3391#ifdef FEAT_DIFF
3392 room += curwin->w_filler_rows;
3393#endif
3394 if (flag)
3395 {
3396 /*
3397 * scroll the text up
3398 */
3399 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3400 {
3401#ifdef FEAT_DIFF
3402 if (curwin->w_topfill > 0)
3403 {
3404 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003405 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 --curwin->w_topfill;
3407 }
3408 else
3409#endif
3410 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003411 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 n -= i;
3413 if (n < 0 && scrolled > 0)
3414 break;
3415#ifdef FEAT_FOLDING
3416 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3417#endif
3418 ++curwin->w_topline;
3419#ifdef FEAT_DIFF
3420 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3421#endif
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3424 {
3425 ++curwin->w_cursor.lnum;
3426 curwin->w_valid &=
3427 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
3430 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3431 scrolled += i;
3432
3433 /*
3434 * Correct w_botline for changed w_topline.
3435 * Won't work when there are filler lines.
3436 */
3437#ifdef FEAT_DIFF
3438 if (curwin->w_p_diff)
3439 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3440 else
3441#endif
3442 {
3443 room += i;
3444 do
3445 {
3446 i = plines(curwin->w_botline);
3447 if (i > room)
3448 break;
3449#ifdef FEAT_FOLDING
3450 (void)hasFolding(curwin->w_botline, NULL,
3451 &curwin->w_botline);
3452#endif
3453 ++curwin->w_botline;
3454 room -= i;
3455 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3456 }
3457 }
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 /*
3460 * When hit bottom of the file: move cursor down.
3461 */
3462 if (n > 0)
3463 {
3464# ifdef FEAT_FOLDING
3465 if (hasAnyFolding(curwin))
3466 {
3467 while (--n >= 0
3468 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3469 {
3470 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3471 &curwin->w_cursor.lnum);
3472 ++curwin->w_cursor.lnum;
3473 }
3474 }
3475 else
3476# endif
3477 curwin->w_cursor.lnum += n;
3478 check_cursor_lnum();
3479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 else
3482 {
3483 /*
3484 * scroll the text down
3485 */
3486 while (n > 0 && curwin->w_topline > 1)
3487 {
3488#ifdef FEAT_DIFF
3489 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3490 {
3491 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003492 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 ++curwin->w_topfill;
3494 }
3495 else
3496#endif
3497 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003498 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 n -= i;
3500 if (n < 0 && scrolled > 0)
3501 break;
3502 --curwin->w_topline;
3503#ifdef FEAT_FOLDING
3504 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3505#endif
3506#ifdef FEAT_DIFF
3507 curwin->w_topfill = 0;
3508#endif
3509 }
3510 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3511 VALID_BOTLINE|VALID_BOTLINE_AP);
3512 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (curwin->w_cursor.lnum > 1)
3514 {
3515 --curwin->w_cursor.lnum;
3516 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 /*
3521 * When hit top of the file: move cursor up.
3522 */
3523 if (n > 0)
3524 {
3525 if (curwin->w_cursor.lnum <= (linenr_T)n)
3526 curwin->w_cursor.lnum = 1;
3527 else
3528# ifdef FEAT_FOLDING
3529 if (hasAnyFolding(curwin))
3530 {
3531 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3532 {
3533 --curwin->w_cursor.lnum;
3534 (void)hasFolding(curwin->w_cursor.lnum,
3535 &curwin->w_cursor.lnum, NULL);
3536 }
3537 }
3538 else
3539# endif
3540 curwin->w_cursor.lnum -= n;
3541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003544 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 foldAdjustCursor();
3546# endif
3547#ifdef FEAT_DIFF
3548 check_topfill(curwin, !flag);
3549#endif
3550 cursor_correct();
3551 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003552 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003554
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003556do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003557{
3558 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003559 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003560 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003561 colnr_T curswant = curwin->w_curswant;
3562 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 win_T *old_curwin = curwin;
3564 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565 int old_VIsual_select = VIsual_select;
3566 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567
3568 /*
3569 * loop through the cursorbound windows
3570 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003572 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 {
3574 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003575 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576 if (curwin != old_curwin && curwin->w_p_crb)
3577 {
3578# ifdef FEAT_DIFF
3579 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003580 curwin->w_cursor.lnum =
3581 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582 else
3583# endif
3584 curwin->w_cursor.lnum = line;
3585 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003586 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003587 curwin->w_curswant = curswant;
3588 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589
Bram Moolenaar85a20022019-12-21 18:25:54 +01003590 // Make sure the cursor is in a valid position. Temporarily set
3591 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003592 int restart_edit_save = restart_edit;
3593 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003595
3596 // Avoid a scroll here for the cursor position, 'scrollbind' is
3597 // more important.
3598 if (!curwin->w_p_scb)
3599 validate_cursor();
3600
Bram Moolenaar61452852011-02-01 18:01:11 +01003601 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003602 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003603 if (has_mbyte)
3604 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003605 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003606
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003608 if (!curwin->w_p_scb)
3609 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003611 }
3612 }
3613
3614 /*
3615 * reset current-window
3616 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 VIsual_select = old_VIsual_select;
3618 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 curwin = old_curwin;
3620 curbuf = old_curbuf;
3621}