blob: 3c50d258c56335ba8edc13bc4361529dedae9375 [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 */
41 static int
42adjust_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/*
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000242 * Set curwin->s_skipcol to zero and redraw later if needed.
243 */
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;
279 linenr_T old_topline;
280#ifdef FEAT_DIFF
281 int old_topfill;
282#endif
283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 old_topline = curwin->w_topline;
315#ifdef FEAT_DIFF
316 old_topfill = curwin->w_topfill;
317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
352 // the smoothscroll marker "<<<" displayed in the top-left if
353 // needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000355 int smoothscroll_overlap = smoothscroll_marker_overlap(
356 curwin_col_off() - curwin_col_off2());
357 if (curwin->w_skipcol + smoothscroll_overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
399 scroll_cursor_halfway(FALSE);
400 else
401 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 check_botline = TRUE;
404 }
405 }
406
407 else
408 {
409#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100410 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
412#endif
413 check_botline = TRUE;
414 }
415 }
416
417 /*
418 * If the cursor is below the bottom of the window, scroll the window
419 * to put the cursor on the window.
420 * When w_botline is invalid, recompute it first, to avoid a redraw later.
421 * If w_botline was approximated, we might need a redraw later in a few
422 * cases, but we don't want to spend (a lot of) time recomputing w_botline
423 * for every small change.
424 */
425 if (check_botline)
426 {
427 if (!(curwin->w_valid & VALID_BOTLINE_AP))
428 validate_botline();
429
430 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
431 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 if (curwin->w_cursor.lnum < curwin->w_botline)
433 {
434 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100435 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_FOLDING
437 || hasAnyFolding(curwin)
438#endif
439 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lineoff_T loff;
442
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // Cursor is (a few lines) above botline, check if there are
444 // 'scrolloff' window lines below the cursor. If not, need to
445 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 n = curwin->w_empty_rows;
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
451#endif
452#ifdef FEAT_DIFF
453 loff.fill = 0;
454 n += curwin->w_filler_rows;
455#endif
456 loff.height = 0;
457 while (loff.lnum < curwin->w_botline
458#ifdef FEAT_DIFF
459 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
460#endif
461 )
462 {
463 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 botline_forw(&loff);
467 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100468 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 }
472 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000474 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 if (check_botline)
477 {
478#ifdef FEAT_FOLDING
479 if (hasAnyFolding(curwin))
480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // Count the number of logical lines between the cursor and
482 // botline - scrolloff (approximation of how much will be
483 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 line_count = 0;
485 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (lnum <= 0 || line_count > curwin->w_height + 1)
491 break;
492 (void)hasFolding(lnum, &lnum, NULL);
493 }
494 }
495 else
496#endif
497 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100498 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
502 scroll_cursor_halfway(FALSE);
503 }
504 }
505 }
506 curwin->w_valid |= VALID_TOPLINE;
507
508 /*
509 * Need to redraw when topline changed.
510 */
511 if (curwin->w_topline != old_topline
512#ifdef FEAT_DIFF
513 || curwin->w_topfill != old_topfill
514#endif
515 )
516 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100517 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000518 redraw_later(UPD_VALID);
519 reset_skipcol();
520
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
665/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000666 * Call changed_window_setting_win() for every window containing "buf".
667 */
668 void
669changed_window_setting_buf(buf_T *buf)
670{
671 tabpage_T *tp;
672 win_T *wp;
673
674 FOR_ALL_TAB_WINDOWS(tp, wp)
675 if (wp->w_buffer == buf)
676 changed_window_setting_win(wp);
677}
678
679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * Set wp->w_topline to a certain number.
681 */
682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200685#ifdef FEAT_DIFF
686 linenr_T prev_topline = wp->w_topline;
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
692#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100693 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100695 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
696 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000698 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200700 if (lnum != prev_topline)
701 // Keep the filler lines when the topline didn't change.
702 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703#endif
704 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100706 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*
710 * Call this function when the length of the cursor line (in screen
711 * characters) has changed, and the change is before the cursor.
712 * Need to take care of w_botline separately!
713 */
714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100715changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
718 |VALID_CHEIGHT|VALID_TOPLINE);
719}
720
721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100722changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
724 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
725 |VALID_CHEIGHT|VALID_TOPLINE);
726}
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728/*
729 * Call this function when the length of a line (in screen characters) above
730 * the cursor have changed.
731 * Need to take care of w_botline separately!
732 */
733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100734changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
737 |VALID_CHEIGHT|VALID_TOPLINE);
738}
739
740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
744 |VALID_CHEIGHT|VALID_TOPLINE);
745}
746
747/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100748 * Display of line has changed for "buf", invalidate cursor position and
749 * w_botline.
750 */
751 void
752changed_line_display_buf(buf_T *buf)
753{
754 win_T *wp;
755
756 FOR_ALL_WINDOWS(wp)
757 if (wp->w_buffer == buf)
758 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
759 |VALID_CROW|VALID_CHEIGHT
760 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
761}
762
763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * Make sure the value of curwin->w_botline is valid.
765 */
766 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100767validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100769 validate_botline_win(curwin);
770}
771
772/*
773 * Make sure the value of wp->w_botline is valid.
774 */
775 void
776validate_botline_win(win_T *wp)
777{
778 if (!(wp->w_valid & VALID_BOTLINE))
779 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Mark curwin->w_botline as invalid (because of some change in the buffer).
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
789}
790
791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
795}
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798approximate_botline_win(
799 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 wp->w_valid &= ~VALID_BOTLINE;
802}
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
805 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
806 */
807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100808cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
810 check_cursor_moved(curwin);
811 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
812 (VALID_WROW|VALID_WCOL));
813}
814
815/*
816 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
817 * w_topline must be valid, you may need to call update_topline() first!
818 */
819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100822 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 check_cursor_moved(curwin);
824 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
825 curs_columns(TRUE);
826}
827
828#if defined(FEAT_GUI) || defined(PROTO)
829/*
830 * validate w_cline_row.
831 */
832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 /*
836 * First make sure that w_topline is valid (after moving the cursor).
837 */
838 update_topline();
839 check_cursor_moved(curwin);
840 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100841 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843#endif
844
845/*
846 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200847 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 */
849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 linenr_T lnum;
853 int i;
854 int all_invalid;
855 int valid;
856#ifdef FEAT_FOLDING
857 long fold_count;
858#endif
859
Bram Moolenaar85a20022019-12-21 18:25:54 +0100860 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 all_invalid = (!redrawing()
862 || wp->w_lines_valid == 0
863 || wp->w_lines[0].wl_lnum > wp->w_topline);
864 i = 0;
865 wp->w_cline_row = 0;
866 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
867 {
868 valid = FALSE;
869 if (!all_invalid && i < wp->w_lines_valid)
870 {
871 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (wp->w_lines[i].wl_lnum == lnum)
874 {
875#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100876 // Check for newly inserted lines below this row, in which
877 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (!wp->w_buffer->b_mod_set
879 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
880 || wp->w_buffer->b_mod_top
881 > wp->w_lines[i].wl_lastlnum + 1)
882#endif
883 valid = TRUE;
884 }
885 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100886 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 if (valid
889#ifdef FEAT_DIFF
890 && (lnum != wp->w_topline || !wp->w_p_diff)
891#endif
892 )
893 {
894#ifdef FEAT_FOLDING
895 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100896 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (lnum > wp->w_cursor.lnum)
898 break;
899#else
900 ++lnum;
901#endif
902 wp->w_cline_row += wp->w_lines[i].wl_size;
903 }
904 else
905 {
906#ifdef FEAT_FOLDING
907 fold_count = foldedCount(wp, lnum, NULL);
908 if (fold_count)
909 {
910 lnum += fold_count;
911 if (lnum > wp->w_cursor.lnum)
912 break;
913 ++wp->w_cline_row;
914 }
915 else
916#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100917 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100918 wp->w_cline_row += plines_correct_topline(wp, lnum);
919 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922 }
923
924 check_cursor_moved(wp);
925 if (!(wp->w_valid & VALID_CHEIGHT))
926 {
927 if (all_invalid
928 || i == wp->w_lines_valid
929 || (i < wp->w_lines_valid
930 && (!wp->w_lines[i].wl_valid
931 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
932 {
933#ifdef FEAT_DIFF
934 if (wp->w_cursor.lnum == wp->w_topline)
935 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
936 TRUE) + wp->w_topfill;
937 else
938#endif
939 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
940#ifdef FEAT_FOLDING
941 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
942 NULL, NULL, TRUE, NULL);
943#endif
944 }
945 else if (i > wp->w_lines_valid)
946 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100947 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 wp->w_cline_height = 0;
949#ifdef FEAT_FOLDING
950 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
951 NULL, NULL, TRUE, NULL);
952#endif
953 }
954 else
955 {
956 wp->w_cline_height = wp->w_lines[i].wl_size;
957#ifdef FEAT_FOLDING
958 wp->w_cline_folded = wp->w_lines[i].wl_folded;
959#endif
960 }
961 }
962
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100963 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Validate curwin->w_virtcol only.
969 */
970 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100971validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 validate_virtcol_win(curwin);
974}
975
976/*
977 * Validate wp->w_virtcol only.
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000983
984 if (wp->w_valid & VALID_VIRTCOL)
985 return;
986
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100987#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000988 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100989#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000990 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000991#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000993#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995}
996
997/*
998 * Validate curwin->w_cline_height only.
999 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001004
1005 if (curwin->w_valid & VALID_CHEIGHT)
1006 return;
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001009 if (curwin->w_cursor.lnum == curwin->w_topline)
1010 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1011 + curwin->w_topfill;
1012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001014 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
1020
1021/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001022 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
1024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001025validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
1027 colnr_T off;
1028 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001029 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001033 if (curwin->w_valid & VALID_WCOL)
1034 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001035
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001036 col = curwin->w_virtcol;
1037 off = curwin_col_off();
1038 col += off;
1039 width = curwin->w_width - off + curwin_col_off2();
1040
1041 // long line wrapping, adjust curwin->w_wrow
1042 if (curwin->w_p_wrap
1043 && col >= (colnr_T)curwin->w_width
1044 && width > 0)
1045 // use same formula as what is used in curs_columns()
1046 col -= ((col - curwin->w_width) / width + 1) * width;
1047 if (col > (int)curwin->w_leftcol)
1048 col -= curwin->w_leftcol;
1049 else
1050 col = 0;
1051 curwin->w_wcol = col;
1052
1053 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001054#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001055 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001060 * Compute offset of a window, occupied by absolute or relative line number,
1061 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 */
1063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001064win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar64486672010-05-16 15:46:46 +02001066 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_FOLDING
1069 + wp->w_p_fdc
1070#endif
1071#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001072 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
1074 );
1075}
1076
1077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001078curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 return win_col_off(curwin);
1081}
1082
1083/*
1084 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001085 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1086 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
1088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001089win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar64486672010-05-16 15:46:46 +02001091 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001092 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 return 0;
1094}
1095
1096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001097curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 return win_col_off2(curwin);
1100}
1101
1102/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001103 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * Also updates curwin->w_wrow and curwin->w_cline_row.
1105 * Also updates curwin->w_leftcol.
1106 */
1107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001109 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001112 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 int off_left, off_right;
1114 int n;
1115 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001116 int width1; // text width for first screen line
1117 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 int new_leftcol;
1119 colnr_T startcol;
1120 colnr_T endcol;
1121 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001122 long so = get_scrolloff_value();
1123 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001124 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126 /*
1127 * First make sure that w_topline is valid (after moving the cursor).
1128 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001129 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 /*
1132 * Next make sure that w_cline_row is valid.
1133 */
1134 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001135 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001137#ifdef FEAT_PROP_POPUP
1138 // will be set by getvvcol() but not reset
1139 curwin->w_virtcol_first_char = 0;
1140#endif
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 /*
1143 * Compute the number of virtual columns.
1144 */
1145#ifdef FEAT_FOLDING
1146 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1149 else
1150#endif
1151 getvvcol(curwin, &curwin->w_cursor,
1152 &startcol, &(curwin->w_virtcol), &endcol);
1153
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001156 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 extra = curwin_col_off();
1159 curwin->w_wcol = curwin->w_virtcol + extra;
1160 endcol += extra;
1161
1162 /*
1163 * Now compute w_wrow, counting screen lines from w_cline_row.
1164 */
1165 curwin->w_wrow = curwin->w_cline_row;
1166
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001167 width1 = curwin->w_width - extra;
1168 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001170 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001171 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001172 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001173 if (curwin->w_p_wrap)
1174 curwin->w_wrow = curwin->w_height - 1;
1175 else
1176 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001178 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001180 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001182 // skip columns that are not visible
1183 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001184 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001185 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001186 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001187 // Deduct by multiples of width2. This allows the long line
1188 // wrapping formula below to correctly calculate the w_wcol value
1189 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001190 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001191 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001196 did_sub_skipcol = TRUE;
1197 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001198
Bram Moolenaar85a20022019-12-21 18:25:54 +01001199 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001200 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001203 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1204 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 curwin->w_wrow += n;
1206
1207#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // When cursor wraps to first char of next line in Insert
1209 // mode, the 'showbreak' string isn't shown, backup to first
1210 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001211 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001212 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001213 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 curwin->w_wcol = 0;
1215#endif
1216 }
1217 }
1218
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1220 // is not folded.
1221 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001222 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_FOLDING
1224 && !curwin->w_cline_folded
1225#endif
1226 )
1227 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001228#ifdef FEAT_PROP_POPUP
1229 if (curwin->w_virtcol_first_char > 0)
1230 {
1231 int cols = (curwin->w_width - extra);
1232 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1233
1234 // each "above" text prop shifts the text one row down
1235 curwin->w_wrow += rows;
1236 curwin->w_wcol -= rows * cols;
1237 endcol -= rows * cols;
1238 curwin->w_cline_height = rows + 1;
1239 }
1240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 /*
1242 * If Cursor is left of the screen, scroll rightwards.
1243 * If Cursor is right of the screen, scroll leftwards
1244 * If we get closer to the edge than 'sidescrolloff', scroll a little
1245 * extra
1246 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001247 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001248 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001249 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (off_left < 0 || off_right > 0)
1251 {
1252 if (off_left < 0)
1253 diff = -off_left;
1254 else
1255 diff = off_right;
1256
Bram Moolenaar85a20022019-12-21 18:25:54 +01001257 // When far off or not enough room on either side, put cursor in
1258 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001259 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1260 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 else
1262 {
1263 if (diff < p_ss)
1264 diff = p_ss;
1265 if (off_left < 0)
1266 new_leftcol = curwin->w_leftcol - diff;
1267 else
1268 new_leftcol = curwin->w_leftcol + diff;
1269 }
1270 if (new_leftcol < 0)
1271 new_leftcol = 0;
1272 if (new_leftcol != (int)curwin->w_leftcol)
1273 {
1274 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001275 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001276 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 }
1279 curwin->w_wcol -= curwin->w_leftcol;
1280 }
1281 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1282 curwin->w_wcol -= curwin->w_leftcol;
1283 else
1284 curwin->w_wcol = 0;
1285
1286#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // Skip over filler lines. At the top use w_topfill, there
1288 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (curwin->w_cursor.lnum == curwin->w_topline)
1290 curwin->w_wrow += curwin->w_topfill;
1291 else
1292 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1293#endif
1294
1295 prev_skipcol = curwin->w_skipcol;
1296
1297 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if ((curwin->w_wrow >= curwin->w_height
1300 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001301 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (p_lines =
1303#ifdef FEAT_DIFF
1304 plines_win_nofill
1305#else
1306 plines_win
1307#endif
1308 (curwin, curwin->w_cursor.lnum, FALSE))
1309 - 1 >= curwin->w_height))
1310 && curwin->w_height != 0
1311 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001312 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001313 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001315 // Cursor past end of screen. Happens with a single line that does
1316 // not fit on screen. Find a skipcol to show the text around the
1317 // cursor. Avoid scrolling all the time. compute value of "extra":
1318 // 1: Less than 'scrolloff' lines above
1319 // 2: Less than 'scrolloff' lines below
1320 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 // Compute last display line of the buffer line that we want at the
1325 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (p_lines == 0)
1327 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1328 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001329 if (p_lines > curwin->w_wrow + so)
1330 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001333 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 extra += 2;
1335
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001336 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001339 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (n > curwin->w_height / 2)
1341 n -= curwin->w_height / 2;
1342 else
1343 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001344 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (n > p_lines - curwin->w_height + 1)
1346 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001347 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else if (extra == 1)
1350 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001351 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001352 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1353 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (extra > 0)
1355 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001356 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1357 extra = curwin->w_skipcol / width2;
1358 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 }
1361 else if (extra == 2)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001366 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (endcol > curwin->w_skipcol)
1368 curwin->w_skipcol = endcol;
1369 }
1370
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371 // adjust w_wrow for the changed w_skipcol
1372 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001373 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001374 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001375 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (curwin->w_wrow >= curwin->w_height)
1378 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001381 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 curwin->w_wrow -= extra;
1383 }
1384
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (extra > 0)
1387 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1388 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001389 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001391 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 curwin->w_skipcol = 0;
1393 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001394 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001396#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001397 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001398#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001399#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1400 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1401 {
1402 curwin->w_wrow += popup_top_extra(curwin);
1403 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001404 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001405 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001406 else
1407 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001408#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001409
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001410 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1411 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001412 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001413 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1416}
1417
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001418#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001419/*
1420 * Compute the screen position of text character at "pos" in window "wp"
1421 * The resulting values are one-based, zero when character is not visible.
1422 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001423 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001424textpos2screenpos(
1425 win_T *wp,
1426 pos_T *pos,
1427 int *rowp, // screen row
1428 int *scolp, // start screen column
1429 int *ccolp, // cursor screen column
1430 int *ecolp) // end screen column
1431{
1432 colnr_T scol = 0, ccol = 0, ecol = 0;
1433 int row = 0;
1434 int rowoff = 0;
1435 colnr_T coloff = 0;
1436
Bram Moolenaar189663b2021-07-21 18:04:56 +02001437 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001439 colnr_T col;
1440 int width;
1441 linenr_T lnum = pos->lnum;
1442#ifdef FEAT_FOLDING
1443 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001444
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001445 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1446#endif
1447 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001448
1449#ifdef FEAT_DIFF
1450 // Add filler lines above this buffer line.
1451 row += diff_check_fill(wp, lnum);
1452#endif
1453
zeertzjqba2d1912022-12-18 12:28:59 +00001454 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001455#ifdef FEAT_FOLDING
1456 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001457 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001458 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001459 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001460 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001462#endif
1463 {
1464 getvcol(wp, pos, &scol, &ccol, &ecol);
1465
1466 // similar to what is done in validate_cursor_col()
1467 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468 col += off;
1469 width = wp->w_width - off + win_col_off2(wp);
1470
1471 // long line wrapping, adjust row
1472 if (wp->w_p_wrap
1473 && col >= (colnr_T)wp->w_width
1474 && width > 0)
1475 {
1476 // use same formula as what is used in curs_columns()
1477 rowoff = ((col - wp->w_width) / width + 1);
1478 col -= rowoff * width;
1479 }
1480 col -= wp->w_leftcol;
1481 if (col >= wp->w_width)
1482 col = -1;
1483 if (col >= 0 && row + rowoff <= wp->w_height)
1484 {
1485 coloff = col - scol + wp->w_wincol + 1;
1486 row += W_WINROW(wp);
1487 }
1488 else
1489 // character is left, right or below of the window
1490 row = rowoff = scol = ccol = ecol = 0;
1491 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001492 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001493 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001494 *scolp = scol + coloff;
1495 *ccolp = ccol + coloff;
1496 *ecolp = ecol + coloff;
1497}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001498#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001499
Bram Moolenaar12034e22019-08-25 22:25:02 +02001500#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001501/*
1502 * "screenpos({winid}, {lnum}, {col})" function
1503 */
1504 void
1505f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1506{
1507 dict_T *dict;
1508 win_T *wp;
1509 pos_T pos;
1510 int row = 0;
1511 int scol = 0, ccol = 0, ecol = 0;
1512
Bram Moolenaar93a10962022-06-16 11:42:09 +01001513 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001514 return;
1515 dict = rettv->vval.v_dict;
1516
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001517 if (in_vim9script()
1518 && (check_for_number_arg(argvars, 0) == FAIL
1519 || check_for_number_arg(argvars, 1) == FAIL
1520 || check_for_number_arg(argvars, 2) == FAIL))
1521 return;
1522
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001523 wp = find_win_by_nr_or_id(&argvars[0]);
1524 if (wp == NULL)
1525 return;
1526
1527 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001528 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1529 {
1530 semsg(_(e_invalid_line_number_nr), pos.lnum);
1531 return;
1532 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001533 pos.col = tv_get_number(&argvars[2]) - 1;
1534 pos.coladd = 0;
1535 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1536
1537 dict_add_number(dict, "row", row);
1538 dict_add_number(dict, "col", scol);
1539 dict_add_number(dict, "curscol", ccol);
1540 dict_add_number(dict, "endcol", ecol);
1541}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001542
1543/*
1544 * "virtcol2col({winid}, {lnum}, {col})" function
1545 */
1546 void
1547f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1548{
1549 win_T *wp;
1550 linenr_T lnum;
1551 int screencol;
1552 int error = FALSE;
1553
1554 rettv->vval.v_number = -1;
1555
1556 if (check_for_number_arg(argvars, 0) == FAIL
1557 || check_for_number_arg(argvars, 1) == FAIL
1558 || check_for_number_arg(argvars, 2) == FAIL)
1559 return;
1560
1561 wp = find_win_by_nr_or_id(&argvars[0]);
1562 if (wp == NULL)
1563 return;
1564
1565 lnum = tv_get_number_chk(&argvars[1], &error);
1566 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1567 return;
1568
1569 screencol = tv_get_number_chk(&argvars[2], &error);
1570 if (error || screencol < 0)
1571 return;
1572
1573 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1574}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001575#endif
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577/*
1578 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001581scrolldown(
1582 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001585 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 int wrow;
1587 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001588 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001589 int width1 = 0;
1590 int width2 = 0;
1591
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001592 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001593 {
1594 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001595 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598#ifdef FEAT_FOLDING
1599 linenr_T first;
1600
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1603#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001605 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001608 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1609 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 ++curwin->w_topfill;
1612 ++done;
1613 }
1614 else
1615#endif
1616 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001617 // break when at the very top
1618 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001619 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001621 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001623 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001624 if (curwin->w_skipcol >= width1 + width2)
1625 curwin->w_skipcol -= width2;
1626 else
1627 curwin->w_skipcol -= width1;
1628 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001632 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001633 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001634 --curwin->w_topline;
1635 curwin->w_skipcol = 0;
1636#ifdef FEAT_DIFF
1637 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001639#ifdef FEAT_FOLDING
1640 // A sequence of folded lines only counts for one logical line
1641 if (hasFolding(curwin->w_topline, &first, NULL))
1642 {
1643 ++done;
1644 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001645 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001646 curwin->w_botline -= curwin->w_topline - first;
1647 curwin->w_topline = first;
1648 }
1649 else
1650#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001651 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001652 {
1653 int size = win_linetabsize(curwin, curwin->w_topline,
1654 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1655 if (size > width1)
1656 {
1657 curwin->w_skipcol = width1;
1658 size -= width1;
1659 redraw_later(UPD_NOT_VALID);
1660 }
1661 while (size > width2)
1662 {
1663 curwin->w_skipcol += width2;
1664 size -= width2;
1665 }
1666 ++done;
1667 }
1668 else
1669 done += PLINES_NOFILL(curwin->w_topline);
1670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001672 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 invalidate_botline();
1674 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001675 curwin->w_wrow += done; // keep w_wrow updated
1676 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678#ifdef FEAT_DIFF
1679 if (curwin->w_cursor.lnum == curwin->w_topline)
1680 curwin->w_cline_row = 0;
1681 check_topfill(curwin, TRUE);
1682#endif
1683
1684 /*
1685 * Compute the row number of the last row of the cursor line
1686 * and move the cursor onto the displayed part of the window.
1687 */
1688 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001689 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 validate_virtcol();
1692 validate_cheight();
1693 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001694 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
1696 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1697 {
1698#ifdef FEAT_FOLDING
1699 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1700 {
1701 --wrow;
1702 if (first == 1)
1703 curwin->w_cursor.lnum = 1;
1704 else
1705 curwin->w_cursor.lnum = first - 1;
1706 }
1707 else
1708#endif
1709 wrow -= plines(curwin->w_cursor.lnum--);
1710 curwin->w_valid &=
1711 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1712 moved = TRUE;
1713 }
1714 if (moved)
1715 {
1716#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001717 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 foldAdjustCursor();
1719#endif
1720 coladvance(curwin->w_curswant);
1721 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001722
1723 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1724 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001725 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001726 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1727
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001728 // make sure the cursor is in the visible text
1729 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001730 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001731 int row = 0;
1732 if (col >= width1)
1733 {
1734 col -= width1;
1735 ++row;
1736 }
1737 if (col > width2)
1738 {
1739 row += col / width2;
1740 col = col % width2;
1741 }
1742 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001743 {
1744 curwin->w_curswant = curwin->w_virtcol
1745 - (row - curwin->w_height + 1) * width2;
1746 coladvance(curwin->w_curswant);
1747 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
1751/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001752 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1753 */
1754 static int
1755scrolling_screenlines(int byfold UNUSED)
1756{
1757 return (curwin->w_p_wrap && curwin->w_p_sms)
1758# ifdef FEAT_FOLDING
1759 || (byfold && hasAnyFolding(curwin))
1760# endif
1761# ifdef FEAT_DIFF
1762 || curwin->w_p_diff
1763# endif
1764 ;
1765}
1766
1767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771scrollup(
1772 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001775 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001777 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001779 int width1 = curwin->w_width - curwin_col_off();
1780 int width2 = width1 + curwin_col_off2();
1781 int size = 0;
1782 linenr_T prev_topline = curwin->w_topline;
1783
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001784 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001785 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001786
1787 // diff mode: first consume "topfill"
1788 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1789 // the line, then advance to the next line.
1790 // folding: count each sequence of folded lines as one logical line.
1791 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
1793# ifdef FEAT_DIFF
1794 if (curwin->w_topfill > 0)
1795 --curwin->w_topfill;
1796 else
1797# endif
1798 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001799 linenr_T lnum = curwin->w_topline;
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801# ifdef FEAT_FOLDING
1802 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 (void)hasFolding(lnum, NULL, &lnum);
1805# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001806 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001807 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 // 'smoothscroll': increase "w_skipcol" until it goes over
1809 // the end of the line, then advance to the next line.
1810 int add = curwin->w_skipcol > 0 ? width2 : width1;
1811 curwin->w_skipcol += add;
1812 if (curwin->w_skipcol >= size)
1813 {
1814 if (lnum == curbuf->b_ml.ml_line_count)
1815 {
1816 // at the last screen line, can't scroll further
1817 curwin->w_skipcol -= add;
1818 break;
1819 }
1820 ++lnum;
1821 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001822 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001823 else
1824 {
1825 if (lnum >= curbuf->b_ml.ml_line_count)
1826 break;
1827 ++lnum;
1828 }
1829
1830 if (lnum > curwin->w_topline)
1831 {
1832 // approximate w_botline
1833 curwin->w_botline += lnum - curwin->w_topline;
1834 curwin->w_topline = lnum;
1835# ifdef FEAT_DIFF
1836 curwin->w_topfill = diff_check_fill(curwin, lnum);
1837# endif
1838 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001839 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001840 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 }
1843 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001844
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001845 if (curwin->w_topline == prev_topline)
1846 // need to redraw even though w_topline didn't change
1847 redraw_later(UPD_NOT_VALID);
1848 }
1849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001852 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
1855 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1856 curwin->w_topline = curbuf->b_ml.ml_line_count;
1857 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1858 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1859
1860#ifdef FEAT_DIFF
1861 check_topfill(curwin, FALSE);
1862#endif
1863
1864#ifdef FEAT_FOLDING
1865 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001866 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1868#endif
1869
1870 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1871 if (curwin->w_cursor.lnum < curwin->w_topline)
1872 {
1873 curwin->w_cursor.lnum = curwin->w_topline;
1874 curwin->w_valid &=
1875 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1876 coladvance(curwin->w_curswant);
1877 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001878 if (curwin->w_cursor.lnum == curwin->w_topline
1879 && do_sms && curwin->w_skipcol > 0)
1880 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001881 int col_off = curwin_col_off();
1882 int col_off2 = curwin_col_off2();
1883
1884 int width1 = curwin->w_width - col_off;
1885 int width2 = width1 + col_off2;
1886 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001887 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001888 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001889 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001890
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001891 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1892 // going past it anyway.
1893 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1894 smoothscroll_marker_overlap(extra2);
1895
Bram Moolenaar118c2352022-10-09 17:19:27 +01001896 // Make sure the cursor is in a visible part of the line, taking
1897 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001898 // If there are not enough screen lines put the cursor in the middle.
1899 if (scrolloff_cols > space_cols / 2)
1900 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001901 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001902 if (curwin->w_virtcol < curwin->w_skipcol
1903 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001904 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 colnr_T col = curwin->w_virtcol;
1906
1907 if (col < width1)
1908 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001909 while (col < curwin->w_skipcol
1910 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001912 curwin->w_curswant = col;
1913 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001914
1915 // validate_virtcol() marked various things as valid, but after
1916 // moving the cursor they need to be recomputed
1917 curwin->w_valid &=
1918 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 }
1920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921}
1922
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001923/*
1924 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1925 * valid for 'smoothscroll'.
1926 */
1927 void
1928adjust_skipcol(void)
1929{
1930 if (!curwin->w_p_wrap
1931 || !curwin->w_p_sms
1932 || curwin->w_cursor.lnum != curwin->w_topline)
1933 return;
1934
1935 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001936 if (width1 <= 0)
1937 return; // no text will be displayed
1938
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001939 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001940 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001941 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1942 int scrolled = FALSE;
1943
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001944 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001945 if (curwin->w_cline_height == curwin->w_height
1946 // w_cline_height may be capped at w_height, check there aren't
1947 // actually more lines.
1948 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1949 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001950 {
1951 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001952 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001953 return;
1954 }
1955
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001956 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001957 while (curwin->w_skipcol > 0
1958 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1959 {
1960 // scroll a screen line down
1961 if (curwin->w_skipcol >= width1 + width2)
1962 curwin->w_skipcol -= width2;
1963 else
1964 curwin->w_skipcol -= width1;
1965 redraw_later(UPD_NOT_VALID);
1966 scrolled = TRUE;
1967 validate_virtcol();
1968 }
1969 if (scrolled)
1970 return; // don't scroll in the other direction now
1971
1972 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1973 int row = 0;
1974 if (col >= width1)
1975 {
1976 col -= width1;
1977 ++row;
1978 }
1979 if (col > width2)
1980 {
1981 row += col / width2;
1982 col = col % width2;
1983 }
1984 if (row >= curwin->w_height)
1985 {
1986 if (curwin->w_skipcol == 0)
1987 {
1988 curwin->w_skipcol += width1;
1989 --row;
1990 }
1991 if (row >= curwin->w_height)
1992 curwin->w_skipcol += (row - curwin->w_height) * width2;
1993 redraw_later(UPD_NOT_VALID);
1994 }
1995}
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#ifdef FEAT_DIFF
1998/*
1999 * Don't end up with too many filler lines in the window.
2000 */
2001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002002check_topfill(
2003 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002004 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int n;
2007
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002008 if (wp->w_topfill <= 0)
2009 return;
2010
2011 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2012 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002014 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002016 --wp->w_topline;
2017 wp->w_topfill = 0;
2018 }
2019 else
2020 {
2021 wp->w_topfill = wp->w_height - n;
2022 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 }
2026}
2027
2028/*
2029 * Use as many filler lines as possible for w_topline. Make sure w_topline
2030 * is still visible.
2031 */
2032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 int n;
2036
2037 n = plines_nofill(curwin->w_topline);
2038 if (n >= curwin->w_height)
2039 curwin->w_topfill = 0;
2040 else
2041 {
2042 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2043 if (curwin->w_topfill + n > curwin->w_height)
2044 curwin->w_topfill = curwin->w_height - n;
2045 }
2046}
2047#endif
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049/*
2050 * Scroll the screen one line down, but don't do it if it would move the
2051 * cursor off the screen.
2052 */
2053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002054scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 int end_row;
2057#ifdef FEAT_DIFF
2058 int can_fill = (curwin->w_topfill
2059 < diff_check_fill(curwin, curwin->w_topline));
2060#endif
2061
2062 if (curwin->w_topline <= 1
2063#ifdef FEAT_DIFF
2064 && !can_fill
2065#endif
2066 )
2067 return;
2068
Bram Moolenaar85a20022019-12-21 18:25:54 +01002069 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 /*
2072 * Compute the row number of the last row of the cursor line
2073 * and make sure it doesn't go off the screen. Make sure the cursor
2074 * doesn't go past 'scrolloff' lines from the screen end.
2075 */
2076 end_row = curwin->w_wrow;
2077#ifdef FEAT_DIFF
2078 if (can_fill)
2079 ++end_row;
2080 else
2081 end_row += plines_nofill(curwin->w_topline - 1);
2082#else
2083 end_row += plines(curwin->w_topline - 1);
2084#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002085 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 validate_cheight();
2088 validate_virtcol();
2089 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002090 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002092 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094#ifdef FEAT_DIFF
2095 if (can_fill)
2096 {
2097 ++curwin->w_topfill;
2098 check_topfill(curwin, TRUE);
2099 }
2100 else
2101 {
2102 --curwin->w_topline;
2103 curwin->w_topfill = 0;
2104 }
2105#else
2106 --curwin->w_topline;
2107#endif
2108#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002109 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002111 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2113 }
2114}
2115
2116/*
2117 * Scroll the screen one line up, but don't do it if it would move the cursor
2118 * off the screen.
2119 */
2120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002121scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 int start_row;
2124
2125 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2126#ifdef FEAT_DIFF
2127 && curwin->w_topfill == 0
2128#endif
2129 )
2130 return;
2131
Bram Moolenaar85a20022019-12-21 18:25:54 +01002132 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 /*
2135 * Compute the row number of the first row of the cursor line
2136 * and make sure it doesn't go off the screen. Make sure the cursor
2137 * doesn't go before 'scrolloff' lines from the screen start.
2138 */
2139#ifdef FEAT_DIFF
2140 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2141 - curwin->w_topfill;
2142#else
2143 start_row = curwin->w_wrow - plines(curwin->w_topline);
2144#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002145 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002148 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002150 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152#ifdef FEAT_DIFF
2153 if (curwin->w_topfill > 0)
2154 --curwin->w_topfill;
2155 else
2156#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002157 {
2158#ifdef FEAT_FOLDING
2159 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002162 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002163 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2165 }
2166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168/*
2169 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2170 * a (wrapped) text line. Uses and sets "lp->fill".
2171 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002172 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 */
2174 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002175topline_back_winheight(
2176 lineoff_T *lp,
2177 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179#ifdef FEAT_DIFF
2180 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 ++lp->fill;
2184 lp->height = 1;
2185 }
2186 else
2187#endif
2188 {
2189 --lp->lnum;
2190#ifdef FEAT_DIFF
2191 lp->fill = 0;
2192#endif
2193 if (lp->lnum < 1)
2194 lp->height = MAXCOL;
2195 else
2196#ifdef FEAT_FOLDING
2197 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 lp->height = 1;
2200 else
2201#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002202 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204}
2205
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002206 static void
2207topline_back(lineoff_T *lp)
2208{
2209 topline_back_winheight(lp, TRUE);
2210}
2211
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213/*
2214 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2215 * a (wrapped) text line. Uses and sets "lp->fill".
2216 * Returns the height of the added line in "lp->height".
2217 * Lines below the last one are incredibly high.
2218 */
2219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222#ifdef FEAT_DIFF
2223 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2224 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 ++lp->fill;
2227 lp->height = 1;
2228 }
2229 else
2230#endif
2231 {
2232 ++lp->lnum;
2233#ifdef FEAT_DIFF
2234 lp->fill = 0;
2235#endif
2236 if (lp->lnum > curbuf->b_ml.ml_line_count)
2237 lp->height = MAXCOL;
2238 else
2239#ifdef FEAT_FOLDING
2240 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002241 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 lp->height = 1;
2243 else
2244#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002245 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247}
2248
2249#ifdef FEAT_DIFF
2250/*
2251 * Switch from including filler lines below lp->lnum to including filler
2252 * lines above loff.lnum + 1. This keeps pointing to the same line.
2253 * When there are no filler lines nothing changes.
2254 */
2255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002256botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258 if (lp->fill > 0)
2259 {
2260 ++lp->lnum;
2261 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2262 }
2263}
2264
2265/*
2266 * Switch from including filler lines above lp->lnum to including filler
2267 * lines below loff.lnum - 1. This keeps pointing to the same line.
2268 * When there are no filler lines nothing changes.
2269 */
2270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002271topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 if (lp->fill > 0)
2274 {
2275 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2276 --lp->lnum;
2277 }
2278}
2279#endif
2280
2281/*
2282 * Recompute topline to put the cursor at the top of the window.
2283 * Scroll at least "min_scroll" lines.
2284 * If "always" is TRUE, always set topline (for "zt").
2285 */
2286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int scrolled = 0;
2290 int extra = 0;
2291 int used;
2292 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002293 linenr_T top; // just above displayed lines
2294 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002296 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297#ifdef FEAT_DIFF
2298 linenr_T old_topfill = curwin->w_topfill;
2299#endif
2300 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002301 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (mouse_dragging > 0)
2304 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /*
2307 * Decrease topline until:
2308 * - it has become 1
2309 * - (part of) the cursor line is moved off the screen or
2310 * - moved at least 'scrolljump' lines and
2311 * - at least 'scrolloff' lines above and below the cursor
2312 */
2313 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002314 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (curwin->w_cursor.lnum < curwin->w_topline)
2316 scrolled = used;
2317
2318#ifdef FEAT_FOLDING
2319 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2320 {
2321 --top;
2322 ++bot;
2323 }
2324 else
2325#endif
2326 {
2327 top = curwin->w_cursor.lnum - 1;
2328 bot = curwin->w_cursor.lnum + 1;
2329 }
2330 new_topline = top + 1;
2331
2332#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 // "used" already contains the number of filler lines above, don't add it
2334 // again.
2335 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002336 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#endif
2338
2339 /*
2340 * Check if the lines from "top" to "bot" fit in the window. If they do,
2341 * set new_topline and advance "top" and "bot" to include more lines.
2342 */
2343 while (top > 0)
2344 {
2345#ifdef FEAT_FOLDING
2346 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002347 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 i = 1;
2349 else
2350#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002351 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002352 if (top < curwin->w_topline)
2353 scrolled += i;
2354
2355 // If scrolling is needed, scroll at least 'sj' lines.
2356 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2357 && extra >= off)
2358 break;
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 used += i;
2361 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2362 {
2363#ifdef FEAT_FOLDING
2364 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002365 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 ++used;
2367 else
2368#endif
2369 used += plines(bot);
2370 }
2371 if (used > curwin->w_height)
2372 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 extra += i;
2375 new_topline = top;
2376 --top;
2377 ++bot;
2378 }
2379
2380 /*
2381 * If we don't have enough space, put cursor in the middle.
2382 * This makes sure we get the same position when using "k" and "j"
2383 * in a small window.
2384 */
2385 if (used > curwin->w_height)
2386 scroll_cursor_halfway(FALSE);
2387 else
2388 {
2389 /*
2390 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002391 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 if (new_topline < curwin->w_topline || always)
2394 curwin->w_topline = new_topline;
2395 if (curwin->w_topline > curwin->w_cursor.lnum)
2396 curwin->w_topline = curwin->w_cursor.lnum;
2397#ifdef FEAT_DIFF
2398 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2399 if (curwin->w_topfill > 0 && extra > off)
2400 {
2401 curwin->w_topfill -= extra - off;
2402 if (curwin->w_topfill < 0)
2403 curwin->w_topfill = 0;
2404 }
2405 check_topfill(curwin, FALSE);
2406#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002407 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002408 if (curwin->w_topline == curwin->w_cursor.lnum
2409 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002410 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002412 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef FEAT_DIFF
2414 || curwin->w_topfill != old_topfill
2415#endif
2416 )
2417 curwin->w_valid &=
2418 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2419 curwin->w_valid |= VALID_TOPLINE;
2420 }
2421}
2422
2423/*
2424 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2425 * screen lines for text lines.
2426 */
2427 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002428set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430#ifdef FEAT_DIFF
2431 wp->w_filler_rows = 0;
2432#endif
2433 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002434 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 else
2436 {
2437 wp->w_empty_rows = wp->w_height - used;
2438#ifdef FEAT_DIFF
2439 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2440 {
2441 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2442 if (wp->w_empty_rows > wp->w_filler_rows)
2443 wp->w_empty_rows -= wp->w_filler_rows;
2444 else
2445 {
2446 wp->w_filler_rows = wp->w_empty_rows;
2447 wp->w_empty_rows = 0;
2448 }
2449 }
2450#endif
2451 }
2452}
2453
2454/*
2455 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002456 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2458 * This is messy stuff!!!
2459 */
2460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 int used;
2464 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002465 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 int extra = 0;
2467 int i;
2468 linenr_T line_count;
2469 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002470 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 lineoff_T loff;
2472 lineoff_T boff;
2473#ifdef FEAT_DIFF
2474 int old_topfill = curwin->w_topfill;
2475 int fill_below_window;
2476#endif
2477 linenr_T old_botline = curwin->w_botline;
2478 linenr_T old_valid = curwin->w_valid;
2479 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002480 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002481 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 cln = curwin->w_cursor.lnum;
2484 if (set_topbot)
2485 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002486 int set_skipcol = FALSE;
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 used = 0;
2489 curwin->w_botline = cln + 1;
2490#ifdef FEAT_DIFF
2491 loff.fill = 0;
2492#endif
2493 for (curwin->w_topline = curwin->w_botline;
2494 curwin->w_topline > 1;
2495 curwin->w_topline = loff.lnum)
2496 {
2497 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002498 topline_back_winheight(&loff, FALSE);
2499 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002501 if (used + loff.height > curwin->w_height)
2502 {
2503 if (curwin->w_p_sms && curwin->w_p_wrap)
2504 {
2505 // 'smoothscroll' and 'wrap' are set. The above line is
2506 // too long to show in its entirety, so we show just a part
2507 // of it.
2508 if (used < curwin->w_height)
2509 {
2510 int plines_offset = used + loff.height
2511 - curwin->w_height;
2512 used = curwin->w_height;
2513#ifdef FEAT_DIFF
2514 curwin->w_topfill = loff.fill;
2515#endif
2516 curwin->w_topline = loff.lnum;
2517 curwin->w_skipcol = skipcol_from_plines(
2518 curwin, plines_offset);
2519 set_skipcol = TRUE;
2520 }
2521 }
2522 break;
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 used += loff.height;
2525#ifdef FEAT_DIFF
2526 curwin->w_topfill = loff.fill;
2527#endif
2528 }
2529 set_empty_rows(curwin, used);
2530 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2531 if (curwin->w_topline != old_topline
2532#ifdef FEAT_DIFF
2533 || curwin->w_topfill != old_topfill
2534#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002535 || set_skipcol
2536 || curwin->w_skipcol != 0)
2537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002539 if (set_skipcol)
2540 redraw_later(UPD_NOT_VALID);
2541 else
2542 reset_skipcol();
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 else
2546 validate_botline();
2547
Bram Moolenaar85a20022019-12-21 18:25:54 +01002548 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#ifdef FEAT_DIFF
2550 used = plines_nofill(cln);
2551#else
2552 validate_cheight();
2553 used = curwin->w_cline_height;
2554#endif
2555
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002556 // If the cursor is on or below botline, we will at least scroll by the
2557 // height of the cursor line, which is "used". Correct for empty lines,
2558 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (cln >= curwin->w_botline)
2560 {
2561 scrolled = used;
2562 if (cln == curwin->w_botline)
2563 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002564 min_scrolled = scrolled;
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002565 if (curwin->w_p_sms && curwin->w_p_wrap)
2566 {
2567 // 'smoothscroll' and 'wrap' are set
2568 if (cln > curwin->w_botline)
2569 // add screen lines below w_botline
2570 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2571 min_scrolled += PLINES_NOFILL(lnum);
2572
2573 // Calculate how many screen lines the current top line of window
2574 // occupies. If it is occupying more than the entire window, we
2575 // need to scroll the additional clipped lines to scroll past the
2576 // top line before we can move on to the other lines.
2577 int top_plines =
2578#ifdef FEAT_DIFF
2579 plines_win_nofill
2580#else
2581 plines_win
2582#endif
2583 (curwin, curwin->w_topline, FALSE);
2584 int skip_lines = 0;
2585 int width1 = curwin->w_width - curwin_col_off();
2586 int width2 = width1 + curwin_col_off2();
2587 // similar formula is used in curs_columns()
2588 if (curwin->w_skipcol > width1)
2589 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2590 else if (curwin->w_skipcol > 0)
2591 skip_lines = 1;
2592
2593 top_plines -= skip_lines;
2594 if (top_plines > curwin->w_height)
2595 {
2596 scrolled += (top_plines - curwin->w_height);
2597 min_scrolled += (top_plines - curwin->w_height);
2598 }
2599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
2602 /*
2603 * Stop counting lines to scroll when
2604 * - hitting start of the file
2605 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002606 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * - lines between botline and cursor have been counted
2608 */
2609#ifdef FEAT_FOLDING
2610 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2611#endif
2612 {
2613 loff.lnum = cln;
2614 boff.lnum = cln;
2615 }
2616#ifdef FEAT_DIFF
2617 loff.fill = 0;
2618 boff.fill = 0;
2619 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2620 - curwin->w_filler_rows;
2621#endif
2622
2623 while (loff.lnum > 1)
2624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2626 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002628 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2630 && loff.lnum <= curwin->w_botline
2631#ifdef FEAT_DIFF
2632 && (loff.lnum < curwin->w_botline
2633 || loff.fill >= fill_below_window)
2634#endif
2635 )
2636 break;
2637
Bram Moolenaar85a20022019-12-21 18:25:54 +01002638 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002640 if (loff.height == MAXCOL)
2641 used = MAXCOL;
2642 else
2643 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (used > curwin->w_height)
2645 break;
2646 if (loff.lnum >= curwin->w_botline
2647#ifdef FEAT_DIFF
2648 && (loff.lnum > curwin->w_botline
2649 || loff.fill <= fill_below_window)
2650#endif
2651 )
2652 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 scrolled += loff.height;
2655 if (loff.lnum == curwin->w_botline
2656#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002657 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#endif
2659 )
2660 scrolled -= curwin->w_empty_rows;
2661 }
2662
2663 if (boff.lnum < curbuf->b_ml.ml_line_count)
2664 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002665 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 botline_forw(&boff);
2667 used += boff.height;
2668 if (used > curwin->w_height)
2669 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002670 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2671 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 extra += boff.height;
2674 if (boff.lnum >= curwin->w_botline
2675#ifdef FEAT_DIFF
2676 || (boff.lnum + 1 == curwin->w_botline
2677 && boff.fill > curwin->w_filler_rows)
2678#endif
2679 )
2680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 scrolled += boff.height;
2683 if (boff.lnum == curwin->w_botline
2684#ifdef FEAT_DIFF
2685 && boff.fill == 0
2686#endif
2687 )
2688 scrolled -= curwin->w_empty_rows;
2689 }
2690 }
2691 }
2692 }
2693
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (scrolled <= 0)
2696 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else if (used > curwin->w_height)
2699 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002700 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 else
2702 {
2703 line_count = 0;
2704#ifdef FEAT_DIFF
2705 boff.fill = curwin->w_topfill;
2706#endif
2707 boff.lnum = curwin->w_topline - 1;
2708 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2709 {
2710 botline_forw(&boff);
2711 i += boff.height;
2712 ++line_count;
2713 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 line_count = 9999;
2716 }
2717
2718 /*
2719 * Scroll up if the cursor is off the bottom of the screen a bit.
2720 * Otherwise put it at 1/2 of the screen.
2721 */
2722 if (line_count >= curwin->w_height && line_count > min_scroll)
2723 scroll_cursor_halfway(FALSE);
2724 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002725 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002726 // With 'smoothscroll' scroll at least the height of the cursor line,
2727 // unless it would move the cursor.
2728 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2729 && (curwin->w_cursor.lnum < curwin->w_topline
2730 || (curwin->w_virtcol - curwin->w_skipcol >=
2731 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002732 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002733 if (line_count > 0)
2734 {
2735 if (scrolling_screenlines(TRUE))
2736 scrollup(scrolled, TRUE); // TODO
2737 else
2738 scrollup(line_count, TRUE);
2739 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 /*
2743 * If topline didn't change we need to restore w_botline and w_empty_rows
2744 * (we changed them).
2745 * If topline did change, update_screen() will set botline.
2746 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002747 if (curwin->w_topline == old_topline
2748 && curwin->w_skipcol == old_skipcol
2749 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
2751 curwin->w_botline = old_botline;
2752 curwin->w_empty_rows = old_empty_rows;
2753 curwin->w_valid = old_valid;
2754 }
2755 curwin->w_valid |= VALID_TOPLINE;
2756}
2757
2758/*
2759 * Recompute topline to put the cursor halfway the window
2760 * If "atend" is TRUE, also put it halfway at the end of the file.
2761 */
2762 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002763scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 int above = 0;
2766 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002767 colnr_T skipcol = 0;
2768 int set_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#ifdef FEAT_DIFF
2770 int topfill = 0;
2771#endif
2772 int below = 0;
2773 int used;
2774 lineoff_T loff;
2775 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002776#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002777 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002780#ifdef FEAT_PROP_POPUP
2781 // if the width changed this needs to be updated first
2782 may_update_popup_position();
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2785#ifdef FEAT_FOLDING
2786 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2787#endif
2788#ifdef FEAT_DIFF
2789 used = plines_nofill(loff.lnum);
2790 loff.fill = 0;
2791 boff.fill = 0;
2792#else
2793 used = plines(loff.lnum);
2794#endif
2795 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002796
2797 int half_height = 0;
2798 int smooth_scroll = FALSE;
2799 if (curwin->w_p_sms && curwin->w_p_wrap)
2800 {
2801 // 'smoothscroll' and 'wrap' are set
2802 smooth_scroll = TRUE;
2803 half_height = (curwin->w_height - used) / 2;
2804 used = 0;
2805 }
2806
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 while (topline > 1)
2808 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002809 // If using smoothscroll, we can precisely scroll to the
2810 // exact point where the cursor is halfway down the screen.
2811 if (smooth_scroll)
2812 {
2813 topline_back_winheight(&loff, FALSE);
2814 if (loff.height == MAXCOL)
2815 break;
2816 else
2817 used += loff.height;
2818 if (used > half_height)
2819 {
2820 if (used - loff.height < half_height)
2821 {
2822 int plines_offset = used - half_height;
2823 loff.height -= plines_offset;
2824 used = half_height;
2825
2826 topline = loff.lnum;
2827#ifdef FEAT_DIFF
2828 topfill = loff.fill;
2829#endif
2830 skipcol = skipcol_from_plines(curwin, plines_offset);
2831 set_skipcol = TRUE;
2832 }
2833 break;
2834 }
2835 topline = loff.lnum;
2836#ifdef FEAT_DIFF
2837 topfill = loff.fill;
2838#endif
2839 continue;
2840 }
2841
2842 // If not using smoothscroll, we have to iteratively find how many
2843 // lines to scroll down to roughly fit the cursor.
2844 // This may not be right in the middle if the lines' physical height >
2845 // 1 (e.g. 'wrap' is on).
2846
Bram Moolenaar85a20022019-12-21 18:25:54 +01002847 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 if (boff.lnum < curbuf->b_ml.ml_line_count)
2850 {
2851 botline_forw(&boff);
2852 used += boff.height;
2853 if (used > curwin->w_height)
2854 break;
2855 below += boff.height;
2856 }
2857 else
2858 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (atend)
2861 ++used;
2862 }
2863 }
2864
Bram Moolenaar85a20022019-12-21 18:25:54 +01002865 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
2867 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002868 if (loff.height == MAXCOL)
2869 used = MAXCOL;
2870 else
2871 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (used > curwin->w_height)
2873 break;
2874 above += loff.height;
2875 topline = loff.lnum;
2876#ifdef FEAT_DIFF
2877 topfill = loff.fill;
2878#endif
2879 }
2880 }
2881#ifdef FEAT_FOLDING
2882 if (!hasFolding(topline, &curwin->w_topline, NULL))
2883#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002884 {
2885 if (curwin->w_topline != topline
2886 || set_skipcol
2887 || curwin->w_skipcol != 0)
2888 {
2889 curwin->w_topline = topline;
2890 if (set_skipcol)
2891 {
2892 curwin->w_skipcol = skipcol;
2893 redraw_later(UPD_NOT_VALID);
2894 }
2895 else
2896 reset_skipcol();
2897 }
2898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#ifdef FEAT_DIFF
2900 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002901 if (old_topline > curwin->w_topline + curwin->w_height)
2902 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 check_topfill(curwin, FALSE);
2904#endif
2905 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2906 curwin->w_valid |= VALID_TOPLINE;
2907}
2908
2909/*
2910 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002911 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 * If not possible, put it at the same position as scroll_cursor_halfway().
2913 * When called topline must be valid!
2914 */
2915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002916cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002918 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002920 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 linenr_T botline;
2922 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002923 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002925 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 /*
2928 * How many lines we would like to have above/below the cursor depends on
2929 * whether the first/last line of the file is on screen.
2930 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002931 above_wanted = so;
2932 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002933 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 above_wanted = mouse_dragging - 1;
2936 below_wanted = mouse_dragging - 1;
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (curwin->w_topline == 1)
2939 {
2940 above_wanted = 0;
2941 max_off = curwin->w_height / 2;
2942 if (below_wanted > max_off)
2943 below_wanted = max_off;
2944 }
2945 validate_botline();
2946 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002947 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 below_wanted = 0;
2950 max_off = (curwin->w_height - 1) / 2;
2951 if (above_wanted > max_off)
2952 above_wanted = max_off;
2953 }
2954
2955 /*
2956 * If there are sufficient file-lines above and below the cursor, we can
2957 * return now.
2958 */
2959 cln = curwin->w_cursor.lnum;
2960 if (cln >= curwin->w_topline + above_wanted
2961 && cln < curwin->w_botline - below_wanted
2962#ifdef FEAT_FOLDING
2963 && !hasAnyFolding(curwin)
2964#endif
2965 )
2966 return;
2967
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002968 if (curwin->w_p_sms && !curwin->w_p_wrap)
2969 {
2970 // 'smoothscroll is active
2971 if (curwin->w_cline_height == curwin->w_height)
2972 {
2973 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002974 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002975 return;
2976 }
2977 // TODO: If the cursor line doesn't fit in the window then only adjust
2978 // w_skipcol.
2979 }
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 /*
2982 * Narrow down the area where the cursor can be put by taking lines from
2983 * the top and the bottom until:
2984 * - the desired context lines are found
2985 * - the lines from the top is past the lines from the bottom
2986 */
2987 topline = curwin->w_topline;
2988 botline = curwin->w_botline - 1;
2989#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002990 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 above = curwin->w_topfill;
2992 below = curwin->w_filler_rows;
2993#endif
2994 while ((above < above_wanted || below < below_wanted) && topline < botline)
2995 {
2996 if (below < below_wanted && (below <= above || above >= above_wanted))
2997 {
2998#ifdef FEAT_FOLDING
2999 if (hasFolding(botline, &botline, NULL))
3000 ++below;
3001 else
3002#endif
3003 below += plines(botline);
3004 --botline;
3005 }
3006 if (above < above_wanted && (above < below || below >= below_wanted))
3007 {
3008#ifdef FEAT_FOLDING
3009 if (hasFolding(topline, NULL, &topline))
3010 ++above;
3011 else
3012#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003013 above += PLINES_NOFILL(topline);
3014#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003015 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (topline < botline)
3017 above += diff_check_fill(curwin, topline + 1);
3018#endif
3019 ++topline;
3020 }
3021 }
3022 if (topline == botline || botline == 0)
3023 curwin->w_cursor.lnum = topline;
3024 else if (topline > botline)
3025 curwin->w_cursor.lnum = botline;
3026 else
3027 {
3028 if (cln < topline && curwin->w_topline > 1)
3029 {
3030 curwin->w_cursor.lnum = topline;
3031 curwin->w_valid &=
3032 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3033 }
3034 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3035 {
3036 curwin->w_cursor.lnum = botline;
3037 curwin->w_valid &=
3038 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3039 }
3040 }
3041 curwin->w_valid |= VALID_TOPLINE;
3042}
3043
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003044static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003047 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3048 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003050 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 */
3052 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003053onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 long n;
3056 int retval = OK;
3057 lineoff_T loff;
3058 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003059 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar85a20022019-12-21 18:25:54 +01003061 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 {
3063 beep_flush();
3064 return FAIL;
3065 }
3066
3067 for ( ; count > 0; --count)
3068 {
3069 validate_botline();
3070 /*
3071 * It's an error to move a page up when the first line is already on
3072 * the screen. It's an error to move a page down when the last line
3073 * is on the screen and the topline is 'scrolloff' lines from the
3074 * last line.
3075 */
3076 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003077 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3079 : (curwin->w_topline == 1
3080#ifdef FEAT_DIFF
3081 && curwin->w_topfill ==
3082 diff_check_fill(curwin, curwin->w_topline)
3083#endif
3084 ))
3085 {
3086 beep_flush();
3087 retval = FAIL;
3088 break;
3089 }
3090
3091#ifdef FEAT_DIFF
3092 loff.fill = 0;
3093#endif
3094 if (dir == FORWARD)
3095 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003096 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003099 if (p_window <= 2)
3100 ++curwin->w_topline;
3101 else
3102 curwin->w_topline += p_window - 2;
3103 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3104 curwin->w_topline = curbuf->b_ml.ml_line_count;
3105 curwin->w_cursor.lnum = curwin->w_topline;
3106 }
3107 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3108 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003109 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 curwin->w_topline = curbuf->b_ml.ml_line_count;
3111#ifdef FEAT_DIFF
3112 curwin->w_topfill = 0;
3113#endif
3114 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3115 }
3116 else
3117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003118 // For the overlap, start with the line just below the window
3119 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 loff.lnum = curwin->w_botline;
3121#ifdef FEAT_DIFF
3122 loff.fill = diff_check_fill(curwin, loff.lnum)
3123 - curwin->w_filler_rows;
3124#endif
3125 get_scroll_overlap(&loff, -1);
3126 curwin->w_topline = loff.lnum;
3127#ifdef FEAT_DIFF
3128 curwin->w_topfill = loff.fill;
3129 check_topfill(curwin, FALSE);
3130#endif
3131 curwin->w_cursor.lnum = curwin->w_topline;
3132 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3133 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3134 }
3135 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138#ifdef FEAT_DIFF
3139 if (curwin->w_topline == 1)
3140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 max_topfill();
3143 continue;
3144 }
3145#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003146 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003147 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003148 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003149 if (p_window <= 2)
3150 --curwin->w_topline;
3151 else
3152 curwin->w_topline -= p_window - 2;
3153 if (curwin->w_topline < 1)
3154 curwin->w_topline = 1;
3155 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3156 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3157 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3158 continue;
3159 }
3160
Bram Moolenaar85a20022019-12-21 18:25:54 +01003161 // Find the line at the top of the window that is going to be the
3162 // line at the bottom of the window. Make sure this results in
3163 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 loff.lnum = curwin->w_topline - 1;
3165#ifdef FEAT_DIFF
3166 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3167 - curwin->w_topfill;
3168#endif
3169 get_scroll_overlap(&loff, 1);
3170
3171 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3172 {
3173 loff.lnum = curbuf->b_ml.ml_line_count;
3174#ifdef FEAT_DIFF
3175 loff.fill = 0;
3176 }
3177 else
3178 {
3179 botline_topline(&loff);
3180#endif
3181 }
3182 curwin->w_cursor.lnum = loff.lnum;
3183
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 // Find the line just above the new topline to get the right line
3185 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 n = 0;
3187 while (n <= curwin->w_height && loff.lnum >= 1)
3188 {
3189 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003190 if (loff.height == MAXCOL)
3191 n = MAXCOL;
3192 else
3193 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 curwin->w_topline = 1;
3198#ifdef FEAT_DIFF
3199 max_topfill();
3200#endif
3201 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3202 }
3203 else
3204 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003205 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#ifdef FEAT_DIFF
3207 topline_botline(&loff);
3208#endif
3209 botline_forw(&loff);
3210 botline_forw(&loff);
3211#ifdef FEAT_DIFF
3212 botline_topline(&loff);
3213#endif
3214#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003215 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3217#endif
3218
Bram Moolenaar85a20022019-12-21 18:25:54 +01003219 // Always scroll at least one line. Avoid getting stuck on
3220 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (loff.lnum >= curwin->w_topline
3222#ifdef FEAT_DIFF
3223 && (loff.lnum > curwin->w_topline
3224 || loff.fill >= curwin->w_topfill)
3225#endif
3226 )
3227 {
3228#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003229 // First try using the maximum number of filler lines. If
3230 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 loff.fill = curwin->w_topfill;
3232 if (curwin->w_topfill < diff_check_fill(curwin,
3233 curwin->w_topline))
3234 max_topfill();
3235 if (curwin->w_topfill == loff.fill)
3236#endif
3237 {
3238 --curwin->w_topline;
3239#ifdef FEAT_DIFF
3240 curwin->w_topfill = 0;
3241#endif
3242 }
3243 comp_botline(curwin);
3244 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003245 curwin->w_valid &=
3246 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248 else
3249 {
3250 curwin->w_topline = loff.lnum;
3251#ifdef FEAT_DIFF
3252 curwin->w_topfill = loff.fill;
3253 check_topfill(curwin, FALSE);
3254#endif
3255 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3256 }
3257 }
3258 }
3259 }
3260#ifdef FEAT_FOLDING
3261 foldAdjustCursor();
3262#endif
3263 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003264 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003265 if (retval == OK)
3266 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3268
Bram Moolenaar907dad72018-07-10 15:07:15 +02003269 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003271 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3272 // But make sure we scroll at least one line (happens with mix of long
3273 // wrapping lines and non-wrapping line).
3274 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003276 scroll_cursor_top(1, FALSE);
3277 if (curwin->w_topline <= old_topline
3278 && old_topline < curbuf->b_ml.ml_line_count)
3279 {
3280 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003282 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3283#endif
3284 }
3285 }
3286#ifdef FEAT_FOLDING
3287 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003292 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return retval;
3294}
3295
3296/*
3297 * Decide how much overlap to use for page-up or page-down scrolling.
3298 * This is symmetric, so that doing both keeps the same lines displayed.
3299 * Three lines are examined:
3300 *
3301 * before CTRL-F after CTRL-F / before CTRL-B
3302 * etc. l1
3303 * l1 last but one line ------------
3304 * l2 last text line l2 top text line
3305 * ------------- l3 second text line
3306 * l3 etc.
3307 */
3308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003309get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int h1, h2, h3, h4;
3312 int min_height = curwin->w_height - 2;
3313 lineoff_T loff0, loff1, loff2;
3314
3315#ifdef FEAT_DIFF
3316 if (lp->fill > 0)
3317 lp->height = 1;
3318 else
3319 lp->height = plines_nofill(lp->lnum);
3320#else
3321 lp->height = plines(lp->lnum);
3322#endif
3323 h1 = lp->height;
3324 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003325 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 loff0 = *lp;
3328 if (dir > 0)
3329 botline_forw(lp);
3330 else
3331 topline_back(lp);
3332 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003333 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003335 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return;
3337 }
3338
3339 loff1 = *lp;
3340 if (dir > 0)
3341 botline_forw(lp);
3342 else
3343 topline_back(lp);
3344 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003345 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003347 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 return;
3349 }
3350
3351 loff2 = *lp;
3352 if (dir > 0)
3353 botline_forw(lp);
3354 else
3355 topline_back(lp);
3356 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003357 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003360 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361}
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363/*
3364 * Scroll 'scroll' lines up or down.
3365 */
3366 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003367halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 long scrolled = 0;
3370 int i;
3371 int n;
3372 int room;
3373
3374 if (Prenum)
3375 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3376 curwin->w_height : Prenum;
3377 n = (curwin->w_p_scr <= curwin->w_height) ?
3378 curwin->w_p_scr : curwin->w_height;
3379
Bram Moolenaard5d37532017-03-27 23:02:07 +02003380 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 validate_botline();
3382 room = curwin->w_empty_rows;
3383#ifdef FEAT_DIFF
3384 room += curwin->w_filler_rows;
3385#endif
3386 if (flag)
3387 {
3388 /*
3389 * scroll the text up
3390 */
3391 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3392 {
3393#ifdef FEAT_DIFF
3394 if (curwin->w_topfill > 0)
3395 {
3396 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003397 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 --curwin->w_topfill;
3399 }
3400 else
3401#endif
3402 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003403 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 n -= i;
3405 if (n < 0 && scrolled > 0)
3406 break;
3407#ifdef FEAT_FOLDING
3408 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3409#endif
3410 ++curwin->w_topline;
3411#ifdef FEAT_DIFF
3412 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3413#endif
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3416 {
3417 ++curwin->w_cursor.lnum;
3418 curwin->w_valid &=
3419 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3423 scrolled += i;
3424
3425 /*
3426 * Correct w_botline for changed w_topline.
3427 * Won't work when there are filler lines.
3428 */
3429#ifdef FEAT_DIFF
3430 if (curwin->w_p_diff)
3431 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3432 else
3433#endif
3434 {
3435 room += i;
3436 do
3437 {
3438 i = plines(curwin->w_botline);
3439 if (i > room)
3440 break;
3441#ifdef FEAT_FOLDING
3442 (void)hasFolding(curwin->w_botline, NULL,
3443 &curwin->w_botline);
3444#endif
3445 ++curwin->w_botline;
3446 room -= i;
3447 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3448 }
3449 }
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 /*
3452 * When hit bottom of the file: move cursor down.
3453 */
3454 if (n > 0)
3455 {
3456# ifdef FEAT_FOLDING
3457 if (hasAnyFolding(curwin))
3458 {
3459 while (--n >= 0
3460 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3461 {
3462 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3463 &curwin->w_cursor.lnum);
3464 ++curwin->w_cursor.lnum;
3465 }
3466 }
3467 else
3468# endif
3469 curwin->w_cursor.lnum += n;
3470 check_cursor_lnum();
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 else
3474 {
3475 /*
3476 * scroll the text down
3477 */
3478 while (n > 0 && curwin->w_topline > 1)
3479 {
3480#ifdef FEAT_DIFF
3481 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3482 {
3483 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003484 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 ++curwin->w_topfill;
3486 }
3487 else
3488#endif
3489 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003490 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 n -= i;
3492 if (n < 0 && scrolled > 0)
3493 break;
3494 --curwin->w_topline;
3495#ifdef FEAT_FOLDING
3496 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3497#endif
3498#ifdef FEAT_DIFF
3499 curwin->w_topfill = 0;
3500#endif
3501 }
3502 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3503 VALID_BOTLINE|VALID_BOTLINE_AP);
3504 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (curwin->w_cursor.lnum > 1)
3506 {
3507 --curwin->w_cursor.lnum;
3508 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 /*
3513 * When hit top of the file: move cursor up.
3514 */
3515 if (n > 0)
3516 {
3517 if (curwin->w_cursor.lnum <= (linenr_T)n)
3518 curwin->w_cursor.lnum = 1;
3519 else
3520# ifdef FEAT_FOLDING
3521 if (hasAnyFolding(curwin))
3522 {
3523 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3524 {
3525 --curwin->w_cursor.lnum;
3526 (void)hasFolding(curwin->w_cursor.lnum,
3527 &curwin->w_cursor.lnum, NULL);
3528 }
3529 }
3530 else
3531# endif
3532 curwin->w_cursor.lnum -= n;
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003536 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 foldAdjustCursor();
3538# endif
3539#ifdef FEAT_DIFF
3540 check_topfill(curwin, !flag);
3541#endif
3542 cursor_correct();
3543 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003544 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546
Bram Moolenaar860cae12010-06-05 23:22:07 +02003547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003548do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003549{
3550 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003551 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003552 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003553 colnr_T curswant = curwin->w_curswant;
3554 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 win_T *old_curwin = curwin;
3556 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003557 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558 int old_VIsual_select = VIsual_select;
3559 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560
3561 /*
3562 * loop through the cursorbound windows
3563 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003564 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003565 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003566 {
3567 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003568 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 if (curwin != old_curwin && curwin->w_p_crb)
3570 {
3571# ifdef FEAT_DIFF
3572 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003573 curwin->w_cursor.lnum =
3574 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003575 else
3576# endif
3577 curwin->w_cursor.lnum = line;
3578 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003579 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003580 curwin->w_curswant = curswant;
3581 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582
Bram Moolenaar85a20022019-12-21 18:25:54 +01003583 // Make sure the cursor is in a valid position. Temporarily set
3584 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003585 restart_edit_save = restart_edit;
3586 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003588
3589 // Avoid a scroll here for the cursor position, 'scrollbind' is
3590 // more important.
3591 if (!curwin->w_p_scb)
3592 validate_cursor();
3593
Bram Moolenaar61452852011-02-01 18:01:11 +01003594 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003595 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003596 if (has_mbyte)
3597 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003598 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003599
Bram Moolenaar85a20022019-12-21 18:25:54 +01003600 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003601 if (!curwin->w_p_scb)
3602 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003603 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 }
3605 }
3606
3607 /*
3608 * reset current-window
3609 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610 VIsual_select = old_VIsual_select;
3611 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 curwin = old_curwin;
3613 curbuf = old_curbuf;
3614}