blob: d35bd693d67b248bbe4ea8cc68aa48823e489440 [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/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001756 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1757 */
1758 static int
1759scrolling_screenlines(int byfold UNUSED)
1760{
1761 return (curwin->w_p_wrap && curwin->w_p_sms)
1762# ifdef FEAT_FOLDING
1763 || (byfold && hasAnyFolding(curwin))
1764# endif
1765# ifdef FEAT_DIFF
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001766 || (curwin->w_p_diff && !curwin->w_p_wrap)
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001767# endif
1768 ;
1769}
1770
1771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775scrollup(
1776 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001779 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001781 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001783 int width1 = curwin->w_width - curwin_col_off();
1784 int width2 = width1 + curwin_col_off2();
1785 int size = 0;
1786 linenr_T prev_topline = curwin->w_topline;
1787
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001788 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001789 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001790
1791 // diff mode: first consume "topfill"
1792 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1793 // the line, then advance to the next line.
1794 // folding: count each sequence of folded lines as one logical line.
1795 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
1797# ifdef FEAT_DIFF
1798 if (curwin->w_topfill > 0)
1799 --curwin->w_topfill;
1800 else
1801# endif
1802 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 linenr_T lnum = curwin->w_topline;
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805# ifdef FEAT_FOLDING
1806 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001807 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 (void)hasFolding(lnum, NULL, &lnum);
1809# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001810 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001811 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001812 // 'smoothscroll': increase "w_skipcol" until it goes over
1813 // the end of the line, then advance to the next line.
1814 int add = curwin->w_skipcol > 0 ? width2 : width1;
1815 curwin->w_skipcol += add;
1816 if (curwin->w_skipcol >= size)
1817 {
1818 if (lnum == curbuf->b_ml.ml_line_count)
1819 {
1820 // at the last screen line, can't scroll further
1821 curwin->w_skipcol -= add;
1822 break;
1823 }
1824 ++lnum;
1825 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001826 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001827 else
1828 {
1829 if (lnum >= curbuf->b_ml.ml_line_count)
1830 break;
1831 ++lnum;
1832 }
1833
1834 if (lnum > curwin->w_topline)
1835 {
1836 // approximate w_botline
1837 curwin->w_botline += lnum - curwin->w_topline;
1838 curwin->w_topline = lnum;
1839# ifdef FEAT_DIFF
1840 curwin->w_topfill = diff_check_fill(curwin, lnum);
1841# endif
1842 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001843 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001844 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001845 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001846 }
1847 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001848
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001849 if (curwin->w_topline == prev_topline)
1850 // need to redraw even though w_topline didn't change
1851 redraw_later(UPD_NOT_VALID);
1852 }
1853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001856 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858
1859 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1860 curwin->w_topline = curbuf->b_ml.ml_line_count;
1861 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1862 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1863
1864#ifdef FEAT_DIFF
1865 check_topfill(curwin, FALSE);
1866#endif
1867
1868#ifdef FEAT_FOLDING
1869 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1872#endif
1873
1874 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1875 if (curwin->w_cursor.lnum < curwin->w_topline)
1876 {
1877 curwin->w_cursor.lnum = curwin->w_topline;
1878 curwin->w_valid &=
1879 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1880 coladvance(curwin->w_curswant);
1881 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001882 if (curwin->w_cursor.lnum == curwin->w_topline
1883 && do_sms && curwin->w_skipcol > 0)
1884 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001885 int col_off = curwin_col_off();
1886 int col_off2 = curwin_col_off2();
1887
1888 int width1 = curwin->w_width - col_off;
1889 int width2 = width1 + col_off2;
1890 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001891 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001892 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001893 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001894
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001895 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1896 // going past it anyway.
1897 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1898 smoothscroll_marker_overlap(extra2);
1899
Bram Moolenaar118c2352022-10-09 17:19:27 +01001900 // Make sure the cursor is in a visible part of the line, taking
1901 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001902 // If there are not enough screen lines put the cursor in the middle.
1903 if (scrolloff_cols > space_cols / 2)
1904 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001906 if (curwin->w_virtcol < curwin->w_skipcol
1907 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001909 colnr_T col = curwin->w_virtcol;
1910
1911 if (col < width1)
1912 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001913 while (col < curwin->w_skipcol
1914 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001915 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001916 curwin->w_curswant = col;
1917 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001918
1919 // validate_virtcol() marked various things as valid, but after
1920 // moving the cursor they need to be recomputed
1921 curwin->w_valid &=
1922 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001923 }
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925}
1926
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001927/*
1928 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1929 * valid for 'smoothscroll'.
1930 */
1931 void
1932adjust_skipcol(void)
1933{
1934 if (!curwin->w_p_wrap
1935 || !curwin->w_p_sms
1936 || curwin->w_cursor.lnum != curwin->w_topline)
1937 return;
1938
1939 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001940 if (width1 <= 0)
1941 return; // no text will be displayed
1942
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001943 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001944 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001945 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1946 int scrolled = FALSE;
1947
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001948 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001949 if (curwin->w_cline_height == curwin->w_height
1950 // w_cline_height may be capped at w_height, check there aren't
1951 // actually more lines.
1952 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1953 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001954 {
1955 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001956 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001957 return;
1958 }
1959
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001960 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001961 while (curwin->w_skipcol > 0
1962 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1963 {
1964 // scroll a screen line down
1965 if (curwin->w_skipcol >= width1 + width2)
1966 curwin->w_skipcol -= width2;
1967 else
1968 curwin->w_skipcol -= width1;
1969 redraw_later(UPD_NOT_VALID);
1970 scrolled = TRUE;
1971 validate_virtcol();
1972 }
1973 if (scrolled)
1974 return; // don't scroll in the other direction now
1975
1976 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1977 int row = 0;
1978 if (col >= width1)
1979 {
1980 col -= width1;
1981 ++row;
1982 }
1983 if (col > width2)
1984 {
1985 row += col / width2;
1986 col = col % width2;
1987 }
1988 if (row >= curwin->w_height)
1989 {
1990 if (curwin->w_skipcol == 0)
1991 {
1992 curwin->w_skipcol += width1;
1993 --row;
1994 }
1995 if (row >= curwin->w_height)
1996 curwin->w_skipcol += (row - curwin->w_height) * width2;
1997 redraw_later(UPD_NOT_VALID);
1998 }
1999}
2000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#ifdef FEAT_DIFF
2002/*
2003 * Don't end up with too many filler lines in the window.
2004 */
2005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006check_topfill(
2007 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002008 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 int n;
2011
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002012 if (wp->w_topfill <= 0)
2013 return;
2014
2015 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2016 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002018 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 --wp->w_topline;
2021 wp->w_topfill = 0;
2022 }
2023 else
2024 {
2025 wp->w_topfill = wp->w_height - n;
2026 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 }
2030}
2031
2032/*
2033 * Use as many filler lines as possible for w_topline. Make sure w_topline
2034 * is still visible.
2035 */
2036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int n;
2040
2041 n = plines_nofill(curwin->w_topline);
2042 if (n >= curwin->w_height)
2043 curwin->w_topfill = 0;
2044 else
2045 {
2046 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2047 if (curwin->w_topfill + n > curwin->w_height)
2048 curwin->w_topfill = curwin->w_height - n;
2049 }
2050}
2051#endif
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053/*
2054 * Scroll the screen one line down, but don't do it if it would move the
2055 * cursor off the screen.
2056 */
2057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int end_row;
2061#ifdef FEAT_DIFF
2062 int can_fill = (curwin->w_topfill
2063 < diff_check_fill(curwin, curwin->w_topline));
2064#endif
2065
2066 if (curwin->w_topline <= 1
2067#ifdef FEAT_DIFF
2068 && !can_fill
2069#endif
2070 )
2071 return;
2072
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 /*
2076 * Compute the row number of the last row of the cursor line
2077 * and make sure it doesn't go off the screen. Make sure the cursor
2078 * doesn't go past 'scrolloff' lines from the screen end.
2079 */
2080 end_row = curwin->w_wrow;
2081#ifdef FEAT_DIFF
2082 if (can_fill)
2083 ++end_row;
2084 else
2085 end_row += plines_nofill(curwin->w_topline - 1);
2086#else
2087 end_row += plines(curwin->w_topline - 1);
2088#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002089 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 validate_cheight();
2092 validate_virtcol();
2093 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002094 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002096 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
2098#ifdef FEAT_DIFF
2099 if (can_fill)
2100 {
2101 ++curwin->w_topfill;
2102 check_topfill(curwin, TRUE);
2103 }
2104 else
2105 {
2106 --curwin->w_topline;
2107 curwin->w_topfill = 0;
2108 }
2109#else
2110 --curwin->w_topline;
2111#endif
2112#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002113 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002115 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2117 }
2118}
2119
2120/*
2121 * Scroll the screen one line up, but don't do it if it would move the cursor
2122 * off the screen.
2123 */
2124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002125scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int start_row;
2128
2129 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2130#ifdef FEAT_DIFF
2131 && curwin->w_topfill == 0
2132#endif
2133 )
2134 return;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 /*
2139 * Compute the row number of the first row of the cursor line
2140 * and make sure it doesn't go off the screen. Make sure the cursor
2141 * doesn't go before 'scrolloff' lines from the screen start.
2142 */
2143#ifdef FEAT_DIFF
2144 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2145 - curwin->w_topfill;
2146#else
2147 start_row = curwin->w_wrow - plines(curwin->w_topline);
2148#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002149 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002152 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002154 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156#ifdef FEAT_DIFF
2157 if (curwin->w_topfill > 0)
2158 --curwin->w_topfill;
2159 else
2160#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002161 {
2162#ifdef FEAT_FOLDING
2163 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002166 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2169 }
2170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172/*
2173 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2174 * a (wrapped) text line. Uses and sets "lp->fill".
2175 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002176 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002179topline_back_winheight(
2180 lineoff_T *lp,
2181 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183#ifdef FEAT_DIFF
2184 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2185 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ++lp->fill;
2188 lp->height = 1;
2189 }
2190 else
2191#endif
2192 {
2193 --lp->lnum;
2194#ifdef FEAT_DIFF
2195 lp->fill = 0;
2196#endif
2197 if (lp->lnum < 1)
2198 lp->height = MAXCOL;
2199 else
2200#ifdef FEAT_FOLDING
2201 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 lp->height = 1;
2204 else
2205#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002206 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208}
2209
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002210 static void
2211topline_back(lineoff_T *lp)
2212{
2213 topline_back_winheight(lp, TRUE);
2214}
2215
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2219 * a (wrapped) text line. Uses and sets "lp->fill".
2220 * Returns the height of the added line in "lp->height".
2221 * Lines below the last one are incredibly high.
2222 */
2223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002224botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226#ifdef FEAT_DIFF
2227 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 ++lp->fill;
2231 lp->height = 1;
2232 }
2233 else
2234#endif
2235 {
2236 ++lp->lnum;
2237#ifdef FEAT_DIFF
2238 lp->fill = 0;
2239#endif
2240 if (lp->lnum > curbuf->b_ml.ml_line_count)
2241 lp->height = MAXCOL;
2242 else
2243#ifdef FEAT_FOLDING
2244 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 lp->height = 1;
2247 else
2248#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002249 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251}
2252
2253#ifdef FEAT_DIFF
2254/*
2255 * Switch from including filler lines below lp->lnum to including filler
2256 * lines above loff.lnum + 1. This keeps pointing to the same line.
2257 * When there are no filler lines nothing changes.
2258 */
2259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002260botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 if (lp->fill > 0)
2263 {
2264 ++lp->lnum;
2265 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2266 }
2267}
2268
2269/*
2270 * Switch from including filler lines above lp->lnum to including filler
2271 * lines below loff.lnum - 1. This keeps pointing to the same line.
2272 * When there are no filler lines nothing changes.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 if (lp->fill > 0)
2278 {
2279 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2280 --lp->lnum;
2281 }
2282}
2283#endif
2284
2285/*
2286 * Recompute topline to put the cursor at the top of the window.
2287 * Scroll at least "min_scroll" lines.
2288 * If "always" is TRUE, always set topline (for "zt").
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 int scrolled = 0;
2294 int extra = 0;
2295 int used;
2296 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 linenr_T top; // just above displayed lines
2298 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002300 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_DIFF
2302 linenr_T old_topfill = curwin->w_topfill;
2303#endif
2304 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002305 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (mouse_dragging > 0)
2308 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /*
2311 * Decrease topline until:
2312 * - it has become 1
2313 * - (part of) the cursor line is moved off the screen or
2314 * - moved at least 'scrolljump' lines and
2315 * - at least 'scrolloff' lines above and below the cursor
2316 */
2317 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (curwin->w_cursor.lnum < curwin->w_topline)
2320 scrolled = used;
2321
2322#ifdef FEAT_FOLDING
2323 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2324 {
2325 --top;
2326 ++bot;
2327 }
2328 else
2329#endif
2330 {
2331 top = curwin->w_cursor.lnum - 1;
2332 bot = curwin->w_cursor.lnum + 1;
2333 }
2334 new_topline = top + 1;
2335
2336#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 // "used" already contains the number of filler lines above, don't add it
2338 // again.
2339 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002340 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342
2343 /*
2344 * Check if the lines from "top" to "bot" fit in the window. If they do,
2345 * set new_topline and advance "top" and "bot" to include more lines.
2346 */
2347 while (top > 0)
2348 {
2349#ifdef FEAT_FOLDING
2350 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 i = 1;
2353 else
2354#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002355 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002356 if (top < curwin->w_topline)
2357 scrolled += i;
2358
2359 // If scrolling is needed, scroll at least 'sj' lines.
2360 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2361 && extra >= off)
2362 break;
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 used += i;
2365 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2366 {
2367#ifdef FEAT_FOLDING
2368 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 ++used;
2371 else
2372#endif
2373 used += plines(bot);
2374 }
2375 if (used > curwin->w_height)
2376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 extra += i;
2379 new_topline = top;
2380 --top;
2381 ++bot;
2382 }
2383
2384 /*
2385 * If we don't have enough space, put cursor in the middle.
2386 * This makes sure we get the same position when using "k" and "j"
2387 * in a small window.
2388 */
2389 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002390 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 {
2393 /*
2394 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002395 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 if (new_topline < curwin->w_topline || always)
2398 curwin->w_topline = new_topline;
2399 if (curwin->w_topline > curwin->w_cursor.lnum)
2400 curwin->w_topline = curwin->w_cursor.lnum;
2401#ifdef FEAT_DIFF
2402 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2403 if (curwin->w_topfill > 0 && extra > off)
2404 {
2405 curwin->w_topfill -= extra - off;
2406 if (curwin->w_topfill < 0)
2407 curwin->w_topfill = 0;
2408 }
2409 check_topfill(curwin, FALSE);
2410#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002411 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002412 if (curwin->w_topline == curwin->w_cursor.lnum
2413 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002414 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002416 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#ifdef FEAT_DIFF
2418 || curwin->w_topfill != old_topfill
2419#endif
2420 )
2421 curwin->w_valid &=
2422 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2423 curwin->w_valid |= VALID_TOPLINE;
2424 }
2425}
2426
2427/*
2428 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2429 * screen lines for text lines.
2430 */
2431 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002432set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434#ifdef FEAT_DIFF
2435 wp->w_filler_rows = 0;
2436#endif
2437 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 else
2440 {
2441 wp->w_empty_rows = wp->w_height - used;
2442#ifdef FEAT_DIFF
2443 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2444 {
2445 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2446 if (wp->w_empty_rows > wp->w_filler_rows)
2447 wp->w_empty_rows -= wp->w_filler_rows;
2448 else
2449 {
2450 wp->w_filler_rows = wp->w_empty_rows;
2451 wp->w_empty_rows = 0;
2452 }
2453 }
2454#endif
2455 }
2456}
2457
2458/*
2459 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002460 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2462 * This is messy stuff!!!
2463 */
2464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 int used;
2468 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002469 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 int extra = 0;
2471 int i;
2472 linenr_T line_count;
2473 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002474 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 lineoff_T loff;
2476 lineoff_T boff;
2477#ifdef FEAT_DIFF
2478 int old_topfill = curwin->w_topfill;
2479 int fill_below_window;
2480#endif
2481 linenr_T old_botline = curwin->w_botline;
2482 linenr_T old_valid = curwin->w_valid;
2483 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002484 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002485 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 cln = curwin->w_cursor.lnum;
2488 if (set_topbot)
2489 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002490 int set_skipcol = FALSE;
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 used = 0;
2493 curwin->w_botline = cln + 1;
2494#ifdef FEAT_DIFF
2495 loff.fill = 0;
2496#endif
2497 for (curwin->w_topline = curwin->w_botline;
2498 curwin->w_topline > 1;
2499 curwin->w_topline = loff.lnum)
2500 {
2501 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 topline_back_winheight(&loff, FALSE);
2503 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002505 if (used + loff.height > curwin->w_height)
2506 {
2507 if (curwin->w_p_sms && curwin->w_p_wrap)
2508 {
2509 // 'smoothscroll' and 'wrap' are set. The above line is
2510 // too long to show in its entirety, so we show just a part
2511 // of it.
2512 if (used < curwin->w_height)
2513 {
2514 int plines_offset = used + loff.height
2515 - curwin->w_height;
2516 used = curwin->w_height;
2517#ifdef FEAT_DIFF
2518 curwin->w_topfill = loff.fill;
2519#endif
2520 curwin->w_topline = loff.lnum;
2521 curwin->w_skipcol = skipcol_from_plines(
2522 curwin, plines_offset);
2523 set_skipcol = TRUE;
2524 }
2525 }
2526 break;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 used += loff.height;
2529#ifdef FEAT_DIFF
2530 curwin->w_topfill = loff.fill;
2531#endif
2532 }
2533 set_empty_rows(curwin, used);
2534 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2535 if (curwin->w_topline != old_topline
2536#ifdef FEAT_DIFF
2537 || curwin->w_topfill != old_topfill
2538#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002539 || set_skipcol
2540 || curwin->w_skipcol != 0)
2541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002543 if (set_skipcol)
2544 redraw_later(UPD_NOT_VALID);
2545 else
2546 reset_skipcol();
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 validate_botline();
2551
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#ifdef FEAT_DIFF
2554 used = plines_nofill(cln);
2555#else
2556 validate_cheight();
2557 used = curwin->w_cline_height;
2558#endif
2559
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002560 // If the cursor is on or below botline, we will at least scroll by the
2561 // height of the cursor line, which is "used". Correct for empty lines,
2562 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (cln >= curwin->w_botline)
2564 {
2565 scrolled = used;
2566 if (cln == curwin->w_botline)
2567 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002568 min_scrolled = scrolled;
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002569 if (curwin->w_p_sms && curwin->w_p_wrap)
2570 {
2571 // 'smoothscroll' and 'wrap' are set
2572 if (cln > curwin->w_botline)
2573 // add screen lines below w_botline
2574 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2575 min_scrolled += PLINES_NOFILL(lnum);
2576
2577 // Calculate how many screen lines the current top line of window
2578 // occupies. If it is occupying more than the entire window, we
2579 // need to scroll the additional clipped lines to scroll past the
2580 // top line before we can move on to the other lines.
2581 int top_plines =
2582#ifdef FEAT_DIFF
2583 plines_win_nofill
2584#else
2585 plines_win
2586#endif
2587 (curwin, curwin->w_topline, FALSE);
2588 int skip_lines = 0;
2589 int width1 = curwin->w_width - curwin_col_off();
2590 int width2 = width1 + curwin_col_off2();
2591 // similar formula is used in curs_columns()
2592 if (curwin->w_skipcol > width1)
2593 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2594 else if (curwin->w_skipcol > 0)
2595 skip_lines = 1;
2596
2597 top_plines -= skip_lines;
2598 if (top_plines > curwin->w_height)
2599 {
2600 scrolled += (top_plines - curwin->w_height);
2601 min_scrolled += (top_plines - curwin->w_height);
2602 }
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
2606 /*
2607 * Stop counting lines to scroll when
2608 * - hitting start of the file
2609 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002610 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 * - lines between botline and cursor have been counted
2612 */
2613#ifdef FEAT_FOLDING
2614 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2615#endif
2616 {
2617 loff.lnum = cln;
2618 boff.lnum = cln;
2619 }
2620#ifdef FEAT_DIFF
2621 loff.fill = 0;
2622 boff.fill = 0;
2623 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2624 - curwin->w_filler_rows;
2625#endif
2626
2627 while (loff.lnum > 1)
2628 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2630 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002632 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2634 && 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 break;
2641
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002644 if (loff.height == MAXCOL)
2645 used = MAXCOL;
2646 else
2647 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (used > curwin->w_height)
2649 break;
2650 if (loff.lnum >= curwin->w_botline
2651#ifdef FEAT_DIFF
2652 && (loff.lnum > curwin->w_botline
2653 || loff.fill <= fill_below_window)
2654#endif
2655 )
2656 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 scrolled += loff.height;
2659 if (loff.lnum == curwin->w_botline
2660#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002661 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663 )
2664 scrolled -= curwin->w_empty_rows;
2665 }
2666
2667 if (boff.lnum < curbuf->b_ml.ml_line_count)
2668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 botline_forw(&boff);
2671 used += boff.height;
2672 if (used > curwin->w_height)
2673 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002674 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2675 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 extra += boff.height;
2678 if (boff.lnum >= curwin->w_botline
2679#ifdef FEAT_DIFF
2680 || (boff.lnum + 1 == curwin->w_botline
2681 && boff.fill > curwin->w_filler_rows)
2682#endif
2683 )
2684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 scrolled += boff.height;
2687 if (boff.lnum == curwin->w_botline
2688#ifdef FEAT_DIFF
2689 && boff.fill == 0
2690#endif
2691 )
2692 scrolled -= curwin->w_empty_rows;
2693 }
2694 }
2695 }
2696 }
2697
Bram Moolenaar85a20022019-12-21 18:25:54 +01002698 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (scrolled <= 0)
2700 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 else if (used > curwin->w_height)
2703 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002704 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
2706 {
2707 line_count = 0;
2708#ifdef FEAT_DIFF
2709 boff.fill = curwin->w_topfill;
2710#endif
2711 boff.lnum = curwin->w_topline - 1;
2712 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2713 {
2714 botline_forw(&boff);
2715 i += boff.height;
2716 ++line_count;
2717 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 line_count = 9999;
2720 }
2721
2722 /*
2723 * Scroll up if the cursor is off the bottom of the screen a bit.
2724 * Otherwise put it at 1/2 of the screen.
2725 */
2726 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002727 scroll_cursor_halfway(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002729 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002730 // With 'smoothscroll' scroll at least the height of the cursor line,
2731 // unless it would move the cursor.
2732 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2733 && (curwin->w_cursor.lnum < curwin->w_topline
2734 || (curwin->w_virtcol - curwin->w_skipcol >=
2735 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002736 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002737 if (line_count > 0)
2738 {
2739 if (scrolling_screenlines(TRUE))
2740 scrollup(scrolled, TRUE); // TODO
2741 else
2742 scrollup(line_count, TRUE);
2743 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
2747 * If topline didn't change we need to restore w_botline and w_empty_rows
2748 * (we changed them).
2749 * If topline did change, update_screen() will set botline.
2750 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002751 if (curwin->w_topline == old_topline
2752 && curwin->w_skipcol == old_skipcol
2753 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
2755 curwin->w_botline = old_botline;
2756 curwin->w_empty_rows = old_empty_rows;
2757 curwin->w_valid = old_valid;
2758 }
2759 curwin->w_valid |= VALID_TOPLINE;
2760}
2761
2762/*
2763 * Recompute topline to put the cursor halfway the window
2764 * If "atend" is TRUE, also put it halfway at the end of the file.
2765 */
2766 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002767scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 int above = 0;
2770 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002771 colnr_T skipcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#ifdef FEAT_DIFF
2773 int topfill = 0;
2774#endif
2775 int below = 0;
2776 int used;
2777 lineoff_T loff;
2778 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002779#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002780 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002783#ifdef FEAT_PROP_POPUP
2784 // if the width changed this needs to be updated first
2785 may_update_popup_position();
2786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2788#ifdef FEAT_FOLDING
2789 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2790#endif
2791#ifdef FEAT_DIFF
2792 used = plines_nofill(loff.lnum);
2793 loff.fill = 0;
2794 boff.fill = 0;
2795#else
2796 used = plines(loff.lnum);
2797#endif
2798 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002799
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002800 int want_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002801 int smooth_scroll = FALSE;
2802 if (curwin->w_p_sms && curwin->w_p_wrap)
2803 {
2804 // 'smoothscroll' and 'wrap' are set
2805 smooth_scroll = TRUE;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002806 if (atend)
2807 {
2808 want_height = (curwin->w_height - used) / 2;
2809 used = 0;
2810 }
2811 else
2812 want_height = curwin->w_height;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002813 }
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 while (topline > 1)
2816 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002817 // If using smoothscroll, we can precisely scroll to the
2818 // exact point where the cursor is halfway down the screen.
2819 if (smooth_scroll)
2820 {
2821 topline_back_winheight(&loff, FALSE);
2822 if (loff.height == MAXCOL)
2823 break;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002824 used += loff.height;
2825 if (!atend && boff.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002826 {
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002827 botline_forw(&boff);
2828 used += boff.height;
2829 }
2830 if (used > want_height)
2831 {
2832 if (used - loff.height < want_height)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002833 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002834 topline = loff.lnum;
2835#ifdef FEAT_DIFF
2836 topfill = loff.fill;
2837#endif
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002838 skipcol = skipcol_from_plines(curwin, used - want_height);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002839 }
2840 break;
2841 }
2842 topline = loff.lnum;
2843#ifdef FEAT_DIFF
2844 topfill = loff.fill;
2845#endif
2846 continue;
2847 }
2848
2849 // If not using smoothscroll, we have to iteratively find how many
2850 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002851 // This may not be right in the middle if the lines'
2852 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002853
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002854 // Depending on "prefer_above" we add a line above or below first.
2855 // Loop twice to avoid duplicating code.
2856 int done = FALSE;
2857 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002859 if (prefer_above ? (round == 2 && below < above)
2860 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002862 // add a line below the cursor
2863 if (boff.lnum < curbuf->b_ml.ml_line_count)
2864 {
2865 botline_forw(&boff);
2866 used += boff.height;
2867 if (used > curwin->w_height)
2868 {
2869 done = TRUE;
2870 break;
2871 }
2872 below += boff.height;
2873 }
2874 else
2875 {
2876 ++below; // count a "~" line
2877 if (atend)
2878 ++used;
2879 }
2880 }
2881
2882 if (prefer_above ? (round == 1 && below >= above)
2883 : (round == 1 && below > above))
2884 {
2885 // add a line above the cursor
2886 topline_back(&loff);
2887 if (loff.height == MAXCOL)
2888 used = MAXCOL;
2889 else
2890 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002892 {
2893 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002895 }
2896 above += loff.height;
2897 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002899 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002903 if (done)
2904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#ifdef FEAT_FOLDING
2908 if (!hasFolding(topline, &curwin->w_topline, NULL))
2909#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002910 {
2911 if (curwin->w_topline != topline
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002912 || skipcol != 0
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002913 || curwin->w_skipcol != 0)
2914 {
2915 curwin->w_topline = topline;
Luuk van Baal3ce8c382023-05-08 15:51:14 +01002916 if (skipcol != 0)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002917 {
2918 curwin->w_skipcol = skipcol;
2919 redraw_later(UPD_NOT_VALID);
2920 }
2921 else
2922 reset_skipcol();
2923 }
2924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#ifdef FEAT_DIFF
2926 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002927 if (old_topline > curwin->w_topline + curwin->w_height)
2928 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 check_topfill(curwin, FALSE);
2930#endif
2931 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2932 curwin->w_valid |= VALID_TOPLINE;
2933}
2934
2935/*
2936 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002937 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 * If not possible, put it at the same position as scroll_cursor_halfway().
2939 * When called topline must be valid!
2940 */
2941 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002942cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002944 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 linenr_T botline;
2948 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002949 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002951 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
2953 /*
2954 * How many lines we would like to have above/below the cursor depends on
2955 * whether the first/last line of the file is on screen.
2956 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002957 above_wanted = so;
2958 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002959 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 above_wanted = mouse_dragging - 1;
2962 below_wanted = mouse_dragging - 1;
2963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (curwin->w_topline == 1)
2965 {
2966 above_wanted = 0;
2967 max_off = curwin->w_height / 2;
2968 if (below_wanted > max_off)
2969 below_wanted = max_off;
2970 }
2971 validate_botline();
2972 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002973 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
2975 below_wanted = 0;
2976 max_off = (curwin->w_height - 1) / 2;
2977 if (above_wanted > max_off)
2978 above_wanted = max_off;
2979 }
2980
2981 /*
2982 * If there are sufficient file-lines above and below the cursor, we can
2983 * return now.
2984 */
2985 cln = curwin->w_cursor.lnum;
2986 if (cln >= curwin->w_topline + above_wanted
2987 && cln < curwin->w_botline - below_wanted
2988#ifdef FEAT_FOLDING
2989 && !hasAnyFolding(curwin)
2990#endif
2991 )
2992 return;
2993
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002994 if (curwin->w_p_sms && !curwin->w_p_wrap)
2995 {
Luuk van Baalc8502f92023-05-06 12:40:15 +01002996 // 'smoothscroll' is active
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002997 if (curwin->w_cline_height == curwin->w_height)
2998 {
2999 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00003000 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003001 return;
3002 }
3003 // TODO: If the cursor line doesn't fit in the window then only adjust
3004 // w_skipcol.
3005 }
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 /*
3008 * Narrow down the area where the cursor can be put by taking lines from
3009 * the top and the bottom until:
3010 * - the desired context lines are found
3011 * - the lines from the top is past the lines from the bottom
3012 */
3013 topline = curwin->w_topline;
3014 botline = curwin->w_botline - 1;
3015#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003016 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 above = curwin->w_topfill;
3018 below = curwin->w_filler_rows;
3019#endif
3020 while ((above < above_wanted || below < below_wanted) && topline < botline)
3021 {
3022 if (below < below_wanted && (below <= above || above >= above_wanted))
3023 {
3024#ifdef FEAT_FOLDING
3025 if (hasFolding(botline, &botline, NULL))
3026 ++below;
3027 else
3028#endif
3029 below += plines(botline);
3030 --botline;
3031 }
3032 if (above < above_wanted && (above < below || below >= below_wanted))
3033 {
3034#ifdef FEAT_FOLDING
3035 if (hasFolding(topline, NULL, &topline))
3036 ++above;
3037 else
3038#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003039 above += PLINES_NOFILL(topline);
3040#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003041 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (topline < botline)
3043 above += diff_check_fill(curwin, topline + 1);
3044#endif
3045 ++topline;
3046 }
3047 }
3048 if (topline == botline || botline == 0)
3049 curwin->w_cursor.lnum = topline;
3050 else if (topline > botline)
3051 curwin->w_cursor.lnum = botline;
3052 else
3053 {
3054 if (cln < topline && curwin->w_topline > 1)
3055 {
3056 curwin->w_cursor.lnum = topline;
3057 curwin->w_valid &=
3058 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3059 }
3060 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3061 {
3062 curwin->w_cursor.lnum = botline;
3063 curwin->w_valid &=
3064 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3065 }
3066 }
3067 curwin->w_valid |= VALID_TOPLINE;
3068}
3069
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003070static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
3072/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003073 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3074 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003076 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 */
3078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 long n;
3082 int retval = OK;
3083 lineoff_T loff;
3084 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003085 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
Bram Moolenaar85a20022019-12-21 18:25:54 +01003087 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 {
3089 beep_flush();
3090 return FAIL;
3091 }
3092
3093 for ( ; count > 0; --count)
3094 {
3095 validate_botline();
3096 /*
3097 * It's an error to move a page up when the first line is already on
3098 * the screen. It's an error to move a page down when the last line
3099 * is on the screen and the topline is 'scrolloff' lines from the
3100 * last line.
3101 */
3102 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003103 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3105 : (curwin->w_topline == 1
3106#ifdef FEAT_DIFF
3107 && curwin->w_topfill ==
3108 diff_check_fill(curwin, curwin->w_topline)
3109#endif
3110 ))
3111 {
3112 beep_flush();
3113 retval = FAIL;
3114 break;
3115 }
3116
3117#ifdef FEAT_DIFF
3118 loff.fill = 0;
3119#endif
3120 if (dir == FORWARD)
3121 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003122 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003124 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003125 if (p_window <= 2)
3126 ++curwin->w_topline;
3127 else
3128 curwin->w_topline += p_window - 2;
3129 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3130 curwin->w_topline = curbuf->b_ml.ml_line_count;
3131 curwin->w_cursor.lnum = curwin->w_topline;
3132 }
3133 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003135 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 curwin->w_topline = curbuf->b_ml.ml_line_count;
3137#ifdef FEAT_DIFF
3138 curwin->w_topfill = 0;
3139#endif
3140 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3141 }
3142 else
3143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 // For the overlap, start with the line just below the window
3145 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 loff.lnum = curwin->w_botline;
3147#ifdef FEAT_DIFF
3148 loff.fill = diff_check_fill(curwin, loff.lnum)
3149 - curwin->w_filler_rows;
3150#endif
3151 get_scroll_overlap(&loff, -1);
3152 curwin->w_topline = loff.lnum;
3153#ifdef FEAT_DIFF
3154 curwin->w_topfill = loff.fill;
3155 check_topfill(curwin, FALSE);
3156#endif
3157 curwin->w_cursor.lnum = curwin->w_topline;
3158 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3159 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3160 }
3161 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003162 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164#ifdef FEAT_DIFF
3165 if (curwin->w_topline == 1)
3166 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003167 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 max_topfill();
3169 continue;
3170 }
3171#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003172 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003174 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003175 if (p_window <= 2)
3176 --curwin->w_topline;
3177 else
3178 curwin->w_topline -= p_window - 2;
3179 if (curwin->w_topline < 1)
3180 curwin->w_topline = 1;
3181 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3182 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3183 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3184 continue;
3185 }
3186
Bram Moolenaar85a20022019-12-21 18:25:54 +01003187 // Find the line at the top of the window that is going to be the
3188 // line at the bottom of the window. Make sure this results in
3189 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 loff.lnum = curwin->w_topline - 1;
3191#ifdef FEAT_DIFF
3192 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3193 - curwin->w_topfill;
3194#endif
3195 get_scroll_overlap(&loff, 1);
3196
3197 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3198 {
3199 loff.lnum = curbuf->b_ml.ml_line_count;
3200#ifdef FEAT_DIFF
3201 loff.fill = 0;
3202 }
3203 else
3204 {
3205 botline_topline(&loff);
3206#endif
3207 }
3208 curwin->w_cursor.lnum = loff.lnum;
3209
Bram Moolenaar85a20022019-12-21 18:25:54 +01003210 // Find the line just above the new topline to get the right line
3211 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 n = 0;
3213 while (n <= curwin->w_height && loff.lnum >= 1)
3214 {
3215 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003216 if (loff.height == MAXCOL)
3217 n = MAXCOL;
3218 else
3219 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003221 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
3223 curwin->w_topline = 1;
3224#ifdef FEAT_DIFF
3225 max_topfill();
3226#endif
3227 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3228 }
3229 else
3230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#ifdef FEAT_DIFF
3233 topline_botline(&loff);
3234#endif
3235 botline_forw(&loff);
3236 botline_forw(&loff);
3237#ifdef FEAT_DIFF
3238 botline_topline(&loff);
3239#endif
3240#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003241 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3243#endif
3244
Bram Moolenaar85a20022019-12-21 18:25:54 +01003245 // Always scroll at least one line. Avoid getting stuck on
3246 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (loff.lnum >= curwin->w_topline
3248#ifdef FEAT_DIFF
3249 && (loff.lnum > curwin->w_topline
3250 || loff.fill >= curwin->w_topfill)
3251#endif
3252 )
3253 {
3254#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003255 // First try using the maximum number of filler lines. If
3256 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 loff.fill = curwin->w_topfill;
3258 if (curwin->w_topfill < diff_check_fill(curwin,
3259 curwin->w_topline))
3260 max_topfill();
3261 if (curwin->w_topfill == loff.fill)
3262#endif
3263 {
3264 --curwin->w_topline;
3265#ifdef FEAT_DIFF
3266 curwin->w_topfill = 0;
3267#endif
3268 }
3269 comp_botline(curwin);
3270 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003271 curwin->w_valid &=
3272 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 else
3275 {
3276 curwin->w_topline = loff.lnum;
3277#ifdef FEAT_DIFF
3278 curwin->w_topfill = loff.fill;
3279 check_topfill(curwin, FALSE);
3280#endif
3281 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3282 }
3283 }
3284 }
3285 }
3286#ifdef FEAT_FOLDING
3287 foldAdjustCursor();
3288#endif
3289 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003290 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003291 if (retval == OK)
3292 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3294
Bram Moolenaar907dad72018-07-10 15:07:15 +02003295 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003297 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3298 // But make sure we scroll at least one line (happens with mix of long
3299 // wrapping lines and non-wrapping line).
3300 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003302 scroll_cursor_top(1, FALSE);
3303 if (curwin->w_topline <= old_topline
3304 && old_topline < curbuf->b_ml.ml_line_count)
3305 {
3306 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003308 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3309#endif
3310 }
3311 }
3312#ifdef FEAT_FOLDING
3313 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003318 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return retval;
3320}
3321
3322/*
3323 * Decide how much overlap to use for page-up or page-down scrolling.
3324 * This is symmetric, so that doing both keeps the same lines displayed.
3325 * Three lines are examined:
3326 *
3327 * before CTRL-F after CTRL-F / before CTRL-B
3328 * etc. l1
3329 * l1 last but one line ------------
3330 * l2 last text line l2 top text line
3331 * ------------- l3 second text line
3332 * l3 etc.
3333 */
3334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003335get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 int h1, h2, h3, h4;
3338 int min_height = curwin->w_height - 2;
3339 lineoff_T loff0, loff1, loff2;
3340
3341#ifdef FEAT_DIFF
3342 if (lp->fill > 0)
3343 lp->height = 1;
3344 else
3345 lp->height = plines_nofill(lp->lnum);
3346#else
3347 lp->height = plines(lp->lnum);
3348#endif
3349 h1 = lp->height;
3350 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003351 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
3353 loff0 = *lp;
3354 if (dir > 0)
3355 botline_forw(lp);
3356 else
3357 topline_back(lp);
3358 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003359 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003361 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 return;
3363 }
3364
3365 loff1 = *lp;
3366 if (dir > 0)
3367 botline_forw(lp);
3368 else
3369 topline_back(lp);
3370 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003371 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003373 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return;
3375 }
3376
3377 loff2 = *lp;
3378 if (dir > 0)
3379 botline_forw(lp);
3380 else
3381 topline_back(lp);
3382 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003383 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003384 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387}
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389/*
3390 * Scroll 'scroll' lines up or down.
3391 */
3392 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003393halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 long scrolled = 0;
3396 int i;
3397 int n;
3398 int room;
3399
3400 if (Prenum)
3401 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3402 curwin->w_height : Prenum;
3403 n = (curwin->w_p_scr <= curwin->w_height) ?
3404 curwin->w_p_scr : curwin->w_height;
3405
Bram Moolenaard5d37532017-03-27 23:02:07 +02003406 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 validate_botline();
3408 room = curwin->w_empty_rows;
3409#ifdef FEAT_DIFF
3410 room += curwin->w_filler_rows;
3411#endif
3412 if (flag)
3413 {
3414 /*
3415 * scroll the text up
3416 */
3417 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3418 {
3419#ifdef FEAT_DIFF
3420 if (curwin->w_topfill > 0)
3421 {
3422 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003423 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 --curwin->w_topfill;
3425 }
3426 else
3427#endif
3428 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003429 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 n -= i;
3431 if (n < 0 && scrolled > 0)
3432 break;
3433#ifdef FEAT_FOLDING
3434 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3435#endif
3436 ++curwin->w_topline;
3437#ifdef FEAT_DIFF
3438 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3439#endif
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3442 {
3443 ++curwin->w_cursor.lnum;
3444 curwin->w_valid &=
3445 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3449 scrolled += i;
3450
3451 /*
3452 * Correct w_botline for changed w_topline.
3453 * Won't work when there are filler lines.
3454 */
3455#ifdef FEAT_DIFF
3456 if (curwin->w_p_diff)
3457 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3458 else
3459#endif
3460 {
3461 room += i;
3462 do
3463 {
3464 i = plines(curwin->w_botline);
3465 if (i > room)
3466 break;
3467#ifdef FEAT_FOLDING
3468 (void)hasFolding(curwin->w_botline, NULL,
3469 &curwin->w_botline);
3470#endif
3471 ++curwin->w_botline;
3472 room -= i;
3473 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3474 }
3475 }
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /*
3478 * When hit bottom of the file: move cursor down.
3479 */
3480 if (n > 0)
3481 {
3482# ifdef FEAT_FOLDING
3483 if (hasAnyFolding(curwin))
3484 {
3485 while (--n >= 0
3486 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3487 {
3488 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3489 &curwin->w_cursor.lnum);
3490 ++curwin->w_cursor.lnum;
3491 }
3492 }
3493 else
3494# endif
3495 curwin->w_cursor.lnum += n;
3496 check_cursor_lnum();
3497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 else
3500 {
3501 /*
3502 * scroll the text down
3503 */
3504 while (n > 0 && curwin->w_topline > 1)
3505 {
3506#ifdef FEAT_DIFF
3507 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3508 {
3509 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003510 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 ++curwin->w_topfill;
3512 }
3513 else
3514#endif
3515 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003516 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 n -= i;
3518 if (n < 0 && scrolled > 0)
3519 break;
3520 --curwin->w_topline;
3521#ifdef FEAT_FOLDING
3522 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3523#endif
3524#ifdef FEAT_DIFF
3525 curwin->w_topfill = 0;
3526#endif
3527 }
3528 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3529 VALID_BOTLINE|VALID_BOTLINE_AP);
3530 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (curwin->w_cursor.lnum > 1)
3532 {
3533 --curwin->w_cursor.lnum;
3534 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003537
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 /*
3539 * When hit top of the file: move cursor up.
3540 */
3541 if (n > 0)
3542 {
3543 if (curwin->w_cursor.lnum <= (linenr_T)n)
3544 curwin->w_cursor.lnum = 1;
3545 else
3546# ifdef FEAT_FOLDING
3547 if (hasAnyFolding(curwin))
3548 {
3549 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3550 {
3551 --curwin->w_cursor.lnum;
3552 (void)hasFolding(curwin->w_cursor.lnum,
3553 &curwin->w_cursor.lnum, NULL);
3554 }
3555 }
3556 else
3557# endif
3558 curwin->w_cursor.lnum -= n;
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003562 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 foldAdjustCursor();
3564# endif
3565#ifdef FEAT_DIFF
3566 check_topfill(curwin, !flag);
3567#endif
3568 cursor_correct();
3569 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003570 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572
Bram Moolenaar860cae12010-06-05 23:22:07 +02003573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003574do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575{
3576 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003577 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003578 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003579 colnr_T curswant = curwin->w_curswant;
3580 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 win_T *old_curwin = curwin;
3582 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583 int old_VIsual_select = VIsual_select;
3584 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585
3586 /*
3587 * loop through the cursorbound windows
3588 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003590 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 {
3592 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003593 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003594 if (curwin != old_curwin && curwin->w_p_crb)
3595 {
3596# ifdef FEAT_DIFF
3597 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003598 curwin->w_cursor.lnum =
3599 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 else
3601# endif
3602 curwin->w_cursor.lnum = line;
3603 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003604 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003605 curwin->w_curswant = curswant;
3606 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607
Bram Moolenaar85a20022019-12-21 18:25:54 +01003608 // Make sure the cursor is in a valid position. Temporarily set
3609 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003610 int restart_edit_save = restart_edit;
3611 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003613
3614 // Avoid a scroll here for the cursor position, 'scrollbind' is
3615 // more important.
3616 if (!curwin->w_p_scb)
3617 validate_cursor();
3618
Bram Moolenaar61452852011-02-01 18:01:11 +01003619 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003620 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621 if (has_mbyte)
3622 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003623 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003624
Bram Moolenaar85a20022019-12-21 18:25:54 +01003625 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003626 if (!curwin->w_p_scb)
3627 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003628 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003629 }
3630 }
3631
3632 /*
3633 * reset current-window
3634 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003635 VIsual_select = old_VIsual_select;
3636 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003637 curwin = old_curwin;
3638 curbuf = old_curbuf;
3639}