blob: e021d71c59f0a007cd4435f8297dedcf658967a8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
Bram Moolenaarf6196f42022-10-02 21:29:55 +010042adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
206 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 int
213sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000214{
215#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100216 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100218 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return 0;
220#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100221 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
222 if (wp->w_p_list && wp->w_lcs_chars.prec)
223 return 1;
224
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000225 return extra2 > 3 ? 0 : 3 - extra2;
226}
227
228/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000229 * Calculates the skipcol offset for window "wp" given how many
230 * physical lines we want to scroll down.
231 */
232 static int
233skipcol_from_plines(win_T *wp, int plines_off)
234{
235 int width1 = wp->w_width - win_col_off(wp);
236
237 int skipcol = 0;
238 if (plines_off > 0)
239 skipcol += width1;
240 if (plines_off > 1)
241 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
242 return skipcol;
243}
244
245/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100246 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247 */
248 static void
249reset_skipcol(void)
250{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000251 if (curwin->w_skipcol == 0)
252 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000253
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000254 curwin->w_skipcol = 0;
255
256 // Should use the least expensive way that displays all that changed.
257 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
258 // enough when the top line gets another screen line.
259 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000260}
261
262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * Update curwin->w_topline and redraw if necessary.
264 * Used to update the screen before printing a message.
265 */
266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100267update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 update_topline();
270 if (must_redraw)
271 update_screen(0);
272}
273
274/*
275 * Update curwin->w_topline to move the cursor onto the screen.
276 */
277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 long line_count;
281 int halfheight;
282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100316 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100347 else if (curwin->w_skipcol > 0
348 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 {
350 colnr_T vcol;
351
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000352 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100353 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100355 int overlap = sms_marker_overlap(curwin, curwin_col_off()
356 - curwin_col_off2());
357 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000399 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 else
401 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 check_botline = TRUE;
404 }
405 }
406
407 else
408 {
409#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100410 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
412#endif
413 check_botline = TRUE;
414 }
415 }
416
417 /*
418 * If the cursor is below the bottom of the window, scroll the window
419 * to put the cursor on the window.
420 * When w_botline is invalid, recompute it first, to avoid a redraw later.
421 * If w_botline was approximated, we might need a redraw later in a few
422 * cases, but we don't want to spend (a lot of) time recomputing w_botline
423 * for every small change.
424 */
425 if (check_botline)
426 {
427 if (!(curwin->w_valid & VALID_BOTLINE_AP))
428 validate_botline();
429
430 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
431 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 if (curwin->w_cursor.lnum < curwin->w_botline)
433 {
434 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100435 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_FOLDING
437 || hasAnyFolding(curwin)
438#endif
439 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lineoff_T loff;
442
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // Cursor is (a few lines) above botline, check if there are
444 // 'scrolloff' window lines below the cursor. If not, need to
445 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 n = curwin->w_empty_rows;
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
451#endif
452#ifdef FEAT_DIFF
453 loff.fill = 0;
454 n += curwin->w_filler_rows;
455#endif
456 loff.height = 0;
457 while (loff.lnum < curwin->w_botline
458#ifdef FEAT_DIFF
459 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
460#endif
461 )
462 {
463 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 botline_forw(&loff);
467 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100468 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 }
472 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000474 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 if (check_botline)
477 {
478#ifdef FEAT_FOLDING
479 if (hasAnyFolding(curwin))
480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // Count the number of logical lines between the cursor and
482 // botline - scrolloff (approximation of how much will be
483 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 line_count = 0;
485 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (lnum <= 0 || line_count > curwin->w_height + 1)
491 break;
492 (void)hasFolding(lnum, &lnum, NULL);
493 }
494 }
495 else
496#endif
497 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100498 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000502 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
504 }
505 }
506 curwin->w_valid |= VALID_TOPLINE;
507
508 /*
509 * Need to redraw when topline changed.
510 */
511 if (curwin->w_topline != old_topline
512#ifdef FEAT_DIFF
513 || curwin->w_topfill != old_topfill
514#endif
515 )
516 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100517 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000518 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100519
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100520 // When 'smoothscroll' is not set, should reset w_skipcol.
521 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100522 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100523 else if (curwin->w_skipcol != 0)
524 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000525
Bram Moolenaar85a20022019-12-21 18:25:54 +0100526 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (curwin->w_cursor.lnum == curwin->w_topline)
528 validate_cursor();
529 }
530
Bram Moolenaar375e3392019-01-31 18:26:10 +0100531 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
533
534/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000535 * Return the scrolljump value to use for the current window.
536 * When 'scrolljump' is positive use it as-is.
537 * When 'scrolljump' is negative use it as a percentage of the window height.
538 */
539 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000541{
542 if (p_sj >= 0)
543 return (int)p_sj;
544 return (curwin->w_height * -p_sj) / 100;
545}
546
547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
549 * current window.
550 */
551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 lineoff_T loff;
555 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100556 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar375e3392019-01-31 18:26:10 +0100558 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_FOLDING
560 || hasAnyFolding(curwin)
561#endif
562 )
563 {
564 loff.lnum = curwin->w_cursor.lnum;
565#ifdef FEAT_DIFF
566 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100567 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#else
569 n = 0;
570#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100571 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100572 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100575 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (loff.lnum < curwin->w_topline
577#ifdef FEAT_DIFF
578 || (loff.lnum == curwin->w_topline && loff.fill > 0)
579#endif
580 )
581 break;
582 n += loff.height;
583 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100584 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 return TRUE;
586 }
587 return FALSE;
588}
589
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100590/*
591 * Update w_curswant.
592 */
593 void
594update_curswant_force(void)
595{
596 validate_virtcol();
597 curwin->w_curswant = curwin->w_virtcol
598#ifdef FEAT_PROP_POPUP
599 - curwin->w_virtcol_first_char
600#endif
601 ;
602 curwin->w_set_curswant = FALSE;
603}
604
605/*
606 * Update w_curswant if w_set_curswant is set.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100612 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
615/*
616 * Check if the cursor has moved. Set the w_valid flag accordingly.
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
622 {
623 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100624 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
625 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 wp->w_valid_cursor = wp->w_cursor;
627 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100628 wp->w_valid_skipcol = wp->w_skipcol;
629 }
630 else if (wp->w_skipcol != wp->w_valid_skipcol)
631 {
632 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
633 |VALID_CHEIGHT|VALID_CROW
634 |VALID_BOTLINE|VALID_BOTLINE_AP);
635 wp->w_valid_cursor = wp->w_cursor;
636 wp->w_valid_leftcol = wp->w_leftcol;
637 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639 else if (wp->w_cursor.col != wp->w_valid_cursor.col
640 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100641 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
644 wp->w_valid_cursor.col = wp->w_cursor.col;
645 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648}
649
650/*
651 * Call this function when some window settings have changed, which require
652 * the cursor position, botline and topline to be recomputed and the window to
653 * be redrawn. E.g, when changing the 'wrap' option or folding.
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 changed_window_setting_win(curwin);
659}
660
661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 wp->w_lines_valid = 0;
665 changed_line_abv_curs_win(wp);
666 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100667 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668}
669
Dominique Pellee764d1b2023-03-12 21:20:59 +0000670#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000672 * Call changed_window_setting_win() for every window containing "buf".
673 */
674 void
675changed_window_setting_buf(buf_T *buf)
676{
677 tabpage_T *tp;
678 win_T *wp;
679
680 FOR_ALL_TAB_WINDOWS(tp, wp)
681 if (wp->w_buffer == buf)
682 changed_window_setting_win(wp);
683}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000684#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000685
686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * Set wp->w_topline to a certain number.
688 */
689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100690set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200692#ifdef FEAT_DIFF
693 linenr_T prev_topline = wp->w_topline;
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
699#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100700 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100702 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
703 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000705 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200707 if (lnum != prev_topline)
708 // Keep the filler lines when the topline didn't change.
709 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
711 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100713 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715
716/*
717 * Call this function when the length of the cursor line (in screen
718 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100719 * If the line length changed the number of screen lines might change,
720 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 * Need to take care of w_botline separately!
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaar85090142023-06-01 19:27:08 +0100726 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 |VALID_CHEIGHT|VALID_TOPLINE);
728}
729
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaar85090142023-06-01 19:27:08 +0100733 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 |VALID_CHEIGHT|VALID_TOPLINE);
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Call this function when the length of a line (in screen characters) above
739 * the cursor have changed.
740 * Need to take care of w_botline separately!
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
746 |VALID_CHEIGHT|VALID_TOPLINE);
747}
748
749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
753 |VALID_CHEIGHT|VALID_TOPLINE);
754}
755
Dominique Pellee764d1b2023-03-12 21:20:59 +0000756#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100758 * Display of line has changed for "buf", invalidate cursor position and
759 * w_botline.
760 */
761 void
762changed_line_display_buf(buf_T *buf)
763{
764 win_T *wp;
765
766 FOR_ALL_WINDOWS(wp)
767 if (wp->w_buffer == buf)
768 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
769 |VALID_CROW|VALID_CHEIGHT
770 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
771}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000772#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100773
774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * Make sure the value of curwin->w_botline is valid.
776 */
777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100778validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100780 validate_botline_win(curwin);
781}
782
783/*
784 * Make sure the value of wp->w_botline is valid.
785 */
786 void
787validate_botline_win(win_T *wp)
788{
789 if (!(wp->w_valid & VALID_BOTLINE))
790 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * Mark curwin->w_botline as invalid (because of some change in the buffer).
795 */
796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100797invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
799 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
800}
801
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809approximate_botline_win(
810 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 wp->w_valid &= ~VALID_BOTLINE;
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
817 */
818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 check_cursor_moved(curwin);
822 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
823 (VALID_WROW|VALID_WCOL));
824}
825
826/*
827 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
828 * w_topline must be valid, you may need to call update_topline() first!
829 */
830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100831validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100833 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 check_cursor_moved(curwin);
835 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
836 curs_columns(TRUE);
837}
838
839#if defined(FEAT_GUI) || defined(PROTO)
840/*
841 * validate w_cline_row.
842 */
843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 /*
847 * First make sure that w_topline is valid (after moving the cursor).
848 */
849 update_topline();
850 check_cursor_moved(curwin);
851 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100852 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854#endif
855
856/*
857 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200858 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100861curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 linenr_T lnum;
864 int i;
865 int all_invalid;
866 int valid;
867#ifdef FEAT_FOLDING
868 long fold_count;
869#endif
870
Bram Moolenaar85a20022019-12-21 18:25:54 +0100871 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 all_invalid = (!redrawing()
873 || wp->w_lines_valid == 0
874 || wp->w_lines[0].wl_lnum > wp->w_topline);
875 i = 0;
876 wp->w_cline_row = 0;
877 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
878 {
879 valid = FALSE;
880 if (!all_invalid && i < wp->w_lines_valid)
881 {
882 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100883 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (wp->w_lines[i].wl_lnum == lnum)
885 {
886#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 // Check for newly inserted lines below this row, in which
888 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (!wp->w_buffer->b_mod_set
890 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
891 || wp->w_buffer->b_mod_top
892 > wp->w_lines[i].wl_lastlnum + 1)
893#endif
894 valid = TRUE;
895 }
896 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100897 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100900 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100902 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100904 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906#ifdef FEAT_FOLDING
907 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (lnum > wp->w_cursor.lnum)
910 break;
911#else
912 ++lnum;
913#endif
914 wp->w_cline_row += wp->w_lines[i].wl_size;
915 }
916 else
917 {
918#ifdef FEAT_FOLDING
919 fold_count = foldedCount(wp, lnum, NULL);
920 if (fold_count)
921 {
922 lnum += fold_count;
923 if (lnum > wp->w_cursor.lnum)
924 break;
925 ++wp->w_cline_row;
926 }
927 else
928#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100929 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100930 wp->w_cline_row += plines_correct_topline(wp, lnum);
931 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 }
935
936 check_cursor_moved(wp);
937 if (!(wp->w_valid & VALID_CHEIGHT))
938 {
939 if (all_invalid
940 || i == wp->w_lines_valid
941 || (i < wp->w_lines_valid
942 && (!wp->w_lines[i].wl_valid
943 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
944 {
945#ifdef FEAT_DIFF
946 if (wp->w_cursor.lnum == wp->w_topline)
947 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
948 TRUE) + wp->w_topfill;
949 else
950#endif
951 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
952#ifdef FEAT_FOLDING
953 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
954 NULL, NULL, TRUE, NULL);
955#endif
956 }
957 else if (i > wp->w_lines_valid)
958 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 wp->w_cline_height = 0;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
963 NULL, NULL, TRUE, NULL);
964#endif
965 }
966 else
967 {
968 wp->w_cline_height = wp->w_lines[i].wl_size;
969#ifdef FEAT_FOLDING
970 wp->w_cline_folded = wp->w_lines[i].wl_folded;
971#endif
972 }
973 }
974
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100975 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Validate curwin->w_virtcol only.
981 */
982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100983validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 validate_virtcol_win(curwin);
986}
987
988/*
989 * Validate wp->w_virtcol only.
990 */
991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100992validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000995
996 if (wp->w_valid & VALID_VIRTCOL)
997 return;
998
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100999#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001000 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001001#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001002 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001003#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001004 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001005#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001006 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/*
1010 * Validate curwin->w_cline_height only.
1011 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001012 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001013validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016
1017 if (curwin->w_valid & VALID_CHEIGHT)
1018 return;
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001021 if (curwin->w_cursor.lnum == curwin->w_topline)
1022 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1023 + curwin->w_topfill;
1024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001026 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001028 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001030 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001034 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 colnr_T off;
1040 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001041 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001045 if (curwin->w_valid & VALID_WCOL)
1046 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001047
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001048 col = curwin->w_virtcol;
1049 off = curwin_col_off();
1050 col += off;
1051 width = curwin->w_width - off + curwin_col_off2();
1052
1053 // long line wrapping, adjust curwin->w_wrow
1054 if (curwin->w_p_wrap
1055 && col >= (colnr_T)curwin->w_width
1056 && width > 0)
1057 // use same formula as what is used in curs_columns()
1058 col -= ((col - curwin->w_width) / width + 1) * width;
1059 if (col > (int)curwin->w_leftcol)
1060 col -= curwin->w_leftcol;
1061 else
1062 col = 0;
1063 curwin->w_wcol = col;
1064
1065 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001066#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001067 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001072 * Compute offset of a window, occupied by absolute or relative line number,
1073 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar64486672010-05-16 15:46:46 +02001078 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#ifdef FEAT_FOLDING
1081 + wp->w_p_fdc
1082#endif
1083#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001084 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#endif
1086 );
1087}
1088
1089 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 return win_col_off(curwin);
1093}
1094
1095/*
1096 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001097 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1098 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar64486672010-05-16 15:46:46 +02001103 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001104 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 return 0;
1106}
1107
1108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001109curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 return win_col_off2(curwin);
1112}
1113
1114/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001115 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * Also updates curwin->w_wrow and curwin->w_cline_row.
1117 * Also updates curwin->w_leftcol.
1118 */
1119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001120curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001121 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int off_left, off_right;
1126 int n;
1127 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001128 int width1; // text width for first screen line
1129 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int new_leftcol;
1131 colnr_T startcol;
1132 colnr_T endcol;
1133 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001134 long so = get_scrolloff_value();
1135 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001136 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 /*
1139 * First make sure that w_topline is valid (after moving the cursor).
1140 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001141 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 /*
1144 * Next make sure that w_cline_row is valid.
1145 */
1146 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001147 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001149#ifdef FEAT_PROP_POPUP
1150 // will be set by getvvcol() but not reset
1151 curwin->w_virtcol_first_char = 0;
1152#endif
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /*
1155 * Compute the number of virtual columns.
1156 */
1157#ifdef FEAT_FOLDING
1158 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001159 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1161 else
1162#endif
1163 getvvcol(curwin, &curwin->w_cursor,
1164 &startcol, &(curwin->w_virtcol), &endcol);
1165
Bram Moolenaar85a20022019-12-21 18:25:54 +01001166 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001168 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170 extra = curwin_col_off();
1171 curwin->w_wcol = curwin->w_virtcol + extra;
1172 endcol += extra;
1173
1174 /*
1175 * Now compute w_wrow, counting screen lines from w_cline_row.
1176 */
1177 curwin->w_wrow = curwin->w_cline_row;
1178
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001179 width1 = curwin->w_width - extra;
1180 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001183 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001184 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001185 if (curwin->w_p_wrap)
1186 curwin->w_wrow = curwin->w_height - 1;
1187 else
1188 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001190 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001194 // skip columns that are not visible
1195 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001197 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001198 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199 // Deduct by multiples of width2. This allows the long line
1200 // wrapping formula below to correctly calculate the w_wcol value
1201 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001202 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001203 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001204 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001205 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001206 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001207
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001208 did_sub_skipcol = TRUE;
1209 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001212 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001215 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1216 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 curwin->w_wrow += n;
1218
1219#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001220 // When cursor wraps to first char of next line in Insert
1221 // mode, the 'showbreak' string isn't shown, backup to first
1222 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001223 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001224 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001225 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 curwin->w_wcol = 0;
1227#endif
1228 }
1229 }
1230
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1232 // is not folded.
1233 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001234 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_FOLDING
1236 && !curwin->w_cline_folded
1237#endif
1238 )
1239 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001240#ifdef FEAT_PROP_POPUP
1241 if (curwin->w_virtcol_first_char > 0)
1242 {
1243 int cols = (curwin->w_width - extra);
1244 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1245
1246 // each "above" text prop shifts the text one row down
1247 curwin->w_wrow += rows;
1248 curwin->w_wcol -= rows * cols;
1249 endcol -= rows * cols;
1250 curwin->w_cline_height = rows + 1;
1251 }
1252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 /*
1254 * If Cursor is left of the screen, scroll rightwards.
1255 * If Cursor is right of the screen, scroll leftwards
1256 * If we get closer to the edge than 'sidescrolloff', scroll a little
1257 * extra
1258 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001259 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001260 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001261 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (off_left < 0 || off_right > 0)
1263 {
1264 if (off_left < 0)
1265 diff = -off_left;
1266 else
1267 diff = off_right;
1268
Bram Moolenaar85a20022019-12-21 18:25:54 +01001269 // When far off or not enough room on either side, put cursor in
1270 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001271 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1272 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 if (diff < p_ss)
1276 diff = p_ss;
1277 if (off_left < 0)
1278 new_leftcol = curwin->w_leftcol - diff;
1279 else
1280 new_leftcol = curwin->w_leftcol + diff;
1281 }
1282 if (new_leftcol < 0)
1283 new_leftcol = 0;
1284 if (new_leftcol != (int)curwin->w_leftcol)
1285 {
1286 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001288 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
1291 curwin->w_wcol -= curwin->w_leftcol;
1292 }
1293 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1294 curwin->w_wcol -= curwin->w_leftcol;
1295 else
1296 curwin->w_wcol = 0;
1297
1298#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001299 // Skip over filler lines. At the top use w_topfill, there
1300 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (curwin->w_cursor.lnum == curwin->w_topline)
1302 curwin->w_wrow += curwin->w_topfill;
1303 else
1304 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1305#endif
1306
1307 prev_skipcol = curwin->w_skipcol;
1308
1309 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if ((curwin->w_wrow >= curwin->w_height
1312 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001313 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && (p_lines =
1315#ifdef FEAT_DIFF
1316 plines_win_nofill
1317#else
1318 plines_win
1319#endif
1320 (curwin, curwin->w_cursor.lnum, FALSE))
1321 - 1 >= curwin->w_height))
1322 && curwin->w_height != 0
1323 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001324 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001325 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 // Cursor past end of screen. Happens with a single line that does
1328 // not fit on screen. Find a skipcol to show the text around the
1329 // cursor. Avoid scrolling all the time. compute value of "extra":
1330 // 1: Less than 'scrolloff' lines above
1331 // 2: Less than 'scrolloff' lines below
1332 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001334 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 // Compute last display line of the buffer line that we want at the
1337 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (p_lines == 0)
1339 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1340 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001341 if (p_lines > curwin->w_wrow + so)
1342 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001345 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 extra += 2;
1347
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001348 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (n > curwin->w_height / 2)
1353 n -= curwin->w_height / 2;
1354 else
1355 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001356 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (n > p_lines - curwin->w_height + 1)
1358 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001359 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else if (extra == 1)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1365 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (extra > 0)
1367 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1369 extra = curwin->w_skipcol / width2;
1370 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 }
1373 else if (extra == 2)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (endcol > curwin->w_skipcol)
1380 curwin->w_skipcol = endcol;
1381 }
1382
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001383 // adjust w_wrow for the changed w_skipcol
1384 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001386 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001387 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (curwin->w_wrow >= curwin->w_height)
1390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001393 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 curwin->w_wrow -= extra;
1395 }
1396
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001397 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (extra > 0)
1399 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1400 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001401 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001403 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_skipcol = 0;
1405 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001406 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001408#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001409 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001410#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001411#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1412 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1413 {
1414 curwin->w_wrow += popup_top_extra(curwin);
1415 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001416 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001417 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001418 else
1419 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001420#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001421
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001422 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1423 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001424 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001425 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1428}
1429
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001430#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001431/*
1432 * Compute the screen position of text character at "pos" in window "wp"
1433 * The resulting values are one-based, zero when character is not visible.
1434 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001435 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436textpos2screenpos(
1437 win_T *wp,
1438 pos_T *pos,
1439 int *rowp, // screen row
1440 int *scolp, // start screen column
1441 int *ccolp, // cursor screen column
1442 int *ecolp) // end screen column
1443{
1444 colnr_T scol = 0, ccol = 0, ecol = 0;
1445 int row = 0;
1446 int rowoff = 0;
1447 colnr_T coloff = 0;
1448
Bram Moolenaar189663b2021-07-21 18:04:56 +02001449 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 colnr_T col;
1452 int width;
1453 linenr_T lnum = pos->lnum;
1454#ifdef FEAT_FOLDING
1455 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001457 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1458#endif
1459 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001460
1461#ifdef FEAT_DIFF
1462 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001463 row += lnum == wp->w_topline ? wp->w_topfill
1464 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001465#endif
1466
zeertzjqba2d1912022-12-18 12:28:59 +00001467 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468#ifdef FEAT_FOLDING
1469 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001470 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001471 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001472 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001473 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001474 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001475#endif
1476 {
1477 getvcol(wp, pos, &scol, &ccol, &ecol);
1478
1479 // similar to what is done in validate_cursor_col()
1480 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001481 col += off;
1482 width = wp->w_width - off + win_col_off2(wp);
1483
zeertzjq55daae32023-06-04 19:29:22 +01001484 if (lnum == wp->w_topline)
zeertzjqf0e68c02023-06-03 17:11:47 +01001485 col -= wp->w_skipcol;
1486
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001487 // long line wrapping, adjust row
1488 if (wp->w_p_wrap
1489 && col >= (colnr_T)wp->w_width
1490 && width > 0)
1491 {
1492 // use same formula as what is used in curs_columns()
1493 rowoff = ((col - wp->w_width) / width + 1);
1494 col -= rowoff * width;
1495 }
1496 col -= wp->w_leftcol;
1497 if (col >= wp->w_width)
1498 col = -1;
1499 if (col >= 0 && row + rowoff <= wp->w_height)
1500 {
1501 coloff = col - scol + wp->w_wincol + 1;
1502 row += W_WINROW(wp);
1503 }
1504 else
1505 // character is left, right or below of the window
1506 row = rowoff = scol = ccol = ecol = 0;
1507 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001509 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001510 *scolp = scol + coloff;
1511 *ccolp = ccol + coloff;
1512 *ecolp = ecol + coloff;
1513}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001514#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515
Bram Moolenaar12034e22019-08-25 22:25:02 +02001516#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001517/*
1518 * "screenpos({winid}, {lnum}, {col})" function
1519 */
1520 void
1521f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1522{
1523 dict_T *dict;
1524 win_T *wp;
1525 pos_T pos;
1526 int row = 0;
1527 int scol = 0, ccol = 0, ecol = 0;
1528
Bram Moolenaar93a10962022-06-16 11:42:09 +01001529 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530 return;
1531 dict = rettv->vval.v_dict;
1532
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001533 if (in_vim9script()
1534 && (check_for_number_arg(argvars, 0) == FAIL
1535 || check_for_number_arg(argvars, 1) == FAIL
1536 || check_for_number_arg(argvars, 2) == FAIL))
1537 return;
1538
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp == NULL)
1541 return;
1542
1543 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001544 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1545 {
1546 semsg(_(e_invalid_line_number_nr), pos.lnum);
1547 return;
1548 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001549 pos.col = tv_get_number(&argvars[2]) - 1;
1550 pos.coladd = 0;
1551 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1552
1553 dict_add_number(dict, "row", row);
1554 dict_add_number(dict, "col", scol);
1555 dict_add_number(dict, "curscol", ccol);
1556 dict_add_number(dict, "endcol", ecol);
1557}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001558
1559/*
1560 * "virtcol2col({winid}, {lnum}, {col})" function
1561 */
1562 void
1563f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1564{
1565 win_T *wp;
1566 linenr_T lnum;
1567 int screencol;
1568 int error = FALSE;
1569
1570 rettv->vval.v_number = -1;
1571
1572 if (check_for_number_arg(argvars, 0) == FAIL
1573 || check_for_number_arg(argvars, 1) == FAIL
1574 || check_for_number_arg(argvars, 2) == FAIL)
1575 return;
1576
1577 wp = find_win_by_nr_or_id(&argvars[0]);
1578 if (wp == NULL)
1579 return;
1580
1581 lnum = tv_get_number_chk(&argvars[1], &error);
1582 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1583 return;
1584
1585 screencol = tv_get_number_chk(&argvars[2], &error);
1586 if (error || screencol < 0)
1587 return;
1588
1589 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1590}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001591#endif
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
1594 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001597scrolldown(
1598 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001599 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int wrow;
1603 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001604 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001605 int width1 = 0;
1606 int width2 = 0;
1607
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001608 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001609 {
1610 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001611 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614#ifdef FEAT_FOLDING
1615 linenr_T first;
1616
Bram Moolenaar85a20022019-12-21 18:25:54 +01001617 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1619#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001620 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001621 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001624 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1625 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 ++curwin->w_topfill;
1628 ++done;
1629 }
1630 else
1631#endif
1632 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001633 // break when at the very top
1634 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001635 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001637 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001639 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001640 if (curwin->w_skipcol >= width1 + width2)
1641 curwin->w_skipcol -= width2;
1642 else
1643 curwin->w_skipcol -= width1;
1644 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001648 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001649 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650 --curwin->w_topline;
1651 curwin->w_skipcol = 0;
1652#ifdef FEAT_DIFF
1653 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001655#ifdef FEAT_FOLDING
1656 // A sequence of folded lines only counts for one logical line
1657 if (hasFolding(curwin->w_topline, &first, NULL))
1658 {
1659 ++done;
1660 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001661 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001662 curwin->w_botline -= curwin->w_topline - first;
1663 curwin->w_topline = first;
1664 }
1665 else
1666#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001667 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001668 {
1669 int size = win_linetabsize(curwin, curwin->w_topline,
1670 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1671 if (size > width1)
1672 {
1673 curwin->w_skipcol = width1;
1674 size -= width1;
1675 redraw_later(UPD_NOT_VALID);
1676 }
1677 while (size > width2)
1678 {
1679 curwin->w_skipcol += width2;
1680 size -= width2;
1681 }
1682 ++done;
1683 }
1684 else
1685 done += PLINES_NOFILL(curwin->w_topline);
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 invalidate_botline();
1690 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 curwin->w_wrow += done; // keep w_wrow updated
1692 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694#ifdef FEAT_DIFF
1695 if (curwin->w_cursor.lnum == curwin->w_topline)
1696 curwin->w_cline_row = 0;
1697 check_topfill(curwin, TRUE);
1698#endif
1699
1700 /*
1701 * Compute the row number of the last row of the cursor line
1702 * and move the cursor onto the displayed part of the window.
1703 */
1704 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001705 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 validate_virtcol();
1708 validate_cheight();
1709 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001710 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
1712 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1713 {
1714#ifdef FEAT_FOLDING
1715 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1716 {
1717 --wrow;
1718 if (first == 1)
1719 curwin->w_cursor.lnum = 1;
1720 else
1721 curwin->w_cursor.lnum = first - 1;
1722 }
1723 else
1724#endif
1725 wrow -= plines(curwin->w_cursor.lnum--);
1726 curwin->w_valid &=
1727 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1728 moved = TRUE;
1729 }
1730 if (moved)
1731 {
1732#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001733 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 foldAdjustCursor();
1735#endif
1736 coladvance(curwin->w_curswant);
1737 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001738
1739 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1740 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001741 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001742 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1743
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001744 // make sure the cursor is in the visible text
1745 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001746 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001747 int row = 0;
1748 if (col >= width1)
1749 {
1750 col -= width1;
1751 ++row;
1752 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001753 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001754 {
1755 row += col / width2;
1756 col = col % width2;
1757 }
1758 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001759 {
1760 curwin->w_curswant = curwin->w_virtcol
1761 - (row - curwin->w_height + 1) * width2;
1762 coladvance(curwin->w_curswant);
1763 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765}
1766
1767/*
1768 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771scrollup(
1772 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001775 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001777 if (do_sms
1778# ifdef FEAT_FOLDING
1779 || (byfold && hasAnyFolding(curwin))
1780# endif
1781# ifdef FEAT_DIFF
1782 || (curwin->w_p_diff && !curwin->w_p_wrap)
1783# endif
1784 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001786 int width1 = curwin->w_width - curwin_col_off();
1787 int width2 = width1 + curwin_col_off2();
1788 int size = 0;
1789 linenr_T prev_topline = curwin->w_topline;
zeertzjq3c802272023-06-03 22:08:33 +01001790 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001791
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001792 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001793 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001794
1795 // diff mode: first consume "topfill"
1796 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1797 // the line, then advance to the next line.
1798 // folding: count each sequence of folded lines as one logical line.
1799 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801# ifdef FEAT_DIFF
1802 if (curwin->w_topfill > 0)
1803 --curwin->w_topfill;
1804 else
1805# endif
1806 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001807 linenr_T lnum = curwin->w_topline;
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809# ifdef FEAT_FOLDING
1810 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001811 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 (void)hasFolding(lnum, NULL, &lnum);
1813# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001814 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001815 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001816 // 'smoothscroll': increase "w_skipcol" until it goes over
1817 // the end of the line, then advance to the next line.
1818 int add = curwin->w_skipcol > 0 ? width2 : width1;
1819 curwin->w_skipcol += add;
1820 if (curwin->w_skipcol >= size)
1821 {
1822 if (lnum == curbuf->b_ml.ml_line_count)
1823 {
1824 // at the last screen line, can't scroll further
1825 curwin->w_skipcol -= add;
1826 break;
1827 }
1828 ++lnum;
1829 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001830 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001831 else
1832 {
1833 if (lnum >= curbuf->b_ml.ml_line_count)
1834 break;
1835 ++lnum;
1836 }
1837
1838 if (lnum > curwin->w_topline)
1839 {
1840 // approximate w_botline
1841 curwin->w_botline += lnum - curwin->w_topline;
1842 curwin->w_topline = lnum;
1843# ifdef FEAT_DIFF
1844 curwin->w_topfill = diff_check_fill(curwin, lnum);
1845# endif
1846 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001847 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001848 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001849 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001850 }
1851 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001852
zeertzjq55daae32023-06-04 19:29:22 +01001853 // TODO: is comparing w_topline with prev_topline still needed?
zeertzjq3c802272023-06-03 22:08:33 +01001854 if (curwin->w_topline == prev_topline
1855 || curwin->w_skipcol != prev_skipcol)
1856 // need to redraw because wl_size of the topline may now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001857 redraw_later(UPD_NOT_VALID);
1858 }
1859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001862 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864
1865 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1866 curwin->w_topline = curbuf->b_ml.ml_line_count;
1867 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1868 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1869
1870#ifdef FEAT_DIFF
1871 check_topfill(curwin, FALSE);
1872#endif
1873
1874#ifdef FEAT_FOLDING
1875 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001876 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1878#endif
1879
1880 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1881 if (curwin->w_cursor.lnum < curwin->w_topline)
1882 {
1883 curwin->w_cursor.lnum = curwin->w_topline;
1884 curwin->w_valid &=
1885 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1886 coladvance(curwin->w_curswant);
1887 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001888 if (curwin->w_cursor.lnum == curwin->w_topline
1889 && do_sms && curwin->w_skipcol > 0)
1890 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001891 int col_off = curwin_col_off();
1892 int col_off2 = curwin_col_off2();
1893
1894 int width1 = curwin->w_width - col_off;
1895 int width2 = width1 + col_off2;
1896 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001897 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001898 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001899 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001900
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001901 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001902 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001903 int overlap = scrolloff_cols != 0 ? 0
1904 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001905
Bram Moolenaar118c2352022-10-09 17:19:27 +01001906 // Make sure the cursor is in a visible part of the line, taking
1907 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001908 // If there are not enough screen lines put the cursor in the middle.
1909 if (scrolloff_cols > space_cols / 2)
1910 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001912 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001913 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001914 colnr_T col = curwin->w_virtcol;
1915
1916 if (col < width1)
1917 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001918 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001920 curwin->w_curswant = col;
1921 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001922
1923 // validate_virtcol() marked various things as valid, but after
1924 // moving the cursor they need to be recomputed
1925 curwin->w_valid &=
1926 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001927 }
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001931/*
1932 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1933 * valid for 'smoothscroll'.
1934 */
1935 void
1936adjust_skipcol(void)
1937{
1938 if (!curwin->w_p_wrap
1939 || !curwin->w_p_sms
1940 || curwin->w_cursor.lnum != curwin->w_topline)
1941 return;
1942
1943 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001944 if (width1 <= 0)
1945 return; // no text will be displayed
1946
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001947 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001948 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001949 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1950 int scrolled = FALSE;
1951
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001952 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001953 if (curwin->w_cline_height == curwin->w_height
1954 // w_cline_height may be capped at w_height, check there aren't
1955 // actually more lines.
1956 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1957 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001958 {
1959 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001960 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001961 return;
1962 }
1963
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001964 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001965 int overlap = sms_marker_overlap(curwin,
1966 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001967 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001968 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001969 {
1970 // scroll a screen line down
1971 if (curwin->w_skipcol >= width1 + width2)
1972 curwin->w_skipcol -= width2;
1973 else
1974 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001975 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001976 }
1977 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001978 {
1979 validate_virtcol();
1980 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001981 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001982 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001983
1984 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1985 int row = 0;
1986 if (col >= width1)
1987 {
1988 col -= width1;
1989 ++row;
1990 }
1991 if (col > width2)
1992 {
1993 row += col / width2;
1994 col = col % width2;
1995 }
1996 if (row >= curwin->w_height)
1997 {
1998 if (curwin->w_skipcol == 0)
1999 {
2000 curwin->w_skipcol += width1;
2001 --row;
2002 }
2003 if (row >= curwin->w_height)
2004 curwin->w_skipcol += (row - curwin->w_height) * width2;
2005 redraw_later(UPD_NOT_VALID);
2006 }
2007}
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#ifdef FEAT_DIFF
2010/*
2011 * Don't end up with too many filler lines in the window.
2012 */
2013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002014check_topfill(
2015 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002016 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 int n;
2019
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 if (wp->w_topfill <= 0)
2021 return;
2022
2023 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2024 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002026 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002028 --wp->w_topline;
2029 wp->w_topfill = 0;
2030 }
2031 else
2032 {
2033 wp->w_topfill = wp->w_height - n;
2034 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037 }
2038}
2039
2040/*
2041 * Use as many filler lines as possible for w_topline. Make sure w_topline
2042 * is still visible.
2043 */
2044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 int n;
2048
2049 n = plines_nofill(curwin->w_topline);
2050 if (n >= curwin->w_height)
2051 curwin->w_topfill = 0;
2052 else
2053 {
2054 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2055 if (curwin->w_topfill + n > curwin->w_height)
2056 curwin->w_topfill = curwin->w_height - n;
2057 }
2058}
2059#endif
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061/*
2062 * Scroll the screen one line down, but don't do it if it would move the
2063 * cursor off the screen.
2064 */
2065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002066scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 int end_row;
2069#ifdef FEAT_DIFF
2070 int can_fill = (curwin->w_topfill
2071 < diff_check_fill(curwin, curwin->w_topline));
2072#endif
2073
2074 if (curwin->w_topline <= 1
2075#ifdef FEAT_DIFF
2076 && !can_fill
2077#endif
2078 )
2079 return;
2080
Bram Moolenaar85a20022019-12-21 18:25:54 +01002081 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
2084 * Compute the row number of the last row of the cursor line
2085 * and make sure it doesn't go off the screen. Make sure the cursor
2086 * doesn't go past 'scrolloff' lines from the screen end.
2087 */
2088 end_row = curwin->w_wrow;
2089#ifdef FEAT_DIFF
2090 if (can_fill)
2091 ++end_row;
2092 else
2093 end_row += plines_nofill(curwin->w_topline - 1);
2094#else
2095 end_row += plines(curwin->w_topline - 1);
2096#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002097 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 validate_cheight();
2100 validate_virtcol();
2101 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002102 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002104 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106#ifdef FEAT_DIFF
2107 if (can_fill)
2108 {
2109 ++curwin->w_topfill;
2110 check_topfill(curwin, TRUE);
2111 }
2112 else
2113 {
2114 --curwin->w_topline;
2115 curwin->w_topfill = 0;
2116 }
2117#else
2118 --curwin->w_topline;
2119#endif
2120#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002121 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002123 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2125 }
2126}
2127
2128/*
2129 * Scroll the screen one line up, but don't do it if it would move the cursor
2130 * off the screen.
2131 */
2132 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 int start_row;
2136
2137 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2138#ifdef FEAT_DIFF
2139 && curwin->w_topfill == 0
2140#endif
2141 )
2142 return;
2143
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 /*
2147 * Compute the row number of the first row of the cursor line
2148 * and make sure it doesn't go off the screen. Make sure the cursor
2149 * doesn't go before 'scrolloff' lines from the screen start.
2150 */
2151#ifdef FEAT_DIFF
2152 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2153 - curwin->w_topfill;
2154#else
2155 start_row = curwin->w_wrow - plines(curwin->w_topline);
2156#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002157 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002160 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002162 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164#ifdef FEAT_DIFF
2165 if (curwin->w_topfill > 0)
2166 --curwin->w_topfill;
2167 else
2168#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002169 {
2170#ifdef FEAT_FOLDING
2171 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002174 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002175 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2177 }
2178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180/*
2181 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2182 * a (wrapped) text line. Uses and sets "lp->fill".
2183 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002184 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
2186 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002187topline_back_winheight(
2188 lineoff_T *lp,
2189 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191#ifdef FEAT_DIFF
2192 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2193 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 ++lp->fill;
2196 lp->height = 1;
2197 }
2198 else
2199#endif
2200 {
2201 --lp->lnum;
2202#ifdef FEAT_DIFF
2203 lp->fill = 0;
2204#endif
2205 if (lp->lnum < 1)
2206 lp->height = MAXCOL;
2207 else
2208#ifdef FEAT_FOLDING
2209 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 lp->height = 1;
2212 else
2213#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002214 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216}
2217
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002218 static void
2219topline_back(lineoff_T *lp)
2220{
2221 topline_back_winheight(lp, TRUE);
2222}
2223
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225/*
2226 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2227 * a (wrapped) text line. Uses and sets "lp->fill".
2228 * Returns the height of the added line in "lp->height".
2229 * Lines below the last one are incredibly high.
2230 */
2231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234#ifdef FEAT_DIFF
2235 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ++lp->fill;
2239 lp->height = 1;
2240 }
2241 else
2242#endif
2243 {
2244 ++lp->lnum;
2245#ifdef FEAT_DIFF
2246 lp->fill = 0;
2247#endif
2248 if (lp->lnum > curbuf->b_ml.ml_line_count)
2249 lp->height = MAXCOL;
2250 else
2251#ifdef FEAT_FOLDING
2252 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 lp->height = 1;
2255 else
2256#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002257 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259}
2260
2261#ifdef FEAT_DIFF
2262/*
2263 * Switch from including filler lines below lp->lnum to including filler
2264 * lines above loff.lnum + 1. This keeps pointing to the same line.
2265 * When there are no filler lines nothing changes.
2266 */
2267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 if (lp->fill > 0)
2271 {
2272 ++lp->lnum;
2273 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2274 }
2275}
2276
2277/*
2278 * Switch from including filler lines above lp->lnum to including filler
2279 * lines below loff.lnum - 1. This keeps pointing to the same line.
2280 * When there are no filler lines nothing changes.
2281 */
2282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (lp->fill > 0)
2286 {
2287 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2288 --lp->lnum;
2289 }
2290}
2291#endif
2292
2293/*
2294 * Recompute topline to put the cursor at the top of the window.
2295 * Scroll at least "min_scroll" lines.
2296 * If "always" is TRUE, always set topline (for "zt").
2297 */
2298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002299scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 int scrolled = 0;
2302 int extra = 0;
2303 int used;
2304 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 linenr_T top; // just above displayed lines
2306 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002308 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#ifdef FEAT_DIFF
2310 linenr_T old_topfill = curwin->w_topfill;
2311#endif
2312 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002313 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (mouse_dragging > 0)
2316 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /*
2319 * Decrease topline until:
2320 * - it has become 1
2321 * - (part of) the cursor line is moved off the screen or
2322 * - moved at least 'scrolljump' lines and
2323 * - at least 'scrolloff' lines above and below the cursor
2324 */
2325 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002326 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (curwin->w_cursor.lnum < curwin->w_topline)
2328 scrolled = used;
2329
2330#ifdef FEAT_FOLDING
2331 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2332 {
2333 --top;
2334 ++bot;
2335 }
2336 else
2337#endif
2338 {
2339 top = curwin->w_cursor.lnum - 1;
2340 bot = curwin->w_cursor.lnum + 1;
2341 }
2342 new_topline = top + 1;
2343
2344#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002345 // "used" already contains the number of filler lines above, don't add it
2346 // again.
2347 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002348 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#endif
2350
2351 /*
2352 * Check if the lines from "top" to "bot" fit in the window. If they do,
2353 * set new_topline and advance "top" and "bot" to include more lines.
2354 */
2355 while (top > 0)
2356 {
2357#ifdef FEAT_FOLDING
2358 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 i = 1;
2361 else
2362#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002363 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002364 if (top < curwin->w_topline)
2365 scrolled += i;
2366
2367 // If scrolling is needed, scroll at least 'sj' lines.
2368 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2369 && extra >= off)
2370 break;
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 used += i;
2373 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2374 {
2375#ifdef FEAT_FOLDING
2376 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 ++used;
2379 else
2380#endif
2381 used += plines(bot);
2382 }
2383 if (used > curwin->w_height)
2384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 extra += i;
2387 new_topline = top;
2388 --top;
2389 ++bot;
2390 }
2391
2392 /*
2393 * If we don't have enough space, put cursor in the middle.
2394 * This makes sure we get the same position when using "k" and "j"
2395 * in a small window.
2396 */
2397 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002398 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 else
2400 {
2401 /*
2402 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002403 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
2405 if (new_topline < curwin->w_topline || always)
2406 curwin->w_topline = new_topline;
2407 if (curwin->w_topline > curwin->w_cursor.lnum)
2408 curwin->w_topline = curwin->w_cursor.lnum;
2409#ifdef FEAT_DIFF
2410 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2411 if (curwin->w_topfill > 0 && extra > off)
2412 {
2413 curwin->w_topfill -= extra - off;
2414 if (curwin->w_topfill < 0)
2415 curwin->w_topfill = 0;
2416 }
2417 check_topfill(curwin, FALSE);
2418#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002419 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002420 if (curwin->w_topline == curwin->w_cursor.lnum
2421 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002422 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002424 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#ifdef FEAT_DIFF
2426 || curwin->w_topfill != old_topfill
2427#endif
2428 )
2429 curwin->w_valid &=
2430 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2431 curwin->w_valid |= VALID_TOPLINE;
2432 }
2433}
2434
2435/*
2436 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2437 * screen lines for text lines.
2438 */
2439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002440set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442#ifdef FEAT_DIFF
2443 wp->w_filler_rows = 0;
2444#endif
2445 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002446 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 else
2448 {
2449 wp->w_empty_rows = wp->w_height - used;
2450#ifdef FEAT_DIFF
2451 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2452 {
2453 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2454 if (wp->w_empty_rows > wp->w_filler_rows)
2455 wp->w_empty_rows -= wp->w_filler_rows;
2456 else
2457 {
2458 wp->w_filler_rows = wp->w_empty_rows;
2459 wp->w_empty_rows = 0;
2460 }
2461 }
2462#endif
2463 }
2464}
2465
2466/*
2467 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002468 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2470 * This is messy stuff!!!
2471 */
2472 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002473scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 int used;
2476 int scrolled = 0;
2477 int extra = 0;
2478 int i;
2479 linenr_T line_count;
2480 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002481 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 lineoff_T loff;
2483 lineoff_T boff;
2484#ifdef FEAT_DIFF
2485 int old_topfill = curwin->w_topfill;
2486 int fill_below_window;
2487#endif
2488 linenr_T old_botline = curwin->w_botline;
2489 linenr_T old_valid = curwin->w_valid;
2490 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002491 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002492 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002493 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 cln = curwin->w_cursor.lnum;
2496 if (set_topbot)
2497 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002498 int set_skipcol = FALSE;
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 used = 0;
2501 curwin->w_botline = cln + 1;
2502#ifdef FEAT_DIFF
2503 loff.fill = 0;
2504#endif
2505 for (curwin->w_topline = curwin->w_botline;
2506 curwin->w_topline > 1;
2507 curwin->w_topline = loff.lnum)
2508 {
2509 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002510 topline_back_winheight(&loff, FALSE);
2511 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002513 if (used + loff.height > curwin->w_height)
2514 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002515 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002516 {
2517 // 'smoothscroll' and 'wrap' are set. The above line is
2518 // too long to show in its entirety, so we show just a part
2519 // of it.
2520 if (used < curwin->w_height)
2521 {
2522 int plines_offset = used + loff.height
2523 - curwin->w_height;
2524 used = curwin->w_height;
2525#ifdef FEAT_DIFF
2526 curwin->w_topfill = loff.fill;
2527#endif
2528 curwin->w_topline = loff.lnum;
2529 curwin->w_skipcol = skipcol_from_plines(
2530 curwin, plines_offset);
2531 set_skipcol = TRUE;
2532 }
2533 }
2534 break;
2535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 used += loff.height;
2537#ifdef FEAT_DIFF
2538 curwin->w_topfill = loff.fill;
2539#endif
2540 }
2541 set_empty_rows(curwin, used);
2542 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2543 if (curwin->w_topline != old_topline
2544#ifdef FEAT_DIFF
2545 || curwin->w_topfill != old_topfill
2546#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002547 || set_skipcol
2548 || curwin->w_skipcol != 0)
2549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002551 if (set_skipcol)
2552 redraw_later(UPD_NOT_VALID);
2553 else
2554 reset_skipcol();
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557 else
2558 validate_botline();
2559
Bram Moolenaar85a20022019-12-21 18:25:54 +01002560 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#ifdef FEAT_DIFF
2562 used = plines_nofill(cln);
2563#else
2564 validate_cheight();
2565 used = curwin->w_cline_height;
2566#endif
2567
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002568 // If the cursor is on or below botline, we will at least scroll by the
2569 // height of the cursor line, which is "used". Correct for empty lines,
2570 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (cln >= curwin->w_botline)
2572 {
2573 scrolled = used;
2574 if (cln == curwin->w_botline)
2575 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002576 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002577 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002578 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002579 // Calculate how many screen lines the current top line of window
2580 // occupies. If it is occupying more than the entire window, we
2581 // need to scroll the additional clipped lines to scroll past the
2582 // top line before we can move on to the other lines.
2583 int top_plines =
2584#ifdef FEAT_DIFF
2585 plines_win_nofill
2586#else
2587 plines_win
2588#endif
2589 (curwin, curwin->w_topline, FALSE);
2590 int skip_lines = 0;
2591 int width1 = curwin->w_width - curwin_col_off();
2592 int width2 = width1 + curwin_col_off2();
2593 // similar formula is used in curs_columns()
2594 if (curwin->w_skipcol > width1)
2595 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2596 else if (curwin->w_skipcol > 0)
2597 skip_lines = 1;
2598
2599 top_plines -= skip_lines;
2600 if (top_plines > curwin->w_height)
2601 {
2602 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002603 }
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606
2607 /*
2608 * Stop counting lines to scroll when
2609 * - hitting start of the file
2610 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002611 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 * - lines between botline and cursor have been counted
2613 */
2614#ifdef FEAT_FOLDING
2615 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2616#endif
2617 {
2618 loff.lnum = cln;
2619 boff.lnum = cln;
2620 }
2621#ifdef FEAT_DIFF
2622 loff.fill = 0;
2623 boff.fill = 0;
2624 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2625 - curwin->w_filler_rows;
2626#endif
2627
2628 while (loff.lnum > 1)
2629 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002630 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2631 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002633 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2635 && loff.lnum <= curwin->w_botline
2636#ifdef FEAT_DIFF
2637 && (loff.lnum < curwin->w_botline
2638 || loff.fill >= fill_below_window)
2639#endif
2640 )
2641 break;
2642
Bram Moolenaar85a20022019-12-21 18:25:54 +01002643 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002645 if (loff.height == MAXCOL)
2646 used = MAXCOL;
2647 else
2648 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (used > curwin->w_height)
2650 break;
2651 if (loff.lnum >= curwin->w_botline
2652#ifdef FEAT_DIFF
2653 && (loff.lnum > curwin->w_botline
2654 || loff.fill <= fill_below_window)
2655#endif
2656 )
2657 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002658 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 scrolled += loff.height;
2660 if (loff.lnum == curwin->w_botline
2661#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002662 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#endif
2664 )
2665 scrolled -= curwin->w_empty_rows;
2666 }
2667
2668 if (boff.lnum < curbuf->b_ml.ml_line_count)
2669 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002670 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 botline_forw(&boff);
2672 used += boff.height;
2673 if (used > curwin->w_height)
2674 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002675 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2676 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 extra += boff.height;
2679 if (boff.lnum >= curwin->w_botline
2680#ifdef FEAT_DIFF
2681 || (boff.lnum + 1 == curwin->w_botline
2682 && boff.fill > curwin->w_filler_rows)
2683#endif
2684 )
2685 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 scrolled += boff.height;
2688 if (boff.lnum == curwin->w_botline
2689#ifdef FEAT_DIFF
2690 && boff.fill == 0
2691#endif
2692 )
2693 scrolled -= curwin->w_empty_rows;
2694 }
2695 }
2696 }
2697 }
2698
Bram Moolenaar85a20022019-12-21 18:25:54 +01002699 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (scrolled <= 0)
2701 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 else if (used > curwin->w_height)
2704 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 else
2707 {
2708 line_count = 0;
2709#ifdef FEAT_DIFF
2710 boff.fill = curwin->w_topfill;
2711#endif
2712 boff.lnum = curwin->w_topline - 1;
2713 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2714 {
2715 botline_forw(&boff);
2716 i += boff.height;
2717 ++line_count;
2718 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002719 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 line_count = 9999;
2721 }
2722
2723 /*
2724 * Scroll up if the cursor is off the bottom of the screen a bit.
2725 * Otherwise put it at 1/2 of the screen.
2726 */
2727 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002728 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002729 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002730 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002731 if (do_sms)
2732 scrollup(scrolled, TRUE); // TODO
2733 else
2734 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 /*
2738 * If topline didn't change we need to restore w_botline and w_empty_rows
2739 * (we changed them).
2740 * If topline did change, update_screen() will set botline.
2741 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002742 if (curwin->w_topline == old_topline
2743 && curwin->w_skipcol == old_skipcol
2744 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 curwin->w_botline = old_botline;
2747 curwin->w_empty_rows = old_empty_rows;
2748 curwin->w_valid = old_valid;
2749 }
2750 curwin->w_valid |= VALID_TOPLINE;
2751}
2752
2753/*
2754 * Recompute topline to put the cursor halfway the window
2755 * If "atend" is TRUE, also put it halfway at the end of the file.
2756 */
2757 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002758scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
2760 int above = 0;
2761 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002762 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#ifdef FEAT_DIFF
2764 int topfill = 0;
2765#endif
2766 int below = 0;
2767 int used;
2768 lineoff_T loff;
2769 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002770#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002771 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002774#ifdef FEAT_PROP_POPUP
2775 // if the width changed this needs to be updated first
2776 may_update_popup_position();
2777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2779#ifdef FEAT_FOLDING
2780 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2781#endif
2782#ifdef FEAT_DIFF
2783 used = plines_nofill(loff.lnum);
2784 loff.fill = 0;
2785 boff.fill = 0;
2786#else
2787 used = plines(loff.lnum);
2788#endif
2789 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002790
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002791 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002792 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2793 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002794 {
2795 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002796 if (atend)
2797 {
2798 want_height = (curwin->w_height - used) / 2;
2799 used = 0;
2800 }
2801 else
2802 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002803 }
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 while (topline > 1)
2806 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002807 // If using smoothscroll, we can precisely scroll to the
2808 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002809 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002810 {
2811 topline_back_winheight(&loff, FALSE);
2812 if (loff.height == MAXCOL)
2813 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002814 used += loff.height;
2815 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002816 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002817 botline_forw(&boff);
2818 used += boff.height;
2819 }
2820 if (used > want_height)
2821 {
2822 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002823 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824 topline = loff.lnum;
2825#ifdef FEAT_DIFF
2826 topfill = loff.fill;
2827#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002828 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002829 }
2830 break;
2831 }
2832 topline = loff.lnum;
2833#ifdef FEAT_DIFF
2834 topfill = loff.fill;
2835#endif
2836 continue;
2837 }
2838
2839 // If not using smoothscroll, we have to iteratively find how many
2840 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002841 // This may not be right in the middle if the lines'
2842 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002843
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002844 // Depending on "prefer_above" we add a line above or below first.
2845 // Loop twice to avoid duplicating code.
2846 int done = FALSE;
2847 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002849 if (prefer_above ? (round == 2 && below < above)
2850 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002852 // add a line below the cursor
2853 if (boff.lnum < curbuf->b_ml.ml_line_count)
2854 {
2855 botline_forw(&boff);
2856 used += boff.height;
2857 if (used > curwin->w_height)
2858 {
2859 done = TRUE;
2860 break;
2861 }
2862 below += boff.height;
2863 }
2864 else
2865 {
2866 ++below; // count a "~" line
2867 if (atend)
2868 ++used;
2869 }
2870 }
2871
2872 if (prefer_above ? (round == 1 && below >= above)
2873 : (round == 1 && below > above))
2874 {
2875 // add a line above the cursor
2876 topline_back(&loff);
2877 if (loff.height == MAXCOL)
2878 used = MAXCOL;
2879 else
2880 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002882 {
2883 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002885 }
2886 above += loff.height;
2887 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002889 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002893 if (done)
2894 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#ifdef FEAT_FOLDING
2898 if (!hasFolding(topline, &curwin->w_topline, NULL))
2899#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002900 {
2901 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002902 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002903 || curwin->w_skipcol != 0)
2904 {
2905 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002906 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002907 {
2908 curwin->w_skipcol = skipcol;
2909 redraw_later(UPD_NOT_VALID);
2910 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002911 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002912 reset_skipcol();
2913 }
2914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#ifdef FEAT_DIFF
2916 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002917 if (old_topline > curwin->w_topline + curwin->w_height)
2918 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 check_topfill(curwin, FALSE);
2920#endif
2921 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2922 curwin->w_valid |= VALID_TOPLINE;
2923}
2924
2925/*
2926 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002927 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 * If not possible, put it at the same position as scroll_cursor_halfway().
2929 * When called topline must be valid!
2930 */
2931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002936 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 linenr_T botline;
2938 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002939 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002941 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 /*
2944 * How many lines we would like to have above/below the cursor depends on
2945 * whether the first/last line of the file is on screen.
2946 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002947 above_wanted = so;
2948 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002949 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 above_wanted = mouse_dragging - 1;
2952 below_wanted = mouse_dragging - 1;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (curwin->w_topline == 1)
2955 {
2956 above_wanted = 0;
2957 max_off = curwin->w_height / 2;
2958 if (below_wanted > max_off)
2959 below_wanted = max_off;
2960 }
2961 validate_botline();
2962 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002963 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 below_wanted = 0;
2966 max_off = (curwin->w_height - 1) / 2;
2967 if (above_wanted > max_off)
2968 above_wanted = max_off;
2969 }
2970
2971 /*
2972 * If there are sufficient file-lines above and below the cursor, we can
2973 * return now.
2974 */
2975 cln = curwin->w_cursor.lnum;
2976 if (cln >= curwin->w_topline + above_wanted
2977 && cln < curwin->w_botline - below_wanted
2978#ifdef FEAT_FOLDING
2979 && !hasAnyFolding(curwin)
2980#endif
2981 )
2982 return;
2983
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002984 if (curwin->w_p_sms && !curwin->w_p_wrap)
2985 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002986 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002987 if (curwin->w_cline_height == curwin->w_height)
2988 {
2989 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002990 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002991 return;
2992 }
2993 // TODO: If the cursor line doesn't fit in the window then only adjust
2994 // w_skipcol.
2995 }
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 /*
2998 * Narrow down the area where the cursor can be put by taking lines from
2999 * the top and the bottom until:
3000 * - the desired context lines are found
3001 * - the lines from the top is past the lines from the bottom
3002 */
3003 topline = curwin->w_topline;
3004 botline = curwin->w_botline - 1;
3005#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003006 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 above = curwin->w_topfill;
3008 below = curwin->w_filler_rows;
3009#endif
3010 while ((above < above_wanted || below < below_wanted) && topline < botline)
3011 {
3012 if (below < below_wanted && (below <= above || above >= above_wanted))
3013 {
3014#ifdef FEAT_FOLDING
3015 if (hasFolding(botline, &botline, NULL))
3016 ++below;
3017 else
3018#endif
3019 below += plines(botline);
3020 --botline;
3021 }
3022 if (above < above_wanted && (above < below || below >= below_wanted))
3023 {
3024#ifdef FEAT_FOLDING
3025 if (hasFolding(topline, NULL, &topline))
3026 ++above;
3027 else
3028#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003029 above += PLINES_NOFILL(topline);
3030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003031 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (topline < botline)
3033 above += diff_check_fill(curwin, topline + 1);
3034#endif
3035 ++topline;
3036 }
3037 }
3038 if (topline == botline || botline == 0)
3039 curwin->w_cursor.lnum = topline;
3040 else if (topline > botline)
3041 curwin->w_cursor.lnum = botline;
3042 else
3043 {
3044 if (cln < topline && curwin->w_topline > 1)
3045 {
3046 curwin->w_cursor.lnum = topline;
3047 curwin->w_valid &=
3048 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3049 }
3050 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3051 {
3052 curwin->w_cursor.lnum = botline;
3053 curwin->w_valid &=
3054 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3055 }
3056 }
3057 curwin->w_valid |= VALID_TOPLINE;
3058}
3059
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003060static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003063 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3064 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003066 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
3068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 long n;
3072 int retval = OK;
3073 lineoff_T loff;
3074 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003075 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaar85a20022019-12-21 18:25:54 +01003077 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
3079 beep_flush();
3080 return FAIL;
3081 }
3082
3083 for ( ; count > 0; --count)
3084 {
3085 validate_botline();
3086 /*
3087 * It's an error to move a page up when the first line is already on
3088 * the screen. It's an error to move a page down when the last line
3089 * is on the screen and the topline is 'scrolloff' lines from the
3090 * last line.
3091 */
3092 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003093 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3095 : (curwin->w_topline == 1
3096#ifdef FEAT_DIFF
3097 && curwin->w_topfill ==
3098 diff_check_fill(curwin, curwin->w_topline)
3099#endif
3100 ))
3101 {
3102 beep_flush();
3103 retval = FAIL;
3104 break;
3105 }
3106
3107#ifdef FEAT_DIFF
3108 loff.fill = 0;
3109#endif
3110 if (dir == FORWARD)
3111 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003112 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003114 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003115 if (p_window <= 2)
3116 ++curwin->w_topline;
3117 else
3118 curwin->w_topline += p_window - 2;
3119 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3120 curwin->w_topline = curbuf->b_ml.ml_line_count;
3121 curwin->w_cursor.lnum = curwin->w_topline;
3122 }
3123 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3124 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 curwin->w_topline = curbuf->b_ml.ml_line_count;
3127#ifdef FEAT_DIFF
3128 curwin->w_topfill = 0;
3129#endif
3130 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3131 }
3132 else
3133 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003134 // For the overlap, start with the line just below the window
3135 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 loff.lnum = curwin->w_botline;
3137#ifdef FEAT_DIFF
3138 loff.fill = diff_check_fill(curwin, loff.lnum)
3139 - curwin->w_filler_rows;
3140#endif
3141 get_scroll_overlap(&loff, -1);
3142 curwin->w_topline = loff.lnum;
3143#ifdef FEAT_DIFF
3144 curwin->w_topfill = loff.fill;
3145 check_topfill(curwin, FALSE);
3146#endif
3147 curwin->w_cursor.lnum = curwin->w_topline;
3148 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3149 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3150 }
3151 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003152 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154#ifdef FEAT_DIFF
3155 if (curwin->w_topline == 1)
3156 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003157 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 max_topfill();
3159 continue;
3160 }
3161#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003162 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003165 if (p_window <= 2)
3166 --curwin->w_topline;
3167 else
3168 curwin->w_topline -= p_window - 2;
3169 if (curwin->w_topline < 1)
3170 curwin->w_topline = 1;
3171 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3172 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3173 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3174 continue;
3175 }
3176
Bram Moolenaar85a20022019-12-21 18:25:54 +01003177 // Find the line at the top of the window that is going to be the
3178 // line at the bottom of the window. Make sure this results in
3179 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 loff.lnum = curwin->w_topline - 1;
3181#ifdef FEAT_DIFF
3182 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3183 - curwin->w_topfill;
3184#endif
3185 get_scroll_overlap(&loff, 1);
3186
3187 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3188 {
3189 loff.lnum = curbuf->b_ml.ml_line_count;
3190#ifdef FEAT_DIFF
3191 loff.fill = 0;
3192 }
3193 else
3194 {
3195 botline_topline(&loff);
3196#endif
3197 }
3198 curwin->w_cursor.lnum = loff.lnum;
3199
Bram Moolenaar85a20022019-12-21 18:25:54 +01003200 // Find the line just above the new topline to get the right line
3201 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 n = 0;
3203 while (n <= curwin->w_height && loff.lnum >= 1)
3204 {
3205 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003206 if (loff.height == MAXCOL)
3207 n = MAXCOL;
3208 else
3209 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003211 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
3213 curwin->w_topline = 1;
3214#ifdef FEAT_DIFF
3215 max_topfill();
3216#endif
3217 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3218 }
3219 else
3220 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003221 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222#ifdef FEAT_DIFF
3223 topline_botline(&loff);
3224#endif
3225 botline_forw(&loff);
3226 botline_forw(&loff);
3227#ifdef FEAT_DIFF
3228 botline_topline(&loff);
3229#endif
3230#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3233#endif
3234
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 // Always scroll at least one line. Avoid getting stuck on
3236 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (loff.lnum >= curwin->w_topline
3238#ifdef FEAT_DIFF
3239 && (loff.lnum > curwin->w_topline
3240 || loff.fill >= curwin->w_topfill)
3241#endif
3242 )
3243 {
3244#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003245 // First try using the maximum number of filler lines. If
3246 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 loff.fill = curwin->w_topfill;
3248 if (curwin->w_topfill < diff_check_fill(curwin,
3249 curwin->w_topline))
3250 max_topfill();
3251 if (curwin->w_topfill == loff.fill)
3252#endif
3253 {
3254 --curwin->w_topline;
3255#ifdef FEAT_DIFF
3256 curwin->w_topfill = 0;
3257#endif
3258 }
3259 comp_botline(curwin);
3260 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003261 curwin->w_valid &=
3262 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 else
3265 {
3266 curwin->w_topline = loff.lnum;
3267#ifdef FEAT_DIFF
3268 curwin->w_topfill = loff.fill;
3269 check_topfill(curwin, FALSE);
3270#endif
3271 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3272 }
3273 }
3274 }
3275 }
3276#ifdef FEAT_FOLDING
3277 foldAdjustCursor();
3278#endif
3279 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003280 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003281 if (retval == OK)
3282 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3284
Bram Moolenaar907dad72018-07-10 15:07:15 +02003285 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003287 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3288 // But make sure we scroll at least one line (happens with mix of long
3289 // wrapping lines and non-wrapping line).
3290 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003292 scroll_cursor_top(1, FALSE);
3293 if (curwin->w_topline <= old_topline
3294 && old_topline < curbuf->b_ml.ml_line_count)
3295 {
3296 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003298 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3299#endif
3300 }
3301 }
3302#ifdef FEAT_FOLDING
3303 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003308 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 return retval;
3310}
3311
3312/*
3313 * Decide how much overlap to use for page-up or page-down scrolling.
3314 * This is symmetric, so that doing both keeps the same lines displayed.
3315 * Three lines are examined:
3316 *
3317 * before CTRL-F after CTRL-F / before CTRL-B
3318 * etc. l1
3319 * l1 last but one line ------------
3320 * l2 last text line l2 top text line
3321 * ------------- l3 second text line
3322 * l3 etc.
3323 */
3324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003325get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 int h1, h2, h3, h4;
3328 int min_height = curwin->w_height - 2;
3329 lineoff_T loff0, loff1, loff2;
3330
3331#ifdef FEAT_DIFF
3332 if (lp->fill > 0)
3333 lp->height = 1;
3334 else
3335 lp->height = plines_nofill(lp->lnum);
3336#else
3337 lp->height = plines(lp->lnum);
3338#endif
3339 h1 = lp->height;
3340 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343 loff0 = *lp;
3344 if (dir > 0)
3345 botline_forw(lp);
3346 else
3347 topline_back(lp);
3348 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003349 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003351 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 return;
3353 }
3354
3355 loff1 = *lp;
3356 if (dir > 0)
3357 botline_forw(lp);
3358 else
3359 topline_back(lp);
3360 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003361 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return;
3365 }
3366
3367 loff2 = *lp;
3368 if (dir > 0)
3369 botline_forw(lp);
3370 else
3371 topline_back(lp);
3372 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003373 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003374 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003376 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377}
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * Scroll 'scroll' lines up or down.
3381 */
3382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003383halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 long scrolled = 0;
3386 int i;
3387 int n;
3388 int room;
3389
3390 if (Prenum)
3391 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3392 curwin->w_height : Prenum;
3393 n = (curwin->w_p_scr <= curwin->w_height) ?
3394 curwin->w_p_scr : curwin->w_height;
3395
Bram Moolenaard5d37532017-03-27 23:02:07 +02003396 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 validate_botline();
3398 room = curwin->w_empty_rows;
3399#ifdef FEAT_DIFF
3400 room += curwin->w_filler_rows;
3401#endif
3402 if (flag)
3403 {
3404 /*
3405 * scroll the text up
3406 */
3407 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3408 {
3409#ifdef FEAT_DIFF
3410 if (curwin->w_topfill > 0)
3411 {
3412 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003413 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 --curwin->w_topfill;
3415 }
3416 else
3417#endif
3418 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003419 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 n -= i;
3421 if (n < 0 && scrolled > 0)
3422 break;
3423#ifdef FEAT_FOLDING
3424 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3425#endif
3426 ++curwin->w_topline;
3427#ifdef FEAT_DIFF
3428 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3432 {
3433 ++curwin->w_cursor.lnum;
3434 curwin->w_valid &=
3435 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3439 scrolled += i;
3440
3441 /*
3442 * Correct w_botline for changed w_topline.
3443 * Won't work when there are filler lines.
3444 */
3445#ifdef FEAT_DIFF
3446 if (curwin->w_p_diff)
3447 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3448 else
3449#endif
3450 {
3451 room += i;
3452 do
3453 {
3454 i = plines(curwin->w_botline);
3455 if (i > room)
3456 break;
3457#ifdef FEAT_FOLDING
3458 (void)hasFolding(curwin->w_botline, NULL,
3459 &curwin->w_botline);
3460#endif
3461 ++curwin->w_botline;
3462 room -= i;
3463 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3464 }
3465 }
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 /*
3468 * When hit bottom of the file: move cursor down.
3469 */
3470 if (n > 0)
3471 {
3472# ifdef FEAT_FOLDING
3473 if (hasAnyFolding(curwin))
3474 {
3475 while (--n >= 0
3476 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3477 {
3478 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3479 &curwin->w_cursor.lnum);
3480 ++curwin->w_cursor.lnum;
3481 }
3482 }
3483 else
3484# endif
3485 curwin->w_cursor.lnum += n;
3486 check_cursor_lnum();
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 else
3490 {
3491 /*
3492 * scroll the text down
3493 */
3494 while (n > 0 && curwin->w_topline > 1)
3495 {
3496#ifdef FEAT_DIFF
3497 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3498 {
3499 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003500 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 ++curwin->w_topfill;
3502 }
3503 else
3504#endif
3505 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003506 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 n -= i;
3508 if (n < 0 && scrolled > 0)
3509 break;
3510 --curwin->w_topline;
3511#ifdef FEAT_FOLDING
3512 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3513#endif
3514#ifdef FEAT_DIFF
3515 curwin->w_topfill = 0;
3516#endif
3517 }
3518 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3519 VALID_BOTLINE|VALID_BOTLINE_AP);
3520 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (curwin->w_cursor.lnum > 1)
3522 {
3523 --curwin->w_cursor.lnum;
3524 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /*
3529 * When hit top of the file: move cursor up.
3530 */
3531 if (n > 0)
3532 {
3533 if (curwin->w_cursor.lnum <= (linenr_T)n)
3534 curwin->w_cursor.lnum = 1;
3535 else
3536# ifdef FEAT_FOLDING
3537 if (hasAnyFolding(curwin))
3538 {
3539 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3540 {
3541 --curwin->w_cursor.lnum;
3542 (void)hasFolding(curwin->w_cursor.lnum,
3543 &curwin->w_cursor.lnum, NULL);
3544 }
3545 }
3546 else
3547# endif
3548 curwin->w_cursor.lnum -= n;
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003552 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 foldAdjustCursor();
3554# endif
3555#ifdef FEAT_DIFF
3556 check_topfill(curwin, !flag);
3557#endif
3558 cursor_correct();
3559 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003560 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003562
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003564do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565{
3566 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003567 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003568 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003569 colnr_T curswant = curwin->w_curswant;
3570 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 win_T *old_curwin = curwin;
3572 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 int old_VIsual_select = VIsual_select;
3574 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575
3576 /*
3577 * loop through the cursorbound windows
3578 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003580 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 {
3582 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003583 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003584 if (curwin != old_curwin && curwin->w_p_crb)
3585 {
3586# ifdef FEAT_DIFF
3587 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003588 curwin->w_cursor.lnum =
3589 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 else
3591# endif
3592 curwin->w_cursor.lnum = line;
3593 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003594 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003595 curwin->w_curswant = curswant;
3596 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597
Bram Moolenaar85a20022019-12-21 18:25:54 +01003598 // Make sure the cursor is in a valid position. Temporarily set
3599 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003600 int restart_edit_save = restart_edit;
3601 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003603
3604 // Avoid a scroll here for the cursor position, 'scrollbind' is
3605 // more important.
3606 if (!curwin->w_p_scb)
3607 validate_cursor();
3608
Bram Moolenaar61452852011-02-01 18:01:11 +01003609 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003610 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003611 if (has_mbyte)
3612 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003613 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003614
Bram Moolenaar85a20022019-12-21 18:25:54 +01003615 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003616 if (!curwin->w_p_scb)
3617 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003618 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 }
3620 }
3621
3622 /*
3623 * reset current-window
3624 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625 VIsual_select = old_VIsual_select;
3626 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627 curwin = old_curwin;
3628 curbuf = old_curbuf;
3629}