blob: ff2f4940170d17afff9e7a8b236007c6197c485b [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
zeertzjqf0e68c02023-06-03 17:11:47 +01001482 if (pos->lnum == wp->w_topline)
1483 col -= wp->w_skipcol;
1484
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001485 // long line wrapping, adjust row
1486 if (wp->w_p_wrap
1487 && col >= (colnr_T)wp->w_width
1488 && width > 0)
1489 {
1490 // use same formula as what is used in curs_columns()
1491 rowoff = ((col - wp->w_width) / width + 1);
1492 col -= rowoff * width;
1493 }
1494 col -= wp->w_leftcol;
1495 if (col >= wp->w_width)
1496 col = -1;
1497 if (col >= 0 && row + rowoff <= wp->w_height)
1498 {
1499 coloff = col - scol + wp->w_wincol + 1;
1500 row += W_WINROW(wp);
1501 }
1502 else
1503 // character is left, right or below of the window
1504 row = rowoff = scol = ccol = ecol = 0;
1505 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001506 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001507 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508 *scolp = scol + coloff;
1509 *ccolp = ccol + coloff;
1510 *ecolp = ecol + coloff;
1511}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001512#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001513
Bram Moolenaar12034e22019-08-25 22:25:02 +02001514#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515/*
1516 * "screenpos({winid}, {lnum}, {col})" function
1517 */
1518 void
1519f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1520{
1521 dict_T *dict;
1522 win_T *wp;
1523 pos_T pos;
1524 int row = 0;
1525 int scol = 0, ccol = 0, ecol = 0;
1526
Bram Moolenaar93a10962022-06-16 11:42:09 +01001527 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001528 return;
1529 dict = rettv->vval.v_dict;
1530
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001531 if (in_vim9script()
1532 && (check_for_number_arg(argvars, 0) == FAIL
1533 || check_for_number_arg(argvars, 1) == FAIL
1534 || check_for_number_arg(argvars, 2) == FAIL))
1535 return;
1536
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001537 wp = find_win_by_nr_or_id(&argvars[0]);
1538 if (wp == NULL)
1539 return;
1540
1541 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001542 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1543 {
1544 semsg(_(e_invalid_line_number_nr), pos.lnum);
1545 return;
1546 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001547 pos.col = tv_get_number(&argvars[2]) - 1;
1548 pos.coladd = 0;
1549 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1550
1551 dict_add_number(dict, "row", row);
1552 dict_add_number(dict, "col", scol);
1553 dict_add_number(dict, "curscol", ccol);
1554 dict_add_number(dict, "endcol", ecol);
1555}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001556
1557/*
1558 * "virtcol2col({winid}, {lnum}, {col})" function
1559 */
1560 void
1561f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1562{
1563 win_T *wp;
1564 linenr_T lnum;
1565 int screencol;
1566 int error = FALSE;
1567
1568 rettv->vval.v_number = -1;
1569
1570 if (check_for_number_arg(argvars, 0) == FAIL
1571 || check_for_number_arg(argvars, 1) == FAIL
1572 || check_for_number_arg(argvars, 2) == FAIL)
1573 return;
1574
1575 wp = find_win_by_nr_or_id(&argvars[0]);
1576 if (wp == NULL)
1577 return;
1578
1579 lnum = tv_get_number_chk(&argvars[1], &error);
1580 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1581 return;
1582
1583 screencol = tv_get_number_chk(&argvars[2], &error);
1584 if (error || screencol < 0)
1585 return;
1586
1587 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1588}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001589#endif
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
1592 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001595scrolldown(
1596 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001597 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001599 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 int wrow;
1601 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001602 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001603 int width1 = 0;
1604 int width2 = 0;
1605
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001606 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001607 {
1608 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001609 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612#ifdef FEAT_FOLDING
1613 linenr_T first;
1614
Bram Moolenaar85a20022019-12-21 18:25:54 +01001615 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1617#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001618 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001619 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001622 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1623 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 ++curwin->w_topfill;
1626 ++done;
1627 }
1628 else
1629#endif
1630 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001631 // break when at the very top
1632 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001633 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001635 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001637 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001638 if (curwin->w_skipcol >= width1 + width2)
1639 curwin->w_skipcol -= width2;
1640 else
1641 curwin->w_skipcol -= width1;
1642 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001646 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001647 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001648 --curwin->w_topline;
1649 curwin->w_skipcol = 0;
1650#ifdef FEAT_DIFF
1651 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001653#ifdef FEAT_FOLDING
1654 // A sequence of folded lines only counts for one logical line
1655 if (hasFolding(curwin->w_topline, &first, NULL))
1656 {
1657 ++done;
1658 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001659 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001660 curwin->w_botline -= curwin->w_topline - first;
1661 curwin->w_topline = first;
1662 }
1663 else
1664#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001665 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001666 {
1667 int size = win_linetabsize(curwin, curwin->w_topline,
1668 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1669 if (size > width1)
1670 {
1671 curwin->w_skipcol = width1;
1672 size -= width1;
1673 redraw_later(UPD_NOT_VALID);
1674 }
1675 while (size > width2)
1676 {
1677 curwin->w_skipcol += width2;
1678 size -= width2;
1679 }
1680 ++done;
1681 }
1682 else
1683 done += PLINES_NOFILL(curwin->w_topline);
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001686 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 invalidate_botline();
1688 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001689 curwin->w_wrow += done; // keep w_wrow updated
1690 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692#ifdef FEAT_DIFF
1693 if (curwin->w_cursor.lnum == curwin->w_topline)
1694 curwin->w_cline_row = 0;
1695 check_topfill(curwin, TRUE);
1696#endif
1697
1698 /*
1699 * Compute the row number of the last row of the cursor line
1700 * and move the cursor onto the displayed part of the window.
1701 */
1702 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001703 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
1705 validate_virtcol();
1706 validate_cheight();
1707 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001708 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1711 {
1712#ifdef FEAT_FOLDING
1713 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1714 {
1715 --wrow;
1716 if (first == 1)
1717 curwin->w_cursor.lnum = 1;
1718 else
1719 curwin->w_cursor.lnum = first - 1;
1720 }
1721 else
1722#endif
1723 wrow -= plines(curwin->w_cursor.lnum--);
1724 curwin->w_valid &=
1725 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1726 moved = TRUE;
1727 }
1728 if (moved)
1729 {
1730#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001731 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 foldAdjustCursor();
1733#endif
1734 coladvance(curwin->w_curswant);
1735 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001736
1737 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1738 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001739 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001740 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1741
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001742 // make sure the cursor is in the visible text
1743 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001744 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001745 int row = 0;
1746 if (col >= width1)
1747 {
1748 col -= width1;
1749 ++row;
1750 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001751 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 {
1753 row += col / width2;
1754 col = col % width2;
1755 }
1756 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001757 {
1758 curwin->w_curswant = curwin->w_virtcol
1759 - (row - curwin->w_height + 1) * width2;
1760 coladvance(curwin->w_curswant);
1761 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763}
1764
1765/*
1766 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001769scrollup(
1770 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001771 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001773 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001775 if (do_sms
1776# ifdef FEAT_FOLDING
1777 || (byfold && hasAnyFolding(curwin))
1778# endif
1779# ifdef FEAT_DIFF
1780 || (curwin->w_p_diff && !curwin->w_p_wrap)
1781# endif
1782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001784 int width1 = curwin->w_width - curwin_col_off();
1785 int width2 = width1 + curwin_col_off2();
1786 int size = 0;
1787 linenr_T prev_topline = curwin->w_topline;
1788
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001789 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001790 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001791
1792 // diff mode: first consume "topfill"
1793 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1794 // the line, then advance to the next line.
1795 // folding: count each sequence of folded lines as one logical line.
1796 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798# ifdef FEAT_DIFF
1799 if (curwin->w_topfill > 0)
1800 --curwin->w_topfill;
1801 else
1802# endif
1803 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001804 linenr_T lnum = curwin->w_topline;
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806# ifdef FEAT_FOLDING
1807 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 (void)hasFolding(lnum, NULL, &lnum);
1810# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001811 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001812 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001813 // 'smoothscroll': increase "w_skipcol" until it goes over
1814 // the end of the line, then advance to the next line.
1815 int add = curwin->w_skipcol > 0 ? width2 : width1;
1816 curwin->w_skipcol += add;
1817 if (curwin->w_skipcol >= size)
1818 {
1819 if (lnum == curbuf->b_ml.ml_line_count)
1820 {
1821 // at the last screen line, can't scroll further
1822 curwin->w_skipcol -= add;
1823 break;
1824 }
1825 ++lnum;
1826 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001827 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001828 else
1829 {
1830 if (lnum >= curbuf->b_ml.ml_line_count)
1831 break;
1832 ++lnum;
1833 }
1834
1835 if (lnum > curwin->w_topline)
1836 {
1837 // approximate w_botline
1838 curwin->w_botline += lnum - curwin->w_topline;
1839 curwin->w_topline = lnum;
1840# ifdef FEAT_DIFF
1841 curwin->w_topfill = diff_check_fill(curwin, lnum);
1842# endif
1843 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001844 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001845 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001846 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001847 }
1848 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001849
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001850 if (curwin->w_topline == prev_topline)
1851 // need to redraw even though w_topline didn't change
1852 redraw_later(UPD_NOT_VALID);
1853 }
1854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
1856 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001857 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859
1860 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1861 curwin->w_topline = curbuf->b_ml.ml_line_count;
1862 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1863 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1864
1865#ifdef FEAT_DIFF
1866 check_topfill(curwin, FALSE);
1867#endif
1868
1869#ifdef FEAT_FOLDING
1870 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001871 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1873#endif
1874
1875 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1876 if (curwin->w_cursor.lnum < curwin->w_topline)
1877 {
1878 curwin->w_cursor.lnum = curwin->w_topline;
1879 curwin->w_valid &=
1880 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1881 coladvance(curwin->w_curswant);
1882 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001883 if (curwin->w_cursor.lnum == curwin->w_topline
1884 && do_sms && curwin->w_skipcol > 0)
1885 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001886 int col_off = curwin_col_off();
1887 int col_off2 = curwin_col_off2();
1888
1889 int width1 = curwin->w_width - col_off;
1890 int width2 = width1 + col_off2;
1891 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001892 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001893 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001894 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001895
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001896 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001897 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001898 int overlap = scrolloff_cols != 0 ? 0
1899 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001900
Bram Moolenaar118c2352022-10-09 17:19:27 +01001901 // Make sure the cursor is in a visible part of the line, taking
1902 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001903 // If there are not enough screen lines put the cursor in the middle.
1904 if (scrolloff_cols > space_cols / 2)
1905 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001906 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001907 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001909 colnr_T col = curwin->w_virtcol;
1910
1911 if (col < width1)
1912 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001913 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001914 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001915 curwin->w_curswant = col;
1916 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001917
1918 // validate_virtcol() marked various things as valid, but after
1919 // moving the cursor they need to be recomputed
1920 curwin->w_valid &=
1921 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001922 }
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924}
1925
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001926/*
1927 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1928 * valid for 'smoothscroll'.
1929 */
1930 void
1931adjust_skipcol(void)
1932{
1933 if (!curwin->w_p_wrap
1934 || !curwin->w_p_sms
1935 || curwin->w_cursor.lnum != curwin->w_topline)
1936 return;
1937
1938 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001939 if (width1 <= 0)
1940 return; // no text will be displayed
1941
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001942 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001943 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001944 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1945 int scrolled = FALSE;
1946
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001947 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001948 if (curwin->w_cline_height == curwin->w_height
1949 // w_cline_height may be capped at w_height, check there aren't
1950 // actually more lines.
1951 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1952 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001953 {
1954 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001955 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001956 return;
1957 }
1958
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001959 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001960 int overlap = sms_marker_overlap(curwin,
1961 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001962 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001963 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001964 {
1965 // scroll a screen line down
1966 if (curwin->w_skipcol >= width1 + width2)
1967 curwin->w_skipcol -= width2;
1968 else
1969 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001970 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001971 }
1972 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001973 {
1974 validate_virtcol();
1975 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001976 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001977 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001978
1979 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1980 int row = 0;
1981 if (col >= width1)
1982 {
1983 col -= width1;
1984 ++row;
1985 }
1986 if (col > width2)
1987 {
1988 row += col / width2;
1989 col = col % width2;
1990 }
1991 if (row >= curwin->w_height)
1992 {
1993 if (curwin->w_skipcol == 0)
1994 {
1995 curwin->w_skipcol += width1;
1996 --row;
1997 }
1998 if (row >= curwin->w_height)
1999 curwin->w_skipcol += (row - curwin->w_height) * width2;
2000 redraw_later(UPD_NOT_VALID);
2001 }
2002}
2003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef FEAT_DIFF
2005/*
2006 * Don't end up with too many filler lines in the window.
2007 */
2008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002009check_topfill(
2010 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002011 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 int n;
2014
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002015 if (wp->w_topfill <= 0)
2016 return;
2017
2018 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2019 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002021 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002023 --wp->w_topline;
2024 wp->w_topfill = 0;
2025 }
2026 else
2027 {
2028 wp->w_topfill = wp->w_height - n;
2029 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 }
2033}
2034
2035/*
2036 * Use as many filler lines as possible for w_topline. Make sure w_topline
2037 * is still visible.
2038 */
2039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002040max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
2042 int n;
2043
2044 n = plines_nofill(curwin->w_topline);
2045 if (n >= curwin->w_height)
2046 curwin->w_topfill = 0;
2047 else
2048 {
2049 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2050 if (curwin->w_topfill + n > curwin->w_height)
2051 curwin->w_topfill = curwin->w_height - n;
2052 }
2053}
2054#endif
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056/*
2057 * Scroll the screen one line down, but don't do it if it would move the
2058 * cursor off the screen.
2059 */
2060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002061scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 int end_row;
2064#ifdef FEAT_DIFF
2065 int can_fill = (curwin->w_topfill
2066 < diff_check_fill(curwin, curwin->w_topline));
2067#endif
2068
2069 if (curwin->w_topline <= 1
2070#ifdef FEAT_DIFF
2071 && !can_fill
2072#endif
2073 )
2074 return;
2075
Bram Moolenaar85a20022019-12-21 18:25:54 +01002076 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Compute the row number of the last row of the cursor line
2080 * and make sure it doesn't go off the screen. Make sure the cursor
2081 * doesn't go past 'scrolloff' lines from the screen end.
2082 */
2083 end_row = curwin->w_wrow;
2084#ifdef FEAT_DIFF
2085 if (can_fill)
2086 ++end_row;
2087 else
2088 end_row += plines_nofill(curwin->w_topline - 1);
2089#else
2090 end_row += plines(curwin->w_topline - 1);
2091#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002092 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 validate_cheight();
2095 validate_virtcol();
2096 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002097 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002099 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101#ifdef FEAT_DIFF
2102 if (can_fill)
2103 {
2104 ++curwin->w_topfill;
2105 check_topfill(curwin, TRUE);
2106 }
2107 else
2108 {
2109 --curwin->w_topline;
2110 curwin->w_topfill = 0;
2111 }
2112#else
2113 --curwin->w_topline;
2114#endif
2115#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002116 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2120 }
2121}
2122
2123/*
2124 * Scroll the screen one line up, but don't do it if it would move the cursor
2125 * off the screen.
2126 */
2127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 int start_row;
2131
2132 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2133#ifdef FEAT_DIFF
2134 && curwin->w_topfill == 0
2135#endif
2136 )
2137 return;
2138
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
2142 * Compute the row number of the first row of the cursor line
2143 * and make sure it doesn't go off the screen. Make sure the cursor
2144 * doesn't go before 'scrolloff' lines from the screen start.
2145 */
2146#ifdef FEAT_DIFF
2147 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2148 - curwin->w_topfill;
2149#else
2150 start_row = curwin->w_wrow - plines(curwin->w_topline);
2151#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002152 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002155 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002157 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159#ifdef FEAT_DIFF
2160 if (curwin->w_topfill > 0)
2161 --curwin->w_topfill;
2162 else
2163#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002164 {
2165#ifdef FEAT_FOLDING
2166 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002169 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002170 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2172 }
2173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175/*
2176 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2177 * a (wrapped) text line. Uses and sets "lp->fill".
2178 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002179 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002182topline_back_winheight(
2183 lineoff_T *lp,
2184 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186#ifdef FEAT_DIFF
2187 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2188 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ++lp->fill;
2191 lp->height = 1;
2192 }
2193 else
2194#endif
2195 {
2196 --lp->lnum;
2197#ifdef FEAT_DIFF
2198 lp->fill = 0;
2199#endif
2200 if (lp->lnum < 1)
2201 lp->height = MAXCOL;
2202 else
2203#ifdef FEAT_FOLDING
2204 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002205 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 lp->height = 1;
2207 else
2208#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002209 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211}
2212
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002213 static void
2214topline_back(lineoff_T *lp)
2215{
2216 topline_back_winheight(lp, TRUE);
2217}
2218
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220/*
2221 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2222 * a (wrapped) text line. Uses and sets "lp->fill".
2223 * Returns the height of the added line in "lp->height".
2224 * Lines below the last one are incredibly high.
2225 */
2226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002227botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229#ifdef FEAT_DIFF
2230 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002232 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ++lp->fill;
2234 lp->height = 1;
2235 }
2236 else
2237#endif
2238 {
2239 ++lp->lnum;
2240#ifdef FEAT_DIFF
2241 lp->fill = 0;
2242#endif
2243 if (lp->lnum > curbuf->b_ml.ml_line_count)
2244 lp->height = MAXCOL;
2245 else
2246#ifdef FEAT_FOLDING
2247 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002248 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 lp->height = 1;
2250 else
2251#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002252 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254}
2255
2256#ifdef FEAT_DIFF
2257/*
2258 * Switch from including filler lines below lp->lnum to including filler
2259 * lines above loff.lnum + 1. This keeps pointing to the same line.
2260 * When there are no filler lines nothing changes.
2261 */
2262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (lp->fill > 0)
2266 {
2267 ++lp->lnum;
2268 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2269 }
2270}
2271
2272/*
2273 * Switch from including filler lines above lp->lnum to including filler
2274 * lines below loff.lnum - 1. This keeps pointing to the same line.
2275 * When there are no filler lines nothing changes.
2276 */
2277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002278topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 if (lp->fill > 0)
2281 {
2282 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2283 --lp->lnum;
2284 }
2285}
2286#endif
2287
2288/*
2289 * Recompute topline to put the cursor at the top of the window.
2290 * Scroll at least "min_scroll" lines.
2291 * If "always" is TRUE, always set topline (for "zt").
2292 */
2293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 int scrolled = 0;
2297 int extra = 0;
2298 int used;
2299 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002300 linenr_T top; // just above displayed lines
2301 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002303 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#ifdef FEAT_DIFF
2305 linenr_T old_topfill = curwin->w_topfill;
2306#endif
2307 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002308 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (mouse_dragging > 0)
2311 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
2313 /*
2314 * Decrease topline until:
2315 * - it has become 1
2316 * - (part of) the cursor line is moved off the screen or
2317 * - moved at least 'scrolljump' lines and
2318 * - at least 'scrolloff' lines above and below the cursor
2319 */
2320 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002321 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (curwin->w_cursor.lnum < curwin->w_topline)
2323 scrolled = used;
2324
2325#ifdef FEAT_FOLDING
2326 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2327 {
2328 --top;
2329 ++bot;
2330 }
2331 else
2332#endif
2333 {
2334 top = curwin->w_cursor.lnum - 1;
2335 bot = curwin->w_cursor.lnum + 1;
2336 }
2337 new_topline = top + 1;
2338
2339#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 // "used" already contains the number of filler lines above, don't add it
2341 // again.
2342 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002343 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#endif
2345
2346 /*
2347 * Check if the lines from "top" to "bot" fit in the window. If they do,
2348 * set new_topline and advance "top" and "bot" to include more lines.
2349 */
2350 while (top > 0)
2351 {
2352#ifdef FEAT_FOLDING
2353 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002354 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 i = 1;
2356 else
2357#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002358 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002359 if (top < curwin->w_topline)
2360 scrolled += i;
2361
2362 // If scrolling is needed, scroll at least 'sj' lines.
2363 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2364 && extra >= off)
2365 break;
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 used += i;
2368 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2369 {
2370#ifdef FEAT_FOLDING
2371 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002372 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 ++used;
2374 else
2375#endif
2376 used += plines(bot);
2377 }
2378 if (used > curwin->w_height)
2379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 extra += i;
2382 new_topline = top;
2383 --top;
2384 ++bot;
2385 }
2386
2387 /*
2388 * If we don't have enough space, put cursor in the middle.
2389 * This makes sure we get the same position when using "k" and "j"
2390 * in a small window.
2391 */
2392 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002393 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 else
2395 {
2396 /*
2397 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002398 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 if (new_topline < curwin->w_topline || always)
2401 curwin->w_topline = new_topline;
2402 if (curwin->w_topline > curwin->w_cursor.lnum)
2403 curwin->w_topline = curwin->w_cursor.lnum;
2404#ifdef FEAT_DIFF
2405 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2406 if (curwin->w_topfill > 0 && extra > off)
2407 {
2408 curwin->w_topfill -= extra - off;
2409 if (curwin->w_topfill < 0)
2410 curwin->w_topfill = 0;
2411 }
2412 check_topfill(curwin, FALSE);
2413#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002414 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002415 if (curwin->w_topline == curwin->w_cursor.lnum
2416 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002417 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002419 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef FEAT_DIFF
2421 || curwin->w_topfill != old_topfill
2422#endif
2423 )
2424 curwin->w_valid &=
2425 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2426 curwin->w_valid |= VALID_TOPLINE;
2427 }
2428}
2429
2430/*
2431 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2432 * screen lines for text lines.
2433 */
2434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437#ifdef FEAT_DIFF
2438 wp->w_filler_rows = 0;
2439#endif
2440 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002441 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 else
2443 {
2444 wp->w_empty_rows = wp->w_height - used;
2445#ifdef FEAT_DIFF
2446 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2447 {
2448 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2449 if (wp->w_empty_rows > wp->w_filler_rows)
2450 wp->w_empty_rows -= wp->w_filler_rows;
2451 else
2452 {
2453 wp->w_filler_rows = wp->w_empty_rows;
2454 wp->w_empty_rows = 0;
2455 }
2456 }
2457#endif
2458 }
2459}
2460
2461/*
2462 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002463 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2465 * This is messy stuff!!!
2466 */
2467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 int used;
2471 int scrolled = 0;
2472 int extra = 0;
2473 int i;
2474 linenr_T line_count;
2475 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002476 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 lineoff_T loff;
2478 lineoff_T boff;
2479#ifdef FEAT_DIFF
2480 int old_topfill = curwin->w_topfill;
2481 int fill_below_window;
2482#endif
2483 linenr_T old_botline = curwin->w_botline;
2484 linenr_T old_valid = curwin->w_valid;
2485 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002486 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002487 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002488 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 cln = curwin->w_cursor.lnum;
2491 if (set_topbot)
2492 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002493 int set_skipcol = FALSE;
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 used = 0;
2496 curwin->w_botline = cln + 1;
2497#ifdef FEAT_DIFF
2498 loff.fill = 0;
2499#endif
2500 for (curwin->w_topline = curwin->w_botline;
2501 curwin->w_topline > 1;
2502 curwin->w_topline = loff.lnum)
2503 {
2504 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002505 topline_back_winheight(&loff, FALSE);
2506 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002508 if (used + loff.height > curwin->w_height)
2509 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002510 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002511 {
2512 // 'smoothscroll' and 'wrap' are set. The above line is
2513 // too long to show in its entirety, so we show just a part
2514 // of it.
2515 if (used < curwin->w_height)
2516 {
2517 int plines_offset = used + loff.height
2518 - curwin->w_height;
2519 used = curwin->w_height;
2520#ifdef FEAT_DIFF
2521 curwin->w_topfill = loff.fill;
2522#endif
2523 curwin->w_topline = loff.lnum;
2524 curwin->w_skipcol = skipcol_from_plines(
2525 curwin, plines_offset);
2526 set_skipcol = TRUE;
2527 }
2528 }
2529 break;
2530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 used += loff.height;
2532#ifdef FEAT_DIFF
2533 curwin->w_topfill = loff.fill;
2534#endif
2535 }
2536 set_empty_rows(curwin, used);
2537 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2538 if (curwin->w_topline != old_topline
2539#ifdef FEAT_DIFF
2540 || curwin->w_topfill != old_topfill
2541#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002542 || set_skipcol
2543 || curwin->w_skipcol != 0)
2544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002546 if (set_skipcol)
2547 redraw_later(UPD_NOT_VALID);
2548 else
2549 reset_skipcol();
2550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552 else
2553 validate_botline();
2554
Bram Moolenaar85a20022019-12-21 18:25:54 +01002555 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556#ifdef FEAT_DIFF
2557 used = plines_nofill(cln);
2558#else
2559 validate_cheight();
2560 used = curwin->w_cline_height;
2561#endif
2562
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002563 // If the cursor is on or below botline, we will at least scroll by the
2564 // height of the cursor line, which is "used". Correct for empty lines,
2565 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (cln >= curwin->w_botline)
2567 {
2568 scrolled = used;
2569 if (cln == curwin->w_botline)
2570 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002571 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002572 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002573 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002574 // Calculate how many screen lines the current top line of window
2575 // occupies. If it is occupying more than the entire window, we
2576 // need to scroll the additional clipped lines to scroll past the
2577 // top line before we can move on to the other lines.
2578 int top_plines =
2579#ifdef FEAT_DIFF
2580 plines_win_nofill
2581#else
2582 plines_win
2583#endif
2584 (curwin, curwin->w_topline, FALSE);
2585 int skip_lines = 0;
2586 int width1 = curwin->w_width - curwin_col_off();
2587 int width2 = width1 + curwin_col_off2();
2588 // similar formula is used in curs_columns()
2589 if (curwin->w_skipcol > width1)
2590 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2591 else if (curwin->w_skipcol > 0)
2592 skip_lines = 1;
2593
2594 top_plines -= skip_lines;
2595 if (top_plines > curwin->w_height)
2596 {
2597 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002598 }
2599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
2602 /*
2603 * Stop counting lines to scroll when
2604 * - hitting start of the file
2605 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002606 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * - lines between botline and cursor have been counted
2608 */
2609#ifdef FEAT_FOLDING
2610 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2611#endif
2612 {
2613 loff.lnum = cln;
2614 boff.lnum = cln;
2615 }
2616#ifdef FEAT_DIFF
2617 loff.fill = 0;
2618 boff.fill = 0;
2619 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2620 - curwin->w_filler_rows;
2621#endif
2622
2623 while (loff.lnum > 1)
2624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2626 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002628 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2630 && loff.lnum <= curwin->w_botline
2631#ifdef FEAT_DIFF
2632 && (loff.lnum < curwin->w_botline
2633 || loff.fill >= fill_below_window)
2634#endif
2635 )
2636 break;
2637
Bram Moolenaar85a20022019-12-21 18:25:54 +01002638 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002640 if (loff.height == MAXCOL)
2641 used = MAXCOL;
2642 else
2643 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (used > curwin->w_height)
2645 break;
2646 if (loff.lnum >= curwin->w_botline
2647#ifdef FEAT_DIFF
2648 && (loff.lnum > curwin->w_botline
2649 || loff.fill <= fill_below_window)
2650#endif
2651 )
2652 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 scrolled += loff.height;
2655 if (loff.lnum == curwin->w_botline
2656#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002657 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#endif
2659 )
2660 scrolled -= curwin->w_empty_rows;
2661 }
2662
2663 if (boff.lnum < curbuf->b_ml.ml_line_count)
2664 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002665 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 botline_forw(&boff);
2667 used += boff.height;
2668 if (used > curwin->w_height)
2669 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002670 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2671 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 extra += boff.height;
2674 if (boff.lnum >= curwin->w_botline
2675#ifdef FEAT_DIFF
2676 || (boff.lnum + 1 == curwin->w_botline
2677 && boff.fill > curwin->w_filler_rows)
2678#endif
2679 )
2680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 scrolled += boff.height;
2683 if (boff.lnum == curwin->w_botline
2684#ifdef FEAT_DIFF
2685 && boff.fill == 0
2686#endif
2687 )
2688 scrolled -= curwin->w_empty_rows;
2689 }
2690 }
2691 }
2692 }
2693
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (scrolled <= 0)
2696 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else if (used > curwin->w_height)
2699 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 else
2702 {
2703 line_count = 0;
2704#ifdef FEAT_DIFF
2705 boff.fill = curwin->w_topfill;
2706#endif
2707 boff.lnum = curwin->w_topline - 1;
2708 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2709 {
2710 botline_forw(&boff);
2711 i += boff.height;
2712 ++line_count;
2713 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 line_count = 9999;
2716 }
2717
2718 /*
2719 * Scroll up if the cursor is off the bottom of the screen a bit.
2720 * Otherwise put it at 1/2 of the screen.
2721 */
2722 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002723 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002724 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002725 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002726 if (do_sms)
2727 scrollup(scrolled, TRUE); // TODO
2728 else
2729 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * If topline didn't change we need to restore w_botline and w_empty_rows
2734 * (we changed them).
2735 * If topline did change, update_screen() will set botline.
2736 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002737 if (curwin->w_topline == old_topline
2738 && curwin->w_skipcol == old_skipcol
2739 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 curwin->w_botline = old_botline;
2742 curwin->w_empty_rows = old_empty_rows;
2743 curwin->w_valid = old_valid;
2744 }
2745 curwin->w_valid |= VALID_TOPLINE;
2746}
2747
2748/*
2749 * Recompute topline to put the cursor halfway the window
2750 * If "atend" is TRUE, also put it halfway at the end of the file.
2751 */
2752 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002753scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
2755 int above = 0;
2756 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002757 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#ifdef FEAT_DIFF
2759 int topfill = 0;
2760#endif
2761 int below = 0;
2762 int used;
2763 lineoff_T loff;
2764 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002765#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002766 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002769#ifdef FEAT_PROP_POPUP
2770 // if the width changed this needs to be updated first
2771 may_update_popup_position();
2772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2774#ifdef FEAT_FOLDING
2775 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2776#endif
2777#ifdef FEAT_DIFF
2778 used = plines_nofill(loff.lnum);
2779 loff.fill = 0;
2780 boff.fill = 0;
2781#else
2782 used = plines(loff.lnum);
2783#endif
2784 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002785
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002786 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002787 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2788 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002789 {
2790 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002791 if (atend)
2792 {
2793 want_height = (curwin->w_height - used) / 2;
2794 used = 0;
2795 }
2796 else
2797 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002798 }
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 while (topline > 1)
2801 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002802 // If using smoothscroll, we can precisely scroll to the
2803 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002804 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002805 {
2806 topline_back_winheight(&loff, FALSE);
2807 if (loff.height == MAXCOL)
2808 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002809 used += loff.height;
2810 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002811 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002812 botline_forw(&boff);
2813 used += boff.height;
2814 }
2815 if (used > want_height)
2816 {
2817 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002818 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002819 topline = loff.lnum;
2820#ifdef FEAT_DIFF
2821 topfill = loff.fill;
2822#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002823 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824 }
2825 break;
2826 }
2827 topline = loff.lnum;
2828#ifdef FEAT_DIFF
2829 topfill = loff.fill;
2830#endif
2831 continue;
2832 }
2833
2834 // If not using smoothscroll, we have to iteratively find how many
2835 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002836 // This may not be right in the middle if the lines'
2837 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002838
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002839 // Depending on "prefer_above" we add a line above or below first.
2840 // Loop twice to avoid duplicating code.
2841 int done = FALSE;
2842 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002844 if (prefer_above ? (round == 2 && below < above)
2845 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002847 // add a line below the cursor
2848 if (boff.lnum < curbuf->b_ml.ml_line_count)
2849 {
2850 botline_forw(&boff);
2851 used += boff.height;
2852 if (used > curwin->w_height)
2853 {
2854 done = TRUE;
2855 break;
2856 }
2857 below += boff.height;
2858 }
2859 else
2860 {
2861 ++below; // count a "~" line
2862 if (atend)
2863 ++used;
2864 }
2865 }
2866
2867 if (prefer_above ? (round == 1 && below >= above)
2868 : (round == 1 && below > above))
2869 {
2870 // add a line above the cursor
2871 topline_back(&loff);
2872 if (loff.height == MAXCOL)
2873 used = MAXCOL;
2874 else
2875 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002877 {
2878 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002880 }
2881 above += loff.height;
2882 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002884 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002888 if (done)
2889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#ifdef FEAT_FOLDING
2893 if (!hasFolding(topline, &curwin->w_topline, NULL))
2894#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002895 {
2896 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002897 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002898 || curwin->w_skipcol != 0)
2899 {
2900 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002901 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002902 {
2903 curwin->w_skipcol = skipcol;
2904 redraw_later(UPD_NOT_VALID);
2905 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002906 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002907 reset_skipcol();
2908 }
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#ifdef FEAT_DIFF
2911 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002912 if (old_topline > curwin->w_topline + curwin->w_height)
2913 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 check_topfill(curwin, FALSE);
2915#endif
2916 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2917 curwin->w_valid |= VALID_TOPLINE;
2918}
2919
2920/*
2921 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002922 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 * If not possible, put it at the same position as scroll_cursor_halfway().
2924 * When called topline must be valid!
2925 */
2926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002929 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 linenr_T botline;
2933 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002936 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /*
2939 * How many lines we would like to have above/below the cursor depends on
2940 * whether the first/last line of the file is on screen.
2941 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002942 above_wanted = so;
2943 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002944 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
2946 above_wanted = mouse_dragging - 1;
2947 below_wanted = mouse_dragging - 1;
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (curwin->w_topline == 1)
2950 {
2951 above_wanted = 0;
2952 max_off = curwin->w_height / 2;
2953 if (below_wanted > max_off)
2954 below_wanted = max_off;
2955 }
2956 validate_botline();
2957 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002958 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 below_wanted = 0;
2961 max_off = (curwin->w_height - 1) / 2;
2962 if (above_wanted > max_off)
2963 above_wanted = max_off;
2964 }
2965
2966 /*
2967 * If there are sufficient file-lines above and below the cursor, we can
2968 * return now.
2969 */
2970 cln = curwin->w_cursor.lnum;
2971 if (cln >= curwin->w_topline + above_wanted
2972 && cln < curwin->w_botline - below_wanted
2973#ifdef FEAT_FOLDING
2974 && !hasAnyFolding(curwin)
2975#endif
2976 )
2977 return;
2978
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002979 if (curwin->w_p_sms && !curwin->w_p_wrap)
2980 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002981 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002982 if (curwin->w_cline_height == curwin->w_height)
2983 {
2984 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002985 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002986 return;
2987 }
2988 // TODO: If the cursor line doesn't fit in the window then only adjust
2989 // w_skipcol.
2990 }
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /*
2993 * Narrow down the area where the cursor can be put by taking lines from
2994 * the top and the bottom until:
2995 * - the desired context lines are found
2996 * - the lines from the top is past the lines from the bottom
2997 */
2998 topline = curwin->w_topline;
2999 botline = curwin->w_botline - 1;
3000#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003001 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 above = curwin->w_topfill;
3003 below = curwin->w_filler_rows;
3004#endif
3005 while ((above < above_wanted || below < below_wanted) && topline < botline)
3006 {
3007 if (below < below_wanted && (below <= above || above >= above_wanted))
3008 {
3009#ifdef FEAT_FOLDING
3010 if (hasFolding(botline, &botline, NULL))
3011 ++below;
3012 else
3013#endif
3014 below += plines(botline);
3015 --botline;
3016 }
3017 if (above < above_wanted && (above < below || below >= below_wanted))
3018 {
3019#ifdef FEAT_FOLDING
3020 if (hasFolding(topline, NULL, &topline))
3021 ++above;
3022 else
3023#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003024 above += PLINES_NOFILL(topline);
3025#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003026 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (topline < botline)
3028 above += diff_check_fill(curwin, topline + 1);
3029#endif
3030 ++topline;
3031 }
3032 }
3033 if (topline == botline || botline == 0)
3034 curwin->w_cursor.lnum = topline;
3035 else if (topline > botline)
3036 curwin->w_cursor.lnum = botline;
3037 else
3038 {
3039 if (cln < topline && curwin->w_topline > 1)
3040 {
3041 curwin->w_cursor.lnum = topline;
3042 curwin->w_valid &=
3043 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3044 }
3045 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3046 {
3047 curwin->w_cursor.lnum = botline;
3048 curwin->w_valid &=
3049 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3050 }
3051 }
3052 curwin->w_valid |= VALID_TOPLINE;
3053}
3054
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003055static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003058 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3059 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003061 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 */
3063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003064onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 long n;
3067 int retval = OK;
3068 lineoff_T loff;
3069 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003070 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
Bram Moolenaar85a20022019-12-21 18:25:54 +01003072 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 beep_flush();
3075 return FAIL;
3076 }
3077
3078 for ( ; count > 0; --count)
3079 {
3080 validate_botline();
3081 /*
3082 * It's an error to move a page up when the first line is already on
3083 * the screen. It's an error to move a page down when the last line
3084 * is on the screen and the topline is 'scrolloff' lines from the
3085 * last line.
3086 */
3087 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003088 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3090 : (curwin->w_topline == 1
3091#ifdef FEAT_DIFF
3092 && curwin->w_topfill ==
3093 diff_check_fill(curwin, curwin->w_topline)
3094#endif
3095 ))
3096 {
3097 beep_flush();
3098 retval = FAIL;
3099 break;
3100 }
3101
3102#ifdef FEAT_DIFF
3103 loff.fill = 0;
3104#endif
3105 if (dir == FORWARD)
3106 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003107 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003109 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003110 if (p_window <= 2)
3111 ++curwin->w_topline;
3112 else
3113 curwin->w_topline += p_window - 2;
3114 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3115 curwin->w_topline = curbuf->b_ml.ml_line_count;
3116 curwin->w_cursor.lnum = curwin->w_topline;
3117 }
3118 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003120 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 curwin->w_topline = curbuf->b_ml.ml_line_count;
3122#ifdef FEAT_DIFF
3123 curwin->w_topfill = 0;
3124#endif
3125 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3126 }
3127 else
3128 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003129 // For the overlap, start with the line just below the window
3130 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 loff.lnum = curwin->w_botline;
3132#ifdef FEAT_DIFF
3133 loff.fill = diff_check_fill(curwin, loff.lnum)
3134 - curwin->w_filler_rows;
3135#endif
3136 get_scroll_overlap(&loff, -1);
3137 curwin->w_topline = loff.lnum;
3138#ifdef FEAT_DIFF
3139 curwin->w_topfill = loff.fill;
3140 check_topfill(curwin, FALSE);
3141#endif
3142 curwin->w_cursor.lnum = curwin->w_topline;
3143 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3144 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3145 }
3146 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003147 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149#ifdef FEAT_DIFF
3150 if (curwin->w_topline == 1)
3151 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003152 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 max_topfill();
3154 continue;
3155 }
3156#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003157 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003158 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003160 if (p_window <= 2)
3161 --curwin->w_topline;
3162 else
3163 curwin->w_topline -= p_window - 2;
3164 if (curwin->w_topline < 1)
3165 curwin->w_topline = 1;
3166 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3167 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3168 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3169 continue;
3170 }
3171
Bram Moolenaar85a20022019-12-21 18:25:54 +01003172 // Find the line at the top of the window that is going to be the
3173 // line at the bottom of the window. Make sure this results in
3174 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 loff.lnum = curwin->w_topline - 1;
3176#ifdef FEAT_DIFF
3177 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3178 - curwin->w_topfill;
3179#endif
3180 get_scroll_overlap(&loff, 1);
3181
3182 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3183 {
3184 loff.lnum = curbuf->b_ml.ml_line_count;
3185#ifdef FEAT_DIFF
3186 loff.fill = 0;
3187 }
3188 else
3189 {
3190 botline_topline(&loff);
3191#endif
3192 }
3193 curwin->w_cursor.lnum = loff.lnum;
3194
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 // Find the line just above the new topline to get the right line
3196 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 n = 0;
3198 while (n <= curwin->w_height && loff.lnum >= 1)
3199 {
3200 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003201 if (loff.height == MAXCOL)
3202 n = MAXCOL;
3203 else
3204 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003206 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 curwin->w_topline = 1;
3209#ifdef FEAT_DIFF
3210 max_topfill();
3211#endif
3212 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3213 }
3214 else
3215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#ifdef FEAT_DIFF
3218 topline_botline(&loff);
3219#endif
3220 botline_forw(&loff);
3221 botline_forw(&loff);
3222#ifdef FEAT_DIFF
3223 botline_topline(&loff);
3224#endif
3225#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3228#endif
3229
Bram Moolenaar85a20022019-12-21 18:25:54 +01003230 // Always scroll at least one line. Avoid getting stuck on
3231 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (loff.lnum >= curwin->w_topline
3233#ifdef FEAT_DIFF
3234 && (loff.lnum > curwin->w_topline
3235 || loff.fill >= curwin->w_topfill)
3236#endif
3237 )
3238 {
3239#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003240 // First try using the maximum number of filler lines. If
3241 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 loff.fill = curwin->w_topfill;
3243 if (curwin->w_topfill < diff_check_fill(curwin,
3244 curwin->w_topline))
3245 max_topfill();
3246 if (curwin->w_topfill == loff.fill)
3247#endif
3248 {
3249 --curwin->w_topline;
3250#ifdef FEAT_DIFF
3251 curwin->w_topfill = 0;
3252#endif
3253 }
3254 comp_botline(curwin);
3255 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003256 curwin->w_valid &=
3257 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 else
3260 {
3261 curwin->w_topline = loff.lnum;
3262#ifdef FEAT_DIFF
3263 curwin->w_topfill = loff.fill;
3264 check_topfill(curwin, FALSE);
3265#endif
3266 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3267 }
3268 }
3269 }
3270 }
3271#ifdef FEAT_FOLDING
3272 foldAdjustCursor();
3273#endif
3274 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003275 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003276 if (retval == OK)
3277 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3279
Bram Moolenaar907dad72018-07-10 15:07:15 +02003280 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003282 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3283 // But make sure we scroll at least one line (happens with mix of long
3284 // wrapping lines and non-wrapping line).
3285 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003287 scroll_cursor_top(1, FALSE);
3288 if (curwin->w_topline <= old_topline
3289 && old_topline < curbuf->b_ml.ml_line_count)
3290 {
3291 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003293 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3294#endif
3295 }
3296 }
3297#ifdef FEAT_FOLDING
3298 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003303 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return retval;
3305}
3306
3307/*
3308 * Decide how much overlap to use for page-up or page-down scrolling.
3309 * This is symmetric, so that doing both keeps the same lines displayed.
3310 * Three lines are examined:
3311 *
3312 * before CTRL-F after CTRL-F / before CTRL-B
3313 * etc. l1
3314 * l1 last but one line ------------
3315 * l2 last text line l2 top text line
3316 * ------------- l3 second text line
3317 * l3 etc.
3318 */
3319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003320get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 int h1, h2, h3, h4;
3323 int min_height = curwin->w_height - 2;
3324 lineoff_T loff0, loff1, loff2;
3325
3326#ifdef FEAT_DIFF
3327 if (lp->fill > 0)
3328 lp->height = 1;
3329 else
3330 lp->height = plines_nofill(lp->lnum);
3331#else
3332 lp->height = plines(lp->lnum);
3333#endif
3334 h1 = lp->height;
3335 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003336 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 loff0 = *lp;
3339 if (dir > 0)
3340 botline_forw(lp);
3341 else
3342 topline_back(lp);
3343 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003344 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003346 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 return;
3348 }
3349
3350 loff1 = *lp;
3351 if (dir > 0)
3352 botline_forw(lp);
3353 else
3354 topline_back(lp);
3355 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003356 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return;
3360 }
3361
3362 loff2 = *lp;
3363 if (dir > 0)
3364 botline_forw(lp);
3365 else
3366 topline_back(lp);
3367 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003368 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003369 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003371 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * Scroll 'scroll' lines up or down.
3376 */
3377 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003378halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 long scrolled = 0;
3381 int i;
3382 int n;
3383 int room;
3384
3385 if (Prenum)
3386 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3387 curwin->w_height : Prenum;
3388 n = (curwin->w_p_scr <= curwin->w_height) ?
3389 curwin->w_p_scr : curwin->w_height;
3390
Bram Moolenaard5d37532017-03-27 23:02:07 +02003391 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 validate_botline();
3393 room = curwin->w_empty_rows;
3394#ifdef FEAT_DIFF
3395 room += curwin->w_filler_rows;
3396#endif
3397 if (flag)
3398 {
3399 /*
3400 * scroll the text up
3401 */
3402 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3403 {
3404#ifdef FEAT_DIFF
3405 if (curwin->w_topfill > 0)
3406 {
3407 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003408 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 --curwin->w_topfill;
3410 }
3411 else
3412#endif
3413 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003414 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 n -= i;
3416 if (n < 0 && scrolled > 0)
3417 break;
3418#ifdef FEAT_FOLDING
3419 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3420#endif
3421 ++curwin->w_topline;
3422#ifdef FEAT_DIFF
3423 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3424#endif
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3427 {
3428 ++curwin->w_cursor.lnum;
3429 curwin->w_valid &=
3430 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3434 scrolled += i;
3435
3436 /*
3437 * Correct w_botline for changed w_topline.
3438 * Won't work when there are filler lines.
3439 */
3440#ifdef FEAT_DIFF
3441 if (curwin->w_p_diff)
3442 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3443 else
3444#endif
3445 {
3446 room += i;
3447 do
3448 {
3449 i = plines(curwin->w_botline);
3450 if (i > room)
3451 break;
3452#ifdef FEAT_FOLDING
3453 (void)hasFolding(curwin->w_botline, NULL,
3454 &curwin->w_botline);
3455#endif
3456 ++curwin->w_botline;
3457 room -= i;
3458 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3459 }
3460 }
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 /*
3463 * When hit bottom of the file: move cursor down.
3464 */
3465 if (n > 0)
3466 {
3467# ifdef FEAT_FOLDING
3468 if (hasAnyFolding(curwin))
3469 {
3470 while (--n >= 0
3471 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3472 {
3473 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3474 &curwin->w_cursor.lnum);
3475 ++curwin->w_cursor.lnum;
3476 }
3477 }
3478 else
3479# endif
3480 curwin->w_cursor.lnum += n;
3481 check_cursor_lnum();
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484 else
3485 {
3486 /*
3487 * scroll the text down
3488 */
3489 while (n > 0 && curwin->w_topline > 1)
3490 {
3491#ifdef FEAT_DIFF
3492 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3493 {
3494 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003495 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 ++curwin->w_topfill;
3497 }
3498 else
3499#endif
3500 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003501 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 n -= i;
3503 if (n < 0 && scrolled > 0)
3504 break;
3505 --curwin->w_topline;
3506#ifdef FEAT_FOLDING
3507 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3508#endif
3509#ifdef FEAT_DIFF
3510 curwin->w_topfill = 0;
3511#endif
3512 }
3513 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3514 VALID_BOTLINE|VALID_BOTLINE_AP);
3515 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (curwin->w_cursor.lnum > 1)
3517 {
3518 --curwin->w_cursor.lnum;
3519 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 /*
3524 * When hit top of the file: move cursor up.
3525 */
3526 if (n > 0)
3527 {
3528 if (curwin->w_cursor.lnum <= (linenr_T)n)
3529 curwin->w_cursor.lnum = 1;
3530 else
3531# ifdef FEAT_FOLDING
3532 if (hasAnyFolding(curwin))
3533 {
3534 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3535 {
3536 --curwin->w_cursor.lnum;
3537 (void)hasFolding(curwin->w_cursor.lnum,
3538 &curwin->w_cursor.lnum, NULL);
3539 }
3540 }
3541 else
3542# endif
3543 curwin->w_cursor.lnum -= n;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003547 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 foldAdjustCursor();
3549# endif
3550#ifdef FEAT_DIFF
3551 check_topfill(curwin, !flag);
3552#endif
3553 cursor_correct();
3554 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003555 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003557
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003559do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560{
3561 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003562 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003563 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003564 colnr_T curswant = curwin->w_curswant;
3565 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003566 win_T *old_curwin = curwin;
3567 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568 int old_VIsual_select = VIsual_select;
3569 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570
3571 /*
3572 * loop through the cursorbound windows
3573 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003574 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003575 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576 {
3577 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003578 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 if (curwin != old_curwin && curwin->w_p_crb)
3580 {
3581# ifdef FEAT_DIFF
3582 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003583 curwin->w_cursor.lnum =
3584 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585 else
3586# endif
3587 curwin->w_cursor.lnum = line;
3588 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003589 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003590 curwin->w_curswant = curswant;
3591 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592
Bram Moolenaar85a20022019-12-21 18:25:54 +01003593 // Make sure the cursor is in a valid position. Temporarily set
3594 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003595 int restart_edit_save = restart_edit;
3596 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003598
3599 // Avoid a scroll here for the cursor position, 'scrollbind' is
3600 // more important.
3601 if (!curwin->w_p_scb)
3602 validate_cursor();
3603
Bram Moolenaar61452852011-02-01 18:01:11 +01003604 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003605 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 if (has_mbyte)
3607 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003608 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003609
Bram Moolenaar85a20022019-12-21 18:25:54 +01003610 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003611 if (!curwin->w_p_scb)
3612 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003613 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003614 }
3615 }
3616
3617 /*
3618 * reset current-window
3619 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620 VIsual_select = old_VIsual_select;
3621 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 curwin = old_curwin;
3623 curbuf = old_curbuf;
3624}