blob: 25f419e5aae320c9e8efe95bb8c4cde118fffc59 [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
zeertzjq3f1b5312024-02-06 10:43:36 +010022static void redraw_for_cursorline(win_T *wp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static int scrolljump_value(void);
24static int check_top_offset(void);
25static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27typedef struct
28{
Bram Moolenaar85a20022019-12-21 18:25:54 +010029 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010031 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010033 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000034} lineoff_T;
35
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void topline_back(lineoff_T *lp);
37static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
zeertzjq6235a102023-08-19 14:12:42 +020040 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010041 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010042 int
zeertzjq6235a102023-08-19 14:12:42 +020043adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010044{
45 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020046 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047
Bram Moolenaarf6196f42022-10-02 21:29:55 +010048 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010049 int w2 = width + win_col_off2(wp);
50 if (wp->w_skipcol >= width && w2 > 0)
51 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020052
53 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054}
55
56/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010057 * Return how many lines "lnum" will take on the screen, taking into account
58 * whether it is the first line, whether w_skipcol is non-zero and limiting to
59 * the window height.
60 */
61 static int
62plines_correct_topline(win_T *wp, linenr_T lnum)
63{
64 int n;
65#ifdef FEAT_DIFF
66 if (lnum == wp->w_topline)
67 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
68 else
69#endif
70 n = plines_win(wp, lnum, FALSE);
71 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020072 n -= adjust_plines_for_skipcol(wp);
Bram Moolenaard5337ef2022-10-20 20:15:47 +010073 if (n > wp->w_height)
74 n = wp->w_height;
75 return n;
76}
77
78/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Compute wp->w_botline for the current wp->w_topline. Can be called after
80 * wp->w_topline changed.
81 */
82 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010083comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int n;
86 linenr_T lnum;
87 int done;
88#ifdef FEAT_FOLDING
89 linenr_T last;
90 int folded;
91#endif
92
93 /*
94 * If w_cline_row is valid, start there.
95 * Otherwise have to start at w_topline.
96 */
97 check_cursor_moved(wp);
98 if (wp->w_valid & VALID_CROW)
99 {
100 lnum = wp->w_cursor.lnum;
101 done = wp->w_cline_row;
102 }
103 else
104 {
105 lnum = wp->w_topline;
106 done = 0;
107 }
108
109 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
110 {
111#ifdef FEAT_FOLDING
112 last = lnum;
113 folded = FALSE;
114 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
115 {
116 n = 1;
117 folded = TRUE;
118 }
119 else
120#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100122 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 if (
125#ifdef FEAT_FOLDING
126 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
127#else
128 lnum == wp->w_cursor.lnum
129#endif
130 )
131 {
132 wp->w_cline_row = done;
133 wp->w_cline_height = n;
134#ifdef FEAT_FOLDING
135 wp->w_cline_folded = folded;
136#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100137 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
139 }
140 if (done + n > wp->w_height)
141 break;
142 done += n;
143#ifdef FEAT_FOLDING
144 lnum = last;
145#endif
146 }
147
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 wp->w_botline = lnum;
150 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
151
152 set_empty_rows(wp, done);
153}
154
155/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100156 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
157 * set.
158 */
zeertzjq3f1b5312024-02-06 10:43:36 +0100159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100160redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100161{
162 if ((wp->w_p_rnu
163#ifdef FEAT_SYN_HL
164 || wp->w_p_cul
165#endif
166 )
167 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200168 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200169 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000170 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100171 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200172 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100173}
174
zeertzjq3e559cd2022-03-27 19:26:55 +0100175#ifdef FEAT_SYN_HL
176/*
177 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
178 * contains "screenline".
179 */
180 static void
181redraw_for_cursorcolumn(win_T *wp)
182{
183 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
184 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100186 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100187 redraw_win_later(wp, UPD_SOME_VALID);
188 // When 'cursorlineopt' contains "screenline" need to redraw with
189 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100192 }
193}
194#endif
195
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100196/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100197 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
198 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000199 * Parameter "extra2" should be the padding on the 2nd line, not the first
200 * line.
201 * Returns the number of columns of overlap with buffer text, excluding the
202 * extra padding on the ledge.
203 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100204 int
205sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000206{
207#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100208 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100210 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000211 return 0;
212#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100213 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
214 if (wp->w_p_list && wp->w_lcs_chars.prec)
215 return 1;
216
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 return extra2 > 3 ? 0 : 3 - extra2;
218}
219
220/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000221 * Calculates the skipcol offset for window "wp" given how many
222 * physical lines we want to scroll down.
223 */
224 static int
225skipcol_from_plines(win_T *wp, int plines_off)
226{
227 int width1 = wp->w_width - win_col_off(wp);
228
229 int skipcol = 0;
230 if (plines_off > 0)
231 skipcol += width1;
232 if (plines_off > 1)
233 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
234 return skipcol;
235}
236
237/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100238 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000239 */
240 static void
241reset_skipcol(void)
242{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000243 if (curwin->w_skipcol == 0)
244 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000245
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000246 curwin->w_skipcol = 0;
247
248 // Should use the least expensive way that displays all that changed.
249 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
250 // enough when the top line gets another screen line.
251 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000252}
253
254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 * Update curwin->w_topline and redraw if necessary.
256 * Used to update the screen before printing a message.
257 */
258 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100259update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 update_topline();
262 if (must_redraw)
263 update_screen(0);
264}
265
266/*
267 * Update curwin->w_topline to move the cursor onto the screen.
268 */
269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100270update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271{
272 long line_count;
273 int halfheight;
274 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifdef FEAT_FOLDING
276 linenr_T lnum;
277#endif
278 int check_topline = FALSE;
279 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100280 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100281 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100283 // Cursor is updated instead when this is TRUE for 'splitkeep'.
284 if (skip_update_topline)
285 return;
286
Bram Moolenaar85a20022019-12-21 18:25:54 +0100287 // If there is no valid screen and when the window height is zero just use
288 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200289 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200290 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100291 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 curwin->w_topline = curwin->w_cursor.lnum;
293 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200295 return;
296 }
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 check_cursor_moved(curwin);
299 if (curwin->w_valid & VALID_TOPLINE)
300 return;
301
Bram Moolenaar85a20022019-12-21 18:25:54 +0100302 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000303 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100304 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100306 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100308 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
310
311 /*
312 * If the buffer is empty, always set topline to 1.
313 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
316 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100317 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 curwin->w_botline = 2;
320 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 }
323
324 /*
325 * If the cursor is above or near the top of the window, scroll the window
326 * to show the line the cursor is in, with 'scrolloff' context.
327 */
328 else
329 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100330 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // If the cursor is above topline, scrolling is always needed.
333 // If the cursor is far below topline and there is no folding,
334 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (curwin->w_cursor.lnum < curwin->w_topline)
336 check_topline = TRUE;
337 else if (check_top_offset())
338 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100339 else if (curwin->w_skipcol > 0
340 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100341 {
342 colnr_T vcol;
343
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000344 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100345 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100346 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100347 int overlap = sms_marker_overlap(curwin, curwin_col_off()
348 - curwin_col_off2());
349 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100350 check_topline = TRUE;
351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
353#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100354 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
356 curwin->w_topline))
357 check_topline = TRUE;
358#endif
359
360 if (check_topline)
361 {
362 halfheight = curwin->w_height / 2 - 1;
363 if (halfheight < 2)
364 halfheight = 2;
365
366#ifdef FEAT_FOLDING
367 if (hasAnyFolding(curwin))
368 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100369 // Count the number of logical lines between the cursor and
370 // topline + scrolloff (approximation of how much will be
371 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 n = 0;
373 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
376 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
379 break;
380 (void)hasFolding(lnum, NULL, &lnum);
381 }
382 }
383 else
384#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100385 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar85a20022019-12-21 18:25:54 +0100387 // If we weren't very close to begin with, we scroll to put the
388 // cursor in the middle of the window. Otherwise put the cursor
389 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000391 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 else
393 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000394 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 check_botline = TRUE;
396 }
397 }
398
399 else
400 {
401#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100402 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
404#endif
405 check_botline = TRUE;
406 }
407 }
408
409 /*
410 * If the cursor is below the bottom of the window, scroll the window
411 * to put the cursor on the window.
412 * When w_botline is invalid, recompute it first, to avoid a redraw later.
413 * If w_botline was approximated, we might need a redraw later in a few
414 * cases, but we don't want to spend (a lot of) time recomputing w_botline
415 * for every small change.
416 */
417 if (check_botline)
418 {
419 if (!(curwin->w_valid & VALID_BOTLINE_AP))
420 validate_botline();
421
422 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
423 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000424 if (curwin->w_cursor.lnum < curwin->w_botline)
425 {
426 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100427 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#ifdef FEAT_FOLDING
429 || hasAnyFolding(curwin)
430#endif
431 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 lineoff_T loff;
434
Bram Moolenaar85a20022019-12-21 18:25:54 +0100435 // Cursor is (a few lines) above botline, check if there are
436 // 'scrolloff' window lines below the cursor. If not, need to
437 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 n = curwin->w_empty_rows;
439 loff.lnum = curwin->w_cursor.lnum;
440#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100441 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
443#endif
444#ifdef FEAT_DIFF
445 loff.fill = 0;
446 n += curwin->w_filler_rows;
447#endif
448 loff.height = 0;
449 while (loff.lnum < curwin->w_botline
450#ifdef FEAT_DIFF
451 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
452#endif
453 )
454 {
455 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100456 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 break;
458 botline_forw(&loff);
459 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100460 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100461 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000463 }
464 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100465 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000466 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 }
468 if (check_botline)
469 {
470#ifdef FEAT_FOLDING
471 if (hasAnyFolding(curwin))
472 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // Count the number of logical lines between the cursor and
474 // botline - scrolloff (approximation of how much will be
475 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 line_count = 0;
477 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100478 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
480 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (lnum <= 0 || line_count > curwin->w_height + 1)
483 break;
484 (void)hasFolding(lnum, &lnum, NULL);
485 }
486 }
487 else
488#endif
489 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100490 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000492 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000494 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496 }
497 }
498 curwin->w_valid |= VALID_TOPLINE;
499
500 /*
501 * Need to redraw when topline changed.
502 */
503 if (curwin->w_topline != old_topline
504#ifdef FEAT_DIFF
505 || curwin->w_topfill != old_topfill
506#endif
507 )
508 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100509 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000510 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100511
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100512 // When 'smoothscroll' is not set, should reset w_skipcol.
513 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100514 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100515 else if (curwin->w_skipcol != 0)
516 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000517
Bram Moolenaar85a20022019-12-21 18:25:54 +0100518 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (curwin->w_cursor.lnum == curwin->w_topline)
520 validate_cursor();
521 }
522
Bram Moolenaar375e3392019-01-31 18:26:10 +0100523 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524}
525
526/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000527 * Return the scrolljump value to use for the current window.
528 * When 'scrolljump' is positive use it as-is.
529 * When 'scrolljump' is negative use it as a percentage of the window height.
530 */
531 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100532scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000533{
534 if (p_sj >= 0)
535 return (int)p_sj;
536 return (curwin->w_height * -p_sj) / 100;
537}
538
539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
541 * current window.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 lineoff_T loff;
547 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100548 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaar375e3392019-01-31 18:26:10 +0100550 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#ifdef FEAT_FOLDING
552 || hasAnyFolding(curwin)
553#endif
554 )
555 {
556 loff.lnum = curwin->w_cursor.lnum;
557#ifdef FEAT_DIFF
558 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100559 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#else
561 n = 0;
562#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100563 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100564 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100567 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (loff.lnum < curwin->w_topline
569#ifdef FEAT_DIFF
570 || (loff.lnum == curwin->w_topline && loff.fill > 0)
571#endif
572 )
573 break;
574 n += loff.height;
575 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100576 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 return TRUE;
578 }
579 return FALSE;
580}
581
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100582/*
583 * Update w_curswant.
584 */
585 void
586update_curswant_force(void)
587{
588 validate_virtcol();
589 curwin->w_curswant = curwin->w_virtcol
590#ifdef FEAT_PROP_POPUP
591 - curwin->w_virtcol_first_char
592#endif
593 ;
594 curwin->w_set_curswant = FALSE;
595}
596
597/*
598 * Update w_curswant if w_set_curswant is set.
599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100604 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605}
606
607/*
608 * Check if the cursor has moved. Set the w_valid flag accordingly.
609 */
610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100611check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
614 {
615 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100616 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
617 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 wp->w_valid_cursor = wp->w_cursor;
619 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100620 wp->w_valid_skipcol = wp->w_skipcol;
621 }
622 else if (wp->w_skipcol != wp->w_valid_skipcol)
623 {
624 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
625 |VALID_CHEIGHT|VALID_CROW
626 |VALID_BOTLINE|VALID_BOTLINE_AP);
627 wp->w_valid_cursor = wp->w_cursor;
628 wp->w_valid_leftcol = wp->w_leftcol;
629 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631 else if (wp->w_cursor.col != wp->w_valid_cursor.col
632 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100633 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
636 wp->w_valid_cursor.col = wp->w_cursor.col;
637 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 }
640}
641
642/*
643 * Call this function when some window settings have changed, which require
644 * the cursor position, botline and topline to be recomputed and the window to
645 * be redrawn. E.g, when changing the 'wrap' option or folding.
646 */
647 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 changed_window_setting_win(curwin);
651}
652
653 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100654changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 wp->w_lines_valid = 0;
657 changed_line_abv_curs_win(wp);
658 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100659 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661
Dominique Pellee764d1b2023-03-12 21:20:59 +0000662#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000664 * Call changed_window_setting_win() for every window containing "buf".
665 */
666 void
667changed_window_setting_buf(buf_T *buf)
668{
669 tabpage_T *tp;
670 win_T *wp;
671
672 FOR_ALL_TAB_WINDOWS(tp, wp)
673 if (wp->w_buffer == buf)
674 changed_window_setting_win(wp);
675}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000676#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000677
678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 * Set wp->w_topline to a certain number.
680 */
681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200684#ifdef FEAT_DIFF
685 linenr_T prev_topline = wp->w_topline;
686#endif
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100689 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
691#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100692 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100694 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
695 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000697 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200699 if (lnum != prev_topline)
700 // Keep the filler lines when the topline didn't change.
701 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100705 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707
708/*
709 * Call this function when the length of the cursor line (in screen
710 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100711 * If the line length changed the number of screen lines might change,
712 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 * Need to take care of w_botline separately!
714 */
715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100716changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar85090142023-06-01 19:27:08 +0100718 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 |VALID_CHEIGHT|VALID_TOPLINE);
720}
721
722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100723changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
Bram Moolenaar85090142023-06-01 19:27:08 +0100725 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 |VALID_CHEIGHT|VALID_TOPLINE);
727}
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729/*
730 * Call this function when the length of a line (in screen characters) above
731 * the cursor have changed.
732 * Need to take care of w_botline separately!
733 */
734 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100735changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
738 |VALID_CHEIGHT|VALID_TOPLINE);
739}
740
741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
745 |VALID_CHEIGHT|VALID_TOPLINE);
746}
747
Dominique Pellee764d1b2023-03-12 21:20:59 +0000748#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100750 * Display of line has changed for "buf", invalidate cursor position and
751 * w_botline.
752 */
753 void
754changed_line_display_buf(buf_T *buf)
755{
756 win_T *wp;
757
758 FOR_ALL_WINDOWS(wp)
759 if (wp->w_buffer == buf)
760 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
761 |VALID_CROW|VALID_CHEIGHT
762 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
763}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000764#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100765
766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 * Make sure the value of curwin->w_botline is valid.
768 */
769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100772 validate_botline_win(curwin);
773}
774
775/*
776 * Make sure the value of wp->w_botline is valid.
777 */
778 void
779validate_botline_win(win_T *wp)
780{
781 if (!(wp->w_valid & VALID_BOTLINE))
782 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783}
784
785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * Mark curwin->w_botline as invalid (because of some change in the buffer).
787 */
788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100789invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
792}
793
794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
798}
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100801approximate_botline_win(
802 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 wp->w_valid &= ~VALID_BOTLINE;
805}
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807/*
808 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
809 */
810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 check_cursor_moved(curwin);
814 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
815 (VALID_WROW|VALID_WCOL));
816}
817
818/*
819 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
820 * w_topline must be valid, you may need to call update_topline() first!
821 */
822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100825 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 check_cursor_moved(curwin);
827 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
828 curs_columns(TRUE);
829}
830
831#if defined(FEAT_GUI) || defined(PROTO)
832/*
833 * validate w_cline_row.
834 */
835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 /*
839 * First make sure that w_topline is valid (after moving the cursor).
840 */
841 update_topline();
842 check_cursor_moved(curwin);
843 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100844 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846#endif
847
848/*
849 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200850 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 */
852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100853curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
855 linenr_T lnum;
856 int i;
857 int all_invalid;
858 int valid;
859#ifdef FEAT_FOLDING
860 long fold_count;
861#endif
862
Bram Moolenaar85a20022019-12-21 18:25:54 +0100863 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 all_invalid = (!redrawing()
865 || wp->w_lines_valid == 0
866 || wp->w_lines[0].wl_lnum > wp->w_topline);
867 i = 0;
868 wp->w_cline_row = 0;
869 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
870 {
871 valid = FALSE;
872 if (!all_invalid && i < wp->w_lines_valid)
873 {
874 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100875 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (wp->w_lines[i].wl_lnum == lnum)
877 {
878#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100879 // Check for newly inserted lines below this row, in which
880 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (!wp->w_buffer->b_mod_set
882 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
883 || wp->w_buffer->b_mod_top
884 > wp->w_lines[i].wl_lastlnum + 1)
885#endif
886 valid = TRUE;
887 }
888 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100889 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100892 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100894 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100896 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898#ifdef FEAT_FOLDING
899 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (lnum > wp->w_cursor.lnum)
902 break;
903#else
904 ++lnum;
905#endif
906 wp->w_cline_row += wp->w_lines[i].wl_size;
907 }
908 else
909 {
910#ifdef FEAT_FOLDING
911 fold_count = foldedCount(wp, lnum, NULL);
912 if (fold_count)
913 {
914 lnum += fold_count;
915 if (lnum > wp->w_cursor.lnum)
916 break;
917 ++wp->w_cline_row;
918 }
919 else
920#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100921 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100922 wp->w_cline_row += plines_correct_topline(wp, lnum);
923 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 }
927
928 check_cursor_moved(wp);
929 if (!(wp->w_valid & VALID_CHEIGHT))
930 {
931 if (all_invalid
932 || i == wp->w_lines_valid
933 || (i < wp->w_lines_valid
934 && (!wp->w_lines[i].wl_valid
935 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
936 {
937#ifdef FEAT_DIFF
938 if (wp->w_cursor.lnum == wp->w_topline)
939 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
940 TRUE) + wp->w_topfill;
941 else
942#endif
943 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
944#ifdef FEAT_FOLDING
945 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
946 NULL, NULL, TRUE, NULL);
947#endif
948 }
949 else if (i > wp->w_lines_valid)
950 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100951 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_cline_height = 0;
953#ifdef FEAT_FOLDING
954 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
955 NULL, NULL, TRUE, NULL);
956#endif
957 }
958 else
959 {
960 wp->w_cline_height = wp->w_lines[i].wl_size;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = wp->w_lines[i].wl_folded;
963#endif
964 }
965 }
966
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100967 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
971/*
972 * Validate curwin->w_virtcol only.
973 */
974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 validate_virtcol_win(curwin);
978}
979
980/*
981 * Validate wp->w_virtcol only.
982 */
983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100984validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000987
988 if (wp->w_valid & VALID_VIRTCOL)
989 return;
990
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100991#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000995#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000997#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000998 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Validate curwin->w_cline_height only.
1003 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001008
1009 if (curwin->w_valid & VALID_CHEIGHT)
1010 return;
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001013 if (curwin->w_cursor.lnum == curwin->w_topline)
1014 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1015 + curwin->w_topfill;
1016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001022 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001026 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 colnr_T off;
1032 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001033 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001037 if (curwin->w_valid & VALID_WCOL)
1038 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001039
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 col = curwin->w_virtcol;
1041 off = curwin_col_off();
1042 col += off;
1043 width = curwin->w_width - off + curwin_col_off2();
1044
1045 // long line wrapping, adjust curwin->w_wrow
1046 if (curwin->w_p_wrap
1047 && col >= (colnr_T)curwin->w_width
1048 && width > 0)
1049 // use same formula as what is used in curs_columns()
1050 col -= ((col - curwin->w_width) / width + 1) * width;
1051 if (col > (int)curwin->w_leftcol)
1052 col -= curwin->w_leftcol;
1053 else
1054 col = 0;
1055 curwin->w_wcol = col;
1056
1057 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001058#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001059 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001064 * Compute offset of a window, occupied by absolute or relative line number,
1065 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001068win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar64486672010-05-16 15:46:46 +02001070 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001071 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_FOLDING
1073 + wp->w_p_fdc
1074#endif
1075#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001076 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
1078 );
1079}
1080
1081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001082curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return win_col_off(curwin);
1085}
1086
1087/*
1088 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001089 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1090 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
1092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar64486672010-05-16 15:46:46 +02001095 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001096 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 return 0;
1098}
1099
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 return win_col_off2(curwin);
1104}
1105
1106/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001107 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * Also updates curwin->w_wrow and curwin->w_cline_row.
1109 * Also updates curwin->w_leftcol.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int off_left, off_right;
1118 int n;
1119 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001120 int width1; // text width for first screen line
1121 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int new_leftcol;
1123 colnr_T startcol;
1124 colnr_T endcol;
1125 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001126 long so = get_scrolloff_value();
1127 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001128 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 /*
1131 * First make sure that w_topline is valid (after moving the cursor).
1132 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001133 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 /*
1136 * Next make sure that w_cline_row is valid.
1137 */
1138 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001139 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001141#ifdef FEAT_PROP_POPUP
1142 // will be set by getvvcol() but not reset
1143 curwin->w_virtcol_first_char = 0;
1144#endif
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 /*
1147 * Compute the number of virtual columns.
1148 */
1149#ifdef FEAT_FOLDING
1150 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1153 else
1154#endif
1155 getvvcol(curwin, &curwin->w_cursor,
1156 &startcol, &(curwin->w_virtcol), &endcol);
1157
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001160 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 extra = curwin_col_off();
1163 curwin->w_wcol = curwin->w_virtcol + extra;
1164 endcol += extra;
1165
1166 /*
1167 * Now compute w_wrow, counting screen lines from w_cline_row.
1168 */
1169 curwin->w_wrow = curwin->w_cline_row;
1170
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001171 width1 = curwin->w_width - extra;
1172 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001175 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001176 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001177 if (curwin->w_p_wrap)
1178 curwin->w_wrow = curwin->w_height - 1;
1179 else
1180 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001182 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001184 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001186 // skip columns that are not visible
1187 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001188 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001189 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001190 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001191 // Deduct by multiples of width2. This allows the long line
1192 // wrapping formula below to correctly calculate the w_wcol value
1193 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001198 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001200 did_sub_skipcol = TRUE;
1201 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001202
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001204 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001207 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1208 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 }
1212
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1214 // is not folded.
1215 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001216 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_FOLDING
1218 && !curwin->w_cline_folded
1219#endif
1220 )
1221 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001222#ifdef FEAT_PROP_POPUP
1223 if (curwin->w_virtcol_first_char > 0)
1224 {
1225 int cols = (curwin->w_width - extra);
1226 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1227
1228 // each "above" text prop shifts the text one row down
1229 curwin->w_wrow += rows;
1230 curwin->w_wcol -= rows * cols;
1231 endcol -= rows * cols;
1232 curwin->w_cline_height = rows + 1;
1233 }
1234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 /*
1236 * If Cursor is left of the screen, scroll rightwards.
1237 * If Cursor is right of the screen, scroll leftwards
1238 * If we get closer to the edge than 'sidescrolloff', scroll a little
1239 * extra
1240 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001241 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001242 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001243 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (off_left < 0 || off_right > 0)
1245 {
1246 if (off_left < 0)
1247 diff = -off_left;
1248 else
1249 diff = off_right;
1250
Bram Moolenaar85a20022019-12-21 18:25:54 +01001251 // When far off or not enough room on either side, put cursor in
1252 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001253 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1254 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
1256 {
1257 if (diff < p_ss)
1258 diff = p_ss;
1259 if (off_left < 0)
1260 new_leftcol = curwin->w_leftcol - diff;
1261 else
1262 new_leftcol = curwin->w_leftcol + diff;
1263 }
1264 if (new_leftcol < 0)
1265 new_leftcol = 0;
1266 if (new_leftcol != (int)curwin->w_leftcol)
1267 {
1268 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001269 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001270 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 curwin->w_wcol -= curwin->w_leftcol;
1274 }
1275 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1276 curwin->w_wcol -= curwin->w_leftcol;
1277 else
1278 curwin->w_wcol = 0;
1279
1280#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001281 // Skip over filler lines. At the top use w_topfill, there
1282 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (curwin->w_cursor.lnum == curwin->w_topline)
1284 curwin->w_wrow += curwin->w_topfill;
1285 else
1286 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1287#endif
1288
1289 prev_skipcol = curwin->w_skipcol;
1290
1291 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if ((curwin->w_wrow >= curwin->w_height
1294 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001295 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (p_lines =
1297#ifdef FEAT_DIFF
1298 plines_win_nofill
1299#else
1300 plines_win
1301#endif
1302 (curwin, curwin->w_cursor.lnum, FALSE))
1303 - 1 >= curwin->w_height))
1304 && curwin->w_height != 0
1305 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001306 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001307 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 // Cursor past end of screen. Happens with a single line that does
1310 // not fit on screen. Find a skipcol to show the text around the
1311 // cursor. Avoid scrolling all the time. compute value of "extra":
1312 // 1: Less than 'scrolloff' lines above
1313 // 2: Less than 'scrolloff' lines below
1314 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001316 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 // Compute last display line of the buffer line that we want at the
1319 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (p_lines == 0)
1321 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1322 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001323 if (p_lines > curwin->w_wrow + so)
1324 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 else
1326 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001327 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 extra += 2;
1329
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001330 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001333 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (n > curwin->w_height / 2)
1335 n -= curwin->w_height / 2;
1336 else
1337 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (n > p_lines - curwin->w_height + 1)
1340 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001341 if (n > 0)
1342 curwin->w_skipcol = width1 + (n - 1) * width2;
1343 else
1344 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 else if (extra == 1)
1347 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001348 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001349 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1350 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (extra > 0)
1352 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001353 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1354 extra = curwin->w_skipcol / width2;
1355 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 }
1358 else if (extra == 2)
1359 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001360 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001361 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001363 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (endcol > curwin->w_skipcol)
1365 curwin->w_skipcol = endcol;
1366 }
1367
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001368 // adjust w_wrow for the changed w_skipcol
1369 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001372 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (curwin->w_wrow >= curwin->w_height)
1375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001376 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 curwin->w_wrow -= extra;
1380 }
1381
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001382 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (extra > 0)
1384 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1385 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001386 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001388 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 curwin->w_skipcol = 0;
1390 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001391 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001393#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001394 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001395#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001396#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1397 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1398 {
1399 curwin->w_wrow += popup_top_extra(curwin);
1400 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001401 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001402 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001403 else
1404 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001405#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001406
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001407 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1408 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001409 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001410 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1413}
1414
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001415#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001416/*
1417 * Compute the screen position of text character at "pos" in window "wp"
1418 * The resulting values are one-based, zero when character is not visible.
1419 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001420 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001421textpos2screenpos(
1422 win_T *wp,
1423 pos_T *pos,
1424 int *rowp, // screen row
1425 int *scolp, // start screen column
1426 int *ccolp, // cursor screen column
1427 int *ecolp) // end screen column
1428{
1429 colnr_T scol = 0, ccol = 0, ecol = 0;
1430 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001431 colnr_T coloff = 0;
1432
Bram Moolenaar189663b2021-07-21 18:04:56 +02001433 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001434 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001435 colnr_T col;
1436 int width;
1437 linenr_T lnum = pos->lnum;
1438#ifdef FEAT_FOLDING
1439 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001440
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001441 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1442#endif
zeertzjq6235a102023-08-19 14:12:42 +02001443 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE);
zeertzjqbfe377b2023-08-17 22:58:53 +02001444 // "row" should be the screen line where line "lnum" begins, which can
1445 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001446 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001447
1448#ifdef FEAT_DIFF
1449 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001450 row += lnum == wp->w_topline ? wp->w_topfill
1451 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001452#endif
1453
zeertzjqba2d1912022-12-18 12:28:59 +00001454 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001455#ifdef FEAT_FOLDING
1456 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001457 {
zeertzjq6235a102023-08-19 14:12:42 +02001458 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001459 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001460 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001462#endif
1463 {
1464 getvcol(wp, pos, &scol, &ccol, &ecol);
1465
1466 // similar to what is done in validate_cursor_col()
1467 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468 col += off;
1469 width = wp->w_width - off + win_col_off2(wp);
1470
1471 // long line wrapping, adjust row
1472 if (wp->w_p_wrap
1473 && col >= (colnr_T)wp->w_width
1474 && width > 0)
1475 {
1476 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001477 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001478 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001479 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001480 }
1481 col -= wp->w_leftcol;
1482 if (col >= wp->w_width)
1483 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001484 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001485 {
1486 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001487 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001488 }
1489 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001490 // character is out of the window
1491 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001492 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001494 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001495 *scolp = scol + coloff;
1496 *ccolp = ccol + coloff;
1497 *ecolp = ecol + coloff;
1498}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001499#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001500
Bram Moolenaar12034e22019-08-25 22:25:02 +02001501#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001502/*
1503 * "screenpos({winid}, {lnum}, {col})" function
1504 */
1505 void
1506f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1507{
1508 dict_T *dict;
1509 win_T *wp;
1510 pos_T pos;
1511 int row = 0;
1512 int scol = 0, ccol = 0, ecol = 0;
1513
Bram Moolenaar93a10962022-06-16 11:42:09 +01001514 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001515 return;
1516 dict = rettv->vval.v_dict;
1517
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001518 if (in_vim9script()
1519 && (check_for_number_arg(argvars, 0) == FAIL
1520 || check_for_number_arg(argvars, 1) == FAIL
1521 || check_for_number_arg(argvars, 2) == FAIL))
1522 return;
1523
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001524 wp = find_win_by_nr_or_id(&argvars[0]);
1525 if (wp == NULL)
1526 return;
1527
1528 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001529 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1530 {
1531 semsg(_(e_invalid_line_number_nr), pos.lnum);
1532 return;
1533 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001534 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001535 if (pos.col < 0)
1536 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001537 pos.coladd = 0;
1538 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1539
1540 dict_add_number(dict, "row", row);
1541 dict_add_number(dict, "col", scol);
1542 dict_add_number(dict, "curscol", ccol);
1543 dict_add_number(dict, "endcol", ecol);
1544}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001545
1546/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001547 * Convert a virtual (screen) column to a character column. The first column
1548 * is one. For a multibyte character, the column number of the first byte is
1549 * returned.
1550 */
1551 static int
1552virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1553{
zeertzjqf5a94d52023-10-15 10:03:30 +02001554 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001555 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1556 char_u *p = line + offset;
1557
zeertzjqb583eda2023-10-14 11:32:28 +02001558 if (*p == NUL)
1559 {
1560 if (p == line) // empty line
1561 return 0;
1562 // Move to the first byte of the last char.
1563 MB_PTR_BACK(line, p);
1564 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001565 return (int)(p - line + 1);
1566}
1567
1568/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001569 * "virtcol2col({winid}, {lnum}, {col})" function
1570 */
1571 void
1572f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1573{
1574 win_T *wp;
1575 linenr_T lnum;
1576 int screencol;
1577 int error = FALSE;
1578
1579 rettv->vval.v_number = -1;
1580
1581 if (check_for_number_arg(argvars, 0) == FAIL
1582 || check_for_number_arg(argvars, 1) == FAIL
1583 || check_for_number_arg(argvars, 2) == FAIL)
1584 return;
1585
1586 wp = find_win_by_nr_or_id(&argvars[0]);
1587 if (wp == NULL)
1588 return;
1589
1590 lnum = tv_get_number_chk(&argvars[1], &error);
1591 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1592 return;
1593
1594 screencol = tv_get_number_chk(&argvars[2], &error);
1595 if (error || screencol < 0)
1596 return;
1597
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001598 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001599}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001600#endif
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
1603 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001606scrolldown(
1607 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 int wrow;
1612 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001613 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001614 int width1 = 0;
1615 int width2 = 0;
1616
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001617 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001618 {
1619 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001620 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623#ifdef FEAT_FOLDING
1624 linenr_T first;
1625
Bram Moolenaar85a20022019-12-21 18:25:54 +01001626 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1628#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001629 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001630 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001633 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1634 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 ++curwin->w_topfill;
1637 ++done;
1638 }
1639 else
1640#endif
1641 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001642 // break when at the very top
1643 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001644 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001646 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001648 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001649 if (curwin->w_skipcol >= width1 + width2)
1650 curwin->w_skipcol -= width2;
1651 else
1652 curwin->w_skipcol -= width1;
1653 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
1656 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001657 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001658 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001659 --curwin->w_topline;
1660 curwin->w_skipcol = 0;
1661#ifdef FEAT_DIFF
1662 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001664#ifdef FEAT_FOLDING
1665 // A sequence of folded lines only counts for one logical line
1666 if (hasFolding(curwin->w_topline, &first, NULL))
1667 {
1668 ++done;
1669 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001670 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001671 curwin->w_botline -= curwin->w_topline - first;
1672 curwin->w_topline = first;
1673 }
1674 else
1675#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001676 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001677 {
1678 int size = win_linetabsize(curwin, curwin->w_topline,
1679 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1680 if (size > width1)
1681 {
1682 curwin->w_skipcol = width1;
1683 size -= width1;
1684 redraw_later(UPD_NOT_VALID);
1685 }
1686 while (size > width2)
1687 {
1688 curwin->w_skipcol += width2;
1689 size -= width2;
1690 }
1691 ++done;
1692 }
1693 else
1694 done += PLINES_NOFILL(curwin->w_topline);
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001697 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 invalidate_botline();
1699 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001700 curwin->w_wrow += done; // keep w_wrow updated
1701 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703#ifdef FEAT_DIFF
1704 if (curwin->w_cursor.lnum == curwin->w_topline)
1705 curwin->w_cline_row = 0;
1706 check_topfill(curwin, TRUE);
1707#endif
1708
1709 /*
1710 * Compute the row number of the last row of the cursor line
1711 * and move the cursor onto the displayed part of the window.
1712 */
1713 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001714 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
1716 validate_virtcol();
1717 validate_cheight();
1718 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001719 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1722 {
1723#ifdef FEAT_FOLDING
1724 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1725 {
1726 --wrow;
1727 if (first == 1)
1728 curwin->w_cursor.lnum = 1;
1729 else
1730 curwin->w_cursor.lnum = first - 1;
1731 }
1732 else
1733#endif
1734 wrow -= plines(curwin->w_cursor.lnum--);
1735 curwin->w_valid &=
1736 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1737 moved = TRUE;
1738 }
1739 if (moved)
1740 {
1741#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001742 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 foldAdjustCursor();
1744#endif
1745 coladvance(curwin->w_curswant);
1746 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001747
1748 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1749 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001750 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001751 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1752
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001753 // make sure the cursor is in the visible text
1754 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001755 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001756 int row = 0;
1757 if (col >= width1)
1758 {
1759 col -= width1;
1760 ++row;
1761 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001762 if (col > width2 && width2 > 0)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001763 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001764 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001765 // even so col is not used anymore,
1766 // make sure it is correct, just in case
1767 col = col % width2;
1768 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001769 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001770 {
1771 curwin->w_curswant = curwin->w_virtcol
1772 - (row - curwin->w_height + 1) * width2;
1773 coladvance(curwin->w_curswant);
1774 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776}
1777
1778/*
1779 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001782scrollup(
1783 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001784 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001786 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001788 if (do_sms
1789# ifdef FEAT_FOLDING
1790 || (byfold && hasAnyFolding(curwin))
1791# endif
1792# ifdef FEAT_DIFF
1793 || (curwin->w_p_diff && !curwin->w_p_wrap)
1794# endif
1795 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001797 int width1 = curwin->w_width - curwin_col_off();
1798 int width2 = width1 + curwin_col_off2();
1799 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001800 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001801
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001802 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001803 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001804
1805 // diff mode: first consume "topfill"
1806 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1807 // the line, then advance to the next line.
1808 // folding: count each sequence of folded lines as one logical line.
1809 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811# ifdef FEAT_DIFF
1812 if (curwin->w_topfill > 0)
1813 --curwin->w_topfill;
1814 else
1815# endif
1816 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001817 linenr_T lnum = curwin->w_topline;
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819# ifdef FEAT_FOLDING
1820 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001821 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 (void)hasFolding(lnum, NULL, &lnum);
1823# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001824 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001825 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001826 // 'smoothscroll': increase "w_skipcol" until it goes over
1827 // the end of the line, then advance to the next line.
1828 int add = curwin->w_skipcol > 0 ? width2 : width1;
1829 curwin->w_skipcol += add;
1830 if (curwin->w_skipcol >= size)
1831 {
1832 if (lnum == curbuf->b_ml.ml_line_count)
1833 {
1834 // at the last screen line, can't scroll further
1835 curwin->w_skipcol -= add;
1836 break;
1837 }
1838 ++lnum;
1839 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001840 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841 else
1842 {
1843 if (lnum >= curbuf->b_ml.ml_line_count)
1844 break;
1845 ++lnum;
1846 }
1847
1848 if (lnum > curwin->w_topline)
1849 {
1850 // approximate w_botline
1851 curwin->w_botline += lnum - curwin->w_topline;
1852 curwin->w_topline = lnum;
1853# ifdef FEAT_DIFF
1854 curwin->w_topfill = diff_check_fill(curwin, lnum);
1855# endif
1856 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001857 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001858 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001859 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001860 }
1861 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001862
zeertzjqd9a92dc2023-06-05 18:41:35 +01001863 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1864 // need to redraw more, because wl_size of the (new) topline may
1865 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001866 redraw_later(UPD_NOT_VALID);
1867 }
1868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001871 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873
1874 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1875 curwin->w_topline = curbuf->b_ml.ml_line_count;
1876 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1877 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1878
1879#ifdef FEAT_DIFF
1880 check_topfill(curwin, FALSE);
1881#endif
1882
1883#ifdef FEAT_FOLDING
1884 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001885 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1887#endif
1888
1889 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1890 if (curwin->w_cursor.lnum < curwin->w_topline)
1891 {
1892 curwin->w_cursor.lnum = curwin->w_topline;
1893 curwin->w_valid &=
1894 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1895 coladvance(curwin->w_curswant);
1896 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001897 if (curwin->w_cursor.lnum == curwin->w_topline
1898 && do_sms && curwin->w_skipcol > 0)
1899 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001900 int col_off = curwin_col_off();
1901 int col_off2 = curwin_col_off2();
1902
1903 int width1 = curwin->w_width - col_off;
1904 int width2 = width1 + col_off2;
1905 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001906 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001907 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001908 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001909
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001910 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001911 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001912 int overlap = scrolloff_cols != 0 ? 0
1913 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001914
Bram Moolenaar118c2352022-10-09 17:19:27 +01001915 // Make sure the cursor is in a visible part of the line, taking
1916 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001917 // If there are not enough screen lines put the cursor in the middle.
1918 if (scrolloff_cols > space_cols / 2)
1919 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001920 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001921 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001922 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001923 colnr_T col = curwin->w_virtcol;
1924
1925 if (col < width1)
1926 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001927 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001928 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001929 curwin->w_curswant = col;
1930 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001931
1932 // validate_virtcol() marked various things as valid, but after
1933 // moving the cursor they need to be recomputed
1934 curwin->w_valid &=
1935 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001936 }
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938}
1939
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001940/*
1941 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1942 * valid for 'smoothscroll'.
1943 */
1944 void
1945adjust_skipcol(void)
1946{
1947 if (!curwin->w_p_wrap
1948 || !curwin->w_p_sms
1949 || curwin->w_cursor.lnum != curwin->w_topline)
1950 return;
1951
1952 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001953 if (width1 <= 0)
1954 return; // no text will be displayed
1955
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001956 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001957 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001958 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1959 int scrolled = FALSE;
1960
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001961 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001962 if (curwin->w_cline_height == curwin->w_height
1963 // w_cline_height may be capped at w_height, check there aren't
1964 // actually more lines.
1965 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1966 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001967 {
1968 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001969 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001970 return;
1971 }
1972
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001973 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001974 int overlap = sms_marker_overlap(curwin,
1975 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001976 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001977 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001978 {
1979 // scroll a screen line down
1980 if (curwin->w_skipcol >= width1 + width2)
1981 curwin->w_skipcol -= width2;
1982 else
1983 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001984 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001985 }
1986 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001987 {
1988 validate_virtcol();
1989 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001990 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001991 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001992
1993 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1994 int row = 0;
1995 if (col >= width1)
1996 {
1997 col -= width1;
1998 ++row;
1999 }
2000 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002001 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002002 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002003 // col may no longer be used, but make
2004 // sure it is correct anyhow, just in case
2005 col = col % width2;
2006 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002007 if (row >= curwin->w_height)
2008 {
2009 if (curwin->w_skipcol == 0)
2010 {
2011 curwin->w_skipcol += width1;
2012 --row;
2013 }
2014 if (row >= curwin->w_height)
2015 curwin->w_skipcol += (row - curwin->w_height) * width2;
2016 redraw_later(UPD_NOT_VALID);
2017 }
2018}
2019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020#ifdef FEAT_DIFF
2021/*
2022 * Don't end up with too many filler lines in the window.
2023 */
2024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002025check_topfill(
2026 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int n;
2030
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002031 if (wp->w_topfill <= 0)
2032 return;
2033
2034 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2035 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002037 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002039 --wp->w_topline;
2040 wp->w_topfill = 0;
2041 }
2042 else
2043 {
2044 wp->w_topfill = wp->w_height - n;
2045 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 }
2049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#endif
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052/*
2053 * Scroll the screen one line down, but don't do it if it would move the
2054 * cursor off the screen.
2055 */
2056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002057scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 int end_row;
2060#ifdef FEAT_DIFF
2061 int can_fill = (curwin->w_topfill
2062 < diff_check_fill(curwin, curwin->w_topline));
2063#endif
2064
2065 if (curwin->w_topline <= 1
2066#ifdef FEAT_DIFF
2067 && !can_fill
2068#endif
2069 )
2070 return;
2071
Bram Moolenaar85a20022019-12-21 18:25:54 +01002072 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /*
2075 * Compute the row number of the last row of the cursor line
2076 * and make sure it doesn't go off the screen. Make sure the cursor
2077 * doesn't go past 'scrolloff' lines from the screen end.
2078 */
2079 end_row = curwin->w_wrow;
2080#ifdef FEAT_DIFF
2081 if (can_fill)
2082 ++end_row;
2083 else
2084 end_row += plines_nofill(curwin->w_topline - 1);
2085#else
2086 end_row += plines(curwin->w_topline - 1);
2087#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002088 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 validate_cheight();
2091 validate_virtcol();
2092 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002093 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002095 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097#ifdef FEAT_DIFF
2098 if (can_fill)
2099 {
2100 ++curwin->w_topfill;
2101 check_topfill(curwin, TRUE);
2102 }
2103 else
2104 {
2105 --curwin->w_topline;
2106 curwin->w_topfill = 0;
2107 }
2108#else
2109 --curwin->w_topline;
2110#endif
2111#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002112 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2116 }
2117}
2118
2119/*
2120 * Scroll the screen one line up, but don't do it if it would move the cursor
2121 * off the screen.
2122 */
2123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 int start_row;
2127
2128 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2129#ifdef FEAT_DIFF
2130 && curwin->w_topfill == 0
2131#endif
2132 )
2133 return;
2134
Bram Moolenaar85a20022019-12-21 18:25:54 +01002135 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 /*
2138 * Compute the row number of the first row of the cursor line
2139 * and make sure it doesn't go off the screen. Make sure the cursor
2140 * doesn't go before 'scrolloff' lines from the screen start.
2141 */
2142#ifdef FEAT_DIFF
2143 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2144 - curwin->w_topfill;
2145#else
2146 start_row = curwin->w_wrow - plines(curwin->w_topline);
2147#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002148 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002151 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002153 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155#ifdef FEAT_DIFF
2156 if (curwin->w_topfill > 0)
2157 --curwin->w_topfill;
2158 else
2159#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002160 {
2161#ifdef FEAT_FOLDING
2162 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002165 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002166 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2168 }
2169}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171/*
2172 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2173 * a (wrapped) text line. Uses and sets "lp->fill".
2174 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002175 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 */
2177 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002178topline_back_winheight(
2179 lineoff_T *lp,
2180 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182#ifdef FEAT_DIFF
2183 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2184 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002185 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 ++lp->fill;
2187 lp->height = 1;
2188 }
2189 else
2190#endif
2191 {
2192 --lp->lnum;
2193#ifdef FEAT_DIFF
2194 lp->fill = 0;
2195#endif
2196 if (lp->lnum < 1)
2197 lp->height = MAXCOL;
2198 else
2199#ifdef FEAT_FOLDING
2200 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 lp->height = 1;
2203 else
2204#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002205 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207}
2208
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002209 static void
2210topline_back(lineoff_T *lp)
2211{
2212 topline_back_winheight(lp, TRUE);
2213}
2214
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216/*
2217 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2218 * a (wrapped) text line. Uses and sets "lp->fill".
2219 * Returns the height of the added line in "lp->height".
2220 * Lines below the last one are incredibly high.
2221 */
2222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225#ifdef FEAT_DIFF
2226 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 ++lp->fill;
2230 lp->height = 1;
2231 }
2232 else
2233#endif
2234 {
2235 ++lp->lnum;
2236#ifdef FEAT_DIFF
2237 lp->fill = 0;
2238#endif
2239 if (lp->lnum > curbuf->b_ml.ml_line_count)
2240 lp->height = MAXCOL;
2241 else
2242#ifdef FEAT_FOLDING
2243 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002244 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 lp->height = 1;
2246 else
2247#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002248 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250}
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252/*
2253 * Recompute topline to put the cursor at the top of the window.
2254 * Scroll at least "min_scroll" lines.
2255 * If "always" is TRUE, always set topline (for "zt").
2256 */
2257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002258scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 int scrolled = 0;
2261 int extra = 0;
2262 int used;
2263 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002264 linenr_T top; // just above displayed lines
2265 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002267 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#ifdef FEAT_DIFF
2269 linenr_T old_topfill = curwin->w_topfill;
2270#endif
2271 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002272 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (mouse_dragging > 0)
2275 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277 /*
2278 * Decrease topline until:
2279 * - it has become 1
2280 * - (part of) the cursor line is moved off the screen or
2281 * - moved at least 'scrolljump' lines and
2282 * - at least 'scrolloff' lines above and below the cursor
2283 */
2284 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002285 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (curwin->w_cursor.lnum < curwin->w_topline)
2287 scrolled = used;
2288
2289#ifdef FEAT_FOLDING
2290 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2291 {
2292 --top;
2293 ++bot;
2294 }
2295 else
2296#endif
2297 {
2298 top = curwin->w_cursor.lnum - 1;
2299 bot = curwin->w_cursor.lnum + 1;
2300 }
2301 new_topline = top + 1;
2302
2303#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002304 // "used" already contains the number of filler lines above, don't add it
2305 // again.
2306 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002307 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309
2310 /*
2311 * Check if the lines from "top" to "bot" fit in the window. If they do,
2312 * set new_topline and advance "top" and "bot" to include more lines.
2313 */
2314 while (top > 0)
2315 {
2316#ifdef FEAT_FOLDING
2317 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 i = 1;
2320 else
2321#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002322 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002323 if (top < curwin->w_topline)
2324 scrolled += i;
2325
2326 // If scrolling is needed, scroll at least 'sj' lines.
2327 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2328 && extra >= off)
2329 break;
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 used += i;
2332 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2333 {
2334#ifdef FEAT_FOLDING
2335 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 ++used;
2338 else
2339#endif
2340 used += plines(bot);
2341 }
2342 if (used > curwin->w_height)
2343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 extra += i;
2346 new_topline = top;
2347 --top;
2348 ++bot;
2349 }
2350
2351 /*
2352 * If we don't have enough space, put cursor in the middle.
2353 * This makes sure we get the same position when using "k" and "j"
2354 * in a small window.
2355 */
2356 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002357 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 else
2359 {
2360 /*
2361 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002362 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
2364 if (new_topline < curwin->w_topline || always)
2365 curwin->w_topline = new_topline;
2366 if (curwin->w_topline > curwin->w_cursor.lnum)
2367 curwin->w_topline = curwin->w_cursor.lnum;
2368#ifdef FEAT_DIFF
2369 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2370 if (curwin->w_topfill > 0 && extra > off)
2371 {
2372 curwin->w_topfill -= extra - off;
2373 if (curwin->w_topfill < 0)
2374 curwin->w_topfill = 0;
2375 }
2376 check_topfill(curwin, FALSE);
2377#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002378 if (curwin->w_topline != old_topline)
2379 reset_skipcol();
2380 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002381 {
2382 validate_virtcol();
2383 if (curwin->w_skipcol >= curwin->w_virtcol)
2384 // TODO: if the line doesn't fit may optimize w_skipcol instead
2385 // of making it zero
2386 reset_skipcol();
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002389 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#ifdef FEAT_DIFF
2391 || curwin->w_topfill != old_topfill
2392#endif
2393 )
2394 curwin->w_valid &=
2395 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2396 curwin->w_valid |= VALID_TOPLINE;
2397 }
2398}
2399
2400/*
2401 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2402 * screen lines for text lines.
2403 */
2404 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407#ifdef FEAT_DIFF
2408 wp->w_filler_rows = 0;
2409#endif
2410 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002411 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 else
2413 {
2414 wp->w_empty_rows = wp->w_height - used;
2415#ifdef FEAT_DIFF
2416 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2417 {
2418 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2419 if (wp->w_empty_rows > wp->w_filler_rows)
2420 wp->w_empty_rows -= wp->w_filler_rows;
2421 else
2422 {
2423 wp->w_filler_rows = wp->w_empty_rows;
2424 wp->w_empty_rows = 0;
2425 }
2426 }
2427#endif
2428 }
2429}
2430
2431/*
2432 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002433 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2435 * This is messy stuff!!!
2436 */
2437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 int used;
2441 int scrolled = 0;
2442 int extra = 0;
2443 int i;
2444 linenr_T line_count;
2445 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002446 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 lineoff_T loff;
2448 lineoff_T boff;
2449#ifdef FEAT_DIFF
2450 int old_topfill = curwin->w_topfill;
2451 int fill_below_window;
2452#endif
2453 linenr_T old_botline = curwin->w_botline;
2454 linenr_T old_valid = curwin->w_valid;
2455 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002456 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002457 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002458 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 cln = curwin->w_cursor.lnum;
2461 if (set_topbot)
2462 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002463 int set_skipcol = FALSE;
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 used = 0;
2466 curwin->w_botline = cln + 1;
2467#ifdef FEAT_DIFF
2468 loff.fill = 0;
2469#endif
2470 for (curwin->w_topline = curwin->w_botline;
2471 curwin->w_topline > 1;
2472 curwin->w_topline = loff.lnum)
2473 {
2474 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002475 topline_back_winheight(&loff, FALSE);
2476 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002478 if (used + loff.height > curwin->w_height)
2479 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002480 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002481 {
2482 // 'smoothscroll' and 'wrap' are set. The above line is
2483 // too long to show in its entirety, so we show just a part
2484 // of it.
2485 if (used < curwin->w_height)
2486 {
2487 int plines_offset = used + loff.height
2488 - curwin->w_height;
2489 used = curwin->w_height;
2490#ifdef FEAT_DIFF
2491 curwin->w_topfill = loff.fill;
2492#endif
2493 curwin->w_topline = loff.lnum;
2494 curwin->w_skipcol = skipcol_from_plines(
2495 curwin, plines_offset);
2496 set_skipcol = TRUE;
2497 }
2498 }
2499 break;
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 used += loff.height;
2502#ifdef FEAT_DIFF
2503 curwin->w_topfill = loff.fill;
2504#endif
2505 }
2506 set_empty_rows(curwin, used);
2507 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2508 if (curwin->w_topline != old_topline
2509#ifdef FEAT_DIFF
2510 || curwin->w_topfill != old_topfill
2511#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002512 || set_skipcol
2513 || curwin->w_skipcol != 0)
2514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002516 if (set_skipcol)
2517 redraw_later(UPD_NOT_VALID);
2518 else
2519 reset_skipcol();
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522 else
2523 validate_botline();
2524
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#ifdef FEAT_DIFF
2527 used = plines_nofill(cln);
2528#else
2529 validate_cheight();
2530 used = curwin->w_cline_height;
2531#endif
2532
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002533 // If the cursor is on or below botline, we will at least scroll by the
2534 // height of the cursor line, which is "used". Correct for empty lines,
2535 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (cln >= curwin->w_botline)
2537 {
2538 scrolled = used;
2539 if (cln == curwin->w_botline)
2540 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002541 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002542 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002543 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002544 // Calculate how many screen lines the current top line of window
2545 // occupies. If it is occupying more than the entire window, we
2546 // need to scroll the additional clipped lines to scroll past the
2547 // top line before we can move on to the other lines.
2548 int top_plines =
2549#ifdef FEAT_DIFF
2550 plines_win_nofill
2551#else
2552 plines_win
2553#endif
2554 (curwin, curwin->w_topline, FALSE);
2555 int skip_lines = 0;
2556 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002557 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002558 {
fullwaywang8154e642023-06-24 21:58:09 +01002559 int width2 = width1 + curwin_col_off2();
2560 // similar formula is used in curs_columns()
2561 if (curwin->w_skipcol > width1)
2562 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2563 else if (curwin->w_skipcol > 0)
2564 skip_lines = 1;
2565
2566 top_plines -= skip_lines;
2567 if (top_plines > curwin->w_height)
2568 {
2569 scrolled += (top_plines - curwin->w_height);
2570 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002571 }
2572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
2575 /*
2576 * Stop counting lines to scroll when
2577 * - hitting start of the file
2578 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002579 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * - lines between botline and cursor have been counted
2581 */
2582#ifdef FEAT_FOLDING
2583 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2584#endif
2585 {
2586 loff.lnum = cln;
2587 boff.lnum = cln;
2588 }
2589#ifdef FEAT_DIFF
2590 loff.fill = 0;
2591 boff.fill = 0;
2592 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2593 - curwin->w_filler_rows;
2594#endif
2595
2596 while (loff.lnum > 1)
2597 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2599 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002601 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2603 && loff.lnum <= curwin->w_botline
2604#ifdef FEAT_DIFF
2605 && (loff.lnum < curwin->w_botline
2606 || loff.fill >= fill_below_window)
2607#endif
2608 )
2609 break;
2610
Bram Moolenaar85a20022019-12-21 18:25:54 +01002611 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002613 if (loff.height == MAXCOL)
2614 used = MAXCOL;
2615 else
2616 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (used > curwin->w_height)
2618 break;
2619 if (loff.lnum >= curwin->w_botline
2620#ifdef FEAT_DIFF
2621 && (loff.lnum > curwin->w_botline
2622 || loff.fill <= fill_below_window)
2623#endif
2624 )
2625 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 scrolled += loff.height;
2628 if (loff.lnum == curwin->w_botline
2629#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002630 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
2632 )
2633 scrolled -= curwin->w_empty_rows;
2634 }
2635
2636 if (boff.lnum < curbuf->b_ml.ml_line_count)
2637 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002638 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 botline_forw(&boff);
2640 used += boff.height;
2641 if (used > curwin->w_height)
2642 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002643 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2644 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
2646 extra += boff.height;
2647 if (boff.lnum >= curwin->w_botline
2648#ifdef FEAT_DIFF
2649 || (boff.lnum + 1 == curwin->w_botline
2650 && boff.fill > curwin->w_filler_rows)
2651#endif
2652 )
2653 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 scrolled += boff.height;
2656 if (boff.lnum == curwin->w_botline
2657#ifdef FEAT_DIFF
2658 && boff.fill == 0
2659#endif
2660 )
2661 scrolled -= curwin->w_empty_rows;
2662 }
2663 }
2664 }
2665 }
2666
Bram Moolenaar85a20022019-12-21 18:25:54 +01002667 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (scrolled <= 0)
2669 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002670 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else if (used > curwin->w_height)
2672 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 else
2675 {
2676 line_count = 0;
2677#ifdef FEAT_DIFF
2678 boff.fill = curwin->w_topfill;
2679#endif
2680 boff.lnum = curwin->w_topline - 1;
2681 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2682 {
2683 botline_forw(&boff);
2684 i += boff.height;
2685 ++line_count;
2686 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002687 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 line_count = 9999;
2689 }
2690
2691 /*
2692 * Scroll up if the cursor is off the bottom of the screen a bit.
2693 * Otherwise put it at 1/2 of the screen.
2694 */
2695 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002696 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002697 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002698 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002699 if (do_sms)
2700 scrollup(scrolled, TRUE); // TODO
2701 else
2702 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
2706 * If topline didn't change we need to restore w_botline and w_empty_rows
2707 * (we changed them).
2708 * If topline did change, update_screen() will set botline.
2709 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002710 if (curwin->w_topline == old_topline
2711 && curwin->w_skipcol == old_skipcol
2712 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 curwin->w_botline = old_botline;
2715 curwin->w_empty_rows = old_empty_rows;
2716 curwin->w_valid = old_valid;
2717 }
2718 curwin->w_valid |= VALID_TOPLINE;
2719}
2720
2721/*
2722 * Recompute topline to put the cursor halfway the window
2723 * If "atend" is TRUE, also put it halfway at the end of the file.
2724 */
2725 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002726scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 int above = 0;
2729 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002730 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731#ifdef FEAT_DIFF
2732 int topfill = 0;
2733#endif
2734 int below = 0;
2735 int used;
2736 lineoff_T loff;
2737 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002738#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002739 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002742#ifdef FEAT_PROP_POPUP
2743 // if the width changed this needs to be updated first
2744 may_update_popup_position();
2745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2747#ifdef FEAT_FOLDING
2748 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2749#endif
2750#ifdef FEAT_DIFF
2751 used = plines_nofill(loff.lnum);
2752 loff.fill = 0;
2753 boff.fill = 0;
2754#else
2755 used = plines(loff.lnum);
2756#endif
2757 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002758
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002759 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002760 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2761 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002762 {
2763 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002764 if (atend)
2765 {
2766 want_height = (curwin->w_height - used) / 2;
2767 used = 0;
2768 }
2769 else
2770 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002771 }
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 while (topline > 1)
2774 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002775 // If using smoothscroll, we can precisely scroll to the
2776 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002777 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002778 {
2779 topline_back_winheight(&loff, FALSE);
2780 if (loff.height == MAXCOL)
2781 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002782 used += loff.height;
2783 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002784 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002785 botline_forw(&boff);
2786 used += boff.height;
2787 }
2788 if (used > want_height)
2789 {
2790 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002791 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002792 topline = loff.lnum;
2793#ifdef FEAT_DIFF
2794 topfill = loff.fill;
2795#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002796 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002797 }
2798 break;
2799 }
2800 topline = loff.lnum;
2801#ifdef FEAT_DIFF
2802 topfill = loff.fill;
2803#endif
2804 continue;
2805 }
2806
2807 // If not using smoothscroll, we have to iteratively find how many
2808 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002809 // This may not be right in the middle if the lines'
2810 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002811
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002812 // Depending on "prefer_above" we add a line above or below first.
2813 // Loop twice to avoid duplicating code.
2814 int done = FALSE;
2815 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002817 if (prefer_above ? (round == 2 && below < above)
2818 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002820 // add a line below the cursor
2821 if (boff.lnum < curbuf->b_ml.ml_line_count)
2822 {
2823 botline_forw(&boff);
2824 used += boff.height;
2825 if (used > curwin->w_height)
2826 {
2827 done = TRUE;
2828 break;
2829 }
2830 below += boff.height;
2831 }
2832 else
2833 {
2834 ++below; // count a "~" line
2835 if (atend)
2836 ++used;
2837 }
2838 }
2839
2840 if (prefer_above ? (round == 1 && below >= above)
2841 : (round == 1 && below > above))
2842 {
2843 // add a line above the cursor
2844 topline_back(&loff);
2845 if (loff.height == MAXCOL)
2846 used = MAXCOL;
2847 else
2848 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002850 {
2851 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002853 }
2854 above += loff.height;
2855 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002857 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002861 if (done)
2862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#ifdef FEAT_FOLDING
2866 if (!hasFolding(topline, &curwin->w_topline, NULL))
2867#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002868 {
2869 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002870 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002871 || curwin->w_skipcol != 0)
2872 {
2873 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002874 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002875 {
2876 curwin->w_skipcol = skipcol;
2877 redraw_later(UPD_NOT_VALID);
2878 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002879 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002880 reset_skipcol();
2881 }
2882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#ifdef FEAT_DIFF
2884 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002885 if (old_topline > curwin->w_topline + curwin->w_height)
2886 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 check_topfill(curwin, FALSE);
2888#endif
2889 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2890 curwin->w_valid |= VALID_TOPLINE;
2891}
2892
2893/*
2894 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002895 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 * If not possible, put it at the same position as scroll_cursor_halfway().
2897 * When called topline must be valid!
2898 */
2899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002900cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002904 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 linenr_T botline;
2906 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002909 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 /*
2912 * How many lines we would like to have above/below the cursor depends on
2913 * whether the first/last line of the file is on screen.
2914 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002915 above_wanted = so;
2916 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002917 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 above_wanted = mouse_dragging - 1;
2920 below_wanted = mouse_dragging - 1;
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (curwin->w_topline == 1)
2923 {
2924 above_wanted = 0;
2925 max_off = curwin->w_height / 2;
2926 if (below_wanted > max_off)
2927 below_wanted = max_off;
2928 }
2929 validate_botline();
2930 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002931 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
2933 below_wanted = 0;
2934 max_off = (curwin->w_height - 1) / 2;
2935 if (above_wanted > max_off)
2936 above_wanted = max_off;
2937 }
2938
2939 /*
2940 * If there are sufficient file-lines above and below the cursor, we can
2941 * return now.
2942 */
2943 cln = curwin->w_cursor.lnum;
2944 if (cln >= curwin->w_topline + above_wanted
2945 && cln < curwin->w_botline - below_wanted
2946#ifdef FEAT_FOLDING
2947 && !hasAnyFolding(curwin)
2948#endif
2949 )
2950 return;
2951
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002952 if (curwin->w_p_sms && !curwin->w_p_wrap)
2953 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002954 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002955 if (curwin->w_cline_height == curwin->w_height)
2956 {
2957 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002958 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002959 return;
2960 }
2961 // TODO: If the cursor line doesn't fit in the window then only adjust
2962 // w_skipcol.
2963 }
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 /*
2966 * Narrow down the area where the cursor can be put by taking lines from
2967 * the top and the bottom until:
2968 * - the desired context lines are found
2969 * - the lines from the top is past the lines from the bottom
2970 */
2971 topline = curwin->w_topline;
2972 botline = curwin->w_botline - 1;
2973#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002974 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 above = curwin->w_topfill;
2976 below = curwin->w_filler_rows;
2977#endif
2978 while ((above < above_wanted || below < below_wanted) && topline < botline)
2979 {
2980 if (below < below_wanted && (below <= above || above >= above_wanted))
2981 {
2982#ifdef FEAT_FOLDING
2983 if (hasFolding(botline, &botline, NULL))
2984 ++below;
2985 else
2986#endif
2987 below += plines(botline);
2988 --botline;
2989 }
2990 if (above < above_wanted && (above < below || below >= below_wanted))
2991 {
2992#ifdef FEAT_FOLDING
2993 if (hasFolding(topline, NULL, &topline))
2994 ++above;
2995 else
2996#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002997 above += PLINES_NOFILL(topline);
2998#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002999 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (topline < botline)
3001 above += diff_check_fill(curwin, topline + 1);
3002#endif
3003 ++topline;
3004 }
3005 }
3006 if (topline == botline || botline == 0)
3007 curwin->w_cursor.lnum = topline;
3008 else if (topline > botline)
3009 curwin->w_cursor.lnum = botline;
3010 else
3011 {
3012 if (cln < topline && curwin->w_topline > 1)
3013 {
3014 curwin->w_cursor.lnum = topline;
3015 curwin->w_valid &=
3016 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3017 }
3018 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3019 {
3020 curwin->w_cursor.lnum = botline;
3021 curwin->w_valid &=
3022 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3023 }
3024 }
3025 curwin->w_valid |= VALID_TOPLINE;
3026}
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028/*
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003029 * Decide how much overlap to use for page-up or page-down scrolling.
3030 * This is symmetric, so that doing both keeps the same lines displayed.
3031 * Three lines are examined:
3032 *
3033 * before CTRL-F after CTRL-F / before CTRL-B
3034 * etc. l1
3035 * l1 last but one line ------------
3036 * l2 last text line l2 top text line
3037 * ------------- l3 second text line
3038 * l3 etc.
3039 */
3040static int get_scroll_overlap(int dir)
3041{
3042 lineoff_T loff;
3043 int min_height = curwin->w_height - 2;
3044
3045 validate_botline();
3046 if (dir == FORWARD && curwin->w_botline > curbuf->b_ml.ml_line_count)
3047 return min_height + 2; // no overlap, still handle 'smoothscroll'
3048
3049 loff.lnum = dir == FORWARD ? curwin->w_botline : curwin->w_topline - 1;
3050#ifdef FEAT_DIFF
3051 loff.fill = diff_check_fill(curwin, loff.lnum + dir == BACKWARD)
3052 - (dir == FORWARD ? curwin->w_filler_rows : curwin->w_topfill);
3053 loff.height = loff.fill > 0 ? 1 : plines_nofill(loff.lnum);
3054#else
3055 loff.height = plines(loff.lnum);
3056#endif
3057
3058 int h1 = loff.height;
3059 if (h1 > min_height)
3060 return min_height + 2; // no overlap
3061 if (dir == FORWARD)
3062 topline_back(&loff);
3063 else
3064 botline_forw(&loff);
3065
3066 int h2 = loff.height;
3067 if (h2 == MAXCOL || h2 + h1 > min_height)
3068 return min_height + 2; // no overlap
3069 if (dir == FORWARD)
3070 topline_back(&loff);
3071 else
3072 botline_forw(&loff);
3073
3074 int h3 = loff.height;
3075 if (h3 == MAXCOL || h3 + h2 > min_height)
3076 return min_height + 2; // no overlap
3077 if (dir == FORWARD)
3078 topline_back(&loff);
3079 else
3080 botline_forw(&loff);
3081
3082 int h4 = loff.height;
3083 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
3084 return min_height + 1; // 1 line overlap
3085 else
3086 return min_height; // 2 lines overlap
3087}
3088
3089/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003090 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3091 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003093 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
3095 int
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003096pagescroll(int dir, long count, int half)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003098#ifdef FEAT_DIFF
3099 int prev_topfill = curwin->w_topfill;
3100#endif
3101 linenr_T prev_topline = curwin->w_topline;
3102 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003104 if (half)
3105 {
3106 // Scroll [count], 'scroll' or current window height lines.
3107 if (count)
3108 curwin->w_p_scr = MIN(curwin->w_height, count);
3109 count = MIN(curwin->w_height, curwin->w_p_scr);
3110 }
3111 else
3112 // Scroll 'window' or current window height lines.
3113 count *= ((ONE_WINDOW && p_window > 0 && p_window < Rows - 1) ?
3114 p_window - 2 : get_scroll_overlap(dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003116 if (curwin->w_p_sms)
3117 scroll_redraw(dir == FORWARD, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003119 {
3120 // Scroll at least one full line without 'smoothscroll'.
3121#ifdef FEAT_DIFF
3122 count -= plines_nofill(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#else
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003124 count -= plines(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#endif
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003126 scroll_redraw(dir == FORWARD, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003128 // Temporarily set 'smoothscroll' so that scrolling count lines
3129 // does not skip over parts of the buffer with wrapped lines.
3130 curwin->w_p_sms = TRUE;
3131 if (count > 0)
3132 scroll_redraw(dir == FORWARD, count);
3133 curwin->w_p_sms = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003135#ifdef FEAT_FOLDING
3136 // Move cursor to first line of closed fold.
3137 foldAdjustCursor();
3138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003140 int nochange = curwin->w_topline == prev_topline
3141#ifdef FEAT_DIFF
3142 && curwin->w_topfill == prev_topfill
3143#endif
3144 && curwin->w_skipcol == prev_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003146 // Error if the viewport did not change and the cursor is already
3147 // at the boundary.
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003148 if (nochange)
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01003149 {
3150 int prev_cursor = curwin->w_cursor.lnum;
3151 curwin->w_cursor.lnum += (count + 1) * (dir == FORWARD ? 1 : -1);
3152 check_cursor();
3153 if (curwin->w_cursor.lnum == prev_cursor)
3154 beep_flush();
3155 }
Luuk van Baalb9f5b952024-03-26 18:46:45 +01003156 else if (!curwin->w_p_sms || curwin->w_skipcol == prev_skipcol)
3157 beginline(BL_SOL | BL_FIX);
3158
3159 return nochange;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160}
3161
Bram Moolenaar860cae12010-06-05 23:22:07 +02003162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003163do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003164{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003165 static win_T *prev_curwin = NULL;
3166 static pos_T prev_cursor = {0, 0, 0};
3167
3168 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3169 return;
3170 prev_curwin = curwin;
3171 prev_cursor = curwin->w_cursor;
3172
Bram Moolenaar860cae12010-06-05 23:22:07 +02003173 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003174 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003175 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003176 colnr_T curswant = curwin->w_curswant;
3177 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003178 win_T *old_curwin = curwin;
3179 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003180 int old_VIsual_select = VIsual_select;
3181 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003182
3183 /*
3184 * loop through the cursorbound windows
3185 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003186 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003187 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003188 {
3189 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003190 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003191 if (curwin != old_curwin && curwin->w_p_crb)
3192 {
3193# ifdef FEAT_DIFF
3194 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003195 curwin->w_cursor.lnum =
3196 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003197 else
3198# endif
3199 curwin->w_cursor.lnum = line;
3200 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003201 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003202 curwin->w_curswant = curswant;
3203 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003204
Bram Moolenaar85a20022019-12-21 18:25:54 +01003205 // Make sure the cursor is in a valid position. Temporarily set
3206 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003207 int restart_edit_save = restart_edit;
3208 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003209 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003210
3211 // Avoid a scroll here for the cursor position, 'scrollbind' is
3212 // more important.
3213 if (!curwin->w_p_scb)
3214 validate_cursor();
3215
Bram Moolenaar61452852011-02-01 18:01:11 +01003216 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003217 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003218 if (has_mbyte)
3219 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003220 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003221
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003223 if (!curwin->w_p_scb)
3224 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003225 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003226 }
3227 }
3228
3229 /*
3230 * reset current-window
3231 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003232 VIsual_select = old_VIsual_select;
3233 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003234 curwin = old_curwin;
3235 curbuf = old_curbuf;
3236}