blob: 3f6802ac7f30a9842574f0d980cab344f200ea08 [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/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
Luuk van Baalc8502f92023-05-06 12:40:15 +010041 int
Bram Moolenaarf6196f42022-10-02 21:29:55 +010042adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100205 * Calculates how much the 'listchars' "precedes" or 'smoothscroll' "<<<"
206 * marker overlaps with buffer text for window "wp".
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100212 int
213sms_marker_overlap(win_T *wp, int extra2)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000214{
215#if defined(FEAT_LINEBREAK)
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100216 // There is no marker overlap when in showbreak mode, thus no need to
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000217 // account for it. See wlv_screen_line().
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100218 if (*get_showbreak_value(wp) != NUL)
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000219 return 0;
220#endif
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100221 // Overlap when 'list' and 'listchars' "precedes" are set is 1.
222 if (wp->w_p_list && wp->w_lcs_chars.prec)
223 return 1;
224
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000225 return extra2 > 3 ? 0 : 3 - extra2;
226}
227
228/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000229 * Calculates the skipcol offset for window "wp" given how many
230 * physical lines we want to scroll down.
231 */
232 static int
233skipcol_from_plines(win_T *wp, int plines_off)
234{
235 int width1 = wp->w_width - win_col_off(wp);
236
237 int skipcol = 0;
238 if (plines_off > 0)
239 skipcol += width1;
240 if (plines_off > 1)
241 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
242 return skipcol;
243}
244
245/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100246 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000247 */
248 static void
249reset_skipcol(void)
250{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000251 if (curwin->w_skipcol == 0)
252 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000253
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000254 curwin->w_skipcol = 0;
255
256 // Should use the least expensive way that displays all that changed.
257 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
258 // enough when the top line gets another screen line.
259 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000260}
261
262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * Update curwin->w_topline and redraw if necessary.
264 * Used to update the screen before printing a message.
265 */
266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100267update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 update_topline();
270 if (must_redraw)
271 update_screen(0);
272}
273
274/*
275 * Update curwin->w_topline to move the cursor onto the screen.
276 */
277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 long line_count;
281 int halfheight;
282 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100314 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100316 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100352 // the marker displayed in the top-left if needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100353 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Luuk van Baal24b62ec2023-05-13 14:12:15 +0100354 int overlap = sms_marker_overlap(curwin, curwin_col_off()
355 - curwin_col_off2());
356 if (curwin->w_skipcol + overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100357 check_topline = TRUE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
363 curwin->w_topline))
364 check_topline = TRUE;
365#endif
366
367 if (check_topline)
368 {
369 halfheight = curwin->w_height / 2 - 1;
370 if (halfheight < 2)
371 halfheight = 2;
372
373#ifdef FEAT_FOLDING
374 if (hasAnyFolding(curwin))
375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // Count the number of logical lines between the cursor and
377 // topline + scrolloff (approximation of how much will be
378 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 n = 0;
380 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100381 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
383 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100384 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
386 break;
387 (void)hasFolding(lnum, NULL, &lnum);
388 }
389 }
390 else
391#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar85a20022019-12-21 18:25:54 +0100394 // If we weren't very close to begin with, we scroll to put the
395 // cursor in the middle of the window. Otherwise put the cursor
396 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000398 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 else
400 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 check_botline = TRUE;
403 }
404 }
405
406 else
407 {
408#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
411#endif
412 check_botline = TRUE;
413 }
414 }
415
416 /*
417 * If the cursor is below the bottom of the window, scroll the window
418 * to put the cursor on the window.
419 * When w_botline is invalid, recompute it first, to avoid a redraw later.
420 * If w_botline was approximated, we might need a redraw later in a few
421 * cases, but we don't want to spend (a lot of) time recomputing w_botline
422 * for every small change.
423 */
424 if (check_botline)
425 {
426 if (!(curwin->w_valid & VALID_BOTLINE_AP))
427 validate_botline();
428
429 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
430 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000431 if (curwin->w_cursor.lnum < curwin->w_botline)
432 {
433 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100434 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#ifdef FEAT_FOLDING
436 || hasAnyFolding(curwin)
437#endif
438 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 lineoff_T loff;
441
Bram Moolenaar85a20022019-12-21 18:25:54 +0100442 // Cursor is (a few lines) above botline, check if there are
443 // 'scrolloff' window lines below the cursor. If not, need to
444 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 n = curwin->w_empty_rows;
446 loff.lnum = curwin->w_cursor.lnum;
447#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100448 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
450#endif
451#ifdef FEAT_DIFF
452 loff.fill = 0;
453 n += curwin->w_filler_rows;
454#endif
455 loff.height = 0;
456 while (loff.lnum < curwin->w_botline
457#ifdef FEAT_DIFF
458 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
459#endif
460 )
461 {
462 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100463 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 break;
465 botline_forw(&loff);
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000470 }
471 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100472 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000473 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475 if (check_botline)
476 {
477#ifdef FEAT_FOLDING
478 if (hasAnyFolding(curwin))
479 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // Count the number of logical lines between the cursor and
481 // botline - scrolloff (approximation of how much will be
482 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 line_count = 0;
484 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100488 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (lnum <= 0 || line_count > curwin->w_height + 1)
490 break;
491 (void)hasFolding(lnum, &lnum, NULL);
492 }
493 }
494 else
495#endif
496 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100497 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000499 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000501 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 }
504 }
505 curwin->w_valid |= VALID_TOPLINE;
506
507 /*
508 * Need to redraw when topline changed.
509 */
510 if (curwin->w_topline != old_topline
511#ifdef FEAT_DIFF
512 || curwin->w_topfill != old_topfill
513#endif
514 )
515 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100516 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000517 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100518
Luuk van Baal0222c2d2023-05-18 13:26:57 +0100519 // When 'smoothscroll' is not set, should reset w_skipcol.
520 if (!curwin->w_p_sms)
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100521 reset_skipcol();
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000522
Bram Moolenaar85a20022019-12-21 18:25:54 +0100523 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (curwin->w_cursor.lnum == curwin->w_topline)
525 validate_cursor();
526 }
527
Bram Moolenaar375e3392019-01-31 18:26:10 +0100528 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529}
530
531/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000532 * Return the scrolljump value to use for the current window.
533 * When 'scrolljump' is positive use it as-is.
534 * When 'scrolljump' is negative use it as a percentage of the window height.
535 */
536 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100537scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000538{
539 if (p_sj >= 0)
540 return (int)p_sj;
541 return (curwin->w_height * -p_sj) / 100;
542}
543
544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
546 * current window.
547 */
548 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100549check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551 lineoff_T loff;
552 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100553 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar375e3392019-01-31 18:26:10 +0100555 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#ifdef FEAT_FOLDING
557 || hasAnyFolding(curwin)
558#endif
559 )
560 {
561 loff.lnum = curwin->w_cursor.lnum;
562#ifdef FEAT_DIFF
563 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100564 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#else
566 n = 0;
567#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100568 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100569 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100572 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (loff.lnum < curwin->w_topline
574#ifdef FEAT_DIFF
575 || (loff.lnum == curwin->w_topline && loff.fill > 0)
576#endif
577 )
578 break;
579 n += loff.height;
580 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100581 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return TRUE;
583 }
584 return FALSE;
585}
586
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100587/*
588 * Update w_curswant.
589 */
590 void
591update_curswant_force(void)
592{
593 validate_virtcol();
594 curwin->w_curswant = curwin->w_virtcol
595#ifdef FEAT_PROP_POPUP
596 - curwin->w_virtcol_first_char
597#endif
598 ;
599 curwin->w_set_curswant = FALSE;
600}
601
602/*
603 * Update w_curswant if w_set_curswant is set.
604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100606update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100609 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610}
611
612/*
613 * Check if the cursor has moved. Set the w_valid flag accordingly.
614 */
615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100616check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
619 {
620 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100621 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
622 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 wp->w_valid_cursor = wp->w_cursor;
624 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100625 wp->w_valid_skipcol = wp->w_skipcol;
626 }
627 else if (wp->w_skipcol != wp->w_valid_skipcol)
628 {
629 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
630 |VALID_CHEIGHT|VALID_CROW
631 |VALID_BOTLINE|VALID_BOTLINE_AP);
632 wp->w_valid_cursor = wp->w_cursor;
633 wp->w_valid_leftcol = wp->w_leftcol;
634 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636 else if (wp->w_cursor.col != wp->w_valid_cursor.col
637 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100638 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
641 wp->w_valid_cursor.col = wp->w_cursor.col;
642 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645}
646
647/*
648 * Call this function when some window settings have changed, which require
649 * the cursor position, botline and topline to be recomputed and the window to
650 * be redrawn. E.g, when changing the 'wrap' option or folding.
651 */
652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100653changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 changed_window_setting_win(curwin);
656}
657
658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100659changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 wp->w_lines_valid = 0;
662 changed_line_abv_curs_win(wp);
663 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100664 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666
Dominique Pellee764d1b2023-03-12 21:20:59 +0000667#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000669 * Call changed_window_setting_win() for every window containing "buf".
670 */
671 void
672changed_window_setting_buf(buf_T *buf)
673{
674 tabpage_T *tp;
675 win_T *wp;
676
677 FOR_ALL_TAB_WINDOWS(tp, wp)
678 if (wp->w_buffer == buf)
679 changed_window_setting_win(wp);
680}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000681#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000682
683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * Set wp->w_topline to a certain number.
685 */
686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100687set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200689#ifdef FEAT_DIFF
690 linenr_T prev_topline = wp->w_topline;
691#endif
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100694 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
696#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100699 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
700 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000702 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200704 if (lnum != prev_topline)
705 // Keep the filler lines when the topline didn't change.
706 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100709 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100710 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711}
712
713/*
714 * Call this function when the length of the cursor line (in screen
715 * characters) has changed, and the change is before the cursor.
716 * Need to take care of w_botline separately!
717 */
718 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100719changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
722 |VALID_CHEIGHT|VALID_TOPLINE);
723}
724
725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
729 |VALID_CHEIGHT|VALID_TOPLINE);
730}
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732/*
733 * Call this function when the length of a line (in screen characters) above
734 * the cursor have changed.
735 * Need to take care of w_botline separately!
736 */
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
741 |VALID_CHEIGHT|VALID_TOPLINE);
742}
743
744 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100745changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
748 |VALID_CHEIGHT|VALID_TOPLINE);
749}
750
Dominique Pellee764d1b2023-03-12 21:20:59 +0000751#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100753 * Display of line has changed for "buf", invalidate cursor position and
754 * w_botline.
755 */
756 void
757changed_line_display_buf(buf_T *buf)
758{
759 win_T *wp;
760
761 FOR_ALL_WINDOWS(wp)
762 if (wp->w_buffer == buf)
763 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
764 |VALID_CROW|VALID_CHEIGHT
765 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
766}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000767#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100768
769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 * Make sure the value of curwin->w_botline is valid.
771 */
772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100773validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100775 validate_botline_win(curwin);
776}
777
778/*
779 * Make sure the value of wp->w_botline is valid.
780 */
781 void
782validate_botline_win(win_T *wp)
783{
784 if (!(wp->w_valid & VALID_BOTLINE))
785 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 * Mark curwin->w_botline as invalid (because of some change in the buffer).
790 */
791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
795}
796
797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
801}
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100804approximate_botline_win(
805 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 wp->w_valid &= ~VALID_BOTLINE;
808}
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810/*
811 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
812 */
813 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100814cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 check_cursor_moved(curwin);
817 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
818 (VALID_WROW|VALID_WCOL));
819}
820
821/*
822 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
823 * w_topline must be valid, you may need to call update_topline() first!
824 */
825 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100826validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100828 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 check_cursor_moved(curwin);
830 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
831 curs_columns(TRUE);
832}
833
834#if defined(FEAT_GUI) || defined(PROTO)
835/*
836 * validate w_cline_row.
837 */
838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100839validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 /*
842 * First make sure that w_topline is valid (after moving the cursor).
843 */
844 update_topline();
845 check_cursor_moved(curwin);
846 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100847 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848}
849#endif
850
851/*
852 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200853 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100856curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
858 linenr_T lnum;
859 int i;
860 int all_invalid;
861 int valid;
862#ifdef FEAT_FOLDING
863 long fold_count;
864#endif
865
Bram Moolenaar85a20022019-12-21 18:25:54 +0100866 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 all_invalid = (!redrawing()
868 || wp->w_lines_valid == 0
869 || wp->w_lines[0].wl_lnum > wp->w_topline);
870 i = 0;
871 wp->w_cline_row = 0;
872 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
873 {
874 valid = FALSE;
875 if (!all_invalid && i < wp->w_lines_valid)
876 {
877 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (wp->w_lines[i].wl_lnum == lnum)
880 {
881#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 // Check for newly inserted lines below this row, in which
883 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (!wp->w_buffer->b_mod_set
885 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
886 || wp->w_buffer->b_mod_top
887 > wp->w_lines[i].wl_lastlnum + 1)
888#endif
889 valid = TRUE;
890 }
891 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100892 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 }
894 if (valid
895#ifdef FEAT_DIFF
896 && (lnum != wp->w_topline || !wp->w_p_diff)
897#endif
898 )
899 {
900#ifdef FEAT_FOLDING
901 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100902 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (lnum > wp->w_cursor.lnum)
904 break;
905#else
906 ++lnum;
907#endif
908 wp->w_cline_row += wp->w_lines[i].wl_size;
909 }
910 else
911 {
912#ifdef FEAT_FOLDING
913 fold_count = foldedCount(wp, lnum, NULL);
914 if (fold_count)
915 {
916 lnum += fold_count;
917 if (lnum > wp->w_cursor.lnum)
918 break;
919 ++wp->w_cline_row;
920 }
921 else
922#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100923 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100924 wp->w_cline_row += plines_correct_topline(wp, lnum);
925 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 }
929
930 check_cursor_moved(wp);
931 if (!(wp->w_valid & VALID_CHEIGHT))
932 {
933 if (all_invalid
934 || i == wp->w_lines_valid
935 || (i < wp->w_lines_valid
936 && (!wp->w_lines[i].wl_valid
937 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
938 {
939#ifdef FEAT_DIFF
940 if (wp->w_cursor.lnum == wp->w_topline)
941 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
942 TRUE) + wp->w_topfill;
943 else
944#endif
945 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
946#ifdef FEAT_FOLDING
947 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
948 NULL, NULL, TRUE, NULL);
949#endif
950 }
951 else if (i > wp->w_lines_valid)
952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100953 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 wp->w_cline_height = 0;
955#ifdef FEAT_FOLDING
956 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
957 NULL, NULL, TRUE, NULL);
958#endif
959 }
960 else
961 {
962 wp->w_cline_height = wp->w_lines[i].wl_size;
963#ifdef FEAT_FOLDING
964 wp->w_cline_folded = wp->w_lines[i].wl_folded;
965#endif
966 }
967 }
968
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100969 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Validate curwin->w_virtcol only.
975 */
976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100977validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 validate_virtcol_win(curwin);
980}
981
982/*
983 * Validate wp->w_virtcol only.
984 */
985 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100986validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000989
990 if (wp->w_valid & VALID_VIRTCOL)
991 return;
992
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100995#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000997#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000998 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000999#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001000 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/*
1004 * Validate curwin->w_cline_height only.
1005 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001007validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001010
1011 if (curwin->w_valid & VALID_CHEIGHT)
1012 return;
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001015 if (curwin->w_cursor.lnum == curwin->w_topline)
1016 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1017 + curwin->w_topfill;
1018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001022 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001024 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001028 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 */
1030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001031validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
1033 colnr_T off;
1034 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001035 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001039 if (curwin->w_valid & VALID_WCOL)
1040 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001041
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001042 col = curwin->w_virtcol;
1043 off = curwin_col_off();
1044 col += off;
1045 width = curwin->w_width - off + curwin_col_off2();
1046
1047 // long line wrapping, adjust curwin->w_wrow
1048 if (curwin->w_p_wrap
1049 && col >= (colnr_T)curwin->w_width
1050 && width > 0)
1051 // use same formula as what is used in curs_columns()
1052 col -= ((col - curwin->w_width) / width + 1) * width;
1053 if (col > (int)curwin->w_leftcol)
1054 col -= curwin->w_leftcol;
1055 else
1056 col = 0;
1057 curwin->w_wcol = col;
1058
1059 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001060#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001061 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001066 * Compute offset of a window, occupied by absolute or relative line number,
1067 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar64486672010-05-16 15:46:46 +02001072 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_FOLDING
1075 + wp->w_p_fdc
1076#endif
1077#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001078 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
1080 );
1081}
1082
1083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001084curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 return win_col_off(curwin);
1087}
1088
1089/*
1090 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001091 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1092 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
1094 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001095win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar64486672010-05-16 15:46:46 +02001097 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001098 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 return 0;
1100}
1101
1102 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001103curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 return win_col_off2(curwin);
1106}
1107
1108/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001109 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Also updates curwin->w_wrow and curwin->w_cline_row.
1111 * Also updates curwin->w_leftcol.
1112 */
1113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001115 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int off_left, off_right;
1120 int n;
1121 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001122 int width1; // text width for first screen line
1123 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 int new_leftcol;
1125 colnr_T startcol;
1126 colnr_T endcol;
1127 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001128 long so = get_scrolloff_value();
1129 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001130 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 /*
1133 * First make sure that w_topline is valid (after moving the cursor).
1134 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001135 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 /*
1138 * Next make sure that w_cline_row is valid.
1139 */
1140 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001141 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001143#ifdef FEAT_PROP_POPUP
1144 // will be set by getvvcol() but not reset
1145 curwin->w_virtcol_first_char = 0;
1146#endif
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /*
1149 * Compute the number of virtual columns.
1150 */
1151#ifdef FEAT_FOLDING
1152 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1155 else
1156#endif
1157 getvvcol(curwin, &curwin->w_cursor,
1158 &startcol, &(curwin->w_virtcol), &endcol);
1159
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001162 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 extra = curwin_col_off();
1165 curwin->w_wcol = curwin->w_virtcol + extra;
1166 endcol += extra;
1167
1168 /*
1169 * Now compute w_wrow, counting screen lines from w_cline_row.
1170 */
1171 curwin->w_wrow = curwin->w_cline_row;
1172
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001173 width1 = curwin->w_width - extra;
1174 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001177 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001178 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001179 if (curwin->w_p_wrap)
1180 curwin->w_wrow = curwin->w_height - 1;
1181 else
1182 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001184 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001186 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001188 // skip columns that are not visible
1189 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001190 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001191 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001192 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 // Deduct by multiples of width2. This allows the long line
1194 // wrapping formula below to correctly calculate the w_wcol value
1195 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001198 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001201
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001202 did_sub_skipcol = TRUE;
1203 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001204
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001206 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001209 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1210 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 curwin->w_wrow += n;
1212
1213#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // When cursor wraps to first char of next line in Insert
1215 // mode, the 'showbreak' string isn't shown, backup to first
1216 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001217 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001218 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001219 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 curwin->w_wcol = 0;
1221#endif
1222 }
1223 }
1224
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1226 // is not folded.
1227 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001228 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#ifdef FEAT_FOLDING
1230 && !curwin->w_cline_folded
1231#endif
1232 )
1233 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001234#ifdef FEAT_PROP_POPUP
1235 if (curwin->w_virtcol_first_char > 0)
1236 {
1237 int cols = (curwin->w_width - extra);
1238 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1239
1240 // each "above" text prop shifts the text one row down
1241 curwin->w_wrow += rows;
1242 curwin->w_wcol -= rows * cols;
1243 endcol -= rows * cols;
1244 curwin->w_cline_height = rows + 1;
1245 }
1246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 /*
1248 * If Cursor is left of the screen, scroll rightwards.
1249 * If Cursor is right of the screen, scroll leftwards
1250 * If we get closer to the edge than 'sidescrolloff', scroll a little
1251 * extra
1252 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001253 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001254 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001255 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (off_left < 0 || off_right > 0)
1257 {
1258 if (off_left < 0)
1259 diff = -off_left;
1260 else
1261 diff = off_right;
1262
Bram Moolenaar85a20022019-12-21 18:25:54 +01001263 // When far off or not enough room on either side, put cursor in
1264 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001265 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1266 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 else
1268 {
1269 if (diff < p_ss)
1270 diff = p_ss;
1271 if (off_left < 0)
1272 new_leftcol = curwin->w_leftcol - diff;
1273 else
1274 new_leftcol = curwin->w_leftcol + diff;
1275 }
1276 if (new_leftcol < 0)
1277 new_leftcol = 0;
1278 if (new_leftcol != (int)curwin->w_leftcol)
1279 {
1280 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001281 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001282 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 curwin->w_wcol -= curwin->w_leftcol;
1286 }
1287 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1288 curwin->w_wcol -= curwin->w_leftcol;
1289 else
1290 curwin->w_wcol = 0;
1291
1292#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001293 // Skip over filler lines. At the top use w_topfill, there
1294 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (curwin->w_cursor.lnum == curwin->w_topline)
1296 curwin->w_wrow += curwin->w_topfill;
1297 else
1298 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1299#endif
1300
1301 prev_skipcol = curwin->w_skipcol;
1302
1303 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if ((curwin->w_wrow >= curwin->w_height
1306 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001307 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (p_lines =
1309#ifdef FEAT_DIFF
1310 plines_win_nofill
1311#else
1312 plines_win
1313#endif
1314 (curwin, curwin->w_cursor.lnum, FALSE))
1315 - 1 >= curwin->w_height))
1316 && curwin->w_height != 0
1317 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001318 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001319 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 // Cursor past end of screen. Happens with a single line that does
1322 // not fit on screen. Find a skipcol to show the text around the
1323 // cursor. Avoid scrolling all the time. compute value of "extra":
1324 // 1: Less than 'scrolloff' lines above
1325 // 2: Less than 'scrolloff' lines below
1326 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001328 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 // Compute last display line of the buffer line that we want at the
1331 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (p_lines == 0)
1333 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1334 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001335 if (p_lines > curwin->w_wrow + so)
1336 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 else
1338 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001339 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 extra += 2;
1341
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001342 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001344 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001345 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (n > curwin->w_height / 2)
1347 n -= curwin->w_height / 2;
1348 else
1349 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (n > p_lines - curwin->w_height + 1)
1352 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001353 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else if (extra == 1)
1356 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001357 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001358 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1359 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (extra > 0)
1361 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001362 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1363 extra = curwin->w_skipcol / width2;
1364 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366 }
1367 else if (extra == 2)
1368 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001369 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001372 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (endcol > curwin->w_skipcol)
1374 curwin->w_skipcol = endcol;
1375 }
1376
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001377 // adjust w_wrow for the changed w_skipcol
1378 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001380 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001381 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (curwin->w_wrow >= curwin->w_height)
1384 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001385 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001387 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 curwin->w_wrow -= extra;
1389 }
1390
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001391 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (extra > 0)
1393 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1394 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001395 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001397 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 curwin->w_skipcol = 0;
1399 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001400 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001402#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001403 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001404#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001405#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1406 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1407 {
1408 curwin->w_wrow += popup_top_extra(curwin);
1409 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001410 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001411 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001412 else
1413 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001414#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001415
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001416 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1417 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001418 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001419 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1422}
1423
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001424#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001425/*
1426 * Compute the screen position of text character at "pos" in window "wp"
1427 * The resulting values are one-based, zero when character is not visible.
1428 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001429 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430textpos2screenpos(
1431 win_T *wp,
1432 pos_T *pos,
1433 int *rowp, // screen row
1434 int *scolp, // start screen column
1435 int *ccolp, // cursor screen column
1436 int *ecolp) // end screen column
1437{
1438 colnr_T scol = 0, ccol = 0, ecol = 0;
1439 int row = 0;
1440 int rowoff = 0;
1441 colnr_T coloff = 0;
1442
Bram Moolenaar189663b2021-07-21 18:04:56 +02001443 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001444 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001445 colnr_T col;
1446 int width;
1447 linenr_T lnum = pos->lnum;
1448#ifdef FEAT_FOLDING
1449 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001451 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1452#endif
1453 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001454
1455#ifdef FEAT_DIFF
1456 // Add filler lines above this buffer line.
1457 row += diff_check_fill(wp, lnum);
1458#endif
1459
zeertzjqba2d1912022-12-18 12:28:59 +00001460 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001461#ifdef FEAT_FOLDING
1462 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001463 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001464 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001465 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001466 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001467 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468#endif
1469 {
1470 getvcol(wp, pos, &scol, &ccol, &ecol);
1471
1472 // similar to what is done in validate_cursor_col()
1473 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001474 col += off;
1475 width = wp->w_width - off + win_col_off2(wp);
1476
1477 // long line wrapping, adjust row
1478 if (wp->w_p_wrap
1479 && col >= (colnr_T)wp->w_width
1480 && width > 0)
1481 {
1482 // use same formula as what is used in curs_columns()
1483 rowoff = ((col - wp->w_width) / width + 1);
1484 col -= rowoff * width;
1485 }
1486 col -= wp->w_leftcol;
1487 if (col >= wp->w_width)
1488 col = -1;
1489 if (col >= 0 && row + rowoff <= wp->w_height)
1490 {
1491 coloff = col - scol + wp->w_wincol + 1;
1492 row += W_WINROW(wp);
1493 }
1494 else
1495 // character is left, right or below of the window
1496 row = rowoff = scol = ccol = ecol = 0;
1497 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001499 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001500 *scolp = scol + coloff;
1501 *ccolp = ccol + coloff;
1502 *ecolp = ecol + coloff;
1503}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001504#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001505
Bram Moolenaar12034e22019-08-25 22:25:02 +02001506#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001507/*
1508 * "screenpos({winid}, {lnum}, {col})" function
1509 */
1510 void
1511f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1512{
1513 dict_T *dict;
1514 win_T *wp;
1515 pos_T pos;
1516 int row = 0;
1517 int scol = 0, ccol = 0, ecol = 0;
1518
Bram Moolenaar93a10962022-06-16 11:42:09 +01001519 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001520 return;
1521 dict = rettv->vval.v_dict;
1522
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001523 if (in_vim9script()
1524 && (check_for_number_arg(argvars, 0) == FAIL
1525 || check_for_number_arg(argvars, 1) == FAIL
1526 || check_for_number_arg(argvars, 2) == FAIL))
1527 return;
1528
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001529 wp = find_win_by_nr_or_id(&argvars[0]);
1530 if (wp == NULL)
1531 return;
1532
1533 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001534 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1535 {
1536 semsg(_(e_invalid_line_number_nr), pos.lnum);
1537 return;
1538 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 pos.col = tv_get_number(&argvars[2]) - 1;
1540 pos.coladd = 0;
1541 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1542
1543 dict_add_number(dict, "row", row);
1544 dict_add_number(dict, "col", scol);
1545 dict_add_number(dict, "curscol", ccol);
1546 dict_add_number(dict, "endcol", ecol);
1547}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001548
1549/*
1550 * "virtcol2col({winid}, {lnum}, {col})" function
1551 */
1552 void
1553f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1554{
1555 win_T *wp;
1556 linenr_T lnum;
1557 int screencol;
1558 int error = FALSE;
1559
1560 rettv->vval.v_number = -1;
1561
1562 if (check_for_number_arg(argvars, 0) == FAIL
1563 || check_for_number_arg(argvars, 1) == FAIL
1564 || check_for_number_arg(argvars, 2) == FAIL)
1565 return;
1566
1567 wp = find_win_by_nr_or_id(&argvars[0]);
1568 if (wp == NULL)
1569 return;
1570
1571 lnum = tv_get_number_chk(&argvars[1], &error);
1572 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1573 return;
1574
1575 screencol = tv_get_number_chk(&argvars[2], &error);
1576 if (error || screencol < 0)
1577 return;
1578
1579 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1580}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001581#endif
1582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583/*
1584 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001587scrolldown(
1588 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001589 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001591 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 int wrow;
1593 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001594 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001595 int width1 = 0;
1596 int width2 = 0;
1597
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001598 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001599 {
1600 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001601 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604#ifdef FEAT_FOLDING
1605 linenr_T first;
1606
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1609#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001611 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001614 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1615 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 ++curwin->w_topfill;
1618 ++done;
1619 }
1620 else
1621#endif
1622 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001623 // break when at the very top
1624 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001625 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001627 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001629 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001630 if (curwin->w_skipcol >= width1 + width2)
1631 curwin->w_skipcol -= width2;
1632 else
1633 curwin->w_skipcol -= width1;
1634 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001638 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001639 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001640 --curwin->w_topline;
1641 curwin->w_skipcol = 0;
1642#ifdef FEAT_DIFF
1643 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001645#ifdef FEAT_FOLDING
1646 // A sequence of folded lines only counts for one logical line
1647 if (hasFolding(curwin->w_topline, &first, NULL))
1648 {
1649 ++done;
1650 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001651 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001652 curwin->w_botline -= curwin->w_topline - first;
1653 curwin->w_topline = first;
1654 }
1655 else
1656#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001657 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001658 {
1659 int size = win_linetabsize(curwin, curwin->w_topline,
1660 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1661 if (size > width1)
1662 {
1663 curwin->w_skipcol = width1;
1664 size -= width1;
1665 redraw_later(UPD_NOT_VALID);
1666 }
1667 while (size > width2)
1668 {
1669 curwin->w_skipcol += width2;
1670 size -= width2;
1671 }
1672 ++done;
1673 }
1674 else
1675 done += PLINES_NOFILL(curwin->w_topline);
1676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001678 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 invalidate_botline();
1680 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001681 curwin->w_wrow += done; // keep w_wrow updated
1682 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684#ifdef FEAT_DIFF
1685 if (curwin->w_cursor.lnum == curwin->w_topline)
1686 curwin->w_cline_row = 0;
1687 check_topfill(curwin, TRUE);
1688#endif
1689
1690 /*
1691 * Compute the row number of the last row of the cursor line
1692 * and move the cursor onto the displayed part of the window.
1693 */
1694 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001695 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 validate_virtcol();
1698 validate_cheight();
1699 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001700 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1703 {
1704#ifdef FEAT_FOLDING
1705 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1706 {
1707 --wrow;
1708 if (first == 1)
1709 curwin->w_cursor.lnum = 1;
1710 else
1711 curwin->w_cursor.lnum = first - 1;
1712 }
1713 else
1714#endif
1715 wrow -= plines(curwin->w_cursor.lnum--);
1716 curwin->w_valid &=
1717 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1718 moved = TRUE;
1719 }
1720 if (moved)
1721 {
1722#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001723 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 foldAdjustCursor();
1725#endif
1726 coladvance(curwin->w_curswant);
1727 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001728
1729 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1730 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001731 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001732 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1733
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001734 // make sure the cursor is in the visible text
1735 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001736 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001737 int row = 0;
1738 if (col >= width1)
1739 {
1740 col -= width1;
1741 ++row;
1742 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001743 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001744 {
1745 row += col / width2;
1746 col = col % width2;
1747 }
1748 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001749 {
1750 curwin->w_curswant = curwin->w_virtcol
1751 - (row - curwin->w_height + 1) * width2;
1752 coladvance(curwin->w_curswant);
1753 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
1758 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001761scrollup(
1762 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001765 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001767 if (do_sms
1768# ifdef FEAT_FOLDING
1769 || (byfold && hasAnyFolding(curwin))
1770# endif
1771# ifdef FEAT_DIFF
1772 || (curwin->w_p_diff && !curwin->w_p_wrap)
1773# endif
1774 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001776 int width1 = curwin->w_width - curwin_col_off();
1777 int width2 = width1 + curwin_col_off2();
1778 int size = 0;
1779 linenr_T prev_topline = curwin->w_topline;
1780
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001781 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001782 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001783
1784 // diff mode: first consume "topfill"
1785 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1786 // the line, then advance to the next line.
1787 // folding: count each sequence of folded lines as one logical line.
1788 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790# ifdef FEAT_DIFF
1791 if (curwin->w_topfill > 0)
1792 --curwin->w_topfill;
1793 else
1794# endif
1795 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001796 linenr_T lnum = curwin->w_topline;
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798# ifdef FEAT_FOLDING
1799 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 (void)hasFolding(lnum, NULL, &lnum);
1802# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001803 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001804 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001805 // 'smoothscroll': increase "w_skipcol" until it goes over
1806 // the end of the line, then advance to the next line.
1807 int add = curwin->w_skipcol > 0 ? width2 : width1;
1808 curwin->w_skipcol += add;
1809 if (curwin->w_skipcol >= size)
1810 {
1811 if (lnum == curbuf->b_ml.ml_line_count)
1812 {
1813 // at the last screen line, can't scroll further
1814 curwin->w_skipcol -= add;
1815 break;
1816 }
1817 ++lnum;
1818 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001819 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001820 else
1821 {
1822 if (lnum >= curbuf->b_ml.ml_line_count)
1823 break;
1824 ++lnum;
1825 }
1826
1827 if (lnum > curwin->w_topline)
1828 {
1829 // approximate w_botline
1830 curwin->w_botline += lnum - curwin->w_topline;
1831 curwin->w_topline = lnum;
1832# ifdef FEAT_DIFF
1833 curwin->w_topfill = diff_check_fill(curwin, lnum);
1834# endif
1835 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001836 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001837 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001838 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001839 }
1840 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 if (curwin->w_topline == prev_topline)
1843 // need to redraw even though w_topline didn't change
1844 redraw_later(UPD_NOT_VALID);
1845 }
1846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001849 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851
1852 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1853 curwin->w_topline = curbuf->b_ml.ml_line_count;
1854 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1855 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1856
1857#ifdef FEAT_DIFF
1858 check_topfill(curwin, FALSE);
1859#endif
1860
1861#ifdef FEAT_FOLDING
1862 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001863 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1865#endif
1866
1867 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1868 if (curwin->w_cursor.lnum < curwin->w_topline)
1869 {
1870 curwin->w_cursor.lnum = curwin->w_topline;
1871 curwin->w_valid &=
1872 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1873 coladvance(curwin->w_curswant);
1874 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001875 if (curwin->w_cursor.lnum == curwin->w_topline
1876 && do_sms && curwin->w_skipcol > 0)
1877 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001878 int col_off = curwin_col_off();
1879 int col_off2 = curwin_col_off2();
1880
1881 int width1 = curwin->w_width - col_off;
1882 int width2 = width1 + col_off2;
1883 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001884 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001885 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001886 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001887
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001888 // If we have non-zero scrolloff, just ignore the marker as we are
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001889 // going past it anyway.
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001890 int overlap = scrolloff_cols != 0 ? 0
1891 : sms_marker_overlap(curwin, extra2);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001892
Bram Moolenaar118c2352022-10-09 17:19:27 +01001893 // Make sure the cursor is in a visible part of the line, taking
1894 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001895 // If there are not enough screen lines put the cursor in the middle.
1896 if (scrolloff_cols > space_cols / 2)
1897 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001898 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001899 if (curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001900 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001901 colnr_T col = curwin->w_virtcol;
1902
1903 if (col < width1)
1904 col += width1;
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001905 while (col < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001906 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001907 curwin->w_curswant = col;
1908 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001909
1910 // validate_virtcol() marked various things as valid, but after
1911 // moving the cursor they need to be recomputed
1912 curwin->w_valid &=
1913 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001914 }
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916}
1917
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001918/*
1919 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1920 * valid for 'smoothscroll'.
1921 */
1922 void
1923adjust_skipcol(void)
1924{
1925 if (!curwin->w_p_wrap
1926 || !curwin->w_p_sms
1927 || curwin->w_cursor.lnum != curwin->w_topline)
1928 return;
1929
1930 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001931 if (width1 <= 0)
1932 return; // no text will be displayed
1933
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001934 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001935 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001936 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1937 int scrolled = FALSE;
1938
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001939 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001940 if (curwin->w_cline_height == curwin->w_height
1941 // w_cline_height may be capped at w_height, check there aren't
1942 // actually more lines.
1943 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1944 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001945 {
1946 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001947 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001948 return;
1949 }
1950
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001951 validate_virtcol();
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001952 int overlap = sms_marker_overlap(curwin,
1953 curwin_col_off() - curwin_col_off2());
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001954 while (curwin->w_skipcol > 0
Luuk van Baal24b62ec2023-05-13 14:12:15 +01001955 && curwin->w_virtcol < curwin->w_skipcol + overlap + scrolloff_cols)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001956 {
1957 // scroll a screen line down
1958 if (curwin->w_skipcol >= width1 + width2)
1959 curwin->w_skipcol -= width2;
1960 else
1961 curwin->w_skipcol -= width1;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001962 scrolled = TRUE;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001963 }
1964 if (scrolled)
Luuk van Baal798fa762023-05-15 18:17:43 +01001965 {
1966 validate_virtcol();
1967 redraw_later(UPD_NOT_VALID);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001968 return; // don't scroll in the other direction now
Luuk van Baal798fa762023-05-15 18:17:43 +01001969 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001970
1971 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1972 int row = 0;
1973 if (col >= width1)
1974 {
1975 col -= width1;
1976 ++row;
1977 }
1978 if (col > width2)
1979 {
1980 row += col / width2;
1981 col = col % width2;
1982 }
1983 if (row >= curwin->w_height)
1984 {
1985 if (curwin->w_skipcol == 0)
1986 {
1987 curwin->w_skipcol += width1;
1988 --row;
1989 }
1990 if (row >= curwin->w_height)
1991 curwin->w_skipcol += (row - curwin->w_height) * width2;
1992 redraw_later(UPD_NOT_VALID);
1993 }
1994}
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996#ifdef FEAT_DIFF
1997/*
1998 * Don't end up with too many filler lines in the window.
1999 */
2000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002001check_topfill(
2002 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002003 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 int n;
2006
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002007 if (wp->w_topfill <= 0)
2008 return;
2009
2010 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2011 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002013 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002015 --wp->w_topline;
2016 wp->w_topfill = 0;
2017 }
2018 else
2019 {
2020 wp->w_topfill = wp->w_height - n;
2021 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 }
2025}
2026
2027/*
2028 * Use as many filler lines as possible for w_topline. Make sure w_topline
2029 * is still visible.
2030 */
2031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002032max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 int n;
2035
2036 n = plines_nofill(curwin->w_topline);
2037 if (n >= curwin->w_height)
2038 curwin->w_topfill = 0;
2039 else
2040 {
2041 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2042 if (curwin->w_topfill + n > curwin->w_height)
2043 curwin->w_topfill = curwin->w_height - n;
2044 }
2045}
2046#endif
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048/*
2049 * Scroll the screen one line down, but don't do it if it would move the
2050 * cursor off the screen.
2051 */
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 int end_row;
2056#ifdef FEAT_DIFF
2057 int can_fill = (curwin->w_topfill
2058 < diff_check_fill(curwin, curwin->w_topline));
2059#endif
2060
2061 if (curwin->w_topline <= 1
2062#ifdef FEAT_DIFF
2063 && !can_fill
2064#endif
2065 )
2066 return;
2067
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 /*
2071 * Compute the row number of the last row of the cursor line
2072 * and make sure it doesn't go off the screen. Make sure the cursor
2073 * doesn't go past 'scrolloff' lines from the screen end.
2074 */
2075 end_row = curwin->w_wrow;
2076#ifdef FEAT_DIFF
2077 if (can_fill)
2078 ++end_row;
2079 else
2080 end_row += plines_nofill(curwin->w_topline - 1);
2081#else
2082 end_row += plines(curwin->w_topline - 1);
2083#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002084 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 validate_cheight();
2087 validate_virtcol();
2088 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002089 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002091 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093#ifdef FEAT_DIFF
2094 if (can_fill)
2095 {
2096 ++curwin->w_topfill;
2097 check_topfill(curwin, TRUE);
2098 }
2099 else
2100 {
2101 --curwin->w_topline;
2102 curwin->w_topfill = 0;
2103 }
2104#else
2105 --curwin->w_topline;
2106#endif
2107#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002108 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002110 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2112 }
2113}
2114
2115/*
2116 * Scroll the screen one line up, but don't do it if it would move the cursor
2117 * off the screen.
2118 */
2119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002120scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 int start_row;
2123
2124 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2125#ifdef FEAT_DIFF
2126 && curwin->w_topfill == 0
2127#endif
2128 )
2129 return;
2130
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 /*
2134 * Compute the row number of the first row of the cursor line
2135 * and make sure it doesn't go off the screen. Make sure the cursor
2136 * doesn't go before 'scrolloff' lines from the screen start.
2137 */
2138#ifdef FEAT_DIFF
2139 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2140 - curwin->w_topfill;
2141#else
2142 start_row = curwin->w_wrow - plines(curwin->w_topline);
2143#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002144 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002147 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002149 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151#ifdef FEAT_DIFF
2152 if (curwin->w_topfill > 0)
2153 --curwin->w_topfill;
2154 else
2155#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002156 {
2157#ifdef FEAT_FOLDING
2158 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002161 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002162 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2164 }
2165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
2167/*
2168 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2169 * a (wrapped) text line. Uses and sets "lp->fill".
2170 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002171 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 */
2173 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002174topline_back_winheight(
2175 lineoff_T *lp,
2176 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178#ifdef FEAT_DIFF
2179 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002181 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 ++lp->fill;
2183 lp->height = 1;
2184 }
2185 else
2186#endif
2187 {
2188 --lp->lnum;
2189#ifdef FEAT_DIFF
2190 lp->fill = 0;
2191#endif
2192 if (lp->lnum < 1)
2193 lp->height = MAXCOL;
2194 else
2195#ifdef FEAT_FOLDING
2196 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 lp->height = 1;
2199 else
2200#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002201 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203}
2204
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002205 static void
2206topline_back(lineoff_T *lp)
2207{
2208 topline_back_winheight(lp, TRUE);
2209}
2210
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212/*
2213 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2214 * a (wrapped) text line. Uses and sets "lp->fill".
2215 * Returns the height of the added line in "lp->height".
2216 * Lines below the last one are incredibly high.
2217 */
2218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002219botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221#ifdef FEAT_DIFF
2222 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2223 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ++lp->fill;
2226 lp->height = 1;
2227 }
2228 else
2229#endif
2230 {
2231 ++lp->lnum;
2232#ifdef FEAT_DIFF
2233 lp->fill = 0;
2234#endif
2235 if (lp->lnum > curbuf->b_ml.ml_line_count)
2236 lp->height = MAXCOL;
2237 else
2238#ifdef FEAT_FOLDING
2239 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002240 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 lp->height = 1;
2242 else
2243#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002244 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246}
2247
2248#ifdef FEAT_DIFF
2249/*
2250 * Switch from including filler lines below lp->lnum to including filler
2251 * lines above loff.lnum + 1. This keeps pointing to the same line.
2252 * When there are no filler lines nothing changes.
2253 */
2254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002255botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 if (lp->fill > 0)
2258 {
2259 ++lp->lnum;
2260 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2261 }
2262}
2263
2264/*
2265 * Switch from including filler lines above lp->lnum to including filler
2266 * lines below loff.lnum - 1. This keeps pointing to the same line.
2267 * When there are no filler lines nothing changes.
2268 */
2269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 if (lp->fill > 0)
2273 {
2274 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2275 --lp->lnum;
2276 }
2277}
2278#endif
2279
2280/*
2281 * Recompute topline to put the cursor at the top of the window.
2282 * Scroll at least "min_scroll" lines.
2283 * If "always" is TRUE, always set topline (for "zt").
2284 */
2285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 int scrolled = 0;
2289 int extra = 0;
2290 int used;
2291 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002292 linenr_T top; // just above displayed lines
2293 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002295 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#ifdef FEAT_DIFF
2297 linenr_T old_topfill = curwin->w_topfill;
2298#endif
2299 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002300 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (mouse_dragging > 0)
2303 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 /*
2306 * Decrease topline until:
2307 * - it has become 1
2308 * - (part of) the cursor line is moved off the screen or
2309 * - moved at least 'scrolljump' lines and
2310 * - at least 'scrolloff' lines above and below the cursor
2311 */
2312 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002313 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (curwin->w_cursor.lnum < curwin->w_topline)
2315 scrolled = used;
2316
2317#ifdef FEAT_FOLDING
2318 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2319 {
2320 --top;
2321 ++bot;
2322 }
2323 else
2324#endif
2325 {
2326 top = curwin->w_cursor.lnum - 1;
2327 bot = curwin->w_cursor.lnum + 1;
2328 }
2329 new_topline = top + 1;
2330
2331#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // "used" already contains the number of filler lines above, don't add it
2333 // again.
2334 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002335 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#endif
2337
2338 /*
2339 * Check if the lines from "top" to "bot" fit in the window. If they do,
2340 * set new_topline and advance "top" and "bot" to include more lines.
2341 */
2342 while (top > 0)
2343 {
2344#ifdef FEAT_FOLDING
2345 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002346 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 i = 1;
2348 else
2349#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002350 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002351 if (top < curwin->w_topline)
2352 scrolled += i;
2353
2354 // If scrolling is needed, scroll at least 'sj' lines.
2355 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2356 && extra >= off)
2357 break;
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 used += i;
2360 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2361 {
2362#ifdef FEAT_FOLDING
2363 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002364 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 ++used;
2366 else
2367#endif
2368 used += plines(bot);
2369 }
2370 if (used > curwin->w_height)
2371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 extra += i;
2374 new_topline = top;
2375 --top;
2376 ++bot;
2377 }
2378
2379 /*
2380 * If we don't have enough space, put cursor in the middle.
2381 * This makes sure we get the same position when using "k" and "j"
2382 * in a small window.
2383 */
2384 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002385 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 else
2387 {
2388 /*
2389 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002390 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
2392 if (new_topline < curwin->w_topline || always)
2393 curwin->w_topline = new_topline;
2394 if (curwin->w_topline > curwin->w_cursor.lnum)
2395 curwin->w_topline = curwin->w_cursor.lnum;
2396#ifdef FEAT_DIFF
2397 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2398 if (curwin->w_topfill > 0 && extra > off)
2399 {
2400 curwin->w_topfill -= extra - off;
2401 if (curwin->w_topfill < 0)
2402 curwin->w_topfill = 0;
2403 }
2404 check_topfill(curwin, FALSE);
2405#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002406 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002407 if (curwin->w_topline == curwin->w_cursor.lnum
2408 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002409 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002411 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_DIFF
2413 || curwin->w_topfill != old_topfill
2414#endif
2415 )
2416 curwin->w_valid &=
2417 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2418 curwin->w_valid |= VALID_TOPLINE;
2419 }
2420}
2421
2422/*
2423 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2424 * screen lines for text lines.
2425 */
2426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002427set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429#ifdef FEAT_DIFF
2430 wp->w_filler_rows = 0;
2431#endif
2432 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002433 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 else
2435 {
2436 wp->w_empty_rows = wp->w_height - used;
2437#ifdef FEAT_DIFF
2438 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2439 {
2440 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2441 if (wp->w_empty_rows > wp->w_filler_rows)
2442 wp->w_empty_rows -= wp->w_filler_rows;
2443 else
2444 {
2445 wp->w_filler_rows = wp->w_empty_rows;
2446 wp->w_empty_rows = 0;
2447 }
2448 }
2449#endif
2450 }
2451}
2452
2453/*
2454 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002455 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2457 * This is messy stuff!!!
2458 */
2459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 int used;
2463 int scrolled = 0;
2464 int extra = 0;
2465 int i;
2466 linenr_T line_count;
2467 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002468 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 lineoff_T loff;
2470 lineoff_T boff;
2471#ifdef FEAT_DIFF
2472 int old_topfill = curwin->w_topfill;
2473 int fill_below_window;
2474#endif
2475 linenr_T old_botline = curwin->w_botline;
2476 linenr_T old_valid = curwin->w_valid;
2477 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002478 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002479 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002480 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
2482 cln = curwin->w_cursor.lnum;
2483 if (set_topbot)
2484 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002485 int set_skipcol = FALSE;
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 used = 0;
2488 curwin->w_botline = cln + 1;
2489#ifdef FEAT_DIFF
2490 loff.fill = 0;
2491#endif
2492 for (curwin->w_topline = curwin->w_botline;
2493 curwin->w_topline > 1;
2494 curwin->w_topline = loff.lnum)
2495 {
2496 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002497 topline_back_winheight(&loff, FALSE);
2498 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002500 if (used + loff.height > curwin->w_height)
2501 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002502 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002503 {
2504 // 'smoothscroll' and 'wrap' are set. The above line is
2505 // too long to show in its entirety, so we show just a part
2506 // of it.
2507 if (used < curwin->w_height)
2508 {
2509 int plines_offset = used + loff.height
2510 - curwin->w_height;
2511 used = curwin->w_height;
2512#ifdef FEAT_DIFF
2513 curwin->w_topfill = loff.fill;
2514#endif
2515 curwin->w_topline = loff.lnum;
2516 curwin->w_skipcol = skipcol_from_plines(
2517 curwin, plines_offset);
2518 set_skipcol = TRUE;
2519 }
2520 }
2521 break;
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 used += loff.height;
2524#ifdef FEAT_DIFF
2525 curwin->w_topfill = loff.fill;
2526#endif
2527 }
2528 set_empty_rows(curwin, used);
2529 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2530 if (curwin->w_topline != old_topline
2531#ifdef FEAT_DIFF
2532 || curwin->w_topfill != old_topfill
2533#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002534 || set_skipcol
2535 || curwin->w_skipcol != 0)
2536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002538 if (set_skipcol)
2539 redraw_later(UPD_NOT_VALID);
2540 else
2541 reset_skipcol();
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 else
2545 validate_botline();
2546
Bram Moolenaar85a20022019-12-21 18:25:54 +01002547 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_DIFF
2549 used = plines_nofill(cln);
2550#else
2551 validate_cheight();
2552 used = curwin->w_cline_height;
2553#endif
2554
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002555 // If the cursor is on or below botline, we will at least scroll by the
2556 // height of the cursor line, which is "used". Correct for empty lines,
2557 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (cln >= curwin->w_botline)
2559 {
2560 scrolled = used;
2561 if (cln == curwin->w_botline)
2562 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002563 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002564 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002565 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002566 // Calculate how many screen lines the current top line of window
2567 // occupies. If it is occupying more than the entire window, we
2568 // need to scroll the additional clipped lines to scroll past the
2569 // top line before we can move on to the other lines.
2570 int top_plines =
2571#ifdef FEAT_DIFF
2572 plines_win_nofill
2573#else
2574 plines_win
2575#endif
2576 (curwin, curwin->w_topline, FALSE);
2577 int skip_lines = 0;
2578 int width1 = curwin->w_width - curwin_col_off();
2579 int width2 = width1 + curwin_col_off2();
2580 // similar formula is used in curs_columns()
2581 if (curwin->w_skipcol > width1)
2582 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2583 else if (curwin->w_skipcol > 0)
2584 skip_lines = 1;
2585
2586 top_plines -= skip_lines;
2587 if (top_plines > curwin->w_height)
2588 {
2589 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002590 }
2591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593
2594 /*
2595 * Stop counting lines to scroll when
2596 * - hitting start of the file
2597 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002598 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 * - lines between botline and cursor have been counted
2600 */
2601#ifdef FEAT_FOLDING
2602 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2603#endif
2604 {
2605 loff.lnum = cln;
2606 boff.lnum = cln;
2607 }
2608#ifdef FEAT_DIFF
2609 loff.fill = 0;
2610 boff.fill = 0;
2611 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2612 - curwin->w_filler_rows;
2613#endif
2614
2615 while (loff.lnum > 1)
2616 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002617 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2618 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002620 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2622 && loff.lnum <= curwin->w_botline
2623#ifdef FEAT_DIFF
2624 && (loff.lnum < curwin->w_botline
2625 || loff.fill >= fill_below_window)
2626#endif
2627 )
2628 break;
2629
Bram Moolenaar85a20022019-12-21 18:25:54 +01002630 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002632 if (loff.height == MAXCOL)
2633 used = MAXCOL;
2634 else
2635 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (used > curwin->w_height)
2637 break;
2638 if (loff.lnum >= curwin->w_botline
2639#ifdef FEAT_DIFF
2640 && (loff.lnum > curwin->w_botline
2641 || loff.fill <= fill_below_window)
2642#endif
2643 )
2644 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 scrolled += loff.height;
2647 if (loff.lnum == curwin->w_botline
2648#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002649 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#endif
2651 )
2652 scrolled -= curwin->w_empty_rows;
2653 }
2654
2655 if (boff.lnum < curbuf->b_ml.ml_line_count)
2656 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 botline_forw(&boff);
2659 used += boff.height;
2660 if (used > curwin->w_height)
2661 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002662 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2663 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 extra += boff.height;
2666 if (boff.lnum >= curwin->w_botline
2667#ifdef FEAT_DIFF
2668 || (boff.lnum + 1 == curwin->w_botline
2669 && boff.fill > curwin->w_filler_rows)
2670#endif
2671 )
2672 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 scrolled += boff.height;
2675 if (boff.lnum == curwin->w_botline
2676#ifdef FEAT_DIFF
2677 && boff.fill == 0
2678#endif
2679 )
2680 scrolled -= curwin->w_empty_rows;
2681 }
2682 }
2683 }
2684 }
2685
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (scrolled <= 0)
2688 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 else if (used > curwin->w_height)
2691 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 else
2694 {
2695 line_count = 0;
2696#ifdef FEAT_DIFF
2697 boff.fill = curwin->w_topfill;
2698#endif
2699 boff.lnum = curwin->w_topline - 1;
2700 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2701 {
2702 botline_forw(&boff);
2703 i += boff.height;
2704 ++line_count;
2705 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002706 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 line_count = 9999;
2708 }
2709
2710 /*
2711 * Scroll up if the cursor is off the bottom of the screen a bit.
2712 * Otherwise put it at 1/2 of the screen.
2713 */
2714 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002715 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002716 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002717 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002718 if (do_sms)
2719 scrollup(scrolled, TRUE); // TODO
2720 else
2721 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 /*
2725 * If topline didn't change we need to restore w_botline and w_empty_rows
2726 * (we changed them).
2727 * If topline did change, update_screen() will set botline.
2728 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002729 if (curwin->w_topline == old_topline
2730 && curwin->w_skipcol == old_skipcol
2731 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 curwin->w_botline = old_botline;
2734 curwin->w_empty_rows = old_empty_rows;
2735 curwin->w_valid = old_valid;
2736 }
2737 curwin->w_valid |= VALID_TOPLINE;
2738}
2739
2740/*
2741 * Recompute topline to put the cursor halfway the window
2742 * If "atend" is TRUE, also put it halfway at the end of the file.
2743 */
2744 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002745scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 int above = 0;
2748 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002749 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#ifdef FEAT_DIFF
2751 int topfill = 0;
2752#endif
2753 int below = 0;
2754 int used;
2755 lineoff_T loff;
2756 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002757#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002758 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002761#ifdef FEAT_PROP_POPUP
2762 // if the width changed this needs to be updated first
2763 may_update_popup_position();
2764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2766#ifdef FEAT_FOLDING
2767 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2768#endif
2769#ifdef FEAT_DIFF
2770 used = plines_nofill(loff.lnum);
2771 loff.fill = 0;
2772 boff.fill = 0;
2773#else
2774 used = plines(loff.lnum);
2775#endif
2776 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002777
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002778 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002779 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2780 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002781 {
2782 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002783 if (atend)
2784 {
2785 want_height = (curwin->w_height - used) / 2;
2786 used = 0;
2787 }
2788 else
2789 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002790 }
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 while (topline > 1)
2793 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002794 // If using smoothscroll, we can precisely scroll to the
2795 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002796 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002797 {
2798 topline_back_winheight(&loff, FALSE);
2799 if (loff.height == MAXCOL)
2800 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002801 used += loff.height;
2802 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002803 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002804 botline_forw(&boff);
2805 used += boff.height;
2806 }
2807 if (used > want_height)
2808 {
2809 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002810 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002811 topline = loff.lnum;
2812#ifdef FEAT_DIFF
2813 topfill = loff.fill;
2814#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002815 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002816 }
2817 break;
2818 }
2819 topline = loff.lnum;
2820#ifdef FEAT_DIFF
2821 topfill = loff.fill;
2822#endif
2823 continue;
2824 }
2825
2826 // If not using smoothscroll, we have to iteratively find how many
2827 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002828 // This may not be right in the middle if the lines'
2829 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002830
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002831 // Depending on "prefer_above" we add a line above or below first.
2832 // Loop twice to avoid duplicating code.
2833 int done = FALSE;
2834 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002836 if (prefer_above ? (round == 2 && below < above)
2837 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002839 // add a line below the cursor
2840 if (boff.lnum < curbuf->b_ml.ml_line_count)
2841 {
2842 botline_forw(&boff);
2843 used += boff.height;
2844 if (used > curwin->w_height)
2845 {
2846 done = TRUE;
2847 break;
2848 }
2849 below += boff.height;
2850 }
2851 else
2852 {
2853 ++below; // count a "~" line
2854 if (atend)
2855 ++used;
2856 }
2857 }
2858
2859 if (prefer_above ? (round == 1 && below >= above)
2860 : (round == 1 && below > above))
2861 {
2862 // add a line above the cursor
2863 topline_back(&loff);
2864 if (loff.height == MAXCOL)
2865 used = MAXCOL;
2866 else
2867 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002869 {
2870 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002872 }
2873 above += loff.height;
2874 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002876 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002880 if (done)
2881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#ifdef FEAT_FOLDING
2885 if (!hasFolding(topline, &curwin->w_topline, NULL))
2886#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002887 {
2888 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002889 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002890 || curwin->w_skipcol != 0)
2891 {
2892 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002893 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002894 {
2895 curwin->w_skipcol = skipcol;
2896 redraw_later(UPD_NOT_VALID);
2897 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002898 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002899 reset_skipcol();
2900 }
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902#ifdef FEAT_DIFF
2903 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002904 if (old_topline > curwin->w_topline + curwin->w_height)
2905 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 check_topfill(curwin, FALSE);
2907#endif
2908 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2909 curwin->w_valid |= VALID_TOPLINE;
2910}
2911
2912/*
2913 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002914 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 * If not possible, put it at the same position as scroll_cursor_halfway().
2916 * When called topline must be valid!
2917 */
2918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002919cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002923 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 linenr_T botline;
2925 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002928 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 /*
2931 * How many lines we would like to have above/below the cursor depends on
2932 * whether the first/last line of the file is on screen.
2933 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002934 above_wanted = so;
2935 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002936 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
2938 above_wanted = mouse_dragging - 1;
2939 below_wanted = mouse_dragging - 1;
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (curwin->w_topline == 1)
2942 {
2943 above_wanted = 0;
2944 max_off = curwin->w_height / 2;
2945 if (below_wanted > max_off)
2946 below_wanted = max_off;
2947 }
2948 validate_botline();
2949 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002950 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
2952 below_wanted = 0;
2953 max_off = (curwin->w_height - 1) / 2;
2954 if (above_wanted > max_off)
2955 above_wanted = max_off;
2956 }
2957
2958 /*
2959 * If there are sufficient file-lines above and below the cursor, we can
2960 * return now.
2961 */
2962 cln = curwin->w_cursor.lnum;
2963 if (cln >= curwin->w_topline + above_wanted
2964 && cln < curwin->w_botline - below_wanted
2965#ifdef FEAT_FOLDING
2966 && !hasAnyFolding(curwin)
2967#endif
2968 )
2969 return;
2970
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002971 if (curwin->w_p_sms && !curwin->w_p_wrap)
2972 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002973 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002974 if (curwin->w_cline_height == curwin->w_height)
2975 {
2976 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002977 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002978 return;
2979 }
2980 // TODO: If the cursor line doesn't fit in the window then only adjust
2981 // w_skipcol.
2982 }
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 /*
2985 * Narrow down the area where the cursor can be put by taking lines from
2986 * the top and the bottom until:
2987 * - the desired context lines are found
2988 * - the lines from the top is past the lines from the bottom
2989 */
2990 topline = curwin->w_topline;
2991 botline = curwin->w_botline - 1;
2992#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002993 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 above = curwin->w_topfill;
2995 below = curwin->w_filler_rows;
2996#endif
2997 while ((above < above_wanted || below < below_wanted) && topline < botline)
2998 {
2999 if (below < below_wanted && (below <= above || above >= above_wanted))
3000 {
3001#ifdef FEAT_FOLDING
3002 if (hasFolding(botline, &botline, NULL))
3003 ++below;
3004 else
3005#endif
3006 below += plines(botline);
3007 --botline;
3008 }
3009 if (above < above_wanted && (above < below || below >= below_wanted))
3010 {
3011#ifdef FEAT_FOLDING
3012 if (hasFolding(topline, NULL, &topline))
3013 ++above;
3014 else
3015#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003016 above += PLINES_NOFILL(topline);
3017#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003018 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (topline < botline)
3020 above += diff_check_fill(curwin, topline + 1);
3021#endif
3022 ++topline;
3023 }
3024 }
3025 if (topline == botline || botline == 0)
3026 curwin->w_cursor.lnum = topline;
3027 else if (topline > botline)
3028 curwin->w_cursor.lnum = botline;
3029 else
3030 {
3031 if (cln < topline && curwin->w_topline > 1)
3032 {
3033 curwin->w_cursor.lnum = topline;
3034 curwin->w_valid &=
3035 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3036 }
3037 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3038 {
3039 curwin->w_cursor.lnum = botline;
3040 curwin->w_valid &=
3041 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3042 }
3043 }
3044 curwin->w_valid |= VALID_TOPLINE;
3045}
3046
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003047static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003050 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3051 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003053 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
3055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003056onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 long n;
3059 int retval = OK;
3060 lineoff_T loff;
3061 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003062 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar85a20022019-12-21 18:25:54 +01003064 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
3066 beep_flush();
3067 return FAIL;
3068 }
3069
3070 for ( ; count > 0; --count)
3071 {
3072 validate_botline();
3073 /*
3074 * It's an error to move a page up when the first line is already on
3075 * the screen. It's an error to move a page down when the last line
3076 * is on the screen and the topline is 'scrolloff' lines from the
3077 * last line.
3078 */
3079 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003080 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3082 : (curwin->w_topline == 1
3083#ifdef FEAT_DIFF
3084 && curwin->w_topfill ==
3085 diff_check_fill(curwin, curwin->w_topline)
3086#endif
3087 ))
3088 {
3089 beep_flush();
3090 retval = FAIL;
3091 break;
3092 }
3093
3094#ifdef FEAT_DIFF
3095 loff.fill = 0;
3096#endif
3097 if (dir == FORWARD)
3098 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003099 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003101 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003102 if (p_window <= 2)
3103 ++curwin->w_topline;
3104 else
3105 curwin->w_topline += p_window - 2;
3106 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3107 curwin->w_topline = curbuf->b_ml.ml_line_count;
3108 curwin->w_cursor.lnum = curwin->w_topline;
3109 }
3110 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3111 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 curwin->w_topline = curbuf->b_ml.ml_line_count;
3114#ifdef FEAT_DIFF
3115 curwin->w_topfill = 0;
3116#endif
3117 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3118 }
3119 else
3120 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003121 // For the overlap, start with the line just below the window
3122 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 loff.lnum = curwin->w_botline;
3124#ifdef FEAT_DIFF
3125 loff.fill = diff_check_fill(curwin, loff.lnum)
3126 - curwin->w_filler_rows;
3127#endif
3128 get_scroll_overlap(&loff, -1);
3129 curwin->w_topline = loff.lnum;
3130#ifdef FEAT_DIFF
3131 curwin->w_topfill = loff.fill;
3132 check_topfill(curwin, FALSE);
3133#endif
3134 curwin->w_cursor.lnum = curwin->w_topline;
3135 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3136 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3137 }
3138 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003139 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141#ifdef FEAT_DIFF
3142 if (curwin->w_topline == 1)
3143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 max_topfill();
3146 continue;
3147 }
3148#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003149 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003150 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003151 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003152 if (p_window <= 2)
3153 --curwin->w_topline;
3154 else
3155 curwin->w_topline -= p_window - 2;
3156 if (curwin->w_topline < 1)
3157 curwin->w_topline = 1;
3158 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3159 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3160 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3161 continue;
3162 }
3163
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // Find the line at the top of the window that is going to be the
3165 // line at the bottom of the window. Make sure this results in
3166 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 loff.lnum = curwin->w_topline - 1;
3168#ifdef FEAT_DIFF
3169 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3170 - curwin->w_topfill;
3171#endif
3172 get_scroll_overlap(&loff, 1);
3173
3174 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3175 {
3176 loff.lnum = curbuf->b_ml.ml_line_count;
3177#ifdef FEAT_DIFF
3178 loff.fill = 0;
3179 }
3180 else
3181 {
3182 botline_topline(&loff);
3183#endif
3184 }
3185 curwin->w_cursor.lnum = loff.lnum;
3186
Bram Moolenaar85a20022019-12-21 18:25:54 +01003187 // Find the line just above the new topline to get the right line
3188 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 n = 0;
3190 while (n <= curwin->w_height && loff.lnum >= 1)
3191 {
3192 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003193 if (loff.height == MAXCOL)
3194 n = MAXCOL;
3195 else
3196 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003198 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 curwin->w_topline = 1;
3201#ifdef FEAT_DIFF
3202 max_topfill();
3203#endif
3204 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3205 }
3206 else
3207 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003208 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#ifdef FEAT_DIFF
3210 topline_botline(&loff);
3211#endif
3212 botline_forw(&loff);
3213 botline_forw(&loff);
3214#ifdef FEAT_DIFF
3215 botline_topline(&loff);
3216#endif
3217#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3220#endif
3221
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 // Always scroll at least one line. Avoid getting stuck on
3223 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (loff.lnum >= curwin->w_topline
3225#ifdef FEAT_DIFF
3226 && (loff.lnum > curwin->w_topline
3227 || loff.fill >= curwin->w_topfill)
3228#endif
3229 )
3230 {
3231#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003232 // First try using the maximum number of filler lines. If
3233 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 loff.fill = curwin->w_topfill;
3235 if (curwin->w_topfill < diff_check_fill(curwin,
3236 curwin->w_topline))
3237 max_topfill();
3238 if (curwin->w_topfill == loff.fill)
3239#endif
3240 {
3241 --curwin->w_topline;
3242#ifdef FEAT_DIFF
3243 curwin->w_topfill = 0;
3244#endif
3245 }
3246 comp_botline(curwin);
3247 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003248 curwin->w_valid &=
3249 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 else
3252 {
3253 curwin->w_topline = loff.lnum;
3254#ifdef FEAT_DIFF
3255 curwin->w_topfill = loff.fill;
3256 check_topfill(curwin, FALSE);
3257#endif
3258 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3259 }
3260 }
3261 }
3262 }
3263#ifdef FEAT_FOLDING
3264 foldAdjustCursor();
3265#endif
3266 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003267 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003268 if (retval == OK)
3269 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3271
Bram Moolenaar907dad72018-07-10 15:07:15 +02003272 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003274 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3275 // But make sure we scroll at least one line (happens with mix of long
3276 // wrapping lines and non-wrapping line).
3277 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003279 scroll_cursor_top(1, FALSE);
3280 if (curwin->w_topline <= old_topline
3281 && old_topline < curbuf->b_ml.ml_line_count)
3282 {
3283 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003285 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3286#endif
3287 }
3288 }
3289#ifdef FEAT_FOLDING
3290 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003295 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 return retval;
3297}
3298
3299/*
3300 * Decide how much overlap to use for page-up or page-down scrolling.
3301 * This is symmetric, so that doing both keeps the same lines displayed.
3302 * Three lines are examined:
3303 *
3304 * before CTRL-F after CTRL-F / before CTRL-B
3305 * etc. l1
3306 * l1 last but one line ------------
3307 * l2 last text line l2 top text line
3308 * ------------- l3 second text line
3309 * l3 etc.
3310 */
3311 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003312get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 int h1, h2, h3, h4;
3315 int min_height = curwin->w_height - 2;
3316 lineoff_T loff0, loff1, loff2;
3317
3318#ifdef FEAT_DIFF
3319 if (lp->fill > 0)
3320 lp->height = 1;
3321 else
3322 lp->height = plines_nofill(lp->lnum);
3323#else
3324 lp->height = plines(lp->lnum);
3325#endif
3326 h1 = lp->height;
3327 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003328 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330 loff0 = *lp;
3331 if (dir > 0)
3332 botline_forw(lp);
3333 else
3334 topline_back(lp);
3335 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003336 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003338 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return;
3340 }
3341
3342 loff1 = *lp;
3343 if (dir > 0)
3344 botline_forw(lp);
3345 else
3346 topline_back(lp);
3347 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003348 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003350 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 return;
3352 }
3353
3354 loff2 = *lp;
3355 if (dir > 0)
3356 botline_forw(lp);
3357 else
3358 topline_back(lp);
3359 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003360 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003361 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Scroll 'scroll' lines up or down.
3368 */
3369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003370halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 long scrolled = 0;
3373 int i;
3374 int n;
3375 int room;
3376
3377 if (Prenum)
3378 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3379 curwin->w_height : Prenum;
3380 n = (curwin->w_p_scr <= curwin->w_height) ?
3381 curwin->w_p_scr : curwin->w_height;
3382
Bram Moolenaard5d37532017-03-27 23:02:07 +02003383 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 validate_botline();
3385 room = curwin->w_empty_rows;
3386#ifdef FEAT_DIFF
3387 room += curwin->w_filler_rows;
3388#endif
3389 if (flag)
3390 {
3391 /*
3392 * scroll the text up
3393 */
3394 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3395 {
3396#ifdef FEAT_DIFF
3397 if (curwin->w_topfill > 0)
3398 {
3399 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003400 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 --curwin->w_topfill;
3402 }
3403 else
3404#endif
3405 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003406 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 n -= i;
3408 if (n < 0 && scrolled > 0)
3409 break;
3410#ifdef FEAT_FOLDING
3411 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3412#endif
3413 ++curwin->w_topline;
3414#ifdef FEAT_DIFF
3415 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3416#endif
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3419 {
3420 ++curwin->w_cursor.lnum;
3421 curwin->w_valid &=
3422 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3426 scrolled += i;
3427
3428 /*
3429 * Correct w_botline for changed w_topline.
3430 * Won't work when there are filler lines.
3431 */
3432#ifdef FEAT_DIFF
3433 if (curwin->w_p_diff)
3434 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3435 else
3436#endif
3437 {
3438 room += i;
3439 do
3440 {
3441 i = plines(curwin->w_botline);
3442 if (i > room)
3443 break;
3444#ifdef FEAT_FOLDING
3445 (void)hasFolding(curwin->w_botline, NULL,
3446 &curwin->w_botline);
3447#endif
3448 ++curwin->w_botline;
3449 room -= i;
3450 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3451 }
3452 }
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 /*
3455 * When hit bottom of the file: move cursor down.
3456 */
3457 if (n > 0)
3458 {
3459# ifdef FEAT_FOLDING
3460 if (hasAnyFolding(curwin))
3461 {
3462 while (--n >= 0
3463 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3464 {
3465 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3466 &curwin->w_cursor.lnum);
3467 ++curwin->w_cursor.lnum;
3468 }
3469 }
3470 else
3471# endif
3472 curwin->w_cursor.lnum += n;
3473 check_cursor_lnum();
3474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476 else
3477 {
3478 /*
3479 * scroll the text down
3480 */
3481 while (n > 0 && curwin->w_topline > 1)
3482 {
3483#ifdef FEAT_DIFF
3484 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3485 {
3486 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003487 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 ++curwin->w_topfill;
3489 }
3490 else
3491#endif
3492 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003493 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 n -= i;
3495 if (n < 0 && scrolled > 0)
3496 break;
3497 --curwin->w_topline;
3498#ifdef FEAT_FOLDING
3499 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3500#endif
3501#ifdef FEAT_DIFF
3502 curwin->w_topfill = 0;
3503#endif
3504 }
3505 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3506 VALID_BOTLINE|VALID_BOTLINE_AP);
3507 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (curwin->w_cursor.lnum > 1)
3509 {
3510 --curwin->w_cursor.lnum;
3511 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 /*
3516 * When hit top of the file: move cursor up.
3517 */
3518 if (n > 0)
3519 {
3520 if (curwin->w_cursor.lnum <= (linenr_T)n)
3521 curwin->w_cursor.lnum = 1;
3522 else
3523# ifdef FEAT_FOLDING
3524 if (hasAnyFolding(curwin))
3525 {
3526 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3527 {
3528 --curwin->w_cursor.lnum;
3529 (void)hasFolding(curwin->w_cursor.lnum,
3530 &curwin->w_cursor.lnum, NULL);
3531 }
3532 }
3533 else
3534# endif
3535 curwin->w_cursor.lnum -= n;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003539 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 foldAdjustCursor();
3541# endif
3542#ifdef FEAT_DIFF
3543 check_topfill(curwin, !flag);
3544#endif
3545 cursor_correct();
3546 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003547 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003549
Bram Moolenaar860cae12010-06-05 23:22:07 +02003550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003551do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552{
3553 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003554 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003555 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003556 colnr_T curswant = curwin->w_curswant;
3557 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558 win_T *old_curwin = curwin;
3559 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560 int old_VIsual_select = VIsual_select;
3561 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003562
3563 /*
3564 * loop through the cursorbound windows
3565 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003566 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003567 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568 {
3569 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003570 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003571 if (curwin != old_curwin && curwin->w_p_crb)
3572 {
3573# ifdef FEAT_DIFF
3574 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003575 curwin->w_cursor.lnum =
3576 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577 else
3578# endif
3579 curwin->w_cursor.lnum = line;
3580 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003581 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003582 curwin->w_curswant = curswant;
3583 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003584
Bram Moolenaar85a20022019-12-21 18:25:54 +01003585 // Make sure the cursor is in a valid position. Temporarily set
3586 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003587 int restart_edit_save = restart_edit;
3588 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003590
3591 // Avoid a scroll here for the cursor position, 'scrollbind' is
3592 // more important.
3593 if (!curwin->w_p_scb)
3594 validate_cursor();
3595
Bram Moolenaar61452852011-02-01 18:01:11 +01003596 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003597 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 if (has_mbyte)
3599 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003600 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003601
Bram Moolenaar85a20022019-12-21 18:25:54 +01003602 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003603 if (!curwin->w_p_scb)
3604 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 }
3607 }
3608
3609 /*
3610 * reset current-window
3611 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 VIsual_select = old_VIsual_select;
3613 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003614 curwin = old_curwin;
3615 curbuf = old_curbuf;
3616}