blob: 877560792a23bfc10e39939d30a9682442fae475 [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/*
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000205 * Calculates how much overlap the smoothscroll marker "<<<" overlaps with
206 * buffer text for curwin.
207 * 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 */
212 static int
213smoothscroll_marker_overlap(int extra2)
214{
215#if defined(FEAT_LINEBREAK)
216 // We don't draw the <<< marker when in showbreak mode, thus no need to
217 // account for it. See wlv_screen_line().
218 if (*get_showbreak_value(curwin) != NUL)
219 return 0;
220#endif
221 return extra2 > 3 ? 0 : 3 - extra2;
222}
223
224/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000225 * Calculates the skipcol offset for window "wp" given how many
226 * physical lines we want to scroll down.
227 */
228 static int
229skipcol_from_plines(win_T *wp, int plines_off)
230{
231 int width1 = wp->w_width - win_col_off(wp);
232
233 int skipcol = 0;
234 if (plines_off > 0)
235 skipcol += width1;
236 if (plines_off > 1)
237 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
238 return skipcol;
239}
240
241/*
Luuk van Baalc8502f92023-05-06 12:40:15 +0100242 * Set curwin->w_skipcol to zero and redraw later if needed.
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000243 */
244 static void
245reset_skipcol(void)
246{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000247 if (curwin->w_skipcol == 0)
248 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000249
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000250 curwin->w_skipcol = 0;
251
252 // Should use the least expensive way that displays all that changed.
253 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
254 // enough when the top line gets another screen line.
255 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000256}
257
258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 * Update curwin->w_topline and redraw if necessary.
260 * Used to update the screen before printing a message.
261 */
262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100263update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264{
265 update_topline();
266 if (must_redraw)
267 update_screen(0);
268}
269
270/*
271 * Update curwin->w_topline to move the cursor onto the screen.
272 */
273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100274update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
276 long line_count;
277 int halfheight;
278 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#ifdef FEAT_FOLDING
280 linenr_T lnum;
281#endif
282 int check_topline = FALSE;
283 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100284 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100285 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100287 // Cursor is updated instead when this is TRUE for 'splitkeep'.
288 if (skip_update_topline)
289 return;
290
Bram Moolenaar85a20022019-12-21 18:25:54 +0100291 // If there is no valid screen and when the window height is zero just use
292 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200293 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200294 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100295 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200296 curwin->w_topline = curwin->w_cursor.lnum;
297 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200299 return;
300 }
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 check_cursor_moved(curwin);
303 if (curwin->w_valid & VALID_TOPLINE)
304 return;
305
Bram Moolenaar85a20022019-12-21 18:25:54 +0100306 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000307 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100308 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100310 linenr_T old_topline = curwin->w_topline;
311 colnr_T old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_DIFF
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100313 int old_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#endif
315
316 /*
317 * If the buffer is empty, always set topline to 1.
318 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100319 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 {
321 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100322 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 curwin->w_botline = 2;
325 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 }
328
329 /*
330 * If the cursor is above or near the top of the window, scroll the window
331 * to show the line the cursor is in, with 'scrolloff' context.
332 */
333 else
334 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100335 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100337 // If the cursor is above topline, scrolling is always needed.
338 // If the cursor is far below topline and there is no folding,
339 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (curwin->w_cursor.lnum < curwin->w_topline)
341 check_topline = TRUE;
342 else if (check_top_offset())
343 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100344 else if (curwin->w_cursor.lnum == curwin->w_topline)
345 {
346 colnr_T vcol;
347
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000348 // Check that the cursor position is visible. Add columns for
349 // the smoothscroll marker "<<<" displayed in the top-left if
350 // needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100351 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000352 int smoothscroll_overlap = smoothscroll_marker_overlap(
353 curwin_col_off() - curwin_col_off2());
354 if (curwin->w_skipcol + smoothscroll_overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100355 check_topline = TRUE;
356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100359 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
361 curwin->w_topline))
362 check_topline = TRUE;
363#endif
364
365 if (check_topline)
366 {
367 halfheight = curwin->w_height / 2 - 1;
368 if (halfheight < 2)
369 halfheight = 2;
370
371#ifdef FEAT_FOLDING
372 if (hasAnyFolding(curwin))
373 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 // Count the number of logical lines between the cursor and
375 // topline + scrolloff (approximation of how much will be
376 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 n = 0;
378 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100379 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
381 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
384 break;
385 (void)hasFolding(lnum, NULL, &lnum);
386 }
387 }
388 else
389#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100390 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
Bram Moolenaar85a20022019-12-21 18:25:54 +0100392 // If we weren't very close to begin with, we scroll to put the
393 // cursor in the middle of the window. Otherwise put the cursor
394 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000396 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 else
398 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000399 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 check_botline = TRUE;
401 }
402 }
403
404 else
405 {
406#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100407 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
409#endif
410 check_botline = TRUE;
411 }
412 }
413
414 /*
415 * If the cursor is below the bottom of the window, scroll the window
416 * to put the cursor on the window.
417 * When w_botline is invalid, recompute it first, to avoid a redraw later.
418 * If w_botline was approximated, we might need a redraw later in a few
419 * cases, but we don't want to spend (a lot of) time recomputing w_botline
420 * for every small change.
421 */
422 if (check_botline)
423 {
424 if (!(curwin->w_valid & VALID_BOTLINE_AP))
425 validate_botline();
426
427 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
428 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000429 if (curwin->w_cursor.lnum < curwin->w_botline)
430 {
431 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100432 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_FOLDING
434 || hasAnyFolding(curwin)
435#endif
436 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 lineoff_T loff;
439
Bram Moolenaar85a20022019-12-21 18:25:54 +0100440 // Cursor is (a few lines) above botline, check if there are
441 // 'scrolloff' window lines below the cursor. If not, need to
442 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 n = curwin->w_empty_rows;
444 loff.lnum = curwin->w_cursor.lnum;
445#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100446 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
448#endif
449#ifdef FEAT_DIFF
450 loff.fill = 0;
451 n += curwin->w_filler_rows;
452#endif
453 loff.height = 0;
454 while (loff.lnum < curwin->w_botline
455#ifdef FEAT_DIFF
456 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
457#endif
458 )
459 {
460 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100461 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 break;
463 botline_forw(&loff);
464 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100465 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100466 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000468 }
469 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100470 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473 if (check_botline)
474 {
475#ifdef FEAT_FOLDING
476 if (hasAnyFolding(curwin))
477 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100478 // Count the number of logical lines between the cursor and
479 // botline - scrolloff (approximation of how much will be
480 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 line_count = 0;
482 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100483 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {
485 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 if (lnum <= 0 || line_count > curwin->w_height + 1)
488 break;
489 (void)hasFolding(lnum, &lnum, NULL);
490 }
491 }
492 else
493#endif
494 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100495 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000497 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 else
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000499 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 }
501 }
502 }
503 curwin->w_valid |= VALID_TOPLINE;
504
505 /*
506 * Need to redraw when topline changed.
507 */
508 if (curwin->w_topline != old_topline
509#ifdef FEAT_DIFF
510 || curwin->w_topfill != old_topfill
511#endif
512 )
513 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100514 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000515 redraw_later(UPD_VALID);
Luuk van Baal3ce8c382023-05-08 15:51:14 +0100516
517 // Only reset w_skipcol if it was not just set to make cursor visible.
518 if (curwin->w_skipcol == old_skipcol)
519 reset_skipcol();
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000520
Bram Moolenaar85a20022019-12-21 18:25:54 +0100521 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (curwin->w_cursor.lnum == curwin->w_topline)
523 validate_cursor();
524 }
525
Bram Moolenaar375e3392019-01-31 18:26:10 +0100526 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000530 * Return the scrolljump value to use for the current window.
531 * When 'scrolljump' is positive use it as-is.
532 * When 'scrolljump' is negative use it as a percentage of the window height.
533 */
534 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000536{
537 if (p_sj >= 0)
538 return (int)p_sj;
539 return (curwin->w_height * -p_sj) / 100;
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
544 * current window.
545 */
546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 lineoff_T loff;
550 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100551 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar375e3392019-01-31 18:26:10 +0100553 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555 || hasAnyFolding(curwin)
556#endif
557 )
558 {
559 loff.lnum = curwin->w_cursor.lnum;
560#ifdef FEAT_DIFF
561 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100562 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#else
564 n = 0;
565#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100567 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (loff.lnum < curwin->w_topline
572#ifdef FEAT_DIFF
573 || (loff.lnum == curwin->w_topline && loff.fill > 0)
574#endif
575 )
576 break;
577 n += loff.height;
578 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100579 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return TRUE;
581 }
582 return FALSE;
583}
584
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100585/*
586 * Update w_curswant.
587 */
588 void
589update_curswant_force(void)
590{
591 validate_virtcol();
592 curwin->w_curswant = curwin->w_virtcol
593#ifdef FEAT_PROP_POPUP
594 - curwin->w_virtcol_first_char
595#endif
596 ;
597 curwin->w_set_curswant = FALSE;
598}
599
600/*
601 * Update w_curswant if w_set_curswant is set.
602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100607 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Check if the cursor has moved. Set the w_valid flag accordingly.
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
617 {
618 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100619 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
620 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 wp->w_valid_cursor = wp->w_cursor;
622 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100623 wp->w_valid_skipcol = wp->w_skipcol;
624 }
625 else if (wp->w_skipcol != wp->w_valid_skipcol)
626 {
627 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
628 |VALID_CHEIGHT|VALID_CROW
629 |VALID_BOTLINE|VALID_BOTLINE_AP);
630 wp->w_valid_cursor = wp->w_cursor;
631 wp->w_valid_leftcol = wp->w_leftcol;
632 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 else if (wp->w_cursor.col != wp->w_valid_cursor.col
635 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100636 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
639 wp->w_valid_cursor.col = wp->w_cursor.col;
640 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643}
644
645/*
646 * Call this function when some window settings have changed, which require
647 * the cursor position, botline and topline to be recomputed and the window to
648 * be redrawn. E.g, when changing the 'wrap' option or folding.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 changed_window_setting_win(curwin);
654}
655
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 wp->w_lines_valid = 0;
660 changed_line_abv_curs_win(wp);
661 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100662 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663}
664
Dominique Pellee764d1b2023-03-12 21:20:59 +0000665#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000667 * Call changed_window_setting_win() for every window containing "buf".
668 */
669 void
670changed_window_setting_buf(buf_T *buf)
671{
672 tabpage_T *tp;
673 win_T *wp;
674
675 FOR_ALL_TAB_WINDOWS(tp, wp)
676 if (wp->w_buffer == buf)
677 changed_window_setting_win(wp);
678}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000679#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000680
681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 * Set wp->w_topline to a certain number.
683 */
684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200687#ifdef FEAT_DIFF
688 linenr_T prev_topline = wp->w_topline;
689#endif
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100692 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
694#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100697 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
698 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000700 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200702 if (lnum != prev_topline)
703 // Keep the filler lines when the topline didn't change.
704 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#endif
706 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100708 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
711/*
712 * Call this function when the length of the cursor line (in screen
713 * characters) has changed, and the change is before the cursor.
714 * Need to take care of w_botline separately!
715 */
716 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100717changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
720 |VALID_CHEIGHT|VALID_TOPLINE);
721}
722
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
727 |VALID_CHEIGHT|VALID_TOPLINE);
728}
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730/*
731 * Call this function when the length of a line (in screen characters) above
732 * the cursor have changed.
733 * Need to take care of w_botline separately!
734 */
735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
739 |VALID_CHEIGHT|VALID_TOPLINE);
740}
741
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
746 |VALID_CHEIGHT|VALID_TOPLINE);
747}
748
Dominique Pellee764d1b2023-03-12 21:20:59 +0000749#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100751 * Display of line has changed for "buf", invalidate cursor position and
752 * w_botline.
753 */
754 void
755changed_line_display_buf(buf_T *buf)
756{
757 win_T *wp;
758
759 FOR_ALL_WINDOWS(wp)
760 if (wp->w_buffer == buf)
761 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
762 |VALID_CROW|VALID_CHEIGHT
763 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
764}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000765#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100766
767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 * Make sure the value of curwin->w_botline is valid.
769 */
770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100771validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100773 validate_botline_win(curwin);
774}
775
776/*
777 * Make sure the value of wp->w_botline is valid.
778 */
779 void
780validate_botline_win(win_T *wp)
781{
782 if (!(wp->w_valid & VALID_BOTLINE))
783 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * Mark curwin->w_botline as invalid (because of some change in the buffer).
788 */
789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100790invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
793}
794
795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100796invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
799}
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802approximate_botline_win(
803 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 wp->w_valid &= ~VALID_BOTLINE;
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
810 */
811 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 check_cursor_moved(curwin);
815 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
816 (VALID_WROW|VALID_WCOL));
817}
818
819/*
820 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
821 * w_topline must be valid, you may need to call update_topline() first!
822 */
823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100824validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100826 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 check_cursor_moved(curwin);
828 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
829 curs_columns(TRUE);
830}
831
832#if defined(FEAT_GUI) || defined(PROTO)
833/*
834 * validate w_cline_row.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 /*
840 * First make sure that w_topline is valid (after moving the cursor).
841 */
842 update_topline();
843 check_cursor_moved(curwin);
844 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100845 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847#endif
848
849/*
850 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200851 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 linenr_T lnum;
857 int i;
858 int all_invalid;
859 int valid;
860#ifdef FEAT_FOLDING
861 long fold_count;
862#endif
863
Bram Moolenaar85a20022019-12-21 18:25:54 +0100864 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 all_invalid = (!redrawing()
866 || wp->w_lines_valid == 0
867 || wp->w_lines[0].wl_lnum > wp->w_topline);
868 i = 0;
869 wp->w_cline_row = 0;
870 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
871 {
872 valid = FALSE;
873 if (!all_invalid && i < wp->w_lines_valid)
874 {
875 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100876 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (wp->w_lines[i].wl_lnum == lnum)
878 {
879#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880 // Check for newly inserted lines below this row, in which
881 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (!wp->w_buffer->b_mod_set
883 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
884 || wp->w_buffer->b_mod_top
885 > wp->w_lines[i].wl_lastlnum + 1)
886#endif
887 valid = TRUE;
888 }
889 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892 if (valid
893#ifdef FEAT_DIFF
894 && (lnum != wp->w_topline || !wp->w_p_diff)
895#endif
896 )
897 {
898#ifdef FEAT_FOLDING
899 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (lnum > wp->w_cursor.lnum)
902 break;
903#else
904 ++lnum;
905#endif
906 wp->w_cline_row += wp->w_lines[i].wl_size;
907 }
908 else
909 {
910#ifdef FEAT_FOLDING
911 fold_count = foldedCount(wp, lnum, NULL);
912 if (fold_count)
913 {
914 lnum += fold_count;
915 if (lnum > wp->w_cursor.lnum)
916 break;
917 ++wp->w_cline_row;
918 }
919 else
920#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100921 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100922 wp->w_cline_row += plines_correct_topline(wp, lnum);
923 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 }
927
928 check_cursor_moved(wp);
929 if (!(wp->w_valid & VALID_CHEIGHT))
930 {
931 if (all_invalid
932 || i == wp->w_lines_valid
933 || (i < wp->w_lines_valid
934 && (!wp->w_lines[i].wl_valid
935 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
936 {
937#ifdef FEAT_DIFF
938 if (wp->w_cursor.lnum == wp->w_topline)
939 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
940 TRUE) + wp->w_topfill;
941 else
942#endif
943 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
944#ifdef FEAT_FOLDING
945 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
946 NULL, NULL, TRUE, NULL);
947#endif
948 }
949 else if (i > wp->w_lines_valid)
950 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100951 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_cline_height = 0;
953#ifdef FEAT_FOLDING
954 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
955 NULL, NULL, TRUE, NULL);
956#endif
957 }
958 else
959 {
960 wp->w_cline_height = wp->w_lines[i].wl_size;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = wp->w_lines[i].wl_folded;
963#endif
964 }
965 }
966
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100967 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
971/*
972 * Validate curwin->w_virtcol only.
973 */
974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 validate_virtcol_win(curwin);
978}
979
980/*
981 * Validate wp->w_virtcol only.
982 */
983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100984validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000987
988 if (wp->w_valid & VALID_VIRTCOL)
989 return;
990
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100991#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000995#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000997#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000998 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Validate curwin->w_cline_height only.
1003 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001008
1009 if (curwin->w_valid & VALID_CHEIGHT)
1010 return;
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001013 if (curwin->w_cursor.lnum == curwin->w_topline)
1014 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1015 + curwin->w_topfill;
1016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001022 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001026 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 colnr_T off;
1032 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001033 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001037 if (curwin->w_valid & VALID_WCOL)
1038 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001039
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 col = curwin->w_virtcol;
1041 off = curwin_col_off();
1042 col += off;
1043 width = curwin->w_width - off + curwin_col_off2();
1044
1045 // long line wrapping, adjust curwin->w_wrow
1046 if (curwin->w_p_wrap
1047 && col >= (colnr_T)curwin->w_width
1048 && width > 0)
1049 // use same formula as what is used in curs_columns()
1050 col -= ((col - curwin->w_width) / width + 1) * width;
1051 if (col > (int)curwin->w_leftcol)
1052 col -= curwin->w_leftcol;
1053 else
1054 col = 0;
1055 curwin->w_wcol = col;
1056
1057 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001058#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001059 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001064 * Compute offset of a window, occupied by absolute or relative line number,
1065 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001068win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar64486672010-05-16 15:46:46 +02001070 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_FOLDING
1073 + wp->w_p_fdc
1074#endif
1075#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001076 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
1078 );
1079}
1080
1081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001082curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return win_col_off(curwin);
1085}
1086
1087/*
1088 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001089 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1090 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
1092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar64486672010-05-16 15:46:46 +02001095 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001096 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 return 0;
1098}
1099
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 return win_col_off2(curwin);
1104}
1105
1106/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001107 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * Also updates curwin->w_wrow and curwin->w_cline_row.
1109 * Also updates curwin->w_leftcol.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int off_left, off_right;
1118 int n;
1119 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001120 int width1; // text width for first screen line
1121 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int new_leftcol;
1123 colnr_T startcol;
1124 colnr_T endcol;
1125 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001126 long so = get_scrolloff_value();
1127 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001128 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 /*
1131 * First make sure that w_topline is valid (after moving the cursor).
1132 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001133 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 /*
1136 * Next make sure that w_cline_row is valid.
1137 */
1138 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001139 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001141#ifdef FEAT_PROP_POPUP
1142 // will be set by getvvcol() but not reset
1143 curwin->w_virtcol_first_char = 0;
1144#endif
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 /*
1147 * Compute the number of virtual columns.
1148 */
1149#ifdef FEAT_FOLDING
1150 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1153 else
1154#endif
1155 getvvcol(curwin, &curwin->w_cursor,
1156 &startcol, &(curwin->w_virtcol), &endcol);
1157
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001160 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 extra = curwin_col_off();
1163 curwin->w_wcol = curwin->w_virtcol + extra;
1164 endcol += extra;
1165
1166 /*
1167 * Now compute w_wrow, counting screen lines from w_cline_row.
1168 */
1169 curwin->w_wrow = curwin->w_cline_row;
1170
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001171 width1 = curwin->w_width - extra;
1172 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001175 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001176 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001177 if (curwin->w_p_wrap)
1178 curwin->w_wrow = curwin->w_height - 1;
1179 else
1180 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001182 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001184 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001186 // skip columns that are not visible
1187 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001188 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001189 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001190 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001191 // Deduct by multiples of width2. This allows the long line
1192 // wrapping formula below to correctly calculate the w_wcol value
1193 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001198 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001200 did_sub_skipcol = TRUE;
1201 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001202
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001204 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001207 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1208 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 curwin->w_wrow += n;
1210
1211#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001212 // When cursor wraps to first char of next line in Insert
1213 // mode, the 'showbreak' string isn't shown, backup to first
1214 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001215 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001216 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001217 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 curwin->w_wcol = 0;
1219#endif
1220 }
1221 }
1222
Bram Moolenaar85a20022019-12-21 18:25:54 +01001223 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1224 // is not folded.
1225 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001226 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#ifdef FEAT_FOLDING
1228 && !curwin->w_cline_folded
1229#endif
1230 )
1231 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001232#ifdef FEAT_PROP_POPUP
1233 if (curwin->w_virtcol_first_char > 0)
1234 {
1235 int cols = (curwin->w_width - extra);
1236 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1237
1238 // each "above" text prop shifts the text one row down
1239 curwin->w_wrow += rows;
1240 curwin->w_wcol -= rows * cols;
1241 endcol -= rows * cols;
1242 curwin->w_cline_height = rows + 1;
1243 }
1244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /*
1246 * If Cursor is left of the screen, scroll rightwards.
1247 * If Cursor is right of the screen, scroll leftwards
1248 * If we get closer to the edge than 'sidescrolloff', scroll a little
1249 * extra
1250 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001251 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001252 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001253 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (off_left < 0 || off_right > 0)
1255 {
1256 if (off_left < 0)
1257 diff = -off_left;
1258 else
1259 diff = off_right;
1260
Bram Moolenaar85a20022019-12-21 18:25:54 +01001261 // When far off or not enough room on either side, put cursor in
1262 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001263 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1264 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 else
1266 {
1267 if (diff < p_ss)
1268 diff = p_ss;
1269 if (off_left < 0)
1270 new_leftcol = curwin->w_leftcol - diff;
1271 else
1272 new_leftcol = curwin->w_leftcol + diff;
1273 }
1274 if (new_leftcol < 0)
1275 new_leftcol = 0;
1276 if (new_leftcol != (int)curwin->w_leftcol)
1277 {
1278 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001280 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 curwin->w_wcol -= curwin->w_leftcol;
1284 }
1285 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1286 curwin->w_wcol -= curwin->w_leftcol;
1287 else
1288 curwin->w_wcol = 0;
1289
1290#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 // Skip over filler lines. At the top use w_topfill, there
1292 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (curwin->w_cursor.lnum == curwin->w_topline)
1294 curwin->w_wrow += curwin->w_topfill;
1295 else
1296 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1297#endif
1298
1299 prev_skipcol = curwin->w_skipcol;
1300
1301 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if ((curwin->w_wrow >= curwin->w_height
1304 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001305 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && (p_lines =
1307#ifdef FEAT_DIFF
1308 plines_win_nofill
1309#else
1310 plines_win
1311#endif
1312 (curwin, curwin->w_cursor.lnum, FALSE))
1313 - 1 >= curwin->w_height))
1314 && curwin->w_height != 0
1315 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001316 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001317 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 // Cursor past end of screen. Happens with a single line that does
1320 // not fit on screen. Find a skipcol to show the text around the
1321 // cursor. Avoid scrolling all the time. compute value of "extra":
1322 // 1: Less than 'scrolloff' lines above
1323 // 2: Less than 'scrolloff' lines below
1324 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001326 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 // Compute last display line of the buffer line that we want at the
1329 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (p_lines == 0)
1331 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1332 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001333 if (p_lines > curwin->w_wrow + so)
1334 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
1336 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001337 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 extra += 2;
1339
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001340 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001343 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (n > curwin->w_height / 2)
1345 n -= curwin->w_height / 2;
1346 else
1347 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (n > p_lines - curwin->w_height + 1)
1350 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else if (extra == 1)
1354 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001355 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001356 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1357 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (extra > 0)
1359 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001360 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1361 extra = curwin->w_skipcol / width2;
1362 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 }
1365 else if (extra == 2)
1366 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001367 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (endcol > curwin->w_skipcol)
1372 curwin->w_skipcol = endcol;
1373 }
1374
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001375 // adjust w_wrow for the changed w_skipcol
1376 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001378 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (curwin->w_wrow >= curwin->w_height)
1382 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 curwin->w_wrow -= extra;
1387 }
1388
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001389 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (extra > 0)
1391 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1392 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001393 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001395 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 curwin->w_skipcol = 0;
1397 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001398 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001400#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001401 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001402#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001403#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1404 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1405 {
1406 curwin->w_wrow += popup_top_extra(curwin);
1407 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001408 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001409 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001410 else
1411 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001412#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001413
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001414 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1415 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001416 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001417 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1420}
1421
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001422#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001423/*
1424 * Compute the screen position of text character at "pos" in window "wp"
1425 * The resulting values are one-based, zero when character is not visible.
1426 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001427 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001428textpos2screenpos(
1429 win_T *wp,
1430 pos_T *pos,
1431 int *rowp, // screen row
1432 int *scolp, // start screen column
1433 int *ccolp, // cursor screen column
1434 int *ecolp) // end screen column
1435{
1436 colnr_T scol = 0, ccol = 0, ecol = 0;
1437 int row = 0;
1438 int rowoff = 0;
1439 colnr_T coloff = 0;
1440
Bram Moolenaar189663b2021-07-21 18:04:56 +02001441 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001442 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001443 colnr_T col;
1444 int width;
1445 linenr_T lnum = pos->lnum;
1446#ifdef FEAT_FOLDING
1447 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001448
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001449 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1450#endif
1451 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001452
1453#ifdef FEAT_DIFF
1454 // Add filler lines above this buffer line.
1455 row += diff_check_fill(wp, lnum);
1456#endif
1457
zeertzjqba2d1912022-12-18 12:28:59 +00001458 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001459#ifdef FEAT_FOLDING
1460 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001462 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001463 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001464 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001465 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001466#endif
1467 {
1468 getvcol(wp, pos, &scol, &ccol, &ecol);
1469
1470 // similar to what is done in validate_cursor_col()
1471 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001472 col += off;
1473 width = wp->w_width - off + win_col_off2(wp);
1474
1475 // long line wrapping, adjust row
1476 if (wp->w_p_wrap
1477 && col >= (colnr_T)wp->w_width
1478 && width > 0)
1479 {
1480 // use same formula as what is used in curs_columns()
1481 rowoff = ((col - wp->w_width) / width + 1);
1482 col -= rowoff * width;
1483 }
1484 col -= wp->w_leftcol;
1485 if (col >= wp->w_width)
1486 col = -1;
1487 if (col >= 0 && row + rowoff <= wp->w_height)
1488 {
1489 coloff = col - scol + wp->w_wincol + 1;
1490 row += W_WINROW(wp);
1491 }
1492 else
1493 // character is left, right or below of the window
1494 row = rowoff = scol = ccol = ecol = 0;
1495 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001496 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001497 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498 *scolp = scol + coloff;
1499 *ccolp = ccol + coloff;
1500 *ecolp = ecol + coloff;
1501}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001502#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503
Bram Moolenaar12034e22019-08-25 22:25:02 +02001504#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001505/*
1506 * "screenpos({winid}, {lnum}, {col})" function
1507 */
1508 void
1509f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1510{
1511 dict_T *dict;
1512 win_T *wp;
1513 pos_T pos;
1514 int row = 0;
1515 int scol = 0, ccol = 0, ecol = 0;
1516
Bram Moolenaar93a10962022-06-16 11:42:09 +01001517 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518 return;
1519 dict = rettv->vval.v_dict;
1520
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001521 if (in_vim9script()
1522 && (check_for_number_arg(argvars, 0) == FAIL
1523 || check_for_number_arg(argvars, 1) == FAIL
1524 || check_for_number_arg(argvars, 2) == FAIL))
1525 return;
1526
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001527 wp = find_win_by_nr_or_id(&argvars[0]);
1528 if (wp == NULL)
1529 return;
1530
1531 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001532 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1533 {
1534 semsg(_(e_invalid_line_number_nr), pos.lnum);
1535 return;
1536 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001537 pos.col = tv_get_number(&argvars[2]) - 1;
1538 pos.coladd = 0;
1539 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1540
1541 dict_add_number(dict, "row", row);
1542 dict_add_number(dict, "col", scol);
1543 dict_add_number(dict, "curscol", ccol);
1544 dict_add_number(dict, "endcol", ecol);
1545}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001546
1547/*
1548 * "virtcol2col({winid}, {lnum}, {col})" function
1549 */
1550 void
1551f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1552{
1553 win_T *wp;
1554 linenr_T lnum;
1555 int screencol;
1556 int error = FALSE;
1557
1558 rettv->vval.v_number = -1;
1559
1560 if (check_for_number_arg(argvars, 0) == FAIL
1561 || check_for_number_arg(argvars, 1) == FAIL
1562 || check_for_number_arg(argvars, 2) == FAIL)
1563 return;
1564
1565 wp = find_win_by_nr_or_id(&argvars[0]);
1566 if (wp == NULL)
1567 return;
1568
1569 lnum = tv_get_number_chk(&argvars[1], &error);
1570 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1571 return;
1572
1573 screencol = tv_get_number_chk(&argvars[2], &error);
1574 if (error || screencol < 0)
1575 return;
1576
1577 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1578}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001579#endif
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581/*
1582 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001585scrolldown(
1586 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001589 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 int wrow;
1591 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001592 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001593 int width1 = 0;
1594 int width2 = 0;
1595
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001596 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001597 {
1598 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001599 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602#ifdef FEAT_FOLDING
1603 linenr_T first;
1604
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1607#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001609 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001612 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1613 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 ++curwin->w_topfill;
1616 ++done;
1617 }
1618 else
1619#endif
1620 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001621 // break when at the very top
1622 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001623 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001625 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001627 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001628 if (curwin->w_skipcol >= width1 + width2)
1629 curwin->w_skipcol -= width2;
1630 else
1631 curwin->w_skipcol -= width1;
1632 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001636 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001637 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001638 --curwin->w_topline;
1639 curwin->w_skipcol = 0;
1640#ifdef FEAT_DIFF
1641 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001643#ifdef FEAT_FOLDING
1644 // A sequence of folded lines only counts for one logical line
1645 if (hasFolding(curwin->w_topline, &first, NULL))
1646 {
1647 ++done;
1648 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001649 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650 curwin->w_botline -= curwin->w_topline - first;
1651 curwin->w_topline = first;
1652 }
1653 else
1654#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001655 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001656 {
1657 int size = win_linetabsize(curwin, curwin->w_topline,
1658 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1659 if (size > width1)
1660 {
1661 curwin->w_skipcol = width1;
1662 size -= width1;
1663 redraw_later(UPD_NOT_VALID);
1664 }
1665 while (size > width2)
1666 {
1667 curwin->w_skipcol += width2;
1668 size -= width2;
1669 }
1670 ++done;
1671 }
1672 else
1673 done += PLINES_NOFILL(curwin->w_topline);
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001676 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 invalidate_botline();
1678 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001679 curwin->w_wrow += done; // keep w_wrow updated
1680 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682#ifdef FEAT_DIFF
1683 if (curwin->w_cursor.lnum == curwin->w_topline)
1684 curwin->w_cline_row = 0;
1685 check_topfill(curwin, TRUE);
1686#endif
1687
1688 /*
1689 * Compute the row number of the last row of the cursor line
1690 * and move the cursor onto the displayed part of the window.
1691 */
1692 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001693 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 validate_virtcol();
1696 validate_cheight();
1697 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001698 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1701 {
1702#ifdef FEAT_FOLDING
1703 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1704 {
1705 --wrow;
1706 if (first == 1)
1707 curwin->w_cursor.lnum = 1;
1708 else
1709 curwin->w_cursor.lnum = first - 1;
1710 }
1711 else
1712#endif
1713 wrow -= plines(curwin->w_cursor.lnum--);
1714 curwin->w_valid &=
1715 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1716 moved = TRUE;
1717 }
1718 if (moved)
1719 {
1720#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001721 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 foldAdjustCursor();
1723#endif
1724 coladvance(curwin->w_curswant);
1725 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001726
1727 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1728 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001729 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001730 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1731
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001732 // make sure the cursor is in the visible text
1733 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001734 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001735 int row = 0;
1736 if (col >= width1)
1737 {
1738 col -= width1;
1739 ++row;
1740 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001741 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001742 {
1743 row += col / width2;
1744 col = col % width2;
1745 }
1746 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001747 {
1748 curwin->w_curswant = curwin->w_virtcol
1749 - (row - curwin->w_height + 1) * width2;
1750 coladvance(curwin->w_curswant);
1751 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753}
1754
1755/*
1756 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759scrollup(
1760 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001763 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Luuk van Baalaa6ba302023-05-09 16:01:17 +01001765 if (do_sms
1766# ifdef FEAT_FOLDING
1767 || (byfold && hasAnyFolding(curwin))
1768# endif
1769# ifdef FEAT_DIFF
1770 || (curwin->w_p_diff && !curwin->w_p_wrap)
1771# endif
1772 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001774 int width1 = curwin->w_width - curwin_col_off();
1775 int width2 = width1 + curwin_col_off2();
1776 int size = 0;
1777 linenr_T prev_topline = curwin->w_topline;
1778
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001779 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001780 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001781
1782 // diff mode: first consume "topfill"
1783 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1784 // the line, then advance to the next line.
1785 // folding: count each sequence of folded lines as one logical line.
1786 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
1788# ifdef FEAT_DIFF
1789 if (curwin->w_topfill > 0)
1790 --curwin->w_topfill;
1791 else
1792# endif
1793 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001794 linenr_T lnum = curwin->w_topline;
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796# ifdef FEAT_FOLDING
1797 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001798 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 (void)hasFolding(lnum, NULL, &lnum);
1800# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001801 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001802 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 // 'smoothscroll': increase "w_skipcol" until it goes over
1804 // the end of the line, then advance to the next line.
1805 int add = curwin->w_skipcol > 0 ? width2 : width1;
1806 curwin->w_skipcol += add;
1807 if (curwin->w_skipcol >= size)
1808 {
1809 if (lnum == curbuf->b_ml.ml_line_count)
1810 {
1811 // at the last screen line, can't scroll further
1812 curwin->w_skipcol -= add;
1813 break;
1814 }
1815 ++lnum;
1816 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001817 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001818 else
1819 {
1820 if (lnum >= curbuf->b_ml.ml_line_count)
1821 break;
1822 ++lnum;
1823 }
1824
1825 if (lnum > curwin->w_topline)
1826 {
1827 // approximate w_botline
1828 curwin->w_botline += lnum - curwin->w_topline;
1829 curwin->w_topline = lnum;
1830# ifdef FEAT_DIFF
1831 curwin->w_topfill = diff_check_fill(curwin, lnum);
1832# endif
1833 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001834 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001835 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001836 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001837 }
1838 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001839
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001840 if (curwin->w_topline == prev_topline)
1841 // need to redraw even though w_topline didn't change
1842 redraw_later(UPD_NOT_VALID);
1843 }
1844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001847 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849
1850 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1851 curwin->w_topline = curbuf->b_ml.ml_line_count;
1852 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1853 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1854
1855#ifdef FEAT_DIFF
1856 check_topfill(curwin, FALSE);
1857#endif
1858
1859#ifdef FEAT_FOLDING
1860 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001861 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1863#endif
1864
1865 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1866 if (curwin->w_cursor.lnum < curwin->w_topline)
1867 {
1868 curwin->w_cursor.lnum = curwin->w_topline;
1869 curwin->w_valid &=
1870 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1871 coladvance(curwin->w_curswant);
1872 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001873 if (curwin->w_cursor.lnum == curwin->w_topline
1874 && do_sms && curwin->w_skipcol > 0)
1875 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001876 int col_off = curwin_col_off();
1877 int col_off2 = curwin_col_off2();
1878
1879 int width1 = curwin->w_width - col_off;
1880 int width2 = width1 + col_off2;
1881 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001882 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001883 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001884 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001885
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001886 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1887 // going past it anyway.
1888 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1889 smoothscroll_marker_overlap(extra2);
1890
Bram Moolenaar118c2352022-10-09 17:19:27 +01001891 // Make sure the cursor is in a visible part of the line, taking
1892 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001893 // If there are not enough screen lines put the cursor in the middle.
1894 if (scrolloff_cols > space_cols / 2)
1895 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001896 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001897 if (curwin->w_virtcol < curwin->w_skipcol
1898 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001899 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001900 colnr_T col = curwin->w_virtcol;
1901
1902 if (col < width1)
1903 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001904 while (col < curwin->w_skipcol
1905 + smoothscroll_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();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001952 while (curwin->w_skipcol > 0
1953 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1954 {
1955 // scroll a screen line down
1956 if (curwin->w_skipcol >= width1 + width2)
1957 curwin->w_skipcol -= width2;
1958 else
1959 curwin->w_skipcol -= width1;
1960 redraw_later(UPD_NOT_VALID);
1961 scrolled = TRUE;
1962 validate_virtcol();
1963 }
1964 if (scrolled)
1965 return; // don't scroll in the other direction now
1966
1967 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1968 int row = 0;
1969 if (col >= width1)
1970 {
1971 col -= width1;
1972 ++row;
1973 }
1974 if (col > width2)
1975 {
1976 row += col / width2;
1977 col = col % width2;
1978 }
1979 if (row >= curwin->w_height)
1980 {
1981 if (curwin->w_skipcol == 0)
1982 {
1983 curwin->w_skipcol += width1;
1984 --row;
1985 }
1986 if (row >= curwin->w_height)
1987 curwin->w_skipcol += (row - curwin->w_height) * width2;
1988 redraw_later(UPD_NOT_VALID);
1989 }
1990}
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#ifdef FEAT_DIFF
1993/*
1994 * Don't end up with too many filler lines in the window.
1995 */
1996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997check_topfill(
1998 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001999 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 int n;
2002
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002003 if (wp->w_topfill <= 0)
2004 return;
2005
2006 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2007 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002009 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002011 --wp->w_topline;
2012 wp->w_topfill = 0;
2013 }
2014 else
2015 {
2016 wp->w_topfill = wp->w_height - n;
2017 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 }
2021}
2022
2023/*
2024 * Use as many filler lines as possible for w_topline. Make sure w_topline
2025 * is still visible.
2026 */
2027 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002028max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 int n;
2031
2032 n = plines_nofill(curwin->w_topline);
2033 if (n >= curwin->w_height)
2034 curwin->w_topfill = 0;
2035 else
2036 {
2037 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2038 if (curwin->w_topfill + n > curwin->w_height)
2039 curwin->w_topfill = curwin->w_height - n;
2040 }
2041}
2042#endif
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044/*
2045 * Scroll the screen one line down, but don't do it if it would move the
2046 * cursor off the screen.
2047 */
2048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002049scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 int end_row;
2052#ifdef FEAT_DIFF
2053 int can_fill = (curwin->w_topfill
2054 < diff_check_fill(curwin, curwin->w_topline));
2055#endif
2056
2057 if (curwin->w_topline <= 1
2058#ifdef FEAT_DIFF
2059 && !can_fill
2060#endif
2061 )
2062 return;
2063
Bram Moolenaar85a20022019-12-21 18:25:54 +01002064 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 /*
2067 * Compute the row number of the last row of the cursor line
2068 * and make sure it doesn't go off the screen. Make sure the cursor
2069 * doesn't go past 'scrolloff' lines from the screen end.
2070 */
2071 end_row = curwin->w_wrow;
2072#ifdef FEAT_DIFF
2073 if (can_fill)
2074 ++end_row;
2075 else
2076 end_row += plines_nofill(curwin->w_topline - 1);
2077#else
2078 end_row += plines(curwin->w_topline - 1);
2079#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002080 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 validate_cheight();
2083 validate_virtcol();
2084 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002085 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002087 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089#ifdef FEAT_DIFF
2090 if (can_fill)
2091 {
2092 ++curwin->w_topfill;
2093 check_topfill(curwin, TRUE);
2094 }
2095 else
2096 {
2097 --curwin->w_topline;
2098 curwin->w_topfill = 0;
2099 }
2100#else
2101 --curwin->w_topline;
2102#endif
2103#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002104 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002106 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2108 }
2109}
2110
2111/*
2112 * Scroll the screen one line up, but don't do it if it would move the cursor
2113 * off the screen.
2114 */
2115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002116scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
2118 int start_row;
2119
2120 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2121#ifdef FEAT_DIFF
2122 && curwin->w_topfill == 0
2123#endif
2124 )
2125 return;
2126
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /*
2130 * Compute the row number of the first row of the cursor line
2131 * and make sure it doesn't go off the screen. Make sure the cursor
2132 * doesn't go before 'scrolloff' lines from the screen start.
2133 */
2134#ifdef FEAT_DIFF
2135 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2136 - curwin->w_topfill;
2137#else
2138 start_row = curwin->w_wrow - plines(curwin->w_topline);
2139#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002140 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002143 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002145 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147#ifdef FEAT_DIFF
2148 if (curwin->w_topfill > 0)
2149 --curwin->w_topfill;
2150 else
2151#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002152 {
2153#ifdef FEAT_FOLDING
2154 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002157 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002158 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2160 }
2161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163/*
2164 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2165 * a (wrapped) text line. Uses and sets "lp->fill".
2166 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002167 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002170topline_back_winheight(
2171 lineoff_T *lp,
2172 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174#ifdef FEAT_DIFF
2175 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2176 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002177 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ++lp->fill;
2179 lp->height = 1;
2180 }
2181 else
2182#endif
2183 {
2184 --lp->lnum;
2185#ifdef FEAT_DIFF
2186 lp->fill = 0;
2187#endif
2188 if (lp->lnum < 1)
2189 lp->height = MAXCOL;
2190 else
2191#ifdef FEAT_FOLDING
2192 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002193 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 lp->height = 1;
2195 else
2196#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002197 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199}
2200
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002201 static void
2202topline_back(lineoff_T *lp)
2203{
2204 topline_back_winheight(lp, TRUE);
2205}
2206
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208/*
2209 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2210 * a (wrapped) text line. Uses and sets "lp->fill".
2211 * Returns the height of the added line in "lp->height".
2212 * Lines below the last one are incredibly high.
2213 */
2214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002215botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217#ifdef FEAT_DIFF
2218 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 ++lp->fill;
2222 lp->height = 1;
2223 }
2224 else
2225#endif
2226 {
2227 ++lp->lnum;
2228#ifdef FEAT_DIFF
2229 lp->fill = 0;
2230#endif
2231 if (lp->lnum > curbuf->b_ml.ml_line_count)
2232 lp->height = MAXCOL;
2233 else
2234#ifdef FEAT_FOLDING
2235 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002236 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 lp->height = 1;
2238 else
2239#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002240 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242}
2243
2244#ifdef FEAT_DIFF
2245/*
2246 * Switch from including filler lines below lp->lnum to including filler
2247 * lines above loff.lnum + 1. This keeps pointing to the same line.
2248 * When there are no filler lines nothing changes.
2249 */
2250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002251botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 if (lp->fill > 0)
2254 {
2255 ++lp->lnum;
2256 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2257 }
2258}
2259
2260/*
2261 * Switch from including filler lines above lp->lnum to including filler
2262 * lines below loff.lnum - 1. This keeps pointing to the same line.
2263 * When there are no filler lines nothing changes.
2264 */
2265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 if (lp->fill > 0)
2269 {
2270 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2271 --lp->lnum;
2272 }
2273}
2274#endif
2275
2276/*
2277 * Recompute topline to put the cursor at the top of the window.
2278 * Scroll at least "min_scroll" lines.
2279 * If "always" is TRUE, always set topline (for "zt").
2280 */
2281 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002282scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 int scrolled = 0;
2285 int extra = 0;
2286 int used;
2287 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 linenr_T top; // just above displayed lines
2289 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002291 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#ifdef FEAT_DIFF
2293 linenr_T old_topfill = curwin->w_topfill;
2294#endif
2295 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002296 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (mouse_dragging > 0)
2299 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 /*
2302 * Decrease topline until:
2303 * - it has become 1
2304 * - (part of) the cursor line is moved off the screen or
2305 * - moved at least 'scrolljump' lines and
2306 * - at least 'scrolloff' lines above and below the cursor
2307 */
2308 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002309 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (curwin->w_cursor.lnum < curwin->w_topline)
2311 scrolled = used;
2312
2313#ifdef FEAT_FOLDING
2314 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2315 {
2316 --top;
2317 ++bot;
2318 }
2319 else
2320#endif
2321 {
2322 top = curwin->w_cursor.lnum - 1;
2323 bot = curwin->w_cursor.lnum + 1;
2324 }
2325 new_topline = top + 1;
2326
2327#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002328 // "used" already contains the number of filler lines above, don't add it
2329 // again.
2330 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002331 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333
2334 /*
2335 * Check if the lines from "top" to "bot" fit in the window. If they do,
2336 * set new_topline and advance "top" and "bot" to include more lines.
2337 */
2338 while (top > 0)
2339 {
2340#ifdef FEAT_FOLDING
2341 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002342 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 i = 1;
2344 else
2345#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002346 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002347 if (top < curwin->w_topline)
2348 scrolled += i;
2349
2350 // If scrolling is needed, scroll at least 'sj' lines.
2351 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2352 && extra >= off)
2353 break;
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 used += i;
2356 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2357 {
2358#ifdef FEAT_FOLDING
2359 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002360 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 ++used;
2362 else
2363#endif
2364 used += plines(bot);
2365 }
2366 if (used > curwin->w_height)
2367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 extra += i;
2370 new_topline = top;
2371 --top;
2372 ++bot;
2373 }
2374
2375 /*
2376 * If we don't have enough space, put cursor in the middle.
2377 * This makes sure we get the same position when using "k" and "j"
2378 * in a small window.
2379 */
2380 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002381 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 else
2383 {
2384 /*
2385 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002386 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
2388 if (new_topline < curwin->w_topline || always)
2389 curwin->w_topline = new_topline;
2390 if (curwin->w_topline > curwin->w_cursor.lnum)
2391 curwin->w_topline = curwin->w_cursor.lnum;
2392#ifdef FEAT_DIFF
2393 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2394 if (curwin->w_topfill > 0 && extra > off)
2395 {
2396 curwin->w_topfill -= extra - off;
2397 if (curwin->w_topfill < 0)
2398 curwin->w_topfill = 0;
2399 }
2400 check_topfill(curwin, FALSE);
2401#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002402 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002403 if (curwin->w_topline == curwin->w_cursor.lnum
2404 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002405 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002407 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#ifdef FEAT_DIFF
2409 || curwin->w_topfill != old_topfill
2410#endif
2411 )
2412 curwin->w_valid &=
2413 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2414 curwin->w_valid |= VALID_TOPLINE;
2415 }
2416}
2417
2418/*
2419 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2420 * screen lines for text lines.
2421 */
2422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002423set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425#ifdef FEAT_DIFF
2426 wp->w_filler_rows = 0;
2427#endif
2428 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002429 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 else
2431 {
2432 wp->w_empty_rows = wp->w_height - used;
2433#ifdef FEAT_DIFF
2434 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2435 {
2436 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2437 if (wp->w_empty_rows > wp->w_filler_rows)
2438 wp->w_empty_rows -= wp->w_filler_rows;
2439 else
2440 {
2441 wp->w_filler_rows = wp->w_empty_rows;
2442 wp->w_empty_rows = 0;
2443 }
2444 }
2445#endif
2446 }
2447}
2448
2449/*
2450 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002451 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2453 * This is messy stuff!!!
2454 */
2455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 int used;
2459 int scrolled = 0;
2460 int extra = 0;
2461 int i;
2462 linenr_T line_count;
2463 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002464 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 lineoff_T loff;
2466 lineoff_T boff;
2467#ifdef FEAT_DIFF
2468 int old_topfill = curwin->w_topfill;
2469 int fill_below_window;
2470#endif
2471 linenr_T old_botline = curwin->w_botline;
2472 linenr_T old_valid = curwin->w_valid;
2473 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002475 long so = get_scrolloff_value();
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002476 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 cln = curwin->w_cursor.lnum;
2479 if (set_topbot)
2480 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002481 int set_skipcol = FALSE;
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 used = 0;
2484 curwin->w_botline = cln + 1;
2485#ifdef FEAT_DIFF
2486 loff.fill = 0;
2487#endif
2488 for (curwin->w_topline = curwin->w_botline;
2489 curwin->w_topline > 1;
2490 curwin->w_topline = loff.lnum)
2491 {
2492 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002493 topline_back_winheight(&loff, FALSE);
2494 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002496 if (used + loff.height > curwin->w_height)
2497 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002498 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002499 {
2500 // 'smoothscroll' and 'wrap' are set. The above line is
2501 // too long to show in its entirety, so we show just a part
2502 // of it.
2503 if (used < curwin->w_height)
2504 {
2505 int plines_offset = used + loff.height
2506 - curwin->w_height;
2507 used = curwin->w_height;
2508#ifdef FEAT_DIFF
2509 curwin->w_topfill = loff.fill;
2510#endif
2511 curwin->w_topline = loff.lnum;
2512 curwin->w_skipcol = skipcol_from_plines(
2513 curwin, plines_offset);
2514 set_skipcol = TRUE;
2515 }
2516 }
2517 break;
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 used += loff.height;
2520#ifdef FEAT_DIFF
2521 curwin->w_topfill = loff.fill;
2522#endif
2523 }
2524 set_empty_rows(curwin, used);
2525 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2526 if (curwin->w_topline != old_topline
2527#ifdef FEAT_DIFF
2528 || curwin->w_topfill != old_topfill
2529#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002530 || set_skipcol
2531 || curwin->w_skipcol != 0)
2532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002534 if (set_skipcol)
2535 redraw_later(UPD_NOT_VALID);
2536 else
2537 reset_skipcol();
2538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 else
2541 validate_botline();
2542
Bram Moolenaar85a20022019-12-21 18:25:54 +01002543 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544#ifdef FEAT_DIFF
2545 used = plines_nofill(cln);
2546#else
2547 validate_cheight();
2548 used = curwin->w_cline_height;
2549#endif
2550
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002551 // If the cursor is on or below botline, we will at least scroll by the
2552 // height of the cursor line, which is "used". Correct for empty lines,
2553 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (cln >= curwin->w_botline)
2555 {
2556 scrolled = used;
2557 if (cln == curwin->w_botline)
2558 scrolled -= curwin->w_empty_rows;
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002559 if (do_sms)
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002560 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002561 // 'smoothscroll' and 'wrap' are set.
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002562 // Calculate how many screen lines the current top line of window
2563 // occupies. If it is occupying more than the entire window, we
2564 // need to scroll the additional clipped lines to scroll past the
2565 // top line before we can move on to the other lines.
2566 int top_plines =
2567#ifdef FEAT_DIFF
2568 plines_win_nofill
2569#else
2570 plines_win
2571#endif
2572 (curwin, curwin->w_topline, FALSE);
2573 int skip_lines = 0;
2574 int width1 = curwin->w_width - curwin_col_off();
2575 int width2 = width1 + curwin_col_off2();
2576 // similar formula is used in curs_columns()
2577 if (curwin->w_skipcol > width1)
2578 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2579 else if (curwin->w_skipcol > 0)
2580 skip_lines = 1;
2581
2582 top_plines -= skip_lines;
2583 if (top_plines > curwin->w_height)
2584 {
2585 scrolled += (top_plines - curwin->w_height);
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002586 }
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589
2590 /*
2591 * Stop counting lines to scroll when
2592 * - hitting start of the file
2593 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002594 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 * - lines between botline and cursor have been counted
2596 */
2597#ifdef FEAT_FOLDING
2598 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2599#endif
2600 {
2601 loff.lnum = cln;
2602 boff.lnum = cln;
2603 }
2604#ifdef FEAT_DIFF
2605 loff.fill = 0;
2606 boff.fill = 0;
2607 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2608 - curwin->w_filler_rows;
2609#endif
2610
2611 while (loff.lnum > 1)
2612 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002613 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2614 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002616 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2618 && loff.lnum <= curwin->w_botline
2619#ifdef FEAT_DIFF
2620 && (loff.lnum < curwin->w_botline
2621 || loff.fill >= fill_below_window)
2622#endif
2623 )
2624 break;
2625
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002628 if (loff.height == MAXCOL)
2629 used = MAXCOL;
2630 else
2631 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (used > curwin->w_height)
2633 break;
2634 if (loff.lnum >= curwin->w_botline
2635#ifdef FEAT_DIFF
2636 && (loff.lnum > curwin->w_botline
2637 || loff.fill <= fill_below_window)
2638#endif
2639 )
2640 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002641 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 scrolled += loff.height;
2643 if (loff.lnum == curwin->w_botline
2644#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002645 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647 )
2648 scrolled -= curwin->w_empty_rows;
2649 }
2650
2651 if (boff.lnum < curbuf->b_ml.ml_line_count)
2652 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 botline_forw(&boff);
2655 used += boff.height;
2656 if (used > curwin->w_height)
2657 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002658 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2659 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 extra += boff.height;
2662 if (boff.lnum >= curwin->w_botline
2663#ifdef FEAT_DIFF
2664 || (boff.lnum + 1 == curwin->w_botline
2665 && boff.fill > curwin->w_filler_rows)
2666#endif
2667 )
2668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 scrolled += boff.height;
2671 if (boff.lnum == curwin->w_botline
2672#ifdef FEAT_DIFF
2673 && boff.fill == 0
2674#endif
2675 )
2676 scrolled -= curwin->w_empty_rows;
2677 }
2678 }
2679 }
2680 }
2681
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (scrolled <= 0)
2684 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 else if (used > curwin->w_height)
2687 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002688 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 else
2690 {
2691 line_count = 0;
2692#ifdef FEAT_DIFF
2693 boff.fill = curwin->w_topfill;
2694#endif
2695 boff.lnum = curwin->w_topline - 1;
2696 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2697 {
2698 botline_forw(&boff);
2699 i += boff.height;
2700 ++line_count;
2701 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 line_count = 9999;
2704 }
2705
2706 /*
2707 * Scroll up if the cursor is off the bottom of the screen a bit.
2708 * Otherwise put it at 1/2 of the screen.
2709 */
2710 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002711 scroll_cursor_halfway(FALSE, TRUE);
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002712 else if (line_count > 0)
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002713 {
Luuk van Baalaa6ba302023-05-09 16:01:17 +01002714 if (do_sms)
2715 scrollup(scrolled, TRUE); // TODO
2716 else
2717 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * If topline didn't change we need to restore w_botline and w_empty_rows
2722 * (we changed them).
2723 * If topline did change, update_screen() will set botline.
2724 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002725 if (curwin->w_topline == old_topline
2726 && curwin->w_skipcol == old_skipcol
2727 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 curwin->w_botline = old_botline;
2730 curwin->w_empty_rows = old_empty_rows;
2731 curwin->w_valid = old_valid;
2732 }
2733 curwin->w_valid |= VALID_TOPLINE;
2734}
2735
2736/*
2737 * Recompute topline to put the cursor halfway the window
2738 * If "atend" is TRUE, also put it halfway at the end of the file.
2739 */
2740 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002741scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 int above = 0;
2744 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002745 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#ifdef FEAT_DIFF
2747 int topfill = 0;
2748#endif
2749 int below = 0;
2750 int used;
2751 lineoff_T loff;
2752 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002753#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002754 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002757#ifdef FEAT_PROP_POPUP
2758 // if the width changed this needs to be updated first
2759 may_update_popup_position();
2760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2762#ifdef FEAT_FOLDING
2763 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2764#endif
2765#ifdef FEAT_DIFF
2766 used = plines_nofill(loff.lnum);
2767 loff.fill = 0;
2768 boff.fill = 0;
2769#else
2770 used = plines(loff.lnum);
2771#endif
2772 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002773
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002774 int want_height;
Luuk van Baal6c018682023-05-11 18:38:14 +01002775 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
2776 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002777 {
2778 // 'smoothscroll' and 'wrap' are set
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002779 if (atend)
2780 {
2781 want_height = (curwin->w_height - used) / 2;
2782 used = 0;
2783 }
2784 else
2785 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002786 }
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 while (topline > 1)
2789 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002790 // If using smoothscroll, we can precisely scroll to the
2791 // exact point where the cursor is halfway down the screen.
Luuk van Baal6c018682023-05-11 18:38:14 +01002792 if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002793 {
2794 topline_back_winheight(&loff, FALSE);
2795 if (loff.height == MAXCOL)
2796 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002797 used += loff.height;
2798 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002799 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002800 botline_forw(&boff);
2801 used += boff.height;
2802 }
2803 if (used > want_height)
2804 {
2805 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002806 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002807 topline = loff.lnum;
2808#ifdef FEAT_DIFF
2809 topfill = loff.fill;
2810#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002811 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002812 }
2813 break;
2814 }
2815 topline = loff.lnum;
2816#ifdef FEAT_DIFF
2817 topfill = loff.fill;
2818#endif
2819 continue;
2820 }
2821
2822 // If not using smoothscroll, we have to iteratively find how many
2823 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002824 // This may not be right in the middle if the lines'
2825 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002826
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002827 // Depending on "prefer_above" we add a line above or below first.
2828 // Loop twice to avoid duplicating code.
2829 int done = FALSE;
2830 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002832 if (prefer_above ? (round == 2 && below < above)
2833 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002835 // add a line below the cursor
2836 if (boff.lnum < curbuf->b_ml.ml_line_count)
2837 {
2838 botline_forw(&boff);
2839 used += boff.height;
2840 if (used > curwin->w_height)
2841 {
2842 done = TRUE;
2843 break;
2844 }
2845 below += boff.height;
2846 }
2847 else
2848 {
2849 ++below; // count a "~" line
2850 if (atend)
2851 ++used;
2852 }
2853 }
2854
2855 if (prefer_above ? (round == 1 && below >= above)
2856 : (round == 1 && below > above))
2857 {
2858 // add a line above the cursor
2859 topline_back(&loff);
2860 if (loff.height == MAXCOL)
2861 used = MAXCOL;
2862 else
2863 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002865 {
2866 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002868 }
2869 above += loff.height;
2870 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002872 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002876 if (done)
2877 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#ifdef FEAT_FOLDING
2881 if (!hasFolding(topline, &curwin->w_topline, NULL))
2882#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002883 {
2884 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002885 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002886 || curwin->w_skipcol != 0)
2887 {
2888 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002889 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002890 {
2891 curwin->w_skipcol = skipcol;
2892 redraw_later(UPD_NOT_VALID);
2893 }
Luuk van Baal6c018682023-05-11 18:38:14 +01002894 else if (do_sms)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002895 reset_skipcol();
2896 }
2897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898#ifdef FEAT_DIFF
2899 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002900 if (old_topline > curwin->w_topline + curwin->w_height)
2901 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 check_topfill(curwin, FALSE);
2903#endif
2904 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2905 curwin->w_valid |= VALID_TOPLINE;
2906}
2907
2908/*
2909 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002910 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 * If not possible, put it at the same position as scroll_cursor_halfway().
2912 * When called topline must be valid!
2913 */
2914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002915cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 linenr_T botline;
2921 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002924 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
2926 /*
2927 * How many lines we would like to have above/below the cursor depends on
2928 * whether the first/last line of the file is on screen.
2929 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002930 above_wanted = so;
2931 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002932 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
2934 above_wanted = mouse_dragging - 1;
2935 below_wanted = mouse_dragging - 1;
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (curwin->w_topline == 1)
2938 {
2939 above_wanted = 0;
2940 max_off = curwin->w_height / 2;
2941 if (below_wanted > max_off)
2942 below_wanted = max_off;
2943 }
2944 validate_botline();
2945 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002946 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
2948 below_wanted = 0;
2949 max_off = (curwin->w_height - 1) / 2;
2950 if (above_wanted > max_off)
2951 above_wanted = max_off;
2952 }
2953
2954 /*
2955 * If there are sufficient file-lines above and below the cursor, we can
2956 * return now.
2957 */
2958 cln = curwin->w_cursor.lnum;
2959 if (cln >= curwin->w_topline + above_wanted
2960 && cln < curwin->w_botline - below_wanted
2961#ifdef FEAT_FOLDING
2962 && !hasAnyFolding(curwin)
2963#endif
2964 )
2965 return;
2966
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002967 if (curwin->w_p_sms && !curwin->w_p_wrap)
2968 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002969 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002970 if (curwin->w_cline_height == curwin->w_height)
2971 {
2972 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002973 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002974 return;
2975 }
2976 // TODO: If the cursor line doesn't fit in the window then only adjust
2977 // w_skipcol.
2978 }
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /*
2981 * Narrow down the area where the cursor can be put by taking lines from
2982 * the top and the bottom until:
2983 * - the desired context lines are found
2984 * - the lines from the top is past the lines from the bottom
2985 */
2986 topline = curwin->w_topline;
2987 botline = curwin->w_botline - 1;
2988#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002989 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 above = curwin->w_topfill;
2991 below = curwin->w_filler_rows;
2992#endif
2993 while ((above < above_wanted || below < below_wanted) && topline < botline)
2994 {
2995 if (below < below_wanted && (below <= above || above >= above_wanted))
2996 {
2997#ifdef FEAT_FOLDING
2998 if (hasFolding(botline, &botline, NULL))
2999 ++below;
3000 else
3001#endif
3002 below += plines(botline);
3003 --botline;
3004 }
3005 if (above < above_wanted && (above < below || below >= below_wanted))
3006 {
3007#ifdef FEAT_FOLDING
3008 if (hasFolding(topline, NULL, &topline))
3009 ++above;
3010 else
3011#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003012 above += PLINES_NOFILL(topline);
3013#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003014 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (topline < botline)
3016 above += diff_check_fill(curwin, topline + 1);
3017#endif
3018 ++topline;
3019 }
3020 }
3021 if (topline == botline || botline == 0)
3022 curwin->w_cursor.lnum = topline;
3023 else if (topline > botline)
3024 curwin->w_cursor.lnum = botline;
3025 else
3026 {
3027 if (cln < topline && curwin->w_topline > 1)
3028 {
3029 curwin->w_cursor.lnum = topline;
3030 curwin->w_valid &=
3031 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3032 }
3033 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3034 {
3035 curwin->w_cursor.lnum = botline;
3036 curwin->w_valid &=
3037 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3038 }
3039 }
3040 curwin->w_valid |= VALID_TOPLINE;
3041}
3042
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003043static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003046 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3047 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003049 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 */
3051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 long n;
3055 int retval = OK;
3056 lineoff_T loff;
3057 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003058 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
3062 beep_flush();
3063 return FAIL;
3064 }
3065
3066 for ( ; count > 0; --count)
3067 {
3068 validate_botline();
3069 /*
3070 * It's an error to move a page up when the first line is already on
3071 * the screen. It's an error to move a page down when the last line
3072 * is on the screen and the topline is 'scrolloff' lines from the
3073 * last line.
3074 */
3075 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003076 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3078 : (curwin->w_topline == 1
3079#ifdef FEAT_DIFF
3080 && curwin->w_topfill ==
3081 diff_check_fill(curwin, curwin->w_topline)
3082#endif
3083 ))
3084 {
3085 beep_flush();
3086 retval = FAIL;
3087 break;
3088 }
3089
3090#ifdef FEAT_DIFF
3091 loff.fill = 0;
3092#endif
3093 if (dir == FORWARD)
3094 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003095 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003097 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003098 if (p_window <= 2)
3099 ++curwin->w_topline;
3100 else
3101 curwin->w_topline += p_window - 2;
3102 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3103 curwin->w_topline = curbuf->b_ml.ml_line_count;
3104 curwin->w_cursor.lnum = curwin->w_topline;
3105 }
3106 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3107 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003108 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 curwin->w_topline = curbuf->b_ml.ml_line_count;
3110#ifdef FEAT_DIFF
3111 curwin->w_topfill = 0;
3112#endif
3113 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3114 }
3115 else
3116 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003117 // For the overlap, start with the line just below the window
3118 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 loff.lnum = curwin->w_botline;
3120#ifdef FEAT_DIFF
3121 loff.fill = diff_check_fill(curwin, loff.lnum)
3122 - curwin->w_filler_rows;
3123#endif
3124 get_scroll_overlap(&loff, -1);
3125 curwin->w_topline = loff.lnum;
3126#ifdef FEAT_DIFF
3127 curwin->w_topfill = loff.fill;
3128 check_topfill(curwin, FALSE);
3129#endif
3130 curwin->w_cursor.lnum = curwin->w_topline;
3131 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3132 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3133 }
3134 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003135 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137#ifdef FEAT_DIFF
3138 if (curwin->w_topline == 1)
3139 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003140 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 max_topfill();
3142 continue;
3143 }
3144#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003145 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003146 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003147 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003148 if (p_window <= 2)
3149 --curwin->w_topline;
3150 else
3151 curwin->w_topline -= p_window - 2;
3152 if (curwin->w_topline < 1)
3153 curwin->w_topline = 1;
3154 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3155 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3156 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3157 continue;
3158 }
3159
Bram Moolenaar85a20022019-12-21 18:25:54 +01003160 // Find the line at the top of the window that is going to be the
3161 // line at the bottom of the window. Make sure this results in
3162 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 loff.lnum = curwin->w_topline - 1;
3164#ifdef FEAT_DIFF
3165 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3166 - curwin->w_topfill;
3167#endif
3168 get_scroll_overlap(&loff, 1);
3169
3170 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3171 {
3172 loff.lnum = curbuf->b_ml.ml_line_count;
3173#ifdef FEAT_DIFF
3174 loff.fill = 0;
3175 }
3176 else
3177 {
3178 botline_topline(&loff);
3179#endif
3180 }
3181 curwin->w_cursor.lnum = loff.lnum;
3182
Bram Moolenaar85a20022019-12-21 18:25:54 +01003183 // Find the line just above the new topline to get the right line
3184 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 n = 0;
3186 while (n <= curwin->w_height && loff.lnum >= 1)
3187 {
3188 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003189 if (loff.height == MAXCOL)
3190 n = MAXCOL;
3191 else
3192 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003194 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
3196 curwin->w_topline = 1;
3197#ifdef FEAT_DIFF
3198 max_topfill();
3199#endif
3200 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3201 }
3202 else
3203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003204 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#ifdef FEAT_DIFF
3206 topline_botline(&loff);
3207#endif
3208 botline_forw(&loff);
3209 botline_forw(&loff);
3210#ifdef FEAT_DIFF
3211 botline_topline(&loff);
3212#endif
3213#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003214 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3216#endif
3217
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 // Always scroll at least one line. Avoid getting stuck on
3219 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (loff.lnum >= curwin->w_topline
3221#ifdef FEAT_DIFF
3222 && (loff.lnum > curwin->w_topline
3223 || loff.fill >= curwin->w_topfill)
3224#endif
3225 )
3226 {
3227#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003228 // First try using the maximum number of filler lines. If
3229 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 loff.fill = curwin->w_topfill;
3231 if (curwin->w_topfill < diff_check_fill(curwin,
3232 curwin->w_topline))
3233 max_topfill();
3234 if (curwin->w_topfill == loff.fill)
3235#endif
3236 {
3237 --curwin->w_topline;
3238#ifdef FEAT_DIFF
3239 curwin->w_topfill = 0;
3240#endif
3241 }
3242 comp_botline(curwin);
3243 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003244 curwin->w_valid &=
3245 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 else
3248 {
3249 curwin->w_topline = loff.lnum;
3250#ifdef FEAT_DIFF
3251 curwin->w_topfill = loff.fill;
3252 check_topfill(curwin, FALSE);
3253#endif
3254 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3255 }
3256 }
3257 }
3258 }
3259#ifdef FEAT_FOLDING
3260 foldAdjustCursor();
3261#endif
3262 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003263 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003264 if (retval == OK)
3265 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3267
Bram Moolenaar907dad72018-07-10 15:07:15 +02003268 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003270 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3271 // But make sure we scroll at least one line (happens with mix of long
3272 // wrapping lines and non-wrapping line).
3273 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003275 scroll_cursor_top(1, FALSE);
3276 if (curwin->w_topline <= old_topline
3277 && old_topline < curbuf->b_ml.ml_line_count)
3278 {
3279 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003281 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3282#endif
3283 }
3284 }
3285#ifdef FEAT_FOLDING
3286 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003291 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return retval;
3293}
3294
3295/*
3296 * Decide how much overlap to use for page-up or page-down scrolling.
3297 * This is symmetric, so that doing both keeps the same lines displayed.
3298 * Three lines are examined:
3299 *
3300 * before CTRL-F after CTRL-F / before CTRL-B
3301 * etc. l1
3302 * l1 last but one line ------------
3303 * l2 last text line l2 top text line
3304 * ------------- l3 second text line
3305 * l3 etc.
3306 */
3307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003308get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int h1, h2, h3, h4;
3311 int min_height = curwin->w_height - 2;
3312 lineoff_T loff0, loff1, loff2;
3313
3314#ifdef FEAT_DIFF
3315 if (lp->fill > 0)
3316 lp->height = 1;
3317 else
3318 lp->height = plines_nofill(lp->lnum);
3319#else
3320 lp->height = plines(lp->lnum);
3321#endif
3322 h1 = lp->height;
3323 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003324 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
3326 loff0 = *lp;
3327 if (dir > 0)
3328 botline_forw(lp);
3329 else
3330 topline_back(lp);
3331 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003332 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003334 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return;
3336 }
3337
3338 loff1 = *lp;
3339 if (dir > 0)
3340 botline_forw(lp);
3341 else
3342 topline_back(lp);
3343 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003344 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003346 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 return;
3348 }
3349
3350 loff2 = *lp;
3351 if (dir > 0)
3352 botline_forw(lp);
3353 else
3354 topline_back(lp);
3355 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003356 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003357 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003359 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Scroll 'scroll' lines up or down.
3364 */
3365 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003366halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 long scrolled = 0;
3369 int i;
3370 int n;
3371 int room;
3372
3373 if (Prenum)
3374 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3375 curwin->w_height : Prenum;
3376 n = (curwin->w_p_scr <= curwin->w_height) ?
3377 curwin->w_p_scr : curwin->w_height;
3378
Bram Moolenaard5d37532017-03-27 23:02:07 +02003379 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 validate_botline();
3381 room = curwin->w_empty_rows;
3382#ifdef FEAT_DIFF
3383 room += curwin->w_filler_rows;
3384#endif
3385 if (flag)
3386 {
3387 /*
3388 * scroll the text up
3389 */
3390 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3391 {
3392#ifdef FEAT_DIFF
3393 if (curwin->w_topfill > 0)
3394 {
3395 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003396 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 --curwin->w_topfill;
3398 }
3399 else
3400#endif
3401 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003402 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 n -= i;
3404 if (n < 0 && scrolled > 0)
3405 break;
3406#ifdef FEAT_FOLDING
3407 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3408#endif
3409 ++curwin->w_topline;
3410#ifdef FEAT_DIFF
3411 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3412#endif
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3415 {
3416 ++curwin->w_cursor.lnum;
3417 curwin->w_valid &=
3418 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3422 scrolled += i;
3423
3424 /*
3425 * Correct w_botline for changed w_topline.
3426 * Won't work when there are filler lines.
3427 */
3428#ifdef FEAT_DIFF
3429 if (curwin->w_p_diff)
3430 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3431 else
3432#endif
3433 {
3434 room += i;
3435 do
3436 {
3437 i = plines(curwin->w_botline);
3438 if (i > room)
3439 break;
3440#ifdef FEAT_FOLDING
3441 (void)hasFolding(curwin->w_botline, NULL,
3442 &curwin->w_botline);
3443#endif
3444 ++curwin->w_botline;
3445 room -= i;
3446 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3447 }
3448 }
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 /*
3451 * When hit bottom of the file: move cursor down.
3452 */
3453 if (n > 0)
3454 {
3455# ifdef FEAT_FOLDING
3456 if (hasAnyFolding(curwin))
3457 {
3458 while (--n >= 0
3459 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3460 {
3461 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3462 &curwin->w_cursor.lnum);
3463 ++curwin->w_cursor.lnum;
3464 }
3465 }
3466 else
3467# endif
3468 curwin->w_cursor.lnum += n;
3469 check_cursor_lnum();
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 else
3473 {
3474 /*
3475 * scroll the text down
3476 */
3477 while (n > 0 && curwin->w_topline > 1)
3478 {
3479#ifdef FEAT_DIFF
3480 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3481 {
3482 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003483 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 ++curwin->w_topfill;
3485 }
3486 else
3487#endif
3488 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003489 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 n -= i;
3491 if (n < 0 && scrolled > 0)
3492 break;
3493 --curwin->w_topline;
3494#ifdef FEAT_FOLDING
3495 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3496#endif
3497#ifdef FEAT_DIFF
3498 curwin->w_topfill = 0;
3499#endif
3500 }
3501 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3502 VALID_BOTLINE|VALID_BOTLINE_AP);
3503 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (curwin->w_cursor.lnum > 1)
3505 {
3506 --curwin->w_cursor.lnum;
3507 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 /*
3512 * When hit top of the file: move cursor up.
3513 */
3514 if (n > 0)
3515 {
3516 if (curwin->w_cursor.lnum <= (linenr_T)n)
3517 curwin->w_cursor.lnum = 1;
3518 else
3519# ifdef FEAT_FOLDING
3520 if (hasAnyFolding(curwin))
3521 {
3522 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3523 {
3524 --curwin->w_cursor.lnum;
3525 (void)hasFolding(curwin->w_cursor.lnum,
3526 &curwin->w_cursor.lnum, NULL);
3527 }
3528 }
3529 else
3530# endif
3531 curwin->w_cursor.lnum -= n;
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003535 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 foldAdjustCursor();
3537# endif
3538#ifdef FEAT_DIFF
3539 check_topfill(curwin, !flag);
3540#endif
3541 cursor_correct();
3542 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003543 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003545
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003547do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003548{
3549 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003550 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003551 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003552 colnr_T curswant = curwin->w_curswant;
3553 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003554 win_T *old_curwin = curwin;
3555 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003556 int old_VIsual_select = VIsual_select;
3557 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558
3559 /*
3560 * loop through the cursorbound windows
3561 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003562 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003563 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003564 {
3565 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003566 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567 if (curwin != old_curwin && curwin->w_p_crb)
3568 {
3569# ifdef FEAT_DIFF
3570 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003571 curwin->w_cursor.lnum =
3572 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 else
3574# endif
3575 curwin->w_cursor.lnum = line;
3576 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003577 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003578 curwin->w_curswant = curswant;
3579 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003580
Bram Moolenaar85a20022019-12-21 18:25:54 +01003581 // Make sure the cursor is in a valid position. Temporarily set
3582 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003583 int restart_edit_save = restart_edit;
3584 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003586
3587 // Avoid a scroll here for the cursor position, 'scrollbind' is
3588 // more important.
3589 if (!curwin->w_p_scb)
3590 validate_cursor();
3591
Bram Moolenaar61452852011-02-01 18:01:11 +01003592 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003593 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 if (has_mbyte)
3595 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003596 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003597
Bram Moolenaar85a20022019-12-21 18:25:54 +01003598 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003599 if (!curwin->w_p_scb)
3600 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602 }
3603 }
3604
3605 /*
3606 * reset current-window
3607 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003608 VIsual_select = old_VIsual_select;
3609 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 curwin = old_curwin;
3611 curbuf = old_curbuf;
3612}