blob: 5d2fece63a77bcd4c178c0fbfdaca55c2c133795 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
zeertzjq6235a102023-08-19 14:12:42 +020039 * Get the number of screen lines skipped with "wp->w_skipcol".
Bram Moolenaarf6196f42022-10-02 21:29:55 +010040 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
zeertzjq6235a102023-08-19 14:12:42 +020042adjust_plines_for_skipcol(win_T *wp)
Bram Moolenaarf6196f42022-10-02 21:29:55 +010043{
44 if (wp->w_skipcol == 0)
zeertzjq6235a102023-08-19 14:12:42 +020045 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010046
Bram Moolenaarf6196f42022-10-02 21:29:55 +010047 int width = wp->w_width - win_col_off(wp);
Christian Brabandtcb0b99f2023-11-14 20:05:59 +010048 int w2 = width + win_col_off2(wp);
49 if (wp->w_skipcol >= width && w2 > 0)
50 return (wp->w_skipcol - width) / w2 + 1;
zeertzjq6235a102023-08-19 14:12:42 +020051
52 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010053}
54
55/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010056 * Return how many lines "lnum" will take on the screen, taking into account
57 * whether it is the first line, whether w_skipcol is non-zero and limiting to
58 * the window height.
59 */
60 static int
61plines_correct_topline(win_T *wp, linenr_T lnum)
62{
63 int n;
64#ifdef FEAT_DIFF
65 if (lnum == wp->w_topline)
66 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
67 else
68#endif
69 n = plines_win(wp, lnum, FALSE);
70 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020071 n -= adjust_plines_for_skipcol(wp);
Bram Moolenaard5337ef2022-10-20 20:15:47 +010072 if (n > wp->w_height)
73 n = wp->w_height;
74 return n;
75}
76
77/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Compute wp->w_botline for the current wp->w_topline. Can be called after
79 * wp->w_topline changed.
80 */
81 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010082comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int n;
85 linenr_T lnum;
86 int done;
87#ifdef FEAT_FOLDING
88 linenr_T last;
89 int folded;
90#endif
91
92 /*
93 * If w_cline_row is valid, start there.
94 * Otherwise have to start at w_topline.
95 */
96 check_cursor_moved(wp);
97 if (wp->w_valid & VALID_CROW)
98 {
99 lnum = wp->w_cursor.lnum;
100 done = wp->w_cline_row;
101 }
102 else
103 {
104 lnum = wp->w_topline;
105 done = 0;
106 }
107
108 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
109 {
110#ifdef FEAT_FOLDING
111 last = lnum;
112 folded = FALSE;
113 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
114 {
115 n = 1;
116 folded = TRUE;
117 }
118 else
119#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100120 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100121 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 if (
124#ifdef FEAT_FOLDING
125 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
126#else
127 lnum == wp->w_cursor.lnum
128#endif
129 )
130 {
131 wp->w_cline_row = done;
132 wp->w_cline_height = n;
133#ifdef FEAT_FOLDING
134 wp->w_cline_folded = folded;
135#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100136 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
138 }
139 if (done + n > wp->w_height)
140 break;
141 done += n;
142#ifdef FEAT_FOLDING
143 lnum = last;
144#endif
145 }
146
Bram Moolenaar85a20022019-12-21 18:25:54 +0100147 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 wp->w_botline = lnum;
149 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
150
151 set_empty_rows(wp, done);
152}
153
154/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100155 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
156 * set.
157 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100159redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160{
161 if ((wp->w_p_rnu
162#ifdef FEAT_SYN_HL
163 || wp->w_p_cul
164#endif
165 )
166 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200167 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200168 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000169 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100170 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200171 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100172}
173
zeertzjq3e559cd2022-03-27 19:26:55 +0100174#ifdef FEAT_SYN_HL
175/*
176 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
177 * contains "screenline".
178 */
179 static void
180redraw_for_cursorcolumn(win_T *wp)
181{
182 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
183 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100184 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100185 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100186 redraw_win_later(wp, UPD_SOME_VALID);
187 // When 'cursorlineopt' contains "screenline" need to redraw with
188 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100189 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100190 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100191 }
192}
193#endif
194
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100195/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100196 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
197 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000198 * Parameter "extra2" should be the padding on the 2nd line, not the first
199 * line.
200 * Returns the number of columns of overlap with buffer text, excluding the
201 * extra padding on the ledge.
202 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100203 int
204sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000205{
206#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100207 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000208 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100209 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000210 return 0;
211#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
213 if (wp->w_p_list && wp->w_lcs_chars.prec)
214 return 1;
215
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000216 return extra2 > 3 ? 0 : 3 - extra2;
217}
218
219/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000220 * Calculates the skipcol offset for window "wp" given how many
221 * physical lines we want to scroll down.
222 */
223 static int
224skipcol_from_plines(win_T *wp, int plines_off)
225{
226 int width1 = wp->w_width - win_col_off(wp);
227
228 int skipcol = 0;
229 if (plines_off > 0)
230 skipcol += width1;
231 if (plines_off > 1)
232 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
233 return skipcol;
234}
235
236/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100237 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000238 */
239 static void
240reset_skipcol(void)
241{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000242 if (curwin->w_skipcol == 0)
243 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000244
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000245 curwin->w_skipcol = 0;
246
247 // Should use the least expensive way that displays all that changed.
248 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
249 // enough when the top line gets another screen line.
250 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000251}
252
253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 * Update curwin->w_topline and redraw if necessary.
255 * Used to update the screen before printing a message.
256 */
257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100258update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 update_topline();
261 if (must_redraw)
262 update_screen(0);
263}
264
265/*
266 * Update curwin->w_topline to move the cursor onto the screen.
267 */
268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100269update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270{
271 long line_count;
272 int halfheight;
273 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#ifdef FEAT_FOLDING
275 linenr_T lnum;
276#endif
277 int check_topline = FALSE;
278 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100279 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100280 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100282 // Cursor is updated instead when this is TRUE for 'splitkeep'.
283 if (skip_update_topline)
284 return;
285
Bram Moolenaar85a20022019-12-21 18:25:54 +0100286 // If there is no valid screen and when the window height is zero just use
287 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200288 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200289 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100290 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200291 curwin->w_topline = curwin->w_cursor.lnum;
292 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200293 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 return;
295 }
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 check_cursor_moved(curwin);
298 if (curwin->w_valid & VALID_TOPLINE)
299 return;
300
Bram Moolenaar85a20022019-12-21 18:25:54 +0100301 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000302 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100303 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100305 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100307 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#endif
309
310 /*
311 * If the buffer is empty, always set topline to 1.
312 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 {
315 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100316 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 curwin->w_botline = 2;
319 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 }
322
323 /*
324 * If the cursor is above or near the top of the window, scroll the window
325 * to show the line the cursor is in, with 'scrolloff' context.
326 */
327 else
328 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100329 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100331 // If the cursor is above topline, scrolling is always needed.
332 // If the cursor is far below topline and there is no folding,
333 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 if (curwin->w_cursor.lnum < curwin->w_topline)
335 check_topline = TRUE;
336 else if (check_top_offset())
337 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100338 else if (curwin->w_skipcol > 0
339 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100340 {
341 colnr_T vcol;
342
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000343 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100344 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100345 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100346 int overlap = sms_marker_overlap(curwin, curwin_col_off()
347 - curwin_col_off2());
348 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100349 check_topline = TRUE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
355 curwin->w_topline))
356 check_topline = TRUE;
357#endif
358
359 if (check_topline)
360 {
361 halfheight = curwin->w_height / 2 - 1;
362 if (halfheight < 2)
363 halfheight = 2;
364
365#ifdef FEAT_FOLDING
366 if (hasAnyFolding(curwin))
367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100368 // Count the number of logical lines between the cursor and
369 // topline + scrolloff (approximation of how much will be
370 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 n = 0;
372 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
378 break;
379 (void)hasFolding(lnum, NULL, &lnum);
380 }
381 }
382 else
383#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100384 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaar85a20022019-12-21 18:25:54 +0100386 // If we weren't very close to begin with, we scroll to put the
387 // cursor in the middle of the window. Otherwise put the cursor
388 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000390 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 else
392 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 check_botline = TRUE;
395 }
396 }
397
398 else
399 {
400#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100401 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
403#endif
404 check_botline = TRUE;
405 }
406 }
407
408 /*
409 * If the cursor is below the bottom of the window, scroll the window
410 * to put the cursor on the window.
411 * When w_botline is invalid, recompute it first, to avoid a redraw later.
412 * If w_botline was approximated, we might need a redraw later in a few
413 * cases, but we don't want to spend (a lot of) time recomputing w_botline
414 * for every small change.
415 */
416 if (check_botline)
417 {
418 if (!(curwin->w_valid & VALID_BOTLINE_AP))
419 validate_botline();
420
421 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
422 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000423 if (curwin->w_cursor.lnum < curwin->w_botline)
424 {
425 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100426 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#ifdef FEAT_FOLDING
428 || hasAnyFolding(curwin)
429#endif
430 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 lineoff_T loff;
433
Bram Moolenaar85a20022019-12-21 18:25:54 +0100434 // Cursor is (a few lines) above botline, check if there are
435 // 'scrolloff' window lines below the cursor. If not, need to
436 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 n = curwin->w_empty_rows;
438 loff.lnum = curwin->w_cursor.lnum;
439#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100440 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
442#endif
443#ifdef FEAT_DIFF
444 loff.fill = 0;
445 n += curwin->w_filler_rows;
446#endif
447 loff.height = 0;
448 while (loff.lnum < curwin->w_botline
449#ifdef FEAT_DIFF
450 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
451#endif
452 )
453 {
454 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100455 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 break;
457 botline_forw(&loff);
458 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100459 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100460 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000462 }
463 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100464 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000465 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 }
467 if (check_botline)
468 {
469#ifdef FEAT_FOLDING
470 if (hasAnyFolding(curwin))
471 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100472 // Count the number of logical lines between the cursor and
473 // botline - scrolloff (approximation of how much will be
474 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 line_count = 0;
476 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100477 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {
479 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (lnum <= 0 || line_count > curwin->w_height + 1)
482 break;
483 (void)hasFolding(lnum, &lnum, NULL);
484 }
485 }
486 else
487#endif
488 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100489 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000491 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000493 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495 }
496 }
497 curwin->w_valid |= VALID_TOPLINE;
498
499 /*
500 * Need to redraw when topline changed.
501 */
502 if (curwin->w_topline != old_topline
503#ifdef FEAT_DIFF
504 || curwin->w_topfill != old_topfill
505#endif
506 )
507 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100508 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000509 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100510
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100511 // When 'smoothscroll' is not set, should reset w_skipcol.
512 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100513 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100514 else if (curwin->w_skipcol != 0)
515 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000516
Bram Moolenaar85a20022019-12-21 18:25:54 +0100517 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (curwin->w_cursor.lnum == curwin->w_topline)
519 validate_cursor();
520 }
521
Bram Moolenaar375e3392019-01-31 18:26:10 +0100522 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523}
524
525/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000526 * Return the scrolljump value to use for the current window.
527 * When 'scrolljump' is positive use it as-is.
528 * When 'scrolljump' is negative use it as a percentage of the window height.
529 */
530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100531scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000532{
533 if (p_sj >= 0)
534 return (int)p_sj;
535 return (curwin->w_height * -p_sj) / 100;
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
540 * current window.
541 */
542 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 lineoff_T loff;
546 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100547 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar375e3392019-01-31 18:26:10 +0100549 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#ifdef FEAT_FOLDING
551 || hasAnyFolding(curwin)
552#endif
553 )
554 {
555 loff.lnum = curwin->w_cursor.lnum;
556#ifdef FEAT_DIFF
557 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100558 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#else
560 n = 0;
561#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100562 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100563 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (loff.lnum < curwin->w_topline
568#ifdef FEAT_DIFF
569 || (loff.lnum == curwin->w_topline && loff.fill > 0)
570#endif
571 )
572 break;
573 n += loff.height;
574 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100575 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 return TRUE;
577 }
578 return FALSE;
579}
580
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100581/*
582 * Update w_curswant.
583 */
584 void
585update_curswant_force(void)
586{
587 validate_virtcol();
588 curwin->w_curswant = curwin->w_virtcol
589#ifdef FEAT_PROP_POPUP
590 - curwin->w_virtcol_first_char
591#endif
592 ;
593 curwin->w_set_curswant = FALSE;
594}
595
596/*
597 * Update w_curswant if w_set_curswant is set.
598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100600update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100603 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604}
605
606/*
607 * Check if the cursor has moved. Set the w_valid flag accordingly.
608 */
609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100610check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
613 {
614 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100615 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
616 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 wp->w_valid_cursor = wp->w_cursor;
618 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100619 wp->w_valid_skipcol = wp->w_skipcol;
620 }
621 else if (wp->w_skipcol != wp->w_valid_skipcol)
622 {
623 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
624 |VALID_CHEIGHT|VALID_CROW
625 |VALID_BOTLINE|VALID_BOTLINE_AP);
626 wp->w_valid_cursor = wp->w_cursor;
627 wp->w_valid_leftcol = wp->w_leftcol;
628 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
630 else if (wp->w_cursor.col != wp->w_valid_cursor.col
631 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100632 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {
634 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
635 wp->w_valid_cursor.col = wp->w_cursor.col;
636 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639}
640
641/*
642 * Call this function when some window settings have changed, which require
643 * the cursor position, botline and topline to be recomputed and the window to
644 * be redrawn. E.g, when changing the 'wrap' option or folding.
645 */
646 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100647changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 changed_window_setting_win(curwin);
650}
651
652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100653changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 wp->w_lines_valid = 0;
656 changed_line_abv_curs_win(wp);
657 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100658 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659}
660
Dominique Pellee764d1b2023-03-12 21:20:59 +0000661#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000663 * Call changed_window_setting_win() for every window containing "buf".
664 */
665 void
666changed_window_setting_buf(buf_T *buf)
667{
668 tabpage_T *tp;
669 win_T *wp;
670
671 FOR_ALL_TAB_WINDOWS(tp, wp)
672 if (wp->w_buffer == buf)
673 changed_window_setting_win(wp);
674}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000675#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000676
677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 * Set wp->w_topline to a certain number.
679 */
680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100681set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200683#ifdef FEAT_DIFF
684 linenr_T prev_topline = wp->w_topline;
685#endif
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100688 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
690#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100691 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100693 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
694 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000696 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200698 if (lnum != prev_topline)
699 // Keep the filler lines when the topline didn't change.
700 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#endif
702 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100703 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100704 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705}
706
707/*
708 * Call this function when the length of the cursor line (in screen
709 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100710 * If the line length changed the number of screen lines might change,
711 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 * Need to take care of w_botline separately!
713 */
714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100715changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar85090142023-06-01 19:27:08 +0100717 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 |VALID_CHEIGHT|VALID_TOPLINE);
719}
720
721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100722changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
Bram Moolenaar85090142023-06-01 19:27:08 +0100724 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 |VALID_CHEIGHT|VALID_TOPLINE);
726}
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728/*
729 * Call this function when the length of a line (in screen characters) above
730 * the cursor have changed.
731 * Need to take care of w_botline separately!
732 */
733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100734changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
737 |VALID_CHEIGHT|VALID_TOPLINE);
738}
739
740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
744 |VALID_CHEIGHT|VALID_TOPLINE);
745}
746
Dominique Pellee764d1b2023-03-12 21:20:59 +0000747#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100749 * Display of line has changed for "buf", invalidate cursor position and
750 * w_botline.
751 */
752 void
753changed_line_display_buf(buf_T *buf)
754{
755 win_T *wp;
756
757 FOR_ALL_WINDOWS(wp)
758 if (wp->w_buffer == buf)
759 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
760 |VALID_CROW|VALID_CHEIGHT
761 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
762}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000763#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100764
765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * Make sure the value of curwin->w_botline is valid.
767 */
768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100769validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100771 validate_botline_win(curwin);
772}
773
774/*
775 * Make sure the value of wp->w_botline is valid.
776 */
777 void
778validate_botline_win(win_T *wp)
779{
780 if (!(wp->w_valid & VALID_BOTLINE))
781 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782}
783
784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * Mark curwin->w_botline as invalid (because of some change in the buffer).
786 */
787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100788invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
790 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
791}
792
793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100794invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
797}
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100800approximate_botline_win(
801 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
803 wp->w_valid &= ~VALID_BOTLINE;
804}
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
807 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
808 */
809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100810cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 check_cursor_moved(curwin);
813 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
814 (VALID_WROW|VALID_WCOL));
815}
816
817/*
818 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
819 * w_topline must be valid, you may need to call update_topline() first!
820 */
821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100822validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100824 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 check_cursor_moved(curwin);
826 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
827 curs_columns(TRUE);
828}
829
830#if defined(FEAT_GUI) || defined(PROTO)
831/*
832 * validate w_cline_row.
833 */
834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100835validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
837 /*
838 * First make sure that w_topline is valid (after moving the cursor).
839 */
840 update_topline();
841 check_cursor_moved(curwin);
842 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100843 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845#endif
846
847/*
848 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200849 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 */
851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 linenr_T lnum;
855 int i;
856 int all_invalid;
857 int valid;
858#ifdef FEAT_FOLDING
859 long fold_count;
860#endif
861
Bram Moolenaar85a20022019-12-21 18:25:54 +0100862 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 all_invalid = (!redrawing()
864 || wp->w_lines_valid == 0
865 || wp->w_lines[0].wl_lnum > wp->w_topline);
866 i = 0;
867 wp->w_cline_row = 0;
868 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
869 {
870 valid = FALSE;
871 if (!all_invalid && i < wp->w_lines_valid)
872 {
873 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100874 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (wp->w_lines[i].wl_lnum == lnum)
876 {
877#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 // Check for newly inserted lines below this row, in which
879 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (!wp->w_buffer->b_mod_set
881 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
882 || wp->w_buffer->b_mod_top
883 > wp->w_lines[i].wl_lastlnum + 1)
884#endif
885 valid = TRUE;
886 }
887 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100888 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
890 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100891 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100893 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100895 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897#ifdef FEAT_FOLDING
898 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100899 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (lnum > wp->w_cursor.lnum)
901 break;
902#else
903 ++lnum;
904#endif
905 wp->w_cline_row += wp->w_lines[i].wl_size;
906 }
907 else
908 {
909#ifdef FEAT_FOLDING
910 fold_count = foldedCount(wp, lnum, NULL);
911 if (fold_count)
912 {
913 lnum += fold_count;
914 if (lnum > wp->w_cursor.lnum)
915 break;
916 ++wp->w_cline_row;
917 }
918 else
919#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100920 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100921 wp->w_cline_row += plines_correct_topline(wp, lnum);
922 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925 }
926
927 check_cursor_moved(wp);
928 if (!(wp->w_valid & VALID_CHEIGHT))
929 {
930 if (all_invalid
931 || i == wp->w_lines_valid
932 || (i < wp->w_lines_valid
933 && (!wp->w_lines[i].wl_valid
934 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
935 {
936#ifdef FEAT_DIFF
937 if (wp->w_cursor.lnum == wp->w_topline)
938 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
939 TRUE) + wp->w_topfill;
940 else
941#endif
942 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
943#ifdef FEAT_FOLDING
944 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
945 NULL, NULL, TRUE, NULL);
946#endif
947 }
948 else if (i > wp->w_lines_valid)
949 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100950 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 wp->w_cline_height = 0;
952#ifdef FEAT_FOLDING
953 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
954 NULL, NULL, TRUE, NULL);
955#endif
956 }
957 else
958 {
959 wp->w_cline_height = wp->w_lines[i].wl_size;
960#ifdef FEAT_FOLDING
961 wp->w_cline_folded = wp->w_lines[i].wl_folded;
962#endif
963 }
964 }
965
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100966 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Validate curwin->w_virtcol only.
972 */
973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100974validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 validate_virtcol_win(curwin);
977}
978
979/*
980 * Validate wp->w_virtcol only.
981 */
982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100983validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000986
987 if (wp->w_valid & VALID_VIRTCOL)
988 return;
989
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100990#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000991 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100992#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000993 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000994#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000995 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000996#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000997 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998}
999
1000/*
1001 * Validate curwin->w_cline_height only.
1002 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001004validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001007
1008 if (curwin->w_valid & VALID_CHEIGHT)
1009 return;
1010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001012 if (curwin->w_cursor.lnum == curwin->w_topline)
1013 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1014 + curwin->w_topfill;
1015 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001017 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001019 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001021 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001025 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 */
1027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001028validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 colnr_T off;
1031 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001032 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001036 if (curwin->w_valid & VALID_WCOL)
1037 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001038
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001039 col = curwin->w_virtcol;
1040 off = curwin_col_off();
1041 col += off;
1042 width = curwin->w_width - off + curwin_col_off2();
1043
1044 // long line wrapping, adjust curwin->w_wrow
1045 if (curwin->w_p_wrap
1046 && col >= (colnr_T)curwin->w_width
1047 && width > 0)
1048 // use same formula as what is used in curs_columns()
1049 col -= ((col - curwin->w_width) / width + 1) * width;
1050 if (col > (int)curwin->w_leftcol)
1051 col -= curwin->w_leftcol;
1052 else
1053 col = 0;
1054 curwin->w_wcol = col;
1055
1056 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001057#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001058 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001063 * Compute offset of a window, occupied by absolute or relative line number,
1064 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
1066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001067win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
Bram Moolenaar64486672010-05-16 15:46:46 +02001069 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Sean Dewar988f7432023-08-16 14:17:36 +01001070 + (wp != cmdwin_win ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071#ifdef FEAT_FOLDING
1072 + wp->w_p_fdc
1073#endif
1074#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001075 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
1077 );
1078}
1079
1080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001081curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 return win_col_off(curwin);
1084}
1085
1086/*
1087 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001088 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1089 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
1091 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001092win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar64486672010-05-16 15:46:46 +02001094 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001095 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 return 0;
1097}
1098
1099 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001100curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 return win_col_off2(curwin);
1103}
1104
1105/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001106 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 * Also updates curwin->w_wrow and curwin->w_cline_row.
1108 * Also updates curwin->w_leftcol.
1109 */
1110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001111curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001112 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001115 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 int off_left, off_right;
1117 int n;
1118 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001119 int width1; // text width for first screen line
1120 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 int new_leftcol;
1122 colnr_T startcol;
1123 colnr_T endcol;
1124 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001125 long so = get_scrolloff_value();
1126 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001127 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 /*
1130 * First make sure that w_topline is valid (after moving the cursor).
1131 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001132 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 /*
1135 * Next make sure that w_cline_row is valid.
1136 */
1137 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001138 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001140#ifdef FEAT_PROP_POPUP
1141 // will be set by getvvcol() but not reset
1142 curwin->w_virtcol_first_char = 0;
1143#endif
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /*
1146 * Compute the number of virtual columns.
1147 */
1148#ifdef FEAT_FOLDING
1149 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001150 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1152 else
1153#endif
1154 getvvcol(curwin, &curwin->w_cursor,
1155 &startcol, &(curwin->w_virtcol), &endcol);
1156
Bram Moolenaar85a20022019-12-21 18:25:54 +01001157 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001159 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 extra = curwin_col_off();
1162 curwin->w_wcol = curwin->w_virtcol + extra;
1163 endcol += extra;
1164
1165 /*
1166 * Now compute w_wrow, counting screen lines from w_cline_row.
1167 */
1168 curwin->w_wrow = curwin->w_cline_row;
1169
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001170 width1 = curwin->w_width - extra;
1171 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001174 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001175 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001176 if (curwin->w_p_wrap)
1177 curwin->w_wrow = curwin->w_height - 1;
1178 else
1179 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001181 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001183 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001185 // skip columns that are not visible
1186 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001187 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001188 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001189 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001190 // Deduct by multiples of width2. This allows the long line
1191 // wrapping formula below to correctly calculate the w_wcol value
1192 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001193 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001194 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001195 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001196 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001197 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001198
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001199 did_sub_skipcol = TRUE;
1200 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001201
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001203 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001206 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1207 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 }
1211
Bram Moolenaar85a20022019-12-21 18:25:54 +01001212 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1213 // is not folded.
1214 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001215 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#ifdef FEAT_FOLDING
1217 && !curwin->w_cline_folded
1218#endif
1219 )
1220 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001221#ifdef FEAT_PROP_POPUP
1222 if (curwin->w_virtcol_first_char > 0)
1223 {
1224 int cols = (curwin->w_width - extra);
1225 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1226
1227 // each "above" text prop shifts the text one row down
1228 curwin->w_wrow += rows;
1229 curwin->w_wcol -= rows * cols;
1230 endcol -= rows * cols;
1231 curwin->w_cline_height = rows + 1;
1232 }
1233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 /*
1235 * If Cursor is left of the screen, scroll rightwards.
1236 * If Cursor is right of the screen, scroll leftwards
1237 * If we get closer to the edge than 'sidescrolloff', scroll a little
1238 * extra
1239 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001240 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001241 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001242 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (off_left < 0 || off_right > 0)
1244 {
1245 if (off_left < 0)
1246 diff = -off_left;
1247 else
1248 diff = off_right;
1249
Bram Moolenaar85a20022019-12-21 18:25:54 +01001250 // When far off or not enough room on either side, put cursor in
1251 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001252 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1253 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 else
1255 {
1256 if (diff < p_ss)
1257 diff = p_ss;
1258 if (off_left < 0)
1259 new_leftcol = curwin->w_leftcol - diff;
1260 else
1261 new_leftcol = curwin->w_leftcol + diff;
1262 }
1263 if (new_leftcol < 0)
1264 new_leftcol = 0;
1265 if (new_leftcol != (int)curwin->w_leftcol)
1266 {
1267 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001269 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 }
1272 curwin->w_wcol -= curwin->w_leftcol;
1273 }
1274 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1275 curwin->w_wcol -= curwin->w_leftcol;
1276 else
1277 curwin->w_wcol = 0;
1278
1279#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001280 // Skip over filler lines. At the top use w_topfill, there
1281 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (curwin->w_cursor.lnum == curwin->w_topline)
1283 curwin->w_wrow += curwin->w_topfill;
1284 else
1285 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1286#endif
1287
1288 prev_skipcol = curwin->w_skipcol;
1289
1290 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if ((curwin->w_wrow >= curwin->w_height
1293 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001294 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 && (p_lines =
1296#ifdef FEAT_DIFF
1297 plines_win_nofill
1298#else
1299 plines_win
1300#endif
1301 (curwin, curwin->w_cursor.lnum, FALSE))
1302 - 1 >= curwin->w_height))
1303 && curwin->w_height != 0
1304 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001305 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001306 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001308 // Cursor past end of screen. Happens with a single line that does
1309 // not fit on screen. Find a skipcol to show the text around the
1310 // cursor. Avoid scrolling all the time. compute value of "extra":
1311 // 1: Less than 'scrolloff' lines above
1312 // 2: Less than 'scrolloff' lines below
1313 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001315 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001317 // Compute last display line of the buffer line that we want at the
1318 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (p_lines == 0)
1320 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1321 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001322 if (p_lines > curwin->w_wrow + so)
1323 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 else
1325 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001326 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 extra += 2;
1328
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001329 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001331 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001332 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (n > curwin->w_height / 2)
1334 n -= curwin->w_height / 2;
1335 else
1336 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (n > p_lines - curwin->w_height + 1)
1339 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001340 if (n > 0)
1341 curwin->w_skipcol = width1 + (n - 1) * width2;
1342 else
1343 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 else if (extra == 1)
1346 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001347 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001348 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1349 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (extra > 0)
1351 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001352 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1353 extra = curwin->w_skipcol / width2;
1354 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 }
1357 else if (extra == 2)
1358 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001359 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001360 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001362 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (endcol > curwin->w_skipcol)
1364 curwin->w_skipcol = endcol;
1365 }
1366
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001367 // adjust w_wrow for the changed w_skipcol
1368 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001369 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001370 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001371 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (curwin->w_wrow >= curwin->w_height)
1374 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001375 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 curwin->w_wrow -= extra;
1379 }
1380
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001381 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (extra > 0)
1383 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1384 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001385 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001387 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 curwin->w_skipcol = 0;
1389 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001390 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001392#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001393 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001394#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001395#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1396 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1397 {
1398 curwin->w_wrow += popup_top_extra(curwin);
1399 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001400 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001401 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001402 else
1403 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001404#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001405
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001406 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1407 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001408 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001409 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1412}
1413
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001414#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001415/*
1416 * Compute the screen position of text character at "pos" in window "wp"
1417 * The resulting values are one-based, zero when character is not visible.
1418 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001419 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001420textpos2screenpos(
1421 win_T *wp,
1422 pos_T *pos,
1423 int *rowp, // screen row
1424 int *scolp, // start screen column
1425 int *ccolp, // cursor screen column
1426 int *ecolp) // end screen column
1427{
1428 colnr_T scol = 0, ccol = 0, ecol = 0;
1429 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430 colnr_T coloff = 0;
1431
Bram Moolenaar189663b2021-07-21 18:04:56 +02001432 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001433 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001434 colnr_T col;
1435 int width;
1436 linenr_T lnum = pos->lnum;
1437#ifdef FEAT_FOLDING
1438 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001440 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1441#endif
zeertzjq6235a102023-08-19 14:12:42 +02001442 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE);
zeertzjqbfe377b2023-08-17 22:58:53 +02001443 // "row" should be the screen line where line "lnum" begins, which can
1444 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001445 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001446
1447#ifdef FEAT_DIFF
1448 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001449 row += lnum == wp->w_topline ? wp->w_topfill
1450 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001451#endif
1452
zeertzjqba2d1912022-12-18 12:28:59 +00001453 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001454#ifdef FEAT_FOLDING
1455 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456 {
zeertzjq6235a102023-08-19 14:12:42 +02001457 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001458 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001459 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001460 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001461#endif
1462 {
1463 getvcol(wp, pos, &scol, &ccol, &ecol);
1464
1465 // similar to what is done in validate_cursor_col()
1466 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001467 col += off;
1468 width = wp->w_width - off + win_col_off2(wp);
1469
1470 // long line wrapping, adjust row
1471 if (wp->w_p_wrap
1472 && col >= (colnr_T)wp->w_width
1473 && width > 0)
1474 {
1475 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001476 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001477 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001478 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001479 }
1480 col -= wp->w_leftcol;
1481 if (col >= wp->w_width)
1482 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001483 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001484 {
1485 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001486 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001487 }
1488 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001489 // character is out of the window
1490 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001491 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001492 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001493 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001494 *scolp = scol + coloff;
1495 *ccolp = ccol + coloff;
1496 *ecolp = ecol + coloff;
1497}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001498#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001499
Bram Moolenaar12034e22019-08-25 22:25:02 +02001500#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001501/*
1502 * "screenpos({winid}, {lnum}, {col})" function
1503 */
1504 void
1505f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1506{
1507 dict_T *dict;
1508 win_T *wp;
1509 pos_T pos;
1510 int row = 0;
1511 int scol = 0, ccol = 0, ecol = 0;
1512
Bram Moolenaar93a10962022-06-16 11:42:09 +01001513 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001514 return;
1515 dict = rettv->vval.v_dict;
1516
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001517 if (in_vim9script()
1518 && (check_for_number_arg(argvars, 0) == FAIL
1519 || check_for_number_arg(argvars, 1) == FAIL
1520 || check_for_number_arg(argvars, 2) == FAIL))
1521 return;
1522
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001523 wp = find_win_by_nr_or_id(&argvars[0]);
1524 if (wp == NULL)
1525 return;
1526
1527 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001528 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1529 {
1530 semsg(_(e_invalid_line_number_nr), pos.lnum);
1531 return;
1532 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001533 pos.col = tv_get_number(&argvars[2]) - 1;
zeertzjqec54af42023-12-12 16:43:44 +01001534 if (pos.col < 0)
1535 pos.col = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001536 pos.coladd = 0;
1537 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1538
1539 dict_add_number(dict, "row", row);
1540 dict_add_number(dict, "col", scol);
1541 dict_add_number(dict, "curscol", ccol);
1542 dict_add_number(dict, "endcol", ecol);
1543}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001544
1545/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001546 * Convert a virtual (screen) column to a character column. The first column
1547 * is one. For a multibyte character, the column number of the first byte is
1548 * returned.
1549 */
1550 static int
1551virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1552{
zeertzjqf5a94d52023-10-15 10:03:30 +02001553 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001554 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1555 char_u *p = line + offset;
1556
zeertzjqb583eda2023-10-14 11:32:28 +02001557 if (*p == NUL)
1558 {
1559 if (p == line) // empty line
1560 return 0;
1561 // Move to the first byte of the last char.
1562 MB_PTR_BACK(line, p);
1563 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001564 return (int)(p - line + 1);
1565}
1566
1567/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001568 * "virtcol2col({winid}, {lnum}, {col})" function
1569 */
1570 void
1571f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1572{
1573 win_T *wp;
1574 linenr_T lnum;
1575 int screencol;
1576 int error = FALSE;
1577
1578 rettv->vval.v_number = -1;
1579
1580 if (check_for_number_arg(argvars, 0) == FAIL
1581 || check_for_number_arg(argvars, 1) == FAIL
1582 || check_for_number_arg(argvars, 2) == FAIL)
1583 return;
1584
1585 wp = find_win_by_nr_or_id(&argvars[0]);
1586 if (wp == NULL)
1587 return;
1588
1589 lnum = tv_get_number_chk(&argvars[1], &error);
1590 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1591 return;
1592
1593 screencol = tv_get_number_chk(&argvars[2], &error);
1594 if (error || screencol < 0)
1595 return;
1596
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001597 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001598}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001599#endif
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601/*
1602 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001605scrolldown(
1606 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001609 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int wrow;
1611 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001612 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001613 int width1 = 0;
1614 int width2 = 0;
1615
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001616 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001617 {
1618 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001619 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622#ifdef FEAT_FOLDING
1623 linenr_T first;
1624
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1627#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001629 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001632 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1633 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 ++curwin->w_topfill;
1636 ++done;
1637 }
1638 else
1639#endif
1640 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001641 // break when at the very top
1642 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001643 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001645 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001647 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001648 if (curwin->w_skipcol >= width1 + width2)
1649 curwin->w_skipcol -= width2;
1650 else
1651 curwin->w_skipcol -= width1;
1652 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001656 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001657 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001658 --curwin->w_topline;
1659 curwin->w_skipcol = 0;
1660#ifdef FEAT_DIFF
1661 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001663#ifdef FEAT_FOLDING
1664 // A sequence of folded lines only counts for one logical line
1665 if (hasFolding(curwin->w_topline, &first, NULL))
1666 {
1667 ++done;
1668 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001669 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001670 curwin->w_botline -= curwin->w_topline - first;
1671 curwin->w_topline = first;
1672 }
1673 else
1674#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001675 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001676 {
1677 int size = win_linetabsize(curwin, curwin->w_topline,
1678 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1679 if (size > width1)
1680 {
1681 curwin->w_skipcol = width1;
1682 size -= width1;
1683 redraw_later(UPD_NOT_VALID);
1684 }
1685 while (size > width2)
1686 {
1687 curwin->w_skipcol += width2;
1688 size -= width2;
1689 }
1690 ++done;
1691 }
1692 else
1693 done += PLINES_NOFILL(curwin->w_topline);
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 invalidate_botline();
1698 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 curwin->w_wrow += done; // keep w_wrow updated
1700 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702#ifdef FEAT_DIFF
1703 if (curwin->w_cursor.lnum == curwin->w_topline)
1704 curwin->w_cline_row = 0;
1705 check_topfill(curwin, TRUE);
1706#endif
1707
1708 /*
1709 * Compute the row number of the last row of the cursor line
1710 * and move the cursor onto the displayed part of the window.
1711 */
1712 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001713 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
1715 validate_virtcol();
1716 validate_cheight();
1717 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001718 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1721 {
1722#ifdef FEAT_FOLDING
1723 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1724 {
1725 --wrow;
1726 if (first == 1)
1727 curwin->w_cursor.lnum = 1;
1728 else
1729 curwin->w_cursor.lnum = first - 1;
1730 }
1731 else
1732#endif
1733 wrow -= plines(curwin->w_cursor.lnum--);
1734 curwin->w_valid &=
1735 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1736 moved = TRUE;
1737 }
1738 if (moved)
1739 {
1740#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001741 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 foldAdjustCursor();
1743#endif
1744 coladvance(curwin->w_curswant);
1745 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001746
1747 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1748 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001749 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001750 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1751
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 // make sure the cursor is in the visible text
1753 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001754 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001755 int row = 0;
1756 if (col >= width1)
1757 {
1758 col -= width1;
1759 ++row;
1760 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001761 if (col > width2 && width2 > 0)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001762 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001763 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001764 // even so col is not used anymore,
1765 // make sure it is correct, just in case
1766 col = col % width2;
1767 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001768 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001769 {
1770 curwin->w_curswant = curwin->w_virtcol
1771 - (row - curwin->w_height + 1) * width2;
1772 coladvance(curwin->w_curswant);
1773 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775}
1776
1777/*
1778 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781scrollup(
1782 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001783 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001785 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001787 if (do_sms
1788# ifdef FEAT_FOLDING
1789 || (byfold && hasAnyFolding(curwin))
1790# endif
1791# ifdef FEAT_DIFF
1792 || (curwin->w_p_diff && !curwin->w_p_wrap)
1793# endif
1794 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001796 int width1 = curwin->w_width - curwin_col_off();
1797 int width2 = width1 + curwin_col_off2();
1798 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001799 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001801 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001802 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803
1804 // diff mode: first consume "topfill"
1805 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1806 // the line, then advance to the next line.
1807 // folding: count each sequence of folded lines as one logical line.
1808 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810# ifdef FEAT_DIFF
1811 if (curwin->w_topfill > 0)
1812 --curwin->w_topfill;
1813 else
1814# endif
1815 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001816 linenr_T lnum = curwin->w_topline;
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818# ifdef FEAT_FOLDING
1819 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001820 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 (void)hasFolding(lnum, NULL, &lnum);
1822# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001823 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001824 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001825 // 'smoothscroll': increase "w_skipcol" until it goes over
1826 // the end of the line, then advance to the next line.
1827 int add = curwin->w_skipcol > 0 ? width2 : width1;
1828 curwin->w_skipcol += add;
1829 if (curwin->w_skipcol >= size)
1830 {
1831 if (lnum == curbuf->b_ml.ml_line_count)
1832 {
1833 // at the last screen line, can't scroll further
1834 curwin->w_skipcol -= add;
1835 break;
1836 }
1837 ++lnum;
1838 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001839 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001840 else
1841 {
1842 if (lnum >= curbuf->b_ml.ml_line_count)
1843 break;
1844 ++lnum;
1845 }
1846
1847 if (lnum > curwin->w_topline)
1848 {
1849 // approximate w_botline
1850 curwin->w_botline += lnum - curwin->w_topline;
1851 curwin->w_topline = lnum;
1852# ifdef FEAT_DIFF
1853 curwin->w_topfill = diff_check_fill(curwin, lnum);
1854# endif
1855 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001856 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001857 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001858 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001859 }
1860 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001861
zeertzjqd9a92dc2023-06-05 18:41:35 +01001862 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1863 // need to redraw more, because wl_size of the (new) topline may
1864 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001865 redraw_later(UPD_NOT_VALID);
1866 }
1867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872
1873 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1874 curwin->w_topline = curbuf->b_ml.ml_line_count;
1875 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1876 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1877
1878#ifdef FEAT_DIFF
1879 check_topfill(curwin, FALSE);
1880#endif
1881
1882#ifdef FEAT_FOLDING
1883 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001884 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1886#endif
1887
1888 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1889 if (curwin->w_cursor.lnum < curwin->w_topline)
1890 {
1891 curwin->w_cursor.lnum = curwin->w_topline;
1892 curwin->w_valid &=
1893 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1894 coladvance(curwin->w_curswant);
1895 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001896 if (curwin->w_cursor.lnum == curwin->w_topline
1897 && do_sms && curwin->w_skipcol > 0)
1898 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001899 int col_off = curwin_col_off();
1900 int col_off2 = curwin_col_off2();
1901
1902 int width1 = curwin->w_width - col_off;
1903 int width2 = width1 + col_off2;
1904 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001905 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001906 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001907 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001908
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001909 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001910 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001911 int overlap = scrolloff_cols != 0 ? 0
1912 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001913
Bram Moolenaar118c2352022-10-09 17:19:27 +01001914 // Make sure the cursor is in a visible part of the line, taking
1915 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001916 // If there are not enough screen lines put the cursor in the middle.
1917 if (scrolloff_cols > space_cols / 2)
1918 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001920 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001921 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001922 colnr_T col = curwin->w_virtcol;
1923
1924 if (col < width1)
1925 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001926 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001927 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001928 curwin->w_curswant = col;
1929 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001930
1931 // validate_virtcol() marked various things as valid, but after
1932 // moving the cursor they need to be recomputed
1933 curwin->w_valid &=
1934 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001935 }
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937}
1938
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001939/*
1940 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1941 * valid for 'smoothscroll'.
1942 */
1943 void
1944adjust_skipcol(void)
1945{
1946 if (!curwin->w_p_wrap
1947 || !curwin->w_p_sms
1948 || curwin->w_cursor.lnum != curwin->w_topline)
1949 return;
1950
1951 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001952 if (width1 <= 0)
1953 return; // no text will be displayed
1954
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001955 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001956 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001957 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1958 int scrolled = FALSE;
1959
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001960 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001961 if (curwin->w_cline_height == curwin->w_height
1962 // w_cline_height may be capped at w_height, check there aren't
1963 // actually more lines.
1964 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1965 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001966 {
1967 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001968 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001969 return;
1970 }
1971
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001972 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001973 int overlap = sms_marker_overlap(curwin,
1974 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001975 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001976 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001977 {
1978 // scroll a screen line down
1979 if (curwin->w_skipcol >= width1 + width2)
1980 curwin->w_skipcol -= width2;
1981 else
1982 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001983 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001984 }
1985 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001986 {
1987 validate_virtcol();
1988 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001989 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001990 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001991
1992 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1993 int row = 0;
1994 if (col >= width1)
1995 {
1996 col -= width1;
1997 ++row;
1998 }
1999 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02002000 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002001 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02002002 // col may no longer be used, but make
2003 // sure it is correct anyhow, just in case
2004 col = col % width2;
2005 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002006 if (row >= curwin->w_height)
2007 {
2008 if (curwin->w_skipcol == 0)
2009 {
2010 curwin->w_skipcol += width1;
2011 --row;
2012 }
2013 if (row >= curwin->w_height)
2014 curwin->w_skipcol += (row - curwin->w_height) * width2;
2015 redraw_later(UPD_NOT_VALID);
2016 }
2017}
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#ifdef FEAT_DIFF
2020/*
2021 * Don't end up with too many filler lines in the window.
2022 */
2023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002024check_topfill(
2025 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027{
2028 int n;
2029
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002030 if (wp->w_topfill <= 0)
2031 return;
2032
2033 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2034 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002036 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002038 --wp->w_topline;
2039 wp->w_topfill = 0;
2040 }
2041 else
2042 {
2043 wp->w_topfill = wp->w_height - n;
2044 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 }
2048}
2049
2050/*
2051 * Use as many filler lines as possible for w_topline. Make sure w_topline
2052 * is still visible.
2053 */
2054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002055max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int n;
2058
2059 n = plines_nofill(curwin->w_topline);
2060 if (n >= curwin->w_height)
2061 curwin->w_topfill = 0;
2062 else
2063 {
2064 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2065 if (curwin->w_topfill + n > curwin->w_height)
2066 curwin->w_topfill = curwin->w_height - n;
2067 }
2068}
2069#endif
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071/*
2072 * Scroll the screen one line down, but don't do it if it would move the
2073 * cursor off the screen.
2074 */
2075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002076scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 int end_row;
2079#ifdef FEAT_DIFF
2080 int can_fill = (curwin->w_topfill
2081 < diff_check_fill(curwin, curwin->w_topline));
2082#endif
2083
2084 if (curwin->w_topline <= 1
2085#ifdef FEAT_DIFF
2086 && !can_fill
2087#endif
2088 )
2089 return;
2090
Bram Moolenaar85a20022019-12-21 18:25:54 +01002091 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 /*
2094 * Compute the row number of the last row of the cursor line
2095 * and make sure it doesn't go off the screen. Make sure the cursor
2096 * doesn't go past 'scrolloff' lines from the screen end.
2097 */
2098 end_row = curwin->w_wrow;
2099#ifdef FEAT_DIFF
2100 if (can_fill)
2101 ++end_row;
2102 else
2103 end_row += plines_nofill(curwin->w_topline - 1);
2104#else
2105 end_row += plines(curwin->w_topline - 1);
2106#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002107 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 validate_cheight();
2110 validate_virtcol();
2111 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002112 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002114 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116#ifdef FEAT_DIFF
2117 if (can_fill)
2118 {
2119 ++curwin->w_topfill;
2120 check_topfill(curwin, TRUE);
2121 }
2122 else
2123 {
2124 --curwin->w_topline;
2125 curwin->w_topfill = 0;
2126 }
2127#else
2128 --curwin->w_topline;
2129#endif
2130#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002131 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2135 }
2136}
2137
2138/*
2139 * Scroll the screen one line up, but don't do it if it would move the cursor
2140 * off the screen.
2141 */
2142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002143scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int start_row;
2146
2147 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2148#ifdef FEAT_DIFF
2149 && curwin->w_topfill == 0
2150#endif
2151 )
2152 return;
2153
Bram Moolenaar85a20022019-12-21 18:25:54 +01002154 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 /*
2157 * Compute the row number of the first row of the cursor line
2158 * and make sure it doesn't go off the screen. Make sure the cursor
2159 * doesn't go before 'scrolloff' lines from the screen start.
2160 */
2161#ifdef FEAT_DIFF
2162 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2163 - curwin->w_topfill;
2164#else
2165 start_row = curwin->w_wrow - plines(curwin->w_topline);
2166#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002167 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002170 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002172 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174#ifdef FEAT_DIFF
2175 if (curwin->w_topfill > 0)
2176 --curwin->w_topfill;
2177 else
2178#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002179 {
2180#ifdef FEAT_FOLDING
2181 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002184 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002185 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2187 }
2188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190/*
2191 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2192 * a (wrapped) text line. Uses and sets "lp->fill".
2193 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002194 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 */
2196 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002197topline_back_winheight(
2198 lineoff_T *lp,
2199 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201#ifdef FEAT_DIFF
2202 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 ++lp->fill;
2206 lp->height = 1;
2207 }
2208 else
2209#endif
2210 {
2211 --lp->lnum;
2212#ifdef FEAT_DIFF
2213 lp->fill = 0;
2214#endif
2215 if (lp->lnum < 1)
2216 lp->height = MAXCOL;
2217 else
2218#ifdef FEAT_FOLDING
2219 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 lp->height = 1;
2222 else
2223#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002224 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226}
2227
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002228 static void
2229topline_back(lineoff_T *lp)
2230{
2231 topline_back_winheight(lp, TRUE);
2232}
2233
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235/*
2236 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2237 * a (wrapped) text line. Uses and sets "lp->fill".
2238 * Returns the height of the added line in "lp->height".
2239 * Lines below the last one are incredibly high.
2240 */
2241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002242botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244#ifdef FEAT_DIFF
2245 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2246 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002247 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 ++lp->fill;
2249 lp->height = 1;
2250 }
2251 else
2252#endif
2253 {
2254 ++lp->lnum;
2255#ifdef FEAT_DIFF
2256 lp->fill = 0;
2257#endif
2258 if (lp->lnum > curbuf->b_ml.ml_line_count)
2259 lp->height = MAXCOL;
2260 else
2261#ifdef FEAT_FOLDING
2262 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 lp->height = 1;
2265 else
2266#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002267 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269}
2270
2271#ifdef FEAT_DIFF
2272/*
2273 * Switch from including filler lines below lp->lnum to including filler
2274 * lines above loff.lnum + 1. This keeps pointing to the same line.
2275 * When there are no filler lines nothing changes.
2276 */
2277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002278botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 if (lp->fill > 0)
2281 {
2282 ++lp->lnum;
2283 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2284 }
2285}
2286
2287/*
2288 * Switch from including filler lines above lp->lnum to including filler
2289 * lines below loff.lnum - 1. This keeps pointing to the same line.
2290 * When there are no filler lines nothing changes.
2291 */
2292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 if (lp->fill > 0)
2296 {
2297 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2298 --lp->lnum;
2299 }
2300}
2301#endif
2302
2303/*
2304 * Recompute topline to put the cursor at the top of the window.
2305 * Scroll at least "min_scroll" lines.
2306 * If "always" is TRUE, always set topline (for "zt").
2307 */
2308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002309scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 int scrolled = 0;
2312 int extra = 0;
2313 int used;
2314 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 linenr_T top; // just above displayed lines
2316 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002318 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#ifdef FEAT_DIFF
2320 linenr_T old_topfill = curwin->w_topfill;
2321#endif
2322 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002323 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (mouse_dragging > 0)
2326 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /*
2329 * Decrease topline until:
2330 * - it has become 1
2331 * - (part of) the cursor line is moved off the screen or
2332 * - moved at least 'scrolljump' lines and
2333 * - at least 'scrolloff' lines above and below the cursor
2334 */
2335 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (curwin->w_cursor.lnum < curwin->w_topline)
2338 scrolled = used;
2339
2340#ifdef FEAT_FOLDING
2341 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2342 {
2343 --top;
2344 ++bot;
2345 }
2346 else
2347#endif
2348 {
2349 top = curwin->w_cursor.lnum - 1;
2350 bot = curwin->w_cursor.lnum + 1;
2351 }
2352 new_topline = top + 1;
2353
2354#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002355 // "used" already contains the number of filler lines above, don't add it
2356 // again.
2357 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002358 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360
2361 /*
2362 * Check if the lines from "top" to "bot" fit in the window. If they do,
2363 * set new_topline and advance "top" and "bot" to include more lines.
2364 */
2365 while (top > 0)
2366 {
2367#ifdef FEAT_FOLDING
2368 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 i = 1;
2371 else
2372#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002373 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002374 if (top < curwin->w_topline)
2375 scrolled += i;
2376
2377 // If scrolling is needed, scroll at least 'sj' lines.
2378 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2379 && extra >= off)
2380 break;
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 used += i;
2383 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2384 {
2385#ifdef FEAT_FOLDING
2386 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 ++used;
2389 else
2390#endif
2391 used += plines(bot);
2392 }
2393 if (used > curwin->w_height)
2394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 extra += i;
2397 new_topline = top;
2398 --top;
2399 ++bot;
2400 }
2401
2402 /*
2403 * If we don't have enough space, put cursor in the middle.
2404 * This makes sure we get the same position when using "k" and "j"
2405 * in a small window.
2406 */
2407 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002408 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 else
2410 {
2411 /*
2412 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002413 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 */
2415 if (new_topline < curwin->w_topline || always)
2416 curwin->w_topline = new_topline;
2417 if (curwin->w_topline > curwin->w_cursor.lnum)
2418 curwin->w_topline = curwin->w_cursor.lnum;
2419#ifdef FEAT_DIFF
2420 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2421 if (curwin->w_topfill > 0 && extra > off)
2422 {
2423 curwin->w_topfill -= extra - off;
2424 if (curwin->w_topfill < 0)
2425 curwin->w_topfill = 0;
2426 }
2427 check_topfill(curwin, FALSE);
2428#endif
Luuk van Baalbb800a72023-11-14 17:05:18 +01002429 if (curwin->w_topline != old_topline)
2430 reset_skipcol();
2431 else if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaar15d47472023-06-05 20:44:55 +01002432 {
2433 validate_virtcol();
2434 if (curwin->w_skipcol >= curwin->w_virtcol)
2435 // TODO: if the line doesn't fit may optimize w_skipcol instead
2436 // of making it zero
2437 reset_skipcol();
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002440 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441#ifdef FEAT_DIFF
2442 || curwin->w_topfill != old_topfill
2443#endif
2444 )
2445 curwin->w_valid &=
2446 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2447 curwin->w_valid |= VALID_TOPLINE;
2448 }
2449}
2450
2451/*
2452 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2453 * screen lines for text lines.
2454 */
2455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458#ifdef FEAT_DIFF
2459 wp->w_filler_rows = 0;
2460#endif
2461 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002462 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 else
2464 {
2465 wp->w_empty_rows = wp->w_height - used;
2466#ifdef FEAT_DIFF
2467 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2468 {
2469 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2470 if (wp->w_empty_rows > wp->w_filler_rows)
2471 wp->w_empty_rows -= wp->w_filler_rows;
2472 else
2473 {
2474 wp->w_filler_rows = wp->w_empty_rows;
2475 wp->w_empty_rows = 0;
2476 }
2477 }
2478#endif
2479 }
2480}
2481
2482/*
2483 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002484 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2486 * This is messy stuff!!!
2487 */
2488 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002489scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 int used;
2492 int scrolled = 0;
2493 int extra = 0;
2494 int i;
2495 linenr_T line_count;
2496 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002497 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 lineoff_T loff;
2499 lineoff_T boff;
2500#ifdef FEAT_DIFF
2501 int old_topfill = curwin->w_topfill;
2502 int fill_below_window;
2503#endif
2504 linenr_T old_botline = curwin->w_botline;
2505 linenr_T old_valid = curwin->w_valid;
2506 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002507 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002508 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002509 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511 cln = curwin->w_cursor.lnum;
2512 if (set_topbot)
2513 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002514 int set_skipcol = FALSE;
2515
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 used = 0;
2517 curwin->w_botline = cln + 1;
2518#ifdef FEAT_DIFF
2519 loff.fill = 0;
2520#endif
2521 for (curwin->w_topline = curwin->w_botline;
2522 curwin->w_topline > 1;
2523 curwin->w_topline = loff.lnum)
2524 {
2525 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002526 topline_back_winheight(&loff, FALSE);
2527 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002529 if (used + loff.height > curwin->w_height)
2530 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002531 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002532 {
2533 // 'smoothscroll' and 'wrap' are set. The above line is
2534 // too long to show in its entirety, so we show just a part
2535 // of it.
2536 if (used < curwin->w_height)
2537 {
2538 int plines_offset = used + loff.height
2539 - curwin->w_height;
2540 used = curwin->w_height;
2541#ifdef FEAT_DIFF
2542 curwin->w_topfill = loff.fill;
2543#endif
2544 curwin->w_topline = loff.lnum;
2545 curwin->w_skipcol = skipcol_from_plines(
2546 curwin, plines_offset);
2547 set_skipcol = TRUE;
2548 }
2549 }
2550 break;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 used += loff.height;
2553#ifdef FEAT_DIFF
2554 curwin->w_topfill = loff.fill;
2555#endif
2556 }
2557 set_empty_rows(curwin, used);
2558 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2559 if (curwin->w_topline != old_topline
2560#ifdef FEAT_DIFF
2561 || curwin->w_topfill != old_topfill
2562#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002563 || set_skipcol
2564 || curwin->w_skipcol != 0)
2565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002567 if (set_skipcol)
2568 redraw_later(UPD_NOT_VALID);
2569 else
2570 reset_skipcol();
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 else
2574 validate_botline();
2575
Bram Moolenaar85a20022019-12-21 18:25:54 +01002576 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#ifdef FEAT_DIFF
2578 used = plines_nofill(cln);
2579#else
2580 validate_cheight();
2581 used = curwin->w_cline_height;
2582#endif
2583
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002584 // If the cursor is on or below botline, we will at least scroll by the
2585 // height of the cursor line, which is "used". Correct for empty lines,
2586 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (cln >= curwin->w_botline)
2588 {
2589 scrolled = used;
2590 if (cln == curwin->w_botline)
2591 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002592 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002593 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002594 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002595 // Calculate how many screen lines the current top line of window
2596 // occupies. If it is occupying more than the entire window, we
2597 // need to scroll the additional clipped lines to scroll past the
2598 // top line before we can move on to the other lines.
2599 int top_plines =
2600#ifdef FEAT_DIFF
2601 plines_win_nofill
2602#else
2603 plines_win
2604#endif
2605 (curwin, curwin->w_topline, FALSE);
2606 int skip_lines = 0;
2607 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002608 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002609 {
fullwaywang8154e642023-06-24 21:58:09 +01002610 int width2 = width1 + curwin_col_off2();
2611 // similar formula is used in curs_columns()
2612 if (curwin->w_skipcol > width1)
2613 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2614 else if (curwin->w_skipcol > 0)
2615 skip_lines = 1;
2616
2617 top_plines -= skip_lines;
2618 if (top_plines > curwin->w_height)
2619 {
2620 scrolled += (top_plines - curwin->w_height);
2621 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002622 }
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 /*
2627 * Stop counting lines to scroll when
2628 * - hitting start of the file
2629 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002630 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * - lines between botline and cursor have been counted
2632 */
2633#ifdef FEAT_FOLDING
2634 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2635#endif
2636 {
2637 loff.lnum = cln;
2638 boff.lnum = cln;
2639 }
2640#ifdef FEAT_DIFF
2641 loff.fill = 0;
2642 boff.fill = 0;
2643 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2644 - curwin->w_filler_rows;
2645#endif
2646
2647 while (loff.lnum > 1)
2648 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2650 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002652 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2654 && loff.lnum <= curwin->w_botline
2655#ifdef FEAT_DIFF
2656 && (loff.lnum < curwin->w_botline
2657 || loff.fill >= fill_below_window)
2658#endif
2659 )
2660 break;
2661
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002664 if (loff.height == MAXCOL)
2665 used = MAXCOL;
2666 else
2667 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (used > curwin->w_height)
2669 break;
2670 if (loff.lnum >= curwin->w_botline
2671#ifdef FEAT_DIFF
2672 && (loff.lnum > curwin->w_botline
2673 || loff.fill <= fill_below_window)
2674#endif
2675 )
2676 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 scrolled += loff.height;
2679 if (loff.lnum == curwin->w_botline
2680#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002681 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#endif
2683 )
2684 scrolled -= curwin->w_empty_rows;
2685 }
2686
2687 if (boff.lnum < curbuf->b_ml.ml_line_count)
2688 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 botline_forw(&boff);
2691 used += boff.height;
2692 if (used > curwin->w_height)
2693 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002694 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2695 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 extra += boff.height;
2698 if (boff.lnum >= curwin->w_botline
2699#ifdef FEAT_DIFF
2700 || (boff.lnum + 1 == curwin->w_botline
2701 && boff.fill > curwin->w_filler_rows)
2702#endif
2703 )
2704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 scrolled += boff.height;
2707 if (boff.lnum == curwin->w_botline
2708#ifdef FEAT_DIFF
2709 && boff.fill == 0
2710#endif
2711 )
2712 scrolled -= curwin->w_empty_rows;
2713 }
2714 }
2715 }
2716 }
2717
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (scrolled <= 0)
2720 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 else if (used > curwin->w_height)
2723 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002724 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else
2726 {
2727 line_count = 0;
2728#ifdef FEAT_DIFF
2729 boff.fill = curwin->w_topfill;
2730#endif
2731 boff.lnum = curwin->w_topline - 1;
2732 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2733 {
2734 botline_forw(&boff);
2735 i += boff.height;
2736 ++line_count;
2737 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002738 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 line_count = 9999;
2740 }
2741
2742 /*
2743 * Scroll up if the cursor is off the bottom of the screen a bit.
2744 * Otherwise put it at 1/2 of the screen.
2745 */
2746 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002747 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002748 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002749 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002750 if (do_sms)
2751 scrollup(scrolled, TRUE); // TODO
2752 else
2753 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
2757 * If topline didn't change we need to restore w_botline and w_empty_rows
2758 * (we changed them).
2759 * If topline did change, update_screen() will set botline.
2760 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002761 if (curwin->w_topline == old_topline
2762 && curwin->w_skipcol == old_skipcol
2763 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
2765 curwin->w_botline = old_botline;
2766 curwin->w_empty_rows = old_empty_rows;
2767 curwin->w_valid = old_valid;
2768 }
2769 curwin->w_valid |= VALID_TOPLINE;
2770}
2771
2772/*
2773 * Recompute topline to put the cursor halfway the window
2774 * If "atend" is TRUE, also put it halfway at the end of the file.
2775 */
2776 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002777scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int above = 0;
2780 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002781 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#ifdef FEAT_DIFF
2783 int topfill = 0;
2784#endif
2785 int below = 0;
2786 int used;
2787 lineoff_T loff;
2788 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002789#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002790 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002793#ifdef FEAT_PROP_POPUP
2794 // if the width changed this needs to be updated first
2795 may_update_popup_position();
2796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2798#ifdef FEAT_FOLDING
2799 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2800#endif
2801#ifdef FEAT_DIFF
2802 used = plines_nofill(loff.lnum);
2803 loff.fill = 0;
2804 boff.fill = 0;
2805#else
2806 used = plines(loff.lnum);
2807#endif
2808 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002809
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002810 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002811 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2812 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002813 {
2814 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002815 if (atend)
2816 {
2817 want_height = (curwin->w_height - used) / 2;
2818 used = 0;
2819 }
2820 else
2821 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002822 }
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 while (topline > 1)
2825 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002826 // If using smoothscroll, we can precisely scroll to the
2827 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002828 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002829 {
2830 topline_back_winheight(&loff, FALSE);
2831 if (loff.height == MAXCOL)
2832 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002833 used += loff.height;
2834 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002835 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002836 botline_forw(&boff);
2837 used += boff.height;
2838 }
2839 if (used > want_height)
2840 {
2841 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002842 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002843 topline = loff.lnum;
2844#ifdef FEAT_DIFF
2845 topfill = loff.fill;
2846#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002847 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002848 }
2849 break;
2850 }
2851 topline = loff.lnum;
2852#ifdef FEAT_DIFF
2853 topfill = loff.fill;
2854#endif
2855 continue;
2856 }
2857
2858 // If not using smoothscroll, we have to iteratively find how many
2859 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002860 // This may not be right in the middle if the lines'
2861 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002862
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002863 // Depending on "prefer_above" we add a line above or below first.
2864 // Loop twice to avoid duplicating code.
2865 int done = FALSE;
2866 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002868 if (prefer_above ? (round == 2 && below < above)
2869 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002871 // add a line below the cursor
2872 if (boff.lnum < curbuf->b_ml.ml_line_count)
2873 {
2874 botline_forw(&boff);
2875 used += boff.height;
2876 if (used > curwin->w_height)
2877 {
2878 done = TRUE;
2879 break;
2880 }
2881 below += boff.height;
2882 }
2883 else
2884 {
2885 ++below; // count a "~" line
2886 if (atend)
2887 ++used;
2888 }
2889 }
2890
2891 if (prefer_above ? (round == 1 && below >= above)
2892 : (round == 1 && below > above))
2893 {
2894 // add a line above the cursor
2895 topline_back(&loff);
2896 if (loff.height == MAXCOL)
2897 used = MAXCOL;
2898 else
2899 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002901 {
2902 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002904 }
2905 above += loff.height;
2906 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002908 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002912 if (done)
2913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916#ifdef FEAT_FOLDING
2917 if (!hasFolding(topline, &curwin->w_topline, NULL))
2918#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002919 {
2920 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002921 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002922 || curwin->w_skipcol != 0)
2923 {
2924 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002925 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002926 {
2927 curwin->w_skipcol = skipcol;
2928 redraw_later(UPD_NOT_VALID);
2929 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002930 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002931 reset_skipcol();
2932 }
2933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#ifdef FEAT_DIFF
2935 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002936 if (old_topline > curwin->w_topline + curwin->w_height)
2937 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 check_topfill(curwin, FALSE);
2939#endif
2940 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2941 curwin->w_valid |= VALID_TOPLINE;
2942}
2943
2944/*
2945 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002946 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 * If not possible, put it at the same position as scroll_cursor_halfway().
2948 * When called topline must be valid!
2949 */
2950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002951cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 linenr_T botline;
2957 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002958 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002960 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /*
2963 * How many lines we would like to have above/below the cursor depends on
2964 * whether the first/last line of the file is on screen.
2965 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002966 above_wanted = so;
2967 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002968 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
2970 above_wanted = mouse_dragging - 1;
2971 below_wanted = mouse_dragging - 1;
2972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (curwin->w_topline == 1)
2974 {
2975 above_wanted = 0;
2976 max_off = curwin->w_height / 2;
2977 if (below_wanted > max_off)
2978 below_wanted = max_off;
2979 }
2980 validate_botline();
2981 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002982 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
2984 below_wanted = 0;
2985 max_off = (curwin->w_height - 1) / 2;
2986 if (above_wanted > max_off)
2987 above_wanted = max_off;
2988 }
2989
2990 /*
2991 * If there are sufficient file-lines above and below the cursor, we can
2992 * return now.
2993 */
2994 cln = curwin->w_cursor.lnum;
2995 if (cln >= curwin->w_topline + above_wanted
2996 && cln < curwin->w_botline - below_wanted
2997#ifdef FEAT_FOLDING
2998 && !hasAnyFolding(curwin)
2999#endif
3000 )
3001 return;
3002
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003003 if (curwin->w_p_sms && !curwin->w_p_wrap)
3004 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003005 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003006 if (curwin->w_cline_height == curwin->w_height)
3007 {
3008 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003009 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003010 return;
3011 }
3012 // TODO: If the cursor line doesn't fit in the window then only adjust
3013 // w_skipcol.
3014 }
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 /*
3017 * Narrow down the area where the cursor can be put by taking lines from
3018 * the top and the bottom until:
3019 * - the desired context lines are found
3020 * - the lines from the top is past the lines from the bottom
3021 */
3022 topline = curwin->w_topline;
3023 botline = curwin->w_botline - 1;
3024#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003025 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 above = curwin->w_topfill;
3027 below = curwin->w_filler_rows;
3028#endif
3029 while ((above < above_wanted || below < below_wanted) && topline < botline)
3030 {
3031 if (below < below_wanted && (below <= above || above >= above_wanted))
3032 {
3033#ifdef FEAT_FOLDING
3034 if (hasFolding(botline, &botline, NULL))
3035 ++below;
3036 else
3037#endif
3038 below += plines(botline);
3039 --botline;
3040 }
3041 if (above < above_wanted && (above < below || below >= below_wanted))
3042 {
3043#ifdef FEAT_FOLDING
3044 if (hasFolding(topline, NULL, &topline))
3045 ++above;
3046 else
3047#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003048 above += PLINES_NOFILL(topline);
3049#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003050 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (topline < botline)
3052 above += diff_check_fill(curwin, topline + 1);
3053#endif
3054 ++topline;
3055 }
3056 }
3057 if (topline == botline || botline == 0)
3058 curwin->w_cursor.lnum = topline;
3059 else if (topline > botline)
3060 curwin->w_cursor.lnum = botline;
3061 else
3062 {
3063 if (cln < topline && curwin->w_topline > 1)
3064 {
3065 curwin->w_cursor.lnum = topline;
3066 curwin->w_valid &=
3067 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3068 }
3069 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3070 {
3071 curwin->w_cursor.lnum = botline;
3072 curwin->w_valid &=
3073 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3074 }
3075 }
3076 curwin->w_valid |= VALID_TOPLINE;
3077}
3078
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003079static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003082 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3083 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003085 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 */
3087 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003088onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 long n;
3091 int retval = OK;
3092 lineoff_T loff;
3093 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003094 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar85a20022019-12-21 18:25:54 +01003096 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 beep_flush();
3099 return FAIL;
3100 }
3101
3102 for ( ; count > 0; --count)
3103 {
3104 validate_botline();
3105 /*
3106 * It's an error to move a page up when the first line is already on
3107 * the screen. It's an error to move a page down when the last line
3108 * is on the screen and the topline is 'scrolloff' lines from the
3109 * last line.
3110 */
3111 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003112 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3114 : (curwin->w_topline == 1
3115#ifdef FEAT_DIFF
3116 && curwin->w_topfill ==
3117 diff_check_fill(curwin, curwin->w_topline)
3118#endif
3119 ))
3120 {
3121 beep_flush();
3122 retval = FAIL;
3123 break;
3124 }
3125
3126#ifdef FEAT_DIFF
3127 loff.fill = 0;
3128#endif
3129 if (dir == FORWARD)
3130 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003131 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003133 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003134 if (p_window <= 2)
3135 ++curwin->w_topline;
3136 else
3137 curwin->w_topline += p_window - 2;
3138 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3139 curwin->w_topline = curbuf->b_ml.ml_line_count;
3140 curwin->w_cursor.lnum = curwin->w_topline;
3141 }
3142 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 curwin->w_topline = curbuf->b_ml.ml_line_count;
3146#ifdef FEAT_DIFF
3147 curwin->w_topfill = 0;
3148#endif
3149 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3150 }
3151 else
3152 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003153 // For the overlap, start with the line just below the window
3154 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 loff.lnum = curwin->w_botline;
3156#ifdef FEAT_DIFF
3157 loff.fill = diff_check_fill(curwin, loff.lnum)
3158 - curwin->w_filler_rows;
3159#endif
3160 get_scroll_overlap(&loff, -1);
3161 curwin->w_topline = loff.lnum;
3162#ifdef FEAT_DIFF
3163 curwin->w_topfill = loff.fill;
3164 check_topfill(curwin, FALSE);
3165#endif
3166 curwin->w_cursor.lnum = curwin->w_topline;
3167 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3168 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3169 }
3170 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173#ifdef FEAT_DIFF
3174 if (curwin->w_topline == 1)
3175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003176 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 max_topfill();
3178 continue;
3179 }
3180#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003181 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003182 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003183 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003184 if (p_window <= 2)
3185 --curwin->w_topline;
3186 else
3187 curwin->w_topline -= p_window - 2;
3188 if (curwin->w_topline < 1)
3189 curwin->w_topline = 1;
3190 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3191 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3192 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3193 continue;
3194 }
3195
Bram Moolenaar85a20022019-12-21 18:25:54 +01003196 // Find the line at the top of the window that is going to be the
3197 // line at the bottom of the window. Make sure this results in
3198 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 loff.lnum = curwin->w_topline - 1;
3200#ifdef FEAT_DIFF
3201 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3202 - curwin->w_topfill;
3203#endif
3204 get_scroll_overlap(&loff, 1);
3205
3206 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3207 {
3208 loff.lnum = curbuf->b_ml.ml_line_count;
3209#ifdef FEAT_DIFF
3210 loff.fill = 0;
3211 }
3212 else
3213 {
3214 botline_topline(&loff);
3215#endif
3216 }
3217 curwin->w_cursor.lnum = loff.lnum;
3218
Bram Moolenaar85a20022019-12-21 18:25:54 +01003219 // Find the line just above the new topline to get the right line
3220 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 n = 0;
3222 while (n <= curwin->w_height && loff.lnum >= 1)
3223 {
3224 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003225 if (loff.height == MAXCOL)
3226 n = MAXCOL;
3227 else
3228 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003230 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 curwin->w_topline = 1;
3233#ifdef FEAT_DIFF
3234 max_topfill();
3235#endif
3236 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3237 }
3238 else
3239 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003240 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#ifdef FEAT_DIFF
3242 topline_botline(&loff);
3243#endif
3244 botline_forw(&loff);
3245 botline_forw(&loff);
3246#ifdef FEAT_DIFF
3247 botline_topline(&loff);
3248#endif
3249#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003250 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3252#endif
3253
Bram Moolenaar85a20022019-12-21 18:25:54 +01003254 // Always scroll at least one line. Avoid getting stuck on
3255 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (loff.lnum >= curwin->w_topline
3257#ifdef FEAT_DIFF
3258 && (loff.lnum > curwin->w_topline
3259 || loff.fill >= curwin->w_topfill)
3260#endif
3261 )
3262 {
3263#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003264 // First try using the maximum number of filler lines. If
3265 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 loff.fill = curwin->w_topfill;
3267 if (curwin->w_topfill < diff_check_fill(curwin,
3268 curwin->w_topline))
3269 max_topfill();
3270 if (curwin->w_topfill == loff.fill)
3271#endif
3272 {
3273 --curwin->w_topline;
3274#ifdef FEAT_DIFF
3275 curwin->w_topfill = 0;
3276#endif
zeertzjq05834912023-10-04 20:12:37 +02003277 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 comp_botline(curwin);
3280 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003281 curwin->w_valid &=
3282 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 else
3285 {
3286 curwin->w_topline = loff.lnum;
3287#ifdef FEAT_DIFF
3288 curwin->w_topfill = loff.fill;
3289 check_topfill(curwin, FALSE);
3290#endif
3291 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3292 }
3293 }
3294 }
3295 }
3296#ifdef FEAT_FOLDING
3297 foldAdjustCursor();
3298#endif
3299 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003300 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003301 if (retval == OK)
3302 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3304
Bram Moolenaar907dad72018-07-10 15:07:15 +02003305 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003307 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3308 // But make sure we scroll at least one line (happens with mix of long
3309 // wrapping lines and non-wrapping line).
3310 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003312 scroll_cursor_top(1, FALSE);
3313 if (curwin->w_topline <= old_topline
3314 && old_topline < curbuf->b_ml.ml_line_count)
3315 {
3316 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003318 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3319#endif
3320 }
3321 }
3322#ifdef FEAT_FOLDING
3323 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003328 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return retval;
3330}
3331
3332/*
3333 * Decide how much overlap to use for page-up or page-down scrolling.
3334 * This is symmetric, so that doing both keeps the same lines displayed.
3335 * Three lines are examined:
3336 *
3337 * before CTRL-F after CTRL-F / before CTRL-B
3338 * etc. l1
3339 * l1 last but one line ------------
3340 * l2 last text line l2 top text line
3341 * ------------- l3 second text line
3342 * l3 etc.
3343 */
3344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003345get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 int h1, h2, h3, h4;
3348 int min_height = curwin->w_height - 2;
3349 lineoff_T loff0, loff1, loff2;
3350
3351#ifdef FEAT_DIFF
3352 if (lp->fill > 0)
3353 lp->height = 1;
3354 else
3355 lp->height = plines_nofill(lp->lnum);
3356#else
3357 lp->height = plines(lp->lnum);
3358#endif
3359 h1 = lp->height;
3360 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003361 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 loff0 = *lp;
3364 if (dir > 0)
3365 botline_forw(lp);
3366 else
3367 topline_back(lp);
3368 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003369 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003371 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return;
3373 }
3374
3375 loff1 = *lp;
3376 if (dir > 0)
3377 botline_forw(lp);
3378 else
3379 topline_back(lp);
3380 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003381 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003383 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return;
3385 }
3386
3387 loff2 = *lp;
3388 if (dir > 0)
3389 botline_forw(lp);
3390 else
3391 topline_back(lp);
3392 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003393 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003394 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003396 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * Scroll 'scroll' lines up or down.
3401 */
3402 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003403halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 long scrolled = 0;
3406 int i;
3407 int n;
3408 int room;
3409
3410 if (Prenum)
3411 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3412 curwin->w_height : Prenum;
3413 n = (curwin->w_p_scr <= curwin->w_height) ?
3414 curwin->w_p_scr : curwin->w_height;
3415
Bram Moolenaard5d37532017-03-27 23:02:07 +02003416 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 validate_botline();
3418 room = curwin->w_empty_rows;
3419#ifdef FEAT_DIFF
3420 room += curwin->w_filler_rows;
3421#endif
3422 if (flag)
3423 {
3424 /*
3425 * scroll the text up
3426 */
3427 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3428 {
3429#ifdef FEAT_DIFF
3430 if (curwin->w_topfill > 0)
3431 {
3432 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003433 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 --curwin->w_topfill;
3435 }
3436 else
3437#endif
3438 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003439 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 n -= i;
3441 if (n < 0 && scrolled > 0)
3442 break;
3443#ifdef FEAT_FOLDING
3444 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3445#endif
3446 ++curwin->w_topline;
3447#ifdef FEAT_DIFF
3448 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3449#endif
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3452 {
3453 ++curwin->w_cursor.lnum;
3454 curwin->w_valid &=
3455 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3459 scrolled += i;
3460
3461 /*
3462 * Correct w_botline for changed w_topline.
3463 * Won't work when there are filler lines.
3464 */
3465#ifdef FEAT_DIFF
3466 if (curwin->w_p_diff)
3467 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3468 else
3469#endif
3470 {
3471 room += i;
3472 do
3473 {
3474 i = plines(curwin->w_botline);
3475 if (i > room)
3476 break;
3477#ifdef FEAT_FOLDING
3478 (void)hasFolding(curwin->w_botline, NULL,
3479 &curwin->w_botline);
3480#endif
3481 ++curwin->w_botline;
3482 room -= i;
3483 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3484 }
3485 }
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 /*
3488 * When hit bottom of the file: move cursor down.
3489 */
3490 if (n > 0)
3491 {
3492# ifdef FEAT_FOLDING
3493 if (hasAnyFolding(curwin))
3494 {
3495 while (--n >= 0
3496 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3497 {
3498 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3499 &curwin->w_cursor.lnum);
3500 ++curwin->w_cursor.lnum;
3501 }
3502 }
3503 else
3504# endif
3505 curwin->w_cursor.lnum += n;
3506 check_cursor_lnum();
3507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 else
3510 {
3511 /*
3512 * scroll the text down
3513 */
3514 while (n > 0 && curwin->w_topline > 1)
3515 {
3516#ifdef FEAT_DIFF
3517 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3518 {
3519 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003520 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 ++curwin->w_topfill;
3522 }
3523 else
3524#endif
3525 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003526 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 n -= i;
3528 if (n < 0 && scrolled > 0)
3529 break;
3530 --curwin->w_topline;
3531#ifdef FEAT_FOLDING
3532 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3533#endif
3534#ifdef FEAT_DIFF
3535 curwin->w_topfill = 0;
3536#endif
3537 }
3538 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3539 VALID_BOTLINE|VALID_BOTLINE_AP);
3540 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (curwin->w_cursor.lnum > 1)
3542 {
3543 --curwin->w_cursor.lnum;
3544 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 /*
3549 * When hit top of the file: move cursor up.
3550 */
3551 if (n > 0)
3552 {
3553 if (curwin->w_cursor.lnum <= (linenr_T)n)
3554 curwin->w_cursor.lnum = 1;
3555 else
3556# ifdef FEAT_FOLDING
3557 if (hasAnyFolding(curwin))
3558 {
3559 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3560 {
3561 --curwin->w_cursor.lnum;
3562 (void)hasFolding(curwin->w_cursor.lnum,
3563 &curwin->w_cursor.lnum, NULL);
3564 }
3565 }
3566 else
3567# endif
3568 curwin->w_cursor.lnum -= n;
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003572 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 foldAdjustCursor();
3574# endif
3575#ifdef FEAT_DIFF
3576 check_topfill(curwin, !flag);
3577#endif
3578 cursor_correct();
3579 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003580 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003584do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003586 static win_T *prev_curwin = NULL;
3587 static pos_T prev_cursor = {0, 0, 0};
3588
3589 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3590 return;
3591 prev_curwin = curwin;
3592 prev_cursor = curwin->w_cursor;
3593
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003595 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003596 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003597 colnr_T curswant = curwin->w_curswant;
3598 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 win_T *old_curwin = curwin;
3600 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 int old_VIsual_select = VIsual_select;
3602 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003603
3604 /*
3605 * loop through the cursorbound windows
3606 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003608 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 {
3610 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003611 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 if (curwin != old_curwin && curwin->w_p_crb)
3613 {
3614# ifdef FEAT_DIFF
3615 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003616 curwin->w_cursor.lnum =
3617 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003618 else
3619# endif
3620 curwin->w_cursor.lnum = line;
3621 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003622 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003623 curwin->w_curswant = curswant;
3624 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625
Bram Moolenaar85a20022019-12-21 18:25:54 +01003626 // Make sure the cursor is in a valid position. Temporarily set
3627 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003628 int restart_edit_save = restart_edit;
3629 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003630 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003631
3632 // Avoid a scroll here for the cursor position, 'scrollbind' is
3633 // more important.
3634 if (!curwin->w_p_scb)
3635 validate_cursor();
3636
Bram Moolenaar61452852011-02-01 18:01:11 +01003637 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003638 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003639 if (has_mbyte)
3640 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003641 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003642
Bram Moolenaar85a20022019-12-21 18:25:54 +01003643 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003644 if (!curwin->w_p_scb)
3645 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003646 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003647 }
3648 }
3649
3650 /*
3651 * reset current-window
3652 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003653 VIsual_select = old_VIsual_select;
3654 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003655 curwin = old_curwin;
3656 curbuf = old_curbuf;
3657}