blob: c7fbc9defb0aa2c907ac7b34d9093ecc726d1b29 [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{
247 if (curwin->w_skipcol != 0)
248 {
249 curwin->w_skipcol = 0;
250
251 // Should use the least expensive way that displays all that changed.
252 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
253 // enough when the top line gets another screen line.
254 redraw_later(UPD_SOME_VALID);
255 }
256}
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);
983 if (!(wp->w_valid & VALID_VIRTCOL))
984 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100985#ifdef FEAT_PROP_POPUP
986 wp->w_virtcol_first_char = 0;
987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000989#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100990 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000991#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100992 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994}
995
996/*
997 * Validate curwin->w_cline_height only.
998 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 check_cursor_moved(curwin);
1003 if (!(curwin->w_valid & VALID_CHEIGHT))
1004 {
1005#ifdef FEAT_DIFF
1006 if (curwin->w_cursor.lnum == curwin->w_topline)
1007 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1008 + curwin->w_topfill;
1009 else
1010#endif
1011 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
1012#ifdef FEAT_FOLDING
1013 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
1014#endif
1015 curwin->w_valid |= VALID_CHEIGHT;
1016 }
1017}
1018
1019/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001020 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 */
1022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001023validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 colnr_T off;
1026 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001027 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 validate_virtcol();
1030 if (!(curwin->w_valid & VALID_WCOL))
1031 {
1032 col = curwin->w_virtcol;
1033 off = curwin_col_off();
1034 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +02001035 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +00001038 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +02001039 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +01001040 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001041 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +02001042 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001043 if (col > (int)curwin->w_leftcol)
1044 col -= curwin->w_leftcol;
1045 else
1046 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001050#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01001051 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054}
1055
1056/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001057 * Compute offset of a window, occupied by absolute or relative line number,
1058 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
1060 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar64486672010-05-16 15:46:46 +02001063 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_FOLDING
1066 + wp->w_p_fdc
1067#endif
1068#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001069 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#endif
1071 );
1072}
1073
1074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 return win_col_off(curwin);
1078}
1079
1080/*
1081 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001082 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1083 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 */
1085 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001086win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar64486672010-05-16 15:46:46 +02001088 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001089 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 return 0;
1091}
1092
1093 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001094curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
1096 return win_col_off2(curwin);
1097}
1098
1099/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001100 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 * Also updates curwin->w_wrow and curwin->w_cline_row.
1102 * Also updates curwin->w_leftcol.
1103 */
1104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001105curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001106 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001109 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int off_left, off_right;
1111 int n;
1112 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001113 int width1; // text width for first screen line
1114 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int new_leftcol;
1116 colnr_T startcol;
1117 colnr_T endcol;
1118 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001119 long so = get_scrolloff_value();
1120 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001121 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
1123 /*
1124 * First make sure that w_topline is valid (after moving the cursor).
1125 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001126 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 /*
1129 * Next make sure that w_cline_row is valid.
1130 */
1131 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001132 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001134#ifdef FEAT_PROP_POPUP
1135 // will be set by getvvcol() but not reset
1136 curwin->w_virtcol_first_char = 0;
1137#endif
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 /*
1140 * Compute the number of virtual columns.
1141 */
1142#ifdef FEAT_FOLDING
1143 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1146 else
1147#endif
1148 getvvcol(curwin, &curwin->w_cursor,
1149 &startcol, &(curwin->w_virtcol), &endcol);
1150
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001153 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 extra = curwin_col_off();
1156 curwin->w_wcol = curwin->w_virtcol + extra;
1157 endcol += extra;
1158
1159 /*
1160 * Now compute w_wrow, counting screen lines from w_cline_row.
1161 */
1162 curwin->w_wrow = curwin->w_cline_row;
1163
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001164 width1 = curwin->w_width - extra;
1165 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001167 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001168 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001169 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001170 if (curwin->w_p_wrap)
1171 curwin->w_wrow = curwin->w_height - 1;
1172 else
1173 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001175 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001177 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001179 // skip columns that are not visible
1180 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001181 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001182 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001183 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001184 // Deduct by multiples of width2. This allows the long line
1185 // wrapping formula below to correctly calculate the w_wcol value
1186 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001187 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001188 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001189 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001190 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001191 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001192
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001193 did_sub_skipcol = TRUE;
1194 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001195
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001197 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001199 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1201 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 curwin->w_wrow += n;
1203
1204#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // When cursor wraps to first char of next line in Insert
1206 // mode, the 'showbreak' string isn't shown, backup to first
1207 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001208 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001209 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001210 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 curwin->w_wcol = 0;
1212#endif
1213 }
1214 }
1215
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1217 // is not folded.
1218 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001219 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_FOLDING
1221 && !curwin->w_cline_folded
1222#endif
1223 )
1224 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001225#ifdef FEAT_PROP_POPUP
1226 if (curwin->w_virtcol_first_char > 0)
1227 {
1228 int cols = (curwin->w_width - extra);
1229 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1230
1231 // each "above" text prop shifts the text one row down
1232 curwin->w_wrow += rows;
1233 curwin->w_wcol -= rows * cols;
1234 endcol -= rows * cols;
1235 curwin->w_cline_height = rows + 1;
1236 }
1237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 /*
1239 * If Cursor is left of the screen, scroll rightwards.
1240 * If Cursor is right of the screen, scroll leftwards
1241 * If we get closer to the edge than 'sidescrolloff', scroll a little
1242 * extra
1243 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001244 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001245 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001246 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (off_left < 0 || off_right > 0)
1248 {
1249 if (off_left < 0)
1250 diff = -off_left;
1251 else
1252 diff = off_right;
1253
Bram Moolenaar85a20022019-12-21 18:25:54 +01001254 // When far off or not enough room on either side, put cursor in
1255 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001256 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1257 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else
1259 {
1260 if (diff < p_ss)
1261 diff = p_ss;
1262 if (off_left < 0)
1263 new_leftcol = curwin->w_leftcol - diff;
1264 else
1265 new_leftcol = curwin->w_leftcol + diff;
1266 }
1267 if (new_leftcol < 0)
1268 new_leftcol = 0;
1269 if (new_leftcol != (int)curwin->w_leftcol)
1270 {
1271 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001272 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001273 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
1276 curwin->w_wcol -= curwin->w_leftcol;
1277 }
1278 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1279 curwin->w_wcol -= curwin->w_leftcol;
1280 else
1281 curwin->w_wcol = 0;
1282
1283#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001284 // Skip over filler lines. At the top use w_topfill, there
1285 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (curwin->w_cursor.lnum == curwin->w_topline)
1287 curwin->w_wrow += curwin->w_topfill;
1288 else
1289 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1290#endif
1291
1292 prev_skipcol = curwin->w_skipcol;
1293
1294 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if ((curwin->w_wrow >= curwin->w_height
1297 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001298 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 && (p_lines =
1300#ifdef FEAT_DIFF
1301 plines_win_nofill
1302#else
1303 plines_win
1304#endif
1305 (curwin, curwin->w_cursor.lnum, FALSE))
1306 - 1 >= curwin->w_height))
1307 && curwin->w_height != 0
1308 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001309 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001310 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001312 // Cursor past end of screen. Happens with a single line that does
1313 // not fit on screen. Find a skipcol to show the text around the
1314 // cursor. Avoid scrolling all the time. compute value of "extra":
1315 // 1: Less than 'scrolloff' lines above
1316 // 2: Less than 'scrolloff' lines below
1317 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001319 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 // Compute last display line of the buffer line that we want at the
1322 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (p_lines == 0)
1324 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1325 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001326 if (p_lines > curwin->w_wrow + so)
1327 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 else
1329 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001330 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 extra += 2;
1332
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001333 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001336 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (n > curwin->w_height / 2)
1338 n -= curwin->w_height / 2;
1339 else
1340 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (n > p_lines - curwin->w_height + 1)
1343 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001344 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 else if (extra == 1)
1347 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001348 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001349 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1350 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (extra > 0)
1352 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001353 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1354 extra = curwin->w_skipcol / width2;
1355 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 }
1358 else if (extra == 2)
1359 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001360 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001361 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001363 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (endcol > curwin->w_skipcol)
1365 curwin->w_skipcol = endcol;
1366 }
1367
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001368 // adjust w_wrow for the changed w_skipcol
1369 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001372 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (curwin->w_wrow >= curwin->w_height)
1375 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001376 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001378 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 curwin->w_wrow -= extra;
1380 }
1381
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001382 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (extra > 0)
1384 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1385 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001386 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001388 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 curwin->w_skipcol = 0;
1390 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001391 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001393#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001394 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001395#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001396#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1397 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1398 {
1399 curwin->w_wrow += popup_top_extra(curwin);
1400 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001401 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001402 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001403 else
1404 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001405#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001406
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001407 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1408 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001409 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001410 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1413}
1414
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001415#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001416/*
1417 * Compute the screen position of text character at "pos" in window "wp"
1418 * The resulting values are one-based, zero when character is not visible.
1419 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001420 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001421textpos2screenpos(
1422 win_T *wp,
1423 pos_T *pos,
1424 int *rowp, // screen row
1425 int *scolp, // start screen column
1426 int *ccolp, // cursor screen column
1427 int *ecolp) // end screen column
1428{
1429 colnr_T scol = 0, ccol = 0, ecol = 0;
1430 int row = 0;
1431 int rowoff = 0;
1432 colnr_T coloff = 0;
1433
Bram Moolenaar189663b2021-07-21 18:04:56 +02001434 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001435 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001436 colnr_T col;
1437 int width;
1438 linenr_T lnum = pos->lnum;
1439#ifdef FEAT_FOLDING
1440 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001441
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001442 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1443#endif
1444 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001445
1446#ifdef FEAT_DIFF
1447 // Add filler lines above this buffer line.
1448 row += diff_check_fill(wp, lnum);
1449#endif
1450
zeertzjqba2d1912022-12-18 12:28:59 +00001451 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001452#ifdef FEAT_FOLDING
1453 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001454 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001455 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001456 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001457 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001458 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001459#endif
1460 {
1461 getvcol(wp, pos, &scol, &ccol, &ecol);
1462
1463 // similar to what is done in validate_cursor_col()
1464 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001465 col += off;
1466 width = wp->w_width - off + win_col_off2(wp);
1467
1468 // long line wrapping, adjust row
1469 if (wp->w_p_wrap
1470 && col >= (colnr_T)wp->w_width
1471 && width > 0)
1472 {
1473 // use same formula as what is used in curs_columns()
1474 rowoff = ((col - wp->w_width) / width + 1);
1475 col -= rowoff * width;
1476 }
1477 col -= wp->w_leftcol;
1478 if (col >= wp->w_width)
1479 col = -1;
1480 if (col >= 0 && row + rowoff <= wp->w_height)
1481 {
1482 coloff = col - scol + wp->w_wincol + 1;
1483 row += W_WINROW(wp);
1484 }
1485 else
1486 // character is left, right or below of the window
1487 row = rowoff = scol = ccol = ecol = 0;
1488 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001489 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001490 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001491 *scolp = scol + coloff;
1492 *ccolp = ccol + coloff;
1493 *ecolp = ecol + coloff;
1494}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001495#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001496
Bram Moolenaar12034e22019-08-25 22:25:02 +02001497#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498/*
1499 * "screenpos({winid}, {lnum}, {col})" function
1500 */
1501 void
1502f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1503{
1504 dict_T *dict;
1505 win_T *wp;
1506 pos_T pos;
1507 int row = 0;
1508 int scol = 0, ccol = 0, ecol = 0;
1509
Bram Moolenaar93a10962022-06-16 11:42:09 +01001510 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001511 return;
1512 dict = rettv->vval.v_dict;
1513
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001514 if (in_vim9script()
1515 && (check_for_number_arg(argvars, 0) == FAIL
1516 || check_for_number_arg(argvars, 1) == FAIL
1517 || check_for_number_arg(argvars, 2) == FAIL))
1518 return;
1519
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001520 wp = find_win_by_nr_or_id(&argvars[0]);
1521 if (wp == NULL)
1522 return;
1523
1524 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001525 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1526 {
1527 semsg(_(e_invalid_line_number_nr), pos.lnum);
1528 return;
1529 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530 pos.col = tv_get_number(&argvars[2]) - 1;
1531 pos.coladd = 0;
1532 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1533
1534 dict_add_number(dict, "row", row);
1535 dict_add_number(dict, "col", scol);
1536 dict_add_number(dict, "curscol", ccol);
1537 dict_add_number(dict, "endcol", ecol);
1538}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001539
1540/*
1541 * "virtcol2col({winid}, {lnum}, {col})" function
1542 */
1543 void
1544f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1545{
1546 win_T *wp;
1547 linenr_T lnum;
1548 int screencol;
1549 int error = FALSE;
1550
1551 rettv->vval.v_number = -1;
1552
1553 if (check_for_number_arg(argvars, 0) == FAIL
1554 || check_for_number_arg(argvars, 1) == FAIL
1555 || check_for_number_arg(argvars, 2) == FAIL)
1556 return;
1557
1558 wp = find_win_by_nr_or_id(&argvars[0]);
1559 if (wp == NULL)
1560 return;
1561
1562 lnum = tv_get_number_chk(&argvars[1], &error);
1563 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1564 return;
1565
1566 screencol = tv_get_number_chk(&argvars[2], &error);
1567 if (error || screencol < 0)
1568 return;
1569
1570 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1571}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001572#endif
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
1575 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001578scrolldown(
1579 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001580 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001582 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 int wrow;
1584 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001585 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001586 int width1 = 0;
1587 int width2 = 0;
1588
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001589 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001590 {
1591 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001592 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595#ifdef FEAT_FOLDING
1596 linenr_T first;
1597
Bram Moolenaar85a20022019-12-21 18:25:54 +01001598 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1600#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001602 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001605 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1606 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 ++curwin->w_topfill;
1609 ++done;
1610 }
1611 else
1612#endif
1613 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001614 // break when at the very top
1615 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001616 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001618 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001620 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001621 if (curwin->w_skipcol >= width1 + width2)
1622 curwin->w_skipcol -= width2;
1623 else
1624 curwin->w_skipcol -= width1;
1625 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
1628 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001629 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001630 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001631 --curwin->w_topline;
1632 curwin->w_skipcol = 0;
1633#ifdef FEAT_DIFF
1634 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001636#ifdef FEAT_FOLDING
1637 // A sequence of folded lines only counts for one logical line
1638 if (hasFolding(curwin->w_topline, &first, NULL))
1639 {
1640 ++done;
1641 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001642 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001643 curwin->w_botline -= curwin->w_topline - first;
1644 curwin->w_topline = first;
1645 }
1646 else
1647#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001648 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001649 {
1650 int size = win_linetabsize(curwin, curwin->w_topline,
1651 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1652 if (size > width1)
1653 {
1654 curwin->w_skipcol = width1;
1655 size -= width1;
1656 redraw_later(UPD_NOT_VALID);
1657 }
1658 while (size > width2)
1659 {
1660 curwin->w_skipcol += width2;
1661 size -= width2;
1662 }
1663 ++done;
1664 }
1665 else
1666 done += PLINES_NOFILL(curwin->w_topline);
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001669 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 invalidate_botline();
1671 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001672 curwin->w_wrow += done; // keep w_wrow updated
1673 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675#ifdef FEAT_DIFF
1676 if (curwin->w_cursor.lnum == curwin->w_topline)
1677 curwin->w_cline_row = 0;
1678 check_topfill(curwin, TRUE);
1679#endif
1680
1681 /*
1682 * Compute the row number of the last row of the cursor line
1683 * and move the cursor onto the displayed part of the window.
1684 */
1685 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001686 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 validate_virtcol();
1689 validate_cheight();
1690 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001691 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1694 {
1695#ifdef FEAT_FOLDING
1696 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1697 {
1698 --wrow;
1699 if (first == 1)
1700 curwin->w_cursor.lnum = 1;
1701 else
1702 curwin->w_cursor.lnum = first - 1;
1703 }
1704 else
1705#endif
1706 wrow -= plines(curwin->w_cursor.lnum--);
1707 curwin->w_valid &=
1708 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1709 moved = TRUE;
1710 }
1711 if (moved)
1712 {
1713#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001714 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 foldAdjustCursor();
1716#endif
1717 coladvance(curwin->w_curswant);
1718 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001719
1720 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1721 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001722 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001723 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1724
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001725 // make sure the cursor is in the visible text
1726 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001727 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001728 int row = 0;
1729 if (col >= width1)
1730 {
1731 col -= width1;
1732 ++row;
1733 }
1734 if (col > width2)
1735 {
1736 row += col / width2;
1737 col = col % width2;
1738 }
1739 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001740 {
1741 curwin->w_curswant = curwin->w_virtcol
1742 - (row - curwin->w_height + 1) * width2;
1743 coladvance(curwin->w_curswant);
1744 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
1747
1748/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001749 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1750 */
1751 static int
1752scrolling_screenlines(int byfold UNUSED)
1753{
1754 return (curwin->w_p_wrap && curwin->w_p_sms)
1755# ifdef FEAT_FOLDING
1756 || (byfold && hasAnyFolding(curwin))
1757# endif
1758# ifdef FEAT_DIFF
1759 || curwin->w_p_diff
1760# endif
1761 ;
1762}
1763
1764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001768scrollup(
1769 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001772 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001774 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001776 int width1 = curwin->w_width - curwin_col_off();
1777 int width2 = width1 + curwin_col_off2();
1778 int size = 0;
1779 linenr_T prev_topline = curwin->w_topline;
1780
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001781 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001782 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001783
1784 // diff mode: first consume "topfill"
1785 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1786 // the line, then advance to the next line.
1787 // folding: count each sequence of folded lines as one logical line.
1788 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790# ifdef FEAT_DIFF
1791 if (curwin->w_topfill > 0)
1792 --curwin->w_topfill;
1793 else
1794# endif
1795 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001796 linenr_T lnum = curwin->w_topline;
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798# ifdef FEAT_FOLDING
1799 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 (void)hasFolding(lnum, NULL, &lnum);
1802# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001803 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001804 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001805 // 'smoothscroll': increase "w_skipcol" until it goes over
1806 // the end of the line, then advance to the next line.
1807 int add = curwin->w_skipcol > 0 ? width2 : width1;
1808 curwin->w_skipcol += add;
1809 if (curwin->w_skipcol >= size)
1810 {
1811 if (lnum == curbuf->b_ml.ml_line_count)
1812 {
1813 // at the last screen line, can't scroll further
1814 curwin->w_skipcol -= add;
1815 break;
1816 }
1817 ++lnum;
1818 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001819 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001820 else
1821 {
1822 if (lnum >= curbuf->b_ml.ml_line_count)
1823 break;
1824 ++lnum;
1825 }
1826
1827 if (lnum > curwin->w_topline)
1828 {
1829 // approximate w_botline
1830 curwin->w_botline += lnum - curwin->w_topline;
1831 curwin->w_topline = lnum;
1832# ifdef FEAT_DIFF
1833 curwin->w_topfill = diff_check_fill(curwin, lnum);
1834# endif
1835 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001836 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001837 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001838 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001839 }
1840 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 if (curwin->w_topline == prev_topline)
1843 // need to redraw even though w_topline didn't change
1844 redraw_later(UPD_NOT_VALID);
1845 }
1846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001849 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851
1852 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1853 curwin->w_topline = curbuf->b_ml.ml_line_count;
1854 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1855 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1856
1857#ifdef FEAT_DIFF
1858 check_topfill(curwin, FALSE);
1859#endif
1860
1861#ifdef FEAT_FOLDING
1862 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001863 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1865#endif
1866
1867 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1868 if (curwin->w_cursor.lnum < curwin->w_topline)
1869 {
1870 curwin->w_cursor.lnum = curwin->w_topline;
1871 curwin->w_valid &=
1872 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1873 coladvance(curwin->w_curswant);
1874 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001875 if (curwin->w_cursor.lnum == curwin->w_topline
1876 && do_sms && curwin->w_skipcol > 0)
1877 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001878 int col_off = curwin_col_off();
1879 int col_off2 = curwin_col_off2();
1880
1881 int width1 = curwin->w_width - col_off;
1882 int width2 = width1 + col_off2;
1883 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001884 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001885 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001886 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001887
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001888 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1889 // going past it anyway.
1890 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1891 smoothscroll_marker_overlap(extra2);
1892
Bram Moolenaar118c2352022-10-09 17:19:27 +01001893 // Make sure the cursor is in a visible part of the line, taking
1894 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001895 // If there are not enough screen lines put the cursor in the middle.
1896 if (scrolloff_cols > space_cols / 2)
1897 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001898 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001899 if (curwin->w_virtcol < curwin->w_skipcol
1900 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001901 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001902 colnr_T col = curwin->w_virtcol;
1903
1904 if (col < width1)
1905 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001906 while (col < curwin->w_skipcol
1907 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001909 curwin->w_curswant = col;
1910 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001911
1912 // validate_virtcol() marked various things as valid, but after
1913 // moving the cursor they need to be recomputed
1914 curwin->w_valid &=
1915 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001916 }
1917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918}
1919
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001920/*
1921 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1922 * valid for 'smoothscroll'.
1923 */
1924 void
1925adjust_skipcol(void)
1926{
1927 if (!curwin->w_p_wrap
1928 || !curwin->w_p_sms
1929 || curwin->w_cursor.lnum != curwin->w_topline)
1930 return;
1931
1932 int width1 = curwin->w_width - curwin_col_off();
1933 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001934 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001935 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1936 int scrolled = FALSE;
1937
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001938 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001939 if (curwin->w_cline_height == curwin->w_height
1940 // w_cline_height may be capped at w_height, check there aren't
1941 // actually more lines.
1942 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1943 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001944 {
1945 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001946 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001947 return;
1948 }
1949
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001950 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001951 while (curwin->w_skipcol > 0
1952 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1953 {
1954 // scroll a screen line down
1955 if (curwin->w_skipcol >= width1 + width2)
1956 curwin->w_skipcol -= width2;
1957 else
1958 curwin->w_skipcol -= width1;
1959 redraw_later(UPD_NOT_VALID);
1960 scrolled = TRUE;
1961 validate_virtcol();
1962 }
1963 if (scrolled)
1964 return; // don't scroll in the other direction now
1965
1966 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1967 int row = 0;
1968 if (col >= width1)
1969 {
1970 col -= width1;
1971 ++row;
1972 }
1973 if (col > width2)
1974 {
1975 row += col / width2;
1976 col = col % width2;
1977 }
1978 if (row >= curwin->w_height)
1979 {
1980 if (curwin->w_skipcol == 0)
1981 {
1982 curwin->w_skipcol += width1;
1983 --row;
1984 }
1985 if (row >= curwin->w_height)
1986 curwin->w_skipcol += (row - curwin->w_height) * width2;
1987 redraw_later(UPD_NOT_VALID);
1988 }
1989}
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#ifdef FEAT_DIFF
1992/*
1993 * Don't end up with too many filler lines in the window.
1994 */
1995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996check_topfill(
1997 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001998 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 int n;
2001
2002 if (wp->w_topfill > 0)
2003 {
2004 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2005 if (wp->w_topfill + n > wp->w_height)
2006 {
2007 if (down && wp->w_topline > 1)
2008 {
2009 --wp->w_topline;
2010 wp->w_topfill = 0;
2011 }
2012 else
2013 {
2014 wp->w_topfill = wp->w_height - n;
2015 if (wp->w_topfill < 0)
2016 wp->w_topfill = 0;
2017 }
2018 }
2019 }
2020}
2021
2022/*
2023 * Use as many filler lines as possible for w_topline. Make sure w_topline
2024 * is still visible.
2025 */
2026 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002027max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int n;
2030
2031 n = plines_nofill(curwin->w_topline);
2032 if (n >= curwin->w_height)
2033 curwin->w_topfill = 0;
2034 else
2035 {
2036 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2037 if (curwin->w_topfill + n > curwin->w_height)
2038 curwin->w_topfill = curwin->w_height - n;
2039 }
2040}
2041#endif
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043/*
2044 * Scroll the screen one line down, but don't do it if it would move the
2045 * cursor off the screen.
2046 */
2047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int end_row;
2051#ifdef FEAT_DIFF
2052 int can_fill = (curwin->w_topfill
2053 < diff_check_fill(curwin, curwin->w_topline));
2054#endif
2055
2056 if (curwin->w_topline <= 1
2057#ifdef FEAT_DIFF
2058 && !can_fill
2059#endif
2060 )
2061 return;
2062
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 /*
2066 * Compute the row number of the last row of the cursor line
2067 * and make sure it doesn't go off the screen. Make sure the cursor
2068 * doesn't go past 'scrolloff' lines from the screen end.
2069 */
2070 end_row = curwin->w_wrow;
2071#ifdef FEAT_DIFF
2072 if (can_fill)
2073 ++end_row;
2074 else
2075 end_row += plines_nofill(curwin->w_topline - 1);
2076#else
2077 end_row += plines(curwin->w_topline - 1);
2078#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002079 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 validate_cheight();
2082 validate_virtcol();
2083 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002084 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002086 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088#ifdef FEAT_DIFF
2089 if (can_fill)
2090 {
2091 ++curwin->w_topfill;
2092 check_topfill(curwin, TRUE);
2093 }
2094 else
2095 {
2096 --curwin->w_topline;
2097 curwin->w_topfill = 0;
2098 }
2099#else
2100 --curwin->w_topline;
2101#endif
2102#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002103 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002105 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2107 }
2108}
2109
2110/*
2111 * Scroll the screen one line up, but don't do it if it would move the cursor
2112 * off the screen.
2113 */
2114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002115scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 int start_row;
2118
2119 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2120#ifdef FEAT_DIFF
2121 && curwin->w_topfill == 0
2122#endif
2123 )
2124 return;
2125
Bram Moolenaar85a20022019-12-21 18:25:54 +01002126 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 /*
2129 * Compute the row number of the first row of the cursor line
2130 * and make sure it doesn't go off the screen. Make sure the cursor
2131 * doesn't go before 'scrolloff' lines from the screen start.
2132 */
2133#ifdef FEAT_DIFF
2134 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2135 - curwin->w_topfill;
2136#else
2137 start_row = curwin->w_wrow - plines(curwin->w_topline);
2138#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002139 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002142 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002144 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146#ifdef FEAT_DIFF
2147 if (curwin->w_topfill > 0)
2148 --curwin->w_topfill;
2149 else
2150#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002151 {
2152#ifdef FEAT_FOLDING
2153 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002156 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2159 }
2160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162/*
2163 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2164 * a (wrapped) text line. Uses and sets "lp->fill".
2165 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002166 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
2168 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002169topline_back_winheight(
2170 lineoff_T *lp,
2171 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173#ifdef FEAT_DIFF
2174 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002176 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ++lp->fill;
2178 lp->height = 1;
2179 }
2180 else
2181#endif
2182 {
2183 --lp->lnum;
2184#ifdef FEAT_DIFF
2185 lp->fill = 0;
2186#endif
2187 if (lp->lnum < 1)
2188 lp->height = MAXCOL;
2189 else
2190#ifdef FEAT_FOLDING
2191 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 lp->height = 1;
2194 else
2195#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002196 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198}
2199
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002200 static void
2201topline_back(lineoff_T *lp)
2202{
2203 topline_back_winheight(lp, TRUE);
2204}
2205
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207/*
2208 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2209 * a (wrapped) text line. Uses and sets "lp->fill".
2210 * Returns the height of the added line in "lp->height".
2211 * Lines below the last one are incredibly high.
2212 */
2213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002214botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216#ifdef FEAT_DIFF
2217 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2218 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002219 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 ++lp->fill;
2221 lp->height = 1;
2222 }
2223 else
2224#endif
2225 {
2226 ++lp->lnum;
2227#ifdef FEAT_DIFF
2228 lp->fill = 0;
2229#endif
2230 if (lp->lnum > curbuf->b_ml.ml_line_count)
2231 lp->height = MAXCOL;
2232 else
2233#ifdef FEAT_FOLDING
2234 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 lp->height = 1;
2237 else
2238#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002239 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241}
2242
2243#ifdef FEAT_DIFF
2244/*
2245 * Switch from including filler lines below lp->lnum to including filler
2246 * lines above loff.lnum + 1. This keeps pointing to the same line.
2247 * When there are no filler lines nothing changes.
2248 */
2249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002250botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 if (lp->fill > 0)
2253 {
2254 ++lp->lnum;
2255 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2256 }
2257}
2258
2259/*
2260 * Switch from including filler lines above lp->lnum to including filler
2261 * lines below loff.lnum - 1. This keeps pointing to the same line.
2262 * When there are no filler lines nothing changes.
2263 */
2264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002265topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 if (lp->fill > 0)
2268 {
2269 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2270 --lp->lnum;
2271 }
2272}
2273#endif
2274
2275/*
2276 * Recompute topline to put the cursor at the top of the window.
2277 * Scroll at least "min_scroll" lines.
2278 * If "always" is TRUE, always set topline (for "zt").
2279 */
2280 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002281scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 int scrolled = 0;
2284 int extra = 0;
2285 int used;
2286 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002287 linenr_T top; // just above displayed lines
2288 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002290 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#ifdef FEAT_DIFF
2292 linenr_T old_topfill = curwin->w_topfill;
2293#endif
2294 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002295 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (mouse_dragging > 0)
2298 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300 /*
2301 * Decrease topline until:
2302 * - it has become 1
2303 * - (part of) the cursor line is moved off the screen or
2304 * - moved at least 'scrolljump' lines and
2305 * - at least 'scrolloff' lines above and below the cursor
2306 */
2307 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (curwin->w_cursor.lnum < curwin->w_topline)
2310 scrolled = used;
2311
2312#ifdef FEAT_FOLDING
2313 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2314 {
2315 --top;
2316 ++bot;
2317 }
2318 else
2319#endif
2320 {
2321 top = curwin->w_cursor.lnum - 1;
2322 bot = curwin->w_cursor.lnum + 1;
2323 }
2324 new_topline = top + 1;
2325
2326#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002327 // "used" already contains the number of filler lines above, don't add it
2328 // again.
2329 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002330 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332
2333 /*
2334 * Check if the lines from "top" to "bot" fit in the window. If they do,
2335 * set new_topline and advance "top" and "bot" to include more lines.
2336 */
2337 while (top > 0)
2338 {
2339#ifdef FEAT_FOLDING
2340 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002341 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 i = 1;
2343 else
2344#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002345 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002346 if (top < curwin->w_topline)
2347 scrolled += i;
2348
2349 // If scrolling is needed, scroll at least 'sj' lines.
2350 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2351 && extra >= off)
2352 break;
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 used += i;
2355 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2356 {
2357#ifdef FEAT_FOLDING
2358 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 ++used;
2361 else
2362#endif
2363 used += plines(bot);
2364 }
2365 if (used > curwin->w_height)
2366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368 extra += i;
2369 new_topline = top;
2370 --top;
2371 ++bot;
2372 }
2373
2374 /*
2375 * If we don't have enough space, put cursor in the middle.
2376 * This makes sure we get the same position when using "k" and "j"
2377 * in a small window.
2378 */
2379 if (used > curwin->w_height)
2380 scroll_cursor_halfway(FALSE);
2381 else
2382 {
2383 /*
2384 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002385 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 */
2387 if (new_topline < curwin->w_topline || always)
2388 curwin->w_topline = new_topline;
2389 if (curwin->w_topline > curwin->w_cursor.lnum)
2390 curwin->w_topline = curwin->w_cursor.lnum;
2391#ifdef FEAT_DIFF
2392 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2393 if (curwin->w_topfill > 0 && extra > off)
2394 {
2395 curwin->w_topfill -= extra - off;
2396 if (curwin->w_topfill < 0)
2397 curwin->w_topfill = 0;
2398 }
2399 check_topfill(curwin, FALSE);
2400#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002401 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002402 if (curwin->w_topline == curwin->w_cursor.lnum
2403 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002404 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002406 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#ifdef FEAT_DIFF
2408 || curwin->w_topfill != old_topfill
2409#endif
2410 )
2411 curwin->w_valid &=
2412 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2413 curwin->w_valid |= VALID_TOPLINE;
2414 }
2415}
2416
2417/*
2418 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2419 * screen lines for text lines.
2420 */
2421 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002422set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424#ifdef FEAT_DIFF
2425 wp->w_filler_rows = 0;
2426#endif
2427 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else
2430 {
2431 wp->w_empty_rows = wp->w_height - used;
2432#ifdef FEAT_DIFF
2433 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2434 {
2435 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2436 if (wp->w_empty_rows > wp->w_filler_rows)
2437 wp->w_empty_rows -= wp->w_filler_rows;
2438 else
2439 {
2440 wp->w_filler_rows = wp->w_empty_rows;
2441 wp->w_empty_rows = 0;
2442 }
2443 }
2444#endif
2445 }
2446}
2447
2448/*
2449 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002450 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2452 * This is messy stuff!!!
2453 */
2454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002455scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 int used;
2458 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002459 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 int extra = 0;
2461 int i;
2462 linenr_T line_count;
2463 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002464 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 lineoff_T loff;
2466 lineoff_T boff;
2467#ifdef FEAT_DIFF
2468 int old_topfill = curwin->w_topfill;
2469 int fill_below_window;
2470#endif
2471 linenr_T old_botline = curwin->w_botline;
2472 linenr_T old_valid = curwin->w_valid;
2473 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002475 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 cln = curwin->w_cursor.lnum;
2478 if (set_topbot)
2479 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002480 int set_skipcol = FALSE;
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 used = 0;
2483 curwin->w_botline = cln + 1;
2484#ifdef FEAT_DIFF
2485 loff.fill = 0;
2486#endif
2487 for (curwin->w_topline = curwin->w_botline;
2488 curwin->w_topline > 1;
2489 curwin->w_topline = loff.lnum)
2490 {
2491 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002492 topline_back_winheight(&loff, FALSE);
2493 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002495 if (used + loff.height > curwin->w_height)
2496 {
2497 if (curwin->w_p_sms && curwin->w_p_wrap)
2498 {
2499 // 'smoothscroll' and 'wrap' are set. The above line is
2500 // too long to show in its entirety, so we show just a part
2501 // of it.
2502 if (used < curwin->w_height)
2503 {
2504 int plines_offset = used + loff.height
2505 - curwin->w_height;
2506 used = curwin->w_height;
2507#ifdef FEAT_DIFF
2508 curwin->w_topfill = loff.fill;
2509#endif
2510 curwin->w_topline = loff.lnum;
2511 curwin->w_skipcol = skipcol_from_plines(
2512 curwin, plines_offset);
2513 set_skipcol = TRUE;
2514 }
2515 }
2516 break;
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 used += loff.height;
2519#ifdef FEAT_DIFF
2520 curwin->w_topfill = loff.fill;
2521#endif
2522 }
2523 set_empty_rows(curwin, used);
2524 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2525 if (curwin->w_topline != old_topline
2526#ifdef FEAT_DIFF
2527 || curwin->w_topfill != old_topfill
2528#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002529 || set_skipcol
2530 || curwin->w_skipcol != 0)
2531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002533 if (set_skipcol)
2534 redraw_later(UPD_NOT_VALID);
2535 else
2536 reset_skipcol();
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 else
2540 validate_botline();
2541
Bram Moolenaar85a20022019-12-21 18:25:54 +01002542 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef FEAT_DIFF
2544 used = plines_nofill(cln);
2545#else
2546 validate_cheight();
2547 used = curwin->w_cline_height;
2548#endif
2549
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002550 // If the cursor is on or below botline, we will at least scroll by the
2551 // height of the cursor line, which is "used". Correct for empty lines,
2552 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (cln >= curwin->w_botline)
2554 {
2555 scrolled = used;
2556 if (cln == curwin->w_botline)
2557 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002558 min_scrolled = scrolled;
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002559 if (curwin->w_p_sms && curwin->w_p_wrap)
2560 {
2561 // 'smoothscroll' and 'wrap' are set
2562 if (cln > curwin->w_botline)
2563 // add screen lines below w_botline
2564 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2565 min_scrolled += PLINES_NOFILL(lnum);
2566
2567 // Calculate how many screen lines the current top line of window
2568 // occupies. If it is occupying more than the entire window, we
2569 // need to scroll the additional clipped lines to scroll past the
2570 // top line before we can move on to the other lines.
2571 int top_plines =
2572#ifdef FEAT_DIFF
2573 plines_win_nofill
2574#else
2575 plines_win
2576#endif
2577 (curwin, curwin->w_topline, FALSE);
2578 int skip_lines = 0;
2579 int width1 = curwin->w_width - curwin_col_off();
2580 int width2 = width1 + curwin_col_off2();
2581 // similar formula is used in curs_columns()
2582 if (curwin->w_skipcol > width1)
2583 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2584 else if (curwin->w_skipcol > 0)
2585 skip_lines = 1;
2586
2587 top_plines -= skip_lines;
2588 if (top_plines > curwin->w_height)
2589 {
2590 scrolled += (top_plines - curwin->w_height);
2591 min_scrolled += (top_plines - curwin->w_height);
2592 }
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
2596 /*
2597 * Stop counting lines to scroll when
2598 * - hitting start of the file
2599 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002600 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 * - lines between botline and cursor have been counted
2602 */
2603#ifdef FEAT_FOLDING
2604 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2605#endif
2606 {
2607 loff.lnum = cln;
2608 boff.lnum = cln;
2609 }
2610#ifdef FEAT_DIFF
2611 loff.fill = 0;
2612 boff.fill = 0;
2613 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2614 - curwin->w_filler_rows;
2615#endif
2616
2617 while (loff.lnum > 1)
2618 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002619 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2620 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002622 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2624 && loff.lnum <= curwin->w_botline
2625#ifdef FEAT_DIFF
2626 && (loff.lnum < curwin->w_botline
2627 || loff.fill >= fill_below_window)
2628#endif
2629 )
2630 break;
2631
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002634 if (loff.height == MAXCOL)
2635 used = MAXCOL;
2636 else
2637 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (used > curwin->w_height)
2639 break;
2640 if (loff.lnum >= curwin->w_botline
2641#ifdef FEAT_DIFF
2642 && (loff.lnum > curwin->w_botline
2643 || loff.fill <= fill_below_window)
2644#endif
2645 )
2646 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002647 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 scrolled += loff.height;
2649 if (loff.lnum == curwin->w_botline
2650#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002651 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#endif
2653 )
2654 scrolled -= curwin->w_empty_rows;
2655 }
2656
2657 if (boff.lnum < curbuf->b_ml.ml_line_count)
2658 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 botline_forw(&boff);
2661 used += boff.height;
2662 if (used > curwin->w_height)
2663 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002664 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2665 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
2667 extra += boff.height;
2668 if (boff.lnum >= curwin->w_botline
2669#ifdef FEAT_DIFF
2670 || (boff.lnum + 1 == curwin->w_botline
2671 && boff.fill > curwin->w_filler_rows)
2672#endif
2673 )
2674 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002675 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 scrolled += boff.height;
2677 if (boff.lnum == curwin->w_botline
2678#ifdef FEAT_DIFF
2679 && boff.fill == 0
2680#endif
2681 )
2682 scrolled -= curwin->w_empty_rows;
2683 }
2684 }
2685 }
2686 }
2687
Bram Moolenaar85a20022019-12-21 18:25:54 +01002688 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (scrolled <= 0)
2690 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 else if (used > curwin->w_height)
2693 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 else
2696 {
2697 line_count = 0;
2698#ifdef FEAT_DIFF
2699 boff.fill = curwin->w_topfill;
2700#endif
2701 boff.lnum = curwin->w_topline - 1;
2702 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2703 {
2704 botline_forw(&boff);
2705 i += boff.height;
2706 ++line_count;
2707 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002708 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 line_count = 9999;
2710 }
2711
2712 /*
2713 * Scroll up if the cursor is off the bottom of the screen a bit.
2714 * Otherwise put it at 1/2 of the screen.
2715 */
2716 if (line_count >= curwin->w_height && line_count > min_scroll)
2717 scroll_cursor_halfway(FALSE);
2718 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002719 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002720 // With 'smoothscroll' scroll at least the height of the cursor line,
2721 // unless it would move the cursor.
2722 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2723 && (curwin->w_cursor.lnum < curwin->w_topline
2724 || (curwin->w_virtcol - curwin->w_skipcol >=
2725 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002726 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002727 if (line_count > 0)
2728 {
2729 if (scrolling_screenlines(TRUE))
2730 scrollup(scrolled, TRUE); // TODO
2731 else
2732 scrollup(line_count, TRUE);
2733 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * If topline didn't change we need to restore w_botline and w_empty_rows
2738 * (we changed them).
2739 * If topline did change, update_screen() will set botline.
2740 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002741 if (curwin->w_topline == old_topline
2742 && curwin->w_skipcol == old_skipcol
2743 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 curwin->w_botline = old_botline;
2746 curwin->w_empty_rows = old_empty_rows;
2747 curwin->w_valid = old_valid;
2748 }
2749 curwin->w_valid |= VALID_TOPLINE;
2750}
2751
2752/*
2753 * Recompute topline to put the cursor halfway the window
2754 * If "atend" is TRUE, also put it halfway at the end of the file.
2755 */
2756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002757scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 int above = 0;
2760 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002761 colnr_T skipcol = 0;
2762 int set_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#ifdef FEAT_DIFF
2764 int topfill = 0;
2765#endif
2766 int below = 0;
2767 int used;
2768 lineoff_T loff;
2769 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002770#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002771 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002774#ifdef FEAT_PROP_POPUP
2775 // if the width changed this needs to be updated first
2776 may_update_popup_position();
2777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2779#ifdef FEAT_FOLDING
2780 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2781#endif
2782#ifdef FEAT_DIFF
2783 used = plines_nofill(loff.lnum);
2784 loff.fill = 0;
2785 boff.fill = 0;
2786#else
2787 used = plines(loff.lnum);
2788#endif
2789 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002790
2791 int half_height = 0;
2792 int smooth_scroll = FALSE;
2793 if (curwin->w_p_sms && curwin->w_p_wrap)
2794 {
2795 // 'smoothscroll' and 'wrap' are set
2796 smooth_scroll = TRUE;
2797 half_height = (curwin->w_height - used) / 2;
2798 used = 0;
2799 }
2800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 while (topline > 1)
2802 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002803 // If using smoothscroll, we can precisely scroll to the
2804 // exact point where the cursor is halfway down the screen.
2805 if (smooth_scroll)
2806 {
2807 topline_back_winheight(&loff, FALSE);
2808 if (loff.height == MAXCOL)
2809 break;
2810 else
2811 used += loff.height;
2812 if (used > half_height)
2813 {
2814 if (used - loff.height < half_height)
2815 {
2816 int plines_offset = used - half_height;
2817 loff.height -= plines_offset;
2818 used = half_height;
2819
2820 topline = loff.lnum;
2821#ifdef FEAT_DIFF
2822 topfill = loff.fill;
2823#endif
2824 skipcol = skipcol_from_plines(curwin, plines_offset);
2825 set_skipcol = TRUE;
2826 }
2827 break;
2828 }
2829 topline = loff.lnum;
2830#ifdef FEAT_DIFF
2831 topfill = loff.fill;
2832#endif
2833 continue;
2834 }
2835
2836 // If not using smoothscroll, we have to iteratively find how many
2837 // lines to scroll down to roughly fit the cursor.
2838 // This may not be right in the middle if the lines' physical height >
2839 // 1 (e.g. 'wrap' is on).
2840
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843 if (boff.lnum < curbuf->b_ml.ml_line_count)
2844 {
2845 botline_forw(&boff);
2846 used += boff.height;
2847 if (used > curwin->w_height)
2848 break;
2849 below += boff.height;
2850 }
2851 else
2852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (atend)
2855 ++used;
2856 }
2857 }
2858
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002862 if (loff.height == MAXCOL)
2863 used = MAXCOL;
2864 else
2865 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (used > curwin->w_height)
2867 break;
2868 above += loff.height;
2869 topline = loff.lnum;
2870#ifdef FEAT_DIFF
2871 topfill = loff.fill;
2872#endif
2873 }
2874 }
2875#ifdef FEAT_FOLDING
2876 if (!hasFolding(topline, &curwin->w_topline, NULL))
2877#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002878 {
2879 if (curwin->w_topline != topline
2880 || set_skipcol
2881 || curwin->w_skipcol != 0)
2882 {
2883 curwin->w_topline = topline;
2884 if (set_skipcol)
2885 {
2886 curwin->w_skipcol = skipcol;
2887 redraw_later(UPD_NOT_VALID);
2888 }
2889 else
2890 reset_skipcol();
2891 }
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#ifdef FEAT_DIFF
2894 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002895 if (old_topline > curwin->w_topline + curwin->w_height)
2896 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 check_topfill(curwin, FALSE);
2898#endif
2899 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2900 curwin->w_valid |= VALID_TOPLINE;
2901}
2902
2903/*
2904 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002905 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 * If not possible, put it at the same position as scroll_cursor_halfway().
2907 * When called topline must be valid!
2908 */
2909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002910cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002914 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 linenr_T botline;
2916 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002919 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 /*
2922 * How many lines we would like to have above/below the cursor depends on
2923 * whether the first/last line of the file is on screen.
2924 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002925 above_wanted = so;
2926 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002927 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 above_wanted = mouse_dragging - 1;
2930 below_wanted = mouse_dragging - 1;
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (curwin->w_topline == 1)
2933 {
2934 above_wanted = 0;
2935 max_off = curwin->w_height / 2;
2936 if (below_wanted > max_off)
2937 below_wanted = max_off;
2938 }
2939 validate_botline();
2940 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002941 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 below_wanted = 0;
2944 max_off = (curwin->w_height - 1) / 2;
2945 if (above_wanted > max_off)
2946 above_wanted = max_off;
2947 }
2948
2949 /*
2950 * If there are sufficient file-lines above and below the cursor, we can
2951 * return now.
2952 */
2953 cln = curwin->w_cursor.lnum;
2954 if (cln >= curwin->w_topline + above_wanted
2955 && cln < curwin->w_botline - below_wanted
2956#ifdef FEAT_FOLDING
2957 && !hasAnyFolding(curwin)
2958#endif
2959 )
2960 return;
2961
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002962 if (curwin->w_p_sms && !curwin->w_p_wrap)
2963 {
2964 // 'smoothscroll is active
2965 if (curwin->w_cline_height == curwin->w_height)
2966 {
2967 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002968 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002969 return;
2970 }
2971 // TODO: If the cursor line doesn't fit in the window then only adjust
2972 // w_skipcol.
2973 }
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /*
2976 * Narrow down the area where the cursor can be put by taking lines from
2977 * the top and the bottom until:
2978 * - the desired context lines are found
2979 * - the lines from the top is past the lines from the bottom
2980 */
2981 topline = curwin->w_topline;
2982 botline = curwin->w_botline - 1;
2983#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002984 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 above = curwin->w_topfill;
2986 below = curwin->w_filler_rows;
2987#endif
2988 while ((above < above_wanted || below < below_wanted) && topline < botline)
2989 {
2990 if (below < below_wanted && (below <= above || above >= above_wanted))
2991 {
2992#ifdef FEAT_FOLDING
2993 if (hasFolding(botline, &botline, NULL))
2994 ++below;
2995 else
2996#endif
2997 below += plines(botline);
2998 --botline;
2999 }
3000 if (above < above_wanted && (above < below || below >= below_wanted))
3001 {
3002#ifdef FEAT_FOLDING
3003 if (hasFolding(topline, NULL, &topline))
3004 ++above;
3005 else
3006#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003007 above += PLINES_NOFILL(topline);
3008#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003009 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (topline < botline)
3011 above += diff_check_fill(curwin, topline + 1);
3012#endif
3013 ++topline;
3014 }
3015 }
3016 if (topline == botline || botline == 0)
3017 curwin->w_cursor.lnum = topline;
3018 else if (topline > botline)
3019 curwin->w_cursor.lnum = botline;
3020 else
3021 {
3022 if (cln < topline && curwin->w_topline > 1)
3023 {
3024 curwin->w_cursor.lnum = topline;
3025 curwin->w_valid &=
3026 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3027 }
3028 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3029 {
3030 curwin->w_cursor.lnum = botline;
3031 curwin->w_valid &=
3032 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3033 }
3034 }
3035 curwin->w_valid |= VALID_TOPLINE;
3036}
3037
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003038static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003041 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3042 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003044 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 */
3046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003047onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 long n;
3050 int retval = OK;
3051 lineoff_T loff;
3052 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003053 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaar85a20022019-12-21 18:25:54 +01003055 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 beep_flush();
3058 return FAIL;
3059 }
3060
3061 for ( ; count > 0; --count)
3062 {
3063 validate_botline();
3064 /*
3065 * It's an error to move a page up when the first line is already on
3066 * the screen. It's an error to move a page down when the last line
3067 * is on the screen and the topline is 'scrolloff' lines from the
3068 * last line.
3069 */
3070 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003071 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3073 : (curwin->w_topline == 1
3074#ifdef FEAT_DIFF
3075 && curwin->w_topfill ==
3076 diff_check_fill(curwin, curwin->w_topline)
3077#endif
3078 ))
3079 {
3080 beep_flush();
3081 retval = FAIL;
3082 break;
3083 }
3084
3085#ifdef FEAT_DIFF
3086 loff.fill = 0;
3087#endif
3088 if (dir == FORWARD)
3089 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003090 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003092 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003093 if (p_window <= 2)
3094 ++curwin->w_topline;
3095 else
3096 curwin->w_topline += p_window - 2;
3097 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3098 curwin->w_topline = curbuf->b_ml.ml_line_count;
3099 curwin->w_cursor.lnum = curwin->w_topline;
3100 }
3101 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3102 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003103 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 curwin->w_topline = curbuf->b_ml.ml_line_count;
3105#ifdef FEAT_DIFF
3106 curwin->w_topfill = 0;
3107#endif
3108 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3109 }
3110 else
3111 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 // For the overlap, start with the line just below the window
3113 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 loff.lnum = curwin->w_botline;
3115#ifdef FEAT_DIFF
3116 loff.fill = diff_check_fill(curwin, loff.lnum)
3117 - curwin->w_filler_rows;
3118#endif
3119 get_scroll_overlap(&loff, -1);
3120 curwin->w_topline = loff.lnum;
3121#ifdef FEAT_DIFF
3122 curwin->w_topfill = loff.fill;
3123 check_topfill(curwin, FALSE);
3124#endif
3125 curwin->w_cursor.lnum = curwin->w_topline;
3126 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3127 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3128 }
3129 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003130 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
3132#ifdef FEAT_DIFF
3133 if (curwin->w_topline == 1)
3134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003135 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 max_topfill();
3137 continue;
3138 }
3139#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003140 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003142 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003143 if (p_window <= 2)
3144 --curwin->w_topline;
3145 else
3146 curwin->w_topline -= p_window - 2;
3147 if (curwin->w_topline < 1)
3148 curwin->w_topline = 1;
3149 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3150 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3151 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3152 continue;
3153 }
3154
Bram Moolenaar85a20022019-12-21 18:25:54 +01003155 // Find the line at the top of the window that is going to be the
3156 // line at the bottom of the window. Make sure this results in
3157 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 loff.lnum = curwin->w_topline - 1;
3159#ifdef FEAT_DIFF
3160 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3161 - curwin->w_topfill;
3162#endif
3163 get_scroll_overlap(&loff, 1);
3164
3165 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3166 {
3167 loff.lnum = curbuf->b_ml.ml_line_count;
3168#ifdef FEAT_DIFF
3169 loff.fill = 0;
3170 }
3171 else
3172 {
3173 botline_topline(&loff);
3174#endif
3175 }
3176 curwin->w_cursor.lnum = loff.lnum;
3177
Bram Moolenaar85a20022019-12-21 18:25:54 +01003178 // Find the line just above the new topline to get the right line
3179 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 n = 0;
3181 while (n <= curwin->w_height && loff.lnum >= 1)
3182 {
3183 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003184 if (loff.height == MAXCOL)
3185 n = MAXCOL;
3186 else
3187 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003189 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 curwin->w_topline = 1;
3192#ifdef FEAT_DIFF
3193 max_topfill();
3194#endif
3195 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3196 }
3197 else
3198 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003199 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_DIFF
3201 topline_botline(&loff);
3202#endif
3203 botline_forw(&loff);
3204 botline_forw(&loff);
3205#ifdef FEAT_DIFF
3206 botline_topline(&loff);
3207#endif
3208#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003209 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3211#endif
3212
Bram Moolenaar85a20022019-12-21 18:25:54 +01003213 // Always scroll at least one line. Avoid getting stuck on
3214 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (loff.lnum >= curwin->w_topline
3216#ifdef FEAT_DIFF
3217 && (loff.lnum > curwin->w_topline
3218 || loff.fill >= curwin->w_topfill)
3219#endif
3220 )
3221 {
3222#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003223 // First try using the maximum number of filler lines. If
3224 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 loff.fill = curwin->w_topfill;
3226 if (curwin->w_topfill < diff_check_fill(curwin,
3227 curwin->w_topline))
3228 max_topfill();
3229 if (curwin->w_topfill == loff.fill)
3230#endif
3231 {
3232 --curwin->w_topline;
3233#ifdef FEAT_DIFF
3234 curwin->w_topfill = 0;
3235#endif
3236 }
3237 comp_botline(curwin);
3238 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003239 curwin->w_valid &=
3240 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 else
3243 {
3244 curwin->w_topline = loff.lnum;
3245#ifdef FEAT_DIFF
3246 curwin->w_topfill = loff.fill;
3247 check_topfill(curwin, FALSE);
3248#endif
3249 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3250 }
3251 }
3252 }
3253 }
3254#ifdef FEAT_FOLDING
3255 foldAdjustCursor();
3256#endif
3257 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003258 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003259 if (retval == OK)
3260 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3262
Bram Moolenaar907dad72018-07-10 15:07:15 +02003263 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003265 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3266 // But make sure we scroll at least one line (happens with mix of long
3267 // wrapping lines and non-wrapping line).
3268 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003270 scroll_cursor_top(1, FALSE);
3271 if (curwin->w_topline <= old_topline
3272 && old_topline < curbuf->b_ml.ml_line_count)
3273 {
3274 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003276 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3277#endif
3278 }
3279 }
3280#ifdef FEAT_FOLDING
3281 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003286 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return retval;
3288}
3289
3290/*
3291 * Decide how much overlap to use for page-up or page-down scrolling.
3292 * This is symmetric, so that doing both keeps the same lines displayed.
3293 * Three lines are examined:
3294 *
3295 * before CTRL-F after CTRL-F / before CTRL-B
3296 * etc. l1
3297 * l1 last but one line ------------
3298 * l2 last text line l2 top text line
3299 * ------------- l3 second text line
3300 * l3 etc.
3301 */
3302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003303get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 int h1, h2, h3, h4;
3306 int min_height = curwin->w_height - 2;
3307 lineoff_T loff0, loff1, loff2;
3308
3309#ifdef FEAT_DIFF
3310 if (lp->fill > 0)
3311 lp->height = 1;
3312 else
3313 lp->height = plines_nofill(lp->lnum);
3314#else
3315 lp->height = plines(lp->lnum);
3316#endif
3317 h1 = lp->height;
3318 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003319 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 loff0 = *lp;
3322 if (dir > 0)
3323 botline_forw(lp);
3324 else
3325 topline_back(lp);
3326 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003327 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003329 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return;
3331 }
3332
3333 loff1 = *lp;
3334 if (dir > 0)
3335 botline_forw(lp);
3336 else
3337 topline_back(lp);
3338 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003339 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return;
3343 }
3344
3345 loff2 = *lp;
3346 if (dir > 0)
3347 botline_forw(lp);
3348 else
3349 topline_back(lp);
3350 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003351 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003352 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003354 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355}
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357/*
3358 * Scroll 'scroll' lines up or down.
3359 */
3360 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003361halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 long scrolled = 0;
3364 int i;
3365 int n;
3366 int room;
3367
3368 if (Prenum)
3369 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3370 curwin->w_height : Prenum;
3371 n = (curwin->w_p_scr <= curwin->w_height) ?
3372 curwin->w_p_scr : curwin->w_height;
3373
Bram Moolenaard5d37532017-03-27 23:02:07 +02003374 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 validate_botline();
3376 room = curwin->w_empty_rows;
3377#ifdef FEAT_DIFF
3378 room += curwin->w_filler_rows;
3379#endif
3380 if (flag)
3381 {
3382 /*
3383 * scroll the text up
3384 */
3385 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3386 {
3387#ifdef FEAT_DIFF
3388 if (curwin->w_topfill > 0)
3389 {
3390 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003391 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 --curwin->w_topfill;
3393 }
3394 else
3395#endif
3396 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003397 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 n -= i;
3399 if (n < 0 && scrolled > 0)
3400 break;
3401#ifdef FEAT_FOLDING
3402 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3403#endif
3404 ++curwin->w_topline;
3405#ifdef FEAT_DIFF
3406 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3407#endif
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3410 {
3411 ++curwin->w_cursor.lnum;
3412 curwin->w_valid &=
3413 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3417 scrolled += i;
3418
3419 /*
3420 * Correct w_botline for changed w_topline.
3421 * Won't work when there are filler lines.
3422 */
3423#ifdef FEAT_DIFF
3424 if (curwin->w_p_diff)
3425 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3426 else
3427#endif
3428 {
3429 room += i;
3430 do
3431 {
3432 i = plines(curwin->w_botline);
3433 if (i > room)
3434 break;
3435#ifdef FEAT_FOLDING
3436 (void)hasFolding(curwin->w_botline, NULL,
3437 &curwin->w_botline);
3438#endif
3439 ++curwin->w_botline;
3440 room -= i;
3441 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3442 }
3443 }
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 /*
3446 * When hit bottom of the file: move cursor down.
3447 */
3448 if (n > 0)
3449 {
3450# ifdef FEAT_FOLDING
3451 if (hasAnyFolding(curwin))
3452 {
3453 while (--n >= 0
3454 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3455 {
3456 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3457 &curwin->w_cursor.lnum);
3458 ++curwin->w_cursor.lnum;
3459 }
3460 }
3461 else
3462# endif
3463 curwin->w_cursor.lnum += n;
3464 check_cursor_lnum();
3465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 else
3468 {
3469 /*
3470 * scroll the text down
3471 */
3472 while (n > 0 && curwin->w_topline > 1)
3473 {
3474#ifdef FEAT_DIFF
3475 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3476 {
3477 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003478 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 ++curwin->w_topfill;
3480 }
3481 else
3482#endif
3483 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003484 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 n -= i;
3486 if (n < 0 && scrolled > 0)
3487 break;
3488 --curwin->w_topline;
3489#ifdef FEAT_FOLDING
3490 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3491#endif
3492#ifdef FEAT_DIFF
3493 curwin->w_topfill = 0;
3494#endif
3495 }
3496 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3497 VALID_BOTLINE|VALID_BOTLINE_AP);
3498 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (curwin->w_cursor.lnum > 1)
3500 {
3501 --curwin->w_cursor.lnum;
3502 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /*
3507 * When hit top of the file: move cursor up.
3508 */
3509 if (n > 0)
3510 {
3511 if (curwin->w_cursor.lnum <= (linenr_T)n)
3512 curwin->w_cursor.lnum = 1;
3513 else
3514# ifdef FEAT_FOLDING
3515 if (hasAnyFolding(curwin))
3516 {
3517 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3518 {
3519 --curwin->w_cursor.lnum;
3520 (void)hasFolding(curwin->w_cursor.lnum,
3521 &curwin->w_cursor.lnum, NULL);
3522 }
3523 }
3524 else
3525# endif
3526 curwin->w_cursor.lnum -= n;
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003530 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 foldAdjustCursor();
3532# endif
3533#ifdef FEAT_DIFF
3534 check_topfill(curwin, !flag);
3535#endif
3536 cursor_correct();
3537 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003538 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003540
Bram Moolenaar860cae12010-06-05 23:22:07 +02003541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003542do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003543{
3544 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003545 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003546 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003547 colnr_T curswant = curwin->w_curswant;
3548 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003549 win_T *old_curwin = curwin;
3550 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003551 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552 int old_VIsual_select = VIsual_select;
3553 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003554
3555 /*
3556 * loop through the cursorbound windows
3557 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003559 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003560 {
3561 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003562 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 if (curwin != old_curwin && curwin->w_p_crb)
3564 {
3565# ifdef FEAT_DIFF
3566 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003567 curwin->w_cursor.lnum =
3568 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569 else
3570# endif
3571 curwin->w_cursor.lnum = line;
3572 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003573 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003574 curwin->w_curswant = curswant;
3575 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003576
Bram Moolenaar85a20022019-12-21 18:25:54 +01003577 // Make sure the cursor is in a valid position. Temporarily set
3578 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003579 restart_edit_save = restart_edit;
3580 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003582
3583 // Avoid a scroll here for the cursor position, 'scrollbind' is
3584 // more important.
3585 if (!curwin->w_p_scb)
3586 validate_cursor();
3587
Bram Moolenaar61452852011-02-01 18:01:11 +01003588 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003589 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 if (has_mbyte)
3591 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003592 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003593
Bram Moolenaar85a20022019-12-21 18:25:54 +01003594 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003595 if (!curwin->w_p_scb)
3596 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 }
3599 }
3600
3601 /*
3602 * reset current-window
3603 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604 VIsual_select = old_VIsual_select;
3605 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003606 curwin = old_curwin;
3607 curbuf = old_curbuf;
3608}