blob: bb6502467e9fe1e77ea7a2e5ff0f4c9369c4d275 [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);
48 if (wp->w_skipcol >= width)
zeertzjq6235a102023-08-19 14:12:42 +020049 return (wp->w_skipcol - width) / (width + win_col_off2(wp)) + 1;
50
51 return 0;
Bram Moolenaarf6196f42022-10-02 21:29:55 +010052}
53
54/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010055 * Return how many lines "lnum" will take on the screen, taking into account
56 * whether it is the first line, whether w_skipcol is non-zero and limiting to
57 * the window height.
58 */
59 static int
60plines_correct_topline(win_T *wp, linenr_T lnum)
61{
62 int n;
63#ifdef FEAT_DIFF
64 if (lnum == wp->w_topline)
65 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
66 else
67#endif
68 n = plines_win(wp, lnum, FALSE);
69 if (lnum == wp->w_topline)
zeertzjq6235a102023-08-19 14:12:42 +020070 n -= adjust_plines_for_skipcol(wp);
Bram Moolenaard5337ef2022-10-20 20:15:47 +010071 if (n > wp->w_height)
72 n = wp->w_height;
73 return n;
74}
75
76/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 * Compute wp->w_botline for the current wp->w_topline. Can be called after
78 * wp->w_topline changed.
79 */
80 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010081comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000082{
83 int n;
84 linenr_T lnum;
85 int done;
86#ifdef FEAT_FOLDING
87 linenr_T last;
88 int folded;
89#endif
90
91 /*
92 * If w_cline_row is valid, start there.
93 * Otherwise have to start at w_topline.
94 */
95 check_cursor_moved(wp);
96 if (wp->w_valid & VALID_CROW)
97 {
98 lnum = wp->w_cursor.lnum;
99 done = wp->w_cline_row;
100 }
101 else
102 {
103 lnum = wp->w_topline;
104 done = 0;
105 }
106
107 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
108 {
109#ifdef FEAT_FOLDING
110 last = lnum;
111 folded = FALSE;
112 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
113 {
114 n = 1;
115 folded = TRUE;
116 }
117 else
118#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100119 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100120 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 if (
123#ifdef FEAT_FOLDING
124 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
125#else
126 lnum == wp->w_cursor.lnum
127#endif
128 )
129 {
130 wp->w_cline_row = done;
131 wp->w_cline_height = n;
132#ifdef FEAT_FOLDING
133 wp->w_cline_folded = folded;
134#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100135 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
137 }
138 if (done + n > wp->w_height)
139 break;
140 done += n;
141#ifdef FEAT_FOLDING
142 lnum = last;
143#endif
144 }
145
Bram Moolenaar85a20022019-12-21 18:25:54 +0100146 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 wp->w_botline = lnum;
148 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
149
150 set_empty_rows(wp, done);
151}
152
153/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100154 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
155 * set.
156 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100157 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100158redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100159{
160 if ((wp->w_p_rnu
161#ifdef FEAT_SYN_HL
162 || wp->w_p_cul
163#endif
164 )
165 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200166 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200167 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000168 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100169 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200170 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100171}
172
zeertzjq3e559cd2022-03-27 19:26:55 +0100173#ifdef FEAT_SYN_HL
174/*
175 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
176 * contains "screenline".
177 */
178 static void
179redraw_for_cursorcolumn(win_T *wp)
180{
181 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
182 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100183 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100184 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100185 redraw_win_later(wp, UPD_SOME_VALID);
186 // When 'cursorlineopt' contains "screenline" need to redraw with
187 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100188 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100189 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100190 }
191}
192#endif
193
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100194/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100195 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
196 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000197 * Parameter "extra2" should be the padding on the 2nd line, not the first
198 * line.
199 * Returns the number of columns of overlap with buffer text, excluding the
200 * extra padding on the ledge.
201 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100202 int
203sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000204{
205#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100206 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100208 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000209 return 0;
210#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100211 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
212 if (wp->w_p_list && wp->w_lcs_chars.prec)
213 return 1;
214
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000215 return extra2 > 3 ? 0 : 3 - extra2;
216}
217
218/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000219 * Calculates the skipcol offset for window "wp" given how many
220 * physical lines we want to scroll down.
221 */
222 static int
223skipcol_from_plines(win_T *wp, int plines_off)
224{
225 int width1 = wp->w_width - win_col_off(wp);
226
227 int skipcol = 0;
228 if (plines_off > 0)
229 skipcol += width1;
230 if (plines_off > 1)
231 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
232 return skipcol;
233}
234
235/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100236 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000237 */
238 static void
239reset_skipcol(void)
240{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000241 if (curwin->w_skipcol == 0)
242 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000243
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000244 curwin->w_skipcol = 0;
245
246 // Should use the least expensive way that displays all that changed.
247 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
248 // enough when the top line gets another screen line.
249 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000250}
251
252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 * Update curwin->w_topline and redraw if necessary.
254 * Used to update the screen before printing a message.
255 */
256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100257update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 update_topline();
260 if (must_redraw)
261 update_screen(0);
262}
263
264/*
265 * Update curwin->w_topline to move the cursor onto the screen.
266 */
267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100268update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269{
270 long line_count;
271 int halfheight;
272 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifdef FEAT_FOLDING
274 linenr_T lnum;
275#endif
276 int check_topline = FALSE;
277 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100278 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100279 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100281 // Cursor is updated instead when this is TRUE for 'splitkeep'.
282 if (skip_update_topline)
283 return;
284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // If there is no valid screen and when the window height is zero just use
286 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200287 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200288 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100289 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200290 curwin->w_topline = curwin->w_cursor.lnum;
291 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200292 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200293 return;
294 }
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 check_cursor_moved(curwin);
297 if (curwin->w_valid & VALID_TOPLINE)
298 return;
299
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000301 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100302 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100304 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100306 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
308
309 /*
310 * If the buffer is empty, always set topline to 1.
311 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {
314 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100315 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 curwin->w_botline = 2;
318 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321
322 /*
323 * If the cursor is above or near the top of the window, scroll the window
324 * to show the line the cursor is in, with 'scrolloff' context.
325 */
326 else
327 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100328 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // If the cursor is above topline, scrolling is always needed.
331 // If the cursor is far below topline and there is no folding,
332 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (curwin->w_cursor.lnum < curwin->w_topline)
334 check_topline = TRUE;
335 else if (check_top_offset())
336 check_topline = TRUE;
zeertzjq55daae32023-06-04 19:29:22 +0100337 else if (curwin->w_skipcol > 0
338 && curwin->w_cursor.lnum == curwin->w_topline)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100339 {
340 colnr_T vcol;
341
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000342 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100343 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100344 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100345 int overlap = sms_marker_overlap(curwin, curwin_col_off()
346 - curwin_col_off2());
347 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100348 check_topline = TRUE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
354 curwin->w_topline))
355 check_topline = TRUE;
356#endif
357
358 if (check_topline)
359 {
360 halfheight = curwin->w_height / 2 - 1;
361 if (halfheight < 2)
362 halfheight = 2;
363
364#ifdef FEAT_FOLDING
365 if (hasAnyFolding(curwin))
366 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100367 // Count the number of logical lines between the cursor and
368 // topline + scrolloff (approximation of how much will be
369 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 n = 0;
371 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {
374 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100375 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
377 break;
378 (void)hasFolding(lnum, NULL, &lnum);
379 }
380 }
381 else
382#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100383 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // If we weren't very close to begin with, we scroll to put the
386 // cursor in the middle of the window. Otherwise put the cursor
387 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000389 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 else
391 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 check_botline = TRUE;
394 }
395 }
396
397 else
398 {
399#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100400 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
402#endif
403 check_botline = TRUE;
404 }
405 }
406
407 /*
408 * If the cursor is below the bottom of the window, scroll the window
409 * to put the cursor on the window.
410 * When w_botline is invalid, recompute it first, to avoid a redraw later.
411 * If w_botline was approximated, we might need a redraw later in a few
412 * cases, but we don't want to spend (a lot of) time recomputing w_botline
413 * for every small change.
414 */
415 if (check_botline)
416 {
417 if (!(curwin->w_valid & VALID_BOTLINE_AP))
418 validate_botline();
419
420 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
421 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000422 if (curwin->w_cursor.lnum < curwin->w_botline)
423 {
424 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100425 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef FEAT_FOLDING
427 || hasAnyFolding(curwin)
428#endif
429 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 lineoff_T loff;
432
Bram Moolenaar85a20022019-12-21 18:25:54 +0100433 // Cursor is (a few lines) above botline, check if there are
434 // 'scrolloff' window lines below the cursor. If not, need to
435 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 n = curwin->w_empty_rows;
437 loff.lnum = curwin->w_cursor.lnum;
438#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100439 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
441#endif
442#ifdef FEAT_DIFF
443 loff.fill = 0;
444 n += curwin->w_filler_rows;
445#endif
446 loff.height = 0;
447 while (loff.lnum < curwin->w_botline
448#ifdef FEAT_DIFF
449 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
450#endif
451 )
452 {
453 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100454 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 break;
456 botline_forw(&loff);
457 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100459 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000461 }
462 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100463 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000464 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466 if (check_botline)
467 {
468#ifdef FEAT_FOLDING
469 if (hasAnyFolding(curwin))
470 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100471 // Count the number of logical lines between the cursor and
472 // botline - scrolloff (approximation of how much will be
473 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 line_count = 0;
475 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100476 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
478 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100479 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (lnum <= 0 || line_count > curwin->w_height + 1)
481 break;
482 (void)hasFolding(lnum, &lnum, NULL);
483 }
484 }
485 else
486#endif
487 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100488 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000490 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000492 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 }
494 }
495 }
496 curwin->w_valid |= VALID_TOPLINE;
497
498 /*
499 * Need to redraw when topline changed.
500 */
501 if (curwin->w_topline != old_topline
502#ifdef FEAT_DIFF
503 || curwin->w_topfill != old_topfill
504#endif
505 )
506 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100507 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000508 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100509
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100510 // When 'smoothscroll' is not set, should reset w_skipcol.
511 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100512 reset_skipcol();
Luuk van Baald49f6462023-05-19 14:04:47 +0100513 else if (curwin->w_skipcol != 0)
514 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000515
Bram Moolenaar85a20022019-12-21 18:25:54 +0100516 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (curwin->w_cursor.lnum == curwin->w_topline)
518 validate_cursor();
519 }
520
Bram Moolenaar375e3392019-01-31 18:26:10 +0100521 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522}
523
524/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000525 * Return the scrolljump value to use for the current window.
526 * When 'scrolljump' is positive use it as-is.
527 * When 'scrolljump' is negative use it as a percentage of the window height.
528 */
529 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100530scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000531{
532 if (p_sj >= 0)
533 return (int)p_sj;
534 return (curwin->w_height * -p_sj) / 100;
535}
536
537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
539 * current window.
540 */
541 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 lineoff_T loff;
545 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100546 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar375e3392019-01-31 18:26:10 +0100548 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#ifdef FEAT_FOLDING
550 || hasAnyFolding(curwin)
551#endif
552 )
553 {
554 loff.lnum = curwin->w_cursor.lnum;
555#ifdef FEAT_DIFF
556 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#else
559 n = 0;
560#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100561 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100562 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
564 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100565 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (loff.lnum < curwin->w_topline
567#ifdef FEAT_DIFF
568 || (loff.lnum == curwin->w_topline && loff.fill > 0)
569#endif
570 )
571 break;
572 n += loff.height;
573 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100574 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 return TRUE;
576 }
577 return FALSE;
578}
579
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100580/*
581 * Update w_curswant.
582 */
583 void
584update_curswant_force(void)
585{
586 validate_virtcol();
587 curwin->w_curswant = curwin->w_virtcol
588#ifdef FEAT_PROP_POPUP
589 - curwin->w_virtcol_first_char
590#endif
591 ;
592 curwin->w_set_curswant = FALSE;
593}
594
595/*
596 * Update w_curswant if w_set_curswant is set.
597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100602 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603}
604
605/*
606 * Check if the cursor has moved. Set the w_valid flag accordingly.
607 */
608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
612 {
613 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100614 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
615 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 wp->w_valid_cursor = wp->w_cursor;
617 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100618 wp->w_valid_skipcol = wp->w_skipcol;
619 }
620 else if (wp->w_skipcol != wp->w_valid_skipcol)
621 {
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
623 |VALID_CHEIGHT|VALID_CROW
624 |VALID_BOTLINE|VALID_BOTLINE_AP);
625 wp->w_valid_cursor = wp->w_cursor;
626 wp->w_valid_leftcol = wp->w_leftcol;
627 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 else if (wp->w_cursor.col != wp->w_valid_cursor.col
630 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100631 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
634 wp->w_valid_cursor.col = wp->w_cursor.col;
635 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638}
639
640/*
641 * Call this function when some window settings have changed, which require
642 * the cursor position, botline and topline to be recomputed and the window to
643 * be redrawn. E.g, when changing the 'wrap' option or folding.
644 */
645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
648 changed_window_setting_win(curwin);
649}
650
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 wp->w_lines_valid = 0;
655 changed_line_abv_curs_win(wp);
656 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100657 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658}
659
Dominique Pellee764d1b2023-03-12 21:20:59 +0000660#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000662 * Call changed_window_setting_win() for every window containing "buf".
663 */
664 void
665changed_window_setting_buf(buf_T *buf)
666{
667 tabpage_T *tp;
668 win_T *wp;
669
670 FOR_ALL_TAB_WINDOWS(tp, wp)
671 if (wp->w_buffer == buf)
672 changed_window_setting_win(wp);
673}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000674#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Set wp->w_topline to a certain number.
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200682#ifdef FEAT_DIFF
683 linenr_T prev_topline = wp->w_topline;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100687 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
689#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100692 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
693 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000695 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200697 if (lnum != prev_topline)
698 // Keep the filler lines when the topline didn't change.
699 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
701 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100703 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704}
705
706/*
707 * Call this function when the length of the cursor line (in screen
708 * characters) has changed, and the change is before the cursor.
Bram Moolenaar85090142023-06-01 19:27:08 +0100709 * If the line length changed the number of screen lines might change,
710 * requiring updating w_topline. That may also invalidate w_crow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 * Need to take care of w_botline separately!
712 */
713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
Bram Moolenaar85090142023-06-01 19:27:08 +0100716 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 |VALID_CHEIGHT|VALID_TOPLINE);
718}
719
720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
Bram Moolenaar85090142023-06-01 19:27:08 +0100723 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 |VALID_CHEIGHT|VALID_TOPLINE);
725}
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727/*
728 * Call this function when the length of a line (in screen characters) above
729 * the cursor have changed.
730 * Need to take care of w_botline separately!
731 */
732 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100733changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
736 |VALID_CHEIGHT|VALID_TOPLINE);
737}
738
739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100740changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
743 |VALID_CHEIGHT|VALID_TOPLINE);
744}
745
Dominique Pellee764d1b2023-03-12 21:20:59 +0000746#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100748 * Display of line has changed for "buf", invalidate cursor position and
749 * w_botline.
750 */
751 void
752changed_line_display_buf(buf_T *buf)
753{
754 win_T *wp;
755
756 FOR_ALL_WINDOWS(wp)
757 if (wp->w_buffer == buf)
758 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
759 |VALID_CROW|VALID_CHEIGHT
760 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
761}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000762#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100763
764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * Make sure the value of curwin->w_botline is valid.
766 */
767 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100770 validate_botline_win(curwin);
771}
772
773/*
774 * Make sure the value of wp->w_botline is valid.
775 */
776 void
777validate_botline_win(win_T *wp)
778{
779 if (!(wp->w_valid & VALID_BOTLINE))
780 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * Mark curwin->w_botline as invalid (because of some change in the buffer).
785 */
786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100787invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
789 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
790}
791
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
796}
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100799approximate_botline_win(
800 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 wp->w_valid &= ~VALID_BOTLINE;
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
807 */
808 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100809cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 check_cursor_moved(curwin);
812 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
813 (VALID_WROW|VALID_WCOL));
814}
815
816/*
817 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
818 * w_topline must be valid, you may need to call update_topline() first!
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100823 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 check_cursor_moved(curwin);
825 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
826 curs_columns(TRUE);
827}
828
829#if defined(FEAT_GUI) || defined(PROTO)
830/*
831 * validate w_cline_row.
832 */
833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100834validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 /*
837 * First make sure that w_topline is valid (after moving the cursor).
838 */
839 update_topline();
840 check_cursor_moved(curwin);
841 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100842 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844#endif
845
846/*
847 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200848 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 */
850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100851curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 linenr_T lnum;
854 int i;
855 int all_invalid;
856 int valid;
857#ifdef FEAT_FOLDING
858 long fold_count;
859#endif
860
Bram Moolenaar85a20022019-12-21 18:25:54 +0100861 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 all_invalid = (!redrawing()
863 || wp->w_lines_valid == 0
864 || wp->w_lines[0].wl_lnum > wp->w_topline);
865 i = 0;
866 wp->w_cline_row = 0;
867 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
868 {
869 valid = FALSE;
870 if (!all_invalid && i < wp->w_lines_valid)
871 {
872 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (wp->w_lines[i].wl_lnum == lnum)
875 {
876#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 // Check for newly inserted lines below this row, in which
878 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (!wp->w_buffer->b_mod_set
880 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
881 || wp->w_buffer->b_mod_top
882 > wp->w_lines[i].wl_lastlnum + 1)
883#endif
884 valid = TRUE;
885 }
886 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 if (valid
Luuk van Baald49f6462023-05-19 14:04:47 +0100890 && (lnum != wp->w_topline || (wp->w_skipcol == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_DIFF
Luuk van Baald49f6462023-05-19 14:04:47 +0100892 && !wp->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
Luuk van Baald49f6462023-05-19 14:04:47 +0100894 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
896#ifdef FEAT_FOLDING
897 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (lnum > wp->w_cursor.lnum)
900 break;
901#else
902 ++lnum;
903#endif
904 wp->w_cline_row += wp->w_lines[i].wl_size;
905 }
906 else
907 {
908#ifdef FEAT_FOLDING
909 fold_count = foldedCount(wp, lnum, NULL);
910 if (fold_count)
911 {
912 lnum += fold_count;
913 if (lnum > wp->w_cursor.lnum)
914 break;
915 ++wp->w_cline_row;
916 }
917 else
918#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100919 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100920 wp->w_cline_row += plines_correct_topline(wp, lnum);
921 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 }
925
926 check_cursor_moved(wp);
927 if (!(wp->w_valid & VALID_CHEIGHT))
928 {
929 if (all_invalid
930 || i == wp->w_lines_valid
931 || (i < wp->w_lines_valid
932 && (!wp->w_lines[i].wl_valid
933 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
934 {
935#ifdef FEAT_DIFF
936 if (wp->w_cursor.lnum == wp->w_topline)
937 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
938 TRUE) + wp->w_topfill;
939 else
940#endif
941 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
942#ifdef FEAT_FOLDING
943 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
944 NULL, NULL, TRUE, NULL);
945#endif
946 }
947 else if (i > wp->w_lines_valid)
948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100949 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 wp->w_cline_height = 0;
951#ifdef FEAT_FOLDING
952 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
953 NULL, NULL, TRUE, NULL);
954#endif
955 }
956 else
957 {
958 wp->w_cline_height = wp->w_lines[i].wl_size;
959#ifdef FEAT_FOLDING
960 wp->w_cline_folded = wp->w_lines[i].wl_folded;
961#endif
962 }
963 }
964
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100965 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Validate curwin->w_virtcol only.
971 */
972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100973validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975 validate_virtcol_win(curwin);
976}
977
978/*
979 * Validate wp->w_virtcol only.
980 */
981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100982validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000985
986 if (wp->w_valid & VALID_VIRTCOL)
987 return;
988
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100989#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000990 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100991#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000993#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000995#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
998
999/*
1000 * Validate curwin->w_cline_height only.
1001 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001003validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001006
1007 if (curwin->w_valid & VALID_CHEIGHT)
1008 return;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001011 if (curwin->w_cursor.lnum == curwin->w_topline)
1012 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1013 + curwin->w_topfill;
1014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001024 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 */
1026 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001027validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 colnr_T off;
1030 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001031 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001035 if (curwin->w_valid & VALID_WCOL)
1036 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001037
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001038 col = curwin->w_virtcol;
1039 off = curwin_col_off();
1040 col += off;
1041 width = curwin->w_width - off + curwin_col_off2();
1042
1043 // long line wrapping, adjust curwin->w_wrow
1044 if (curwin->w_p_wrap
1045 && col >= (colnr_T)curwin->w_width
1046 && width > 0)
1047 // use same formula as what is used in curs_columns()
1048 col -= ((col - curwin->w_width) / width + 1) * width;
1049 if (col > (int)curwin->w_leftcol)
1050 col -= curwin->w_leftcol;
1051 else
1052 col = 0;
1053 curwin->w_wcol = col;
1054
1055 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001056#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001057 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059}
1060
1061/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001062 * Compute offset of a window, occupied by absolute or relative line number,
1063 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
1065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001066win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar64486672010-05-16 15:46:46 +02001068 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#ifdef FEAT_FOLDING
1071 + wp->w_p_fdc
1072#endif
1073#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001074 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
1076 );
1077}
1078
1079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 return win_col_off(curwin);
1083}
1084
1085/*
1086 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001087 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1088 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001091win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar64486672010-05-16 15:46:46 +02001093 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001094 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 return 0;
1096}
1097
1098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001099curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 return win_col_off2(curwin);
1102}
1103
1104/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001105 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * Also updates curwin->w_wrow and curwin->w_cline_row.
1107 * Also updates curwin->w_leftcol.
1108 */
1109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001110curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001111 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001114 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int off_left, off_right;
1116 int n;
1117 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001118 int width1; // text width for first screen line
1119 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int new_leftcol;
1121 colnr_T startcol;
1122 colnr_T endcol;
1123 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001124 long so = get_scrolloff_value();
1125 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001126 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 /*
1129 * First make sure that w_topline is valid (after moving the cursor).
1130 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001131 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
1133 /*
1134 * Next make sure that w_cline_row is valid.
1135 */
1136 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001137 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001139#ifdef FEAT_PROP_POPUP
1140 // will be set by getvvcol() but not reset
1141 curwin->w_virtcol_first_char = 0;
1142#endif
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
1145 * Compute the number of virtual columns.
1146 */
1147#ifdef FEAT_FOLDING
1148 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1151 else
1152#endif
1153 getvvcol(curwin, &curwin->w_cursor,
1154 &startcol, &(curwin->w_virtcol), &endcol);
1155
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001158 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 extra = curwin_col_off();
1161 curwin->w_wcol = curwin->w_virtcol + extra;
1162 endcol += extra;
1163
1164 /*
1165 * Now compute w_wrow, counting screen lines from w_cline_row.
1166 */
1167 curwin->w_wrow = curwin->w_cline_row;
1168
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001169 width1 = curwin->w_width - extra;
1170 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001173 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001174 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001175 if (curwin->w_p_wrap)
1176 curwin->w_wrow = curwin->w_height - 1;
1177 else
1178 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001180 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001182 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001184 // skip columns that are not visible
1185 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001186 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001187 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001188 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001189 // Deduct by multiples of width2. This allows the long line
1190 // wrapping formula below to correctly calculate the w_wcol value
1191 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001198 did_sub_skipcol = TRUE;
1199 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001200
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001202 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001205 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1206 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 curwin->w_wrow += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209 }
1210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1212 // is not folded.
1213 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001214 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#ifdef FEAT_FOLDING
1216 && !curwin->w_cline_folded
1217#endif
1218 )
1219 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001220#ifdef FEAT_PROP_POPUP
1221 if (curwin->w_virtcol_first_char > 0)
1222 {
1223 int cols = (curwin->w_width - extra);
1224 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1225
1226 // each "above" text prop shifts the text one row down
1227 curwin->w_wrow += rows;
1228 curwin->w_wcol -= rows * cols;
1229 endcol -= rows * cols;
1230 curwin->w_cline_height = rows + 1;
1231 }
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /*
1234 * If Cursor is left of the screen, scroll rightwards.
1235 * If Cursor is right of the screen, scroll leftwards
1236 * If we get closer to the edge than 'sidescrolloff', scroll a little
1237 * extra
1238 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001239 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001240 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001241 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (off_left < 0 || off_right > 0)
1243 {
1244 if (off_left < 0)
1245 diff = -off_left;
1246 else
1247 diff = off_right;
1248
Bram Moolenaar85a20022019-12-21 18:25:54 +01001249 // When far off or not enough room on either side, put cursor in
1250 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001251 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1252 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 else
1254 {
1255 if (diff < p_ss)
1256 diff = p_ss;
1257 if (off_left < 0)
1258 new_leftcol = curwin->w_leftcol - diff;
1259 else
1260 new_leftcol = curwin->w_leftcol + diff;
1261 }
1262 if (new_leftcol < 0)
1263 new_leftcol = 0;
1264 if (new_leftcol != (int)curwin->w_leftcol)
1265 {
1266 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001268 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 }
1271 curwin->w_wcol -= curwin->w_leftcol;
1272 }
1273 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1274 curwin->w_wcol -= curwin->w_leftcol;
1275 else
1276 curwin->w_wcol = 0;
1277
1278#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // Skip over filler lines. At the top use w_topfill, there
1280 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (curwin->w_cursor.lnum == curwin->w_topline)
1282 curwin->w_wrow += curwin->w_topfill;
1283 else
1284 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1285#endif
1286
1287 prev_skipcol = curwin->w_skipcol;
1288
1289 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if ((curwin->w_wrow >= curwin->w_height
1292 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001293 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && (p_lines =
1295#ifdef FEAT_DIFF
1296 plines_win_nofill
1297#else
1298 plines_win
1299#endif
1300 (curwin, curwin->w_cursor.lnum, FALSE))
1301 - 1 >= curwin->w_height))
1302 && curwin->w_height != 0
1303 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001304 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001305 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001307 // Cursor past end of screen. Happens with a single line that does
1308 // not fit on screen. Find a skipcol to show the text around the
1309 // cursor. Avoid scrolling all the time. compute value of "extra":
1310 // 1: Less than 'scrolloff' lines above
1311 // 2: Less than 'scrolloff' lines below
1312 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001314 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 // Compute last display line of the buffer line that we want at the
1317 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (p_lines == 0)
1319 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1320 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001321 if (p_lines > curwin->w_wrow + so)
1322 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else
1324 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001325 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 extra += 2;
1327
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001328 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001331 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (n > curwin->w_height / 2)
1333 n -= curwin->w_height / 2;
1334 else
1335 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (n > p_lines - curwin->w_height + 1)
1338 n = p_lines - curwin->w_height + 1;
zeertzjqbfe377b2023-08-17 22:58:53 +02001339 if (n > 0)
1340 curwin->w_skipcol = width1 + (n - 1) * width2;
1341 else
1342 curwin->w_skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 else if (extra == 1)
1345 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001346 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001347 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1348 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (extra > 0)
1350 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1352 extra = curwin->w_skipcol / width2;
1353 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 }
1356 else if (extra == 2)
1357 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001358 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001359 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001361 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (endcol > curwin->w_skipcol)
1363 curwin->w_skipcol = endcol;
1364 }
1365
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001366 // adjust w_wrow for the changed w_skipcol
1367 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001369 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (curwin->w_wrow >= curwin->w_height)
1373 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001376 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 curwin->w_wrow -= extra;
1378 }
1379
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001380 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (extra > 0)
1382 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1383 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001384 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001386 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 curwin->w_skipcol = 0;
1388 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001389 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001391#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001392 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001393#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001394#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1395 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1396 {
1397 curwin->w_wrow += popup_top_extra(curwin);
1398 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001399 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001400 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001401 else
1402 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001403#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001404
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001405 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1406 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001407 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001408 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1411}
1412
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001413#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001414/*
1415 * Compute the screen position of text character at "pos" in window "wp"
1416 * The resulting values are one-based, zero when character is not visible.
1417 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001418 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001419textpos2screenpos(
1420 win_T *wp,
1421 pos_T *pos,
1422 int *rowp, // screen row
1423 int *scolp, // start screen column
1424 int *ccolp, // cursor screen column
1425 int *ecolp) // end screen column
1426{
1427 colnr_T scol = 0, ccol = 0, ecol = 0;
1428 int row = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001429 colnr_T coloff = 0;
1430
Bram Moolenaar189663b2021-07-21 18:04:56 +02001431 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001433 colnr_T col;
1434 int width;
1435 linenr_T lnum = pos->lnum;
1436#ifdef FEAT_FOLDING
1437 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001439 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1440#endif
zeertzjq6235a102023-08-19 14:12:42 +02001441 row = plines_m_win(wp, wp->w_topline, lnum - 1, FALSE);
zeertzjqbfe377b2023-08-17 22:58:53 +02001442 // "row" should be the screen line where line "lnum" begins, which can
1443 // be negative if "lnum" is "w_topline" and "w_skipcol" is non-zero.
zeertzjq6235a102023-08-19 14:12:42 +02001444 row -= adjust_plines_for_skipcol(wp);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001445
1446#ifdef FEAT_DIFF
1447 // Add filler lines above this buffer line.
zeertzjq55daae32023-06-04 19:29:22 +01001448 row += lnum == wp->w_topline ? wp->w_topfill
1449 : diff_check_fill(wp, lnum);
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001450#endif
1451
zeertzjqba2d1912022-12-18 12:28:59 +00001452 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001453#ifdef FEAT_FOLDING
1454 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455 {
zeertzjq6235a102023-08-19 14:12:42 +02001456 row += W_WINROW(wp) + 1;
zeertzjqba2d1912022-12-18 12:28:59 +00001457 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001458 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001460#endif
1461 {
1462 getvcol(wp, pos, &scol, &ccol, &ecol);
1463
1464 // similar to what is done in validate_cursor_col()
1465 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001466 col += off;
1467 width = wp->w_width - off + win_col_off2(wp);
1468
1469 // long line wrapping, adjust row
1470 if (wp->w_p_wrap
1471 && col >= (colnr_T)wp->w_width
1472 && width > 0)
1473 {
1474 // use same formula as what is used in curs_columns()
zeertzjqbfe377b2023-08-17 22:58:53 +02001475 int rowoff = ((col - wp->w_width) / width + 1);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001476 col -= rowoff * width;
zeertzjqbfe377b2023-08-17 22:58:53 +02001477 row += rowoff;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001478 }
1479 col -= wp->w_leftcol;
1480 if (col >= wp->w_width)
1481 col = -1;
zeertzjq6235a102023-08-19 14:12:42 +02001482 if (col >= 0 && row >= 0 && row < wp->w_height)
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001483 {
1484 coloff = col - scol + wp->w_wincol + 1;
zeertzjq6235a102023-08-19 14:12:42 +02001485 row += W_WINROW(wp) + 1;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001486 }
1487 else
zeertzjqbfe377b2023-08-17 22:58:53 +02001488 // character is out of the window
1489 row = scol = ccol = ecol = 0;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001490 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001491 }
zeertzjqbfe377b2023-08-17 22:58:53 +02001492 *rowp = row;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493 *scolp = scol + coloff;
1494 *ccolp = ccol + coloff;
1495 *ecolp = ecol + coloff;
1496}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001497#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498
Bram Moolenaar12034e22019-08-25 22:25:02 +02001499#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001500/*
1501 * "screenpos({winid}, {lnum}, {col})" function
1502 */
1503 void
1504f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1505{
1506 dict_T *dict;
1507 win_T *wp;
1508 pos_T pos;
1509 int row = 0;
1510 int scol = 0, ccol = 0, ecol = 0;
1511
Bram Moolenaar93a10962022-06-16 11:42:09 +01001512 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001513 return;
1514 dict = rettv->vval.v_dict;
1515
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001516 if (in_vim9script()
1517 && (check_for_number_arg(argvars, 0) == FAIL
1518 || check_for_number_arg(argvars, 1) == FAIL
1519 || check_for_number_arg(argvars, 2) == FAIL))
1520 return;
1521
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001522 wp = find_win_by_nr_or_id(&argvars[0]);
1523 if (wp == NULL)
1524 return;
1525
1526 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001527 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1528 {
1529 semsg(_(e_invalid_line_number_nr), pos.lnum);
1530 return;
1531 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001532 pos.col = tv_get_number(&argvars[2]) - 1;
1533 pos.coladd = 0;
1534 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1535
1536 dict_add_number(dict, "row", row);
1537 dict_add_number(dict, "col", scol);
1538 dict_add_number(dict, "curscol", ccol);
1539 dict_add_number(dict, "endcol", ecol);
1540}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001541
1542/*
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001543 * Convert a virtual (screen) column to a character column. The first column
1544 * is one. For a multibyte character, the column number of the first byte is
1545 * returned.
1546 */
1547 static int
1548virtcol2col(win_T *wp, linenr_T lnum, int vcol)
1549{
zeertzjqf5a94d52023-10-15 10:03:30 +02001550 int offset = vcol2col(wp, lnum, vcol - 1, NULL);
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001551 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1552 char_u *p = line + offset;
1553
zeertzjqb583eda2023-10-14 11:32:28 +02001554 if (*p == NUL)
1555 {
1556 if (p == line) // empty line
1557 return 0;
1558 // Move to the first byte of the last char.
1559 MB_PTR_BACK(line, p);
1560 }
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001561 return (int)(p - line + 1);
1562}
1563
1564/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001565 * "virtcol2col({winid}, {lnum}, {col})" function
1566 */
1567 void
1568f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1569{
1570 win_T *wp;
1571 linenr_T lnum;
1572 int screencol;
1573 int error = FALSE;
1574
1575 rettv->vval.v_number = -1;
1576
1577 if (check_for_number_arg(argvars, 0) == FAIL
1578 || check_for_number_arg(argvars, 1) == FAIL
1579 || check_for_number_arg(argvars, 2) == FAIL)
1580 return;
1581
1582 wp = find_win_by_nr_or_id(&argvars[0]);
1583 if (wp == NULL)
1584 return;
1585
1586 lnum = tv_get_number_chk(&argvars[1], &error);
1587 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1588 return;
1589
1590 screencol = tv_get_number_chk(&argvars[2], &error);
1591 if (error || screencol < 0)
1592 return;
1593
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001594 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001595}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
1599 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602scrolldown(
1603 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int wrow;
1608 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001609 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001610 int width1 = 0;
1611 int width2 = 0;
1612
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001613 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001614 {
1615 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001616 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619#ifdef FEAT_FOLDING
1620 linenr_T first;
1621
Bram Moolenaar85a20022019-12-21 18:25:54 +01001622 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1624#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001626 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001629 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1630 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 ++curwin->w_topfill;
1633 ++done;
1634 }
1635 else
1636#endif
1637 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001638 // break when at the very top
1639 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001640 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001642 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001644 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001645 if (curwin->w_skipcol >= width1 + width2)
1646 curwin->w_skipcol -= width2;
1647 else
1648 curwin->w_skipcol -= width1;
1649 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001653 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001654 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001655 --curwin->w_topline;
1656 curwin->w_skipcol = 0;
1657#ifdef FEAT_DIFF
1658 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001660#ifdef FEAT_FOLDING
1661 // A sequence of folded lines only counts for one logical line
1662 if (hasFolding(curwin->w_topline, &first, NULL))
1663 {
1664 ++done;
1665 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001666 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001667 curwin->w_botline -= curwin->w_topline - first;
1668 curwin->w_topline = first;
1669 }
1670 else
1671#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001672 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001673 {
1674 int size = win_linetabsize(curwin, curwin->w_topline,
1675 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1676 if (size > width1)
1677 {
1678 curwin->w_skipcol = width1;
1679 size -= width1;
1680 redraw_later(UPD_NOT_VALID);
1681 }
1682 while (size > width2)
1683 {
1684 curwin->w_skipcol += width2;
1685 size -= width2;
1686 }
1687 ++done;
1688 }
1689 else
1690 done += PLINES_NOFILL(curwin->w_topline);
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 invalidate_botline();
1695 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 curwin->w_wrow += done; // keep w_wrow updated
1697 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699#ifdef FEAT_DIFF
1700 if (curwin->w_cursor.lnum == curwin->w_topline)
1701 curwin->w_cline_row = 0;
1702 check_topfill(curwin, TRUE);
1703#endif
1704
1705 /*
1706 * Compute the row number of the last row of the cursor line
1707 * and move the cursor onto the displayed part of the window.
1708 */
1709 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001710 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
1712 validate_virtcol();
1713 validate_cheight();
1714 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001715 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1718 {
1719#ifdef FEAT_FOLDING
1720 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1721 {
1722 --wrow;
1723 if (first == 1)
1724 curwin->w_cursor.lnum = 1;
1725 else
1726 curwin->w_cursor.lnum = first - 1;
1727 }
1728 else
1729#endif
1730 wrow -= plines(curwin->w_cursor.lnum--);
1731 curwin->w_valid &=
1732 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1733 moved = TRUE;
1734 }
1735 if (moved)
1736 {
1737#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 foldAdjustCursor();
1740#endif
1741 coladvance(curwin->w_curswant);
1742 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001743
1744 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1745 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001746 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001747 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1748
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001749 // make sure the cursor is in the visible text
1750 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001751 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 int row = 0;
1753 if (col >= width1)
1754 {
1755 col -= width1;
1756 ++row;
1757 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001758 if (col > width2 && width2 > 0)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001759 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001760 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001761 // even so col is not used anymore,
1762 // make sure it is correct, just in case
1763 col = col % width2;
1764 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001765 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001766 {
1767 curwin->w_curswant = curwin->w_virtcol
1768 - (row - curwin->w_height + 1) * width2;
1769 coladvance(curwin->w_curswant);
1770 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772}
1773
1774/*
1775 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001778scrollup(
1779 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001780 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001782 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001784 if (do_sms
1785# ifdef FEAT_FOLDING
1786 || (byfold && hasAnyFolding(curwin))
1787# endif
1788# ifdef FEAT_DIFF
1789 || (curwin->w_p_diff && !curwin->w_p_wrap)
1790# endif
1791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001793 int width1 = curwin->w_width - curwin_col_off();
1794 int width2 = width1 + curwin_col_off2();
1795 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001796 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001797
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001798 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001799 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800
1801 // diff mode: first consume "topfill"
1802 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1803 // the line, then advance to the next line.
1804 // folding: count each sequence of folded lines as one logical line.
1805 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807# ifdef FEAT_DIFF
1808 if (curwin->w_topfill > 0)
1809 --curwin->w_topfill;
1810 else
1811# endif
1812 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001813 linenr_T lnum = curwin->w_topline;
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815# ifdef FEAT_FOLDING
1816 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001817 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 (void)hasFolding(lnum, NULL, &lnum);
1819# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001820 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001821 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001822 // 'smoothscroll': increase "w_skipcol" until it goes over
1823 // the end of the line, then advance to the next line.
1824 int add = curwin->w_skipcol > 0 ? width2 : width1;
1825 curwin->w_skipcol += add;
1826 if (curwin->w_skipcol >= size)
1827 {
1828 if (lnum == curbuf->b_ml.ml_line_count)
1829 {
1830 // at the last screen line, can't scroll further
1831 curwin->w_skipcol -= add;
1832 break;
1833 }
1834 ++lnum;
1835 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001836 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001837 else
1838 {
1839 if (lnum >= curbuf->b_ml.ml_line_count)
1840 break;
1841 ++lnum;
1842 }
1843
1844 if (lnum > curwin->w_topline)
1845 {
1846 // approximate w_botline
1847 curwin->w_botline += lnum - curwin->w_topline;
1848 curwin->w_topline = lnum;
1849# ifdef FEAT_DIFF
1850 curwin->w_topfill = diff_check_fill(curwin, lnum);
1851# endif
1852 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001853 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001854 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001855 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001856 }
1857 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001858
zeertzjqd9a92dc2023-06-05 18:41:35 +01001859 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1860 // need to redraw more, because wl_size of the (new) topline may
1861 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001862 redraw_later(UPD_NOT_VALID);
1863 }
1864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001867 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
1870 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1871 curwin->w_topline = curbuf->b_ml.ml_line_count;
1872 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1873 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1874
1875#ifdef FEAT_DIFF
1876 check_topfill(curwin, FALSE);
1877#endif
1878
1879#ifdef FEAT_FOLDING
1880 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001881 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1883#endif
1884
1885 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1886 if (curwin->w_cursor.lnum < curwin->w_topline)
1887 {
1888 curwin->w_cursor.lnum = curwin->w_topline;
1889 curwin->w_valid &=
1890 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1891 coladvance(curwin->w_curswant);
1892 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001893 if (curwin->w_cursor.lnum == curwin->w_topline
1894 && do_sms && curwin->w_skipcol > 0)
1895 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001896 int col_off = curwin_col_off();
1897 int col_off2 = curwin_col_off2();
1898
1899 int width1 = curwin->w_width - col_off;
1900 int width2 = width1 + col_off2;
1901 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001902 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001903 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001904 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001905
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001906 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001907 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001908 int overlap = scrolloff_cols != 0 ? 0
1909 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001910
Bram Moolenaar118c2352022-10-09 17:19:27 +01001911 // Make sure the cursor is in a visible part of the line, taking
1912 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001913 // If there are not enough screen lines put the cursor in the middle.
1914 if (scrolloff_cols > space_cols / 2)
1915 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001916 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001917 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001918 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 colnr_T col = curwin->w_virtcol;
1920
1921 if (col < width1)
1922 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001923 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001924 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001925 curwin->w_curswant = col;
1926 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001927
1928 // validate_virtcol() marked various things as valid, but after
1929 // moving the cursor they need to be recomputed
1930 curwin->w_valid &=
1931 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001932 }
1933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001936/*
1937 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1938 * valid for 'smoothscroll'.
1939 */
1940 void
1941adjust_skipcol(void)
1942{
1943 if (!curwin->w_p_wrap
1944 || !curwin->w_p_sms
1945 || curwin->w_cursor.lnum != curwin->w_topline)
1946 return;
1947
1948 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001949 if (width1 <= 0)
1950 return; // no text will be displayed
1951
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001952 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001953 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001954 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1955 int scrolled = FALSE;
1956
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001957 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001958 if (curwin->w_cline_height == curwin->w_height
1959 // w_cline_height may be capped at w_height, check there aren't
1960 // actually more lines.
1961 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1962 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001963 {
1964 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001965 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001966 return;
1967 }
1968
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001969 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001970 int overlap = sms_marker_overlap(curwin,
1971 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001972 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001973 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001974 {
1975 // scroll a screen line down
1976 if (curwin->w_skipcol >= width1 + width2)
1977 curwin->w_skipcol -= width2;
1978 else
1979 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001980 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001981 }
1982 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001983 {
1984 validate_virtcol();
1985 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001986 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001987 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001988
1989 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1990 int row = 0;
1991 if (col >= width1)
1992 {
1993 col -= width1;
1994 ++row;
1995 }
1996 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001997 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001998 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001999 // col may no longer be used, but make
2000 // sure it is correct anyhow, just in case
2001 col = col % width2;
2002 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002003 if (row >= curwin->w_height)
2004 {
2005 if (curwin->w_skipcol == 0)
2006 {
2007 curwin->w_skipcol += width1;
2008 --row;
2009 }
2010 if (row >= curwin->w_height)
2011 curwin->w_skipcol += (row - curwin->w_height) * width2;
2012 redraw_later(UPD_NOT_VALID);
2013 }
2014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#ifdef FEAT_DIFF
2017/*
2018 * Don't end up with too many filler lines in the window.
2019 */
2020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021check_topfill(
2022 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002023 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int n;
2026
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002027 if (wp->w_topfill <= 0)
2028 return;
2029
2030 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2031 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002033 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002035 --wp->w_topline;
2036 wp->w_topfill = 0;
2037 }
2038 else
2039 {
2040 wp->w_topfill = wp->w_height - n;
2041 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 }
2045}
2046
2047/*
2048 * Use as many filler lines as possible for w_topline. Make sure w_topline
2049 * is still visible.
2050 */
2051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 int n;
2055
2056 n = plines_nofill(curwin->w_topline);
2057 if (n >= curwin->w_height)
2058 curwin->w_topfill = 0;
2059 else
2060 {
2061 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2062 if (curwin->w_topfill + n > curwin->w_height)
2063 curwin->w_topfill = curwin->w_height - n;
2064 }
2065}
2066#endif
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068/*
2069 * Scroll the screen one line down, but don't do it if it would move the
2070 * cursor off the screen.
2071 */
2072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 int end_row;
2076#ifdef FEAT_DIFF
2077 int can_fill = (curwin->w_topfill
2078 < diff_check_fill(curwin, curwin->w_topline));
2079#endif
2080
2081 if (curwin->w_topline <= 1
2082#ifdef FEAT_DIFF
2083 && !can_fill
2084#endif
2085 )
2086 return;
2087
Bram Moolenaar85a20022019-12-21 18:25:54 +01002088 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /*
2091 * Compute the row number of the last row of the cursor line
2092 * and make sure it doesn't go off the screen. Make sure the cursor
2093 * doesn't go past 'scrolloff' lines from the screen end.
2094 */
2095 end_row = curwin->w_wrow;
2096#ifdef FEAT_DIFF
2097 if (can_fill)
2098 ++end_row;
2099 else
2100 end_row += plines_nofill(curwin->w_topline - 1);
2101#else
2102 end_row += plines(curwin->w_topline - 1);
2103#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002104 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 validate_cheight();
2107 validate_virtcol();
2108 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002109 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002111 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113#ifdef FEAT_DIFF
2114 if (can_fill)
2115 {
2116 ++curwin->w_topfill;
2117 check_topfill(curwin, TRUE);
2118 }
2119 else
2120 {
2121 --curwin->w_topline;
2122 curwin->w_topfill = 0;
2123 }
2124#else
2125 --curwin->w_topline;
2126#endif
2127#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002128 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002130 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2132 }
2133}
2134
2135/*
2136 * Scroll the screen one line up, but don't do it if it would move the cursor
2137 * off the screen.
2138 */
2139 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002140scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 int start_row;
2143
2144 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2145#ifdef FEAT_DIFF
2146 && curwin->w_topfill == 0
2147#endif
2148 )
2149 return;
2150
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 /*
2154 * Compute the row number of the first row of the cursor line
2155 * and make sure it doesn't go off the screen. Make sure the cursor
2156 * doesn't go before 'scrolloff' lines from the screen start.
2157 */
2158#ifdef FEAT_DIFF
2159 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2160 - curwin->w_topfill;
2161#else
2162 start_row = curwin->w_wrow - plines(curwin->w_topline);
2163#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002164 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002167 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002169 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171#ifdef FEAT_DIFF
2172 if (curwin->w_topfill > 0)
2173 --curwin->w_topfill;
2174 else
2175#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002176 {
2177#ifdef FEAT_FOLDING
2178 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002181 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2184 }
2185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187/*
2188 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2189 * a (wrapped) text line. Uses and sets "lp->fill".
2190 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002191 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
2193 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002194topline_back_winheight(
2195 lineoff_T *lp,
2196 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198#ifdef FEAT_DIFF
2199 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2200 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ++lp->fill;
2203 lp->height = 1;
2204 }
2205 else
2206#endif
2207 {
2208 --lp->lnum;
2209#ifdef FEAT_DIFF
2210 lp->fill = 0;
2211#endif
2212 if (lp->lnum < 1)
2213 lp->height = MAXCOL;
2214 else
2215#ifdef FEAT_FOLDING
2216 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 lp->height = 1;
2219 else
2220#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002221 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223}
2224
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002225 static void
2226topline_back(lineoff_T *lp)
2227{
2228 topline_back_winheight(lp, TRUE);
2229}
2230
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232/*
2233 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2234 * a (wrapped) text line. Uses and sets "lp->fill".
2235 * Returns the height of the added line in "lp->height".
2236 * Lines below the last one are incredibly high.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241#ifdef FEAT_DIFF
2242 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2243 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002244 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++lp->fill;
2246 lp->height = 1;
2247 }
2248 else
2249#endif
2250 {
2251 ++lp->lnum;
2252#ifdef FEAT_DIFF
2253 lp->fill = 0;
2254#endif
2255 if (lp->lnum > curbuf->b_ml.ml_line_count)
2256 lp->height = MAXCOL;
2257 else
2258#ifdef FEAT_FOLDING
2259 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002260 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 lp->height = 1;
2262 else
2263#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002264 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266}
2267
2268#ifdef FEAT_DIFF
2269/*
2270 * Switch from including filler lines below lp->lnum to including filler
2271 * lines above loff.lnum + 1. This keeps pointing to the same line.
2272 * When there are no filler lines nothing changes.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 if (lp->fill > 0)
2278 {
2279 ++lp->lnum;
2280 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2281 }
2282}
2283
2284/*
2285 * Switch from including filler lines above lp->lnum to including filler
2286 * lines below loff.lnum - 1. This keeps pointing to the same line.
2287 * When there are no filler lines nothing changes.
2288 */
2289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 if (lp->fill > 0)
2293 {
2294 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2295 --lp->lnum;
2296 }
2297}
2298#endif
2299
2300/*
2301 * Recompute topline to put the cursor at the top of the window.
2302 * Scroll at least "min_scroll" lines.
2303 * If "always" is TRUE, always set topline (for "zt").
2304 */
2305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002306scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 int scrolled = 0;
2309 int extra = 0;
2310 int used;
2311 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 linenr_T top; // just above displayed lines
2313 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002315 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_DIFF
2317 linenr_T old_topfill = curwin->w_topfill;
2318#endif
2319 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002320 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (mouse_dragging > 0)
2323 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 /*
2326 * Decrease topline until:
2327 * - it has become 1
2328 * - (part of) the cursor line is moved off the screen or
2329 * - moved at least 'scrolljump' lines and
2330 * - at least 'scrolloff' lines above and below the cursor
2331 */
2332 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (curwin->w_cursor.lnum < curwin->w_topline)
2335 scrolled = used;
2336
2337#ifdef FEAT_FOLDING
2338 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2339 {
2340 --top;
2341 ++bot;
2342 }
2343 else
2344#endif
2345 {
2346 top = curwin->w_cursor.lnum - 1;
2347 bot = curwin->w_cursor.lnum + 1;
2348 }
2349 new_topline = top + 1;
2350
2351#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 // "used" already contains the number of filler lines above, don't add it
2353 // again.
2354 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002355 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357
2358 /*
2359 * Check if the lines from "top" to "bot" fit in the window. If they do,
2360 * set new_topline and advance "top" and "bot" to include more lines.
2361 */
2362 while (top > 0)
2363 {
2364#ifdef FEAT_FOLDING
2365 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002366 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 i = 1;
2368 else
2369#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002370 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002371 if (top < curwin->w_topline)
2372 scrolled += i;
2373
2374 // If scrolling is needed, scroll at least 'sj' lines.
2375 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2376 && extra >= off)
2377 break;
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 used += i;
2380 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2381 {
2382#ifdef FEAT_FOLDING
2383 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002384 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 ++used;
2386 else
2387#endif
2388 used += plines(bot);
2389 }
2390 if (used > curwin->w_height)
2391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 extra += i;
2394 new_topline = top;
2395 --top;
2396 ++bot;
2397 }
2398
2399 /*
2400 * If we don't have enough space, put cursor in the middle.
2401 * This makes sure we get the same position when using "k" and "j"
2402 * in a small window.
2403 */
2404 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002405 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
2407 {
2408 /*
2409 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002410 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 */
2412 if (new_topline < curwin->w_topline || always)
2413 curwin->w_topline = new_topline;
2414 if (curwin->w_topline > curwin->w_cursor.lnum)
2415 curwin->w_topline = curwin->w_cursor.lnum;
2416#ifdef FEAT_DIFF
2417 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2418 if (curwin->w_topfill > 0 && extra > off)
2419 {
2420 curwin->w_topfill -= extra - off;
2421 if (curwin->w_topfill < 0)
2422 curwin->w_topfill = 0;
2423 }
2424 check_topfill(curwin, FALSE);
2425#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002426 if (curwin->w_topline == curwin->w_cursor.lnum)
2427 {
2428 validate_virtcol();
2429 if (curwin->w_skipcol >= curwin->w_virtcol)
2430 // TODO: if the line doesn't fit may optimize w_skipcol instead
2431 // of making it zero
2432 reset_skipcol();
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002435 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_DIFF
2437 || curwin->w_topfill != old_topfill
2438#endif
2439 )
2440 curwin->w_valid &=
2441 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2442 curwin->w_valid |= VALID_TOPLINE;
2443 }
2444}
2445
2446/*
2447 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2448 * screen lines for text lines.
2449 */
2450 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002451set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453#ifdef FEAT_DIFF
2454 wp->w_filler_rows = 0;
2455#endif
2456 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002457 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 else
2459 {
2460 wp->w_empty_rows = wp->w_height - used;
2461#ifdef FEAT_DIFF
2462 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2463 {
2464 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2465 if (wp->w_empty_rows > wp->w_filler_rows)
2466 wp->w_empty_rows -= wp->w_filler_rows;
2467 else
2468 {
2469 wp->w_filler_rows = wp->w_empty_rows;
2470 wp->w_empty_rows = 0;
2471 }
2472 }
2473#endif
2474 }
2475}
2476
2477/*
2478 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002479 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2481 * This is messy stuff!!!
2482 */
2483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002484scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 int used;
2487 int scrolled = 0;
2488 int extra = 0;
2489 int i;
2490 linenr_T line_count;
2491 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002492 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 lineoff_T loff;
2494 lineoff_T boff;
2495#ifdef FEAT_DIFF
2496 int old_topfill = curwin->w_topfill;
2497 int fill_below_window;
2498#endif
2499 linenr_T old_botline = curwin->w_botline;
2500 linenr_T old_valid = curwin->w_valid;
2501 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002502 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002503 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002504 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 cln = curwin->w_cursor.lnum;
2507 if (set_topbot)
2508 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002509 int set_skipcol = FALSE;
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 used = 0;
2512 curwin->w_botline = cln + 1;
2513#ifdef FEAT_DIFF
2514 loff.fill = 0;
2515#endif
2516 for (curwin->w_topline = curwin->w_botline;
2517 curwin->w_topline > 1;
2518 curwin->w_topline = loff.lnum)
2519 {
2520 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002521 topline_back_winheight(&loff, FALSE);
2522 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002524 if (used + loff.height > curwin->w_height)
2525 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002526 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002527 {
2528 // 'smoothscroll' and 'wrap' are set. The above line is
2529 // too long to show in its entirety, so we show just a part
2530 // of it.
2531 if (used < curwin->w_height)
2532 {
2533 int plines_offset = used + loff.height
2534 - curwin->w_height;
2535 used = curwin->w_height;
2536#ifdef FEAT_DIFF
2537 curwin->w_topfill = loff.fill;
2538#endif
2539 curwin->w_topline = loff.lnum;
2540 curwin->w_skipcol = skipcol_from_plines(
2541 curwin, plines_offset);
2542 set_skipcol = TRUE;
2543 }
2544 }
2545 break;
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 used += loff.height;
2548#ifdef FEAT_DIFF
2549 curwin->w_topfill = loff.fill;
2550#endif
2551 }
2552 set_empty_rows(curwin, used);
2553 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2554 if (curwin->w_topline != old_topline
2555#ifdef FEAT_DIFF
2556 || curwin->w_topfill != old_topfill
2557#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002558 || set_skipcol
2559 || curwin->w_skipcol != 0)
2560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002562 if (set_skipcol)
2563 redraw_later(UPD_NOT_VALID);
2564 else
2565 reset_skipcol();
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 else
2569 validate_botline();
2570
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_DIFF
2573 used = plines_nofill(cln);
2574#else
2575 validate_cheight();
2576 used = curwin->w_cline_height;
2577#endif
2578
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002579 // If the cursor is on or below botline, we will at least scroll by the
2580 // height of the cursor line, which is "used". Correct for empty lines,
2581 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (cln >= curwin->w_botline)
2583 {
2584 scrolled = used;
2585 if (cln == curwin->w_botline)
2586 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002587 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002588 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002589 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002590 // Calculate how many screen lines the current top line of window
2591 // occupies. If it is occupying more than the entire window, we
2592 // need to scroll the additional clipped lines to scroll past the
2593 // top line before we can move on to the other lines.
2594 int top_plines =
2595#ifdef FEAT_DIFF
2596 plines_win_nofill
2597#else
2598 plines_win
2599#endif
2600 (curwin, curwin->w_topline, FALSE);
2601 int skip_lines = 0;
2602 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002603 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002604 {
fullwaywang8154e642023-06-24 21:58:09 +01002605 int width2 = width1 + curwin_col_off2();
2606 // similar formula is used in curs_columns()
2607 if (curwin->w_skipcol > width1)
2608 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2609 else if (curwin->w_skipcol > 0)
2610 skip_lines = 1;
2611
2612 top_plines -= skip_lines;
2613 if (top_plines > curwin->w_height)
2614 {
2615 scrolled += (top_plines - curwin->w_height);
2616 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002617 }
2618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
2621 /*
2622 * Stop counting lines to scroll when
2623 * - hitting start of the file
2624 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002625 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 * - lines between botline and cursor have been counted
2627 */
2628#ifdef FEAT_FOLDING
2629 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2630#endif
2631 {
2632 loff.lnum = cln;
2633 boff.lnum = cln;
2634 }
2635#ifdef FEAT_DIFF
2636 loff.fill = 0;
2637 boff.fill = 0;
2638 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2639 - curwin->w_filler_rows;
2640#endif
2641
2642 while (loff.lnum > 1)
2643 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002644 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2645 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002647 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2649 && loff.lnum <= curwin->w_botline
2650#ifdef FEAT_DIFF
2651 && (loff.lnum < curwin->w_botline
2652 || loff.fill >= fill_below_window)
2653#endif
2654 )
2655 break;
2656
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002659 if (loff.height == MAXCOL)
2660 used = MAXCOL;
2661 else
2662 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (used > curwin->w_height)
2664 break;
2665 if (loff.lnum >= curwin->w_botline
2666#ifdef FEAT_DIFF
2667 && (loff.lnum > curwin->w_botline
2668 || loff.fill <= fill_below_window)
2669#endif
2670 )
2671 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002672 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 scrolled += loff.height;
2674 if (loff.lnum == curwin->w_botline
2675#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002676 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677#endif
2678 )
2679 scrolled -= curwin->w_empty_rows;
2680 }
2681
2682 if (boff.lnum < curbuf->b_ml.ml_line_count)
2683 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 botline_forw(&boff);
2686 used += boff.height;
2687 if (used > curwin->w_height)
2688 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002689 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2690 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
2692 extra += boff.height;
2693 if (boff.lnum >= curwin->w_botline
2694#ifdef FEAT_DIFF
2695 || (boff.lnum + 1 == curwin->w_botline
2696 && boff.fill > curwin->w_filler_rows)
2697#endif
2698 )
2699 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 scrolled += boff.height;
2702 if (boff.lnum == curwin->w_botline
2703#ifdef FEAT_DIFF
2704 && boff.fill == 0
2705#endif
2706 )
2707 scrolled -= curwin->w_empty_rows;
2708 }
2709 }
2710 }
2711 }
2712
Bram Moolenaar85a20022019-12-21 18:25:54 +01002713 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (scrolled <= 0)
2715 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002716 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 else if (used > curwin->w_height)
2718 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002719 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 else
2721 {
2722 line_count = 0;
2723#ifdef FEAT_DIFF
2724 boff.fill = curwin->w_topfill;
2725#endif
2726 boff.lnum = curwin->w_topline - 1;
2727 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2728 {
2729 botline_forw(&boff);
2730 i += boff.height;
2731 ++line_count;
2732 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002733 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 line_count = 9999;
2735 }
2736
2737 /*
2738 * Scroll up if the cursor is off the bottom of the screen a bit.
2739 * Otherwise put it at 1/2 of the screen.
2740 */
2741 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002742 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002743 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002744 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002745 if (do_sms)
2746 scrollup(scrolled, TRUE); // TODO
2747 else
2748 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 /*
2752 * If topline didn't change we need to restore w_botline and w_empty_rows
2753 * (we changed them).
2754 * If topline did change, update_screen() will set botline.
2755 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002756 if (curwin->w_topline == old_topline
2757 && curwin->w_skipcol == old_skipcol
2758 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 curwin->w_botline = old_botline;
2761 curwin->w_empty_rows = old_empty_rows;
2762 curwin->w_valid = old_valid;
2763 }
2764 curwin->w_valid |= VALID_TOPLINE;
2765}
2766
2767/*
2768 * Recompute topline to put the cursor halfway the window
2769 * If "atend" is TRUE, also put it halfway at the end of the file.
2770 */
2771 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002772scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 int above = 0;
2775 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002776 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#ifdef FEAT_DIFF
2778 int topfill = 0;
2779#endif
2780 int below = 0;
2781 int used;
2782 lineoff_T loff;
2783 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002784#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002785 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002788#ifdef FEAT_PROP_POPUP
2789 // if the width changed this needs to be updated first
2790 may_update_popup_position();
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2793#ifdef FEAT_FOLDING
2794 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2795#endif
2796#ifdef FEAT_DIFF
2797 used = plines_nofill(loff.lnum);
2798 loff.fill = 0;
2799 boff.fill = 0;
2800#else
2801 used = plines(loff.lnum);
2802#endif
2803 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002804
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002805 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002806 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2807 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002808 {
2809 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002810 if (atend)
2811 {
2812 want_height = (curwin->w_height - used) / 2;
2813 used = 0;
2814 }
2815 else
2816 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002817 }
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 while (topline > 1)
2820 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002821 // If using smoothscroll, we can precisely scroll to the
2822 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002823 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824 {
2825 topline_back_winheight(&loff, FALSE);
2826 if (loff.height == MAXCOL)
2827 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002828 used += loff.height;
2829 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002830 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002831 botline_forw(&boff);
2832 used += boff.height;
2833 }
2834 if (used > want_height)
2835 {
2836 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002837 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002838 topline = loff.lnum;
2839#ifdef FEAT_DIFF
2840 topfill = loff.fill;
2841#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002842 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002843 }
2844 break;
2845 }
2846 topline = loff.lnum;
2847#ifdef FEAT_DIFF
2848 topfill = loff.fill;
2849#endif
2850 continue;
2851 }
2852
2853 // If not using smoothscroll, we have to iteratively find how many
2854 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002855 // This may not be right in the middle if the lines'
2856 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002857
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002858 // Depending on "prefer_above" we add a line above or below first.
2859 // Loop twice to avoid duplicating code.
2860 int done = FALSE;
2861 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002863 if (prefer_above ? (round == 2 && below < above)
2864 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002866 // add a line below the cursor
2867 if (boff.lnum < curbuf->b_ml.ml_line_count)
2868 {
2869 botline_forw(&boff);
2870 used += boff.height;
2871 if (used > curwin->w_height)
2872 {
2873 done = TRUE;
2874 break;
2875 }
2876 below += boff.height;
2877 }
2878 else
2879 {
2880 ++below; // count a "~" line
2881 if (atend)
2882 ++used;
2883 }
2884 }
2885
2886 if (prefer_above ? (round == 1 && below >= above)
2887 : (round == 1 && below > above))
2888 {
2889 // add a line above the cursor
2890 topline_back(&loff);
2891 if (loff.height == MAXCOL)
2892 used = MAXCOL;
2893 else
2894 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002896 {
2897 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002899 }
2900 above += loff.height;
2901 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002903 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002907 if (done)
2908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#ifdef FEAT_FOLDING
2912 if (!hasFolding(topline, &curwin->w_topline, NULL))
2913#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002914 {
2915 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002916 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002917 || curwin->w_skipcol != 0)
2918 {
2919 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002920 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002921 {
2922 curwin->w_skipcol = skipcol;
2923 redraw_later(UPD_NOT_VALID);
2924 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002925 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002926 reset_skipcol();
2927 }
2928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#ifdef FEAT_DIFF
2930 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002931 if (old_topline > curwin->w_topline + curwin->w_height)
2932 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 check_topfill(curwin, FALSE);
2934#endif
2935 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2936 curwin->w_valid |= VALID_TOPLINE;
2937}
2938
2939/*
2940 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002941 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 * If not possible, put it at the same position as scroll_cursor_halfway().
2943 * When called topline must be valid!
2944 */
2945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002946cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002950 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 linenr_T botline;
2952 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002955 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 /*
2958 * How many lines we would like to have above/below the cursor depends on
2959 * whether the first/last line of the file is on screen.
2960 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002961 above_wanted = so;
2962 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002963 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 above_wanted = mouse_dragging - 1;
2966 below_wanted = mouse_dragging - 1;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (curwin->w_topline == 1)
2969 {
2970 above_wanted = 0;
2971 max_off = curwin->w_height / 2;
2972 if (below_wanted > max_off)
2973 below_wanted = max_off;
2974 }
2975 validate_botline();
2976 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002977 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 below_wanted = 0;
2980 max_off = (curwin->w_height - 1) / 2;
2981 if (above_wanted > max_off)
2982 above_wanted = max_off;
2983 }
2984
2985 /*
2986 * If there are sufficient file-lines above and below the cursor, we can
2987 * return now.
2988 */
2989 cln = curwin->w_cursor.lnum;
2990 if (cln >= curwin->w_topline + above_wanted
2991 && cln < curwin->w_botline - below_wanted
2992#ifdef FEAT_FOLDING
2993 && !hasAnyFolding(curwin)
2994#endif
2995 )
2996 return;
2997
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002998 if (curwin->w_p_sms && !curwin->w_p_wrap)
2999 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01003000 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003001 if (curwin->w_cline_height == curwin->w_height)
3002 {
3003 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003004 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003005 return;
3006 }
3007 // TODO: If the cursor line doesn't fit in the window then only adjust
3008 // w_skipcol.
3009 }
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 /*
3012 * Narrow down the area where the cursor can be put by taking lines from
3013 * the top and the bottom until:
3014 * - the desired context lines are found
3015 * - the lines from the top is past the lines from the bottom
3016 */
3017 topline = curwin->w_topline;
3018 botline = curwin->w_botline - 1;
3019#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003020 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 above = curwin->w_topfill;
3022 below = curwin->w_filler_rows;
3023#endif
3024 while ((above < above_wanted || below < below_wanted) && topline < botline)
3025 {
3026 if (below < below_wanted && (below <= above || above >= above_wanted))
3027 {
3028#ifdef FEAT_FOLDING
3029 if (hasFolding(botline, &botline, NULL))
3030 ++below;
3031 else
3032#endif
3033 below += plines(botline);
3034 --botline;
3035 }
3036 if (above < above_wanted && (above < below || below >= below_wanted))
3037 {
3038#ifdef FEAT_FOLDING
3039 if (hasFolding(topline, NULL, &topline))
3040 ++above;
3041 else
3042#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003043 above += PLINES_NOFILL(topline);
3044#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003045 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (topline < botline)
3047 above += diff_check_fill(curwin, topline + 1);
3048#endif
3049 ++topline;
3050 }
3051 }
3052 if (topline == botline || botline == 0)
3053 curwin->w_cursor.lnum = topline;
3054 else if (topline > botline)
3055 curwin->w_cursor.lnum = botline;
3056 else
3057 {
3058 if (cln < topline && curwin->w_topline > 1)
3059 {
3060 curwin->w_cursor.lnum = topline;
3061 curwin->w_valid &=
3062 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3063 }
3064 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3065 {
3066 curwin->w_cursor.lnum = botline;
3067 curwin->w_valid &=
3068 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3069 }
3070 }
3071 curwin->w_valid |= VALID_TOPLINE;
3072}
3073
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003074static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003077 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3078 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003080 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 */
3082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003083onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 long n;
3086 int retval = OK;
3087 lineoff_T loff;
3088 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003089 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaar85a20022019-12-21 18:25:54 +01003091 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 beep_flush();
3094 return FAIL;
3095 }
3096
3097 for ( ; count > 0; --count)
3098 {
3099 validate_botline();
3100 /*
3101 * It's an error to move a page up when the first line is already on
3102 * the screen. It's an error to move a page down when the last line
3103 * is on the screen and the topline is 'scrolloff' lines from the
3104 * last line.
3105 */
3106 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003107 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3109 : (curwin->w_topline == 1
3110#ifdef FEAT_DIFF
3111 && curwin->w_topfill ==
3112 diff_check_fill(curwin, curwin->w_topline)
3113#endif
3114 ))
3115 {
3116 beep_flush();
3117 retval = FAIL;
3118 break;
3119 }
3120
3121#ifdef FEAT_DIFF
3122 loff.fill = 0;
3123#endif
3124 if (dir == FORWARD)
3125 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003126 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003128 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003129 if (p_window <= 2)
3130 ++curwin->w_topline;
3131 else
3132 curwin->w_topline += p_window - 2;
3133 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3134 curwin->w_topline = curbuf->b_ml.ml_line_count;
3135 curwin->w_cursor.lnum = curwin->w_topline;
3136 }
3137 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3138 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003139 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 curwin->w_topline = curbuf->b_ml.ml_line_count;
3141#ifdef FEAT_DIFF
3142 curwin->w_topfill = 0;
3143#endif
3144 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3145 }
3146 else
3147 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003148 // For the overlap, start with the line just below the window
3149 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 loff.lnum = curwin->w_botline;
3151#ifdef FEAT_DIFF
3152 loff.fill = diff_check_fill(curwin, loff.lnum)
3153 - curwin->w_filler_rows;
3154#endif
3155 get_scroll_overlap(&loff, -1);
3156 curwin->w_topline = loff.lnum;
3157#ifdef FEAT_DIFF
3158 curwin->w_topfill = loff.fill;
3159 check_topfill(curwin, FALSE);
3160#endif
3161 curwin->w_cursor.lnum = curwin->w_topline;
3162 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3163 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3164 }
3165 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003166 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168#ifdef FEAT_DIFF
3169 if (curwin->w_topline == 1)
3170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 max_topfill();
3173 continue;
3174 }
3175#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003176 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003177 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003178 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003179 if (p_window <= 2)
3180 --curwin->w_topline;
3181 else
3182 curwin->w_topline -= p_window - 2;
3183 if (curwin->w_topline < 1)
3184 curwin->w_topline = 1;
3185 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3186 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3187 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3188 continue;
3189 }
3190
Bram Moolenaar85a20022019-12-21 18:25:54 +01003191 // Find the line at the top of the window that is going to be the
3192 // line at the bottom of the window. Make sure this results in
3193 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 loff.lnum = curwin->w_topline - 1;
3195#ifdef FEAT_DIFF
3196 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3197 - curwin->w_topfill;
3198#endif
3199 get_scroll_overlap(&loff, 1);
3200
3201 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3202 {
3203 loff.lnum = curbuf->b_ml.ml_line_count;
3204#ifdef FEAT_DIFF
3205 loff.fill = 0;
3206 }
3207 else
3208 {
3209 botline_topline(&loff);
3210#endif
3211 }
3212 curwin->w_cursor.lnum = loff.lnum;
3213
Bram Moolenaar85a20022019-12-21 18:25:54 +01003214 // Find the line just above the new topline to get the right line
3215 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 n = 0;
3217 while (n <= curwin->w_height && loff.lnum >= 1)
3218 {
3219 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003220 if (loff.height == MAXCOL)
3221 n = MAXCOL;
3222 else
3223 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003225 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 curwin->w_topline = 1;
3228#ifdef FEAT_DIFF
3229 max_topfill();
3230#endif
3231 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3232 }
3233 else
3234 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#ifdef FEAT_DIFF
3237 topline_botline(&loff);
3238#endif
3239 botline_forw(&loff);
3240 botline_forw(&loff);
3241#ifdef FEAT_DIFF
3242 botline_topline(&loff);
3243#endif
3244#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003245 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3247#endif
3248
Bram Moolenaar85a20022019-12-21 18:25:54 +01003249 // Always scroll at least one line. Avoid getting stuck on
3250 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (loff.lnum >= curwin->w_topline
3252#ifdef FEAT_DIFF
3253 && (loff.lnum > curwin->w_topline
3254 || loff.fill >= curwin->w_topfill)
3255#endif
3256 )
3257 {
3258#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003259 // First try using the maximum number of filler lines. If
3260 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 loff.fill = curwin->w_topfill;
3262 if (curwin->w_topfill < diff_check_fill(curwin,
3263 curwin->w_topline))
3264 max_topfill();
3265 if (curwin->w_topfill == loff.fill)
3266#endif
3267 {
3268 --curwin->w_topline;
3269#ifdef FEAT_DIFF
3270 curwin->w_topfill = 0;
3271#endif
zeertzjq05834912023-10-04 20:12:37 +02003272 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 comp_botline(curwin);
3275 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003276 curwin->w_valid &=
3277 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 else
3280 {
3281 curwin->w_topline = loff.lnum;
3282#ifdef FEAT_DIFF
3283 curwin->w_topfill = loff.fill;
3284 check_topfill(curwin, FALSE);
3285#endif
3286 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3287 }
3288 }
3289 }
3290 }
3291#ifdef FEAT_FOLDING
3292 foldAdjustCursor();
3293#endif
3294 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003295 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003296 if (retval == OK)
3297 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3299
Bram Moolenaar907dad72018-07-10 15:07:15 +02003300 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003302 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3303 // But make sure we scroll at least one line (happens with mix of long
3304 // wrapping lines and non-wrapping line).
3305 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003307 scroll_cursor_top(1, FALSE);
3308 if (curwin->w_topline <= old_topline
3309 && old_topline < curbuf->b_ml.ml_line_count)
3310 {
3311 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003313 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3314#endif
3315 }
3316 }
3317#ifdef FEAT_FOLDING
3318 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003323 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 return retval;
3325}
3326
3327/*
3328 * Decide how much overlap to use for page-up or page-down scrolling.
3329 * This is symmetric, so that doing both keeps the same lines displayed.
3330 * Three lines are examined:
3331 *
3332 * before CTRL-F after CTRL-F / before CTRL-B
3333 * etc. l1
3334 * l1 last but one line ------------
3335 * l2 last text line l2 top text line
3336 * ------------- l3 second text line
3337 * l3 etc.
3338 */
3339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int h1, h2, h3, h4;
3343 int min_height = curwin->w_height - 2;
3344 lineoff_T loff0, loff1, loff2;
3345
3346#ifdef FEAT_DIFF
3347 if (lp->fill > 0)
3348 lp->height = 1;
3349 else
3350 lp->height = plines_nofill(lp->lnum);
3351#else
3352 lp->height = plines(lp->lnum);
3353#endif
3354 h1 = lp->height;
3355 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003356 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 loff0 = *lp;
3359 if (dir > 0)
3360 botline_forw(lp);
3361 else
3362 topline_back(lp);
3363 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003364 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 return;
3368 }
3369
3370 loff1 = *lp;
3371 if (dir > 0)
3372 botline_forw(lp);
3373 else
3374 topline_back(lp);
3375 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003376 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 return;
3380 }
3381
3382 loff2 = *lp;
3383 if (dir > 0)
3384 botline_forw(lp);
3385 else
3386 topline_back(lp);
3387 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003388 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003389 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003391 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392}
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394/*
3395 * Scroll 'scroll' lines up or down.
3396 */
3397 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003398halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
3400 long scrolled = 0;
3401 int i;
3402 int n;
3403 int room;
3404
3405 if (Prenum)
3406 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3407 curwin->w_height : Prenum;
3408 n = (curwin->w_p_scr <= curwin->w_height) ?
3409 curwin->w_p_scr : curwin->w_height;
3410
Bram Moolenaard5d37532017-03-27 23:02:07 +02003411 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 validate_botline();
3413 room = curwin->w_empty_rows;
3414#ifdef FEAT_DIFF
3415 room += curwin->w_filler_rows;
3416#endif
3417 if (flag)
3418 {
3419 /*
3420 * scroll the text up
3421 */
3422 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3423 {
3424#ifdef FEAT_DIFF
3425 if (curwin->w_topfill > 0)
3426 {
3427 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003428 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 --curwin->w_topfill;
3430 }
3431 else
3432#endif
3433 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003434 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 n -= i;
3436 if (n < 0 && scrolled > 0)
3437 break;
3438#ifdef FEAT_FOLDING
3439 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3440#endif
3441 ++curwin->w_topline;
3442#ifdef FEAT_DIFF
3443 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3447 {
3448 ++curwin->w_cursor.lnum;
3449 curwin->w_valid &=
3450 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3454 scrolled += i;
3455
3456 /*
3457 * Correct w_botline for changed w_topline.
3458 * Won't work when there are filler lines.
3459 */
3460#ifdef FEAT_DIFF
3461 if (curwin->w_p_diff)
3462 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3463 else
3464#endif
3465 {
3466 room += i;
3467 do
3468 {
3469 i = plines(curwin->w_botline);
3470 if (i > room)
3471 break;
3472#ifdef FEAT_FOLDING
3473 (void)hasFolding(curwin->w_botline, NULL,
3474 &curwin->w_botline);
3475#endif
3476 ++curwin->w_botline;
3477 room -= i;
3478 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3479 }
3480 }
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /*
3483 * When hit bottom of the file: move cursor down.
3484 */
3485 if (n > 0)
3486 {
3487# ifdef FEAT_FOLDING
3488 if (hasAnyFolding(curwin))
3489 {
3490 while (--n >= 0
3491 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3492 {
3493 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3494 &curwin->w_cursor.lnum);
3495 ++curwin->w_cursor.lnum;
3496 }
3497 }
3498 else
3499# endif
3500 curwin->w_cursor.lnum += n;
3501 check_cursor_lnum();
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 else
3505 {
3506 /*
3507 * scroll the text down
3508 */
3509 while (n > 0 && curwin->w_topline > 1)
3510 {
3511#ifdef FEAT_DIFF
3512 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3513 {
3514 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003515 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 ++curwin->w_topfill;
3517 }
3518 else
3519#endif
3520 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003521 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 n -= i;
3523 if (n < 0 && scrolled > 0)
3524 break;
3525 --curwin->w_topline;
3526#ifdef FEAT_FOLDING
3527 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3528#endif
3529#ifdef FEAT_DIFF
3530 curwin->w_topfill = 0;
3531#endif
3532 }
3533 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3534 VALID_BOTLINE|VALID_BOTLINE_AP);
3535 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (curwin->w_cursor.lnum > 1)
3537 {
3538 --curwin->w_cursor.lnum;
3539 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * When hit top of the file: move cursor up.
3545 */
3546 if (n > 0)
3547 {
3548 if (curwin->w_cursor.lnum <= (linenr_T)n)
3549 curwin->w_cursor.lnum = 1;
3550 else
3551# ifdef FEAT_FOLDING
3552 if (hasAnyFolding(curwin))
3553 {
3554 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3555 {
3556 --curwin->w_cursor.lnum;
3557 (void)hasFolding(curwin->w_cursor.lnum,
3558 &curwin->w_cursor.lnum, NULL);
3559 }
3560 }
3561 else
3562# endif
3563 curwin->w_cursor.lnum -= n;
3564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003567 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 foldAdjustCursor();
3569# endif
3570#ifdef FEAT_DIFF
3571 check_topfill(curwin, !flag);
3572#endif
3573 cursor_correct();
3574 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003575 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577
Bram Moolenaar860cae12010-06-05 23:22:07 +02003578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003579do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003580{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003581 static win_T *prev_curwin = NULL;
3582 static pos_T prev_cursor = {0, 0, 0};
3583
3584 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3585 return;
3586 prev_curwin = curwin;
3587 prev_cursor = curwin->w_cursor;
3588
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003590 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003591 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003592 colnr_T curswant = curwin->w_curswant;
3593 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 win_T *old_curwin = curwin;
3595 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 int old_VIsual_select = VIsual_select;
3597 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598
3599 /*
3600 * loop through the cursorbound windows
3601 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003603 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 {
3605 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003606 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607 if (curwin != old_curwin && curwin->w_p_crb)
3608 {
3609# ifdef FEAT_DIFF
3610 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003611 curwin->w_cursor.lnum =
3612 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003613 else
3614# endif
3615 curwin->w_cursor.lnum = line;
3616 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003617 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003618 curwin->w_curswant = curswant;
3619 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620
Bram Moolenaar85a20022019-12-21 18:25:54 +01003621 // Make sure the cursor is in a valid position. Temporarily set
3622 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003623 int restart_edit_save = restart_edit;
3624 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003626
3627 // Avoid a scroll here for the cursor position, 'scrollbind' is
3628 // more important.
3629 if (!curwin->w_p_scb)
3630 validate_cursor();
3631
Bram Moolenaar61452852011-02-01 18:01:11 +01003632 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003633 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003634 if (has_mbyte)
3635 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003636 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003637
Bram Moolenaar85a20022019-12-21 18:25:54 +01003638 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003639 if (!curwin->w_p_scb)
3640 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003641 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003642 }
3643 }
3644
3645 /*
3646 * reset current-window
3647 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003648 VIsual_select = old_VIsual_select;
3649 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003650 curwin = old_curwin;
3651 curbuf = old_curbuf;
3652}