blob: e5309df486a428d12c3d6f19ecab4f57c583f69e [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 {
1757 row += col / width2;
1758 col = col % width2;
1759 }
1760 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001761 {
1762 curwin->w_curswant = curwin->w_virtcol
1763 - (row - curwin->w_height + 1) * width2;
1764 coladvance(curwin->w_curswant);
1765 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
1769/*
1770 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001773scrollup(
1774 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001775 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001777 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001779 if (do_sms
1780# ifdef FEAT_FOLDING
1781 || (byfold && hasAnyFolding(curwin))
1782# endif
1783# ifdef FEAT_DIFF
1784 || (curwin->w_p_diff && !curwin->w_p_wrap)
1785# endif
1786 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001788 int width1 = curwin->w_width - curwin_col_off();
1789 int width2 = width1 + curwin_col_off2();
1790 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001791 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001792
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001793 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001794 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001795
1796 // diff mode: first consume "topfill"
1797 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1798 // the line, then advance to the next line.
1799 // folding: count each sequence of folded lines as one logical line.
1800 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802# ifdef FEAT_DIFF
1803 if (curwin->w_topfill > 0)
1804 --curwin->w_topfill;
1805 else
1806# endif
1807 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 linenr_T lnum = curwin->w_topline;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810# ifdef FEAT_FOLDING
1811 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001812 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 (void)hasFolding(lnum, NULL, &lnum);
1814# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001815 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001816 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001817 // 'smoothscroll': increase "w_skipcol" until it goes over
1818 // the end of the line, then advance to the next line.
1819 int add = curwin->w_skipcol > 0 ? width2 : width1;
1820 curwin->w_skipcol += add;
1821 if (curwin->w_skipcol >= size)
1822 {
1823 if (lnum == curbuf->b_ml.ml_line_count)
1824 {
1825 // at the last screen line, can't scroll further
1826 curwin->w_skipcol -= add;
1827 break;
1828 }
1829 ++lnum;
1830 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001831 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001832 else
1833 {
1834 if (lnum >= curbuf->b_ml.ml_line_count)
1835 break;
1836 ++lnum;
1837 }
1838
1839 if (lnum > curwin->w_topline)
1840 {
1841 // approximate w_botline
1842 curwin->w_botline += lnum - curwin->w_topline;
1843 curwin->w_topline = lnum;
1844# ifdef FEAT_DIFF
1845 curwin->w_topfill = diff_check_fill(curwin, lnum);
1846# endif
1847 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001848 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001849 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001850 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001851 }
1852 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001853
zeertzjqd9a92dc2023-06-05 18:41:35 +01001854 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1855 // need to redraw more, because wl_size of the (new) topline may
1856 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001857 redraw_later(UPD_NOT_VALID);
1858 }
1859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001862 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864
1865 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1866 curwin->w_topline = curbuf->b_ml.ml_line_count;
1867 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1868 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1869
1870#ifdef FEAT_DIFF
1871 check_topfill(curwin, FALSE);
1872#endif
1873
1874#ifdef FEAT_FOLDING
1875 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001876 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1878#endif
1879
1880 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1881 if (curwin->w_cursor.lnum < curwin->w_topline)
1882 {
1883 curwin->w_cursor.lnum = curwin->w_topline;
1884 curwin->w_valid &=
1885 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1886 coladvance(curwin->w_curswant);
1887 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001888 if (curwin->w_cursor.lnum == curwin->w_topline
1889 && do_sms && curwin->w_skipcol > 0)
1890 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001891 int col_off = curwin_col_off();
1892 int col_off2 = curwin_col_off2();
1893
1894 int width1 = curwin->w_width - col_off;
1895 int width2 = width1 + col_off2;
1896 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001897 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001898 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001899 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001900
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001901 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001902 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001903 int overlap = scrolloff_cols != 0 ? 0
1904 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001905
Bram Moolenaar118c2352022-10-09 17:19:27 +01001906 // Make sure the cursor is in a visible part of the line, taking
1907 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001908 // If there are not enough screen lines put the cursor in the middle.
1909 if (scrolloff_cols > space_cols / 2)
1910 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001912 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001913 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001914 colnr_T col = curwin->w_virtcol;
1915
1916 if (col < width1)
1917 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001918 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001920 curwin->w_curswant = col;
1921 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001922
1923 // validate_virtcol() marked various things as valid, but after
1924 // moving the cursor they need to be recomputed
1925 curwin->w_valid &=
1926 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001927 }
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929}
1930
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001931/*
1932 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1933 * valid for 'smoothscroll'.
1934 */
1935 void
1936adjust_skipcol(void)
1937{
1938 if (!curwin->w_p_wrap
1939 || !curwin->w_p_sms
1940 || curwin->w_cursor.lnum != curwin->w_topline)
1941 return;
1942
1943 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001944 if (width1 <= 0)
1945 return; // no text will be displayed
1946
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001947 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001948 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001949 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1950 int scrolled = FALSE;
1951
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001952 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001953 if (curwin->w_cline_height == curwin->w_height
1954 // w_cline_height may be capped at w_height, check there aren't
1955 // actually more lines.
1956 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1957 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001958 {
1959 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001960 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001961 return;
1962 }
1963
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001964 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001965 int overlap = sms_marker_overlap(curwin,
1966 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001967 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001968 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001969 {
1970 // scroll a screen line down
1971 if (curwin->w_skipcol >= width1 + width2)
1972 curwin->w_skipcol -= width2;
1973 else
1974 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001975 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001976 }
1977 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001978 {
1979 validate_virtcol();
1980 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001981 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001982 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001983
1984 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1985 int row = 0;
1986 if (col >= width1)
1987 {
1988 col -= width1;
1989 ++row;
1990 }
1991 if (col > width2)
1992 {
1993 row += col / width2;
1994 col = col % width2;
1995 }
1996 if (row >= curwin->w_height)
1997 {
1998 if (curwin->w_skipcol == 0)
1999 {
2000 curwin->w_skipcol += width1;
2001 --row;
2002 }
2003 if (row >= curwin->w_height)
2004 curwin->w_skipcol += (row - curwin->w_height) * width2;
2005 redraw_later(UPD_NOT_VALID);
2006 }
2007}
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#ifdef FEAT_DIFF
2010/*
2011 * Don't end up with too many filler lines in the window.
2012 */
2013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002014check_topfill(
2015 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002016 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 int n;
2019
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 if (wp->w_topfill <= 0)
2021 return;
2022
2023 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2024 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002026 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002028 --wp->w_topline;
2029 wp->w_topfill = 0;
2030 }
2031 else
2032 {
2033 wp->w_topfill = wp->w_height - n;
2034 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037 }
2038}
2039
2040/*
2041 * Use as many filler lines as possible for w_topline. Make sure w_topline
2042 * is still visible.
2043 */
2044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 int n;
2048
2049 n = plines_nofill(curwin->w_topline);
2050 if (n >= curwin->w_height)
2051 curwin->w_topfill = 0;
2052 else
2053 {
2054 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2055 if (curwin->w_topfill + n > curwin->w_height)
2056 curwin->w_topfill = curwin->w_height - n;
2057 }
2058}
2059#endif
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061/*
2062 * Scroll the screen one line down, but don't do it if it would move the
2063 * cursor off the screen.
2064 */
2065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002066scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 int end_row;
2069#ifdef FEAT_DIFF
2070 int can_fill = (curwin->w_topfill
2071 < diff_check_fill(curwin, curwin->w_topline));
2072#endif
2073
2074 if (curwin->w_topline <= 1
2075#ifdef FEAT_DIFF
2076 && !can_fill
2077#endif
2078 )
2079 return;
2080
Bram Moolenaar85a20022019-12-21 18:25:54 +01002081 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
2084 * Compute the row number of the last row of the cursor line
2085 * and make sure it doesn't go off the screen. Make sure the cursor
2086 * doesn't go past 'scrolloff' lines from the screen end.
2087 */
2088 end_row = curwin->w_wrow;
2089#ifdef FEAT_DIFF
2090 if (can_fill)
2091 ++end_row;
2092 else
2093 end_row += plines_nofill(curwin->w_topline - 1);
2094#else
2095 end_row += plines(curwin->w_topline - 1);
2096#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002097 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 validate_cheight();
2100 validate_virtcol();
2101 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002102 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002104 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106#ifdef FEAT_DIFF
2107 if (can_fill)
2108 {
2109 ++curwin->w_topfill;
2110 check_topfill(curwin, TRUE);
2111 }
2112 else
2113 {
2114 --curwin->w_topline;
2115 curwin->w_topfill = 0;
2116 }
2117#else
2118 --curwin->w_topline;
2119#endif
2120#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002121 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002123 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2125 }
2126}
2127
2128/*
2129 * Scroll the screen one line up, but don't do it if it would move the cursor
2130 * off the screen.
2131 */
2132 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 int start_row;
2136
2137 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2138#ifdef FEAT_DIFF
2139 && curwin->w_topfill == 0
2140#endif
2141 )
2142 return;
2143
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 /*
2147 * Compute the row number of the first row of the cursor line
2148 * and make sure it doesn't go off the screen. Make sure the cursor
2149 * doesn't go before 'scrolloff' lines from the screen start.
2150 */
2151#ifdef FEAT_DIFF
2152 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2153 - curwin->w_topfill;
2154#else
2155 start_row = curwin->w_wrow - plines(curwin->w_topline);
2156#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002157 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002160 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002162 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164#ifdef FEAT_DIFF
2165 if (curwin->w_topfill > 0)
2166 --curwin->w_topfill;
2167 else
2168#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002169 {
2170#ifdef FEAT_FOLDING
2171 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002174 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002175 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2177 }
2178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180/*
2181 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2182 * a (wrapped) text line. Uses and sets "lp->fill".
2183 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002184 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
2186 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002187topline_back_winheight(
2188 lineoff_T *lp,
2189 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191#ifdef FEAT_DIFF
2192 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2193 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 ++lp->fill;
2196 lp->height = 1;
2197 }
2198 else
2199#endif
2200 {
2201 --lp->lnum;
2202#ifdef FEAT_DIFF
2203 lp->fill = 0;
2204#endif
2205 if (lp->lnum < 1)
2206 lp->height = MAXCOL;
2207 else
2208#ifdef FEAT_FOLDING
2209 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 lp->height = 1;
2212 else
2213#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002214 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216}
2217
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002218 static void
2219topline_back(lineoff_T *lp)
2220{
2221 topline_back_winheight(lp, TRUE);
2222}
2223
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225/*
2226 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2227 * a (wrapped) text line. Uses and sets "lp->fill".
2228 * Returns the height of the added line in "lp->height".
2229 * Lines below the last one are incredibly high.
2230 */
2231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234#ifdef FEAT_DIFF
2235 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ++lp->fill;
2239 lp->height = 1;
2240 }
2241 else
2242#endif
2243 {
2244 ++lp->lnum;
2245#ifdef FEAT_DIFF
2246 lp->fill = 0;
2247#endif
2248 if (lp->lnum > curbuf->b_ml.ml_line_count)
2249 lp->height = MAXCOL;
2250 else
2251#ifdef FEAT_FOLDING
2252 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 lp->height = 1;
2255 else
2256#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002257 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259}
2260
2261#ifdef FEAT_DIFF
2262/*
2263 * Switch from including filler lines below lp->lnum to including filler
2264 * lines above loff.lnum + 1. This keeps pointing to the same line.
2265 * When there are no filler lines nothing changes.
2266 */
2267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 if (lp->fill > 0)
2271 {
2272 ++lp->lnum;
2273 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2274 }
2275}
2276
2277/*
2278 * Switch from including filler lines above lp->lnum to including filler
2279 * lines below loff.lnum - 1. This keeps pointing to the same line.
2280 * When there are no filler lines nothing changes.
2281 */
2282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (lp->fill > 0)
2286 {
2287 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2288 --lp->lnum;
2289 }
2290}
2291#endif
2292
2293/*
2294 * Recompute topline to put the cursor at the top of the window.
2295 * Scroll at least "min_scroll" lines.
2296 * If "always" is TRUE, always set topline (for "zt").
2297 */
2298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002299scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 int scrolled = 0;
2302 int extra = 0;
2303 int used;
2304 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 linenr_T top; // just above displayed lines
2306 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002308 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#ifdef FEAT_DIFF
2310 linenr_T old_topfill = curwin->w_topfill;
2311#endif
2312 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002313 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (mouse_dragging > 0)
2316 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /*
2319 * Decrease topline until:
2320 * - it has become 1
2321 * - (part of) the cursor line is moved off the screen or
2322 * - moved at least 'scrolljump' lines and
2323 * - at least 'scrolloff' lines above and below the cursor
2324 */
2325 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002326 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (curwin->w_cursor.lnum < curwin->w_topline)
2328 scrolled = used;
2329
2330#ifdef FEAT_FOLDING
2331 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2332 {
2333 --top;
2334 ++bot;
2335 }
2336 else
2337#endif
2338 {
2339 top = curwin->w_cursor.lnum - 1;
2340 bot = curwin->w_cursor.lnum + 1;
2341 }
2342 new_topline = top + 1;
2343
2344#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002345 // "used" already contains the number of filler lines above, don't add it
2346 // again.
2347 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002348 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#endif
2350
2351 /*
2352 * Check if the lines from "top" to "bot" fit in the window. If they do,
2353 * set new_topline and advance "top" and "bot" to include more lines.
2354 */
2355 while (top > 0)
2356 {
2357#ifdef FEAT_FOLDING
2358 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 i = 1;
2361 else
2362#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002363 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002364 if (top < curwin->w_topline)
2365 scrolled += i;
2366
2367 // If scrolling is needed, scroll at least 'sj' lines.
2368 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2369 && extra >= off)
2370 break;
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 used += i;
2373 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2374 {
2375#ifdef FEAT_FOLDING
2376 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 ++used;
2379 else
2380#endif
2381 used += plines(bot);
2382 }
2383 if (used > curwin->w_height)
2384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 extra += i;
2387 new_topline = top;
2388 --top;
2389 ++bot;
2390 }
2391
2392 /*
2393 * If we don't have enough space, put cursor in the middle.
2394 * This makes sure we get the same position when using "k" and "j"
2395 * in a small window.
2396 */
2397 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002398 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 else
2400 {
2401 /*
2402 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002403 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
2405 if (new_topline < curwin->w_topline || always)
2406 curwin->w_topline = new_topline;
2407 if (curwin->w_topline > curwin->w_cursor.lnum)
2408 curwin->w_topline = curwin->w_cursor.lnum;
2409#ifdef FEAT_DIFF
2410 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2411 if (curwin->w_topfill > 0 && extra > off)
2412 {
2413 curwin->w_topfill -= extra - off;
2414 if (curwin->w_topfill < 0)
2415 curwin->w_topfill = 0;
2416 }
2417 check_topfill(curwin, FALSE);
2418#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002419 if (curwin->w_topline == curwin->w_cursor.lnum)
2420 {
2421 validate_virtcol();
2422 if (curwin->w_skipcol >= curwin->w_virtcol)
2423 // TODO: if the line doesn't fit may optimize w_skipcol instead
2424 // of making it zero
2425 reset_skipcol();
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002428 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#ifdef FEAT_DIFF
2430 || curwin->w_topfill != old_topfill
2431#endif
2432 )
2433 curwin->w_valid &=
2434 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2435 curwin->w_valid |= VALID_TOPLINE;
2436 }
2437}
2438
2439/*
2440 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2441 * screen lines for text lines.
2442 */
2443 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002444set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446#ifdef FEAT_DIFF
2447 wp->w_filler_rows = 0;
2448#endif
2449 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002450 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 else
2452 {
2453 wp->w_empty_rows = wp->w_height - used;
2454#ifdef FEAT_DIFF
2455 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2456 {
2457 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2458 if (wp->w_empty_rows > wp->w_filler_rows)
2459 wp->w_empty_rows -= wp->w_filler_rows;
2460 else
2461 {
2462 wp->w_filler_rows = wp->w_empty_rows;
2463 wp->w_empty_rows = 0;
2464 }
2465 }
2466#endif
2467 }
2468}
2469
2470/*
2471 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002472 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2474 * This is messy stuff!!!
2475 */
2476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002477scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 int used;
2480 int scrolled = 0;
2481 int extra = 0;
2482 int i;
2483 linenr_T line_count;
2484 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002485 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 lineoff_T loff;
2487 lineoff_T boff;
2488#ifdef FEAT_DIFF
2489 int old_topfill = curwin->w_topfill;
2490 int fill_below_window;
2491#endif
2492 linenr_T old_botline = curwin->w_botline;
2493 linenr_T old_valid = curwin->w_valid;
2494 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002495 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002496 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002497 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 cln = curwin->w_cursor.lnum;
2500 if (set_topbot)
2501 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 int set_skipcol = FALSE;
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 used = 0;
2505 curwin->w_botline = cln + 1;
2506#ifdef FEAT_DIFF
2507 loff.fill = 0;
2508#endif
2509 for (curwin->w_topline = curwin->w_botline;
2510 curwin->w_topline > 1;
2511 curwin->w_topline = loff.lnum)
2512 {
2513 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002514 topline_back_winheight(&loff, FALSE);
2515 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002517 if (used + loff.height > curwin->w_height)
2518 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002519 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002520 {
2521 // 'smoothscroll' and 'wrap' are set. The above line is
2522 // too long to show in its entirety, so we show just a part
2523 // of it.
2524 if (used < curwin->w_height)
2525 {
2526 int plines_offset = used + loff.height
2527 - curwin->w_height;
2528 used = curwin->w_height;
2529#ifdef FEAT_DIFF
2530 curwin->w_topfill = loff.fill;
2531#endif
2532 curwin->w_topline = loff.lnum;
2533 curwin->w_skipcol = skipcol_from_plines(
2534 curwin, plines_offset);
2535 set_skipcol = TRUE;
2536 }
2537 }
2538 break;
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 used += loff.height;
2541#ifdef FEAT_DIFF
2542 curwin->w_topfill = loff.fill;
2543#endif
2544 }
2545 set_empty_rows(curwin, used);
2546 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2547 if (curwin->w_topline != old_topline
2548#ifdef FEAT_DIFF
2549 || curwin->w_topfill != old_topfill
2550#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002551 || set_skipcol
2552 || curwin->w_skipcol != 0)
2553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002555 if (set_skipcol)
2556 redraw_later(UPD_NOT_VALID);
2557 else
2558 reset_skipcol();
2559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561 else
2562 validate_botline();
2563
Bram Moolenaar85a20022019-12-21 18:25:54 +01002564 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#ifdef FEAT_DIFF
2566 used = plines_nofill(cln);
2567#else
2568 validate_cheight();
2569 used = curwin->w_cline_height;
2570#endif
2571
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002572 // If the cursor is on or below botline, we will at least scroll by the
2573 // height of the cursor line, which is "used". Correct for empty lines,
2574 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (cln >= curwin->w_botline)
2576 {
2577 scrolled = used;
2578 if (cln == curwin->w_botline)
2579 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002580 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002581 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002582 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002583 // Calculate how many screen lines the current top line of window
2584 // occupies. If it is occupying more than the entire window, we
2585 // need to scroll the additional clipped lines to scroll past the
2586 // top line before we can move on to the other lines.
2587 int top_plines =
2588#ifdef FEAT_DIFF
2589 plines_win_nofill
2590#else
2591 plines_win
2592#endif
2593 (curwin, curwin->w_topline, FALSE);
2594 int skip_lines = 0;
2595 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002596 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002597 {
fullwaywang8154e642023-06-24 21:58:09 +01002598 int width2 = width1 + curwin_col_off2();
2599 // similar formula is used in curs_columns()
2600 if (curwin->w_skipcol > width1)
2601 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2602 else if (curwin->w_skipcol > 0)
2603 skip_lines = 1;
2604
2605 top_plines -= skip_lines;
2606 if (top_plines > curwin->w_height)
2607 {
2608 scrolled += (top_plines - curwin->w_height);
2609 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002610 }
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
2614 /*
2615 * Stop counting lines to scroll when
2616 * - hitting start of the file
2617 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002618 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * - lines between botline and cursor have been counted
2620 */
2621#ifdef FEAT_FOLDING
2622 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2623#endif
2624 {
2625 loff.lnum = cln;
2626 boff.lnum = cln;
2627 }
2628#ifdef FEAT_DIFF
2629 loff.fill = 0;
2630 boff.fill = 0;
2631 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2632 - curwin->w_filler_rows;
2633#endif
2634
2635 while (loff.lnum > 1)
2636 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002637 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2638 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002640 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2642 && loff.lnum <= curwin->w_botline
2643#ifdef FEAT_DIFF
2644 && (loff.lnum < curwin->w_botline
2645 || loff.fill >= fill_below_window)
2646#endif
2647 )
2648 break;
2649
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002652 if (loff.height == MAXCOL)
2653 used = MAXCOL;
2654 else
2655 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (used > curwin->w_height)
2657 break;
2658 if (loff.lnum >= curwin->w_botline
2659#ifdef FEAT_DIFF
2660 && (loff.lnum > curwin->w_botline
2661 || loff.fill <= fill_below_window)
2662#endif
2663 )
2664 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002665 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 scrolled += loff.height;
2667 if (loff.lnum == curwin->w_botline
2668#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002669 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670#endif
2671 )
2672 scrolled -= curwin->w_empty_rows;
2673 }
2674
2675 if (boff.lnum < curbuf->b_ml.ml_line_count)
2676 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 botline_forw(&boff);
2679 used += boff.height;
2680 if (used > curwin->w_height)
2681 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002682 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2683 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
2685 extra += boff.height;
2686 if (boff.lnum >= curwin->w_botline
2687#ifdef FEAT_DIFF
2688 || (boff.lnum + 1 == curwin->w_botline
2689 && boff.fill > curwin->w_filler_rows)
2690#endif
2691 )
2692 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 scrolled += boff.height;
2695 if (boff.lnum == curwin->w_botline
2696#ifdef FEAT_DIFF
2697 && boff.fill == 0
2698#endif
2699 )
2700 scrolled -= curwin->w_empty_rows;
2701 }
2702 }
2703 }
2704 }
2705
Bram Moolenaar85a20022019-12-21 18:25:54 +01002706 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (scrolled <= 0)
2708 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002709 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 else if (used > curwin->w_height)
2711 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002712 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 else
2714 {
2715 line_count = 0;
2716#ifdef FEAT_DIFF
2717 boff.fill = curwin->w_topfill;
2718#endif
2719 boff.lnum = curwin->w_topline - 1;
2720 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2721 {
2722 botline_forw(&boff);
2723 i += boff.height;
2724 ++line_count;
2725 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002726 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 line_count = 9999;
2728 }
2729
2730 /*
2731 * Scroll up if the cursor is off the bottom of the screen a bit.
2732 * Otherwise put it at 1/2 of the screen.
2733 */
2734 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002735 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002736 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002737 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002738 if (do_sms)
2739 scrollup(scrolled, TRUE); // TODO
2740 else
2741 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /*
2745 * If topline didn't change we need to restore w_botline and w_empty_rows
2746 * (we changed them).
2747 * If topline did change, update_screen() will set botline.
2748 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002749 if (curwin->w_topline == old_topline
2750 && curwin->w_skipcol == old_skipcol
2751 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 curwin->w_botline = old_botline;
2754 curwin->w_empty_rows = old_empty_rows;
2755 curwin->w_valid = old_valid;
2756 }
2757 curwin->w_valid |= VALID_TOPLINE;
2758}
2759
2760/*
2761 * Recompute topline to put the cursor halfway the window
2762 * If "atend" is TRUE, also put it halfway at the end of the file.
2763 */
2764 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002765scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 int above = 0;
2768 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002769 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#ifdef FEAT_DIFF
2771 int topfill = 0;
2772#endif
2773 int below = 0;
2774 int used;
2775 lineoff_T loff;
2776 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002777#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002778 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002781#ifdef FEAT_PROP_POPUP
2782 // if the width changed this needs to be updated first
2783 may_update_popup_position();
2784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2786#ifdef FEAT_FOLDING
2787 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2788#endif
2789#ifdef FEAT_DIFF
2790 used = plines_nofill(loff.lnum);
2791 loff.fill = 0;
2792 boff.fill = 0;
2793#else
2794 used = plines(loff.lnum);
2795#endif
2796 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002797
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002798 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002799 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2800 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002801 {
2802 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002803 if (atend)
2804 {
2805 want_height = (curwin->w_height - used) / 2;
2806 used = 0;
2807 }
2808 else
2809 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002810 }
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 while (topline > 1)
2813 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002814 // If using smoothscroll, we can precisely scroll to the
2815 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002816 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002817 {
2818 topline_back_winheight(&loff, FALSE);
2819 if (loff.height == MAXCOL)
2820 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002821 used += loff.height;
2822 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002823 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002824 botline_forw(&boff);
2825 used += boff.height;
2826 }
2827 if (used > want_height)
2828 {
2829 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002830 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002831 topline = loff.lnum;
2832#ifdef FEAT_DIFF
2833 topfill = loff.fill;
2834#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002835 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002836 }
2837 break;
2838 }
2839 topline = loff.lnum;
2840#ifdef FEAT_DIFF
2841 topfill = loff.fill;
2842#endif
2843 continue;
2844 }
2845
2846 // If not using smoothscroll, we have to iteratively find how many
2847 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002848 // This may not be right in the middle if the lines'
2849 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002850
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002851 // Depending on "prefer_above" we add a line above or below first.
2852 // Loop twice to avoid duplicating code.
2853 int done = FALSE;
2854 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002856 if (prefer_above ? (round == 2 && below < above)
2857 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002859 // add a line below the cursor
2860 if (boff.lnum < curbuf->b_ml.ml_line_count)
2861 {
2862 botline_forw(&boff);
2863 used += boff.height;
2864 if (used > curwin->w_height)
2865 {
2866 done = TRUE;
2867 break;
2868 }
2869 below += boff.height;
2870 }
2871 else
2872 {
2873 ++below; // count a "~" line
2874 if (atend)
2875 ++used;
2876 }
2877 }
2878
2879 if (prefer_above ? (round == 1 && below >= above)
2880 : (round == 1 && below > above))
2881 {
2882 // add a line above the cursor
2883 topline_back(&loff);
2884 if (loff.height == MAXCOL)
2885 used = MAXCOL;
2886 else
2887 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002889 {
2890 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002892 }
2893 above += loff.height;
2894 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002896 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002900 if (done)
2901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#ifdef FEAT_FOLDING
2905 if (!hasFolding(topline, &curwin->w_topline, NULL))
2906#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002907 {
2908 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002909 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002910 || curwin->w_skipcol != 0)
2911 {
2912 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002913 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002914 {
2915 curwin->w_skipcol = skipcol;
2916 redraw_later(UPD_NOT_VALID);
2917 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002918 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002919 reset_skipcol();
2920 }
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#ifdef FEAT_DIFF
2923 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002924 if (old_topline > curwin->w_topline + curwin->w_height)
2925 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 check_topfill(curwin, FALSE);
2927#endif
2928 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2929 curwin->w_valid |= VALID_TOPLINE;
2930}
2931
2932/*
2933 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002934 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * If not possible, put it at the same position as scroll_cursor_halfway().
2936 * When called topline must be valid!
2937 */
2938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002939cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002941 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002943 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 linenr_T botline;
2945 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002948 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /*
2951 * How many lines we would like to have above/below the cursor depends on
2952 * whether the first/last line of the file is on screen.
2953 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002954 above_wanted = so;
2955 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002956 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
2958 above_wanted = mouse_dragging - 1;
2959 below_wanted = mouse_dragging - 1;
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (curwin->w_topline == 1)
2962 {
2963 above_wanted = 0;
2964 max_off = curwin->w_height / 2;
2965 if (below_wanted > max_off)
2966 below_wanted = max_off;
2967 }
2968 validate_botline();
2969 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002970 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 below_wanted = 0;
2973 max_off = (curwin->w_height - 1) / 2;
2974 if (above_wanted > max_off)
2975 above_wanted = max_off;
2976 }
2977
2978 /*
2979 * If there are sufficient file-lines above and below the cursor, we can
2980 * return now.
2981 */
2982 cln = curwin->w_cursor.lnum;
2983 if (cln >= curwin->w_topline + above_wanted
2984 && cln < curwin->w_botline - below_wanted
2985#ifdef FEAT_FOLDING
2986 && !hasAnyFolding(curwin)
2987#endif
2988 )
2989 return;
2990
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002991 if (curwin->w_p_sms && !curwin->w_p_wrap)
2992 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002993 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002994 if (curwin->w_cline_height == curwin->w_height)
2995 {
2996 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002997 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002998 return;
2999 }
3000 // TODO: If the cursor line doesn't fit in the window then only adjust
3001 // w_skipcol.
3002 }
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /*
3005 * Narrow down the area where the cursor can be put by taking lines from
3006 * the top and the bottom until:
3007 * - the desired context lines are found
3008 * - the lines from the top is past the lines from the bottom
3009 */
3010 topline = curwin->w_topline;
3011 botline = curwin->w_botline - 1;
3012#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003013 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 above = curwin->w_topfill;
3015 below = curwin->w_filler_rows;
3016#endif
3017 while ((above < above_wanted || below < below_wanted) && topline < botline)
3018 {
3019 if (below < below_wanted && (below <= above || above >= above_wanted))
3020 {
3021#ifdef FEAT_FOLDING
3022 if (hasFolding(botline, &botline, NULL))
3023 ++below;
3024 else
3025#endif
3026 below += plines(botline);
3027 --botline;
3028 }
3029 if (above < above_wanted && (above < below || below >= below_wanted))
3030 {
3031#ifdef FEAT_FOLDING
3032 if (hasFolding(topline, NULL, &topline))
3033 ++above;
3034 else
3035#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003036 above += PLINES_NOFILL(topline);
3037#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003038 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (topline < botline)
3040 above += diff_check_fill(curwin, topline + 1);
3041#endif
3042 ++topline;
3043 }
3044 }
3045 if (topline == botline || botline == 0)
3046 curwin->w_cursor.lnum = topline;
3047 else if (topline > botline)
3048 curwin->w_cursor.lnum = botline;
3049 else
3050 {
3051 if (cln < topline && curwin->w_topline > 1)
3052 {
3053 curwin->w_cursor.lnum = topline;
3054 curwin->w_valid &=
3055 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3056 }
3057 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3058 {
3059 curwin->w_cursor.lnum = botline;
3060 curwin->w_valid &=
3061 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3062 }
3063 }
3064 curwin->w_valid |= VALID_TOPLINE;
3065}
3066
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003067static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003070 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3071 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003073 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
3075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 long n;
3079 int retval = OK;
3080 lineoff_T loff;
3081 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003082 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaar85a20022019-12-21 18:25:54 +01003084 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 beep_flush();
3087 return FAIL;
3088 }
3089
3090 for ( ; count > 0; --count)
3091 {
3092 validate_botline();
3093 /*
3094 * It's an error to move a page up when the first line is already on
3095 * the screen. It's an error to move a page down when the last line
3096 * is on the screen and the topline is 'scrolloff' lines from the
3097 * last line.
3098 */
3099 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003100 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3102 : (curwin->w_topline == 1
3103#ifdef FEAT_DIFF
3104 && curwin->w_topfill ==
3105 diff_check_fill(curwin, curwin->w_topline)
3106#endif
3107 ))
3108 {
3109 beep_flush();
3110 retval = FAIL;
3111 break;
3112 }
3113
3114#ifdef FEAT_DIFF
3115 loff.fill = 0;
3116#endif
3117 if (dir == FORWARD)
3118 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003119 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003121 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003122 if (p_window <= 2)
3123 ++curwin->w_topline;
3124 else
3125 curwin->w_topline += p_window - 2;
3126 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3127 curwin->w_topline = curbuf->b_ml.ml_line_count;
3128 curwin->w_cursor.lnum = curwin->w_topline;
3129 }
3130 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003132 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 curwin->w_topline = curbuf->b_ml.ml_line_count;
3134#ifdef FEAT_DIFF
3135 curwin->w_topfill = 0;
3136#endif
3137 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3138 }
3139 else
3140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // For the overlap, start with the line just below the window
3142 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 loff.lnum = curwin->w_botline;
3144#ifdef FEAT_DIFF
3145 loff.fill = diff_check_fill(curwin, loff.lnum)
3146 - curwin->w_filler_rows;
3147#endif
3148 get_scroll_overlap(&loff, -1);
3149 curwin->w_topline = loff.lnum;
3150#ifdef FEAT_DIFF
3151 curwin->w_topfill = loff.fill;
3152 check_topfill(curwin, FALSE);
3153#endif
3154 curwin->w_cursor.lnum = curwin->w_topline;
3155 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3156 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3157 }
3158 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161#ifdef FEAT_DIFF
3162 if (curwin->w_topline == 1)
3163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 max_topfill();
3166 continue;
3167 }
3168#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003169 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003172 if (p_window <= 2)
3173 --curwin->w_topline;
3174 else
3175 curwin->w_topline -= p_window - 2;
3176 if (curwin->w_topline < 1)
3177 curwin->w_topline = 1;
3178 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3179 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3180 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3181 continue;
3182 }
3183
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 // Find the line at the top of the window that is going to be the
3185 // line at the bottom of the window. Make sure this results in
3186 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 loff.lnum = curwin->w_topline - 1;
3188#ifdef FEAT_DIFF
3189 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3190 - curwin->w_topfill;
3191#endif
3192 get_scroll_overlap(&loff, 1);
3193
3194 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3195 {
3196 loff.lnum = curbuf->b_ml.ml_line_count;
3197#ifdef FEAT_DIFF
3198 loff.fill = 0;
3199 }
3200 else
3201 {
3202 botline_topline(&loff);
3203#endif
3204 }
3205 curwin->w_cursor.lnum = loff.lnum;
3206
Bram Moolenaar85a20022019-12-21 18:25:54 +01003207 // Find the line just above the new topline to get the right line
3208 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 n = 0;
3210 while (n <= curwin->w_height && loff.lnum >= 1)
3211 {
3212 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003213 if (loff.height == MAXCOL)
3214 n = MAXCOL;
3215 else
3216 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 curwin->w_topline = 1;
3221#ifdef FEAT_DIFF
3222 max_topfill();
3223#endif
3224 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3225 }
3226 else
3227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003228 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#ifdef FEAT_DIFF
3230 topline_botline(&loff);
3231#endif
3232 botline_forw(&loff);
3233 botline_forw(&loff);
3234#ifdef FEAT_DIFF
3235 botline_topline(&loff);
3236#endif
3237#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3240#endif
3241
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 // Always scroll at least one line. Avoid getting stuck on
3243 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (loff.lnum >= curwin->w_topline
3245#ifdef FEAT_DIFF
3246 && (loff.lnum > curwin->w_topline
3247 || loff.fill >= curwin->w_topfill)
3248#endif
3249 )
3250 {
3251#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003252 // First try using the maximum number of filler lines. If
3253 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 loff.fill = curwin->w_topfill;
3255 if (curwin->w_topfill < diff_check_fill(curwin,
3256 curwin->w_topline))
3257 max_topfill();
3258 if (curwin->w_topfill == loff.fill)
3259#endif
3260 {
3261 --curwin->w_topline;
3262#ifdef FEAT_DIFF
3263 curwin->w_topfill = 0;
3264#endif
zeertzjq05834912023-10-04 20:12:37 +02003265 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267 comp_botline(curwin);
3268 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003269 curwin->w_valid &=
3270 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272 else
3273 {
3274 curwin->w_topline = loff.lnum;
3275#ifdef FEAT_DIFF
3276 curwin->w_topfill = loff.fill;
3277 check_topfill(curwin, FALSE);
3278#endif
3279 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3280 }
3281 }
3282 }
3283 }
3284#ifdef FEAT_FOLDING
3285 foldAdjustCursor();
3286#endif
3287 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003288 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003289 if (retval == OK)
3290 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3292
Bram Moolenaar907dad72018-07-10 15:07:15 +02003293 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003295 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3296 // But make sure we scroll at least one line (happens with mix of long
3297 // wrapping lines and non-wrapping line).
3298 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003300 scroll_cursor_top(1, FALSE);
3301 if (curwin->w_topline <= old_topline
3302 && old_topline < curbuf->b_ml.ml_line_count)
3303 {
3304 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003306 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3307#endif
3308 }
3309 }
3310#ifdef FEAT_FOLDING
3311 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003316 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return retval;
3318}
3319
3320/*
3321 * Decide how much overlap to use for page-up or page-down scrolling.
3322 * This is symmetric, so that doing both keeps the same lines displayed.
3323 * Three lines are examined:
3324 *
3325 * before CTRL-F after CTRL-F / before CTRL-B
3326 * etc. l1
3327 * l1 last but one line ------------
3328 * l2 last text line l2 top text line
3329 * ------------- l3 second text line
3330 * l3 etc.
3331 */
3332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003333get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int h1, h2, h3, h4;
3336 int min_height = curwin->w_height - 2;
3337 lineoff_T loff0, loff1, loff2;
3338
3339#ifdef FEAT_DIFF
3340 if (lp->fill > 0)
3341 lp->height = 1;
3342 else
3343 lp->height = plines_nofill(lp->lnum);
3344#else
3345 lp->height = plines(lp->lnum);
3346#endif
3347 h1 = lp->height;
3348 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003349 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 loff0 = *lp;
3352 if (dir > 0)
3353 botline_forw(lp);
3354 else
3355 topline_back(lp);
3356 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003357 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003359 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 return;
3361 }
3362
3363 loff1 = *lp;
3364 if (dir > 0)
3365 botline_forw(lp);
3366 else
3367 topline_back(lp);
3368 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003369 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003371 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return;
3373 }
3374
3375 loff2 = *lp;
3376 if (dir > 0)
3377 botline_forw(lp);
3378 else
3379 topline_back(lp);
3380 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003381 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003382 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003384 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385}
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387/*
3388 * Scroll 'scroll' lines up or down.
3389 */
3390 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003391halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 long scrolled = 0;
3394 int i;
3395 int n;
3396 int room;
3397
3398 if (Prenum)
3399 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3400 curwin->w_height : Prenum;
3401 n = (curwin->w_p_scr <= curwin->w_height) ?
3402 curwin->w_p_scr : curwin->w_height;
3403
Bram Moolenaard5d37532017-03-27 23:02:07 +02003404 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 validate_botline();
3406 room = curwin->w_empty_rows;
3407#ifdef FEAT_DIFF
3408 room += curwin->w_filler_rows;
3409#endif
3410 if (flag)
3411 {
3412 /*
3413 * scroll the text up
3414 */
3415 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3416 {
3417#ifdef FEAT_DIFF
3418 if (curwin->w_topfill > 0)
3419 {
3420 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003421 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 --curwin->w_topfill;
3423 }
3424 else
3425#endif
3426 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003427 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 n -= i;
3429 if (n < 0 && scrolled > 0)
3430 break;
3431#ifdef FEAT_FOLDING
3432 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3433#endif
3434 ++curwin->w_topline;
3435#ifdef FEAT_DIFF
3436 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3437#endif
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3440 {
3441 ++curwin->w_cursor.lnum;
3442 curwin->w_valid &=
3443 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3447 scrolled += i;
3448
3449 /*
3450 * Correct w_botline for changed w_topline.
3451 * Won't work when there are filler lines.
3452 */
3453#ifdef FEAT_DIFF
3454 if (curwin->w_p_diff)
3455 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3456 else
3457#endif
3458 {
3459 room += i;
3460 do
3461 {
3462 i = plines(curwin->w_botline);
3463 if (i > room)
3464 break;
3465#ifdef FEAT_FOLDING
3466 (void)hasFolding(curwin->w_botline, NULL,
3467 &curwin->w_botline);
3468#endif
3469 ++curwin->w_botline;
3470 room -= i;
3471 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3472 }
3473 }
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 /*
3476 * When hit bottom of the file: move cursor down.
3477 */
3478 if (n > 0)
3479 {
3480# ifdef FEAT_FOLDING
3481 if (hasAnyFolding(curwin))
3482 {
3483 while (--n >= 0
3484 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3485 {
3486 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3487 &curwin->w_cursor.lnum);
3488 ++curwin->w_cursor.lnum;
3489 }
3490 }
3491 else
3492# endif
3493 curwin->w_cursor.lnum += n;
3494 check_cursor_lnum();
3495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else
3498 {
3499 /*
3500 * scroll the text down
3501 */
3502 while (n > 0 && curwin->w_topline > 1)
3503 {
3504#ifdef FEAT_DIFF
3505 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3506 {
3507 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003508 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 ++curwin->w_topfill;
3510 }
3511 else
3512#endif
3513 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003514 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 n -= i;
3516 if (n < 0 && scrolled > 0)
3517 break;
3518 --curwin->w_topline;
3519#ifdef FEAT_FOLDING
3520 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3521#endif
3522#ifdef FEAT_DIFF
3523 curwin->w_topfill = 0;
3524#endif
3525 }
3526 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3527 VALID_BOTLINE|VALID_BOTLINE_AP);
3528 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (curwin->w_cursor.lnum > 1)
3530 {
3531 --curwin->w_cursor.lnum;
3532 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
3537 * When hit top of the file: move cursor up.
3538 */
3539 if (n > 0)
3540 {
3541 if (curwin->w_cursor.lnum <= (linenr_T)n)
3542 curwin->w_cursor.lnum = 1;
3543 else
3544# ifdef FEAT_FOLDING
3545 if (hasAnyFolding(curwin))
3546 {
3547 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3548 {
3549 --curwin->w_cursor.lnum;
3550 (void)hasFolding(curwin->w_cursor.lnum,
3551 &curwin->w_cursor.lnum, NULL);
3552 }
3553 }
3554 else
3555# endif
3556 curwin->w_cursor.lnum -= n;
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003560 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 foldAdjustCursor();
3562# endif
3563#ifdef FEAT_DIFF
3564 check_topfill(curwin, !flag);
3565#endif
3566 cursor_correct();
3567 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003568 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003572do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573{
3574 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003575 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003576 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003577 colnr_T curswant = curwin->w_curswant;
3578 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579 win_T *old_curwin = curwin;
3580 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 int old_VIsual_select = VIsual_select;
3582 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583
3584 /*
3585 * loop through the cursorbound windows
3586 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003588 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 {
3590 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003591 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 if (curwin != old_curwin && curwin->w_p_crb)
3593 {
3594# ifdef FEAT_DIFF
3595 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003596 curwin->w_cursor.lnum =
3597 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 else
3599# endif
3600 curwin->w_cursor.lnum = line;
3601 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003602 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003603 curwin->w_curswant = curswant;
3604 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605
Bram Moolenaar85a20022019-12-21 18:25:54 +01003606 // Make sure the cursor is in a valid position. Temporarily set
3607 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003608 int restart_edit_save = restart_edit;
3609 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003611
3612 // Avoid a scroll here for the cursor position, 'scrollbind' is
3613 // more important.
3614 if (!curwin->w_p_scb)
3615 validate_cursor();
3616
Bram Moolenaar61452852011-02-01 18:01:11 +01003617 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003618 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 if (has_mbyte)
3620 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003621 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003622
Bram Moolenaar85a20022019-12-21 18:25:54 +01003623 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003624 if (!curwin->w_p_scb)
3625 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627 }
3628 }
3629
3630 /*
3631 * reset current-window
3632 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003633 VIsual_select = old_VIsual_select;
3634 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 curwin = old_curwin;
3636 curbuf = old_curbuf;
3637}