blob: a0d3a6014a9d934504bbb71576b926d956149c7e [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{
1550 int offset = vcol2col(wp, lnum, vcol);
1551 char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1552 char_u *p = line + offset;
1553
1554 // For a multibyte character, need to return the column number of the first
1555 // byte.
1556 MB_PTR_BACK(line, p);
1557
1558 return (int)(p - line + 1);
1559}
1560
1561/*
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001562 * "virtcol2col({winid}, {lnum}, {col})" function
1563 */
1564 void
1565f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1566{
1567 win_T *wp;
1568 linenr_T lnum;
1569 int screencol;
1570 int error = FALSE;
1571
1572 rettv->vval.v_number = -1;
1573
1574 if (check_for_number_arg(argvars, 0) == FAIL
1575 || check_for_number_arg(argvars, 1) == FAIL
1576 || check_for_number_arg(argvars, 2) == FAIL)
1577 return;
1578
1579 wp = find_win_by_nr_or_id(&argvars[0]);
1580 if (wp == NULL)
1581 return;
1582
1583 lnum = tv_get_number_chk(&argvars[1], &error);
1584 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1585 return;
1586
1587 screencol = tv_get_number_chk(&argvars[2], &error);
1588 if (error || screencol < 0)
1589 return;
1590
Yegappan Lakshmananb209b862023-08-15 23:01:44 +02001591 rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001592}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001593#endif
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595/*
1596 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001599scrolldown(
1600 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 int wrow;
1605 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001606 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001607 int width1 = 0;
1608 int width2 = 0;
1609
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001610 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001611 {
1612 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001613 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616#ifdef FEAT_FOLDING
1617 linenr_T first;
1618
Bram Moolenaar85a20022019-12-21 18:25:54 +01001619 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1621#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001622 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001623 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001626 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1627 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 ++curwin->w_topfill;
1630 ++done;
1631 }
1632 else
1633#endif
1634 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001635 // break when at the very top
1636 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001637 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001639 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001641 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001642 if (curwin->w_skipcol >= width1 + width2)
1643 curwin->w_skipcol -= width2;
1644 else
1645 curwin->w_skipcol -= width1;
1646 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
1649 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001651 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001652 --curwin->w_topline;
1653 curwin->w_skipcol = 0;
1654#ifdef FEAT_DIFF
1655 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001657#ifdef FEAT_FOLDING
1658 // A sequence of folded lines only counts for one logical line
1659 if (hasFolding(curwin->w_topline, &first, NULL))
1660 {
1661 ++done;
1662 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001663 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001664 curwin->w_botline -= curwin->w_topline - first;
1665 curwin->w_topline = first;
1666 }
1667 else
1668#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001669 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001670 {
1671 int size = win_linetabsize(curwin, curwin->w_topline,
1672 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1673 if (size > width1)
1674 {
1675 curwin->w_skipcol = width1;
1676 size -= width1;
1677 redraw_later(UPD_NOT_VALID);
1678 }
1679 while (size > width2)
1680 {
1681 curwin->w_skipcol += width2;
1682 size -= width2;
1683 }
1684 ++done;
1685 }
1686 else
1687 done += PLINES_NOFILL(curwin->w_topline);
1688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001690 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 invalidate_botline();
1692 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 curwin->w_wrow += done; // keep w_wrow updated
1694 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696#ifdef FEAT_DIFF
1697 if (curwin->w_cursor.lnum == curwin->w_topline)
1698 curwin->w_cline_row = 0;
1699 check_topfill(curwin, TRUE);
1700#endif
1701
1702 /*
1703 * Compute the row number of the last row of the cursor line
1704 * and move the cursor onto the displayed part of the window.
1705 */
1706 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001707 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
1709 validate_virtcol();
1710 validate_cheight();
1711 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001712 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1715 {
1716#ifdef FEAT_FOLDING
1717 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1718 {
1719 --wrow;
1720 if (first == 1)
1721 curwin->w_cursor.lnum = 1;
1722 else
1723 curwin->w_cursor.lnum = first - 1;
1724 }
1725 else
1726#endif
1727 wrow -= plines(curwin->w_cursor.lnum--);
1728 curwin->w_valid &=
1729 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1730 moved = TRUE;
1731 }
1732 if (moved)
1733 {
1734#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001735 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 foldAdjustCursor();
1737#endif
1738 coladvance(curwin->w_curswant);
1739 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001740
1741 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1742 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001743 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001744 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1745
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001746 // make sure the cursor is in the visible text
1747 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001748 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001749 int row = 0;
1750 if (col >= width1)
1751 {
1752 col -= width1;
1753 ++row;
1754 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001755 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001756 row += col / width2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001757 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001758 {
1759 curwin->w_curswant = curwin->w_virtcol
1760 - (row - curwin->w_height + 1) * width2;
1761 coladvance(curwin->w_curswant);
1762 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
1766/*
1767 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001770scrollup(
1771 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001772 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001774 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001776 if (do_sms
1777# ifdef FEAT_FOLDING
1778 || (byfold && hasAnyFolding(curwin))
1779# endif
1780# ifdef FEAT_DIFF
1781 || (curwin->w_p_diff && !curwin->w_p_wrap)
1782# endif
1783 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001785 int width1 = curwin->w_width - curwin_col_off();
1786 int width2 = width1 + curwin_col_off2();
1787 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001788 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001789
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001790 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001791 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001792
1793 // diff mode: first consume "topfill"
1794 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1795 // the line, then advance to the next line.
1796 // folding: count each sequence of folded lines as one logical line.
1797 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
1799# ifdef FEAT_DIFF
1800 if (curwin->w_topfill > 0)
1801 --curwin->w_topfill;
1802 else
1803# endif
1804 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001805 linenr_T lnum = curwin->w_topline;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807# ifdef FEAT_FOLDING
1808 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001809 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 (void)hasFolding(lnum, NULL, &lnum);
1811# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001812 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001813 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001814 // 'smoothscroll': increase "w_skipcol" until it goes over
1815 // the end of the line, then advance to the next line.
1816 int add = curwin->w_skipcol > 0 ? width2 : width1;
1817 curwin->w_skipcol += add;
1818 if (curwin->w_skipcol >= size)
1819 {
1820 if (lnum == curbuf->b_ml.ml_line_count)
1821 {
1822 // at the last screen line, can't scroll further
1823 curwin->w_skipcol -= add;
1824 break;
1825 }
1826 ++lnum;
1827 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001828 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001829 else
1830 {
1831 if (lnum >= curbuf->b_ml.ml_line_count)
1832 break;
1833 ++lnum;
1834 }
1835
1836 if (lnum > curwin->w_topline)
1837 {
1838 // approximate w_botline
1839 curwin->w_botline += lnum - curwin->w_topline;
1840 curwin->w_topline = lnum;
1841# ifdef FEAT_DIFF
1842 curwin->w_topfill = diff_check_fill(curwin, lnum);
1843# endif
1844 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001845 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001846 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001847 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001848 }
1849 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001850
zeertzjqd9a92dc2023-06-05 18:41:35 +01001851 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1852 // need to redraw more, because wl_size of the (new) topline may
1853 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001854 redraw_later(UPD_NOT_VALID);
1855 }
1856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001859 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861
1862 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1863 curwin->w_topline = curbuf->b_ml.ml_line_count;
1864 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1865 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1866
1867#ifdef FEAT_DIFF
1868 check_topfill(curwin, FALSE);
1869#endif
1870
1871#ifdef FEAT_FOLDING
1872 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1875#endif
1876
1877 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1878 if (curwin->w_cursor.lnum < curwin->w_topline)
1879 {
1880 curwin->w_cursor.lnum = curwin->w_topline;
1881 curwin->w_valid &=
1882 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1883 coladvance(curwin->w_curswant);
1884 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001885 if (curwin->w_cursor.lnum == curwin->w_topline
1886 && do_sms && curwin->w_skipcol > 0)
1887 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001888 int col_off = curwin_col_off();
1889 int col_off2 = curwin_col_off2();
1890
1891 int width1 = curwin->w_width - col_off;
1892 int width2 = width1 + col_off2;
1893 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001894 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001895 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001896 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001897
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001898 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001899 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001900 int overlap = scrolloff_cols != 0 ? 0
1901 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001902
Bram Moolenaar118c2352022-10-09 17:19:27 +01001903 // Make sure the cursor is in a visible part of the line, taking
1904 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001905 // If there are not enough screen lines put the cursor in the middle.
1906 if (scrolloff_cols > space_cols / 2)
1907 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001909 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001910 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 colnr_T col = curwin->w_virtcol;
1912
1913 if (col < width1)
1914 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001915 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001916 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001917 curwin->w_curswant = col;
1918 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001919
1920 // validate_virtcol() marked various things as valid, but after
1921 // moving the cursor they need to be recomputed
1922 curwin->w_valid &=
1923 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001924 }
1925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926}
1927
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001928/*
1929 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1930 * valid for 'smoothscroll'.
1931 */
1932 void
1933adjust_skipcol(void)
1934{
1935 if (!curwin->w_p_wrap
1936 || !curwin->w_p_sms
1937 || curwin->w_cursor.lnum != curwin->w_topline)
1938 return;
1939
1940 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001941 if (width1 <= 0)
1942 return; // no text will be displayed
1943
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001944 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001945 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001946 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1947 int scrolled = FALSE;
1948
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001949 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001950 if (curwin->w_cline_height == curwin->w_height
1951 // w_cline_height may be capped at w_height, check there aren't
1952 // actually more lines.
1953 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1954 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001955 {
1956 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001957 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001958 return;
1959 }
1960
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001961 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001962 int overlap = sms_marker_overlap(curwin,
1963 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001964 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001965 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001966 {
1967 // scroll a screen line down
1968 if (curwin->w_skipcol >= width1 + width2)
1969 curwin->w_skipcol -= width2;
1970 else
1971 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001972 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001973 }
1974 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001975 {
1976 validate_virtcol();
1977 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001978 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001979 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001980
1981 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1982 int row = 0;
1983 if (col >= width1)
1984 {
1985 col -= width1;
1986 ++row;
1987 }
1988 if (col > width2)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001989 row += col / width2;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001990 if (row >= curwin->w_height)
1991 {
1992 if (curwin->w_skipcol == 0)
1993 {
1994 curwin->w_skipcol += width1;
1995 --row;
1996 }
1997 if (row >= curwin->w_height)
1998 curwin->w_skipcol += (row - curwin->w_height) * width2;
1999 redraw_later(UPD_NOT_VALID);
2000 }
2001}
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#ifdef FEAT_DIFF
2004/*
2005 * Don't end up with too many filler lines in the window.
2006 */
2007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008check_topfill(
2009 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002010 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 int n;
2013
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002014 if (wp->w_topfill <= 0)
2015 return;
2016
2017 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2018 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002022 --wp->w_topline;
2023 wp->w_topfill = 0;
2024 }
2025 else
2026 {
2027 wp->w_topfill = wp->w_height - n;
2028 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 }
2032}
2033
2034/*
2035 * Use as many filler lines as possible for w_topline. Make sure w_topline
2036 * is still visible.
2037 */
2038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002039max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
2041 int n;
2042
2043 n = plines_nofill(curwin->w_topline);
2044 if (n >= curwin->w_height)
2045 curwin->w_topfill = 0;
2046 else
2047 {
2048 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2049 if (curwin->w_topfill + n > curwin->w_height)
2050 curwin->w_topfill = curwin->w_height - n;
2051 }
2052}
2053#endif
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/*
2056 * Scroll the screen one line down, but don't do it if it would move the
2057 * cursor off the screen.
2058 */
2059 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002060scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
2062 int end_row;
2063#ifdef FEAT_DIFF
2064 int can_fill = (curwin->w_topfill
2065 < diff_check_fill(curwin, curwin->w_topline));
2066#endif
2067
2068 if (curwin->w_topline <= 1
2069#ifdef FEAT_DIFF
2070 && !can_fill
2071#endif
2072 )
2073 return;
2074
Bram Moolenaar85a20022019-12-21 18:25:54 +01002075 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 /*
2078 * Compute the row number of the last row of the cursor line
2079 * and make sure it doesn't go off the screen. Make sure the cursor
2080 * doesn't go past 'scrolloff' lines from the screen end.
2081 */
2082 end_row = curwin->w_wrow;
2083#ifdef FEAT_DIFF
2084 if (can_fill)
2085 ++end_row;
2086 else
2087 end_row += plines_nofill(curwin->w_topline - 1);
2088#else
2089 end_row += plines(curwin->w_topline - 1);
2090#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002091 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 validate_cheight();
2094 validate_virtcol();
2095 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002096 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002098 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100#ifdef FEAT_DIFF
2101 if (can_fill)
2102 {
2103 ++curwin->w_topfill;
2104 check_topfill(curwin, TRUE);
2105 }
2106 else
2107 {
2108 --curwin->w_topline;
2109 curwin->w_topfill = 0;
2110 }
2111#else
2112 --curwin->w_topline;
2113#endif
2114#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002115 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002117 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2119 }
2120}
2121
2122/*
2123 * Scroll the screen one line up, but don't do it if it would move the cursor
2124 * off the screen.
2125 */
2126 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002127scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 int start_row;
2130
2131 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2132#ifdef FEAT_DIFF
2133 && curwin->w_topfill == 0
2134#endif
2135 )
2136 return;
2137
Bram Moolenaar85a20022019-12-21 18:25:54 +01002138 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 /*
2141 * Compute the row number of the first row of the cursor line
2142 * and make sure it doesn't go off the screen. Make sure the cursor
2143 * doesn't go before 'scrolloff' lines from the screen start.
2144 */
2145#ifdef FEAT_DIFF
2146 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2147 - curwin->w_topfill;
2148#else
2149 start_row = curwin->w_wrow - plines(curwin->w_topline);
2150#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002151 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002154 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002156 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158#ifdef FEAT_DIFF
2159 if (curwin->w_topfill > 0)
2160 --curwin->w_topfill;
2161 else
2162#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002163 {
2164#ifdef FEAT_FOLDING
2165 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002168 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002169 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2171 }
2172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174/*
2175 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2176 * a (wrapped) text line. Uses and sets "lp->fill".
2177 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002178 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 */
2180 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002181topline_back_winheight(
2182 lineoff_T *lp,
2183 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185#ifdef FEAT_DIFF
2186 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2187 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ++lp->fill;
2190 lp->height = 1;
2191 }
2192 else
2193#endif
2194 {
2195 --lp->lnum;
2196#ifdef FEAT_DIFF
2197 lp->fill = 0;
2198#endif
2199 if (lp->lnum < 1)
2200 lp->height = MAXCOL;
2201 else
2202#ifdef FEAT_FOLDING
2203 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 lp->height = 1;
2206 else
2207#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002208 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210}
2211
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002212 static void
2213topline_back(lineoff_T *lp)
2214{
2215 topline_back_winheight(lp, TRUE);
2216}
2217
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219/*
2220 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2221 * a (wrapped) text line. Uses and sets "lp->fill".
2222 * Returns the height of the added line in "lp->height".
2223 * Lines below the last one are incredibly high.
2224 */
2225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002226botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228#ifdef FEAT_DIFF
2229 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 ++lp->fill;
2233 lp->height = 1;
2234 }
2235 else
2236#endif
2237 {
2238 ++lp->lnum;
2239#ifdef FEAT_DIFF
2240 lp->fill = 0;
2241#endif
2242 if (lp->lnum > curbuf->b_ml.ml_line_count)
2243 lp->height = MAXCOL;
2244 else
2245#ifdef FEAT_FOLDING
2246 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002247 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 lp->height = 1;
2249 else
2250#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002251 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253}
2254
2255#ifdef FEAT_DIFF
2256/*
2257 * Switch from including filler lines below lp->lnum to including filler
2258 * lines above loff.lnum + 1. This keeps pointing to the same line.
2259 * When there are no filler lines nothing changes.
2260 */
2261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002262botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 if (lp->fill > 0)
2265 {
2266 ++lp->lnum;
2267 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2268 }
2269}
2270
2271/*
2272 * Switch from including filler lines above lp->lnum to including filler
2273 * lines below loff.lnum - 1. This keeps pointing to the same line.
2274 * When there are no filler lines nothing changes.
2275 */
2276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002277topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (lp->fill > 0)
2280 {
2281 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2282 --lp->lnum;
2283 }
2284}
2285#endif
2286
2287/*
2288 * Recompute topline to put the cursor at the top of the window.
2289 * Scroll at least "min_scroll" lines.
2290 * If "always" is TRUE, always set topline (for "zt").
2291 */
2292 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
2295 int scrolled = 0;
2296 int extra = 0;
2297 int used;
2298 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002299 linenr_T top; // just above displayed lines
2300 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002302 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#ifdef FEAT_DIFF
2304 linenr_T old_topfill = curwin->w_topfill;
2305#endif
2306 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002307 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (mouse_dragging > 0)
2310 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
2312 /*
2313 * Decrease topline until:
2314 * - it has become 1
2315 * - (part of) the cursor line is moved off the screen or
2316 * - moved at least 'scrolljump' lines and
2317 * - at least 'scrolloff' lines above and below the cursor
2318 */
2319 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002320 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (curwin->w_cursor.lnum < curwin->w_topline)
2322 scrolled = used;
2323
2324#ifdef FEAT_FOLDING
2325 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2326 {
2327 --top;
2328 ++bot;
2329 }
2330 else
2331#endif
2332 {
2333 top = curwin->w_cursor.lnum - 1;
2334 bot = curwin->w_cursor.lnum + 1;
2335 }
2336 new_topline = top + 1;
2337
2338#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 // "used" already contains the number of filler lines above, don't add it
2340 // again.
2341 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002342 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343#endif
2344
2345 /*
2346 * Check if the lines from "top" to "bot" fit in the window. If they do,
2347 * set new_topline and advance "top" and "bot" to include more lines.
2348 */
2349 while (top > 0)
2350 {
2351#ifdef FEAT_FOLDING
2352 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002353 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 i = 1;
2355 else
2356#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002357 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002358 if (top < curwin->w_topline)
2359 scrolled += i;
2360
2361 // If scrolling is needed, scroll at least 'sj' lines.
2362 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2363 && extra >= off)
2364 break;
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 used += i;
2367 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2368 {
2369#ifdef FEAT_FOLDING
2370 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002371 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 ++used;
2373 else
2374#endif
2375 used += plines(bot);
2376 }
2377 if (used > curwin->w_height)
2378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 extra += i;
2381 new_topline = top;
2382 --top;
2383 ++bot;
2384 }
2385
2386 /*
2387 * If we don't have enough space, put cursor in the middle.
2388 * This makes sure we get the same position when using "k" and "j"
2389 * in a small window.
2390 */
2391 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002392 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else
2394 {
2395 /*
2396 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002397 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 if (new_topline < curwin->w_topline || always)
2400 curwin->w_topline = new_topline;
2401 if (curwin->w_topline > curwin->w_cursor.lnum)
2402 curwin->w_topline = curwin->w_cursor.lnum;
2403#ifdef FEAT_DIFF
2404 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2405 if (curwin->w_topfill > 0 && extra > off)
2406 {
2407 curwin->w_topfill -= extra - off;
2408 if (curwin->w_topfill < 0)
2409 curwin->w_topfill = 0;
2410 }
2411 check_topfill(curwin, FALSE);
2412#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002413 if (curwin->w_topline == curwin->w_cursor.lnum)
2414 {
2415 validate_virtcol();
2416 if (curwin->w_skipcol >= curwin->w_virtcol)
2417 // TODO: if the line doesn't fit may optimize w_skipcol instead
2418 // of making it zero
2419 reset_skipcol();
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002422 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#ifdef FEAT_DIFF
2424 || curwin->w_topfill != old_topfill
2425#endif
2426 )
2427 curwin->w_valid &=
2428 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2429 curwin->w_valid |= VALID_TOPLINE;
2430 }
2431}
2432
2433/*
2434 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2435 * screen lines for text lines.
2436 */
2437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440#ifdef FEAT_DIFF
2441 wp->w_filler_rows = 0;
2442#endif
2443 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 else
2446 {
2447 wp->w_empty_rows = wp->w_height - used;
2448#ifdef FEAT_DIFF
2449 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2450 {
2451 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2452 if (wp->w_empty_rows > wp->w_filler_rows)
2453 wp->w_empty_rows -= wp->w_filler_rows;
2454 else
2455 {
2456 wp->w_filler_rows = wp->w_empty_rows;
2457 wp->w_empty_rows = 0;
2458 }
2459 }
2460#endif
2461 }
2462}
2463
2464/*
2465 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002466 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2468 * This is messy stuff!!!
2469 */
2470 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002471scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 int used;
2474 int scrolled = 0;
2475 int extra = 0;
2476 int i;
2477 linenr_T line_count;
2478 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002479 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 lineoff_T loff;
2481 lineoff_T boff;
2482#ifdef FEAT_DIFF
2483 int old_topfill = curwin->w_topfill;
2484 int fill_below_window;
2485#endif
2486 linenr_T old_botline = curwin->w_botline;
2487 linenr_T old_valid = curwin->w_valid;
2488 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002489 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002490 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002491 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 cln = curwin->w_cursor.lnum;
2494 if (set_topbot)
2495 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002496 int set_skipcol = FALSE;
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 used = 0;
2499 curwin->w_botline = cln + 1;
2500#ifdef FEAT_DIFF
2501 loff.fill = 0;
2502#endif
2503 for (curwin->w_topline = curwin->w_botline;
2504 curwin->w_topline > 1;
2505 curwin->w_topline = loff.lnum)
2506 {
2507 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002508 topline_back_winheight(&loff, FALSE);
2509 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002511 if (used + loff.height > curwin->w_height)
2512 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002513 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002514 {
2515 // 'smoothscroll' and 'wrap' are set. The above line is
2516 // too long to show in its entirety, so we show just a part
2517 // of it.
2518 if (used < curwin->w_height)
2519 {
2520 int plines_offset = used + loff.height
2521 - curwin->w_height;
2522 used = curwin->w_height;
2523#ifdef FEAT_DIFF
2524 curwin->w_topfill = loff.fill;
2525#endif
2526 curwin->w_topline = loff.lnum;
2527 curwin->w_skipcol = skipcol_from_plines(
2528 curwin, plines_offset);
2529 set_skipcol = TRUE;
2530 }
2531 }
2532 break;
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 used += loff.height;
2535#ifdef FEAT_DIFF
2536 curwin->w_topfill = loff.fill;
2537#endif
2538 }
2539 set_empty_rows(curwin, used);
2540 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2541 if (curwin->w_topline != old_topline
2542#ifdef FEAT_DIFF
2543 || curwin->w_topfill != old_topfill
2544#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002545 || set_skipcol
2546 || curwin->w_skipcol != 0)
2547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002549 if (set_skipcol)
2550 redraw_later(UPD_NOT_VALID);
2551 else
2552 reset_skipcol();
2553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 else
2556 validate_botline();
2557
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#ifdef FEAT_DIFF
2560 used = plines_nofill(cln);
2561#else
2562 validate_cheight();
2563 used = curwin->w_cline_height;
2564#endif
2565
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002566 // If the cursor is on or below botline, we will at least scroll by the
2567 // height of the cursor line, which is "used". Correct for empty lines,
2568 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (cln >= curwin->w_botline)
2570 {
2571 scrolled = used;
2572 if (cln == curwin->w_botline)
2573 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002574 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002575 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002576 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002577 // Calculate how many screen lines the current top line of window
2578 // occupies. If it is occupying more than the entire window, we
2579 // need to scroll the additional clipped lines to scroll past the
2580 // top line before we can move on to the other lines.
2581 int top_plines =
2582#ifdef FEAT_DIFF
2583 plines_win_nofill
2584#else
2585 plines_win
2586#endif
2587 (curwin, curwin->w_topline, FALSE);
2588 int skip_lines = 0;
2589 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002590 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002591 {
fullwaywang8154e642023-06-24 21:58:09 +01002592 int width2 = width1 + curwin_col_off2();
2593 // similar formula is used in curs_columns()
2594 if (curwin->w_skipcol > width1)
2595 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2596 else if (curwin->w_skipcol > 0)
2597 skip_lines = 1;
2598
2599 top_plines -= skip_lines;
2600 if (top_plines > curwin->w_height)
2601 {
2602 scrolled += (top_plines - curwin->w_height);
2603 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002604 }
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607
2608 /*
2609 * Stop counting lines to scroll when
2610 * - hitting start of the file
2611 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002612 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 * - lines between botline and cursor have been counted
2614 */
2615#ifdef FEAT_FOLDING
2616 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2617#endif
2618 {
2619 loff.lnum = cln;
2620 boff.lnum = cln;
2621 }
2622#ifdef FEAT_DIFF
2623 loff.fill = 0;
2624 boff.fill = 0;
2625 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2626 - curwin->w_filler_rows;
2627#endif
2628
2629 while (loff.lnum > 1)
2630 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002631 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2632 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002634 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2636 && loff.lnum <= curwin->w_botline
2637#ifdef FEAT_DIFF
2638 && (loff.lnum < curwin->w_botline
2639 || loff.fill >= fill_below_window)
2640#endif
2641 )
2642 break;
2643
Bram Moolenaar85a20022019-12-21 18:25:54 +01002644 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002646 if (loff.height == MAXCOL)
2647 used = MAXCOL;
2648 else
2649 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (used > curwin->w_height)
2651 break;
2652 if (loff.lnum >= curwin->w_botline
2653#ifdef FEAT_DIFF
2654 && (loff.lnum > curwin->w_botline
2655 || loff.fill <= fill_below_window)
2656#endif
2657 )
2658 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 scrolled += loff.height;
2661 if (loff.lnum == curwin->w_botline
2662#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002663 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 )
2666 scrolled -= curwin->w_empty_rows;
2667 }
2668
2669 if (boff.lnum < curbuf->b_ml.ml_line_count)
2670 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002671 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 botline_forw(&boff);
2673 used += boff.height;
2674 if (used > curwin->w_height)
2675 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002676 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2677 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 extra += boff.height;
2680 if (boff.lnum >= curwin->w_botline
2681#ifdef FEAT_DIFF
2682 || (boff.lnum + 1 == curwin->w_botline
2683 && boff.fill > curwin->w_filler_rows)
2684#endif
2685 )
2686 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002687 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 scrolled += boff.height;
2689 if (boff.lnum == curwin->w_botline
2690#ifdef FEAT_DIFF
2691 && boff.fill == 0
2692#endif
2693 )
2694 scrolled -= curwin->w_empty_rows;
2695 }
2696 }
2697 }
2698 }
2699
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (scrolled <= 0)
2702 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002703 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 else if (used > curwin->w_height)
2705 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002706 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
2708 {
2709 line_count = 0;
2710#ifdef FEAT_DIFF
2711 boff.fill = curwin->w_topfill;
2712#endif
2713 boff.lnum = curwin->w_topline - 1;
2714 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2715 {
2716 botline_forw(&boff);
2717 i += boff.height;
2718 ++line_count;
2719 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002720 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 line_count = 9999;
2722 }
2723
2724 /*
2725 * Scroll up if the cursor is off the bottom of the screen a bit.
2726 * Otherwise put it at 1/2 of the screen.
2727 */
2728 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002729 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002730 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002731 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002732 if (do_sms)
2733 scrollup(scrolled, TRUE); // TODO
2734 else
2735 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 /*
2739 * If topline didn't change we need to restore w_botline and w_empty_rows
2740 * (we changed them).
2741 * If topline did change, update_screen() will set botline.
2742 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002743 if (curwin->w_topline == old_topline
2744 && curwin->w_skipcol == old_skipcol
2745 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 curwin->w_botline = old_botline;
2748 curwin->w_empty_rows = old_empty_rows;
2749 curwin->w_valid = old_valid;
2750 }
2751 curwin->w_valid |= VALID_TOPLINE;
2752}
2753
2754/*
2755 * Recompute topline to put the cursor halfway the window
2756 * If "atend" is TRUE, also put it halfway at the end of the file.
2757 */
2758 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002759scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
2761 int above = 0;
2762 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002763 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764#ifdef FEAT_DIFF
2765 int topfill = 0;
2766#endif
2767 int below = 0;
2768 int used;
2769 lineoff_T loff;
2770 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002771#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002772 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002775#ifdef FEAT_PROP_POPUP
2776 // if the width changed this needs to be updated first
2777 may_update_popup_position();
2778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2780#ifdef FEAT_FOLDING
2781 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2782#endif
2783#ifdef FEAT_DIFF
2784 used = plines_nofill(loff.lnum);
2785 loff.fill = 0;
2786 boff.fill = 0;
2787#else
2788 used = plines(loff.lnum);
2789#endif
2790 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002791
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002792 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002793 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2794 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002795 {
2796 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002797 if (atend)
2798 {
2799 want_height = (curwin->w_height - used) / 2;
2800 used = 0;
2801 }
2802 else
2803 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002804 }
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 while (topline > 1)
2807 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002808 // If using smoothscroll, we can precisely scroll to the
2809 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002810 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002811 {
2812 topline_back_winheight(&loff, FALSE);
2813 if (loff.height == MAXCOL)
2814 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002815 used += loff.height;
2816 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002817 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002818 botline_forw(&boff);
2819 used += boff.height;
2820 }
2821 if (used > want_height)
2822 {
2823 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002824 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002825 topline = loff.lnum;
2826#ifdef FEAT_DIFF
2827 topfill = loff.fill;
2828#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002829 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002830 }
2831 break;
2832 }
2833 topline = loff.lnum;
2834#ifdef FEAT_DIFF
2835 topfill = loff.fill;
2836#endif
2837 continue;
2838 }
2839
2840 // If not using smoothscroll, we have to iteratively find how many
2841 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002842 // This may not be right in the middle if the lines'
2843 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002844
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002845 // Depending on "prefer_above" we add a line above or below first.
2846 // Loop twice to avoid duplicating code.
2847 int done = FALSE;
2848 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002850 if (prefer_above ? (round == 2 && below < above)
2851 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002853 // add a line below the cursor
2854 if (boff.lnum < curbuf->b_ml.ml_line_count)
2855 {
2856 botline_forw(&boff);
2857 used += boff.height;
2858 if (used > curwin->w_height)
2859 {
2860 done = TRUE;
2861 break;
2862 }
2863 below += boff.height;
2864 }
2865 else
2866 {
2867 ++below; // count a "~" line
2868 if (atend)
2869 ++used;
2870 }
2871 }
2872
2873 if (prefer_above ? (round == 1 && below >= above)
2874 : (round == 1 && below > above))
2875 {
2876 // add a line above the cursor
2877 topline_back(&loff);
2878 if (loff.height == MAXCOL)
2879 used = MAXCOL;
2880 else
2881 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883 {
2884 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002886 }
2887 above += loff.height;
2888 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002890 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002894 if (done)
2895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898#ifdef FEAT_FOLDING
2899 if (!hasFolding(topline, &curwin->w_topline, NULL))
2900#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002901 {
2902 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002903 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002904 || curwin->w_skipcol != 0)
2905 {
2906 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002907 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002908 {
2909 curwin->w_skipcol = skipcol;
2910 redraw_later(UPD_NOT_VALID);
2911 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002912 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002913 reset_skipcol();
2914 }
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916#ifdef FEAT_DIFF
2917 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002918 if (old_topline > curwin->w_topline + curwin->w_height)
2919 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 check_topfill(curwin, FALSE);
2921#endif
2922 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2923 curwin->w_valid |= VALID_TOPLINE;
2924}
2925
2926/*
2927 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002928 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 * If not possible, put it at the same position as scroll_cursor_halfway().
2930 * When called topline must be valid!
2931 */
2932 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002933cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002935 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002937 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 linenr_T botline;
2939 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002940 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002942 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /*
2945 * How many lines we would like to have above/below the cursor depends on
2946 * whether the first/last line of the file is on screen.
2947 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002948 above_wanted = so;
2949 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002950 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
2952 above_wanted = mouse_dragging - 1;
2953 below_wanted = mouse_dragging - 1;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (curwin->w_topline == 1)
2956 {
2957 above_wanted = 0;
2958 max_off = curwin->w_height / 2;
2959 if (below_wanted > max_off)
2960 below_wanted = max_off;
2961 }
2962 validate_botline();
2963 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002964 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {
2966 below_wanted = 0;
2967 max_off = (curwin->w_height - 1) / 2;
2968 if (above_wanted > max_off)
2969 above_wanted = max_off;
2970 }
2971
2972 /*
2973 * If there are sufficient file-lines above and below the cursor, we can
2974 * return now.
2975 */
2976 cln = curwin->w_cursor.lnum;
2977 if (cln >= curwin->w_topline + above_wanted
2978 && cln < curwin->w_botline - below_wanted
2979#ifdef FEAT_FOLDING
2980 && !hasAnyFolding(curwin)
2981#endif
2982 )
2983 return;
2984
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002985 if (curwin->w_p_sms && !curwin->w_p_wrap)
2986 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002987 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002988 if (curwin->w_cline_height == curwin->w_height)
2989 {
2990 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002991 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002992 return;
2993 }
2994 // TODO: If the cursor line doesn't fit in the window then only adjust
2995 // w_skipcol.
2996 }
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 /*
2999 * Narrow down the area where the cursor can be put by taking lines from
3000 * the top and the bottom until:
3001 * - the desired context lines are found
3002 * - the lines from the top is past the lines from the bottom
3003 */
3004 topline = curwin->w_topline;
3005 botline = curwin->w_botline - 1;
3006#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003007 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 above = curwin->w_topfill;
3009 below = curwin->w_filler_rows;
3010#endif
3011 while ((above < above_wanted || below < below_wanted) && topline < botline)
3012 {
3013 if (below < below_wanted && (below <= above || above >= above_wanted))
3014 {
3015#ifdef FEAT_FOLDING
3016 if (hasFolding(botline, &botline, NULL))
3017 ++below;
3018 else
3019#endif
3020 below += plines(botline);
3021 --botline;
3022 }
3023 if (above < above_wanted && (above < below || below >= below_wanted))
3024 {
3025#ifdef FEAT_FOLDING
3026 if (hasFolding(topline, NULL, &topline))
3027 ++above;
3028 else
3029#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003030 above += PLINES_NOFILL(topline);
3031#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003032 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (topline < botline)
3034 above += diff_check_fill(curwin, topline + 1);
3035#endif
3036 ++topline;
3037 }
3038 }
3039 if (topline == botline || botline == 0)
3040 curwin->w_cursor.lnum = topline;
3041 else if (topline > botline)
3042 curwin->w_cursor.lnum = botline;
3043 else
3044 {
3045 if (cln < topline && curwin->w_topline > 1)
3046 {
3047 curwin->w_cursor.lnum = topline;
3048 curwin->w_valid &=
3049 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3050 }
3051 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3052 {
3053 curwin->w_cursor.lnum = botline;
3054 curwin->w_valid &=
3055 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3056 }
3057 }
3058 curwin->w_valid |= VALID_TOPLINE;
3059}
3060
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003061static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003064 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3065 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003067 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 */
3069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 long n;
3073 int retval = OK;
3074 lineoff_T loff;
3075 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003076 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaar85a20022019-12-21 18:25:54 +01003078 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 beep_flush();
3081 return FAIL;
3082 }
3083
3084 for ( ; count > 0; --count)
3085 {
3086 validate_botline();
3087 /*
3088 * It's an error to move a page up when the first line is already on
3089 * the screen. It's an error to move a page down when the last line
3090 * is on the screen and the topline is 'scrolloff' lines from the
3091 * last line.
3092 */
3093 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003094 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3096 : (curwin->w_topline == 1
3097#ifdef FEAT_DIFF
3098 && curwin->w_topfill ==
3099 diff_check_fill(curwin, curwin->w_topline)
3100#endif
3101 ))
3102 {
3103 beep_flush();
3104 retval = FAIL;
3105 break;
3106 }
3107
3108#ifdef FEAT_DIFF
3109 loff.fill = 0;
3110#endif
3111 if (dir == FORWARD)
3112 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003113 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003116 if (p_window <= 2)
3117 ++curwin->w_topline;
3118 else
3119 curwin->w_topline += p_window - 2;
3120 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3121 curwin->w_topline = curbuf->b_ml.ml_line_count;
3122 curwin->w_cursor.lnum = curwin->w_topline;
3123 }
3124 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3125 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003126 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 curwin->w_topline = curbuf->b_ml.ml_line_count;
3128#ifdef FEAT_DIFF
3129 curwin->w_topfill = 0;
3130#endif
3131 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3132 }
3133 else
3134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003135 // For the overlap, start with the line just below the window
3136 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 loff.lnum = curwin->w_botline;
3138#ifdef FEAT_DIFF
3139 loff.fill = diff_check_fill(curwin, loff.lnum)
3140 - curwin->w_filler_rows;
3141#endif
3142 get_scroll_overlap(&loff, -1);
3143 curwin->w_topline = loff.lnum;
3144#ifdef FEAT_DIFF
3145 curwin->w_topfill = loff.fill;
3146 check_topfill(curwin, FALSE);
3147#endif
3148 curwin->w_cursor.lnum = curwin->w_topline;
3149 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3150 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3151 }
3152 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003153 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155#ifdef FEAT_DIFF
3156 if (curwin->w_topline == 1)
3157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003158 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 max_topfill();
3160 continue;
3161 }
3162#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003163 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003164 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003165 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003166 if (p_window <= 2)
3167 --curwin->w_topline;
3168 else
3169 curwin->w_topline -= p_window - 2;
3170 if (curwin->w_topline < 1)
3171 curwin->w_topline = 1;
3172 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3173 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3174 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3175 continue;
3176 }
3177
Bram Moolenaar85a20022019-12-21 18:25:54 +01003178 // Find the line at the top of the window that is going to be the
3179 // line at the bottom of the window. Make sure this results in
3180 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 loff.lnum = curwin->w_topline - 1;
3182#ifdef FEAT_DIFF
3183 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3184 - curwin->w_topfill;
3185#endif
3186 get_scroll_overlap(&loff, 1);
3187
3188 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3189 {
3190 loff.lnum = curbuf->b_ml.ml_line_count;
3191#ifdef FEAT_DIFF
3192 loff.fill = 0;
3193 }
3194 else
3195 {
3196 botline_topline(&loff);
3197#endif
3198 }
3199 curwin->w_cursor.lnum = loff.lnum;
3200
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 // Find the line just above the new topline to get the right line
3202 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 n = 0;
3204 while (n <= curwin->w_height && loff.lnum >= 1)
3205 {
3206 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003207 if (loff.height == MAXCOL)
3208 n = MAXCOL;
3209 else
3210 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003212 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 curwin->w_topline = 1;
3215#ifdef FEAT_DIFF
3216 max_topfill();
3217#endif
3218 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3219 }
3220 else
3221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#ifdef FEAT_DIFF
3224 topline_botline(&loff);
3225#endif
3226 botline_forw(&loff);
3227 botline_forw(&loff);
3228#ifdef FEAT_DIFF
3229 botline_topline(&loff);
3230#endif
3231#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003232 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3234#endif
3235
Bram Moolenaar85a20022019-12-21 18:25:54 +01003236 // Always scroll at least one line. Avoid getting stuck on
3237 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (loff.lnum >= curwin->w_topline
3239#ifdef FEAT_DIFF
3240 && (loff.lnum > curwin->w_topline
3241 || loff.fill >= curwin->w_topfill)
3242#endif
3243 )
3244 {
3245#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003246 // First try using the maximum number of filler lines. If
3247 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 loff.fill = curwin->w_topfill;
3249 if (curwin->w_topfill < diff_check_fill(curwin,
3250 curwin->w_topline))
3251 max_topfill();
3252 if (curwin->w_topfill == loff.fill)
3253#endif
3254 {
3255 --curwin->w_topline;
3256#ifdef FEAT_DIFF
3257 curwin->w_topfill = 0;
3258#endif
zeertzjq05834912023-10-04 20:12:37 +02003259 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
3261 comp_botline(curwin);
3262 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003263 curwin->w_valid &=
3264 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266 else
3267 {
3268 curwin->w_topline = loff.lnum;
3269#ifdef FEAT_DIFF
3270 curwin->w_topfill = loff.fill;
3271 check_topfill(curwin, FALSE);
3272#endif
3273 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3274 }
3275 }
3276 }
3277 }
3278#ifdef FEAT_FOLDING
3279 foldAdjustCursor();
3280#endif
3281 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003282 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003283 if (retval == OK)
3284 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3286
Bram Moolenaar907dad72018-07-10 15:07:15 +02003287 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003289 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3290 // But make sure we scroll at least one line (happens with mix of long
3291 // wrapping lines and non-wrapping line).
3292 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003294 scroll_cursor_top(1, FALSE);
3295 if (curwin->w_topline <= old_topline
3296 && old_topline < curbuf->b_ml.ml_line_count)
3297 {
3298 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003300 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3301#endif
3302 }
3303 }
3304#ifdef FEAT_FOLDING
3305 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003310 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return retval;
3312}
3313
3314/*
3315 * Decide how much overlap to use for page-up or page-down scrolling.
3316 * This is symmetric, so that doing both keeps the same lines displayed.
3317 * Three lines are examined:
3318 *
3319 * before CTRL-F after CTRL-F / before CTRL-B
3320 * etc. l1
3321 * l1 last but one line ------------
3322 * l2 last text line l2 top text line
3323 * ------------- l3 second text line
3324 * l3 etc.
3325 */
3326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int h1, h2, h3, h4;
3330 int min_height = curwin->w_height - 2;
3331 lineoff_T loff0, loff1, loff2;
3332
3333#ifdef FEAT_DIFF
3334 if (lp->fill > 0)
3335 lp->height = 1;
3336 else
3337 lp->height = plines_nofill(lp->lnum);
3338#else
3339 lp->height = plines(lp->lnum);
3340#endif
3341 h1 = lp->height;
3342 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 loff0 = *lp;
3346 if (dir > 0)
3347 botline_forw(lp);
3348 else
3349 topline_back(lp);
3350 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003351 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003353 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 return;
3355 }
3356
3357 loff1 = *lp;
3358 if (dir > 0)
3359 botline_forw(lp);
3360 else
3361 topline_back(lp);
3362 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003363 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 return;
3367 }
3368
3369 loff2 = *lp;
3370 if (dir > 0)
3371 botline_forw(lp);
3372 else
3373 topline_back(lp);
3374 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003375 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003376 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Scroll 'scroll' lines up or down.
3383 */
3384 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003385halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387 long scrolled = 0;
3388 int i;
3389 int n;
3390 int room;
3391
3392 if (Prenum)
3393 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3394 curwin->w_height : Prenum;
3395 n = (curwin->w_p_scr <= curwin->w_height) ?
3396 curwin->w_p_scr : curwin->w_height;
3397
Bram Moolenaard5d37532017-03-27 23:02:07 +02003398 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 validate_botline();
3400 room = curwin->w_empty_rows;
3401#ifdef FEAT_DIFF
3402 room += curwin->w_filler_rows;
3403#endif
3404 if (flag)
3405 {
3406 /*
3407 * scroll the text up
3408 */
3409 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3410 {
3411#ifdef FEAT_DIFF
3412 if (curwin->w_topfill > 0)
3413 {
3414 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003415 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 --curwin->w_topfill;
3417 }
3418 else
3419#endif
3420 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003421 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 n -= i;
3423 if (n < 0 && scrolled > 0)
3424 break;
3425#ifdef FEAT_FOLDING
3426 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3427#endif
3428 ++curwin->w_topline;
3429#ifdef FEAT_DIFF
3430 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3431#endif
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3434 {
3435 ++curwin->w_cursor.lnum;
3436 curwin->w_valid &=
3437 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3441 scrolled += i;
3442
3443 /*
3444 * Correct w_botline for changed w_topline.
3445 * Won't work when there are filler lines.
3446 */
3447#ifdef FEAT_DIFF
3448 if (curwin->w_p_diff)
3449 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3450 else
3451#endif
3452 {
3453 room += i;
3454 do
3455 {
3456 i = plines(curwin->w_botline);
3457 if (i > room)
3458 break;
3459#ifdef FEAT_FOLDING
3460 (void)hasFolding(curwin->w_botline, NULL,
3461 &curwin->w_botline);
3462#endif
3463 ++curwin->w_botline;
3464 room -= i;
3465 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3466 }
3467 }
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 /*
3470 * When hit bottom of the file: move cursor down.
3471 */
3472 if (n > 0)
3473 {
3474# ifdef FEAT_FOLDING
3475 if (hasAnyFolding(curwin))
3476 {
3477 while (--n >= 0
3478 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3479 {
3480 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3481 &curwin->w_cursor.lnum);
3482 ++curwin->w_cursor.lnum;
3483 }
3484 }
3485 else
3486# endif
3487 curwin->w_cursor.lnum += n;
3488 check_cursor_lnum();
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 else
3492 {
3493 /*
3494 * scroll the text down
3495 */
3496 while (n > 0 && curwin->w_topline > 1)
3497 {
3498#ifdef FEAT_DIFF
3499 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3500 {
3501 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003502 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 ++curwin->w_topfill;
3504 }
3505 else
3506#endif
3507 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003508 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 n -= i;
3510 if (n < 0 && scrolled > 0)
3511 break;
3512 --curwin->w_topline;
3513#ifdef FEAT_FOLDING
3514 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3515#endif
3516#ifdef FEAT_DIFF
3517 curwin->w_topfill = 0;
3518#endif
3519 }
3520 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3521 VALID_BOTLINE|VALID_BOTLINE_AP);
3522 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (curwin->w_cursor.lnum > 1)
3524 {
3525 --curwin->w_cursor.lnum;
3526 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 /*
3531 * When hit top of the file: move cursor up.
3532 */
3533 if (n > 0)
3534 {
3535 if (curwin->w_cursor.lnum <= (linenr_T)n)
3536 curwin->w_cursor.lnum = 1;
3537 else
3538# ifdef FEAT_FOLDING
3539 if (hasAnyFolding(curwin))
3540 {
3541 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3542 {
3543 --curwin->w_cursor.lnum;
3544 (void)hasFolding(curwin->w_cursor.lnum,
3545 &curwin->w_cursor.lnum, NULL);
3546 }
3547 }
3548 else
3549# endif
3550 curwin->w_cursor.lnum -= n;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003554 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 foldAdjustCursor();
3556# endif
3557#ifdef FEAT_DIFF
3558 check_topfill(curwin, !flag);
3559#endif
3560 cursor_correct();
3561 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003562 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003564
Bram Moolenaar860cae12010-06-05 23:22:07 +02003565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003566do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003568 static win_T *prev_curwin = NULL;
3569 static pos_T prev_cursor = {0, 0, 0};
3570
3571 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3572 return;
3573 prev_curwin = curwin;
3574 prev_cursor = curwin->w_cursor;
3575
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003577 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003578 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003579 colnr_T curswant = curwin->w_curswant;
3580 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 win_T *old_curwin = curwin;
3582 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583 int old_VIsual_select = VIsual_select;
3584 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585
3586 /*
3587 * loop through the cursorbound windows
3588 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003590 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 {
3592 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003593 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 if (curwin != old_curwin && curwin->w_p_crb)
3595 {
3596# ifdef FEAT_DIFF
3597 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003598 curwin->w_cursor.lnum =
3599 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 else
3601# endif
3602 curwin->w_cursor.lnum = line;
3603 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003604 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003605 curwin->w_curswant = curswant;
3606 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607
Bram Moolenaar85a20022019-12-21 18:25:54 +01003608 // Make sure the cursor is in a valid position. Temporarily set
3609 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003610 int restart_edit_save = restart_edit;
3611 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003613
3614 // Avoid a scroll here for the cursor position, 'scrollbind' is
3615 // more important.
3616 if (!curwin->w_p_scb)
3617 validate_cursor();
3618
Bram Moolenaar61452852011-02-01 18:01:11 +01003619 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003620 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621 if (has_mbyte)
3622 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003623 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003624
Bram Moolenaar85a20022019-12-21 18:25:54 +01003625 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003626 if (!curwin->w_p_scb)
3627 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003628 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003629 }
3630 }
3631
3632 /*
3633 * reset current-window
3634 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 VIsual_select = old_VIsual_select;
3636 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003637 curwin = old_curwin;
3638 curbuf = old_curbuf;
3639}