blob: 42878e4ad9c2879a2a417434c86290ccf7a9067c [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)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001756 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001757 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001758 // even so col is not used anymore,
1759 // make sure it is correct, just in case
1760 col = col % width2;
1761 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001762 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001763 {
1764 curwin->w_curswant = curwin->w_virtcol
1765 - (row - curwin->w_height + 1) * width2;
1766 coladvance(curwin->w_curswant);
1767 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775scrollup(
1776 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001779 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001781 if (do_sms
1782# ifdef FEAT_FOLDING
1783 || (byfold && hasAnyFolding(curwin))
1784# endif
1785# ifdef FEAT_DIFF
1786 || (curwin->w_p_diff && !curwin->w_p_wrap)
1787# endif
1788 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001790 int width1 = curwin->w_width - curwin_col_off();
1791 int width2 = width1 + curwin_col_off2();
1792 int size = 0;
zeertzjq3c802272023-06-03 22:08:33 +01001793 colnr_T prev_skipcol = curwin->w_skipcol;
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001794
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001795 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001796 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001797
1798 // diff mode: first consume "topfill"
1799 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1800 // the line, then advance to the next line.
1801 // folding: count each sequence of folded lines as one logical line.
1802 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804# ifdef FEAT_DIFF
1805 if (curwin->w_topfill > 0)
1806 --curwin->w_topfill;
1807 else
1808# endif
1809 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001810 linenr_T lnum = curwin->w_topline;
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812# ifdef FEAT_FOLDING
1813 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001814 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 (void)hasFolding(lnum, NULL, &lnum);
1816# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001817 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001818 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001819 // 'smoothscroll': increase "w_skipcol" until it goes over
1820 // the end of the line, then advance to the next line.
1821 int add = curwin->w_skipcol > 0 ? width2 : width1;
1822 curwin->w_skipcol += add;
1823 if (curwin->w_skipcol >= size)
1824 {
1825 if (lnum == curbuf->b_ml.ml_line_count)
1826 {
1827 // at the last screen line, can't scroll further
1828 curwin->w_skipcol -= add;
1829 break;
1830 }
1831 ++lnum;
1832 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001833 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001834 else
1835 {
1836 if (lnum >= curbuf->b_ml.ml_line_count)
1837 break;
1838 ++lnum;
1839 }
1840
1841 if (lnum > curwin->w_topline)
1842 {
1843 // approximate w_botline
1844 curwin->w_botline += lnum - curwin->w_topline;
1845 curwin->w_topline = lnum;
1846# ifdef FEAT_DIFF
1847 curwin->w_topfill = diff_check_fill(curwin, lnum);
1848# endif
1849 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001850 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001851 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001852 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001853 }
1854 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001855
zeertzjqd9a92dc2023-06-05 18:41:35 +01001856 if (prev_skipcol > 0 || curwin->w_skipcol > 0)
1857 // need to redraw more, because wl_size of the (new) topline may
1858 // now be invalid
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001859 redraw_later(UPD_NOT_VALID);
1860 }
1861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
1863 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001864 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866
1867 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1868 curwin->w_topline = curbuf->b_ml.ml_line_count;
1869 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1870 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1871
1872#ifdef FEAT_DIFF
1873 check_topfill(curwin, FALSE);
1874#endif
1875
1876#ifdef FEAT_FOLDING
1877 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001878 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1880#endif
1881
1882 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1883 if (curwin->w_cursor.lnum < curwin->w_topline)
1884 {
1885 curwin->w_cursor.lnum = curwin->w_topline;
1886 curwin->w_valid &=
1887 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1888 coladvance(curwin->w_curswant);
1889 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001890 if (curwin->w_cursor.lnum == curwin->w_topline
1891 && do_sms && curwin->w_skipcol > 0)
1892 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001893 int col_off = curwin_col_off();
1894 int col_off2 = curwin_col_off2();
1895
1896 int width1 = curwin->w_width - col_off;
1897 int width2 = width1 + col_off2;
1898 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001899 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001900 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001901 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001902
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001903 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001904 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001905 int overlap = scrolloff_cols != 0 ? 0
1906 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001907
Bram Moolenaar118c2352022-10-09 17:19:27 +01001908 // Make sure the cursor is in a visible part of the line, taking
1909 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001910 // If there are not enough screen lines put the cursor in the middle.
1911 if (scrolloff_cols > space_cols / 2)
1912 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001913 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001914 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001915 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001916 colnr_T col = curwin->w_virtcol;
1917
1918 if (col < width1)
1919 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001920 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001921 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001922 curwin->w_curswant = col;
1923 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001924
1925 // validate_virtcol() marked various things as valid, but after
1926 // moving the cursor they need to be recomputed
1927 curwin->w_valid &=
1928 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001929 }
1930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001933/*
1934 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1935 * valid for 'smoothscroll'.
1936 */
1937 void
1938adjust_skipcol(void)
1939{
1940 if (!curwin->w_p_wrap
1941 || !curwin->w_p_sms
1942 || curwin->w_cursor.lnum != curwin->w_topline)
1943 return;
1944
1945 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001946 if (width1 <= 0)
1947 return; // no text will be displayed
1948
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001949 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001950 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001951 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1952 int scrolled = FALSE;
1953
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001954 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001955 if (curwin->w_cline_height == curwin->w_height
1956 // w_cline_height may be capped at w_height, check there aren't
1957 // actually more lines.
1958 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1959 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001960 {
1961 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001962 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001963 return;
1964 }
1965
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001966 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001967 int overlap = sms_marker_overlap(curwin,
1968 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001969 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001970 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001971 {
1972 // scroll a screen line down
1973 if (curwin->w_skipcol >= width1 + width2)
1974 curwin->w_skipcol -= width2;
1975 else
1976 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001977 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001978 }
1979 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001980 {
1981 validate_virtcol();
1982 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001983 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001984 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001985
1986 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1987 int row = 0;
1988 if (col >= width1)
1989 {
1990 col -= width1;
1991 ++row;
1992 }
1993 if (col > width2)
Christian Brabandtb56cef02023-10-09 17:52:14 +02001994 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001995 row += col / width2;
Christian Brabandtb56cef02023-10-09 17:52:14 +02001996 // col may no longer be used, but make
1997 // sure it is correct anyhow, just in case
1998 col = col % width2;
1999 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002000 if (row >= curwin->w_height)
2001 {
2002 if (curwin->w_skipcol == 0)
2003 {
2004 curwin->w_skipcol += width1;
2005 --row;
2006 }
2007 if (row >= curwin->w_height)
2008 curwin->w_skipcol += (row - curwin->w_height) * width2;
2009 redraw_later(UPD_NOT_VALID);
2010 }
2011}
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#ifdef FEAT_DIFF
2014/*
2015 * Don't end up with too many filler lines in the window.
2016 */
2017 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002018check_topfill(
2019 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002020 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 int n;
2023
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002024 if (wp->w_topfill <= 0)
2025 return;
2026
2027 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2028 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002030 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002032 --wp->w_topline;
2033 wp->w_topfill = 0;
2034 }
2035 else
2036 {
2037 wp->w_topfill = wp->w_height - n;
2038 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041 }
2042}
2043
2044/*
2045 * Use as many filler lines as possible for w_topline. Make sure w_topline
2046 * is still visible.
2047 */
2048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002049max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 int n;
2052
2053 n = plines_nofill(curwin->w_topline);
2054 if (n >= curwin->w_height)
2055 curwin->w_topfill = 0;
2056 else
2057 {
2058 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2059 if (curwin->w_topfill + n > curwin->w_height)
2060 curwin->w_topfill = curwin->w_height - n;
2061 }
2062}
2063#endif
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065/*
2066 * Scroll the screen one line down, but don't do it if it would move the
2067 * cursor off the screen.
2068 */
2069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002070scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
2072 int end_row;
2073#ifdef FEAT_DIFF
2074 int can_fill = (curwin->w_topfill
2075 < diff_check_fill(curwin, curwin->w_topline));
2076#endif
2077
2078 if (curwin->w_topline <= 1
2079#ifdef FEAT_DIFF
2080 && !can_fill
2081#endif
2082 )
2083 return;
2084
Bram Moolenaar85a20022019-12-21 18:25:54 +01002085 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 /*
2088 * Compute the row number of the last row of the cursor line
2089 * and make sure it doesn't go off the screen. Make sure the cursor
2090 * doesn't go past 'scrolloff' lines from the screen end.
2091 */
2092 end_row = curwin->w_wrow;
2093#ifdef FEAT_DIFF
2094 if (can_fill)
2095 ++end_row;
2096 else
2097 end_row += plines_nofill(curwin->w_topline - 1);
2098#else
2099 end_row += plines(curwin->w_topline - 1);
2100#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002101 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 validate_cheight();
2104 validate_virtcol();
2105 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002106 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002108 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110#ifdef FEAT_DIFF
2111 if (can_fill)
2112 {
2113 ++curwin->w_topfill;
2114 check_topfill(curwin, TRUE);
2115 }
2116 else
2117 {
2118 --curwin->w_topline;
2119 curwin->w_topfill = 0;
2120 }
2121#else
2122 --curwin->w_topline;
2123#endif
2124#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002125 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2129 }
2130}
2131
2132/*
2133 * Scroll the screen one line up, but don't do it if it would move the cursor
2134 * off the screen.
2135 */
2136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 int start_row;
2140
2141 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2142#ifdef FEAT_DIFF
2143 && curwin->w_topfill == 0
2144#endif
2145 )
2146 return;
2147
Bram Moolenaar85a20022019-12-21 18:25:54 +01002148 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 /*
2151 * Compute the row number of the first row of the cursor line
2152 * and make sure it doesn't go off the screen. Make sure the cursor
2153 * doesn't go before 'scrolloff' lines from the screen start.
2154 */
2155#ifdef FEAT_DIFF
2156 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2157 - curwin->w_topfill;
2158#else
2159 start_row = curwin->w_wrow - plines(curwin->w_topline);
2160#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002161 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002164 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002166 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168#ifdef FEAT_DIFF
2169 if (curwin->w_topfill > 0)
2170 --curwin->w_topfill;
2171 else
2172#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002173 {
2174#ifdef FEAT_FOLDING
2175 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002178 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2181 }
2182}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184/*
2185 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2186 * a (wrapped) text line. Uses and sets "lp->fill".
2187 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002188 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 */
2190 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002191topline_back_winheight(
2192 lineoff_T *lp,
2193 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195#ifdef FEAT_DIFF
2196 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2197 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 ++lp->fill;
2200 lp->height = 1;
2201 }
2202 else
2203#endif
2204 {
2205 --lp->lnum;
2206#ifdef FEAT_DIFF
2207 lp->fill = 0;
2208#endif
2209 if (lp->lnum < 1)
2210 lp->height = MAXCOL;
2211 else
2212#ifdef FEAT_FOLDING
2213 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 lp->height = 1;
2216 else
2217#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002218 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220}
2221
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002222 static void
2223topline_back(lineoff_T *lp)
2224{
2225 topline_back_winheight(lp, TRUE);
2226}
2227
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229/*
2230 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2231 * a (wrapped) text line. Uses and sets "lp->fill".
2232 * Returns the height of the added line in "lp->height".
2233 * Lines below the last one are incredibly high.
2234 */
2235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002236botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238#ifdef FEAT_DIFF
2239 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2240 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002241 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ++lp->fill;
2243 lp->height = 1;
2244 }
2245 else
2246#endif
2247 {
2248 ++lp->lnum;
2249#ifdef FEAT_DIFF
2250 lp->fill = 0;
2251#endif
2252 if (lp->lnum > curbuf->b_ml.ml_line_count)
2253 lp->height = MAXCOL;
2254 else
2255#ifdef FEAT_FOLDING
2256 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 lp->height = 1;
2259 else
2260#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002261 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263}
2264
2265#ifdef FEAT_DIFF
2266/*
2267 * Switch from including filler lines below lp->lnum to including filler
2268 * lines above loff.lnum + 1. This keeps pointing to the same line.
2269 * When there are no filler lines nothing changes.
2270 */
2271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002272botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 if (lp->fill > 0)
2275 {
2276 ++lp->lnum;
2277 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2278 }
2279}
2280
2281/*
2282 * Switch from including filler lines above lp->lnum to including filler
2283 * lines below loff.lnum - 1. This keeps pointing to the same line.
2284 * When there are no filler lines nothing changes.
2285 */
2286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 if (lp->fill > 0)
2290 {
2291 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2292 --lp->lnum;
2293 }
2294}
2295#endif
2296
2297/*
2298 * Recompute topline to put the cursor at the top of the window.
2299 * Scroll at least "min_scroll" lines.
2300 * If "always" is TRUE, always set topline (for "zt").
2301 */
2302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002303scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 int scrolled = 0;
2306 int extra = 0;
2307 int used;
2308 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002309 linenr_T top; // just above displayed lines
2310 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002312 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313#ifdef FEAT_DIFF
2314 linenr_T old_topfill = curwin->w_topfill;
2315#endif
2316 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002317 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (mouse_dragging > 0)
2320 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 /*
2323 * Decrease topline until:
2324 * - it has become 1
2325 * - (part of) the cursor line is moved off the screen or
2326 * - moved at least 'scrolljump' lines and
2327 * - at least 'scrolloff' lines above and below the cursor
2328 */
2329 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002330 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 if (curwin->w_cursor.lnum < curwin->w_topline)
2332 scrolled = used;
2333
2334#ifdef FEAT_FOLDING
2335 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2336 {
2337 --top;
2338 ++bot;
2339 }
2340 else
2341#endif
2342 {
2343 top = curwin->w_cursor.lnum - 1;
2344 bot = curwin->w_cursor.lnum + 1;
2345 }
2346 new_topline = top + 1;
2347
2348#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002349 // "used" already contains the number of filler lines above, don't add it
2350 // again.
2351 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002352 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#endif
2354
2355 /*
2356 * Check if the lines from "top" to "bot" fit in the window. If they do,
2357 * set new_topline and advance "top" and "bot" to include more lines.
2358 */
2359 while (top > 0)
2360 {
2361#ifdef FEAT_FOLDING
2362 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002363 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 i = 1;
2365 else
2366#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002367 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002368 if (top < curwin->w_topline)
2369 scrolled += i;
2370
2371 // If scrolling is needed, scroll at least 'sj' lines.
2372 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2373 && extra >= off)
2374 break;
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 used += i;
2377 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2378 {
2379#ifdef FEAT_FOLDING
2380 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002381 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 ++used;
2383 else
2384#endif
2385 used += plines(bot);
2386 }
2387 if (used > curwin->w_height)
2388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 extra += i;
2391 new_topline = top;
2392 --top;
2393 ++bot;
2394 }
2395
2396 /*
2397 * If we don't have enough space, put cursor in the middle.
2398 * This makes sure we get the same position when using "k" and "j"
2399 * in a small window.
2400 */
2401 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002402 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 else
2404 {
2405 /*
2406 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002407 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
2409 if (new_topline < curwin->w_topline || always)
2410 curwin->w_topline = new_topline;
2411 if (curwin->w_topline > curwin->w_cursor.lnum)
2412 curwin->w_topline = curwin->w_cursor.lnum;
2413#ifdef FEAT_DIFF
2414 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2415 if (curwin->w_topfill > 0 && extra > off)
2416 {
2417 curwin->w_topfill -= extra - off;
2418 if (curwin->w_topfill < 0)
2419 curwin->w_topfill = 0;
2420 }
2421 check_topfill(curwin, FALSE);
2422#endif
Bram Moolenaar15d47472023-06-05 20:44:55 +01002423 if (curwin->w_topline == curwin->w_cursor.lnum)
2424 {
2425 validate_virtcol();
2426 if (curwin->w_skipcol >= curwin->w_virtcol)
2427 // TODO: if the line doesn't fit may optimize w_skipcol instead
2428 // of making it zero
2429 reset_skipcol();
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002432 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#ifdef FEAT_DIFF
2434 || curwin->w_topfill != old_topfill
2435#endif
2436 )
2437 curwin->w_valid &=
2438 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2439 curwin->w_valid |= VALID_TOPLINE;
2440 }
2441}
2442
2443/*
2444 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2445 * screen lines for text lines.
2446 */
2447 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002448set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450#ifdef FEAT_DIFF
2451 wp->w_filler_rows = 0;
2452#endif
2453 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002454 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 else
2456 {
2457 wp->w_empty_rows = wp->w_height - used;
2458#ifdef FEAT_DIFF
2459 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2460 {
2461 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2462 if (wp->w_empty_rows > wp->w_filler_rows)
2463 wp->w_empty_rows -= wp->w_filler_rows;
2464 else
2465 {
2466 wp->w_filler_rows = wp->w_empty_rows;
2467 wp->w_empty_rows = 0;
2468 }
2469 }
2470#endif
2471 }
2472}
2473
2474/*
2475 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002476 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2478 * This is messy stuff!!!
2479 */
2480 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002481scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 int used;
2484 int scrolled = 0;
2485 int extra = 0;
2486 int i;
2487 linenr_T line_count;
2488 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002489 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 lineoff_T loff;
2491 lineoff_T boff;
2492#ifdef FEAT_DIFF
2493 int old_topfill = curwin->w_topfill;
2494 int fill_below_window;
2495#endif
2496 linenr_T old_botline = curwin->w_botline;
2497 linenr_T old_valid = curwin->w_valid;
2498 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002499 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002500 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002501 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 cln = curwin->w_cursor.lnum;
2504 if (set_topbot)
2505 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002506 int set_skipcol = FALSE;
2507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 used = 0;
2509 curwin->w_botline = cln + 1;
2510#ifdef FEAT_DIFF
2511 loff.fill = 0;
2512#endif
2513 for (curwin->w_topline = curwin->w_botline;
2514 curwin->w_topline > 1;
2515 curwin->w_topline = loff.lnum)
2516 {
2517 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002518 topline_back_winheight(&loff, FALSE);
2519 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002521 if (used + loff.height > curwin->w_height)
2522 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002523 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002524 {
2525 // 'smoothscroll' and 'wrap' are set. The above line is
2526 // too long to show in its entirety, so we show just a part
2527 // of it.
2528 if (used < curwin->w_height)
2529 {
2530 int plines_offset = used + loff.height
2531 - curwin->w_height;
2532 used = curwin->w_height;
2533#ifdef FEAT_DIFF
2534 curwin->w_topfill = loff.fill;
2535#endif
2536 curwin->w_topline = loff.lnum;
2537 curwin->w_skipcol = skipcol_from_plines(
2538 curwin, plines_offset);
2539 set_skipcol = TRUE;
2540 }
2541 }
2542 break;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 used += loff.height;
2545#ifdef FEAT_DIFF
2546 curwin->w_topfill = loff.fill;
2547#endif
2548 }
2549 set_empty_rows(curwin, used);
2550 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2551 if (curwin->w_topline != old_topline
2552#ifdef FEAT_DIFF
2553 || curwin->w_topfill != old_topfill
2554#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002555 || set_skipcol
2556 || curwin->w_skipcol != 0)
2557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002559 if (set_skipcol)
2560 redraw_later(UPD_NOT_VALID);
2561 else
2562 reset_skipcol();
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565 else
2566 validate_botline();
2567
Bram Moolenaar85a20022019-12-21 18:25:54 +01002568 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#ifdef FEAT_DIFF
2570 used = plines_nofill(cln);
2571#else
2572 validate_cheight();
2573 used = curwin->w_cline_height;
2574#endif
2575
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002576 // If the cursor is on or below botline, we will at least scroll by the
2577 // height of the cursor line, which is "used". Correct for empty lines,
2578 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (cln >= curwin->w_botline)
2580 {
2581 scrolled = used;
2582 if (cln == curwin->w_botline)
2583 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002584 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002585 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002586 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002587 // Calculate how many screen lines the current top line of window
2588 // occupies. If it is occupying more than the entire window, we
2589 // need to scroll the additional clipped lines to scroll past the
2590 // top line before we can move on to the other lines.
2591 int top_plines =
2592#ifdef FEAT_DIFF
2593 plines_win_nofill
2594#else
2595 plines_win
2596#endif
2597 (curwin, curwin->w_topline, FALSE);
2598 int skip_lines = 0;
2599 int width1 = curwin->w_width - curwin_col_off();
fullwaywang8154e642023-06-24 21:58:09 +01002600 if (width1 > 0)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002601 {
fullwaywang8154e642023-06-24 21:58:09 +01002602 int width2 = width1 + curwin_col_off2();
2603 // similar formula is used in curs_columns()
2604 if (curwin->w_skipcol > width1)
2605 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2606 else if (curwin->w_skipcol > 0)
2607 skip_lines = 1;
2608
2609 top_plines -= skip_lines;
2610 if (top_plines > curwin->w_height)
2611 {
2612 scrolled += (top_plines - curwin->w_height);
2613 }
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002614 }
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
2618 /*
2619 * Stop counting lines to scroll when
2620 * - hitting start of the file
2621 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002622 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * - lines between botline and cursor have been counted
2624 */
2625#ifdef FEAT_FOLDING
2626 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2627#endif
2628 {
2629 loff.lnum = cln;
2630 boff.lnum = cln;
2631 }
2632#ifdef FEAT_DIFF
2633 loff.fill = 0;
2634 boff.fill = 0;
2635 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2636 - curwin->w_filler_rows;
2637#endif
2638
2639 while (loff.lnum > 1)
2640 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002641 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2642 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002644 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2646 && loff.lnum <= curwin->w_botline
2647#ifdef FEAT_DIFF
2648 && (loff.lnum < curwin->w_botline
2649 || loff.fill >= fill_below_window)
2650#endif
2651 )
2652 break;
2653
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002656 if (loff.height == MAXCOL)
2657 used = MAXCOL;
2658 else
2659 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (used > curwin->w_height)
2661 break;
2662 if (loff.lnum >= curwin->w_botline
2663#ifdef FEAT_DIFF
2664 && (loff.lnum > curwin->w_botline
2665 || loff.fill <= fill_below_window)
2666#endif
2667 )
2668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 scrolled += loff.height;
2671 if (loff.lnum == curwin->w_botline
2672#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002673 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#endif
2675 )
2676 scrolled -= curwin->w_empty_rows;
2677 }
2678
2679 if (boff.lnum < curbuf->b_ml.ml_line_count)
2680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 botline_forw(&boff);
2683 used += boff.height;
2684 if (used > curwin->w_height)
2685 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002686 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2687 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
2689 extra += boff.height;
2690 if (boff.lnum >= curwin->w_botline
2691#ifdef FEAT_DIFF
2692 || (boff.lnum + 1 == curwin->w_botline
2693 && boff.fill > curwin->w_filler_rows)
2694#endif
2695 )
2696 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 scrolled += boff.height;
2699 if (boff.lnum == curwin->w_botline
2700#ifdef FEAT_DIFF
2701 && boff.fill == 0
2702#endif
2703 )
2704 scrolled -= curwin->w_empty_rows;
2705 }
2706 }
2707 }
2708 }
2709
Bram Moolenaar85a20022019-12-21 18:25:54 +01002710 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (scrolled <= 0)
2712 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002713 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 else if (used > curwin->w_height)
2715 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002716 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 else
2718 {
2719 line_count = 0;
2720#ifdef FEAT_DIFF
2721 boff.fill = curwin->w_topfill;
2722#endif
2723 boff.lnum = curwin->w_topline - 1;
2724 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2725 {
2726 botline_forw(&boff);
2727 i += boff.height;
2728 ++line_count;
2729 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002730 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 line_count = 9999;
2732 }
2733
2734 /*
2735 * Scroll up if the cursor is off the bottom of the screen a bit.
2736 * Otherwise put it at 1/2 of the screen.
2737 */
2738 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002739 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002740 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002741 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002742 if (do_sms)
2743 scrollup(scrolled, TRUE); // TODO
2744 else
2745 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 /*
2749 * If topline didn't change we need to restore w_botline and w_empty_rows
2750 * (we changed them).
2751 * If topline did change, update_screen() will set botline.
2752 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002753 if (curwin->w_topline == old_topline
2754 && curwin->w_skipcol == old_skipcol
2755 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 curwin->w_botline = old_botline;
2758 curwin->w_empty_rows = old_empty_rows;
2759 curwin->w_valid = old_valid;
2760 }
2761 curwin->w_valid |= VALID_TOPLINE;
2762}
2763
2764/*
2765 * Recompute topline to put the cursor halfway the window
2766 * If "atend" is TRUE, also put it halfway at the end of the file.
2767 */
2768 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002769scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 int above = 0;
2772 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002773 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#ifdef FEAT_DIFF
2775 int topfill = 0;
2776#endif
2777 int below = 0;
2778 int used;
2779 lineoff_T loff;
2780 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002781#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002782 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002785#ifdef FEAT_PROP_POPUP
2786 // if the width changed this needs to be updated first
2787 may_update_popup_position();
2788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2790#ifdef FEAT_FOLDING
2791 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2792#endif
2793#ifdef FEAT_DIFF
2794 used = plines_nofill(loff.lnum);
2795 loff.fill = 0;
2796 boff.fill = 0;
2797#else
2798 used = plines(loff.lnum);
2799#endif
2800 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002801
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002802 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002803 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2804 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002805 {
2806 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002807 if (atend)
2808 {
2809 want_height = (curwin->w_height - used) / 2;
2810 used = 0;
2811 }
2812 else
2813 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002814 }
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 while (topline > 1)
2817 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002818 // If using smoothscroll, we can precisely scroll to the
2819 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002820 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002821 {
2822 topline_back_winheight(&loff, FALSE);
2823 if (loff.height == MAXCOL)
2824 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002825 used += loff.height;
2826 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002827 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002828 botline_forw(&boff);
2829 used += boff.height;
2830 }
2831 if (used > want_height)
2832 {
2833 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002834 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002835 topline = loff.lnum;
2836#ifdef FEAT_DIFF
2837 topfill = loff.fill;
2838#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002839 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002840 }
2841 break;
2842 }
2843 topline = loff.lnum;
2844#ifdef FEAT_DIFF
2845 topfill = loff.fill;
2846#endif
2847 continue;
2848 }
2849
2850 // If not using smoothscroll, we have to iteratively find how many
2851 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002852 // This may not be right in the middle if the lines'
2853 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002854
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002855 // Depending on "prefer_above" we add a line above or below first.
2856 // Loop twice to avoid duplicating code.
2857 int done = FALSE;
2858 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002860 if (prefer_above ? (round == 2 && below < above)
2861 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002863 // add a line below the cursor
2864 if (boff.lnum < curbuf->b_ml.ml_line_count)
2865 {
2866 botline_forw(&boff);
2867 used += boff.height;
2868 if (used > curwin->w_height)
2869 {
2870 done = TRUE;
2871 break;
2872 }
2873 below += boff.height;
2874 }
2875 else
2876 {
2877 ++below; // count a "~" line
2878 if (atend)
2879 ++used;
2880 }
2881 }
2882
2883 if (prefer_above ? (round == 1 && below >= above)
2884 : (round == 1 && below > above))
2885 {
2886 // add a line above the cursor
2887 topline_back(&loff);
2888 if (loff.height == MAXCOL)
2889 used = MAXCOL;
2890 else
2891 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002893 {
2894 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002896 }
2897 above += loff.height;
2898 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002900 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002904 if (done)
2905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908#ifdef FEAT_FOLDING
2909 if (!hasFolding(topline, &curwin->w_topline, NULL))
2910#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002911 {
2912 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002913 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002914 || curwin->w_skipcol != 0)
2915 {
2916 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002917 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002918 {
2919 curwin->w_skipcol = skipcol;
2920 redraw_later(UPD_NOT_VALID);
2921 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002922 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002923 reset_skipcol();
2924 }
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#ifdef FEAT_DIFF
2927 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002928 if (old_topline > curwin->w_topline + curwin->w_height)
2929 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 check_topfill(curwin, FALSE);
2931#endif
2932 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2933 curwin->w_valid |= VALID_TOPLINE;
2934}
2935
2936/*
2937 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002938 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 * If not possible, put it at the same position as scroll_cursor_halfway().
2940 * When called topline must be valid!
2941 */
2942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002943cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002945 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002947 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 linenr_T botline;
2949 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002950 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002952 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 /*
2955 * How many lines we would like to have above/below the cursor depends on
2956 * whether the first/last line of the file is on screen.
2957 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002958 above_wanted = so;
2959 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002960 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 above_wanted = mouse_dragging - 1;
2963 below_wanted = mouse_dragging - 1;
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (curwin->w_topline == 1)
2966 {
2967 above_wanted = 0;
2968 max_off = curwin->w_height / 2;
2969 if (below_wanted > max_off)
2970 below_wanted = max_off;
2971 }
2972 validate_botline();
2973 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002974 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 below_wanted = 0;
2977 max_off = (curwin->w_height - 1) / 2;
2978 if (above_wanted > max_off)
2979 above_wanted = max_off;
2980 }
2981
2982 /*
2983 * If there are sufficient file-lines above and below the cursor, we can
2984 * return now.
2985 */
2986 cln = curwin->w_cursor.lnum;
2987 if (cln >= curwin->w_topline + above_wanted
2988 && cln < curwin->w_botline - below_wanted
2989#ifdef FEAT_FOLDING
2990 && !hasAnyFolding(curwin)
2991#endif
2992 )
2993 return;
2994
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002995 if (curwin->w_p_sms && !curwin->w_p_wrap)
2996 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002997 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002998 if (curwin->w_cline_height == curwin->w_height)
2999 {
3000 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003001 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003002 return;
3003 }
3004 // TODO: If the cursor line doesn't fit in the window then only adjust
3005 // w_skipcol.
3006 }
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /*
3009 * Narrow down the area where the cursor can be put by taking lines from
3010 * the top and the bottom until:
3011 * - the desired context lines are found
3012 * - the lines from the top is past the lines from the bottom
3013 */
3014 topline = curwin->w_topline;
3015 botline = curwin->w_botline - 1;
3016#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003017 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 above = curwin->w_topfill;
3019 below = curwin->w_filler_rows;
3020#endif
3021 while ((above < above_wanted || below < below_wanted) && topline < botline)
3022 {
3023 if (below < below_wanted && (below <= above || above >= above_wanted))
3024 {
3025#ifdef FEAT_FOLDING
3026 if (hasFolding(botline, &botline, NULL))
3027 ++below;
3028 else
3029#endif
3030 below += plines(botline);
3031 --botline;
3032 }
3033 if (above < above_wanted && (above < below || below >= below_wanted))
3034 {
3035#ifdef FEAT_FOLDING
3036 if (hasFolding(topline, NULL, &topline))
3037 ++above;
3038 else
3039#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003040 above += PLINES_NOFILL(topline);
3041#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003042 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (topline < botline)
3044 above += diff_check_fill(curwin, topline + 1);
3045#endif
3046 ++topline;
3047 }
3048 }
3049 if (topline == botline || botline == 0)
3050 curwin->w_cursor.lnum = topline;
3051 else if (topline > botline)
3052 curwin->w_cursor.lnum = botline;
3053 else
3054 {
3055 if (cln < topline && curwin->w_topline > 1)
3056 {
3057 curwin->w_cursor.lnum = topline;
3058 curwin->w_valid &=
3059 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3060 }
3061 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3062 {
3063 curwin->w_cursor.lnum = botline;
3064 curwin->w_valid &=
3065 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3066 }
3067 }
3068 curwin->w_valid |= VALID_TOPLINE;
3069}
3070
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003071static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003074 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3075 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003077 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 long n;
3083 int retval = OK;
3084 lineoff_T loff;
3085 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003086 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
Bram Moolenaar85a20022019-12-21 18:25:54 +01003088 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090 beep_flush();
3091 return FAIL;
3092 }
3093
3094 for ( ; count > 0; --count)
3095 {
3096 validate_botline();
3097 /*
3098 * It's an error to move a page up when the first line is already on
3099 * the screen. It's an error to move a page down when the last line
3100 * is on the screen and the topline is 'scrolloff' lines from the
3101 * last line.
3102 */
3103 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003104 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3106 : (curwin->w_topline == 1
3107#ifdef FEAT_DIFF
3108 && curwin->w_topfill ==
3109 diff_check_fill(curwin, curwin->w_topline)
3110#endif
3111 ))
3112 {
3113 beep_flush();
3114 retval = FAIL;
3115 break;
3116 }
3117
3118#ifdef FEAT_DIFF
3119 loff.fill = 0;
3120#endif
3121 if (dir == FORWARD)
3122 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003123 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003126 if (p_window <= 2)
3127 ++curwin->w_topline;
3128 else
3129 curwin->w_topline += p_window - 2;
3130 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3131 curwin->w_topline = curbuf->b_ml.ml_line_count;
3132 curwin->w_cursor.lnum = curwin->w_topline;
3133 }
3134 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3135 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 curwin->w_topline = curbuf->b_ml.ml_line_count;
3138#ifdef FEAT_DIFF
3139 curwin->w_topfill = 0;
3140#endif
3141 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3142 }
3143 else
3144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003145 // For the overlap, start with the line just below the window
3146 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 loff.lnum = curwin->w_botline;
3148#ifdef FEAT_DIFF
3149 loff.fill = diff_check_fill(curwin, loff.lnum)
3150 - curwin->w_filler_rows;
3151#endif
3152 get_scroll_overlap(&loff, -1);
3153 curwin->w_topline = loff.lnum;
3154#ifdef FEAT_DIFF
3155 curwin->w_topfill = loff.fill;
3156 check_topfill(curwin, FALSE);
3157#endif
3158 curwin->w_cursor.lnum = curwin->w_topline;
3159 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3160 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3161 }
3162 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003163 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165#ifdef FEAT_DIFF
3166 if (curwin->w_topline == 1)
3167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003168 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 max_topfill();
3170 continue;
3171 }
3172#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003173 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003174 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003175 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003176 if (p_window <= 2)
3177 --curwin->w_topline;
3178 else
3179 curwin->w_topline -= p_window - 2;
3180 if (curwin->w_topline < 1)
3181 curwin->w_topline = 1;
3182 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3183 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3184 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3185 continue;
3186 }
3187
Bram Moolenaar85a20022019-12-21 18:25:54 +01003188 // Find the line at the top of the window that is going to be the
3189 // line at the bottom of the window. Make sure this results in
3190 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 loff.lnum = curwin->w_topline - 1;
3192#ifdef FEAT_DIFF
3193 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3194 - curwin->w_topfill;
3195#endif
3196 get_scroll_overlap(&loff, 1);
3197
3198 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3199 {
3200 loff.lnum = curbuf->b_ml.ml_line_count;
3201#ifdef FEAT_DIFF
3202 loff.fill = 0;
3203 }
3204 else
3205 {
3206 botline_topline(&loff);
3207#endif
3208 }
3209 curwin->w_cursor.lnum = loff.lnum;
3210
Bram Moolenaar85a20022019-12-21 18:25:54 +01003211 // Find the line just above the new topline to get the right line
3212 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 n = 0;
3214 while (n <= curwin->w_height && loff.lnum >= 1)
3215 {
3216 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003217 if (loff.height == MAXCOL)
3218 n = MAXCOL;
3219 else
3220 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 curwin->w_topline = 1;
3225#ifdef FEAT_DIFF
3226 max_topfill();
3227#endif
3228 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3229 }
3230 else
3231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003232 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#ifdef FEAT_DIFF
3234 topline_botline(&loff);
3235#endif
3236 botline_forw(&loff);
3237 botline_forw(&loff);
3238#ifdef FEAT_DIFF
3239 botline_topline(&loff);
3240#endif
3241#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3244#endif
3245
Bram Moolenaar85a20022019-12-21 18:25:54 +01003246 // Always scroll at least one line. Avoid getting stuck on
3247 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (loff.lnum >= curwin->w_topline
3249#ifdef FEAT_DIFF
3250 && (loff.lnum > curwin->w_topline
3251 || loff.fill >= curwin->w_topfill)
3252#endif
3253 )
3254 {
3255#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003256 // First try using the maximum number of filler lines. If
3257 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 loff.fill = curwin->w_topfill;
3259 if (curwin->w_topfill < diff_check_fill(curwin,
3260 curwin->w_topline))
3261 max_topfill();
3262 if (curwin->w_topfill == loff.fill)
3263#endif
3264 {
3265 --curwin->w_topline;
3266#ifdef FEAT_DIFF
3267 curwin->w_topfill = 0;
3268#endif
zeertzjq05834912023-10-04 20:12:37 +02003269 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 comp_botline(curwin);
3272 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003273 curwin->w_valid &=
3274 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 else
3277 {
3278 curwin->w_topline = loff.lnum;
3279#ifdef FEAT_DIFF
3280 curwin->w_topfill = loff.fill;
3281 check_topfill(curwin, FALSE);
3282#endif
3283 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3284 }
3285 }
3286 }
3287 }
3288#ifdef FEAT_FOLDING
3289 foldAdjustCursor();
3290#endif
3291 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003292 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003293 if (retval == OK)
3294 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3296
Bram Moolenaar907dad72018-07-10 15:07:15 +02003297 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003299 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3300 // But make sure we scroll at least one line (happens with mix of long
3301 // wrapping lines and non-wrapping line).
3302 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003304 scroll_cursor_top(1, FALSE);
3305 if (curwin->w_topline <= old_topline
3306 && old_topline < curbuf->b_ml.ml_line_count)
3307 {
3308 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003310 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3311#endif
3312 }
3313 }
3314#ifdef FEAT_FOLDING
3315 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003320 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return retval;
3322}
3323
3324/*
3325 * Decide how much overlap to use for page-up or page-down scrolling.
3326 * This is symmetric, so that doing both keeps the same lines displayed.
3327 * Three lines are examined:
3328 *
3329 * before CTRL-F after CTRL-F / before CTRL-B
3330 * etc. l1
3331 * l1 last but one line ------------
3332 * l2 last text line l2 top text line
3333 * ------------- l3 second text line
3334 * l3 etc.
3335 */
3336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003337get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 int h1, h2, h3, h4;
3340 int min_height = curwin->w_height - 2;
3341 lineoff_T loff0, loff1, loff2;
3342
3343#ifdef FEAT_DIFF
3344 if (lp->fill > 0)
3345 lp->height = 1;
3346 else
3347 lp->height = plines_nofill(lp->lnum);
3348#else
3349 lp->height = plines(lp->lnum);
3350#endif
3351 h1 = lp->height;
3352 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003353 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 loff0 = *lp;
3356 if (dir > 0)
3357 botline_forw(lp);
3358 else
3359 topline_back(lp);
3360 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003361 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return;
3365 }
3366
3367 loff1 = *lp;
3368 if (dir > 0)
3369 botline_forw(lp);
3370 else
3371 topline_back(lp);
3372 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003373 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003375 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return;
3377 }
3378
3379 loff2 = *lp;
3380 if (dir > 0)
3381 botline_forw(lp);
3382 else
3383 topline_back(lp);
3384 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003385 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003388 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389}
3390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391/*
3392 * Scroll 'scroll' lines up or down.
3393 */
3394 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003395halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 long scrolled = 0;
3398 int i;
3399 int n;
3400 int room;
3401
3402 if (Prenum)
3403 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3404 curwin->w_height : Prenum;
3405 n = (curwin->w_p_scr <= curwin->w_height) ?
3406 curwin->w_p_scr : curwin->w_height;
3407
Bram Moolenaard5d37532017-03-27 23:02:07 +02003408 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 validate_botline();
3410 room = curwin->w_empty_rows;
3411#ifdef FEAT_DIFF
3412 room += curwin->w_filler_rows;
3413#endif
3414 if (flag)
3415 {
3416 /*
3417 * scroll the text up
3418 */
3419 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3420 {
3421#ifdef FEAT_DIFF
3422 if (curwin->w_topfill > 0)
3423 {
3424 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003425 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 --curwin->w_topfill;
3427 }
3428 else
3429#endif
3430 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003431 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 n -= i;
3433 if (n < 0 && scrolled > 0)
3434 break;
3435#ifdef FEAT_FOLDING
3436 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3437#endif
3438 ++curwin->w_topline;
3439#ifdef FEAT_DIFF
3440 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3441#endif
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3444 {
3445 ++curwin->w_cursor.lnum;
3446 curwin->w_valid &=
3447 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3451 scrolled += i;
3452
3453 /*
3454 * Correct w_botline for changed w_topline.
3455 * Won't work when there are filler lines.
3456 */
3457#ifdef FEAT_DIFF
3458 if (curwin->w_p_diff)
3459 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3460 else
3461#endif
3462 {
3463 room += i;
3464 do
3465 {
3466 i = plines(curwin->w_botline);
3467 if (i > room)
3468 break;
3469#ifdef FEAT_FOLDING
3470 (void)hasFolding(curwin->w_botline, NULL,
3471 &curwin->w_botline);
3472#endif
3473 ++curwin->w_botline;
3474 room -= i;
3475 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3476 }
3477 }
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /*
3480 * When hit bottom of the file: move cursor down.
3481 */
3482 if (n > 0)
3483 {
3484# ifdef FEAT_FOLDING
3485 if (hasAnyFolding(curwin))
3486 {
3487 while (--n >= 0
3488 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3489 {
3490 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3491 &curwin->w_cursor.lnum);
3492 ++curwin->w_cursor.lnum;
3493 }
3494 }
3495 else
3496# endif
3497 curwin->w_cursor.lnum += n;
3498 check_cursor_lnum();
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 else
3502 {
3503 /*
3504 * scroll the text down
3505 */
3506 while (n > 0 && curwin->w_topline > 1)
3507 {
3508#ifdef FEAT_DIFF
3509 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3510 {
3511 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003512 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 ++curwin->w_topfill;
3514 }
3515 else
3516#endif
3517 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003518 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 n -= i;
3520 if (n < 0 && scrolled > 0)
3521 break;
3522 --curwin->w_topline;
3523#ifdef FEAT_FOLDING
3524 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3525#endif
3526#ifdef FEAT_DIFF
3527 curwin->w_topfill = 0;
3528#endif
3529 }
3530 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3531 VALID_BOTLINE|VALID_BOTLINE_AP);
3532 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (curwin->w_cursor.lnum > 1)
3534 {
3535 --curwin->w_cursor.lnum;
3536 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /*
3541 * When hit top of the file: move cursor up.
3542 */
3543 if (n > 0)
3544 {
3545 if (curwin->w_cursor.lnum <= (linenr_T)n)
3546 curwin->w_cursor.lnum = 1;
3547 else
3548# ifdef FEAT_FOLDING
3549 if (hasAnyFolding(curwin))
3550 {
3551 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3552 {
3553 --curwin->w_cursor.lnum;
3554 (void)hasFolding(curwin->w_cursor.lnum,
3555 &curwin->w_cursor.lnum, NULL);
3556 }
3557 }
3558 else
3559# endif
3560 curwin->w_cursor.lnum -= n;
3561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003564 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 foldAdjustCursor();
3566# endif
3567#ifdef FEAT_DIFF
3568 check_topfill(curwin, !flag);
3569#endif
3570 cursor_correct();
3571 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003572 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003574
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003576do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577{
zeertzjq8e5f26e2023-10-04 21:45:33 +02003578 static win_T *prev_curwin = NULL;
3579 static pos_T prev_cursor = {0, 0, 0};
3580
3581 if (curwin == prev_curwin && EQUAL_POS(curwin->w_cursor, prev_cursor))
3582 return;
3583 prev_curwin = curwin;
3584 prev_cursor = curwin->w_cursor;
3585
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003587 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003588 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003589 colnr_T curswant = curwin->w_curswant;
3590 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 win_T *old_curwin = curwin;
3592 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 int old_VIsual_select = VIsual_select;
3594 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595
3596 /*
3597 * loop through the cursorbound windows
3598 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003600 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 {
3602 curbuf = curwin->w_buffer;
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003603 // skip original window and windows with 'nocursorbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 if (curwin != old_curwin && curwin->w_p_crb)
3605 {
3606# ifdef FEAT_DIFF
3607 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003608 curwin->w_cursor.lnum =
3609 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 else
3611# endif
3612 curwin->w_cursor.lnum = line;
3613 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003614 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003615 curwin->w_curswant = curswant;
3616 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617
Bram Moolenaar85a20022019-12-21 18:25:54 +01003618 // Make sure the cursor is in a valid position. Temporarily set
3619 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003620 int restart_edit_save = restart_edit;
3621 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003623
3624 // Avoid a scroll here for the cursor position, 'scrollbind' is
3625 // more important.
3626 if (!curwin->w_p_scb)
3627 validate_cursor();
3628
Bram Moolenaar61452852011-02-01 18:01:11 +01003629 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003630 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003631 if (has_mbyte)
3632 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003633 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003634
Bram Moolenaar85a20022019-12-21 18:25:54 +01003635 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003636 if (!curwin->w_p_scb)
3637 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003638 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003639 }
3640 }
3641
3642 /*
3643 * reset current-window
3644 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003645 VIsual_select = old_VIsual_select;
3646 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003647 curwin = old_curwin;
3648 curbuf = old_curbuf;
3649}