blob: e8b86caf17d8013e82adb39d4d226b2aefa49e7d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
41 static int
42adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000205 * Calculates how much overlap the smoothscroll marker "<<<" overlaps with
206 * buffer text for curwin.
207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
212 static int
213smoothscroll_marker_overlap(int extra2)
214{
215#if defined(FEAT_LINEBREAK)
216 // We don't draw the <<< marker when in showbreak mode, thus no need to
217 // account for it. See wlv_screen_line().
218 if (*get_showbreak_value(curwin) != NUL)
219 return 0;
220#endif
221 return extra2 > 3 ? 0 : 3 - extra2;
222}
223
224/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000225 * Calculates the skipcol offset for window "wp" given how many
226 * physical lines we want to scroll down.
227 */
228 static int
229skipcol_from_plines(win_T *wp, int plines_off)
230{
231 int width1 = wp->w_width - win_col_off(wp);
232
233 int skipcol = 0;
234 if (plines_off > 0)
235 skipcol += width1;
236 if (plines_off > 1)
237 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
238 return skipcol;
239}
240
241/*
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000242 * Set curwin->s_skipcol to zero and redraw later if needed.
243 */
244 static void
245reset_skipcol(void)
246{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000247 if (curwin->w_skipcol == 0)
248 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000249
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000250 curwin->w_skipcol = 0;
251
252 // Should use the least expensive way that displays all that changed.
253 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
254 // enough when the top line gets another screen line.
255 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000256}
257
258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 * Update curwin->w_topline and redraw if necessary.
260 * Used to update the screen before printing a message.
261 */
262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100263update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264{
265 update_topline();
266 if (must_redraw)
267 update_screen(0);
268}
269
270/*
271 * Update curwin->w_topline to move the cursor onto the screen.
272 */
273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100274update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
276 long line_count;
277 int halfheight;
278 int n;
279 linenr_T old_topline;
280#ifdef FEAT_DIFF
281 int old_topfill;
282#endif
283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 old_topline = curwin->w_topline;
315#ifdef FEAT_DIFF
316 old_topfill = curwin->w_topfill;
317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
352 // the smoothscroll marker "<<<" displayed in the top-left if
353 // needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000355 int smoothscroll_overlap = smoothscroll_marker_overlap(
356 curwin_col_off() - curwin_col_off2());
357 if (curwin->w_skipcol + smoothscroll_overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000399 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 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
Bram Moolenaar1d6539c2023-02-14 17:41:20 +0000502 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
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
Dominique Pellee764d1b2023-03-12 21:20:59 +0000665#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000667 * Call changed_window_setting_win() for every window containing "buf".
668 */
669 void
670changed_window_setting_buf(buf_T *buf)
671{
672 tabpage_T *tp;
673 win_T *wp;
674
675 FOR_ALL_TAB_WINDOWS(tp, wp)
676 if (wp->w_buffer == buf)
677 changed_window_setting_win(wp);
678}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000679#endif
Bram Moolenaar89469d12022-12-02 20:46:26 +0000680
681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 * Set wp->w_topline to a certain number.
683 */
684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200687#ifdef FEAT_DIFF
688 linenr_T prev_topline = wp->w_topline;
689#endif
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100692 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
694#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100697 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
698 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000700 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200702 if (lnum != prev_topline)
703 // Keep the filler lines when the topline didn't change.
704 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#endif
706 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100708 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
711/*
712 * Call this function when the length of the cursor line (in screen
713 * characters) has changed, and the change is before the cursor.
714 * Need to take care of w_botline separately!
715 */
716 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100717changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
720 |VALID_CHEIGHT|VALID_TOPLINE);
721}
722
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
727 |VALID_CHEIGHT|VALID_TOPLINE);
728}
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730/*
731 * Call this function when the length of a line (in screen characters) above
732 * the cursor have changed.
733 * Need to take care of w_botline separately!
734 */
735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
739 |VALID_CHEIGHT|VALID_TOPLINE);
740}
741
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
746 |VALID_CHEIGHT|VALID_TOPLINE);
747}
748
Dominique Pellee764d1b2023-03-12 21:20:59 +0000749#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100751 * Display of line has changed for "buf", invalidate cursor position and
752 * w_botline.
753 */
754 void
755changed_line_display_buf(buf_T *buf)
756{
757 win_T *wp;
758
759 FOR_ALL_WINDOWS(wp)
760 if (wp->w_buffer == buf)
761 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
762 |VALID_CROW|VALID_CHEIGHT
763 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
764}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000765#endif
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100766
767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 * Make sure the value of curwin->w_botline is valid.
769 */
770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100771validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100773 validate_botline_win(curwin);
774}
775
776/*
777 * Make sure the value of wp->w_botline is valid.
778 */
779 void
780validate_botline_win(win_T *wp)
781{
782 if (!(wp->w_valid & VALID_BOTLINE))
783 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * Mark curwin->w_botline as invalid (because of some change in the buffer).
788 */
789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100790invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
793}
794
795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100796invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
799}
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802approximate_botline_win(
803 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 wp->w_valid &= ~VALID_BOTLINE;
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
810 */
811 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 check_cursor_moved(curwin);
815 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
816 (VALID_WROW|VALID_WCOL));
817}
818
819/*
820 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
821 * w_topline must be valid, you may need to call update_topline() first!
822 */
823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100824validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100826 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 check_cursor_moved(curwin);
828 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
829 curs_columns(TRUE);
830}
831
832#if defined(FEAT_GUI) || defined(PROTO)
833/*
834 * validate w_cline_row.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 /*
840 * First make sure that w_topline is valid (after moving the cursor).
841 */
842 update_topline();
843 check_cursor_moved(curwin);
844 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100845 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847#endif
848
849/*
850 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200851 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 linenr_T lnum;
857 int i;
858 int all_invalid;
859 int valid;
860#ifdef FEAT_FOLDING
861 long fold_count;
862#endif
863
Bram Moolenaar85a20022019-12-21 18:25:54 +0100864 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 all_invalid = (!redrawing()
866 || wp->w_lines_valid == 0
867 || wp->w_lines[0].wl_lnum > wp->w_topline);
868 i = 0;
869 wp->w_cline_row = 0;
870 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
871 {
872 valid = FALSE;
873 if (!all_invalid && i < wp->w_lines_valid)
874 {
875 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100876 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (wp->w_lines[i].wl_lnum == lnum)
878 {
879#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880 // Check for newly inserted lines below this row, in which
881 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (!wp->w_buffer->b_mod_set
883 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
884 || wp->w_buffer->b_mod_top
885 > wp->w_lines[i].wl_lastlnum + 1)
886#endif
887 valid = TRUE;
888 }
889 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892 if (valid
893#ifdef FEAT_DIFF
894 && (lnum != wp->w_topline || !wp->w_p_diff)
895#endif
896 )
897 {
898#ifdef FEAT_FOLDING
899 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (lnum > wp->w_cursor.lnum)
902 break;
903#else
904 ++lnum;
905#endif
906 wp->w_cline_row += wp->w_lines[i].wl_size;
907 }
908 else
909 {
910#ifdef FEAT_FOLDING
911 fold_count = foldedCount(wp, lnum, NULL);
912 if (fold_count)
913 {
914 lnum += fold_count;
915 if (lnum > wp->w_cursor.lnum)
916 break;
917 ++wp->w_cline_row;
918 }
919 else
920#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100921 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100922 wp->w_cline_row += plines_correct_topline(wp, lnum);
923 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 }
927
928 check_cursor_moved(wp);
929 if (!(wp->w_valid & VALID_CHEIGHT))
930 {
931 if (all_invalid
932 || i == wp->w_lines_valid
933 || (i < wp->w_lines_valid
934 && (!wp->w_lines[i].wl_valid
935 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
936 {
937#ifdef FEAT_DIFF
938 if (wp->w_cursor.lnum == wp->w_topline)
939 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
940 TRUE) + wp->w_topfill;
941 else
942#endif
943 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
944#ifdef FEAT_FOLDING
945 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
946 NULL, NULL, TRUE, NULL);
947#endif
948 }
949 else if (i > wp->w_lines_valid)
950 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100951 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_cline_height = 0;
953#ifdef FEAT_FOLDING
954 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
955 NULL, NULL, TRUE, NULL);
956#endif
957 }
958 else
959 {
960 wp->w_cline_height = wp->w_lines[i].wl_size;
961#ifdef FEAT_FOLDING
962 wp->w_cline_folded = wp->w_lines[i].wl_folded;
963#endif
964 }
965 }
966
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100967 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
971/*
972 * Validate curwin->w_virtcol only.
973 */
974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 validate_virtcol_win(curwin);
978}
979
980/*
981 * Validate wp->w_virtcol only.
982 */
983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100984validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000987
988 if (wp->w_valid & VALID_VIRTCOL)
989 return;
990
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100991#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100993#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000995#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000996 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000997#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000998 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Validate curwin->w_cline_height only.
1003 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001008
1009 if (curwin->w_valid & VALID_CHEIGHT)
1010 return;
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001013 if (curwin->w_cursor.lnum == curwin->w_topline)
1014 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1015 + curwin->w_topfill;
1016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001020 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001022 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001026 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 colnr_T off;
1032 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001033 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001037 if (curwin->w_valid & VALID_WCOL)
1038 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001039
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 col = curwin->w_virtcol;
1041 off = curwin_col_off();
1042 col += off;
1043 width = curwin->w_width - off + curwin_col_off2();
1044
1045 // long line wrapping, adjust curwin->w_wrow
1046 if (curwin->w_p_wrap
1047 && col >= (colnr_T)curwin->w_width
1048 && width > 0)
1049 // use same formula as what is used in curs_columns()
1050 col -= ((col - curwin->w_width) / width + 1) * width;
1051 if (col > (int)curwin->w_leftcol)
1052 col -= curwin->w_leftcol;
1053 else
1054 col = 0;
1055 curwin->w_wcol = col;
1056
1057 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001058#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001059 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001064 * Compute offset of a window, occupied by absolute or relative line number,
1065 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001068win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar64486672010-05-16 15:46:46 +02001070 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_FOLDING
1073 + wp->w_p_fdc
1074#endif
1075#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001076 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
1078 );
1079}
1080
1081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001082curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return win_col_off(curwin);
1085}
1086
1087/*
1088 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001089 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1090 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
1092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar64486672010-05-16 15:46:46 +02001095 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001096 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 return 0;
1098}
1099
1100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 return win_col_off2(curwin);
1104}
1105
1106/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001107 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * Also updates curwin->w_wrow and curwin->w_cline_row.
1109 * Also updates curwin->w_leftcol.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int off_left, off_right;
1118 int n;
1119 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001120 int width1; // text width for first screen line
1121 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int new_leftcol;
1123 colnr_T startcol;
1124 colnr_T endcol;
1125 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001126 long so = get_scrolloff_value();
1127 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001128 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 /*
1131 * First make sure that w_topline is valid (after moving the cursor).
1132 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001133 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 /*
1136 * Next make sure that w_cline_row is valid.
1137 */
1138 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001139 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001141#ifdef FEAT_PROP_POPUP
1142 // will be set by getvvcol() but not reset
1143 curwin->w_virtcol_first_char = 0;
1144#endif
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 /*
1147 * Compute the number of virtual columns.
1148 */
1149#ifdef FEAT_FOLDING
1150 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1153 else
1154#endif
1155 getvvcol(curwin, &curwin->w_cursor,
1156 &startcol, &(curwin->w_virtcol), &endcol);
1157
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001160 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 extra = curwin_col_off();
1163 curwin->w_wcol = curwin->w_virtcol + extra;
1164 endcol += extra;
1165
1166 /*
1167 * Now compute w_wrow, counting screen lines from w_cline_row.
1168 */
1169 curwin->w_wrow = curwin->w_cline_row;
1170
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001171 width1 = curwin->w_width - extra;
1172 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001175 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001176 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001177 if (curwin->w_p_wrap)
1178 curwin->w_wrow = curwin->w_height - 1;
1179 else
1180 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001182 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001184 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001186 // skip columns that are not visible
1187 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001188 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001189 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001190 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001191 // Deduct by multiples of width2. This allows the long line
1192 // wrapping formula below to correctly calculate the w_wcol value
1193 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001196 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001197 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001198 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001199
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001200 did_sub_skipcol = TRUE;
1201 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001202
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001204 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001207 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1208 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 curwin->w_wrow += n;
1210
1211#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001212 // When cursor wraps to first char of next line in Insert
1213 // mode, the 'showbreak' string isn't shown, backup to first
1214 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001215 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001216 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001217 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 curwin->w_wcol = 0;
1219#endif
1220 }
1221 }
1222
Bram Moolenaar85a20022019-12-21 18:25:54 +01001223 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1224 // is not folded.
1225 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001226 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#ifdef FEAT_FOLDING
1228 && !curwin->w_cline_folded
1229#endif
1230 )
1231 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001232#ifdef FEAT_PROP_POPUP
1233 if (curwin->w_virtcol_first_char > 0)
1234 {
1235 int cols = (curwin->w_width - extra);
1236 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1237
1238 // each "above" text prop shifts the text one row down
1239 curwin->w_wrow += rows;
1240 curwin->w_wcol -= rows * cols;
1241 endcol -= rows * cols;
1242 curwin->w_cline_height = rows + 1;
1243 }
1244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /*
1246 * If Cursor is left of the screen, scroll rightwards.
1247 * If Cursor is right of the screen, scroll leftwards
1248 * If we get closer to the edge than 'sidescrolloff', scroll a little
1249 * extra
1250 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001251 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001252 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001253 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (off_left < 0 || off_right > 0)
1255 {
1256 if (off_left < 0)
1257 diff = -off_left;
1258 else
1259 diff = off_right;
1260
Bram Moolenaar85a20022019-12-21 18:25:54 +01001261 // When far off or not enough room on either side, put cursor in
1262 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001263 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1264 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 else
1266 {
1267 if (diff < p_ss)
1268 diff = p_ss;
1269 if (off_left < 0)
1270 new_leftcol = curwin->w_leftcol - diff;
1271 else
1272 new_leftcol = curwin->w_leftcol + diff;
1273 }
1274 if (new_leftcol < 0)
1275 new_leftcol = 0;
1276 if (new_leftcol != (int)curwin->w_leftcol)
1277 {
1278 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001280 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 curwin->w_wcol -= curwin->w_leftcol;
1284 }
1285 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1286 curwin->w_wcol -= curwin->w_leftcol;
1287 else
1288 curwin->w_wcol = 0;
1289
1290#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 // Skip over filler lines. At the top use w_topfill, there
1292 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (curwin->w_cursor.lnum == curwin->w_topline)
1294 curwin->w_wrow += curwin->w_topfill;
1295 else
1296 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1297#endif
1298
1299 prev_skipcol = curwin->w_skipcol;
1300
1301 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if ((curwin->w_wrow >= curwin->w_height
1304 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001305 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && (p_lines =
1307#ifdef FEAT_DIFF
1308 plines_win_nofill
1309#else
1310 plines_win
1311#endif
1312 (curwin, curwin->w_cursor.lnum, FALSE))
1313 - 1 >= curwin->w_height))
1314 && curwin->w_height != 0
1315 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001316 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001317 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 // Cursor past end of screen. Happens with a single line that does
1320 // not fit on screen. Find a skipcol to show the text around the
1321 // cursor. Avoid scrolling all the time. compute value of "extra":
1322 // 1: Less than 'scrolloff' lines above
1323 // 2: Less than 'scrolloff' lines below
1324 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001326 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 // Compute last display line of the buffer line that we want at the
1329 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (p_lines == 0)
1331 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1332 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001333 if (p_lines > curwin->w_wrow + so)
1334 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
1336 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001337 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 extra += 2;
1339
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001340 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001343 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (n > curwin->w_height / 2)
1345 n -= curwin->w_height / 2;
1346 else
1347 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (n > p_lines - curwin->w_height + 1)
1350 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else if (extra == 1)
1354 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001355 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001356 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1357 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (extra > 0)
1359 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001360 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1361 extra = curwin->w_skipcol / width2;
1362 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 }
1365 else if (extra == 2)
1366 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001367 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001368 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001370 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (endcol > curwin->w_skipcol)
1372 curwin->w_skipcol = endcol;
1373 }
1374
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001375 // adjust w_wrow for the changed w_skipcol
1376 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001377 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001378 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001379 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (curwin->w_wrow >= curwin->w_height)
1382 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 curwin->w_wrow -= extra;
1387 }
1388
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001389 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (extra > 0)
1391 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1392 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001393 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001395 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 curwin->w_skipcol = 0;
1397 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001398 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001400#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001401 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001402#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001403#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1404 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1405 {
1406 curwin->w_wrow += popup_top_extra(curwin);
1407 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001408 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001409 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001410 else
1411 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001412#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001413
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001414 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1415 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001416 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001417 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1420}
1421
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001422#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001423/*
1424 * Compute the screen position of text character at "pos" in window "wp"
1425 * The resulting values are one-based, zero when character is not visible.
1426 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001427 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001428textpos2screenpos(
1429 win_T *wp,
1430 pos_T *pos,
1431 int *rowp, // screen row
1432 int *scolp, // start screen column
1433 int *ccolp, // cursor screen column
1434 int *ecolp) // end screen column
1435{
1436 colnr_T scol = 0, ccol = 0, ecol = 0;
1437 int row = 0;
1438 int rowoff = 0;
1439 colnr_T coloff = 0;
1440
Bram Moolenaar189663b2021-07-21 18:04:56 +02001441 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001442 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001443 colnr_T col;
1444 int width;
1445 linenr_T lnum = pos->lnum;
1446#ifdef FEAT_FOLDING
1447 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001448
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001449 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1450#endif
1451 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001452
1453#ifdef FEAT_DIFF
1454 // Add filler lines above this buffer line.
1455 row += diff_check_fill(wp, lnum);
1456#endif
1457
zeertzjqba2d1912022-12-18 12:28:59 +00001458 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001459#ifdef FEAT_FOLDING
1460 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001462 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001463 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001464 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001465 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001466#endif
1467 {
1468 getvcol(wp, pos, &scol, &ccol, &ecol);
1469
1470 // similar to what is done in validate_cursor_col()
1471 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001472 col += off;
1473 width = wp->w_width - off + win_col_off2(wp);
1474
1475 // long line wrapping, adjust row
1476 if (wp->w_p_wrap
1477 && col >= (colnr_T)wp->w_width
1478 && width > 0)
1479 {
1480 // use same formula as what is used in curs_columns()
1481 rowoff = ((col - wp->w_width) / width + 1);
1482 col -= rowoff * width;
1483 }
1484 col -= wp->w_leftcol;
1485 if (col >= wp->w_width)
1486 col = -1;
1487 if (col >= 0 && row + rowoff <= wp->w_height)
1488 {
1489 coloff = col - scol + wp->w_wincol + 1;
1490 row += W_WINROW(wp);
1491 }
1492 else
1493 // character is left, right or below of the window
1494 row = rowoff = scol = ccol = ecol = 0;
1495 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001496 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001497 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001498 *scolp = scol + coloff;
1499 *ccolp = ccol + coloff;
1500 *ecolp = ecol + coloff;
1501}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001502#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503
Bram Moolenaar12034e22019-08-25 22:25:02 +02001504#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001505/*
1506 * "screenpos({winid}, {lnum}, {col})" function
1507 */
1508 void
1509f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1510{
1511 dict_T *dict;
1512 win_T *wp;
1513 pos_T pos;
1514 int row = 0;
1515 int scol = 0, ccol = 0, ecol = 0;
1516
Bram Moolenaar93a10962022-06-16 11:42:09 +01001517 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518 return;
1519 dict = rettv->vval.v_dict;
1520
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001521 if (in_vim9script()
1522 && (check_for_number_arg(argvars, 0) == FAIL
1523 || check_for_number_arg(argvars, 1) == FAIL
1524 || check_for_number_arg(argvars, 2) == FAIL))
1525 return;
1526
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001527 wp = find_win_by_nr_or_id(&argvars[0]);
1528 if (wp == NULL)
1529 return;
1530
1531 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001532 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1533 {
1534 semsg(_(e_invalid_line_number_nr), pos.lnum);
1535 return;
1536 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001537 pos.col = tv_get_number(&argvars[2]) - 1;
1538 pos.coladd = 0;
1539 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1540
1541 dict_add_number(dict, "row", row);
1542 dict_add_number(dict, "col", scol);
1543 dict_add_number(dict, "curscol", ccol);
1544 dict_add_number(dict, "endcol", ecol);
1545}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001546
1547/*
1548 * "virtcol2col({winid}, {lnum}, {col})" function
1549 */
1550 void
1551f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1552{
1553 win_T *wp;
1554 linenr_T lnum;
1555 int screencol;
1556 int error = FALSE;
1557
1558 rettv->vval.v_number = -1;
1559
1560 if (check_for_number_arg(argvars, 0) == FAIL
1561 || check_for_number_arg(argvars, 1) == FAIL
1562 || check_for_number_arg(argvars, 2) == FAIL)
1563 return;
1564
1565 wp = find_win_by_nr_or_id(&argvars[0]);
1566 if (wp == NULL)
1567 return;
1568
1569 lnum = tv_get_number_chk(&argvars[1], &error);
1570 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1571 return;
1572
1573 screencol = tv_get_number_chk(&argvars[2], &error);
1574 if (error || screencol < 0)
1575 return;
1576
1577 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1578}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001579#endif
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581/*
1582 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001585scrolldown(
1586 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001589 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 int wrow;
1591 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001592 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001593 int width1 = 0;
1594 int width2 = 0;
1595
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001596 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001597 {
1598 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001599 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602#ifdef FEAT_FOLDING
1603 linenr_T first;
1604
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1607#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001609 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001612 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1613 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 ++curwin->w_topfill;
1616 ++done;
1617 }
1618 else
1619#endif
1620 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001621 // break when at the very top
1622 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001623 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001625 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001627 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001628 if (curwin->w_skipcol >= width1 + width2)
1629 curwin->w_skipcol -= width2;
1630 else
1631 curwin->w_skipcol -= width1;
1632 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001636 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001637 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001638 --curwin->w_topline;
1639 curwin->w_skipcol = 0;
1640#ifdef FEAT_DIFF
1641 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001643#ifdef FEAT_FOLDING
1644 // A sequence of folded lines only counts for one logical line
1645 if (hasFolding(curwin->w_topline, &first, NULL))
1646 {
1647 ++done;
1648 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001649 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001650 curwin->w_botline -= curwin->w_topline - first;
1651 curwin->w_topline = first;
1652 }
1653 else
1654#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001655 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001656 {
1657 int size = win_linetabsize(curwin, curwin->w_topline,
1658 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1659 if (size > width1)
1660 {
1661 curwin->w_skipcol = width1;
1662 size -= width1;
1663 redraw_later(UPD_NOT_VALID);
1664 }
1665 while (size > width2)
1666 {
1667 curwin->w_skipcol += width2;
1668 size -= width2;
1669 }
1670 ++done;
1671 }
1672 else
1673 done += PLINES_NOFILL(curwin->w_topline);
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001676 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 invalidate_botline();
1678 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001679 curwin->w_wrow += done; // keep w_wrow updated
1680 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682#ifdef FEAT_DIFF
1683 if (curwin->w_cursor.lnum == curwin->w_topline)
1684 curwin->w_cline_row = 0;
1685 check_topfill(curwin, TRUE);
1686#endif
1687
1688 /*
1689 * Compute the row number of the last row of the cursor line
1690 * and move the cursor onto the displayed part of the window.
1691 */
1692 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001693 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 validate_virtcol();
1696 validate_cheight();
1697 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001698 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1701 {
1702#ifdef FEAT_FOLDING
1703 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1704 {
1705 --wrow;
1706 if (first == 1)
1707 curwin->w_cursor.lnum = 1;
1708 else
1709 curwin->w_cursor.lnum = first - 1;
1710 }
1711 else
1712#endif
1713 wrow -= plines(curwin->w_cursor.lnum--);
1714 curwin->w_valid &=
1715 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1716 moved = TRUE;
1717 }
1718 if (moved)
1719 {
1720#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001721 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 foldAdjustCursor();
1723#endif
1724 coladvance(curwin->w_curswant);
1725 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001726
1727 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1728 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001729 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001730 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1731
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001732 // make sure the cursor is in the visible text
1733 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001734 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001735 int row = 0;
1736 if (col >= width1)
1737 {
1738 col -= width1;
1739 ++row;
1740 }
Bram Moolenaare0f86912023-03-01 17:55:31 +00001741 if (col > width2 && width2 > 0)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001742 {
1743 row += col / width2;
1744 col = col % width2;
1745 }
1746 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001747 {
1748 curwin->w_curswant = curwin->w_virtcol
1749 - (row - curwin->w_height + 1) * width2;
1750 coladvance(curwin->w_curswant);
1751 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753}
1754
1755/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001756 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1757 */
1758 static int
1759scrolling_screenlines(int byfold UNUSED)
1760{
1761 return (curwin->w_p_wrap && curwin->w_p_sms)
1762# ifdef FEAT_FOLDING
1763 || (byfold && hasAnyFolding(curwin))
1764# endif
1765# ifdef FEAT_DIFF
1766 || curwin->w_p_diff
1767# endif
1768 ;
1769}
1770
1771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775scrollup(
1776 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001779 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001781 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001783 int width1 = curwin->w_width - curwin_col_off();
1784 int width2 = width1 + curwin_col_off2();
1785 int size = 0;
1786 linenr_T prev_topline = curwin->w_topline;
1787
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001788 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001789 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001790
1791 // diff mode: first consume "topfill"
1792 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1793 // the line, then advance to the next line.
1794 // folding: count each sequence of folded lines as one logical line.
1795 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
1797# ifdef FEAT_DIFF
1798 if (curwin->w_topfill > 0)
1799 --curwin->w_topfill;
1800 else
1801# endif
1802 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 linenr_T lnum = curwin->w_topline;
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805# ifdef FEAT_FOLDING
1806 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001807 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 (void)hasFolding(lnum, NULL, &lnum);
1809# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001810 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001811 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001812 // 'smoothscroll': increase "w_skipcol" until it goes over
1813 // the end of the line, then advance to the next line.
1814 int add = curwin->w_skipcol > 0 ? width2 : width1;
1815 curwin->w_skipcol += add;
1816 if (curwin->w_skipcol >= size)
1817 {
1818 if (lnum == curbuf->b_ml.ml_line_count)
1819 {
1820 // at the last screen line, can't scroll further
1821 curwin->w_skipcol -= add;
1822 break;
1823 }
1824 ++lnum;
1825 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001826 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001827 else
1828 {
1829 if (lnum >= curbuf->b_ml.ml_line_count)
1830 break;
1831 ++lnum;
1832 }
1833
1834 if (lnum > curwin->w_topline)
1835 {
1836 // approximate w_botline
1837 curwin->w_botline += lnum - curwin->w_topline;
1838 curwin->w_topline = lnum;
1839# ifdef FEAT_DIFF
1840 curwin->w_topfill = diff_check_fill(curwin, lnum);
1841# endif
1842 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001843 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001844 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001845 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001846 }
1847 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001848
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001849 if (curwin->w_topline == prev_topline)
1850 // need to redraw even though w_topline didn't change
1851 redraw_later(UPD_NOT_VALID);
1852 }
1853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001856 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858
1859 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1860 curwin->w_topline = curbuf->b_ml.ml_line_count;
1861 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1862 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1863
1864#ifdef FEAT_DIFF
1865 check_topfill(curwin, FALSE);
1866#endif
1867
1868#ifdef FEAT_FOLDING
1869 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1872#endif
1873
1874 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1875 if (curwin->w_cursor.lnum < curwin->w_topline)
1876 {
1877 curwin->w_cursor.lnum = curwin->w_topline;
1878 curwin->w_valid &=
1879 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1880 coladvance(curwin->w_curswant);
1881 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001882 if (curwin->w_cursor.lnum == curwin->w_topline
1883 && do_sms && curwin->w_skipcol > 0)
1884 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001885 int col_off = curwin_col_off();
1886 int col_off2 = curwin_col_off2();
1887
1888 int width1 = curwin->w_width - col_off;
1889 int width2 = width1 + col_off2;
1890 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001891 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001892 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001893 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001894
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001895 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1896 // going past it anyway.
1897 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1898 smoothscroll_marker_overlap(extra2);
1899
Bram Moolenaar118c2352022-10-09 17:19:27 +01001900 // Make sure the cursor is in a visible part of the line, taking
1901 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001902 // If there are not enough screen lines put the cursor in the middle.
1903 if (scrolloff_cols > space_cols / 2)
1904 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001906 if (curwin->w_virtcol < curwin->w_skipcol
1907 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001908 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001909 colnr_T col = curwin->w_virtcol;
1910
1911 if (col < width1)
1912 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001913 while (col < curwin->w_skipcol
1914 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001915 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001916 curwin->w_curswant = col;
1917 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001918
1919 // validate_virtcol() marked various things as valid, but after
1920 // moving the cursor they need to be recomputed
1921 curwin->w_valid &=
1922 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001923 }
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925}
1926
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001927/*
1928 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1929 * valid for 'smoothscroll'.
1930 */
1931 void
1932adjust_skipcol(void)
1933{
1934 if (!curwin->w_p_wrap
1935 || !curwin->w_p_sms
1936 || curwin->w_cursor.lnum != curwin->w_topline)
1937 return;
1938
1939 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar870219c2023-01-26 14:14:43 +00001940 if (width1 <= 0)
1941 return; // no text will be displayed
1942
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001943 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001944 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001945 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1946 int scrolled = FALSE;
1947
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001948 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001949 if (curwin->w_cline_height == curwin->w_height
1950 // w_cline_height may be capped at w_height, check there aren't
1951 // actually more lines.
1952 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1953 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001954 {
1955 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001956 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001957 return;
1958 }
1959
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001960 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001961 while (curwin->w_skipcol > 0
1962 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1963 {
1964 // scroll a screen line down
1965 if (curwin->w_skipcol >= width1 + width2)
1966 curwin->w_skipcol -= width2;
1967 else
1968 curwin->w_skipcol -= width1;
1969 redraw_later(UPD_NOT_VALID);
1970 scrolled = TRUE;
1971 validate_virtcol();
1972 }
1973 if (scrolled)
1974 return; // don't scroll in the other direction now
1975
1976 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1977 int row = 0;
1978 if (col >= width1)
1979 {
1980 col -= width1;
1981 ++row;
1982 }
1983 if (col > width2)
1984 {
1985 row += col / width2;
1986 col = col % width2;
1987 }
1988 if (row >= curwin->w_height)
1989 {
1990 if (curwin->w_skipcol == 0)
1991 {
1992 curwin->w_skipcol += width1;
1993 --row;
1994 }
1995 if (row >= curwin->w_height)
1996 curwin->w_skipcol += (row - curwin->w_height) * width2;
1997 redraw_later(UPD_NOT_VALID);
1998 }
1999}
2000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#ifdef FEAT_DIFF
2002/*
2003 * Don't end up with too many filler lines in the window.
2004 */
2005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006check_topfill(
2007 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002008 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 int n;
2011
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002012 if (wp->w_topfill <= 0)
2013 return;
2014
2015 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2016 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002018 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002020 --wp->w_topline;
2021 wp->w_topfill = 0;
2022 }
2023 else
2024 {
2025 wp->w_topfill = wp->w_height - n;
2026 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 }
2030}
2031
2032/*
2033 * Use as many filler lines as possible for w_topline. Make sure w_topline
2034 * is still visible.
2035 */
2036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int n;
2040
2041 n = plines_nofill(curwin->w_topline);
2042 if (n >= curwin->w_height)
2043 curwin->w_topfill = 0;
2044 else
2045 {
2046 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2047 if (curwin->w_topfill + n > curwin->w_height)
2048 curwin->w_topfill = curwin->w_height - n;
2049 }
2050}
2051#endif
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053/*
2054 * Scroll the screen one line down, but don't do it if it would move the
2055 * cursor off the screen.
2056 */
2057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int end_row;
2061#ifdef FEAT_DIFF
2062 int can_fill = (curwin->w_topfill
2063 < diff_check_fill(curwin, curwin->w_topline));
2064#endif
2065
2066 if (curwin->w_topline <= 1
2067#ifdef FEAT_DIFF
2068 && !can_fill
2069#endif
2070 )
2071 return;
2072
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 /*
2076 * Compute the row number of the last row of the cursor line
2077 * and make sure it doesn't go off the screen. Make sure the cursor
2078 * doesn't go past 'scrolloff' lines from the screen end.
2079 */
2080 end_row = curwin->w_wrow;
2081#ifdef FEAT_DIFF
2082 if (can_fill)
2083 ++end_row;
2084 else
2085 end_row += plines_nofill(curwin->w_topline - 1);
2086#else
2087 end_row += plines(curwin->w_topline - 1);
2088#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002089 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 validate_cheight();
2092 validate_virtcol();
2093 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002094 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002096 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
2098#ifdef FEAT_DIFF
2099 if (can_fill)
2100 {
2101 ++curwin->w_topfill;
2102 check_topfill(curwin, TRUE);
2103 }
2104 else
2105 {
2106 --curwin->w_topline;
2107 curwin->w_topfill = 0;
2108 }
2109#else
2110 --curwin->w_topline;
2111#endif
2112#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002113 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002115 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2117 }
2118}
2119
2120/*
2121 * Scroll the screen one line up, but don't do it if it would move the cursor
2122 * off the screen.
2123 */
2124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002125scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 int start_row;
2128
2129 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2130#ifdef FEAT_DIFF
2131 && curwin->w_topfill == 0
2132#endif
2133 )
2134 return;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 /*
2139 * Compute the row number of the first row of the cursor line
2140 * and make sure it doesn't go off the screen. Make sure the cursor
2141 * doesn't go before 'scrolloff' lines from the screen start.
2142 */
2143#ifdef FEAT_DIFF
2144 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2145 - curwin->w_topfill;
2146#else
2147 start_row = curwin->w_wrow - plines(curwin->w_topline);
2148#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002149 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002152 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002154 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156#ifdef FEAT_DIFF
2157 if (curwin->w_topfill > 0)
2158 --curwin->w_topfill;
2159 else
2160#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002161 {
2162#ifdef FEAT_FOLDING
2163 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002166 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2169 }
2170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172/*
2173 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2174 * a (wrapped) text line. Uses and sets "lp->fill".
2175 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002176 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002179topline_back_winheight(
2180 lineoff_T *lp,
2181 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183#ifdef FEAT_DIFF
2184 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2185 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ++lp->fill;
2188 lp->height = 1;
2189 }
2190 else
2191#endif
2192 {
2193 --lp->lnum;
2194#ifdef FEAT_DIFF
2195 lp->fill = 0;
2196#endif
2197 if (lp->lnum < 1)
2198 lp->height = MAXCOL;
2199 else
2200#ifdef FEAT_FOLDING
2201 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 lp->height = 1;
2204 else
2205#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002206 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208}
2209
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002210 static void
2211topline_back(lineoff_T *lp)
2212{
2213 topline_back_winheight(lp, TRUE);
2214}
2215
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2219 * a (wrapped) text line. Uses and sets "lp->fill".
2220 * Returns the height of the added line in "lp->height".
2221 * Lines below the last one are incredibly high.
2222 */
2223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002224botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226#ifdef FEAT_DIFF
2227 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 ++lp->fill;
2231 lp->height = 1;
2232 }
2233 else
2234#endif
2235 {
2236 ++lp->lnum;
2237#ifdef FEAT_DIFF
2238 lp->fill = 0;
2239#endif
2240 if (lp->lnum > curbuf->b_ml.ml_line_count)
2241 lp->height = MAXCOL;
2242 else
2243#ifdef FEAT_FOLDING
2244 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 lp->height = 1;
2247 else
2248#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002249 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251}
2252
2253#ifdef FEAT_DIFF
2254/*
2255 * Switch from including filler lines below lp->lnum to including filler
2256 * lines above loff.lnum + 1. This keeps pointing to the same line.
2257 * When there are no filler lines nothing changes.
2258 */
2259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002260botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 if (lp->fill > 0)
2263 {
2264 ++lp->lnum;
2265 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2266 }
2267}
2268
2269/*
2270 * Switch from including filler lines above lp->lnum to including filler
2271 * lines below loff.lnum - 1. This keeps pointing to the same line.
2272 * When there are no filler lines nothing changes.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 if (lp->fill > 0)
2278 {
2279 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2280 --lp->lnum;
2281 }
2282}
2283#endif
2284
2285/*
2286 * Recompute topline to put the cursor at the top of the window.
2287 * Scroll at least "min_scroll" lines.
2288 * If "always" is TRUE, always set topline (for "zt").
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 int scrolled = 0;
2294 int extra = 0;
2295 int used;
2296 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 linenr_T top; // just above displayed lines
2298 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002300 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#ifdef FEAT_DIFF
2302 linenr_T old_topfill = curwin->w_topfill;
2303#endif
2304 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002305 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (mouse_dragging > 0)
2308 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /*
2311 * Decrease topline until:
2312 * - it has become 1
2313 * - (part of) the cursor line is moved off the screen or
2314 * - moved at least 'scrolljump' lines and
2315 * - at least 'scrolloff' lines above and below the cursor
2316 */
2317 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (curwin->w_cursor.lnum < curwin->w_topline)
2320 scrolled = used;
2321
2322#ifdef FEAT_FOLDING
2323 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2324 {
2325 --top;
2326 ++bot;
2327 }
2328 else
2329#endif
2330 {
2331 top = curwin->w_cursor.lnum - 1;
2332 bot = curwin->w_cursor.lnum + 1;
2333 }
2334 new_topline = top + 1;
2335
2336#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 // "used" already contains the number of filler lines above, don't add it
2338 // again.
2339 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002340 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342
2343 /*
2344 * Check if the lines from "top" to "bot" fit in the window. If they do,
2345 * set new_topline and advance "top" and "bot" to include more lines.
2346 */
2347 while (top > 0)
2348 {
2349#ifdef FEAT_FOLDING
2350 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 i = 1;
2353 else
2354#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002355 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002356 if (top < curwin->w_topline)
2357 scrolled += i;
2358
2359 // If scrolling is needed, scroll at least 'sj' lines.
2360 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2361 && extra >= off)
2362 break;
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 used += i;
2365 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2366 {
2367#ifdef FEAT_FOLDING
2368 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 ++used;
2371 else
2372#endif
2373 used += plines(bot);
2374 }
2375 if (used > curwin->w_height)
2376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 extra += i;
2379 new_topline = top;
2380 --top;
2381 ++bot;
2382 }
2383
2384 /*
2385 * If we don't have enough space, put cursor in the middle.
2386 * This makes sure we get the same position when using "k" and "j"
2387 * in a small window.
2388 */
2389 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002390 scroll_cursor_halfway(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 {
2393 /*
2394 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002395 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 if (new_topline < curwin->w_topline || always)
2398 curwin->w_topline = new_topline;
2399 if (curwin->w_topline > curwin->w_cursor.lnum)
2400 curwin->w_topline = curwin->w_cursor.lnum;
2401#ifdef FEAT_DIFF
2402 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2403 if (curwin->w_topfill > 0 && extra > off)
2404 {
2405 curwin->w_topfill -= extra - off;
2406 if (curwin->w_topfill < 0)
2407 curwin->w_topfill = 0;
2408 }
2409 check_topfill(curwin, FALSE);
2410#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002411 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002412 if (curwin->w_topline == curwin->w_cursor.lnum
2413 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002414 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002416 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#ifdef FEAT_DIFF
2418 || curwin->w_topfill != old_topfill
2419#endif
2420 )
2421 curwin->w_valid &=
2422 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2423 curwin->w_valid |= VALID_TOPLINE;
2424 }
2425}
2426
2427/*
2428 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2429 * screen lines for text lines.
2430 */
2431 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002432set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434#ifdef FEAT_DIFF
2435 wp->w_filler_rows = 0;
2436#endif
2437 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 else
2440 {
2441 wp->w_empty_rows = wp->w_height - used;
2442#ifdef FEAT_DIFF
2443 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2444 {
2445 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2446 if (wp->w_empty_rows > wp->w_filler_rows)
2447 wp->w_empty_rows -= wp->w_filler_rows;
2448 else
2449 {
2450 wp->w_filler_rows = wp->w_empty_rows;
2451 wp->w_empty_rows = 0;
2452 }
2453 }
2454#endif
2455 }
2456}
2457
2458/*
2459 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002460 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2462 * This is messy stuff!!!
2463 */
2464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 int used;
2468 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002469 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 int extra = 0;
2471 int i;
2472 linenr_T line_count;
2473 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002474 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 lineoff_T loff;
2476 lineoff_T boff;
2477#ifdef FEAT_DIFF
2478 int old_topfill = curwin->w_topfill;
2479 int fill_below_window;
2480#endif
2481 linenr_T old_botline = curwin->w_botline;
2482 linenr_T old_valid = curwin->w_valid;
2483 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002484 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002485 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 cln = curwin->w_cursor.lnum;
2488 if (set_topbot)
2489 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002490 int set_skipcol = FALSE;
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 used = 0;
2493 curwin->w_botline = cln + 1;
2494#ifdef FEAT_DIFF
2495 loff.fill = 0;
2496#endif
2497 for (curwin->w_topline = curwin->w_botline;
2498 curwin->w_topline > 1;
2499 curwin->w_topline = loff.lnum)
2500 {
2501 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002502 topline_back_winheight(&loff, FALSE);
2503 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002505 if (used + loff.height > curwin->w_height)
2506 {
2507 if (curwin->w_p_sms && curwin->w_p_wrap)
2508 {
2509 // 'smoothscroll' and 'wrap' are set. The above line is
2510 // too long to show in its entirety, so we show just a part
2511 // of it.
2512 if (used < curwin->w_height)
2513 {
2514 int plines_offset = used + loff.height
2515 - curwin->w_height;
2516 used = curwin->w_height;
2517#ifdef FEAT_DIFF
2518 curwin->w_topfill = loff.fill;
2519#endif
2520 curwin->w_topline = loff.lnum;
2521 curwin->w_skipcol = skipcol_from_plines(
2522 curwin, plines_offset);
2523 set_skipcol = TRUE;
2524 }
2525 }
2526 break;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 used += loff.height;
2529#ifdef FEAT_DIFF
2530 curwin->w_topfill = loff.fill;
2531#endif
2532 }
2533 set_empty_rows(curwin, used);
2534 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2535 if (curwin->w_topline != old_topline
2536#ifdef FEAT_DIFF
2537 || curwin->w_topfill != old_topfill
2538#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002539 || set_skipcol
2540 || curwin->w_skipcol != 0)
2541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002543 if (set_skipcol)
2544 redraw_later(UPD_NOT_VALID);
2545 else
2546 reset_skipcol();
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 validate_botline();
2551
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#ifdef FEAT_DIFF
2554 used = plines_nofill(cln);
2555#else
2556 validate_cheight();
2557 used = curwin->w_cline_height;
2558#endif
2559
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002560 // If the cursor is on or below botline, we will at least scroll by the
2561 // height of the cursor line, which is "used". Correct for empty lines,
2562 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (cln >= curwin->w_botline)
2564 {
2565 scrolled = used;
2566 if (cln == curwin->w_botline)
2567 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002568 min_scrolled = scrolled;
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002569 if (curwin->w_p_sms && curwin->w_p_wrap)
2570 {
2571 // 'smoothscroll' and 'wrap' are set
2572 if (cln > curwin->w_botline)
2573 // add screen lines below w_botline
2574 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2575 min_scrolled += PLINES_NOFILL(lnum);
2576
2577 // Calculate how many screen lines the current top line of window
2578 // occupies. If it is occupying more than the entire window, we
2579 // need to scroll the additional clipped lines to scroll past the
2580 // top line before we can move on to the other lines.
2581 int top_plines =
2582#ifdef FEAT_DIFF
2583 plines_win_nofill
2584#else
2585 plines_win
2586#endif
2587 (curwin, curwin->w_topline, FALSE);
2588 int skip_lines = 0;
2589 int width1 = curwin->w_width - curwin_col_off();
2590 int width2 = width1 + curwin_col_off2();
2591 // similar formula is used in curs_columns()
2592 if (curwin->w_skipcol > width1)
2593 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2594 else if (curwin->w_skipcol > 0)
2595 skip_lines = 1;
2596
2597 top_plines -= skip_lines;
2598 if (top_plines > curwin->w_height)
2599 {
2600 scrolled += (top_plines - curwin->w_height);
2601 min_scrolled += (top_plines - curwin->w_height);
2602 }
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
2606 /*
2607 * Stop counting lines to scroll when
2608 * - hitting start of the file
2609 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002610 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 * - lines between botline and cursor have been counted
2612 */
2613#ifdef FEAT_FOLDING
2614 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2615#endif
2616 {
2617 loff.lnum = cln;
2618 boff.lnum = cln;
2619 }
2620#ifdef FEAT_DIFF
2621 loff.fill = 0;
2622 boff.fill = 0;
2623 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2624 - curwin->w_filler_rows;
2625#endif
2626
2627 while (loff.lnum > 1)
2628 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2630 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002632 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2634 && loff.lnum <= curwin->w_botline
2635#ifdef FEAT_DIFF
2636 && (loff.lnum < curwin->w_botline
2637 || loff.fill >= fill_below_window)
2638#endif
2639 )
2640 break;
2641
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002644 if (loff.height == MAXCOL)
2645 used = MAXCOL;
2646 else
2647 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (used > curwin->w_height)
2649 break;
2650 if (loff.lnum >= curwin->w_botline
2651#ifdef FEAT_DIFF
2652 && (loff.lnum > curwin->w_botline
2653 || loff.fill <= fill_below_window)
2654#endif
2655 )
2656 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 scrolled += loff.height;
2659 if (loff.lnum == curwin->w_botline
2660#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002661 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663 )
2664 scrolled -= curwin->w_empty_rows;
2665 }
2666
2667 if (boff.lnum < curbuf->b_ml.ml_line_count)
2668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 botline_forw(&boff);
2671 used += boff.height;
2672 if (used > curwin->w_height)
2673 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002674 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2675 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 extra += boff.height;
2678 if (boff.lnum >= curwin->w_botline
2679#ifdef FEAT_DIFF
2680 || (boff.lnum + 1 == curwin->w_botline
2681 && boff.fill > curwin->w_filler_rows)
2682#endif
2683 )
2684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 scrolled += boff.height;
2687 if (boff.lnum == curwin->w_botline
2688#ifdef FEAT_DIFF
2689 && boff.fill == 0
2690#endif
2691 )
2692 scrolled -= curwin->w_empty_rows;
2693 }
2694 }
2695 }
2696 }
2697
Bram Moolenaar85a20022019-12-21 18:25:54 +01002698 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (scrolled <= 0)
2700 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 else if (used > curwin->w_height)
2703 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002704 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
2706 {
2707 line_count = 0;
2708#ifdef FEAT_DIFF
2709 boff.fill = curwin->w_topfill;
2710#endif
2711 boff.lnum = curwin->w_topline - 1;
2712 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2713 {
2714 botline_forw(&boff);
2715 i += boff.height;
2716 ++line_count;
2717 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 line_count = 9999;
2720 }
2721
2722 /*
2723 * Scroll up if the cursor is off the bottom of the screen a bit.
2724 * Otherwise put it at 1/2 of the screen.
2725 */
2726 if (line_count >= curwin->w_height && line_count > min_scroll)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002727 scroll_cursor_halfway(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002729 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002730 // With 'smoothscroll' scroll at least the height of the cursor line,
2731 // unless it would move the cursor.
2732 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2733 && (curwin->w_cursor.lnum < curwin->w_topline
2734 || (curwin->w_virtcol - curwin->w_skipcol >=
2735 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002736 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002737 if (line_count > 0)
2738 {
2739 if (scrolling_screenlines(TRUE))
2740 scrollup(scrolled, TRUE); // TODO
2741 else
2742 scrollup(line_count, TRUE);
2743 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
2747 * If topline didn't change we need to restore w_botline and w_empty_rows
2748 * (we changed them).
2749 * If topline did change, update_screen() will set botline.
2750 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002751 if (curwin->w_topline == old_topline
2752 && curwin->w_skipcol == old_skipcol
2753 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
2755 curwin->w_botline = old_botline;
2756 curwin->w_empty_rows = old_empty_rows;
2757 curwin->w_valid = old_valid;
2758 }
2759 curwin->w_valid |= VALID_TOPLINE;
2760}
2761
2762/*
2763 * Recompute topline to put the cursor halfway the window
2764 * If "atend" is TRUE, also put it halfway at the end of the file.
2765 */
2766 void
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002767scroll_cursor_halfway(int atend, int prefer_above)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 int above = 0;
2770 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002771 colnr_T skipcol = 0;
2772 int set_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#ifdef FEAT_DIFF
2774 int topfill = 0;
2775#endif
2776 int below = 0;
2777 int used;
2778 lineoff_T loff;
2779 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002780#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002781 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002784#ifdef FEAT_PROP_POPUP
2785 // if the width changed this needs to be updated first
2786 may_update_popup_position();
2787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2789#ifdef FEAT_FOLDING
2790 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2791#endif
2792#ifdef FEAT_DIFF
2793 used = plines_nofill(loff.lnum);
2794 loff.fill = 0;
2795 boff.fill = 0;
2796#else
2797 used = plines(loff.lnum);
2798#endif
2799 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002800
2801 int half_height = 0;
2802 int smooth_scroll = FALSE;
2803 if (curwin->w_p_sms && curwin->w_p_wrap)
2804 {
2805 // 'smoothscroll' and 'wrap' are set
2806 smooth_scroll = TRUE;
2807 half_height = (curwin->w_height - used) / 2;
2808 used = 0;
2809 }
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 while (topline > 1)
2812 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002813 // If using smoothscroll, we can precisely scroll to the
2814 // exact point where the cursor is halfway down the screen.
2815 if (smooth_scroll)
2816 {
2817 topline_back_winheight(&loff, FALSE);
2818 if (loff.height == MAXCOL)
2819 break;
2820 else
2821 used += loff.height;
2822 if (used > half_height)
2823 {
2824 if (used - loff.height < half_height)
2825 {
2826 int plines_offset = used - half_height;
2827 loff.height -= plines_offset;
2828 used = half_height;
2829
2830 topline = loff.lnum;
2831#ifdef FEAT_DIFF
2832 topfill = loff.fill;
2833#endif
2834 skipcol = skipcol_from_plines(curwin, plines_offset);
2835 set_skipcol = TRUE;
2836 }
2837 break;
2838 }
2839 topline = loff.lnum;
2840#ifdef FEAT_DIFF
2841 topfill = loff.fill;
2842#endif
2843 continue;
2844 }
2845
2846 // If not using smoothscroll, we have to iteratively find how many
2847 // lines to scroll down to roughly fit the cursor.
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002848 // This may not be right in the middle if the lines'
2849 // physical height > 1 (e.g. 'wrap' is on).
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002850
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002851 // Depending on "prefer_above" we add a line above or below first.
2852 // Loop twice to avoid duplicating code.
2853 int done = FALSE;
2854 for (int round = 1; round <= 2; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002856 if (prefer_above ? (round == 2 && below < above)
2857 : (round == 1 && below <= above))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002859 // add a line below the cursor
2860 if (boff.lnum < curbuf->b_ml.ml_line_count)
2861 {
2862 botline_forw(&boff);
2863 used += boff.height;
2864 if (used > curwin->w_height)
2865 {
2866 done = TRUE;
2867 break;
2868 }
2869 below += boff.height;
2870 }
2871 else
2872 {
2873 ++below; // count a "~" line
2874 if (atend)
2875 ++used;
2876 }
2877 }
2878
2879 if (prefer_above ? (round == 1 && below >= above)
2880 : (round == 1 && below > above))
2881 {
2882 // add a line above the cursor
2883 topline_back(&loff);
2884 if (loff.height == MAXCOL)
2885 used = MAXCOL;
2886 else
2887 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (used > curwin->w_height)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002889 {
2890 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002892 }
2893 above += loff.height;
2894 topline = loff.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#ifdef FEAT_DIFF
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002896 topfill = loff.fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002900 if (done)
2901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#ifdef FEAT_FOLDING
2905 if (!hasFolding(topline, &curwin->w_topline, NULL))
2906#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002907 {
2908 if (curwin->w_topline != topline
2909 || set_skipcol
2910 || curwin->w_skipcol != 0)
2911 {
2912 curwin->w_topline = topline;
2913 if (set_skipcol)
2914 {
2915 curwin->w_skipcol = skipcol;
2916 redraw_later(UPD_NOT_VALID);
2917 }
2918 else
2919 reset_skipcol();
2920 }
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#ifdef FEAT_DIFF
2923 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002924 if (old_topline > curwin->w_topline + curwin->w_height)
2925 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 check_topfill(curwin, FALSE);
2927#endif
2928 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2929 curwin->w_valid |= VALID_TOPLINE;
2930}
2931
2932/*
2933 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002934 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * If not possible, put it at the same position as scroll_cursor_halfway().
2936 * When called topline must be valid!
2937 */
2938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002939cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002941 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002943 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 linenr_T botline;
2945 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002948 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /*
2951 * How many lines we would like to have above/below the cursor depends on
2952 * whether the first/last line of the file is on screen.
2953 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002954 above_wanted = so;
2955 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002956 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
2958 above_wanted = mouse_dragging - 1;
2959 below_wanted = mouse_dragging - 1;
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (curwin->w_topline == 1)
2962 {
2963 above_wanted = 0;
2964 max_off = curwin->w_height / 2;
2965 if (below_wanted > max_off)
2966 below_wanted = max_off;
2967 }
2968 validate_botline();
2969 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002970 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 below_wanted = 0;
2973 max_off = (curwin->w_height - 1) / 2;
2974 if (above_wanted > max_off)
2975 above_wanted = max_off;
2976 }
2977
2978 /*
2979 * If there are sufficient file-lines above and below the cursor, we can
2980 * return now.
2981 */
2982 cln = curwin->w_cursor.lnum;
2983 if (cln >= curwin->w_topline + above_wanted
2984 && cln < curwin->w_botline - below_wanted
2985#ifdef FEAT_FOLDING
2986 && !hasAnyFolding(curwin)
2987#endif
2988 )
2989 return;
2990
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002991 if (curwin->w_p_sms && !curwin->w_p_wrap)
2992 {
2993 // 'smoothscroll is active
2994 if (curwin->w_cline_height == curwin->w_height)
2995 {
2996 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002997 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002998 return;
2999 }
3000 // TODO: If the cursor line doesn't fit in the window then only adjust
3001 // w_skipcol.
3002 }
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /*
3005 * Narrow down the area where the cursor can be put by taking lines from
3006 * the top and the bottom until:
3007 * - the desired context lines are found
3008 * - the lines from the top is past the lines from the bottom
3009 */
3010 topline = curwin->w_topline;
3011 botline = curwin->w_botline - 1;
3012#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003013 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 above = curwin->w_topfill;
3015 below = curwin->w_filler_rows;
3016#endif
3017 while ((above < above_wanted || below < below_wanted) && topline < botline)
3018 {
3019 if (below < below_wanted && (below <= above || above >= above_wanted))
3020 {
3021#ifdef FEAT_FOLDING
3022 if (hasFolding(botline, &botline, NULL))
3023 ++below;
3024 else
3025#endif
3026 below += plines(botline);
3027 --botline;
3028 }
3029 if (above < above_wanted && (above < below || below >= below_wanted))
3030 {
3031#ifdef FEAT_FOLDING
3032 if (hasFolding(topline, NULL, &topline))
3033 ++above;
3034 else
3035#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003036 above += PLINES_NOFILL(topline);
3037#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003038 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (topline < botline)
3040 above += diff_check_fill(curwin, topline + 1);
3041#endif
3042 ++topline;
3043 }
3044 }
3045 if (topline == botline || botline == 0)
3046 curwin->w_cursor.lnum = topline;
3047 else if (topline > botline)
3048 curwin->w_cursor.lnum = botline;
3049 else
3050 {
3051 if (cln < topline && curwin->w_topline > 1)
3052 {
3053 curwin->w_cursor.lnum = topline;
3054 curwin->w_valid &=
3055 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3056 }
3057 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3058 {
3059 curwin->w_cursor.lnum = botline;
3060 curwin->w_valid &=
3061 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3062 }
3063 }
3064 curwin->w_valid |= VALID_TOPLINE;
3065}
3066
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003067static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003070 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3071 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003073 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
3075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 long n;
3079 int retval = OK;
3080 lineoff_T loff;
3081 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003082 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaar85a20022019-12-21 18:25:54 +01003084 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 beep_flush();
3087 return FAIL;
3088 }
3089
3090 for ( ; count > 0; --count)
3091 {
3092 validate_botline();
3093 /*
3094 * It's an error to move a page up when the first line is already on
3095 * the screen. It's an error to move a page down when the last line
3096 * is on the screen and the topline is 'scrolloff' lines from the
3097 * last line.
3098 */
3099 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003100 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3102 : (curwin->w_topline == 1
3103#ifdef FEAT_DIFF
3104 && curwin->w_topfill ==
3105 diff_check_fill(curwin, curwin->w_topline)
3106#endif
3107 ))
3108 {
3109 beep_flush();
3110 retval = FAIL;
3111 break;
3112 }
3113
3114#ifdef FEAT_DIFF
3115 loff.fill = 0;
3116#endif
3117 if (dir == FORWARD)
3118 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003119 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003121 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003122 if (p_window <= 2)
3123 ++curwin->w_topline;
3124 else
3125 curwin->w_topline += p_window - 2;
3126 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3127 curwin->w_topline = curbuf->b_ml.ml_line_count;
3128 curwin->w_cursor.lnum = curwin->w_topline;
3129 }
3130 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003132 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 curwin->w_topline = curbuf->b_ml.ml_line_count;
3134#ifdef FEAT_DIFF
3135 curwin->w_topfill = 0;
3136#endif
3137 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3138 }
3139 else
3140 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // For the overlap, start with the line just below the window
3142 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 loff.lnum = curwin->w_botline;
3144#ifdef FEAT_DIFF
3145 loff.fill = diff_check_fill(curwin, loff.lnum)
3146 - curwin->w_filler_rows;
3147#endif
3148 get_scroll_overlap(&loff, -1);
3149 curwin->w_topline = loff.lnum;
3150#ifdef FEAT_DIFF
3151 curwin->w_topfill = loff.fill;
3152 check_topfill(curwin, FALSE);
3153#endif
3154 curwin->w_cursor.lnum = curwin->w_topline;
3155 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3156 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3157 }
3158 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161#ifdef FEAT_DIFF
3162 if (curwin->w_topline == 1)
3163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 max_topfill();
3166 continue;
3167 }
3168#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003169 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003172 if (p_window <= 2)
3173 --curwin->w_topline;
3174 else
3175 curwin->w_topline -= p_window - 2;
3176 if (curwin->w_topline < 1)
3177 curwin->w_topline = 1;
3178 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3179 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3180 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3181 continue;
3182 }
3183
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 // Find the line at the top of the window that is going to be the
3185 // line at the bottom of the window. Make sure this results in
3186 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 loff.lnum = curwin->w_topline - 1;
3188#ifdef FEAT_DIFF
3189 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3190 - curwin->w_topfill;
3191#endif
3192 get_scroll_overlap(&loff, 1);
3193
3194 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3195 {
3196 loff.lnum = curbuf->b_ml.ml_line_count;
3197#ifdef FEAT_DIFF
3198 loff.fill = 0;
3199 }
3200 else
3201 {
3202 botline_topline(&loff);
3203#endif
3204 }
3205 curwin->w_cursor.lnum = loff.lnum;
3206
Bram Moolenaar85a20022019-12-21 18:25:54 +01003207 // Find the line just above the new topline to get the right line
3208 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 n = 0;
3210 while (n <= curwin->w_height && loff.lnum >= 1)
3211 {
3212 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003213 if (loff.height == MAXCOL)
3214 n = MAXCOL;
3215 else
3216 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 curwin->w_topline = 1;
3221#ifdef FEAT_DIFF
3222 max_topfill();
3223#endif
3224 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3225 }
3226 else
3227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003228 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#ifdef FEAT_DIFF
3230 topline_botline(&loff);
3231#endif
3232 botline_forw(&loff);
3233 botline_forw(&loff);
3234#ifdef FEAT_DIFF
3235 botline_topline(&loff);
3236#endif
3237#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3240#endif
3241
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 // Always scroll at least one line. Avoid getting stuck on
3243 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (loff.lnum >= curwin->w_topline
3245#ifdef FEAT_DIFF
3246 && (loff.lnum > curwin->w_topline
3247 || loff.fill >= curwin->w_topfill)
3248#endif
3249 )
3250 {
3251#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003252 // First try using the maximum number of filler lines. If
3253 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 loff.fill = curwin->w_topfill;
3255 if (curwin->w_topfill < diff_check_fill(curwin,
3256 curwin->w_topline))
3257 max_topfill();
3258 if (curwin->w_topfill == loff.fill)
3259#endif
3260 {
3261 --curwin->w_topline;
3262#ifdef FEAT_DIFF
3263 curwin->w_topfill = 0;
3264#endif
3265 }
3266 comp_botline(curwin);
3267 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003268 curwin->w_valid &=
3269 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 else
3272 {
3273 curwin->w_topline = loff.lnum;
3274#ifdef FEAT_DIFF
3275 curwin->w_topfill = loff.fill;
3276 check_topfill(curwin, FALSE);
3277#endif
3278 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3279 }
3280 }
3281 }
3282 }
3283#ifdef FEAT_FOLDING
3284 foldAdjustCursor();
3285#endif
3286 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003287 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003288 if (retval == OK)
3289 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3291
Bram Moolenaar907dad72018-07-10 15:07:15 +02003292 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003294 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3295 // But make sure we scroll at least one line (happens with mix of long
3296 // wrapping lines and non-wrapping line).
3297 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003299 scroll_cursor_top(1, FALSE);
3300 if (curwin->w_topline <= old_topline
3301 && old_topline < curbuf->b_ml.ml_line_count)
3302 {
3303 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003305 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3306#endif
3307 }
3308 }
3309#ifdef FEAT_FOLDING
3310 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003315 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 return retval;
3317}
3318
3319/*
3320 * Decide how much overlap to use for page-up or page-down scrolling.
3321 * This is symmetric, so that doing both keeps the same lines displayed.
3322 * Three lines are examined:
3323 *
3324 * before CTRL-F after CTRL-F / before CTRL-B
3325 * etc. l1
3326 * l1 last but one line ------------
3327 * l2 last text line l2 top text line
3328 * ------------- l3 second text line
3329 * l3 etc.
3330 */
3331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 int h1, h2, h3, h4;
3335 int min_height = curwin->w_height - 2;
3336 lineoff_T loff0, loff1, loff2;
3337
3338#ifdef FEAT_DIFF
3339 if (lp->fill > 0)
3340 lp->height = 1;
3341 else
3342 lp->height = plines_nofill(lp->lnum);
3343#else
3344 lp->height = plines(lp->lnum);
3345#endif
3346 h1 = lp->height;
3347 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003348 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 loff0 = *lp;
3351 if (dir > 0)
3352 botline_forw(lp);
3353 else
3354 topline_back(lp);
3355 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003356 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return;
3360 }
3361
3362 loff1 = *lp;
3363 if (dir > 0)
3364 botline_forw(lp);
3365 else
3366 topline_back(lp);
3367 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003368 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003370 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return;
3372 }
3373
3374 loff2 = *lp;
3375 if (dir > 0)
3376 botline_forw(lp);
3377 else
3378 topline_back(lp);
3379 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003380 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003381 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003383 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386/*
3387 * Scroll 'scroll' lines up or down.
3388 */
3389 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003390halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 long scrolled = 0;
3393 int i;
3394 int n;
3395 int room;
3396
3397 if (Prenum)
3398 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3399 curwin->w_height : Prenum;
3400 n = (curwin->w_p_scr <= curwin->w_height) ?
3401 curwin->w_p_scr : curwin->w_height;
3402
Bram Moolenaard5d37532017-03-27 23:02:07 +02003403 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 validate_botline();
3405 room = curwin->w_empty_rows;
3406#ifdef FEAT_DIFF
3407 room += curwin->w_filler_rows;
3408#endif
3409 if (flag)
3410 {
3411 /*
3412 * scroll the text up
3413 */
3414 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3415 {
3416#ifdef FEAT_DIFF
3417 if (curwin->w_topfill > 0)
3418 {
3419 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003420 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 --curwin->w_topfill;
3422 }
3423 else
3424#endif
3425 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003426 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 n -= i;
3428 if (n < 0 && scrolled > 0)
3429 break;
3430#ifdef FEAT_FOLDING
3431 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3432#endif
3433 ++curwin->w_topline;
3434#ifdef FEAT_DIFF
3435 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3436#endif
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3439 {
3440 ++curwin->w_cursor.lnum;
3441 curwin->w_valid &=
3442 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3446 scrolled += i;
3447
3448 /*
3449 * Correct w_botline for changed w_topline.
3450 * Won't work when there are filler lines.
3451 */
3452#ifdef FEAT_DIFF
3453 if (curwin->w_p_diff)
3454 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3455 else
3456#endif
3457 {
3458 room += i;
3459 do
3460 {
3461 i = plines(curwin->w_botline);
3462 if (i > room)
3463 break;
3464#ifdef FEAT_FOLDING
3465 (void)hasFolding(curwin->w_botline, NULL,
3466 &curwin->w_botline);
3467#endif
3468 ++curwin->w_botline;
3469 room -= i;
3470 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3471 }
3472 }
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /*
3475 * When hit bottom of the file: move cursor down.
3476 */
3477 if (n > 0)
3478 {
3479# ifdef FEAT_FOLDING
3480 if (hasAnyFolding(curwin))
3481 {
3482 while (--n >= 0
3483 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3484 {
3485 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3486 &curwin->w_cursor.lnum);
3487 ++curwin->w_cursor.lnum;
3488 }
3489 }
3490 else
3491# endif
3492 curwin->w_cursor.lnum += n;
3493 check_cursor_lnum();
3494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 else
3497 {
3498 /*
3499 * scroll the text down
3500 */
3501 while (n > 0 && curwin->w_topline > 1)
3502 {
3503#ifdef FEAT_DIFF
3504 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3505 {
3506 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003507 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 ++curwin->w_topfill;
3509 }
3510 else
3511#endif
3512 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003513 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 n -= i;
3515 if (n < 0 && scrolled > 0)
3516 break;
3517 --curwin->w_topline;
3518#ifdef FEAT_FOLDING
3519 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3520#endif
3521#ifdef FEAT_DIFF
3522 curwin->w_topfill = 0;
3523#endif
3524 }
3525 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3526 VALID_BOTLINE|VALID_BOTLINE_AP);
3527 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (curwin->w_cursor.lnum > 1)
3529 {
3530 --curwin->w_cursor.lnum;
3531 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /*
3536 * When hit top of the file: move cursor up.
3537 */
3538 if (n > 0)
3539 {
3540 if (curwin->w_cursor.lnum <= (linenr_T)n)
3541 curwin->w_cursor.lnum = 1;
3542 else
3543# ifdef FEAT_FOLDING
3544 if (hasAnyFolding(curwin))
3545 {
3546 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3547 {
3548 --curwin->w_cursor.lnum;
3549 (void)hasFolding(curwin->w_cursor.lnum,
3550 &curwin->w_cursor.lnum, NULL);
3551 }
3552 }
3553 else
3554# endif
3555 curwin->w_cursor.lnum -= n;
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003559 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 foldAdjustCursor();
3561# endif
3562#ifdef FEAT_DIFF
3563 check_topfill(curwin, !flag);
3564#endif
3565 cursor_correct();
3566 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003567 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569
Bram Moolenaar860cae12010-06-05 23:22:07 +02003570 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003571do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572{
3573 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003574 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003575 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003576 colnr_T curswant = curwin->w_curswant;
3577 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003578 win_T *old_curwin = curwin;
3579 buf_T *old_curbuf = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003580 int old_VIsual_select = VIsual_select;
3581 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003582
3583 /*
3584 * loop through the cursorbound windows
3585 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003587 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003588 {
3589 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003590 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003591 if (curwin != old_curwin && curwin->w_p_crb)
3592 {
3593# ifdef FEAT_DIFF
3594 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003595 curwin->w_cursor.lnum =
3596 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 else
3598# endif
3599 curwin->w_cursor.lnum = line;
3600 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003601 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003602 curwin->w_curswant = curswant;
3603 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003604
Bram Moolenaar85a20022019-12-21 18:25:54 +01003605 // Make sure the cursor is in a valid position. Temporarily set
3606 // "restart_edit" to allow the cursor to be beyond the EOL.
Dominique Pellee764d1b2023-03-12 21:20:59 +00003607 int restart_edit_save = restart_edit;
3608 restart_edit = 'a';
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003610
3611 // Avoid a scroll here for the cursor position, 'scrollbind' is
3612 // more important.
3613 if (!curwin->w_p_scb)
3614 validate_cursor();
3615
Bram Moolenaar61452852011-02-01 18:01:11 +01003616 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003617 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003618 if (has_mbyte)
3619 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003620 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003621
Bram Moolenaar85a20022019-12-21 18:25:54 +01003622 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003623 if (!curwin->w_p_scb)
3624 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 }
3627 }
3628
3629 /*
3630 * reset current-window
3631 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003632 VIsual_select = old_VIsual_select;
3633 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003634 curwin = old_curwin;
3635 curbuf = old_curbuf;
3636}